blob: ebdeeb4a5756302352f03f663d3879b8c8c3b7e2 [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanbac9a7e2017-02-12 19:18:10 -05004 * Copyright (c) 2016-2017 Broadcom Limited
Michael Chanc0c050c2015-10-22 16:01:17 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12
13#include <linux/stringify.h>
14#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/errno.h>
17#include <linux/ioport.h>
18#include <linux/slab.h>
19#include <linux/vmalloc.h>
20#include <linux/interrupt.h>
21#include <linux/pci.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/skbuff.h>
25#include <linux/dma-mapping.h>
26#include <linux/bitops.h>
27#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/delay.h>
30#include <asm/byteorder.h>
31#include <asm/page.h>
32#include <linux/time.h>
33#include <linux/mii.h>
34#include <linux/if.h>
35#include <linux/if_vlan.h>
Michael Chan32e8239c2017-07-24 12:34:21 -040036#include <linux/if_bridge.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040037#include <linux/rtc.h>
Michael Chanc6d30e82017-02-06 16:55:42 -050038#include <linux/bpf.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040039#include <net/ip.h>
40#include <net/tcp.h>
41#include <net/udp.h>
42#include <net/checksum.h>
43#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070044#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040045#include <linux/workqueue.h>
46#include <linux/prefetch.h>
47#include <linux/cache.h>
48#include <linux/log2.h>
49#include <linux/aer.h>
50#include <linux/bitmap.h>
51#include <linux/cpu_rmap.h>
52
53#include "bnxt_hsi.h"
54#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050055#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056#include "bnxt_sriov.h"
57#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050058#include "bnxt_dcb.h"
Michael Chanc6d30e82017-02-06 16:55:42 -050059#include "bnxt_xdp.h"
Sathya Perla4ab0c6a2017-07-24 12:34:27 -040060#include "bnxt_vfr.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040061
62#define BNXT_TX_TIMEOUT (5 * HZ)
63
64static const char version[] =
65 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
66
67MODULE_LICENSE("GPL");
68MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
69MODULE_VERSION(DRV_MODULE_VERSION);
70
71#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
72#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
73#define BNXT_RX_COPY_THRESH 256
74
Michael Chan4419dbe2016-02-10 17:33:49 -050075#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040076
77enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050078 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040079 BCM57302,
80 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040081 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040082 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040083 BCM57311,
84 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050085 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040086 BCM57404,
87 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040088 BCM57402_NPAR,
89 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040090 BCM57412,
91 BCM57414,
92 BCM57416,
93 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040094 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040095 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040096 BCM57417_SFP,
97 BCM57416_SFP,
98 BCM57404_NPAR,
99 BCM57406_NPAR,
100 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -0400101 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -0400102 BCM57414_NPAR,
103 BCM57416_NPAR,
Deepak Khungar32b40792017-02-12 19:18:18 -0500104 BCM57452,
105 BCM57454,
Michael Chanadbc8302016-09-19 03:58:01 -0400106 NETXTREME_E_VF,
107 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400108};
109
110/* indexed by enum above */
111static const struct {
112 char *name;
113} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400114 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
115 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
119 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
120 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
122 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
123 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400124 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400125 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
126 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
127 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
128 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
129 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400130 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400131 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
133 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400134 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
135 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400136 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
137 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400138 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
139 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Deepak Khungar32b40792017-02-12 19:18:18 -0500140 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
141 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
Michael Chanadbc8302016-09-19 03:58:01 -0400142 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
143 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400144};
145
146static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400147 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500148 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400149 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400151 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400152 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400153 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500155 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400156 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400158 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400160 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400165 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400171 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400173 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400174 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400175 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400176 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Deepak Khungar32b40792017-02-12 19:18:18 -0500177 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
178 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400179#ifdef CONFIG_BNXT_SRIOV
Deepak Khungarc7ef35e2017-05-29 19:06:05 -0400180 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
Michael Chanadbc8302016-09-19 03:58:01 -0400182 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
186 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
187 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400188#endif
189 { 0 }
190};
191
192MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
193
194static const u16 bnxt_vf_req_snif[] = {
195 HWRM_FUNC_CFG,
196 HWRM_PORT_PHY_QCFG,
197 HWRM_CFA_L2_FILTER_ALLOC,
198};
199
Michael Chan25be8622016-04-05 14:09:00 -0400200static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500201 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
202 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
203 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
204 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
205 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400206};
207
Michael Chanc0c050c2015-10-22 16:01:17 -0400208static bool bnxt_vf_pciid(enum board_idx idx)
209{
Michael Chanadbc8302016-09-19 03:58:01 -0400210 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400211}
212
213#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
214#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
215#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
216
217#define BNXT_CP_DB_REARM(db, raw_cons) \
218 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
219
220#define BNXT_CP_DB(db, raw_cons) \
221 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
222
223#define BNXT_CP_DB_IRQ_DIS(db) \
224 writel(DB_CP_IRQ_DIS_FLAGS, db)
225
Michael Chan38413402017-02-06 16:55:43 -0500226const u16 bnxt_lhint_arr[] = {
Michael Chanc0c050c2015-10-22 16:01:17 -0400227 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
228 TX_BD_FLAGS_LHINT_512_TO_1023,
229 TX_BD_FLAGS_LHINT_1024_TO_2047,
230 TX_BD_FLAGS_LHINT_1024_TO_2047,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
244 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
245 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
246};
247
248static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
249{
250 struct bnxt *bp = netdev_priv(dev);
251 struct tx_bd *txbd;
252 struct tx_bd_ext *txbd1;
253 struct netdev_queue *txq;
254 int i;
255 dma_addr_t mapping;
256 unsigned int length, pad = 0;
257 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
258 u16 prod, last_frag;
259 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400260 struct bnxt_tx_ring_info *txr;
261 struct bnxt_sw_tx_bd *tx_buf;
262
263 i = skb_get_queue_mapping(skb);
264 if (unlikely(i >= bp->tx_nr_rings)) {
265 dev_kfree_skb_any(skb);
266 return NETDEV_TX_OK;
267 }
268
Michael Chanc0c050c2015-10-22 16:01:17 -0400269 txq = netdev_get_tx_queue(dev, i);
Michael Chana960dec2017-02-06 16:55:39 -0500270 txr = &bp->tx_ring[bp->tx_ring_map[i]];
Michael Chanc0c050c2015-10-22 16:01:17 -0400271 prod = txr->tx_prod;
272
273 free_size = bnxt_tx_avail(bp, txr);
274 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
275 netif_tx_stop_queue(txq);
276 return NETDEV_TX_BUSY;
277 }
278
279 length = skb->len;
280 len = skb_headlen(skb);
281 last_frag = skb_shinfo(skb)->nr_frags;
282
283 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
284
285 txbd->tx_bd_opaque = prod;
286
287 tx_buf = &txr->tx_buf_ring[prod];
288 tx_buf->skb = skb;
289 tx_buf->nr_frags = last_frag;
290
291 vlan_tag_flags = 0;
292 cfa_action = 0;
293 if (skb_vlan_tag_present(skb)) {
294 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
295 skb_vlan_tag_get(skb);
296 /* Currently supports 8021Q, 8021AD vlan offloads
297 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
298 */
299 if (skb->vlan_proto == htons(ETH_P_8021Q))
300 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
301 }
302
303 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500304 struct tx_push_buffer *tx_push_buf = txr->tx_push;
305 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
306 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
307 void *pdata = tx_push_buf->data;
308 u64 *end;
309 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400310
311 /* Set COAL_NOW to be ready quickly for the next push */
312 tx_push->tx_bd_len_flags_type =
313 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
314 TX_BD_TYPE_LONG_TX_BD |
315 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
316 TX_BD_FLAGS_COAL_NOW |
317 TX_BD_FLAGS_PACKET_END |
318 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
319
320 if (skb->ip_summed == CHECKSUM_PARTIAL)
321 tx_push1->tx_bd_hsize_lflags =
322 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
323 else
324 tx_push1->tx_bd_hsize_lflags = 0;
325
326 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
327 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
328
Michael Chanfbb0fa82016-02-22 02:10:26 -0500329 end = pdata + length;
330 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500331 *end = 0;
332
Michael Chanc0c050c2015-10-22 16:01:17 -0400333 skb_copy_from_linear_data(skb, pdata, len);
334 pdata += len;
335 for (j = 0; j < last_frag; j++) {
336 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
337 void *fptr;
338
339 fptr = skb_frag_address_safe(frag);
340 if (!fptr)
341 goto normal_tx;
342
343 memcpy(pdata, fptr, skb_frag_size(frag));
344 pdata += skb_frag_size(frag);
345 }
346
Michael Chan4419dbe2016-02-10 17:33:49 -0500347 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
348 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400349 prod = NEXT_TX(prod);
350 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
351 memcpy(txbd, tx_push1, sizeof(*txbd));
352 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500353 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
355 txr->tx_prod = prod;
356
Michael Chanb9a84602016-06-06 02:37:14 -0400357 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400358 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400359 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400360
Michael Chan4419dbe2016-02-10 17:33:49 -0500361 push_len = (length + sizeof(*tx_push) + 7) / 8;
362 if (push_len > 16) {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400364 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
365 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500366 } else {
367 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
368 push_len);
369 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400370
Michael Chanc0c050c2015-10-22 16:01:17 -0400371 goto tx_done;
372 }
373
374normal_tx:
375 if (length < BNXT_MIN_PKT_SIZE) {
376 pad = BNXT_MIN_PKT_SIZE - length;
377 if (skb_pad(skb, pad)) {
378 /* SKB already freed. */
379 tx_buf->skb = NULL;
380 return NETDEV_TX_OK;
381 }
382 length = BNXT_MIN_PKT_SIZE;
383 }
384
385 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
386
387 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
388 dev_kfree_skb_any(skb);
389 tx_buf->skb = NULL;
390 return NETDEV_TX_OK;
391 }
392
393 dma_unmap_addr_set(tx_buf, mapping, mapping);
394 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
395 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
396
397 txbd->tx_bd_haddr = cpu_to_le64(mapping);
398
399 prod = NEXT_TX(prod);
400 txbd1 = (struct tx_bd_ext *)
401 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
402
403 txbd1->tx_bd_hsize_lflags = 0;
404 if (skb_is_gso(skb)) {
405 u32 hdr_len;
406
407 if (skb->encapsulation)
408 hdr_len = skb_inner_network_offset(skb) +
409 skb_inner_network_header_len(skb) +
410 inner_tcp_hdrlen(skb);
411 else
412 hdr_len = skb_transport_offset(skb) +
413 tcp_hdrlen(skb);
414
415 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
416 TX_BD_FLAGS_T_IPID |
417 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
418 length = skb_shinfo(skb)->gso_size;
419 txbd1->tx_bd_mss = cpu_to_le32(length);
420 length += hdr_len;
421 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
422 txbd1->tx_bd_hsize_lflags =
423 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
424 txbd1->tx_bd_mss = 0;
425 }
426
427 length >>= 9;
428 flags |= bnxt_lhint_arr[length];
429 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
430
431 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
432 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
433 for (i = 0; i < last_frag; i++) {
434 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
435
436 prod = NEXT_TX(prod);
437 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
438
439 len = skb_frag_size(frag);
440 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
441 DMA_TO_DEVICE);
442
443 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
444 goto tx_dma_error;
445
446 tx_buf = &txr->tx_buf_ring[prod];
447 dma_unmap_addr_set(tx_buf, mapping, mapping);
448
449 txbd->tx_bd_haddr = cpu_to_le64(mapping);
450
451 flags = len << TX_BD_LEN_SHIFT;
452 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
453 }
454
455 flags &= ~TX_BD_LEN;
456 txbd->tx_bd_len_flags_type =
457 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
458 TX_BD_FLAGS_PACKET_END);
459
460 netdev_tx_sent_queue(txq, skb->len);
461
462 /* Sync BD data before updating doorbell */
463 wmb();
464
465 prod = NEXT_TX(prod);
466 txr->tx_prod = prod;
467
Michael Chanffe40642017-05-30 20:03:00 -0400468 if (!skb->xmit_more || netif_xmit_stopped(txq))
Michael Chan4d172f22017-05-29 19:06:09 -0400469 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
Michael Chanc0c050c2015-10-22 16:01:17 -0400470
471tx_done:
472
473 mmiowb();
474
475 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
Michael Chan4d172f22017-05-29 19:06:09 -0400476 if (skb->xmit_more && !tx_buf->is_push)
477 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
478
Michael Chanc0c050c2015-10-22 16:01:17 -0400479 netif_tx_stop_queue(txq);
480
481 /* netif_tx_stop_queue() must be done before checking
482 * tx index in bnxt_tx_avail() below, because in
483 * bnxt_tx_int(), we update tx index before checking for
484 * netif_tx_queue_stopped().
485 */
486 smp_mb();
487 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
488 netif_tx_wake_queue(txq);
489 }
490 return NETDEV_TX_OK;
491
492tx_dma_error:
493 last_frag = i;
494
495 /* start back at beginning and unmap skb */
496 prod = txr->tx_prod;
497 tx_buf = &txr->tx_buf_ring[prod];
498 tx_buf->skb = NULL;
499 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
500 skb_headlen(skb), PCI_DMA_TODEVICE);
501 prod = NEXT_TX(prod);
502
503 /* unmap remaining mapped pages */
504 for (i = 0; i < last_frag; i++) {
505 prod = NEXT_TX(prod);
506 tx_buf = &txr->tx_buf_ring[prod];
507 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
508 skb_frag_size(&skb_shinfo(skb)->frags[i]),
509 PCI_DMA_TODEVICE);
510 }
511
512 dev_kfree_skb_any(skb);
513 return NETDEV_TX_OK;
514}
515
516static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
517{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500518 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chana960dec2017-02-06 16:55:39 -0500519 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
Michael Chanc0c050c2015-10-22 16:01:17 -0400520 u16 cons = txr->tx_cons;
521 struct pci_dev *pdev = bp->pdev;
522 int i;
523 unsigned int tx_bytes = 0;
524
525 for (i = 0; i < nr_pkts; i++) {
526 struct bnxt_sw_tx_bd *tx_buf;
527 struct sk_buff *skb;
528 int j, last;
529
530 tx_buf = &txr->tx_buf_ring[cons];
531 cons = NEXT_TX(cons);
532 skb = tx_buf->skb;
533 tx_buf->skb = NULL;
534
535 if (tx_buf->is_push) {
536 tx_buf->is_push = 0;
537 goto next_tx_int;
538 }
539
540 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
541 skb_headlen(skb), PCI_DMA_TODEVICE);
542 last = tx_buf->nr_frags;
543
544 for (j = 0; j < last; j++) {
545 cons = NEXT_TX(cons);
546 tx_buf = &txr->tx_buf_ring[cons];
547 dma_unmap_page(
548 &pdev->dev,
549 dma_unmap_addr(tx_buf, mapping),
550 skb_frag_size(&skb_shinfo(skb)->frags[j]),
551 PCI_DMA_TODEVICE);
552 }
553
554next_tx_int:
555 cons = NEXT_TX(cons);
556
557 tx_bytes += skb->len;
558 dev_kfree_skb_any(skb);
559 }
560
561 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
562 txr->tx_cons = cons;
563
564 /* Need to make the tx_cons update visible to bnxt_start_xmit()
565 * before checking for netif_tx_queue_stopped(). Without the
566 * memory barrier, there is a small possibility that bnxt_start_xmit()
567 * will miss it and cause the queue to be stopped forever.
568 */
569 smp_mb();
570
571 if (unlikely(netif_tx_queue_stopped(txq)) &&
572 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
573 __netif_tx_lock(txq, smp_processor_id());
574 if (netif_tx_queue_stopped(txq) &&
575 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
576 txr->dev_state != BNXT_DEV_STATE_CLOSING)
577 netif_tx_wake_queue(txq);
578 __netif_tx_unlock(txq);
579 }
580}
581
Michael Chanc61fb992017-02-06 16:55:36 -0500582static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
583 gfp_t gfp)
584{
585 struct device *dev = &bp->pdev->dev;
586 struct page *page;
587
588 page = alloc_page(gfp);
589 if (!page)
590 return NULL;
591
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700592 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
593 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500594 if (dma_mapping_error(dev, *mapping)) {
595 __free_page(page);
596 return NULL;
597 }
598 *mapping += bp->rx_dma_offset;
599 return page;
600}
601
Michael Chanc0c050c2015-10-22 16:01:17 -0400602static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
603 gfp_t gfp)
604{
605 u8 *data;
606 struct pci_dev *pdev = bp->pdev;
607
608 data = kmalloc(bp->rx_buf_size, gfp);
609 if (!data)
610 return NULL;
611
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700612 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
613 bp->rx_buf_use_size, bp->rx_dir,
614 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400615
616 if (dma_mapping_error(&pdev->dev, *mapping)) {
617 kfree(data);
618 data = NULL;
619 }
620 return data;
621}
622
Michael Chan38413402017-02-06 16:55:43 -0500623int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
624 u16 prod, gfp_t gfp)
Michael Chanc0c050c2015-10-22 16:01:17 -0400625{
626 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
627 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400628 dma_addr_t mapping;
629
Michael Chanc61fb992017-02-06 16:55:36 -0500630 if (BNXT_RX_PAGE_MODE(bp)) {
631 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400632
Michael Chanc61fb992017-02-06 16:55:36 -0500633 if (!page)
634 return -ENOMEM;
635
636 rx_buf->data = page;
637 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
638 } else {
639 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
640
641 if (!data)
642 return -ENOMEM;
643
644 rx_buf->data = data;
645 rx_buf->data_ptr = data + bp->rx_offset;
646 }
Michael Chan11cd1192017-02-06 16:55:33 -0500647 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400648
649 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400650 return 0;
651}
652
Michael Chanc6d30e82017-02-06 16:55:42 -0500653void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400654{
655 u16 prod = rxr->rx_prod;
656 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
657 struct rx_bd *cons_bd, *prod_bd;
658
659 prod_rx_buf = &rxr->rx_buf_ring[prod];
660 cons_rx_buf = &rxr->rx_buf_ring[cons];
661
662 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500663 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400664
Michael Chan11cd1192017-02-06 16:55:33 -0500665 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400666
667 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
668 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
669
670 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
671}
672
673static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
674{
675 u16 next, max = rxr->rx_agg_bmap_size;
676
677 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
678 if (next >= max)
679 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
680 return next;
681}
682
683static inline int bnxt_alloc_rx_page(struct bnxt *bp,
684 struct bnxt_rx_ring_info *rxr,
685 u16 prod, gfp_t gfp)
686{
687 struct rx_bd *rxbd =
688 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
689 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
690 struct pci_dev *pdev = bp->pdev;
691 struct page *page;
692 dma_addr_t mapping;
693 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400694 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400695
Michael Chan89d0a062016-04-25 02:30:51 -0400696 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
697 page = rxr->rx_page;
698 if (!page) {
699 page = alloc_page(gfp);
700 if (!page)
701 return -ENOMEM;
702 rxr->rx_page = page;
703 rxr->rx_page_offset = 0;
704 }
705 offset = rxr->rx_page_offset;
706 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
707 if (rxr->rx_page_offset == PAGE_SIZE)
708 rxr->rx_page = NULL;
709 else
710 get_page(page);
711 } else {
712 page = alloc_page(gfp);
713 if (!page)
714 return -ENOMEM;
715 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400716
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700717 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
718 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
719 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400720 if (dma_mapping_error(&pdev->dev, mapping)) {
721 __free_page(page);
722 return -EIO;
723 }
724
725 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
726 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
727
728 __set_bit(sw_prod, rxr->rx_agg_bmap);
729 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
730 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
731
732 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400733 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400734 rx_agg_buf->mapping = mapping;
735 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
736 rxbd->rx_bd_opaque = sw_prod;
737 return 0;
738}
739
740static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
741 u32 agg_bufs)
742{
743 struct bnxt *bp = bnapi->bp;
744 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500745 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400746 u16 prod = rxr->rx_agg_prod;
747 u16 sw_prod = rxr->rx_sw_agg_prod;
748 u32 i;
749
750 for (i = 0; i < agg_bufs; i++) {
751 u16 cons;
752 struct rx_agg_cmp *agg;
753 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
754 struct rx_bd *prod_bd;
755 struct page *page;
756
757 agg = (struct rx_agg_cmp *)
758 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
759 cons = agg->rx_agg_cmp_opaque;
760 __clear_bit(cons, rxr->rx_agg_bmap);
761
762 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
763 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
764
765 __set_bit(sw_prod, rxr->rx_agg_bmap);
766 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
767 cons_rx_buf = &rxr->rx_agg_ring[cons];
768
769 /* It is possible for sw_prod to be equal to cons, so
770 * set cons_rx_buf->page to NULL first.
771 */
772 page = cons_rx_buf->page;
773 cons_rx_buf->page = NULL;
774 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400775 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400776
777 prod_rx_buf->mapping = cons_rx_buf->mapping;
778
779 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
780
781 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
782 prod_bd->rx_bd_opaque = sw_prod;
783
784 prod = NEXT_RX_AGG(prod);
785 sw_prod = NEXT_RX_AGG(sw_prod);
786 cp_cons = NEXT_CMP(cp_cons);
787 }
788 rxr->rx_agg_prod = prod;
789 rxr->rx_sw_agg_prod = sw_prod;
790}
791
Michael Chanc61fb992017-02-06 16:55:36 -0500792static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
793 struct bnxt_rx_ring_info *rxr,
794 u16 cons, void *data, u8 *data_ptr,
795 dma_addr_t dma_addr,
796 unsigned int offset_and_len)
797{
798 unsigned int payload = offset_and_len >> 16;
799 unsigned int len = offset_and_len & 0xffff;
800 struct skb_frag_struct *frag;
801 struct page *page = data;
802 u16 prod = rxr->rx_prod;
803 struct sk_buff *skb;
804 int off, err;
805
806 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
807 if (unlikely(err)) {
808 bnxt_reuse_rx_data(rxr, cons, data);
809 return NULL;
810 }
811 dma_addr -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700812 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
813 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -0500814
815 if (unlikely(!payload))
816 payload = eth_get_headlen(data_ptr, len);
817
818 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
819 if (!skb) {
820 __free_page(page);
821 return NULL;
822 }
823
824 off = (void *)data_ptr - page_address(page);
825 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
826 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
827 payload + NET_IP_ALIGN);
828
829 frag = &skb_shinfo(skb)->frags[0];
830 skb_frag_size_sub(frag, payload);
831 frag->page_offset += payload;
832 skb->data_len -= payload;
833 skb->tail += payload;
834
835 return skb;
836}
837
Michael Chanc0c050c2015-10-22 16:01:17 -0400838static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
839 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500840 void *data, u8 *data_ptr,
841 dma_addr_t dma_addr,
842 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400843{
Michael Chan6bb19472017-02-06 16:55:32 -0500844 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400845 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500846 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400847
848 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
849 if (unlikely(err)) {
850 bnxt_reuse_rx_data(rxr, cons, data);
851 return NULL;
852 }
853
854 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700855 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
856 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400857 if (!skb) {
858 kfree(data);
859 return NULL;
860 }
861
Michael Chanb3dba772017-02-06 16:55:35 -0500862 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500863 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400864 return skb;
865}
866
867static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
868 struct sk_buff *skb, u16 cp_cons,
869 u32 agg_bufs)
870{
871 struct pci_dev *pdev = bp->pdev;
872 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500873 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400874 u16 prod = rxr->rx_agg_prod;
875 u32 i;
876
877 for (i = 0; i < agg_bufs; i++) {
878 u16 cons, frag_len;
879 struct rx_agg_cmp *agg;
880 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
881 struct page *page;
882 dma_addr_t mapping;
883
884 agg = (struct rx_agg_cmp *)
885 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
886 cons = agg->rx_agg_cmp_opaque;
887 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
888 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
889
890 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400891 skb_fill_page_desc(skb, i, cons_rx_buf->page,
892 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400893 __clear_bit(cons, rxr->rx_agg_bmap);
894
895 /* It is possible for bnxt_alloc_rx_page() to allocate
896 * a sw_prod index that equals the cons index, so we
897 * need to clear the cons entry now.
898 */
Michael Chan11cd1192017-02-06 16:55:33 -0500899 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400900 page = cons_rx_buf->page;
901 cons_rx_buf->page = NULL;
902
903 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
904 struct skb_shared_info *shinfo;
905 unsigned int nr_frags;
906
907 shinfo = skb_shinfo(skb);
908 nr_frags = --shinfo->nr_frags;
909 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
910
911 dev_kfree_skb(skb);
912
913 cons_rx_buf->page = page;
914
915 /* Update prod since possibly some pages have been
916 * allocated already.
917 */
918 rxr->rx_agg_prod = prod;
919 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
920 return NULL;
921 }
922
Shannon Nelsonc519fe92017-05-09 18:30:12 -0700923 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
924 PCI_DMA_FROMDEVICE,
925 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -0400926
927 skb->data_len += frag_len;
928 skb->len += frag_len;
929 skb->truesize += PAGE_SIZE;
930
931 prod = NEXT_RX_AGG(prod);
932 cp_cons = NEXT_CMP(cp_cons);
933 }
934 rxr->rx_agg_prod = prod;
935 return skb;
936}
937
938static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
939 u8 agg_bufs, u32 *raw_cons)
940{
941 u16 last;
942 struct rx_agg_cmp *agg;
943
944 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
945 last = RING_CMP(*raw_cons);
946 agg = (struct rx_agg_cmp *)
947 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
948 return RX_AGG_CMP_VALID(agg, *raw_cons);
949}
950
951static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
952 unsigned int len,
953 dma_addr_t mapping)
954{
955 struct bnxt *bp = bnapi->bp;
956 struct pci_dev *pdev = bp->pdev;
957 struct sk_buff *skb;
958
959 skb = napi_alloc_skb(&bnapi->napi, len);
960 if (!skb)
961 return NULL;
962
Michael Chan745fc052017-02-06 16:55:34 -0500963 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
964 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400965
Michael Chan6bb19472017-02-06 16:55:32 -0500966 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
967 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400968
Michael Chan745fc052017-02-06 16:55:34 -0500969 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
970 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400971
972 skb_put(skb, len);
973 return skb;
974}
975
Michael Chanfa7e2812016-05-10 19:18:00 -0400976static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
977 u32 *raw_cons, void *cmp)
978{
979 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
980 struct rx_cmp *rxcmp = cmp;
981 u32 tmp_raw_cons = *raw_cons;
982 u8 cmp_type, agg_bufs = 0;
983
984 cmp_type = RX_CMP_TYPE(rxcmp);
985
986 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
987 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
988 RX_CMP_AGG_BUFS) >>
989 RX_CMP_AGG_BUFS_SHIFT;
990 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
991 struct rx_tpa_end_cmp *tpa_end = cmp;
992
993 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
994 RX_TPA_END_CMP_AGG_BUFS) >>
995 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
996 }
997
998 if (agg_bufs) {
999 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1000 return -EBUSY;
1001 }
1002 *raw_cons = tmp_raw_cons;
1003 return 0;
1004}
1005
1006static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1007{
1008 if (!rxr->bnapi->in_reset) {
1009 rxr->bnapi->in_reset = true;
1010 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1011 schedule_work(&bp->sp_task);
1012 }
1013 rxr->rx_next_cons = 0xffff;
1014}
1015
Michael Chanc0c050c2015-10-22 16:01:17 -04001016static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1017 struct rx_tpa_start_cmp *tpa_start,
1018 struct rx_tpa_start_cmp_ext *tpa_start1)
1019{
1020 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1021 u16 cons, prod;
1022 struct bnxt_tpa_info *tpa_info;
1023 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1024 struct rx_bd *prod_bd;
1025 dma_addr_t mapping;
1026
1027 cons = tpa_start->rx_tpa_start_cmp_opaque;
1028 prod = rxr->rx_prod;
1029 cons_rx_buf = &rxr->rx_buf_ring[cons];
1030 prod_rx_buf = &rxr->rx_buf_ring[prod];
1031 tpa_info = &rxr->rx_tpa[agg_id];
1032
Michael Chanfa7e2812016-05-10 19:18:00 -04001033 if (unlikely(cons != rxr->rx_next_cons)) {
1034 bnxt_sched_reset(bp, rxr);
1035 return;
1036 }
1037
Michael Chanc0c050c2015-10-22 16:01:17 -04001038 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001039 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001040
1041 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001042 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001043
1044 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1045
1046 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1047
1048 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001049 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001050 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001051 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001052
1053 tpa_info->len =
1054 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1055 RX_TPA_START_CMP_LEN_SHIFT;
1056 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1057 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1058
1059 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1060 tpa_info->gso_type = SKB_GSO_TCPV4;
1061 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1062 if (hash_type == 3)
1063 tpa_info->gso_type = SKB_GSO_TCPV6;
1064 tpa_info->rss_hash =
1065 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1066 } else {
1067 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1068 tpa_info->gso_type = 0;
1069 if (netif_msg_rx_err(bp))
1070 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1071 }
1072 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1073 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001074 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001075
1076 rxr->rx_prod = NEXT_RX(prod);
1077 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001078 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001079 cons_rx_buf = &rxr->rx_buf_ring[cons];
1080
1081 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1082 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1083 cons_rx_buf->data = NULL;
1084}
1085
1086static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1087 u16 cp_cons, u32 agg_bufs)
1088{
1089 if (agg_bufs)
1090 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1091}
1092
Michael Chan94758f82016-06-13 02:25:35 -04001093static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1094 int payload_off, int tcp_ts,
1095 struct sk_buff *skb)
1096{
1097#ifdef CONFIG_INET
1098 struct tcphdr *th;
1099 int len, nw_off;
1100 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1101 u32 hdr_info = tpa_info->hdr_info;
1102 bool loopback = false;
1103
1104 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1105 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1106 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1107
1108 /* If the packet is an internal loopback packet, the offsets will
1109 * have an extra 4 bytes.
1110 */
1111 if (inner_mac_off == 4) {
1112 loopback = true;
1113 } else if (inner_mac_off > 4) {
1114 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1115 ETH_HLEN - 2));
1116
1117 /* We only support inner iPv4/ipv6. If we don't see the
1118 * correct protocol ID, it must be a loopback packet where
1119 * the offsets are off by 4.
1120 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001121 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001122 loopback = true;
1123 }
1124 if (loopback) {
1125 /* internal loopback packet, subtract all offsets by 4 */
1126 inner_ip_off -= 4;
1127 inner_mac_off -= 4;
1128 outer_ip_off -= 4;
1129 }
1130
1131 nw_off = inner_ip_off - ETH_HLEN;
1132 skb_set_network_header(skb, nw_off);
1133 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1134 struct ipv6hdr *iph = ipv6_hdr(skb);
1135
1136 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1137 len = skb->len - skb_transport_offset(skb);
1138 th = tcp_hdr(skb);
1139 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1140 } else {
1141 struct iphdr *iph = ip_hdr(skb);
1142
1143 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1144 len = skb->len - skb_transport_offset(skb);
1145 th = tcp_hdr(skb);
1146 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1147 }
1148
1149 if (inner_mac_off) { /* tunnel */
1150 struct udphdr *uh = NULL;
1151 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1152 ETH_HLEN - 2));
1153
1154 if (proto == htons(ETH_P_IP)) {
1155 struct iphdr *iph = (struct iphdr *)skb->data;
1156
1157 if (iph->protocol == IPPROTO_UDP)
1158 uh = (struct udphdr *)(iph + 1);
1159 } else {
1160 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1161
1162 if (iph->nexthdr == IPPROTO_UDP)
1163 uh = (struct udphdr *)(iph + 1);
1164 }
1165 if (uh) {
1166 if (uh->check)
1167 skb_shinfo(skb)->gso_type |=
1168 SKB_GSO_UDP_TUNNEL_CSUM;
1169 else
1170 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1171 }
1172 }
1173#endif
1174 return skb;
1175}
1176
Michael Chanc0c050c2015-10-22 16:01:17 -04001177#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1178#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1179
Michael Chan309369c2016-06-13 02:25:34 -04001180static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1181 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001182 struct sk_buff *skb)
1183{
Michael Chand1611c32015-10-25 22:27:57 -04001184#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001185 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001186 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001187
Michael Chan309369c2016-06-13 02:25:34 -04001188 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001189 tcp_opt_len = 12;
1190
Michael Chanc0c050c2015-10-22 16:01:17 -04001191 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1192 struct iphdr *iph;
1193
1194 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1195 ETH_HLEN;
1196 skb_set_network_header(skb, nw_off);
1197 iph = ip_hdr(skb);
1198 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1199 len = skb->len - skb_transport_offset(skb);
1200 th = tcp_hdr(skb);
1201 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1202 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1203 struct ipv6hdr *iph;
1204
1205 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1206 ETH_HLEN;
1207 skb_set_network_header(skb, nw_off);
1208 iph = ipv6_hdr(skb);
1209 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1210 len = skb->len - skb_transport_offset(skb);
1211 th = tcp_hdr(skb);
1212 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1213 } else {
1214 dev_kfree_skb_any(skb);
1215 return NULL;
1216 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001217
1218 if (nw_off) { /* tunnel */
1219 struct udphdr *uh = NULL;
1220
1221 if (skb->protocol == htons(ETH_P_IP)) {
1222 struct iphdr *iph = (struct iphdr *)skb->data;
1223
1224 if (iph->protocol == IPPROTO_UDP)
1225 uh = (struct udphdr *)(iph + 1);
1226 } else {
1227 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1228
1229 if (iph->nexthdr == IPPROTO_UDP)
1230 uh = (struct udphdr *)(iph + 1);
1231 }
1232 if (uh) {
1233 if (uh->check)
1234 skb_shinfo(skb)->gso_type |=
1235 SKB_GSO_UDP_TUNNEL_CSUM;
1236 else
1237 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1238 }
1239 }
1240#endif
1241 return skb;
1242}
1243
Michael Chan309369c2016-06-13 02:25:34 -04001244static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1245 struct bnxt_tpa_info *tpa_info,
1246 struct rx_tpa_end_cmp *tpa_end,
1247 struct rx_tpa_end_cmp_ext *tpa_end1,
1248 struct sk_buff *skb)
1249{
1250#ifdef CONFIG_INET
1251 int payload_off;
1252 u16 segs;
1253
1254 segs = TPA_END_TPA_SEGS(tpa_end);
1255 if (segs == 1)
1256 return skb;
1257
1258 NAPI_GRO_CB(skb)->count = segs;
1259 skb_shinfo(skb)->gso_size =
1260 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1261 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1262 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1263 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1264 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1265 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001266 if (likely(skb))
1267 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001268#endif
1269 return skb;
1270}
1271
Michael Chanc0c050c2015-10-22 16:01:17 -04001272static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1273 struct bnxt_napi *bnapi,
1274 u32 *raw_cons,
1275 struct rx_tpa_end_cmp *tpa_end,
1276 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001277 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001278{
1279 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001280 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001281 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001282 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001283 u16 cp_cons = RING_CMP(*raw_cons);
1284 unsigned int len;
1285 struct bnxt_tpa_info *tpa_info;
1286 dma_addr_t mapping;
1287 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001288 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001289
Michael Chanfa7e2812016-05-10 19:18:00 -04001290 if (unlikely(bnapi->in_reset)) {
1291 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1292
1293 if (rc < 0)
1294 return ERR_PTR(-EBUSY);
1295 return NULL;
1296 }
1297
Michael Chanc0c050c2015-10-22 16:01:17 -04001298 tpa_info = &rxr->rx_tpa[agg_id];
1299 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001300 data_ptr = tpa_info->data_ptr;
1301 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001302 len = tpa_info->len;
1303 mapping = tpa_info->mapping;
1304
1305 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1306 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1307
1308 if (agg_bufs) {
1309 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1310 return ERR_PTR(-EBUSY);
1311
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001312 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001313 cp_cons = NEXT_CMP(cp_cons);
1314 }
1315
Michael Chan69c149e2017-06-23 14:01:00 -04001316 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001317 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
Michael Chan69c149e2017-06-23 14:01:00 -04001318 if (agg_bufs > MAX_SKB_FRAGS)
1319 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1320 agg_bufs, (int)MAX_SKB_FRAGS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001321 return NULL;
1322 }
1323
1324 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001325 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001326 if (!skb) {
1327 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1328 return NULL;
1329 }
1330 } else {
1331 u8 *new_data;
1332 dma_addr_t new_mapping;
1333
1334 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1335 if (!new_data) {
1336 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1337 return NULL;
1338 }
1339
1340 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001341 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001342 tpa_info->mapping = new_mapping;
1343
1344 skb = build_skb(data, 0);
Shannon Nelsonc519fe92017-05-09 18:30:12 -07001345 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1346 bp->rx_buf_use_size, bp->rx_dir,
1347 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04001348
1349 if (!skb) {
1350 kfree(data);
1351 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1352 return NULL;
1353 }
Michael Chanb3dba772017-02-06 16:55:35 -05001354 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001355 skb_put(skb, len);
1356 }
1357
1358 if (agg_bufs) {
1359 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1360 if (!skb) {
1361 /* Page reuse already handled by bnxt_rx_pages(). */
1362 return NULL;
1363 }
1364 }
1365 skb->protocol = eth_type_trans(skb, bp->dev);
1366
1367 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1368 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1369
Michael Chan8852ddb2016-06-06 02:37:16 -04001370 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1371 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001372 u16 vlan_proto = tpa_info->metadata >>
1373 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001374 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001375
Michael Chan8852ddb2016-06-06 02:37:16 -04001376 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001377 }
1378
1379 skb_checksum_none_assert(skb);
1380 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1381 skb->ip_summed = CHECKSUM_UNNECESSARY;
1382 skb->csum_level =
1383 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1384 }
1385
1386 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001387 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001388
1389 return skb;
1390}
1391
1392/* returns the following:
1393 * 1 - 1 packet successfully received
1394 * 0 - successful TPA_START, packet not completed yet
1395 * -EBUSY - completion ring does not have all the agg buffers yet
1396 * -ENOMEM - packet aborted due to out of memory
1397 * -EIO - packet aborted due to hw error indicated in BD
1398 */
1399static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001400 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001401{
1402 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001403 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001404 struct net_device *dev = bp->dev;
1405 struct rx_cmp *rxcmp;
1406 struct rx_cmp_ext *rxcmp1;
1407 u32 tmp_raw_cons = *raw_cons;
1408 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1409 struct bnxt_sw_rx_bd *rx_buf;
1410 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001411 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001412 dma_addr_t dma_addr;
1413 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001414 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001415 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001416 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001417
1418 rxcmp = (struct rx_cmp *)
1419 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1420
1421 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1422 cp_cons = RING_CMP(tmp_raw_cons);
1423 rxcmp1 = (struct rx_cmp_ext *)
1424 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1425
1426 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1427 return -EBUSY;
1428
1429 cmp_type = RX_CMP_TYPE(rxcmp);
1430
1431 prod = rxr->rx_prod;
1432
1433 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1434 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1435 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1436
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001437 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001438 goto next_rx_no_prod;
1439
1440 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1441 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1442 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001443 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001444
1445 if (unlikely(IS_ERR(skb)))
1446 return -EBUSY;
1447
1448 rc = -ENOMEM;
1449 if (likely(skb)) {
1450 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001451 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001452 rc = 1;
1453 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001454 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001455 goto next_rx_no_prod;
1456 }
1457
1458 cons = rxcmp->rx_cmp_opaque;
1459 rx_buf = &rxr->rx_buf_ring[cons];
1460 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001461 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001462 if (unlikely(cons != rxr->rx_next_cons)) {
1463 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1464
1465 bnxt_sched_reset(bp, rxr);
1466 return rc1;
1467 }
Michael Chan6bb19472017-02-06 16:55:32 -05001468 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001469
Michael Chanc61fb992017-02-06 16:55:36 -05001470 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1471 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001472
1473 if (agg_bufs) {
1474 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1475 return -EBUSY;
1476
1477 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001478 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001479 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001480 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001481
1482 rx_buf->data = NULL;
1483 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1484 bnxt_reuse_rx_data(rxr, cons, data);
1485 if (agg_bufs)
1486 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1487
1488 rc = -EIO;
1489 goto next_rx;
1490 }
1491
1492 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001493 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001494
Michael Chanc6d30e82017-02-06 16:55:42 -05001495 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1496 rc = 1;
1497 goto next_rx;
1498 }
1499
Michael Chanc0c050c2015-10-22 16:01:17 -04001500 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001501 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001502 bnxt_reuse_rx_data(rxr, cons, data);
1503 if (!skb) {
1504 rc = -ENOMEM;
1505 goto next_rx;
1506 }
1507 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001508 u32 payload;
1509
Michael Chanc6d30e82017-02-06 16:55:42 -05001510 if (rx_buf->data_ptr == data_ptr)
1511 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1512 else
1513 payload = 0;
Michael Chan6bb19472017-02-06 16:55:32 -05001514 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001515 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001516 if (!skb) {
1517 rc = -ENOMEM;
1518 goto next_rx;
1519 }
1520 }
1521
1522 if (agg_bufs) {
1523 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1524 if (!skb) {
1525 rc = -ENOMEM;
1526 goto next_rx;
1527 }
1528 }
1529
1530 if (RX_CMP_HASH_VALID(rxcmp)) {
1531 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1532 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1533
1534 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1535 if (hash_type != 1 && hash_type != 3)
1536 type = PKT_HASH_TYPE_L3;
1537 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1538 }
1539
1540 skb->protocol = eth_type_trans(skb, dev);
1541
Michael Chan8852ddb2016-06-06 02:37:16 -04001542 if ((rxcmp1->rx_cmp_flags2 &
1543 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1544 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001545 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001546 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001547 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1548
Michael Chan8852ddb2016-06-06 02:37:16 -04001549 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001550 }
1551
1552 skb_checksum_none_assert(skb);
1553 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1554 if (dev->features & NETIF_F_RXCSUM) {
1555 skb->ip_summed = CHECKSUM_UNNECESSARY;
1556 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1557 }
1558 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001559 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1560 if (dev->features & NETIF_F_RXCSUM)
1561 cpr->rx_l4_csum_errors++;
1562 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001563 }
1564
1565 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001566 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001567 rc = 1;
1568
1569next_rx:
1570 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001571 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001572
1573next_rx_no_prod:
1574 *raw_cons = tmp_raw_cons;
1575
1576 return rc;
1577}
1578
Michael Chan2270bc52017-06-23 14:01:01 -04001579/* In netpoll mode, if we are using a combined completion ring, we need to
1580 * discard the rx packets and recycle the buffers.
1581 */
1582static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1583 u32 *raw_cons, u8 *event)
1584{
1585 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1586 u32 tmp_raw_cons = *raw_cons;
1587 struct rx_cmp_ext *rxcmp1;
1588 struct rx_cmp *rxcmp;
1589 u16 cp_cons;
1590 u8 cmp_type;
1591
1592 cp_cons = RING_CMP(tmp_raw_cons);
1593 rxcmp = (struct rx_cmp *)
1594 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1595
1596 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1597 cp_cons = RING_CMP(tmp_raw_cons);
1598 rxcmp1 = (struct rx_cmp_ext *)
1599 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1600
1601 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1602 return -EBUSY;
1603
1604 cmp_type = RX_CMP_TYPE(rxcmp);
1605 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1606 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1607 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1608 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1609 struct rx_tpa_end_cmp_ext *tpa_end1;
1610
1611 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1612 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1613 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1614 }
1615 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1616}
1617
Michael Chan4bb13ab2016-04-05 14:09:01 -04001618#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001619 ((data) & \
1620 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001621
Michael Chanc0c050c2015-10-22 16:01:17 -04001622static int bnxt_async_event_process(struct bnxt *bp,
1623 struct hwrm_async_event_cmpl *cmpl)
1624{
1625 u16 event_id = le16_to_cpu(cmpl->event_id);
1626
1627 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1628 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001629 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001630 u32 data1 = le32_to_cpu(cmpl->event_data1);
1631 struct bnxt_link_info *link_info = &bp->link_info;
1632
1633 if (BNXT_VF(bp))
1634 goto async_event_process_exit;
1635 if (data1 & 0x20000) {
1636 u16 fw_speed = link_info->force_link_speed;
1637 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1638
1639 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1640 speed);
1641 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001642 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001643 /* fall thru */
1644 }
Michael Chan87c374d2016-12-02 21:17:16 -05001645 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001646 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001647 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001648 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001649 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001650 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001651 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001652 u32 data1 = le32_to_cpu(cmpl->event_data1);
1653 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1654
1655 if (BNXT_VF(bp))
1656 break;
1657
1658 if (bp->pf.port_id != port_id)
1659 break;
1660
Michael Chan4bb13ab2016-04-05 14:09:01 -04001661 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1662 break;
1663 }
Michael Chan87c374d2016-12-02 21:17:16 -05001664 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001665 if (BNXT_PF(bp))
1666 goto async_event_process_exit;
1667 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1668 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001669 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001670 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001671 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001672 schedule_work(&bp->sp_task);
1673async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001674 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001675 return 0;
1676}
1677
1678static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1679{
1680 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1681 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1682 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1683 (struct hwrm_fwd_req_cmpl *)txcmp;
1684
1685 switch (cmpl_type) {
1686 case CMPL_BASE_TYPE_HWRM_DONE:
1687 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1688 if (seq_id == bp->hwrm_intr_seq_id)
1689 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1690 else
1691 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1692 break;
1693
1694 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1695 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1696
1697 if ((vf_id < bp->pf.first_vf_id) ||
1698 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1699 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1700 vf_id);
1701 return -EINVAL;
1702 }
1703
1704 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1705 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1706 schedule_work(&bp->sp_task);
1707 break;
1708
1709 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1710 bnxt_async_event_process(bp,
1711 (struct hwrm_async_event_cmpl *)txcmp);
1712
1713 default:
1714 break;
1715 }
1716
1717 return 0;
1718}
1719
1720static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1721{
1722 struct bnxt_napi *bnapi = dev_instance;
1723 struct bnxt *bp = bnapi->bp;
1724 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1725 u32 cons = RING_CMP(cpr->cp_raw_cons);
1726
1727 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1728 napi_schedule(&bnapi->napi);
1729 return IRQ_HANDLED;
1730}
1731
1732static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1733{
1734 u32 raw_cons = cpr->cp_raw_cons;
1735 u16 cons = RING_CMP(raw_cons);
1736 struct tx_cmp *txcmp;
1737
1738 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1739
1740 return TX_CMP_VALID(txcmp, raw_cons);
1741}
1742
Michael Chanc0c050c2015-10-22 16:01:17 -04001743static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1744{
1745 struct bnxt_napi *bnapi = dev_instance;
1746 struct bnxt *bp = bnapi->bp;
1747 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1748 u32 cons = RING_CMP(cpr->cp_raw_cons);
1749 u32 int_status;
1750
1751 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1752
1753 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001754 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001755 /* return if erroneous interrupt */
1756 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1757 return IRQ_NONE;
1758 }
1759
1760 /* disable ring IRQ */
1761 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1762
1763 /* Return here if interrupt is shared and is disabled. */
1764 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1765 return IRQ_HANDLED;
1766
1767 napi_schedule(&bnapi->napi);
1768 return IRQ_HANDLED;
1769}
1770
1771static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1772{
1773 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1774 u32 raw_cons = cpr->cp_raw_cons;
1775 u32 cons;
1776 int tx_pkts = 0;
1777 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001778 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001779 struct tx_cmp *txcmp;
1780
1781 while (1) {
1782 int rc;
1783
1784 cons = RING_CMP(raw_cons);
1785 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1786
1787 if (!TX_CMP_VALID(txcmp, raw_cons))
1788 break;
1789
Michael Chan67a95e22016-05-04 16:56:43 -04001790 /* The valid test of the entry must be done first before
1791 * reading any further.
1792 */
Michael Chanb67daab2016-05-15 03:04:51 -04001793 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001794 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1795 tx_pkts++;
1796 /* return full budget so NAPI will complete. */
1797 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1798 rx_pkts = budget;
1799 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan2270bc52017-06-23 14:01:01 -04001800 if (likely(budget))
1801 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1802 else
1803 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1804 &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001805 if (likely(rc >= 0))
1806 rx_pkts += rc;
1807 else if (rc == -EBUSY) /* partial completion */
1808 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001809 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1810 CMPL_BASE_TYPE_HWRM_DONE) ||
1811 (TX_CMP_TYPE(txcmp) ==
1812 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1813 (TX_CMP_TYPE(txcmp) ==
1814 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1815 bnxt_hwrm_handler(bp, txcmp);
1816 }
1817 raw_cons = NEXT_RAW_CMP(raw_cons);
1818
1819 if (rx_pkts == budget)
1820 break;
1821 }
1822
Michael Chan38413402017-02-06 16:55:43 -05001823 if (event & BNXT_TX_EVENT) {
1824 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1825 void __iomem *db = txr->tx_doorbell;
1826 u16 prod = txr->tx_prod;
1827
1828 /* Sync BD data before updating doorbell */
1829 wmb();
1830
Michael Chan434c9752017-05-29 19:06:08 -04001831 bnxt_db_write(bp, db, DB_KEY_TX | prod);
Michael Chan38413402017-02-06 16:55:43 -05001832 }
1833
Michael Chanc0c050c2015-10-22 16:01:17 -04001834 cpr->cp_raw_cons = raw_cons;
1835 /* ACK completion ring before freeing tx ring and producing new
1836 * buffers in rx/agg rings to prevent overflowing the completion
1837 * ring.
1838 */
1839 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1840
1841 if (tx_pkts)
Michael Chanfa3e93e2017-02-06 16:55:41 -05001842 bnapi->tx_int(bp, bnapi, tx_pkts);
Michael Chanc0c050c2015-10-22 16:01:17 -04001843
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001844 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001845 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001846
Michael Chan434c9752017-05-29 19:06:08 -04001847 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1848 if (event & BNXT_AGG_EVENT)
1849 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1850 DB_KEY_RX | rxr->rx_agg_prod);
Michael Chanc0c050c2015-10-22 16:01:17 -04001851 }
1852 return rx_pkts;
1853}
1854
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001855static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1856{
1857 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1858 struct bnxt *bp = bnapi->bp;
1859 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1860 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1861 struct tx_cmp *txcmp;
1862 struct rx_cmp_ext *rxcmp1;
1863 u32 cp_cons, tmp_raw_cons;
1864 u32 raw_cons = cpr->cp_raw_cons;
1865 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001866 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001867
1868 while (1) {
1869 int rc;
1870
1871 cp_cons = RING_CMP(raw_cons);
1872 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1873
1874 if (!TX_CMP_VALID(txcmp, raw_cons))
1875 break;
1876
1877 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1878 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1879 cp_cons = RING_CMP(tmp_raw_cons);
1880 rxcmp1 = (struct rx_cmp_ext *)
1881 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1882
1883 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1884 break;
1885
1886 /* force an error to recycle the buffer */
1887 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1888 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1889
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001890 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001891 if (likely(rc == -EIO))
1892 rx_pkts++;
1893 else if (rc == -EBUSY) /* partial completion */
1894 break;
1895 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1896 CMPL_BASE_TYPE_HWRM_DONE)) {
1897 bnxt_hwrm_handler(bp, txcmp);
1898 } else {
1899 netdev_err(bp->dev,
1900 "Invalid completion received on special ring\n");
1901 }
1902 raw_cons = NEXT_RAW_CMP(raw_cons);
1903
1904 if (rx_pkts == budget)
1905 break;
1906 }
1907
1908 cpr->cp_raw_cons = raw_cons;
1909 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan434c9752017-05-29 19:06:08 -04001910 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001911
Michael Chan434c9752017-05-29 19:06:08 -04001912 if (event & BNXT_AGG_EVENT)
1913 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1914 DB_KEY_RX | rxr->rx_agg_prod);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001915
1916 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001917 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001918 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1919 }
1920 return rx_pkts;
1921}
1922
Michael Chanc0c050c2015-10-22 16:01:17 -04001923static int bnxt_poll(struct napi_struct *napi, int budget)
1924{
1925 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1926 struct bnxt *bp = bnapi->bp;
1927 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1928 int work_done = 0;
1929
Michael Chanc0c050c2015-10-22 16:01:17 -04001930 while (1) {
1931 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1932
1933 if (work_done >= budget)
1934 break;
1935
1936 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001937 if (napi_complete_done(napi, work_done))
1938 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1939 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001940 break;
1941 }
1942 }
1943 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001944 return work_done;
1945}
1946
Michael Chanc0c050c2015-10-22 16:01:17 -04001947static void bnxt_free_tx_skbs(struct bnxt *bp)
1948{
1949 int i, max_idx;
1950 struct pci_dev *pdev = bp->pdev;
1951
Michael Chanb6ab4b02016-01-02 23:44:59 -05001952 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001953 return;
1954
1955 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1956 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001957 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001958 int j;
1959
Michael Chanc0c050c2015-10-22 16:01:17 -04001960 for (j = 0; j < max_idx;) {
1961 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1962 struct sk_buff *skb = tx_buf->skb;
1963 int k, last;
1964
1965 if (!skb) {
1966 j++;
1967 continue;
1968 }
1969
1970 tx_buf->skb = NULL;
1971
1972 if (tx_buf->is_push) {
1973 dev_kfree_skb(skb);
1974 j += 2;
1975 continue;
1976 }
1977
1978 dma_unmap_single(&pdev->dev,
1979 dma_unmap_addr(tx_buf, mapping),
1980 skb_headlen(skb),
1981 PCI_DMA_TODEVICE);
1982
1983 last = tx_buf->nr_frags;
1984 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001985 for (k = 0; k < last; k++, j++) {
1986 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001987 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1988
Michael Chand612a572016-01-28 03:11:22 -05001989 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001990 dma_unmap_page(
1991 &pdev->dev,
1992 dma_unmap_addr(tx_buf, mapping),
1993 skb_frag_size(frag), PCI_DMA_TODEVICE);
1994 }
1995 dev_kfree_skb(skb);
1996 }
1997 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1998 }
1999}
2000
2001static void bnxt_free_rx_skbs(struct bnxt *bp)
2002{
2003 int i, max_idx, max_agg_idx;
2004 struct pci_dev *pdev = bp->pdev;
2005
Michael Chanb6ab4b02016-01-02 23:44:59 -05002006 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002007 return;
2008
2009 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2010 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2011 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002012 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002013 int j;
2014
Michael Chanc0c050c2015-10-22 16:01:17 -04002015 if (rxr->rx_tpa) {
2016 for (j = 0; j < MAX_TPA; j++) {
2017 struct bnxt_tpa_info *tpa_info =
2018 &rxr->rx_tpa[j];
2019 u8 *data = tpa_info->data;
2020
2021 if (!data)
2022 continue;
2023
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002024 dma_unmap_single_attrs(&pdev->dev,
2025 tpa_info->mapping,
2026 bp->rx_buf_use_size,
2027 bp->rx_dir,
2028 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002029
2030 tpa_info->data = NULL;
2031
2032 kfree(data);
2033 }
2034 }
2035
2036 for (j = 0; j < max_idx; j++) {
2037 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan3ed3a832017-03-28 19:47:31 -04002038 dma_addr_t mapping = rx_buf->mapping;
Michael Chan6bb19472017-02-06 16:55:32 -05002039 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04002040
2041 if (!data)
2042 continue;
2043
Michael Chanc0c050c2015-10-22 16:01:17 -04002044 rx_buf->data = NULL;
2045
Michael Chan3ed3a832017-03-28 19:47:31 -04002046 if (BNXT_RX_PAGE_MODE(bp)) {
2047 mapping -= bp->rx_dma_offset;
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002048 dma_unmap_page_attrs(&pdev->dev, mapping,
2049 PAGE_SIZE, bp->rx_dir,
2050 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002051 __free_page(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002052 } else {
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002053 dma_unmap_single_attrs(&pdev->dev, mapping,
2054 bp->rx_buf_use_size,
2055 bp->rx_dir,
2056 DMA_ATTR_WEAK_ORDERING);
Michael Chanc61fb992017-02-06 16:55:36 -05002057 kfree(data);
Michael Chan3ed3a832017-03-28 19:47:31 -04002058 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002059 }
2060
2061 for (j = 0; j < max_agg_idx; j++) {
2062 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2063 &rxr->rx_agg_ring[j];
2064 struct page *page = rx_agg_buf->page;
2065
2066 if (!page)
2067 continue;
2068
Shannon Nelsonc519fe92017-05-09 18:30:12 -07002069 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2070 BNXT_RX_PAGE_SIZE,
2071 PCI_DMA_FROMDEVICE,
2072 DMA_ATTR_WEAK_ORDERING);
Michael Chanc0c050c2015-10-22 16:01:17 -04002073
2074 rx_agg_buf->page = NULL;
2075 __clear_bit(j, rxr->rx_agg_bmap);
2076
2077 __free_page(page);
2078 }
Michael Chan89d0a062016-04-25 02:30:51 -04002079 if (rxr->rx_page) {
2080 __free_page(rxr->rx_page);
2081 rxr->rx_page = NULL;
2082 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002083 }
2084}
2085
2086static void bnxt_free_skbs(struct bnxt *bp)
2087{
2088 bnxt_free_tx_skbs(bp);
2089 bnxt_free_rx_skbs(bp);
2090}
2091
2092static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2093{
2094 struct pci_dev *pdev = bp->pdev;
2095 int i;
2096
2097 for (i = 0; i < ring->nr_pages; i++) {
2098 if (!ring->pg_arr[i])
2099 continue;
2100
2101 dma_free_coherent(&pdev->dev, ring->page_size,
2102 ring->pg_arr[i], ring->dma_arr[i]);
2103
2104 ring->pg_arr[i] = NULL;
2105 }
2106 if (ring->pg_tbl) {
2107 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2108 ring->pg_tbl, ring->pg_tbl_map);
2109 ring->pg_tbl = NULL;
2110 }
2111 if (ring->vmem_size && *ring->vmem) {
2112 vfree(*ring->vmem);
2113 *ring->vmem = NULL;
2114 }
2115}
2116
2117static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2118{
2119 int i;
2120 struct pci_dev *pdev = bp->pdev;
2121
2122 if (ring->nr_pages > 1) {
2123 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2124 ring->nr_pages * 8,
2125 &ring->pg_tbl_map,
2126 GFP_KERNEL);
2127 if (!ring->pg_tbl)
2128 return -ENOMEM;
2129 }
2130
2131 for (i = 0; i < ring->nr_pages; i++) {
2132 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2133 ring->page_size,
2134 &ring->dma_arr[i],
2135 GFP_KERNEL);
2136 if (!ring->pg_arr[i])
2137 return -ENOMEM;
2138
2139 if (ring->nr_pages > 1)
2140 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2141 }
2142
2143 if (ring->vmem_size) {
2144 *ring->vmem = vzalloc(ring->vmem_size);
2145 if (!(*ring->vmem))
2146 return -ENOMEM;
2147 }
2148 return 0;
2149}
2150
2151static void bnxt_free_rx_rings(struct bnxt *bp)
2152{
2153 int i;
2154
Michael Chanb6ab4b02016-01-02 23:44:59 -05002155 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002156 return;
2157
2158 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002159 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002160 struct bnxt_ring_struct *ring;
2161
Michael Chanc6d30e82017-02-06 16:55:42 -05002162 if (rxr->xdp_prog)
2163 bpf_prog_put(rxr->xdp_prog);
2164
Michael Chanc0c050c2015-10-22 16:01:17 -04002165 kfree(rxr->rx_tpa);
2166 rxr->rx_tpa = NULL;
2167
2168 kfree(rxr->rx_agg_bmap);
2169 rxr->rx_agg_bmap = NULL;
2170
2171 ring = &rxr->rx_ring_struct;
2172 bnxt_free_ring(bp, ring);
2173
2174 ring = &rxr->rx_agg_ring_struct;
2175 bnxt_free_ring(bp, ring);
2176 }
2177}
2178
2179static int bnxt_alloc_rx_rings(struct bnxt *bp)
2180{
2181 int i, rc, agg_rings = 0, tpa_rings = 0;
2182
Michael Chanb6ab4b02016-01-02 23:44:59 -05002183 if (!bp->rx_ring)
2184 return -ENOMEM;
2185
Michael Chanc0c050c2015-10-22 16:01:17 -04002186 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2187 agg_rings = 1;
2188
2189 if (bp->flags & BNXT_FLAG_TPA)
2190 tpa_rings = 1;
2191
2192 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002193 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002194 struct bnxt_ring_struct *ring;
2195
Michael Chanc0c050c2015-10-22 16:01:17 -04002196 ring = &rxr->rx_ring_struct;
2197
2198 rc = bnxt_alloc_ring(bp, ring);
2199 if (rc)
2200 return rc;
2201
2202 if (agg_rings) {
2203 u16 mem_size;
2204
2205 ring = &rxr->rx_agg_ring_struct;
2206 rc = bnxt_alloc_ring(bp, ring);
2207 if (rc)
2208 return rc;
2209
2210 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2211 mem_size = rxr->rx_agg_bmap_size / 8;
2212 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2213 if (!rxr->rx_agg_bmap)
2214 return -ENOMEM;
2215
2216 if (tpa_rings) {
2217 rxr->rx_tpa = kcalloc(MAX_TPA,
2218 sizeof(struct bnxt_tpa_info),
2219 GFP_KERNEL);
2220 if (!rxr->rx_tpa)
2221 return -ENOMEM;
2222 }
2223 }
2224 }
2225 return 0;
2226}
2227
2228static void bnxt_free_tx_rings(struct bnxt *bp)
2229{
2230 int i;
2231 struct pci_dev *pdev = bp->pdev;
2232
Michael Chanb6ab4b02016-01-02 23:44:59 -05002233 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002234 return;
2235
2236 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002237 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002238 struct bnxt_ring_struct *ring;
2239
Michael Chanc0c050c2015-10-22 16:01:17 -04002240 if (txr->tx_push) {
2241 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2242 txr->tx_push, txr->tx_push_mapping);
2243 txr->tx_push = NULL;
2244 }
2245
2246 ring = &txr->tx_ring_struct;
2247
2248 bnxt_free_ring(bp, ring);
2249 }
2250}
2251
2252static int bnxt_alloc_tx_rings(struct bnxt *bp)
2253{
2254 int i, j, rc;
2255 struct pci_dev *pdev = bp->pdev;
2256
2257 bp->tx_push_size = 0;
2258 if (bp->tx_push_thresh) {
2259 int push_size;
2260
2261 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2262 bp->tx_push_thresh);
2263
Michael Chan4419dbe2016-02-10 17:33:49 -05002264 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002265 push_size = 0;
2266 bp->tx_push_thresh = 0;
2267 }
2268
2269 bp->tx_push_size = push_size;
2270 }
2271
2272 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002273 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002274 struct bnxt_ring_struct *ring;
2275
Michael Chanc0c050c2015-10-22 16:01:17 -04002276 ring = &txr->tx_ring_struct;
2277
2278 rc = bnxt_alloc_ring(bp, ring);
2279 if (rc)
2280 return rc;
2281
2282 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002283 dma_addr_t mapping;
2284
2285 /* One pre-allocated DMA buffer to backup
2286 * TX push operation
2287 */
2288 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2289 bp->tx_push_size,
2290 &txr->tx_push_mapping,
2291 GFP_KERNEL);
2292
2293 if (!txr->tx_push)
2294 return -ENOMEM;
2295
Michael Chanc0c050c2015-10-22 16:01:17 -04002296 mapping = txr->tx_push_mapping +
2297 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002298 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002299
Michael Chan4419dbe2016-02-10 17:33:49 -05002300 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002301 }
2302 ring->queue_id = bp->q_info[j].queue_id;
Michael Chan5f449242017-02-06 16:55:40 -05002303 if (i < bp->tx_nr_rings_xdp)
2304 continue;
Michael Chanc0c050c2015-10-22 16:01:17 -04002305 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2306 j++;
2307 }
2308 return 0;
2309}
2310
2311static void bnxt_free_cp_rings(struct bnxt *bp)
2312{
2313 int i;
2314
2315 if (!bp->bnapi)
2316 return;
2317
2318 for (i = 0; i < bp->cp_nr_rings; i++) {
2319 struct bnxt_napi *bnapi = bp->bnapi[i];
2320 struct bnxt_cp_ring_info *cpr;
2321 struct bnxt_ring_struct *ring;
2322
2323 if (!bnapi)
2324 continue;
2325
2326 cpr = &bnapi->cp_ring;
2327 ring = &cpr->cp_ring_struct;
2328
2329 bnxt_free_ring(bp, ring);
2330 }
2331}
2332
2333static int bnxt_alloc_cp_rings(struct bnxt *bp)
2334{
2335 int i, rc;
2336
2337 for (i = 0; i < bp->cp_nr_rings; i++) {
2338 struct bnxt_napi *bnapi = bp->bnapi[i];
2339 struct bnxt_cp_ring_info *cpr;
2340 struct bnxt_ring_struct *ring;
2341
2342 if (!bnapi)
2343 continue;
2344
2345 cpr = &bnapi->cp_ring;
2346 ring = &cpr->cp_ring_struct;
2347
2348 rc = bnxt_alloc_ring(bp, ring);
2349 if (rc)
2350 return rc;
2351 }
2352 return 0;
2353}
2354
2355static void bnxt_init_ring_struct(struct bnxt *bp)
2356{
2357 int i;
2358
2359 for (i = 0; i < bp->cp_nr_rings; i++) {
2360 struct bnxt_napi *bnapi = bp->bnapi[i];
2361 struct bnxt_cp_ring_info *cpr;
2362 struct bnxt_rx_ring_info *rxr;
2363 struct bnxt_tx_ring_info *txr;
2364 struct bnxt_ring_struct *ring;
2365
2366 if (!bnapi)
2367 continue;
2368
2369 cpr = &bnapi->cp_ring;
2370 ring = &cpr->cp_ring_struct;
2371 ring->nr_pages = bp->cp_nr_pages;
2372 ring->page_size = HW_CMPD_RING_SIZE;
2373 ring->pg_arr = (void **)cpr->cp_desc_ring;
2374 ring->dma_arr = cpr->cp_desc_mapping;
2375 ring->vmem_size = 0;
2376
Michael Chanb6ab4b02016-01-02 23:44:59 -05002377 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002378 if (!rxr)
2379 goto skip_rx;
2380
Michael Chanc0c050c2015-10-22 16:01:17 -04002381 ring = &rxr->rx_ring_struct;
2382 ring->nr_pages = bp->rx_nr_pages;
2383 ring->page_size = HW_RXBD_RING_SIZE;
2384 ring->pg_arr = (void **)rxr->rx_desc_ring;
2385 ring->dma_arr = rxr->rx_desc_mapping;
2386 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2387 ring->vmem = (void **)&rxr->rx_buf_ring;
2388
2389 ring = &rxr->rx_agg_ring_struct;
2390 ring->nr_pages = bp->rx_agg_nr_pages;
2391 ring->page_size = HW_RXBD_RING_SIZE;
2392 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2393 ring->dma_arr = rxr->rx_agg_desc_mapping;
2394 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2395 ring->vmem = (void **)&rxr->rx_agg_ring;
2396
Michael Chan3b2b7d92016-01-02 23:45:00 -05002397skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002398 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002399 if (!txr)
2400 continue;
2401
Michael Chanc0c050c2015-10-22 16:01:17 -04002402 ring = &txr->tx_ring_struct;
2403 ring->nr_pages = bp->tx_nr_pages;
2404 ring->page_size = HW_RXBD_RING_SIZE;
2405 ring->pg_arr = (void **)txr->tx_desc_ring;
2406 ring->dma_arr = txr->tx_desc_mapping;
2407 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2408 ring->vmem = (void **)&txr->tx_buf_ring;
2409 }
2410}
2411
2412static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2413{
2414 int i;
2415 u32 prod;
2416 struct rx_bd **rx_buf_ring;
2417
2418 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2419 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2420 int j;
2421 struct rx_bd *rxbd;
2422
2423 rxbd = rx_buf_ring[i];
2424 if (!rxbd)
2425 continue;
2426
2427 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2428 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2429 rxbd->rx_bd_opaque = prod;
2430 }
2431 }
2432}
2433
2434static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2435{
2436 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002437 struct bnxt_rx_ring_info *rxr;
2438 struct bnxt_ring_struct *ring;
2439 u32 prod, type;
2440 int i;
2441
Michael Chanc0c050c2015-10-22 16:01:17 -04002442 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2443 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2444
2445 if (NET_IP_ALIGN == 2)
2446 type |= RX_BD_FLAGS_SOP;
2447
Michael Chanb6ab4b02016-01-02 23:44:59 -05002448 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002449 ring = &rxr->rx_ring_struct;
2450 bnxt_init_rxbd_pages(ring, type);
2451
Michael Chanc6d30e82017-02-06 16:55:42 -05002452 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2453 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2454 if (IS_ERR(rxr->xdp_prog)) {
2455 int rc = PTR_ERR(rxr->xdp_prog);
2456
2457 rxr->xdp_prog = NULL;
2458 return rc;
2459 }
2460 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002461 prod = rxr->rx_prod;
2462 for (i = 0; i < bp->rx_ring_size; i++) {
2463 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2464 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2465 ring_nr, i, bp->rx_ring_size);
2466 break;
2467 }
2468 prod = NEXT_RX(prod);
2469 }
2470 rxr->rx_prod = prod;
2471 ring->fw_ring_id = INVALID_HW_RING_ID;
2472
Michael Chanedd0c2c2015-12-27 18:19:19 -05002473 ring = &rxr->rx_agg_ring_struct;
2474 ring->fw_ring_id = INVALID_HW_RING_ID;
2475
Michael Chanc0c050c2015-10-22 16:01:17 -04002476 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2477 return 0;
2478
Michael Chan2839f282016-04-25 02:30:50 -04002479 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002480 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2481
2482 bnxt_init_rxbd_pages(ring, type);
2483
2484 prod = rxr->rx_agg_prod;
2485 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2486 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2487 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2488 ring_nr, i, bp->rx_ring_size);
2489 break;
2490 }
2491 prod = NEXT_RX_AGG(prod);
2492 }
2493 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002494
2495 if (bp->flags & BNXT_FLAG_TPA) {
2496 if (rxr->rx_tpa) {
2497 u8 *data;
2498 dma_addr_t mapping;
2499
2500 for (i = 0; i < MAX_TPA; i++) {
2501 data = __bnxt_alloc_rx_data(bp, &mapping,
2502 GFP_KERNEL);
2503 if (!data)
2504 return -ENOMEM;
2505
2506 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002507 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002508 rxr->rx_tpa[i].mapping = mapping;
2509 }
2510 } else {
2511 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2512 return -ENOMEM;
2513 }
2514 }
2515
2516 return 0;
2517}
2518
Sankar Patchineelam22479252017-03-28 19:47:29 -04002519static void bnxt_init_cp_rings(struct bnxt *bp)
2520{
2521 int i;
2522
2523 for (i = 0; i < bp->cp_nr_rings; i++) {
2524 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2525 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2526
2527 ring->fw_ring_id = INVALID_HW_RING_ID;
2528 }
2529}
2530
Michael Chanc0c050c2015-10-22 16:01:17 -04002531static int bnxt_init_rx_rings(struct bnxt *bp)
2532{
2533 int i, rc = 0;
2534
Michael Chanc61fb992017-02-06 16:55:36 -05002535 if (BNXT_RX_PAGE_MODE(bp)) {
Michael Chanc6d30e82017-02-06 16:55:42 -05002536 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2537 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
Michael Chanc61fb992017-02-06 16:55:36 -05002538 } else {
2539 bp->rx_offset = BNXT_RX_OFFSET;
2540 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2541 }
Michael Chanb3dba772017-02-06 16:55:35 -05002542
Michael Chanc0c050c2015-10-22 16:01:17 -04002543 for (i = 0; i < bp->rx_nr_rings; i++) {
2544 rc = bnxt_init_one_rx_ring(bp, i);
2545 if (rc)
2546 break;
2547 }
2548
2549 return rc;
2550}
2551
2552static int bnxt_init_tx_rings(struct bnxt *bp)
2553{
2554 u16 i;
2555
2556 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2557 MAX_SKB_FRAGS + 1);
2558
2559 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002560 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002561 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2562
2563 ring->fw_ring_id = INVALID_HW_RING_ID;
2564 }
2565
2566 return 0;
2567}
2568
2569static void bnxt_free_ring_grps(struct bnxt *bp)
2570{
2571 kfree(bp->grp_info);
2572 bp->grp_info = NULL;
2573}
2574
2575static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2576{
2577 int i;
2578
2579 if (irq_re_init) {
2580 bp->grp_info = kcalloc(bp->cp_nr_rings,
2581 sizeof(struct bnxt_ring_grp_info),
2582 GFP_KERNEL);
2583 if (!bp->grp_info)
2584 return -ENOMEM;
2585 }
2586 for (i = 0; i < bp->cp_nr_rings; i++) {
2587 if (irq_re_init)
2588 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2589 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2590 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2591 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2592 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2593 }
2594 return 0;
2595}
2596
2597static void bnxt_free_vnics(struct bnxt *bp)
2598{
2599 kfree(bp->vnic_info);
2600 bp->vnic_info = NULL;
2601 bp->nr_vnics = 0;
2602}
2603
2604static int bnxt_alloc_vnics(struct bnxt *bp)
2605{
2606 int num_vnics = 1;
2607
2608#ifdef CONFIG_RFS_ACCEL
2609 if (bp->flags & BNXT_FLAG_RFS)
2610 num_vnics += bp->rx_nr_rings;
2611#endif
2612
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002613 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2614 num_vnics++;
2615
Michael Chanc0c050c2015-10-22 16:01:17 -04002616 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2617 GFP_KERNEL);
2618 if (!bp->vnic_info)
2619 return -ENOMEM;
2620
2621 bp->nr_vnics = num_vnics;
2622 return 0;
2623}
2624
2625static void bnxt_init_vnics(struct bnxt *bp)
2626{
2627 int i;
2628
2629 for (i = 0; i < bp->nr_vnics; i++) {
2630 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2631
2632 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002633 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2634 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002635 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2636
2637 if (bp->vnic_info[i].rss_hash_key) {
2638 if (i == 0)
2639 prandom_bytes(vnic->rss_hash_key,
2640 HW_HASH_KEY_SIZE);
2641 else
2642 memcpy(vnic->rss_hash_key,
2643 bp->vnic_info[0].rss_hash_key,
2644 HW_HASH_KEY_SIZE);
2645 }
2646 }
2647}
2648
2649static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2650{
2651 int pages;
2652
2653 pages = ring_size / desc_per_pg;
2654
2655 if (!pages)
2656 return 1;
2657
2658 pages++;
2659
2660 while (pages & (pages - 1))
2661 pages++;
2662
2663 return pages;
2664}
2665
Michael Chanc6d30e82017-02-06 16:55:42 -05002666void bnxt_set_tpa_flags(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04002667{
2668 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002669 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2670 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002671 if (bp->dev->features & NETIF_F_LRO)
2672 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002673 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002674 bp->flags |= BNXT_FLAG_GRO;
2675}
2676
2677/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2678 * be set on entry.
2679 */
2680void bnxt_set_ring_params(struct bnxt *bp)
2681{
2682 u32 ring_size, rx_size, rx_space;
2683 u32 agg_factor = 0, agg_ring_size = 0;
2684
2685 /* 8 for CRC and VLAN */
2686 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2687
2688 rx_space = rx_size + NET_SKB_PAD +
2689 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2690
2691 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2692 ring_size = bp->rx_ring_size;
2693 bp->rx_agg_ring_size = 0;
2694 bp->rx_agg_nr_pages = 0;
2695
2696 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002697 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002698
2699 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002700 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002701 u32 jumbo_factor;
2702
2703 bp->flags |= BNXT_FLAG_JUMBO;
2704 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2705 if (jumbo_factor > agg_factor)
2706 agg_factor = jumbo_factor;
2707 }
2708 agg_ring_size = ring_size * agg_factor;
2709
2710 if (agg_ring_size) {
2711 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2712 RX_DESC_CNT);
2713 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2714 u32 tmp = agg_ring_size;
2715
2716 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2717 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2718 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2719 tmp, agg_ring_size);
2720 }
2721 bp->rx_agg_ring_size = agg_ring_size;
2722 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2723 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2724 rx_space = rx_size + NET_SKB_PAD +
2725 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2726 }
2727
2728 bp->rx_buf_use_size = rx_size;
2729 bp->rx_buf_size = rx_space;
2730
2731 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2732 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2733
2734 ring_size = bp->tx_ring_size;
2735 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2736 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2737
2738 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2739 bp->cp_ring_size = ring_size;
2740
2741 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2742 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2743 bp->cp_nr_pages = MAX_CP_PAGES;
2744 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2745 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2746 ring_size, bp->cp_ring_size);
2747 }
2748 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2749 bp->cp_ring_mask = bp->cp_bit - 1;
2750}
2751
Michael Chanc61fb992017-02-06 16:55:36 -05002752int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002753{
Michael Chanc61fb992017-02-06 16:55:36 -05002754 if (page_mode) {
2755 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2756 return -EOPNOTSUPP;
2757 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2758 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2759 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2760 bp->dev->hw_features &= ~NETIF_F_LRO;
2761 bp->dev->features &= ~NETIF_F_LRO;
2762 bp->rx_dir = DMA_BIDIRECTIONAL;
2763 bp->rx_skb_func = bnxt_rx_page_skb;
2764 } else {
2765 bp->dev->max_mtu = BNXT_MAX_MTU;
2766 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2767 bp->rx_dir = DMA_FROM_DEVICE;
2768 bp->rx_skb_func = bnxt_rx_skb;
2769 }
Michael Chan6bb19472017-02-06 16:55:32 -05002770 return 0;
2771}
2772
Michael Chanc0c050c2015-10-22 16:01:17 -04002773static void bnxt_free_vnic_attributes(struct bnxt *bp)
2774{
2775 int i;
2776 struct bnxt_vnic_info *vnic;
2777 struct pci_dev *pdev = bp->pdev;
2778
2779 if (!bp->vnic_info)
2780 return;
2781
2782 for (i = 0; i < bp->nr_vnics; i++) {
2783 vnic = &bp->vnic_info[i];
2784
2785 kfree(vnic->fw_grp_ids);
2786 vnic->fw_grp_ids = NULL;
2787
2788 kfree(vnic->uc_list);
2789 vnic->uc_list = NULL;
2790
2791 if (vnic->mc_list) {
2792 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2793 vnic->mc_list, vnic->mc_list_mapping);
2794 vnic->mc_list = NULL;
2795 }
2796
2797 if (vnic->rss_table) {
2798 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2799 vnic->rss_table,
2800 vnic->rss_table_dma_addr);
2801 vnic->rss_table = NULL;
2802 }
2803
2804 vnic->rss_hash_key = NULL;
2805 vnic->flags = 0;
2806 }
2807}
2808
2809static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2810{
2811 int i, rc = 0, size;
2812 struct bnxt_vnic_info *vnic;
2813 struct pci_dev *pdev = bp->pdev;
2814 int max_rings;
2815
2816 for (i = 0; i < bp->nr_vnics; i++) {
2817 vnic = &bp->vnic_info[i];
2818
2819 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2820 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2821
2822 if (mem_size > 0) {
2823 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2824 if (!vnic->uc_list) {
2825 rc = -ENOMEM;
2826 goto out;
2827 }
2828 }
2829 }
2830
2831 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2832 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2833 vnic->mc_list =
2834 dma_alloc_coherent(&pdev->dev,
2835 vnic->mc_list_size,
2836 &vnic->mc_list_mapping,
2837 GFP_KERNEL);
2838 if (!vnic->mc_list) {
2839 rc = -ENOMEM;
2840 goto out;
2841 }
2842 }
2843
2844 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2845 max_rings = bp->rx_nr_rings;
2846 else
2847 max_rings = 1;
2848
2849 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2850 if (!vnic->fw_grp_ids) {
2851 rc = -ENOMEM;
2852 goto out;
2853 }
2854
Michael Chanae10ae72016-12-29 12:13:38 -05002855 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2856 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2857 continue;
2858
Michael Chanc0c050c2015-10-22 16:01:17 -04002859 /* Allocate rss table and hash key */
2860 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2861 &vnic->rss_table_dma_addr,
2862 GFP_KERNEL);
2863 if (!vnic->rss_table) {
2864 rc = -ENOMEM;
2865 goto out;
2866 }
2867
2868 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2869
2870 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2871 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2872 }
2873 return 0;
2874
2875out:
2876 return rc;
2877}
2878
2879static void bnxt_free_hwrm_resources(struct bnxt *bp)
2880{
2881 struct pci_dev *pdev = bp->pdev;
2882
2883 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2884 bp->hwrm_cmd_resp_dma_addr);
2885
2886 bp->hwrm_cmd_resp_addr = NULL;
2887 if (bp->hwrm_dbg_resp_addr) {
2888 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2889 bp->hwrm_dbg_resp_addr,
2890 bp->hwrm_dbg_resp_dma_addr);
2891
2892 bp->hwrm_dbg_resp_addr = NULL;
2893 }
2894}
2895
2896static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2897{
2898 struct pci_dev *pdev = bp->pdev;
2899
2900 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2901 &bp->hwrm_cmd_resp_dma_addr,
2902 GFP_KERNEL);
2903 if (!bp->hwrm_cmd_resp_addr)
2904 return -ENOMEM;
2905 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2906 HWRM_DBG_REG_BUF_SIZE,
2907 &bp->hwrm_dbg_resp_dma_addr,
2908 GFP_KERNEL);
2909 if (!bp->hwrm_dbg_resp_addr)
2910 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2911
2912 return 0;
2913}
2914
Deepak Khungare605db82017-05-29 19:06:04 -04002915static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2916{
2917 if (bp->hwrm_short_cmd_req_addr) {
2918 struct pci_dev *pdev = bp->pdev;
2919
2920 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2921 bp->hwrm_short_cmd_req_addr,
2922 bp->hwrm_short_cmd_req_dma_addr);
2923 bp->hwrm_short_cmd_req_addr = NULL;
2924 }
2925}
2926
2927static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2928{
2929 struct pci_dev *pdev = bp->pdev;
2930
2931 bp->hwrm_short_cmd_req_addr =
2932 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2933 &bp->hwrm_short_cmd_req_dma_addr,
2934 GFP_KERNEL);
2935 if (!bp->hwrm_short_cmd_req_addr)
2936 return -ENOMEM;
2937
2938 return 0;
2939}
2940
Michael Chanc0c050c2015-10-22 16:01:17 -04002941static void bnxt_free_stats(struct bnxt *bp)
2942{
2943 u32 size, i;
2944 struct pci_dev *pdev = bp->pdev;
2945
Michael Chan3bdf56c2016-03-07 15:38:45 -05002946 if (bp->hw_rx_port_stats) {
2947 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2948 bp->hw_rx_port_stats,
2949 bp->hw_rx_port_stats_map);
2950 bp->hw_rx_port_stats = NULL;
2951 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2952 }
2953
Michael Chanc0c050c2015-10-22 16:01:17 -04002954 if (!bp->bnapi)
2955 return;
2956
2957 size = sizeof(struct ctx_hw_stats);
2958
2959 for (i = 0; i < bp->cp_nr_rings; i++) {
2960 struct bnxt_napi *bnapi = bp->bnapi[i];
2961 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2962
2963 if (cpr->hw_stats) {
2964 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2965 cpr->hw_stats_map);
2966 cpr->hw_stats = NULL;
2967 }
2968 }
2969}
2970
2971static int bnxt_alloc_stats(struct bnxt *bp)
2972{
2973 u32 size, i;
2974 struct pci_dev *pdev = bp->pdev;
2975
2976 size = sizeof(struct ctx_hw_stats);
2977
2978 for (i = 0; i < bp->cp_nr_rings; i++) {
2979 struct bnxt_napi *bnapi = bp->bnapi[i];
2980 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2981
2982 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2983 &cpr->hw_stats_map,
2984 GFP_KERNEL);
2985 if (!cpr->hw_stats)
2986 return -ENOMEM;
2987
2988 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2989 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002990
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002991 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002992 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2993 sizeof(struct tx_port_stats) + 1024;
2994
2995 bp->hw_rx_port_stats =
2996 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2997 &bp->hw_rx_port_stats_map,
2998 GFP_KERNEL);
2999 if (!bp->hw_rx_port_stats)
3000 return -ENOMEM;
3001
3002 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3003 512;
3004 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3005 sizeof(struct rx_port_stats) + 512;
3006 bp->flags |= BNXT_FLAG_PORT_STATS;
3007 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003008 return 0;
3009}
3010
3011static void bnxt_clear_ring_indices(struct bnxt *bp)
3012{
3013 int i;
3014
3015 if (!bp->bnapi)
3016 return;
3017
3018 for (i = 0; i < bp->cp_nr_rings; i++) {
3019 struct bnxt_napi *bnapi = bp->bnapi[i];
3020 struct bnxt_cp_ring_info *cpr;
3021 struct bnxt_rx_ring_info *rxr;
3022 struct bnxt_tx_ring_info *txr;
3023
3024 if (!bnapi)
3025 continue;
3026
3027 cpr = &bnapi->cp_ring;
3028 cpr->cp_raw_cons = 0;
3029
Michael Chanb6ab4b02016-01-02 23:44:59 -05003030 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003031 if (txr) {
3032 txr->tx_prod = 0;
3033 txr->tx_cons = 0;
3034 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003035
Michael Chanb6ab4b02016-01-02 23:44:59 -05003036 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003037 if (rxr) {
3038 rxr->rx_prod = 0;
3039 rxr->rx_agg_prod = 0;
3040 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04003041 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05003042 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003043 }
3044}
3045
3046static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3047{
3048#ifdef CONFIG_RFS_ACCEL
3049 int i;
3050
3051 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3052 * safe to delete the hash table.
3053 */
3054 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3055 struct hlist_head *head;
3056 struct hlist_node *tmp;
3057 struct bnxt_ntuple_filter *fltr;
3058
3059 head = &bp->ntp_fltr_hash_tbl[i];
3060 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3061 hlist_del(&fltr->hash);
3062 kfree(fltr);
3063 }
3064 }
3065 if (irq_reinit) {
3066 kfree(bp->ntp_fltr_bmap);
3067 bp->ntp_fltr_bmap = NULL;
3068 }
3069 bp->ntp_fltr_count = 0;
3070#endif
3071}
3072
3073static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3074{
3075#ifdef CONFIG_RFS_ACCEL
3076 int i, rc = 0;
3077
3078 if (!(bp->flags & BNXT_FLAG_RFS))
3079 return 0;
3080
3081 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3082 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3083
3084 bp->ntp_fltr_count = 0;
Dan Carpenterac45bd92017-05-06 03:49:01 +03003085 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3086 sizeof(long),
Michael Chanc0c050c2015-10-22 16:01:17 -04003087 GFP_KERNEL);
3088
3089 if (!bp->ntp_fltr_bmap)
3090 rc = -ENOMEM;
3091
3092 return rc;
3093#else
3094 return 0;
3095#endif
3096}
3097
3098static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3099{
3100 bnxt_free_vnic_attributes(bp);
3101 bnxt_free_tx_rings(bp);
3102 bnxt_free_rx_rings(bp);
3103 bnxt_free_cp_rings(bp);
3104 bnxt_free_ntp_fltrs(bp, irq_re_init);
3105 if (irq_re_init) {
3106 bnxt_free_stats(bp);
3107 bnxt_free_ring_grps(bp);
3108 bnxt_free_vnics(bp);
Michael Chana960dec2017-02-06 16:55:39 -05003109 kfree(bp->tx_ring_map);
3110 bp->tx_ring_map = NULL;
Michael Chanb6ab4b02016-01-02 23:44:59 -05003111 kfree(bp->tx_ring);
3112 bp->tx_ring = NULL;
3113 kfree(bp->rx_ring);
3114 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04003115 kfree(bp->bnapi);
3116 bp->bnapi = NULL;
3117 } else {
3118 bnxt_clear_ring_indices(bp);
3119 }
3120}
3121
3122static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3123{
Michael Chan01657bc2016-01-02 23:45:03 -05003124 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04003125 void *bnapi;
3126
3127 if (irq_re_init) {
3128 /* Allocate bnapi mem pointer array and mem block for
3129 * all queues
3130 */
3131 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3132 bp->cp_nr_rings);
3133 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3134 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3135 if (!bnapi)
3136 return -ENOMEM;
3137
3138 bp->bnapi = bnapi;
3139 bnapi += arr_size;
3140 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3141 bp->bnapi[i] = bnapi;
3142 bp->bnapi[i]->index = i;
3143 bp->bnapi[i]->bp = bp;
3144 }
3145
Michael Chanb6ab4b02016-01-02 23:44:59 -05003146 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3147 sizeof(struct bnxt_rx_ring_info),
3148 GFP_KERNEL);
3149 if (!bp->rx_ring)
3150 return -ENOMEM;
3151
3152 for (i = 0; i < bp->rx_nr_rings; i++) {
3153 bp->rx_ring[i].bnapi = bp->bnapi[i];
3154 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3155 }
3156
3157 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3158 sizeof(struct bnxt_tx_ring_info),
3159 GFP_KERNEL);
3160 if (!bp->tx_ring)
3161 return -ENOMEM;
3162
Michael Chana960dec2017-02-06 16:55:39 -05003163 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3164 GFP_KERNEL);
3165
3166 if (!bp->tx_ring_map)
3167 return -ENOMEM;
3168
Michael Chan01657bc2016-01-02 23:45:03 -05003169 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3170 j = 0;
3171 else
3172 j = bp->rx_nr_rings;
3173
3174 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3175 bp->tx_ring[i].bnapi = bp->bnapi[j];
3176 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chan5f449242017-02-06 16:55:40 -05003177 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
Michael Chan38413402017-02-06 16:55:43 -05003178 if (i >= bp->tx_nr_rings_xdp) {
Michael Chan5f449242017-02-06 16:55:40 -05003179 bp->tx_ring[i].txq_index = i -
3180 bp->tx_nr_rings_xdp;
Michael Chan38413402017-02-06 16:55:43 -05003181 bp->bnapi[j]->tx_int = bnxt_tx_int;
3182 } else {
Michael Chanfa3e93e2017-02-06 16:55:41 -05003183 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
Michael Chan38413402017-02-06 16:55:43 -05003184 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3185 }
Michael Chanb6ab4b02016-01-02 23:44:59 -05003186 }
3187
Michael Chanc0c050c2015-10-22 16:01:17 -04003188 rc = bnxt_alloc_stats(bp);
3189 if (rc)
3190 goto alloc_mem_err;
3191
3192 rc = bnxt_alloc_ntp_fltrs(bp);
3193 if (rc)
3194 goto alloc_mem_err;
3195
3196 rc = bnxt_alloc_vnics(bp);
3197 if (rc)
3198 goto alloc_mem_err;
3199 }
3200
3201 bnxt_init_ring_struct(bp);
3202
3203 rc = bnxt_alloc_rx_rings(bp);
3204 if (rc)
3205 goto alloc_mem_err;
3206
3207 rc = bnxt_alloc_tx_rings(bp);
3208 if (rc)
3209 goto alloc_mem_err;
3210
3211 rc = bnxt_alloc_cp_rings(bp);
3212 if (rc)
3213 goto alloc_mem_err;
3214
3215 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3216 BNXT_VNIC_UCAST_FLAG;
3217 rc = bnxt_alloc_vnic_attributes(bp);
3218 if (rc)
3219 goto alloc_mem_err;
3220 return 0;
3221
3222alloc_mem_err:
3223 bnxt_free_mem(bp, true);
3224 return rc;
3225}
3226
Michael Chan9d8bc092016-12-29 12:13:33 -05003227static void bnxt_disable_int(struct bnxt *bp)
3228{
3229 int i;
3230
3231 if (!bp->bnapi)
3232 return;
3233
3234 for (i = 0; i < bp->cp_nr_rings; i++) {
3235 struct bnxt_napi *bnapi = bp->bnapi[i];
3236 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chandaf1f1e2017-02-20 19:25:17 -05003237 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chan9d8bc092016-12-29 12:13:33 -05003238
Michael Chandaf1f1e2017-02-20 19:25:17 -05003239 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3240 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
Michael Chan9d8bc092016-12-29 12:13:33 -05003241 }
3242}
3243
3244static void bnxt_disable_int_sync(struct bnxt *bp)
3245{
3246 int i;
3247
3248 atomic_inc(&bp->intr_sem);
3249
3250 bnxt_disable_int(bp);
3251 for (i = 0; i < bp->cp_nr_rings; i++)
3252 synchronize_irq(bp->irq_tbl[i].vector);
3253}
3254
3255static void bnxt_enable_int(struct bnxt *bp)
3256{
3257 int i;
3258
3259 atomic_set(&bp->intr_sem, 0);
3260 for (i = 0; i < bp->cp_nr_rings; i++) {
3261 struct bnxt_napi *bnapi = bp->bnapi[i];
3262 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3263
3264 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3265 }
3266}
3267
Michael Chanc0c050c2015-10-22 16:01:17 -04003268void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3269 u16 cmpl_ring, u16 target_id)
3270{
Michael Chana8643e12016-02-26 04:00:05 -05003271 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003272
Michael Chana8643e12016-02-26 04:00:05 -05003273 req->req_type = cpu_to_le16(req_type);
3274 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3275 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003276 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3277}
3278
Michael Chanfbfbc482016-02-26 04:00:07 -05003279static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3280 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003281{
Michael Chana11fa2b2016-05-15 03:04:47 -04003282 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003283 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003284 u32 *data = msg;
3285 __le32 *resp_len, *valid;
3286 u16 cp_ring_id, len = 0;
3287 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04003288 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04003289
Michael Chana8643e12016-02-26 04:00:05 -05003290 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003291 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003292 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003293 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3294
Deepak Khungare605db82017-05-29 19:06:04 -04003295 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3296 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3297 struct hwrm_short_input short_input = {0};
3298
3299 memcpy(short_cmd_req, req, msg_len);
3300 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3301 msg_len);
3302
3303 short_input.req_type = req->req_type;
3304 short_input.signature =
3305 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3306 short_input.size = cpu_to_le16(msg_len);
3307 short_input.req_addr =
3308 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3309
3310 data = (u32 *)&short_input;
3311 msg_len = sizeof(short_input);
3312
3313 /* Sync memory write before updating doorbell */
3314 wmb();
3315
3316 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3317 }
3318
Michael Chanc0c050c2015-10-22 16:01:17 -04003319 /* Write request msg to hwrm channel */
3320 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3321
Deepak Khungare605db82017-05-29 19:06:04 -04003322 for (i = msg_len; i < max_req_len; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003323 writel(0, bp->bar0 + i);
3324
Michael Chanc0c050c2015-10-22 16:01:17 -04003325 /* currently supports only one outstanding message */
3326 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003327 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003328
3329 /* Ring channel doorbell */
3330 writel(1, bp->bar0 + 0x100);
3331
Michael Chanff4fe812016-02-26 04:00:04 -05003332 if (!timeout)
3333 timeout = DFLT_HWRM_CMD_TIMEOUT;
3334
Michael Chanc0c050c2015-10-22 16:01:17 -04003335 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003336 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003337 if (intr_process) {
3338 /* Wait until hwrm response cmpl interrupt is processed */
3339 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003340 i++ < tmo_count) {
3341 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003342 }
3343
3344 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3345 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003346 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003347 return -1;
3348 }
3349 } else {
3350 /* Check if response len is updated */
3351 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003352 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003353 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3354 HWRM_RESP_LEN_SFT;
3355 if (len)
3356 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003357 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003358 }
3359
Michael Chana11fa2b2016-05-15 03:04:47 -04003360 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003361 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003362 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003363 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003364 return -1;
3365 }
3366
3367 /* Last word of resp contains valid bit */
3368 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003369 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003370 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3371 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003372 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003373 }
3374
Michael Chana11fa2b2016-05-15 03:04:47 -04003375 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003376 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003377 timeout, le16_to_cpu(req->req_type),
3378 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003379 return -1;
3380 }
3381 }
3382
3383 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003384 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003385 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3386 le16_to_cpu(resp->req_type),
3387 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003388 return rc;
3389}
3390
3391int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3392{
3393 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003394}
3395
3396int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3397{
3398 int rc;
3399
3400 mutex_lock(&bp->hwrm_cmd_lock);
3401 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3402 mutex_unlock(&bp->hwrm_cmd_lock);
3403 return rc;
3404}
3405
Michael Chan90e209212016-02-26 04:00:08 -05003406int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3407 int timeout)
3408{
3409 int rc;
3410
3411 mutex_lock(&bp->hwrm_cmd_lock);
3412 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3413 mutex_unlock(&bp->hwrm_cmd_lock);
3414 return rc;
3415}
3416
Michael Chana1653b12016-12-07 00:26:20 -05003417int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3418 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003419{
3420 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003421 DECLARE_BITMAP(async_events_bmap, 256);
3422 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003423 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003424
3425 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3426
3427 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003428 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003429
Michael Chan25be8622016-04-05 14:09:00 -04003430 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3431 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3432 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3433
Michael Chana1653b12016-12-07 00:26:20 -05003434 if (bmap && bmap_size) {
3435 for (i = 0; i < bmap_size; i++) {
3436 if (test_bit(i, bmap))
3437 __set_bit(i, async_events_bmap);
3438 }
3439 }
3440
Michael Chan25be8622016-04-05 14:09:00 -04003441 for (i = 0; i < 8; i++)
3442 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3443
Michael Chana1653b12016-12-07 00:26:20 -05003444 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3445}
3446
3447static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3448{
3449 struct hwrm_func_drv_rgtr_input req = {0};
3450
3451 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3452
3453 req.enables =
3454 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3455 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3456
Michael Chan11f15ed2016-04-05 14:08:55 -04003457 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003458 req.ver_maj = DRV_VER_MAJ;
3459 req.ver_min = DRV_VER_MIN;
3460 req.ver_upd = DRV_VER_UPD;
3461
3462 if (BNXT_PF(bp)) {
Michael Chan9b0436c2017-07-11 13:05:36 -04003463 u32 data[8];
Michael Chana1653b12016-12-07 00:26:20 -05003464 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003465
Michael Chan9b0436c2017-07-11 13:05:36 -04003466 memset(data, 0, sizeof(data));
3467 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3468 u16 cmd = bnxt_vf_req_snif[i];
3469 unsigned int bit, idx;
3470
3471 idx = cmd / 32;
3472 bit = cmd % 32;
3473 data[idx] |= 1 << bit;
3474 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003475
Michael Chande68f5de2015-12-09 19:35:41 -05003476 for (i = 0; i < 8; i++)
3477 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3478
Michael Chanc0c050c2015-10-22 16:01:17 -04003479 req.enables |=
3480 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3481 }
3482
3483 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3484}
3485
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003486static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3487{
3488 struct hwrm_func_drv_unrgtr_input req = {0};
3489
3490 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3491 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3492}
3493
Michael Chanc0c050c2015-10-22 16:01:17 -04003494static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3495{
3496 u32 rc = 0;
3497 struct hwrm_tunnel_dst_port_free_input req = {0};
3498
3499 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3500 req.tunnel_type = tunnel_type;
3501
3502 switch (tunnel_type) {
3503 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3504 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3505 break;
3506 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3507 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3508 break;
3509 default:
3510 break;
3511 }
3512
3513 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3514 if (rc)
3515 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3516 rc);
3517 return rc;
3518}
3519
3520static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3521 u8 tunnel_type)
3522{
3523 u32 rc = 0;
3524 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3525 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3526
3527 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3528
3529 req.tunnel_type = tunnel_type;
3530 req.tunnel_dst_port_val = port;
3531
3532 mutex_lock(&bp->hwrm_cmd_lock);
3533 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3534 if (rc) {
3535 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3536 rc);
3537 goto err_out;
3538 }
3539
Christophe Jaillet57aac712016-11-22 06:14:40 +01003540 switch (tunnel_type) {
3541 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003542 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003543 break;
3544 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003545 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003546 break;
3547 default:
3548 break;
3549 }
3550
Michael Chanc0c050c2015-10-22 16:01:17 -04003551err_out:
3552 mutex_unlock(&bp->hwrm_cmd_lock);
3553 return rc;
3554}
3555
3556static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3557{
3558 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3559 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3560
3561 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003562 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003563
3564 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3565 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3566 req.mask = cpu_to_le32(vnic->rx_mask);
3567 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3568}
3569
3570#ifdef CONFIG_RFS_ACCEL
3571static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3572 struct bnxt_ntuple_filter *fltr)
3573{
3574 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3575
3576 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3577 req.ntuple_filter_id = fltr->filter_id;
3578 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3579}
3580
3581#define BNXT_NTP_FLTR_FLAGS \
3582 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3583 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3584 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3585 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3586 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3587 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3588 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3589 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3590 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3591 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3592 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3593 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3594 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003595 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003596
Michael Chan61aad722017-02-12 19:18:14 -05003597#define BNXT_NTP_TUNNEL_FLTR_FLAG \
3598 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3599
Michael Chanc0c050c2015-10-22 16:01:17 -04003600static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3601 struct bnxt_ntuple_filter *fltr)
3602{
3603 int rc = 0;
3604 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3605 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3606 bp->hwrm_cmd_resp_addr;
3607 struct flow_keys *keys = &fltr->fkeys;
3608 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3609
3610 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003611 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003612
3613 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3614
3615 req.ethertype = htons(ETH_P_IP);
3616 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003617 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003618 req.ip_protocol = keys->basic.ip_proto;
3619
Michael Chandda0e742016-12-29 12:13:40 -05003620 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3621 int i;
3622
3623 req.ethertype = htons(ETH_P_IPV6);
3624 req.ip_addr_type =
3625 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3626 *(struct in6_addr *)&req.src_ipaddr[0] =
3627 keys->addrs.v6addrs.src;
3628 *(struct in6_addr *)&req.dst_ipaddr[0] =
3629 keys->addrs.v6addrs.dst;
3630 for (i = 0; i < 4; i++) {
3631 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3632 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3633 }
3634 } else {
3635 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3636 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3637 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3638 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3639 }
Michael Chan61aad722017-02-12 19:18:14 -05003640 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3641 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3642 req.tunnel_type =
3643 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3644 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003645
3646 req.src_port = keys->ports.src;
3647 req.src_port_mask = cpu_to_be16(0xffff);
3648 req.dst_port = keys->ports.dst;
3649 req.dst_port_mask = cpu_to_be16(0xffff);
3650
Michael Chanc1935542015-12-27 18:19:28 -05003651 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003652 mutex_lock(&bp->hwrm_cmd_lock);
3653 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3654 if (!rc)
3655 fltr->filter_id = resp->ntuple_filter_id;
3656 mutex_unlock(&bp->hwrm_cmd_lock);
3657 return rc;
3658}
3659#endif
3660
3661static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3662 u8 *mac_addr)
3663{
3664 u32 rc = 0;
3665 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3666 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3667
3668 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003669 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3670 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3671 req.flags |=
3672 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003673 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003674 req.enables =
3675 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003676 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003677 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3678 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3679 req.l2_addr_mask[0] = 0xff;
3680 req.l2_addr_mask[1] = 0xff;
3681 req.l2_addr_mask[2] = 0xff;
3682 req.l2_addr_mask[3] = 0xff;
3683 req.l2_addr_mask[4] = 0xff;
3684 req.l2_addr_mask[5] = 0xff;
3685
3686 mutex_lock(&bp->hwrm_cmd_lock);
3687 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3688 if (!rc)
3689 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3690 resp->l2_filter_id;
3691 mutex_unlock(&bp->hwrm_cmd_lock);
3692 return rc;
3693}
3694
3695static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3696{
3697 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3698 int rc = 0;
3699
3700 /* Any associated ntuple filters will also be cleared by firmware. */
3701 mutex_lock(&bp->hwrm_cmd_lock);
3702 for (i = 0; i < num_of_vnics; i++) {
3703 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3704
3705 for (j = 0; j < vnic->uc_filter_count; j++) {
3706 struct hwrm_cfa_l2_filter_free_input req = {0};
3707
3708 bnxt_hwrm_cmd_hdr_init(bp, &req,
3709 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3710
3711 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3712
3713 rc = _hwrm_send_message(bp, &req, sizeof(req),
3714 HWRM_CMD_TIMEOUT);
3715 }
3716 vnic->uc_filter_count = 0;
3717 }
3718 mutex_unlock(&bp->hwrm_cmd_lock);
3719
3720 return rc;
3721}
3722
3723static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3724{
3725 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3726 struct hwrm_vnic_tpa_cfg_input req = {0};
3727
3728 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3729
3730 if (tpa_flags) {
3731 u16 mss = bp->dev->mtu - 40;
3732 u32 nsegs, n, segs = 0, flags;
3733
3734 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3735 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3736 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3737 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3738 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3739 if (tpa_flags & BNXT_FLAG_GRO)
3740 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3741
3742 req.flags = cpu_to_le32(flags);
3743
3744 req.enables =
3745 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003746 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3747 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003748
3749 /* Number of segs are log2 units, and first packet is not
3750 * included as part of this units.
3751 */
Michael Chan2839f282016-04-25 02:30:50 -04003752 if (mss <= BNXT_RX_PAGE_SIZE) {
3753 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003754 nsegs = (MAX_SKB_FRAGS - 1) * n;
3755 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003756 n = mss / BNXT_RX_PAGE_SIZE;
3757 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003758 n++;
3759 nsegs = (MAX_SKB_FRAGS - n) / n;
3760 }
3761
3762 segs = ilog2(nsegs);
3763 req.max_agg_segs = cpu_to_le16(segs);
3764 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003765
3766 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003767 }
3768 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3769
3770 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3771}
3772
3773static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3774{
3775 u32 i, j, max_rings;
3776 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3777 struct hwrm_vnic_rss_cfg_input req = {0};
3778
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003779 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003780 return 0;
3781
3782 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3783 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003784 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003785 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3786 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3787 max_rings = bp->rx_nr_rings - 1;
3788 else
3789 max_rings = bp->rx_nr_rings;
3790 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003791 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003792 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003793
3794 /* Fill the RSS indirection table with ring group ids */
3795 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3796 if (j == max_rings)
3797 j = 0;
3798 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3799 }
3800
3801 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3802 req.hash_key_tbl_addr =
3803 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3804 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003805 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003806 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3807}
3808
3809static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3810{
3811 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3812 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3813
3814 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3815 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3816 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3817 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3818 req.enables =
3819 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3820 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3821 /* thresholds not implemented in firmware yet */
3822 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3823 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3824 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3825 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3826}
3827
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003828static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3829 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003830{
3831 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3832
3833 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3834 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003835 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003836
3837 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003838 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003839}
3840
3841static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3842{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003843 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003844
3845 for (i = 0; i < bp->nr_vnics; i++) {
3846 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3847
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003848 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3849 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3850 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3851 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003852 }
3853 bp->rsscos_nr_ctxs = 0;
3854}
3855
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003856static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003857{
3858 int rc;
3859 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3860 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3861 bp->hwrm_cmd_resp_addr;
3862
3863 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3864 -1);
3865
3866 mutex_lock(&bp->hwrm_cmd_lock);
3867 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3868 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003869 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003870 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3871 mutex_unlock(&bp->hwrm_cmd_lock);
3872
3873 return rc;
3874}
3875
Michael Chana588e452016-12-07 00:26:21 -05003876int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003877{
Michael Chanb81a90d2016-01-02 23:45:01 -05003878 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003879 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3880 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003881 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003882
3883 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003884
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003885 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3886 /* Only RSS support for now TBD: COS & LB */
3887 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3888 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3889 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3890 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003891 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3892 req.rss_rule =
3893 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3894 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3895 VNIC_CFG_REQ_ENABLES_MRU);
3896 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003897 } else {
3898 req.rss_rule = cpu_to_le16(0xffff);
3899 }
3900
3901 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3902 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003903 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3904 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3905 } else {
3906 req.cos_rule = cpu_to_le16(0xffff);
3907 }
3908
Michael Chanc0c050c2015-10-22 16:01:17 -04003909 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003910 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003911 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003912 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003913 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3914 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003915
Michael Chanb81a90d2016-01-02 23:45:01 -05003916 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003917 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3918 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3919
3920 req.lb_rule = cpu_to_le16(0xffff);
3921 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3922 VLAN_HLEN);
3923
Michael Chancf6645f2016-06-13 02:25:28 -04003924#ifdef CONFIG_BNXT_SRIOV
3925 if (BNXT_VF(bp))
3926 def_vlan = bp->vf.vlan;
3927#endif
3928 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003929 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003930 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3931 req.flags |=
3932 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003933
3934 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3935}
3936
3937static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3938{
3939 u32 rc = 0;
3940
3941 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3942 struct hwrm_vnic_free_input req = {0};
3943
3944 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3945 req.vnic_id =
3946 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3947
3948 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3949 if (rc)
3950 return rc;
3951 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3952 }
3953 return rc;
3954}
3955
3956static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3957{
3958 u16 i;
3959
3960 for (i = 0; i < bp->nr_vnics; i++)
3961 bnxt_hwrm_vnic_free_one(bp, i);
3962}
3963
Michael Chanb81a90d2016-01-02 23:45:01 -05003964static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3965 unsigned int start_rx_ring_idx,
3966 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003967{
Michael Chanb81a90d2016-01-02 23:45:01 -05003968 int rc = 0;
3969 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003970 struct hwrm_vnic_alloc_input req = {0};
3971 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3972
3973 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003974 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3975 grp_idx = bp->rx_ring[i].bnapi->index;
3976 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003977 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003978 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003979 break;
3980 }
3981 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003982 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003983 }
3984
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003985 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3986 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003987 if (vnic_id == 0)
3988 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3989
3990 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3991
3992 mutex_lock(&bp->hwrm_cmd_lock);
3993 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3994 if (!rc)
3995 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3996 mutex_unlock(&bp->hwrm_cmd_lock);
3997 return rc;
3998}
3999
Michael Chan8fdefd62016-12-29 12:13:36 -05004000static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4001{
4002 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4003 struct hwrm_vnic_qcaps_input req = {0};
4004 int rc;
4005
4006 if (bp->hwrm_spec_code < 0x10600)
4007 return 0;
4008
4009 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4010 mutex_lock(&bp->hwrm_cmd_lock);
4011 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4012 if (!rc) {
4013 if (resp->flags &
4014 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4015 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4016 }
4017 mutex_unlock(&bp->hwrm_cmd_lock);
4018 return rc;
4019}
4020
Michael Chanc0c050c2015-10-22 16:01:17 -04004021static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4022{
4023 u16 i;
4024 u32 rc = 0;
4025
4026 mutex_lock(&bp->hwrm_cmd_lock);
4027 for (i = 0; i < bp->rx_nr_rings; i++) {
4028 struct hwrm_ring_grp_alloc_input req = {0};
4029 struct hwrm_ring_grp_alloc_output *resp =
4030 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05004031 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004032
4033 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4034
Michael Chanb81a90d2016-01-02 23:45:01 -05004035 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4036 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4037 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4038 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004039
4040 rc = _hwrm_send_message(bp, &req, sizeof(req),
4041 HWRM_CMD_TIMEOUT);
4042 if (rc)
4043 break;
4044
Michael Chanb81a90d2016-01-02 23:45:01 -05004045 bp->grp_info[grp_idx].fw_grp_id =
4046 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04004047 }
4048 mutex_unlock(&bp->hwrm_cmd_lock);
4049 return rc;
4050}
4051
4052static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4053{
4054 u16 i;
4055 u32 rc = 0;
4056 struct hwrm_ring_grp_free_input req = {0};
4057
4058 if (!bp->grp_info)
4059 return 0;
4060
4061 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4062
4063 mutex_lock(&bp->hwrm_cmd_lock);
4064 for (i = 0; i < bp->cp_nr_rings; i++) {
4065 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4066 continue;
4067 req.ring_group_id =
4068 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4069
4070 rc = _hwrm_send_message(bp, &req, sizeof(req),
4071 HWRM_CMD_TIMEOUT);
4072 if (rc)
4073 break;
4074 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4075 }
4076 mutex_unlock(&bp->hwrm_cmd_lock);
4077 return rc;
4078}
4079
4080static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4081 struct bnxt_ring_struct *ring,
4082 u32 ring_type, u32 map_index,
4083 u32 stats_ctx_id)
4084{
4085 int rc = 0, err = 0;
4086 struct hwrm_ring_alloc_input req = {0};
4087 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4088 u16 ring_id;
4089
4090 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4091
4092 req.enables = 0;
4093 if (ring->nr_pages > 1) {
4094 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4095 /* Page size is in log2 units */
4096 req.page_size = BNXT_PAGE_SHIFT;
4097 req.page_tbl_depth = 1;
4098 } else {
4099 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4100 }
4101 req.fbo = 0;
4102 /* Association of ring index with doorbell index and MSIX number */
4103 req.logical_id = cpu_to_le16(map_index);
4104
4105 switch (ring_type) {
4106 case HWRM_RING_ALLOC_TX:
4107 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4108 /* Association of transmit ring with completion ring */
4109 req.cmpl_ring_id =
4110 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4111 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4112 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4113 req.queue_id = cpu_to_le16(ring->queue_id);
4114 break;
4115 case HWRM_RING_ALLOC_RX:
4116 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4117 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4118 break;
4119 case HWRM_RING_ALLOC_AGG:
4120 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4121 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4122 break;
4123 case HWRM_RING_ALLOC_CMPL:
Michael Chanbac9a7e2017-02-12 19:18:10 -05004124 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004125 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4126 if (bp->flags & BNXT_FLAG_USING_MSIX)
4127 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4128 break;
4129 default:
4130 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4131 ring_type);
4132 return -1;
4133 }
4134
4135 mutex_lock(&bp->hwrm_cmd_lock);
4136 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4137 err = le16_to_cpu(resp->error_code);
4138 ring_id = le16_to_cpu(resp->ring_id);
4139 mutex_unlock(&bp->hwrm_cmd_lock);
4140
4141 if (rc || err) {
4142 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004143 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004144 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4145 rc, err);
4146 return -1;
4147
4148 case RING_FREE_REQ_RING_TYPE_RX:
4149 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4150 rc, err);
4151 return -1;
4152
4153 case RING_FREE_REQ_RING_TYPE_TX:
4154 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4155 rc, err);
4156 return -1;
4157
4158 default:
4159 netdev_err(bp->dev, "Invalid ring\n");
4160 return -1;
4161 }
4162 }
4163 ring->fw_ring_id = ring_id;
4164 return rc;
4165}
4166
Michael Chan486b5c22016-12-29 12:13:42 -05004167static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4168{
4169 int rc;
4170
4171 if (BNXT_PF(bp)) {
4172 struct hwrm_func_cfg_input req = {0};
4173
4174 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4175 req.fid = cpu_to_le16(0xffff);
4176 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4177 req.async_event_cr = cpu_to_le16(idx);
4178 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4179 } else {
4180 struct hwrm_func_vf_cfg_input req = {0};
4181
4182 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4183 req.enables =
4184 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4185 req.async_event_cr = cpu_to_le16(idx);
4186 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4187 }
4188 return rc;
4189}
4190
Michael Chanc0c050c2015-10-22 16:01:17 -04004191static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4192{
4193 int i, rc = 0;
4194
Michael Chanedd0c2c2015-12-27 18:19:19 -05004195 for (i = 0; i < bp->cp_nr_rings; i++) {
4196 struct bnxt_napi *bnapi = bp->bnapi[i];
4197 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4198 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004199
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004200 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004201 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4202 INVALID_STATS_CTX_ID);
4203 if (rc)
4204 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004205 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4206 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004207
4208 if (!i) {
4209 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4210 if (rc)
4211 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4212 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004213 }
4214
Michael Chanedd0c2c2015-12-27 18:19:19 -05004215 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004216 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004217 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004218 u32 map_idx = txr->bnapi->index;
4219 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004220
Michael Chanb81a90d2016-01-02 23:45:01 -05004221 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4222 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004223 if (rc)
4224 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004225 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004226 }
4227
Michael Chanedd0c2c2015-12-27 18:19:19 -05004228 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004229 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004230 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004231 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004232
Michael Chanb81a90d2016-01-02 23:45:01 -05004233 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4234 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004235 if (rc)
4236 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004237 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004238 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004239 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004240 }
4241
4242 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4243 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004244 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004245 struct bnxt_ring_struct *ring =
4246 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004247 u32 grp_idx = rxr->bnapi->index;
4248 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004249
4250 rc = hwrm_ring_alloc_send_msg(bp, ring,
4251 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004252 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004253 INVALID_STATS_CTX_ID);
4254 if (rc)
4255 goto err_out;
4256
Michael Chanb81a90d2016-01-02 23:45:01 -05004257 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004258 writel(DB_KEY_RX | rxr->rx_agg_prod,
4259 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004260 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004261 }
4262 }
4263err_out:
4264 return rc;
4265}
4266
4267static int hwrm_ring_free_send_msg(struct bnxt *bp,
4268 struct bnxt_ring_struct *ring,
4269 u32 ring_type, int cmpl_ring_id)
4270{
4271 int rc;
4272 struct hwrm_ring_free_input req = {0};
4273 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4274 u16 error_code;
4275
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004276 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004277 req.ring_type = ring_type;
4278 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4279
4280 mutex_lock(&bp->hwrm_cmd_lock);
4281 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4282 error_code = le16_to_cpu(resp->error_code);
4283 mutex_unlock(&bp->hwrm_cmd_lock);
4284
4285 if (rc || error_code) {
4286 switch (ring_type) {
Michael Chanbac9a7e2017-02-12 19:18:10 -05004287 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
Michael Chanc0c050c2015-10-22 16:01:17 -04004288 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4289 rc);
4290 return rc;
4291 case RING_FREE_REQ_RING_TYPE_RX:
4292 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4293 rc);
4294 return rc;
4295 case RING_FREE_REQ_RING_TYPE_TX:
4296 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4297 rc);
4298 return rc;
4299 default:
4300 netdev_err(bp->dev, "Invalid ring\n");
4301 return -1;
4302 }
4303 }
4304 return 0;
4305}
4306
Michael Chanedd0c2c2015-12-27 18:19:19 -05004307static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004308{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004309 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004310
4311 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004312 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004313
Michael Chanedd0c2c2015-12-27 18:19:19 -05004314 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004315 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004316 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004317 u32 grp_idx = txr->bnapi->index;
4318 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004319
Michael Chanedd0c2c2015-12-27 18:19:19 -05004320 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4321 hwrm_ring_free_send_msg(bp, ring,
4322 RING_FREE_REQ_RING_TYPE_TX,
4323 close_path ? cmpl_ring_id :
4324 INVALID_HW_RING_ID);
4325 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004326 }
4327 }
4328
Michael Chanedd0c2c2015-12-27 18:19:19 -05004329 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004330 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004331 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004332 u32 grp_idx = rxr->bnapi->index;
4333 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004334
Michael Chanedd0c2c2015-12-27 18:19:19 -05004335 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4336 hwrm_ring_free_send_msg(bp, ring,
4337 RING_FREE_REQ_RING_TYPE_RX,
4338 close_path ? cmpl_ring_id :
4339 INVALID_HW_RING_ID);
4340 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004341 bp->grp_info[grp_idx].rx_fw_ring_id =
4342 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004343 }
4344 }
4345
Michael Chanedd0c2c2015-12-27 18:19:19 -05004346 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004347 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004348 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004349 u32 grp_idx = rxr->bnapi->index;
4350 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004351
Michael Chanedd0c2c2015-12-27 18:19:19 -05004352 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4353 hwrm_ring_free_send_msg(bp, ring,
4354 RING_FREE_REQ_RING_TYPE_RX,
4355 close_path ? cmpl_ring_id :
4356 INVALID_HW_RING_ID);
4357 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004358 bp->grp_info[grp_idx].agg_fw_ring_id =
4359 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004360 }
4361 }
4362
Michael Chan9d8bc092016-12-29 12:13:33 -05004363 /* The completion rings are about to be freed. After that the
4364 * IRQ doorbell will not work anymore. So we need to disable
4365 * IRQ here.
4366 */
4367 bnxt_disable_int_sync(bp);
4368
Michael Chanedd0c2c2015-12-27 18:19:19 -05004369 for (i = 0; i < bp->cp_nr_rings; i++) {
4370 struct bnxt_napi *bnapi = bp->bnapi[i];
4371 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4372 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004373
Michael Chanedd0c2c2015-12-27 18:19:19 -05004374 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4375 hwrm_ring_free_send_msg(bp, ring,
Michael Chanbac9a7e2017-02-12 19:18:10 -05004376 RING_FREE_REQ_RING_TYPE_L2_CMPL,
Michael Chanedd0c2c2015-12-27 18:19:19 -05004377 INVALID_HW_RING_ID);
4378 ring->fw_ring_id = INVALID_HW_RING_ID;
4379 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004380 }
4381 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004382}
4383
Michael Chan391be5c2016-12-29 12:13:41 -05004384/* Caller must hold bp->hwrm_cmd_lock */
4385int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4386{
4387 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4388 struct hwrm_func_qcfg_input req = {0};
4389 int rc;
4390
4391 if (bp->hwrm_spec_code < 0x10601)
4392 return 0;
4393
4394 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4395 req.fid = cpu_to_le16(fid);
4396 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4397 if (!rc)
4398 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4399
4400 return rc;
4401}
4402
Michael Chand1e79252017-02-06 16:55:38 -05004403static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
Michael Chan391be5c2016-12-29 12:13:41 -05004404{
4405 struct hwrm_func_cfg_input req = {0};
4406 int rc;
4407
4408 if (bp->hwrm_spec_code < 0x10601)
4409 return 0;
4410
4411 if (BNXT_VF(bp))
4412 return 0;
4413
4414 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4415 req.fid = cpu_to_le16(0xffff);
4416 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4417 req.num_tx_rings = cpu_to_le16(*tx_rings);
4418 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4419 if (rc)
4420 return rc;
4421
4422 mutex_lock(&bp->hwrm_cmd_lock);
4423 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4424 mutex_unlock(&bp->hwrm_cmd_lock);
4425 return rc;
4426}
4427
Michael Chanbb053f52016-02-26 04:00:02 -05004428static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4429 u32 buf_tmrs, u16 flags,
4430 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4431{
4432 req->flags = cpu_to_le16(flags);
4433 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4434 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4435 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4436 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4437 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4438 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4439 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4440 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4441}
4442
Michael Chanc0c050c2015-10-22 16:01:17 -04004443int bnxt_hwrm_set_coal(struct bnxt *bp)
4444{
4445 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004446 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4447 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004448 u16 max_buf, max_buf_irq;
4449 u16 buf_tmr, buf_tmr_irq;
4450 u32 flags;
4451
Michael Chandfc9c942016-02-26 04:00:03 -05004452 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4453 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4454 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4455 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004456
Michael Chandfb5b892016-02-26 04:00:01 -05004457 /* Each rx completion (2 records) should be DMAed immediately.
4458 * DMA 1/4 of the completion buffers at a time.
4459 */
4460 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004461 /* max_buf must not be zero */
4462 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004463 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4464 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4465 /* buf timer set to 1/4 of interrupt timer */
4466 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4467 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4468 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004469
4470 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4471
4472 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4473 * if coal_ticks is less than 25 us.
4474 */
Michael Chandfb5b892016-02-26 04:00:01 -05004475 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004476 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4477
Michael Chanbb053f52016-02-26 04:00:02 -05004478 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004479 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4480
4481 /* max_buf must not be zero */
4482 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4483 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4484 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4485 /* buf timer set to 1/4 of interrupt timer */
4486 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4487 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4488 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4489
4490 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4491 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4492 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004493
4494 mutex_lock(&bp->hwrm_cmd_lock);
4495 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004496 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004497
Michael Chandfc9c942016-02-26 04:00:03 -05004498 req = &req_rx;
4499 if (!bnapi->rx_ring)
4500 req = &req_tx;
4501 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4502
4503 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004504 HWRM_CMD_TIMEOUT);
4505 if (rc)
4506 break;
4507 }
4508 mutex_unlock(&bp->hwrm_cmd_lock);
4509 return rc;
4510}
4511
4512static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4513{
4514 int rc = 0, i;
4515 struct hwrm_stat_ctx_free_input req = {0};
4516
4517 if (!bp->bnapi)
4518 return 0;
4519
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004520 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4521 return 0;
4522
Michael Chanc0c050c2015-10-22 16:01:17 -04004523 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4524
4525 mutex_lock(&bp->hwrm_cmd_lock);
4526 for (i = 0; i < bp->cp_nr_rings; i++) {
4527 struct bnxt_napi *bnapi = bp->bnapi[i];
4528 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4529
4530 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4531 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4532
4533 rc = _hwrm_send_message(bp, &req, sizeof(req),
4534 HWRM_CMD_TIMEOUT);
4535 if (rc)
4536 break;
4537
4538 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4539 }
4540 }
4541 mutex_unlock(&bp->hwrm_cmd_lock);
4542 return rc;
4543}
4544
4545static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4546{
4547 int rc = 0, i;
4548 struct hwrm_stat_ctx_alloc_input req = {0};
4549 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4550
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004551 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4552 return 0;
4553
Michael Chanc0c050c2015-10-22 16:01:17 -04004554 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4555
Michael Chan51f30782016-07-01 18:46:29 -04004556 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004557
4558 mutex_lock(&bp->hwrm_cmd_lock);
4559 for (i = 0; i < bp->cp_nr_rings; i++) {
4560 struct bnxt_napi *bnapi = bp->bnapi[i];
4561 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4562
4563 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4564
4565 rc = _hwrm_send_message(bp, &req, sizeof(req),
4566 HWRM_CMD_TIMEOUT);
4567 if (rc)
4568 break;
4569
4570 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4571
4572 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4573 }
4574 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004575 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004576}
4577
Michael Chancf6645f2016-06-13 02:25:28 -04004578static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4579{
4580 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004581 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan9315edc2017-07-24 12:34:25 -04004582 u16 flags;
Michael Chancf6645f2016-06-13 02:25:28 -04004583 int rc;
4584
4585 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4586 req.fid = cpu_to_le16(0xffff);
4587 mutex_lock(&bp->hwrm_cmd_lock);
4588 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4589 if (rc)
4590 goto func_qcfg_exit;
4591
4592#ifdef CONFIG_BNXT_SRIOV
4593 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004594 struct bnxt_vf_info *vf = &bp->vf;
4595
4596 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4597 }
4598#endif
Michael Chan9315edc2017-07-24 12:34:25 -04004599 flags = le16_to_cpu(resp->flags);
4600 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4601 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
4602 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4603 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
4604 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
Deepak Khungar9e54e322017-04-21 20:11:26 -04004605 }
Michael Chan9315edc2017-07-24 12:34:25 -04004606 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
4607 bp->flags |= BNXT_FLAG_MULTI_HOST;
Michael Chanbc39f882017-03-08 18:44:34 -05004608
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004609 switch (resp->port_partition_type) {
4610 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4611 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4612 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4613 bp->port_partition_type = resp->port_partition_type;
4614 break;
4615 }
Michael Chan32e8239c2017-07-24 12:34:21 -04004616 if (bp->hwrm_spec_code < 0x10707 ||
4617 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
4618 bp->br_mode = BRIDGE_MODE_VEB;
4619 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
4620 bp->br_mode = BRIDGE_MODE_VEPA;
4621 else
4622 bp->br_mode = BRIDGE_MODE_UNDEF;
Michael Chancf6645f2016-06-13 02:25:28 -04004623
4624func_qcfg_exit:
4625 mutex_unlock(&bp->hwrm_cmd_lock);
4626 return rc;
4627}
4628
Michael Chan7b08f662016-12-07 00:26:18 -05004629static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004630{
4631 int rc = 0;
4632 struct hwrm_func_qcaps_input req = {0};
4633 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4634
4635 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4636 req.fid = cpu_to_le16(0xffff);
4637
4638 mutex_lock(&bp->hwrm_cmd_lock);
4639 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4640 if (rc)
4641 goto hwrm_func_qcaps_exit;
4642
Michael Chane4060d32016-12-07 00:26:19 -05004643 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4644 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4645 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4646 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4647
Michael Chan7cc5a202016-09-19 03:58:05 -04004648 bp->tx_push_thresh = 0;
4649 if (resp->flags &
4650 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4651 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4652
Michael Chanc0c050c2015-10-22 16:01:17 -04004653 if (BNXT_PF(bp)) {
4654 struct bnxt_pf_info *pf = &bp->pf;
4655
4656 pf->fw_fid = le16_to_cpu(resp->fid);
4657 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004658 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004659 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004660 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004661 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4662 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4663 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004664 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004665 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4666 if (!pf->max_hw_ring_grps)
4667 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004668 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4669 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4670 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4671 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4672 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4673 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4674 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4675 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4676 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4677 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4678 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
Michael Chanc1ef1462017-04-04 18:14:07 -04004679 if (resp->flags &
4680 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4681 bp->flags |= BNXT_FLAG_WOL_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04004682 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004683#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004684 struct bnxt_vf_info *vf = &bp->vf;
4685
4686 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004687
4688 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4689 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4690 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4691 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004692 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4693 if (!vf->max_hw_ring_grps)
4694 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004695 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4696 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4697 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004698
4699 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004700 mutex_unlock(&bp->hwrm_cmd_lock);
4701
4702 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004703 /* overwrite netdev dev_adr with admin VF MAC */
4704 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004705 } else {
Tobias Klauser1faaa782017-02-21 15:27:28 +01004706 eth_hw_addr_random(bp->dev);
Michael Chan001154e2016-09-19 03:58:06 -04004707 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4708 }
4709 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004710#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004711 }
4712
Michael Chanc0c050c2015-10-22 16:01:17 -04004713hwrm_func_qcaps_exit:
4714 mutex_unlock(&bp->hwrm_cmd_lock);
4715 return rc;
4716}
4717
4718static int bnxt_hwrm_func_reset(struct bnxt *bp)
4719{
4720 struct hwrm_func_reset_input req = {0};
4721
4722 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4723 req.enables = 0;
4724
4725 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4726}
4727
4728static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4729{
4730 int rc = 0;
4731 struct hwrm_queue_qportcfg_input req = {0};
4732 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4733 u8 i, *qptr;
4734
4735 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4736
4737 mutex_lock(&bp->hwrm_cmd_lock);
4738 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4739 if (rc)
4740 goto qportcfg_exit;
4741
4742 if (!resp->max_configurable_queues) {
4743 rc = -EINVAL;
4744 goto qportcfg_exit;
4745 }
4746 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004747 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004748 if (bp->max_tc > BNXT_MAX_QUEUE)
4749 bp->max_tc = BNXT_MAX_QUEUE;
4750
Michael Chan441cabb2016-09-19 03:58:02 -04004751 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4752 bp->max_tc = 1;
4753
Michael Chan87c374d2016-12-02 21:17:16 -05004754 if (bp->max_lltc > bp->max_tc)
4755 bp->max_lltc = bp->max_tc;
4756
Michael Chanc0c050c2015-10-22 16:01:17 -04004757 qptr = &resp->queue_id0;
4758 for (i = 0; i < bp->max_tc; i++) {
4759 bp->q_info[i].queue_id = *qptr++;
4760 bp->q_info[i].queue_profile = *qptr++;
4761 }
4762
4763qportcfg_exit:
4764 mutex_unlock(&bp->hwrm_cmd_lock);
4765 return rc;
4766}
4767
4768static int bnxt_hwrm_ver_get(struct bnxt *bp)
4769{
4770 int rc;
4771 struct hwrm_ver_get_input req = {0};
4772 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
Deepak Khungare605db82017-05-29 19:06:04 -04004773 u32 dev_caps_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04004774
Michael Chane6ef2692016-03-28 19:46:05 -04004775 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004776 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4777 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4778 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4779 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4780 mutex_lock(&bp->hwrm_cmd_lock);
4781 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4782 if (rc)
4783 goto hwrm_ver_get_exit;
4784
4785 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4786
Michael Chan11f15ed2016-04-05 14:08:55 -04004787 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4788 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004789 if (resp->hwrm_intf_maj < 1) {
4790 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004791 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004792 resp->hwrm_intf_upd);
4793 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004794 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004795 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004796 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4797 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4798
Michael Chanff4fe812016-02-26 04:00:04 -05004799 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4800 if (!bp->hwrm_cmd_timeout)
4801 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4802
Michael Chane6ef2692016-03-28 19:46:05 -04004803 if (resp->hwrm_intf_maj >= 1)
4804 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4805
Michael Chan659c8052016-06-13 02:25:33 -04004806 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004807 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4808 !resp->chip_metal)
4809 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004810
Deepak Khungare605db82017-05-29 19:06:04 -04004811 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4812 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4813 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4814 bp->flags |= BNXT_FLAG_SHORT_CMD;
4815
Michael Chanc0c050c2015-10-22 16:01:17 -04004816hwrm_ver_get_exit:
4817 mutex_unlock(&bp->hwrm_cmd_lock);
4818 return rc;
4819}
4820
Rob Swindell5ac67d82016-09-19 03:58:03 -04004821int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4822{
Rob Swindell878786d2016-09-20 03:36:33 -04004823#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004824 struct hwrm_fw_set_time_input req = {0};
4825 struct rtc_time tm;
4826 struct timeval tv;
4827
4828 if (bp->hwrm_spec_code < 0x10400)
4829 return -EOPNOTSUPP;
4830
4831 do_gettimeofday(&tv);
4832 rtc_time_to_tm(tv.tv_sec, &tm);
4833 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4834 req.year = cpu_to_le16(1900 + tm.tm_year);
4835 req.month = 1 + tm.tm_mon;
4836 req.day = tm.tm_mday;
4837 req.hour = tm.tm_hour;
4838 req.minute = tm.tm_min;
4839 req.second = tm.tm_sec;
4840 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004841#else
4842 return -EOPNOTSUPP;
4843#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004844}
4845
Michael Chan3bdf56c2016-03-07 15:38:45 -05004846static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4847{
4848 int rc;
4849 struct bnxt_pf_info *pf = &bp->pf;
4850 struct hwrm_port_qstats_input req = {0};
4851
4852 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4853 return 0;
4854
4855 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4856 req.port_id = cpu_to_le16(pf->port_id);
4857 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4858 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4859 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4860 return rc;
4861}
4862
Michael Chanc0c050c2015-10-22 16:01:17 -04004863static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4864{
4865 if (bp->vxlan_port_cnt) {
4866 bnxt_hwrm_tunnel_dst_port_free(
4867 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4868 }
4869 bp->vxlan_port_cnt = 0;
4870 if (bp->nge_port_cnt) {
4871 bnxt_hwrm_tunnel_dst_port_free(
4872 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4873 }
4874 bp->nge_port_cnt = 0;
4875}
4876
4877static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4878{
4879 int rc, i;
4880 u32 tpa_flags = 0;
4881
4882 if (set_tpa)
4883 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4884 for (i = 0; i < bp->nr_vnics; i++) {
4885 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4886 if (rc) {
4887 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
Sankar Patchineelam23e12c82017-03-28 19:47:30 -04004888 i, rc);
Michael Chanc0c050c2015-10-22 16:01:17 -04004889 return rc;
4890 }
4891 }
4892 return 0;
4893}
4894
4895static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4896{
4897 int i;
4898
4899 for (i = 0; i < bp->nr_vnics; i++)
4900 bnxt_hwrm_vnic_set_rss(bp, i, false);
4901}
4902
4903static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4904 bool irq_re_init)
4905{
4906 if (bp->vnic_info) {
4907 bnxt_hwrm_clear_vnic_filter(bp);
4908 /* clear all RSS setting before free vnic ctx */
4909 bnxt_hwrm_clear_vnic_rss(bp);
4910 bnxt_hwrm_vnic_ctx_free(bp);
4911 /* before free the vnic, undo the vnic tpa settings */
4912 if (bp->flags & BNXT_FLAG_TPA)
4913 bnxt_set_tpa(bp, false);
4914 bnxt_hwrm_vnic_free(bp);
4915 }
4916 bnxt_hwrm_ring_free(bp, close_path);
4917 bnxt_hwrm_ring_grp_free(bp);
4918 if (irq_re_init) {
4919 bnxt_hwrm_stat_ctx_free(bp);
4920 bnxt_hwrm_free_tunnel_ports(bp);
4921 }
4922}
4923
Michael Chan39d8ba22017-07-24 12:34:22 -04004924static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
4925{
4926 struct hwrm_func_cfg_input req = {0};
4927 int rc;
4928
4929 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4930 req.fid = cpu_to_le16(0xffff);
4931 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
4932 if (br_mode == BRIDGE_MODE_VEB)
4933 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
4934 else if (br_mode == BRIDGE_MODE_VEPA)
4935 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
4936 else
4937 return -EINVAL;
4938 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4939 if (rc)
4940 rc = -EIO;
4941 return rc;
4942}
4943
Michael Chanc0c050c2015-10-22 16:01:17 -04004944static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4945{
Michael Chanae10ae72016-12-29 12:13:38 -05004946 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004947 int rc;
4948
Michael Chanae10ae72016-12-29 12:13:38 -05004949 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4950 goto skip_rss_ctx;
4951
Michael Chanc0c050c2015-10-22 16:01:17 -04004952 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004953 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004954 if (rc) {
4955 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4956 vnic_id, rc);
4957 goto vnic_setup_err;
4958 }
4959 bp->rsscos_nr_ctxs++;
4960
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004961 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4962 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4963 if (rc) {
4964 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4965 vnic_id, rc);
4966 goto vnic_setup_err;
4967 }
4968 bp->rsscos_nr_ctxs++;
4969 }
4970
Michael Chanae10ae72016-12-29 12:13:38 -05004971skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004972 /* configure default vnic, ring grp */
4973 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4974 if (rc) {
4975 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4976 vnic_id, rc);
4977 goto vnic_setup_err;
4978 }
4979
4980 /* Enable RSS hashing on vnic */
4981 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4982 if (rc) {
4983 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4984 vnic_id, rc);
4985 goto vnic_setup_err;
4986 }
4987
4988 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4989 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4990 if (rc) {
4991 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4992 vnic_id, rc);
4993 }
4994 }
4995
4996vnic_setup_err:
4997 return rc;
4998}
4999
5000static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5001{
5002#ifdef CONFIG_RFS_ACCEL
5003 int i, rc = 0;
5004
5005 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05005006 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04005007 u16 vnic_id = i + 1;
5008 u16 ring_id = i;
5009
5010 if (vnic_id >= bp->nr_vnics)
5011 break;
5012
Michael Chanae10ae72016-12-29 12:13:38 -05005013 vnic = &bp->vnic_info[vnic_id];
5014 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5015 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5016 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05005017 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04005018 if (rc) {
5019 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5020 vnic_id, rc);
5021 break;
5022 }
5023 rc = bnxt_setup_vnic(bp, vnic_id);
5024 if (rc)
5025 break;
5026 }
5027 return rc;
5028#else
5029 return 0;
5030#endif
5031}
5032
Michael Chan17c71ac2016-07-01 18:46:27 -04005033/* Allow PF and VF with default VLAN to be in promiscuous mode */
5034static bool bnxt_promisc_ok(struct bnxt *bp)
5035{
5036#ifdef CONFIG_BNXT_SRIOV
5037 if (BNXT_VF(bp) && !bp->vf.vlan)
5038 return false;
5039#endif
5040 return true;
5041}
5042
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005043static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5044{
5045 unsigned int rc = 0;
5046
5047 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5048 if (rc) {
5049 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5050 rc);
5051 return rc;
5052 }
5053
5054 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5055 if (rc) {
5056 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5057 rc);
5058 return rc;
5059 }
5060 return rc;
5061}
5062
Michael Chanb664f002015-12-02 01:54:08 -05005063static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04005064static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05005065
Michael Chanc0c050c2015-10-22 16:01:17 -04005066static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5067{
Michael Chan7d2837d2016-05-04 16:56:44 -04005068 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04005069 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04005070 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005071
5072 if (irq_re_init) {
5073 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5074 if (rc) {
5075 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5076 rc);
5077 goto err_out;
5078 }
5079 }
5080
5081 rc = bnxt_hwrm_ring_alloc(bp);
5082 if (rc) {
5083 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5084 goto err_out;
5085 }
5086
5087 rc = bnxt_hwrm_ring_grp_alloc(bp);
5088 if (rc) {
5089 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5090 goto err_out;
5091 }
5092
Prashant Sreedharan76595192016-07-18 07:15:22 -04005093 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5094 rx_nr_rings--;
5095
Michael Chanc0c050c2015-10-22 16:01:17 -04005096 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04005097 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005098 if (rc) {
5099 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5100 goto err_out;
5101 }
5102
5103 rc = bnxt_setup_vnic(bp, 0);
5104 if (rc)
5105 goto err_out;
5106
5107 if (bp->flags & BNXT_FLAG_RFS) {
5108 rc = bnxt_alloc_rfs_vnics(bp);
5109 if (rc)
5110 goto err_out;
5111 }
5112
5113 if (bp->flags & BNXT_FLAG_TPA) {
5114 rc = bnxt_set_tpa(bp, true);
5115 if (rc)
5116 goto err_out;
5117 }
5118
5119 if (BNXT_VF(bp))
5120 bnxt_update_vf_mac(bp);
5121
5122 /* Filter for default vnic 0 */
5123 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5124 if (rc) {
5125 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5126 goto err_out;
5127 }
Michael Chan7d2837d2016-05-04 16:56:44 -04005128 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005129
Michael Chan7d2837d2016-05-04 16:56:44 -04005130 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04005131
Michael Chan17c71ac2016-07-01 18:46:27 -04005132 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04005133 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5134
5135 if (bp->dev->flags & IFF_ALLMULTI) {
5136 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5137 vnic->mc_list_count = 0;
5138 } else {
5139 u32 mask = 0;
5140
5141 bnxt_mc_list_updated(bp, &mask);
5142 vnic->rx_mask |= mask;
5143 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005144
Michael Chanb664f002015-12-02 01:54:08 -05005145 rc = bnxt_cfg_rx_mode(bp);
5146 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005147 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04005148
5149 rc = bnxt_hwrm_set_coal(bp);
5150 if (rc)
5151 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04005152 rc);
5153
5154 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5155 rc = bnxt_setup_nitroa0_vnic(bp);
5156 if (rc)
5157 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5158 rc);
5159 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005160
Michael Chancf6645f2016-06-13 02:25:28 -04005161 if (BNXT_VF(bp)) {
5162 bnxt_hwrm_func_qcfg(bp);
5163 netdev_update_features(bp->dev);
5164 }
5165
Michael Chanc0c050c2015-10-22 16:01:17 -04005166 return 0;
5167
5168err_out:
5169 bnxt_hwrm_resource_free(bp, 0, true);
5170
5171 return rc;
5172}
5173
5174static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5175{
5176 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5177 return 0;
5178}
5179
5180static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5181{
Sankar Patchineelam22479252017-03-28 19:47:29 -04005182 bnxt_init_cp_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005183 bnxt_init_rx_rings(bp);
5184 bnxt_init_tx_rings(bp);
5185 bnxt_init_ring_grps(bp, irq_re_init);
5186 bnxt_init_vnics(bp);
5187
5188 return bnxt_init_chip(bp, irq_re_init);
5189}
5190
Michael Chanc0c050c2015-10-22 16:01:17 -04005191static int bnxt_set_real_num_queues(struct bnxt *bp)
5192{
5193 int rc;
5194 struct net_device *dev = bp->dev;
5195
Michael Chan5f449242017-02-06 16:55:40 -05005196 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5197 bp->tx_nr_rings_xdp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005198 if (rc)
5199 return rc;
5200
5201 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5202 if (rc)
5203 return rc;
5204
5205#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05005206 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04005207 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04005208#endif
5209
5210 return rc;
5211}
5212
Michael Chan6e6c5a52016-01-02 23:45:02 -05005213static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5214 bool shared)
5215{
5216 int _rx = *rx, _tx = *tx;
5217
5218 if (shared) {
5219 *rx = min_t(int, _rx, max);
5220 *tx = min_t(int, _tx, max);
5221 } else {
5222 if (max < 2)
5223 return -ENOMEM;
5224
5225 while (_rx + _tx > max) {
5226 if (_rx > _tx && _rx > 1)
5227 _rx--;
5228 else if (_tx > 1)
5229 _tx--;
5230 }
5231 *rx = _rx;
5232 *tx = _tx;
5233 }
5234 return 0;
5235}
5236
Michael Chan78095922016-12-07 00:26:16 -05005237static void bnxt_setup_msix(struct bnxt *bp)
5238{
5239 const int len = sizeof(bp->irq_tbl[0].name);
5240 struct net_device *dev = bp->dev;
5241 int tcs, i;
5242
5243 tcs = netdev_get_num_tc(dev);
5244 if (tcs > 1) {
Michael Chand1e79252017-02-06 16:55:38 -05005245 int i, off, count;
Michael Chan78095922016-12-07 00:26:16 -05005246
Michael Chand1e79252017-02-06 16:55:38 -05005247 for (i = 0; i < tcs; i++) {
5248 count = bp->tx_nr_rings_per_tc;
5249 off = i * count;
5250 netdev_set_tc_queue(dev, i, count, off);
Michael Chan78095922016-12-07 00:26:16 -05005251 }
5252 }
5253
5254 for (i = 0; i < bp->cp_nr_rings; i++) {
5255 char *attr;
5256
5257 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5258 attr = "TxRx";
5259 else if (i < bp->rx_nr_rings)
5260 attr = "rx";
5261 else
5262 attr = "tx";
5263
5264 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5265 i);
5266 bp->irq_tbl[i].handler = bnxt_msix;
5267 }
5268}
5269
5270static void bnxt_setup_inta(struct bnxt *bp)
5271{
5272 const int len = sizeof(bp->irq_tbl[0].name);
5273
5274 if (netdev_get_num_tc(bp->dev))
5275 netdev_reset_tc(bp->dev);
5276
5277 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5278 0);
5279 bp->irq_tbl[0].handler = bnxt_inta;
5280}
5281
5282static int bnxt_setup_int_mode(struct bnxt *bp)
5283{
5284 int rc;
5285
5286 if (bp->flags & BNXT_FLAG_USING_MSIX)
5287 bnxt_setup_msix(bp);
5288 else
5289 bnxt_setup_inta(bp);
5290
5291 rc = bnxt_set_real_num_queues(bp);
5292 return rc;
5293}
5294
Michael Chanb7429952017-01-13 01:32:00 -05005295#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005296static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5297{
5298#if defined(CONFIG_BNXT_SRIOV)
5299 if (BNXT_VF(bp))
5300 return bp->vf.max_rsscos_ctxs;
5301#endif
5302 return bp->pf.max_rsscos_ctxs;
5303}
5304
5305static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5306{
5307#if defined(CONFIG_BNXT_SRIOV)
5308 if (BNXT_VF(bp))
5309 return bp->vf.max_vnics;
5310#endif
5311 return bp->pf.max_vnics;
5312}
Michael Chanb7429952017-01-13 01:32:00 -05005313#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005314
Michael Chane4060d32016-12-07 00:26:19 -05005315unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5316{
5317#if defined(CONFIG_BNXT_SRIOV)
5318 if (BNXT_VF(bp))
5319 return bp->vf.max_stat_ctxs;
5320#endif
5321 return bp->pf.max_stat_ctxs;
5322}
5323
Michael Chana588e452016-12-07 00:26:21 -05005324void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5325{
5326#if defined(CONFIG_BNXT_SRIOV)
5327 if (BNXT_VF(bp))
5328 bp->vf.max_stat_ctxs = max;
5329 else
5330#endif
5331 bp->pf.max_stat_ctxs = max;
5332}
5333
Michael Chane4060d32016-12-07 00:26:19 -05005334unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5335{
5336#if defined(CONFIG_BNXT_SRIOV)
5337 if (BNXT_VF(bp))
5338 return bp->vf.max_cp_rings;
5339#endif
5340 return bp->pf.max_cp_rings;
5341}
5342
Michael Chana588e452016-12-07 00:26:21 -05005343void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5344{
5345#if defined(CONFIG_BNXT_SRIOV)
5346 if (BNXT_VF(bp))
5347 bp->vf.max_cp_rings = max;
5348 else
5349#endif
5350 bp->pf.max_cp_rings = max;
5351}
5352
Michael Chan78095922016-12-07 00:26:16 -05005353static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5354{
5355#if defined(CONFIG_BNXT_SRIOV)
5356 if (BNXT_VF(bp))
Michael Chan68a946b2017-04-04 18:14:17 -04005357 return min_t(unsigned int, bp->vf.max_irqs,
5358 bp->vf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005359#endif
Michael Chan68a946b2017-04-04 18:14:17 -04005360 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
Michael Chan78095922016-12-07 00:26:16 -05005361}
5362
Michael Chan33c26572016-12-07 00:26:15 -05005363void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5364{
5365#if defined(CONFIG_BNXT_SRIOV)
5366 if (BNXT_VF(bp))
5367 bp->vf.max_irqs = max_irqs;
5368 else
5369#endif
5370 bp->pf.max_irqs = max_irqs;
5371}
5372
Michael Chan78095922016-12-07 00:26:16 -05005373static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005374{
Michael Chan01657bc2016-01-02 23:45:03 -05005375 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005376 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005377
Michael Chan78095922016-12-07 00:26:16 -05005378 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005379 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5380 if (!msix_ent)
5381 return -ENOMEM;
5382
5383 for (i = 0; i < total_vecs; i++) {
5384 msix_ent[i].entry = i;
5385 msix_ent[i].vector = 0;
5386 }
5387
Michael Chan01657bc2016-01-02 23:45:03 -05005388 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5389 min = 2;
5390
5391 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005392 if (total_vecs < 0) {
5393 rc = -ENODEV;
5394 goto msix_setup_exit;
5395 }
5396
5397 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5398 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005399 for (i = 0; i < total_vecs; i++)
5400 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005401
Michael Chan78095922016-12-07 00:26:16 -05005402 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005403 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005404 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005405 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005406 if (rc)
5407 goto msix_setup_exit;
5408
Michael Chanc0c050c2015-10-22 16:01:17 -04005409 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005410 bp->cp_nr_rings = (min == 1) ?
5411 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5412 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005413
Michael Chanc0c050c2015-10-22 16:01:17 -04005414 } else {
5415 rc = -ENOMEM;
5416 goto msix_setup_exit;
5417 }
5418 bp->flags |= BNXT_FLAG_USING_MSIX;
5419 kfree(msix_ent);
5420 return 0;
5421
5422msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005423 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5424 kfree(bp->irq_tbl);
5425 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005426 pci_disable_msix(bp->pdev);
5427 kfree(msix_ent);
5428 return rc;
5429}
5430
Michael Chan78095922016-12-07 00:26:16 -05005431static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005432{
Michael Chanc0c050c2015-10-22 16:01:17 -04005433 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005434 if (!bp->irq_tbl)
5435 return -ENOMEM;
5436
5437 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005438 bp->rx_nr_rings = 1;
5439 bp->tx_nr_rings = 1;
5440 bp->cp_nr_rings = 1;
5441 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005442 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005443 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005444 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005445}
5446
Michael Chan78095922016-12-07 00:26:16 -05005447static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005448{
5449 int rc = 0;
5450
5451 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005452 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005453
Michael Chan1fa72e22016-04-25 02:30:49 -04005454 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005455 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005456 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005457 }
5458 return rc;
5459}
5460
Michael Chan78095922016-12-07 00:26:16 -05005461static void bnxt_clear_int_mode(struct bnxt *bp)
5462{
5463 if (bp->flags & BNXT_FLAG_USING_MSIX)
5464 pci_disable_msix(bp->pdev);
5465
5466 kfree(bp->irq_tbl);
5467 bp->irq_tbl = NULL;
5468 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5469}
5470
Michael Chanc0c050c2015-10-22 16:01:17 -04005471static void bnxt_free_irq(struct bnxt *bp)
5472{
5473 struct bnxt_irq *irq;
5474 int i;
5475
5476#ifdef CONFIG_RFS_ACCEL
5477 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5478 bp->dev->rx_cpu_rmap = NULL;
5479#endif
5480 if (!bp->irq_tbl)
5481 return;
5482
5483 for (i = 0; i < bp->cp_nr_rings; i++) {
5484 irq = &bp->irq_tbl[i];
5485 if (irq->requested)
5486 free_irq(irq->vector, bp->bnapi[i]);
5487 irq->requested = 0;
5488 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005489}
5490
5491static int bnxt_request_irq(struct bnxt *bp)
5492{
Michael Chanb81a90d2016-01-02 23:45:01 -05005493 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005494 unsigned long flags = 0;
5495#ifdef CONFIG_RFS_ACCEL
5496 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5497#endif
5498
5499 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5500 flags = IRQF_SHARED;
5501
Michael Chanb81a90d2016-01-02 23:45:01 -05005502 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005503 struct bnxt_irq *irq = &bp->irq_tbl[i];
5504#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005505 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005506 rc = irq_cpu_rmap_add(rmap, irq->vector);
5507 if (rc)
5508 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005509 j);
5510 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005511 }
5512#endif
5513 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5514 bp->bnapi[i]);
5515 if (rc)
5516 break;
5517
5518 irq->requested = 1;
5519 }
5520 return rc;
5521}
5522
5523static void bnxt_del_napi(struct bnxt *bp)
5524{
5525 int i;
5526
5527 if (!bp->bnapi)
5528 return;
5529
5530 for (i = 0; i < bp->cp_nr_rings; i++) {
5531 struct bnxt_napi *bnapi = bp->bnapi[i];
5532
5533 napi_hash_del(&bnapi->napi);
5534 netif_napi_del(&bnapi->napi);
5535 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005536 /* We called napi_hash_del() before netif_napi_del(), we need
5537 * to respect an RCU grace period before freeing napi structures.
5538 */
5539 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005540}
5541
5542static void bnxt_init_napi(struct bnxt *bp)
5543{
5544 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005545 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005546 struct bnxt_napi *bnapi;
5547
5548 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005549 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5550 cp_nr_rings--;
5551 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005552 bnapi = bp->bnapi[i];
5553 netif_napi_add(bp->dev, &bnapi->napi,
5554 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005555 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005556 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5557 bnapi = bp->bnapi[cp_nr_rings];
5558 netif_napi_add(bp->dev, &bnapi->napi,
5559 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005560 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005561 } else {
5562 bnapi = bp->bnapi[0];
5563 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005564 }
5565}
5566
5567static void bnxt_disable_napi(struct bnxt *bp)
5568{
5569 int i;
5570
5571 if (!bp->bnapi)
5572 return;
5573
Michael Chanb356a2e2016-12-29 12:13:31 -05005574 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005575 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005576}
5577
5578static void bnxt_enable_napi(struct bnxt *bp)
5579{
5580 int i;
5581
5582 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005583 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005584 napi_enable(&bp->bnapi[i]->napi);
5585 }
5586}
5587
Michael Chan7df4ae92016-12-02 21:17:17 -05005588void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005589{
5590 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005591 struct bnxt_tx_ring_info *txr;
5592 struct netdev_queue *txq;
5593
Michael Chanb6ab4b02016-01-02 23:44:59 -05005594 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005595 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005596 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005597 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005598 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005599 }
5600 }
5601 /* Stop all TX queues */
5602 netif_tx_disable(bp->dev);
5603 netif_carrier_off(bp->dev);
5604}
5605
Michael Chan7df4ae92016-12-02 21:17:17 -05005606void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005607{
5608 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005609 struct bnxt_tx_ring_info *txr;
5610 struct netdev_queue *txq;
5611
5612 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005613 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005614 txq = netdev_get_tx_queue(bp->dev, i);
5615 txr->dev_state = 0;
5616 }
5617 netif_tx_wake_all_queues(bp->dev);
5618 if (bp->link_info.link_up)
5619 netif_carrier_on(bp->dev);
5620}
5621
5622static void bnxt_report_link(struct bnxt *bp)
5623{
5624 if (bp->link_info.link_up) {
5625 const char *duplex;
5626 const char *flow_ctrl;
Deepak Khungar38a21b32017-04-21 20:11:24 -04005627 u32 speed;
5628 u16 fec;
Michael Chanc0c050c2015-10-22 16:01:17 -04005629
5630 netif_carrier_on(bp->dev);
5631 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5632 duplex = "full";
5633 else
5634 duplex = "half";
5635 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5636 flow_ctrl = "ON - receive & transmit";
5637 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5638 flow_ctrl = "ON - transmit";
5639 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5640 flow_ctrl = "ON - receive";
5641 else
5642 flow_ctrl = "none";
5643 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
Deepak Khungar38a21b32017-04-21 20:11:24 -04005644 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04005645 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005646 if (bp->flags & BNXT_FLAG_EEE_CAP)
5647 netdev_info(bp->dev, "EEE is %s\n",
5648 bp->eee.eee_active ? "active" :
5649 "not active");
Michael Chane70c7522017-02-12 19:18:16 -05005650 fec = bp->link_info.fec_cfg;
5651 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5652 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5653 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5654 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5655 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
Michael Chanc0c050c2015-10-22 16:01:17 -04005656 } else {
5657 netif_carrier_off(bp->dev);
5658 netdev_err(bp->dev, "NIC Link is Down\n");
5659 }
5660}
5661
Michael Chan170ce012016-04-05 14:08:57 -04005662static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5663{
5664 int rc = 0;
5665 struct hwrm_port_phy_qcaps_input req = {0};
5666 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005667 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005668
5669 if (bp->hwrm_spec_code < 0x10201)
5670 return 0;
5671
5672 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5673
5674 mutex_lock(&bp->hwrm_cmd_lock);
5675 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5676 if (rc)
5677 goto hwrm_phy_qcaps_exit;
5678
Michael Chanacb20052017-07-24 12:34:20 -04005679 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
Michael Chan170ce012016-04-05 14:08:57 -04005680 struct ethtool_eee *eee = &bp->eee;
5681 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5682
5683 bp->flags |= BNXT_FLAG_EEE_CAP;
5684 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5685 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5686 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5687 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5688 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5689 }
Michael Chan520ad892017-03-08 18:44:35 -05005690 if (resp->supported_speeds_auto_mode)
5691 link_info->support_auto_speeds =
5692 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005693
5694hwrm_phy_qcaps_exit:
5695 mutex_unlock(&bp->hwrm_cmd_lock);
5696 return rc;
5697}
5698
Michael Chanc0c050c2015-10-22 16:01:17 -04005699static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5700{
5701 int rc = 0;
5702 struct bnxt_link_info *link_info = &bp->link_info;
5703 struct hwrm_port_phy_qcfg_input req = {0};
5704 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5705 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005706 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005707
5708 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5709
5710 mutex_lock(&bp->hwrm_cmd_lock);
5711 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5712 if (rc) {
5713 mutex_unlock(&bp->hwrm_cmd_lock);
5714 return rc;
5715 }
5716
5717 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5718 link_info->phy_link_status = resp->link;
Michael Chanacb20052017-07-24 12:34:20 -04005719 link_info->duplex = resp->duplex_cfg;
5720 if (bp->hwrm_spec_code >= 0x10800)
5721 link_info->duplex = resp->duplex_state;
Michael Chanc0c050c2015-10-22 16:01:17 -04005722 link_info->pause = resp->pause;
5723 link_info->auto_mode = resp->auto_mode;
5724 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005725 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005726 link_info->force_pause_setting = resp->force_pause;
Michael Chanacb20052017-07-24 12:34:20 -04005727 link_info->duplex_setting = resp->duplex_cfg;
Michael Chanc0c050c2015-10-22 16:01:17 -04005728 if (link_info->phy_link_status == BNXT_LINK_LINK)
5729 link_info->link_speed = le16_to_cpu(resp->link_speed);
5730 else
5731 link_info->link_speed = 0;
5732 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005733 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5734 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005735 link_info->lp_auto_link_speeds =
5736 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005737 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5738 link_info->phy_ver[0] = resp->phy_maj;
5739 link_info->phy_ver[1] = resp->phy_min;
5740 link_info->phy_ver[2] = resp->phy_bld;
5741 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005742 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005743 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005744 link_info->phy_addr = resp->eee_config_phy_addr &
5745 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005746 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005747
Michael Chan170ce012016-04-05 14:08:57 -04005748 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5749 struct ethtool_eee *eee = &bp->eee;
5750 u16 fw_speeds;
5751
5752 eee->eee_active = 0;
5753 if (resp->eee_config_phy_addr &
5754 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5755 eee->eee_active = 1;
5756 fw_speeds = le16_to_cpu(
5757 resp->link_partner_adv_eee_link_speed_mask);
5758 eee->lp_advertised =
5759 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5760 }
5761
5762 /* Pull initial EEE config */
5763 if (!chng_link_state) {
5764 if (resp->eee_config_phy_addr &
5765 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5766 eee->eee_enabled = 1;
5767
5768 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5769 eee->advertised =
5770 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5771
5772 if (resp->eee_config_phy_addr &
5773 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5774 __le32 tmr;
5775
5776 eee->tx_lpi_enabled = 1;
5777 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5778 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5779 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5780 }
5781 }
5782 }
Michael Chane70c7522017-02-12 19:18:16 -05005783
5784 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5785 if (bp->hwrm_spec_code >= 0x10504)
5786 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5787
Michael Chanc0c050c2015-10-22 16:01:17 -04005788 /* TODO: need to add more logic to report VF link */
5789 if (chng_link_state) {
5790 if (link_info->phy_link_status == BNXT_LINK_LINK)
5791 link_info->link_up = 1;
5792 else
5793 link_info->link_up = 0;
5794 if (link_up != link_info->link_up)
5795 bnxt_report_link(bp);
5796 } else {
5797 /* alwasy link down if not require to update link state */
5798 link_info->link_up = 0;
5799 }
5800 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005801
5802 diff = link_info->support_auto_speeds ^ link_info->advertising;
5803 if ((link_info->support_auto_speeds | diff) !=
5804 link_info->support_auto_speeds) {
5805 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005806 * update the advertisement settings. Caller holds RTNL
5807 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005808 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005809 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005810 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005811 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005812 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005813 return 0;
5814}
5815
Michael Chan10289be2016-05-15 03:04:49 -04005816static void bnxt_get_port_module_status(struct bnxt *bp)
5817{
5818 struct bnxt_link_info *link_info = &bp->link_info;
5819 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5820 u8 module_status;
5821
5822 if (bnxt_update_link(bp, true))
5823 return;
5824
5825 module_status = link_info->module_status;
5826 switch (module_status) {
5827 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5828 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5829 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5830 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5831 bp->pf.port_id);
5832 if (bp->hwrm_spec_code >= 0x10201) {
5833 netdev_warn(bp->dev, "Module part number %s\n",
5834 resp->phy_vendor_partnumber);
5835 }
5836 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5837 netdev_warn(bp->dev, "TX is disabled\n");
5838 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5839 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5840 }
5841}
5842
Michael Chanc0c050c2015-10-22 16:01:17 -04005843static void
5844bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5845{
5846 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005847 if (bp->hwrm_spec_code >= 0x10201)
5848 req->auto_pause =
5849 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005850 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5851 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5852 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005853 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005854 req->enables |=
5855 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5856 } else {
5857 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5858 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5859 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5860 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5861 req->enables |=
5862 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005863 if (bp->hwrm_spec_code >= 0x10201) {
5864 req->auto_pause = req->force_pause;
5865 req->enables |= cpu_to_le32(
5866 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5867 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005868 }
5869}
5870
5871static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5872 struct hwrm_port_phy_cfg_input *req)
5873{
5874 u8 autoneg = bp->link_info.autoneg;
5875 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005876 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005877
5878 if (autoneg & BNXT_AUTONEG_SPEED) {
5879 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005880 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005881
5882 req->enables |= cpu_to_le32(
5883 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5884 req->auto_link_speed_mask = cpu_to_le16(advertising);
5885
5886 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5887 req->flags |=
5888 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5889 } else {
5890 req->force_link_speed = cpu_to_le16(fw_link_speed);
5891 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5892 }
5893
Michael Chanc0c050c2015-10-22 16:01:17 -04005894 /* tell chimp that the setting takes effect immediately */
5895 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5896}
5897
5898int bnxt_hwrm_set_pause(struct bnxt *bp)
5899{
5900 struct hwrm_port_phy_cfg_input req = {0};
5901 int rc;
5902
5903 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5904 bnxt_hwrm_set_pause_common(bp, &req);
5905
5906 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5907 bp->link_info.force_link_chng)
5908 bnxt_hwrm_set_link_common(bp, &req);
5909
5910 mutex_lock(&bp->hwrm_cmd_lock);
5911 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5912 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5913 /* since changing of pause setting doesn't trigger any link
5914 * change event, the driver needs to update the current pause
5915 * result upon successfully return of the phy_cfg command
5916 */
5917 bp->link_info.pause =
5918 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5919 bp->link_info.auto_pause_setting = 0;
5920 if (!bp->link_info.force_link_chng)
5921 bnxt_report_link(bp);
5922 }
5923 bp->link_info.force_link_chng = false;
5924 mutex_unlock(&bp->hwrm_cmd_lock);
5925 return rc;
5926}
5927
Michael Chan939f7f02016-04-05 14:08:58 -04005928static void bnxt_hwrm_set_eee(struct bnxt *bp,
5929 struct hwrm_port_phy_cfg_input *req)
5930{
5931 struct ethtool_eee *eee = &bp->eee;
5932
5933 if (eee->eee_enabled) {
5934 u16 eee_speeds;
5935 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5936
5937 if (eee->tx_lpi_enabled)
5938 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5939 else
5940 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5941
5942 req->flags |= cpu_to_le32(flags);
5943 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5944 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5945 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5946 } else {
5947 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5948 }
5949}
5950
5951int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005952{
5953 struct hwrm_port_phy_cfg_input req = {0};
5954
5955 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5956 if (set_pause)
5957 bnxt_hwrm_set_pause_common(bp, &req);
5958
5959 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005960
5961 if (set_eee)
5962 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005963 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5964}
5965
Michael Chan33f7d552016-04-11 04:11:12 -04005966static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5967{
5968 struct hwrm_port_phy_cfg_input req = {0};
5969
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005970 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005971 return 0;
5972
5973 if (pci_num_vf(bp->pdev))
5974 return 0;
5975
5976 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005977 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005978 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5979}
5980
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005981static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5982{
5983 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5984 struct hwrm_port_led_qcaps_input req = {0};
5985 struct bnxt_pf_info *pf = &bp->pf;
5986 int rc;
5987
5988 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5989 return 0;
5990
5991 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5992 req.port_id = cpu_to_le16(pf->port_id);
5993 mutex_lock(&bp->hwrm_cmd_lock);
5994 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5995 if (rc) {
5996 mutex_unlock(&bp->hwrm_cmd_lock);
5997 return rc;
5998 }
5999 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6000 int i;
6001
6002 bp->num_leds = resp->num_leds;
6003 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6004 bp->num_leds);
6005 for (i = 0; i < bp->num_leds; i++) {
6006 struct bnxt_led_info *led = &bp->leds[i];
6007 __le16 caps = led->led_state_caps;
6008
6009 if (!led->led_group_id ||
6010 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6011 bp->num_leds = 0;
6012 break;
6013 }
6014 }
6015 }
6016 mutex_unlock(&bp->hwrm_cmd_lock);
6017 return 0;
6018}
6019
Michael Chan5282db62017-04-04 18:14:10 -04006020int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6021{
6022 struct hwrm_wol_filter_alloc_input req = {0};
6023 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6024 int rc;
6025
6026 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6027 req.port_id = cpu_to_le16(bp->pf.port_id);
6028 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6029 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6030 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6031 mutex_lock(&bp->hwrm_cmd_lock);
6032 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6033 if (!rc)
6034 bp->wol_filter_id = resp->wol_filter_id;
6035 mutex_unlock(&bp->hwrm_cmd_lock);
6036 return rc;
6037}
6038
6039int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6040{
6041 struct hwrm_wol_filter_free_input req = {0};
6042 int rc;
6043
6044 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6045 req.port_id = cpu_to_le16(bp->pf.port_id);
6046 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6047 req.wol_filter_id = bp->wol_filter_id;
6048 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6049 return rc;
6050}
6051
Michael Chanc1ef1462017-04-04 18:14:07 -04006052static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6053{
6054 struct hwrm_wol_filter_qcfg_input req = {0};
6055 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6056 u16 next_handle = 0;
6057 int rc;
6058
6059 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6060 req.port_id = cpu_to_le16(bp->pf.port_id);
6061 req.handle = cpu_to_le16(handle);
6062 mutex_lock(&bp->hwrm_cmd_lock);
6063 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6064 if (!rc) {
6065 next_handle = le16_to_cpu(resp->next_handle);
6066 if (next_handle != 0) {
6067 if (resp->wol_type ==
6068 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6069 bp->wol = 1;
6070 bp->wol_filter_id = resp->wol_filter_id;
6071 }
6072 }
6073 }
6074 mutex_unlock(&bp->hwrm_cmd_lock);
6075 return next_handle;
6076}
6077
6078static void bnxt_get_wol_settings(struct bnxt *bp)
6079{
6080 u16 handle = 0;
6081
6082 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6083 return;
6084
6085 do {
6086 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6087 } while (handle && handle != 0xffff);
6088}
6089
Michael Chan939f7f02016-04-05 14:08:58 -04006090static bool bnxt_eee_config_ok(struct bnxt *bp)
6091{
6092 struct ethtool_eee *eee = &bp->eee;
6093 struct bnxt_link_info *link_info = &bp->link_info;
6094
6095 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6096 return true;
6097
6098 if (eee->eee_enabled) {
6099 u32 advertising =
6100 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6101
6102 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6103 eee->eee_enabled = 0;
6104 return false;
6105 }
6106 if (eee->advertised & ~advertising) {
6107 eee->advertised = advertising & eee->supported;
6108 return false;
6109 }
6110 }
6111 return true;
6112}
6113
Michael Chanc0c050c2015-10-22 16:01:17 -04006114static int bnxt_update_phy_setting(struct bnxt *bp)
6115{
6116 int rc;
6117 bool update_link = false;
6118 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04006119 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006120 struct bnxt_link_info *link_info = &bp->link_info;
6121
6122 rc = bnxt_update_link(bp, true);
6123 if (rc) {
6124 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6125 rc);
6126 return rc;
6127 }
Michael Chan33dac242017-02-12 19:18:15 -05006128 if (!BNXT_SINGLE_PF(bp))
6129 return 0;
6130
Michael Chanc0c050c2015-10-22 16:01:17 -04006131 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04006132 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6133 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04006134 update_pause = true;
6135 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6136 link_info->force_pause_setting != link_info->req_flow_ctrl)
6137 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006138 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6139 if (BNXT_AUTO_MODE(link_info->auto_mode))
6140 update_link = true;
6141 if (link_info->req_link_speed != link_info->force_link_speed)
6142 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05006143 if (link_info->req_duplex != link_info->duplex_setting)
6144 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006145 } else {
6146 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6147 update_link = true;
6148 if (link_info->advertising != link_info->auto_link_speeds)
6149 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04006150 }
6151
Michael Chan16d663a2016-11-16 21:13:07 -05006152 /* The last close may have shutdown the link, so need to call
6153 * PHY_CFG to bring it back up.
6154 */
6155 if (!netif_carrier_ok(bp->dev))
6156 update_link = true;
6157
Michael Chan939f7f02016-04-05 14:08:58 -04006158 if (!bnxt_eee_config_ok(bp))
6159 update_eee = true;
6160
Michael Chanc0c050c2015-10-22 16:01:17 -04006161 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04006162 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04006163 else if (update_pause)
6164 rc = bnxt_hwrm_set_pause(bp);
6165 if (rc) {
6166 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6167 rc);
6168 return rc;
6169 }
6170
6171 return rc;
6172}
6173
Jeffrey Huang11809492015-11-05 16:25:49 -05006174/* Common routine to pre-map certain register block to different GRC window.
6175 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6176 * in PF and 3 windows in VF that can be customized to map in different
6177 * register blocks.
6178 */
6179static void bnxt_preset_reg_win(struct bnxt *bp)
6180{
6181 if (BNXT_PF(bp)) {
6182 /* CAG registers map to GRC window #4 */
6183 writel(BNXT_CAG_REG_BASE,
6184 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6185 }
6186}
6187
Michael Chanc0c050c2015-10-22 16:01:17 -04006188static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6189{
6190 int rc = 0;
6191
Jeffrey Huang11809492015-11-05 16:25:49 -05006192 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006193 netif_carrier_off(bp->dev);
6194 if (irq_re_init) {
6195 rc = bnxt_setup_int_mode(bp);
6196 if (rc) {
6197 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6198 rc);
6199 return rc;
6200 }
6201 }
6202 if ((bp->flags & BNXT_FLAG_RFS) &&
6203 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6204 /* disable RFS if falling back to INTA */
6205 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6206 bp->flags &= ~BNXT_FLAG_RFS;
6207 }
6208
6209 rc = bnxt_alloc_mem(bp, irq_re_init);
6210 if (rc) {
6211 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6212 goto open_err_free_mem;
6213 }
6214
6215 if (irq_re_init) {
6216 bnxt_init_napi(bp);
6217 rc = bnxt_request_irq(bp);
6218 if (rc) {
6219 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6220 goto open_err;
6221 }
6222 }
6223
6224 bnxt_enable_napi(bp);
6225
6226 rc = bnxt_init_nic(bp, irq_re_init);
6227 if (rc) {
6228 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6229 goto open_err;
6230 }
6231
6232 if (link_re_init) {
6233 rc = bnxt_update_phy_setting(bp);
6234 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05006235 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04006236 }
6237
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006238 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006239 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006240
Michael Chancaefe522015-12-09 19:35:42 -05006241 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006242 bnxt_enable_int(bp);
6243 /* Enable TX queues */
6244 bnxt_tx_enable(bp);
6245 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04006246 /* Poll link status and check for SFP+ module status */
6247 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006248
6249 return 0;
6250
6251open_err:
6252 bnxt_disable_napi(bp);
6253 bnxt_del_napi(bp);
6254
6255open_err_free_mem:
6256 bnxt_free_skbs(bp);
6257 bnxt_free_irq(bp);
6258 bnxt_free_mem(bp, true);
6259 return rc;
6260}
6261
6262/* rtnl_lock held */
6263int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6264{
6265 int rc = 0;
6266
6267 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6268 if (rc) {
6269 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6270 dev_close(bp->dev);
6271 }
6272 return rc;
6273}
6274
Michael Chanf7dc1ea2017-04-04 18:14:13 -04006275/* rtnl_lock held, open the NIC half way by allocating all resources, but
6276 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6277 * self tests.
6278 */
6279int bnxt_half_open_nic(struct bnxt *bp)
6280{
6281 int rc = 0;
6282
6283 rc = bnxt_alloc_mem(bp, false);
6284 if (rc) {
6285 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6286 goto half_open_err;
6287 }
6288 rc = bnxt_init_nic(bp, false);
6289 if (rc) {
6290 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6291 goto half_open_err;
6292 }
6293 return 0;
6294
6295half_open_err:
6296 bnxt_free_skbs(bp);
6297 bnxt_free_mem(bp, false);
6298 dev_close(bp->dev);
6299 return rc;
6300}
6301
6302/* rtnl_lock held, this call can only be made after a previous successful
6303 * call to bnxt_half_open_nic().
6304 */
6305void bnxt_half_close_nic(struct bnxt *bp)
6306{
6307 bnxt_hwrm_resource_free(bp, false, false);
6308 bnxt_free_skbs(bp);
6309 bnxt_free_mem(bp, false);
6310}
6311
Michael Chanc0c050c2015-10-22 16:01:17 -04006312static int bnxt_open(struct net_device *dev)
6313{
6314 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006315
Michael Chanc0c050c2015-10-22 16:01:17 -04006316 return __bnxt_open_nic(bp, true, true);
6317}
6318
Michael Chanf9b76eb2017-07-11 13:05:34 -04006319static bool bnxt_drv_busy(struct bnxt *bp)
6320{
6321 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6322 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6323}
6324
Michael Chanc0c050c2015-10-22 16:01:17 -04006325int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6326{
6327 int rc = 0;
6328
6329#ifdef CONFIG_BNXT_SRIOV
6330 if (bp->sriov_cfg) {
6331 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6332 !bp->sriov_cfg,
6333 BNXT_SRIOV_CFG_WAIT_TMO);
6334 if (rc)
6335 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6336 }
6337#endif
6338 /* Change device state to avoid TX queue wake up's */
6339 bnxt_tx_disable(bp);
6340
Michael Chancaefe522015-12-09 19:35:42 -05006341 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05006342 smp_mb__after_atomic();
Michael Chanf9b76eb2017-07-11 13:05:34 -04006343 while (bnxt_drv_busy(bp))
Michael Chan4cebdce2015-12-09 19:35:43 -05006344 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04006345
Michael Chan9d8bc092016-12-29 12:13:33 -05006346 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04006347 bnxt_shutdown_nic(bp, irq_re_init);
6348
6349 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6350
6351 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006352 del_timer_sync(&bp->timer);
6353 bnxt_free_skbs(bp);
6354
6355 if (irq_re_init) {
6356 bnxt_free_irq(bp);
6357 bnxt_del_napi(bp);
6358 }
6359 bnxt_free_mem(bp, irq_re_init);
6360 return rc;
6361}
6362
6363static int bnxt_close(struct net_device *dev)
6364{
6365 struct bnxt *bp = netdev_priv(dev);
6366
6367 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006368 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006369 return 0;
6370}
6371
6372/* rtnl_lock held */
6373static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6374{
6375 switch (cmd) {
6376 case SIOCGMIIPHY:
6377 /* fallthru */
6378 case SIOCGMIIREG: {
6379 if (!netif_running(dev))
6380 return -EAGAIN;
6381
6382 return 0;
6383 }
6384
6385 case SIOCSMIIREG:
6386 if (!netif_running(dev))
6387 return -EAGAIN;
6388
6389 return 0;
6390
6391 default:
6392 /* do nothing */
6393 break;
6394 }
6395 return -EOPNOTSUPP;
6396}
6397
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006398static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006399bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6400{
6401 u32 i;
6402 struct bnxt *bp = netdev_priv(dev);
6403
Michael Chanf9b76eb2017-07-11 13:05:34 -04006404 set_bit(BNXT_STATE_READ_STATS, &bp->state);
6405 /* Make sure bnxt_close_nic() sees that we are reading stats before
6406 * we check the BNXT_STATE_OPEN flag.
6407 */
6408 smp_mb__after_atomic();
6409 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6410 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006411 return;
Michael Chanf9b76eb2017-07-11 13:05:34 -04006412 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006413
6414 /* TODO check if we need to synchronize with bnxt_close path */
6415 for (i = 0; i < bp->cp_nr_rings; i++) {
6416 struct bnxt_napi *bnapi = bp->bnapi[i];
6417 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6418 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6419
6420 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6421 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6422 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6423
6424 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6425 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6426 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6427
6428 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6429 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6430 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6431
6432 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6433 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6434 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6435
6436 stats->rx_missed_errors +=
6437 le64_to_cpu(hw_stats->rx_discard_pkts);
6438
6439 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6440
Michael Chanc0c050c2015-10-22 16:01:17 -04006441 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6442 }
6443
Michael Chan9947f832016-03-07 15:38:46 -05006444 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6445 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6446 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6447
6448 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6449 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6450 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6451 le64_to_cpu(rx->rx_ovrsz_frames) +
6452 le64_to_cpu(rx->rx_runt_frames);
6453 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6454 le64_to_cpu(rx->rx_jbr_frames);
6455 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6456 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6457 stats->tx_errors = le64_to_cpu(tx->tx_err);
6458 }
Michael Chanf9b76eb2017-07-11 13:05:34 -04006459 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006460}
6461
6462static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6463{
6464 struct net_device *dev = bp->dev;
6465 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6466 struct netdev_hw_addr *ha;
6467 u8 *haddr;
6468 int mc_count = 0;
6469 bool update = false;
6470 int off = 0;
6471
6472 netdev_for_each_mc_addr(ha, dev) {
6473 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6474 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6475 vnic->mc_list_count = 0;
6476 return false;
6477 }
6478 haddr = ha->addr;
6479 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6480 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6481 update = true;
6482 }
6483 off += ETH_ALEN;
6484 mc_count++;
6485 }
6486 if (mc_count)
6487 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6488
6489 if (mc_count != vnic->mc_list_count) {
6490 vnic->mc_list_count = mc_count;
6491 update = true;
6492 }
6493 return update;
6494}
6495
6496static bool bnxt_uc_list_updated(struct bnxt *bp)
6497{
6498 struct net_device *dev = bp->dev;
6499 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6500 struct netdev_hw_addr *ha;
6501 int off = 0;
6502
6503 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6504 return true;
6505
6506 netdev_for_each_uc_addr(ha, dev) {
6507 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6508 return true;
6509
6510 off += ETH_ALEN;
6511 }
6512 return false;
6513}
6514
6515static void bnxt_set_rx_mode(struct net_device *dev)
6516{
6517 struct bnxt *bp = netdev_priv(dev);
6518 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6519 u32 mask = vnic->rx_mask;
6520 bool mc_update = false;
6521 bool uc_update;
6522
6523 if (!netif_running(dev))
6524 return;
6525
6526 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6527 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6528 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6529
Michael Chan17c71ac2016-07-01 18:46:27 -04006530 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006531 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6532
6533 uc_update = bnxt_uc_list_updated(bp);
6534
6535 if (dev->flags & IFF_ALLMULTI) {
6536 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6537 vnic->mc_list_count = 0;
6538 } else {
6539 mc_update = bnxt_mc_list_updated(bp, &mask);
6540 }
6541
6542 if (mask != vnic->rx_mask || uc_update || mc_update) {
6543 vnic->rx_mask = mask;
6544
6545 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6546 schedule_work(&bp->sp_task);
6547 }
6548}
6549
Michael Chanb664f002015-12-02 01:54:08 -05006550static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006551{
6552 struct net_device *dev = bp->dev;
6553 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6554 struct netdev_hw_addr *ha;
6555 int i, off = 0, rc;
6556 bool uc_update;
6557
6558 netif_addr_lock_bh(dev);
6559 uc_update = bnxt_uc_list_updated(bp);
6560 netif_addr_unlock_bh(dev);
6561
6562 if (!uc_update)
6563 goto skip_uc;
6564
6565 mutex_lock(&bp->hwrm_cmd_lock);
6566 for (i = 1; i < vnic->uc_filter_count; i++) {
6567 struct hwrm_cfa_l2_filter_free_input req = {0};
6568
6569 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6570 -1);
6571
6572 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6573
6574 rc = _hwrm_send_message(bp, &req, sizeof(req),
6575 HWRM_CMD_TIMEOUT);
6576 }
6577 mutex_unlock(&bp->hwrm_cmd_lock);
6578
6579 vnic->uc_filter_count = 1;
6580
6581 netif_addr_lock_bh(dev);
6582 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6583 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6584 } else {
6585 netdev_for_each_uc_addr(ha, dev) {
6586 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6587 off += ETH_ALEN;
6588 vnic->uc_filter_count++;
6589 }
6590 }
6591 netif_addr_unlock_bh(dev);
6592
6593 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6594 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6595 if (rc) {
6596 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6597 rc);
6598 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006599 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006600 }
6601 }
6602
6603skip_uc:
6604 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6605 if (rc)
6606 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6607 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006608
6609 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006610}
6611
Michael Chan8079e8f2016-12-29 12:13:37 -05006612/* If the chip and firmware supports RFS */
6613static bool bnxt_rfs_supported(struct bnxt *bp)
6614{
6615 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6616 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006617 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6618 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006619 return false;
6620}
6621
6622/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006623static bool bnxt_rfs_capable(struct bnxt *bp)
6624{
6625#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006626 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006627
Michael Chan964fd482017-02-12 19:18:13 -05006628 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006629 return false;
6630
6631 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006632 max_vnics = bnxt_get_max_func_vnics(bp);
6633 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006634
6635 /* RSS contexts not a limiting factor */
6636 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6637 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006638 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006639 netdev_warn(bp->dev,
6640 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006641 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006642 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006643 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006644
6645 return true;
6646#else
6647 return false;
6648#endif
6649}
6650
Michael Chanc0c050c2015-10-22 16:01:17 -04006651static netdev_features_t bnxt_fix_features(struct net_device *dev,
6652 netdev_features_t features)
6653{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006654 struct bnxt *bp = netdev_priv(dev);
6655
Vasundhara Volama2304902016-07-25 12:33:36 -04006656 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006657 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006658
6659 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6660 * turned on or off together.
6661 */
6662 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6663 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6664 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6665 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6666 NETIF_F_HW_VLAN_STAG_RX);
6667 else
6668 features |= NETIF_F_HW_VLAN_CTAG_RX |
6669 NETIF_F_HW_VLAN_STAG_RX;
6670 }
Michael Chancf6645f2016-06-13 02:25:28 -04006671#ifdef CONFIG_BNXT_SRIOV
6672 if (BNXT_VF(bp)) {
6673 if (bp->vf.vlan) {
6674 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6675 NETIF_F_HW_VLAN_STAG_RX);
6676 }
6677 }
6678#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006679 return features;
6680}
6681
6682static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6683{
6684 struct bnxt *bp = netdev_priv(dev);
6685 u32 flags = bp->flags;
6686 u32 changes;
6687 int rc = 0;
6688 bool re_init = false;
6689 bool update_tpa = false;
6690
6691 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006692 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006693 flags |= BNXT_FLAG_GRO;
6694 if (features & NETIF_F_LRO)
6695 flags |= BNXT_FLAG_LRO;
6696
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006697 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6698 flags &= ~BNXT_FLAG_TPA;
6699
Michael Chanc0c050c2015-10-22 16:01:17 -04006700 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6701 flags |= BNXT_FLAG_STRIP_VLAN;
6702
6703 if (features & NETIF_F_NTUPLE)
6704 flags |= BNXT_FLAG_RFS;
6705
6706 changes = flags ^ bp->flags;
6707 if (changes & BNXT_FLAG_TPA) {
6708 update_tpa = true;
6709 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6710 (flags & BNXT_FLAG_TPA) == 0)
6711 re_init = true;
6712 }
6713
6714 if (changes & ~BNXT_FLAG_TPA)
6715 re_init = true;
6716
6717 if (flags != bp->flags) {
6718 u32 old_flags = bp->flags;
6719
6720 bp->flags = flags;
6721
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006722 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006723 if (update_tpa)
6724 bnxt_set_ring_params(bp);
6725 return rc;
6726 }
6727
6728 if (re_init) {
6729 bnxt_close_nic(bp, false, false);
6730 if (update_tpa)
6731 bnxt_set_ring_params(bp);
6732
6733 return bnxt_open_nic(bp, false, false);
6734 }
6735 if (update_tpa) {
6736 rc = bnxt_set_tpa(bp,
6737 (flags & BNXT_FLAG_TPA) ?
6738 true : false);
6739 if (rc)
6740 bp->flags = old_flags;
6741 }
6742 }
6743 return rc;
6744}
6745
Michael Chan9f554592016-01-02 23:44:58 -05006746static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6747{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006748 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006749 int i = bnapi->index;
6750
Michael Chan3b2b7d92016-01-02 23:45:00 -05006751 if (!txr)
6752 return;
6753
Michael Chan9f554592016-01-02 23:44:58 -05006754 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6755 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6756 txr->tx_cons);
6757}
6758
6759static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6760{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006761 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006762 int i = bnapi->index;
6763
Michael Chan3b2b7d92016-01-02 23:45:00 -05006764 if (!rxr)
6765 return;
6766
Michael Chan9f554592016-01-02 23:44:58 -05006767 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",
6768 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6769 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6770 rxr->rx_sw_agg_prod);
6771}
6772
6773static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6774{
6775 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6776 int i = bnapi->index;
6777
6778 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6779 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6780}
6781
Michael Chanc0c050c2015-10-22 16:01:17 -04006782static void bnxt_dbg_dump_states(struct bnxt *bp)
6783{
6784 int i;
6785 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006786
6787 for (i = 0; i < bp->cp_nr_rings; i++) {
6788 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006789 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006790 bnxt_dump_tx_sw_state(bnapi);
6791 bnxt_dump_rx_sw_state(bnapi);
6792 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006793 }
6794 }
6795}
6796
Michael Chan6988bd92016-06-13 02:25:29 -04006797static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006798{
Michael Chan6988bd92016-06-13 02:25:29 -04006799 if (!silent)
6800 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006801 if (netif_running(bp->dev)) {
Michael Chanb386cd32017-03-08 18:44:33 -05006802 int rc;
6803
6804 if (!silent)
6805 bnxt_ulp_stop(bp);
Michael Chan028de142015-12-09 19:35:44 -05006806 bnxt_close_nic(bp, false, false);
Michael Chanb386cd32017-03-08 18:44:33 -05006807 rc = bnxt_open_nic(bp, false, false);
6808 if (!silent && !rc)
6809 bnxt_ulp_start(bp);
Michael Chan028de142015-12-09 19:35:44 -05006810 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006811}
6812
6813static void bnxt_tx_timeout(struct net_device *dev)
6814{
6815 struct bnxt *bp = netdev_priv(dev);
6816
6817 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6818 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6819 schedule_work(&bp->sp_task);
6820}
6821
6822#ifdef CONFIG_NET_POLL_CONTROLLER
6823static void bnxt_poll_controller(struct net_device *dev)
6824{
6825 struct bnxt *bp = netdev_priv(dev);
6826 int i;
6827
Michael Chan2270bc52017-06-23 14:01:01 -04006828 /* Only process tx rings/combined rings in netpoll mode. */
6829 for (i = 0; i < bp->tx_nr_rings; i++) {
6830 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006831
Michael Chan2270bc52017-06-23 14:01:01 -04006832 napi_schedule(&txr->bnapi->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006833 }
6834}
6835#endif
6836
6837static void bnxt_timer(unsigned long data)
6838{
6839 struct bnxt *bp = (struct bnxt *)data;
6840 struct net_device *dev = bp->dev;
6841
6842 if (!netif_running(dev))
6843 return;
6844
6845 if (atomic_read(&bp->intr_sem) != 0)
6846 goto bnxt_restart_timer;
6847
Michael Chanadcc3312017-07-24 12:34:24 -04006848 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
6849 bp->stats_coal_ticks) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05006850 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6851 schedule_work(&bp->sp_task);
6852 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006853bnxt_restart_timer:
6854 mod_timer(&bp->timer, jiffies + bp->current_interval);
6855}
6856
Michael Chana551ee92017-01-25 02:55:07 -05006857static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006858{
Michael Chana551ee92017-01-25 02:55:07 -05006859 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6860 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006861 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6862 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6863 */
6864 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6865 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006866}
6867
6868static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6869{
Michael Chan6988bd92016-06-13 02:25:29 -04006870 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6871 rtnl_unlock();
6872}
6873
Michael Chana551ee92017-01-25 02:55:07 -05006874/* Only called from bnxt_sp_task() */
6875static void bnxt_reset(struct bnxt *bp, bool silent)
6876{
6877 bnxt_rtnl_lock_sp(bp);
6878 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6879 bnxt_reset_task(bp, silent);
6880 bnxt_rtnl_unlock_sp(bp);
6881}
6882
Michael Chanc0c050c2015-10-22 16:01:17 -04006883static void bnxt_cfg_ntp_filters(struct bnxt *);
6884
6885static void bnxt_sp_task(struct work_struct *work)
6886{
6887 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006888
Michael Chan4cebdce2015-12-09 19:35:43 -05006889 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6890 smp_mb__after_atomic();
6891 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6892 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006893 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006894 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006895
6896 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6897 bnxt_cfg_rx_mode(bp);
6898
6899 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6900 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006901 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6902 bnxt_hwrm_exec_fwd_req(bp);
6903 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6904 bnxt_hwrm_tunnel_dst_port_alloc(
6905 bp, bp->vxlan_port,
6906 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6907 }
6908 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6909 bnxt_hwrm_tunnel_dst_port_free(
6910 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6911 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006912 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6913 bnxt_hwrm_tunnel_dst_port_alloc(
6914 bp, bp->nge_port,
6915 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6916 }
6917 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6918 bnxt_hwrm_tunnel_dst_port_free(
6919 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6920 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006921 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6922 bnxt_hwrm_port_qstats(bp);
6923
Michael Chana551ee92017-01-25 02:55:07 -05006924 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6925 * must be the last functions to be called before exiting.
6926 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006927 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6928 int rc = 0;
6929
6930 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6931 &bp->sp_event))
6932 bnxt_hwrm_phy_qcaps(bp);
6933
6934 bnxt_rtnl_lock_sp(bp);
6935 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6936 rc = bnxt_update_link(bp, true);
6937 bnxt_rtnl_unlock_sp(bp);
6938 if (rc)
6939 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6940 rc);
6941 }
Michael Chan90c694b2017-01-25 02:55:09 -05006942 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6943 bnxt_rtnl_lock_sp(bp);
6944 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6945 bnxt_get_port_module_status(bp);
6946 bnxt_rtnl_unlock_sp(bp);
6947 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006948 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6949 bnxt_reset(bp, false);
6950
6951 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6952 bnxt_reset(bp, true);
6953
Michael Chanc0c050c2015-10-22 16:01:17 -04006954 smp_mb__before_atomic();
6955 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6956}
6957
Michael Chand1e79252017-02-06 16:55:38 -05006958/* Under rtnl_lock */
Michael Chan3b6b34d2017-07-11 13:05:35 -04006959int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
6960 int tx_xdp)
Michael Chand1e79252017-02-06 16:55:38 -05006961{
6962 int max_rx, max_tx, tx_sets = 1;
6963 int tx_rings_needed;
Michael Chand1e79252017-02-06 16:55:38 -05006964 int rc;
6965
Michael Chand1e79252017-02-06 16:55:38 -05006966 if (tcs)
6967 tx_sets = tcs;
6968
6969 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6970 if (rc)
6971 return rc;
6972
6973 if (max_rx < rx)
6974 return -ENOMEM;
6975
Michael Chan5f449242017-02-06 16:55:40 -05006976 tx_rings_needed = tx * tx_sets + tx_xdp;
Michael Chand1e79252017-02-06 16:55:38 -05006977 if (max_tx < tx_rings_needed)
6978 return -ENOMEM;
6979
6980 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
Michael Chan5f449242017-02-06 16:55:40 -05006981 tx_rings_needed < (tx * tx_sets + tx_xdp))
Michael Chand1e79252017-02-06 16:55:38 -05006982 return -ENOMEM;
6983 return 0;
6984}
6985
Sathya Perla17086392017-02-20 19:25:18 -05006986static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6987{
6988 if (bp->bar2) {
6989 pci_iounmap(pdev, bp->bar2);
6990 bp->bar2 = NULL;
6991 }
6992
6993 if (bp->bar1) {
6994 pci_iounmap(pdev, bp->bar1);
6995 bp->bar1 = NULL;
6996 }
6997
6998 if (bp->bar0) {
6999 pci_iounmap(pdev, bp->bar0);
7000 bp->bar0 = NULL;
7001 }
7002}
7003
7004static void bnxt_cleanup_pci(struct bnxt *bp)
7005{
7006 bnxt_unmap_bars(bp, bp->pdev);
7007 pci_release_regions(bp->pdev);
7008 pci_disable_device(bp->pdev);
7009}
7010
Michael Chanc0c050c2015-10-22 16:01:17 -04007011static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7012{
7013 int rc;
7014 struct bnxt *bp = netdev_priv(dev);
7015
7016 SET_NETDEV_DEV(dev, &pdev->dev);
7017
7018 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7019 rc = pci_enable_device(pdev);
7020 if (rc) {
7021 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7022 goto init_err;
7023 }
7024
7025 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7026 dev_err(&pdev->dev,
7027 "Cannot find PCI device base address, aborting\n");
7028 rc = -ENODEV;
7029 goto init_err_disable;
7030 }
7031
7032 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7033 if (rc) {
7034 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7035 goto init_err_disable;
7036 }
7037
7038 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7039 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7040 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7041 goto init_err_disable;
7042 }
7043
7044 pci_set_master(pdev);
7045
7046 bp->dev = dev;
7047 bp->pdev = pdev;
7048
7049 bp->bar0 = pci_ioremap_bar(pdev, 0);
7050 if (!bp->bar0) {
7051 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7052 rc = -ENOMEM;
7053 goto init_err_release;
7054 }
7055
7056 bp->bar1 = pci_ioremap_bar(pdev, 2);
7057 if (!bp->bar1) {
7058 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7059 rc = -ENOMEM;
7060 goto init_err_release;
7061 }
7062
7063 bp->bar2 = pci_ioremap_bar(pdev, 4);
7064 if (!bp->bar2) {
7065 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7066 rc = -ENOMEM;
7067 goto init_err_release;
7068 }
7069
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007070 pci_enable_pcie_error_reporting(pdev);
7071
Michael Chanc0c050c2015-10-22 16:01:17 -04007072 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7073
7074 spin_lock_init(&bp->ntp_fltr_lock);
7075
7076 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7077 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7078
Michael Chandfb5b892016-02-26 04:00:01 -05007079 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05007080 bp->rx_coal_ticks = 12;
7081 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05007082 bp->rx_coal_ticks_irq = 1;
7083 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04007084
Michael Chandfc9c942016-02-26 04:00:03 -05007085 bp->tx_coal_ticks = 25;
7086 bp->tx_coal_bufs = 30;
7087 bp->tx_coal_ticks_irq = 2;
7088 bp->tx_coal_bufs_irq = 2;
7089
Michael Chan51f30782016-07-01 18:46:29 -04007090 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7091
Michael Chanc0c050c2015-10-22 16:01:17 -04007092 init_timer(&bp->timer);
7093 bp->timer.data = (unsigned long)bp;
7094 bp->timer.function = bnxt_timer;
7095 bp->current_interval = BNXT_TIMER_INTERVAL;
7096
Michael Chancaefe522015-12-09 19:35:42 -05007097 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04007098 return 0;
7099
7100init_err_release:
Sathya Perla17086392017-02-20 19:25:18 -05007101 bnxt_unmap_bars(bp, pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007102 pci_release_regions(pdev);
7103
7104init_err_disable:
7105 pci_disable_device(pdev);
7106
7107init_err:
7108 return rc;
7109}
7110
7111/* rtnl_lock held */
7112static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7113{
7114 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007115 struct bnxt *bp = netdev_priv(dev);
7116 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007117
7118 if (!is_valid_ether_addr(addr->sa_data))
7119 return -EADDRNOTAVAIL;
7120
Michael Chan84c33dd2016-04-11 04:11:13 -04007121 rc = bnxt_approve_mac(bp, addr->sa_data);
7122 if (rc)
7123 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007124
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007125 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7126 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007127
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05007128 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7129 if (netif_running(dev)) {
7130 bnxt_close_nic(bp, false, false);
7131 rc = bnxt_open_nic(bp, false, false);
7132 }
7133
7134 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007135}
7136
7137/* rtnl_lock held */
7138static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7139{
7140 struct bnxt *bp = netdev_priv(dev);
7141
Michael Chanc0c050c2015-10-22 16:01:17 -04007142 if (netif_running(dev))
7143 bnxt_close_nic(bp, false, false);
7144
7145 dev->mtu = new_mtu;
7146 bnxt_set_ring_params(bp);
7147
7148 if (netif_running(dev))
7149 return bnxt_open_nic(bp, false, false);
7150
7151 return 0;
7152}
7153
Michael Chanc5e3deb2016-12-02 21:17:15 -05007154int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04007155{
7156 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05007157 bool sh = false;
Michael Chand1e79252017-02-06 16:55:38 -05007158 int rc;
John Fastabend16e5cc62016-02-16 21:16:43 -08007159
Michael Chanc0c050c2015-10-22 16:01:17 -04007160 if (tc > bp->max_tc) {
Michael Chanb451c8b2017-02-12 19:18:17 -05007161 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04007162 tc, bp->max_tc);
7163 return -EINVAL;
7164 }
7165
7166 if (netdev_get_num_tc(dev) == tc)
7167 return 0;
7168
Michael Chan3ffb6a32016-11-11 00:11:42 -05007169 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7170 sh = true;
7171
Michael Chan5f449242017-02-06 16:55:40 -05007172 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
Michael Chan3b6b34d2017-07-11 13:05:35 -04007173 sh, tc, bp->tx_nr_rings_xdp);
Michael Chand1e79252017-02-06 16:55:38 -05007174 if (rc)
7175 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007176
7177 /* Needs to close the device and do hw resource re-allocations */
7178 if (netif_running(bp->dev))
7179 bnxt_close_nic(bp, true, false);
7180
7181 if (tc) {
7182 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7183 netdev_set_num_tc(dev, tc);
7184 } else {
7185 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7186 netdev_reset_tc(dev);
7187 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05007188 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7189 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04007190 bp->num_stat_ctxs = bp->cp_nr_rings;
7191
7192 if (netif_running(bp->dev))
7193 return bnxt_open_nic(bp, true, false);
7194
7195 return 0;
7196}
7197
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02007198static int bnxt_setup_tc(struct net_device *dev, u32 handle, u32 chain_index,
7199 __be16 proto, struct tc_to_netdev *ntc)
Michael Chanc5e3deb2016-12-02 21:17:15 -05007200{
7201 if (ntc->type != TC_SETUP_MQPRIO)
7202 return -EINVAL;
7203
Amritha Nambiar56f36ac2017-03-15 10:39:25 -07007204 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7205
7206 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
Michael Chanc5e3deb2016-12-02 21:17:15 -05007207}
7208
Michael Chanc0c050c2015-10-22 16:01:17 -04007209#ifdef CONFIG_RFS_ACCEL
7210static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7211 struct bnxt_ntuple_filter *f2)
7212{
7213 struct flow_keys *keys1 = &f1->fkeys;
7214 struct flow_keys *keys2 = &f2->fkeys;
7215
7216 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7217 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7218 keys1->ports.ports == keys2->ports.ports &&
7219 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7220 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chan61aad722017-02-12 19:18:14 -05007221 keys1->control.flags == keys2->control.flags &&
Michael Chana54c4d72016-07-25 12:33:35 -04007222 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7223 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04007224 return true;
7225
7226 return false;
7227}
7228
7229static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7230 u16 rxq_index, u32 flow_id)
7231{
7232 struct bnxt *bp = netdev_priv(dev);
7233 struct bnxt_ntuple_filter *fltr, *new_fltr;
7234 struct flow_keys *fkeys;
7235 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04007236 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007237 struct hlist_head *head;
7238
Michael Chana54c4d72016-07-25 12:33:35 -04007239 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7240 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7241 int off = 0, j;
7242
7243 netif_addr_lock_bh(dev);
7244 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7245 if (ether_addr_equal(eth->h_dest,
7246 vnic->uc_list + off)) {
7247 l2_idx = j + 1;
7248 break;
7249 }
7250 }
7251 netif_addr_unlock_bh(dev);
7252 if (!l2_idx)
7253 return -EINVAL;
7254 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007255 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7256 if (!new_fltr)
7257 return -ENOMEM;
7258
7259 fkeys = &new_fltr->fkeys;
7260 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7261 rc = -EPROTONOSUPPORT;
7262 goto err_free;
7263 }
7264
Michael Chandda0e742016-12-29 12:13:40 -05007265 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7266 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04007267 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7268 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7269 rc = -EPROTONOSUPPORT;
7270 goto err_free;
7271 }
Michael Chandda0e742016-12-29 12:13:40 -05007272 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7273 bp->hwrm_spec_code < 0x10601) {
7274 rc = -EPROTONOSUPPORT;
7275 goto err_free;
7276 }
Michael Chan61aad722017-02-12 19:18:14 -05007277 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7278 bp->hwrm_spec_code < 0x10601) {
7279 rc = -EPROTONOSUPPORT;
7280 goto err_free;
7281 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007282
Michael Chana54c4d72016-07-25 12:33:35 -04007283 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04007284 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7285
7286 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7287 head = &bp->ntp_fltr_hash_tbl[idx];
7288 rcu_read_lock();
7289 hlist_for_each_entry_rcu(fltr, head, hash) {
7290 if (bnxt_fltr_match(fltr, new_fltr)) {
7291 rcu_read_unlock();
7292 rc = 0;
7293 goto err_free;
7294 }
7295 }
7296 rcu_read_unlock();
7297
7298 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05007299 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7300 BNXT_NTP_FLTR_MAX_FLTR, 0);
7301 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007302 spin_unlock_bh(&bp->ntp_fltr_lock);
7303 rc = -ENOMEM;
7304 goto err_free;
7305 }
7306
Michael Chan84e86b92015-11-05 16:25:50 -05007307 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04007308 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04007309 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04007310 new_fltr->rxq = rxq_index;
7311 hlist_add_head_rcu(&new_fltr->hash, head);
7312 bp->ntp_fltr_count++;
7313 spin_unlock_bh(&bp->ntp_fltr_lock);
7314
7315 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7316 schedule_work(&bp->sp_task);
7317
7318 return new_fltr->sw_id;
7319
7320err_free:
7321 kfree(new_fltr);
7322 return rc;
7323}
7324
7325static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7326{
7327 int i;
7328
7329 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7330 struct hlist_head *head;
7331 struct hlist_node *tmp;
7332 struct bnxt_ntuple_filter *fltr;
7333 int rc;
7334
7335 head = &bp->ntp_fltr_hash_tbl[i];
7336 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7337 bool del = false;
7338
7339 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7340 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7341 fltr->flow_id,
7342 fltr->sw_id)) {
7343 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7344 fltr);
7345 del = true;
7346 }
7347 } else {
7348 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7349 fltr);
7350 if (rc)
7351 del = true;
7352 else
7353 set_bit(BNXT_FLTR_VALID, &fltr->state);
7354 }
7355
7356 if (del) {
7357 spin_lock_bh(&bp->ntp_fltr_lock);
7358 hlist_del_rcu(&fltr->hash);
7359 bp->ntp_fltr_count--;
7360 spin_unlock_bh(&bp->ntp_fltr_lock);
7361 synchronize_rcu();
7362 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7363 kfree(fltr);
7364 }
7365 }
7366 }
Jeffrey Huang19241362016-02-26 04:00:00 -05007367 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7368 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04007369}
7370
7371#else
7372
7373static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7374{
7375}
7376
7377#endif /* CONFIG_RFS_ACCEL */
7378
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007379static void bnxt_udp_tunnel_add(struct net_device *dev,
7380 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04007381{
7382 struct bnxt *bp = netdev_priv(dev);
7383
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007384 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7385 return;
7386
Michael Chanc0c050c2015-10-22 16:01:17 -04007387 if (!netif_running(dev))
7388 return;
7389
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007390 switch (ti->type) {
7391 case UDP_TUNNEL_TYPE_VXLAN:
7392 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7393 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007394
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007395 bp->vxlan_port_cnt++;
7396 if (bp->vxlan_port_cnt == 1) {
7397 bp->vxlan_port = ti->port;
7398 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04007399 schedule_work(&bp->sp_task);
7400 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007401 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007402 case UDP_TUNNEL_TYPE_GENEVE:
7403 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7404 return;
7405
7406 bp->nge_port_cnt++;
7407 if (bp->nge_port_cnt == 1) {
7408 bp->nge_port = ti->port;
7409 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7410 }
7411 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007412 default:
7413 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007414 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007415
7416 schedule_work(&bp->sp_task);
7417}
7418
7419static void bnxt_udp_tunnel_del(struct net_device *dev,
7420 struct udp_tunnel_info *ti)
7421{
7422 struct bnxt *bp = netdev_priv(dev);
7423
7424 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7425 return;
7426
7427 if (!netif_running(dev))
7428 return;
7429
7430 switch (ti->type) {
7431 case UDP_TUNNEL_TYPE_VXLAN:
7432 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7433 return;
7434 bp->vxlan_port_cnt--;
7435
7436 if (bp->vxlan_port_cnt != 0)
7437 return;
7438
7439 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7440 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007441 case UDP_TUNNEL_TYPE_GENEVE:
7442 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7443 return;
7444 bp->nge_port_cnt--;
7445
7446 if (bp->nge_port_cnt != 0)
7447 return;
7448
7449 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7450 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007451 default:
7452 return;
7453 }
7454
7455 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007456}
7457
Michael Chan39d8ba22017-07-24 12:34:22 -04007458static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7459 struct net_device *dev, u32 filter_mask,
7460 int nlflags)
7461{
7462 struct bnxt *bp = netdev_priv(dev);
7463
7464 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
7465 nlflags, filter_mask, NULL);
7466}
7467
7468static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7469 u16 flags)
7470{
7471 struct bnxt *bp = netdev_priv(dev);
7472 struct nlattr *attr, *br_spec;
7473 int rem, rc = 0;
7474
7475 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
7476 return -EOPNOTSUPP;
7477
7478 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7479 if (!br_spec)
7480 return -EINVAL;
7481
7482 nla_for_each_nested(attr, br_spec, rem) {
7483 u16 mode;
7484
7485 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7486 continue;
7487
7488 if (nla_len(attr) < sizeof(mode))
7489 return -EINVAL;
7490
7491 mode = nla_get_u16(attr);
7492 if (mode == bp->br_mode)
7493 break;
7494
7495 rc = bnxt_hwrm_set_br_mode(bp, mode);
7496 if (!rc)
7497 bp->br_mode = mode;
7498 break;
7499 }
7500 return rc;
7501}
7502
Michael Chanc0c050c2015-10-22 16:01:17 -04007503static const struct net_device_ops bnxt_netdev_ops = {
7504 .ndo_open = bnxt_open,
7505 .ndo_start_xmit = bnxt_start_xmit,
7506 .ndo_stop = bnxt_close,
7507 .ndo_get_stats64 = bnxt_get_stats64,
7508 .ndo_set_rx_mode = bnxt_set_rx_mode,
7509 .ndo_do_ioctl = bnxt_ioctl,
7510 .ndo_validate_addr = eth_validate_addr,
7511 .ndo_set_mac_address = bnxt_change_mac_addr,
7512 .ndo_change_mtu = bnxt_change_mtu,
7513 .ndo_fix_features = bnxt_fix_features,
7514 .ndo_set_features = bnxt_set_features,
7515 .ndo_tx_timeout = bnxt_tx_timeout,
7516#ifdef CONFIG_BNXT_SRIOV
7517 .ndo_get_vf_config = bnxt_get_vf_config,
7518 .ndo_set_vf_mac = bnxt_set_vf_mac,
7519 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7520 .ndo_set_vf_rate = bnxt_set_vf_bw,
7521 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7522 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7523#endif
7524#ifdef CONFIG_NET_POLL_CONTROLLER
7525 .ndo_poll_controller = bnxt_poll_controller,
7526#endif
7527 .ndo_setup_tc = bnxt_setup_tc,
7528#ifdef CONFIG_RFS_ACCEL
7529 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7530#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007531 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7532 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc6d30e82017-02-06 16:55:42 -05007533 .ndo_xdp = bnxt_xdp,
Michael Chan39d8ba22017-07-24 12:34:22 -04007534 .ndo_bridge_getlink = bnxt_bridge_getlink,
7535 .ndo_bridge_setlink = bnxt_bridge_setlink,
Michael Chanc0c050c2015-10-22 16:01:17 -04007536};
7537
7538static void bnxt_remove_one(struct pci_dev *pdev)
7539{
7540 struct net_device *dev = pci_get_drvdata(pdev);
7541 struct bnxt *bp = netdev_priv(dev);
7542
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007543 if (BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007544 bnxt_sriov_disable(bp);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007545 bnxt_dl_unregister(bp);
7546 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007547
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007548 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007549 unregister_netdev(dev);
7550 cancel_work_sync(&bp->sp_task);
7551 bp->sp_event = 0;
7552
Michael Chan78095922016-12-07 00:26:16 -05007553 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007554 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007555 bnxt_free_hwrm_resources(bp);
Deepak Khungare605db82017-05-29 19:06:04 -04007556 bnxt_free_hwrm_short_cmd_req(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007557 bnxt_ethtool_free(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007558 bnxt_dcb_free(bp);
Michael Chana588e452016-12-07 00:26:21 -05007559 kfree(bp->edev);
7560 bp->edev = NULL;
Michael Chanc6d30e82017-02-06 16:55:42 -05007561 if (bp->xdp_prog)
7562 bpf_prog_put(bp->xdp_prog);
Sathya Perla17086392017-02-20 19:25:18 -05007563 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007564 free_netdev(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007565}
7566
7567static int bnxt_probe_phy(struct bnxt *bp)
7568{
7569 int rc = 0;
7570 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007571
Michael Chan170ce012016-04-05 14:08:57 -04007572 rc = bnxt_hwrm_phy_qcaps(bp);
7573 if (rc) {
7574 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7575 rc);
7576 return rc;
7577 }
7578
Michael Chanc0c050c2015-10-22 16:01:17 -04007579 rc = bnxt_update_link(bp, false);
7580 if (rc) {
7581 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7582 rc);
7583 return rc;
7584 }
7585
Michael Chan93ed8112016-06-13 02:25:37 -04007586 /* Older firmware does not have supported_auto_speeds, so assume
7587 * that all supported speeds can be autonegotiated.
7588 */
7589 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7590 link_info->support_auto_speeds = link_info->support_speeds;
7591
Michael Chanc0c050c2015-10-22 16:01:17 -04007592 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007593 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007594 link_info->autoneg = BNXT_AUTONEG_SPEED;
7595 if (bp->hwrm_spec_code >= 0x10201) {
7596 if (link_info->auto_pause_setting &
7597 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7598 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7599 } else {
7600 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7601 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007602 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007603 } else {
7604 link_info->req_link_speed = link_info->force_link_speed;
7605 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007606 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007607 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7608 link_info->req_flow_ctrl =
7609 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7610 else
7611 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007612 return rc;
7613}
7614
7615static int bnxt_get_max_irq(struct pci_dev *pdev)
7616{
7617 u16 ctrl;
7618
7619 if (!pdev->msix_cap)
7620 return 1;
7621
7622 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7623 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7624}
7625
Michael Chan6e6c5a52016-01-02 23:45:02 -05007626static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7627 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007628{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007629 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007630
Michael Chan379a80a2015-10-23 15:06:19 -04007631#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007632 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007633 *max_tx = bp->vf.max_tx_rings;
7634 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007635 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7636 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007637 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007638 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007639#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007640 {
7641 *max_tx = bp->pf.max_tx_rings;
7642 *max_rx = bp->pf.max_rx_rings;
7643 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7644 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7645 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007646 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007647 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7648 *max_cp -= 1;
7649 *max_rx -= 2;
7650 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007651 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7652 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007653 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007654}
7655
7656int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7657{
7658 int rx, tx, cp;
7659
7660 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7661 if (!rx || !tx || !cp)
7662 return -ENOMEM;
7663
7664 *max_rx = rx;
7665 *max_tx = tx;
7666 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7667}
7668
Michael Chane4060d32016-12-07 00:26:19 -05007669static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7670 bool shared)
7671{
7672 int rc;
7673
7674 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007675 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7676 /* Not enough rings, try disabling agg rings. */
7677 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7678 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7679 if (rc)
7680 return rc;
7681 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7682 bp->dev->hw_features &= ~NETIF_F_LRO;
7683 bp->dev->features &= ~NETIF_F_LRO;
7684 bnxt_set_ring_params(bp);
7685 }
Michael Chane4060d32016-12-07 00:26:19 -05007686
7687 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7688 int max_cp, max_stat, max_irq;
7689
7690 /* Reserve minimum resources for RoCE */
7691 max_cp = bnxt_get_max_func_cp_rings(bp);
7692 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7693 max_irq = bnxt_get_max_func_irqs(bp);
7694 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7695 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7696 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7697 return 0;
7698
7699 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7700 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7701 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7702 max_cp = min_t(int, max_cp, max_irq);
7703 max_cp = min_t(int, max_cp, max_stat);
7704 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7705 if (rc)
7706 rc = 0;
7707 }
7708 return rc;
7709}
7710
Michael Chan702c2212017-05-29 19:06:10 -04007711static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
Michael Chan6e6c5a52016-01-02 23:45:02 -05007712{
7713 int dflt_rings, max_rx_rings, max_tx_rings, rc;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007714
7715 if (sh)
7716 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7717 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007718 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007719 if (rc)
7720 return rc;
7721 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7722 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007723
7724 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7725 if (rc)
7726 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7727
Michael Chan6e6c5a52016-01-02 23:45:02 -05007728 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7729 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7730 bp->tx_nr_rings + bp->rx_nr_rings;
7731 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007732 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7733 bp->rx_nr_rings++;
7734 bp->cp_nr_rings++;
7735 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007736 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007737}
7738
Michael Chan7b08f662016-12-07 00:26:18 -05007739void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7740{
7741 ASSERT_RTNL();
7742 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007743 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007744}
7745
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007746static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7747{
7748 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7749 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7750
7751 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7752 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7753 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7754 else
7755 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7756 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7757 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7758 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7759 "Unknown", width);
7760}
7761
Michael Chanc0c050c2015-10-22 16:01:17 -04007762static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7763{
7764 static int version_printed;
7765 struct net_device *dev;
7766 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007767 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007768
Ray Jui4e003382017-02-20 19:25:16 -05007769 if (pci_is_bridge(pdev))
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007770 return -ENODEV;
7771
Michael Chanc0c050c2015-10-22 16:01:17 -04007772 if (version_printed++ == 0)
7773 pr_info("%s", version);
7774
7775 max_irqs = bnxt_get_max_irq(pdev);
7776 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7777 if (!dev)
7778 return -ENOMEM;
7779
7780 bp = netdev_priv(dev);
7781
7782 if (bnxt_vf_pciid(ent->driver_data))
7783 bp->flags |= BNXT_FLAG_VF;
7784
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007785 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007786 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007787
7788 rc = bnxt_init_board(pdev, dev);
7789 if (rc < 0)
7790 goto init_err_free;
7791
7792 dev->netdev_ops = &bnxt_netdev_ops;
7793 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7794 dev->ethtool_ops = &bnxt_ethtool_ops;
Michael Chanc0c050c2015-10-22 16:01:17 -04007795 pci_set_drvdata(pdev, dev);
7796
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007797 rc = bnxt_alloc_hwrm_resources(bp);
7798 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007799 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007800
7801 mutex_init(&bp->hwrm_cmd_lock);
7802 rc = bnxt_hwrm_ver_get(bp);
7803 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007804 goto init_err_pci_clean;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007805
Deepak Khungare605db82017-05-29 19:06:04 -04007806 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7807 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7808 if (rc)
7809 goto init_err_pci_clean;
7810 }
7811
Michael Chan3c2217a2017-03-08 18:44:32 -05007812 rc = bnxt_hwrm_func_reset(bp);
7813 if (rc)
7814 goto init_err_pci_clean;
7815
Rob Swindell5ac67d82016-09-19 03:58:03 -04007816 bnxt_hwrm_fw_set_time(bp);
7817
Michael Chanc0c050c2015-10-22 16:01:17 -04007818 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7819 NETIF_F_TSO | NETIF_F_TSO6 |
7820 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007821 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007822 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7823 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007824 NETIF_F_RXCSUM | NETIF_F_GRO;
7825
7826 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7827 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007828
Michael Chanc0c050c2015-10-22 16:01:17 -04007829 dev->hw_enc_features =
7830 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7831 NETIF_F_TSO | NETIF_F_TSO6 |
7832 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007833 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007834 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007835 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7836 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007837 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7838 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7839 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7840 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7841 dev->priv_flags |= IFF_UNICAST_FLT;
7842
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007843 /* MTU range: 60 - 9500 */
7844 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007845 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007846
Michael Chanc0c050c2015-10-22 16:01:17 -04007847#ifdef CONFIG_BNXT_SRIOV
7848 init_waitqueue_head(&bp->sriov_cfg_wait);
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007849 mutex_init(&bp->sriov_lock);
Michael Chanc0c050c2015-10-22 16:01:17 -04007850#endif
Michael Chan309369c2016-06-13 02:25:34 -04007851 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan3284f9e2017-05-29 19:06:07 -04007852 if (BNXT_CHIP_P4_PLUS(bp))
Michael Chan94758f82016-06-13 02:25:35 -04007853 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan434c9752017-05-29 19:06:08 -04007854 else
7855 bp->flags |= BNXT_FLAG_DOUBLE_DB;
Michael Chan309369c2016-06-13 02:25:34 -04007856
Michael Chanc0c050c2015-10-22 16:01:17 -04007857 rc = bnxt_hwrm_func_drv_rgtr(bp);
7858 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007859 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007860
Michael Chana1653b12016-12-07 00:26:20 -05007861 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7862 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007863 goto init_err_pci_clean;
Michael Chana1653b12016-12-07 00:26:20 -05007864
Michael Chana588e452016-12-07 00:26:21 -05007865 bp->ulp_probe = bnxt_ulp_probe;
7866
Michael Chanc0c050c2015-10-22 16:01:17 -04007867 /* Get the MAX capabilities for this function */
7868 rc = bnxt_hwrm_func_qcaps(bp);
7869 if (rc) {
7870 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7871 rc);
7872 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007873 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007874 }
7875
7876 rc = bnxt_hwrm_queue_qportcfg(bp);
7877 if (rc) {
7878 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7879 rc);
7880 rc = -1;
Sathya Perla17086392017-02-20 19:25:18 -05007881 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007882 }
7883
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007884 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007885 bnxt_hwrm_port_led_qcaps(bp);
Michael Chaneb513652017-04-04 18:14:12 -04007886 bnxt_ethtool_init(bp);
Michael Chan87fe6032017-05-16 16:39:43 -04007887 bnxt_dcb_init(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007888
Michael Chanc61fb992017-02-06 16:55:36 -05007889 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007890 bnxt_set_tpa_flags(bp);
7891 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007892 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan702c2212017-05-29 19:06:10 -04007893 rc = bnxt_set_dflt_rings(bp, true);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007894 if (rc) {
7895 netdev_err(bp->dev, "Not enough rings available.\n");
7896 rc = -ENOMEM;
Sathya Perla17086392017-02-20 19:25:18 -05007897 goto init_err_pci_clean;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007898 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007899
Michael Chan87da7f72016-11-16 21:13:09 -05007900 /* Default RSS hash cfg. */
7901 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7902 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7903 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7904 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chan3284f9e2017-05-29 19:06:07 -04007905 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
Michael Chan87da7f72016-11-16 21:13:09 -05007906 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7907 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7908 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7909 }
7910
Michael Chan8fdefd62016-12-29 12:13:36 -05007911 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007912 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007913 dev->hw_features |= NETIF_F_NTUPLE;
7914 if (bnxt_rfs_capable(bp)) {
7915 bp->flags |= BNXT_FLAG_RFS;
7916 dev->features |= NETIF_F_NTUPLE;
7917 }
7918 }
7919
Michael Chanc0c050c2015-10-22 16:01:17 -04007920 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7921 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7922
7923 rc = bnxt_probe_phy(bp);
7924 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007925 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007926
Michael Chan78095922016-12-07 00:26:16 -05007927 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007928 if (rc)
Sathya Perla17086392017-02-20 19:25:18 -05007929 goto init_err_pci_clean;
Michael Chanc0c050c2015-10-22 16:01:17 -04007930
Michael Chanc1ef1462017-04-04 18:14:07 -04007931 bnxt_get_wol_settings(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007932 if (bp->flags & BNXT_FLAG_WOL_CAP)
7933 device_set_wakeup_enable(&pdev->dev, bp->wol);
7934 else
7935 device_set_wakeup_capable(&pdev->dev, false);
Michael Chanc1ef1462017-04-04 18:14:07 -04007936
Michael Chan78095922016-12-07 00:26:16 -05007937 rc = register_netdev(dev);
7938 if (rc)
7939 goto init_err_clr_int;
7940
Sathya Perla4ab0c6a2017-07-24 12:34:27 -04007941 if (BNXT_PF(bp))
7942 bnxt_dl_register(bp);
7943
Michael Chanc0c050c2015-10-22 16:01:17 -04007944 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7945 board_info[ent->driver_data].name,
7946 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7947
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007948 bnxt_parse_log_pcie_link(bp);
7949
Michael Chanc0c050c2015-10-22 16:01:17 -04007950 return 0;
7951
Michael Chan78095922016-12-07 00:26:16 -05007952init_err_clr_int:
7953 bnxt_clear_int_mode(bp);
7954
Sathya Perla17086392017-02-20 19:25:18 -05007955init_err_pci_clean:
7956 bnxt_cleanup_pci(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007957
7958init_err_free:
7959 free_netdev(dev);
7960 return rc;
7961}
7962
Michael Chand196ece2017-04-04 18:14:08 -04007963static void bnxt_shutdown(struct pci_dev *pdev)
7964{
7965 struct net_device *dev = pci_get_drvdata(pdev);
7966 struct bnxt *bp;
7967
7968 if (!dev)
7969 return;
7970
7971 rtnl_lock();
7972 bp = netdev_priv(dev);
7973 if (!bp)
7974 goto shutdown_exit;
7975
7976 if (netif_running(dev))
7977 dev_close(dev);
7978
7979 if (system_state == SYSTEM_POWER_OFF) {
Michael Chan0efd2fc2017-05-29 19:06:06 -04007980 bnxt_ulp_shutdown(bp);
Michael Chand196ece2017-04-04 18:14:08 -04007981 bnxt_clear_int_mode(bp);
7982 pci_wake_from_d3(pdev, bp->wol);
7983 pci_set_power_state(pdev, PCI_D3hot);
7984 }
7985
7986shutdown_exit:
7987 rtnl_unlock();
7988}
7989
Michael Chanf65a2042017-04-04 18:14:11 -04007990#ifdef CONFIG_PM_SLEEP
7991static int bnxt_suspend(struct device *device)
7992{
7993 struct pci_dev *pdev = to_pci_dev(device);
7994 struct net_device *dev = pci_get_drvdata(pdev);
7995 struct bnxt *bp = netdev_priv(dev);
7996 int rc = 0;
7997
7998 rtnl_lock();
7999 if (netif_running(dev)) {
8000 netif_device_detach(dev);
8001 rc = bnxt_close(dev);
8002 }
8003 bnxt_hwrm_func_drv_unrgtr(bp);
8004 rtnl_unlock();
8005 return rc;
8006}
8007
8008static int bnxt_resume(struct device *device)
8009{
8010 struct pci_dev *pdev = to_pci_dev(device);
8011 struct net_device *dev = pci_get_drvdata(pdev);
8012 struct bnxt *bp = netdev_priv(dev);
8013 int rc = 0;
8014
8015 rtnl_lock();
8016 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8017 rc = -ENODEV;
8018 goto resume_exit;
8019 }
8020 rc = bnxt_hwrm_func_reset(bp);
8021 if (rc) {
8022 rc = -EBUSY;
8023 goto resume_exit;
8024 }
8025 bnxt_get_wol_settings(bp);
8026 if (netif_running(dev)) {
8027 rc = bnxt_open(dev);
8028 if (!rc)
8029 netif_device_attach(dev);
8030 }
8031
8032resume_exit:
8033 rtnl_unlock();
8034 return rc;
8035}
8036
8037static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8038#define BNXT_PM_OPS (&bnxt_pm_ops)
8039
8040#else
8041
8042#define BNXT_PM_OPS NULL
8043
8044#endif /* CONFIG_PM_SLEEP */
8045
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008046/**
8047 * bnxt_io_error_detected - called when PCI error is detected
8048 * @pdev: Pointer to PCI device
8049 * @state: The current pci connection state
8050 *
8051 * This function is called after a PCI bus error affecting
8052 * this device has been detected.
8053 */
8054static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
8055 pci_channel_state_t state)
8056{
8057 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05008058 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008059
8060 netdev_info(netdev, "PCI I/O error detected\n");
8061
8062 rtnl_lock();
8063 netif_device_detach(netdev);
8064
Michael Chana588e452016-12-07 00:26:21 -05008065 bnxt_ulp_stop(bp);
8066
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008067 if (state == pci_channel_io_perm_failure) {
8068 rtnl_unlock();
8069 return PCI_ERS_RESULT_DISCONNECT;
8070 }
8071
8072 if (netif_running(netdev))
8073 bnxt_close(netdev);
8074
8075 pci_disable_device(pdev);
8076 rtnl_unlock();
8077
8078 /* Request a slot slot reset. */
8079 return PCI_ERS_RESULT_NEED_RESET;
8080}
8081
8082/**
8083 * bnxt_io_slot_reset - called after the pci bus has been reset.
8084 * @pdev: Pointer to PCI device
8085 *
8086 * Restart the card from scratch, as if from a cold-boot.
8087 * At this point, the card has exprienced a hard reset,
8088 * followed by fixups by BIOS, and has its config space
8089 * set up identically to what it was at cold boot.
8090 */
8091static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
8092{
8093 struct net_device *netdev = pci_get_drvdata(pdev);
8094 struct bnxt *bp = netdev_priv(netdev);
8095 int err = 0;
8096 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
8097
8098 netdev_info(bp->dev, "PCI Slot Reset\n");
8099
8100 rtnl_lock();
8101
8102 if (pci_enable_device(pdev)) {
8103 dev_err(&pdev->dev,
8104 "Cannot re-enable PCI device after reset.\n");
8105 } else {
8106 pci_set_master(pdev);
8107
Michael Chanaa8ed022016-12-07 00:26:17 -05008108 err = bnxt_hwrm_func_reset(bp);
8109 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008110 err = bnxt_open(netdev);
8111
Michael Chana588e452016-12-07 00:26:21 -05008112 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008113 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05008114 bnxt_ulp_start(bp);
8115 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008116 }
8117
8118 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
8119 dev_close(netdev);
8120
8121 rtnl_unlock();
8122
8123 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8124 if (err) {
8125 dev_err(&pdev->dev,
8126 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8127 err); /* non-fatal, continue */
8128 }
8129
8130 return PCI_ERS_RESULT_RECOVERED;
8131}
8132
8133/**
8134 * bnxt_io_resume - called when traffic can start flowing again.
8135 * @pdev: Pointer to PCI device
8136 *
8137 * This callback is called when the error recovery driver tells
8138 * us that its OK to resume normal operation.
8139 */
8140static void bnxt_io_resume(struct pci_dev *pdev)
8141{
8142 struct net_device *netdev = pci_get_drvdata(pdev);
8143
8144 rtnl_lock();
8145
8146 netif_device_attach(netdev);
8147
8148 rtnl_unlock();
8149}
8150
8151static const struct pci_error_handlers bnxt_err_handler = {
8152 .error_detected = bnxt_io_error_detected,
8153 .slot_reset = bnxt_io_slot_reset,
8154 .resume = bnxt_io_resume
8155};
8156
Michael Chanc0c050c2015-10-22 16:01:17 -04008157static struct pci_driver bnxt_pci_driver = {
8158 .name = DRV_MODULE_NAME,
8159 .id_table = bnxt_pci_tbl,
8160 .probe = bnxt_init_one,
8161 .remove = bnxt_remove_one,
Michael Chand196ece2017-04-04 18:14:08 -04008162 .shutdown = bnxt_shutdown,
Michael Chanf65a2042017-04-04 18:14:11 -04008163 .driver.pm = BNXT_PM_OPS,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05008164 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04008165#if defined(CONFIG_BNXT_SRIOV)
8166 .sriov_configure = bnxt_sriov_configure,
8167#endif
8168};
8169
8170module_pci_driver(bnxt_pci_driver);