blob: 2103f141235990aef385df2c0f0f4b1f0df4ab1e [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>
Rob Swindell5ac67d82016-09-19 03:58:03 -040036#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050037#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040038#include <net/ip.h>
39#include <net/tcp.h>
40#include <net/udp.h>
41#include <net/checksum.h>
42#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070043#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040044#include <linux/workqueue.h>
45#include <linux/prefetch.h>
46#include <linux/cache.h>
47#include <linux/log2.h>
48#include <linux/aer.h>
49#include <linux/bitmap.h>
50#include <linux/cpu_rmap.h>
51
52#include "bnxt_hsi.h"
53#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050054#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040055#include "bnxt_sriov.h"
56#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050057#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050058#include "bnxt_xdp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040059
60#define BNXT_TX_TIMEOUT (5 * HZ)
61
62static const char version[] =
63 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
64
65MODULE_LICENSE("GPL");
66MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
67MODULE_VERSION(DRV_MODULE_VERSION);
68
69#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
70#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
71#define BNXT_RX_COPY_THRESH 256
72
Michael Chan4419dbe2016-02-10 17:33:49 -050073#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040074
75enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050076 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040077 BCM57302,
78 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040079 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040080 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040081 BCM57311,
82 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050083 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040084 BCM57404,
85 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040086 BCM57402_NPAR,
87 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040088 BCM57412,
89 BCM57414,
90 BCM57416,
91 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040093 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040094 BCM57417_SFP,
95 BCM57416_SFP,
96 BCM57404_NPAR,
97 BCM57406_NPAR,
98 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040099 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400100 BCM57414_NPAR,
101 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500102 BCM57452,
103 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400104 NETXTREME_E_VF,
105 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400106};
107
108/* indexed by enum above */
109static const struct {
110 char *name;
111} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400112 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400115 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400116 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
117 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
118 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
119 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400122 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400123 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
124 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
125 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
127 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400128 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
130 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
131 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400132 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400134 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
135 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400136 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
137 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500138 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400140 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
141 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400142};
143
144static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400145 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
148 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400150 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
152 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500153 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400154 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
155 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400156 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
157 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400158 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
159 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
160 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
161 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400162 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400163 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
165 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
166 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
167 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400172 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500175 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
176 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400177#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400178 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
179 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400180 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
182 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400186#endif
187 { 0 }
188};
189
190MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
191
192static const u16 bnxt_vf_req_snif[] = {
193 HWRM_FUNC_CFG,
194 HWRM_PORT_PHY_QCFG,
195 HWRM_CFA_L2_FILTER_ALLOC,
196};
197
Michael Chan25be8622016-04-05 14:09:00 -0400198static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500199 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
200 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
201 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
202 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
203 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400204};
205
Michael Chanc0c050c2015-10-22 16:01:17 -0400206static bool bnxt_vf_pciid(enum board_idx idx)
207{
Michael Chanadbc8302016-09-19 03:58:01 -0400208 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400209}
210
211#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
212#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
213#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
214
215#define BNXT_CP_DB_REARM(db, raw_cons) \
216 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
217
218#define BNXT_CP_DB(db, raw_cons) \
219 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
220
221#define BNXT_CP_DB_IRQ_DIS(db) \
222 writel(DB_CP_IRQ_DIS_FLAGS, db)
223
Michael Chan38413402017-02-06 16:55:43 -0500224const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400225 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
226 TX_BD_FLAGS_LHINT_512_TO_1023,
227 TX_BD_FLAGS_LHINT_1024_TO_2047,
228 TX_BD_FLAGS_LHINT_1024_TO_2047,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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};
245
246static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
247{
248 struct bnxt *bp = netdev_priv(dev);
249 struct tx_bd *txbd;
250 struct tx_bd_ext *txbd1;
251 struct netdev_queue *txq;
252 int i;
253 dma_addr_t mapping;
254 unsigned int length, pad = 0;
255 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
256 u16 prod, last_frag;
257 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400258 struct bnxt_tx_ring_info *txr;
259 struct bnxt_sw_tx_bd *tx_buf;
260
261 i = skb_get_queue_mapping(skb);
262 if (unlikely(i >= bp->tx_nr_rings)) {
263 dev_kfree_skb_any(skb);
264 return NETDEV_TX_OK;
265 }
266
Michael Chanc0c050c2015-10-22 16:01:17 -0400267 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500268 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400269 prod = txr->tx_prod;
270
271 free_size = bnxt_tx_avail(bp, txr);
272 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
273 netif_tx_stop_queue(txq);
274 return NETDEV_TX_BUSY;
275 }
276
277 length = skb->len;
278 len = skb_headlen(skb);
279 last_frag = skb_shinfo(skb)->nr_frags;
280
281 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
282
283 txbd->tx_bd_opaque = prod;
284
285 tx_buf = &txr->tx_buf_ring[prod];
286 tx_buf->skb = skb;
287 tx_buf->nr_frags = last_frag;
288
289 vlan_tag_flags = 0;
290 cfa_action = 0;
291 if (skb_vlan_tag_present(skb)) {
292 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
293 skb_vlan_tag_get(skb);
294 /* Currently supports 8021Q, 8021AD vlan offloads
295 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
296 */
297 if (skb->vlan_proto == htons(ETH_P_8021Q))
298 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
299 }
300
301 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500302 struct tx_push_buffer *tx_push_buf = txr->tx_push;
303 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
304 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
305 void *pdata = tx_push_buf->data;
306 u64 *end;
307 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400308
309 /* Set COAL_NOW to be ready quickly for the next push */
310 tx_push->tx_bd_len_flags_type =
311 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
312 TX_BD_TYPE_LONG_TX_BD |
313 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
314 TX_BD_FLAGS_COAL_NOW |
315 TX_BD_FLAGS_PACKET_END |
316 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
317
318 if (skb->ip_summed == CHECKSUM_PARTIAL)
319 tx_push1->tx_bd_hsize_lflags =
320 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
321 else
322 tx_push1->tx_bd_hsize_lflags = 0;
323
324 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
325 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
326
Michael Chanfbb0fa82016-02-22 02:10:26 -0500327 end = pdata + length;
328 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500329 *end = 0;
330
Michael Chanc0c050c2015-10-22 16:01:17 -0400331 skb_copy_from_linear_data(skb, pdata, len);
332 pdata += len;
333 for (j = 0; j < last_frag; j++) {
334 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
335 void *fptr;
336
337 fptr = skb_frag_address_safe(frag);
338 if (!fptr)
339 goto normal_tx;
340
341 memcpy(pdata, fptr, skb_frag_size(frag));
342 pdata += skb_frag_size(frag);
343 }
344
Michael Chan4419dbe2016-02-10 17:33:49 -0500345 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
346 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400347 prod = NEXT_TX(prod);
348 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
349 memcpy(txbd, tx_push1, sizeof(*txbd));
350 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500351 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400352 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
353 txr->tx_prod = prod;
354
Michael Chanb9a84602016-06-06 02:37:14 -0400355 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400356 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400357 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400358
Michael Chan4419dbe2016-02-10 17:33:49 -0500359 push_len = (length + sizeof(*tx_push) + 7) / 8;
360 if (push_len > 16) {
361 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400362 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
363 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500364 } else {
365 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
366 push_len);
367 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400368
Michael Chanc0c050c2015-10-22 16:01:17 -0400369 goto tx_done;
370 }
371
372normal_tx:
373 if (length < BNXT_MIN_PKT_SIZE) {
374 pad = BNXT_MIN_PKT_SIZE - length;
375 if (skb_pad(skb, pad)) {
376 /* SKB already freed. */
377 tx_buf->skb = NULL;
378 return NETDEV_TX_OK;
379 }
380 length = BNXT_MIN_PKT_SIZE;
381 }
382
383 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
384
385 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
386 dev_kfree_skb_any(skb);
387 tx_buf->skb = NULL;
388 return NETDEV_TX_OK;
389 }
390
391 dma_unmap_addr_set(tx_buf, mapping, mapping);
392 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
393 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
394
395 txbd->tx_bd_haddr = cpu_to_le64(mapping);
396
397 prod = NEXT_TX(prod);
398 txbd1 = (struct tx_bd_ext *)
399 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
400
401 txbd1->tx_bd_hsize_lflags = 0;
402 if (skb_is_gso(skb)) {
403 u32 hdr_len;
404
405 if (skb->encapsulation)
406 hdr_len = skb_inner_network_offset(skb) +
407 skb_inner_network_header_len(skb) +
408 inner_tcp_hdrlen(skb);
409 else
410 hdr_len = skb_transport_offset(skb) +
411 tcp_hdrlen(skb);
412
413 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
414 TX_BD_FLAGS_T_IPID |
415 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
416 length = skb_shinfo(skb)->gso_size;
417 txbd1->tx_bd_mss = cpu_to_le32(length);
418 length += hdr_len;
419 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 txbd1->tx_bd_hsize_lflags =
421 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
422 txbd1->tx_bd_mss = 0;
423 }
424
425 length >>= 9;
426 flags |= bnxt_lhint_arr[length];
427 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
428
429 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
430 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
431 for (i = 0; i < last_frag; i++) {
432 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
433
434 prod = NEXT_TX(prod);
435 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
436
437 len = skb_frag_size(frag);
438 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
439 DMA_TO_DEVICE);
440
441 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
442 goto tx_dma_error;
443
444 tx_buf = &txr->tx_buf_ring[prod];
445 dma_unmap_addr_set(tx_buf, mapping, mapping);
446
447 txbd->tx_bd_haddr = cpu_to_le64(mapping);
448
449 flags = len << TX_BD_LEN_SHIFT;
450 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
451 }
452
453 flags &= ~TX_BD_LEN;
454 txbd->tx_bd_len_flags_type =
455 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
456 TX_BD_FLAGS_PACKET_END);
457
458 netdev_tx_sent_queue(txq, skb->len);
459
460 /* Sync BD data before updating doorbell */
461 wmb();
462
463 prod = NEXT_TX(prod);
464 txr->tx_prod = prod;
465
Michael Chanffe40642017-05-30 20:03:00 -0400466 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400467 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400468
469tx_done:
470
471 mmiowb();
472
473 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400474 if (skb->xmit_more && !tx_buf->is_push)
475 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
476
Michael Chanc0c050c2015-10-22 16:01:17 -0400477 netif_tx_stop_queue(txq);
478
479 /* netif_tx_stop_queue() must be done before checking
480 * tx index in bnxt_tx_avail() below, because in
481 * bnxt_tx_int(), we update tx index before checking for
482 * netif_tx_queue_stopped().
483 */
484 smp_mb();
485 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
486 netif_tx_wake_queue(txq);
487 }
488 return NETDEV_TX_OK;
489
490tx_dma_error:
491 last_frag = i;
492
493 /* start back at beginning and unmap skb */
494 prod = txr->tx_prod;
495 tx_buf = &txr->tx_buf_ring[prod];
496 tx_buf->skb = NULL;
497 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
498 skb_headlen(skb), PCI_DMA_TODEVICE);
499 prod = NEXT_TX(prod);
500
501 /* unmap remaining mapped pages */
502 for (i = 0; i < last_frag; i++) {
503 prod = NEXT_TX(prod);
504 tx_buf = &txr->tx_buf_ring[prod];
505 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
506 skb_frag_size(&skb_shinfo(skb)->frags[i]),
507 PCI_DMA_TODEVICE);
508 }
509
510 dev_kfree_skb_any(skb);
511 return NETDEV_TX_OK;
512}
513
514static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
515{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500516 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500517 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400518 u16 cons = txr->tx_cons;
519 struct pci_dev *pdev = bp->pdev;
520 int i;
521 unsigned int tx_bytes = 0;
522
523 for (i = 0; i < nr_pkts; i++) {
524 struct bnxt_sw_tx_bd *tx_buf;
525 struct sk_buff *skb;
526 int j, last;
527
528 tx_buf = &txr->tx_buf_ring[cons];
529 cons = NEXT_TX(cons);
530 skb = tx_buf->skb;
531 tx_buf->skb = NULL;
532
533 if (tx_buf->is_push) {
534 tx_buf->is_push = 0;
535 goto next_tx_int;
536 }
537
538 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
539 skb_headlen(skb), PCI_DMA_TODEVICE);
540 last = tx_buf->nr_frags;
541
542 for (j = 0; j < last; j++) {
543 cons = NEXT_TX(cons);
544 tx_buf = &txr->tx_buf_ring[cons];
545 dma_unmap_page(
546 &pdev->dev,
547 dma_unmap_addr(tx_buf, mapping),
548 skb_frag_size(&skb_shinfo(skb)->frags[j]),
549 PCI_DMA_TODEVICE);
550 }
551
552next_tx_int:
553 cons = NEXT_TX(cons);
554
555 tx_bytes += skb->len;
556 dev_kfree_skb_any(skb);
557 }
558
559 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
560 txr->tx_cons = cons;
561
562 /* Need to make the tx_cons update visible to bnxt_start_xmit()
563 * before checking for netif_tx_queue_stopped(). Without the
564 * memory barrier, there is a small possibility that bnxt_start_xmit()
565 * will miss it and cause the queue to be stopped forever.
566 */
567 smp_mb();
568
569 if (unlikely(netif_tx_queue_stopped(txq)) &&
570 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
571 __netif_tx_lock(txq, smp_processor_id());
572 if (netif_tx_queue_stopped(txq) &&
573 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
574 txr->dev_state != BNXT_DEV_STATE_CLOSING)
575 netif_tx_wake_queue(txq);
576 __netif_tx_unlock(txq);
577 }
578}
579
Michael Chanc61fb992017-02-06 16:55:36 -0500580static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
581 gfp_t gfp)
582{
583 struct device *dev = &bp->pdev->dev;
584 struct page *page;
585
586 page = alloc_page(gfp);
587 if (!page)
588 return NULL;
589
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700590 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
591 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500592 if (dma_mapping_error(dev, *mapping)) {
593 __free_page(page);
594 return NULL;
595 }
596 *mapping += bp->rx_dma_offset;
597 return page;
598}
599
Michael Chanc0c050c2015-10-22 16:01:17 -0400600static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
601 gfp_t gfp)
602{
603 u8 *data;
604 struct pci_dev *pdev = bp->pdev;
605
606 data = kmalloc(bp->rx_buf_size, gfp);
607 if (!data)
608 return NULL;
609
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700610 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
611 bp->rx_buf_use_size, bp->rx_dir,
612 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400613
614 if (dma_mapping_error(&pdev->dev, *mapping)) {
615 kfree(data);
616 data = NULL;
617 }
618 return data;
619}
620
Michael Chan38413402017-02-06 16:55:43 -0500621int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
622 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400623{
624 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
625 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400626 dma_addr_t mapping;
627
Michael Chanc61fb992017-02-06 16:55:36 -0500628 if (BNXT_RX_PAGE_MODE(bp)) {
629 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400630
Michael Chanc61fb992017-02-06 16:55:36 -0500631 if (!page)
632 return -ENOMEM;
633
634 rx_buf->data = page;
635 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
636 } else {
637 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
638
639 if (!data)
640 return -ENOMEM;
641
642 rx_buf->data = data;
643 rx_buf->data_ptr = data + bp->rx_offset;
644 }
Michael Chan11cd1192017-02-06 16:55:33 -0500645 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400646
647 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400648 return 0;
649}
650
Michael Chanc6d30e82017-02-06 16:55:42 -0500651void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400652{
653 u16 prod = rxr->rx_prod;
654 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
655 struct rx_bd *cons_bd, *prod_bd;
656
657 prod_rx_buf = &rxr->rx_buf_ring[prod];
658 cons_rx_buf = &rxr->rx_buf_ring[cons];
659
660 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500661 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400662
Michael Chan11cd1192017-02-06 16:55:33 -0500663 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400664
665 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
666 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
667
668 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
669}
670
671static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
672{
673 u16 next, max = rxr->rx_agg_bmap_size;
674
675 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
676 if (next >= max)
677 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
678 return next;
679}
680
681static inline int bnxt_alloc_rx_page(struct bnxt *bp,
682 struct bnxt_rx_ring_info *rxr,
683 u16 prod, gfp_t gfp)
684{
685 struct rx_bd *rxbd =
686 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
687 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
688 struct pci_dev *pdev = bp->pdev;
689 struct page *page;
690 dma_addr_t mapping;
691 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400692 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400693
Michael Chan89d0a062016-04-25 02:30:51 -0400694 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
695 page = rxr->rx_page;
696 if (!page) {
697 page = alloc_page(gfp);
698 if (!page)
699 return -ENOMEM;
700 rxr->rx_page = page;
701 rxr->rx_page_offset = 0;
702 }
703 offset = rxr->rx_page_offset;
704 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
705 if (rxr->rx_page_offset == PAGE_SIZE)
706 rxr->rx_page = NULL;
707 else
708 get_page(page);
709 } else {
710 page = alloc_page(gfp);
711 if (!page)
712 return -ENOMEM;
713 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400714
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700715 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
716 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
717 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400718 if (dma_mapping_error(&pdev->dev, mapping)) {
719 __free_page(page);
720 return -EIO;
721 }
722
723 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
724 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
725
726 __set_bit(sw_prod, rxr->rx_agg_bmap);
727 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
728 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
729
730 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400731 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400732 rx_agg_buf->mapping = mapping;
733 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
734 rxbd->rx_bd_opaque = sw_prod;
735 return 0;
736}
737
738static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
739 u32 agg_bufs)
740{
741 struct bnxt *bp = bnapi->bp;
742 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500743 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400744 u16 prod = rxr->rx_agg_prod;
745 u16 sw_prod = rxr->rx_sw_agg_prod;
746 u32 i;
747
748 for (i = 0; i < agg_bufs; i++) {
749 u16 cons;
750 struct rx_agg_cmp *agg;
751 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
752 struct rx_bd *prod_bd;
753 struct page *page;
754
755 agg = (struct rx_agg_cmp *)
756 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
757 cons = agg->rx_agg_cmp_opaque;
758 __clear_bit(cons, rxr->rx_agg_bmap);
759
760 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
761 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
762
763 __set_bit(sw_prod, rxr->rx_agg_bmap);
764 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
765 cons_rx_buf = &rxr->rx_agg_ring[cons];
766
767 /* It is possible for sw_prod to be equal to cons, so
768 * set cons_rx_buf->page to NULL first.
769 */
770 page = cons_rx_buf->page;
771 cons_rx_buf->page = NULL;
772 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400773 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400774
775 prod_rx_buf->mapping = cons_rx_buf->mapping;
776
777 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
778
779 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
780 prod_bd->rx_bd_opaque = sw_prod;
781
782 prod = NEXT_RX_AGG(prod);
783 sw_prod = NEXT_RX_AGG(sw_prod);
784 cp_cons = NEXT_CMP(cp_cons);
785 }
786 rxr->rx_agg_prod = prod;
787 rxr->rx_sw_agg_prod = sw_prod;
788}
789
Michael Chanc61fb992017-02-06 16:55:36 -0500790static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
791 struct bnxt_rx_ring_info *rxr,
792 u16 cons, void *data, u8 *data_ptr,
793 dma_addr_t dma_addr,
794 unsigned int offset_and_len)
795{
796 unsigned int payload = offset_and_len >> 16;
797 unsigned int len = offset_and_len & 0xffff;
798 struct skb_frag_struct *frag;
799 struct page *page = data;
800 u16 prod = rxr->rx_prod;
801 struct sk_buff *skb;
802 int off, err;
803
804 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
805 if (unlikely(err)) {
806 bnxt_reuse_rx_data(rxr, cons, data);
807 return NULL;
808 }
809 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700810 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
811 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500812
813 if (unlikely(!payload))
814 payload = eth_get_headlen(data_ptr, len);
815
816 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
817 if (!skb) {
818 __free_page(page);
819 return NULL;
820 }
821
822 off = (void *)data_ptr - page_address(page);
823 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
824 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
825 payload + NET_IP_ALIGN);
826
827 frag = &skb_shinfo(skb)->frags[0];
828 skb_frag_size_sub(frag, payload);
829 frag->page_offset += payload;
830 skb->data_len -= payload;
831 skb->tail += payload;
832
833 return skb;
834}
835
Michael Chanc0c050c2015-10-22 16:01:17 -0400836static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
837 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500838 void *data, u8 *data_ptr,
839 dma_addr_t dma_addr,
840 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400841{
Michael Chan6bb19472017-02-06 16:55:32 -0500842 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400843 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500844 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400845
846 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
847 if (unlikely(err)) {
848 bnxt_reuse_rx_data(rxr, cons, data);
849 return NULL;
850 }
851
852 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700853 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
854 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400855 if (!skb) {
856 kfree(data);
857 return NULL;
858 }
859
Michael Chanb3dba772017-02-06 16:55:35 -0500860 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500861 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400862 return skb;
863}
864
865static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
866 struct sk_buff *skb, u16 cp_cons,
867 u32 agg_bufs)
868{
869 struct pci_dev *pdev = bp->pdev;
870 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500871 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400872 u16 prod = rxr->rx_agg_prod;
873 u32 i;
874
875 for (i = 0; i < agg_bufs; i++) {
876 u16 cons, frag_len;
877 struct rx_agg_cmp *agg;
878 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
879 struct page *page;
880 dma_addr_t mapping;
881
882 agg = (struct rx_agg_cmp *)
883 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
884 cons = agg->rx_agg_cmp_opaque;
885 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
886 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
887
888 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400889 skb_fill_page_desc(skb, i, cons_rx_buf->page,
890 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400891 __clear_bit(cons, rxr->rx_agg_bmap);
892
893 /* It is possible for bnxt_alloc_rx_page() to allocate
894 * a sw_prod index that equals the cons index, so we
895 * need to clear the cons entry now.
896 */
Michael Chan11cd1192017-02-06 16:55:33 -0500897 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400898 page = cons_rx_buf->page;
899 cons_rx_buf->page = NULL;
900
901 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
902 struct skb_shared_info *shinfo;
903 unsigned int nr_frags;
904
905 shinfo = skb_shinfo(skb);
906 nr_frags = --shinfo->nr_frags;
907 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
908
909 dev_kfree_skb(skb);
910
911 cons_rx_buf->page = page;
912
913 /* Update prod since possibly some pages have been
914 * allocated already.
915 */
916 rxr->rx_agg_prod = prod;
917 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
918 return NULL;
919 }
920
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700921 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
922 PCI_DMA_FROMDEVICE,
923 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400924
925 skb->data_len += frag_len;
926 skb->len += frag_len;
927 skb->truesize += PAGE_SIZE;
928
929 prod = NEXT_RX_AGG(prod);
930 cp_cons = NEXT_CMP(cp_cons);
931 }
932 rxr->rx_agg_prod = prod;
933 return skb;
934}
935
936static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
937 u8 agg_bufs, u32 *raw_cons)
938{
939 u16 last;
940 struct rx_agg_cmp *agg;
941
942 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
943 last = RING_CMP(*raw_cons);
944 agg = (struct rx_agg_cmp *)
945 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
946 return RX_AGG_CMP_VALID(agg, *raw_cons);
947}
948
949static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
950 unsigned int len,
951 dma_addr_t mapping)
952{
953 struct bnxt *bp = bnapi->bp;
954 struct pci_dev *pdev = bp->pdev;
955 struct sk_buff *skb;
956
957 skb = napi_alloc_skb(&bnapi->napi, len);
958 if (!skb)
959 return NULL;
960
Michael Chan745fc052017-02-06 16:55:34 -0500961 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
962 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400963
Michael Chan6bb19472017-02-06 16:55:32 -0500964 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
965 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400966
Michael Chan745fc052017-02-06 16:55:34 -0500967 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
968 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400969
970 skb_put(skb, len);
971 return skb;
972}
973
Michael Chanfa7e2812016-05-10 19:18:00 -0400974static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
975 u32 *raw_cons, void *cmp)
976{
977 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
978 struct rx_cmp *rxcmp = cmp;
979 u32 tmp_raw_cons = *raw_cons;
980 u8 cmp_type, agg_bufs = 0;
981
982 cmp_type = RX_CMP_TYPE(rxcmp);
983
984 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
985 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
986 RX_CMP_AGG_BUFS) >>
987 RX_CMP_AGG_BUFS_SHIFT;
988 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
989 struct rx_tpa_end_cmp *tpa_end = cmp;
990
991 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
992 RX_TPA_END_CMP_AGG_BUFS) >>
993 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
994 }
995
996 if (agg_bufs) {
997 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
998 return -EBUSY;
999 }
1000 *raw_cons = tmp_raw_cons;
1001 return 0;
1002}
1003
1004static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1005{
1006 if (!rxr->bnapi->in_reset) {
1007 rxr->bnapi->in_reset = true;
1008 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1009 schedule_work(&bp->sp_task);
1010 }
1011 rxr->rx_next_cons = 0xffff;
1012}
1013
Michael Chanc0c050c2015-10-22 16:01:17 -04001014static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1015 struct rx_tpa_start_cmp *tpa_start,
1016 struct rx_tpa_start_cmp_ext *tpa_start1)
1017{
1018 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1019 u16 cons, prod;
1020 struct bnxt_tpa_info *tpa_info;
1021 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1022 struct rx_bd *prod_bd;
1023 dma_addr_t mapping;
1024
1025 cons = tpa_start->rx_tpa_start_cmp_opaque;
1026 prod = rxr->rx_prod;
1027 cons_rx_buf = &rxr->rx_buf_ring[cons];
1028 prod_rx_buf = &rxr->rx_buf_ring[prod];
1029 tpa_info = &rxr->rx_tpa[agg_id];
1030
Michael Chanfa7e2812016-05-10 19:18:00 -04001031 if (unlikely(cons != rxr->rx_next_cons)) {
1032 bnxt_sched_reset(bp, rxr);
1033 return;
1034 }
1035
Michael Chanc0c050c2015-10-22 16:01:17 -04001036 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001037 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001038
1039 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001040 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001041
1042 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1043
1044 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1045
1046 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001047 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001048 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001049 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001050
1051 tpa_info->len =
1052 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1053 RX_TPA_START_CMP_LEN_SHIFT;
1054 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1055 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1056
1057 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1058 tpa_info->gso_type = SKB_GSO_TCPV4;
1059 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1060 if (hash_type == 3)
1061 tpa_info->gso_type = SKB_GSO_TCPV6;
1062 tpa_info->rss_hash =
1063 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1064 } else {
1065 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1066 tpa_info->gso_type = 0;
1067 if (netif_msg_rx_err(bp))
1068 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1069 }
1070 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1071 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001072 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001073
1074 rxr->rx_prod = NEXT_RX(prod);
1075 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001076 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001077 cons_rx_buf = &rxr->rx_buf_ring[cons];
1078
1079 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1080 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1081 cons_rx_buf->data = NULL;
1082}
1083
1084static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1085 u16 cp_cons, u32 agg_bufs)
1086{
1087 if (agg_bufs)
1088 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1089}
1090
Michael Chan94758f82016-06-13 02:25:35 -04001091static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1092 int payload_off, int tcp_ts,
1093 struct sk_buff *skb)
1094{
1095#ifdef CONFIG_INET
1096 struct tcphdr *th;
1097 int len, nw_off;
1098 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1099 u32 hdr_info = tpa_info->hdr_info;
1100 bool loopback = false;
1101
1102 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1103 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1104 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1105
1106 /* If the packet is an internal loopback packet, the offsets will
1107 * have an extra 4 bytes.
1108 */
1109 if (inner_mac_off == 4) {
1110 loopback = true;
1111 } else if (inner_mac_off > 4) {
1112 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1113 ETH_HLEN - 2));
1114
1115 /* We only support inner iPv4/ipv6. If we don't see the
1116 * correct protocol ID, it must be a loopback packet where
1117 * the offsets are off by 4.
1118 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001119 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001120 loopback = true;
1121 }
1122 if (loopback) {
1123 /* internal loopback packet, subtract all offsets by 4 */
1124 inner_ip_off -= 4;
1125 inner_mac_off -= 4;
1126 outer_ip_off -= 4;
1127 }
1128
1129 nw_off = inner_ip_off - ETH_HLEN;
1130 skb_set_network_header(skb, nw_off);
1131 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1132 struct ipv6hdr *iph = ipv6_hdr(skb);
1133
1134 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1135 len = skb->len - skb_transport_offset(skb);
1136 th = tcp_hdr(skb);
1137 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1138 } else {
1139 struct iphdr *iph = ip_hdr(skb);
1140
1141 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1142 len = skb->len - skb_transport_offset(skb);
1143 th = tcp_hdr(skb);
1144 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1145 }
1146
1147 if (inner_mac_off) { /* tunnel */
1148 struct udphdr *uh = NULL;
1149 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1150 ETH_HLEN - 2));
1151
1152 if (proto == htons(ETH_P_IP)) {
1153 struct iphdr *iph = (struct iphdr *)skb->data;
1154
1155 if (iph->protocol == IPPROTO_UDP)
1156 uh = (struct udphdr *)(iph + 1);
1157 } else {
1158 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1159
1160 if (iph->nexthdr == IPPROTO_UDP)
1161 uh = (struct udphdr *)(iph + 1);
1162 }
1163 if (uh) {
1164 if (uh->check)
1165 skb_shinfo(skb)->gso_type |=
1166 SKB_GSO_UDP_TUNNEL_CSUM;
1167 else
1168 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1169 }
1170 }
1171#endif
1172 return skb;
1173}
1174
Michael Chanc0c050c2015-10-22 16:01:17 -04001175#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1176#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1177
Michael Chan309369c2016-06-13 02:25:34 -04001178static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1179 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001180 struct sk_buff *skb)
1181{
Michael Chand1611c32015-10-25 22:27:57 -04001182#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001183 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001184 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001185
Michael Chan309369c2016-06-13 02:25:34 -04001186 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001187 tcp_opt_len = 12;
1188
Michael Chanc0c050c2015-10-22 16:01:17 -04001189 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1190 struct iphdr *iph;
1191
1192 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1193 ETH_HLEN;
1194 skb_set_network_header(skb, nw_off);
1195 iph = ip_hdr(skb);
1196 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1197 len = skb->len - skb_transport_offset(skb);
1198 th = tcp_hdr(skb);
1199 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1200 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1201 struct ipv6hdr *iph;
1202
1203 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1204 ETH_HLEN;
1205 skb_set_network_header(skb, nw_off);
1206 iph = ipv6_hdr(skb);
1207 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1208 len = skb->len - skb_transport_offset(skb);
1209 th = tcp_hdr(skb);
1210 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1211 } else {
1212 dev_kfree_skb_any(skb);
1213 return NULL;
1214 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001215
1216 if (nw_off) { /* tunnel */
1217 struct udphdr *uh = NULL;
1218
1219 if (skb->protocol == htons(ETH_P_IP)) {
1220 struct iphdr *iph = (struct iphdr *)skb->data;
1221
1222 if (iph->protocol == IPPROTO_UDP)
1223 uh = (struct udphdr *)(iph + 1);
1224 } else {
1225 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1226
1227 if (iph->nexthdr == IPPROTO_UDP)
1228 uh = (struct udphdr *)(iph + 1);
1229 }
1230 if (uh) {
1231 if (uh->check)
1232 skb_shinfo(skb)->gso_type |=
1233 SKB_GSO_UDP_TUNNEL_CSUM;
1234 else
1235 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1236 }
1237 }
1238#endif
1239 return skb;
1240}
1241
Michael Chan309369c2016-06-13 02:25:34 -04001242static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1243 struct bnxt_tpa_info *tpa_info,
1244 struct rx_tpa_end_cmp *tpa_end,
1245 struct rx_tpa_end_cmp_ext *tpa_end1,
1246 struct sk_buff *skb)
1247{
1248#ifdef CONFIG_INET
1249 int payload_off;
1250 u16 segs;
1251
1252 segs = TPA_END_TPA_SEGS(tpa_end);
1253 if (segs == 1)
1254 return skb;
1255
1256 NAPI_GRO_CB(skb)->count = segs;
1257 skb_shinfo(skb)->gso_size =
1258 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1259 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1260 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1261 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1262 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1263 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001264 if (likely(skb))
1265 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001266#endif
1267 return skb;
1268}
1269
Michael Chanc0c050c2015-10-22 16:01:17 -04001270static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1271 struct bnxt_napi *bnapi,
1272 u32 *raw_cons,
1273 struct rx_tpa_end_cmp *tpa_end,
1274 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001275 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001276{
1277 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001278 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001279 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001280 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001281 u16 cp_cons = RING_CMP(*raw_cons);
1282 unsigned int len;
1283 struct bnxt_tpa_info *tpa_info;
1284 dma_addr_t mapping;
1285 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001286 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001287
Michael Chanfa7e2812016-05-10 19:18:00 -04001288 if (unlikely(bnapi->in_reset)) {
1289 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1290
1291 if (rc < 0)
1292 return ERR_PTR(-EBUSY);
1293 return NULL;
1294 }
1295
Michael Chanc0c050c2015-10-22 16:01:17 -04001296 tpa_info = &rxr->rx_tpa[agg_id];
1297 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001298 data_ptr = tpa_info->data_ptr;
1299 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001300 len = tpa_info->len;
1301 mapping = tpa_info->mapping;
1302
1303 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1304 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1305
1306 if (agg_bufs) {
1307 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1308 return ERR_PTR(-EBUSY);
1309
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001310 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001311 cp_cons = NEXT_CMP(cp_cons);
1312 }
1313
Michael Chan69c149e2017-06-23 14:01:00 -04001314 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001315 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001316 if (agg_bufs > MAX_SKB_FRAGS)
1317 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1318 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001319 return NULL;
1320 }
1321
1322 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001323 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001324 if (!skb) {
1325 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1326 return NULL;
1327 }
1328 } else {
1329 u8 *new_data;
1330 dma_addr_t new_mapping;
1331
1332 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1333 if (!new_data) {
1334 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1335 return NULL;
1336 }
1337
1338 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001339 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001340 tpa_info->mapping = new_mapping;
1341
1342 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001343 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1344 bp->rx_buf_use_size, bp->rx_dir,
1345 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001346
1347 if (!skb) {
1348 kfree(data);
1349 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1350 return NULL;
1351 }
Michael Chanb3dba772017-02-06 16:55:35 -05001352 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001353 skb_put(skb, len);
1354 }
1355
1356 if (agg_bufs) {
1357 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1358 if (!skb) {
1359 /* Page reuse already handled by bnxt_rx_pages(). */
1360 return NULL;
1361 }
1362 }
1363 skb->protocol = eth_type_trans(skb, bp->dev);
1364
1365 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1366 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1367
Michael Chan8852ddb2016-06-06 02:37:16 -04001368 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1369 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001370 u16 vlan_proto = tpa_info->metadata >>
1371 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001372 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001373
Michael Chan8852ddb2016-06-06 02:37:16 -04001374 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001375 }
1376
1377 skb_checksum_none_assert(skb);
1378 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1379 skb->ip_summed = CHECKSUM_UNNECESSARY;
1380 skb->csum_level =
1381 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1382 }
1383
1384 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001385 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001386
1387 return skb;
1388}
1389
1390/* returns the following:
1391 * 1 - 1 packet successfully received
1392 * 0 - successful TPA_START, packet not completed yet
1393 * -EBUSY - completion ring does not have all the agg buffers yet
1394 * -ENOMEM - packet aborted due to out of memory
1395 * -EIO - packet aborted due to hw error indicated in BD
1396 */
1397static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001398 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001399{
1400 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001401 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001402 struct net_device *dev = bp->dev;
1403 struct rx_cmp *rxcmp;
1404 struct rx_cmp_ext *rxcmp1;
1405 u32 tmp_raw_cons = *raw_cons;
1406 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1407 struct bnxt_sw_rx_bd *rx_buf;
1408 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001409 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001410 dma_addr_t dma_addr;
1411 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001412 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001413 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001414 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001415
1416 rxcmp = (struct rx_cmp *)
1417 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1418
1419 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1420 cp_cons = RING_CMP(tmp_raw_cons);
1421 rxcmp1 = (struct rx_cmp_ext *)
1422 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1423
1424 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1425 return -EBUSY;
1426
1427 cmp_type = RX_CMP_TYPE(rxcmp);
1428
1429 prod = rxr->rx_prod;
1430
1431 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1432 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1433 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1434
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001435 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001436 goto next_rx_no_prod;
1437
1438 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1439 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1440 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001441 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001442
1443 if (unlikely(IS_ERR(skb)))
1444 return -EBUSY;
1445
1446 rc = -ENOMEM;
1447 if (likely(skb)) {
1448 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001449 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001450 rc = 1;
1451 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001452 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001453 goto next_rx_no_prod;
1454 }
1455
1456 cons = rxcmp->rx_cmp_opaque;
1457 rx_buf = &rxr->rx_buf_ring[cons];
1458 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001459 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001460 if (unlikely(cons != rxr->rx_next_cons)) {
1461 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1462
1463 bnxt_sched_reset(bp, rxr);
1464 return rc1;
1465 }
Michael Chan6bb19472017-02-06 16:55:32 -05001466 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001467
Michael Chanc61fb992017-02-06 16:55:36 -05001468 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1469 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001470
1471 if (agg_bufs) {
1472 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1473 return -EBUSY;
1474
1475 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001476 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001477 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001478 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001479
1480 rx_buf->data = NULL;
1481 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1482 bnxt_reuse_rx_data(rxr, cons, data);
1483 if (agg_bufs)
1484 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1485
1486 rc = -EIO;
1487 goto next_rx;
1488 }
1489
1490 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001491 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001492
Michael Chanc6d30e82017-02-06 16:55:42 -05001493 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1494 rc = 1;
1495 goto next_rx;
1496 }
1497
Michael Chanc0c050c2015-10-22 16:01:17 -04001498 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001499 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001500 bnxt_reuse_rx_data(rxr, cons, data);
1501 if (!skb) {
1502 rc = -ENOMEM;
1503 goto next_rx;
1504 }
1505 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001506 u32 payload;
1507
Michael Chanc6d30e82017-02-06 16:55:42 -05001508 if (rx_buf->data_ptr == data_ptr)
1509 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1510 else
1511 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001512 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001513 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001514 if (!skb) {
1515 rc = -ENOMEM;
1516 goto next_rx;
1517 }
1518 }
1519
1520 if (agg_bufs) {
1521 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1522 if (!skb) {
1523 rc = -ENOMEM;
1524 goto next_rx;
1525 }
1526 }
1527
1528 if (RX_CMP_HASH_VALID(rxcmp)) {
1529 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1530 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1531
1532 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1533 if (hash_type != 1 && hash_type != 3)
1534 type = PKT_HASH_TYPE_L3;
1535 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1536 }
1537
1538 skb->protocol = eth_type_trans(skb, dev);
1539
Michael Chan8852ddb2016-06-06 02:37:16 -04001540 if ((rxcmp1->rx_cmp_flags2 &
1541 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1542 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001543 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001544 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001545 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1546
Michael Chan8852ddb2016-06-06 02:37:16 -04001547 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001548 }
1549
1550 skb_checksum_none_assert(skb);
1551 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1552 if (dev->features & NETIF_F_RXCSUM) {
1553 skb->ip_summed = CHECKSUM_UNNECESSARY;
1554 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1555 }
1556 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001557 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1558 if (dev->features & NETIF_F_RXCSUM)
1559 cpr->rx_l4_csum_errors++;
1560 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001561 }
1562
1563 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001564 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001565 rc = 1;
1566
1567next_rx:
1568 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001569 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001570
1571next_rx_no_prod:
1572 *raw_cons = tmp_raw_cons;
1573
1574 return rc;
1575}
1576
Michael Chan2270bc52017-06-23 14:01:01 -04001577/* In netpoll mode, if we are using a combined completion ring, we need to
1578 * discard the rx packets and recycle the buffers.
1579 */
1580static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1581 u32 *raw_cons, u8 *event)
1582{
1583 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1584 u32 tmp_raw_cons = *raw_cons;
1585 struct rx_cmp_ext *rxcmp1;
1586 struct rx_cmp *rxcmp;
1587 u16 cp_cons;
1588 u8 cmp_type;
1589
1590 cp_cons = RING_CMP(tmp_raw_cons);
1591 rxcmp = (struct rx_cmp *)
1592 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1593
1594 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1595 cp_cons = RING_CMP(tmp_raw_cons);
1596 rxcmp1 = (struct rx_cmp_ext *)
1597 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1598
1599 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1600 return -EBUSY;
1601
1602 cmp_type = RX_CMP_TYPE(rxcmp);
1603 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1604 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1605 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1606 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1607 struct rx_tpa_end_cmp_ext *tpa_end1;
1608
1609 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1610 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1611 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1612 }
1613 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1614}
1615
Michael Chan4bb13ab2016-04-05 14:09:01 -04001616#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001617 ((data) & \
1618 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001619
Michael Chanc0c050c2015-10-22 16:01:17 -04001620static int bnxt_async_event_process(struct bnxt *bp,
1621 struct hwrm_async_event_cmpl *cmpl)
1622{
1623 u16 event_id = le16_to_cpu(cmpl->event_id);
1624
1625 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1626 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001627 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001628 u32 data1 = le32_to_cpu(cmpl->event_data1);
1629 struct bnxt_link_info *link_info = &bp->link_info;
1630
1631 if (BNXT_VF(bp))
1632 goto async_event_process_exit;
1633 if (data1 & 0x20000) {
1634 u16 fw_speed = link_info->force_link_speed;
1635 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1636
1637 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1638 speed);
1639 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001640 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001641 /* fall thru */
1642 }
Michael Chan87c374d2016-12-02 21:17:16 -05001643 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001644 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001645 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001646 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001647 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001648 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001649 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001650 u32 data1 = le32_to_cpu(cmpl->event_data1);
1651 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1652
1653 if (BNXT_VF(bp))
1654 break;
1655
1656 if (bp->pf.port_id != port_id)
1657 break;
1658
Michael Chan4bb13ab2016-04-05 14:09:01 -04001659 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1660 break;
1661 }
Michael Chan87c374d2016-12-02 21:17:16 -05001662 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001663 if (BNXT_PF(bp))
1664 goto async_event_process_exit;
1665 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1666 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001667 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001668 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001669 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001670 schedule_work(&bp->sp_task);
1671async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001672 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001673 return 0;
1674}
1675
1676static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1677{
1678 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1679 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1680 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1681 (struct hwrm_fwd_req_cmpl *)txcmp;
1682
1683 switch (cmpl_type) {
1684 case CMPL_BASE_TYPE_HWRM_DONE:
1685 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1686 if (seq_id == bp->hwrm_intr_seq_id)
1687 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1688 else
1689 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1690 break;
1691
1692 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1693 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1694
1695 if ((vf_id < bp->pf.first_vf_id) ||
1696 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1697 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1698 vf_id);
1699 return -EINVAL;
1700 }
1701
1702 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1703 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1704 schedule_work(&bp->sp_task);
1705 break;
1706
1707 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1708 bnxt_async_event_process(bp,
1709 (struct hwrm_async_event_cmpl *)txcmp);
1710
1711 default:
1712 break;
1713 }
1714
1715 return 0;
1716}
1717
1718static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1719{
1720 struct bnxt_napi *bnapi = dev_instance;
1721 struct bnxt *bp = bnapi->bp;
1722 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1723 u32 cons = RING_CMP(cpr->cp_raw_cons);
1724
1725 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1726 napi_schedule(&bnapi->napi);
1727 return IRQ_HANDLED;
1728}
1729
1730static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1731{
1732 u32 raw_cons = cpr->cp_raw_cons;
1733 u16 cons = RING_CMP(raw_cons);
1734 struct tx_cmp *txcmp;
1735
1736 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1737
1738 return TX_CMP_VALID(txcmp, raw_cons);
1739}
1740
Michael Chanc0c050c2015-10-22 16:01:17 -04001741static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1742{
1743 struct bnxt_napi *bnapi = dev_instance;
1744 struct bnxt *bp = bnapi->bp;
1745 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1746 u32 cons = RING_CMP(cpr->cp_raw_cons);
1747 u32 int_status;
1748
1749 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1750
1751 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001752 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001753 /* return if erroneous interrupt */
1754 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1755 return IRQ_NONE;
1756 }
1757
1758 /* disable ring IRQ */
1759 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1760
1761 /* Return here if interrupt is shared and is disabled. */
1762 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1763 return IRQ_HANDLED;
1764
1765 napi_schedule(&bnapi->napi);
1766 return IRQ_HANDLED;
1767}
1768
1769static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1770{
1771 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1772 u32 raw_cons = cpr->cp_raw_cons;
1773 u32 cons;
1774 int tx_pkts = 0;
1775 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001776 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001777 struct tx_cmp *txcmp;
1778
1779 while (1) {
1780 int rc;
1781
1782 cons = RING_CMP(raw_cons);
1783 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1784
1785 if (!TX_CMP_VALID(txcmp, raw_cons))
1786 break;
1787
Michael Chan67a95e22016-05-04 16:56:43 -04001788 /* The valid test of the entry must be done first before
1789 * reading any further.
1790 */
Michael Chanb67daab2016-05-15 03:04:51 -04001791 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001792 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1793 tx_pkts++;
1794 /* return full budget so NAPI will complete. */
1795 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1796 rx_pkts = budget;
1797 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001798 if (likely(budget))
1799 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1800 else
1801 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1802 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001803 if (likely(rc >= 0))
1804 rx_pkts += rc;
1805 else if (rc == -EBUSY) /* partial completion */
1806 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001807 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1808 CMPL_BASE_TYPE_HWRM_DONE) ||
1809 (TX_CMP_TYPE(txcmp) ==
1810 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1811 (TX_CMP_TYPE(txcmp) ==
1812 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1813 bnxt_hwrm_handler(bp, txcmp);
1814 }
1815 raw_cons = NEXT_RAW_CMP(raw_cons);
1816
1817 if (rx_pkts == budget)
1818 break;
1819 }
1820
Michael Chan38413402017-02-06 16:55:43 -05001821 if (event & BNXT_TX_EVENT) {
1822 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1823 void __iomem *db = txr->tx_doorbell;
1824 u16 prod = txr->tx_prod;
1825
1826 /* Sync BD data before updating doorbell */
1827 wmb();
1828
Michael Chan434c9752017-05-29 19:06:08 -04001829 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001830 }
1831
Michael Chanc0c050c2015-10-22 16:01:17 -04001832 cpr->cp_raw_cons = raw_cons;
1833 /* ACK completion ring before freeing tx ring and producing new
1834 * buffers in rx/agg rings to prevent overflowing the completion
1835 * ring.
1836 */
1837 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1838
1839 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001840 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001841
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001842 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001843 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001844
Michael Chan434c9752017-05-29 19:06:08 -04001845 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1846 if (event & BNXT_AGG_EVENT)
1847 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1848 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001849 }
1850 return rx_pkts;
1851}
1852
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001853static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1854{
1855 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1856 struct bnxt *bp = bnapi->bp;
1857 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1858 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1859 struct tx_cmp *txcmp;
1860 struct rx_cmp_ext *rxcmp1;
1861 u32 cp_cons, tmp_raw_cons;
1862 u32 raw_cons = cpr->cp_raw_cons;
1863 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001864 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001865
1866 while (1) {
1867 int rc;
1868
1869 cp_cons = RING_CMP(raw_cons);
1870 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1871
1872 if (!TX_CMP_VALID(txcmp, raw_cons))
1873 break;
1874
1875 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1876 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1877 cp_cons = RING_CMP(tmp_raw_cons);
1878 rxcmp1 = (struct rx_cmp_ext *)
1879 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1880
1881 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1882 break;
1883
1884 /* force an error to recycle the buffer */
1885 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1886 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1887
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001888 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001889 if (likely(rc == -EIO))
1890 rx_pkts++;
1891 else if (rc == -EBUSY) /* partial completion */
1892 break;
1893 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1894 CMPL_BASE_TYPE_HWRM_DONE)) {
1895 bnxt_hwrm_handler(bp, txcmp);
1896 } else {
1897 netdev_err(bp->dev,
1898 "Invalid completion received on special ring\n");
1899 }
1900 raw_cons = NEXT_RAW_CMP(raw_cons);
1901
1902 if (rx_pkts == budget)
1903 break;
1904 }
1905
1906 cpr->cp_raw_cons = raw_cons;
1907 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001908 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001909
Michael Chan434c9752017-05-29 19:06:08 -04001910 if (event & BNXT_AGG_EVENT)
1911 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1912 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001913
1914 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001915 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001916 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1917 }
1918 return rx_pkts;
1919}
1920
Michael Chanc0c050c2015-10-22 16:01:17 -04001921static int bnxt_poll(struct napi_struct *napi, int budget)
1922{
1923 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1924 struct bnxt *bp = bnapi->bp;
1925 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1926 int work_done = 0;
1927
Michael Chanc0c050c2015-10-22 16:01:17 -04001928 while (1) {
1929 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1930
1931 if (work_done >= budget)
1932 break;
1933
1934 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001935 if (napi_complete_done(napi, work_done))
1936 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1937 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001938 break;
1939 }
1940 }
1941 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001942 return work_done;
1943}
1944
Michael Chanc0c050c2015-10-22 16:01:17 -04001945static void bnxt_free_tx_skbs(struct bnxt *bp)
1946{
1947 int i, max_idx;
1948 struct pci_dev *pdev = bp->pdev;
1949
Michael Chanb6ab4b02016-01-02 23:44:59 -05001950 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001951 return;
1952
1953 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1954 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001955 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001956 int j;
1957
Michael Chanc0c050c2015-10-22 16:01:17 -04001958 for (j = 0; j < max_idx;) {
1959 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1960 struct sk_buff *skb = tx_buf->skb;
1961 int k, last;
1962
1963 if (!skb) {
1964 j++;
1965 continue;
1966 }
1967
1968 tx_buf->skb = NULL;
1969
1970 if (tx_buf->is_push) {
1971 dev_kfree_skb(skb);
1972 j += 2;
1973 continue;
1974 }
1975
1976 dma_unmap_single(&pdev->dev,
1977 dma_unmap_addr(tx_buf, mapping),
1978 skb_headlen(skb),
1979 PCI_DMA_TODEVICE);
1980
1981 last = tx_buf->nr_frags;
1982 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001983 for (k = 0; k < last; k++, j++) {
1984 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001985 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1986
Michael Chand612a572016-01-28 03:11:22 -05001987 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001988 dma_unmap_page(
1989 &pdev->dev,
1990 dma_unmap_addr(tx_buf, mapping),
1991 skb_frag_size(frag), PCI_DMA_TODEVICE);
1992 }
1993 dev_kfree_skb(skb);
1994 }
1995 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1996 }
1997}
1998
1999static void bnxt_free_rx_skbs(struct bnxt *bp)
2000{
2001 int i, max_idx, max_agg_idx;
2002 struct pci_dev *pdev = bp->pdev;
2003
Michael Chanb6ab4b02016-01-02 23:44:59 -05002004 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002005 return;
2006
2007 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2008 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2009 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002010 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002011 int j;
2012
Michael Chanc0c050c2015-10-22 16:01:17 -04002013 if (rxr->rx_tpa) {
2014 for (j = 0; j < MAX_TPA; j++) {
2015 struct bnxt_tpa_info *tpa_info =
2016 &rxr->rx_tpa[j];
2017 u8 *data = tpa_info->data;
2018
2019 if (!data)
2020 continue;
2021
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002022 dma_unmap_single_attrs(&pdev->dev,
2023 tpa_info->mapping,
2024 bp->rx_buf_use_size,
2025 bp->rx_dir,
2026 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002027
2028 tpa_info->data = NULL;
2029
2030 kfree(data);
2031 }
2032 }
2033
2034 for (j = 0; j < max_idx; j++) {
2035 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002036 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002037 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002038
2039 if (!data)
2040 continue;
2041
Michael Chanc0c050c2015-10-22 16:01:17 -04002042 rx_buf->data = NULL;
2043
Michael Chan3ed3a832017-03-28 19:47:31 -04002044 if (BNXT_RX_PAGE_MODE(bp)) {
2045 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002046 dma_unmap_page_attrs(&pdev->dev, mapping,
2047 PAGE_SIZE, bp->rx_dir,
2048 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002049 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002050 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002051 dma_unmap_single_attrs(&pdev->dev, mapping,
2052 bp->rx_buf_use_size,
2053 bp->rx_dir,
2054 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002055 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002056 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002057 }
2058
2059 for (j = 0; j < max_agg_idx; j++) {
2060 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2061 &rxr->rx_agg_ring[j];
2062 struct page *page = rx_agg_buf->page;
2063
2064 if (!page)
2065 continue;
2066
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002067 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2068 BNXT_RX_PAGE_SIZE,
2069 PCI_DMA_FROMDEVICE,
2070 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002071
2072 rx_agg_buf->page = NULL;
2073 __clear_bit(j, rxr->rx_agg_bmap);
2074
2075 __free_page(page);
2076 }
Michael Chan89d0a062016-04-25 02:30:51 -04002077 if (rxr->rx_page) {
2078 __free_page(rxr->rx_page);
2079 rxr->rx_page = NULL;
2080 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002081 }
2082}
2083
2084static void bnxt_free_skbs(struct bnxt *bp)
2085{
2086 bnxt_free_tx_skbs(bp);
2087 bnxt_free_rx_skbs(bp);
2088}
2089
2090static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2091{
2092 struct pci_dev *pdev = bp->pdev;
2093 int i;
2094
2095 for (i = 0; i < ring->nr_pages; i++) {
2096 if (!ring->pg_arr[i])
2097 continue;
2098
2099 dma_free_coherent(&pdev->dev, ring->page_size,
2100 ring->pg_arr[i], ring->dma_arr[i]);
2101
2102 ring->pg_arr[i] = NULL;
2103 }
2104 if (ring->pg_tbl) {
2105 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2106 ring->pg_tbl, ring->pg_tbl_map);
2107 ring->pg_tbl = NULL;
2108 }
2109 if (ring->vmem_size && *ring->vmem) {
2110 vfree(*ring->vmem);
2111 *ring->vmem = NULL;
2112 }
2113}
2114
2115static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2116{
2117 int i;
2118 struct pci_dev *pdev = bp->pdev;
2119
2120 if (ring->nr_pages > 1) {
2121 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2122 ring->nr_pages * 8,
2123 &ring->pg_tbl_map,
2124 GFP_KERNEL);
2125 if (!ring->pg_tbl)
2126 return -ENOMEM;
2127 }
2128
2129 for (i = 0; i < ring->nr_pages; i++) {
2130 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2131 ring->page_size,
2132 &ring->dma_arr[i],
2133 GFP_KERNEL);
2134 if (!ring->pg_arr[i])
2135 return -ENOMEM;
2136
2137 if (ring->nr_pages > 1)
2138 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2139 }
2140
2141 if (ring->vmem_size) {
2142 *ring->vmem = vzalloc(ring->vmem_size);
2143 if (!(*ring->vmem))
2144 return -ENOMEM;
2145 }
2146 return 0;
2147}
2148
2149static void bnxt_free_rx_rings(struct bnxt *bp)
2150{
2151 int i;
2152
Michael Chanb6ab4b02016-01-02 23:44:59 -05002153 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002154 return;
2155
2156 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002157 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002158 struct bnxt_ring_struct *ring;
2159
Michael Chanc6d30e82017-02-06 16:55:42 -05002160 if (rxr->xdp_prog)
2161 bpf_prog_put(rxr->xdp_prog);
2162
Michael Chanc0c050c2015-10-22 16:01:17 -04002163 kfree(rxr->rx_tpa);
2164 rxr->rx_tpa = NULL;
2165
2166 kfree(rxr->rx_agg_bmap);
2167 rxr->rx_agg_bmap = NULL;
2168
2169 ring = &rxr->rx_ring_struct;
2170 bnxt_free_ring(bp, ring);
2171
2172 ring = &rxr->rx_agg_ring_struct;
2173 bnxt_free_ring(bp, ring);
2174 }
2175}
2176
2177static int bnxt_alloc_rx_rings(struct bnxt *bp)
2178{
2179 int i, rc, agg_rings = 0, tpa_rings = 0;
2180
Michael Chanb6ab4b02016-01-02 23:44:59 -05002181 if (!bp->rx_ring)
2182 return -ENOMEM;
2183
Michael Chanc0c050c2015-10-22 16:01:17 -04002184 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2185 agg_rings = 1;
2186
2187 if (bp->flags & BNXT_FLAG_TPA)
2188 tpa_rings = 1;
2189
2190 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002191 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002192 struct bnxt_ring_struct *ring;
2193
Michael Chanc0c050c2015-10-22 16:01:17 -04002194 ring = &rxr->rx_ring_struct;
2195
2196 rc = bnxt_alloc_ring(bp, ring);
2197 if (rc)
2198 return rc;
2199
2200 if (agg_rings) {
2201 u16 mem_size;
2202
2203 ring = &rxr->rx_agg_ring_struct;
2204 rc = bnxt_alloc_ring(bp, ring);
2205 if (rc)
2206 return rc;
2207
2208 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2209 mem_size = rxr->rx_agg_bmap_size / 8;
2210 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2211 if (!rxr->rx_agg_bmap)
2212 return -ENOMEM;
2213
2214 if (tpa_rings) {
2215 rxr->rx_tpa = kcalloc(MAX_TPA,
2216 sizeof(struct bnxt_tpa_info),
2217 GFP_KERNEL);
2218 if (!rxr->rx_tpa)
2219 return -ENOMEM;
2220 }
2221 }
2222 }
2223 return 0;
2224}
2225
2226static void bnxt_free_tx_rings(struct bnxt *bp)
2227{
2228 int i;
2229 struct pci_dev *pdev = bp->pdev;
2230
Michael Chanb6ab4b02016-01-02 23:44:59 -05002231 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002232 return;
2233
2234 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002235 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002236 struct bnxt_ring_struct *ring;
2237
Michael Chanc0c050c2015-10-22 16:01:17 -04002238 if (txr->tx_push) {
2239 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2240 txr->tx_push, txr->tx_push_mapping);
2241 txr->tx_push = NULL;
2242 }
2243
2244 ring = &txr->tx_ring_struct;
2245
2246 bnxt_free_ring(bp, ring);
2247 }
2248}
2249
2250static int bnxt_alloc_tx_rings(struct bnxt *bp)
2251{
2252 int i, j, rc;
2253 struct pci_dev *pdev = bp->pdev;
2254
2255 bp->tx_push_size = 0;
2256 if (bp->tx_push_thresh) {
2257 int push_size;
2258
2259 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2260 bp->tx_push_thresh);
2261
Michael Chan4419dbe2016-02-10 17:33:49 -05002262 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002263 push_size = 0;
2264 bp->tx_push_thresh = 0;
2265 }
2266
2267 bp->tx_push_size = push_size;
2268 }
2269
2270 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002271 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 struct bnxt_ring_struct *ring;
2273
Michael Chanc0c050c2015-10-22 16:01:17 -04002274 ring = &txr->tx_ring_struct;
2275
2276 rc = bnxt_alloc_ring(bp, ring);
2277 if (rc)
2278 return rc;
2279
2280 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002281 dma_addr_t mapping;
2282
2283 /* One pre-allocated DMA buffer to backup
2284 * TX push operation
2285 */
2286 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2287 bp->tx_push_size,
2288 &txr->tx_push_mapping,
2289 GFP_KERNEL);
2290
2291 if (!txr->tx_push)
2292 return -ENOMEM;
2293
Michael Chanc0c050c2015-10-22 16:01:17 -04002294 mapping = txr->tx_push_mapping +
2295 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002296 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002297
Michael Chan4419dbe2016-02-10 17:33:49 -05002298 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002299 }
2300 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002301 if (i < bp->tx_nr_rings_xdp)
2302 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002303 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2304 j++;
2305 }
2306 return 0;
2307}
2308
2309static void bnxt_free_cp_rings(struct bnxt *bp)
2310{
2311 int i;
2312
2313 if (!bp->bnapi)
2314 return;
2315
2316 for (i = 0; i < bp->cp_nr_rings; i++) {
2317 struct bnxt_napi *bnapi = bp->bnapi[i];
2318 struct bnxt_cp_ring_info *cpr;
2319 struct bnxt_ring_struct *ring;
2320
2321 if (!bnapi)
2322 continue;
2323
2324 cpr = &bnapi->cp_ring;
2325 ring = &cpr->cp_ring_struct;
2326
2327 bnxt_free_ring(bp, ring);
2328 }
2329}
2330
2331static int bnxt_alloc_cp_rings(struct bnxt *bp)
2332{
2333 int i, rc;
2334
2335 for (i = 0; i < bp->cp_nr_rings; i++) {
2336 struct bnxt_napi *bnapi = bp->bnapi[i];
2337 struct bnxt_cp_ring_info *cpr;
2338 struct bnxt_ring_struct *ring;
2339
2340 if (!bnapi)
2341 continue;
2342
2343 cpr = &bnapi->cp_ring;
2344 ring = &cpr->cp_ring_struct;
2345
2346 rc = bnxt_alloc_ring(bp, ring);
2347 if (rc)
2348 return rc;
2349 }
2350 return 0;
2351}
2352
2353static void bnxt_init_ring_struct(struct bnxt *bp)
2354{
2355 int i;
2356
2357 for (i = 0; i < bp->cp_nr_rings; i++) {
2358 struct bnxt_napi *bnapi = bp->bnapi[i];
2359 struct bnxt_cp_ring_info *cpr;
2360 struct bnxt_rx_ring_info *rxr;
2361 struct bnxt_tx_ring_info *txr;
2362 struct bnxt_ring_struct *ring;
2363
2364 if (!bnapi)
2365 continue;
2366
2367 cpr = &bnapi->cp_ring;
2368 ring = &cpr->cp_ring_struct;
2369 ring->nr_pages = bp->cp_nr_pages;
2370 ring->page_size = HW_CMPD_RING_SIZE;
2371 ring->pg_arr = (void **)cpr->cp_desc_ring;
2372 ring->dma_arr = cpr->cp_desc_mapping;
2373 ring->vmem_size = 0;
2374
Michael Chanb6ab4b02016-01-02 23:44:59 -05002375 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002376 if (!rxr)
2377 goto skip_rx;
2378
Michael Chanc0c050c2015-10-22 16:01:17 -04002379 ring = &rxr->rx_ring_struct;
2380 ring->nr_pages = bp->rx_nr_pages;
2381 ring->page_size = HW_RXBD_RING_SIZE;
2382 ring->pg_arr = (void **)rxr->rx_desc_ring;
2383 ring->dma_arr = rxr->rx_desc_mapping;
2384 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2385 ring->vmem = (void **)&rxr->rx_buf_ring;
2386
2387 ring = &rxr->rx_agg_ring_struct;
2388 ring->nr_pages = bp->rx_agg_nr_pages;
2389 ring->page_size = HW_RXBD_RING_SIZE;
2390 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2391 ring->dma_arr = rxr->rx_agg_desc_mapping;
2392 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2393 ring->vmem = (void **)&rxr->rx_agg_ring;
2394
Michael Chan3b2b7d92016-01-02 23:45:00 -05002395skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002396 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002397 if (!txr)
2398 continue;
2399
Michael Chanc0c050c2015-10-22 16:01:17 -04002400 ring = &txr->tx_ring_struct;
2401 ring->nr_pages = bp->tx_nr_pages;
2402 ring->page_size = HW_RXBD_RING_SIZE;
2403 ring->pg_arr = (void **)txr->tx_desc_ring;
2404 ring->dma_arr = txr->tx_desc_mapping;
2405 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2406 ring->vmem = (void **)&txr->tx_buf_ring;
2407 }
2408}
2409
2410static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2411{
2412 int i;
2413 u32 prod;
2414 struct rx_bd **rx_buf_ring;
2415
2416 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2417 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2418 int j;
2419 struct rx_bd *rxbd;
2420
2421 rxbd = rx_buf_ring[i];
2422 if (!rxbd)
2423 continue;
2424
2425 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2426 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2427 rxbd->rx_bd_opaque = prod;
2428 }
2429 }
2430}
2431
2432static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2433{
2434 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002435 struct bnxt_rx_ring_info *rxr;
2436 struct bnxt_ring_struct *ring;
2437 u32 prod, type;
2438 int i;
2439
Michael Chanc0c050c2015-10-22 16:01:17 -04002440 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2441 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2442
2443 if (NET_IP_ALIGN == 2)
2444 type |= RX_BD_FLAGS_SOP;
2445
Michael Chanb6ab4b02016-01-02 23:44:59 -05002446 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002447 ring = &rxr->rx_ring_struct;
2448 bnxt_init_rxbd_pages(ring, type);
2449
Michael Chanc6d30e82017-02-06 16:55:42 -05002450 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2451 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2452 if (IS_ERR(rxr->xdp_prog)) {
2453 int rc = PTR_ERR(rxr->xdp_prog);
2454
2455 rxr->xdp_prog = NULL;
2456 return rc;
2457 }
2458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002459 prod = rxr->rx_prod;
2460 for (i = 0; i < bp->rx_ring_size; i++) {
2461 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2462 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2463 ring_nr, i, bp->rx_ring_size);
2464 break;
2465 }
2466 prod = NEXT_RX(prod);
2467 }
2468 rxr->rx_prod = prod;
2469 ring->fw_ring_id = INVALID_HW_RING_ID;
2470
Michael Chanedd0c2c2015-12-27 18:19:19 -05002471 ring = &rxr->rx_agg_ring_struct;
2472 ring->fw_ring_id = INVALID_HW_RING_ID;
2473
Michael Chanc0c050c2015-10-22 16:01:17 -04002474 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2475 return 0;
2476
Michael Chan2839f282016-04-25 02:30:50 -04002477 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002478 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2479
2480 bnxt_init_rxbd_pages(ring, type);
2481
2482 prod = rxr->rx_agg_prod;
2483 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2484 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2485 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2486 ring_nr, i, bp->rx_ring_size);
2487 break;
2488 }
2489 prod = NEXT_RX_AGG(prod);
2490 }
2491 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002492
2493 if (bp->flags & BNXT_FLAG_TPA) {
2494 if (rxr->rx_tpa) {
2495 u8 *data;
2496 dma_addr_t mapping;
2497
2498 for (i = 0; i < MAX_TPA; i++) {
2499 data = __bnxt_alloc_rx_data(bp, &mapping,
2500 GFP_KERNEL);
2501 if (!data)
2502 return -ENOMEM;
2503
2504 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002505 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002506 rxr->rx_tpa[i].mapping = mapping;
2507 }
2508 } else {
2509 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2510 return -ENOMEM;
2511 }
2512 }
2513
2514 return 0;
2515}
2516
Sankar Patchineelam22479252017-03-28 19:47:29 -04002517static void bnxt_init_cp_rings(struct bnxt *bp)
2518{
2519 int i;
2520
2521 for (i = 0; i < bp->cp_nr_rings; i++) {
2522 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2523 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2524
2525 ring->fw_ring_id = INVALID_HW_RING_ID;
2526 }
2527}
2528
Michael Chanc0c050c2015-10-22 16:01:17 -04002529static int bnxt_init_rx_rings(struct bnxt *bp)
2530{
2531 int i, rc = 0;
2532
Michael Chanc61fb992017-02-06 16:55:36 -05002533 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002534 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2535 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002536 } else {
2537 bp->rx_offset = BNXT_RX_OFFSET;
2538 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2539 }
Michael Chanb3dba772017-02-06 16:55:35 -05002540
Michael Chanc0c050c2015-10-22 16:01:17 -04002541 for (i = 0; i < bp->rx_nr_rings; i++) {
2542 rc = bnxt_init_one_rx_ring(bp, i);
2543 if (rc)
2544 break;
2545 }
2546
2547 return rc;
2548}
2549
2550static int bnxt_init_tx_rings(struct bnxt *bp)
2551{
2552 u16 i;
2553
2554 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2555 MAX_SKB_FRAGS + 1);
2556
2557 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002558 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002559 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2560
2561 ring->fw_ring_id = INVALID_HW_RING_ID;
2562 }
2563
2564 return 0;
2565}
2566
2567static void bnxt_free_ring_grps(struct bnxt *bp)
2568{
2569 kfree(bp->grp_info);
2570 bp->grp_info = NULL;
2571}
2572
2573static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2574{
2575 int i;
2576
2577 if (irq_re_init) {
2578 bp->grp_info = kcalloc(bp->cp_nr_rings,
2579 sizeof(struct bnxt_ring_grp_info),
2580 GFP_KERNEL);
2581 if (!bp->grp_info)
2582 return -ENOMEM;
2583 }
2584 for (i = 0; i < bp->cp_nr_rings; i++) {
2585 if (irq_re_init)
2586 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2587 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2588 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2589 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2590 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2591 }
2592 return 0;
2593}
2594
2595static void bnxt_free_vnics(struct bnxt *bp)
2596{
2597 kfree(bp->vnic_info);
2598 bp->vnic_info = NULL;
2599 bp->nr_vnics = 0;
2600}
2601
2602static int bnxt_alloc_vnics(struct bnxt *bp)
2603{
2604 int num_vnics = 1;
2605
2606#ifdef CONFIG_RFS_ACCEL
2607 if (bp->flags & BNXT_FLAG_RFS)
2608 num_vnics += bp->rx_nr_rings;
2609#endif
2610
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002611 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2612 num_vnics++;
2613
Michael Chanc0c050c2015-10-22 16:01:17 -04002614 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2615 GFP_KERNEL);
2616 if (!bp->vnic_info)
2617 return -ENOMEM;
2618
2619 bp->nr_vnics = num_vnics;
2620 return 0;
2621}
2622
2623static void bnxt_init_vnics(struct bnxt *bp)
2624{
2625 int i;
2626
2627 for (i = 0; i < bp->nr_vnics; i++) {
2628 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2629
2630 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002631 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2632 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002633 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2634
2635 if (bp->vnic_info[i].rss_hash_key) {
2636 if (i == 0)
2637 prandom_bytes(vnic->rss_hash_key,
2638 HW_HASH_KEY_SIZE);
2639 else
2640 memcpy(vnic->rss_hash_key,
2641 bp->vnic_info[0].rss_hash_key,
2642 HW_HASH_KEY_SIZE);
2643 }
2644 }
2645}
2646
2647static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2648{
2649 int pages;
2650
2651 pages = ring_size / desc_per_pg;
2652
2653 if (!pages)
2654 return 1;
2655
2656 pages++;
2657
2658 while (pages & (pages - 1))
2659 pages++;
2660
2661 return pages;
2662}
2663
Michael Chanc6d30e82017-02-06 16:55:42 -05002664void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002665{
2666 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002667 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2668 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002669 if (bp->dev->features & NETIF_F_LRO)
2670 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002671 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002672 bp->flags |= BNXT_FLAG_GRO;
2673}
2674
2675/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2676 * be set on entry.
2677 */
2678void bnxt_set_ring_params(struct bnxt *bp)
2679{
2680 u32 ring_size, rx_size, rx_space;
2681 u32 agg_factor = 0, agg_ring_size = 0;
2682
2683 /* 8 for CRC and VLAN */
2684 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2685
2686 rx_space = rx_size + NET_SKB_PAD +
2687 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2688
2689 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2690 ring_size = bp->rx_ring_size;
2691 bp->rx_agg_ring_size = 0;
2692 bp->rx_agg_nr_pages = 0;
2693
2694 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002695 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002696
2697 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002698 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002699 u32 jumbo_factor;
2700
2701 bp->flags |= BNXT_FLAG_JUMBO;
2702 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2703 if (jumbo_factor > agg_factor)
2704 agg_factor = jumbo_factor;
2705 }
2706 agg_ring_size = ring_size * agg_factor;
2707
2708 if (agg_ring_size) {
2709 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2710 RX_DESC_CNT);
2711 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2712 u32 tmp = agg_ring_size;
2713
2714 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2715 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2716 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2717 tmp, agg_ring_size);
2718 }
2719 bp->rx_agg_ring_size = agg_ring_size;
2720 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2721 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2722 rx_space = rx_size + NET_SKB_PAD +
2723 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2724 }
2725
2726 bp->rx_buf_use_size = rx_size;
2727 bp->rx_buf_size = rx_space;
2728
2729 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2730 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2731
2732 ring_size = bp->tx_ring_size;
2733 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2734 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2735
2736 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2737 bp->cp_ring_size = ring_size;
2738
2739 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2740 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2741 bp->cp_nr_pages = MAX_CP_PAGES;
2742 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2743 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2744 ring_size, bp->cp_ring_size);
2745 }
2746 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2747 bp->cp_ring_mask = bp->cp_bit - 1;
2748}
2749
Michael Chanc61fb992017-02-06 16:55:36 -05002750int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002751{
Michael Chanc61fb992017-02-06 16:55:36 -05002752 if (page_mode) {
2753 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2754 return -EOPNOTSUPP;
2755 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2756 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2757 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2758 bp->dev->hw_features &= ~NETIF_F_LRO;
2759 bp->dev->features &= ~NETIF_F_LRO;
2760 bp->rx_dir = DMA_BIDIRECTIONAL;
2761 bp->rx_skb_func = bnxt_rx_page_skb;
2762 } else {
2763 bp->dev->max_mtu = BNXT_MAX_MTU;
2764 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2765 bp->rx_dir = DMA_FROM_DEVICE;
2766 bp->rx_skb_func = bnxt_rx_skb;
2767 }
Michael Chan6bb19472017-02-06 16:55:32 -05002768 return 0;
2769}
2770
Michael Chanc0c050c2015-10-22 16:01:17 -04002771static void bnxt_free_vnic_attributes(struct bnxt *bp)
2772{
2773 int i;
2774 struct bnxt_vnic_info *vnic;
2775 struct pci_dev *pdev = bp->pdev;
2776
2777 if (!bp->vnic_info)
2778 return;
2779
2780 for (i = 0; i < bp->nr_vnics; i++) {
2781 vnic = &bp->vnic_info[i];
2782
2783 kfree(vnic->fw_grp_ids);
2784 vnic->fw_grp_ids = NULL;
2785
2786 kfree(vnic->uc_list);
2787 vnic->uc_list = NULL;
2788
2789 if (vnic->mc_list) {
2790 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2791 vnic->mc_list, vnic->mc_list_mapping);
2792 vnic->mc_list = NULL;
2793 }
2794
2795 if (vnic->rss_table) {
2796 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2797 vnic->rss_table,
2798 vnic->rss_table_dma_addr);
2799 vnic->rss_table = NULL;
2800 }
2801
2802 vnic->rss_hash_key = NULL;
2803 vnic->flags = 0;
2804 }
2805}
2806
2807static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2808{
2809 int i, rc = 0, size;
2810 struct bnxt_vnic_info *vnic;
2811 struct pci_dev *pdev = bp->pdev;
2812 int max_rings;
2813
2814 for (i = 0; i < bp->nr_vnics; i++) {
2815 vnic = &bp->vnic_info[i];
2816
2817 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2818 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2819
2820 if (mem_size > 0) {
2821 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2822 if (!vnic->uc_list) {
2823 rc = -ENOMEM;
2824 goto out;
2825 }
2826 }
2827 }
2828
2829 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2830 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2831 vnic->mc_list =
2832 dma_alloc_coherent(&pdev->dev,
2833 vnic->mc_list_size,
2834 &vnic->mc_list_mapping,
2835 GFP_KERNEL);
2836 if (!vnic->mc_list) {
2837 rc = -ENOMEM;
2838 goto out;
2839 }
2840 }
2841
2842 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2843 max_rings = bp->rx_nr_rings;
2844 else
2845 max_rings = 1;
2846
2847 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2848 if (!vnic->fw_grp_ids) {
2849 rc = -ENOMEM;
2850 goto out;
2851 }
2852
Michael Chanae10ae72016-12-29 12:13:38 -05002853 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2854 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2855 continue;
2856
Michael Chanc0c050c2015-10-22 16:01:17 -04002857 /* Allocate rss table and hash key */
2858 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2859 &vnic->rss_table_dma_addr,
2860 GFP_KERNEL);
2861 if (!vnic->rss_table) {
2862 rc = -ENOMEM;
2863 goto out;
2864 }
2865
2866 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2867
2868 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2869 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2870 }
2871 return 0;
2872
2873out:
2874 return rc;
2875}
2876
2877static void bnxt_free_hwrm_resources(struct bnxt *bp)
2878{
2879 struct pci_dev *pdev = bp->pdev;
2880
2881 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2882 bp->hwrm_cmd_resp_dma_addr);
2883
2884 bp->hwrm_cmd_resp_addr = NULL;
2885 if (bp->hwrm_dbg_resp_addr) {
2886 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2887 bp->hwrm_dbg_resp_addr,
2888 bp->hwrm_dbg_resp_dma_addr);
2889
2890 bp->hwrm_dbg_resp_addr = NULL;
2891 }
2892}
2893
2894static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2895{
2896 struct pci_dev *pdev = bp->pdev;
2897
2898 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2899 &bp->hwrm_cmd_resp_dma_addr,
2900 GFP_KERNEL);
2901 if (!bp->hwrm_cmd_resp_addr)
2902 return -ENOMEM;
2903 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2904 HWRM_DBG_REG_BUF_SIZE,
2905 &bp->hwrm_dbg_resp_dma_addr,
2906 GFP_KERNEL);
2907 if (!bp->hwrm_dbg_resp_addr)
2908 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2909
2910 return 0;
2911}
2912
Deepak Khungare605db82017-05-29 19:06:04 -04002913static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2914{
2915 if (bp->hwrm_short_cmd_req_addr) {
2916 struct pci_dev *pdev = bp->pdev;
2917
2918 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2919 bp->hwrm_short_cmd_req_addr,
2920 bp->hwrm_short_cmd_req_dma_addr);
2921 bp->hwrm_short_cmd_req_addr = NULL;
2922 }
2923}
2924
2925static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2926{
2927 struct pci_dev *pdev = bp->pdev;
2928
2929 bp->hwrm_short_cmd_req_addr =
2930 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2931 &bp->hwrm_short_cmd_req_dma_addr,
2932 GFP_KERNEL);
2933 if (!bp->hwrm_short_cmd_req_addr)
2934 return -ENOMEM;
2935
2936 return 0;
2937}
2938
Michael Chanc0c050c2015-10-22 16:01:17 -04002939static void bnxt_free_stats(struct bnxt *bp)
2940{
2941 u32 size, i;
2942 struct pci_dev *pdev = bp->pdev;
2943
Michael Chan3bdf56c2016-03-07 15:38:45 -05002944 if (bp->hw_rx_port_stats) {
2945 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2946 bp->hw_rx_port_stats,
2947 bp->hw_rx_port_stats_map);
2948 bp->hw_rx_port_stats = NULL;
2949 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2950 }
2951
Michael Chanc0c050c2015-10-22 16:01:17 -04002952 if (!bp->bnapi)
2953 return;
2954
2955 size = sizeof(struct ctx_hw_stats);
2956
2957 for (i = 0; i < bp->cp_nr_rings; i++) {
2958 struct bnxt_napi *bnapi = bp->bnapi[i];
2959 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2960
2961 if (cpr->hw_stats) {
2962 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2963 cpr->hw_stats_map);
2964 cpr->hw_stats = NULL;
2965 }
2966 }
2967}
2968
2969static int bnxt_alloc_stats(struct bnxt *bp)
2970{
2971 u32 size, i;
2972 struct pci_dev *pdev = bp->pdev;
2973
2974 size = sizeof(struct ctx_hw_stats);
2975
2976 for (i = 0; i < bp->cp_nr_rings; i++) {
2977 struct bnxt_napi *bnapi = bp->bnapi[i];
2978 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2979
2980 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2981 &cpr->hw_stats_map,
2982 GFP_KERNEL);
2983 if (!cpr->hw_stats)
2984 return -ENOMEM;
2985
2986 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2987 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002988
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002989 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002990 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2991 sizeof(struct tx_port_stats) + 1024;
2992
2993 bp->hw_rx_port_stats =
2994 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2995 &bp->hw_rx_port_stats_map,
2996 GFP_KERNEL);
2997 if (!bp->hw_rx_port_stats)
2998 return -ENOMEM;
2999
3000 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3001 512;
3002 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3003 sizeof(struct rx_port_stats) + 512;
3004 bp->flags |= BNXT_FLAG_PORT_STATS;
3005 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003006 return 0;
3007}
3008
3009static void bnxt_clear_ring_indices(struct bnxt *bp)
3010{
3011 int i;
3012
3013 if (!bp->bnapi)
3014 return;
3015
3016 for (i = 0; i < bp->cp_nr_rings; i++) {
3017 struct bnxt_napi *bnapi = bp->bnapi[i];
3018 struct bnxt_cp_ring_info *cpr;
3019 struct bnxt_rx_ring_info *rxr;
3020 struct bnxt_tx_ring_info *txr;
3021
3022 if (!bnapi)
3023 continue;
3024
3025 cpr = &bnapi->cp_ring;
3026 cpr->cp_raw_cons = 0;
3027
Michael Chanb6ab4b02016-01-02 23:44:59 -05003028 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003029 if (txr) {
3030 txr->tx_prod = 0;
3031 txr->tx_cons = 0;
3032 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003033
Michael Chanb6ab4b02016-01-02 23:44:59 -05003034 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003035 if (rxr) {
3036 rxr->rx_prod = 0;
3037 rxr->rx_agg_prod = 0;
3038 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003039 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003040 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003041 }
3042}
3043
3044static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3045{
3046#ifdef CONFIG_RFS_ACCEL
3047 int i;
3048
3049 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3050 * safe to delete the hash table.
3051 */
3052 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3053 struct hlist_head *head;
3054 struct hlist_node *tmp;
3055 struct bnxt_ntuple_filter *fltr;
3056
3057 head = &bp->ntp_fltr_hash_tbl[i];
3058 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3059 hlist_del(&fltr->hash);
3060 kfree(fltr);
3061 }
3062 }
3063 if (irq_reinit) {
3064 kfree(bp->ntp_fltr_bmap);
3065 bp->ntp_fltr_bmap = NULL;
3066 }
3067 bp->ntp_fltr_count = 0;
3068#endif
3069}
3070
3071static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3072{
3073#ifdef CONFIG_RFS_ACCEL
3074 int i, rc = 0;
3075
3076 if (!(bp->flags & BNXT_FLAG_RFS))
3077 return 0;
3078
3079 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3080 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3081
3082 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003083 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3084 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003085 GFP_KERNEL);
3086
3087 if (!bp->ntp_fltr_bmap)
3088 rc = -ENOMEM;
3089
3090 return rc;
3091#else
3092 return 0;
3093#endif
3094}
3095
3096static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3097{
3098 bnxt_free_vnic_attributes(bp);
3099 bnxt_free_tx_rings(bp);
3100 bnxt_free_rx_rings(bp);
3101 bnxt_free_cp_rings(bp);
3102 bnxt_free_ntp_fltrs(bp, irq_re_init);
3103 if (irq_re_init) {
3104 bnxt_free_stats(bp);
3105 bnxt_free_ring_grps(bp);
3106 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003107 kfree(bp->tx_ring_map);
3108 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003109 kfree(bp->tx_ring);
3110 bp->tx_ring = NULL;
3111 kfree(bp->rx_ring);
3112 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003113 kfree(bp->bnapi);
3114 bp->bnapi = NULL;
3115 } else {
3116 bnxt_clear_ring_indices(bp);
3117 }
3118}
3119
3120static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3121{
Michael Chan01657bc2016-01-02 23:45:03 -05003122 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003123 void *bnapi;
3124
3125 if (irq_re_init) {
3126 /* Allocate bnapi mem pointer array and mem block for
3127 * all queues
3128 */
3129 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3130 bp->cp_nr_rings);
3131 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3132 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3133 if (!bnapi)
3134 return -ENOMEM;
3135
3136 bp->bnapi = bnapi;
3137 bnapi += arr_size;
3138 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3139 bp->bnapi[i] = bnapi;
3140 bp->bnapi[i]->index = i;
3141 bp->bnapi[i]->bp = bp;
3142 }
3143
Michael Chanb6ab4b02016-01-02 23:44:59 -05003144 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3145 sizeof(struct bnxt_rx_ring_info),
3146 GFP_KERNEL);
3147 if (!bp->rx_ring)
3148 return -ENOMEM;
3149
3150 for (i = 0; i < bp->rx_nr_rings; i++) {
3151 bp->rx_ring[i].bnapi = bp->bnapi[i];
3152 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3153 }
3154
3155 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3156 sizeof(struct bnxt_tx_ring_info),
3157 GFP_KERNEL);
3158 if (!bp->tx_ring)
3159 return -ENOMEM;
3160
Michael Chana960dec2017-02-06 16:55:39 -05003161 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3162 GFP_KERNEL);
3163
3164 if (!bp->tx_ring_map)
3165 return -ENOMEM;
3166
Michael Chan01657bc2016-01-02 23:45:03 -05003167 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3168 j = 0;
3169 else
3170 j = bp->rx_nr_rings;
3171
3172 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3173 bp->tx_ring[i].bnapi = bp->bnapi[j];
3174 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003175 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003176 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003177 bp->tx_ring[i].txq_index = i -
3178 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003179 bp->bnapi[j]->tx_int = bnxt_tx_int;
3180 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003181 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003182 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3183 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003184 }
3185
Michael Chanc0c050c2015-10-22 16:01:17 -04003186 rc = bnxt_alloc_stats(bp);
3187 if (rc)
3188 goto alloc_mem_err;
3189
3190 rc = bnxt_alloc_ntp_fltrs(bp);
3191 if (rc)
3192 goto alloc_mem_err;
3193
3194 rc = bnxt_alloc_vnics(bp);
3195 if (rc)
3196 goto alloc_mem_err;
3197 }
3198
3199 bnxt_init_ring_struct(bp);
3200
3201 rc = bnxt_alloc_rx_rings(bp);
3202 if (rc)
3203 goto alloc_mem_err;
3204
3205 rc = bnxt_alloc_tx_rings(bp);
3206 if (rc)
3207 goto alloc_mem_err;
3208
3209 rc = bnxt_alloc_cp_rings(bp);
3210 if (rc)
3211 goto alloc_mem_err;
3212
3213 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3214 BNXT_VNIC_UCAST_FLAG;
3215 rc = bnxt_alloc_vnic_attributes(bp);
3216 if (rc)
3217 goto alloc_mem_err;
3218 return 0;
3219
3220alloc_mem_err:
3221 bnxt_free_mem(bp, true);
3222 return rc;
3223}
3224
Michael Chan9d8bc092016-12-29 12:13:33 -05003225static void bnxt_disable_int(struct bnxt *bp)
3226{
3227 int i;
3228
3229 if (!bp->bnapi)
3230 return;
3231
3232 for (i = 0; i < bp->cp_nr_rings; i++) {
3233 struct bnxt_napi *bnapi = bp->bnapi[i];
3234 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003235 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003236
Michael Chandaf1f1e2017-02-20 19:25:17 -05003237 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3238 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003239 }
3240}
3241
3242static void bnxt_disable_int_sync(struct bnxt *bp)
3243{
3244 int i;
3245
3246 atomic_inc(&bp->intr_sem);
3247
3248 bnxt_disable_int(bp);
3249 for (i = 0; i < bp->cp_nr_rings; i++)
3250 synchronize_irq(bp->irq_tbl[i].vector);
3251}
3252
3253static void bnxt_enable_int(struct bnxt *bp)
3254{
3255 int i;
3256
3257 atomic_set(&bp->intr_sem, 0);
3258 for (i = 0; i < bp->cp_nr_rings; i++) {
3259 struct bnxt_napi *bnapi = bp->bnapi[i];
3260 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3261
3262 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3263 }
3264}
3265
Michael Chanc0c050c2015-10-22 16:01:17 -04003266void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3267 u16 cmpl_ring, u16 target_id)
3268{
Michael Chana8643e12016-02-26 04:00:05 -05003269 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003270
Michael Chana8643e12016-02-26 04:00:05 -05003271 req->req_type = cpu_to_le16(req_type);
3272 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3273 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003274 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3275}
3276
Michael Chanfbfbc482016-02-26 04:00:07 -05003277static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3278 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003279{
Michael Chana11fa2b2016-05-15 03:04:47 -04003280 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003281 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003282 u32 *data = msg;
3283 __le32 *resp_len, *valid;
3284 u16 cp_ring_id, len = 0;
3285 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003286 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003287
Michael Chana8643e12016-02-26 04:00:05 -05003288 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003289 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003290 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003291 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3292
Deepak Khungare605db82017-05-29 19:06:04 -04003293 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3294 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3295 struct hwrm_short_input short_input = {0};
3296
3297 memcpy(short_cmd_req, req, msg_len);
3298 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3299 msg_len);
3300
3301 short_input.req_type = req->req_type;
3302 short_input.signature =
3303 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3304 short_input.size = cpu_to_le16(msg_len);
3305 short_input.req_addr =
3306 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3307
3308 data = (u32 *)&short_input;
3309 msg_len = sizeof(short_input);
3310
3311 /* Sync memory write before updating doorbell */
3312 wmb();
3313
3314 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3315 }
3316
Michael Chanc0c050c2015-10-22 16:01:17 -04003317 /* Write request msg to hwrm channel */
3318 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3319
Deepak Khungare605db82017-05-29 19:06:04 -04003320 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003321 writel(0, bp->bar0 + i);
3322
Michael Chanc0c050c2015-10-22 16:01:17 -04003323 /* currently supports only one outstanding message */
3324 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003325 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003326
3327 /* Ring channel doorbell */
3328 writel(1, bp->bar0 + 0x100);
3329
Michael Chanff4fe812016-02-26 04:00:04 -05003330 if (!timeout)
3331 timeout = DFLT_HWRM_CMD_TIMEOUT;
3332
Michael Chanc0c050c2015-10-22 16:01:17 -04003333 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003334 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003335 if (intr_process) {
3336 /* Wait until hwrm response cmpl interrupt is processed */
3337 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003338 i++ < tmo_count) {
3339 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003340 }
3341
3342 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3343 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003344 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003345 return -1;
3346 }
3347 } else {
3348 /* Check if response len is updated */
3349 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003350 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003351 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3352 HWRM_RESP_LEN_SFT;
3353 if (len)
3354 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003355 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003356 }
3357
Michael Chana11fa2b2016-05-15 03:04:47 -04003358 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003359 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003360 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003361 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003362 return -1;
3363 }
3364
3365 /* Last word of resp contains valid bit */
3366 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003367 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003368 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3369 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003370 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003371 }
3372
Michael Chana11fa2b2016-05-15 03:04:47 -04003373 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003374 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003375 timeout, le16_to_cpu(req->req_type),
3376 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003377 return -1;
3378 }
3379 }
3380
3381 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003382 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003383 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3384 le16_to_cpu(resp->req_type),
3385 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003386 return rc;
3387}
3388
3389int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3390{
3391 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003392}
3393
3394int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3395{
3396 int rc;
3397
3398 mutex_lock(&bp->hwrm_cmd_lock);
3399 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3400 mutex_unlock(&bp->hwrm_cmd_lock);
3401 return rc;
3402}
3403
Michael Chan90e209212016-02-26 04:00:08 -05003404int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3405 int timeout)
3406{
3407 int rc;
3408
3409 mutex_lock(&bp->hwrm_cmd_lock);
3410 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3411 mutex_unlock(&bp->hwrm_cmd_lock);
3412 return rc;
3413}
3414
Michael Chana1653b12016-12-07 00:26:20 -05003415int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3416 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003417{
3418 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003419 DECLARE_BITMAP(async_events_bmap, 256);
3420 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003421 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003422
3423 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3424
3425 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003426 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003427
Michael Chan25be8622016-04-05 14:09:00 -04003428 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3429 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3430 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3431
Michael Chana1653b12016-12-07 00:26:20 -05003432 if (bmap && bmap_size) {
3433 for (i = 0; i < bmap_size; i++) {
3434 if (test_bit(i, bmap))
3435 __set_bit(i, async_events_bmap);
3436 }
3437 }
3438
Michael Chan25be8622016-04-05 14:09:00 -04003439 for (i = 0; i < 8; i++)
3440 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3441
Michael Chana1653b12016-12-07 00:26:20 -05003442 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3443}
3444
3445static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3446{
3447 struct hwrm_func_drv_rgtr_input req = {0};
3448
3449 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3450
3451 req.enables =
3452 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3453 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3454
Michael Chan11f15ed2016-04-05 14:08:55 -04003455 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003456 req.ver_maj = DRV_VER_MAJ;
3457 req.ver_min = DRV_VER_MIN;
3458 req.ver_upd = DRV_VER_UPD;
3459
3460 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003461 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003462 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003463
Michael Chan9b0436c2017-07-11 13:05:36 -04003464 memset(data, 0, sizeof(data));
3465 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3466 u16 cmd = bnxt_vf_req_snif[i];
3467 unsigned int bit, idx;
3468
3469 idx = cmd / 32;
3470 bit = cmd % 32;
3471 data[idx] |= 1 << bit;
3472 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003473
Michael Chande68f5de2015-12-09 19:35:41 -05003474 for (i = 0; i < 8; i++)
3475 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3476
Michael Chanc0c050c2015-10-22 16:01:17 -04003477 req.enables |=
3478 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3479 }
3480
3481 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3482}
3483
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003484static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3485{
3486 struct hwrm_func_drv_unrgtr_input req = {0};
3487
3488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3489 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3490}
3491
Michael Chanc0c050c2015-10-22 16:01:17 -04003492static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3493{
3494 u32 rc = 0;
3495 struct hwrm_tunnel_dst_port_free_input req = {0};
3496
3497 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3498 req.tunnel_type = tunnel_type;
3499
3500 switch (tunnel_type) {
3501 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3502 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3503 break;
3504 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3505 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3506 break;
3507 default:
3508 break;
3509 }
3510
3511 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3512 if (rc)
3513 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3514 rc);
3515 return rc;
3516}
3517
3518static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3519 u8 tunnel_type)
3520{
3521 u32 rc = 0;
3522 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3523 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3524
3525 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3526
3527 req.tunnel_type = tunnel_type;
3528 req.tunnel_dst_port_val = port;
3529
3530 mutex_lock(&bp->hwrm_cmd_lock);
3531 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3532 if (rc) {
3533 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3534 rc);
3535 goto err_out;
3536 }
3537
Christophe Jaillet57aac712016-11-22 06:14:40 +01003538 switch (tunnel_type) {
3539 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003540 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003541 break;
3542 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003543 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003544 break;
3545 default:
3546 break;
3547 }
3548
Michael Chanc0c050c2015-10-22 16:01:17 -04003549err_out:
3550 mutex_unlock(&bp->hwrm_cmd_lock);
3551 return rc;
3552}
3553
3554static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3555{
3556 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3557 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3558
3559 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003560 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003561
3562 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3563 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3564 req.mask = cpu_to_le32(vnic->rx_mask);
3565 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3566}
3567
3568#ifdef CONFIG_RFS_ACCEL
3569static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3570 struct bnxt_ntuple_filter *fltr)
3571{
3572 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3573
3574 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3575 req.ntuple_filter_id = fltr->filter_id;
3576 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3577}
3578
3579#define BNXT_NTP_FLTR_FLAGS \
3580 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3581 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3582 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3583 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3584 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3585 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3586 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3587 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3588 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3589 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3590 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3591 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3592 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003593 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003594
Michael Chan61aad722017-02-12 19:18:14 -05003595#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3596 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3597
Michael Chanc0c050c2015-10-22 16:01:17 -04003598static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3599 struct bnxt_ntuple_filter *fltr)
3600{
3601 int rc = 0;
3602 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3603 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3604 bp->hwrm_cmd_resp_addr;
3605 struct flow_keys *keys = &fltr->fkeys;
3606 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3607
3608 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003609 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003610
3611 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3612
3613 req.ethertype = htons(ETH_P_IP);
3614 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003615 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003616 req.ip_protocol = keys->basic.ip_proto;
3617
Michael Chandda0e742016-12-29 12:13:40 -05003618 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3619 int i;
3620
3621 req.ethertype = htons(ETH_P_IPV6);
3622 req.ip_addr_type =
3623 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3624 *(struct in6_addr *)&req.src_ipaddr[0] =
3625 keys->addrs.v6addrs.src;
3626 *(struct in6_addr *)&req.dst_ipaddr[0] =
3627 keys->addrs.v6addrs.dst;
3628 for (i = 0; i < 4; i++) {
3629 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3630 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3631 }
3632 } else {
3633 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3634 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3635 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3636 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3637 }
Michael Chan61aad722017-02-12 19:18:14 -05003638 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3639 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3640 req.tunnel_type =
3641 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3642 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003643
3644 req.src_port = keys->ports.src;
3645 req.src_port_mask = cpu_to_be16(0xffff);
3646 req.dst_port = keys->ports.dst;
3647 req.dst_port_mask = cpu_to_be16(0xffff);
3648
Michael Chanc1935542015-12-27 18:19:28 -05003649 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003650 mutex_lock(&bp->hwrm_cmd_lock);
3651 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3652 if (!rc)
3653 fltr->filter_id = resp->ntuple_filter_id;
3654 mutex_unlock(&bp->hwrm_cmd_lock);
3655 return rc;
3656}
3657#endif
3658
3659static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3660 u8 *mac_addr)
3661{
3662 u32 rc = 0;
3663 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3664 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3665
3666 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003667 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3668 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3669 req.flags |=
3670 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003671 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003672 req.enables =
3673 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003674 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003675 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3676 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3677 req.l2_addr_mask[0] = 0xff;
3678 req.l2_addr_mask[1] = 0xff;
3679 req.l2_addr_mask[2] = 0xff;
3680 req.l2_addr_mask[3] = 0xff;
3681 req.l2_addr_mask[4] = 0xff;
3682 req.l2_addr_mask[5] = 0xff;
3683
3684 mutex_lock(&bp->hwrm_cmd_lock);
3685 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3686 if (!rc)
3687 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3688 resp->l2_filter_id;
3689 mutex_unlock(&bp->hwrm_cmd_lock);
3690 return rc;
3691}
3692
3693static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3694{
3695 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3696 int rc = 0;
3697
3698 /* Any associated ntuple filters will also be cleared by firmware. */
3699 mutex_lock(&bp->hwrm_cmd_lock);
3700 for (i = 0; i < num_of_vnics; i++) {
3701 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3702
3703 for (j = 0; j < vnic->uc_filter_count; j++) {
3704 struct hwrm_cfa_l2_filter_free_input req = {0};
3705
3706 bnxt_hwrm_cmd_hdr_init(bp, &req,
3707 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3708
3709 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3710
3711 rc = _hwrm_send_message(bp, &req, sizeof(req),
3712 HWRM_CMD_TIMEOUT);
3713 }
3714 vnic->uc_filter_count = 0;
3715 }
3716 mutex_unlock(&bp->hwrm_cmd_lock);
3717
3718 return rc;
3719}
3720
3721static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3722{
3723 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3724 struct hwrm_vnic_tpa_cfg_input req = {0};
3725
3726 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3727
3728 if (tpa_flags) {
3729 u16 mss = bp->dev->mtu - 40;
3730 u32 nsegs, n, segs = 0, flags;
3731
3732 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3733 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3734 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3735 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3736 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3737 if (tpa_flags & BNXT_FLAG_GRO)
3738 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3739
3740 req.flags = cpu_to_le32(flags);
3741
3742 req.enables =
3743 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003744 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3745 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003746
3747 /* Number of segs are log2 units, and first packet is not
3748 * included as part of this units.
3749 */
Michael Chan2839f282016-04-25 02:30:50 -04003750 if (mss <= BNXT_RX_PAGE_SIZE) {
3751 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003752 nsegs = (MAX_SKB_FRAGS - 1) * n;
3753 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003754 n = mss / BNXT_RX_PAGE_SIZE;
3755 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003756 n++;
3757 nsegs = (MAX_SKB_FRAGS - n) / n;
3758 }
3759
3760 segs = ilog2(nsegs);
3761 req.max_agg_segs = cpu_to_le16(segs);
3762 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003763
3764 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003765 }
3766 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3767
3768 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3769}
3770
3771static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3772{
3773 u32 i, j, max_rings;
3774 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3775 struct hwrm_vnic_rss_cfg_input req = {0};
3776
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003777 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003778 return 0;
3779
3780 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3781 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003782 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003783 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3784 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3785 max_rings = bp->rx_nr_rings - 1;
3786 else
3787 max_rings = bp->rx_nr_rings;
3788 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003789 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003790 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003791
3792 /* Fill the RSS indirection table with ring group ids */
3793 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3794 if (j == max_rings)
3795 j = 0;
3796 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3797 }
3798
3799 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3800 req.hash_key_tbl_addr =
3801 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3802 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003803 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003804 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3805}
3806
3807static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3808{
3809 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3810 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3811
3812 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3813 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3814 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3815 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3816 req.enables =
3817 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3818 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3819 /* thresholds not implemented in firmware yet */
3820 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3821 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3822 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3823 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3824}
3825
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003826static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3827 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003828{
3829 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3830
3831 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3832 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003833 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003834
3835 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003836 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003837}
3838
3839static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3840{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003841 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003842
3843 for (i = 0; i < bp->nr_vnics; i++) {
3844 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3845
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003846 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3847 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3848 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3849 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003850 }
3851 bp->rsscos_nr_ctxs = 0;
3852}
3853
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003854static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003855{
3856 int rc;
3857 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3858 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3859 bp->hwrm_cmd_resp_addr;
3860
3861 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3862 -1);
3863
3864 mutex_lock(&bp->hwrm_cmd_lock);
3865 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3866 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003867 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003868 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3869 mutex_unlock(&bp->hwrm_cmd_lock);
3870
3871 return rc;
3872}
3873
Michael Chana588e452016-12-07 00:26:21 -05003874int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003875{
Michael Chanb81a90d2016-01-02 23:45:01 -05003876 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003877 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3878 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003879 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003880
3881 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003882
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003883 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3884 /* Only RSS support for now TBD: COS & LB */
3885 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3886 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3887 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3888 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003889 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3890 req.rss_rule =
3891 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3892 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3893 VNIC_CFG_REQ_ENABLES_MRU);
3894 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003895 } else {
3896 req.rss_rule = cpu_to_le16(0xffff);
3897 }
3898
3899 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3900 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003901 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3902 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3903 } else {
3904 req.cos_rule = cpu_to_le16(0xffff);
3905 }
3906
Michael Chanc0c050c2015-10-22 16:01:17 -04003907 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003908 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003909 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003910 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003911 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3912 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003913
Michael Chanb81a90d2016-01-02 23:45:01 -05003914 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003915 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3916 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3917
3918 req.lb_rule = cpu_to_le16(0xffff);
3919 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3920 VLAN_HLEN);
3921
Michael Chancf6645f2016-06-13 02:25:28 -04003922#ifdef CONFIG_BNXT_SRIOV
3923 if (BNXT_VF(bp))
3924 def_vlan = bp->vf.vlan;
3925#endif
3926 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003927 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003928 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3929 req.flags |=
3930 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003931
3932 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3933}
3934
3935static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3936{
3937 u32 rc = 0;
3938
3939 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3940 struct hwrm_vnic_free_input req = {0};
3941
3942 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3943 req.vnic_id =
3944 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3945
3946 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3947 if (rc)
3948 return rc;
3949 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3950 }
3951 return rc;
3952}
3953
3954static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3955{
3956 u16 i;
3957
3958 for (i = 0; i < bp->nr_vnics; i++)
3959 bnxt_hwrm_vnic_free_one(bp, i);
3960}
3961
Michael Chanb81a90d2016-01-02 23:45:01 -05003962static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3963 unsigned int start_rx_ring_idx,
3964 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003965{
Michael Chanb81a90d2016-01-02 23:45:01 -05003966 int rc = 0;
3967 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003968 struct hwrm_vnic_alloc_input req = {0};
3969 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3970
3971 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003972 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3973 grp_idx = bp->rx_ring[i].bnapi->index;
3974 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003975 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003976 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003977 break;
3978 }
3979 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003980 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003981 }
3982
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003983 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3984 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003985 if (vnic_id == 0)
3986 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3987
3988 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3989
3990 mutex_lock(&bp->hwrm_cmd_lock);
3991 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3992 if (!rc)
3993 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3994 mutex_unlock(&bp->hwrm_cmd_lock);
3995 return rc;
3996}
3997
Michael Chan8fdefd62016-12-29 12:13:36 -05003998static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3999{
4000 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4001 struct hwrm_vnic_qcaps_input req = {0};
4002 int rc;
4003
4004 if (bp->hwrm_spec_code < 0x10600)
4005 return 0;
4006
4007 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4008 mutex_lock(&bp->hwrm_cmd_lock);
4009 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4010 if (!rc) {
4011 if (resp->flags &
4012 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4013 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4014 }
4015 mutex_unlock(&bp->hwrm_cmd_lock);
4016 return rc;
4017}
4018
Michael Chanc0c050c2015-10-22 16:01:17 -04004019static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4020{
4021 u16 i;
4022 u32 rc = 0;
4023
4024 mutex_lock(&bp->hwrm_cmd_lock);
4025 for (i = 0; i < bp->rx_nr_rings; i++) {
4026 struct hwrm_ring_grp_alloc_input req = {0};
4027 struct hwrm_ring_grp_alloc_output *resp =
4028 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004029 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004030
4031 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4032
Michael Chanb81a90d2016-01-02 23:45:01 -05004033 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4034 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4035 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4036 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004037
4038 rc = _hwrm_send_message(bp, &req, sizeof(req),
4039 HWRM_CMD_TIMEOUT);
4040 if (rc)
4041 break;
4042
Michael Chanb81a90d2016-01-02 23:45:01 -05004043 bp->grp_info[grp_idx].fw_grp_id =
4044 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004045 }
4046 mutex_unlock(&bp->hwrm_cmd_lock);
4047 return rc;
4048}
4049
4050static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4051{
4052 u16 i;
4053 u32 rc = 0;
4054 struct hwrm_ring_grp_free_input req = {0};
4055
4056 if (!bp->grp_info)
4057 return 0;
4058
4059 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4060
4061 mutex_lock(&bp->hwrm_cmd_lock);
4062 for (i = 0; i < bp->cp_nr_rings; i++) {
4063 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4064 continue;
4065 req.ring_group_id =
4066 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4067
4068 rc = _hwrm_send_message(bp, &req, sizeof(req),
4069 HWRM_CMD_TIMEOUT);
4070 if (rc)
4071 break;
4072 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4073 }
4074 mutex_unlock(&bp->hwrm_cmd_lock);
4075 return rc;
4076}
4077
4078static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4079 struct bnxt_ring_struct *ring,
4080 u32 ring_type, u32 map_index,
4081 u32 stats_ctx_id)
4082{
4083 int rc = 0, err = 0;
4084 struct hwrm_ring_alloc_input req = {0};
4085 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4086 u16 ring_id;
4087
4088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4089
4090 req.enables = 0;
4091 if (ring->nr_pages > 1) {
4092 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4093 /* Page size is in log2 units */
4094 req.page_size = BNXT_PAGE_SHIFT;
4095 req.page_tbl_depth = 1;
4096 } else {
4097 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4098 }
4099 req.fbo = 0;
4100 /* Association of ring index with doorbell index and MSIX number */
4101 req.logical_id = cpu_to_le16(map_index);
4102
4103 switch (ring_type) {
4104 case HWRM_RING_ALLOC_TX:
4105 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4106 /* Association of transmit ring with completion ring */
4107 req.cmpl_ring_id =
4108 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4109 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4110 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4111 req.queue_id = cpu_to_le16(ring->queue_id);
4112 break;
4113 case HWRM_RING_ALLOC_RX:
4114 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4115 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4116 break;
4117 case HWRM_RING_ALLOC_AGG:
4118 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4119 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4120 break;
4121 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004122 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004123 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4124 if (bp->flags & BNXT_FLAG_USING_MSIX)
4125 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4126 break;
4127 default:
4128 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4129 ring_type);
4130 return -1;
4131 }
4132
4133 mutex_lock(&bp->hwrm_cmd_lock);
4134 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4135 err = le16_to_cpu(resp->error_code);
4136 ring_id = le16_to_cpu(resp->ring_id);
4137 mutex_unlock(&bp->hwrm_cmd_lock);
4138
4139 if (rc || err) {
4140 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004141 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004142 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4143 rc, err);
4144 return -1;
4145
4146 case RING_FREE_REQ_RING_TYPE_RX:
4147 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4148 rc, err);
4149 return -1;
4150
4151 case RING_FREE_REQ_RING_TYPE_TX:
4152 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4153 rc, err);
4154 return -1;
4155
4156 default:
4157 netdev_err(bp->dev, "Invalid ring\n");
4158 return -1;
4159 }
4160 }
4161 ring->fw_ring_id = ring_id;
4162 return rc;
4163}
4164
Michael Chan486b5c22016-12-29 12:13:42 -05004165static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4166{
4167 int rc;
4168
4169 if (BNXT_PF(bp)) {
4170 struct hwrm_func_cfg_input req = {0};
4171
4172 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4173 req.fid = cpu_to_le16(0xffff);
4174 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4175 req.async_event_cr = cpu_to_le16(idx);
4176 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4177 } else {
4178 struct hwrm_func_vf_cfg_input req = {0};
4179
4180 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4181 req.enables =
4182 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4183 req.async_event_cr = cpu_to_le16(idx);
4184 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4185 }
4186 return rc;
4187}
4188
Michael Chanc0c050c2015-10-22 16:01:17 -04004189static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4190{
4191 int i, rc = 0;
4192
Michael Chanedd0c2c2015-12-27 18:19:19 -05004193 for (i = 0; i < bp->cp_nr_rings; i++) {
4194 struct bnxt_napi *bnapi = bp->bnapi[i];
4195 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4196 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004197
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004198 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004199 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4200 INVALID_STATS_CTX_ID);
4201 if (rc)
4202 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004203 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4204 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004205
4206 if (!i) {
4207 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4208 if (rc)
4209 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4210 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004211 }
4212
Michael Chanedd0c2c2015-12-27 18:19:19 -05004213 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004214 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004215 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004216 u32 map_idx = txr->bnapi->index;
4217 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004218
Michael Chanb81a90d2016-01-02 23:45:01 -05004219 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4220 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004221 if (rc)
4222 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004223 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004224 }
4225
Michael Chanedd0c2c2015-12-27 18:19:19 -05004226 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004227 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004228 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004229 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004230
Michael Chanb81a90d2016-01-02 23:45:01 -05004231 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4232 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004233 if (rc)
4234 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004235 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004236 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004237 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004238 }
4239
4240 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4241 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004242 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004243 struct bnxt_ring_struct *ring =
4244 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004245 u32 grp_idx = rxr->bnapi->index;
4246 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004247
4248 rc = hwrm_ring_alloc_send_msg(bp, ring,
4249 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004250 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004251 INVALID_STATS_CTX_ID);
4252 if (rc)
4253 goto err_out;
4254
Michael Chanb81a90d2016-01-02 23:45:01 -05004255 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004256 writel(DB_KEY_RX | rxr->rx_agg_prod,
4257 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004258 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004259 }
4260 }
4261err_out:
4262 return rc;
4263}
4264
4265static int hwrm_ring_free_send_msg(struct bnxt *bp,
4266 struct bnxt_ring_struct *ring,
4267 u32 ring_type, int cmpl_ring_id)
4268{
4269 int rc;
4270 struct hwrm_ring_free_input req = {0};
4271 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4272 u16 error_code;
4273
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004274 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004275 req.ring_type = ring_type;
4276 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4277
4278 mutex_lock(&bp->hwrm_cmd_lock);
4279 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4280 error_code = le16_to_cpu(resp->error_code);
4281 mutex_unlock(&bp->hwrm_cmd_lock);
4282
4283 if (rc || error_code) {
4284 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004285 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004286 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4287 rc);
4288 return rc;
4289 case RING_FREE_REQ_RING_TYPE_RX:
4290 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4291 rc);
4292 return rc;
4293 case RING_FREE_REQ_RING_TYPE_TX:
4294 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4295 rc);
4296 return rc;
4297 default:
4298 netdev_err(bp->dev, "Invalid ring\n");
4299 return -1;
4300 }
4301 }
4302 return 0;
4303}
4304
Michael Chanedd0c2c2015-12-27 18:19:19 -05004305static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004306{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004307 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004308
4309 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004310 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004311
Michael Chanedd0c2c2015-12-27 18:19:19 -05004312 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004313 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004314 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004315 u32 grp_idx = txr->bnapi->index;
4316 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004317
Michael Chanedd0c2c2015-12-27 18:19:19 -05004318 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4319 hwrm_ring_free_send_msg(bp, ring,
4320 RING_FREE_REQ_RING_TYPE_TX,
4321 close_path ? cmpl_ring_id :
4322 INVALID_HW_RING_ID);
4323 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004324 }
4325 }
4326
Michael Chanedd0c2c2015-12-27 18:19:19 -05004327 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004328 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004329 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004330 u32 grp_idx = rxr->bnapi->index;
4331 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004332
Michael Chanedd0c2c2015-12-27 18:19:19 -05004333 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4334 hwrm_ring_free_send_msg(bp, ring,
4335 RING_FREE_REQ_RING_TYPE_RX,
4336 close_path ? cmpl_ring_id :
4337 INVALID_HW_RING_ID);
4338 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004339 bp->grp_info[grp_idx].rx_fw_ring_id =
4340 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004341 }
4342 }
4343
Michael Chanedd0c2c2015-12-27 18:19:19 -05004344 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004345 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004346 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004347 u32 grp_idx = rxr->bnapi->index;
4348 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004349
Michael Chanedd0c2c2015-12-27 18:19:19 -05004350 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4351 hwrm_ring_free_send_msg(bp, ring,
4352 RING_FREE_REQ_RING_TYPE_RX,
4353 close_path ? cmpl_ring_id :
4354 INVALID_HW_RING_ID);
4355 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004356 bp->grp_info[grp_idx].agg_fw_ring_id =
4357 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004358 }
4359 }
4360
Michael Chan9d8bc092016-12-29 12:13:33 -05004361 /* The completion rings are about to be freed. After that the
4362 * IRQ doorbell will not work anymore. So we need to disable
4363 * IRQ here.
4364 */
4365 bnxt_disable_int_sync(bp);
4366
Michael Chanedd0c2c2015-12-27 18:19:19 -05004367 for (i = 0; i < bp->cp_nr_rings; i++) {
4368 struct bnxt_napi *bnapi = bp->bnapi[i];
4369 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4370 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004371
Michael Chanedd0c2c2015-12-27 18:19:19 -05004372 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4373 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004374 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004375 INVALID_HW_RING_ID);
4376 ring->fw_ring_id = INVALID_HW_RING_ID;
4377 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004378 }
4379 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004380}
4381
Michael Chan391be5c2016-12-29 12:13:41 -05004382/* Caller must hold bp->hwrm_cmd_lock */
4383int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4384{
4385 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4386 struct hwrm_func_qcfg_input req = {0};
4387 int rc;
4388
4389 if (bp->hwrm_spec_code < 0x10601)
4390 return 0;
4391
4392 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4393 req.fid = cpu_to_le16(fid);
4394 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4395 if (!rc)
4396 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4397
4398 return rc;
4399}
4400
Michael Chand1e79252017-02-06 16:55:38 -05004401static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004402{
4403 struct hwrm_func_cfg_input req = {0};
4404 int rc;
4405
4406 if (bp->hwrm_spec_code < 0x10601)
4407 return 0;
4408
4409 if (BNXT_VF(bp))
4410 return 0;
4411
4412 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4413 req.fid = cpu_to_le16(0xffff);
4414 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4415 req.num_tx_rings = cpu_to_le16(*tx_rings);
4416 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4417 if (rc)
4418 return rc;
4419
4420 mutex_lock(&bp->hwrm_cmd_lock);
4421 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4422 mutex_unlock(&bp->hwrm_cmd_lock);
4423 return rc;
4424}
4425
Michael Chanbb053f52016-02-26 04:00:02 -05004426static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4427 u32 buf_tmrs, u16 flags,
4428 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4429{
4430 req->flags = cpu_to_le16(flags);
4431 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4432 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4433 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4434 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4435 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4436 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4437 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4438 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4439}
4440
Michael Chanc0c050c2015-10-22 16:01:17 -04004441int bnxt_hwrm_set_coal(struct bnxt *bp)
4442{
4443 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004444 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4445 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004446 u16 max_buf, max_buf_irq;
4447 u16 buf_tmr, buf_tmr_irq;
4448 u32 flags;
4449
Michael Chandfc9c942016-02-26 04:00:03 -05004450 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4451 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4452 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4453 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004454
Michael Chandfb5b892016-02-26 04:00:01 -05004455 /* Each rx completion (2 records) should be DMAed immediately.
4456 * DMA 1/4 of the completion buffers at a time.
4457 */
4458 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004459 /* max_buf must not be zero */
4460 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004461 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4462 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4463 /* buf timer set to 1/4 of interrupt timer */
4464 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4465 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4466 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004467
4468 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4469
4470 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4471 * if coal_ticks is less than 25 us.
4472 */
Michael Chandfb5b892016-02-26 04:00:01 -05004473 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004474 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4475
Michael Chanbb053f52016-02-26 04:00:02 -05004476 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004477 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4478
4479 /* max_buf must not be zero */
4480 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4481 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4482 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4483 /* buf timer set to 1/4 of interrupt timer */
4484 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4485 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4486 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4487
4488 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4489 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4490 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004491
4492 mutex_lock(&bp->hwrm_cmd_lock);
4493 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004494 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004495
Michael Chandfc9c942016-02-26 04:00:03 -05004496 req = &req_rx;
4497 if (!bnapi->rx_ring)
4498 req = &req_tx;
4499 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4500
4501 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004502 HWRM_CMD_TIMEOUT);
4503 if (rc)
4504 break;
4505 }
4506 mutex_unlock(&bp->hwrm_cmd_lock);
4507 return rc;
4508}
4509
4510static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4511{
4512 int rc = 0, i;
4513 struct hwrm_stat_ctx_free_input req = {0};
4514
4515 if (!bp->bnapi)
4516 return 0;
4517
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004518 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4519 return 0;
4520
Michael Chanc0c050c2015-10-22 16:01:17 -04004521 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4522
4523 mutex_lock(&bp->hwrm_cmd_lock);
4524 for (i = 0; i < bp->cp_nr_rings; i++) {
4525 struct bnxt_napi *bnapi = bp->bnapi[i];
4526 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4527
4528 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4529 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4530
4531 rc = _hwrm_send_message(bp, &req, sizeof(req),
4532 HWRM_CMD_TIMEOUT);
4533 if (rc)
4534 break;
4535
4536 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4537 }
4538 }
4539 mutex_unlock(&bp->hwrm_cmd_lock);
4540 return rc;
4541}
4542
4543static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4544{
4545 int rc = 0, i;
4546 struct hwrm_stat_ctx_alloc_input req = {0};
4547 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4548
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004549 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4550 return 0;
4551
Michael Chanc0c050c2015-10-22 16:01:17 -04004552 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4553
Michael Chan51f30782016-07-01 18:46:29 -04004554 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004555
4556 mutex_lock(&bp->hwrm_cmd_lock);
4557 for (i = 0; i < bp->cp_nr_rings; i++) {
4558 struct bnxt_napi *bnapi = bp->bnapi[i];
4559 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4560
4561 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4562
4563 rc = _hwrm_send_message(bp, &req, sizeof(req),
4564 HWRM_CMD_TIMEOUT);
4565 if (rc)
4566 break;
4567
4568 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4569
4570 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4571 }
4572 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004573 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004574}
4575
Michael Chancf6645f2016-06-13 02:25:28 -04004576static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4577{
4578 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004579 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004580 int rc;
4581
4582 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4583 req.fid = cpu_to_le16(0xffff);
4584 mutex_lock(&bp->hwrm_cmd_lock);
4585 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4586 if (rc)
4587 goto func_qcfg_exit;
4588
4589#ifdef CONFIG_BNXT_SRIOV
4590 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004591 struct bnxt_vf_info *vf = &bp->vf;
4592
4593 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4594 }
4595#endif
Deepak Khungar9e54e322017-04-21 20:11:26 -04004596 if (BNXT_PF(bp)) {
4597 u16 flags = le16_to_cpu(resp->flags);
4598
4599 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4600 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED))
4601 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4602 if (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
4603 bp->flags |= BNXT_FLAG_MULTI_HOST;
4604 }
Michael Chanbc39f882017-03-08 18:44:34 -05004605
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004606 switch (resp->port_partition_type) {
4607 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4608 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4609 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4610 bp->port_partition_type = resp->port_partition_type;
4611 break;
4612 }
Michael Chancf6645f2016-06-13 02:25:28 -04004613
4614func_qcfg_exit:
4615 mutex_unlock(&bp->hwrm_cmd_lock);
4616 return rc;
4617}
4618
Michael Chan7b08f662016-12-07 00:26:18 -05004619static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004620{
4621 int rc = 0;
4622 struct hwrm_func_qcaps_input req = {0};
4623 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4624
4625 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4626 req.fid = cpu_to_le16(0xffff);
4627
4628 mutex_lock(&bp->hwrm_cmd_lock);
4629 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4630 if (rc)
4631 goto hwrm_func_qcaps_exit;
4632
Michael Chane4060d32016-12-07 00:26:19 -05004633 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4634 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4635 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4636 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4637
Michael Chan7cc5a202016-09-19 03:58:05 -04004638 bp->tx_push_thresh = 0;
4639 if (resp->flags &
4640 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4641 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4642
Michael Chanc0c050c2015-10-22 16:01:17 -04004643 if (BNXT_PF(bp)) {
4644 struct bnxt_pf_info *pf = &bp->pf;
4645
4646 pf->fw_fid = le16_to_cpu(resp->fid);
4647 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004648 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004649 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004650 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004651 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4652 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4653 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004654 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004655 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4656 if (!pf->max_hw_ring_grps)
4657 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004658 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4659 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4660 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4661 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4662 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4663 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4664 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4665 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4666 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4667 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4668 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004669 if (resp->flags &
4670 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4671 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004672 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004673#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004674 struct bnxt_vf_info *vf = &bp->vf;
4675
4676 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004677
4678 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4679 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4680 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4681 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004682 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4683 if (!vf->max_hw_ring_grps)
4684 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004685 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4686 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4687 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004688
4689 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004690 mutex_unlock(&bp->hwrm_cmd_lock);
4691
4692 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004693 /* overwrite netdev dev_adr with admin VF MAC */
4694 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004695 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004696 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004697 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4698 }
4699 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004700#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004701 }
4702
Michael Chanc0c050c2015-10-22 16:01:17 -04004703hwrm_func_qcaps_exit:
4704 mutex_unlock(&bp->hwrm_cmd_lock);
4705 return rc;
4706}
4707
4708static int bnxt_hwrm_func_reset(struct bnxt *bp)
4709{
4710 struct hwrm_func_reset_input req = {0};
4711
4712 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4713 req.enables = 0;
4714
4715 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4716}
4717
4718static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4719{
4720 int rc = 0;
4721 struct hwrm_queue_qportcfg_input req = {0};
4722 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4723 u8 i, *qptr;
4724
4725 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4726
4727 mutex_lock(&bp->hwrm_cmd_lock);
4728 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4729 if (rc)
4730 goto qportcfg_exit;
4731
4732 if (!resp->max_configurable_queues) {
4733 rc = -EINVAL;
4734 goto qportcfg_exit;
4735 }
4736 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004737 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004738 if (bp->max_tc > BNXT_MAX_QUEUE)
4739 bp->max_tc = BNXT_MAX_QUEUE;
4740
Michael Chan441cabb2016-09-19 03:58:02 -04004741 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4742 bp->max_tc = 1;
4743
Michael Chan87c374d2016-12-02 21:17:16 -05004744 if (bp->max_lltc > bp->max_tc)
4745 bp->max_lltc = bp->max_tc;
4746
Michael Chanc0c050c2015-10-22 16:01:17 -04004747 qptr = &resp->queue_id0;
4748 for (i = 0; i < bp->max_tc; i++) {
4749 bp->q_info[i].queue_id = *qptr++;
4750 bp->q_info[i].queue_profile = *qptr++;
4751 }
4752
4753qportcfg_exit:
4754 mutex_unlock(&bp->hwrm_cmd_lock);
4755 return rc;
4756}
4757
4758static int bnxt_hwrm_ver_get(struct bnxt *bp)
4759{
4760 int rc;
4761 struct hwrm_ver_get_input req = {0};
4762 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004763 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004764
Michael Chane6ef2692016-03-28 19:46:05 -04004765 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004766 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4767 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4768 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4769 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4770 mutex_lock(&bp->hwrm_cmd_lock);
4771 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4772 if (rc)
4773 goto hwrm_ver_get_exit;
4774
4775 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4776
Michael Chan11f15ed2016-04-05 14:08:55 -04004777 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4778 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004779 if (resp->hwrm_intf_maj < 1) {
4780 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004781 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004782 resp->hwrm_intf_upd);
4783 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004784 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004785 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004786 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4787 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4788
Michael Chanff4fe812016-02-26 04:00:04 -05004789 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4790 if (!bp->hwrm_cmd_timeout)
4791 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4792
Michael Chane6ef2692016-03-28 19:46:05 -04004793 if (resp->hwrm_intf_maj >= 1)
4794 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4795
Michael Chan659c8052016-06-13 02:25:33 -04004796 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004797 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4798 !resp->chip_metal)
4799 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004800
Deepak Khungare605db82017-05-29 19:06:04 -04004801 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4802 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4803 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4804 bp->flags |= BNXT_FLAG_SHORT_CMD;
4805
Michael Chanc0c050c2015-10-22 16:01:17 -04004806hwrm_ver_get_exit:
4807 mutex_unlock(&bp->hwrm_cmd_lock);
4808 return rc;
4809}
4810
Rob Swindell5ac67d82016-09-19 03:58:03 -04004811int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4812{
Rob Swindell878786d2016-09-20 03:36:33 -04004813#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004814 struct hwrm_fw_set_time_input req = {0};
4815 struct rtc_time tm;
4816 struct timeval tv;
4817
4818 if (bp->hwrm_spec_code < 0x10400)
4819 return -EOPNOTSUPP;
4820
4821 do_gettimeofday(&tv);
4822 rtc_time_to_tm(tv.tv_sec, &tm);
4823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4824 req.year = cpu_to_le16(1900 + tm.tm_year);
4825 req.month = 1 + tm.tm_mon;
4826 req.day = tm.tm_mday;
4827 req.hour = tm.tm_hour;
4828 req.minute = tm.tm_min;
4829 req.second = tm.tm_sec;
4830 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004831#else
4832 return -EOPNOTSUPP;
4833#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004834}
4835
Michael Chan3bdf56c2016-03-07 15:38:45 -05004836static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4837{
4838 int rc;
4839 struct bnxt_pf_info *pf = &bp->pf;
4840 struct hwrm_port_qstats_input req = {0};
4841
4842 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4843 return 0;
4844
4845 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4846 req.port_id = cpu_to_le16(pf->port_id);
4847 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4848 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4849 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4850 return rc;
4851}
4852
Michael Chanc0c050c2015-10-22 16:01:17 -04004853static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4854{
4855 if (bp->vxlan_port_cnt) {
4856 bnxt_hwrm_tunnel_dst_port_free(
4857 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4858 }
4859 bp->vxlan_port_cnt = 0;
4860 if (bp->nge_port_cnt) {
4861 bnxt_hwrm_tunnel_dst_port_free(
4862 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4863 }
4864 bp->nge_port_cnt = 0;
4865}
4866
4867static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4868{
4869 int rc, i;
4870 u32 tpa_flags = 0;
4871
4872 if (set_tpa)
4873 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4874 for (i = 0; i < bp->nr_vnics; i++) {
4875 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4876 if (rc) {
4877 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004878 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004879 return rc;
4880 }
4881 }
4882 return 0;
4883}
4884
4885static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4886{
4887 int i;
4888
4889 for (i = 0; i < bp->nr_vnics; i++)
4890 bnxt_hwrm_vnic_set_rss(bp, i, false);
4891}
4892
4893static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4894 bool irq_re_init)
4895{
4896 if (bp->vnic_info) {
4897 bnxt_hwrm_clear_vnic_filter(bp);
4898 /* clear all RSS setting before free vnic ctx */
4899 bnxt_hwrm_clear_vnic_rss(bp);
4900 bnxt_hwrm_vnic_ctx_free(bp);
4901 /* before free the vnic, undo the vnic tpa settings */
4902 if (bp->flags & BNXT_FLAG_TPA)
4903 bnxt_set_tpa(bp, false);
4904 bnxt_hwrm_vnic_free(bp);
4905 }
4906 bnxt_hwrm_ring_free(bp, close_path);
4907 bnxt_hwrm_ring_grp_free(bp);
4908 if (irq_re_init) {
4909 bnxt_hwrm_stat_ctx_free(bp);
4910 bnxt_hwrm_free_tunnel_ports(bp);
4911 }
4912}
4913
4914static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4915{
Michael Chanae10ae72016-12-29 12:13:38 -05004916 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004917 int rc;
4918
Michael Chanae10ae72016-12-29 12:13:38 -05004919 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4920 goto skip_rss_ctx;
4921
Michael Chanc0c050c2015-10-22 16:01:17 -04004922 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004923 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004924 if (rc) {
4925 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4926 vnic_id, rc);
4927 goto vnic_setup_err;
4928 }
4929 bp->rsscos_nr_ctxs++;
4930
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004931 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4932 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4933 if (rc) {
4934 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4935 vnic_id, rc);
4936 goto vnic_setup_err;
4937 }
4938 bp->rsscos_nr_ctxs++;
4939 }
4940
Michael Chanae10ae72016-12-29 12:13:38 -05004941skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004942 /* configure default vnic, ring grp */
4943 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4944 if (rc) {
4945 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4946 vnic_id, rc);
4947 goto vnic_setup_err;
4948 }
4949
4950 /* Enable RSS hashing on vnic */
4951 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4952 if (rc) {
4953 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4954 vnic_id, rc);
4955 goto vnic_setup_err;
4956 }
4957
4958 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4959 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4960 if (rc) {
4961 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4962 vnic_id, rc);
4963 }
4964 }
4965
4966vnic_setup_err:
4967 return rc;
4968}
4969
4970static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4971{
4972#ifdef CONFIG_RFS_ACCEL
4973 int i, rc = 0;
4974
4975 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004976 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004977 u16 vnic_id = i + 1;
4978 u16 ring_id = i;
4979
4980 if (vnic_id >= bp->nr_vnics)
4981 break;
4982
Michael Chanae10ae72016-12-29 12:13:38 -05004983 vnic = &bp->vnic_info[vnic_id];
4984 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4985 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4986 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004987 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004988 if (rc) {
4989 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4990 vnic_id, rc);
4991 break;
4992 }
4993 rc = bnxt_setup_vnic(bp, vnic_id);
4994 if (rc)
4995 break;
4996 }
4997 return rc;
4998#else
4999 return 0;
5000#endif
5001}
5002
Michael Chan17c71ac2016-07-01 18:46:27 -04005003/* Allow PF and VF with default VLAN to be in promiscuous mode */
5004static bool bnxt_promisc_ok(struct bnxt *bp)
5005{
5006#ifdef CONFIG_BNXT_SRIOV
5007 if (BNXT_VF(bp) && !bp->vf.vlan)
5008 return false;
5009#endif
5010 return true;
5011}
5012
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005013static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5014{
5015 unsigned int rc = 0;
5016
5017 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5018 if (rc) {
5019 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5020 rc);
5021 return rc;
5022 }
5023
5024 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5025 if (rc) {
5026 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5027 rc);
5028 return rc;
5029 }
5030 return rc;
5031}
5032
Michael Chanb664f002015-12-02 01:54:08 -05005033static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005034static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005035
Michael Chanc0c050c2015-10-22 16:01:17 -04005036static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5037{
Michael Chan7d2837d2016-05-04 16:56:44 -04005038 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005039 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005040 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005041
5042 if (irq_re_init) {
5043 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5044 if (rc) {
5045 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5046 rc);
5047 goto err_out;
5048 }
5049 }
5050
5051 rc = bnxt_hwrm_ring_alloc(bp);
5052 if (rc) {
5053 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5054 goto err_out;
5055 }
5056
5057 rc = bnxt_hwrm_ring_grp_alloc(bp);
5058 if (rc) {
5059 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5060 goto err_out;
5061 }
5062
Prashant Sreedharan76595192016-07-18 07:15:22 -04005063 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5064 rx_nr_rings--;
5065
Michael Chanc0c050c2015-10-22 16:01:17 -04005066 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005067 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005068 if (rc) {
5069 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5070 goto err_out;
5071 }
5072
5073 rc = bnxt_setup_vnic(bp, 0);
5074 if (rc)
5075 goto err_out;
5076
5077 if (bp->flags & BNXT_FLAG_RFS) {
5078 rc = bnxt_alloc_rfs_vnics(bp);
5079 if (rc)
5080 goto err_out;
5081 }
5082
5083 if (bp->flags & BNXT_FLAG_TPA) {
5084 rc = bnxt_set_tpa(bp, true);
5085 if (rc)
5086 goto err_out;
5087 }
5088
5089 if (BNXT_VF(bp))
5090 bnxt_update_vf_mac(bp);
5091
5092 /* Filter for default vnic 0 */
5093 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5094 if (rc) {
5095 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5096 goto err_out;
5097 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005098 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005099
Michael Chan7d2837d2016-05-04 16:56:44 -04005100 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005101
Michael Chan17c71ac2016-07-01 18:46:27 -04005102 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005103 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5104
5105 if (bp->dev->flags & IFF_ALLMULTI) {
5106 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5107 vnic->mc_list_count = 0;
5108 } else {
5109 u32 mask = 0;
5110
5111 bnxt_mc_list_updated(bp, &mask);
5112 vnic->rx_mask |= mask;
5113 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005114
Michael Chanb664f002015-12-02 01:54:08 -05005115 rc = bnxt_cfg_rx_mode(bp);
5116 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005117 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005118
5119 rc = bnxt_hwrm_set_coal(bp);
5120 if (rc)
5121 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005122 rc);
5123
5124 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5125 rc = bnxt_setup_nitroa0_vnic(bp);
5126 if (rc)
5127 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5128 rc);
5129 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005130
Michael Chancf6645f2016-06-13 02:25:28 -04005131 if (BNXT_VF(bp)) {
5132 bnxt_hwrm_func_qcfg(bp);
5133 netdev_update_features(bp->dev);
5134 }
5135
Michael Chanc0c050c2015-10-22 16:01:17 -04005136 return 0;
5137
5138err_out:
5139 bnxt_hwrm_resource_free(bp, 0, true);
5140
5141 return rc;
5142}
5143
5144static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5145{
5146 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5147 return 0;
5148}
5149
5150static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5151{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005152 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005153 bnxt_init_rx_rings(bp);
5154 bnxt_init_tx_rings(bp);
5155 bnxt_init_ring_grps(bp, irq_re_init);
5156 bnxt_init_vnics(bp);
5157
5158 return bnxt_init_chip(bp, irq_re_init);
5159}
5160
Michael Chanc0c050c2015-10-22 16:01:17 -04005161static int bnxt_set_real_num_queues(struct bnxt *bp)
5162{
5163 int rc;
5164 struct net_device *dev = bp->dev;
5165
Michael Chan5f449242017-02-06 16:55:40 -05005166 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5167 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005168 if (rc)
5169 return rc;
5170
5171 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5172 if (rc)
5173 return rc;
5174
5175#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005176 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005177 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005178#endif
5179
5180 return rc;
5181}
5182
Michael Chan6e6c5a52016-01-02 23:45:02 -05005183static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5184 bool shared)
5185{
5186 int _rx = *rx, _tx = *tx;
5187
5188 if (shared) {
5189 *rx = min_t(int, _rx, max);
5190 *tx = min_t(int, _tx, max);
5191 } else {
5192 if (max < 2)
5193 return -ENOMEM;
5194
5195 while (_rx + _tx > max) {
5196 if (_rx > _tx && _rx > 1)
5197 _rx--;
5198 else if (_tx > 1)
5199 _tx--;
5200 }
5201 *rx = _rx;
5202 *tx = _tx;
5203 }
5204 return 0;
5205}
5206
Michael Chan78095922016-12-07 00:26:16 -05005207static void bnxt_setup_msix(struct bnxt *bp)
5208{
5209 const int len = sizeof(bp->irq_tbl[0].name);
5210 struct net_device *dev = bp->dev;
5211 int tcs, i;
5212
5213 tcs = netdev_get_num_tc(dev);
5214 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005215 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005216
Michael Chand1e79252017-02-06 16:55:38 -05005217 for (i = 0; i < tcs; i++) {
5218 count = bp->tx_nr_rings_per_tc;
5219 off = i * count;
5220 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005221 }
5222 }
5223
5224 for (i = 0; i < bp->cp_nr_rings; i++) {
5225 char *attr;
5226
5227 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5228 attr = "TxRx";
5229 else if (i < bp->rx_nr_rings)
5230 attr = "rx";
5231 else
5232 attr = "tx";
5233
5234 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5235 i);
5236 bp->irq_tbl[i].handler = bnxt_msix;
5237 }
5238}
5239
5240static void bnxt_setup_inta(struct bnxt *bp)
5241{
5242 const int len = sizeof(bp->irq_tbl[0].name);
5243
5244 if (netdev_get_num_tc(bp->dev))
5245 netdev_reset_tc(bp->dev);
5246
5247 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5248 0);
5249 bp->irq_tbl[0].handler = bnxt_inta;
5250}
5251
5252static int bnxt_setup_int_mode(struct bnxt *bp)
5253{
5254 int rc;
5255
5256 if (bp->flags & BNXT_FLAG_USING_MSIX)
5257 bnxt_setup_msix(bp);
5258 else
5259 bnxt_setup_inta(bp);
5260
5261 rc = bnxt_set_real_num_queues(bp);
5262 return rc;
5263}
5264
Michael Chanb7429952017-01-13 01:32:00 -05005265#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005266static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5267{
5268#if defined(CONFIG_BNXT_SRIOV)
5269 if (BNXT_VF(bp))
5270 return bp->vf.max_rsscos_ctxs;
5271#endif
5272 return bp->pf.max_rsscos_ctxs;
5273}
5274
5275static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5276{
5277#if defined(CONFIG_BNXT_SRIOV)
5278 if (BNXT_VF(bp))
5279 return bp->vf.max_vnics;
5280#endif
5281 return bp->pf.max_vnics;
5282}
Michael Chanb7429952017-01-13 01:32:00 -05005283#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005284
Michael Chane4060d32016-12-07 00:26:19 -05005285unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5286{
5287#if defined(CONFIG_BNXT_SRIOV)
5288 if (BNXT_VF(bp))
5289 return bp->vf.max_stat_ctxs;
5290#endif
5291 return bp->pf.max_stat_ctxs;
5292}
5293
Michael Chana588e452016-12-07 00:26:21 -05005294void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5295{
5296#if defined(CONFIG_BNXT_SRIOV)
5297 if (BNXT_VF(bp))
5298 bp->vf.max_stat_ctxs = max;
5299 else
5300#endif
5301 bp->pf.max_stat_ctxs = max;
5302}
5303
Michael Chane4060d32016-12-07 00:26:19 -05005304unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5305{
5306#if defined(CONFIG_BNXT_SRIOV)
5307 if (BNXT_VF(bp))
5308 return bp->vf.max_cp_rings;
5309#endif
5310 return bp->pf.max_cp_rings;
5311}
5312
Michael Chana588e452016-12-07 00:26:21 -05005313void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5314{
5315#if defined(CONFIG_BNXT_SRIOV)
5316 if (BNXT_VF(bp))
5317 bp->vf.max_cp_rings = max;
5318 else
5319#endif
5320 bp->pf.max_cp_rings = max;
5321}
5322
Michael Chan78095922016-12-07 00:26:16 -05005323static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5324{
5325#if defined(CONFIG_BNXT_SRIOV)
5326 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005327 return min_t(unsigned int, bp->vf.max_irqs,
5328 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005329#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005330 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005331}
5332
Michael Chan33c26572016-12-07 00:26:15 -05005333void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5334{
5335#if defined(CONFIG_BNXT_SRIOV)
5336 if (BNXT_VF(bp))
5337 bp->vf.max_irqs = max_irqs;
5338 else
5339#endif
5340 bp->pf.max_irqs = max_irqs;
5341}
5342
Michael Chan78095922016-12-07 00:26:16 -05005343static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005344{
Michael Chan01657bc2016-01-02 23:45:03 -05005345 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005346 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005347
Michael Chan78095922016-12-07 00:26:16 -05005348 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005349 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5350 if (!msix_ent)
5351 return -ENOMEM;
5352
5353 for (i = 0; i < total_vecs; i++) {
5354 msix_ent[i].entry = i;
5355 msix_ent[i].vector = 0;
5356 }
5357
Michael Chan01657bc2016-01-02 23:45:03 -05005358 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5359 min = 2;
5360
5361 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005362 if (total_vecs < 0) {
5363 rc = -ENODEV;
5364 goto msix_setup_exit;
5365 }
5366
5367 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5368 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005369 for (i = 0; i < total_vecs; i++)
5370 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005371
Michael Chan78095922016-12-07 00:26:16 -05005372 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005373 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005374 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005375 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005376 if (rc)
5377 goto msix_setup_exit;
5378
Michael Chanc0c050c2015-10-22 16:01:17 -04005379 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005380 bp->cp_nr_rings = (min == 1) ?
5381 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5382 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005383
Michael Chanc0c050c2015-10-22 16:01:17 -04005384 } else {
5385 rc = -ENOMEM;
5386 goto msix_setup_exit;
5387 }
5388 bp->flags |= BNXT_FLAG_USING_MSIX;
5389 kfree(msix_ent);
5390 return 0;
5391
5392msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005393 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5394 kfree(bp->irq_tbl);
5395 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005396 pci_disable_msix(bp->pdev);
5397 kfree(msix_ent);
5398 return rc;
5399}
5400
Michael Chan78095922016-12-07 00:26:16 -05005401static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005402{
Michael Chanc0c050c2015-10-22 16:01:17 -04005403 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005404 if (!bp->irq_tbl)
5405 return -ENOMEM;
5406
5407 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005408 bp->rx_nr_rings = 1;
5409 bp->tx_nr_rings = 1;
5410 bp->cp_nr_rings = 1;
5411 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005412 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005413 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005414 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005415}
5416
Michael Chan78095922016-12-07 00:26:16 -05005417static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005418{
5419 int rc = 0;
5420
5421 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005422 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005423
Michael Chan1fa72e22016-04-25 02:30:49 -04005424 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005425 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005426 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005427 }
5428 return rc;
5429}
5430
Michael Chan78095922016-12-07 00:26:16 -05005431static void bnxt_clear_int_mode(struct bnxt *bp)
5432{
5433 if (bp->flags & BNXT_FLAG_USING_MSIX)
5434 pci_disable_msix(bp->pdev);
5435
5436 kfree(bp->irq_tbl);
5437 bp->irq_tbl = NULL;
5438 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5439}
5440
Michael Chanc0c050c2015-10-22 16:01:17 -04005441static void bnxt_free_irq(struct bnxt *bp)
5442{
5443 struct bnxt_irq *irq;
5444 int i;
5445
5446#ifdef CONFIG_RFS_ACCEL
5447 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5448 bp->dev->rx_cpu_rmap = NULL;
5449#endif
5450 if (!bp->irq_tbl)
5451 return;
5452
5453 for (i = 0; i < bp->cp_nr_rings; i++) {
5454 irq = &bp->irq_tbl[i];
5455 if (irq->requested)
5456 free_irq(irq->vector, bp->bnapi[i]);
5457 irq->requested = 0;
5458 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005459}
5460
5461static int bnxt_request_irq(struct bnxt *bp)
5462{
Michael Chanb81a90d2016-01-02 23:45:01 -05005463 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005464 unsigned long flags = 0;
5465#ifdef CONFIG_RFS_ACCEL
5466 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5467#endif
5468
5469 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5470 flags = IRQF_SHARED;
5471
Michael Chanb81a90d2016-01-02 23:45:01 -05005472 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005473 struct bnxt_irq *irq = &bp->irq_tbl[i];
5474#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005475 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005476 rc = irq_cpu_rmap_add(rmap, irq->vector);
5477 if (rc)
5478 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005479 j);
5480 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005481 }
5482#endif
5483 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5484 bp->bnapi[i]);
5485 if (rc)
5486 break;
5487
5488 irq->requested = 1;
5489 }
5490 return rc;
5491}
5492
5493static void bnxt_del_napi(struct bnxt *bp)
5494{
5495 int i;
5496
5497 if (!bp->bnapi)
5498 return;
5499
5500 for (i = 0; i < bp->cp_nr_rings; i++) {
5501 struct bnxt_napi *bnapi = bp->bnapi[i];
5502
5503 napi_hash_del(&bnapi->napi);
5504 netif_napi_del(&bnapi->napi);
5505 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005506 /* We called napi_hash_del() before netif_napi_del(), we need
5507 * to respect an RCU grace period before freeing napi structures.
5508 */
5509 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005510}
5511
5512static void bnxt_init_napi(struct bnxt *bp)
5513{
5514 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005515 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005516 struct bnxt_napi *bnapi;
5517
5518 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005519 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5520 cp_nr_rings--;
5521 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005522 bnapi = bp->bnapi[i];
5523 netif_napi_add(bp->dev, &bnapi->napi,
5524 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005525 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005526 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5527 bnapi = bp->bnapi[cp_nr_rings];
5528 netif_napi_add(bp->dev, &bnapi->napi,
5529 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005530 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005531 } else {
5532 bnapi = bp->bnapi[0];
5533 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005534 }
5535}
5536
5537static void bnxt_disable_napi(struct bnxt *bp)
5538{
5539 int i;
5540
5541 if (!bp->bnapi)
5542 return;
5543
Michael Chanb356a2e2016-12-29 12:13:31 -05005544 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005545 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005546}
5547
5548static void bnxt_enable_napi(struct bnxt *bp)
5549{
5550 int i;
5551
5552 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005553 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005554 napi_enable(&bp->bnapi[i]->napi);
5555 }
5556}
5557
Michael Chan7df4ae92016-12-02 21:17:17 -05005558void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005559{
5560 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005561 struct bnxt_tx_ring_info *txr;
5562 struct netdev_queue *txq;
5563
Michael Chanb6ab4b02016-01-02 23:44:59 -05005564 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005565 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005566 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005567 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005568 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005569 }
5570 }
5571 /* Stop all TX queues */
5572 netif_tx_disable(bp->dev);
5573 netif_carrier_off(bp->dev);
5574}
5575
Michael Chan7df4ae92016-12-02 21:17:17 -05005576void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005577{
5578 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005579 struct bnxt_tx_ring_info *txr;
5580 struct netdev_queue *txq;
5581
5582 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005583 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005584 txq = netdev_get_tx_queue(bp->dev, i);
5585 txr->dev_state = 0;
5586 }
5587 netif_tx_wake_all_queues(bp->dev);
5588 if (bp->link_info.link_up)
5589 netif_carrier_on(bp->dev);
5590}
5591
5592static void bnxt_report_link(struct bnxt *bp)
5593{
5594 if (bp->link_info.link_up) {
5595 const char *duplex;
5596 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005597 u32 speed;
5598 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005599
5600 netif_carrier_on(bp->dev);
5601 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5602 duplex = "full";
5603 else
5604 duplex = "half";
5605 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5606 flow_ctrl = "ON - receive & transmit";
5607 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5608 flow_ctrl = "ON - transmit";
5609 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5610 flow_ctrl = "ON - receive";
5611 else
5612 flow_ctrl = "none";
5613 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005614 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005615 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005616 if (bp->flags & BNXT_FLAG_EEE_CAP)
5617 netdev_info(bp->dev, "EEE is %s\n",
5618 bp->eee.eee_active ? "active" :
5619 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005620 fec = bp->link_info.fec_cfg;
5621 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5622 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5623 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5624 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5625 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005626 } else {
5627 netif_carrier_off(bp->dev);
5628 netdev_err(bp->dev, "NIC Link is Down\n");
5629 }
5630}
5631
Michael Chan170ce012016-04-05 14:08:57 -04005632static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5633{
5634 int rc = 0;
5635 struct hwrm_port_phy_qcaps_input req = {0};
5636 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005637 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005638
5639 if (bp->hwrm_spec_code < 0x10201)
5640 return 0;
5641
5642 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5643
5644 mutex_lock(&bp->hwrm_cmd_lock);
5645 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5646 if (rc)
5647 goto hwrm_phy_qcaps_exit;
5648
Michael Chanacb20052017-07-24 12:34:20 -04005649 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005650 struct ethtool_eee *eee = &bp->eee;
5651 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5652
5653 bp->flags |= BNXT_FLAG_EEE_CAP;
5654 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5655 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5656 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5657 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5658 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5659 }
Michael Chan520ad892017-03-08 18:44:35 -05005660 if (resp->supported_speeds_auto_mode)
5661 link_info->support_auto_speeds =
5662 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005663
5664hwrm_phy_qcaps_exit:
5665 mutex_unlock(&bp->hwrm_cmd_lock);
5666 return rc;
5667}
5668
Michael Chanc0c050c2015-10-22 16:01:17 -04005669static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5670{
5671 int rc = 0;
5672 struct bnxt_link_info *link_info = &bp->link_info;
5673 struct hwrm_port_phy_qcfg_input req = {0};
5674 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5675 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005676 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005677
5678 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5679
5680 mutex_lock(&bp->hwrm_cmd_lock);
5681 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5682 if (rc) {
5683 mutex_unlock(&bp->hwrm_cmd_lock);
5684 return rc;
5685 }
5686
5687 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5688 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005689 link_info->duplex = resp->duplex_cfg;
5690 if (bp->hwrm_spec_code >= 0x10800)
5691 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005692 link_info->pause = resp->pause;
5693 link_info->auto_mode = resp->auto_mode;
5694 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005695 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005696 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005697 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005698 if (link_info->phy_link_status == BNXT_LINK_LINK)
5699 link_info->link_speed = le16_to_cpu(resp->link_speed);
5700 else
5701 link_info->link_speed = 0;
5702 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005703 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5704 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005705 link_info->lp_auto_link_speeds =
5706 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005707 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5708 link_info->phy_ver[0] = resp->phy_maj;
5709 link_info->phy_ver[1] = resp->phy_min;
5710 link_info->phy_ver[2] = resp->phy_bld;
5711 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005712 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005713 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005714 link_info->phy_addr = resp->eee_config_phy_addr &
5715 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005716 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005717
Michael Chan170ce012016-04-05 14:08:57 -04005718 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5719 struct ethtool_eee *eee = &bp->eee;
5720 u16 fw_speeds;
5721
5722 eee->eee_active = 0;
5723 if (resp->eee_config_phy_addr &
5724 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5725 eee->eee_active = 1;
5726 fw_speeds = le16_to_cpu(
5727 resp->link_partner_adv_eee_link_speed_mask);
5728 eee->lp_advertised =
5729 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5730 }
5731
5732 /* Pull initial EEE config */
5733 if (!chng_link_state) {
5734 if (resp->eee_config_phy_addr &
5735 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5736 eee->eee_enabled = 1;
5737
5738 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5739 eee->advertised =
5740 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5741
5742 if (resp->eee_config_phy_addr &
5743 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5744 __le32 tmr;
5745
5746 eee->tx_lpi_enabled = 1;
5747 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5748 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5749 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5750 }
5751 }
5752 }
Michael Chane70c7522017-02-12 19:18:16 -05005753
5754 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5755 if (bp->hwrm_spec_code >= 0x10504)
5756 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5757
Michael Chanc0c050c2015-10-22 16:01:17 -04005758 /* TODO: need to add more logic to report VF link */
5759 if (chng_link_state) {
5760 if (link_info->phy_link_status == BNXT_LINK_LINK)
5761 link_info->link_up = 1;
5762 else
5763 link_info->link_up = 0;
5764 if (link_up != link_info->link_up)
5765 bnxt_report_link(bp);
5766 } else {
5767 /* alwasy link down if not require to update link state */
5768 link_info->link_up = 0;
5769 }
5770 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005771
5772 diff = link_info->support_auto_speeds ^ link_info->advertising;
5773 if ((link_info->support_auto_speeds | diff) !=
5774 link_info->support_auto_speeds) {
5775 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005776 * update the advertisement settings. Caller holds RTNL
5777 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005778 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005779 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005780 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005781 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005782 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005783 return 0;
5784}
5785
Michael Chan10289be2016-05-15 03:04:49 -04005786static void bnxt_get_port_module_status(struct bnxt *bp)
5787{
5788 struct bnxt_link_info *link_info = &bp->link_info;
5789 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5790 u8 module_status;
5791
5792 if (bnxt_update_link(bp, true))
5793 return;
5794
5795 module_status = link_info->module_status;
5796 switch (module_status) {
5797 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5798 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5799 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5800 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5801 bp->pf.port_id);
5802 if (bp->hwrm_spec_code >= 0x10201) {
5803 netdev_warn(bp->dev, "Module part number %s\n",
5804 resp->phy_vendor_partnumber);
5805 }
5806 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5807 netdev_warn(bp->dev, "TX is disabled\n");
5808 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5809 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5810 }
5811}
5812
Michael Chanc0c050c2015-10-22 16:01:17 -04005813static void
5814bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5815{
5816 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005817 if (bp->hwrm_spec_code >= 0x10201)
5818 req->auto_pause =
5819 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005820 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5821 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5822 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005823 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005824 req->enables |=
5825 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5826 } else {
5827 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5828 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5829 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5830 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5831 req->enables |=
5832 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005833 if (bp->hwrm_spec_code >= 0x10201) {
5834 req->auto_pause = req->force_pause;
5835 req->enables |= cpu_to_le32(
5836 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5837 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005838 }
5839}
5840
5841static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5842 struct hwrm_port_phy_cfg_input *req)
5843{
5844 u8 autoneg = bp->link_info.autoneg;
5845 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005846 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005847
5848 if (autoneg & BNXT_AUTONEG_SPEED) {
5849 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005850 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005851
5852 req->enables |= cpu_to_le32(
5853 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5854 req->auto_link_speed_mask = cpu_to_le16(advertising);
5855
5856 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5857 req->flags |=
5858 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5859 } else {
5860 req->force_link_speed = cpu_to_le16(fw_link_speed);
5861 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5862 }
5863
Michael Chanc0c050c2015-10-22 16:01:17 -04005864 /* tell chimp that the setting takes effect immediately */
5865 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5866}
5867
5868int bnxt_hwrm_set_pause(struct bnxt *bp)
5869{
5870 struct hwrm_port_phy_cfg_input req = {0};
5871 int rc;
5872
5873 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5874 bnxt_hwrm_set_pause_common(bp, &req);
5875
5876 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5877 bp->link_info.force_link_chng)
5878 bnxt_hwrm_set_link_common(bp, &req);
5879
5880 mutex_lock(&bp->hwrm_cmd_lock);
5881 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5882 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5883 /* since changing of pause setting doesn't trigger any link
5884 * change event, the driver needs to update the current pause
5885 * result upon successfully return of the phy_cfg command
5886 */
5887 bp->link_info.pause =
5888 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5889 bp->link_info.auto_pause_setting = 0;
5890 if (!bp->link_info.force_link_chng)
5891 bnxt_report_link(bp);
5892 }
5893 bp->link_info.force_link_chng = false;
5894 mutex_unlock(&bp->hwrm_cmd_lock);
5895 return rc;
5896}
5897
Michael Chan939f7f02016-04-05 14:08:58 -04005898static void bnxt_hwrm_set_eee(struct bnxt *bp,
5899 struct hwrm_port_phy_cfg_input *req)
5900{
5901 struct ethtool_eee *eee = &bp->eee;
5902
5903 if (eee->eee_enabled) {
5904 u16 eee_speeds;
5905 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5906
5907 if (eee->tx_lpi_enabled)
5908 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5909 else
5910 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5911
5912 req->flags |= cpu_to_le32(flags);
5913 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5914 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5915 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5916 } else {
5917 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5918 }
5919}
5920
5921int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005922{
5923 struct hwrm_port_phy_cfg_input req = {0};
5924
5925 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5926 if (set_pause)
5927 bnxt_hwrm_set_pause_common(bp, &req);
5928
5929 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005930
5931 if (set_eee)
5932 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005933 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5934}
5935
Michael Chan33f7d552016-04-11 04:11:12 -04005936static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5937{
5938 struct hwrm_port_phy_cfg_input req = {0};
5939
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005940 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005941 return 0;
5942
5943 if (pci_num_vf(bp->pdev))
5944 return 0;
5945
5946 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005947 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005948 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5949}
5950
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005951static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5952{
5953 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5954 struct hwrm_port_led_qcaps_input req = {0};
5955 struct bnxt_pf_info *pf = &bp->pf;
5956 int rc;
5957
5958 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5959 return 0;
5960
5961 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5962 req.port_id = cpu_to_le16(pf->port_id);
5963 mutex_lock(&bp->hwrm_cmd_lock);
5964 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5965 if (rc) {
5966 mutex_unlock(&bp->hwrm_cmd_lock);
5967 return rc;
5968 }
5969 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5970 int i;
5971
5972 bp->num_leds = resp->num_leds;
5973 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5974 bp->num_leds);
5975 for (i = 0; i < bp->num_leds; i++) {
5976 struct bnxt_led_info *led = &bp->leds[i];
5977 __le16 caps = led->led_state_caps;
5978
5979 if (!led->led_group_id ||
5980 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5981 bp->num_leds = 0;
5982 break;
5983 }
5984 }
5985 }
5986 mutex_unlock(&bp->hwrm_cmd_lock);
5987 return 0;
5988}
5989
Michael Chan5282db62017-04-04 18:14:10 -04005990int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
5991{
5992 struct hwrm_wol_filter_alloc_input req = {0};
5993 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5994 int rc;
5995
5996 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
5997 req.port_id = cpu_to_le16(bp->pf.port_id);
5998 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
5999 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6000 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6001 mutex_lock(&bp->hwrm_cmd_lock);
6002 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6003 if (!rc)
6004 bp->wol_filter_id = resp->wol_filter_id;
6005 mutex_unlock(&bp->hwrm_cmd_lock);
6006 return rc;
6007}
6008
6009int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6010{
6011 struct hwrm_wol_filter_free_input req = {0};
6012 int rc;
6013
6014 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6015 req.port_id = cpu_to_le16(bp->pf.port_id);
6016 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6017 req.wol_filter_id = bp->wol_filter_id;
6018 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6019 return rc;
6020}
6021
Michael Chanc1ef1462017-04-04 18:14:07 -04006022static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6023{
6024 struct hwrm_wol_filter_qcfg_input req = {0};
6025 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6026 u16 next_handle = 0;
6027 int rc;
6028
6029 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6030 req.port_id = cpu_to_le16(bp->pf.port_id);
6031 req.handle = cpu_to_le16(handle);
6032 mutex_lock(&bp->hwrm_cmd_lock);
6033 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6034 if (!rc) {
6035 next_handle = le16_to_cpu(resp->next_handle);
6036 if (next_handle != 0) {
6037 if (resp->wol_type ==
6038 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6039 bp->wol = 1;
6040 bp->wol_filter_id = resp->wol_filter_id;
6041 }
6042 }
6043 }
6044 mutex_unlock(&bp->hwrm_cmd_lock);
6045 return next_handle;
6046}
6047
6048static void bnxt_get_wol_settings(struct bnxt *bp)
6049{
6050 u16 handle = 0;
6051
6052 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6053 return;
6054
6055 do {
6056 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6057 } while (handle && handle != 0xffff);
6058}
6059
Michael Chan939f7f02016-04-05 14:08:58 -04006060static bool bnxt_eee_config_ok(struct bnxt *bp)
6061{
6062 struct ethtool_eee *eee = &bp->eee;
6063 struct bnxt_link_info *link_info = &bp->link_info;
6064
6065 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6066 return true;
6067
6068 if (eee->eee_enabled) {
6069 u32 advertising =
6070 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6071
6072 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6073 eee->eee_enabled = 0;
6074 return false;
6075 }
6076 if (eee->advertised & ~advertising) {
6077 eee->advertised = advertising & eee->supported;
6078 return false;
6079 }
6080 }
6081 return true;
6082}
6083
Michael Chanc0c050c2015-10-22 16:01:17 -04006084static int bnxt_update_phy_setting(struct bnxt *bp)
6085{
6086 int rc;
6087 bool update_link = false;
6088 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006089 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006090 struct bnxt_link_info *link_info = &bp->link_info;
6091
6092 rc = bnxt_update_link(bp, true);
6093 if (rc) {
6094 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6095 rc);
6096 return rc;
6097 }
Michael Chan33dac242017-02-12 19:18:15 -05006098 if (!BNXT_SINGLE_PF(bp))
6099 return 0;
6100
Michael Chanc0c050c2015-10-22 16:01:17 -04006101 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006102 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6103 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006104 update_pause = true;
6105 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6106 link_info->force_pause_setting != link_info->req_flow_ctrl)
6107 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006108 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6109 if (BNXT_AUTO_MODE(link_info->auto_mode))
6110 update_link = true;
6111 if (link_info->req_link_speed != link_info->force_link_speed)
6112 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006113 if (link_info->req_duplex != link_info->duplex_setting)
6114 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006115 } else {
6116 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6117 update_link = true;
6118 if (link_info->advertising != link_info->auto_link_speeds)
6119 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006120 }
6121
Michael Chan16d663a2016-11-16 21:13:07 -05006122 /* The last close may have shutdown the link, so need to call
6123 * PHY_CFG to bring it back up.
6124 */
6125 if (!netif_carrier_ok(bp->dev))
6126 update_link = true;
6127
Michael Chan939f7f02016-04-05 14:08:58 -04006128 if (!bnxt_eee_config_ok(bp))
6129 update_eee = true;
6130
Michael Chanc0c050c2015-10-22 16:01:17 -04006131 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006132 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006133 else if (update_pause)
6134 rc = bnxt_hwrm_set_pause(bp);
6135 if (rc) {
6136 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6137 rc);
6138 return rc;
6139 }
6140
6141 return rc;
6142}
6143
Jeffrey Huang11809492015-11-05 16:25:49 -05006144/* Common routine to pre-map certain register block to different GRC window.
6145 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6146 * in PF and 3 windows in VF that can be customized to map in different
6147 * register blocks.
6148 */
6149static void bnxt_preset_reg_win(struct bnxt *bp)
6150{
6151 if (BNXT_PF(bp)) {
6152 /* CAG registers map to GRC window #4 */
6153 writel(BNXT_CAG_REG_BASE,
6154 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6155 }
6156}
6157
Michael Chanc0c050c2015-10-22 16:01:17 -04006158static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6159{
6160 int rc = 0;
6161
Jeffrey Huang11809492015-11-05 16:25:49 -05006162 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006163 netif_carrier_off(bp->dev);
6164 if (irq_re_init) {
6165 rc = bnxt_setup_int_mode(bp);
6166 if (rc) {
6167 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6168 rc);
6169 return rc;
6170 }
6171 }
6172 if ((bp->flags & BNXT_FLAG_RFS) &&
6173 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6174 /* disable RFS if falling back to INTA */
6175 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6176 bp->flags &= ~BNXT_FLAG_RFS;
6177 }
6178
6179 rc = bnxt_alloc_mem(bp, irq_re_init);
6180 if (rc) {
6181 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6182 goto open_err_free_mem;
6183 }
6184
6185 if (irq_re_init) {
6186 bnxt_init_napi(bp);
6187 rc = bnxt_request_irq(bp);
6188 if (rc) {
6189 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6190 goto open_err;
6191 }
6192 }
6193
6194 bnxt_enable_napi(bp);
6195
6196 rc = bnxt_init_nic(bp, irq_re_init);
6197 if (rc) {
6198 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6199 goto open_err;
6200 }
6201
6202 if (link_re_init) {
6203 rc = bnxt_update_phy_setting(bp);
6204 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006205 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006206 }
6207
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006208 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006209 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006210
Michael Chancaefe522015-12-09 19:35:42 -05006211 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006212 bnxt_enable_int(bp);
6213 /* Enable TX queues */
6214 bnxt_tx_enable(bp);
6215 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006216 /* Poll link status and check for SFP+ module status */
6217 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006218
6219 return 0;
6220
6221open_err:
6222 bnxt_disable_napi(bp);
6223 bnxt_del_napi(bp);
6224
6225open_err_free_mem:
6226 bnxt_free_skbs(bp);
6227 bnxt_free_irq(bp);
6228 bnxt_free_mem(bp, true);
6229 return rc;
6230}
6231
6232/* rtnl_lock held */
6233int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6234{
6235 int rc = 0;
6236
6237 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6238 if (rc) {
6239 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6240 dev_close(bp->dev);
6241 }
6242 return rc;
6243}
6244
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006245/* rtnl_lock held, open the NIC half way by allocating all resources, but
6246 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6247 * self tests.
6248 */
6249int bnxt_half_open_nic(struct bnxt *bp)
6250{
6251 int rc = 0;
6252
6253 rc = bnxt_alloc_mem(bp, false);
6254 if (rc) {
6255 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6256 goto half_open_err;
6257 }
6258 rc = bnxt_init_nic(bp, false);
6259 if (rc) {
6260 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6261 goto half_open_err;
6262 }
6263 return 0;
6264
6265half_open_err:
6266 bnxt_free_skbs(bp);
6267 bnxt_free_mem(bp, false);
6268 dev_close(bp->dev);
6269 return rc;
6270}
6271
6272/* rtnl_lock held, this call can only be made after a previous successful
6273 * call to bnxt_half_open_nic().
6274 */
6275void bnxt_half_close_nic(struct bnxt *bp)
6276{
6277 bnxt_hwrm_resource_free(bp, false, false);
6278 bnxt_free_skbs(bp);
6279 bnxt_free_mem(bp, false);
6280}
6281
Michael Chanc0c050c2015-10-22 16:01:17 -04006282static int bnxt_open(struct net_device *dev)
6283{
6284 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006285
Michael Chanc0c050c2015-10-22 16:01:17 -04006286 return __bnxt_open_nic(bp, true, true);
6287}
6288
Michael Chanf9b76eb2017-07-11 13:05:34 -04006289static bool bnxt_drv_busy(struct bnxt *bp)
6290{
6291 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6292 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6293}
6294
Michael Chanc0c050c2015-10-22 16:01:17 -04006295int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6296{
6297 int rc = 0;
6298
6299#ifdef CONFIG_BNXT_SRIOV
6300 if (bp->sriov_cfg) {
6301 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6302 !bp->sriov_cfg,
6303 BNXT_SRIOV_CFG_WAIT_TMO);
6304 if (rc)
6305 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6306 }
6307#endif
6308 /* Change device state to avoid TX queue wake up's */
6309 bnxt_tx_disable(bp);
6310
Michael Chancaefe522015-12-09 19:35:42 -05006311 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006312 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006313 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006314 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006315
Michael Chan9d8bc092016-12-29 12:13:33 -05006316 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006317 bnxt_shutdown_nic(bp, irq_re_init);
6318
6319 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6320
6321 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006322 del_timer_sync(&bp->timer);
6323 bnxt_free_skbs(bp);
6324
6325 if (irq_re_init) {
6326 bnxt_free_irq(bp);
6327 bnxt_del_napi(bp);
6328 }
6329 bnxt_free_mem(bp, irq_re_init);
6330 return rc;
6331}
6332
6333static int bnxt_close(struct net_device *dev)
6334{
6335 struct bnxt *bp = netdev_priv(dev);
6336
6337 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006338 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006339 return 0;
6340}
6341
6342/* rtnl_lock held */
6343static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6344{
6345 switch (cmd) {
6346 case SIOCGMIIPHY:
6347 /* fallthru */
6348 case SIOCGMIIREG: {
6349 if (!netif_running(dev))
6350 return -EAGAIN;
6351
6352 return 0;
6353 }
6354
6355 case SIOCSMIIREG:
6356 if (!netif_running(dev))
6357 return -EAGAIN;
6358
6359 return 0;
6360
6361 default:
6362 /* do nothing */
6363 break;
6364 }
6365 return -EOPNOTSUPP;
6366}
6367
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006368static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006369bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6370{
6371 u32 i;
6372 struct bnxt *bp = netdev_priv(dev);
6373
Michael Chanf9b76eb2017-07-11 13:05:34 -04006374 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6375 /* Make sure bnxt_close_nic() sees that we are reading stats before
6376 * we check the BNXT_STATE_OPEN flag.
6377 */
6378 smp_mb__after_atomic();
6379 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6380 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006381 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006382 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006383
6384 /* TODO check if we need to synchronize with bnxt_close path */
6385 for (i = 0; i < bp->cp_nr_rings; i++) {
6386 struct bnxt_napi *bnapi = bp->bnapi[i];
6387 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6388 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6389
6390 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6391 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6392 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6393
6394 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6395 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6396 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6397
6398 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6399 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6400 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6401
6402 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6403 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6404 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6405
6406 stats->rx_missed_errors +=
6407 le64_to_cpu(hw_stats->rx_discard_pkts);
6408
6409 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6410
Michael Chanc0c050c2015-10-22 16:01:17 -04006411 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6412 }
6413
Michael Chan9947f832016-03-07 15:38:46 -05006414 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6415 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6416 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6417
6418 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6419 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6420 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6421 le64_to_cpu(rx->rx_ovrsz_frames) +
6422 le64_to_cpu(rx->rx_runt_frames);
6423 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6424 le64_to_cpu(rx->rx_jbr_frames);
6425 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6426 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6427 stats->tx_errors = le64_to_cpu(tx->tx_err);
6428 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006429 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006430}
6431
6432static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6433{
6434 struct net_device *dev = bp->dev;
6435 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6436 struct netdev_hw_addr *ha;
6437 u8 *haddr;
6438 int mc_count = 0;
6439 bool update = false;
6440 int off = 0;
6441
6442 netdev_for_each_mc_addr(ha, dev) {
6443 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6444 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6445 vnic->mc_list_count = 0;
6446 return false;
6447 }
6448 haddr = ha->addr;
6449 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6450 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6451 update = true;
6452 }
6453 off += ETH_ALEN;
6454 mc_count++;
6455 }
6456 if (mc_count)
6457 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6458
6459 if (mc_count != vnic->mc_list_count) {
6460 vnic->mc_list_count = mc_count;
6461 update = true;
6462 }
6463 return update;
6464}
6465
6466static bool bnxt_uc_list_updated(struct bnxt *bp)
6467{
6468 struct net_device *dev = bp->dev;
6469 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6470 struct netdev_hw_addr *ha;
6471 int off = 0;
6472
6473 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6474 return true;
6475
6476 netdev_for_each_uc_addr(ha, dev) {
6477 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6478 return true;
6479
6480 off += ETH_ALEN;
6481 }
6482 return false;
6483}
6484
6485static void bnxt_set_rx_mode(struct net_device *dev)
6486{
6487 struct bnxt *bp = netdev_priv(dev);
6488 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6489 u32 mask = vnic->rx_mask;
6490 bool mc_update = false;
6491 bool uc_update;
6492
6493 if (!netif_running(dev))
6494 return;
6495
6496 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6497 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6498 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6499
Michael Chan17c71ac2016-07-01 18:46:27 -04006500 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006501 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6502
6503 uc_update = bnxt_uc_list_updated(bp);
6504
6505 if (dev->flags & IFF_ALLMULTI) {
6506 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6507 vnic->mc_list_count = 0;
6508 } else {
6509 mc_update = bnxt_mc_list_updated(bp, &mask);
6510 }
6511
6512 if (mask != vnic->rx_mask || uc_update || mc_update) {
6513 vnic->rx_mask = mask;
6514
6515 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6516 schedule_work(&bp->sp_task);
6517 }
6518}
6519
Michael Chanb664f002015-12-02 01:54:08 -05006520static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006521{
6522 struct net_device *dev = bp->dev;
6523 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6524 struct netdev_hw_addr *ha;
6525 int i, off = 0, rc;
6526 bool uc_update;
6527
6528 netif_addr_lock_bh(dev);
6529 uc_update = bnxt_uc_list_updated(bp);
6530 netif_addr_unlock_bh(dev);
6531
6532 if (!uc_update)
6533 goto skip_uc;
6534
6535 mutex_lock(&bp->hwrm_cmd_lock);
6536 for (i = 1; i < vnic->uc_filter_count; i++) {
6537 struct hwrm_cfa_l2_filter_free_input req = {0};
6538
6539 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6540 -1);
6541
6542 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6543
6544 rc = _hwrm_send_message(bp, &req, sizeof(req),
6545 HWRM_CMD_TIMEOUT);
6546 }
6547 mutex_unlock(&bp->hwrm_cmd_lock);
6548
6549 vnic->uc_filter_count = 1;
6550
6551 netif_addr_lock_bh(dev);
6552 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6553 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6554 } else {
6555 netdev_for_each_uc_addr(ha, dev) {
6556 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6557 off += ETH_ALEN;
6558 vnic->uc_filter_count++;
6559 }
6560 }
6561 netif_addr_unlock_bh(dev);
6562
6563 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6564 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6565 if (rc) {
6566 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6567 rc);
6568 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006569 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006570 }
6571 }
6572
6573skip_uc:
6574 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6575 if (rc)
6576 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6577 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006578
6579 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006580}
6581
Michael Chan8079e8f2016-12-29 12:13:37 -05006582/* If the chip and firmware supports RFS */
6583static bool bnxt_rfs_supported(struct bnxt *bp)
6584{
6585 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6586 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006587 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6588 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006589 return false;
6590}
6591
6592/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006593static bool bnxt_rfs_capable(struct bnxt *bp)
6594{
6595#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006596 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006597
Michael Chan964fd482017-02-12 19:18:13 -05006598 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006599 return false;
6600
6601 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006602 max_vnics = bnxt_get_max_func_vnics(bp);
6603 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006604
6605 /* RSS contexts not a limiting factor */
6606 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6607 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006608 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006609 netdev_warn(bp->dev,
6610 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006611 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006612 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006613 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006614
6615 return true;
6616#else
6617 return false;
6618#endif
6619}
6620
Michael Chanc0c050c2015-10-22 16:01:17 -04006621static netdev_features_t bnxt_fix_features(struct net_device *dev,
6622 netdev_features_t features)
6623{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006624 struct bnxt *bp = netdev_priv(dev);
6625
Vasundhara Volama2304902016-07-25 12:33:36 -04006626 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006627 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006628
6629 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6630 * turned on or off together.
6631 */
6632 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6633 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6634 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6635 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6636 NETIF_F_HW_VLAN_STAG_RX);
6637 else
6638 features |= NETIF_F_HW_VLAN_CTAG_RX |
6639 NETIF_F_HW_VLAN_STAG_RX;
6640 }
Michael Chancf6645f2016-06-13 02:25:28 -04006641#ifdef CONFIG_BNXT_SRIOV
6642 if (BNXT_VF(bp)) {
6643 if (bp->vf.vlan) {
6644 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6645 NETIF_F_HW_VLAN_STAG_RX);
6646 }
6647 }
6648#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006649 return features;
6650}
6651
6652static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6653{
6654 struct bnxt *bp = netdev_priv(dev);
6655 u32 flags = bp->flags;
6656 u32 changes;
6657 int rc = 0;
6658 bool re_init = false;
6659 bool update_tpa = false;
6660
6661 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006662 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006663 flags |= BNXT_FLAG_GRO;
6664 if (features & NETIF_F_LRO)
6665 flags |= BNXT_FLAG_LRO;
6666
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006667 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6668 flags &= ~BNXT_FLAG_TPA;
6669
Michael Chanc0c050c2015-10-22 16:01:17 -04006670 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6671 flags |= BNXT_FLAG_STRIP_VLAN;
6672
6673 if (features & NETIF_F_NTUPLE)
6674 flags |= BNXT_FLAG_RFS;
6675
6676 changes = flags ^ bp->flags;
6677 if (changes & BNXT_FLAG_TPA) {
6678 update_tpa = true;
6679 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6680 (flags & BNXT_FLAG_TPA) == 0)
6681 re_init = true;
6682 }
6683
6684 if (changes & ~BNXT_FLAG_TPA)
6685 re_init = true;
6686
6687 if (flags != bp->flags) {
6688 u32 old_flags = bp->flags;
6689
6690 bp->flags = flags;
6691
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006692 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006693 if (update_tpa)
6694 bnxt_set_ring_params(bp);
6695 return rc;
6696 }
6697
6698 if (re_init) {
6699 bnxt_close_nic(bp, false, false);
6700 if (update_tpa)
6701 bnxt_set_ring_params(bp);
6702
6703 return bnxt_open_nic(bp, false, false);
6704 }
6705 if (update_tpa) {
6706 rc = bnxt_set_tpa(bp,
6707 (flags & BNXT_FLAG_TPA) ?
6708 true : false);
6709 if (rc)
6710 bp->flags = old_flags;
6711 }
6712 }
6713 return rc;
6714}
6715
Michael Chan9f554592016-01-02 23:44:58 -05006716static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6717{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006718 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006719 int i = bnapi->index;
6720
Michael Chan3b2b7d92016-01-02 23:45:00 -05006721 if (!txr)
6722 return;
6723
Michael Chan9f554592016-01-02 23:44:58 -05006724 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6725 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6726 txr->tx_cons);
6727}
6728
6729static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6730{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006731 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006732 int i = bnapi->index;
6733
Michael Chan3b2b7d92016-01-02 23:45:00 -05006734 if (!rxr)
6735 return;
6736
Michael Chan9f554592016-01-02 23:44:58 -05006737 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",
6738 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6739 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6740 rxr->rx_sw_agg_prod);
6741}
6742
6743static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6744{
6745 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6746 int i = bnapi->index;
6747
6748 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6749 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6750}
6751
Michael Chanc0c050c2015-10-22 16:01:17 -04006752static void bnxt_dbg_dump_states(struct bnxt *bp)
6753{
6754 int i;
6755 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006756
6757 for (i = 0; i < bp->cp_nr_rings; i++) {
6758 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006759 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006760 bnxt_dump_tx_sw_state(bnapi);
6761 bnxt_dump_rx_sw_state(bnapi);
6762 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006763 }
6764 }
6765}
6766
Michael Chan6988bd92016-06-13 02:25:29 -04006767static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006768{
Michael Chan6988bd92016-06-13 02:25:29 -04006769 if (!silent)
6770 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006771 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006772 int rc;
6773
6774 if (!silent)
6775 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006776 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006777 rc = bnxt_open_nic(bp, false, false);
6778 if (!silent && !rc)
6779 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006780 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006781}
6782
6783static void bnxt_tx_timeout(struct net_device *dev)
6784{
6785 struct bnxt *bp = netdev_priv(dev);
6786
6787 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6788 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6789 schedule_work(&bp->sp_task);
6790}
6791
6792#ifdef CONFIG_NET_POLL_CONTROLLER
6793static void bnxt_poll_controller(struct net_device *dev)
6794{
6795 struct bnxt *bp = netdev_priv(dev);
6796 int i;
6797
Michael Chan2270bc52017-06-23 14:01:01 -04006798 /* Only process tx rings/combined rings in netpoll mode. */
6799 for (i = 0; i < bp->tx_nr_rings; i++) {
6800 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006801
Michael Chan2270bc52017-06-23 14:01:01 -04006802 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006803 }
6804}
6805#endif
6806
6807static void bnxt_timer(unsigned long data)
6808{
6809 struct bnxt *bp = (struct bnxt *)data;
6810 struct net_device *dev = bp->dev;
6811
6812 if (!netif_running(dev))
6813 return;
6814
6815 if (atomic_read(&bp->intr_sem) != 0)
6816 goto bnxt_restart_timer;
6817
Michael Chan3bdf56c2016-03-07 15:38:45 -05006818 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6819 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6820 schedule_work(&bp->sp_task);
6821 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006822bnxt_restart_timer:
6823 mod_timer(&bp->timer, jiffies + bp->current_interval);
6824}
6825
Michael Chana551ee92017-01-25 02:55:07 -05006826static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006827{
Michael Chana551ee92017-01-25 02:55:07 -05006828 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6829 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006830 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6831 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6832 */
6833 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6834 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006835}
6836
6837static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6838{
Michael Chan6988bd92016-06-13 02:25:29 -04006839 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6840 rtnl_unlock();
6841}
6842
Michael Chana551ee92017-01-25 02:55:07 -05006843/* Only called from bnxt_sp_task() */
6844static void bnxt_reset(struct bnxt *bp, bool silent)
6845{
6846 bnxt_rtnl_lock_sp(bp);
6847 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6848 bnxt_reset_task(bp, silent);
6849 bnxt_rtnl_unlock_sp(bp);
6850}
6851
Michael Chanc0c050c2015-10-22 16:01:17 -04006852static void bnxt_cfg_ntp_filters(struct bnxt *);
6853
6854static void bnxt_sp_task(struct work_struct *work)
6855{
6856 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006857
Michael Chan4cebdce2015-12-09 19:35:43 -05006858 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6859 smp_mb__after_atomic();
6860 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6861 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006862 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006863 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006864
6865 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6866 bnxt_cfg_rx_mode(bp);
6867
6868 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6869 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006870 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6871 bnxt_hwrm_exec_fwd_req(bp);
6872 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6873 bnxt_hwrm_tunnel_dst_port_alloc(
6874 bp, bp->vxlan_port,
6875 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6876 }
6877 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6878 bnxt_hwrm_tunnel_dst_port_free(
6879 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6880 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006881 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6882 bnxt_hwrm_tunnel_dst_port_alloc(
6883 bp, bp->nge_port,
6884 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6885 }
6886 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6887 bnxt_hwrm_tunnel_dst_port_free(
6888 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6889 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006890 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6891 bnxt_hwrm_port_qstats(bp);
6892
Michael Chana551ee92017-01-25 02:55:07 -05006893 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6894 * must be the last functions to be called before exiting.
6895 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006896 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6897 int rc = 0;
6898
6899 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6900 &bp->sp_event))
6901 bnxt_hwrm_phy_qcaps(bp);
6902
6903 bnxt_rtnl_lock_sp(bp);
6904 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6905 rc = bnxt_update_link(bp, true);
6906 bnxt_rtnl_unlock_sp(bp);
6907 if (rc)
6908 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6909 rc);
6910 }
Michael Chan90c694b2017-01-25 02:55:09 -05006911 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6912 bnxt_rtnl_lock_sp(bp);
6913 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6914 bnxt_get_port_module_status(bp);
6915 bnxt_rtnl_unlock_sp(bp);
6916 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006917 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6918 bnxt_reset(bp, false);
6919
6920 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6921 bnxt_reset(bp, true);
6922
Michael Chanc0c050c2015-10-22 16:01:17 -04006923 smp_mb__before_atomic();
6924 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6925}
6926
Michael Chand1e79252017-02-06 16:55:38 -05006927/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04006928int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
6929 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006930{
6931 int max_rx, max_tx, tx_sets = 1;
6932 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05006933 int rc;
6934
Michael Chand1e79252017-02-06 16:55:38 -05006935 if (tcs)
6936 tx_sets = tcs;
6937
6938 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6939 if (rc)
6940 return rc;
6941
6942 if (max_rx < rx)
6943 return -ENOMEM;
6944
Michael Chan5f449242017-02-06 16:55:40 -05006945 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006946 if (max_tx < tx_rings_needed)
6947 return -ENOMEM;
6948
6949 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006950 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006951 return -ENOMEM;
6952 return 0;
6953}
6954
Sathya Perla17086392017-02-20 19:25:18 -05006955static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6956{
6957 if (bp->bar2) {
6958 pci_iounmap(pdev, bp->bar2);
6959 bp->bar2 = NULL;
6960 }
6961
6962 if (bp->bar1) {
6963 pci_iounmap(pdev, bp->bar1);
6964 bp->bar1 = NULL;
6965 }
6966
6967 if (bp->bar0) {
6968 pci_iounmap(pdev, bp->bar0);
6969 bp->bar0 = NULL;
6970 }
6971}
6972
6973static void bnxt_cleanup_pci(struct bnxt *bp)
6974{
6975 bnxt_unmap_bars(bp, bp->pdev);
6976 pci_release_regions(bp->pdev);
6977 pci_disable_device(bp->pdev);
6978}
6979
Michael Chanc0c050c2015-10-22 16:01:17 -04006980static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6981{
6982 int rc;
6983 struct bnxt *bp = netdev_priv(dev);
6984
6985 SET_NETDEV_DEV(dev, &pdev->dev);
6986
6987 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6988 rc = pci_enable_device(pdev);
6989 if (rc) {
6990 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6991 goto init_err;
6992 }
6993
6994 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6995 dev_err(&pdev->dev,
6996 "Cannot find PCI device base address, aborting\n");
6997 rc = -ENODEV;
6998 goto init_err_disable;
6999 }
7000
7001 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7002 if (rc) {
7003 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7004 goto init_err_disable;
7005 }
7006
7007 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7008 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7009 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7010 goto init_err_disable;
7011 }
7012
7013 pci_set_master(pdev);
7014
7015 bp->dev = dev;
7016 bp->pdev = pdev;
7017
7018 bp->bar0 = pci_ioremap_bar(pdev, 0);
7019 if (!bp->bar0) {
7020 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7021 rc = -ENOMEM;
7022 goto init_err_release;
7023 }
7024
7025 bp->bar1 = pci_ioremap_bar(pdev, 2);
7026 if (!bp->bar1) {
7027 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7028 rc = -ENOMEM;
7029 goto init_err_release;
7030 }
7031
7032 bp->bar2 = pci_ioremap_bar(pdev, 4);
7033 if (!bp->bar2) {
7034 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7035 rc = -ENOMEM;
7036 goto init_err_release;
7037 }
7038
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007039 pci_enable_pcie_error_reporting(pdev);
7040
Michael Chanc0c050c2015-10-22 16:01:17 -04007041 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7042
7043 spin_lock_init(&bp->ntp_fltr_lock);
7044
7045 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7046 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7047
Michael Chandfb5b892016-02-26 04:00:01 -05007048 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007049 bp->rx_coal_ticks = 12;
7050 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007051 bp->rx_coal_ticks_irq = 1;
7052 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007053
Michael Chandfc9c942016-02-26 04:00:03 -05007054 bp->tx_coal_ticks = 25;
7055 bp->tx_coal_bufs = 30;
7056 bp->tx_coal_ticks_irq = 2;
7057 bp->tx_coal_bufs_irq = 2;
7058
Michael Chan51f30782016-07-01 18:46:29 -04007059 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7060
Michael Chanc0c050c2015-10-22 16:01:17 -04007061 init_timer(&bp->timer);
7062 bp->timer.data = (unsigned long)bp;
7063 bp->timer.function = bnxt_timer;
7064 bp->current_interval = BNXT_TIMER_INTERVAL;
7065
Michael Chancaefe522015-12-09 19:35:42 -05007066 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007067 return 0;
7068
7069init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007070 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007071 pci_release_regions(pdev);
7072
7073init_err_disable:
7074 pci_disable_device(pdev);
7075
7076init_err:
7077 return rc;
7078}
7079
7080/* rtnl_lock held */
7081static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7082{
7083 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007084 struct bnxt *bp = netdev_priv(dev);
7085 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007086
7087 if (!is_valid_ether_addr(addr->sa_data))
7088 return -EADDRNOTAVAIL;
7089
Michael Chan84c33dd2016-04-11 04:11:13 -04007090 rc = bnxt_approve_mac(bp, addr->sa_data);
7091 if (rc)
7092 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007093
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007094 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7095 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007096
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007097 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7098 if (netif_running(dev)) {
7099 bnxt_close_nic(bp, false, false);
7100 rc = bnxt_open_nic(bp, false, false);
7101 }
7102
7103 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007104}
7105
7106/* rtnl_lock held */
7107static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7108{
7109 struct bnxt *bp = netdev_priv(dev);
7110
Michael Chanc0c050c2015-10-22 16:01:17 -04007111 if (netif_running(dev))
7112 bnxt_close_nic(bp, false, false);
7113
7114 dev->mtu = new_mtu;
7115 bnxt_set_ring_params(bp);
7116
7117 if (netif_running(dev))
7118 return bnxt_open_nic(bp, false, false);
7119
7120 return 0;
7121}
7122
Michael Chanc5e3deb2016-12-02 21:17:15 -05007123int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007124{
7125 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007126 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007127 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007128
Michael Chanc0c050c2015-10-22 16:01:17 -04007129 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007130 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007131 tc, bp->max_tc);
7132 return -EINVAL;
7133 }
7134
7135 if (netdev_get_num_tc(dev) == tc)
7136 return 0;
7137
Michael Chan3ffb6a32016-11-11 00:11:42 -05007138 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7139 sh = true;
7140
Michael Chan5f449242017-02-06 16:55:40 -05007141 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007142 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007143 if (rc)
7144 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007145
7146 /* Needs to close the device and do hw resource re-allocations */
7147 if (netif_running(bp->dev))
7148 bnxt_close_nic(bp, true, false);
7149
7150 if (tc) {
7151 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7152 netdev_set_num_tc(dev, tc);
7153 } else {
7154 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7155 netdev_reset_tc(dev);
7156 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007157 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7158 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007159 bp->num_stat_ctxs = bp->cp_nr_rings;
7160
7161 if (netif_running(bp->dev))
7162 return bnxt_open_nic(bp, true, false);
7163
7164 return 0;
7165}
7166
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007167static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7168 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007169{
7170 if (ntc->type != TC_SETUP_MQPRIO)
7171 return -EINVAL;
7172
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007173 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7174
7175 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007176}
7177
Michael Chanc0c050c2015-10-22 16:01:17 -04007178#ifdef CONFIG_RFS_ACCEL
7179static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7180 struct bnxt_ntuple_filter *f2)
7181{
7182 struct flow_keys *keys1 = &f1->fkeys;
7183 struct flow_keys *keys2 = &f2->fkeys;
7184
7185 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7186 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7187 keys1->ports.ports == keys2->ports.ports &&
7188 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7189 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007190 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007191 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7192 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007193 return true;
7194
7195 return false;
7196}
7197
7198static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7199 u16 rxq_index, u32 flow_id)
7200{
7201 struct bnxt *bp = netdev_priv(dev);
7202 struct bnxt_ntuple_filter *fltr, *new_fltr;
7203 struct flow_keys *fkeys;
7204 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007205 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007206 struct hlist_head *head;
7207
Michael Chana54c4d72016-07-25 12:33:35 -04007208 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7209 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7210 int off = 0, j;
7211
7212 netif_addr_lock_bh(dev);
7213 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7214 if (ether_addr_equal(eth->h_dest,
7215 vnic->uc_list + off)) {
7216 l2_idx = j + 1;
7217 break;
7218 }
7219 }
7220 netif_addr_unlock_bh(dev);
7221 if (!l2_idx)
7222 return -EINVAL;
7223 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007224 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7225 if (!new_fltr)
7226 return -ENOMEM;
7227
7228 fkeys = &new_fltr->fkeys;
7229 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7230 rc = -EPROTONOSUPPORT;
7231 goto err_free;
7232 }
7233
Michael Chandda0e742016-12-29 12:13:40 -05007234 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7235 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007236 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7237 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7238 rc = -EPROTONOSUPPORT;
7239 goto err_free;
7240 }
Michael Chandda0e742016-12-29 12:13:40 -05007241 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7242 bp->hwrm_spec_code < 0x10601) {
7243 rc = -EPROTONOSUPPORT;
7244 goto err_free;
7245 }
Michael Chan61aad722017-02-12 19:18:14 -05007246 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7247 bp->hwrm_spec_code < 0x10601) {
7248 rc = -EPROTONOSUPPORT;
7249 goto err_free;
7250 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007251
Michael Chana54c4d72016-07-25 12:33:35 -04007252 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007253 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7254
7255 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7256 head = &bp->ntp_fltr_hash_tbl[idx];
7257 rcu_read_lock();
7258 hlist_for_each_entry_rcu(fltr, head, hash) {
7259 if (bnxt_fltr_match(fltr, new_fltr)) {
7260 rcu_read_unlock();
7261 rc = 0;
7262 goto err_free;
7263 }
7264 }
7265 rcu_read_unlock();
7266
7267 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007268 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7269 BNXT_NTP_FLTR_MAX_FLTR, 0);
7270 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007271 spin_unlock_bh(&bp->ntp_fltr_lock);
7272 rc = -ENOMEM;
7273 goto err_free;
7274 }
7275
Michael Chan84e86b92015-11-05 16:25:50 -05007276 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007277 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007278 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007279 new_fltr->rxq = rxq_index;
7280 hlist_add_head_rcu(&new_fltr->hash, head);
7281 bp->ntp_fltr_count++;
7282 spin_unlock_bh(&bp->ntp_fltr_lock);
7283
7284 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7285 schedule_work(&bp->sp_task);
7286
7287 return new_fltr->sw_id;
7288
7289err_free:
7290 kfree(new_fltr);
7291 return rc;
7292}
7293
7294static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7295{
7296 int i;
7297
7298 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7299 struct hlist_head *head;
7300 struct hlist_node *tmp;
7301 struct bnxt_ntuple_filter *fltr;
7302 int rc;
7303
7304 head = &bp->ntp_fltr_hash_tbl[i];
7305 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7306 bool del = false;
7307
7308 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7309 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7310 fltr->flow_id,
7311 fltr->sw_id)) {
7312 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7313 fltr);
7314 del = true;
7315 }
7316 } else {
7317 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7318 fltr);
7319 if (rc)
7320 del = true;
7321 else
7322 set_bit(BNXT_FLTR_VALID, &fltr->state);
7323 }
7324
7325 if (del) {
7326 spin_lock_bh(&bp->ntp_fltr_lock);
7327 hlist_del_rcu(&fltr->hash);
7328 bp->ntp_fltr_count--;
7329 spin_unlock_bh(&bp->ntp_fltr_lock);
7330 synchronize_rcu();
7331 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7332 kfree(fltr);
7333 }
7334 }
7335 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007336 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7337 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007338}
7339
7340#else
7341
7342static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7343{
7344}
7345
7346#endif /* CONFIG_RFS_ACCEL */
7347
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007348static void bnxt_udp_tunnel_add(struct net_device *dev,
7349 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007350{
7351 struct bnxt *bp = netdev_priv(dev);
7352
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007353 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7354 return;
7355
Michael Chanc0c050c2015-10-22 16:01:17 -04007356 if (!netif_running(dev))
7357 return;
7358
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007359 switch (ti->type) {
7360 case UDP_TUNNEL_TYPE_VXLAN:
7361 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7362 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007363
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007364 bp->vxlan_port_cnt++;
7365 if (bp->vxlan_port_cnt == 1) {
7366 bp->vxlan_port = ti->port;
7367 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007368 schedule_work(&bp->sp_task);
7369 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007370 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007371 case UDP_TUNNEL_TYPE_GENEVE:
7372 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7373 return;
7374
7375 bp->nge_port_cnt++;
7376 if (bp->nge_port_cnt == 1) {
7377 bp->nge_port = ti->port;
7378 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7379 }
7380 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007381 default:
7382 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007383 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007384
7385 schedule_work(&bp->sp_task);
7386}
7387
7388static void bnxt_udp_tunnel_del(struct net_device *dev,
7389 struct udp_tunnel_info *ti)
7390{
7391 struct bnxt *bp = netdev_priv(dev);
7392
7393 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7394 return;
7395
7396 if (!netif_running(dev))
7397 return;
7398
7399 switch (ti->type) {
7400 case UDP_TUNNEL_TYPE_VXLAN:
7401 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7402 return;
7403 bp->vxlan_port_cnt--;
7404
7405 if (bp->vxlan_port_cnt != 0)
7406 return;
7407
7408 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7409 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007410 case UDP_TUNNEL_TYPE_GENEVE:
7411 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7412 return;
7413 bp->nge_port_cnt--;
7414
7415 if (bp->nge_port_cnt != 0)
7416 return;
7417
7418 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7419 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007420 default:
7421 return;
7422 }
7423
7424 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007425}
7426
7427static const struct net_device_ops bnxt_netdev_ops = {
7428 .ndo_open = bnxt_open,
7429 .ndo_start_xmit = bnxt_start_xmit,
7430 .ndo_stop = bnxt_close,
7431 .ndo_get_stats64 = bnxt_get_stats64,
7432 .ndo_set_rx_mode = bnxt_set_rx_mode,
7433 .ndo_do_ioctl = bnxt_ioctl,
7434 .ndo_validate_addr = eth_validate_addr,
7435 .ndo_set_mac_address = bnxt_change_mac_addr,
7436 .ndo_change_mtu = bnxt_change_mtu,
7437 .ndo_fix_features = bnxt_fix_features,
7438 .ndo_set_features = bnxt_set_features,
7439 .ndo_tx_timeout = bnxt_tx_timeout,
7440#ifdef CONFIG_BNXT_SRIOV
7441 .ndo_get_vf_config = bnxt_get_vf_config,
7442 .ndo_set_vf_mac = bnxt_set_vf_mac,
7443 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7444 .ndo_set_vf_rate = bnxt_set_vf_bw,
7445 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7446 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7447#endif
7448#ifdef CONFIG_NET_POLL_CONTROLLER
7449 .ndo_poll_controller = bnxt_poll_controller,
7450#endif
7451 .ndo_setup_tc = bnxt_setup_tc,
7452#ifdef CONFIG_RFS_ACCEL
7453 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7454#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007455 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7456 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007457 .ndo_xdp = bnxt_xdp,
Michael Chanc0c050c2015-10-22 16:01:17 -04007458};
7459
7460static void bnxt_remove_one(struct pci_dev *pdev)
7461{
7462 struct net_device *dev = pci_get_drvdata(pdev);
7463 struct bnxt *bp = netdev_priv(dev);
7464
7465 if (BNXT_PF(bp))
7466 bnxt_sriov_disable(bp);
7467
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007468 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007469 unregister_netdev(dev);
7470 cancel_work_sync(&bp->sp_task);
7471 bp->sp_event = 0;
7472
Michael Chan78095922016-12-07 00:26:16 -05007473 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007474 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007475 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007476 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007477 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007478 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007479 kfree(bp->edev);
7480 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007481 if (bp->xdp_prog)
7482 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007483 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007484 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007485}
7486
7487static int bnxt_probe_phy(struct bnxt *bp)
7488{
7489 int rc = 0;
7490 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007491
Michael Chan170ce012016-04-05 14:08:57 -04007492 rc = bnxt_hwrm_phy_qcaps(bp);
7493 if (rc) {
7494 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7495 rc);
7496 return rc;
7497 }
7498
Michael Chanc0c050c2015-10-22 16:01:17 -04007499 rc = bnxt_update_link(bp, false);
7500 if (rc) {
7501 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7502 rc);
7503 return rc;
7504 }
7505
Michael Chan93ed8112016-06-13 02:25:37 -04007506 /* Older firmware does not have supported_auto_speeds, so assume
7507 * that all supported speeds can be autonegotiated.
7508 */
7509 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7510 link_info->support_auto_speeds = link_info->support_speeds;
7511
Michael Chanc0c050c2015-10-22 16:01:17 -04007512 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007513 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007514 link_info->autoneg = BNXT_AUTONEG_SPEED;
7515 if (bp->hwrm_spec_code >= 0x10201) {
7516 if (link_info->auto_pause_setting &
7517 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7518 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7519 } else {
7520 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7521 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007522 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007523 } else {
7524 link_info->req_link_speed = link_info->force_link_speed;
7525 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007526 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007527 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7528 link_info->req_flow_ctrl =
7529 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7530 else
7531 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007532 return rc;
7533}
7534
7535static int bnxt_get_max_irq(struct pci_dev *pdev)
7536{
7537 u16 ctrl;
7538
7539 if (!pdev->msix_cap)
7540 return 1;
7541
7542 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7543 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7544}
7545
Michael Chan6e6c5a52016-01-02 23:45:02 -05007546static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7547 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007548{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007549 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007550
Michael Chan379a80a2015-10-23 15:06:19 -04007551#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007552 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007553 *max_tx = bp->vf.max_tx_rings;
7554 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007555 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7556 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007557 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007558 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007559#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007560 {
7561 *max_tx = bp->pf.max_tx_rings;
7562 *max_rx = bp->pf.max_rx_rings;
7563 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7564 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7565 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007566 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007567 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7568 *max_cp -= 1;
7569 *max_rx -= 2;
7570 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007571 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7572 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007573 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007574}
7575
7576int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7577{
7578 int rx, tx, cp;
7579
7580 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7581 if (!rx || !tx || !cp)
7582 return -ENOMEM;
7583
7584 *max_rx = rx;
7585 *max_tx = tx;
7586 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7587}
7588
Michael Chane4060d32016-12-07 00:26:19 -05007589static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7590 bool shared)
7591{
7592 int rc;
7593
7594 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007595 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7596 /* Not enough rings, try disabling agg rings. */
7597 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7598 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7599 if (rc)
7600 return rc;
7601 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7602 bp->dev->hw_features &= ~NETIF_F_LRO;
7603 bp->dev->features &= ~NETIF_F_LRO;
7604 bnxt_set_ring_params(bp);
7605 }
Michael Chane4060d32016-12-07 00:26:19 -05007606
7607 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7608 int max_cp, max_stat, max_irq;
7609
7610 /* Reserve minimum resources for RoCE */
7611 max_cp = bnxt_get_max_func_cp_rings(bp);
7612 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7613 max_irq = bnxt_get_max_func_irqs(bp);
7614 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7615 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7616 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7617 return 0;
7618
7619 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7620 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7621 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7622 max_cp = min_t(int, max_cp, max_irq);
7623 max_cp = min_t(int, max_cp, max_stat);
7624 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7625 if (rc)
7626 rc = 0;
7627 }
7628 return rc;
7629}
7630
Michael Chan702c2212017-05-29 19:06:10 -04007631static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007632{
7633 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007634
7635 if (sh)
7636 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7637 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007638 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007639 if (rc)
7640 return rc;
7641 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7642 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007643
7644 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7645 if (rc)
7646 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7647
Michael Chan6e6c5a52016-01-02 23:45:02 -05007648 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7649 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7650 bp->tx_nr_rings + bp->rx_nr_rings;
7651 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007652 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7653 bp->rx_nr_rings++;
7654 bp->cp_nr_rings++;
7655 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007656 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007657}
7658
Michael Chan7b08f662016-12-07 00:26:18 -05007659void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7660{
7661 ASSERT_RTNL();
7662 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007663 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007664}
7665
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007666static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7667{
7668 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7669 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7670
7671 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7672 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7673 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7674 else
7675 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7676 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7677 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7678 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7679 "Unknown", width);
7680}
7681
Michael Chanc0c050c2015-10-22 16:01:17 -04007682static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7683{
7684 static int version_printed;
7685 struct net_device *dev;
7686 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007687 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007688
Ray Jui4e003382017-02-20 19:25:16 -05007689 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007690 return -ENODEV;
7691
Michael Chanc0c050c2015-10-22 16:01:17 -04007692 if (version_printed++ == 0)
7693 pr_info("%s", version);
7694
7695 max_irqs = bnxt_get_max_irq(pdev);
7696 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7697 if (!dev)
7698 return -ENOMEM;
7699
7700 bp = netdev_priv(dev);
7701
7702 if (bnxt_vf_pciid(ent->driver_data))
7703 bp->flags |= BNXT_FLAG_VF;
7704
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007705 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007706 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007707
7708 rc = bnxt_init_board(pdev, dev);
7709 if (rc < 0)
7710 goto init_err_free;
7711
7712 dev->netdev_ops = &bnxt_netdev_ops;
7713 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7714 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007715 pci_set_drvdata(pdev, dev);
7716
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007717 rc = bnxt_alloc_hwrm_resources(bp);
7718 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007719 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007720
7721 mutex_init(&bp->hwrm_cmd_lock);
7722 rc = bnxt_hwrm_ver_get(bp);
7723 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007724 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007725
Deepak Khungare605db82017-05-29 19:06:04 -04007726 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7727 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7728 if (rc)
7729 goto init_err_pci_clean;
7730 }
7731
Michael Chan3c2217a2017-03-08 18:44:32 -05007732 rc = bnxt_hwrm_func_reset(bp);
7733 if (rc)
7734 goto init_err_pci_clean;
7735
Rob Swindell5ac67d82016-09-19 03:58:03 -04007736 bnxt_hwrm_fw_set_time(bp);
7737
Michael Chanc0c050c2015-10-22 16:01:17 -04007738 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7739 NETIF_F_TSO | NETIF_F_TSO6 |
7740 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007741 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007742 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7743 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007744 NETIF_F_RXCSUM | NETIF_F_GRO;
7745
7746 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7747 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007748
Michael Chanc0c050c2015-10-22 16:01:17 -04007749 dev->hw_enc_features =
7750 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7751 NETIF_F_TSO | NETIF_F_TSO6 |
7752 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007753 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007754 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007755 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7756 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007757 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7758 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7759 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7760 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7761 dev->priv_flags |= IFF_UNICAST_FLT;
7762
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007763 /* MTU range: 60 - 9500 */
7764 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007765 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007766
Michael Chanc0c050c2015-10-22 16:01:17 -04007767#ifdef CONFIG_BNXT_SRIOV
7768 init_waitqueue_head(&bp->sriov_cfg_wait);
7769#endif
Michael Chan309369c2016-06-13 02:25:34 -04007770 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007771 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007772 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007773 else
7774 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007775
Michael Chanc0c050c2015-10-22 16:01:17 -04007776 rc = bnxt_hwrm_func_drv_rgtr(bp);
7777 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007778 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007779
Michael Chana1653b12016-12-07 00:26:20 -05007780 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7781 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007782 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007783
Michael Chana588e452016-12-07 00:26:21 -05007784 bp->ulp_probe = bnxt_ulp_probe;
7785
Michael Chanc0c050c2015-10-22 16:01:17 -04007786 /* Get the MAX capabilities for this function */
7787 rc = bnxt_hwrm_func_qcaps(bp);
7788 if (rc) {
7789 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7790 rc);
7791 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007792 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007793 }
7794
7795 rc = bnxt_hwrm_queue_qportcfg(bp);
7796 if (rc) {
7797 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7798 rc);
7799 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007800 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007801 }
7802
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007803 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007804 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007805 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007806 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007807
Michael Chanc61fb992017-02-06 16:55:36 -05007808 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007809 bnxt_set_tpa_flags(bp);
7810 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007811 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007812 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007813 if (rc) {
7814 netdev_err(bp->dev, "Not enough rings available.\n");
7815 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007816 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007817 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007818
Michael Chan87da7f72016-11-16 21:13:09 -05007819 /* Default RSS hash cfg. */
7820 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7821 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7822 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7823 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007824 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007825 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7826 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7827 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7828 }
7829
Michael Chan8fdefd62016-12-29 12:13:36 -05007830 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007831 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007832 dev->hw_features |= NETIF_F_NTUPLE;
7833 if (bnxt_rfs_capable(bp)) {
7834 bp->flags |= BNXT_FLAG_RFS;
7835 dev->features |= NETIF_F_NTUPLE;
7836 }
7837 }
7838
Michael Chanc0c050c2015-10-22 16:01:17 -04007839 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7840 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7841
7842 rc = bnxt_probe_phy(bp);
7843 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007844 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007845
Michael Chan78095922016-12-07 00:26:16 -05007846 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007847 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007848 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007849
Michael Chanc1ef1462017-04-04 18:14:07 -04007850 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007851 if (bp->flags & BNXT_FLAG_WOL_CAP)
7852 device_set_wakeup_enable(&pdev->dev, bp->wol);
7853 else
7854 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007855
Michael Chan78095922016-12-07 00:26:16 -05007856 rc = register_netdev(dev);
7857 if (rc)
7858 goto init_err_clr_int;
7859
Michael Chanc0c050c2015-10-22 16:01:17 -04007860 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7861 board_info[ent->driver_data].name,
7862 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7863
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007864 bnxt_parse_log_pcie_link(bp);
7865
Michael Chanc0c050c2015-10-22 16:01:17 -04007866 return 0;
7867
Michael Chan78095922016-12-07 00:26:16 -05007868init_err_clr_int:
7869 bnxt_clear_int_mode(bp);
7870
Sathya Perla17086392017-02-20 19:25:18 -05007871init_err_pci_clean:
7872 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007873
7874init_err_free:
7875 free_netdev(dev);
7876 return rc;
7877}
7878
Michael Chand196ece2017-04-04 18:14:08 -04007879static void bnxt_shutdown(struct pci_dev *pdev)
7880{
7881 struct net_device *dev = pci_get_drvdata(pdev);
7882 struct bnxt *bp;
7883
7884 if (!dev)
7885 return;
7886
7887 rtnl_lock();
7888 bp = netdev_priv(dev);
7889 if (!bp)
7890 goto shutdown_exit;
7891
7892 if (netif_running(dev))
7893 dev_close(dev);
7894
7895 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04007896 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007897 bnxt_clear_int_mode(bp);
7898 pci_wake_from_d3(pdev, bp->wol);
7899 pci_set_power_state(pdev, PCI_D3hot);
7900 }
7901
7902shutdown_exit:
7903 rtnl_unlock();
7904}
7905
Michael Chanf65a2042017-04-04 18:14:11 -04007906#ifdef CONFIG_PM_SLEEP
7907static int bnxt_suspend(struct device *device)
7908{
7909 struct pci_dev *pdev = to_pci_dev(device);
7910 struct net_device *dev = pci_get_drvdata(pdev);
7911 struct bnxt *bp = netdev_priv(dev);
7912 int rc = 0;
7913
7914 rtnl_lock();
7915 if (netif_running(dev)) {
7916 netif_device_detach(dev);
7917 rc = bnxt_close(dev);
7918 }
7919 bnxt_hwrm_func_drv_unrgtr(bp);
7920 rtnl_unlock();
7921 return rc;
7922}
7923
7924static int bnxt_resume(struct device *device)
7925{
7926 struct pci_dev *pdev = to_pci_dev(device);
7927 struct net_device *dev = pci_get_drvdata(pdev);
7928 struct bnxt *bp = netdev_priv(dev);
7929 int rc = 0;
7930
7931 rtnl_lock();
7932 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
7933 rc = -ENODEV;
7934 goto resume_exit;
7935 }
7936 rc = bnxt_hwrm_func_reset(bp);
7937 if (rc) {
7938 rc = -EBUSY;
7939 goto resume_exit;
7940 }
7941 bnxt_get_wol_settings(bp);
7942 if (netif_running(dev)) {
7943 rc = bnxt_open(dev);
7944 if (!rc)
7945 netif_device_attach(dev);
7946 }
7947
7948resume_exit:
7949 rtnl_unlock();
7950 return rc;
7951}
7952
7953static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
7954#define BNXT_PM_OPS (&bnxt_pm_ops)
7955
7956#else
7957
7958#define BNXT_PM_OPS NULL
7959
7960#endif /* CONFIG_PM_SLEEP */
7961
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007962/**
7963 * bnxt_io_error_detected - called when PCI error is detected
7964 * @pdev: Pointer to PCI device
7965 * @state: The current pci connection state
7966 *
7967 * This function is called after a PCI bus error affecting
7968 * this device has been detected.
7969 */
7970static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7971 pci_channel_state_t state)
7972{
7973 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007974 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007975
7976 netdev_info(netdev, "PCI I/O error detected\n");
7977
7978 rtnl_lock();
7979 netif_device_detach(netdev);
7980
Michael Chana588e452016-12-07 00:26:21 -05007981 bnxt_ulp_stop(bp);
7982
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007983 if (state == pci_channel_io_perm_failure) {
7984 rtnl_unlock();
7985 return PCI_ERS_RESULT_DISCONNECT;
7986 }
7987
7988 if (netif_running(netdev))
7989 bnxt_close(netdev);
7990
7991 pci_disable_device(pdev);
7992 rtnl_unlock();
7993
7994 /* Request a slot slot reset. */
7995 return PCI_ERS_RESULT_NEED_RESET;
7996}
7997
7998/**
7999 * bnxt_io_slot_reset - called after the pci bus has been reset.
8000 * @pdev: Pointer to PCI device
8001 *
8002 * Restart the card from scratch, as if from a cold-boot.
8003 * At this point, the card has exprienced a hard reset,
8004 * followed by fixups by BIOS, and has its config space
8005 * set up identically to what it was at cold boot.
8006 */
8007static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8008{
8009 struct net_device *netdev = pci_get_drvdata(pdev);
8010 struct bnxt *bp = netdev_priv(netdev);
8011 int err = 0;
8012 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8013
8014 netdev_info(bp->dev, "PCI Slot Reset\n");
8015
8016 rtnl_lock();
8017
8018 if (pci_enable_device(pdev)) {
8019 dev_err(&pdev->dev,
8020 "Cannot re-enable PCI device after reset.\n");
8021 } else {
8022 pci_set_master(pdev);
8023
Michael Chanaa8ed022016-12-07 00:26:17 -05008024 err = bnxt_hwrm_func_reset(bp);
8025 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008026 err = bnxt_open(netdev);
8027
Michael Chana588e452016-12-07 00:26:21 -05008028 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008029 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008030 bnxt_ulp_start(bp);
8031 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008032 }
8033
8034 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8035 dev_close(netdev);
8036
8037 rtnl_unlock();
8038
8039 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8040 if (err) {
8041 dev_err(&pdev->dev,
8042 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8043 err); /* non-fatal, continue */
8044 }
8045
8046 return PCI_ERS_RESULT_RECOVERED;
8047}
8048
8049/**
8050 * bnxt_io_resume - called when traffic can start flowing again.
8051 * @pdev: Pointer to PCI device
8052 *
8053 * This callback is called when the error recovery driver tells
8054 * us that its OK to resume normal operation.
8055 */
8056static void bnxt_io_resume(struct pci_dev *pdev)
8057{
8058 struct net_device *netdev = pci_get_drvdata(pdev);
8059
8060 rtnl_lock();
8061
8062 netif_device_attach(netdev);
8063
8064 rtnl_unlock();
8065}
8066
8067static const struct pci_error_handlers bnxt_err_handler = {
8068 .error_detected = bnxt_io_error_detected,
8069 .slot_reset = bnxt_io_slot_reset,
8070 .resume = bnxt_io_resume
8071};
8072
Michael Chanc0c050c2015-10-22 16:01:17 -04008073static struct pci_driver bnxt_pci_driver = {
8074 .name = DRV_MODULE_NAME,
8075 .id_table = bnxt_pci_tbl,
8076 .probe = bnxt_init_one,
8077 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008078 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008079 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008080 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008081#if defined(CONFIG_BNXT_SRIOV)
8082 .sriov_configure = bnxt_sriov_configure,
8083#endif
8084};
8085
8086module_pci_driver(bnxt_pci_driver);