blob: 277573b3d2610e55e3cb283b0402ac998d0e8750 [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 Chanc0c050c2015-10-22 16:01:17 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040035#include <linux/rtc.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040036#include <net/ip.h>
37#include <net/tcp.h>
38#include <net/udp.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070041#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040042#include <linux/workqueue.h>
43#include <linux/prefetch.h>
44#include <linux/cache.h>
45#include <linux/log2.h>
46#include <linux/aer.h>
47#include <linux/bitmap.h>
48#include <linux/cpu_rmap.h>
49
50#include "bnxt_hsi.h"
51#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050052#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040053#include "bnxt_sriov.h"
54#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050055#include "bnxt_dcb.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040076 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040077 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040078 BCM57311,
79 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050080 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040081 BCM57404,
82 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040083 BCM57402_NPAR,
84 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040085 BCM57412,
86 BCM57414,
87 BCM57416,
88 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040089 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040090 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57417_SFP,
92 BCM57416_SFP,
93 BCM57404_NPAR,
94 BCM57406_NPAR,
95 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040096 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040097 BCM57414_NPAR,
98 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -040099 NETXTREME_E_VF,
100 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400101};
102
103/* indexed by enum above */
104static const struct {
105 char *name;
106} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400107 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
108 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400110 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400111 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
112 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
115 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
119 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400127 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
128 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
130 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400133 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135};
136
137static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400138 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500139 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
141 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400142 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400144 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
145 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
148 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
152 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
153 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
154 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400155 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400156 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
158 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
160 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400162 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400168#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
170 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400175#endif
176 { 0 }
177};
178
179MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
180
181static const u16 bnxt_vf_req_snif[] = {
182 HWRM_FUNC_CFG,
183 HWRM_PORT_PHY_QCFG,
184 HWRM_CFA_L2_FILTER_ALLOC,
185};
186
Michael Chan25be8622016-04-05 14:09:00 -0400187static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500188 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
189 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
190 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
191 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
192 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400193};
194
Michael Chanc0c050c2015-10-22 16:01:17 -0400195static bool bnxt_vf_pciid(enum board_idx idx)
196{
Michael Chanadbc8302016-09-19 03:58:01 -0400197 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400198}
199
200#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
201#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
202#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
203
204#define BNXT_CP_DB_REARM(db, raw_cons) \
205 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
206
207#define BNXT_CP_DB(db, raw_cons) \
208 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
209
210#define BNXT_CP_DB_IRQ_DIS(db) \
211 writel(DB_CP_IRQ_DIS_FLAGS, db)
212
213static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
214{
215 /* Tell compiler to fetch tx indices from memory. */
216 barrier();
217
218 return bp->tx_ring_size -
219 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
220}
221
222static const u16 bnxt_lhint_arr[] = {
223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanb6ab4b02016-01-02 23:44:59 -0500265 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400266 txq = netdev_get_tx_queue(dev, i);
267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500512 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
514 u16 cons = txr->tx_cons;
515 struct pci_dev *pdev = bp->pdev;
516 int i;
517 unsigned int tx_bytes = 0;
518
519 for (i = 0; i < nr_pkts; i++) {
520 struct bnxt_sw_tx_bd *tx_buf;
521 struct sk_buff *skb;
522 int j, last;
523
524 tx_buf = &txr->tx_buf_ring[cons];
525 cons = NEXT_TX(cons);
526 skb = tx_buf->skb;
527 tx_buf->skb = NULL;
528
529 if (tx_buf->is_push) {
530 tx_buf->is_push = 0;
531 goto next_tx_int;
532 }
533
534 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
535 skb_headlen(skb), PCI_DMA_TODEVICE);
536 last = tx_buf->nr_frags;
537
538 for (j = 0; j < last; j++) {
539 cons = NEXT_TX(cons);
540 tx_buf = &txr->tx_buf_ring[cons];
541 dma_unmap_page(
542 &pdev->dev,
543 dma_unmap_addr(tx_buf, mapping),
544 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 PCI_DMA_TODEVICE);
546 }
547
548next_tx_int:
549 cons = NEXT_TX(cons);
550
551 tx_bytes += skb->len;
552 dev_kfree_skb_any(skb);
553 }
554
555 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
556 txr->tx_cons = cons;
557
558 /* Need to make the tx_cons update visible to bnxt_start_xmit()
559 * before checking for netif_tx_queue_stopped(). Without the
560 * memory barrier, there is a small possibility that bnxt_start_xmit()
561 * will miss it and cause the queue to be stopped forever.
562 */
563 smp_mb();
564
565 if (unlikely(netif_tx_queue_stopped(txq)) &&
566 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
567 __netif_tx_lock(txq, smp_processor_id());
568 if (netif_tx_queue_stopped(txq) &&
569 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
570 txr->dev_state != BNXT_DEV_STATE_CLOSING)
571 netif_tx_wake_queue(txq);
572 __netif_tx_unlock(txq);
573 }
574}
575
576static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 u8 *data;
580 struct pci_dev *pdev = bp->pdev;
581
582 data = kmalloc(bp->rx_buf_size, gfp);
583 if (!data)
584 return NULL;
585
586 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
587 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
588
589 if (dma_mapping_error(&pdev->dev, *mapping)) {
590 kfree(data);
591 data = NULL;
592 }
593 return data;
594}
595
596static inline int bnxt_alloc_rx_data(struct bnxt *bp,
597 struct bnxt_rx_ring_info *rxr,
598 u16 prod, gfp_t gfp)
599{
600 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
601 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
602 u8 *data;
603 dma_addr_t mapping;
604
605 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 if (!data)
607 return -ENOMEM;
608
609 rx_buf->data = data;
610 dma_unmap_addr_set(rx_buf, mapping, mapping);
611
612 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
613
614 return 0;
615}
616
617static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
618 u8 *data)
619{
620 u16 prod = rxr->rx_prod;
621 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
622 struct rx_bd *cons_bd, *prod_bd;
623
624 prod_rx_buf = &rxr->rx_buf_ring[prod];
625 cons_rx_buf = &rxr->rx_buf_ring[cons];
626
627 prod_rx_buf->data = data;
628
629 dma_unmap_addr_set(prod_rx_buf, mapping,
630 dma_unmap_addr(cons_rx_buf, mapping));
631
632 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
633 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
634
635 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
636}
637
638static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
639{
640 u16 next, max = rxr->rx_agg_bmap_size;
641
642 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
643 if (next >= max)
644 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
645 return next;
646}
647
648static inline int bnxt_alloc_rx_page(struct bnxt *bp,
649 struct bnxt_rx_ring_info *rxr,
650 u16 prod, gfp_t gfp)
651{
652 struct rx_bd *rxbd =
653 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
654 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
655 struct pci_dev *pdev = bp->pdev;
656 struct page *page;
657 dma_addr_t mapping;
658 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400659 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400660
Michael Chan89d0a062016-04-25 02:30:51 -0400661 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
662 page = rxr->rx_page;
663 if (!page) {
664 page = alloc_page(gfp);
665 if (!page)
666 return -ENOMEM;
667 rxr->rx_page = page;
668 rxr->rx_page_offset = 0;
669 }
670 offset = rxr->rx_page_offset;
671 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
672 if (rxr->rx_page_offset == PAGE_SIZE)
673 rxr->rx_page = NULL;
674 else
675 get_page(page);
676 } else {
677 page = alloc_page(gfp);
678 if (!page)
679 return -ENOMEM;
680 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400681
Michael Chan89d0a062016-04-25 02:30:51 -0400682 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400683 PCI_DMA_FROMDEVICE);
684 if (dma_mapping_error(&pdev->dev, mapping)) {
685 __free_page(page);
686 return -EIO;
687 }
688
689 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
690 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
691
692 __set_bit(sw_prod, rxr->rx_agg_bmap);
693 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
694 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
695
696 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400697 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400698 rx_agg_buf->mapping = mapping;
699 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
700 rxbd->rx_bd_opaque = sw_prod;
701 return 0;
702}
703
704static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
705 u32 agg_bufs)
706{
707 struct bnxt *bp = bnapi->bp;
708 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500709 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400710 u16 prod = rxr->rx_agg_prod;
711 u16 sw_prod = rxr->rx_sw_agg_prod;
712 u32 i;
713
714 for (i = 0; i < agg_bufs; i++) {
715 u16 cons;
716 struct rx_agg_cmp *agg;
717 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
718 struct rx_bd *prod_bd;
719 struct page *page;
720
721 agg = (struct rx_agg_cmp *)
722 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
723 cons = agg->rx_agg_cmp_opaque;
724 __clear_bit(cons, rxr->rx_agg_bmap);
725
726 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
727 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
728
729 __set_bit(sw_prod, rxr->rx_agg_bmap);
730 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
731 cons_rx_buf = &rxr->rx_agg_ring[cons];
732
733 /* It is possible for sw_prod to be equal to cons, so
734 * set cons_rx_buf->page to NULL first.
735 */
736 page = cons_rx_buf->page;
737 cons_rx_buf->page = NULL;
738 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400739 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400740
741 prod_rx_buf->mapping = cons_rx_buf->mapping;
742
743 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
744
745 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
746 prod_bd->rx_bd_opaque = sw_prod;
747
748 prod = NEXT_RX_AGG(prod);
749 sw_prod = NEXT_RX_AGG(sw_prod);
750 cp_cons = NEXT_CMP(cp_cons);
751 }
752 rxr->rx_agg_prod = prod;
753 rxr->rx_sw_agg_prod = sw_prod;
754}
755
756static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
757 struct bnxt_rx_ring_info *rxr, u16 cons,
758 u16 prod, u8 *data, dma_addr_t dma_addr,
759 unsigned int len)
760{
761 int err;
762 struct sk_buff *skb;
763
764 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
765 if (unlikely(err)) {
766 bnxt_reuse_rx_data(rxr, cons, data);
767 return NULL;
768 }
769
770 skb = build_skb(data, 0);
771 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
772 PCI_DMA_FROMDEVICE);
773 if (!skb) {
774 kfree(data);
775 return NULL;
776 }
777
778 skb_reserve(skb, BNXT_RX_OFFSET);
779 skb_put(skb, len);
780 return skb;
781}
782
783static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
784 struct sk_buff *skb, u16 cp_cons,
785 u32 agg_bufs)
786{
787 struct pci_dev *pdev = bp->pdev;
788 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500789 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400790 u16 prod = rxr->rx_agg_prod;
791 u32 i;
792
793 for (i = 0; i < agg_bufs; i++) {
794 u16 cons, frag_len;
795 struct rx_agg_cmp *agg;
796 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
797 struct page *page;
798 dma_addr_t mapping;
799
800 agg = (struct rx_agg_cmp *)
801 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
802 cons = agg->rx_agg_cmp_opaque;
803 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
804 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
805
806 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400807 skb_fill_page_desc(skb, i, cons_rx_buf->page,
808 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400809 __clear_bit(cons, rxr->rx_agg_bmap);
810
811 /* It is possible for bnxt_alloc_rx_page() to allocate
812 * a sw_prod index that equals the cons index, so we
813 * need to clear the cons entry now.
814 */
815 mapping = dma_unmap_addr(cons_rx_buf, mapping);
816 page = cons_rx_buf->page;
817 cons_rx_buf->page = NULL;
818
819 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
820 struct skb_shared_info *shinfo;
821 unsigned int nr_frags;
822
823 shinfo = skb_shinfo(skb);
824 nr_frags = --shinfo->nr_frags;
825 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
826
827 dev_kfree_skb(skb);
828
829 cons_rx_buf->page = page;
830
831 /* Update prod since possibly some pages have been
832 * allocated already.
833 */
834 rxr->rx_agg_prod = prod;
835 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
836 return NULL;
837 }
838
Michael Chan2839f282016-04-25 02:30:50 -0400839 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400840 PCI_DMA_FROMDEVICE);
841
842 skb->data_len += frag_len;
843 skb->len += frag_len;
844 skb->truesize += PAGE_SIZE;
845
846 prod = NEXT_RX_AGG(prod);
847 cp_cons = NEXT_CMP(cp_cons);
848 }
849 rxr->rx_agg_prod = prod;
850 return skb;
851}
852
853static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
854 u8 agg_bufs, u32 *raw_cons)
855{
856 u16 last;
857 struct rx_agg_cmp *agg;
858
859 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
860 last = RING_CMP(*raw_cons);
861 agg = (struct rx_agg_cmp *)
862 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
863 return RX_AGG_CMP_VALID(agg, *raw_cons);
864}
865
866static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
867 unsigned int len,
868 dma_addr_t mapping)
869{
870 struct bnxt *bp = bnapi->bp;
871 struct pci_dev *pdev = bp->pdev;
872 struct sk_buff *skb;
873
874 skb = napi_alloc_skb(&bnapi->napi, len);
875 if (!skb)
876 return NULL;
877
878 dma_sync_single_for_cpu(&pdev->dev, mapping,
879 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
880
881 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
882
883 dma_sync_single_for_device(&pdev->dev, mapping,
884 bp->rx_copy_thresh,
885 PCI_DMA_FROMDEVICE);
886
887 skb_put(skb, len);
888 return skb;
889}
890
Michael Chanfa7e2812016-05-10 19:18:00 -0400891static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
892 u32 *raw_cons, void *cmp)
893{
894 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
895 struct rx_cmp *rxcmp = cmp;
896 u32 tmp_raw_cons = *raw_cons;
897 u8 cmp_type, agg_bufs = 0;
898
899 cmp_type = RX_CMP_TYPE(rxcmp);
900
901 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
902 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
903 RX_CMP_AGG_BUFS) >>
904 RX_CMP_AGG_BUFS_SHIFT;
905 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
906 struct rx_tpa_end_cmp *tpa_end = cmp;
907
908 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
909 RX_TPA_END_CMP_AGG_BUFS) >>
910 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
911 }
912
913 if (agg_bufs) {
914 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
915 return -EBUSY;
916 }
917 *raw_cons = tmp_raw_cons;
918 return 0;
919}
920
921static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
922{
923 if (!rxr->bnapi->in_reset) {
924 rxr->bnapi->in_reset = true;
925 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
926 schedule_work(&bp->sp_task);
927 }
928 rxr->rx_next_cons = 0xffff;
929}
930
Michael Chanc0c050c2015-10-22 16:01:17 -0400931static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
932 struct rx_tpa_start_cmp *tpa_start,
933 struct rx_tpa_start_cmp_ext *tpa_start1)
934{
935 u8 agg_id = TPA_START_AGG_ID(tpa_start);
936 u16 cons, prod;
937 struct bnxt_tpa_info *tpa_info;
938 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
939 struct rx_bd *prod_bd;
940 dma_addr_t mapping;
941
942 cons = tpa_start->rx_tpa_start_cmp_opaque;
943 prod = rxr->rx_prod;
944 cons_rx_buf = &rxr->rx_buf_ring[cons];
945 prod_rx_buf = &rxr->rx_buf_ring[prod];
946 tpa_info = &rxr->rx_tpa[agg_id];
947
Michael Chanfa7e2812016-05-10 19:18:00 -0400948 if (unlikely(cons != rxr->rx_next_cons)) {
949 bnxt_sched_reset(bp, rxr);
950 return;
951 }
952
Michael Chanc0c050c2015-10-22 16:01:17 -0400953 prod_rx_buf->data = tpa_info->data;
954
955 mapping = tpa_info->mapping;
956 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
957
958 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
959
960 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
961
962 tpa_info->data = cons_rx_buf->data;
963 cons_rx_buf->data = NULL;
964 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
965
966 tpa_info->len =
967 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
968 RX_TPA_START_CMP_LEN_SHIFT;
969 if (likely(TPA_START_HASH_VALID(tpa_start))) {
970 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
971
972 tpa_info->hash_type = PKT_HASH_TYPE_L4;
973 tpa_info->gso_type = SKB_GSO_TCPV4;
974 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
975 if (hash_type == 3)
976 tpa_info->gso_type = SKB_GSO_TCPV6;
977 tpa_info->rss_hash =
978 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
979 } else {
980 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
981 tpa_info->gso_type = 0;
982 if (netif_msg_rx_err(bp))
983 netdev_warn(bp->dev, "TPA packet without valid hash\n");
984 }
985 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
986 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400987 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400988
989 rxr->rx_prod = NEXT_RX(prod);
990 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400991 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400992 cons_rx_buf = &rxr->rx_buf_ring[cons];
993
994 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
995 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
996 cons_rx_buf->data = NULL;
997}
998
999static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1000 u16 cp_cons, u32 agg_bufs)
1001{
1002 if (agg_bufs)
1003 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1004}
1005
Michael Chan94758f82016-06-13 02:25:35 -04001006static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1007 int payload_off, int tcp_ts,
1008 struct sk_buff *skb)
1009{
1010#ifdef CONFIG_INET
1011 struct tcphdr *th;
1012 int len, nw_off;
1013 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1014 u32 hdr_info = tpa_info->hdr_info;
1015 bool loopback = false;
1016
1017 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1018 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1019 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1020
1021 /* If the packet is an internal loopback packet, the offsets will
1022 * have an extra 4 bytes.
1023 */
1024 if (inner_mac_off == 4) {
1025 loopback = true;
1026 } else if (inner_mac_off > 4) {
1027 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1028 ETH_HLEN - 2));
1029
1030 /* We only support inner iPv4/ipv6. If we don't see the
1031 * correct protocol ID, it must be a loopback packet where
1032 * the offsets are off by 4.
1033 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001034 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001035 loopback = true;
1036 }
1037 if (loopback) {
1038 /* internal loopback packet, subtract all offsets by 4 */
1039 inner_ip_off -= 4;
1040 inner_mac_off -= 4;
1041 outer_ip_off -= 4;
1042 }
1043
1044 nw_off = inner_ip_off - ETH_HLEN;
1045 skb_set_network_header(skb, nw_off);
1046 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1047 struct ipv6hdr *iph = ipv6_hdr(skb);
1048
1049 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1050 len = skb->len - skb_transport_offset(skb);
1051 th = tcp_hdr(skb);
1052 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1053 } else {
1054 struct iphdr *iph = ip_hdr(skb);
1055
1056 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1057 len = skb->len - skb_transport_offset(skb);
1058 th = tcp_hdr(skb);
1059 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1060 }
1061
1062 if (inner_mac_off) { /* tunnel */
1063 struct udphdr *uh = NULL;
1064 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1065 ETH_HLEN - 2));
1066
1067 if (proto == htons(ETH_P_IP)) {
1068 struct iphdr *iph = (struct iphdr *)skb->data;
1069
1070 if (iph->protocol == IPPROTO_UDP)
1071 uh = (struct udphdr *)(iph + 1);
1072 } else {
1073 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1074
1075 if (iph->nexthdr == IPPROTO_UDP)
1076 uh = (struct udphdr *)(iph + 1);
1077 }
1078 if (uh) {
1079 if (uh->check)
1080 skb_shinfo(skb)->gso_type |=
1081 SKB_GSO_UDP_TUNNEL_CSUM;
1082 else
1083 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1084 }
1085 }
1086#endif
1087 return skb;
1088}
1089
Michael Chanc0c050c2015-10-22 16:01:17 -04001090#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1091#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1092
Michael Chan309369c2016-06-13 02:25:34 -04001093static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1094 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001095 struct sk_buff *skb)
1096{
Michael Chand1611c32015-10-25 22:27:57 -04001097#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001098 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001099 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001100
Michael Chan309369c2016-06-13 02:25:34 -04001101 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001102 tcp_opt_len = 12;
1103
Michael Chanc0c050c2015-10-22 16:01:17 -04001104 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1105 struct iphdr *iph;
1106
1107 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1108 ETH_HLEN;
1109 skb_set_network_header(skb, nw_off);
1110 iph = ip_hdr(skb);
1111 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1112 len = skb->len - skb_transport_offset(skb);
1113 th = tcp_hdr(skb);
1114 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1115 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1116 struct ipv6hdr *iph;
1117
1118 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1119 ETH_HLEN;
1120 skb_set_network_header(skb, nw_off);
1121 iph = ipv6_hdr(skb);
1122 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1123 len = skb->len - skb_transport_offset(skb);
1124 th = tcp_hdr(skb);
1125 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1126 } else {
1127 dev_kfree_skb_any(skb);
1128 return NULL;
1129 }
1130 tcp_gro_complete(skb);
1131
1132 if (nw_off) { /* tunnel */
1133 struct udphdr *uh = NULL;
1134
1135 if (skb->protocol == htons(ETH_P_IP)) {
1136 struct iphdr *iph = (struct iphdr *)skb->data;
1137
1138 if (iph->protocol == IPPROTO_UDP)
1139 uh = (struct udphdr *)(iph + 1);
1140 } else {
1141 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1142
1143 if (iph->nexthdr == IPPROTO_UDP)
1144 uh = (struct udphdr *)(iph + 1);
1145 }
1146 if (uh) {
1147 if (uh->check)
1148 skb_shinfo(skb)->gso_type |=
1149 SKB_GSO_UDP_TUNNEL_CSUM;
1150 else
1151 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1152 }
1153 }
1154#endif
1155 return skb;
1156}
1157
Michael Chan309369c2016-06-13 02:25:34 -04001158static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1159 struct bnxt_tpa_info *tpa_info,
1160 struct rx_tpa_end_cmp *tpa_end,
1161 struct rx_tpa_end_cmp_ext *tpa_end1,
1162 struct sk_buff *skb)
1163{
1164#ifdef CONFIG_INET
1165 int payload_off;
1166 u16 segs;
1167
1168 segs = TPA_END_TPA_SEGS(tpa_end);
1169 if (segs == 1)
1170 return skb;
1171
1172 NAPI_GRO_CB(skb)->count = segs;
1173 skb_shinfo(skb)->gso_size =
1174 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1175 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1176 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1177 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1178 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1179 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1180#endif
1181 return skb;
1182}
1183
Michael Chanc0c050c2015-10-22 16:01:17 -04001184static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1185 struct bnxt_napi *bnapi,
1186 u32 *raw_cons,
1187 struct rx_tpa_end_cmp *tpa_end,
1188 struct rx_tpa_end_cmp_ext *tpa_end1,
1189 bool *agg_event)
1190{
1191 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001192 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001193 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1194 u8 *data, agg_bufs;
1195 u16 cp_cons = RING_CMP(*raw_cons);
1196 unsigned int len;
1197 struct bnxt_tpa_info *tpa_info;
1198 dma_addr_t mapping;
1199 struct sk_buff *skb;
1200
Michael Chanfa7e2812016-05-10 19:18:00 -04001201 if (unlikely(bnapi->in_reset)) {
1202 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1203
1204 if (rc < 0)
1205 return ERR_PTR(-EBUSY);
1206 return NULL;
1207 }
1208
Michael Chanc0c050c2015-10-22 16:01:17 -04001209 tpa_info = &rxr->rx_tpa[agg_id];
1210 data = tpa_info->data;
1211 prefetch(data);
1212 len = tpa_info->len;
1213 mapping = tpa_info->mapping;
1214
1215 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1216 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1217
1218 if (agg_bufs) {
1219 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1220 return ERR_PTR(-EBUSY);
1221
1222 *agg_event = true;
1223 cp_cons = NEXT_CMP(cp_cons);
1224 }
1225
1226 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1227 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1228 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1229 agg_bufs, (int)MAX_SKB_FRAGS);
1230 return NULL;
1231 }
1232
1233 if (len <= bp->rx_copy_thresh) {
1234 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1235 if (!skb) {
1236 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1237 return NULL;
1238 }
1239 } else {
1240 u8 *new_data;
1241 dma_addr_t new_mapping;
1242
1243 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1244 if (!new_data) {
1245 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1246 return NULL;
1247 }
1248
1249 tpa_info->data = new_data;
1250 tpa_info->mapping = new_mapping;
1251
1252 skb = build_skb(data, 0);
1253 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1254 PCI_DMA_FROMDEVICE);
1255
1256 if (!skb) {
1257 kfree(data);
1258 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1259 return NULL;
1260 }
1261 skb_reserve(skb, BNXT_RX_OFFSET);
1262 skb_put(skb, len);
1263 }
1264
1265 if (agg_bufs) {
1266 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1267 if (!skb) {
1268 /* Page reuse already handled by bnxt_rx_pages(). */
1269 return NULL;
1270 }
1271 }
1272 skb->protocol = eth_type_trans(skb, bp->dev);
1273
1274 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1275 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1276
Michael Chan8852ddb2016-06-06 02:37:16 -04001277 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1278 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001279 u16 vlan_proto = tpa_info->metadata >>
1280 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001281 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001282
Michael Chan8852ddb2016-06-06 02:37:16 -04001283 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001284 }
1285
1286 skb_checksum_none_assert(skb);
1287 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1288 skb->ip_summed = CHECKSUM_UNNECESSARY;
1289 skb->csum_level =
1290 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1291 }
1292
1293 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001294 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001295
1296 return skb;
1297}
1298
1299/* returns the following:
1300 * 1 - 1 packet successfully received
1301 * 0 - successful TPA_START, packet not completed yet
1302 * -EBUSY - completion ring does not have all the agg buffers yet
1303 * -ENOMEM - packet aborted due to out of memory
1304 * -EIO - packet aborted due to hw error indicated in BD
1305 */
1306static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1307 bool *agg_event)
1308{
1309 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001310 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001311 struct net_device *dev = bp->dev;
1312 struct rx_cmp *rxcmp;
1313 struct rx_cmp_ext *rxcmp1;
1314 u32 tmp_raw_cons = *raw_cons;
1315 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1316 struct bnxt_sw_rx_bd *rx_buf;
1317 unsigned int len;
1318 u8 *data, agg_bufs, cmp_type;
1319 dma_addr_t dma_addr;
1320 struct sk_buff *skb;
1321 int rc = 0;
1322
1323 rxcmp = (struct rx_cmp *)
1324 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1325
1326 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1327 cp_cons = RING_CMP(tmp_raw_cons);
1328 rxcmp1 = (struct rx_cmp_ext *)
1329 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1330
1331 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1332 return -EBUSY;
1333
1334 cmp_type = RX_CMP_TYPE(rxcmp);
1335
1336 prod = rxr->rx_prod;
1337
1338 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1339 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1340 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1341
1342 goto next_rx_no_prod;
1343
1344 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1345 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1346 (struct rx_tpa_end_cmp *)rxcmp,
1347 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1348 agg_event);
1349
1350 if (unlikely(IS_ERR(skb)))
1351 return -EBUSY;
1352
1353 rc = -ENOMEM;
1354 if (likely(skb)) {
1355 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001356 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001357 rc = 1;
1358 }
1359 goto next_rx_no_prod;
1360 }
1361
1362 cons = rxcmp->rx_cmp_opaque;
1363 rx_buf = &rxr->rx_buf_ring[cons];
1364 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001365 if (unlikely(cons != rxr->rx_next_cons)) {
1366 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1367
1368 bnxt_sched_reset(bp, rxr);
1369 return rc1;
1370 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001371 prefetch(data);
1372
1373 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1374 RX_CMP_AGG_BUFS_SHIFT;
1375
1376 if (agg_bufs) {
1377 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1378 return -EBUSY;
1379
1380 cp_cons = NEXT_CMP(cp_cons);
1381 *agg_event = true;
1382 }
1383
1384 rx_buf->data = NULL;
1385 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1386 bnxt_reuse_rx_data(rxr, cons, data);
1387 if (agg_bufs)
1388 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1389
1390 rc = -EIO;
1391 goto next_rx;
1392 }
1393
1394 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1395 dma_addr = dma_unmap_addr(rx_buf, mapping);
1396
1397 if (len <= bp->rx_copy_thresh) {
1398 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1399 bnxt_reuse_rx_data(rxr, cons, data);
1400 if (!skb) {
1401 rc = -ENOMEM;
1402 goto next_rx;
1403 }
1404 } else {
1405 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1406 if (!skb) {
1407 rc = -ENOMEM;
1408 goto next_rx;
1409 }
1410 }
1411
1412 if (agg_bufs) {
1413 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1414 if (!skb) {
1415 rc = -ENOMEM;
1416 goto next_rx;
1417 }
1418 }
1419
1420 if (RX_CMP_HASH_VALID(rxcmp)) {
1421 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1422 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1423
1424 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1425 if (hash_type != 1 && hash_type != 3)
1426 type = PKT_HASH_TYPE_L3;
1427 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1428 }
1429
1430 skb->protocol = eth_type_trans(skb, dev);
1431
Michael Chan8852ddb2016-06-06 02:37:16 -04001432 if ((rxcmp1->rx_cmp_flags2 &
1433 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1434 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001435 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001436 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001437 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1438
Michael Chan8852ddb2016-06-06 02:37:16 -04001439 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001440 }
1441
1442 skb_checksum_none_assert(skb);
1443 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1444 if (dev->features & NETIF_F_RXCSUM) {
1445 skb->ip_summed = CHECKSUM_UNNECESSARY;
1446 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1447 }
1448 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001449 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1450 if (dev->features & NETIF_F_RXCSUM)
1451 cpr->rx_l4_csum_errors++;
1452 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001453 }
1454
1455 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001456 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001457 rc = 1;
1458
1459next_rx:
1460 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001461 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001462
1463next_rx_no_prod:
1464 *raw_cons = tmp_raw_cons;
1465
1466 return rc;
1467}
1468
Michael Chan4bb13ab2016-04-05 14:09:01 -04001469#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001470 ((data) & \
1471 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001472
Michael Chanc0c050c2015-10-22 16:01:17 -04001473static int bnxt_async_event_process(struct bnxt *bp,
1474 struct hwrm_async_event_cmpl *cmpl)
1475{
1476 u16 event_id = le16_to_cpu(cmpl->event_id);
1477
1478 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1479 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001480 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001481 u32 data1 = le32_to_cpu(cmpl->event_data1);
1482 struct bnxt_link_info *link_info = &bp->link_info;
1483
1484 if (BNXT_VF(bp))
1485 goto async_event_process_exit;
1486 if (data1 & 0x20000) {
1487 u16 fw_speed = link_info->force_link_speed;
1488 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1489
1490 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1491 speed);
1492 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001493 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001494 /* fall thru */
1495 }
Michael Chan87c374d2016-12-02 21:17:16 -05001496 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001497 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001498 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001499 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001500 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001501 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001502 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001503 u32 data1 = le32_to_cpu(cmpl->event_data1);
1504 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1505
1506 if (BNXT_VF(bp))
1507 break;
1508
1509 if (bp->pf.port_id != port_id)
1510 break;
1511
Michael Chan4bb13ab2016-04-05 14:09:01 -04001512 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1513 break;
1514 }
Michael Chan87c374d2016-12-02 21:17:16 -05001515 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001516 if (BNXT_PF(bp))
1517 goto async_event_process_exit;
1518 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1519 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001520 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001521 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001522 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001523 schedule_work(&bp->sp_task);
1524async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001525 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001526 return 0;
1527}
1528
1529static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1530{
1531 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1532 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1533 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1534 (struct hwrm_fwd_req_cmpl *)txcmp;
1535
1536 switch (cmpl_type) {
1537 case CMPL_BASE_TYPE_HWRM_DONE:
1538 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1539 if (seq_id == bp->hwrm_intr_seq_id)
1540 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1541 else
1542 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1543 break;
1544
1545 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1546 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1547
1548 if ((vf_id < bp->pf.first_vf_id) ||
1549 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1550 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1551 vf_id);
1552 return -EINVAL;
1553 }
1554
1555 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1556 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1557 schedule_work(&bp->sp_task);
1558 break;
1559
1560 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1561 bnxt_async_event_process(bp,
1562 (struct hwrm_async_event_cmpl *)txcmp);
1563
1564 default:
1565 break;
1566 }
1567
1568 return 0;
1569}
1570
1571static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1572{
1573 struct bnxt_napi *bnapi = dev_instance;
1574 struct bnxt *bp = bnapi->bp;
1575 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1576 u32 cons = RING_CMP(cpr->cp_raw_cons);
1577
1578 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1579 napi_schedule(&bnapi->napi);
1580 return IRQ_HANDLED;
1581}
1582
1583static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1584{
1585 u32 raw_cons = cpr->cp_raw_cons;
1586 u16 cons = RING_CMP(raw_cons);
1587 struct tx_cmp *txcmp;
1588
1589 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1590
1591 return TX_CMP_VALID(txcmp, raw_cons);
1592}
1593
Michael Chanc0c050c2015-10-22 16:01:17 -04001594static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1595{
1596 struct bnxt_napi *bnapi = dev_instance;
1597 struct bnxt *bp = bnapi->bp;
1598 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1599 u32 cons = RING_CMP(cpr->cp_raw_cons);
1600 u32 int_status;
1601
1602 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1603
1604 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001605 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001606 /* return if erroneous interrupt */
1607 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1608 return IRQ_NONE;
1609 }
1610
1611 /* disable ring IRQ */
1612 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1613
1614 /* Return here if interrupt is shared and is disabled. */
1615 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1616 return IRQ_HANDLED;
1617
1618 napi_schedule(&bnapi->napi);
1619 return IRQ_HANDLED;
1620}
1621
1622static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1623{
1624 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1625 u32 raw_cons = cpr->cp_raw_cons;
1626 u32 cons;
1627 int tx_pkts = 0;
1628 int rx_pkts = 0;
1629 bool rx_event = false;
1630 bool agg_event = false;
1631 struct tx_cmp *txcmp;
1632
1633 while (1) {
1634 int rc;
1635
1636 cons = RING_CMP(raw_cons);
1637 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1638
1639 if (!TX_CMP_VALID(txcmp, raw_cons))
1640 break;
1641
Michael Chan67a95e22016-05-04 16:56:43 -04001642 /* The valid test of the entry must be done first before
1643 * reading any further.
1644 */
Michael Chanb67daab2016-05-15 03:04:51 -04001645 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001646 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1647 tx_pkts++;
1648 /* return full budget so NAPI will complete. */
1649 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1650 rx_pkts = budget;
1651 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1652 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1653 if (likely(rc >= 0))
1654 rx_pkts += rc;
1655 else if (rc == -EBUSY) /* partial completion */
1656 break;
1657 rx_event = true;
1658 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1659 CMPL_BASE_TYPE_HWRM_DONE) ||
1660 (TX_CMP_TYPE(txcmp) ==
1661 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1662 (TX_CMP_TYPE(txcmp) ==
1663 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1664 bnxt_hwrm_handler(bp, txcmp);
1665 }
1666 raw_cons = NEXT_RAW_CMP(raw_cons);
1667
1668 if (rx_pkts == budget)
1669 break;
1670 }
1671
1672 cpr->cp_raw_cons = raw_cons;
1673 /* ACK completion ring before freeing tx ring and producing new
1674 * buffers in rx/agg rings to prevent overflowing the completion
1675 * ring.
1676 */
1677 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1678
1679 if (tx_pkts)
1680 bnxt_tx_int(bp, bnapi, tx_pkts);
1681
1682 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001683 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001684
1685 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1686 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1687 if (agg_event) {
1688 writel(DB_KEY_RX | rxr->rx_agg_prod,
1689 rxr->rx_agg_doorbell);
1690 writel(DB_KEY_RX | rxr->rx_agg_prod,
1691 rxr->rx_agg_doorbell);
1692 }
1693 }
1694 return rx_pkts;
1695}
1696
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001697static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1698{
1699 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1700 struct bnxt *bp = bnapi->bp;
1701 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1702 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1703 struct tx_cmp *txcmp;
1704 struct rx_cmp_ext *rxcmp1;
1705 u32 cp_cons, tmp_raw_cons;
1706 u32 raw_cons = cpr->cp_raw_cons;
1707 u32 rx_pkts = 0;
1708 bool agg_event = false;
1709
1710 while (1) {
1711 int rc;
1712
1713 cp_cons = RING_CMP(raw_cons);
1714 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1715
1716 if (!TX_CMP_VALID(txcmp, raw_cons))
1717 break;
1718
1719 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1720 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1721 cp_cons = RING_CMP(tmp_raw_cons);
1722 rxcmp1 = (struct rx_cmp_ext *)
1723 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1724
1725 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1726 break;
1727
1728 /* force an error to recycle the buffer */
1729 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1730 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1731
1732 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1733 if (likely(rc == -EIO))
1734 rx_pkts++;
1735 else if (rc == -EBUSY) /* partial completion */
1736 break;
1737 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1738 CMPL_BASE_TYPE_HWRM_DONE)) {
1739 bnxt_hwrm_handler(bp, txcmp);
1740 } else {
1741 netdev_err(bp->dev,
1742 "Invalid completion received on special ring\n");
1743 }
1744 raw_cons = NEXT_RAW_CMP(raw_cons);
1745
1746 if (rx_pkts == budget)
1747 break;
1748 }
1749
1750 cpr->cp_raw_cons = raw_cons;
1751 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1752 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1753 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1754
1755 if (agg_event) {
1756 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1757 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1758 }
1759
1760 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1761 napi_complete(napi);
1762 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1763 }
1764 return rx_pkts;
1765}
1766
Michael Chanc0c050c2015-10-22 16:01:17 -04001767static int bnxt_poll(struct napi_struct *napi, int budget)
1768{
1769 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1770 struct bnxt *bp = bnapi->bp;
1771 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1772 int work_done = 0;
1773
Michael Chanc0c050c2015-10-22 16:01:17 -04001774 while (1) {
1775 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1776
1777 if (work_done >= budget)
1778 break;
1779
1780 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001781 if (napi_complete_done(napi, work_done))
1782 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1783 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001784 break;
1785 }
1786 }
1787 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001788 return work_done;
1789}
1790
Michael Chanc0c050c2015-10-22 16:01:17 -04001791static void bnxt_free_tx_skbs(struct bnxt *bp)
1792{
1793 int i, max_idx;
1794 struct pci_dev *pdev = bp->pdev;
1795
Michael Chanb6ab4b02016-01-02 23:44:59 -05001796 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001797 return;
1798
1799 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1800 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001801 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001802 int j;
1803
Michael Chanc0c050c2015-10-22 16:01:17 -04001804 for (j = 0; j < max_idx;) {
1805 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1806 struct sk_buff *skb = tx_buf->skb;
1807 int k, last;
1808
1809 if (!skb) {
1810 j++;
1811 continue;
1812 }
1813
1814 tx_buf->skb = NULL;
1815
1816 if (tx_buf->is_push) {
1817 dev_kfree_skb(skb);
1818 j += 2;
1819 continue;
1820 }
1821
1822 dma_unmap_single(&pdev->dev,
1823 dma_unmap_addr(tx_buf, mapping),
1824 skb_headlen(skb),
1825 PCI_DMA_TODEVICE);
1826
1827 last = tx_buf->nr_frags;
1828 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001829 for (k = 0; k < last; k++, j++) {
1830 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001831 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1832
Michael Chand612a572016-01-28 03:11:22 -05001833 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001834 dma_unmap_page(
1835 &pdev->dev,
1836 dma_unmap_addr(tx_buf, mapping),
1837 skb_frag_size(frag), PCI_DMA_TODEVICE);
1838 }
1839 dev_kfree_skb(skb);
1840 }
1841 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1842 }
1843}
1844
1845static void bnxt_free_rx_skbs(struct bnxt *bp)
1846{
1847 int i, max_idx, max_agg_idx;
1848 struct pci_dev *pdev = bp->pdev;
1849
Michael Chanb6ab4b02016-01-02 23:44:59 -05001850 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001851 return;
1852
1853 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1854 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1855 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001856 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001857 int j;
1858
Michael Chanc0c050c2015-10-22 16:01:17 -04001859 if (rxr->rx_tpa) {
1860 for (j = 0; j < MAX_TPA; j++) {
1861 struct bnxt_tpa_info *tpa_info =
1862 &rxr->rx_tpa[j];
1863 u8 *data = tpa_info->data;
1864
1865 if (!data)
1866 continue;
1867
1868 dma_unmap_single(
1869 &pdev->dev,
1870 dma_unmap_addr(tpa_info, mapping),
1871 bp->rx_buf_use_size,
1872 PCI_DMA_FROMDEVICE);
1873
1874 tpa_info->data = NULL;
1875
1876 kfree(data);
1877 }
1878 }
1879
1880 for (j = 0; j < max_idx; j++) {
1881 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1882 u8 *data = rx_buf->data;
1883
1884 if (!data)
1885 continue;
1886
1887 dma_unmap_single(&pdev->dev,
1888 dma_unmap_addr(rx_buf, mapping),
1889 bp->rx_buf_use_size,
1890 PCI_DMA_FROMDEVICE);
1891
1892 rx_buf->data = NULL;
1893
1894 kfree(data);
1895 }
1896
1897 for (j = 0; j < max_agg_idx; j++) {
1898 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1899 &rxr->rx_agg_ring[j];
1900 struct page *page = rx_agg_buf->page;
1901
1902 if (!page)
1903 continue;
1904
1905 dma_unmap_page(&pdev->dev,
1906 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001907 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001908
1909 rx_agg_buf->page = NULL;
1910 __clear_bit(j, rxr->rx_agg_bmap);
1911
1912 __free_page(page);
1913 }
Michael Chan89d0a062016-04-25 02:30:51 -04001914 if (rxr->rx_page) {
1915 __free_page(rxr->rx_page);
1916 rxr->rx_page = NULL;
1917 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001918 }
1919}
1920
1921static void bnxt_free_skbs(struct bnxt *bp)
1922{
1923 bnxt_free_tx_skbs(bp);
1924 bnxt_free_rx_skbs(bp);
1925}
1926
1927static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1928{
1929 struct pci_dev *pdev = bp->pdev;
1930 int i;
1931
1932 for (i = 0; i < ring->nr_pages; i++) {
1933 if (!ring->pg_arr[i])
1934 continue;
1935
1936 dma_free_coherent(&pdev->dev, ring->page_size,
1937 ring->pg_arr[i], ring->dma_arr[i]);
1938
1939 ring->pg_arr[i] = NULL;
1940 }
1941 if (ring->pg_tbl) {
1942 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1943 ring->pg_tbl, ring->pg_tbl_map);
1944 ring->pg_tbl = NULL;
1945 }
1946 if (ring->vmem_size && *ring->vmem) {
1947 vfree(*ring->vmem);
1948 *ring->vmem = NULL;
1949 }
1950}
1951
1952static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1953{
1954 int i;
1955 struct pci_dev *pdev = bp->pdev;
1956
1957 if (ring->nr_pages > 1) {
1958 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1959 ring->nr_pages * 8,
1960 &ring->pg_tbl_map,
1961 GFP_KERNEL);
1962 if (!ring->pg_tbl)
1963 return -ENOMEM;
1964 }
1965
1966 for (i = 0; i < ring->nr_pages; i++) {
1967 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1968 ring->page_size,
1969 &ring->dma_arr[i],
1970 GFP_KERNEL);
1971 if (!ring->pg_arr[i])
1972 return -ENOMEM;
1973
1974 if (ring->nr_pages > 1)
1975 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1976 }
1977
1978 if (ring->vmem_size) {
1979 *ring->vmem = vzalloc(ring->vmem_size);
1980 if (!(*ring->vmem))
1981 return -ENOMEM;
1982 }
1983 return 0;
1984}
1985
1986static void bnxt_free_rx_rings(struct bnxt *bp)
1987{
1988 int i;
1989
Michael Chanb6ab4b02016-01-02 23:44:59 -05001990 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001991 return;
1992
1993 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001994 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001995 struct bnxt_ring_struct *ring;
1996
Michael Chanc0c050c2015-10-22 16:01:17 -04001997 kfree(rxr->rx_tpa);
1998 rxr->rx_tpa = NULL;
1999
2000 kfree(rxr->rx_agg_bmap);
2001 rxr->rx_agg_bmap = NULL;
2002
2003 ring = &rxr->rx_ring_struct;
2004 bnxt_free_ring(bp, ring);
2005
2006 ring = &rxr->rx_agg_ring_struct;
2007 bnxt_free_ring(bp, ring);
2008 }
2009}
2010
2011static int bnxt_alloc_rx_rings(struct bnxt *bp)
2012{
2013 int i, rc, agg_rings = 0, tpa_rings = 0;
2014
Michael Chanb6ab4b02016-01-02 23:44:59 -05002015 if (!bp->rx_ring)
2016 return -ENOMEM;
2017
Michael Chanc0c050c2015-10-22 16:01:17 -04002018 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2019 agg_rings = 1;
2020
2021 if (bp->flags & BNXT_FLAG_TPA)
2022 tpa_rings = 1;
2023
2024 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002025 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002026 struct bnxt_ring_struct *ring;
2027
Michael Chanc0c050c2015-10-22 16:01:17 -04002028 ring = &rxr->rx_ring_struct;
2029
2030 rc = bnxt_alloc_ring(bp, ring);
2031 if (rc)
2032 return rc;
2033
2034 if (agg_rings) {
2035 u16 mem_size;
2036
2037 ring = &rxr->rx_agg_ring_struct;
2038 rc = bnxt_alloc_ring(bp, ring);
2039 if (rc)
2040 return rc;
2041
2042 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2043 mem_size = rxr->rx_agg_bmap_size / 8;
2044 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2045 if (!rxr->rx_agg_bmap)
2046 return -ENOMEM;
2047
2048 if (tpa_rings) {
2049 rxr->rx_tpa = kcalloc(MAX_TPA,
2050 sizeof(struct bnxt_tpa_info),
2051 GFP_KERNEL);
2052 if (!rxr->rx_tpa)
2053 return -ENOMEM;
2054 }
2055 }
2056 }
2057 return 0;
2058}
2059
2060static void bnxt_free_tx_rings(struct bnxt *bp)
2061{
2062 int i;
2063 struct pci_dev *pdev = bp->pdev;
2064
Michael Chanb6ab4b02016-01-02 23:44:59 -05002065 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002066 return;
2067
2068 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002069 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002070 struct bnxt_ring_struct *ring;
2071
Michael Chanc0c050c2015-10-22 16:01:17 -04002072 if (txr->tx_push) {
2073 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2074 txr->tx_push, txr->tx_push_mapping);
2075 txr->tx_push = NULL;
2076 }
2077
2078 ring = &txr->tx_ring_struct;
2079
2080 bnxt_free_ring(bp, ring);
2081 }
2082}
2083
2084static int bnxt_alloc_tx_rings(struct bnxt *bp)
2085{
2086 int i, j, rc;
2087 struct pci_dev *pdev = bp->pdev;
2088
2089 bp->tx_push_size = 0;
2090 if (bp->tx_push_thresh) {
2091 int push_size;
2092
2093 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2094 bp->tx_push_thresh);
2095
Michael Chan4419dbe2016-02-10 17:33:49 -05002096 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002097 push_size = 0;
2098 bp->tx_push_thresh = 0;
2099 }
2100
2101 bp->tx_push_size = push_size;
2102 }
2103
2104 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002105 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002106 struct bnxt_ring_struct *ring;
2107
Michael Chanc0c050c2015-10-22 16:01:17 -04002108 ring = &txr->tx_ring_struct;
2109
2110 rc = bnxt_alloc_ring(bp, ring);
2111 if (rc)
2112 return rc;
2113
2114 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002115 dma_addr_t mapping;
2116
2117 /* One pre-allocated DMA buffer to backup
2118 * TX push operation
2119 */
2120 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2121 bp->tx_push_size,
2122 &txr->tx_push_mapping,
2123 GFP_KERNEL);
2124
2125 if (!txr->tx_push)
2126 return -ENOMEM;
2127
Michael Chanc0c050c2015-10-22 16:01:17 -04002128 mapping = txr->tx_push_mapping +
2129 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002130 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002131
Michael Chan4419dbe2016-02-10 17:33:49 -05002132 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002133 }
2134 ring->queue_id = bp->q_info[j].queue_id;
2135 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2136 j++;
2137 }
2138 return 0;
2139}
2140
2141static void bnxt_free_cp_rings(struct bnxt *bp)
2142{
2143 int i;
2144
2145 if (!bp->bnapi)
2146 return;
2147
2148 for (i = 0; i < bp->cp_nr_rings; i++) {
2149 struct bnxt_napi *bnapi = bp->bnapi[i];
2150 struct bnxt_cp_ring_info *cpr;
2151 struct bnxt_ring_struct *ring;
2152
2153 if (!bnapi)
2154 continue;
2155
2156 cpr = &bnapi->cp_ring;
2157 ring = &cpr->cp_ring_struct;
2158
2159 bnxt_free_ring(bp, ring);
2160 }
2161}
2162
2163static int bnxt_alloc_cp_rings(struct bnxt *bp)
2164{
2165 int i, rc;
2166
2167 for (i = 0; i < bp->cp_nr_rings; i++) {
2168 struct bnxt_napi *bnapi = bp->bnapi[i];
2169 struct bnxt_cp_ring_info *cpr;
2170 struct bnxt_ring_struct *ring;
2171
2172 if (!bnapi)
2173 continue;
2174
2175 cpr = &bnapi->cp_ring;
2176 ring = &cpr->cp_ring_struct;
2177
2178 rc = bnxt_alloc_ring(bp, ring);
2179 if (rc)
2180 return rc;
2181 }
2182 return 0;
2183}
2184
2185static void bnxt_init_ring_struct(struct bnxt *bp)
2186{
2187 int i;
2188
2189 for (i = 0; i < bp->cp_nr_rings; i++) {
2190 struct bnxt_napi *bnapi = bp->bnapi[i];
2191 struct bnxt_cp_ring_info *cpr;
2192 struct bnxt_rx_ring_info *rxr;
2193 struct bnxt_tx_ring_info *txr;
2194 struct bnxt_ring_struct *ring;
2195
2196 if (!bnapi)
2197 continue;
2198
2199 cpr = &bnapi->cp_ring;
2200 ring = &cpr->cp_ring_struct;
2201 ring->nr_pages = bp->cp_nr_pages;
2202 ring->page_size = HW_CMPD_RING_SIZE;
2203 ring->pg_arr = (void **)cpr->cp_desc_ring;
2204 ring->dma_arr = cpr->cp_desc_mapping;
2205 ring->vmem_size = 0;
2206
Michael Chanb6ab4b02016-01-02 23:44:59 -05002207 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002208 if (!rxr)
2209 goto skip_rx;
2210
Michael Chanc0c050c2015-10-22 16:01:17 -04002211 ring = &rxr->rx_ring_struct;
2212 ring->nr_pages = bp->rx_nr_pages;
2213 ring->page_size = HW_RXBD_RING_SIZE;
2214 ring->pg_arr = (void **)rxr->rx_desc_ring;
2215 ring->dma_arr = rxr->rx_desc_mapping;
2216 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2217 ring->vmem = (void **)&rxr->rx_buf_ring;
2218
2219 ring = &rxr->rx_agg_ring_struct;
2220 ring->nr_pages = bp->rx_agg_nr_pages;
2221 ring->page_size = HW_RXBD_RING_SIZE;
2222 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2223 ring->dma_arr = rxr->rx_agg_desc_mapping;
2224 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2225 ring->vmem = (void **)&rxr->rx_agg_ring;
2226
Michael Chan3b2b7d92016-01-02 23:45:00 -05002227skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002228 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002229 if (!txr)
2230 continue;
2231
Michael Chanc0c050c2015-10-22 16:01:17 -04002232 ring = &txr->tx_ring_struct;
2233 ring->nr_pages = bp->tx_nr_pages;
2234 ring->page_size = HW_RXBD_RING_SIZE;
2235 ring->pg_arr = (void **)txr->tx_desc_ring;
2236 ring->dma_arr = txr->tx_desc_mapping;
2237 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2238 ring->vmem = (void **)&txr->tx_buf_ring;
2239 }
2240}
2241
2242static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2243{
2244 int i;
2245 u32 prod;
2246 struct rx_bd **rx_buf_ring;
2247
2248 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2249 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2250 int j;
2251 struct rx_bd *rxbd;
2252
2253 rxbd = rx_buf_ring[i];
2254 if (!rxbd)
2255 continue;
2256
2257 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2258 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2259 rxbd->rx_bd_opaque = prod;
2260 }
2261 }
2262}
2263
2264static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2265{
2266 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002267 struct bnxt_rx_ring_info *rxr;
2268 struct bnxt_ring_struct *ring;
2269 u32 prod, type;
2270 int i;
2271
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2273 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2274
2275 if (NET_IP_ALIGN == 2)
2276 type |= RX_BD_FLAGS_SOP;
2277
Michael Chanb6ab4b02016-01-02 23:44:59 -05002278 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002279 ring = &rxr->rx_ring_struct;
2280 bnxt_init_rxbd_pages(ring, type);
2281
2282 prod = rxr->rx_prod;
2283 for (i = 0; i < bp->rx_ring_size; i++) {
2284 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2285 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2286 ring_nr, i, bp->rx_ring_size);
2287 break;
2288 }
2289 prod = NEXT_RX(prod);
2290 }
2291 rxr->rx_prod = prod;
2292 ring->fw_ring_id = INVALID_HW_RING_ID;
2293
Michael Chanedd0c2c2015-12-27 18:19:19 -05002294 ring = &rxr->rx_agg_ring_struct;
2295 ring->fw_ring_id = INVALID_HW_RING_ID;
2296
Michael Chanc0c050c2015-10-22 16:01:17 -04002297 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2298 return 0;
2299
Michael Chan2839f282016-04-25 02:30:50 -04002300 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002301 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2302
2303 bnxt_init_rxbd_pages(ring, type);
2304
2305 prod = rxr->rx_agg_prod;
2306 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2307 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2308 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2309 ring_nr, i, bp->rx_ring_size);
2310 break;
2311 }
2312 prod = NEXT_RX_AGG(prod);
2313 }
2314 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002315
2316 if (bp->flags & BNXT_FLAG_TPA) {
2317 if (rxr->rx_tpa) {
2318 u8 *data;
2319 dma_addr_t mapping;
2320
2321 for (i = 0; i < MAX_TPA; i++) {
2322 data = __bnxt_alloc_rx_data(bp, &mapping,
2323 GFP_KERNEL);
2324 if (!data)
2325 return -ENOMEM;
2326
2327 rxr->rx_tpa[i].data = data;
2328 rxr->rx_tpa[i].mapping = mapping;
2329 }
2330 } else {
2331 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2332 return -ENOMEM;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339static int bnxt_init_rx_rings(struct bnxt *bp)
2340{
2341 int i, rc = 0;
2342
2343 for (i = 0; i < bp->rx_nr_rings; i++) {
2344 rc = bnxt_init_one_rx_ring(bp, i);
2345 if (rc)
2346 break;
2347 }
2348
2349 return rc;
2350}
2351
2352static int bnxt_init_tx_rings(struct bnxt *bp)
2353{
2354 u16 i;
2355
2356 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2357 MAX_SKB_FRAGS + 1);
2358
2359 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002360 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002361 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2362
2363 ring->fw_ring_id = INVALID_HW_RING_ID;
2364 }
2365
2366 return 0;
2367}
2368
2369static void bnxt_free_ring_grps(struct bnxt *bp)
2370{
2371 kfree(bp->grp_info);
2372 bp->grp_info = NULL;
2373}
2374
2375static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2376{
2377 int i;
2378
2379 if (irq_re_init) {
2380 bp->grp_info = kcalloc(bp->cp_nr_rings,
2381 sizeof(struct bnxt_ring_grp_info),
2382 GFP_KERNEL);
2383 if (!bp->grp_info)
2384 return -ENOMEM;
2385 }
2386 for (i = 0; i < bp->cp_nr_rings; i++) {
2387 if (irq_re_init)
2388 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2389 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2390 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2391 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2392 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2393 }
2394 return 0;
2395}
2396
2397static void bnxt_free_vnics(struct bnxt *bp)
2398{
2399 kfree(bp->vnic_info);
2400 bp->vnic_info = NULL;
2401 bp->nr_vnics = 0;
2402}
2403
2404static int bnxt_alloc_vnics(struct bnxt *bp)
2405{
2406 int num_vnics = 1;
2407
2408#ifdef CONFIG_RFS_ACCEL
2409 if (bp->flags & BNXT_FLAG_RFS)
2410 num_vnics += bp->rx_nr_rings;
2411#endif
2412
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002413 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2414 num_vnics++;
2415
Michael Chanc0c050c2015-10-22 16:01:17 -04002416 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2417 GFP_KERNEL);
2418 if (!bp->vnic_info)
2419 return -ENOMEM;
2420
2421 bp->nr_vnics = num_vnics;
2422 return 0;
2423}
2424
2425static void bnxt_init_vnics(struct bnxt *bp)
2426{
2427 int i;
2428
2429 for (i = 0; i < bp->nr_vnics; i++) {
2430 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2431
2432 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002433 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2434 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002435 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2436
2437 if (bp->vnic_info[i].rss_hash_key) {
2438 if (i == 0)
2439 prandom_bytes(vnic->rss_hash_key,
2440 HW_HASH_KEY_SIZE);
2441 else
2442 memcpy(vnic->rss_hash_key,
2443 bp->vnic_info[0].rss_hash_key,
2444 HW_HASH_KEY_SIZE);
2445 }
2446 }
2447}
2448
2449static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2450{
2451 int pages;
2452
2453 pages = ring_size / desc_per_pg;
2454
2455 if (!pages)
2456 return 1;
2457
2458 pages++;
2459
2460 while (pages & (pages - 1))
2461 pages++;
2462
2463 return pages;
2464}
2465
2466static void bnxt_set_tpa_flags(struct bnxt *bp)
2467{
2468 bp->flags &= ~BNXT_FLAG_TPA;
2469 if (bp->dev->features & NETIF_F_LRO)
2470 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002471 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002472 bp->flags |= BNXT_FLAG_GRO;
2473}
2474
2475/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2476 * be set on entry.
2477 */
2478void bnxt_set_ring_params(struct bnxt *bp)
2479{
2480 u32 ring_size, rx_size, rx_space;
2481 u32 agg_factor = 0, agg_ring_size = 0;
2482
2483 /* 8 for CRC and VLAN */
2484 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2485
2486 rx_space = rx_size + NET_SKB_PAD +
2487 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2488
2489 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2490 ring_size = bp->rx_ring_size;
2491 bp->rx_agg_ring_size = 0;
2492 bp->rx_agg_nr_pages = 0;
2493
2494 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002495 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002496
2497 bp->flags &= ~BNXT_FLAG_JUMBO;
2498 if (rx_space > PAGE_SIZE) {
2499 u32 jumbo_factor;
2500
2501 bp->flags |= BNXT_FLAG_JUMBO;
2502 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2503 if (jumbo_factor > agg_factor)
2504 agg_factor = jumbo_factor;
2505 }
2506 agg_ring_size = ring_size * agg_factor;
2507
2508 if (agg_ring_size) {
2509 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2510 RX_DESC_CNT);
2511 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2512 u32 tmp = agg_ring_size;
2513
2514 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2515 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2516 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2517 tmp, agg_ring_size);
2518 }
2519 bp->rx_agg_ring_size = agg_ring_size;
2520 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2521 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2522 rx_space = rx_size + NET_SKB_PAD +
2523 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2524 }
2525
2526 bp->rx_buf_use_size = rx_size;
2527 bp->rx_buf_size = rx_space;
2528
2529 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2530 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2531
2532 ring_size = bp->tx_ring_size;
2533 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2534 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2535
2536 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2537 bp->cp_ring_size = ring_size;
2538
2539 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2540 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2541 bp->cp_nr_pages = MAX_CP_PAGES;
2542 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2543 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2544 ring_size, bp->cp_ring_size);
2545 }
2546 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2547 bp->cp_ring_mask = bp->cp_bit - 1;
2548}
2549
2550static void bnxt_free_vnic_attributes(struct bnxt *bp)
2551{
2552 int i;
2553 struct bnxt_vnic_info *vnic;
2554 struct pci_dev *pdev = bp->pdev;
2555
2556 if (!bp->vnic_info)
2557 return;
2558
2559 for (i = 0; i < bp->nr_vnics; i++) {
2560 vnic = &bp->vnic_info[i];
2561
2562 kfree(vnic->fw_grp_ids);
2563 vnic->fw_grp_ids = NULL;
2564
2565 kfree(vnic->uc_list);
2566 vnic->uc_list = NULL;
2567
2568 if (vnic->mc_list) {
2569 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2570 vnic->mc_list, vnic->mc_list_mapping);
2571 vnic->mc_list = NULL;
2572 }
2573
2574 if (vnic->rss_table) {
2575 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2576 vnic->rss_table,
2577 vnic->rss_table_dma_addr);
2578 vnic->rss_table = NULL;
2579 }
2580
2581 vnic->rss_hash_key = NULL;
2582 vnic->flags = 0;
2583 }
2584}
2585
2586static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2587{
2588 int i, rc = 0, size;
2589 struct bnxt_vnic_info *vnic;
2590 struct pci_dev *pdev = bp->pdev;
2591 int max_rings;
2592
2593 for (i = 0; i < bp->nr_vnics; i++) {
2594 vnic = &bp->vnic_info[i];
2595
2596 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2597 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2598
2599 if (mem_size > 0) {
2600 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2601 if (!vnic->uc_list) {
2602 rc = -ENOMEM;
2603 goto out;
2604 }
2605 }
2606 }
2607
2608 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2609 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2610 vnic->mc_list =
2611 dma_alloc_coherent(&pdev->dev,
2612 vnic->mc_list_size,
2613 &vnic->mc_list_mapping,
2614 GFP_KERNEL);
2615 if (!vnic->mc_list) {
2616 rc = -ENOMEM;
2617 goto out;
2618 }
2619 }
2620
2621 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2622 max_rings = bp->rx_nr_rings;
2623 else
2624 max_rings = 1;
2625
2626 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2627 if (!vnic->fw_grp_ids) {
2628 rc = -ENOMEM;
2629 goto out;
2630 }
2631
2632 /* Allocate rss table and hash key */
2633 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2634 &vnic->rss_table_dma_addr,
2635 GFP_KERNEL);
2636 if (!vnic->rss_table) {
2637 rc = -ENOMEM;
2638 goto out;
2639 }
2640
2641 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2642
2643 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2644 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2645 }
2646 return 0;
2647
2648out:
2649 return rc;
2650}
2651
2652static void bnxt_free_hwrm_resources(struct bnxt *bp)
2653{
2654 struct pci_dev *pdev = bp->pdev;
2655
2656 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2657 bp->hwrm_cmd_resp_dma_addr);
2658
2659 bp->hwrm_cmd_resp_addr = NULL;
2660 if (bp->hwrm_dbg_resp_addr) {
2661 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2662 bp->hwrm_dbg_resp_addr,
2663 bp->hwrm_dbg_resp_dma_addr);
2664
2665 bp->hwrm_dbg_resp_addr = NULL;
2666 }
2667}
2668
2669static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2670{
2671 struct pci_dev *pdev = bp->pdev;
2672
2673 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2674 &bp->hwrm_cmd_resp_dma_addr,
2675 GFP_KERNEL);
2676 if (!bp->hwrm_cmd_resp_addr)
2677 return -ENOMEM;
2678 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2679 HWRM_DBG_REG_BUF_SIZE,
2680 &bp->hwrm_dbg_resp_dma_addr,
2681 GFP_KERNEL);
2682 if (!bp->hwrm_dbg_resp_addr)
2683 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2684
2685 return 0;
2686}
2687
2688static void bnxt_free_stats(struct bnxt *bp)
2689{
2690 u32 size, i;
2691 struct pci_dev *pdev = bp->pdev;
2692
Michael Chan3bdf56c2016-03-07 15:38:45 -05002693 if (bp->hw_rx_port_stats) {
2694 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2695 bp->hw_rx_port_stats,
2696 bp->hw_rx_port_stats_map);
2697 bp->hw_rx_port_stats = NULL;
2698 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2699 }
2700
Michael Chanc0c050c2015-10-22 16:01:17 -04002701 if (!bp->bnapi)
2702 return;
2703
2704 size = sizeof(struct ctx_hw_stats);
2705
2706 for (i = 0; i < bp->cp_nr_rings; i++) {
2707 struct bnxt_napi *bnapi = bp->bnapi[i];
2708 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2709
2710 if (cpr->hw_stats) {
2711 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2712 cpr->hw_stats_map);
2713 cpr->hw_stats = NULL;
2714 }
2715 }
2716}
2717
2718static int bnxt_alloc_stats(struct bnxt *bp)
2719{
2720 u32 size, i;
2721 struct pci_dev *pdev = bp->pdev;
2722
2723 size = sizeof(struct ctx_hw_stats);
2724
2725 for (i = 0; i < bp->cp_nr_rings; i++) {
2726 struct bnxt_napi *bnapi = bp->bnapi[i];
2727 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2728
2729 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2730 &cpr->hw_stats_map,
2731 GFP_KERNEL);
2732 if (!cpr->hw_stats)
2733 return -ENOMEM;
2734
2735 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2736 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002737
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002738 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002739 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2740 sizeof(struct tx_port_stats) + 1024;
2741
2742 bp->hw_rx_port_stats =
2743 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2744 &bp->hw_rx_port_stats_map,
2745 GFP_KERNEL);
2746 if (!bp->hw_rx_port_stats)
2747 return -ENOMEM;
2748
2749 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2750 512;
2751 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2752 sizeof(struct rx_port_stats) + 512;
2753 bp->flags |= BNXT_FLAG_PORT_STATS;
2754 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002755 return 0;
2756}
2757
2758static void bnxt_clear_ring_indices(struct bnxt *bp)
2759{
2760 int i;
2761
2762 if (!bp->bnapi)
2763 return;
2764
2765 for (i = 0; i < bp->cp_nr_rings; i++) {
2766 struct bnxt_napi *bnapi = bp->bnapi[i];
2767 struct bnxt_cp_ring_info *cpr;
2768 struct bnxt_rx_ring_info *rxr;
2769 struct bnxt_tx_ring_info *txr;
2770
2771 if (!bnapi)
2772 continue;
2773
2774 cpr = &bnapi->cp_ring;
2775 cpr->cp_raw_cons = 0;
2776
Michael Chanb6ab4b02016-01-02 23:44:59 -05002777 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002778 if (txr) {
2779 txr->tx_prod = 0;
2780 txr->tx_cons = 0;
2781 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002782
Michael Chanb6ab4b02016-01-02 23:44:59 -05002783 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002784 if (rxr) {
2785 rxr->rx_prod = 0;
2786 rxr->rx_agg_prod = 0;
2787 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002788 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002789 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002790 }
2791}
2792
2793static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2794{
2795#ifdef CONFIG_RFS_ACCEL
2796 int i;
2797
2798 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2799 * safe to delete the hash table.
2800 */
2801 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2802 struct hlist_head *head;
2803 struct hlist_node *tmp;
2804 struct bnxt_ntuple_filter *fltr;
2805
2806 head = &bp->ntp_fltr_hash_tbl[i];
2807 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2808 hlist_del(&fltr->hash);
2809 kfree(fltr);
2810 }
2811 }
2812 if (irq_reinit) {
2813 kfree(bp->ntp_fltr_bmap);
2814 bp->ntp_fltr_bmap = NULL;
2815 }
2816 bp->ntp_fltr_count = 0;
2817#endif
2818}
2819
2820static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2821{
2822#ifdef CONFIG_RFS_ACCEL
2823 int i, rc = 0;
2824
2825 if (!(bp->flags & BNXT_FLAG_RFS))
2826 return 0;
2827
2828 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2829 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2830
2831 bp->ntp_fltr_count = 0;
2832 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2833 GFP_KERNEL);
2834
2835 if (!bp->ntp_fltr_bmap)
2836 rc = -ENOMEM;
2837
2838 return rc;
2839#else
2840 return 0;
2841#endif
2842}
2843
2844static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2845{
2846 bnxt_free_vnic_attributes(bp);
2847 bnxt_free_tx_rings(bp);
2848 bnxt_free_rx_rings(bp);
2849 bnxt_free_cp_rings(bp);
2850 bnxt_free_ntp_fltrs(bp, irq_re_init);
2851 if (irq_re_init) {
2852 bnxt_free_stats(bp);
2853 bnxt_free_ring_grps(bp);
2854 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002855 kfree(bp->tx_ring);
2856 bp->tx_ring = NULL;
2857 kfree(bp->rx_ring);
2858 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002859 kfree(bp->bnapi);
2860 bp->bnapi = NULL;
2861 } else {
2862 bnxt_clear_ring_indices(bp);
2863 }
2864}
2865
2866static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2867{
Michael Chan01657bc2016-01-02 23:45:03 -05002868 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002869 void *bnapi;
2870
2871 if (irq_re_init) {
2872 /* Allocate bnapi mem pointer array and mem block for
2873 * all queues
2874 */
2875 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2876 bp->cp_nr_rings);
2877 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2878 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2879 if (!bnapi)
2880 return -ENOMEM;
2881
2882 bp->bnapi = bnapi;
2883 bnapi += arr_size;
2884 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2885 bp->bnapi[i] = bnapi;
2886 bp->bnapi[i]->index = i;
2887 bp->bnapi[i]->bp = bp;
2888 }
2889
Michael Chanb6ab4b02016-01-02 23:44:59 -05002890 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2891 sizeof(struct bnxt_rx_ring_info),
2892 GFP_KERNEL);
2893 if (!bp->rx_ring)
2894 return -ENOMEM;
2895
2896 for (i = 0; i < bp->rx_nr_rings; i++) {
2897 bp->rx_ring[i].bnapi = bp->bnapi[i];
2898 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2899 }
2900
2901 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2902 sizeof(struct bnxt_tx_ring_info),
2903 GFP_KERNEL);
2904 if (!bp->tx_ring)
2905 return -ENOMEM;
2906
Michael Chan01657bc2016-01-02 23:45:03 -05002907 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2908 j = 0;
2909 else
2910 j = bp->rx_nr_rings;
2911
2912 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2913 bp->tx_ring[i].bnapi = bp->bnapi[j];
2914 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002915 }
2916
Michael Chanc0c050c2015-10-22 16:01:17 -04002917 rc = bnxt_alloc_stats(bp);
2918 if (rc)
2919 goto alloc_mem_err;
2920
2921 rc = bnxt_alloc_ntp_fltrs(bp);
2922 if (rc)
2923 goto alloc_mem_err;
2924
2925 rc = bnxt_alloc_vnics(bp);
2926 if (rc)
2927 goto alloc_mem_err;
2928 }
2929
2930 bnxt_init_ring_struct(bp);
2931
2932 rc = bnxt_alloc_rx_rings(bp);
2933 if (rc)
2934 goto alloc_mem_err;
2935
2936 rc = bnxt_alloc_tx_rings(bp);
2937 if (rc)
2938 goto alloc_mem_err;
2939
2940 rc = bnxt_alloc_cp_rings(bp);
2941 if (rc)
2942 goto alloc_mem_err;
2943
2944 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2945 BNXT_VNIC_UCAST_FLAG;
2946 rc = bnxt_alloc_vnic_attributes(bp);
2947 if (rc)
2948 goto alloc_mem_err;
2949 return 0;
2950
2951alloc_mem_err:
2952 bnxt_free_mem(bp, true);
2953 return rc;
2954}
2955
Michael Chan9d8bc092016-12-29 12:13:33 -05002956static void bnxt_disable_int(struct bnxt *bp)
2957{
2958 int i;
2959
2960 if (!bp->bnapi)
2961 return;
2962
2963 for (i = 0; i < bp->cp_nr_rings; i++) {
2964 struct bnxt_napi *bnapi = bp->bnapi[i];
2965 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2966
2967 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2968 }
2969}
2970
2971static void bnxt_disable_int_sync(struct bnxt *bp)
2972{
2973 int i;
2974
2975 atomic_inc(&bp->intr_sem);
2976
2977 bnxt_disable_int(bp);
2978 for (i = 0; i < bp->cp_nr_rings; i++)
2979 synchronize_irq(bp->irq_tbl[i].vector);
2980}
2981
2982static void bnxt_enable_int(struct bnxt *bp)
2983{
2984 int i;
2985
2986 atomic_set(&bp->intr_sem, 0);
2987 for (i = 0; i < bp->cp_nr_rings; i++) {
2988 struct bnxt_napi *bnapi = bp->bnapi[i];
2989 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2990
2991 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2992 }
2993}
2994
Michael Chanc0c050c2015-10-22 16:01:17 -04002995void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2996 u16 cmpl_ring, u16 target_id)
2997{
Michael Chana8643e12016-02-26 04:00:05 -05002998 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04002999
Michael Chana8643e12016-02-26 04:00:05 -05003000 req->req_type = cpu_to_le16(req_type);
3001 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3002 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003003 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3004}
3005
Michael Chanfbfbc482016-02-26 04:00:07 -05003006static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3007 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003008{
Michael Chana11fa2b2016-05-15 03:04:47 -04003009 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003010 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003011 u32 *data = msg;
3012 __le32 *resp_len, *valid;
3013 u16 cp_ring_id, len = 0;
3014 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3015
Michael Chana8643e12016-02-26 04:00:05 -05003016 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003017 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003018 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003019 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3020
3021 /* Write request msg to hwrm channel */
3022 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3023
Michael Chane6ef2692016-03-28 19:46:05 -04003024 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003025 writel(0, bp->bar0 + i);
3026
Michael Chanc0c050c2015-10-22 16:01:17 -04003027 /* currently supports only one outstanding message */
3028 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003029 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003030
3031 /* Ring channel doorbell */
3032 writel(1, bp->bar0 + 0x100);
3033
Michael Chanff4fe812016-02-26 04:00:04 -05003034 if (!timeout)
3035 timeout = DFLT_HWRM_CMD_TIMEOUT;
3036
Michael Chanc0c050c2015-10-22 16:01:17 -04003037 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003038 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003039 if (intr_process) {
3040 /* Wait until hwrm response cmpl interrupt is processed */
3041 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003042 i++ < tmo_count) {
3043 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003044 }
3045
3046 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3047 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003048 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003049 return -1;
3050 }
3051 } else {
3052 /* Check if response len is updated */
3053 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003054 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003055 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3056 HWRM_RESP_LEN_SFT;
3057 if (len)
3058 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003059 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003060 }
3061
Michael Chana11fa2b2016-05-15 03:04:47 -04003062 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003063 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003064 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003065 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003066 return -1;
3067 }
3068
3069 /* Last word of resp contains valid bit */
3070 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003071 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003072 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3073 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003074 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003075 }
3076
Michael Chana11fa2b2016-05-15 03:04:47 -04003077 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003078 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003079 timeout, le16_to_cpu(req->req_type),
3080 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003081 return -1;
3082 }
3083 }
3084
3085 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003086 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003087 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3088 le16_to_cpu(resp->req_type),
3089 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003090 return rc;
3091}
3092
3093int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3094{
3095 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003096}
3097
3098int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3099{
3100 int rc;
3101
3102 mutex_lock(&bp->hwrm_cmd_lock);
3103 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3104 mutex_unlock(&bp->hwrm_cmd_lock);
3105 return rc;
3106}
3107
Michael Chan90e209212016-02-26 04:00:08 -05003108int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3109 int timeout)
3110{
3111 int rc;
3112
3113 mutex_lock(&bp->hwrm_cmd_lock);
3114 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3115 mutex_unlock(&bp->hwrm_cmd_lock);
3116 return rc;
3117}
3118
Michael Chana1653b12016-12-07 00:26:20 -05003119int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3120 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003121{
3122 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003123 DECLARE_BITMAP(async_events_bmap, 256);
3124 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003125 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003126
3127 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3128
3129 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003130 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003131
Michael Chan25be8622016-04-05 14:09:00 -04003132 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3133 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3134 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3135
Michael Chana1653b12016-12-07 00:26:20 -05003136 if (bmap && bmap_size) {
3137 for (i = 0; i < bmap_size; i++) {
3138 if (test_bit(i, bmap))
3139 __set_bit(i, async_events_bmap);
3140 }
3141 }
3142
Michael Chan25be8622016-04-05 14:09:00 -04003143 for (i = 0; i < 8; i++)
3144 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3145
Michael Chana1653b12016-12-07 00:26:20 -05003146 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3147}
3148
3149static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3150{
3151 struct hwrm_func_drv_rgtr_input req = {0};
3152
3153 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3154
3155 req.enables =
3156 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3157 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3158
Michael Chan11f15ed2016-04-05 14:08:55 -04003159 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003160 req.ver_maj = DRV_VER_MAJ;
3161 req.ver_min = DRV_VER_MIN;
3162 req.ver_upd = DRV_VER_UPD;
3163
3164 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003165 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003166 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003167 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003168
Michael Chande68f5de2015-12-09 19:35:41 -05003169 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003170 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3171 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3172
Michael Chande68f5de2015-12-09 19:35:41 -05003173 for (i = 0; i < 8; i++)
3174 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3175
Michael Chanc0c050c2015-10-22 16:01:17 -04003176 req.enables |=
3177 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3178 }
3179
3180 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3181}
3182
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003183static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3184{
3185 struct hwrm_func_drv_unrgtr_input req = {0};
3186
3187 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3188 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3189}
3190
Michael Chanc0c050c2015-10-22 16:01:17 -04003191static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3192{
3193 u32 rc = 0;
3194 struct hwrm_tunnel_dst_port_free_input req = {0};
3195
3196 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3197 req.tunnel_type = tunnel_type;
3198
3199 switch (tunnel_type) {
3200 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3201 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3202 break;
3203 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3204 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3205 break;
3206 default:
3207 break;
3208 }
3209
3210 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3211 if (rc)
3212 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3213 rc);
3214 return rc;
3215}
3216
3217static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3218 u8 tunnel_type)
3219{
3220 u32 rc = 0;
3221 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3222 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3223
3224 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3225
3226 req.tunnel_type = tunnel_type;
3227 req.tunnel_dst_port_val = port;
3228
3229 mutex_lock(&bp->hwrm_cmd_lock);
3230 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3231 if (rc) {
3232 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3233 rc);
3234 goto err_out;
3235 }
3236
Christophe Jaillet57aac712016-11-22 06:14:40 +01003237 switch (tunnel_type) {
3238 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003239 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003240 break;
3241 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003242 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003243 break;
3244 default:
3245 break;
3246 }
3247
Michael Chanc0c050c2015-10-22 16:01:17 -04003248err_out:
3249 mutex_unlock(&bp->hwrm_cmd_lock);
3250 return rc;
3251}
3252
3253static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3254{
3255 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3256 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3257
3258 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003259 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003260
3261 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3262 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3263 req.mask = cpu_to_le32(vnic->rx_mask);
3264 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3265}
3266
3267#ifdef CONFIG_RFS_ACCEL
3268static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3269 struct bnxt_ntuple_filter *fltr)
3270{
3271 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3272
3273 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3274 req.ntuple_filter_id = fltr->filter_id;
3275 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3276}
3277
3278#define BNXT_NTP_FLTR_FLAGS \
3279 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3280 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3281 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3282 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3283 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3284 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3285 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3286 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3287 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3288 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3289 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3290 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3291 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003292 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003293
3294static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3295 struct bnxt_ntuple_filter *fltr)
3296{
3297 int rc = 0;
3298 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3299 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3300 bp->hwrm_cmd_resp_addr;
3301 struct flow_keys *keys = &fltr->fkeys;
3302 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3303
3304 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003305 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003306
3307 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3308
3309 req.ethertype = htons(ETH_P_IP);
3310 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003311 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003312 req.ip_protocol = keys->basic.ip_proto;
3313
3314 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3315 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3316 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3317 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3318
3319 req.src_port = keys->ports.src;
3320 req.src_port_mask = cpu_to_be16(0xffff);
3321 req.dst_port = keys->ports.dst;
3322 req.dst_port_mask = cpu_to_be16(0xffff);
3323
Michael Chanc1935542015-12-27 18:19:28 -05003324 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003325 mutex_lock(&bp->hwrm_cmd_lock);
3326 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3327 if (!rc)
3328 fltr->filter_id = resp->ntuple_filter_id;
3329 mutex_unlock(&bp->hwrm_cmd_lock);
3330 return rc;
3331}
3332#endif
3333
3334static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3335 u8 *mac_addr)
3336{
3337 u32 rc = 0;
3338 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3339 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3340
3341 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003342 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3343 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3344 req.flags |=
3345 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003346 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003347 req.enables =
3348 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003349 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003350 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3351 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3352 req.l2_addr_mask[0] = 0xff;
3353 req.l2_addr_mask[1] = 0xff;
3354 req.l2_addr_mask[2] = 0xff;
3355 req.l2_addr_mask[3] = 0xff;
3356 req.l2_addr_mask[4] = 0xff;
3357 req.l2_addr_mask[5] = 0xff;
3358
3359 mutex_lock(&bp->hwrm_cmd_lock);
3360 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3361 if (!rc)
3362 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3363 resp->l2_filter_id;
3364 mutex_unlock(&bp->hwrm_cmd_lock);
3365 return rc;
3366}
3367
3368static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3369{
3370 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3371 int rc = 0;
3372
3373 /* Any associated ntuple filters will also be cleared by firmware. */
3374 mutex_lock(&bp->hwrm_cmd_lock);
3375 for (i = 0; i < num_of_vnics; i++) {
3376 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3377
3378 for (j = 0; j < vnic->uc_filter_count; j++) {
3379 struct hwrm_cfa_l2_filter_free_input req = {0};
3380
3381 bnxt_hwrm_cmd_hdr_init(bp, &req,
3382 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3383
3384 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3385
3386 rc = _hwrm_send_message(bp, &req, sizeof(req),
3387 HWRM_CMD_TIMEOUT);
3388 }
3389 vnic->uc_filter_count = 0;
3390 }
3391 mutex_unlock(&bp->hwrm_cmd_lock);
3392
3393 return rc;
3394}
3395
3396static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3397{
3398 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3399 struct hwrm_vnic_tpa_cfg_input req = {0};
3400
3401 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3402
3403 if (tpa_flags) {
3404 u16 mss = bp->dev->mtu - 40;
3405 u32 nsegs, n, segs = 0, flags;
3406
3407 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3408 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3409 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3410 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3411 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3412 if (tpa_flags & BNXT_FLAG_GRO)
3413 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3414
3415 req.flags = cpu_to_le32(flags);
3416
3417 req.enables =
3418 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003419 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3420 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003421
3422 /* Number of segs are log2 units, and first packet is not
3423 * included as part of this units.
3424 */
Michael Chan2839f282016-04-25 02:30:50 -04003425 if (mss <= BNXT_RX_PAGE_SIZE) {
3426 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003427 nsegs = (MAX_SKB_FRAGS - 1) * n;
3428 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003429 n = mss / BNXT_RX_PAGE_SIZE;
3430 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003431 n++;
3432 nsegs = (MAX_SKB_FRAGS - n) / n;
3433 }
3434
3435 segs = ilog2(nsegs);
3436 req.max_agg_segs = cpu_to_le16(segs);
3437 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003438
3439 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003440 }
3441 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3442
3443 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3444}
3445
3446static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3447{
3448 u32 i, j, max_rings;
3449 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3450 struct hwrm_vnic_rss_cfg_input req = {0};
3451
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003452 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003453 return 0;
3454
3455 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3456 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003457 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003458 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3459 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3460 max_rings = bp->rx_nr_rings - 1;
3461 else
3462 max_rings = bp->rx_nr_rings;
3463 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003464 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003465 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003466
3467 /* Fill the RSS indirection table with ring group ids */
3468 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3469 if (j == max_rings)
3470 j = 0;
3471 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3472 }
3473
3474 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3475 req.hash_key_tbl_addr =
3476 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3477 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003478 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003479 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3480}
3481
3482static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3483{
3484 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3485 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3486
3487 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3488 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3489 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3490 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3491 req.enables =
3492 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3493 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3494 /* thresholds not implemented in firmware yet */
3495 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3496 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3497 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3498 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3499}
3500
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003501static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3502 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003503{
3504 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3505
3506 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3507 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003508 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003509
3510 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003511 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003512}
3513
3514static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3515{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003516 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003517
3518 for (i = 0; i < bp->nr_vnics; i++) {
3519 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3520
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003521 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3522 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3523 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3524 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003525 }
3526 bp->rsscos_nr_ctxs = 0;
3527}
3528
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003529static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003530{
3531 int rc;
3532 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3533 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3534 bp->hwrm_cmd_resp_addr;
3535
3536 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3537 -1);
3538
3539 mutex_lock(&bp->hwrm_cmd_lock);
3540 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3541 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003542 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003543 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3544 mutex_unlock(&bp->hwrm_cmd_lock);
3545
3546 return rc;
3547}
3548
Michael Chana588e452016-12-07 00:26:21 -05003549int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003550{
Michael Chanb81a90d2016-01-02 23:45:01 -05003551 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003552 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3553 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003554 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003555
3556 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003557
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003558 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3559 /* Only RSS support for now TBD: COS & LB */
3560 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3561 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3562 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3563 VNIC_CFG_REQ_ENABLES_MRU);
3564 } else {
3565 req.rss_rule = cpu_to_le16(0xffff);
3566 }
3567
3568 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3569 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003570 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3571 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3572 } else {
3573 req.cos_rule = cpu_to_le16(0xffff);
3574 }
3575
Michael Chanc0c050c2015-10-22 16:01:17 -04003576 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003577 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003578 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003579 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003580 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3581 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003582
Michael Chanb81a90d2016-01-02 23:45:01 -05003583 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003584 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3585 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3586
3587 req.lb_rule = cpu_to_le16(0xffff);
3588 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3589 VLAN_HLEN);
3590
Michael Chancf6645f2016-06-13 02:25:28 -04003591#ifdef CONFIG_BNXT_SRIOV
3592 if (BNXT_VF(bp))
3593 def_vlan = bp->vf.vlan;
3594#endif
3595 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003596 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003597 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3598 req.flags |=
3599 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003600
3601 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3602}
3603
3604static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3605{
3606 u32 rc = 0;
3607
3608 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3609 struct hwrm_vnic_free_input req = {0};
3610
3611 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3612 req.vnic_id =
3613 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3614
3615 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3616 if (rc)
3617 return rc;
3618 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3619 }
3620 return rc;
3621}
3622
3623static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3624{
3625 u16 i;
3626
3627 for (i = 0; i < bp->nr_vnics; i++)
3628 bnxt_hwrm_vnic_free_one(bp, i);
3629}
3630
Michael Chanb81a90d2016-01-02 23:45:01 -05003631static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3632 unsigned int start_rx_ring_idx,
3633 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003634{
Michael Chanb81a90d2016-01-02 23:45:01 -05003635 int rc = 0;
3636 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003637 struct hwrm_vnic_alloc_input req = {0};
3638 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3639
3640 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003641 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3642 grp_idx = bp->rx_ring[i].bnapi->index;
3643 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003644 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003645 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003646 break;
3647 }
3648 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003649 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003650 }
3651
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003652 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3653 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003654 if (vnic_id == 0)
3655 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3656
3657 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3658
3659 mutex_lock(&bp->hwrm_cmd_lock);
3660 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3661 if (!rc)
3662 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3663 mutex_unlock(&bp->hwrm_cmd_lock);
3664 return rc;
3665}
3666
3667static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3668{
3669 u16 i;
3670 u32 rc = 0;
3671
3672 mutex_lock(&bp->hwrm_cmd_lock);
3673 for (i = 0; i < bp->rx_nr_rings; i++) {
3674 struct hwrm_ring_grp_alloc_input req = {0};
3675 struct hwrm_ring_grp_alloc_output *resp =
3676 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003677 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003678
3679 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3680
Michael Chanb81a90d2016-01-02 23:45:01 -05003681 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3682 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3683 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3684 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003685
3686 rc = _hwrm_send_message(bp, &req, sizeof(req),
3687 HWRM_CMD_TIMEOUT);
3688 if (rc)
3689 break;
3690
Michael Chanb81a90d2016-01-02 23:45:01 -05003691 bp->grp_info[grp_idx].fw_grp_id =
3692 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003693 }
3694 mutex_unlock(&bp->hwrm_cmd_lock);
3695 return rc;
3696}
3697
3698static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3699{
3700 u16 i;
3701 u32 rc = 0;
3702 struct hwrm_ring_grp_free_input req = {0};
3703
3704 if (!bp->grp_info)
3705 return 0;
3706
3707 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3708
3709 mutex_lock(&bp->hwrm_cmd_lock);
3710 for (i = 0; i < bp->cp_nr_rings; i++) {
3711 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3712 continue;
3713 req.ring_group_id =
3714 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3715
3716 rc = _hwrm_send_message(bp, &req, sizeof(req),
3717 HWRM_CMD_TIMEOUT);
3718 if (rc)
3719 break;
3720 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3721 }
3722 mutex_unlock(&bp->hwrm_cmd_lock);
3723 return rc;
3724}
3725
3726static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3727 struct bnxt_ring_struct *ring,
3728 u32 ring_type, u32 map_index,
3729 u32 stats_ctx_id)
3730{
3731 int rc = 0, err = 0;
3732 struct hwrm_ring_alloc_input req = {0};
3733 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3734 u16 ring_id;
3735
3736 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3737
3738 req.enables = 0;
3739 if (ring->nr_pages > 1) {
3740 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3741 /* Page size is in log2 units */
3742 req.page_size = BNXT_PAGE_SHIFT;
3743 req.page_tbl_depth = 1;
3744 } else {
3745 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3746 }
3747 req.fbo = 0;
3748 /* Association of ring index with doorbell index and MSIX number */
3749 req.logical_id = cpu_to_le16(map_index);
3750
3751 switch (ring_type) {
3752 case HWRM_RING_ALLOC_TX:
3753 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3754 /* Association of transmit ring with completion ring */
3755 req.cmpl_ring_id =
3756 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3757 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3758 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3759 req.queue_id = cpu_to_le16(ring->queue_id);
3760 break;
3761 case HWRM_RING_ALLOC_RX:
3762 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3763 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3764 break;
3765 case HWRM_RING_ALLOC_AGG:
3766 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3767 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3768 break;
3769 case HWRM_RING_ALLOC_CMPL:
3770 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3771 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3772 if (bp->flags & BNXT_FLAG_USING_MSIX)
3773 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3774 break;
3775 default:
3776 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3777 ring_type);
3778 return -1;
3779 }
3780
3781 mutex_lock(&bp->hwrm_cmd_lock);
3782 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3783 err = le16_to_cpu(resp->error_code);
3784 ring_id = le16_to_cpu(resp->ring_id);
3785 mutex_unlock(&bp->hwrm_cmd_lock);
3786
3787 if (rc || err) {
3788 switch (ring_type) {
3789 case RING_FREE_REQ_RING_TYPE_CMPL:
3790 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3791 rc, err);
3792 return -1;
3793
3794 case RING_FREE_REQ_RING_TYPE_RX:
3795 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3796 rc, err);
3797 return -1;
3798
3799 case RING_FREE_REQ_RING_TYPE_TX:
3800 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3801 rc, err);
3802 return -1;
3803
3804 default:
3805 netdev_err(bp->dev, "Invalid ring\n");
3806 return -1;
3807 }
3808 }
3809 ring->fw_ring_id = ring_id;
3810 return rc;
3811}
3812
3813static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3814{
3815 int i, rc = 0;
3816
Michael Chanedd0c2c2015-12-27 18:19:19 -05003817 for (i = 0; i < bp->cp_nr_rings; i++) {
3818 struct bnxt_napi *bnapi = bp->bnapi[i];
3819 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3820 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003821
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003822 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003823 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3824 INVALID_STATS_CTX_ID);
3825 if (rc)
3826 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003827 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3828 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003829 }
3830
Michael Chanedd0c2c2015-12-27 18:19:19 -05003831 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003832 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003833 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003834 u32 map_idx = txr->bnapi->index;
3835 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003836
Michael Chanb81a90d2016-01-02 23:45:01 -05003837 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3838 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003839 if (rc)
3840 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003841 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003842 }
3843
Michael Chanedd0c2c2015-12-27 18:19:19 -05003844 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003845 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003846 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003847 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003848
Michael Chanb81a90d2016-01-02 23:45:01 -05003849 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3850 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003851 if (rc)
3852 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003853 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003854 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003855 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003856 }
3857
3858 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3859 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003860 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003861 struct bnxt_ring_struct *ring =
3862 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003863 u32 grp_idx = rxr->bnapi->index;
3864 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003865
3866 rc = hwrm_ring_alloc_send_msg(bp, ring,
3867 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003868 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003869 INVALID_STATS_CTX_ID);
3870 if (rc)
3871 goto err_out;
3872
Michael Chanb81a90d2016-01-02 23:45:01 -05003873 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003874 writel(DB_KEY_RX | rxr->rx_agg_prod,
3875 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003876 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003877 }
3878 }
3879err_out:
3880 return rc;
3881}
3882
3883static int hwrm_ring_free_send_msg(struct bnxt *bp,
3884 struct bnxt_ring_struct *ring,
3885 u32 ring_type, int cmpl_ring_id)
3886{
3887 int rc;
3888 struct hwrm_ring_free_input req = {0};
3889 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3890 u16 error_code;
3891
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003892 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003893 req.ring_type = ring_type;
3894 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3895
3896 mutex_lock(&bp->hwrm_cmd_lock);
3897 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3898 error_code = le16_to_cpu(resp->error_code);
3899 mutex_unlock(&bp->hwrm_cmd_lock);
3900
3901 if (rc || error_code) {
3902 switch (ring_type) {
3903 case RING_FREE_REQ_RING_TYPE_CMPL:
3904 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3905 rc);
3906 return rc;
3907 case RING_FREE_REQ_RING_TYPE_RX:
3908 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3909 rc);
3910 return rc;
3911 case RING_FREE_REQ_RING_TYPE_TX:
3912 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3913 rc);
3914 return rc;
3915 default:
3916 netdev_err(bp->dev, "Invalid ring\n");
3917 return -1;
3918 }
3919 }
3920 return 0;
3921}
3922
Michael Chanedd0c2c2015-12-27 18:19:19 -05003923static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003924{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003925 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003926
3927 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003928 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003929
Michael Chanedd0c2c2015-12-27 18:19:19 -05003930 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003931 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003932 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003933 u32 grp_idx = txr->bnapi->index;
3934 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003935
Michael Chanedd0c2c2015-12-27 18:19:19 -05003936 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3937 hwrm_ring_free_send_msg(bp, ring,
3938 RING_FREE_REQ_RING_TYPE_TX,
3939 close_path ? cmpl_ring_id :
3940 INVALID_HW_RING_ID);
3941 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003942 }
3943 }
3944
Michael Chanedd0c2c2015-12-27 18:19:19 -05003945 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003946 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003947 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003948 u32 grp_idx = rxr->bnapi->index;
3949 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003950
Michael Chanedd0c2c2015-12-27 18:19:19 -05003951 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3952 hwrm_ring_free_send_msg(bp, ring,
3953 RING_FREE_REQ_RING_TYPE_RX,
3954 close_path ? cmpl_ring_id :
3955 INVALID_HW_RING_ID);
3956 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003957 bp->grp_info[grp_idx].rx_fw_ring_id =
3958 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003959 }
3960 }
3961
Michael Chanedd0c2c2015-12-27 18:19:19 -05003962 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003963 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003964 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003965 u32 grp_idx = rxr->bnapi->index;
3966 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003967
Michael Chanedd0c2c2015-12-27 18:19:19 -05003968 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3969 hwrm_ring_free_send_msg(bp, ring,
3970 RING_FREE_REQ_RING_TYPE_RX,
3971 close_path ? cmpl_ring_id :
3972 INVALID_HW_RING_ID);
3973 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003974 bp->grp_info[grp_idx].agg_fw_ring_id =
3975 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003976 }
3977 }
3978
Michael Chan9d8bc092016-12-29 12:13:33 -05003979 /* The completion rings are about to be freed. After that the
3980 * IRQ doorbell will not work anymore. So we need to disable
3981 * IRQ here.
3982 */
3983 bnxt_disable_int_sync(bp);
3984
Michael Chanedd0c2c2015-12-27 18:19:19 -05003985 for (i = 0; i < bp->cp_nr_rings; i++) {
3986 struct bnxt_napi *bnapi = bp->bnapi[i];
3987 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3988 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003989
Michael Chanedd0c2c2015-12-27 18:19:19 -05003990 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3991 hwrm_ring_free_send_msg(bp, ring,
3992 RING_FREE_REQ_RING_TYPE_CMPL,
3993 INVALID_HW_RING_ID);
3994 ring->fw_ring_id = INVALID_HW_RING_ID;
3995 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003996 }
3997 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003998}
3999
Michael Chanbb053f52016-02-26 04:00:02 -05004000static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4001 u32 buf_tmrs, u16 flags,
4002 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4003{
4004 req->flags = cpu_to_le16(flags);
4005 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4006 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4007 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4008 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4009 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4010 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4011 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4012 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4013}
4014
Michael Chanc0c050c2015-10-22 16:01:17 -04004015int bnxt_hwrm_set_coal(struct bnxt *bp)
4016{
4017 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004018 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4019 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004020 u16 max_buf, max_buf_irq;
4021 u16 buf_tmr, buf_tmr_irq;
4022 u32 flags;
4023
Michael Chandfc9c942016-02-26 04:00:03 -05004024 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4025 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4026 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4027 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004028
Michael Chandfb5b892016-02-26 04:00:01 -05004029 /* Each rx completion (2 records) should be DMAed immediately.
4030 * DMA 1/4 of the completion buffers at a time.
4031 */
4032 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004033 /* max_buf must not be zero */
4034 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004035 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4036 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4037 /* buf timer set to 1/4 of interrupt timer */
4038 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4039 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4040 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004041
4042 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4043
4044 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4045 * if coal_ticks is less than 25 us.
4046 */
Michael Chandfb5b892016-02-26 04:00:01 -05004047 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004048 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4049
Michael Chanbb053f52016-02-26 04:00:02 -05004050 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004051 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4052
4053 /* max_buf must not be zero */
4054 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4055 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4056 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4057 /* buf timer set to 1/4 of interrupt timer */
4058 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4059 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4060 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4061
4062 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4063 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4064 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004065
4066 mutex_lock(&bp->hwrm_cmd_lock);
4067 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004068 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004069
Michael Chandfc9c942016-02-26 04:00:03 -05004070 req = &req_rx;
4071 if (!bnapi->rx_ring)
4072 req = &req_tx;
4073 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4074
4075 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004076 HWRM_CMD_TIMEOUT);
4077 if (rc)
4078 break;
4079 }
4080 mutex_unlock(&bp->hwrm_cmd_lock);
4081 return rc;
4082}
4083
4084static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4085{
4086 int rc = 0, i;
4087 struct hwrm_stat_ctx_free_input req = {0};
4088
4089 if (!bp->bnapi)
4090 return 0;
4091
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004092 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4093 return 0;
4094
Michael Chanc0c050c2015-10-22 16:01:17 -04004095 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4096
4097 mutex_lock(&bp->hwrm_cmd_lock);
4098 for (i = 0; i < bp->cp_nr_rings; i++) {
4099 struct bnxt_napi *bnapi = bp->bnapi[i];
4100 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4101
4102 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4103 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4104
4105 rc = _hwrm_send_message(bp, &req, sizeof(req),
4106 HWRM_CMD_TIMEOUT);
4107 if (rc)
4108 break;
4109
4110 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4111 }
4112 }
4113 mutex_unlock(&bp->hwrm_cmd_lock);
4114 return rc;
4115}
4116
4117static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4118{
4119 int rc = 0, i;
4120 struct hwrm_stat_ctx_alloc_input req = {0};
4121 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4122
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004123 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4124 return 0;
4125
Michael Chanc0c050c2015-10-22 16:01:17 -04004126 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4127
Michael Chan51f30782016-07-01 18:46:29 -04004128 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004129
4130 mutex_lock(&bp->hwrm_cmd_lock);
4131 for (i = 0; i < bp->cp_nr_rings; i++) {
4132 struct bnxt_napi *bnapi = bp->bnapi[i];
4133 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4134
4135 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4136
4137 rc = _hwrm_send_message(bp, &req, sizeof(req),
4138 HWRM_CMD_TIMEOUT);
4139 if (rc)
4140 break;
4141
4142 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4143
4144 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4145 }
4146 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004147 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004148}
4149
Michael Chancf6645f2016-06-13 02:25:28 -04004150static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4151{
4152 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004153 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004154 int rc;
4155
4156 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4157 req.fid = cpu_to_le16(0xffff);
4158 mutex_lock(&bp->hwrm_cmd_lock);
4159 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4160 if (rc)
4161 goto func_qcfg_exit;
4162
4163#ifdef CONFIG_BNXT_SRIOV
4164 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004165 struct bnxt_vf_info *vf = &bp->vf;
4166
4167 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4168 }
4169#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004170 switch (resp->port_partition_type) {
4171 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4172 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4173 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4174 bp->port_partition_type = resp->port_partition_type;
4175 break;
4176 }
Michael Chancf6645f2016-06-13 02:25:28 -04004177
4178func_qcfg_exit:
4179 mutex_unlock(&bp->hwrm_cmd_lock);
4180 return rc;
4181}
4182
Michael Chan7b08f662016-12-07 00:26:18 -05004183static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004184{
4185 int rc = 0;
4186 struct hwrm_func_qcaps_input req = {0};
4187 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4188
4189 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4190 req.fid = cpu_to_le16(0xffff);
4191
4192 mutex_lock(&bp->hwrm_cmd_lock);
4193 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4194 if (rc)
4195 goto hwrm_func_qcaps_exit;
4196
Michael Chane4060d32016-12-07 00:26:19 -05004197 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4198 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4199 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4200 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4201
Michael Chan7cc5a202016-09-19 03:58:05 -04004202 bp->tx_push_thresh = 0;
4203 if (resp->flags &
4204 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4205 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4206
Michael Chanc0c050c2015-10-22 16:01:17 -04004207 if (BNXT_PF(bp)) {
4208 struct bnxt_pf_info *pf = &bp->pf;
4209
4210 pf->fw_fid = le16_to_cpu(resp->fid);
4211 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004212 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004213 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004214 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004215 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4216 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4217 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004218 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004219 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4220 if (!pf->max_hw_ring_grps)
4221 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004222 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4223 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4224 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4225 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4226 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4227 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4228 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4229 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4230 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4231 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4232 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4233 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004234#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004235 struct bnxt_vf_info *vf = &bp->vf;
4236
4237 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004238
4239 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4240 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4241 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4242 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004243 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4244 if (!vf->max_hw_ring_grps)
4245 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004246 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4247 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4248 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004249
4250 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004251 mutex_unlock(&bp->hwrm_cmd_lock);
4252
4253 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004254 /* overwrite netdev dev_adr with admin VF MAC */
4255 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004256 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004257 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004258 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4259 }
4260 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004261#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004262 }
4263
Michael Chanc0c050c2015-10-22 16:01:17 -04004264hwrm_func_qcaps_exit:
4265 mutex_unlock(&bp->hwrm_cmd_lock);
4266 return rc;
4267}
4268
4269static int bnxt_hwrm_func_reset(struct bnxt *bp)
4270{
4271 struct hwrm_func_reset_input req = {0};
4272
4273 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4274 req.enables = 0;
4275
4276 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4277}
4278
4279static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4280{
4281 int rc = 0;
4282 struct hwrm_queue_qportcfg_input req = {0};
4283 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4284 u8 i, *qptr;
4285
4286 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4287
4288 mutex_lock(&bp->hwrm_cmd_lock);
4289 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4290 if (rc)
4291 goto qportcfg_exit;
4292
4293 if (!resp->max_configurable_queues) {
4294 rc = -EINVAL;
4295 goto qportcfg_exit;
4296 }
4297 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004298 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004299 if (bp->max_tc > BNXT_MAX_QUEUE)
4300 bp->max_tc = BNXT_MAX_QUEUE;
4301
Michael Chan441cabb2016-09-19 03:58:02 -04004302 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4303 bp->max_tc = 1;
4304
Michael Chan87c374d2016-12-02 21:17:16 -05004305 if (bp->max_lltc > bp->max_tc)
4306 bp->max_lltc = bp->max_tc;
4307
Michael Chanc0c050c2015-10-22 16:01:17 -04004308 qptr = &resp->queue_id0;
4309 for (i = 0; i < bp->max_tc; i++) {
4310 bp->q_info[i].queue_id = *qptr++;
4311 bp->q_info[i].queue_profile = *qptr++;
4312 }
4313
4314qportcfg_exit:
4315 mutex_unlock(&bp->hwrm_cmd_lock);
4316 return rc;
4317}
4318
4319static int bnxt_hwrm_ver_get(struct bnxt *bp)
4320{
4321 int rc;
4322 struct hwrm_ver_get_input req = {0};
4323 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4324
Michael Chane6ef2692016-03-28 19:46:05 -04004325 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004326 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4327 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4328 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4329 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4330 mutex_lock(&bp->hwrm_cmd_lock);
4331 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4332 if (rc)
4333 goto hwrm_ver_get_exit;
4334
4335 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4336
Michael Chan11f15ed2016-04-05 14:08:55 -04004337 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4338 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004339 if (resp->hwrm_intf_maj < 1) {
4340 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004341 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004342 resp->hwrm_intf_upd);
4343 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004344 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004345 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004346 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4347 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4348
Michael Chanff4fe812016-02-26 04:00:04 -05004349 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4350 if (!bp->hwrm_cmd_timeout)
4351 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4352
Michael Chane6ef2692016-03-28 19:46:05 -04004353 if (resp->hwrm_intf_maj >= 1)
4354 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4355
Michael Chan659c8052016-06-13 02:25:33 -04004356 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004357 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4358 !resp->chip_metal)
4359 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004360
Michael Chanc0c050c2015-10-22 16:01:17 -04004361hwrm_ver_get_exit:
4362 mutex_unlock(&bp->hwrm_cmd_lock);
4363 return rc;
4364}
4365
Rob Swindell5ac67d82016-09-19 03:58:03 -04004366int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4367{
Rob Swindell878786d2016-09-20 03:36:33 -04004368#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004369 struct hwrm_fw_set_time_input req = {0};
4370 struct rtc_time tm;
4371 struct timeval tv;
4372
4373 if (bp->hwrm_spec_code < 0x10400)
4374 return -EOPNOTSUPP;
4375
4376 do_gettimeofday(&tv);
4377 rtc_time_to_tm(tv.tv_sec, &tm);
4378 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4379 req.year = cpu_to_le16(1900 + tm.tm_year);
4380 req.month = 1 + tm.tm_mon;
4381 req.day = tm.tm_mday;
4382 req.hour = tm.tm_hour;
4383 req.minute = tm.tm_min;
4384 req.second = tm.tm_sec;
4385 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004386#else
4387 return -EOPNOTSUPP;
4388#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004389}
4390
Michael Chan3bdf56c2016-03-07 15:38:45 -05004391static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4392{
4393 int rc;
4394 struct bnxt_pf_info *pf = &bp->pf;
4395 struct hwrm_port_qstats_input req = {0};
4396
4397 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4398 return 0;
4399
4400 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4401 req.port_id = cpu_to_le16(pf->port_id);
4402 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4403 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4404 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4405 return rc;
4406}
4407
Michael Chanc0c050c2015-10-22 16:01:17 -04004408static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4409{
4410 if (bp->vxlan_port_cnt) {
4411 bnxt_hwrm_tunnel_dst_port_free(
4412 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4413 }
4414 bp->vxlan_port_cnt = 0;
4415 if (bp->nge_port_cnt) {
4416 bnxt_hwrm_tunnel_dst_port_free(
4417 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4418 }
4419 bp->nge_port_cnt = 0;
4420}
4421
4422static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4423{
4424 int rc, i;
4425 u32 tpa_flags = 0;
4426
4427 if (set_tpa)
4428 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4429 for (i = 0; i < bp->nr_vnics; i++) {
4430 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4431 if (rc) {
4432 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4433 rc, i);
4434 return rc;
4435 }
4436 }
4437 return 0;
4438}
4439
4440static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4441{
4442 int i;
4443
4444 for (i = 0; i < bp->nr_vnics; i++)
4445 bnxt_hwrm_vnic_set_rss(bp, i, false);
4446}
4447
4448static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4449 bool irq_re_init)
4450{
4451 if (bp->vnic_info) {
4452 bnxt_hwrm_clear_vnic_filter(bp);
4453 /* clear all RSS setting before free vnic ctx */
4454 bnxt_hwrm_clear_vnic_rss(bp);
4455 bnxt_hwrm_vnic_ctx_free(bp);
4456 /* before free the vnic, undo the vnic tpa settings */
4457 if (bp->flags & BNXT_FLAG_TPA)
4458 bnxt_set_tpa(bp, false);
4459 bnxt_hwrm_vnic_free(bp);
4460 }
4461 bnxt_hwrm_ring_free(bp, close_path);
4462 bnxt_hwrm_ring_grp_free(bp);
4463 if (irq_re_init) {
4464 bnxt_hwrm_stat_ctx_free(bp);
4465 bnxt_hwrm_free_tunnel_ports(bp);
4466 }
4467}
4468
4469static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4470{
4471 int rc;
4472
4473 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004474 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004475 if (rc) {
4476 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4477 vnic_id, rc);
4478 goto vnic_setup_err;
4479 }
4480 bp->rsscos_nr_ctxs++;
4481
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004482 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4483 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4484 if (rc) {
4485 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4486 vnic_id, rc);
4487 goto vnic_setup_err;
4488 }
4489 bp->rsscos_nr_ctxs++;
4490 }
4491
Michael Chanc0c050c2015-10-22 16:01:17 -04004492 /* configure default vnic, ring grp */
4493 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4494 if (rc) {
4495 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4496 vnic_id, rc);
4497 goto vnic_setup_err;
4498 }
4499
4500 /* Enable RSS hashing on vnic */
4501 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4502 if (rc) {
4503 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4504 vnic_id, rc);
4505 goto vnic_setup_err;
4506 }
4507
4508 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4509 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4510 if (rc) {
4511 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4512 vnic_id, rc);
4513 }
4514 }
4515
4516vnic_setup_err:
4517 return rc;
4518}
4519
4520static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4521{
4522#ifdef CONFIG_RFS_ACCEL
4523 int i, rc = 0;
4524
4525 for (i = 0; i < bp->rx_nr_rings; i++) {
4526 u16 vnic_id = i + 1;
4527 u16 ring_id = i;
4528
4529 if (vnic_id >= bp->nr_vnics)
4530 break;
4531
4532 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004533 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004534 if (rc) {
4535 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4536 vnic_id, rc);
4537 break;
4538 }
4539 rc = bnxt_setup_vnic(bp, vnic_id);
4540 if (rc)
4541 break;
4542 }
4543 return rc;
4544#else
4545 return 0;
4546#endif
4547}
4548
Michael Chan17c71ac2016-07-01 18:46:27 -04004549/* Allow PF and VF with default VLAN to be in promiscuous mode */
4550static bool bnxt_promisc_ok(struct bnxt *bp)
4551{
4552#ifdef CONFIG_BNXT_SRIOV
4553 if (BNXT_VF(bp) && !bp->vf.vlan)
4554 return false;
4555#endif
4556 return true;
4557}
4558
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004559static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4560{
4561 unsigned int rc = 0;
4562
4563 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4564 if (rc) {
4565 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4566 rc);
4567 return rc;
4568 }
4569
4570 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4571 if (rc) {
4572 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4573 rc);
4574 return rc;
4575 }
4576 return rc;
4577}
4578
Michael Chanb664f002015-12-02 01:54:08 -05004579static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004580static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004581
Michael Chanc0c050c2015-10-22 16:01:17 -04004582static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4583{
Michael Chan7d2837d2016-05-04 16:56:44 -04004584 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004585 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004586 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004587
4588 if (irq_re_init) {
4589 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4590 if (rc) {
4591 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4592 rc);
4593 goto err_out;
4594 }
4595 }
4596
4597 rc = bnxt_hwrm_ring_alloc(bp);
4598 if (rc) {
4599 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4600 goto err_out;
4601 }
4602
4603 rc = bnxt_hwrm_ring_grp_alloc(bp);
4604 if (rc) {
4605 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4606 goto err_out;
4607 }
4608
Prashant Sreedharan76595192016-07-18 07:15:22 -04004609 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4610 rx_nr_rings--;
4611
Michael Chanc0c050c2015-10-22 16:01:17 -04004612 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004613 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004614 if (rc) {
4615 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4616 goto err_out;
4617 }
4618
4619 rc = bnxt_setup_vnic(bp, 0);
4620 if (rc)
4621 goto err_out;
4622
4623 if (bp->flags & BNXT_FLAG_RFS) {
4624 rc = bnxt_alloc_rfs_vnics(bp);
4625 if (rc)
4626 goto err_out;
4627 }
4628
4629 if (bp->flags & BNXT_FLAG_TPA) {
4630 rc = bnxt_set_tpa(bp, true);
4631 if (rc)
4632 goto err_out;
4633 }
4634
4635 if (BNXT_VF(bp))
4636 bnxt_update_vf_mac(bp);
4637
4638 /* Filter for default vnic 0 */
4639 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4640 if (rc) {
4641 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4642 goto err_out;
4643 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004644 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004645
Michael Chan7d2837d2016-05-04 16:56:44 -04004646 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004647
Michael Chan17c71ac2016-07-01 18:46:27 -04004648 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004649 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4650
4651 if (bp->dev->flags & IFF_ALLMULTI) {
4652 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4653 vnic->mc_list_count = 0;
4654 } else {
4655 u32 mask = 0;
4656
4657 bnxt_mc_list_updated(bp, &mask);
4658 vnic->rx_mask |= mask;
4659 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004660
Michael Chanb664f002015-12-02 01:54:08 -05004661 rc = bnxt_cfg_rx_mode(bp);
4662 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004663 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004664
4665 rc = bnxt_hwrm_set_coal(bp);
4666 if (rc)
4667 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004668 rc);
4669
4670 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4671 rc = bnxt_setup_nitroa0_vnic(bp);
4672 if (rc)
4673 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4674 rc);
4675 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004676
Michael Chancf6645f2016-06-13 02:25:28 -04004677 if (BNXT_VF(bp)) {
4678 bnxt_hwrm_func_qcfg(bp);
4679 netdev_update_features(bp->dev);
4680 }
4681
Michael Chanc0c050c2015-10-22 16:01:17 -04004682 return 0;
4683
4684err_out:
4685 bnxt_hwrm_resource_free(bp, 0, true);
4686
4687 return rc;
4688}
4689
4690static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4691{
4692 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4693 return 0;
4694}
4695
4696static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4697{
4698 bnxt_init_rx_rings(bp);
4699 bnxt_init_tx_rings(bp);
4700 bnxt_init_ring_grps(bp, irq_re_init);
4701 bnxt_init_vnics(bp);
4702
4703 return bnxt_init_chip(bp, irq_re_init);
4704}
4705
Michael Chanc0c050c2015-10-22 16:01:17 -04004706static int bnxt_set_real_num_queues(struct bnxt *bp)
4707{
4708 int rc;
4709 struct net_device *dev = bp->dev;
4710
4711 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4712 if (rc)
4713 return rc;
4714
4715 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4716 if (rc)
4717 return rc;
4718
4719#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004720 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004721 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004722#endif
4723
4724 return rc;
4725}
4726
Michael Chan6e6c5a52016-01-02 23:45:02 -05004727static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4728 bool shared)
4729{
4730 int _rx = *rx, _tx = *tx;
4731
4732 if (shared) {
4733 *rx = min_t(int, _rx, max);
4734 *tx = min_t(int, _tx, max);
4735 } else {
4736 if (max < 2)
4737 return -ENOMEM;
4738
4739 while (_rx + _tx > max) {
4740 if (_rx > _tx && _rx > 1)
4741 _rx--;
4742 else if (_tx > 1)
4743 _tx--;
4744 }
4745 *rx = _rx;
4746 *tx = _tx;
4747 }
4748 return 0;
4749}
4750
Michael Chan78095922016-12-07 00:26:16 -05004751static void bnxt_setup_msix(struct bnxt *bp)
4752{
4753 const int len = sizeof(bp->irq_tbl[0].name);
4754 struct net_device *dev = bp->dev;
4755 int tcs, i;
4756
4757 tcs = netdev_get_num_tc(dev);
4758 if (tcs > 1) {
4759 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4760 if (bp->tx_nr_rings_per_tc == 0) {
4761 netdev_reset_tc(dev);
4762 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4763 } else {
4764 int i, off, count;
4765
4766 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4767 for (i = 0; i < tcs; i++) {
4768 count = bp->tx_nr_rings_per_tc;
4769 off = i * count;
4770 netdev_set_tc_queue(dev, i, count, off);
4771 }
4772 }
4773 }
4774
4775 for (i = 0; i < bp->cp_nr_rings; i++) {
4776 char *attr;
4777
4778 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4779 attr = "TxRx";
4780 else if (i < bp->rx_nr_rings)
4781 attr = "rx";
4782 else
4783 attr = "tx";
4784
4785 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
4786 i);
4787 bp->irq_tbl[i].handler = bnxt_msix;
4788 }
4789}
4790
4791static void bnxt_setup_inta(struct bnxt *bp)
4792{
4793 const int len = sizeof(bp->irq_tbl[0].name);
4794
4795 if (netdev_get_num_tc(bp->dev))
4796 netdev_reset_tc(bp->dev);
4797
4798 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
4799 0);
4800 bp->irq_tbl[0].handler = bnxt_inta;
4801}
4802
4803static int bnxt_setup_int_mode(struct bnxt *bp)
4804{
4805 int rc;
4806
4807 if (bp->flags & BNXT_FLAG_USING_MSIX)
4808 bnxt_setup_msix(bp);
4809 else
4810 bnxt_setup_inta(bp);
4811
4812 rc = bnxt_set_real_num_queues(bp);
4813 return rc;
4814}
4815
Michael Chane4060d32016-12-07 00:26:19 -05004816unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
4817{
4818#if defined(CONFIG_BNXT_SRIOV)
4819 if (BNXT_VF(bp))
4820 return bp->vf.max_stat_ctxs;
4821#endif
4822 return bp->pf.max_stat_ctxs;
4823}
4824
Michael Chana588e452016-12-07 00:26:21 -05004825void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
4826{
4827#if defined(CONFIG_BNXT_SRIOV)
4828 if (BNXT_VF(bp))
4829 bp->vf.max_stat_ctxs = max;
4830 else
4831#endif
4832 bp->pf.max_stat_ctxs = max;
4833}
4834
Michael Chane4060d32016-12-07 00:26:19 -05004835unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
4836{
4837#if defined(CONFIG_BNXT_SRIOV)
4838 if (BNXT_VF(bp))
4839 return bp->vf.max_cp_rings;
4840#endif
4841 return bp->pf.max_cp_rings;
4842}
4843
Michael Chana588e452016-12-07 00:26:21 -05004844void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
4845{
4846#if defined(CONFIG_BNXT_SRIOV)
4847 if (BNXT_VF(bp))
4848 bp->vf.max_cp_rings = max;
4849 else
4850#endif
4851 bp->pf.max_cp_rings = max;
4852}
4853
Michael Chan78095922016-12-07 00:26:16 -05004854static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
4855{
4856#if defined(CONFIG_BNXT_SRIOV)
4857 if (BNXT_VF(bp))
4858 return bp->vf.max_irqs;
4859#endif
4860 return bp->pf.max_irqs;
4861}
4862
Michael Chan33c26572016-12-07 00:26:15 -05004863void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
4864{
4865#if defined(CONFIG_BNXT_SRIOV)
4866 if (BNXT_VF(bp))
4867 bp->vf.max_irqs = max_irqs;
4868 else
4869#endif
4870 bp->pf.max_irqs = max_irqs;
4871}
4872
Michael Chan78095922016-12-07 00:26:16 -05004873static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004874{
Michael Chan01657bc2016-01-02 23:45:03 -05004875 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05004876 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04004877
Michael Chan78095922016-12-07 00:26:16 -05004878 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004879 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4880 if (!msix_ent)
4881 return -ENOMEM;
4882
4883 for (i = 0; i < total_vecs; i++) {
4884 msix_ent[i].entry = i;
4885 msix_ent[i].vector = 0;
4886 }
4887
Michael Chan01657bc2016-01-02 23:45:03 -05004888 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4889 min = 2;
4890
4891 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004892 if (total_vecs < 0) {
4893 rc = -ENODEV;
4894 goto msix_setup_exit;
4895 }
4896
4897 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4898 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05004899 for (i = 0; i < total_vecs; i++)
4900 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04004901
Michael Chan78095922016-12-07 00:26:16 -05004902 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004903 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004904 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004905 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004906 if (rc)
4907 goto msix_setup_exit;
4908
Michael Chanc0c050c2015-10-22 16:01:17 -04004909 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05004910 bp->cp_nr_rings = (min == 1) ?
4911 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
4912 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004913
Michael Chanc0c050c2015-10-22 16:01:17 -04004914 } else {
4915 rc = -ENOMEM;
4916 goto msix_setup_exit;
4917 }
4918 bp->flags |= BNXT_FLAG_USING_MSIX;
4919 kfree(msix_ent);
4920 return 0;
4921
4922msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05004923 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
4924 kfree(bp->irq_tbl);
4925 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004926 pci_disable_msix(bp->pdev);
4927 kfree(msix_ent);
4928 return rc;
4929}
4930
Michael Chan78095922016-12-07 00:26:16 -05004931static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004932{
Michael Chanc0c050c2015-10-22 16:01:17 -04004933 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05004934 if (!bp->irq_tbl)
4935 return -ENOMEM;
4936
4937 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004938 bp->rx_nr_rings = 1;
4939 bp->tx_nr_rings = 1;
4940 bp->cp_nr_rings = 1;
4941 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004942 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004943 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05004944 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004945}
4946
Michael Chan78095922016-12-07 00:26:16 -05004947static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004948{
4949 int rc = 0;
4950
4951 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05004952 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004953
Michael Chan1fa72e22016-04-25 02:30:49 -04004954 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004955 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05004956 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004957 }
4958 return rc;
4959}
4960
Michael Chan78095922016-12-07 00:26:16 -05004961static void bnxt_clear_int_mode(struct bnxt *bp)
4962{
4963 if (bp->flags & BNXT_FLAG_USING_MSIX)
4964 pci_disable_msix(bp->pdev);
4965
4966 kfree(bp->irq_tbl);
4967 bp->irq_tbl = NULL;
4968 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4969}
4970
Michael Chanc0c050c2015-10-22 16:01:17 -04004971static void bnxt_free_irq(struct bnxt *bp)
4972{
4973 struct bnxt_irq *irq;
4974 int i;
4975
4976#ifdef CONFIG_RFS_ACCEL
4977 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4978 bp->dev->rx_cpu_rmap = NULL;
4979#endif
4980 if (!bp->irq_tbl)
4981 return;
4982
4983 for (i = 0; i < bp->cp_nr_rings; i++) {
4984 irq = &bp->irq_tbl[i];
4985 if (irq->requested)
4986 free_irq(irq->vector, bp->bnapi[i]);
4987 irq->requested = 0;
4988 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004989}
4990
4991static int bnxt_request_irq(struct bnxt *bp)
4992{
Michael Chanb81a90d2016-01-02 23:45:01 -05004993 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004994 unsigned long flags = 0;
4995#ifdef CONFIG_RFS_ACCEL
4996 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4997#endif
4998
4999 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5000 flags = IRQF_SHARED;
5001
Michael Chanb81a90d2016-01-02 23:45:01 -05005002 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005003 struct bnxt_irq *irq = &bp->irq_tbl[i];
5004#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005005 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005006 rc = irq_cpu_rmap_add(rmap, irq->vector);
5007 if (rc)
5008 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005009 j);
5010 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005011 }
5012#endif
5013 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5014 bp->bnapi[i]);
5015 if (rc)
5016 break;
5017
5018 irq->requested = 1;
5019 }
5020 return rc;
5021}
5022
5023static void bnxt_del_napi(struct bnxt *bp)
5024{
5025 int i;
5026
5027 if (!bp->bnapi)
5028 return;
5029
5030 for (i = 0; i < bp->cp_nr_rings; i++) {
5031 struct bnxt_napi *bnapi = bp->bnapi[i];
5032
5033 napi_hash_del(&bnapi->napi);
5034 netif_napi_del(&bnapi->napi);
5035 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005036 /* We called napi_hash_del() before netif_napi_del(), we need
5037 * to respect an RCU grace period before freeing napi structures.
5038 */
5039 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005040}
5041
5042static void bnxt_init_napi(struct bnxt *bp)
5043{
5044 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005045 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005046 struct bnxt_napi *bnapi;
5047
5048 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005049 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5050 cp_nr_rings--;
5051 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005052 bnapi = bp->bnapi[i];
5053 netif_napi_add(bp->dev, &bnapi->napi,
5054 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005055 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005056 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5057 bnapi = bp->bnapi[cp_nr_rings];
5058 netif_napi_add(bp->dev, &bnapi->napi,
5059 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005060 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005061 } else {
5062 bnapi = bp->bnapi[0];
5063 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005064 }
5065}
5066
5067static void bnxt_disable_napi(struct bnxt *bp)
5068{
5069 int i;
5070
5071 if (!bp->bnapi)
5072 return;
5073
Michael Chanb356a2e2016-12-29 12:13:31 -05005074 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005075 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005076}
5077
5078static void bnxt_enable_napi(struct bnxt *bp)
5079{
5080 int i;
5081
5082 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005083 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005084 napi_enable(&bp->bnapi[i]->napi);
5085 }
5086}
5087
Michael Chan7df4ae92016-12-02 21:17:17 -05005088void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005089{
5090 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005091 struct bnxt_tx_ring_info *txr;
5092 struct netdev_queue *txq;
5093
Michael Chanb6ab4b02016-01-02 23:44:59 -05005094 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005095 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005096 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005097 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005098 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005099 }
5100 }
5101 /* Stop all TX queues */
5102 netif_tx_disable(bp->dev);
5103 netif_carrier_off(bp->dev);
5104}
5105
Michael Chan7df4ae92016-12-02 21:17:17 -05005106void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005107{
5108 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005109 struct bnxt_tx_ring_info *txr;
5110 struct netdev_queue *txq;
5111
5112 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005113 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005114 txq = netdev_get_tx_queue(bp->dev, i);
5115 txr->dev_state = 0;
5116 }
5117 netif_tx_wake_all_queues(bp->dev);
5118 if (bp->link_info.link_up)
5119 netif_carrier_on(bp->dev);
5120}
5121
5122static void bnxt_report_link(struct bnxt *bp)
5123{
5124 if (bp->link_info.link_up) {
5125 const char *duplex;
5126 const char *flow_ctrl;
5127 u16 speed;
5128
5129 netif_carrier_on(bp->dev);
5130 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5131 duplex = "full";
5132 else
5133 duplex = "half";
5134 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5135 flow_ctrl = "ON - receive & transmit";
5136 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5137 flow_ctrl = "ON - transmit";
5138 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5139 flow_ctrl = "ON - receive";
5140 else
5141 flow_ctrl = "none";
5142 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5143 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5144 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005145 if (bp->flags & BNXT_FLAG_EEE_CAP)
5146 netdev_info(bp->dev, "EEE is %s\n",
5147 bp->eee.eee_active ? "active" :
5148 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005149 } else {
5150 netif_carrier_off(bp->dev);
5151 netdev_err(bp->dev, "NIC Link is Down\n");
5152 }
5153}
5154
Michael Chan170ce012016-04-05 14:08:57 -04005155static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5156{
5157 int rc = 0;
5158 struct hwrm_port_phy_qcaps_input req = {0};
5159 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005160 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005161
5162 if (bp->hwrm_spec_code < 0x10201)
5163 return 0;
5164
5165 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5166
5167 mutex_lock(&bp->hwrm_cmd_lock);
5168 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5169 if (rc)
5170 goto hwrm_phy_qcaps_exit;
5171
5172 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5173 struct ethtool_eee *eee = &bp->eee;
5174 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5175
5176 bp->flags |= BNXT_FLAG_EEE_CAP;
5177 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5178 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5179 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5180 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5181 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5182 }
Michael Chan93ed8112016-06-13 02:25:37 -04005183 link_info->support_auto_speeds =
5184 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005185
5186hwrm_phy_qcaps_exit:
5187 mutex_unlock(&bp->hwrm_cmd_lock);
5188 return rc;
5189}
5190
Michael Chanc0c050c2015-10-22 16:01:17 -04005191static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5192{
5193 int rc = 0;
5194 struct bnxt_link_info *link_info = &bp->link_info;
5195 struct hwrm_port_phy_qcfg_input req = {0};
5196 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5197 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005198 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005199
5200 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5201
5202 mutex_lock(&bp->hwrm_cmd_lock);
5203 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5204 if (rc) {
5205 mutex_unlock(&bp->hwrm_cmd_lock);
5206 return rc;
5207 }
5208
5209 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5210 link_info->phy_link_status = resp->link;
5211 link_info->duplex = resp->duplex;
5212 link_info->pause = resp->pause;
5213 link_info->auto_mode = resp->auto_mode;
5214 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005215 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005216 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005217 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005218 if (link_info->phy_link_status == BNXT_LINK_LINK)
5219 link_info->link_speed = le16_to_cpu(resp->link_speed);
5220 else
5221 link_info->link_speed = 0;
5222 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005223 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5224 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005225 link_info->lp_auto_link_speeds =
5226 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005227 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5228 link_info->phy_ver[0] = resp->phy_maj;
5229 link_info->phy_ver[1] = resp->phy_min;
5230 link_info->phy_ver[2] = resp->phy_bld;
5231 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005232 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005233 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005234 link_info->phy_addr = resp->eee_config_phy_addr &
5235 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005236 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005237
Michael Chan170ce012016-04-05 14:08:57 -04005238 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5239 struct ethtool_eee *eee = &bp->eee;
5240 u16 fw_speeds;
5241
5242 eee->eee_active = 0;
5243 if (resp->eee_config_phy_addr &
5244 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5245 eee->eee_active = 1;
5246 fw_speeds = le16_to_cpu(
5247 resp->link_partner_adv_eee_link_speed_mask);
5248 eee->lp_advertised =
5249 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5250 }
5251
5252 /* Pull initial EEE config */
5253 if (!chng_link_state) {
5254 if (resp->eee_config_phy_addr &
5255 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5256 eee->eee_enabled = 1;
5257
5258 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5259 eee->advertised =
5260 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5261
5262 if (resp->eee_config_phy_addr &
5263 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5264 __le32 tmr;
5265
5266 eee->tx_lpi_enabled = 1;
5267 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5268 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5269 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5270 }
5271 }
5272 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005273 /* TODO: need to add more logic to report VF link */
5274 if (chng_link_state) {
5275 if (link_info->phy_link_status == BNXT_LINK_LINK)
5276 link_info->link_up = 1;
5277 else
5278 link_info->link_up = 0;
5279 if (link_up != link_info->link_up)
5280 bnxt_report_link(bp);
5281 } else {
5282 /* alwasy link down if not require to update link state */
5283 link_info->link_up = 0;
5284 }
5285 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005286
5287 diff = link_info->support_auto_speeds ^ link_info->advertising;
5288 if ((link_info->support_auto_speeds | diff) !=
5289 link_info->support_auto_speeds) {
5290 /* An advertised speed is no longer supported, so we need to
5291 * update the advertisement settings. See bnxt_reset() for
5292 * comments about the rtnl_lock() sequence below.
5293 */
5294 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5295 rtnl_lock();
5296 link_info->advertising = link_info->support_auto_speeds;
5297 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
5298 (link_info->autoneg & BNXT_AUTONEG_SPEED))
5299 bnxt_hwrm_set_link_setting(bp, true, false);
5300 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5301 rtnl_unlock();
5302 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005303 return 0;
5304}
5305
Michael Chan10289be2016-05-15 03:04:49 -04005306static void bnxt_get_port_module_status(struct bnxt *bp)
5307{
5308 struct bnxt_link_info *link_info = &bp->link_info;
5309 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5310 u8 module_status;
5311
5312 if (bnxt_update_link(bp, true))
5313 return;
5314
5315 module_status = link_info->module_status;
5316 switch (module_status) {
5317 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5318 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5319 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5320 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5321 bp->pf.port_id);
5322 if (bp->hwrm_spec_code >= 0x10201) {
5323 netdev_warn(bp->dev, "Module part number %s\n",
5324 resp->phy_vendor_partnumber);
5325 }
5326 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5327 netdev_warn(bp->dev, "TX is disabled\n");
5328 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5329 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5330 }
5331}
5332
Michael Chanc0c050c2015-10-22 16:01:17 -04005333static void
5334bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5335{
5336 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005337 if (bp->hwrm_spec_code >= 0x10201)
5338 req->auto_pause =
5339 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005340 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5341 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5342 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005343 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005344 req->enables |=
5345 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5346 } else {
5347 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5348 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5349 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5350 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5351 req->enables |=
5352 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005353 if (bp->hwrm_spec_code >= 0x10201) {
5354 req->auto_pause = req->force_pause;
5355 req->enables |= cpu_to_le32(
5356 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5357 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005358 }
5359}
5360
5361static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5362 struct hwrm_port_phy_cfg_input *req)
5363{
5364 u8 autoneg = bp->link_info.autoneg;
5365 u16 fw_link_speed = bp->link_info.req_link_speed;
5366 u32 advertising = bp->link_info.advertising;
5367
5368 if (autoneg & BNXT_AUTONEG_SPEED) {
5369 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005370 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005371
5372 req->enables |= cpu_to_le32(
5373 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5374 req->auto_link_speed_mask = cpu_to_le16(advertising);
5375
5376 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5377 req->flags |=
5378 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5379 } else {
5380 req->force_link_speed = cpu_to_le16(fw_link_speed);
5381 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5382 }
5383
Michael Chanc0c050c2015-10-22 16:01:17 -04005384 /* tell chimp that the setting takes effect immediately */
5385 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5386}
5387
5388int bnxt_hwrm_set_pause(struct bnxt *bp)
5389{
5390 struct hwrm_port_phy_cfg_input req = {0};
5391 int rc;
5392
5393 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5394 bnxt_hwrm_set_pause_common(bp, &req);
5395
5396 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5397 bp->link_info.force_link_chng)
5398 bnxt_hwrm_set_link_common(bp, &req);
5399
5400 mutex_lock(&bp->hwrm_cmd_lock);
5401 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5402 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5403 /* since changing of pause setting doesn't trigger any link
5404 * change event, the driver needs to update the current pause
5405 * result upon successfully return of the phy_cfg command
5406 */
5407 bp->link_info.pause =
5408 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5409 bp->link_info.auto_pause_setting = 0;
5410 if (!bp->link_info.force_link_chng)
5411 bnxt_report_link(bp);
5412 }
5413 bp->link_info.force_link_chng = false;
5414 mutex_unlock(&bp->hwrm_cmd_lock);
5415 return rc;
5416}
5417
Michael Chan939f7f02016-04-05 14:08:58 -04005418static void bnxt_hwrm_set_eee(struct bnxt *bp,
5419 struct hwrm_port_phy_cfg_input *req)
5420{
5421 struct ethtool_eee *eee = &bp->eee;
5422
5423 if (eee->eee_enabled) {
5424 u16 eee_speeds;
5425 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5426
5427 if (eee->tx_lpi_enabled)
5428 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5429 else
5430 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5431
5432 req->flags |= cpu_to_le32(flags);
5433 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5434 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5435 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5436 } else {
5437 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5438 }
5439}
5440
5441int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005442{
5443 struct hwrm_port_phy_cfg_input req = {0};
5444
5445 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5446 if (set_pause)
5447 bnxt_hwrm_set_pause_common(bp, &req);
5448
5449 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005450
5451 if (set_eee)
5452 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005453 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5454}
5455
Michael Chan33f7d552016-04-11 04:11:12 -04005456static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5457{
5458 struct hwrm_port_phy_cfg_input req = {0};
5459
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005460 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005461 return 0;
5462
5463 if (pci_num_vf(bp->pdev))
5464 return 0;
5465
5466 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005467 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005468 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5469}
5470
Michael Chan939f7f02016-04-05 14:08:58 -04005471static bool bnxt_eee_config_ok(struct bnxt *bp)
5472{
5473 struct ethtool_eee *eee = &bp->eee;
5474 struct bnxt_link_info *link_info = &bp->link_info;
5475
5476 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5477 return true;
5478
5479 if (eee->eee_enabled) {
5480 u32 advertising =
5481 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5482
5483 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5484 eee->eee_enabled = 0;
5485 return false;
5486 }
5487 if (eee->advertised & ~advertising) {
5488 eee->advertised = advertising & eee->supported;
5489 return false;
5490 }
5491 }
5492 return true;
5493}
5494
Michael Chanc0c050c2015-10-22 16:01:17 -04005495static int bnxt_update_phy_setting(struct bnxt *bp)
5496{
5497 int rc;
5498 bool update_link = false;
5499 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005500 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005501 struct bnxt_link_info *link_info = &bp->link_info;
5502
5503 rc = bnxt_update_link(bp, true);
5504 if (rc) {
5505 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5506 rc);
5507 return rc;
5508 }
5509 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005510 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5511 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005512 update_pause = true;
5513 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5514 link_info->force_pause_setting != link_info->req_flow_ctrl)
5515 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005516 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5517 if (BNXT_AUTO_MODE(link_info->auto_mode))
5518 update_link = true;
5519 if (link_info->req_link_speed != link_info->force_link_speed)
5520 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005521 if (link_info->req_duplex != link_info->duplex_setting)
5522 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005523 } else {
5524 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5525 update_link = true;
5526 if (link_info->advertising != link_info->auto_link_speeds)
5527 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005528 }
5529
Michael Chan16d663a2016-11-16 21:13:07 -05005530 /* The last close may have shutdown the link, so need to call
5531 * PHY_CFG to bring it back up.
5532 */
5533 if (!netif_carrier_ok(bp->dev))
5534 update_link = true;
5535
Michael Chan939f7f02016-04-05 14:08:58 -04005536 if (!bnxt_eee_config_ok(bp))
5537 update_eee = true;
5538
Michael Chanc0c050c2015-10-22 16:01:17 -04005539 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005540 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005541 else if (update_pause)
5542 rc = bnxt_hwrm_set_pause(bp);
5543 if (rc) {
5544 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5545 rc);
5546 return rc;
5547 }
5548
5549 return rc;
5550}
5551
Jeffrey Huang11809492015-11-05 16:25:49 -05005552/* Common routine to pre-map certain register block to different GRC window.
5553 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5554 * in PF and 3 windows in VF that can be customized to map in different
5555 * register blocks.
5556 */
5557static void bnxt_preset_reg_win(struct bnxt *bp)
5558{
5559 if (BNXT_PF(bp)) {
5560 /* CAG registers map to GRC window #4 */
5561 writel(BNXT_CAG_REG_BASE,
5562 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5563 }
5564}
5565
Michael Chanc0c050c2015-10-22 16:01:17 -04005566static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5567{
5568 int rc = 0;
5569
Jeffrey Huang11809492015-11-05 16:25:49 -05005570 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005571 netif_carrier_off(bp->dev);
5572 if (irq_re_init) {
5573 rc = bnxt_setup_int_mode(bp);
5574 if (rc) {
5575 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5576 rc);
5577 return rc;
5578 }
5579 }
5580 if ((bp->flags & BNXT_FLAG_RFS) &&
5581 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5582 /* disable RFS if falling back to INTA */
5583 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5584 bp->flags &= ~BNXT_FLAG_RFS;
5585 }
5586
5587 rc = bnxt_alloc_mem(bp, irq_re_init);
5588 if (rc) {
5589 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5590 goto open_err_free_mem;
5591 }
5592
5593 if (irq_re_init) {
5594 bnxt_init_napi(bp);
5595 rc = bnxt_request_irq(bp);
5596 if (rc) {
5597 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5598 goto open_err;
5599 }
5600 }
5601
5602 bnxt_enable_napi(bp);
5603
5604 rc = bnxt_init_nic(bp, irq_re_init);
5605 if (rc) {
5606 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5607 goto open_err;
5608 }
5609
5610 if (link_re_init) {
5611 rc = bnxt_update_phy_setting(bp);
5612 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005613 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005614 }
5615
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005616 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005617 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005618
Michael Chancaefe522015-12-09 19:35:42 -05005619 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005620 bnxt_enable_int(bp);
5621 /* Enable TX queues */
5622 bnxt_tx_enable(bp);
5623 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005624 /* Poll link status and check for SFP+ module status */
5625 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005626
5627 return 0;
5628
5629open_err:
5630 bnxt_disable_napi(bp);
5631 bnxt_del_napi(bp);
5632
5633open_err_free_mem:
5634 bnxt_free_skbs(bp);
5635 bnxt_free_irq(bp);
5636 bnxt_free_mem(bp, true);
5637 return rc;
5638}
5639
5640/* rtnl_lock held */
5641int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5642{
5643 int rc = 0;
5644
5645 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5646 if (rc) {
5647 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5648 dev_close(bp->dev);
5649 }
5650 return rc;
5651}
5652
5653static int bnxt_open(struct net_device *dev)
5654{
5655 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005656
Michael Chanc0c050c2015-10-22 16:01:17 -04005657 return __bnxt_open_nic(bp, true, true);
5658}
5659
Michael Chanc0c050c2015-10-22 16:01:17 -04005660int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5661{
5662 int rc = 0;
5663
5664#ifdef CONFIG_BNXT_SRIOV
5665 if (bp->sriov_cfg) {
5666 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5667 !bp->sriov_cfg,
5668 BNXT_SRIOV_CFG_WAIT_TMO);
5669 if (rc)
5670 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5671 }
5672#endif
5673 /* Change device state to avoid TX queue wake up's */
5674 bnxt_tx_disable(bp);
5675
Michael Chancaefe522015-12-09 19:35:42 -05005676 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005677 smp_mb__after_atomic();
5678 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5679 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005680
Michael Chan9d8bc092016-12-29 12:13:33 -05005681 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04005682 bnxt_shutdown_nic(bp, irq_re_init);
5683
5684 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5685
5686 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005687 del_timer_sync(&bp->timer);
5688 bnxt_free_skbs(bp);
5689
5690 if (irq_re_init) {
5691 bnxt_free_irq(bp);
5692 bnxt_del_napi(bp);
5693 }
5694 bnxt_free_mem(bp, irq_re_init);
5695 return rc;
5696}
5697
5698static int bnxt_close(struct net_device *dev)
5699{
5700 struct bnxt *bp = netdev_priv(dev);
5701
5702 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005703 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005704 return 0;
5705}
5706
5707/* rtnl_lock held */
5708static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5709{
5710 switch (cmd) {
5711 case SIOCGMIIPHY:
5712 /* fallthru */
5713 case SIOCGMIIREG: {
5714 if (!netif_running(dev))
5715 return -EAGAIN;
5716
5717 return 0;
5718 }
5719
5720 case SIOCSMIIREG:
5721 if (!netif_running(dev))
5722 return -EAGAIN;
5723
5724 return 0;
5725
5726 default:
5727 /* do nothing */
5728 break;
5729 }
5730 return -EOPNOTSUPP;
5731}
5732
5733static struct rtnl_link_stats64 *
5734bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5735{
5736 u32 i;
5737 struct bnxt *bp = netdev_priv(dev);
5738
5739 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5740
5741 if (!bp->bnapi)
5742 return stats;
5743
5744 /* TODO check if we need to synchronize with bnxt_close path */
5745 for (i = 0; i < bp->cp_nr_rings; i++) {
5746 struct bnxt_napi *bnapi = bp->bnapi[i];
5747 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5748 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5749
5750 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5751 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5752 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5753
5754 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5755 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5756 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5757
5758 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5759 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5760 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5761
5762 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5763 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5764 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5765
5766 stats->rx_missed_errors +=
5767 le64_to_cpu(hw_stats->rx_discard_pkts);
5768
5769 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5770
Michael Chanc0c050c2015-10-22 16:01:17 -04005771 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5772 }
5773
Michael Chan9947f832016-03-07 15:38:46 -05005774 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5775 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5776 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5777
5778 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5779 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5780 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5781 le64_to_cpu(rx->rx_ovrsz_frames) +
5782 le64_to_cpu(rx->rx_runt_frames);
5783 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5784 le64_to_cpu(rx->rx_jbr_frames);
5785 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5786 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5787 stats->tx_errors = le64_to_cpu(tx->tx_err);
5788 }
5789
Michael Chanc0c050c2015-10-22 16:01:17 -04005790 return stats;
5791}
5792
5793static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5794{
5795 struct net_device *dev = bp->dev;
5796 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5797 struct netdev_hw_addr *ha;
5798 u8 *haddr;
5799 int mc_count = 0;
5800 bool update = false;
5801 int off = 0;
5802
5803 netdev_for_each_mc_addr(ha, dev) {
5804 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5805 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5806 vnic->mc_list_count = 0;
5807 return false;
5808 }
5809 haddr = ha->addr;
5810 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5811 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5812 update = true;
5813 }
5814 off += ETH_ALEN;
5815 mc_count++;
5816 }
5817 if (mc_count)
5818 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5819
5820 if (mc_count != vnic->mc_list_count) {
5821 vnic->mc_list_count = mc_count;
5822 update = true;
5823 }
5824 return update;
5825}
5826
5827static bool bnxt_uc_list_updated(struct bnxt *bp)
5828{
5829 struct net_device *dev = bp->dev;
5830 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5831 struct netdev_hw_addr *ha;
5832 int off = 0;
5833
5834 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5835 return true;
5836
5837 netdev_for_each_uc_addr(ha, dev) {
5838 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5839 return true;
5840
5841 off += ETH_ALEN;
5842 }
5843 return false;
5844}
5845
5846static void bnxt_set_rx_mode(struct net_device *dev)
5847{
5848 struct bnxt *bp = netdev_priv(dev);
5849 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5850 u32 mask = vnic->rx_mask;
5851 bool mc_update = false;
5852 bool uc_update;
5853
5854 if (!netif_running(dev))
5855 return;
5856
5857 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5858 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5859 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5860
Michael Chan17c71ac2016-07-01 18:46:27 -04005861 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005862 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5863
5864 uc_update = bnxt_uc_list_updated(bp);
5865
5866 if (dev->flags & IFF_ALLMULTI) {
5867 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5868 vnic->mc_list_count = 0;
5869 } else {
5870 mc_update = bnxt_mc_list_updated(bp, &mask);
5871 }
5872
5873 if (mask != vnic->rx_mask || uc_update || mc_update) {
5874 vnic->rx_mask = mask;
5875
5876 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5877 schedule_work(&bp->sp_task);
5878 }
5879}
5880
Michael Chanb664f002015-12-02 01:54:08 -05005881static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005882{
5883 struct net_device *dev = bp->dev;
5884 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5885 struct netdev_hw_addr *ha;
5886 int i, off = 0, rc;
5887 bool uc_update;
5888
5889 netif_addr_lock_bh(dev);
5890 uc_update = bnxt_uc_list_updated(bp);
5891 netif_addr_unlock_bh(dev);
5892
5893 if (!uc_update)
5894 goto skip_uc;
5895
5896 mutex_lock(&bp->hwrm_cmd_lock);
5897 for (i = 1; i < vnic->uc_filter_count; i++) {
5898 struct hwrm_cfa_l2_filter_free_input req = {0};
5899
5900 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5901 -1);
5902
5903 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5904
5905 rc = _hwrm_send_message(bp, &req, sizeof(req),
5906 HWRM_CMD_TIMEOUT);
5907 }
5908 mutex_unlock(&bp->hwrm_cmd_lock);
5909
5910 vnic->uc_filter_count = 1;
5911
5912 netif_addr_lock_bh(dev);
5913 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5914 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5915 } else {
5916 netdev_for_each_uc_addr(ha, dev) {
5917 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5918 off += ETH_ALEN;
5919 vnic->uc_filter_count++;
5920 }
5921 }
5922 netif_addr_unlock_bh(dev);
5923
5924 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5925 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5926 if (rc) {
5927 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5928 rc);
5929 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005930 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005931 }
5932 }
5933
5934skip_uc:
5935 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5936 if (rc)
5937 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5938 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005939
5940 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005941}
5942
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005943static bool bnxt_rfs_capable(struct bnxt *bp)
5944{
5945#ifdef CONFIG_RFS_ACCEL
5946 struct bnxt_pf_info *pf = &bp->pf;
5947 int vnics;
5948
5949 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5950 return false;
5951
5952 vnics = 1 + bp->rx_nr_rings;
Vasundhara Volama2304902016-07-25 12:33:36 -04005953 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5954 netdev_warn(bp->dev,
5955 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5956 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005957 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04005958 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005959
5960 return true;
5961#else
5962 return false;
5963#endif
5964}
5965
Michael Chanc0c050c2015-10-22 16:01:17 -04005966static netdev_features_t bnxt_fix_features(struct net_device *dev,
5967 netdev_features_t features)
5968{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005969 struct bnxt *bp = netdev_priv(dev);
5970
Vasundhara Volama2304902016-07-25 12:33:36 -04005971 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005972 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005973
5974 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5975 * turned on or off together.
5976 */
5977 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5978 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5979 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5980 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5981 NETIF_F_HW_VLAN_STAG_RX);
5982 else
5983 features |= NETIF_F_HW_VLAN_CTAG_RX |
5984 NETIF_F_HW_VLAN_STAG_RX;
5985 }
Michael Chancf6645f2016-06-13 02:25:28 -04005986#ifdef CONFIG_BNXT_SRIOV
5987 if (BNXT_VF(bp)) {
5988 if (bp->vf.vlan) {
5989 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5990 NETIF_F_HW_VLAN_STAG_RX);
5991 }
5992 }
5993#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005994 return features;
5995}
5996
5997static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5998{
5999 struct bnxt *bp = netdev_priv(dev);
6000 u32 flags = bp->flags;
6001 u32 changes;
6002 int rc = 0;
6003 bool re_init = false;
6004 bool update_tpa = false;
6005
6006 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006007 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006008 flags |= BNXT_FLAG_GRO;
6009 if (features & NETIF_F_LRO)
6010 flags |= BNXT_FLAG_LRO;
6011
6012 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6013 flags |= BNXT_FLAG_STRIP_VLAN;
6014
6015 if (features & NETIF_F_NTUPLE)
6016 flags |= BNXT_FLAG_RFS;
6017
6018 changes = flags ^ bp->flags;
6019 if (changes & BNXT_FLAG_TPA) {
6020 update_tpa = true;
6021 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6022 (flags & BNXT_FLAG_TPA) == 0)
6023 re_init = true;
6024 }
6025
6026 if (changes & ~BNXT_FLAG_TPA)
6027 re_init = true;
6028
6029 if (flags != bp->flags) {
6030 u32 old_flags = bp->flags;
6031
6032 bp->flags = flags;
6033
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006034 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006035 if (update_tpa)
6036 bnxt_set_ring_params(bp);
6037 return rc;
6038 }
6039
6040 if (re_init) {
6041 bnxt_close_nic(bp, false, false);
6042 if (update_tpa)
6043 bnxt_set_ring_params(bp);
6044
6045 return bnxt_open_nic(bp, false, false);
6046 }
6047 if (update_tpa) {
6048 rc = bnxt_set_tpa(bp,
6049 (flags & BNXT_FLAG_TPA) ?
6050 true : false);
6051 if (rc)
6052 bp->flags = old_flags;
6053 }
6054 }
6055 return rc;
6056}
6057
Michael Chan9f554592016-01-02 23:44:58 -05006058static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6059{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006060 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006061 int i = bnapi->index;
6062
Michael Chan3b2b7d92016-01-02 23:45:00 -05006063 if (!txr)
6064 return;
6065
Michael Chan9f554592016-01-02 23:44:58 -05006066 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6067 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6068 txr->tx_cons);
6069}
6070
6071static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6072{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006073 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006074 int i = bnapi->index;
6075
Michael Chan3b2b7d92016-01-02 23:45:00 -05006076 if (!rxr)
6077 return;
6078
Michael Chan9f554592016-01-02 23:44:58 -05006079 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",
6080 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6081 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6082 rxr->rx_sw_agg_prod);
6083}
6084
6085static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6086{
6087 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6088 int i = bnapi->index;
6089
6090 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6091 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6092}
6093
Michael Chanc0c050c2015-10-22 16:01:17 -04006094static void bnxt_dbg_dump_states(struct bnxt *bp)
6095{
6096 int i;
6097 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006098
6099 for (i = 0; i < bp->cp_nr_rings; i++) {
6100 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006101 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006102 bnxt_dump_tx_sw_state(bnapi);
6103 bnxt_dump_rx_sw_state(bnapi);
6104 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006105 }
6106 }
6107}
6108
Michael Chan6988bd92016-06-13 02:25:29 -04006109static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006110{
Michael Chan6988bd92016-06-13 02:25:29 -04006111 if (!silent)
6112 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006113 if (netif_running(bp->dev)) {
6114 bnxt_close_nic(bp, false, false);
6115 bnxt_open_nic(bp, false, false);
6116 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006117}
6118
6119static void bnxt_tx_timeout(struct net_device *dev)
6120{
6121 struct bnxt *bp = netdev_priv(dev);
6122
6123 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6124 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6125 schedule_work(&bp->sp_task);
6126}
6127
6128#ifdef CONFIG_NET_POLL_CONTROLLER
6129static void bnxt_poll_controller(struct net_device *dev)
6130{
6131 struct bnxt *bp = netdev_priv(dev);
6132 int i;
6133
6134 for (i = 0; i < bp->cp_nr_rings; i++) {
6135 struct bnxt_irq *irq = &bp->irq_tbl[i];
6136
6137 disable_irq(irq->vector);
6138 irq->handler(irq->vector, bp->bnapi[i]);
6139 enable_irq(irq->vector);
6140 }
6141}
6142#endif
6143
6144static void bnxt_timer(unsigned long data)
6145{
6146 struct bnxt *bp = (struct bnxt *)data;
6147 struct net_device *dev = bp->dev;
6148
6149 if (!netif_running(dev))
6150 return;
6151
6152 if (atomic_read(&bp->intr_sem) != 0)
6153 goto bnxt_restart_timer;
6154
Michael Chan3bdf56c2016-03-07 15:38:45 -05006155 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6156 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6157 schedule_work(&bp->sp_task);
6158 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006159bnxt_restart_timer:
6160 mod_timer(&bp->timer, jiffies + bp->current_interval);
6161}
6162
Michael Chan6988bd92016-06-13 02:25:29 -04006163/* Only called from bnxt_sp_task() */
6164static void bnxt_reset(struct bnxt *bp, bool silent)
6165{
6166 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6167 * for BNXT_STATE_IN_SP_TASK to clear.
6168 * If there is a parallel dev_close(), bnxt_close() may be holding
6169 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6170 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6171 */
6172 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6173 rtnl_lock();
6174 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6175 bnxt_reset_task(bp, silent);
6176 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6177 rtnl_unlock();
6178}
6179
Michael Chanc0c050c2015-10-22 16:01:17 -04006180static void bnxt_cfg_ntp_filters(struct bnxt *);
6181
6182static void bnxt_sp_task(struct work_struct *work)
6183{
6184 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6185 int rc;
6186
Michael Chan4cebdce2015-12-09 19:35:43 -05006187 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6188 smp_mb__after_atomic();
6189 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6190 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006191 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006192 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006193
6194 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6195 bnxt_cfg_rx_mode(bp);
6196
6197 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6198 bnxt_cfg_ntp_filters(bp);
6199 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chan286ef9d2016-11-16 21:13:08 -05006200 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6201 &bp->sp_event))
6202 bnxt_hwrm_phy_qcaps(bp);
6203
Michael Chanc0c050c2015-10-22 16:01:17 -04006204 rc = bnxt_update_link(bp, true);
6205 if (rc)
6206 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6207 rc);
6208 }
6209 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6210 bnxt_hwrm_exec_fwd_req(bp);
6211 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6212 bnxt_hwrm_tunnel_dst_port_alloc(
6213 bp, bp->vxlan_port,
6214 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6215 }
6216 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6217 bnxt_hwrm_tunnel_dst_port_free(
6218 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6219 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006220 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6221 bnxt_hwrm_tunnel_dst_port_alloc(
6222 bp, bp->nge_port,
6223 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6224 }
6225 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6226 bnxt_hwrm_tunnel_dst_port_free(
6227 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6228 }
Michael Chan6988bd92016-06-13 02:25:29 -04006229 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6230 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006231
Michael Chanfc0f1922016-06-13 02:25:30 -04006232 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6233 bnxt_reset(bp, true);
6234
Michael Chan4bb13ab2016-04-05 14:09:01 -04006235 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006236 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006237
Michael Chan3bdf56c2016-03-07 15:38:45 -05006238 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6239 bnxt_hwrm_port_qstats(bp);
6240
Michael Chan4cebdce2015-12-09 19:35:43 -05006241 smp_mb__before_atomic();
6242 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006243}
6244
6245static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6246{
6247 int rc;
6248 struct bnxt *bp = netdev_priv(dev);
6249
6250 SET_NETDEV_DEV(dev, &pdev->dev);
6251
6252 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6253 rc = pci_enable_device(pdev);
6254 if (rc) {
6255 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6256 goto init_err;
6257 }
6258
6259 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6260 dev_err(&pdev->dev,
6261 "Cannot find PCI device base address, aborting\n");
6262 rc = -ENODEV;
6263 goto init_err_disable;
6264 }
6265
6266 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6267 if (rc) {
6268 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6269 goto init_err_disable;
6270 }
6271
6272 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6273 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6274 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6275 goto init_err_disable;
6276 }
6277
6278 pci_set_master(pdev);
6279
6280 bp->dev = dev;
6281 bp->pdev = pdev;
6282
6283 bp->bar0 = pci_ioremap_bar(pdev, 0);
6284 if (!bp->bar0) {
6285 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6286 rc = -ENOMEM;
6287 goto init_err_release;
6288 }
6289
6290 bp->bar1 = pci_ioremap_bar(pdev, 2);
6291 if (!bp->bar1) {
6292 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6293 rc = -ENOMEM;
6294 goto init_err_release;
6295 }
6296
6297 bp->bar2 = pci_ioremap_bar(pdev, 4);
6298 if (!bp->bar2) {
6299 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6300 rc = -ENOMEM;
6301 goto init_err_release;
6302 }
6303
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006304 pci_enable_pcie_error_reporting(pdev);
6305
Michael Chanc0c050c2015-10-22 16:01:17 -04006306 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6307
6308 spin_lock_init(&bp->ntp_fltr_lock);
6309
6310 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6311 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6312
Michael Chandfb5b892016-02-26 04:00:01 -05006313 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006314 bp->rx_coal_ticks = 12;
6315 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006316 bp->rx_coal_ticks_irq = 1;
6317 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006318
Michael Chandfc9c942016-02-26 04:00:03 -05006319 bp->tx_coal_ticks = 25;
6320 bp->tx_coal_bufs = 30;
6321 bp->tx_coal_ticks_irq = 2;
6322 bp->tx_coal_bufs_irq = 2;
6323
Michael Chan51f30782016-07-01 18:46:29 -04006324 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6325
Michael Chanc0c050c2015-10-22 16:01:17 -04006326 init_timer(&bp->timer);
6327 bp->timer.data = (unsigned long)bp;
6328 bp->timer.function = bnxt_timer;
6329 bp->current_interval = BNXT_TIMER_INTERVAL;
6330
Michael Chancaefe522015-12-09 19:35:42 -05006331 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006332
6333 return 0;
6334
6335init_err_release:
6336 if (bp->bar2) {
6337 pci_iounmap(pdev, bp->bar2);
6338 bp->bar2 = NULL;
6339 }
6340
6341 if (bp->bar1) {
6342 pci_iounmap(pdev, bp->bar1);
6343 bp->bar1 = NULL;
6344 }
6345
6346 if (bp->bar0) {
6347 pci_iounmap(pdev, bp->bar0);
6348 bp->bar0 = NULL;
6349 }
6350
6351 pci_release_regions(pdev);
6352
6353init_err_disable:
6354 pci_disable_device(pdev);
6355
6356init_err:
6357 return rc;
6358}
6359
6360/* rtnl_lock held */
6361static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6362{
6363 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006364 struct bnxt *bp = netdev_priv(dev);
6365 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006366
6367 if (!is_valid_ether_addr(addr->sa_data))
6368 return -EADDRNOTAVAIL;
6369
Michael Chan84c33dd2016-04-11 04:11:13 -04006370 rc = bnxt_approve_mac(bp, addr->sa_data);
6371 if (rc)
6372 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006373
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006374 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6375 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006376
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006377 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6378 if (netif_running(dev)) {
6379 bnxt_close_nic(bp, false, false);
6380 rc = bnxt_open_nic(bp, false, false);
6381 }
6382
6383 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006384}
6385
6386/* rtnl_lock held */
6387static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6388{
6389 struct bnxt *bp = netdev_priv(dev);
6390
Michael Chanc0c050c2015-10-22 16:01:17 -04006391 if (netif_running(dev))
6392 bnxt_close_nic(bp, false, false);
6393
6394 dev->mtu = new_mtu;
6395 bnxt_set_ring_params(bp);
6396
6397 if (netif_running(dev))
6398 return bnxt_open_nic(bp, false, false);
6399
6400 return 0;
6401}
6402
Michael Chanc5e3deb2016-12-02 21:17:15 -05006403int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006404{
6405 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006406 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006407
Michael Chanc0c050c2015-10-22 16:01:17 -04006408 if (tc > bp->max_tc) {
6409 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6410 tc, bp->max_tc);
6411 return -EINVAL;
6412 }
6413
6414 if (netdev_get_num_tc(dev) == tc)
6415 return 0;
6416
Michael Chan3ffb6a32016-11-11 00:11:42 -05006417 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6418 sh = true;
6419
Michael Chanc0c050c2015-10-22 16:01:17 -04006420 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006421 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006422
6423 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006424 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006425 return -ENOMEM;
6426 }
6427
6428 /* Needs to close the device and do hw resource re-allocations */
6429 if (netif_running(bp->dev))
6430 bnxt_close_nic(bp, true, false);
6431
6432 if (tc) {
6433 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6434 netdev_set_num_tc(dev, tc);
6435 } else {
6436 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6437 netdev_reset_tc(dev);
6438 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006439 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6440 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006441 bp->num_stat_ctxs = bp->cp_nr_rings;
6442
6443 if (netif_running(bp->dev))
6444 return bnxt_open_nic(bp, true, false);
6445
6446 return 0;
6447}
6448
Michael Chanc5e3deb2016-12-02 21:17:15 -05006449static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6450 struct tc_to_netdev *ntc)
6451{
6452 if (ntc->type != TC_SETUP_MQPRIO)
6453 return -EINVAL;
6454
6455 return bnxt_setup_mq_tc(dev, ntc->tc);
6456}
6457
Michael Chanc0c050c2015-10-22 16:01:17 -04006458#ifdef CONFIG_RFS_ACCEL
6459static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6460 struct bnxt_ntuple_filter *f2)
6461{
6462 struct flow_keys *keys1 = &f1->fkeys;
6463 struct flow_keys *keys2 = &f2->fkeys;
6464
6465 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6466 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6467 keys1->ports.ports == keys2->ports.ports &&
6468 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6469 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006470 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6471 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006472 return true;
6473
6474 return false;
6475}
6476
6477static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6478 u16 rxq_index, u32 flow_id)
6479{
6480 struct bnxt *bp = netdev_priv(dev);
6481 struct bnxt_ntuple_filter *fltr, *new_fltr;
6482 struct flow_keys *fkeys;
6483 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006484 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006485 struct hlist_head *head;
6486
6487 if (skb->encapsulation)
6488 return -EPROTONOSUPPORT;
6489
Michael Chana54c4d72016-07-25 12:33:35 -04006490 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6491 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6492 int off = 0, j;
6493
6494 netif_addr_lock_bh(dev);
6495 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6496 if (ether_addr_equal(eth->h_dest,
6497 vnic->uc_list + off)) {
6498 l2_idx = j + 1;
6499 break;
6500 }
6501 }
6502 netif_addr_unlock_bh(dev);
6503 if (!l2_idx)
6504 return -EINVAL;
6505 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006506 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6507 if (!new_fltr)
6508 return -ENOMEM;
6509
6510 fkeys = &new_fltr->fkeys;
6511 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6512 rc = -EPROTONOSUPPORT;
6513 goto err_free;
6514 }
6515
6516 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6517 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6518 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6519 rc = -EPROTONOSUPPORT;
6520 goto err_free;
6521 }
6522
Michael Chana54c4d72016-07-25 12:33:35 -04006523 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006524 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6525
6526 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6527 head = &bp->ntp_fltr_hash_tbl[idx];
6528 rcu_read_lock();
6529 hlist_for_each_entry_rcu(fltr, head, hash) {
6530 if (bnxt_fltr_match(fltr, new_fltr)) {
6531 rcu_read_unlock();
6532 rc = 0;
6533 goto err_free;
6534 }
6535 }
6536 rcu_read_unlock();
6537
6538 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006539 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6540 BNXT_NTP_FLTR_MAX_FLTR, 0);
6541 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006542 spin_unlock_bh(&bp->ntp_fltr_lock);
6543 rc = -ENOMEM;
6544 goto err_free;
6545 }
6546
Michael Chan84e86b92015-11-05 16:25:50 -05006547 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006548 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006549 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006550 new_fltr->rxq = rxq_index;
6551 hlist_add_head_rcu(&new_fltr->hash, head);
6552 bp->ntp_fltr_count++;
6553 spin_unlock_bh(&bp->ntp_fltr_lock);
6554
6555 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6556 schedule_work(&bp->sp_task);
6557
6558 return new_fltr->sw_id;
6559
6560err_free:
6561 kfree(new_fltr);
6562 return rc;
6563}
6564
6565static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6566{
6567 int i;
6568
6569 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6570 struct hlist_head *head;
6571 struct hlist_node *tmp;
6572 struct bnxt_ntuple_filter *fltr;
6573 int rc;
6574
6575 head = &bp->ntp_fltr_hash_tbl[i];
6576 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6577 bool del = false;
6578
6579 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6580 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6581 fltr->flow_id,
6582 fltr->sw_id)) {
6583 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6584 fltr);
6585 del = true;
6586 }
6587 } else {
6588 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6589 fltr);
6590 if (rc)
6591 del = true;
6592 else
6593 set_bit(BNXT_FLTR_VALID, &fltr->state);
6594 }
6595
6596 if (del) {
6597 spin_lock_bh(&bp->ntp_fltr_lock);
6598 hlist_del_rcu(&fltr->hash);
6599 bp->ntp_fltr_count--;
6600 spin_unlock_bh(&bp->ntp_fltr_lock);
6601 synchronize_rcu();
6602 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6603 kfree(fltr);
6604 }
6605 }
6606 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006607 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6608 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006609}
6610
6611#else
6612
6613static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6614{
6615}
6616
6617#endif /* CONFIG_RFS_ACCEL */
6618
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006619static void bnxt_udp_tunnel_add(struct net_device *dev,
6620 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006621{
6622 struct bnxt *bp = netdev_priv(dev);
6623
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006624 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6625 return;
6626
Michael Chanc0c050c2015-10-22 16:01:17 -04006627 if (!netif_running(dev))
6628 return;
6629
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006630 switch (ti->type) {
6631 case UDP_TUNNEL_TYPE_VXLAN:
6632 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6633 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006634
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006635 bp->vxlan_port_cnt++;
6636 if (bp->vxlan_port_cnt == 1) {
6637 bp->vxlan_port = ti->port;
6638 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006639 schedule_work(&bp->sp_task);
6640 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006641 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006642 case UDP_TUNNEL_TYPE_GENEVE:
6643 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6644 return;
6645
6646 bp->nge_port_cnt++;
6647 if (bp->nge_port_cnt == 1) {
6648 bp->nge_port = ti->port;
6649 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6650 }
6651 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006652 default:
6653 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006654 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006655
6656 schedule_work(&bp->sp_task);
6657}
6658
6659static void bnxt_udp_tunnel_del(struct net_device *dev,
6660 struct udp_tunnel_info *ti)
6661{
6662 struct bnxt *bp = netdev_priv(dev);
6663
6664 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6665 return;
6666
6667 if (!netif_running(dev))
6668 return;
6669
6670 switch (ti->type) {
6671 case UDP_TUNNEL_TYPE_VXLAN:
6672 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6673 return;
6674 bp->vxlan_port_cnt--;
6675
6676 if (bp->vxlan_port_cnt != 0)
6677 return;
6678
6679 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6680 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006681 case UDP_TUNNEL_TYPE_GENEVE:
6682 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6683 return;
6684 bp->nge_port_cnt--;
6685
6686 if (bp->nge_port_cnt != 0)
6687 return;
6688
6689 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6690 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006691 default:
6692 return;
6693 }
6694
6695 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006696}
6697
6698static const struct net_device_ops bnxt_netdev_ops = {
6699 .ndo_open = bnxt_open,
6700 .ndo_start_xmit = bnxt_start_xmit,
6701 .ndo_stop = bnxt_close,
6702 .ndo_get_stats64 = bnxt_get_stats64,
6703 .ndo_set_rx_mode = bnxt_set_rx_mode,
6704 .ndo_do_ioctl = bnxt_ioctl,
6705 .ndo_validate_addr = eth_validate_addr,
6706 .ndo_set_mac_address = bnxt_change_mac_addr,
6707 .ndo_change_mtu = bnxt_change_mtu,
6708 .ndo_fix_features = bnxt_fix_features,
6709 .ndo_set_features = bnxt_set_features,
6710 .ndo_tx_timeout = bnxt_tx_timeout,
6711#ifdef CONFIG_BNXT_SRIOV
6712 .ndo_get_vf_config = bnxt_get_vf_config,
6713 .ndo_set_vf_mac = bnxt_set_vf_mac,
6714 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6715 .ndo_set_vf_rate = bnxt_set_vf_bw,
6716 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6717 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6718#endif
6719#ifdef CONFIG_NET_POLL_CONTROLLER
6720 .ndo_poll_controller = bnxt_poll_controller,
6721#endif
6722 .ndo_setup_tc = bnxt_setup_tc,
6723#ifdef CONFIG_RFS_ACCEL
6724 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6725#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006726 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6727 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006728};
6729
6730static void bnxt_remove_one(struct pci_dev *pdev)
6731{
6732 struct net_device *dev = pci_get_drvdata(pdev);
6733 struct bnxt *bp = netdev_priv(dev);
6734
6735 if (BNXT_PF(bp))
6736 bnxt_sriov_disable(bp);
6737
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006738 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006739 unregister_netdev(dev);
6740 cancel_work_sync(&bp->sp_task);
6741 bp->sp_event = 0;
6742
Michael Chan78095922016-12-07 00:26:16 -05006743 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006744 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006745 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05006746 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006747 pci_iounmap(pdev, bp->bar2);
6748 pci_iounmap(pdev, bp->bar1);
6749 pci_iounmap(pdev, bp->bar0);
Michael Chana588e452016-12-07 00:26:21 -05006750 kfree(bp->edev);
6751 bp->edev = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04006752 free_netdev(dev);
6753
6754 pci_release_regions(pdev);
6755 pci_disable_device(pdev);
6756}
6757
6758static int bnxt_probe_phy(struct bnxt *bp)
6759{
6760 int rc = 0;
6761 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006762
Michael Chan170ce012016-04-05 14:08:57 -04006763 rc = bnxt_hwrm_phy_qcaps(bp);
6764 if (rc) {
6765 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6766 rc);
6767 return rc;
6768 }
6769
Michael Chanc0c050c2015-10-22 16:01:17 -04006770 rc = bnxt_update_link(bp, false);
6771 if (rc) {
6772 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6773 rc);
6774 return rc;
6775 }
6776
Michael Chan93ed8112016-06-13 02:25:37 -04006777 /* Older firmware does not have supported_auto_speeds, so assume
6778 * that all supported speeds can be autonegotiated.
6779 */
6780 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6781 link_info->support_auto_speeds = link_info->support_speeds;
6782
Michael Chanc0c050c2015-10-22 16:01:17 -04006783 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006784 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006785 link_info->autoneg = BNXT_AUTONEG_SPEED;
6786 if (bp->hwrm_spec_code >= 0x10201) {
6787 if (link_info->auto_pause_setting &
6788 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6789 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6790 } else {
6791 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6792 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006793 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006794 } else {
6795 link_info->req_link_speed = link_info->force_link_speed;
6796 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006797 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006798 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6799 link_info->req_flow_ctrl =
6800 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6801 else
6802 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006803 return rc;
6804}
6805
6806static int bnxt_get_max_irq(struct pci_dev *pdev)
6807{
6808 u16 ctrl;
6809
6810 if (!pdev->msix_cap)
6811 return 1;
6812
6813 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6814 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6815}
6816
Michael Chan6e6c5a52016-01-02 23:45:02 -05006817static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6818 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006819{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006820 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006821
Michael Chan379a80a2015-10-23 15:06:19 -04006822#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006823 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006824 *max_tx = bp->vf.max_tx_rings;
6825 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006826 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6827 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006828 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006829 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006830#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006831 {
6832 *max_tx = bp->pf.max_tx_rings;
6833 *max_rx = bp->pf.max_rx_rings;
6834 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6835 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6836 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006837 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006838 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6839 *max_cp -= 1;
6840 *max_rx -= 2;
6841 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006842 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6843 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006844 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006845}
6846
6847int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6848{
6849 int rx, tx, cp;
6850
6851 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6852 if (!rx || !tx || !cp)
6853 return -ENOMEM;
6854
6855 *max_rx = rx;
6856 *max_tx = tx;
6857 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6858}
6859
Michael Chane4060d32016-12-07 00:26:19 -05006860static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6861 bool shared)
6862{
6863 int rc;
6864
6865 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
6866 if (rc)
6867 return rc;
6868
6869 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
6870 int max_cp, max_stat, max_irq;
6871
6872 /* Reserve minimum resources for RoCE */
6873 max_cp = bnxt_get_max_func_cp_rings(bp);
6874 max_stat = bnxt_get_max_func_stat_ctxs(bp);
6875 max_irq = bnxt_get_max_func_irqs(bp);
6876 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
6877 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
6878 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
6879 return 0;
6880
6881 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
6882 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
6883 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
6884 max_cp = min_t(int, max_cp, max_irq);
6885 max_cp = min_t(int, max_cp, max_stat);
6886 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
6887 if (rc)
6888 rc = 0;
6889 }
6890 return rc;
6891}
6892
Michael Chan6e6c5a52016-01-02 23:45:02 -05006893static int bnxt_set_dflt_rings(struct bnxt *bp)
6894{
6895 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6896 bool sh = true;
6897
6898 if (sh)
6899 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6900 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05006901 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006902 if (rc)
6903 return rc;
6904 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6905 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6906 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6907 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6908 bp->tx_nr_rings + bp->rx_nr_rings;
6909 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006910 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6911 bp->rx_nr_rings++;
6912 bp->cp_nr_rings++;
6913 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006914 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006915}
6916
Michael Chan7b08f662016-12-07 00:26:18 -05006917void bnxt_restore_pf_fw_resources(struct bnxt *bp)
6918{
6919 ASSERT_RTNL();
6920 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05006921 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05006922}
6923
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006924static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6925{
6926 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6927 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6928
6929 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6930 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6931 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6932 else
6933 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6934 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6935 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6936 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6937 "Unknown", width);
6938}
6939
Michael Chanc0c050c2015-10-22 16:01:17 -04006940static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6941{
6942 static int version_printed;
6943 struct net_device *dev;
6944 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006945 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006946
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04006947 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6948 return -ENODEV;
6949
Michael Chanc0c050c2015-10-22 16:01:17 -04006950 if (version_printed++ == 0)
6951 pr_info("%s", version);
6952
6953 max_irqs = bnxt_get_max_irq(pdev);
6954 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6955 if (!dev)
6956 return -ENOMEM;
6957
6958 bp = netdev_priv(dev);
6959
6960 if (bnxt_vf_pciid(ent->driver_data))
6961 bp->flags |= BNXT_FLAG_VF;
6962
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006963 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006964 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006965
6966 rc = bnxt_init_board(pdev, dev);
6967 if (rc < 0)
6968 goto init_err_free;
6969
6970 dev->netdev_ops = &bnxt_netdev_ops;
6971 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6972 dev->ethtool_ops = &bnxt_ethtool_ops;
6973
6974 pci_set_drvdata(pdev, dev);
6975
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006976 rc = bnxt_alloc_hwrm_resources(bp);
6977 if (rc)
6978 goto init_err;
6979
6980 mutex_init(&bp->hwrm_cmd_lock);
6981 rc = bnxt_hwrm_ver_get(bp);
6982 if (rc)
6983 goto init_err;
6984
Rob Swindell5ac67d82016-09-19 03:58:03 -04006985 bnxt_hwrm_fw_set_time(bp);
6986
Michael Chanc0c050c2015-10-22 16:01:17 -04006987 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6988 NETIF_F_TSO | NETIF_F_TSO6 |
6989 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07006990 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07006991 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6992 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006993 NETIF_F_RXCSUM | NETIF_F_GRO;
6994
6995 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6996 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04006997
Michael Chanc0c050c2015-10-22 16:01:17 -04006998 dev->hw_enc_features =
6999 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7000 NETIF_F_TSO | NETIF_F_TSO6 |
7001 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007002 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007003 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007004 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7005 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007006 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7007 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7008 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7009 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7010 dev->priv_flags |= IFF_UNICAST_FLT;
7011
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007012 /* MTU range: 60 - 9500 */
7013 dev->min_mtu = ETH_ZLEN;
7014 dev->max_mtu = 9500;
7015
Michael Chan7df4ae92016-12-02 21:17:17 -05007016 bnxt_dcb_init(bp);
7017
Michael Chanc0c050c2015-10-22 16:01:17 -04007018#ifdef CONFIG_BNXT_SRIOV
7019 init_waitqueue_head(&bp->sriov_cfg_wait);
7020#endif
Michael Chan309369c2016-06-13 02:25:34 -04007021 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007022 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7023 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007024
Michael Chanc0c050c2015-10-22 16:01:17 -04007025 rc = bnxt_hwrm_func_drv_rgtr(bp);
7026 if (rc)
7027 goto init_err;
7028
Michael Chana1653b12016-12-07 00:26:20 -05007029 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7030 if (rc)
7031 goto init_err;
7032
Michael Chana588e452016-12-07 00:26:21 -05007033 bp->ulp_probe = bnxt_ulp_probe;
7034
Michael Chanc0c050c2015-10-22 16:01:17 -04007035 /* Get the MAX capabilities for this function */
7036 rc = bnxt_hwrm_func_qcaps(bp);
7037 if (rc) {
7038 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7039 rc);
7040 rc = -1;
7041 goto init_err;
7042 }
7043
7044 rc = bnxt_hwrm_queue_qportcfg(bp);
7045 if (rc) {
7046 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7047 rc);
7048 rc = -1;
7049 goto init_err;
7050 }
7051
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007052 bnxt_hwrm_func_qcfg(bp);
7053
Michael Chanc0c050c2015-10-22 16:01:17 -04007054 bnxt_set_tpa_flags(bp);
7055 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007056 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007057 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007058
Michael Chan87da7f72016-11-16 21:13:09 -05007059 /* Default RSS hash cfg. */
7060 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7061 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7062 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7063 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7064 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7065 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7066 bp->hwrm_spec_code >= 0x10501) {
7067 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7068 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7069 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7070 }
7071
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007072 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007073 dev->hw_features |= NETIF_F_NTUPLE;
7074 if (bnxt_rfs_capable(bp)) {
7075 bp->flags |= BNXT_FLAG_RFS;
7076 dev->features |= NETIF_F_NTUPLE;
7077 }
7078 }
7079
Michael Chanc0c050c2015-10-22 16:01:17 -04007080 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7081 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7082
7083 rc = bnxt_probe_phy(bp);
7084 if (rc)
7085 goto init_err;
7086
Michael Chanaa8ed022016-12-07 00:26:17 -05007087 rc = bnxt_hwrm_func_reset(bp);
7088 if (rc)
7089 goto init_err;
7090
Michael Chan78095922016-12-07 00:26:16 -05007091 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007092 if (rc)
7093 goto init_err;
7094
Michael Chan78095922016-12-07 00:26:16 -05007095 rc = register_netdev(dev);
7096 if (rc)
7097 goto init_err_clr_int;
7098
Michael Chanc0c050c2015-10-22 16:01:17 -04007099 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7100 board_info[ent->driver_data].name,
7101 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7102
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007103 bnxt_parse_log_pcie_link(bp);
7104
Michael Chanc0c050c2015-10-22 16:01:17 -04007105 return 0;
7106
Michael Chan78095922016-12-07 00:26:16 -05007107init_err_clr_int:
7108 bnxt_clear_int_mode(bp);
7109
Michael Chanc0c050c2015-10-22 16:01:17 -04007110init_err:
7111 pci_iounmap(pdev, bp->bar0);
7112 pci_release_regions(pdev);
7113 pci_disable_device(pdev);
7114
7115init_err_free:
7116 free_netdev(dev);
7117 return rc;
7118}
7119
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007120/**
7121 * bnxt_io_error_detected - called when PCI error is detected
7122 * @pdev: Pointer to PCI device
7123 * @state: The current pci connection state
7124 *
7125 * This function is called after a PCI bus error affecting
7126 * this device has been detected.
7127 */
7128static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7129 pci_channel_state_t state)
7130{
7131 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007132 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007133
7134 netdev_info(netdev, "PCI I/O error detected\n");
7135
7136 rtnl_lock();
7137 netif_device_detach(netdev);
7138
Michael Chana588e452016-12-07 00:26:21 -05007139 bnxt_ulp_stop(bp);
7140
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007141 if (state == pci_channel_io_perm_failure) {
7142 rtnl_unlock();
7143 return PCI_ERS_RESULT_DISCONNECT;
7144 }
7145
7146 if (netif_running(netdev))
7147 bnxt_close(netdev);
7148
7149 pci_disable_device(pdev);
7150 rtnl_unlock();
7151
7152 /* Request a slot slot reset. */
7153 return PCI_ERS_RESULT_NEED_RESET;
7154}
7155
7156/**
7157 * bnxt_io_slot_reset - called after the pci bus has been reset.
7158 * @pdev: Pointer to PCI device
7159 *
7160 * Restart the card from scratch, as if from a cold-boot.
7161 * At this point, the card has exprienced a hard reset,
7162 * followed by fixups by BIOS, and has its config space
7163 * set up identically to what it was at cold boot.
7164 */
7165static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7166{
7167 struct net_device *netdev = pci_get_drvdata(pdev);
7168 struct bnxt *bp = netdev_priv(netdev);
7169 int err = 0;
7170 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7171
7172 netdev_info(bp->dev, "PCI Slot Reset\n");
7173
7174 rtnl_lock();
7175
7176 if (pci_enable_device(pdev)) {
7177 dev_err(&pdev->dev,
7178 "Cannot re-enable PCI device after reset.\n");
7179 } else {
7180 pci_set_master(pdev);
7181
Michael Chanaa8ed022016-12-07 00:26:17 -05007182 err = bnxt_hwrm_func_reset(bp);
7183 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007184 err = bnxt_open(netdev);
7185
Michael Chana588e452016-12-07 00:26:21 -05007186 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007187 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007188 bnxt_ulp_start(bp);
7189 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007190 }
7191
7192 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7193 dev_close(netdev);
7194
7195 rtnl_unlock();
7196
7197 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7198 if (err) {
7199 dev_err(&pdev->dev,
7200 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7201 err); /* non-fatal, continue */
7202 }
7203
7204 return PCI_ERS_RESULT_RECOVERED;
7205}
7206
7207/**
7208 * bnxt_io_resume - called when traffic can start flowing again.
7209 * @pdev: Pointer to PCI device
7210 *
7211 * This callback is called when the error recovery driver tells
7212 * us that its OK to resume normal operation.
7213 */
7214static void bnxt_io_resume(struct pci_dev *pdev)
7215{
7216 struct net_device *netdev = pci_get_drvdata(pdev);
7217
7218 rtnl_lock();
7219
7220 netif_device_attach(netdev);
7221
7222 rtnl_unlock();
7223}
7224
7225static const struct pci_error_handlers bnxt_err_handler = {
7226 .error_detected = bnxt_io_error_detected,
7227 .slot_reset = bnxt_io_slot_reset,
7228 .resume = bnxt_io_resume
7229};
7230
Michael Chanc0c050c2015-10-22 16:01:17 -04007231static struct pci_driver bnxt_pci_driver = {
7232 .name = DRV_MODULE_NAME,
7233 .id_table = bnxt_pci_tbl,
7234 .probe = bnxt_init_one,
7235 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007236 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007237#if defined(CONFIG_BNXT_SRIOV)
7238 .sriov_configure = bnxt_sriov_configure,
7239#endif
7240};
7241
7242module_pci_driver(bnxt_pci_driver);