blob: 32c808fa4fef5cfe73f976b54841270367035835 [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
Michael Chanc61fb992017-02-06 16:55:36 -0500576static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 struct device *dev = &bp->pdev->dev;
580 struct page *page;
581
582 page = alloc_page(gfp);
583 if (!page)
584 return NULL;
585
586 *mapping = dma_map_page(dev, page, 0, PAGE_SIZE, bp->rx_dir);
587 if (dma_mapping_error(dev, *mapping)) {
588 __free_page(page);
589 return NULL;
590 }
591 *mapping += bp->rx_dma_offset;
592 return page;
593}
594
Michael Chanc0c050c2015-10-22 16:01:17 -0400595static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
596 gfp_t gfp)
597{
598 u8 *data;
599 struct pci_dev *pdev = bp->pdev;
600
601 data = kmalloc(bp->rx_buf_size, gfp);
602 if (!data)
603 return NULL;
604
Michael Chanb3dba772017-02-06 16:55:35 -0500605 *mapping = dma_map_single(&pdev->dev, data + bp->rx_dma_offset,
Michael Chan745fc052017-02-06 16:55:34 -0500606 bp->rx_buf_use_size, bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400607
608 if (dma_mapping_error(&pdev->dev, *mapping)) {
609 kfree(data);
610 data = NULL;
611 }
612 return data;
613}
614
615static inline int bnxt_alloc_rx_data(struct bnxt *bp,
616 struct bnxt_rx_ring_info *rxr,
617 u16 prod, gfp_t gfp)
618{
619 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
620 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
Michael Chanc0c050c2015-10-22 16:01:17 -0400621 dma_addr_t mapping;
622
Michael Chanc61fb992017-02-06 16:55:36 -0500623 if (BNXT_RX_PAGE_MODE(bp)) {
624 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
Michael Chanc0c050c2015-10-22 16:01:17 -0400625
Michael Chanc61fb992017-02-06 16:55:36 -0500626 if (!page)
627 return -ENOMEM;
628
629 rx_buf->data = page;
630 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
631 } else {
632 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
633
634 if (!data)
635 return -ENOMEM;
636
637 rx_buf->data = data;
638 rx_buf->data_ptr = data + bp->rx_offset;
639 }
Michael Chan11cd1192017-02-06 16:55:33 -0500640 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400641
642 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -0400643 return 0;
644}
645
646static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500647 void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400648{
649 u16 prod = rxr->rx_prod;
650 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
651 struct rx_bd *cons_bd, *prod_bd;
652
653 prod_rx_buf = &rxr->rx_buf_ring[prod];
654 cons_rx_buf = &rxr->rx_buf_ring[cons];
655
656 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500657 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400658
Michael Chan11cd1192017-02-06 16:55:33 -0500659 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400660
661 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
662 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
663
664 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
665}
666
667static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
668{
669 u16 next, max = rxr->rx_agg_bmap_size;
670
671 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
672 if (next >= max)
673 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
674 return next;
675}
676
677static inline int bnxt_alloc_rx_page(struct bnxt *bp,
678 struct bnxt_rx_ring_info *rxr,
679 u16 prod, gfp_t gfp)
680{
681 struct rx_bd *rxbd =
682 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
683 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
684 struct pci_dev *pdev = bp->pdev;
685 struct page *page;
686 dma_addr_t mapping;
687 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400688 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400689
Michael Chan89d0a062016-04-25 02:30:51 -0400690 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
691 page = rxr->rx_page;
692 if (!page) {
693 page = alloc_page(gfp);
694 if (!page)
695 return -ENOMEM;
696 rxr->rx_page = page;
697 rxr->rx_page_offset = 0;
698 }
699 offset = rxr->rx_page_offset;
700 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
701 if (rxr->rx_page_offset == PAGE_SIZE)
702 rxr->rx_page = NULL;
703 else
704 get_page(page);
705 } else {
706 page = alloc_page(gfp);
707 if (!page)
708 return -ENOMEM;
709 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400710
Michael Chan89d0a062016-04-25 02:30:51 -0400711 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400712 PCI_DMA_FROMDEVICE);
713 if (dma_mapping_error(&pdev->dev, mapping)) {
714 __free_page(page);
715 return -EIO;
716 }
717
718 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
719 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
720
721 __set_bit(sw_prod, rxr->rx_agg_bmap);
722 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
723 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
724
725 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400726 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400727 rx_agg_buf->mapping = mapping;
728 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
729 rxbd->rx_bd_opaque = sw_prod;
730 return 0;
731}
732
733static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
734 u32 agg_bufs)
735{
736 struct bnxt *bp = bnapi->bp;
737 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500738 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400739 u16 prod = rxr->rx_agg_prod;
740 u16 sw_prod = rxr->rx_sw_agg_prod;
741 u32 i;
742
743 for (i = 0; i < agg_bufs; i++) {
744 u16 cons;
745 struct rx_agg_cmp *agg;
746 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
747 struct rx_bd *prod_bd;
748 struct page *page;
749
750 agg = (struct rx_agg_cmp *)
751 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
752 cons = agg->rx_agg_cmp_opaque;
753 __clear_bit(cons, rxr->rx_agg_bmap);
754
755 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
756 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
757
758 __set_bit(sw_prod, rxr->rx_agg_bmap);
759 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
760 cons_rx_buf = &rxr->rx_agg_ring[cons];
761
762 /* It is possible for sw_prod to be equal to cons, so
763 * set cons_rx_buf->page to NULL first.
764 */
765 page = cons_rx_buf->page;
766 cons_rx_buf->page = NULL;
767 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400768 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400769
770 prod_rx_buf->mapping = cons_rx_buf->mapping;
771
772 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
773
774 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
775 prod_bd->rx_bd_opaque = sw_prod;
776
777 prod = NEXT_RX_AGG(prod);
778 sw_prod = NEXT_RX_AGG(sw_prod);
779 cp_cons = NEXT_CMP(cp_cons);
780 }
781 rxr->rx_agg_prod = prod;
782 rxr->rx_sw_agg_prod = sw_prod;
783}
784
Michael Chanc61fb992017-02-06 16:55:36 -0500785static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
786 struct bnxt_rx_ring_info *rxr,
787 u16 cons, void *data, u8 *data_ptr,
788 dma_addr_t dma_addr,
789 unsigned int offset_and_len)
790{
791 unsigned int payload = offset_and_len >> 16;
792 unsigned int len = offset_and_len & 0xffff;
793 struct skb_frag_struct *frag;
794 struct page *page = data;
795 u16 prod = rxr->rx_prod;
796 struct sk_buff *skb;
797 int off, err;
798
799 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
800 if (unlikely(err)) {
801 bnxt_reuse_rx_data(rxr, cons, data);
802 return NULL;
803 }
804 dma_addr -= bp->rx_dma_offset;
805 dma_unmap_page(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir);
806
807 if (unlikely(!payload))
808 payload = eth_get_headlen(data_ptr, len);
809
810 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
811 if (!skb) {
812 __free_page(page);
813 return NULL;
814 }
815
816 off = (void *)data_ptr - page_address(page);
817 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
818 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
819 payload + NET_IP_ALIGN);
820
821 frag = &skb_shinfo(skb)->frags[0];
822 skb_frag_size_sub(frag, payload);
823 frag->page_offset += payload;
824 skb->data_len -= payload;
825 skb->tail += payload;
826
827 return skb;
828}
829
Michael Chanc0c050c2015-10-22 16:01:17 -0400830static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
831 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500832 void *data, u8 *data_ptr,
833 dma_addr_t dma_addr,
834 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400835{
Michael Chan6bb19472017-02-06 16:55:32 -0500836 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400837 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500838 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400839
840 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
841 if (unlikely(err)) {
842 bnxt_reuse_rx_data(rxr, cons, data);
843 return NULL;
844 }
845
846 skb = build_skb(data, 0);
847 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
Michael Chan745fc052017-02-06 16:55:34 -0500848 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400849 if (!skb) {
850 kfree(data);
851 return NULL;
852 }
853
Michael Chanb3dba772017-02-06 16:55:35 -0500854 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500855 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400856 return skb;
857}
858
859static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
860 struct sk_buff *skb, u16 cp_cons,
861 u32 agg_bufs)
862{
863 struct pci_dev *pdev = bp->pdev;
864 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500865 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400866 u16 prod = rxr->rx_agg_prod;
867 u32 i;
868
869 for (i = 0; i < agg_bufs; i++) {
870 u16 cons, frag_len;
871 struct rx_agg_cmp *agg;
872 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
873 struct page *page;
874 dma_addr_t mapping;
875
876 agg = (struct rx_agg_cmp *)
877 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
878 cons = agg->rx_agg_cmp_opaque;
879 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
880 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
881
882 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400883 skb_fill_page_desc(skb, i, cons_rx_buf->page,
884 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400885 __clear_bit(cons, rxr->rx_agg_bmap);
886
887 /* It is possible for bnxt_alloc_rx_page() to allocate
888 * a sw_prod index that equals the cons index, so we
889 * need to clear the cons entry now.
890 */
Michael Chan11cd1192017-02-06 16:55:33 -0500891 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400892 page = cons_rx_buf->page;
893 cons_rx_buf->page = NULL;
894
895 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
896 struct skb_shared_info *shinfo;
897 unsigned int nr_frags;
898
899 shinfo = skb_shinfo(skb);
900 nr_frags = --shinfo->nr_frags;
901 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
902
903 dev_kfree_skb(skb);
904
905 cons_rx_buf->page = page;
906
907 /* Update prod since possibly some pages have been
908 * allocated already.
909 */
910 rxr->rx_agg_prod = prod;
911 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
912 return NULL;
913 }
914
Michael Chan2839f282016-04-25 02:30:50 -0400915 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400916 PCI_DMA_FROMDEVICE);
917
918 skb->data_len += frag_len;
919 skb->len += frag_len;
920 skb->truesize += PAGE_SIZE;
921
922 prod = NEXT_RX_AGG(prod);
923 cp_cons = NEXT_CMP(cp_cons);
924 }
925 rxr->rx_agg_prod = prod;
926 return skb;
927}
928
929static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
930 u8 agg_bufs, u32 *raw_cons)
931{
932 u16 last;
933 struct rx_agg_cmp *agg;
934
935 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
936 last = RING_CMP(*raw_cons);
937 agg = (struct rx_agg_cmp *)
938 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
939 return RX_AGG_CMP_VALID(agg, *raw_cons);
940}
941
942static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
943 unsigned int len,
944 dma_addr_t mapping)
945{
946 struct bnxt *bp = bnapi->bp;
947 struct pci_dev *pdev = bp->pdev;
948 struct sk_buff *skb;
949
950 skb = napi_alloc_skb(&bnapi->napi, len);
951 if (!skb)
952 return NULL;
953
Michael Chan745fc052017-02-06 16:55:34 -0500954 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
955 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400956
Michael Chan6bb19472017-02-06 16:55:32 -0500957 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
958 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400959
Michael Chan745fc052017-02-06 16:55:34 -0500960 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
961 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400962
963 skb_put(skb, len);
964 return skb;
965}
966
Michael Chanfa7e2812016-05-10 19:18:00 -0400967static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
968 u32 *raw_cons, void *cmp)
969{
970 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
971 struct rx_cmp *rxcmp = cmp;
972 u32 tmp_raw_cons = *raw_cons;
973 u8 cmp_type, agg_bufs = 0;
974
975 cmp_type = RX_CMP_TYPE(rxcmp);
976
977 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
978 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
979 RX_CMP_AGG_BUFS) >>
980 RX_CMP_AGG_BUFS_SHIFT;
981 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
982 struct rx_tpa_end_cmp *tpa_end = cmp;
983
984 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
985 RX_TPA_END_CMP_AGG_BUFS) >>
986 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
987 }
988
989 if (agg_bufs) {
990 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
991 return -EBUSY;
992 }
993 *raw_cons = tmp_raw_cons;
994 return 0;
995}
996
997static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
998{
999 if (!rxr->bnapi->in_reset) {
1000 rxr->bnapi->in_reset = true;
1001 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1002 schedule_work(&bp->sp_task);
1003 }
1004 rxr->rx_next_cons = 0xffff;
1005}
1006
Michael Chanc0c050c2015-10-22 16:01:17 -04001007static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1008 struct rx_tpa_start_cmp *tpa_start,
1009 struct rx_tpa_start_cmp_ext *tpa_start1)
1010{
1011 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1012 u16 cons, prod;
1013 struct bnxt_tpa_info *tpa_info;
1014 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1015 struct rx_bd *prod_bd;
1016 dma_addr_t mapping;
1017
1018 cons = tpa_start->rx_tpa_start_cmp_opaque;
1019 prod = rxr->rx_prod;
1020 cons_rx_buf = &rxr->rx_buf_ring[cons];
1021 prod_rx_buf = &rxr->rx_buf_ring[prod];
1022 tpa_info = &rxr->rx_tpa[agg_id];
1023
Michael Chanfa7e2812016-05-10 19:18:00 -04001024 if (unlikely(cons != rxr->rx_next_cons)) {
1025 bnxt_sched_reset(bp, rxr);
1026 return;
1027 }
1028
Michael Chanc0c050c2015-10-22 16:01:17 -04001029 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001030 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001031
1032 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -05001033 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001034
1035 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1036
1037 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1038
1039 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001040 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -04001041 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -05001042 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001043
1044 tpa_info->len =
1045 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1046 RX_TPA_START_CMP_LEN_SHIFT;
1047 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1048 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1049
1050 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1051 tpa_info->gso_type = SKB_GSO_TCPV4;
1052 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1053 if (hash_type == 3)
1054 tpa_info->gso_type = SKB_GSO_TCPV6;
1055 tpa_info->rss_hash =
1056 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1057 } else {
1058 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1059 tpa_info->gso_type = 0;
1060 if (netif_msg_rx_err(bp))
1061 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1062 }
1063 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1064 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -04001065 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -04001066
1067 rxr->rx_prod = NEXT_RX(prod);
1068 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -04001069 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001070 cons_rx_buf = &rxr->rx_buf_ring[cons];
1071
1072 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1073 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1074 cons_rx_buf->data = NULL;
1075}
1076
1077static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1078 u16 cp_cons, u32 agg_bufs)
1079{
1080 if (agg_bufs)
1081 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1082}
1083
Michael Chan94758f82016-06-13 02:25:35 -04001084static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1085 int payload_off, int tcp_ts,
1086 struct sk_buff *skb)
1087{
1088#ifdef CONFIG_INET
1089 struct tcphdr *th;
1090 int len, nw_off;
1091 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1092 u32 hdr_info = tpa_info->hdr_info;
1093 bool loopback = false;
1094
1095 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1096 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1097 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1098
1099 /* If the packet is an internal loopback packet, the offsets will
1100 * have an extra 4 bytes.
1101 */
1102 if (inner_mac_off == 4) {
1103 loopback = true;
1104 } else if (inner_mac_off > 4) {
1105 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1106 ETH_HLEN - 2));
1107
1108 /* We only support inner iPv4/ipv6. If we don't see the
1109 * correct protocol ID, it must be a loopback packet where
1110 * the offsets are off by 4.
1111 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001112 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001113 loopback = true;
1114 }
1115 if (loopback) {
1116 /* internal loopback packet, subtract all offsets by 4 */
1117 inner_ip_off -= 4;
1118 inner_mac_off -= 4;
1119 outer_ip_off -= 4;
1120 }
1121
1122 nw_off = inner_ip_off - ETH_HLEN;
1123 skb_set_network_header(skb, nw_off);
1124 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1125 struct ipv6hdr *iph = ipv6_hdr(skb);
1126
1127 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1128 len = skb->len - skb_transport_offset(skb);
1129 th = tcp_hdr(skb);
1130 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1131 } else {
1132 struct iphdr *iph = ip_hdr(skb);
1133
1134 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1135 len = skb->len - skb_transport_offset(skb);
1136 th = tcp_hdr(skb);
1137 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1138 }
1139
1140 if (inner_mac_off) { /* tunnel */
1141 struct udphdr *uh = NULL;
1142 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1143 ETH_HLEN - 2));
1144
1145 if (proto == htons(ETH_P_IP)) {
1146 struct iphdr *iph = (struct iphdr *)skb->data;
1147
1148 if (iph->protocol == IPPROTO_UDP)
1149 uh = (struct udphdr *)(iph + 1);
1150 } else {
1151 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1152
1153 if (iph->nexthdr == IPPROTO_UDP)
1154 uh = (struct udphdr *)(iph + 1);
1155 }
1156 if (uh) {
1157 if (uh->check)
1158 skb_shinfo(skb)->gso_type |=
1159 SKB_GSO_UDP_TUNNEL_CSUM;
1160 else
1161 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1162 }
1163 }
1164#endif
1165 return skb;
1166}
1167
Michael Chanc0c050c2015-10-22 16:01:17 -04001168#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1169#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1170
Michael Chan309369c2016-06-13 02:25:34 -04001171static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1172 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001173 struct sk_buff *skb)
1174{
Michael Chand1611c32015-10-25 22:27:57 -04001175#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001176 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001177 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001178
Michael Chan309369c2016-06-13 02:25:34 -04001179 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001180 tcp_opt_len = 12;
1181
Michael Chanc0c050c2015-10-22 16:01:17 -04001182 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1183 struct iphdr *iph;
1184
1185 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1186 ETH_HLEN;
1187 skb_set_network_header(skb, nw_off);
1188 iph = ip_hdr(skb);
1189 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1190 len = skb->len - skb_transport_offset(skb);
1191 th = tcp_hdr(skb);
1192 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1193 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1194 struct ipv6hdr *iph;
1195
1196 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1197 ETH_HLEN;
1198 skb_set_network_header(skb, nw_off);
1199 iph = ipv6_hdr(skb);
1200 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1201 len = skb->len - skb_transport_offset(skb);
1202 th = tcp_hdr(skb);
1203 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1204 } else {
1205 dev_kfree_skb_any(skb);
1206 return NULL;
1207 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001208
1209 if (nw_off) { /* tunnel */
1210 struct udphdr *uh = NULL;
1211
1212 if (skb->protocol == htons(ETH_P_IP)) {
1213 struct iphdr *iph = (struct iphdr *)skb->data;
1214
1215 if (iph->protocol == IPPROTO_UDP)
1216 uh = (struct udphdr *)(iph + 1);
1217 } else {
1218 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1219
1220 if (iph->nexthdr == IPPROTO_UDP)
1221 uh = (struct udphdr *)(iph + 1);
1222 }
1223 if (uh) {
1224 if (uh->check)
1225 skb_shinfo(skb)->gso_type |=
1226 SKB_GSO_UDP_TUNNEL_CSUM;
1227 else
1228 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1229 }
1230 }
1231#endif
1232 return skb;
1233}
1234
Michael Chan309369c2016-06-13 02:25:34 -04001235static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1236 struct bnxt_tpa_info *tpa_info,
1237 struct rx_tpa_end_cmp *tpa_end,
1238 struct rx_tpa_end_cmp_ext *tpa_end1,
1239 struct sk_buff *skb)
1240{
1241#ifdef CONFIG_INET
1242 int payload_off;
1243 u16 segs;
1244
1245 segs = TPA_END_TPA_SEGS(tpa_end);
1246 if (segs == 1)
1247 return skb;
1248
1249 NAPI_GRO_CB(skb)->count = segs;
1250 skb_shinfo(skb)->gso_size =
1251 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1252 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1253 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1254 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1255 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1256 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001257 if (likely(skb))
1258 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001259#endif
1260 return skb;
1261}
1262
Michael Chanc0c050c2015-10-22 16:01:17 -04001263static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1264 struct bnxt_napi *bnapi,
1265 u32 *raw_cons,
1266 struct rx_tpa_end_cmp *tpa_end,
1267 struct rx_tpa_end_cmp_ext *tpa_end1,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001268 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001269{
1270 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001271 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001272 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001273 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001274 u16 cp_cons = RING_CMP(*raw_cons);
1275 unsigned int len;
1276 struct bnxt_tpa_info *tpa_info;
1277 dma_addr_t mapping;
1278 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001279 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001280
Michael Chanfa7e2812016-05-10 19:18:00 -04001281 if (unlikely(bnapi->in_reset)) {
1282 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1283
1284 if (rc < 0)
1285 return ERR_PTR(-EBUSY);
1286 return NULL;
1287 }
1288
Michael Chanc0c050c2015-10-22 16:01:17 -04001289 tpa_info = &rxr->rx_tpa[agg_id];
1290 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001291 data_ptr = tpa_info->data_ptr;
1292 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001293 len = tpa_info->len;
1294 mapping = tpa_info->mapping;
1295
1296 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1297 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1298
1299 if (agg_bufs) {
1300 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1301 return ERR_PTR(-EBUSY);
1302
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001303 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001304 cp_cons = NEXT_CMP(cp_cons);
1305 }
1306
1307 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1308 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1309 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1310 agg_bufs, (int)MAX_SKB_FRAGS);
1311 return NULL;
1312 }
1313
1314 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001315 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001316 if (!skb) {
1317 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1318 return NULL;
1319 }
1320 } else {
1321 u8 *new_data;
1322 dma_addr_t new_mapping;
1323
1324 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1325 if (!new_data) {
1326 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1327 return NULL;
1328 }
1329
1330 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001331 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001332 tpa_info->mapping = new_mapping;
1333
1334 skb = build_skb(data, 0);
1335 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
Michael Chan745fc052017-02-06 16:55:34 -05001336 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001337
1338 if (!skb) {
1339 kfree(data);
1340 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1341 return NULL;
1342 }
Michael Chanb3dba772017-02-06 16:55:35 -05001343 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001344 skb_put(skb, len);
1345 }
1346
1347 if (agg_bufs) {
1348 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1349 if (!skb) {
1350 /* Page reuse already handled by bnxt_rx_pages(). */
1351 return NULL;
1352 }
1353 }
1354 skb->protocol = eth_type_trans(skb, bp->dev);
1355
1356 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1357 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1358
Michael Chan8852ddb2016-06-06 02:37:16 -04001359 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1360 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001361 u16 vlan_proto = tpa_info->metadata >>
1362 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001363 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001364
Michael Chan8852ddb2016-06-06 02:37:16 -04001365 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001366 }
1367
1368 skb_checksum_none_assert(skb);
1369 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1370 skb->ip_summed = CHECKSUM_UNNECESSARY;
1371 skb->csum_level =
1372 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1373 }
1374
1375 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001376 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001377
1378 return skb;
1379}
1380
1381/* returns the following:
1382 * 1 - 1 packet successfully received
1383 * 0 - successful TPA_START, packet not completed yet
1384 * -EBUSY - completion ring does not have all the agg buffers yet
1385 * -ENOMEM - packet aborted due to out of memory
1386 * -EIO - packet aborted due to hw error indicated in BD
1387 */
1388static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001389 u8 *event)
Michael Chanc0c050c2015-10-22 16:01:17 -04001390{
1391 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001392 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001393 struct net_device *dev = bp->dev;
1394 struct rx_cmp *rxcmp;
1395 struct rx_cmp_ext *rxcmp1;
1396 u32 tmp_raw_cons = *raw_cons;
1397 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1398 struct bnxt_sw_rx_bd *rx_buf;
1399 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001400 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001401 dma_addr_t dma_addr;
1402 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001403 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001404 int rc = 0;
Michael Chanc61fb992017-02-06 16:55:36 -05001405 u32 misc;
Michael Chanc0c050c2015-10-22 16:01:17 -04001406
1407 rxcmp = (struct rx_cmp *)
1408 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1409
1410 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1411 cp_cons = RING_CMP(tmp_raw_cons);
1412 rxcmp1 = (struct rx_cmp_ext *)
1413 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1414
1415 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1416 return -EBUSY;
1417
1418 cmp_type = RX_CMP_TYPE(rxcmp);
1419
1420 prod = rxr->rx_prod;
1421
1422 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1423 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1424 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1425
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001426 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001427 goto next_rx_no_prod;
1428
1429 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1430 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1431 (struct rx_tpa_end_cmp *)rxcmp,
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001432 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001433
1434 if (unlikely(IS_ERR(skb)))
1435 return -EBUSY;
1436
1437 rc = -ENOMEM;
1438 if (likely(skb)) {
1439 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001440 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001441 rc = 1;
1442 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001443 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001444 goto next_rx_no_prod;
1445 }
1446
1447 cons = rxcmp->rx_cmp_opaque;
1448 rx_buf = &rxr->rx_buf_ring[cons];
1449 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001450 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001451 if (unlikely(cons != rxr->rx_next_cons)) {
1452 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1453
1454 bnxt_sched_reset(bp, rxr);
1455 return rc1;
1456 }
Michael Chan6bb19472017-02-06 16:55:32 -05001457 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001458
Michael Chanc61fb992017-02-06 16:55:36 -05001459 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1460 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001461
1462 if (agg_bufs) {
1463 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1464 return -EBUSY;
1465
1466 cp_cons = NEXT_CMP(cp_cons);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001467 *event |= BNXT_AGG_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001468 }
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001469 *event |= BNXT_RX_EVENT;
Michael Chanc0c050c2015-10-22 16:01:17 -04001470
1471 rx_buf->data = NULL;
1472 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1473 bnxt_reuse_rx_data(rxr, cons, data);
1474 if (agg_bufs)
1475 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1476
1477 rc = -EIO;
1478 goto next_rx;
1479 }
1480
1481 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001482 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001483
1484 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001485 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001486 bnxt_reuse_rx_data(rxr, cons, data);
1487 if (!skb) {
1488 rc = -ENOMEM;
1489 goto next_rx;
1490 }
1491 } else {
Michael Chanc61fb992017-02-06 16:55:36 -05001492 u32 payload;
1493
1494 payload = misc & RX_CMP_PAYLOAD_OFFSET;
Michael Chan6bb19472017-02-06 16:55:32 -05001495 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
Michael Chanc61fb992017-02-06 16:55:36 -05001496 payload | len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001497 if (!skb) {
1498 rc = -ENOMEM;
1499 goto next_rx;
1500 }
1501 }
1502
1503 if (agg_bufs) {
1504 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1505 if (!skb) {
1506 rc = -ENOMEM;
1507 goto next_rx;
1508 }
1509 }
1510
1511 if (RX_CMP_HASH_VALID(rxcmp)) {
1512 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1513 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1514
1515 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1516 if (hash_type != 1 && hash_type != 3)
1517 type = PKT_HASH_TYPE_L3;
1518 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1519 }
1520
1521 skb->protocol = eth_type_trans(skb, dev);
1522
Michael Chan8852ddb2016-06-06 02:37:16 -04001523 if ((rxcmp1->rx_cmp_flags2 &
1524 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1525 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001526 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001527 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001528 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1529
Michael Chan8852ddb2016-06-06 02:37:16 -04001530 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001531 }
1532
1533 skb_checksum_none_assert(skb);
1534 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1535 if (dev->features & NETIF_F_RXCSUM) {
1536 skb->ip_summed = CHECKSUM_UNNECESSARY;
1537 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1538 }
1539 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001540 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1541 if (dev->features & NETIF_F_RXCSUM)
1542 cpr->rx_l4_csum_errors++;
1543 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001544 }
1545
1546 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001547 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001548 rc = 1;
1549
1550next_rx:
1551 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001552 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001553
1554next_rx_no_prod:
1555 *raw_cons = tmp_raw_cons;
1556
1557 return rc;
1558}
1559
Michael Chan4bb13ab2016-04-05 14:09:01 -04001560#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001561 ((data) & \
1562 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001563
Michael Chanc0c050c2015-10-22 16:01:17 -04001564static int bnxt_async_event_process(struct bnxt *bp,
1565 struct hwrm_async_event_cmpl *cmpl)
1566{
1567 u16 event_id = le16_to_cpu(cmpl->event_id);
1568
1569 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1570 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001571 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001572 u32 data1 = le32_to_cpu(cmpl->event_data1);
1573 struct bnxt_link_info *link_info = &bp->link_info;
1574
1575 if (BNXT_VF(bp))
1576 goto async_event_process_exit;
1577 if (data1 & 0x20000) {
1578 u16 fw_speed = link_info->force_link_speed;
1579 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1580
1581 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1582 speed);
1583 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001584 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001585 /* fall thru */
1586 }
Michael Chan87c374d2016-12-02 21:17:16 -05001587 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001588 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001589 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001590 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001591 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001592 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001593 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001594 u32 data1 = le32_to_cpu(cmpl->event_data1);
1595 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1596
1597 if (BNXT_VF(bp))
1598 break;
1599
1600 if (bp->pf.port_id != port_id)
1601 break;
1602
Michael Chan4bb13ab2016-04-05 14:09:01 -04001603 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1604 break;
1605 }
Michael Chan87c374d2016-12-02 21:17:16 -05001606 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001607 if (BNXT_PF(bp))
1608 goto async_event_process_exit;
1609 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1610 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001611 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001612 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001613 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001614 schedule_work(&bp->sp_task);
1615async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001616 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001617 return 0;
1618}
1619
1620static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1621{
1622 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1623 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1624 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1625 (struct hwrm_fwd_req_cmpl *)txcmp;
1626
1627 switch (cmpl_type) {
1628 case CMPL_BASE_TYPE_HWRM_DONE:
1629 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1630 if (seq_id == bp->hwrm_intr_seq_id)
1631 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1632 else
1633 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1634 break;
1635
1636 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1637 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1638
1639 if ((vf_id < bp->pf.first_vf_id) ||
1640 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1641 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1642 vf_id);
1643 return -EINVAL;
1644 }
1645
1646 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1647 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1648 schedule_work(&bp->sp_task);
1649 break;
1650
1651 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1652 bnxt_async_event_process(bp,
1653 (struct hwrm_async_event_cmpl *)txcmp);
1654
1655 default:
1656 break;
1657 }
1658
1659 return 0;
1660}
1661
1662static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1663{
1664 struct bnxt_napi *bnapi = dev_instance;
1665 struct bnxt *bp = bnapi->bp;
1666 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1667 u32 cons = RING_CMP(cpr->cp_raw_cons);
1668
1669 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1670 napi_schedule(&bnapi->napi);
1671 return IRQ_HANDLED;
1672}
1673
1674static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1675{
1676 u32 raw_cons = cpr->cp_raw_cons;
1677 u16 cons = RING_CMP(raw_cons);
1678 struct tx_cmp *txcmp;
1679
1680 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1681
1682 return TX_CMP_VALID(txcmp, raw_cons);
1683}
1684
Michael Chanc0c050c2015-10-22 16:01:17 -04001685static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1686{
1687 struct bnxt_napi *bnapi = dev_instance;
1688 struct bnxt *bp = bnapi->bp;
1689 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1690 u32 cons = RING_CMP(cpr->cp_raw_cons);
1691 u32 int_status;
1692
1693 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1694
1695 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001696 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001697 /* return if erroneous interrupt */
1698 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1699 return IRQ_NONE;
1700 }
1701
1702 /* disable ring IRQ */
1703 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1704
1705 /* Return here if interrupt is shared and is disabled. */
1706 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1707 return IRQ_HANDLED;
1708
1709 napi_schedule(&bnapi->napi);
1710 return IRQ_HANDLED;
1711}
1712
1713static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1714{
1715 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1716 u32 raw_cons = cpr->cp_raw_cons;
1717 u32 cons;
1718 int tx_pkts = 0;
1719 int rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001720 u8 event = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001721 struct tx_cmp *txcmp;
1722
1723 while (1) {
1724 int rc;
1725
1726 cons = RING_CMP(raw_cons);
1727 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1728
1729 if (!TX_CMP_VALID(txcmp, raw_cons))
1730 break;
1731
Michael Chan67a95e22016-05-04 16:56:43 -04001732 /* The valid test of the entry must be done first before
1733 * reading any further.
1734 */
Michael Chanb67daab2016-05-15 03:04:51 -04001735 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001736 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1737 tx_pkts++;
1738 /* return full budget so NAPI will complete. */
1739 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1740 rx_pkts = budget;
1741 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001742 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001743 if (likely(rc >= 0))
1744 rx_pkts += rc;
1745 else if (rc == -EBUSY) /* partial completion */
1746 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001747 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1748 CMPL_BASE_TYPE_HWRM_DONE) ||
1749 (TX_CMP_TYPE(txcmp) ==
1750 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1751 (TX_CMP_TYPE(txcmp) ==
1752 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1753 bnxt_hwrm_handler(bp, txcmp);
1754 }
1755 raw_cons = NEXT_RAW_CMP(raw_cons);
1756
1757 if (rx_pkts == budget)
1758 break;
1759 }
1760
1761 cpr->cp_raw_cons = raw_cons;
1762 /* ACK completion ring before freeing tx ring and producing new
1763 * buffers in rx/agg rings to prevent overflowing the completion
1764 * ring.
1765 */
1766 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1767
1768 if (tx_pkts)
1769 bnxt_tx_int(bp, bnapi, tx_pkts);
1770
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001771 if (event & BNXT_RX_EVENT) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001772 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001773
1774 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1775 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001776 if (event & BNXT_AGG_EVENT) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001777 writel(DB_KEY_RX | rxr->rx_agg_prod,
1778 rxr->rx_agg_doorbell);
1779 writel(DB_KEY_RX | rxr->rx_agg_prod,
1780 rxr->rx_agg_doorbell);
1781 }
1782 }
1783 return rx_pkts;
1784}
1785
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001786static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1787{
1788 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1789 struct bnxt *bp = bnapi->bp;
1790 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1791 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1792 struct tx_cmp *txcmp;
1793 struct rx_cmp_ext *rxcmp1;
1794 u32 cp_cons, tmp_raw_cons;
1795 u32 raw_cons = cpr->cp_raw_cons;
1796 u32 rx_pkts = 0;
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001797 u8 event = 0;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001798
1799 while (1) {
1800 int rc;
1801
1802 cp_cons = RING_CMP(raw_cons);
1803 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1804
1805 if (!TX_CMP_VALID(txcmp, raw_cons))
1806 break;
1807
1808 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1809 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1810 cp_cons = RING_CMP(tmp_raw_cons);
1811 rxcmp1 = (struct rx_cmp_ext *)
1812 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1813
1814 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1815 break;
1816
1817 /* force an error to recycle the buffer */
1818 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1819 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1820
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001821 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001822 if (likely(rc == -EIO))
1823 rx_pkts++;
1824 else if (rc == -EBUSY) /* partial completion */
1825 break;
1826 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1827 CMPL_BASE_TYPE_HWRM_DONE)) {
1828 bnxt_hwrm_handler(bp, txcmp);
1829 } else {
1830 netdev_err(bp->dev,
1831 "Invalid completion received on special ring\n");
1832 }
1833 raw_cons = NEXT_RAW_CMP(raw_cons);
1834
1835 if (rx_pkts == budget)
1836 break;
1837 }
1838
1839 cpr->cp_raw_cons = raw_cons;
1840 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1841 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1842 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1843
Michael Chan4e5dbbda2017-02-06 16:55:37 -05001844 if (event & BNXT_AGG_EVENT) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001845 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1846 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1847 }
1848
1849 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001850 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001851 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1852 }
1853 return rx_pkts;
1854}
1855
Michael Chanc0c050c2015-10-22 16:01:17 -04001856static int bnxt_poll(struct napi_struct *napi, int budget)
1857{
1858 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1859 struct bnxt *bp = bnapi->bp;
1860 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1861 int work_done = 0;
1862
Michael Chanc0c050c2015-10-22 16:01:17 -04001863 while (1) {
1864 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1865
1866 if (work_done >= budget)
1867 break;
1868
1869 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001870 if (napi_complete_done(napi, work_done))
1871 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1872 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001873 break;
1874 }
1875 }
1876 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001877 return work_done;
1878}
1879
Michael Chanc0c050c2015-10-22 16:01:17 -04001880static void bnxt_free_tx_skbs(struct bnxt *bp)
1881{
1882 int i, max_idx;
1883 struct pci_dev *pdev = bp->pdev;
1884
Michael Chanb6ab4b02016-01-02 23:44:59 -05001885 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001886 return;
1887
1888 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1889 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001890 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001891 int j;
1892
Michael Chanc0c050c2015-10-22 16:01:17 -04001893 for (j = 0; j < max_idx;) {
1894 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1895 struct sk_buff *skb = tx_buf->skb;
1896 int k, last;
1897
1898 if (!skb) {
1899 j++;
1900 continue;
1901 }
1902
1903 tx_buf->skb = NULL;
1904
1905 if (tx_buf->is_push) {
1906 dev_kfree_skb(skb);
1907 j += 2;
1908 continue;
1909 }
1910
1911 dma_unmap_single(&pdev->dev,
1912 dma_unmap_addr(tx_buf, mapping),
1913 skb_headlen(skb),
1914 PCI_DMA_TODEVICE);
1915
1916 last = tx_buf->nr_frags;
1917 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001918 for (k = 0; k < last; k++, j++) {
1919 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001920 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1921
Michael Chand612a572016-01-28 03:11:22 -05001922 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001923 dma_unmap_page(
1924 &pdev->dev,
1925 dma_unmap_addr(tx_buf, mapping),
1926 skb_frag_size(frag), PCI_DMA_TODEVICE);
1927 }
1928 dev_kfree_skb(skb);
1929 }
1930 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1931 }
1932}
1933
1934static void bnxt_free_rx_skbs(struct bnxt *bp)
1935{
1936 int i, max_idx, max_agg_idx;
1937 struct pci_dev *pdev = bp->pdev;
1938
Michael Chanb6ab4b02016-01-02 23:44:59 -05001939 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001940 return;
1941
1942 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1943 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1944 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001945 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001946 int j;
1947
Michael Chanc0c050c2015-10-22 16:01:17 -04001948 if (rxr->rx_tpa) {
1949 for (j = 0; j < MAX_TPA; j++) {
1950 struct bnxt_tpa_info *tpa_info =
1951 &rxr->rx_tpa[j];
1952 u8 *data = tpa_info->data;
1953
1954 if (!data)
1955 continue;
1956
Michael Chan745fc052017-02-06 16:55:34 -05001957 dma_unmap_single(&pdev->dev, tpa_info->mapping,
1958 bp->rx_buf_use_size,
1959 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001960
1961 tpa_info->data = NULL;
1962
1963 kfree(data);
1964 }
1965 }
1966
1967 for (j = 0; j < max_idx; j++) {
1968 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan6bb19472017-02-06 16:55:32 -05001969 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001970
1971 if (!data)
1972 continue;
1973
Michael Chan11cd1192017-02-06 16:55:33 -05001974 dma_unmap_single(&pdev->dev, rx_buf->mapping,
Michael Chan745fc052017-02-06 16:55:34 -05001975 bp->rx_buf_use_size, bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001976
1977 rx_buf->data = NULL;
1978
Michael Chanc61fb992017-02-06 16:55:36 -05001979 if (BNXT_RX_PAGE_MODE(bp))
1980 __free_page(data);
1981 else
1982 kfree(data);
Michael Chanc0c050c2015-10-22 16:01:17 -04001983 }
1984
1985 for (j = 0; j < max_agg_idx; j++) {
1986 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1987 &rxr->rx_agg_ring[j];
1988 struct page *page = rx_agg_buf->page;
1989
1990 if (!page)
1991 continue;
1992
Michael Chan11cd1192017-02-06 16:55:33 -05001993 dma_unmap_page(&pdev->dev, rx_agg_buf->mapping,
Michael Chan2839f282016-04-25 02:30:50 -04001994 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001995
1996 rx_agg_buf->page = NULL;
1997 __clear_bit(j, rxr->rx_agg_bmap);
1998
1999 __free_page(page);
2000 }
Michael Chan89d0a062016-04-25 02:30:51 -04002001 if (rxr->rx_page) {
2002 __free_page(rxr->rx_page);
2003 rxr->rx_page = NULL;
2004 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002005 }
2006}
2007
2008static void bnxt_free_skbs(struct bnxt *bp)
2009{
2010 bnxt_free_tx_skbs(bp);
2011 bnxt_free_rx_skbs(bp);
2012}
2013
2014static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2015{
2016 struct pci_dev *pdev = bp->pdev;
2017 int i;
2018
2019 for (i = 0; i < ring->nr_pages; i++) {
2020 if (!ring->pg_arr[i])
2021 continue;
2022
2023 dma_free_coherent(&pdev->dev, ring->page_size,
2024 ring->pg_arr[i], ring->dma_arr[i]);
2025
2026 ring->pg_arr[i] = NULL;
2027 }
2028 if (ring->pg_tbl) {
2029 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2030 ring->pg_tbl, ring->pg_tbl_map);
2031 ring->pg_tbl = NULL;
2032 }
2033 if (ring->vmem_size && *ring->vmem) {
2034 vfree(*ring->vmem);
2035 *ring->vmem = NULL;
2036 }
2037}
2038
2039static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2040{
2041 int i;
2042 struct pci_dev *pdev = bp->pdev;
2043
2044 if (ring->nr_pages > 1) {
2045 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2046 ring->nr_pages * 8,
2047 &ring->pg_tbl_map,
2048 GFP_KERNEL);
2049 if (!ring->pg_tbl)
2050 return -ENOMEM;
2051 }
2052
2053 for (i = 0; i < ring->nr_pages; i++) {
2054 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2055 ring->page_size,
2056 &ring->dma_arr[i],
2057 GFP_KERNEL);
2058 if (!ring->pg_arr[i])
2059 return -ENOMEM;
2060
2061 if (ring->nr_pages > 1)
2062 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2063 }
2064
2065 if (ring->vmem_size) {
2066 *ring->vmem = vzalloc(ring->vmem_size);
2067 if (!(*ring->vmem))
2068 return -ENOMEM;
2069 }
2070 return 0;
2071}
2072
2073static void bnxt_free_rx_rings(struct bnxt *bp)
2074{
2075 int i;
2076
Michael Chanb6ab4b02016-01-02 23:44:59 -05002077 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002078 return;
2079
2080 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002081 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002082 struct bnxt_ring_struct *ring;
2083
Michael Chanc0c050c2015-10-22 16:01:17 -04002084 kfree(rxr->rx_tpa);
2085 rxr->rx_tpa = NULL;
2086
2087 kfree(rxr->rx_agg_bmap);
2088 rxr->rx_agg_bmap = NULL;
2089
2090 ring = &rxr->rx_ring_struct;
2091 bnxt_free_ring(bp, ring);
2092
2093 ring = &rxr->rx_agg_ring_struct;
2094 bnxt_free_ring(bp, ring);
2095 }
2096}
2097
2098static int bnxt_alloc_rx_rings(struct bnxt *bp)
2099{
2100 int i, rc, agg_rings = 0, tpa_rings = 0;
2101
Michael Chanb6ab4b02016-01-02 23:44:59 -05002102 if (!bp->rx_ring)
2103 return -ENOMEM;
2104
Michael Chanc0c050c2015-10-22 16:01:17 -04002105 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2106 agg_rings = 1;
2107
2108 if (bp->flags & BNXT_FLAG_TPA)
2109 tpa_rings = 1;
2110
2111 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002112 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002113 struct bnxt_ring_struct *ring;
2114
Michael Chanc0c050c2015-10-22 16:01:17 -04002115 ring = &rxr->rx_ring_struct;
2116
2117 rc = bnxt_alloc_ring(bp, ring);
2118 if (rc)
2119 return rc;
2120
2121 if (agg_rings) {
2122 u16 mem_size;
2123
2124 ring = &rxr->rx_agg_ring_struct;
2125 rc = bnxt_alloc_ring(bp, ring);
2126 if (rc)
2127 return rc;
2128
2129 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2130 mem_size = rxr->rx_agg_bmap_size / 8;
2131 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2132 if (!rxr->rx_agg_bmap)
2133 return -ENOMEM;
2134
2135 if (tpa_rings) {
2136 rxr->rx_tpa = kcalloc(MAX_TPA,
2137 sizeof(struct bnxt_tpa_info),
2138 GFP_KERNEL);
2139 if (!rxr->rx_tpa)
2140 return -ENOMEM;
2141 }
2142 }
2143 }
2144 return 0;
2145}
2146
2147static void bnxt_free_tx_rings(struct bnxt *bp)
2148{
2149 int i;
2150 struct pci_dev *pdev = bp->pdev;
2151
Michael Chanb6ab4b02016-01-02 23:44:59 -05002152 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002153 return;
2154
2155 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002156 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002157 struct bnxt_ring_struct *ring;
2158
Michael Chanc0c050c2015-10-22 16:01:17 -04002159 if (txr->tx_push) {
2160 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2161 txr->tx_push, txr->tx_push_mapping);
2162 txr->tx_push = NULL;
2163 }
2164
2165 ring = &txr->tx_ring_struct;
2166
2167 bnxt_free_ring(bp, ring);
2168 }
2169}
2170
2171static int bnxt_alloc_tx_rings(struct bnxt *bp)
2172{
2173 int i, j, rc;
2174 struct pci_dev *pdev = bp->pdev;
2175
2176 bp->tx_push_size = 0;
2177 if (bp->tx_push_thresh) {
2178 int push_size;
2179
2180 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2181 bp->tx_push_thresh);
2182
Michael Chan4419dbe2016-02-10 17:33:49 -05002183 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002184 push_size = 0;
2185 bp->tx_push_thresh = 0;
2186 }
2187
2188 bp->tx_push_size = push_size;
2189 }
2190
2191 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002192 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002193 struct bnxt_ring_struct *ring;
2194
Michael Chanc0c050c2015-10-22 16:01:17 -04002195 ring = &txr->tx_ring_struct;
2196
2197 rc = bnxt_alloc_ring(bp, ring);
2198 if (rc)
2199 return rc;
2200
2201 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002202 dma_addr_t mapping;
2203
2204 /* One pre-allocated DMA buffer to backup
2205 * TX push operation
2206 */
2207 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2208 bp->tx_push_size,
2209 &txr->tx_push_mapping,
2210 GFP_KERNEL);
2211
2212 if (!txr->tx_push)
2213 return -ENOMEM;
2214
Michael Chanc0c050c2015-10-22 16:01:17 -04002215 mapping = txr->tx_push_mapping +
2216 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002217 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002218
Michael Chan4419dbe2016-02-10 17:33:49 -05002219 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002220 }
2221 ring->queue_id = bp->q_info[j].queue_id;
2222 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2223 j++;
2224 }
2225 return 0;
2226}
2227
2228static void bnxt_free_cp_rings(struct bnxt *bp)
2229{
2230 int i;
2231
2232 if (!bp->bnapi)
2233 return;
2234
2235 for (i = 0; i < bp->cp_nr_rings; i++) {
2236 struct bnxt_napi *bnapi = bp->bnapi[i];
2237 struct bnxt_cp_ring_info *cpr;
2238 struct bnxt_ring_struct *ring;
2239
2240 if (!bnapi)
2241 continue;
2242
2243 cpr = &bnapi->cp_ring;
2244 ring = &cpr->cp_ring_struct;
2245
2246 bnxt_free_ring(bp, ring);
2247 }
2248}
2249
2250static int bnxt_alloc_cp_rings(struct bnxt *bp)
2251{
2252 int i, rc;
2253
2254 for (i = 0; i < bp->cp_nr_rings; i++) {
2255 struct bnxt_napi *bnapi = bp->bnapi[i];
2256 struct bnxt_cp_ring_info *cpr;
2257 struct bnxt_ring_struct *ring;
2258
2259 if (!bnapi)
2260 continue;
2261
2262 cpr = &bnapi->cp_ring;
2263 ring = &cpr->cp_ring_struct;
2264
2265 rc = bnxt_alloc_ring(bp, ring);
2266 if (rc)
2267 return rc;
2268 }
2269 return 0;
2270}
2271
2272static void bnxt_init_ring_struct(struct bnxt *bp)
2273{
2274 int i;
2275
2276 for (i = 0; i < bp->cp_nr_rings; i++) {
2277 struct bnxt_napi *bnapi = bp->bnapi[i];
2278 struct bnxt_cp_ring_info *cpr;
2279 struct bnxt_rx_ring_info *rxr;
2280 struct bnxt_tx_ring_info *txr;
2281 struct bnxt_ring_struct *ring;
2282
2283 if (!bnapi)
2284 continue;
2285
2286 cpr = &bnapi->cp_ring;
2287 ring = &cpr->cp_ring_struct;
2288 ring->nr_pages = bp->cp_nr_pages;
2289 ring->page_size = HW_CMPD_RING_SIZE;
2290 ring->pg_arr = (void **)cpr->cp_desc_ring;
2291 ring->dma_arr = cpr->cp_desc_mapping;
2292 ring->vmem_size = 0;
2293
Michael Chanb6ab4b02016-01-02 23:44:59 -05002294 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002295 if (!rxr)
2296 goto skip_rx;
2297
Michael Chanc0c050c2015-10-22 16:01:17 -04002298 ring = &rxr->rx_ring_struct;
2299 ring->nr_pages = bp->rx_nr_pages;
2300 ring->page_size = HW_RXBD_RING_SIZE;
2301 ring->pg_arr = (void **)rxr->rx_desc_ring;
2302 ring->dma_arr = rxr->rx_desc_mapping;
2303 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2304 ring->vmem = (void **)&rxr->rx_buf_ring;
2305
2306 ring = &rxr->rx_agg_ring_struct;
2307 ring->nr_pages = bp->rx_agg_nr_pages;
2308 ring->page_size = HW_RXBD_RING_SIZE;
2309 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2310 ring->dma_arr = rxr->rx_agg_desc_mapping;
2311 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2312 ring->vmem = (void **)&rxr->rx_agg_ring;
2313
Michael Chan3b2b7d92016-01-02 23:45:00 -05002314skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002315 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002316 if (!txr)
2317 continue;
2318
Michael Chanc0c050c2015-10-22 16:01:17 -04002319 ring = &txr->tx_ring_struct;
2320 ring->nr_pages = bp->tx_nr_pages;
2321 ring->page_size = HW_RXBD_RING_SIZE;
2322 ring->pg_arr = (void **)txr->tx_desc_ring;
2323 ring->dma_arr = txr->tx_desc_mapping;
2324 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2325 ring->vmem = (void **)&txr->tx_buf_ring;
2326 }
2327}
2328
2329static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2330{
2331 int i;
2332 u32 prod;
2333 struct rx_bd **rx_buf_ring;
2334
2335 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2336 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2337 int j;
2338 struct rx_bd *rxbd;
2339
2340 rxbd = rx_buf_ring[i];
2341 if (!rxbd)
2342 continue;
2343
2344 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2345 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2346 rxbd->rx_bd_opaque = prod;
2347 }
2348 }
2349}
2350
2351static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2352{
2353 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002354 struct bnxt_rx_ring_info *rxr;
2355 struct bnxt_ring_struct *ring;
2356 u32 prod, type;
2357 int i;
2358
Michael Chanc0c050c2015-10-22 16:01:17 -04002359 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2360 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2361
2362 if (NET_IP_ALIGN == 2)
2363 type |= RX_BD_FLAGS_SOP;
2364
Michael Chanb6ab4b02016-01-02 23:44:59 -05002365 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002366 ring = &rxr->rx_ring_struct;
2367 bnxt_init_rxbd_pages(ring, type);
2368
2369 prod = rxr->rx_prod;
2370 for (i = 0; i < bp->rx_ring_size; i++) {
2371 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2372 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2373 ring_nr, i, bp->rx_ring_size);
2374 break;
2375 }
2376 prod = NEXT_RX(prod);
2377 }
2378 rxr->rx_prod = prod;
2379 ring->fw_ring_id = INVALID_HW_RING_ID;
2380
Michael Chanedd0c2c2015-12-27 18:19:19 -05002381 ring = &rxr->rx_agg_ring_struct;
2382 ring->fw_ring_id = INVALID_HW_RING_ID;
2383
Michael Chanc0c050c2015-10-22 16:01:17 -04002384 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2385 return 0;
2386
Michael Chan2839f282016-04-25 02:30:50 -04002387 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002388 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2389
2390 bnxt_init_rxbd_pages(ring, type);
2391
2392 prod = rxr->rx_agg_prod;
2393 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2394 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2395 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2396 ring_nr, i, bp->rx_ring_size);
2397 break;
2398 }
2399 prod = NEXT_RX_AGG(prod);
2400 }
2401 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002402
2403 if (bp->flags & BNXT_FLAG_TPA) {
2404 if (rxr->rx_tpa) {
2405 u8 *data;
2406 dma_addr_t mapping;
2407
2408 for (i = 0; i < MAX_TPA; i++) {
2409 data = __bnxt_alloc_rx_data(bp, &mapping,
2410 GFP_KERNEL);
2411 if (!data)
2412 return -ENOMEM;
2413
2414 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002415 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002416 rxr->rx_tpa[i].mapping = mapping;
2417 }
2418 } else {
2419 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2420 return -ENOMEM;
2421 }
2422 }
2423
2424 return 0;
2425}
2426
2427static int bnxt_init_rx_rings(struct bnxt *bp)
2428{
2429 int i, rc = 0;
2430
Michael Chanc61fb992017-02-06 16:55:36 -05002431 if (BNXT_RX_PAGE_MODE(bp)) {
2432 bp->rx_offset = NET_IP_ALIGN;
2433 bp->rx_dma_offset = 0;
2434 } else {
2435 bp->rx_offset = BNXT_RX_OFFSET;
2436 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2437 }
Michael Chanb3dba772017-02-06 16:55:35 -05002438
Michael Chanc0c050c2015-10-22 16:01:17 -04002439 for (i = 0; i < bp->rx_nr_rings; i++) {
2440 rc = bnxt_init_one_rx_ring(bp, i);
2441 if (rc)
2442 break;
2443 }
2444
2445 return rc;
2446}
2447
2448static int bnxt_init_tx_rings(struct bnxt *bp)
2449{
2450 u16 i;
2451
2452 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2453 MAX_SKB_FRAGS + 1);
2454
2455 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002456 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002457 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2458
2459 ring->fw_ring_id = INVALID_HW_RING_ID;
2460 }
2461
2462 return 0;
2463}
2464
2465static void bnxt_free_ring_grps(struct bnxt *bp)
2466{
2467 kfree(bp->grp_info);
2468 bp->grp_info = NULL;
2469}
2470
2471static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2472{
2473 int i;
2474
2475 if (irq_re_init) {
2476 bp->grp_info = kcalloc(bp->cp_nr_rings,
2477 sizeof(struct bnxt_ring_grp_info),
2478 GFP_KERNEL);
2479 if (!bp->grp_info)
2480 return -ENOMEM;
2481 }
2482 for (i = 0; i < bp->cp_nr_rings; i++) {
2483 if (irq_re_init)
2484 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2485 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2486 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2487 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2488 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2489 }
2490 return 0;
2491}
2492
2493static void bnxt_free_vnics(struct bnxt *bp)
2494{
2495 kfree(bp->vnic_info);
2496 bp->vnic_info = NULL;
2497 bp->nr_vnics = 0;
2498}
2499
2500static int bnxt_alloc_vnics(struct bnxt *bp)
2501{
2502 int num_vnics = 1;
2503
2504#ifdef CONFIG_RFS_ACCEL
2505 if (bp->flags & BNXT_FLAG_RFS)
2506 num_vnics += bp->rx_nr_rings;
2507#endif
2508
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002509 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2510 num_vnics++;
2511
Michael Chanc0c050c2015-10-22 16:01:17 -04002512 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2513 GFP_KERNEL);
2514 if (!bp->vnic_info)
2515 return -ENOMEM;
2516
2517 bp->nr_vnics = num_vnics;
2518 return 0;
2519}
2520
2521static void bnxt_init_vnics(struct bnxt *bp)
2522{
2523 int i;
2524
2525 for (i = 0; i < bp->nr_vnics; i++) {
2526 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2527
2528 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002529 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2530 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002531 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2532
2533 if (bp->vnic_info[i].rss_hash_key) {
2534 if (i == 0)
2535 prandom_bytes(vnic->rss_hash_key,
2536 HW_HASH_KEY_SIZE);
2537 else
2538 memcpy(vnic->rss_hash_key,
2539 bp->vnic_info[0].rss_hash_key,
2540 HW_HASH_KEY_SIZE);
2541 }
2542 }
2543}
2544
2545static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2546{
2547 int pages;
2548
2549 pages = ring_size / desc_per_pg;
2550
2551 if (!pages)
2552 return 1;
2553
2554 pages++;
2555
2556 while (pages & (pages - 1))
2557 pages++;
2558
2559 return pages;
2560}
2561
2562static void bnxt_set_tpa_flags(struct bnxt *bp)
2563{
2564 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002565 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2566 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002567 if (bp->dev->features & NETIF_F_LRO)
2568 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002569 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002570 bp->flags |= BNXT_FLAG_GRO;
2571}
2572
2573/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2574 * be set on entry.
2575 */
2576void bnxt_set_ring_params(struct bnxt *bp)
2577{
2578 u32 ring_size, rx_size, rx_space;
2579 u32 agg_factor = 0, agg_ring_size = 0;
2580
2581 /* 8 for CRC and VLAN */
2582 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2583
2584 rx_space = rx_size + NET_SKB_PAD +
2585 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2586
2587 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2588 ring_size = bp->rx_ring_size;
2589 bp->rx_agg_ring_size = 0;
2590 bp->rx_agg_nr_pages = 0;
2591
2592 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002593 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002594
2595 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002596 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002597 u32 jumbo_factor;
2598
2599 bp->flags |= BNXT_FLAG_JUMBO;
2600 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2601 if (jumbo_factor > agg_factor)
2602 agg_factor = jumbo_factor;
2603 }
2604 agg_ring_size = ring_size * agg_factor;
2605
2606 if (agg_ring_size) {
2607 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2608 RX_DESC_CNT);
2609 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2610 u32 tmp = agg_ring_size;
2611
2612 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2613 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2614 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2615 tmp, agg_ring_size);
2616 }
2617 bp->rx_agg_ring_size = agg_ring_size;
2618 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2619 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2620 rx_space = rx_size + NET_SKB_PAD +
2621 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2622 }
2623
2624 bp->rx_buf_use_size = rx_size;
2625 bp->rx_buf_size = rx_space;
2626
2627 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2628 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2629
2630 ring_size = bp->tx_ring_size;
2631 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2632 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2633
2634 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2635 bp->cp_ring_size = ring_size;
2636
2637 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2638 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2639 bp->cp_nr_pages = MAX_CP_PAGES;
2640 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2641 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2642 ring_size, bp->cp_ring_size);
2643 }
2644 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2645 bp->cp_ring_mask = bp->cp_bit - 1;
2646}
2647
Michael Chanc61fb992017-02-06 16:55:36 -05002648int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
Michael Chan6bb19472017-02-06 16:55:32 -05002649{
Michael Chanc61fb992017-02-06 16:55:36 -05002650 if (page_mode) {
2651 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2652 return -EOPNOTSUPP;
2653 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2654 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2655 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2656 bp->dev->hw_features &= ~NETIF_F_LRO;
2657 bp->dev->features &= ~NETIF_F_LRO;
2658 bp->rx_dir = DMA_BIDIRECTIONAL;
2659 bp->rx_skb_func = bnxt_rx_page_skb;
2660 } else {
2661 bp->dev->max_mtu = BNXT_MAX_MTU;
2662 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2663 bp->rx_dir = DMA_FROM_DEVICE;
2664 bp->rx_skb_func = bnxt_rx_skb;
2665 }
Michael Chan6bb19472017-02-06 16:55:32 -05002666 return 0;
2667}
2668
Michael Chanc0c050c2015-10-22 16:01:17 -04002669static void bnxt_free_vnic_attributes(struct bnxt *bp)
2670{
2671 int i;
2672 struct bnxt_vnic_info *vnic;
2673 struct pci_dev *pdev = bp->pdev;
2674
2675 if (!bp->vnic_info)
2676 return;
2677
2678 for (i = 0; i < bp->nr_vnics; i++) {
2679 vnic = &bp->vnic_info[i];
2680
2681 kfree(vnic->fw_grp_ids);
2682 vnic->fw_grp_ids = NULL;
2683
2684 kfree(vnic->uc_list);
2685 vnic->uc_list = NULL;
2686
2687 if (vnic->mc_list) {
2688 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2689 vnic->mc_list, vnic->mc_list_mapping);
2690 vnic->mc_list = NULL;
2691 }
2692
2693 if (vnic->rss_table) {
2694 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2695 vnic->rss_table,
2696 vnic->rss_table_dma_addr);
2697 vnic->rss_table = NULL;
2698 }
2699
2700 vnic->rss_hash_key = NULL;
2701 vnic->flags = 0;
2702 }
2703}
2704
2705static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2706{
2707 int i, rc = 0, size;
2708 struct bnxt_vnic_info *vnic;
2709 struct pci_dev *pdev = bp->pdev;
2710 int max_rings;
2711
2712 for (i = 0; i < bp->nr_vnics; i++) {
2713 vnic = &bp->vnic_info[i];
2714
2715 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2716 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2717
2718 if (mem_size > 0) {
2719 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2720 if (!vnic->uc_list) {
2721 rc = -ENOMEM;
2722 goto out;
2723 }
2724 }
2725 }
2726
2727 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2728 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2729 vnic->mc_list =
2730 dma_alloc_coherent(&pdev->dev,
2731 vnic->mc_list_size,
2732 &vnic->mc_list_mapping,
2733 GFP_KERNEL);
2734 if (!vnic->mc_list) {
2735 rc = -ENOMEM;
2736 goto out;
2737 }
2738 }
2739
2740 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2741 max_rings = bp->rx_nr_rings;
2742 else
2743 max_rings = 1;
2744
2745 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2746 if (!vnic->fw_grp_ids) {
2747 rc = -ENOMEM;
2748 goto out;
2749 }
2750
Michael Chanae10ae72016-12-29 12:13:38 -05002751 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2752 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2753 continue;
2754
Michael Chanc0c050c2015-10-22 16:01:17 -04002755 /* Allocate rss table and hash key */
2756 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2757 &vnic->rss_table_dma_addr,
2758 GFP_KERNEL);
2759 if (!vnic->rss_table) {
2760 rc = -ENOMEM;
2761 goto out;
2762 }
2763
2764 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2765
2766 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2767 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2768 }
2769 return 0;
2770
2771out:
2772 return rc;
2773}
2774
2775static void bnxt_free_hwrm_resources(struct bnxt *bp)
2776{
2777 struct pci_dev *pdev = bp->pdev;
2778
2779 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2780 bp->hwrm_cmd_resp_dma_addr);
2781
2782 bp->hwrm_cmd_resp_addr = NULL;
2783 if (bp->hwrm_dbg_resp_addr) {
2784 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2785 bp->hwrm_dbg_resp_addr,
2786 bp->hwrm_dbg_resp_dma_addr);
2787
2788 bp->hwrm_dbg_resp_addr = NULL;
2789 }
2790}
2791
2792static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2793{
2794 struct pci_dev *pdev = bp->pdev;
2795
2796 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2797 &bp->hwrm_cmd_resp_dma_addr,
2798 GFP_KERNEL);
2799 if (!bp->hwrm_cmd_resp_addr)
2800 return -ENOMEM;
2801 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2802 HWRM_DBG_REG_BUF_SIZE,
2803 &bp->hwrm_dbg_resp_dma_addr,
2804 GFP_KERNEL);
2805 if (!bp->hwrm_dbg_resp_addr)
2806 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2807
2808 return 0;
2809}
2810
2811static void bnxt_free_stats(struct bnxt *bp)
2812{
2813 u32 size, i;
2814 struct pci_dev *pdev = bp->pdev;
2815
Michael Chan3bdf56c2016-03-07 15:38:45 -05002816 if (bp->hw_rx_port_stats) {
2817 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2818 bp->hw_rx_port_stats,
2819 bp->hw_rx_port_stats_map);
2820 bp->hw_rx_port_stats = NULL;
2821 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2822 }
2823
Michael Chanc0c050c2015-10-22 16:01:17 -04002824 if (!bp->bnapi)
2825 return;
2826
2827 size = sizeof(struct ctx_hw_stats);
2828
2829 for (i = 0; i < bp->cp_nr_rings; i++) {
2830 struct bnxt_napi *bnapi = bp->bnapi[i];
2831 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2832
2833 if (cpr->hw_stats) {
2834 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2835 cpr->hw_stats_map);
2836 cpr->hw_stats = NULL;
2837 }
2838 }
2839}
2840
2841static int bnxt_alloc_stats(struct bnxt *bp)
2842{
2843 u32 size, i;
2844 struct pci_dev *pdev = bp->pdev;
2845
2846 size = sizeof(struct ctx_hw_stats);
2847
2848 for (i = 0; i < bp->cp_nr_rings; i++) {
2849 struct bnxt_napi *bnapi = bp->bnapi[i];
2850 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2851
2852 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2853 &cpr->hw_stats_map,
2854 GFP_KERNEL);
2855 if (!cpr->hw_stats)
2856 return -ENOMEM;
2857
2858 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2859 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002860
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002861 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002862 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2863 sizeof(struct tx_port_stats) + 1024;
2864
2865 bp->hw_rx_port_stats =
2866 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2867 &bp->hw_rx_port_stats_map,
2868 GFP_KERNEL);
2869 if (!bp->hw_rx_port_stats)
2870 return -ENOMEM;
2871
2872 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2873 512;
2874 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2875 sizeof(struct rx_port_stats) + 512;
2876 bp->flags |= BNXT_FLAG_PORT_STATS;
2877 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002878 return 0;
2879}
2880
2881static void bnxt_clear_ring_indices(struct bnxt *bp)
2882{
2883 int i;
2884
2885 if (!bp->bnapi)
2886 return;
2887
2888 for (i = 0; i < bp->cp_nr_rings; i++) {
2889 struct bnxt_napi *bnapi = bp->bnapi[i];
2890 struct bnxt_cp_ring_info *cpr;
2891 struct bnxt_rx_ring_info *rxr;
2892 struct bnxt_tx_ring_info *txr;
2893
2894 if (!bnapi)
2895 continue;
2896
2897 cpr = &bnapi->cp_ring;
2898 cpr->cp_raw_cons = 0;
2899
Michael Chanb6ab4b02016-01-02 23:44:59 -05002900 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002901 if (txr) {
2902 txr->tx_prod = 0;
2903 txr->tx_cons = 0;
2904 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002905
Michael Chanb6ab4b02016-01-02 23:44:59 -05002906 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002907 if (rxr) {
2908 rxr->rx_prod = 0;
2909 rxr->rx_agg_prod = 0;
2910 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002911 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002912 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002913 }
2914}
2915
2916static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2917{
2918#ifdef CONFIG_RFS_ACCEL
2919 int i;
2920
2921 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2922 * safe to delete the hash table.
2923 */
2924 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2925 struct hlist_head *head;
2926 struct hlist_node *tmp;
2927 struct bnxt_ntuple_filter *fltr;
2928
2929 head = &bp->ntp_fltr_hash_tbl[i];
2930 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2931 hlist_del(&fltr->hash);
2932 kfree(fltr);
2933 }
2934 }
2935 if (irq_reinit) {
2936 kfree(bp->ntp_fltr_bmap);
2937 bp->ntp_fltr_bmap = NULL;
2938 }
2939 bp->ntp_fltr_count = 0;
2940#endif
2941}
2942
2943static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2944{
2945#ifdef CONFIG_RFS_ACCEL
2946 int i, rc = 0;
2947
2948 if (!(bp->flags & BNXT_FLAG_RFS))
2949 return 0;
2950
2951 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2952 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2953
2954 bp->ntp_fltr_count = 0;
2955 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2956 GFP_KERNEL);
2957
2958 if (!bp->ntp_fltr_bmap)
2959 rc = -ENOMEM;
2960
2961 return rc;
2962#else
2963 return 0;
2964#endif
2965}
2966
2967static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2968{
2969 bnxt_free_vnic_attributes(bp);
2970 bnxt_free_tx_rings(bp);
2971 bnxt_free_rx_rings(bp);
2972 bnxt_free_cp_rings(bp);
2973 bnxt_free_ntp_fltrs(bp, irq_re_init);
2974 if (irq_re_init) {
2975 bnxt_free_stats(bp);
2976 bnxt_free_ring_grps(bp);
2977 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002978 kfree(bp->tx_ring);
2979 bp->tx_ring = NULL;
2980 kfree(bp->rx_ring);
2981 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002982 kfree(bp->bnapi);
2983 bp->bnapi = NULL;
2984 } else {
2985 bnxt_clear_ring_indices(bp);
2986 }
2987}
2988
2989static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2990{
Michael Chan01657bc2016-01-02 23:45:03 -05002991 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002992 void *bnapi;
2993
2994 if (irq_re_init) {
2995 /* Allocate bnapi mem pointer array and mem block for
2996 * all queues
2997 */
2998 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2999 bp->cp_nr_rings);
3000 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3001 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3002 if (!bnapi)
3003 return -ENOMEM;
3004
3005 bp->bnapi = bnapi;
3006 bnapi += arr_size;
3007 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3008 bp->bnapi[i] = bnapi;
3009 bp->bnapi[i]->index = i;
3010 bp->bnapi[i]->bp = bp;
3011 }
3012
Michael Chanb6ab4b02016-01-02 23:44:59 -05003013 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3014 sizeof(struct bnxt_rx_ring_info),
3015 GFP_KERNEL);
3016 if (!bp->rx_ring)
3017 return -ENOMEM;
3018
3019 for (i = 0; i < bp->rx_nr_rings; i++) {
3020 bp->rx_ring[i].bnapi = bp->bnapi[i];
3021 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3022 }
3023
3024 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3025 sizeof(struct bnxt_tx_ring_info),
3026 GFP_KERNEL);
3027 if (!bp->tx_ring)
3028 return -ENOMEM;
3029
Michael Chan01657bc2016-01-02 23:45:03 -05003030 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3031 j = 0;
3032 else
3033 j = bp->rx_nr_rings;
3034
3035 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3036 bp->tx_ring[i].bnapi = bp->bnapi[j];
3037 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05003038 }
3039
Michael Chanc0c050c2015-10-22 16:01:17 -04003040 rc = bnxt_alloc_stats(bp);
3041 if (rc)
3042 goto alloc_mem_err;
3043
3044 rc = bnxt_alloc_ntp_fltrs(bp);
3045 if (rc)
3046 goto alloc_mem_err;
3047
3048 rc = bnxt_alloc_vnics(bp);
3049 if (rc)
3050 goto alloc_mem_err;
3051 }
3052
3053 bnxt_init_ring_struct(bp);
3054
3055 rc = bnxt_alloc_rx_rings(bp);
3056 if (rc)
3057 goto alloc_mem_err;
3058
3059 rc = bnxt_alloc_tx_rings(bp);
3060 if (rc)
3061 goto alloc_mem_err;
3062
3063 rc = bnxt_alloc_cp_rings(bp);
3064 if (rc)
3065 goto alloc_mem_err;
3066
3067 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3068 BNXT_VNIC_UCAST_FLAG;
3069 rc = bnxt_alloc_vnic_attributes(bp);
3070 if (rc)
3071 goto alloc_mem_err;
3072 return 0;
3073
3074alloc_mem_err:
3075 bnxt_free_mem(bp, true);
3076 return rc;
3077}
3078
Michael Chan9d8bc092016-12-29 12:13:33 -05003079static void bnxt_disable_int(struct bnxt *bp)
3080{
3081 int i;
3082
3083 if (!bp->bnapi)
3084 return;
3085
3086 for (i = 0; i < bp->cp_nr_rings; i++) {
3087 struct bnxt_napi *bnapi = bp->bnapi[i];
3088 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3089
3090 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3091 }
3092}
3093
3094static void bnxt_disable_int_sync(struct bnxt *bp)
3095{
3096 int i;
3097
3098 atomic_inc(&bp->intr_sem);
3099
3100 bnxt_disable_int(bp);
3101 for (i = 0; i < bp->cp_nr_rings; i++)
3102 synchronize_irq(bp->irq_tbl[i].vector);
3103}
3104
3105static void bnxt_enable_int(struct bnxt *bp)
3106{
3107 int i;
3108
3109 atomic_set(&bp->intr_sem, 0);
3110 for (i = 0; i < bp->cp_nr_rings; i++) {
3111 struct bnxt_napi *bnapi = bp->bnapi[i];
3112 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3113
3114 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3115 }
3116}
3117
Michael Chanc0c050c2015-10-22 16:01:17 -04003118void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3119 u16 cmpl_ring, u16 target_id)
3120{
Michael Chana8643e12016-02-26 04:00:05 -05003121 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003122
Michael Chana8643e12016-02-26 04:00:05 -05003123 req->req_type = cpu_to_le16(req_type);
3124 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3125 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003126 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3127}
3128
Michael Chanfbfbc482016-02-26 04:00:07 -05003129static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3130 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003131{
Michael Chana11fa2b2016-05-15 03:04:47 -04003132 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003133 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003134 u32 *data = msg;
3135 __le32 *resp_len, *valid;
3136 u16 cp_ring_id, len = 0;
3137 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3138
Michael Chana8643e12016-02-26 04:00:05 -05003139 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003140 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003141 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003142 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3143
3144 /* Write request msg to hwrm channel */
3145 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3146
Michael Chane6ef2692016-03-28 19:46:05 -04003147 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003148 writel(0, bp->bar0 + i);
3149
Michael Chanc0c050c2015-10-22 16:01:17 -04003150 /* currently supports only one outstanding message */
3151 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003152 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003153
3154 /* Ring channel doorbell */
3155 writel(1, bp->bar0 + 0x100);
3156
Michael Chanff4fe812016-02-26 04:00:04 -05003157 if (!timeout)
3158 timeout = DFLT_HWRM_CMD_TIMEOUT;
3159
Michael Chanc0c050c2015-10-22 16:01:17 -04003160 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003161 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003162 if (intr_process) {
3163 /* Wait until hwrm response cmpl interrupt is processed */
3164 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003165 i++ < tmo_count) {
3166 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003167 }
3168
3169 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3170 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003171 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003172 return -1;
3173 }
3174 } else {
3175 /* Check if response len is updated */
3176 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003177 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003178 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3179 HWRM_RESP_LEN_SFT;
3180 if (len)
3181 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003182 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003183 }
3184
Michael Chana11fa2b2016-05-15 03:04:47 -04003185 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003186 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003187 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003188 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003189 return -1;
3190 }
3191
3192 /* Last word of resp contains valid bit */
3193 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003194 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003195 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3196 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003197 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003198 }
3199
Michael Chana11fa2b2016-05-15 03:04:47 -04003200 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003201 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003202 timeout, le16_to_cpu(req->req_type),
3203 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003204 return -1;
3205 }
3206 }
3207
3208 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003209 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003210 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3211 le16_to_cpu(resp->req_type),
3212 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003213 return rc;
3214}
3215
3216int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3217{
3218 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003219}
3220
3221int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3222{
3223 int rc;
3224
3225 mutex_lock(&bp->hwrm_cmd_lock);
3226 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3227 mutex_unlock(&bp->hwrm_cmd_lock);
3228 return rc;
3229}
3230
Michael Chan90e209212016-02-26 04:00:08 -05003231int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3232 int timeout)
3233{
3234 int rc;
3235
3236 mutex_lock(&bp->hwrm_cmd_lock);
3237 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3238 mutex_unlock(&bp->hwrm_cmd_lock);
3239 return rc;
3240}
3241
Michael Chana1653b12016-12-07 00:26:20 -05003242int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3243 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003244{
3245 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003246 DECLARE_BITMAP(async_events_bmap, 256);
3247 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003248 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003249
3250 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3251
3252 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003253 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003254
Michael Chan25be8622016-04-05 14:09:00 -04003255 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3256 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3257 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3258
Michael Chana1653b12016-12-07 00:26:20 -05003259 if (bmap && bmap_size) {
3260 for (i = 0; i < bmap_size; i++) {
3261 if (test_bit(i, bmap))
3262 __set_bit(i, async_events_bmap);
3263 }
3264 }
3265
Michael Chan25be8622016-04-05 14:09:00 -04003266 for (i = 0; i < 8; i++)
3267 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3268
Michael Chana1653b12016-12-07 00:26:20 -05003269 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3270}
3271
3272static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3273{
3274 struct hwrm_func_drv_rgtr_input req = {0};
3275
3276 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3277
3278 req.enables =
3279 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3280 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3281
Michael Chan11f15ed2016-04-05 14:08:55 -04003282 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003283 req.ver_maj = DRV_VER_MAJ;
3284 req.ver_min = DRV_VER_MIN;
3285 req.ver_upd = DRV_VER_UPD;
3286
3287 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003288 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003289 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003290 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003291
Michael Chande68f5de2015-12-09 19:35:41 -05003292 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003293 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3294 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3295
Michael Chande68f5de2015-12-09 19:35:41 -05003296 for (i = 0; i < 8; i++)
3297 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3298
Michael Chanc0c050c2015-10-22 16:01:17 -04003299 req.enables |=
3300 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3301 }
3302
3303 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3304}
3305
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003306static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3307{
3308 struct hwrm_func_drv_unrgtr_input req = {0};
3309
3310 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3311 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3312}
3313
Michael Chanc0c050c2015-10-22 16:01:17 -04003314static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3315{
3316 u32 rc = 0;
3317 struct hwrm_tunnel_dst_port_free_input req = {0};
3318
3319 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3320 req.tunnel_type = tunnel_type;
3321
3322 switch (tunnel_type) {
3323 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3324 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3325 break;
3326 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3327 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3328 break;
3329 default:
3330 break;
3331 }
3332
3333 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3334 if (rc)
3335 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3336 rc);
3337 return rc;
3338}
3339
3340static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3341 u8 tunnel_type)
3342{
3343 u32 rc = 0;
3344 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3345 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3346
3347 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3348
3349 req.tunnel_type = tunnel_type;
3350 req.tunnel_dst_port_val = port;
3351
3352 mutex_lock(&bp->hwrm_cmd_lock);
3353 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3354 if (rc) {
3355 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3356 rc);
3357 goto err_out;
3358 }
3359
Christophe Jaillet57aac712016-11-22 06:14:40 +01003360 switch (tunnel_type) {
3361 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003362 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003363 break;
3364 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003365 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003366 break;
3367 default:
3368 break;
3369 }
3370
Michael Chanc0c050c2015-10-22 16:01:17 -04003371err_out:
3372 mutex_unlock(&bp->hwrm_cmd_lock);
3373 return rc;
3374}
3375
3376static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3377{
3378 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3379 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3380
3381 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003382 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003383
3384 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3385 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3386 req.mask = cpu_to_le32(vnic->rx_mask);
3387 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3388}
3389
3390#ifdef CONFIG_RFS_ACCEL
3391static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3392 struct bnxt_ntuple_filter *fltr)
3393{
3394 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3395
3396 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3397 req.ntuple_filter_id = fltr->filter_id;
3398 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3399}
3400
3401#define BNXT_NTP_FLTR_FLAGS \
3402 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3403 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3404 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3405 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3406 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3407 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3408 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3409 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3410 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3411 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3412 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3413 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3414 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003415 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003416
3417static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3418 struct bnxt_ntuple_filter *fltr)
3419{
3420 int rc = 0;
3421 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3422 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3423 bp->hwrm_cmd_resp_addr;
3424 struct flow_keys *keys = &fltr->fkeys;
3425 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3426
3427 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003428 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003429
3430 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3431
3432 req.ethertype = htons(ETH_P_IP);
3433 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003434 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003435 req.ip_protocol = keys->basic.ip_proto;
3436
Michael Chandda0e742016-12-29 12:13:40 -05003437 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3438 int i;
3439
3440 req.ethertype = htons(ETH_P_IPV6);
3441 req.ip_addr_type =
3442 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3443 *(struct in6_addr *)&req.src_ipaddr[0] =
3444 keys->addrs.v6addrs.src;
3445 *(struct in6_addr *)&req.dst_ipaddr[0] =
3446 keys->addrs.v6addrs.dst;
3447 for (i = 0; i < 4; i++) {
3448 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3449 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3450 }
3451 } else {
3452 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3453 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3454 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3455 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3456 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003457
3458 req.src_port = keys->ports.src;
3459 req.src_port_mask = cpu_to_be16(0xffff);
3460 req.dst_port = keys->ports.dst;
3461 req.dst_port_mask = cpu_to_be16(0xffff);
3462
Michael Chanc1935542015-12-27 18:19:28 -05003463 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003464 mutex_lock(&bp->hwrm_cmd_lock);
3465 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3466 if (!rc)
3467 fltr->filter_id = resp->ntuple_filter_id;
3468 mutex_unlock(&bp->hwrm_cmd_lock);
3469 return rc;
3470}
3471#endif
3472
3473static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3474 u8 *mac_addr)
3475{
3476 u32 rc = 0;
3477 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3478 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3479
3480 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003481 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3482 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3483 req.flags |=
3484 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003485 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003486 req.enables =
3487 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003488 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003489 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3490 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3491 req.l2_addr_mask[0] = 0xff;
3492 req.l2_addr_mask[1] = 0xff;
3493 req.l2_addr_mask[2] = 0xff;
3494 req.l2_addr_mask[3] = 0xff;
3495 req.l2_addr_mask[4] = 0xff;
3496 req.l2_addr_mask[5] = 0xff;
3497
3498 mutex_lock(&bp->hwrm_cmd_lock);
3499 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3500 if (!rc)
3501 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3502 resp->l2_filter_id;
3503 mutex_unlock(&bp->hwrm_cmd_lock);
3504 return rc;
3505}
3506
3507static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3508{
3509 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3510 int rc = 0;
3511
3512 /* Any associated ntuple filters will also be cleared by firmware. */
3513 mutex_lock(&bp->hwrm_cmd_lock);
3514 for (i = 0; i < num_of_vnics; i++) {
3515 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3516
3517 for (j = 0; j < vnic->uc_filter_count; j++) {
3518 struct hwrm_cfa_l2_filter_free_input req = {0};
3519
3520 bnxt_hwrm_cmd_hdr_init(bp, &req,
3521 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3522
3523 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3524
3525 rc = _hwrm_send_message(bp, &req, sizeof(req),
3526 HWRM_CMD_TIMEOUT);
3527 }
3528 vnic->uc_filter_count = 0;
3529 }
3530 mutex_unlock(&bp->hwrm_cmd_lock);
3531
3532 return rc;
3533}
3534
3535static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3536{
3537 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3538 struct hwrm_vnic_tpa_cfg_input req = {0};
3539
3540 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3541
3542 if (tpa_flags) {
3543 u16 mss = bp->dev->mtu - 40;
3544 u32 nsegs, n, segs = 0, flags;
3545
3546 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3547 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3548 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3549 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3550 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3551 if (tpa_flags & BNXT_FLAG_GRO)
3552 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3553
3554 req.flags = cpu_to_le32(flags);
3555
3556 req.enables =
3557 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003558 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3559 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003560
3561 /* Number of segs are log2 units, and first packet is not
3562 * included as part of this units.
3563 */
Michael Chan2839f282016-04-25 02:30:50 -04003564 if (mss <= BNXT_RX_PAGE_SIZE) {
3565 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003566 nsegs = (MAX_SKB_FRAGS - 1) * n;
3567 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003568 n = mss / BNXT_RX_PAGE_SIZE;
3569 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003570 n++;
3571 nsegs = (MAX_SKB_FRAGS - n) / n;
3572 }
3573
3574 segs = ilog2(nsegs);
3575 req.max_agg_segs = cpu_to_le16(segs);
3576 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003577
3578 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003579 }
3580 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3581
3582 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3583}
3584
3585static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3586{
3587 u32 i, j, max_rings;
3588 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3589 struct hwrm_vnic_rss_cfg_input req = {0};
3590
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003591 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003592 return 0;
3593
3594 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3595 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003596 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003597 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3598 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3599 max_rings = bp->rx_nr_rings - 1;
3600 else
3601 max_rings = bp->rx_nr_rings;
3602 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003603 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003604 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003605
3606 /* Fill the RSS indirection table with ring group ids */
3607 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3608 if (j == max_rings)
3609 j = 0;
3610 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3611 }
3612
3613 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3614 req.hash_key_tbl_addr =
3615 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3616 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003617 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003618 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3619}
3620
3621static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3622{
3623 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3624 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3625
3626 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3627 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3628 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3629 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3630 req.enables =
3631 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3632 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3633 /* thresholds not implemented in firmware yet */
3634 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3635 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3636 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3637 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3638}
3639
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003640static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3641 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003642{
3643 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3644
3645 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3646 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003647 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003648
3649 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003650 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003651}
3652
3653static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3654{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003655 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003656
3657 for (i = 0; i < bp->nr_vnics; i++) {
3658 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3659
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003660 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3661 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3662 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3663 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003664 }
3665 bp->rsscos_nr_ctxs = 0;
3666}
3667
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003668static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003669{
3670 int rc;
3671 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3672 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3673 bp->hwrm_cmd_resp_addr;
3674
3675 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3676 -1);
3677
3678 mutex_lock(&bp->hwrm_cmd_lock);
3679 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3680 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003681 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003682 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3683 mutex_unlock(&bp->hwrm_cmd_lock);
3684
3685 return rc;
3686}
3687
Michael Chana588e452016-12-07 00:26:21 -05003688int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003689{
Michael Chanb81a90d2016-01-02 23:45:01 -05003690 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003691 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3692 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003693 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003694
3695 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003696
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003697 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3698 /* Only RSS support for now TBD: COS & LB */
3699 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3700 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3701 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3702 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003703 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3704 req.rss_rule =
3705 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3706 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3707 VNIC_CFG_REQ_ENABLES_MRU);
3708 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003709 } else {
3710 req.rss_rule = cpu_to_le16(0xffff);
3711 }
3712
3713 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3714 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003715 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3716 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3717 } else {
3718 req.cos_rule = cpu_to_le16(0xffff);
3719 }
3720
Michael Chanc0c050c2015-10-22 16:01:17 -04003721 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003722 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003723 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003724 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003725 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3726 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003727
Michael Chanb81a90d2016-01-02 23:45:01 -05003728 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003729 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3730 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3731
3732 req.lb_rule = cpu_to_le16(0xffff);
3733 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3734 VLAN_HLEN);
3735
Michael Chancf6645f2016-06-13 02:25:28 -04003736#ifdef CONFIG_BNXT_SRIOV
3737 if (BNXT_VF(bp))
3738 def_vlan = bp->vf.vlan;
3739#endif
3740 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003741 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003742 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3743 req.flags |=
3744 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003745
3746 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3747}
3748
3749static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3750{
3751 u32 rc = 0;
3752
3753 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3754 struct hwrm_vnic_free_input req = {0};
3755
3756 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3757 req.vnic_id =
3758 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3759
3760 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3761 if (rc)
3762 return rc;
3763 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3764 }
3765 return rc;
3766}
3767
3768static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3769{
3770 u16 i;
3771
3772 for (i = 0; i < bp->nr_vnics; i++)
3773 bnxt_hwrm_vnic_free_one(bp, i);
3774}
3775
Michael Chanb81a90d2016-01-02 23:45:01 -05003776static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3777 unsigned int start_rx_ring_idx,
3778 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003779{
Michael Chanb81a90d2016-01-02 23:45:01 -05003780 int rc = 0;
3781 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003782 struct hwrm_vnic_alloc_input req = {0};
3783 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3784
3785 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003786 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3787 grp_idx = bp->rx_ring[i].bnapi->index;
3788 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003789 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003790 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003791 break;
3792 }
3793 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003794 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003795 }
3796
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003797 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3798 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003799 if (vnic_id == 0)
3800 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3801
3802 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3803
3804 mutex_lock(&bp->hwrm_cmd_lock);
3805 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3806 if (!rc)
3807 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3808 mutex_unlock(&bp->hwrm_cmd_lock);
3809 return rc;
3810}
3811
Michael Chan8fdefd62016-12-29 12:13:36 -05003812static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3813{
3814 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3815 struct hwrm_vnic_qcaps_input req = {0};
3816 int rc;
3817
3818 if (bp->hwrm_spec_code < 0x10600)
3819 return 0;
3820
3821 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3822 mutex_lock(&bp->hwrm_cmd_lock);
3823 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3824 if (!rc) {
3825 if (resp->flags &
3826 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3827 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3828 }
3829 mutex_unlock(&bp->hwrm_cmd_lock);
3830 return rc;
3831}
3832
Michael Chanc0c050c2015-10-22 16:01:17 -04003833static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3834{
3835 u16 i;
3836 u32 rc = 0;
3837
3838 mutex_lock(&bp->hwrm_cmd_lock);
3839 for (i = 0; i < bp->rx_nr_rings; i++) {
3840 struct hwrm_ring_grp_alloc_input req = {0};
3841 struct hwrm_ring_grp_alloc_output *resp =
3842 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003843 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003844
3845 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3846
Michael Chanb81a90d2016-01-02 23:45:01 -05003847 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3848 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3849 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3850 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003851
3852 rc = _hwrm_send_message(bp, &req, sizeof(req),
3853 HWRM_CMD_TIMEOUT);
3854 if (rc)
3855 break;
3856
Michael Chanb81a90d2016-01-02 23:45:01 -05003857 bp->grp_info[grp_idx].fw_grp_id =
3858 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003859 }
3860 mutex_unlock(&bp->hwrm_cmd_lock);
3861 return rc;
3862}
3863
3864static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3865{
3866 u16 i;
3867 u32 rc = 0;
3868 struct hwrm_ring_grp_free_input req = {0};
3869
3870 if (!bp->grp_info)
3871 return 0;
3872
3873 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3874
3875 mutex_lock(&bp->hwrm_cmd_lock);
3876 for (i = 0; i < bp->cp_nr_rings; i++) {
3877 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3878 continue;
3879 req.ring_group_id =
3880 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3881
3882 rc = _hwrm_send_message(bp, &req, sizeof(req),
3883 HWRM_CMD_TIMEOUT);
3884 if (rc)
3885 break;
3886 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3887 }
3888 mutex_unlock(&bp->hwrm_cmd_lock);
3889 return rc;
3890}
3891
3892static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3893 struct bnxt_ring_struct *ring,
3894 u32 ring_type, u32 map_index,
3895 u32 stats_ctx_id)
3896{
3897 int rc = 0, err = 0;
3898 struct hwrm_ring_alloc_input req = {0};
3899 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3900 u16 ring_id;
3901
3902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3903
3904 req.enables = 0;
3905 if (ring->nr_pages > 1) {
3906 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3907 /* Page size is in log2 units */
3908 req.page_size = BNXT_PAGE_SHIFT;
3909 req.page_tbl_depth = 1;
3910 } else {
3911 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3912 }
3913 req.fbo = 0;
3914 /* Association of ring index with doorbell index and MSIX number */
3915 req.logical_id = cpu_to_le16(map_index);
3916
3917 switch (ring_type) {
3918 case HWRM_RING_ALLOC_TX:
3919 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3920 /* Association of transmit ring with completion ring */
3921 req.cmpl_ring_id =
3922 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3923 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3924 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3925 req.queue_id = cpu_to_le16(ring->queue_id);
3926 break;
3927 case HWRM_RING_ALLOC_RX:
3928 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3929 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3930 break;
3931 case HWRM_RING_ALLOC_AGG:
3932 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3933 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3934 break;
3935 case HWRM_RING_ALLOC_CMPL:
3936 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3937 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3938 if (bp->flags & BNXT_FLAG_USING_MSIX)
3939 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3940 break;
3941 default:
3942 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3943 ring_type);
3944 return -1;
3945 }
3946
3947 mutex_lock(&bp->hwrm_cmd_lock);
3948 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3949 err = le16_to_cpu(resp->error_code);
3950 ring_id = le16_to_cpu(resp->ring_id);
3951 mutex_unlock(&bp->hwrm_cmd_lock);
3952
3953 if (rc || err) {
3954 switch (ring_type) {
3955 case RING_FREE_REQ_RING_TYPE_CMPL:
3956 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3957 rc, err);
3958 return -1;
3959
3960 case RING_FREE_REQ_RING_TYPE_RX:
3961 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3962 rc, err);
3963 return -1;
3964
3965 case RING_FREE_REQ_RING_TYPE_TX:
3966 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3967 rc, err);
3968 return -1;
3969
3970 default:
3971 netdev_err(bp->dev, "Invalid ring\n");
3972 return -1;
3973 }
3974 }
3975 ring->fw_ring_id = ring_id;
3976 return rc;
3977}
3978
Michael Chan486b5c22016-12-29 12:13:42 -05003979static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
3980{
3981 int rc;
3982
3983 if (BNXT_PF(bp)) {
3984 struct hwrm_func_cfg_input req = {0};
3985
3986 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
3987 req.fid = cpu_to_le16(0xffff);
3988 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3989 req.async_event_cr = cpu_to_le16(idx);
3990 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3991 } else {
3992 struct hwrm_func_vf_cfg_input req = {0};
3993
3994 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
3995 req.enables =
3996 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3997 req.async_event_cr = cpu_to_le16(idx);
3998 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3999 }
4000 return rc;
4001}
4002
Michael Chanc0c050c2015-10-22 16:01:17 -04004003static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4004{
4005 int i, rc = 0;
4006
Michael Chanedd0c2c2015-12-27 18:19:19 -05004007 for (i = 0; i < bp->cp_nr_rings; i++) {
4008 struct bnxt_napi *bnapi = bp->bnapi[i];
4009 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4010 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004011
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04004012 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004013 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4014 INVALID_STATS_CTX_ID);
4015 if (rc)
4016 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004017 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4018 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05004019
4020 if (!i) {
4021 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4022 if (rc)
4023 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4024 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004025 }
4026
Michael Chanedd0c2c2015-12-27 18:19:19 -05004027 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004028 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004029 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004030 u32 map_idx = txr->bnapi->index;
4031 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04004032
Michael Chanb81a90d2016-01-02 23:45:01 -05004033 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4034 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004035 if (rc)
4036 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004037 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004038 }
4039
Michael Chanedd0c2c2015-12-27 18:19:19 -05004040 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004041 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004042 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004043 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04004044
Michael Chanb81a90d2016-01-02 23:45:01 -05004045 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4046 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05004047 if (rc)
4048 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05004049 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05004050 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004051 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004052 }
4053
4054 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4055 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004056 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004057 struct bnxt_ring_struct *ring =
4058 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004059 u32 grp_idx = rxr->bnapi->index;
4060 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004061
4062 rc = hwrm_ring_alloc_send_msg(bp, ring,
4063 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05004064 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04004065 INVALID_STATS_CTX_ID);
4066 if (rc)
4067 goto err_out;
4068
Michael Chanb81a90d2016-01-02 23:45:01 -05004069 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04004070 writel(DB_KEY_RX | rxr->rx_agg_prod,
4071 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05004072 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004073 }
4074 }
4075err_out:
4076 return rc;
4077}
4078
4079static int hwrm_ring_free_send_msg(struct bnxt *bp,
4080 struct bnxt_ring_struct *ring,
4081 u32 ring_type, int cmpl_ring_id)
4082{
4083 int rc;
4084 struct hwrm_ring_free_input req = {0};
4085 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4086 u16 error_code;
4087
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05004088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004089 req.ring_type = ring_type;
4090 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4091
4092 mutex_lock(&bp->hwrm_cmd_lock);
4093 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4094 error_code = le16_to_cpu(resp->error_code);
4095 mutex_unlock(&bp->hwrm_cmd_lock);
4096
4097 if (rc || error_code) {
4098 switch (ring_type) {
4099 case RING_FREE_REQ_RING_TYPE_CMPL:
4100 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4101 rc);
4102 return rc;
4103 case RING_FREE_REQ_RING_TYPE_RX:
4104 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4105 rc);
4106 return rc;
4107 case RING_FREE_REQ_RING_TYPE_TX:
4108 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4109 rc);
4110 return rc;
4111 default:
4112 netdev_err(bp->dev, "Invalid ring\n");
4113 return -1;
4114 }
4115 }
4116 return 0;
4117}
4118
Michael Chanedd0c2c2015-12-27 18:19:19 -05004119static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004120{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004121 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004122
4123 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004124 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004125
Michael Chanedd0c2c2015-12-27 18:19:19 -05004126 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004127 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004128 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004129 u32 grp_idx = txr->bnapi->index;
4130 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004131
Michael Chanedd0c2c2015-12-27 18:19:19 -05004132 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4133 hwrm_ring_free_send_msg(bp, ring,
4134 RING_FREE_REQ_RING_TYPE_TX,
4135 close_path ? cmpl_ring_id :
4136 INVALID_HW_RING_ID);
4137 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004138 }
4139 }
4140
Michael Chanedd0c2c2015-12-27 18:19:19 -05004141 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004142 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004143 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004144 u32 grp_idx = rxr->bnapi->index;
4145 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004146
Michael Chanedd0c2c2015-12-27 18:19:19 -05004147 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4148 hwrm_ring_free_send_msg(bp, ring,
4149 RING_FREE_REQ_RING_TYPE_RX,
4150 close_path ? cmpl_ring_id :
4151 INVALID_HW_RING_ID);
4152 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004153 bp->grp_info[grp_idx].rx_fw_ring_id =
4154 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004155 }
4156 }
4157
Michael Chanedd0c2c2015-12-27 18:19:19 -05004158 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004159 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004160 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004161 u32 grp_idx = rxr->bnapi->index;
4162 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004163
Michael Chanedd0c2c2015-12-27 18:19:19 -05004164 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4165 hwrm_ring_free_send_msg(bp, ring,
4166 RING_FREE_REQ_RING_TYPE_RX,
4167 close_path ? cmpl_ring_id :
4168 INVALID_HW_RING_ID);
4169 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004170 bp->grp_info[grp_idx].agg_fw_ring_id =
4171 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004172 }
4173 }
4174
Michael Chan9d8bc092016-12-29 12:13:33 -05004175 /* The completion rings are about to be freed. After that the
4176 * IRQ doorbell will not work anymore. So we need to disable
4177 * IRQ here.
4178 */
4179 bnxt_disable_int_sync(bp);
4180
Michael Chanedd0c2c2015-12-27 18:19:19 -05004181 for (i = 0; i < bp->cp_nr_rings; i++) {
4182 struct bnxt_napi *bnapi = bp->bnapi[i];
4183 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4184 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004185
Michael Chanedd0c2c2015-12-27 18:19:19 -05004186 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4187 hwrm_ring_free_send_msg(bp, ring,
4188 RING_FREE_REQ_RING_TYPE_CMPL,
4189 INVALID_HW_RING_ID);
4190 ring->fw_ring_id = INVALID_HW_RING_ID;
4191 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004192 }
4193 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004194}
4195
Michael Chan391be5c2016-12-29 12:13:41 -05004196/* Caller must hold bp->hwrm_cmd_lock */
4197int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4198{
4199 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4200 struct hwrm_func_qcfg_input req = {0};
4201 int rc;
4202
4203 if (bp->hwrm_spec_code < 0x10601)
4204 return 0;
4205
4206 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4207 req.fid = cpu_to_le16(fid);
4208 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4209 if (!rc)
4210 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4211
4212 return rc;
4213}
4214
4215int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
4216{
4217 struct hwrm_func_cfg_input req = {0};
4218 int rc;
4219
4220 if (bp->hwrm_spec_code < 0x10601)
4221 return 0;
4222
4223 if (BNXT_VF(bp))
4224 return 0;
4225
4226 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4227 req.fid = cpu_to_le16(0xffff);
4228 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4229 req.num_tx_rings = cpu_to_le16(*tx_rings);
4230 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4231 if (rc)
4232 return rc;
4233
4234 mutex_lock(&bp->hwrm_cmd_lock);
4235 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4236 mutex_unlock(&bp->hwrm_cmd_lock);
4237 return rc;
4238}
4239
Michael Chanbb053f52016-02-26 04:00:02 -05004240static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4241 u32 buf_tmrs, u16 flags,
4242 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4243{
4244 req->flags = cpu_to_le16(flags);
4245 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4246 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4247 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4248 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4249 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4250 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4251 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4252 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4253}
4254
Michael Chanc0c050c2015-10-22 16:01:17 -04004255int bnxt_hwrm_set_coal(struct bnxt *bp)
4256{
4257 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004258 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4259 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004260 u16 max_buf, max_buf_irq;
4261 u16 buf_tmr, buf_tmr_irq;
4262 u32 flags;
4263
Michael Chandfc9c942016-02-26 04:00:03 -05004264 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4265 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4266 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4267 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004268
Michael Chandfb5b892016-02-26 04:00:01 -05004269 /* Each rx completion (2 records) should be DMAed immediately.
4270 * DMA 1/4 of the completion buffers at a time.
4271 */
4272 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004273 /* max_buf must not be zero */
4274 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004275 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4276 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4277 /* buf timer set to 1/4 of interrupt timer */
4278 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4279 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4280 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004281
4282 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4283
4284 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4285 * if coal_ticks is less than 25 us.
4286 */
Michael Chandfb5b892016-02-26 04:00:01 -05004287 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004288 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4289
Michael Chanbb053f52016-02-26 04:00:02 -05004290 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004291 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4292
4293 /* max_buf must not be zero */
4294 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4295 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4296 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4297 /* buf timer set to 1/4 of interrupt timer */
4298 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4299 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4300 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4301
4302 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4303 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4304 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004305
4306 mutex_lock(&bp->hwrm_cmd_lock);
4307 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004308 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004309
Michael Chandfc9c942016-02-26 04:00:03 -05004310 req = &req_rx;
4311 if (!bnapi->rx_ring)
4312 req = &req_tx;
4313 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4314
4315 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004316 HWRM_CMD_TIMEOUT);
4317 if (rc)
4318 break;
4319 }
4320 mutex_unlock(&bp->hwrm_cmd_lock);
4321 return rc;
4322}
4323
4324static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4325{
4326 int rc = 0, i;
4327 struct hwrm_stat_ctx_free_input req = {0};
4328
4329 if (!bp->bnapi)
4330 return 0;
4331
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004332 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4333 return 0;
4334
Michael Chanc0c050c2015-10-22 16:01:17 -04004335 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4336
4337 mutex_lock(&bp->hwrm_cmd_lock);
4338 for (i = 0; i < bp->cp_nr_rings; i++) {
4339 struct bnxt_napi *bnapi = bp->bnapi[i];
4340 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4341
4342 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4343 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4344
4345 rc = _hwrm_send_message(bp, &req, sizeof(req),
4346 HWRM_CMD_TIMEOUT);
4347 if (rc)
4348 break;
4349
4350 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4351 }
4352 }
4353 mutex_unlock(&bp->hwrm_cmd_lock);
4354 return rc;
4355}
4356
4357static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4358{
4359 int rc = 0, i;
4360 struct hwrm_stat_ctx_alloc_input req = {0};
4361 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4362
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004363 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4364 return 0;
4365
Michael Chanc0c050c2015-10-22 16:01:17 -04004366 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4367
Michael Chan51f30782016-07-01 18:46:29 -04004368 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004369
4370 mutex_lock(&bp->hwrm_cmd_lock);
4371 for (i = 0; i < bp->cp_nr_rings; i++) {
4372 struct bnxt_napi *bnapi = bp->bnapi[i];
4373 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4374
4375 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4376
4377 rc = _hwrm_send_message(bp, &req, sizeof(req),
4378 HWRM_CMD_TIMEOUT);
4379 if (rc)
4380 break;
4381
4382 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4383
4384 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4385 }
4386 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004387 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004388}
4389
Michael Chancf6645f2016-06-13 02:25:28 -04004390static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4391{
4392 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004393 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004394 int rc;
4395
4396 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4397 req.fid = cpu_to_le16(0xffff);
4398 mutex_lock(&bp->hwrm_cmd_lock);
4399 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4400 if (rc)
4401 goto func_qcfg_exit;
4402
4403#ifdef CONFIG_BNXT_SRIOV
4404 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004405 struct bnxt_vf_info *vf = &bp->vf;
4406
4407 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4408 }
4409#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004410 switch (resp->port_partition_type) {
4411 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4412 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4413 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4414 bp->port_partition_type = resp->port_partition_type;
4415 break;
4416 }
Michael Chancf6645f2016-06-13 02:25:28 -04004417
4418func_qcfg_exit:
4419 mutex_unlock(&bp->hwrm_cmd_lock);
4420 return rc;
4421}
4422
Michael Chan7b08f662016-12-07 00:26:18 -05004423static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004424{
4425 int rc = 0;
4426 struct hwrm_func_qcaps_input req = {0};
4427 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4428
4429 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4430 req.fid = cpu_to_le16(0xffff);
4431
4432 mutex_lock(&bp->hwrm_cmd_lock);
4433 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4434 if (rc)
4435 goto hwrm_func_qcaps_exit;
4436
Michael Chane4060d32016-12-07 00:26:19 -05004437 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4438 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4439 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4440 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4441
Michael Chan7cc5a202016-09-19 03:58:05 -04004442 bp->tx_push_thresh = 0;
4443 if (resp->flags &
4444 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4445 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4446
Michael Chanc0c050c2015-10-22 16:01:17 -04004447 if (BNXT_PF(bp)) {
4448 struct bnxt_pf_info *pf = &bp->pf;
4449
4450 pf->fw_fid = le16_to_cpu(resp->fid);
4451 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004452 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004453 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004454 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004455 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4456 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4457 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004458 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004459 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4460 if (!pf->max_hw_ring_grps)
4461 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004462 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4463 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4464 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4465 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4466 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4467 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4468 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4469 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4470 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4471 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4472 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4473 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004474#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004475 struct bnxt_vf_info *vf = &bp->vf;
4476
4477 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004478
4479 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4480 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4481 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4482 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004483 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4484 if (!vf->max_hw_ring_grps)
4485 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004486 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4487 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4488 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004489
4490 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004491 mutex_unlock(&bp->hwrm_cmd_lock);
4492
4493 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004494 /* overwrite netdev dev_adr with admin VF MAC */
4495 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004496 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004497 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004498 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4499 }
4500 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004501#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004502 }
4503
Michael Chanc0c050c2015-10-22 16:01:17 -04004504hwrm_func_qcaps_exit:
4505 mutex_unlock(&bp->hwrm_cmd_lock);
4506 return rc;
4507}
4508
4509static int bnxt_hwrm_func_reset(struct bnxt *bp)
4510{
4511 struct hwrm_func_reset_input req = {0};
4512
4513 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4514 req.enables = 0;
4515
4516 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4517}
4518
4519static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4520{
4521 int rc = 0;
4522 struct hwrm_queue_qportcfg_input req = {0};
4523 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4524 u8 i, *qptr;
4525
4526 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4527
4528 mutex_lock(&bp->hwrm_cmd_lock);
4529 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4530 if (rc)
4531 goto qportcfg_exit;
4532
4533 if (!resp->max_configurable_queues) {
4534 rc = -EINVAL;
4535 goto qportcfg_exit;
4536 }
4537 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004538 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004539 if (bp->max_tc > BNXT_MAX_QUEUE)
4540 bp->max_tc = BNXT_MAX_QUEUE;
4541
Michael Chan441cabb2016-09-19 03:58:02 -04004542 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4543 bp->max_tc = 1;
4544
Michael Chan87c374d2016-12-02 21:17:16 -05004545 if (bp->max_lltc > bp->max_tc)
4546 bp->max_lltc = bp->max_tc;
4547
Michael Chanc0c050c2015-10-22 16:01:17 -04004548 qptr = &resp->queue_id0;
4549 for (i = 0; i < bp->max_tc; i++) {
4550 bp->q_info[i].queue_id = *qptr++;
4551 bp->q_info[i].queue_profile = *qptr++;
4552 }
4553
4554qportcfg_exit:
4555 mutex_unlock(&bp->hwrm_cmd_lock);
4556 return rc;
4557}
4558
4559static int bnxt_hwrm_ver_get(struct bnxt *bp)
4560{
4561 int rc;
4562 struct hwrm_ver_get_input req = {0};
4563 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4564
Michael Chane6ef2692016-03-28 19:46:05 -04004565 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004566 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4567 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4568 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4569 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4570 mutex_lock(&bp->hwrm_cmd_lock);
4571 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4572 if (rc)
4573 goto hwrm_ver_get_exit;
4574
4575 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4576
Michael Chan11f15ed2016-04-05 14:08:55 -04004577 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4578 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004579 if (resp->hwrm_intf_maj < 1) {
4580 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004581 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004582 resp->hwrm_intf_upd);
4583 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004584 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004585 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004586 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4587 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4588
Michael Chanff4fe812016-02-26 04:00:04 -05004589 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4590 if (!bp->hwrm_cmd_timeout)
4591 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4592
Michael Chane6ef2692016-03-28 19:46:05 -04004593 if (resp->hwrm_intf_maj >= 1)
4594 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4595
Michael Chan659c8052016-06-13 02:25:33 -04004596 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004597 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4598 !resp->chip_metal)
4599 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004600
Michael Chanc0c050c2015-10-22 16:01:17 -04004601hwrm_ver_get_exit:
4602 mutex_unlock(&bp->hwrm_cmd_lock);
4603 return rc;
4604}
4605
Rob Swindell5ac67d82016-09-19 03:58:03 -04004606int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4607{
Rob Swindell878786d2016-09-20 03:36:33 -04004608#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004609 struct hwrm_fw_set_time_input req = {0};
4610 struct rtc_time tm;
4611 struct timeval tv;
4612
4613 if (bp->hwrm_spec_code < 0x10400)
4614 return -EOPNOTSUPP;
4615
4616 do_gettimeofday(&tv);
4617 rtc_time_to_tm(tv.tv_sec, &tm);
4618 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4619 req.year = cpu_to_le16(1900 + tm.tm_year);
4620 req.month = 1 + tm.tm_mon;
4621 req.day = tm.tm_mday;
4622 req.hour = tm.tm_hour;
4623 req.minute = tm.tm_min;
4624 req.second = tm.tm_sec;
4625 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004626#else
4627 return -EOPNOTSUPP;
4628#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004629}
4630
Michael Chan3bdf56c2016-03-07 15:38:45 -05004631static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4632{
4633 int rc;
4634 struct bnxt_pf_info *pf = &bp->pf;
4635 struct hwrm_port_qstats_input req = {0};
4636
4637 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4638 return 0;
4639
4640 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4641 req.port_id = cpu_to_le16(pf->port_id);
4642 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4643 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4644 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4645 return rc;
4646}
4647
Michael Chanc0c050c2015-10-22 16:01:17 -04004648static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4649{
4650 if (bp->vxlan_port_cnt) {
4651 bnxt_hwrm_tunnel_dst_port_free(
4652 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4653 }
4654 bp->vxlan_port_cnt = 0;
4655 if (bp->nge_port_cnt) {
4656 bnxt_hwrm_tunnel_dst_port_free(
4657 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4658 }
4659 bp->nge_port_cnt = 0;
4660}
4661
4662static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4663{
4664 int rc, i;
4665 u32 tpa_flags = 0;
4666
4667 if (set_tpa)
4668 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4669 for (i = 0; i < bp->nr_vnics; i++) {
4670 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4671 if (rc) {
4672 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4673 rc, i);
4674 return rc;
4675 }
4676 }
4677 return 0;
4678}
4679
4680static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4681{
4682 int i;
4683
4684 for (i = 0; i < bp->nr_vnics; i++)
4685 bnxt_hwrm_vnic_set_rss(bp, i, false);
4686}
4687
4688static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4689 bool irq_re_init)
4690{
4691 if (bp->vnic_info) {
4692 bnxt_hwrm_clear_vnic_filter(bp);
4693 /* clear all RSS setting before free vnic ctx */
4694 bnxt_hwrm_clear_vnic_rss(bp);
4695 bnxt_hwrm_vnic_ctx_free(bp);
4696 /* before free the vnic, undo the vnic tpa settings */
4697 if (bp->flags & BNXT_FLAG_TPA)
4698 bnxt_set_tpa(bp, false);
4699 bnxt_hwrm_vnic_free(bp);
4700 }
4701 bnxt_hwrm_ring_free(bp, close_path);
4702 bnxt_hwrm_ring_grp_free(bp);
4703 if (irq_re_init) {
4704 bnxt_hwrm_stat_ctx_free(bp);
4705 bnxt_hwrm_free_tunnel_ports(bp);
4706 }
4707}
4708
4709static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4710{
Michael Chanae10ae72016-12-29 12:13:38 -05004711 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004712 int rc;
4713
Michael Chanae10ae72016-12-29 12:13:38 -05004714 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4715 goto skip_rss_ctx;
4716
Michael Chanc0c050c2015-10-22 16:01:17 -04004717 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004718 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004719 if (rc) {
4720 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4721 vnic_id, rc);
4722 goto vnic_setup_err;
4723 }
4724 bp->rsscos_nr_ctxs++;
4725
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004726 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4727 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4728 if (rc) {
4729 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4730 vnic_id, rc);
4731 goto vnic_setup_err;
4732 }
4733 bp->rsscos_nr_ctxs++;
4734 }
4735
Michael Chanae10ae72016-12-29 12:13:38 -05004736skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004737 /* configure default vnic, ring grp */
4738 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4739 if (rc) {
4740 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4741 vnic_id, rc);
4742 goto vnic_setup_err;
4743 }
4744
4745 /* Enable RSS hashing on vnic */
4746 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4747 if (rc) {
4748 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4749 vnic_id, rc);
4750 goto vnic_setup_err;
4751 }
4752
4753 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4754 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4755 if (rc) {
4756 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4757 vnic_id, rc);
4758 }
4759 }
4760
4761vnic_setup_err:
4762 return rc;
4763}
4764
4765static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4766{
4767#ifdef CONFIG_RFS_ACCEL
4768 int i, rc = 0;
4769
4770 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004771 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004772 u16 vnic_id = i + 1;
4773 u16 ring_id = i;
4774
4775 if (vnic_id >= bp->nr_vnics)
4776 break;
4777
Michael Chanae10ae72016-12-29 12:13:38 -05004778 vnic = &bp->vnic_info[vnic_id];
4779 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4780 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4781 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004782 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004783 if (rc) {
4784 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4785 vnic_id, rc);
4786 break;
4787 }
4788 rc = bnxt_setup_vnic(bp, vnic_id);
4789 if (rc)
4790 break;
4791 }
4792 return rc;
4793#else
4794 return 0;
4795#endif
4796}
4797
Michael Chan17c71ac2016-07-01 18:46:27 -04004798/* Allow PF and VF with default VLAN to be in promiscuous mode */
4799static bool bnxt_promisc_ok(struct bnxt *bp)
4800{
4801#ifdef CONFIG_BNXT_SRIOV
4802 if (BNXT_VF(bp) && !bp->vf.vlan)
4803 return false;
4804#endif
4805 return true;
4806}
4807
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004808static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4809{
4810 unsigned int rc = 0;
4811
4812 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4813 if (rc) {
4814 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4815 rc);
4816 return rc;
4817 }
4818
4819 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4820 if (rc) {
4821 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4822 rc);
4823 return rc;
4824 }
4825 return rc;
4826}
4827
Michael Chanb664f002015-12-02 01:54:08 -05004828static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004829static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004830
Michael Chanc0c050c2015-10-22 16:01:17 -04004831static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4832{
Michael Chan7d2837d2016-05-04 16:56:44 -04004833 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004834 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004835 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004836
4837 if (irq_re_init) {
4838 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4839 if (rc) {
4840 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4841 rc);
4842 goto err_out;
4843 }
4844 }
4845
4846 rc = bnxt_hwrm_ring_alloc(bp);
4847 if (rc) {
4848 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4849 goto err_out;
4850 }
4851
4852 rc = bnxt_hwrm_ring_grp_alloc(bp);
4853 if (rc) {
4854 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4855 goto err_out;
4856 }
4857
Prashant Sreedharan76595192016-07-18 07:15:22 -04004858 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4859 rx_nr_rings--;
4860
Michael Chanc0c050c2015-10-22 16:01:17 -04004861 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004862 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004863 if (rc) {
4864 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4865 goto err_out;
4866 }
4867
4868 rc = bnxt_setup_vnic(bp, 0);
4869 if (rc)
4870 goto err_out;
4871
4872 if (bp->flags & BNXT_FLAG_RFS) {
4873 rc = bnxt_alloc_rfs_vnics(bp);
4874 if (rc)
4875 goto err_out;
4876 }
4877
4878 if (bp->flags & BNXT_FLAG_TPA) {
4879 rc = bnxt_set_tpa(bp, true);
4880 if (rc)
4881 goto err_out;
4882 }
4883
4884 if (BNXT_VF(bp))
4885 bnxt_update_vf_mac(bp);
4886
4887 /* Filter for default vnic 0 */
4888 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4889 if (rc) {
4890 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4891 goto err_out;
4892 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004893 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004894
Michael Chan7d2837d2016-05-04 16:56:44 -04004895 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004896
Michael Chan17c71ac2016-07-01 18:46:27 -04004897 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004898 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4899
4900 if (bp->dev->flags & IFF_ALLMULTI) {
4901 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4902 vnic->mc_list_count = 0;
4903 } else {
4904 u32 mask = 0;
4905
4906 bnxt_mc_list_updated(bp, &mask);
4907 vnic->rx_mask |= mask;
4908 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004909
Michael Chanb664f002015-12-02 01:54:08 -05004910 rc = bnxt_cfg_rx_mode(bp);
4911 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004912 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004913
4914 rc = bnxt_hwrm_set_coal(bp);
4915 if (rc)
4916 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004917 rc);
4918
4919 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4920 rc = bnxt_setup_nitroa0_vnic(bp);
4921 if (rc)
4922 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4923 rc);
4924 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004925
Michael Chancf6645f2016-06-13 02:25:28 -04004926 if (BNXT_VF(bp)) {
4927 bnxt_hwrm_func_qcfg(bp);
4928 netdev_update_features(bp->dev);
4929 }
4930
Michael Chanc0c050c2015-10-22 16:01:17 -04004931 return 0;
4932
4933err_out:
4934 bnxt_hwrm_resource_free(bp, 0, true);
4935
4936 return rc;
4937}
4938
4939static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4940{
4941 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4942 return 0;
4943}
4944
4945static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4946{
4947 bnxt_init_rx_rings(bp);
4948 bnxt_init_tx_rings(bp);
4949 bnxt_init_ring_grps(bp, irq_re_init);
4950 bnxt_init_vnics(bp);
4951
4952 return bnxt_init_chip(bp, irq_re_init);
4953}
4954
Michael Chanc0c050c2015-10-22 16:01:17 -04004955static int bnxt_set_real_num_queues(struct bnxt *bp)
4956{
4957 int rc;
4958 struct net_device *dev = bp->dev;
4959
4960 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4961 if (rc)
4962 return rc;
4963
4964 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4965 if (rc)
4966 return rc;
4967
4968#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004969 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004970 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004971#endif
4972
4973 return rc;
4974}
4975
Michael Chan6e6c5a52016-01-02 23:45:02 -05004976static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4977 bool shared)
4978{
4979 int _rx = *rx, _tx = *tx;
4980
4981 if (shared) {
4982 *rx = min_t(int, _rx, max);
4983 *tx = min_t(int, _tx, max);
4984 } else {
4985 if (max < 2)
4986 return -ENOMEM;
4987
4988 while (_rx + _tx > max) {
4989 if (_rx > _tx && _rx > 1)
4990 _rx--;
4991 else if (_tx > 1)
4992 _tx--;
4993 }
4994 *rx = _rx;
4995 *tx = _tx;
4996 }
4997 return 0;
4998}
4999
Michael Chan78095922016-12-07 00:26:16 -05005000static void bnxt_setup_msix(struct bnxt *bp)
5001{
5002 const int len = sizeof(bp->irq_tbl[0].name);
5003 struct net_device *dev = bp->dev;
5004 int tcs, i;
5005
5006 tcs = netdev_get_num_tc(dev);
5007 if (tcs > 1) {
5008 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
5009 if (bp->tx_nr_rings_per_tc == 0) {
5010 netdev_reset_tc(dev);
5011 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
5012 } else {
5013 int i, off, count;
5014
5015 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
5016 for (i = 0; i < tcs; i++) {
5017 count = bp->tx_nr_rings_per_tc;
5018 off = i * count;
5019 netdev_set_tc_queue(dev, i, count, off);
5020 }
5021 }
5022 }
5023
5024 for (i = 0; i < bp->cp_nr_rings; i++) {
5025 char *attr;
5026
5027 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5028 attr = "TxRx";
5029 else if (i < bp->rx_nr_rings)
5030 attr = "rx";
5031 else
5032 attr = "tx";
5033
5034 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5035 i);
5036 bp->irq_tbl[i].handler = bnxt_msix;
5037 }
5038}
5039
5040static void bnxt_setup_inta(struct bnxt *bp)
5041{
5042 const int len = sizeof(bp->irq_tbl[0].name);
5043
5044 if (netdev_get_num_tc(bp->dev))
5045 netdev_reset_tc(bp->dev);
5046
5047 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5048 0);
5049 bp->irq_tbl[0].handler = bnxt_inta;
5050}
5051
5052static int bnxt_setup_int_mode(struct bnxt *bp)
5053{
5054 int rc;
5055
5056 if (bp->flags & BNXT_FLAG_USING_MSIX)
5057 bnxt_setup_msix(bp);
5058 else
5059 bnxt_setup_inta(bp);
5060
5061 rc = bnxt_set_real_num_queues(bp);
5062 return rc;
5063}
5064
Michael Chanb7429952017-01-13 01:32:00 -05005065#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05005066static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5067{
5068#if defined(CONFIG_BNXT_SRIOV)
5069 if (BNXT_VF(bp))
5070 return bp->vf.max_rsscos_ctxs;
5071#endif
5072 return bp->pf.max_rsscos_ctxs;
5073}
5074
5075static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5076{
5077#if defined(CONFIG_BNXT_SRIOV)
5078 if (BNXT_VF(bp))
5079 return bp->vf.max_vnics;
5080#endif
5081 return bp->pf.max_vnics;
5082}
Michael Chanb7429952017-01-13 01:32:00 -05005083#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05005084
Michael Chane4060d32016-12-07 00:26:19 -05005085unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5086{
5087#if defined(CONFIG_BNXT_SRIOV)
5088 if (BNXT_VF(bp))
5089 return bp->vf.max_stat_ctxs;
5090#endif
5091 return bp->pf.max_stat_ctxs;
5092}
5093
Michael Chana588e452016-12-07 00:26:21 -05005094void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5095{
5096#if defined(CONFIG_BNXT_SRIOV)
5097 if (BNXT_VF(bp))
5098 bp->vf.max_stat_ctxs = max;
5099 else
5100#endif
5101 bp->pf.max_stat_ctxs = max;
5102}
5103
Michael Chane4060d32016-12-07 00:26:19 -05005104unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5105{
5106#if defined(CONFIG_BNXT_SRIOV)
5107 if (BNXT_VF(bp))
5108 return bp->vf.max_cp_rings;
5109#endif
5110 return bp->pf.max_cp_rings;
5111}
5112
Michael Chana588e452016-12-07 00:26:21 -05005113void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5114{
5115#if defined(CONFIG_BNXT_SRIOV)
5116 if (BNXT_VF(bp))
5117 bp->vf.max_cp_rings = max;
5118 else
5119#endif
5120 bp->pf.max_cp_rings = max;
5121}
5122
Michael Chan78095922016-12-07 00:26:16 -05005123static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5124{
5125#if defined(CONFIG_BNXT_SRIOV)
5126 if (BNXT_VF(bp))
5127 return bp->vf.max_irqs;
5128#endif
5129 return bp->pf.max_irqs;
5130}
5131
Michael Chan33c26572016-12-07 00:26:15 -05005132void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5133{
5134#if defined(CONFIG_BNXT_SRIOV)
5135 if (BNXT_VF(bp))
5136 bp->vf.max_irqs = max_irqs;
5137 else
5138#endif
5139 bp->pf.max_irqs = max_irqs;
5140}
5141
Michael Chan78095922016-12-07 00:26:16 -05005142static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005143{
Michael Chan01657bc2016-01-02 23:45:03 -05005144 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005145 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005146
Michael Chan78095922016-12-07 00:26:16 -05005147 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005148 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5149 if (!msix_ent)
5150 return -ENOMEM;
5151
5152 for (i = 0; i < total_vecs; i++) {
5153 msix_ent[i].entry = i;
5154 msix_ent[i].vector = 0;
5155 }
5156
Michael Chan01657bc2016-01-02 23:45:03 -05005157 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5158 min = 2;
5159
5160 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005161 if (total_vecs < 0) {
5162 rc = -ENODEV;
5163 goto msix_setup_exit;
5164 }
5165
5166 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5167 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005168 for (i = 0; i < total_vecs; i++)
5169 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005170
Michael Chan78095922016-12-07 00:26:16 -05005171 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005172 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005173 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005174 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005175 if (rc)
5176 goto msix_setup_exit;
5177
Michael Chanc0c050c2015-10-22 16:01:17 -04005178 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005179 bp->cp_nr_rings = (min == 1) ?
5180 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5181 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005182
Michael Chanc0c050c2015-10-22 16:01:17 -04005183 } else {
5184 rc = -ENOMEM;
5185 goto msix_setup_exit;
5186 }
5187 bp->flags |= BNXT_FLAG_USING_MSIX;
5188 kfree(msix_ent);
5189 return 0;
5190
5191msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005192 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5193 kfree(bp->irq_tbl);
5194 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005195 pci_disable_msix(bp->pdev);
5196 kfree(msix_ent);
5197 return rc;
5198}
5199
Michael Chan78095922016-12-07 00:26:16 -05005200static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005201{
Michael Chanc0c050c2015-10-22 16:01:17 -04005202 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005203 if (!bp->irq_tbl)
5204 return -ENOMEM;
5205
5206 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005207 bp->rx_nr_rings = 1;
5208 bp->tx_nr_rings = 1;
5209 bp->cp_nr_rings = 1;
5210 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005211 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005212 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005213 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005214}
5215
Michael Chan78095922016-12-07 00:26:16 -05005216static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005217{
5218 int rc = 0;
5219
5220 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005221 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005222
Michael Chan1fa72e22016-04-25 02:30:49 -04005223 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005224 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005225 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005226 }
5227 return rc;
5228}
5229
Michael Chan78095922016-12-07 00:26:16 -05005230static void bnxt_clear_int_mode(struct bnxt *bp)
5231{
5232 if (bp->flags & BNXT_FLAG_USING_MSIX)
5233 pci_disable_msix(bp->pdev);
5234
5235 kfree(bp->irq_tbl);
5236 bp->irq_tbl = NULL;
5237 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5238}
5239
Michael Chanc0c050c2015-10-22 16:01:17 -04005240static void bnxt_free_irq(struct bnxt *bp)
5241{
5242 struct bnxt_irq *irq;
5243 int i;
5244
5245#ifdef CONFIG_RFS_ACCEL
5246 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5247 bp->dev->rx_cpu_rmap = NULL;
5248#endif
5249 if (!bp->irq_tbl)
5250 return;
5251
5252 for (i = 0; i < bp->cp_nr_rings; i++) {
5253 irq = &bp->irq_tbl[i];
5254 if (irq->requested)
5255 free_irq(irq->vector, bp->bnapi[i]);
5256 irq->requested = 0;
5257 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005258}
5259
5260static int bnxt_request_irq(struct bnxt *bp)
5261{
Michael Chanb81a90d2016-01-02 23:45:01 -05005262 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005263 unsigned long flags = 0;
5264#ifdef CONFIG_RFS_ACCEL
5265 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5266#endif
5267
5268 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5269 flags = IRQF_SHARED;
5270
Michael Chanb81a90d2016-01-02 23:45:01 -05005271 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005272 struct bnxt_irq *irq = &bp->irq_tbl[i];
5273#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005274 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005275 rc = irq_cpu_rmap_add(rmap, irq->vector);
5276 if (rc)
5277 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005278 j);
5279 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005280 }
5281#endif
5282 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5283 bp->bnapi[i]);
5284 if (rc)
5285 break;
5286
5287 irq->requested = 1;
5288 }
5289 return rc;
5290}
5291
5292static void bnxt_del_napi(struct bnxt *bp)
5293{
5294 int i;
5295
5296 if (!bp->bnapi)
5297 return;
5298
5299 for (i = 0; i < bp->cp_nr_rings; i++) {
5300 struct bnxt_napi *bnapi = bp->bnapi[i];
5301
5302 napi_hash_del(&bnapi->napi);
5303 netif_napi_del(&bnapi->napi);
5304 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005305 /* We called napi_hash_del() before netif_napi_del(), we need
5306 * to respect an RCU grace period before freeing napi structures.
5307 */
5308 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005309}
5310
5311static void bnxt_init_napi(struct bnxt *bp)
5312{
5313 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005314 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005315 struct bnxt_napi *bnapi;
5316
5317 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005318 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5319 cp_nr_rings--;
5320 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005321 bnapi = bp->bnapi[i];
5322 netif_napi_add(bp->dev, &bnapi->napi,
5323 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005324 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005325 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5326 bnapi = bp->bnapi[cp_nr_rings];
5327 netif_napi_add(bp->dev, &bnapi->napi,
5328 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005329 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005330 } else {
5331 bnapi = bp->bnapi[0];
5332 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005333 }
5334}
5335
5336static void bnxt_disable_napi(struct bnxt *bp)
5337{
5338 int i;
5339
5340 if (!bp->bnapi)
5341 return;
5342
Michael Chanb356a2e2016-12-29 12:13:31 -05005343 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005344 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005345}
5346
5347static void bnxt_enable_napi(struct bnxt *bp)
5348{
5349 int i;
5350
5351 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005352 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005353 napi_enable(&bp->bnapi[i]->napi);
5354 }
5355}
5356
Michael Chan7df4ae92016-12-02 21:17:17 -05005357void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005358{
5359 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005360 struct bnxt_tx_ring_info *txr;
5361 struct netdev_queue *txq;
5362
Michael Chanb6ab4b02016-01-02 23:44:59 -05005363 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005364 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005365 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005366 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005367 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005368 }
5369 }
5370 /* Stop all TX queues */
5371 netif_tx_disable(bp->dev);
5372 netif_carrier_off(bp->dev);
5373}
5374
Michael Chan7df4ae92016-12-02 21:17:17 -05005375void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005376{
5377 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005378 struct bnxt_tx_ring_info *txr;
5379 struct netdev_queue *txq;
5380
5381 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005382 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005383 txq = netdev_get_tx_queue(bp->dev, i);
5384 txr->dev_state = 0;
5385 }
5386 netif_tx_wake_all_queues(bp->dev);
5387 if (bp->link_info.link_up)
5388 netif_carrier_on(bp->dev);
5389}
5390
5391static void bnxt_report_link(struct bnxt *bp)
5392{
5393 if (bp->link_info.link_up) {
5394 const char *duplex;
5395 const char *flow_ctrl;
5396 u16 speed;
5397
5398 netif_carrier_on(bp->dev);
5399 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5400 duplex = "full";
5401 else
5402 duplex = "half";
5403 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5404 flow_ctrl = "ON - receive & transmit";
5405 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5406 flow_ctrl = "ON - transmit";
5407 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5408 flow_ctrl = "ON - receive";
5409 else
5410 flow_ctrl = "none";
5411 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5412 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5413 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005414 if (bp->flags & BNXT_FLAG_EEE_CAP)
5415 netdev_info(bp->dev, "EEE is %s\n",
5416 bp->eee.eee_active ? "active" :
5417 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005418 } else {
5419 netif_carrier_off(bp->dev);
5420 netdev_err(bp->dev, "NIC Link is Down\n");
5421 }
5422}
5423
Michael Chan170ce012016-04-05 14:08:57 -04005424static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5425{
5426 int rc = 0;
5427 struct hwrm_port_phy_qcaps_input req = {0};
5428 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005429 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005430
5431 if (bp->hwrm_spec_code < 0x10201)
5432 return 0;
5433
5434 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5435
5436 mutex_lock(&bp->hwrm_cmd_lock);
5437 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5438 if (rc)
5439 goto hwrm_phy_qcaps_exit;
5440
5441 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5442 struct ethtool_eee *eee = &bp->eee;
5443 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5444
5445 bp->flags |= BNXT_FLAG_EEE_CAP;
5446 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5447 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5448 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5449 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5450 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5451 }
Michael Chan93ed8112016-06-13 02:25:37 -04005452 link_info->support_auto_speeds =
5453 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005454
5455hwrm_phy_qcaps_exit:
5456 mutex_unlock(&bp->hwrm_cmd_lock);
5457 return rc;
5458}
5459
Michael Chanc0c050c2015-10-22 16:01:17 -04005460static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5461{
5462 int rc = 0;
5463 struct bnxt_link_info *link_info = &bp->link_info;
5464 struct hwrm_port_phy_qcfg_input req = {0};
5465 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5466 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005467 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005468
5469 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5470
5471 mutex_lock(&bp->hwrm_cmd_lock);
5472 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5473 if (rc) {
5474 mutex_unlock(&bp->hwrm_cmd_lock);
5475 return rc;
5476 }
5477
5478 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5479 link_info->phy_link_status = resp->link;
5480 link_info->duplex = resp->duplex;
5481 link_info->pause = resp->pause;
5482 link_info->auto_mode = resp->auto_mode;
5483 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005484 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005485 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005486 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005487 if (link_info->phy_link_status == BNXT_LINK_LINK)
5488 link_info->link_speed = le16_to_cpu(resp->link_speed);
5489 else
5490 link_info->link_speed = 0;
5491 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005492 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5493 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005494 link_info->lp_auto_link_speeds =
5495 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005496 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5497 link_info->phy_ver[0] = resp->phy_maj;
5498 link_info->phy_ver[1] = resp->phy_min;
5499 link_info->phy_ver[2] = resp->phy_bld;
5500 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005501 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005502 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005503 link_info->phy_addr = resp->eee_config_phy_addr &
5504 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005505 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005506
Michael Chan170ce012016-04-05 14:08:57 -04005507 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5508 struct ethtool_eee *eee = &bp->eee;
5509 u16 fw_speeds;
5510
5511 eee->eee_active = 0;
5512 if (resp->eee_config_phy_addr &
5513 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5514 eee->eee_active = 1;
5515 fw_speeds = le16_to_cpu(
5516 resp->link_partner_adv_eee_link_speed_mask);
5517 eee->lp_advertised =
5518 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5519 }
5520
5521 /* Pull initial EEE config */
5522 if (!chng_link_state) {
5523 if (resp->eee_config_phy_addr &
5524 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5525 eee->eee_enabled = 1;
5526
5527 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5528 eee->advertised =
5529 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5530
5531 if (resp->eee_config_phy_addr &
5532 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5533 __le32 tmr;
5534
5535 eee->tx_lpi_enabled = 1;
5536 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5537 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5538 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5539 }
5540 }
5541 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005542 /* TODO: need to add more logic to report VF link */
5543 if (chng_link_state) {
5544 if (link_info->phy_link_status == BNXT_LINK_LINK)
5545 link_info->link_up = 1;
5546 else
5547 link_info->link_up = 0;
5548 if (link_up != link_info->link_up)
5549 bnxt_report_link(bp);
5550 } else {
5551 /* alwasy link down if not require to update link state */
5552 link_info->link_up = 0;
5553 }
5554 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005555
5556 diff = link_info->support_auto_speeds ^ link_info->advertising;
5557 if ((link_info->support_auto_speeds | diff) !=
5558 link_info->support_auto_speeds) {
5559 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005560 * update the advertisement settings. Caller holds RTNL
5561 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005562 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005563 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005564 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005565 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005566 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005567 return 0;
5568}
5569
Michael Chan10289be2016-05-15 03:04:49 -04005570static void bnxt_get_port_module_status(struct bnxt *bp)
5571{
5572 struct bnxt_link_info *link_info = &bp->link_info;
5573 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5574 u8 module_status;
5575
5576 if (bnxt_update_link(bp, true))
5577 return;
5578
5579 module_status = link_info->module_status;
5580 switch (module_status) {
5581 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5582 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5583 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5584 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5585 bp->pf.port_id);
5586 if (bp->hwrm_spec_code >= 0x10201) {
5587 netdev_warn(bp->dev, "Module part number %s\n",
5588 resp->phy_vendor_partnumber);
5589 }
5590 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5591 netdev_warn(bp->dev, "TX is disabled\n");
5592 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5593 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5594 }
5595}
5596
Michael Chanc0c050c2015-10-22 16:01:17 -04005597static void
5598bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5599{
5600 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005601 if (bp->hwrm_spec_code >= 0x10201)
5602 req->auto_pause =
5603 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005604 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5605 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5606 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005607 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005608 req->enables |=
5609 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5610 } else {
5611 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5612 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5613 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5614 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5615 req->enables |=
5616 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005617 if (bp->hwrm_spec_code >= 0x10201) {
5618 req->auto_pause = req->force_pause;
5619 req->enables |= cpu_to_le32(
5620 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5621 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005622 }
5623}
5624
5625static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5626 struct hwrm_port_phy_cfg_input *req)
5627{
5628 u8 autoneg = bp->link_info.autoneg;
5629 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005630 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005631
5632 if (autoneg & BNXT_AUTONEG_SPEED) {
5633 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005634 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005635
5636 req->enables |= cpu_to_le32(
5637 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5638 req->auto_link_speed_mask = cpu_to_le16(advertising);
5639
5640 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5641 req->flags |=
5642 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5643 } else {
5644 req->force_link_speed = cpu_to_le16(fw_link_speed);
5645 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5646 }
5647
Michael Chanc0c050c2015-10-22 16:01:17 -04005648 /* tell chimp that the setting takes effect immediately */
5649 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5650}
5651
5652int bnxt_hwrm_set_pause(struct bnxt *bp)
5653{
5654 struct hwrm_port_phy_cfg_input req = {0};
5655 int rc;
5656
5657 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5658 bnxt_hwrm_set_pause_common(bp, &req);
5659
5660 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5661 bp->link_info.force_link_chng)
5662 bnxt_hwrm_set_link_common(bp, &req);
5663
5664 mutex_lock(&bp->hwrm_cmd_lock);
5665 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5666 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5667 /* since changing of pause setting doesn't trigger any link
5668 * change event, the driver needs to update the current pause
5669 * result upon successfully return of the phy_cfg command
5670 */
5671 bp->link_info.pause =
5672 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5673 bp->link_info.auto_pause_setting = 0;
5674 if (!bp->link_info.force_link_chng)
5675 bnxt_report_link(bp);
5676 }
5677 bp->link_info.force_link_chng = false;
5678 mutex_unlock(&bp->hwrm_cmd_lock);
5679 return rc;
5680}
5681
Michael Chan939f7f02016-04-05 14:08:58 -04005682static void bnxt_hwrm_set_eee(struct bnxt *bp,
5683 struct hwrm_port_phy_cfg_input *req)
5684{
5685 struct ethtool_eee *eee = &bp->eee;
5686
5687 if (eee->eee_enabled) {
5688 u16 eee_speeds;
5689 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5690
5691 if (eee->tx_lpi_enabled)
5692 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5693 else
5694 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5695
5696 req->flags |= cpu_to_le32(flags);
5697 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5698 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5699 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5700 } else {
5701 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5702 }
5703}
5704
5705int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005706{
5707 struct hwrm_port_phy_cfg_input req = {0};
5708
5709 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5710 if (set_pause)
5711 bnxt_hwrm_set_pause_common(bp, &req);
5712
5713 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005714
5715 if (set_eee)
5716 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005717 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5718}
5719
Michael Chan33f7d552016-04-11 04:11:12 -04005720static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5721{
5722 struct hwrm_port_phy_cfg_input req = {0};
5723
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005724 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005725 return 0;
5726
5727 if (pci_num_vf(bp->pdev))
5728 return 0;
5729
5730 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005731 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005732 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5733}
5734
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005735static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5736{
5737 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5738 struct hwrm_port_led_qcaps_input req = {0};
5739 struct bnxt_pf_info *pf = &bp->pf;
5740 int rc;
5741
5742 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5743 return 0;
5744
5745 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5746 req.port_id = cpu_to_le16(pf->port_id);
5747 mutex_lock(&bp->hwrm_cmd_lock);
5748 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5749 if (rc) {
5750 mutex_unlock(&bp->hwrm_cmd_lock);
5751 return rc;
5752 }
5753 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5754 int i;
5755
5756 bp->num_leds = resp->num_leds;
5757 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5758 bp->num_leds);
5759 for (i = 0; i < bp->num_leds; i++) {
5760 struct bnxt_led_info *led = &bp->leds[i];
5761 __le16 caps = led->led_state_caps;
5762
5763 if (!led->led_group_id ||
5764 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5765 bp->num_leds = 0;
5766 break;
5767 }
5768 }
5769 }
5770 mutex_unlock(&bp->hwrm_cmd_lock);
5771 return 0;
5772}
5773
Michael Chan939f7f02016-04-05 14:08:58 -04005774static bool bnxt_eee_config_ok(struct bnxt *bp)
5775{
5776 struct ethtool_eee *eee = &bp->eee;
5777 struct bnxt_link_info *link_info = &bp->link_info;
5778
5779 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5780 return true;
5781
5782 if (eee->eee_enabled) {
5783 u32 advertising =
5784 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5785
5786 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5787 eee->eee_enabled = 0;
5788 return false;
5789 }
5790 if (eee->advertised & ~advertising) {
5791 eee->advertised = advertising & eee->supported;
5792 return false;
5793 }
5794 }
5795 return true;
5796}
5797
Michael Chanc0c050c2015-10-22 16:01:17 -04005798static int bnxt_update_phy_setting(struct bnxt *bp)
5799{
5800 int rc;
5801 bool update_link = false;
5802 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005803 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005804 struct bnxt_link_info *link_info = &bp->link_info;
5805
5806 rc = bnxt_update_link(bp, true);
5807 if (rc) {
5808 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5809 rc);
5810 return rc;
5811 }
5812 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005813 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5814 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005815 update_pause = true;
5816 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5817 link_info->force_pause_setting != link_info->req_flow_ctrl)
5818 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005819 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5820 if (BNXT_AUTO_MODE(link_info->auto_mode))
5821 update_link = true;
5822 if (link_info->req_link_speed != link_info->force_link_speed)
5823 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005824 if (link_info->req_duplex != link_info->duplex_setting)
5825 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005826 } else {
5827 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5828 update_link = true;
5829 if (link_info->advertising != link_info->auto_link_speeds)
5830 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005831 }
5832
Michael Chan16d663a2016-11-16 21:13:07 -05005833 /* The last close may have shutdown the link, so need to call
5834 * PHY_CFG to bring it back up.
5835 */
5836 if (!netif_carrier_ok(bp->dev))
5837 update_link = true;
5838
Michael Chan939f7f02016-04-05 14:08:58 -04005839 if (!bnxt_eee_config_ok(bp))
5840 update_eee = true;
5841
Michael Chanc0c050c2015-10-22 16:01:17 -04005842 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005843 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005844 else if (update_pause)
5845 rc = bnxt_hwrm_set_pause(bp);
5846 if (rc) {
5847 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5848 rc);
5849 return rc;
5850 }
5851
5852 return rc;
5853}
5854
Jeffrey Huang11809492015-11-05 16:25:49 -05005855/* Common routine to pre-map certain register block to different GRC window.
5856 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5857 * in PF and 3 windows in VF that can be customized to map in different
5858 * register blocks.
5859 */
5860static void bnxt_preset_reg_win(struct bnxt *bp)
5861{
5862 if (BNXT_PF(bp)) {
5863 /* CAG registers map to GRC window #4 */
5864 writel(BNXT_CAG_REG_BASE,
5865 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5866 }
5867}
5868
Michael Chanc0c050c2015-10-22 16:01:17 -04005869static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5870{
5871 int rc = 0;
5872
Jeffrey Huang11809492015-11-05 16:25:49 -05005873 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005874 netif_carrier_off(bp->dev);
5875 if (irq_re_init) {
5876 rc = bnxt_setup_int_mode(bp);
5877 if (rc) {
5878 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5879 rc);
5880 return rc;
5881 }
5882 }
5883 if ((bp->flags & BNXT_FLAG_RFS) &&
5884 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5885 /* disable RFS if falling back to INTA */
5886 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5887 bp->flags &= ~BNXT_FLAG_RFS;
5888 }
5889
5890 rc = bnxt_alloc_mem(bp, irq_re_init);
5891 if (rc) {
5892 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5893 goto open_err_free_mem;
5894 }
5895
5896 if (irq_re_init) {
5897 bnxt_init_napi(bp);
5898 rc = bnxt_request_irq(bp);
5899 if (rc) {
5900 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5901 goto open_err;
5902 }
5903 }
5904
5905 bnxt_enable_napi(bp);
5906
5907 rc = bnxt_init_nic(bp, irq_re_init);
5908 if (rc) {
5909 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5910 goto open_err;
5911 }
5912
5913 if (link_re_init) {
5914 rc = bnxt_update_phy_setting(bp);
5915 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005916 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005917 }
5918
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005919 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005920 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005921
Michael Chancaefe522015-12-09 19:35:42 -05005922 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005923 bnxt_enable_int(bp);
5924 /* Enable TX queues */
5925 bnxt_tx_enable(bp);
5926 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005927 /* Poll link status and check for SFP+ module status */
5928 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005929
5930 return 0;
5931
5932open_err:
5933 bnxt_disable_napi(bp);
5934 bnxt_del_napi(bp);
5935
5936open_err_free_mem:
5937 bnxt_free_skbs(bp);
5938 bnxt_free_irq(bp);
5939 bnxt_free_mem(bp, true);
5940 return rc;
5941}
5942
5943/* rtnl_lock held */
5944int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5945{
5946 int rc = 0;
5947
5948 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5949 if (rc) {
5950 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5951 dev_close(bp->dev);
5952 }
5953 return rc;
5954}
5955
5956static int bnxt_open(struct net_device *dev)
5957{
5958 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005959
Michael Chanc0c050c2015-10-22 16:01:17 -04005960 return __bnxt_open_nic(bp, true, true);
5961}
5962
Michael Chanc0c050c2015-10-22 16:01:17 -04005963int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5964{
5965 int rc = 0;
5966
5967#ifdef CONFIG_BNXT_SRIOV
5968 if (bp->sriov_cfg) {
5969 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5970 !bp->sriov_cfg,
5971 BNXT_SRIOV_CFG_WAIT_TMO);
5972 if (rc)
5973 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5974 }
5975#endif
5976 /* Change device state to avoid TX queue wake up's */
5977 bnxt_tx_disable(bp);
5978
Michael Chancaefe522015-12-09 19:35:42 -05005979 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005980 smp_mb__after_atomic();
5981 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5982 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005983
Michael Chan9d8bc092016-12-29 12:13:33 -05005984 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04005985 bnxt_shutdown_nic(bp, irq_re_init);
5986
5987 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5988
5989 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005990 del_timer_sync(&bp->timer);
5991 bnxt_free_skbs(bp);
5992
5993 if (irq_re_init) {
5994 bnxt_free_irq(bp);
5995 bnxt_del_napi(bp);
5996 }
5997 bnxt_free_mem(bp, irq_re_init);
5998 return rc;
5999}
6000
6001static int bnxt_close(struct net_device *dev)
6002{
6003 struct bnxt *bp = netdev_priv(dev);
6004
6005 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04006006 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006007 return 0;
6008}
6009
6010/* rtnl_lock held */
6011static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6012{
6013 switch (cmd) {
6014 case SIOCGMIIPHY:
6015 /* fallthru */
6016 case SIOCGMIIREG: {
6017 if (!netif_running(dev))
6018 return -EAGAIN;
6019
6020 return 0;
6021 }
6022
6023 case SIOCSMIIREG:
6024 if (!netif_running(dev))
6025 return -EAGAIN;
6026
6027 return 0;
6028
6029 default:
6030 /* do nothing */
6031 break;
6032 }
6033 return -EOPNOTSUPP;
6034}
6035
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006036static void
Michael Chanc0c050c2015-10-22 16:01:17 -04006037bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6038{
6039 u32 i;
6040 struct bnxt *bp = netdev_priv(dev);
6041
Michael Chanc0c050c2015-10-22 16:01:17 -04006042 if (!bp->bnapi)
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006043 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006044
6045 /* TODO check if we need to synchronize with bnxt_close path */
6046 for (i = 0; i < bp->cp_nr_rings; i++) {
6047 struct bnxt_napi *bnapi = bp->bnapi[i];
6048 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6049 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6050
6051 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6052 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6053 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6054
6055 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6056 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6057 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6058
6059 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6060 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6061 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6062
6063 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6064 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6065 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6066
6067 stats->rx_missed_errors +=
6068 le64_to_cpu(hw_stats->rx_discard_pkts);
6069
6070 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6071
Michael Chanc0c050c2015-10-22 16:01:17 -04006072 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6073 }
6074
Michael Chan9947f832016-03-07 15:38:46 -05006075 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6076 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6077 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6078
6079 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6080 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6081 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6082 le64_to_cpu(rx->rx_ovrsz_frames) +
6083 le64_to_cpu(rx->rx_runt_frames);
6084 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6085 le64_to_cpu(rx->rx_jbr_frames);
6086 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6087 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6088 stats->tx_errors = le64_to_cpu(tx->tx_err);
6089 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006090}
6091
6092static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6093{
6094 struct net_device *dev = bp->dev;
6095 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6096 struct netdev_hw_addr *ha;
6097 u8 *haddr;
6098 int mc_count = 0;
6099 bool update = false;
6100 int off = 0;
6101
6102 netdev_for_each_mc_addr(ha, dev) {
6103 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6104 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6105 vnic->mc_list_count = 0;
6106 return false;
6107 }
6108 haddr = ha->addr;
6109 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6110 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6111 update = true;
6112 }
6113 off += ETH_ALEN;
6114 mc_count++;
6115 }
6116 if (mc_count)
6117 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6118
6119 if (mc_count != vnic->mc_list_count) {
6120 vnic->mc_list_count = mc_count;
6121 update = true;
6122 }
6123 return update;
6124}
6125
6126static bool bnxt_uc_list_updated(struct bnxt *bp)
6127{
6128 struct net_device *dev = bp->dev;
6129 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6130 struct netdev_hw_addr *ha;
6131 int off = 0;
6132
6133 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6134 return true;
6135
6136 netdev_for_each_uc_addr(ha, dev) {
6137 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6138 return true;
6139
6140 off += ETH_ALEN;
6141 }
6142 return false;
6143}
6144
6145static void bnxt_set_rx_mode(struct net_device *dev)
6146{
6147 struct bnxt *bp = netdev_priv(dev);
6148 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6149 u32 mask = vnic->rx_mask;
6150 bool mc_update = false;
6151 bool uc_update;
6152
6153 if (!netif_running(dev))
6154 return;
6155
6156 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6157 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6158 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6159
Michael Chan17c71ac2016-07-01 18:46:27 -04006160 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006161 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6162
6163 uc_update = bnxt_uc_list_updated(bp);
6164
6165 if (dev->flags & IFF_ALLMULTI) {
6166 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6167 vnic->mc_list_count = 0;
6168 } else {
6169 mc_update = bnxt_mc_list_updated(bp, &mask);
6170 }
6171
6172 if (mask != vnic->rx_mask || uc_update || mc_update) {
6173 vnic->rx_mask = mask;
6174
6175 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6176 schedule_work(&bp->sp_task);
6177 }
6178}
6179
Michael Chanb664f002015-12-02 01:54:08 -05006180static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006181{
6182 struct net_device *dev = bp->dev;
6183 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6184 struct netdev_hw_addr *ha;
6185 int i, off = 0, rc;
6186 bool uc_update;
6187
6188 netif_addr_lock_bh(dev);
6189 uc_update = bnxt_uc_list_updated(bp);
6190 netif_addr_unlock_bh(dev);
6191
6192 if (!uc_update)
6193 goto skip_uc;
6194
6195 mutex_lock(&bp->hwrm_cmd_lock);
6196 for (i = 1; i < vnic->uc_filter_count; i++) {
6197 struct hwrm_cfa_l2_filter_free_input req = {0};
6198
6199 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6200 -1);
6201
6202 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6203
6204 rc = _hwrm_send_message(bp, &req, sizeof(req),
6205 HWRM_CMD_TIMEOUT);
6206 }
6207 mutex_unlock(&bp->hwrm_cmd_lock);
6208
6209 vnic->uc_filter_count = 1;
6210
6211 netif_addr_lock_bh(dev);
6212 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6213 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6214 } else {
6215 netdev_for_each_uc_addr(ha, dev) {
6216 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6217 off += ETH_ALEN;
6218 vnic->uc_filter_count++;
6219 }
6220 }
6221 netif_addr_unlock_bh(dev);
6222
6223 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6224 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6225 if (rc) {
6226 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6227 rc);
6228 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006229 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006230 }
6231 }
6232
6233skip_uc:
6234 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6235 if (rc)
6236 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6237 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006238
6239 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006240}
6241
Michael Chan8079e8f2016-12-29 12:13:37 -05006242/* If the chip and firmware supports RFS */
6243static bool bnxt_rfs_supported(struct bnxt *bp)
6244{
6245 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6246 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006247 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6248 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006249 return false;
6250}
6251
6252/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006253static bool bnxt_rfs_capable(struct bnxt *bp)
6254{
6255#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006256 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006257
6258 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
6259 return false;
6260
6261 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006262 max_vnics = bnxt_get_max_func_vnics(bp);
6263 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006264
6265 /* RSS contexts not a limiting factor */
6266 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6267 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006268 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006269 netdev_warn(bp->dev,
6270 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006271 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006272 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006273 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006274
6275 return true;
6276#else
6277 return false;
6278#endif
6279}
6280
Michael Chanc0c050c2015-10-22 16:01:17 -04006281static netdev_features_t bnxt_fix_features(struct net_device *dev,
6282 netdev_features_t features)
6283{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006284 struct bnxt *bp = netdev_priv(dev);
6285
Vasundhara Volama2304902016-07-25 12:33:36 -04006286 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006287 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006288
6289 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6290 * turned on or off together.
6291 */
6292 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6293 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6294 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6295 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6296 NETIF_F_HW_VLAN_STAG_RX);
6297 else
6298 features |= NETIF_F_HW_VLAN_CTAG_RX |
6299 NETIF_F_HW_VLAN_STAG_RX;
6300 }
Michael Chancf6645f2016-06-13 02:25:28 -04006301#ifdef CONFIG_BNXT_SRIOV
6302 if (BNXT_VF(bp)) {
6303 if (bp->vf.vlan) {
6304 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6305 NETIF_F_HW_VLAN_STAG_RX);
6306 }
6307 }
6308#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006309 return features;
6310}
6311
6312static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6313{
6314 struct bnxt *bp = netdev_priv(dev);
6315 u32 flags = bp->flags;
6316 u32 changes;
6317 int rc = 0;
6318 bool re_init = false;
6319 bool update_tpa = false;
6320
6321 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006322 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006323 flags |= BNXT_FLAG_GRO;
6324 if (features & NETIF_F_LRO)
6325 flags |= BNXT_FLAG_LRO;
6326
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006327 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6328 flags &= ~BNXT_FLAG_TPA;
6329
Michael Chanc0c050c2015-10-22 16:01:17 -04006330 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6331 flags |= BNXT_FLAG_STRIP_VLAN;
6332
6333 if (features & NETIF_F_NTUPLE)
6334 flags |= BNXT_FLAG_RFS;
6335
6336 changes = flags ^ bp->flags;
6337 if (changes & BNXT_FLAG_TPA) {
6338 update_tpa = true;
6339 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6340 (flags & BNXT_FLAG_TPA) == 0)
6341 re_init = true;
6342 }
6343
6344 if (changes & ~BNXT_FLAG_TPA)
6345 re_init = true;
6346
6347 if (flags != bp->flags) {
6348 u32 old_flags = bp->flags;
6349
6350 bp->flags = flags;
6351
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006352 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006353 if (update_tpa)
6354 bnxt_set_ring_params(bp);
6355 return rc;
6356 }
6357
6358 if (re_init) {
6359 bnxt_close_nic(bp, false, false);
6360 if (update_tpa)
6361 bnxt_set_ring_params(bp);
6362
6363 return bnxt_open_nic(bp, false, false);
6364 }
6365 if (update_tpa) {
6366 rc = bnxt_set_tpa(bp,
6367 (flags & BNXT_FLAG_TPA) ?
6368 true : false);
6369 if (rc)
6370 bp->flags = old_flags;
6371 }
6372 }
6373 return rc;
6374}
6375
Michael Chan9f554592016-01-02 23:44:58 -05006376static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6377{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006378 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006379 int i = bnapi->index;
6380
Michael Chan3b2b7d92016-01-02 23:45:00 -05006381 if (!txr)
6382 return;
6383
Michael Chan9f554592016-01-02 23:44:58 -05006384 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6385 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6386 txr->tx_cons);
6387}
6388
6389static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6390{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006391 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006392 int i = bnapi->index;
6393
Michael Chan3b2b7d92016-01-02 23:45:00 -05006394 if (!rxr)
6395 return;
6396
Michael Chan9f554592016-01-02 23:44:58 -05006397 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",
6398 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6399 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6400 rxr->rx_sw_agg_prod);
6401}
6402
6403static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6404{
6405 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6406 int i = bnapi->index;
6407
6408 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6409 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6410}
6411
Michael Chanc0c050c2015-10-22 16:01:17 -04006412static void bnxt_dbg_dump_states(struct bnxt *bp)
6413{
6414 int i;
6415 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006416
6417 for (i = 0; i < bp->cp_nr_rings; i++) {
6418 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006419 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006420 bnxt_dump_tx_sw_state(bnapi);
6421 bnxt_dump_rx_sw_state(bnapi);
6422 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006423 }
6424 }
6425}
6426
Michael Chan6988bd92016-06-13 02:25:29 -04006427static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006428{
Michael Chan6988bd92016-06-13 02:25:29 -04006429 if (!silent)
6430 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006431 if (netif_running(bp->dev)) {
6432 bnxt_close_nic(bp, false, false);
6433 bnxt_open_nic(bp, false, false);
6434 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006435}
6436
6437static void bnxt_tx_timeout(struct net_device *dev)
6438{
6439 struct bnxt *bp = netdev_priv(dev);
6440
6441 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6442 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6443 schedule_work(&bp->sp_task);
6444}
6445
6446#ifdef CONFIG_NET_POLL_CONTROLLER
6447static void bnxt_poll_controller(struct net_device *dev)
6448{
6449 struct bnxt *bp = netdev_priv(dev);
6450 int i;
6451
6452 for (i = 0; i < bp->cp_nr_rings; i++) {
6453 struct bnxt_irq *irq = &bp->irq_tbl[i];
6454
6455 disable_irq(irq->vector);
6456 irq->handler(irq->vector, bp->bnapi[i]);
6457 enable_irq(irq->vector);
6458 }
6459}
6460#endif
6461
6462static void bnxt_timer(unsigned long data)
6463{
6464 struct bnxt *bp = (struct bnxt *)data;
6465 struct net_device *dev = bp->dev;
6466
6467 if (!netif_running(dev))
6468 return;
6469
6470 if (atomic_read(&bp->intr_sem) != 0)
6471 goto bnxt_restart_timer;
6472
Michael Chan3bdf56c2016-03-07 15:38:45 -05006473 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6474 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6475 schedule_work(&bp->sp_task);
6476 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006477bnxt_restart_timer:
6478 mod_timer(&bp->timer, jiffies + bp->current_interval);
6479}
6480
Michael Chana551ee92017-01-25 02:55:07 -05006481static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006482{
Michael Chana551ee92017-01-25 02:55:07 -05006483 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6484 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006485 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6486 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6487 */
6488 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6489 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006490}
6491
6492static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6493{
Michael Chan6988bd92016-06-13 02:25:29 -04006494 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6495 rtnl_unlock();
6496}
6497
Michael Chana551ee92017-01-25 02:55:07 -05006498/* Only called from bnxt_sp_task() */
6499static void bnxt_reset(struct bnxt *bp, bool silent)
6500{
6501 bnxt_rtnl_lock_sp(bp);
6502 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6503 bnxt_reset_task(bp, silent);
6504 bnxt_rtnl_unlock_sp(bp);
6505}
6506
Michael Chanc0c050c2015-10-22 16:01:17 -04006507static void bnxt_cfg_ntp_filters(struct bnxt *);
6508
6509static void bnxt_sp_task(struct work_struct *work)
6510{
6511 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006512
Michael Chan4cebdce2015-12-09 19:35:43 -05006513 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6514 smp_mb__after_atomic();
6515 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6516 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006517 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006518 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006519
6520 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6521 bnxt_cfg_rx_mode(bp);
6522
6523 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6524 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006525 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6526 bnxt_hwrm_exec_fwd_req(bp);
6527 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6528 bnxt_hwrm_tunnel_dst_port_alloc(
6529 bp, bp->vxlan_port,
6530 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6531 }
6532 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6533 bnxt_hwrm_tunnel_dst_port_free(
6534 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6535 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006536 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6537 bnxt_hwrm_tunnel_dst_port_alloc(
6538 bp, bp->nge_port,
6539 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6540 }
6541 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6542 bnxt_hwrm_tunnel_dst_port_free(
6543 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6544 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006545 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6546 bnxt_hwrm_port_qstats(bp);
6547
Michael Chana551ee92017-01-25 02:55:07 -05006548 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6549 * must be the last functions to be called before exiting.
6550 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006551 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6552 int rc = 0;
6553
6554 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6555 &bp->sp_event))
6556 bnxt_hwrm_phy_qcaps(bp);
6557
6558 bnxt_rtnl_lock_sp(bp);
6559 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6560 rc = bnxt_update_link(bp, true);
6561 bnxt_rtnl_unlock_sp(bp);
6562 if (rc)
6563 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6564 rc);
6565 }
Michael Chan90c694b2017-01-25 02:55:09 -05006566 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6567 bnxt_rtnl_lock_sp(bp);
6568 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6569 bnxt_get_port_module_status(bp);
6570 bnxt_rtnl_unlock_sp(bp);
6571 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006572 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6573 bnxt_reset(bp, false);
6574
6575 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6576 bnxt_reset(bp, true);
6577
Michael Chanc0c050c2015-10-22 16:01:17 -04006578 smp_mb__before_atomic();
6579 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6580}
6581
6582static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6583{
6584 int rc;
6585 struct bnxt *bp = netdev_priv(dev);
6586
6587 SET_NETDEV_DEV(dev, &pdev->dev);
6588
6589 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6590 rc = pci_enable_device(pdev);
6591 if (rc) {
6592 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6593 goto init_err;
6594 }
6595
6596 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6597 dev_err(&pdev->dev,
6598 "Cannot find PCI device base address, aborting\n");
6599 rc = -ENODEV;
6600 goto init_err_disable;
6601 }
6602
6603 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6604 if (rc) {
6605 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6606 goto init_err_disable;
6607 }
6608
6609 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6610 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6611 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6612 goto init_err_disable;
6613 }
6614
6615 pci_set_master(pdev);
6616
6617 bp->dev = dev;
6618 bp->pdev = pdev;
6619
6620 bp->bar0 = pci_ioremap_bar(pdev, 0);
6621 if (!bp->bar0) {
6622 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6623 rc = -ENOMEM;
6624 goto init_err_release;
6625 }
6626
6627 bp->bar1 = pci_ioremap_bar(pdev, 2);
6628 if (!bp->bar1) {
6629 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6630 rc = -ENOMEM;
6631 goto init_err_release;
6632 }
6633
6634 bp->bar2 = pci_ioremap_bar(pdev, 4);
6635 if (!bp->bar2) {
6636 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6637 rc = -ENOMEM;
6638 goto init_err_release;
6639 }
6640
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006641 pci_enable_pcie_error_reporting(pdev);
6642
Michael Chanc0c050c2015-10-22 16:01:17 -04006643 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6644
6645 spin_lock_init(&bp->ntp_fltr_lock);
6646
6647 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6648 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6649
Michael Chandfb5b892016-02-26 04:00:01 -05006650 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006651 bp->rx_coal_ticks = 12;
6652 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006653 bp->rx_coal_ticks_irq = 1;
6654 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006655
Michael Chandfc9c942016-02-26 04:00:03 -05006656 bp->tx_coal_ticks = 25;
6657 bp->tx_coal_bufs = 30;
6658 bp->tx_coal_ticks_irq = 2;
6659 bp->tx_coal_bufs_irq = 2;
6660
Michael Chan51f30782016-07-01 18:46:29 -04006661 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6662
Michael Chanc0c050c2015-10-22 16:01:17 -04006663 init_timer(&bp->timer);
6664 bp->timer.data = (unsigned long)bp;
6665 bp->timer.function = bnxt_timer;
6666 bp->current_interval = BNXT_TIMER_INTERVAL;
6667
Michael Chancaefe522015-12-09 19:35:42 -05006668 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006669
6670 return 0;
6671
6672init_err_release:
6673 if (bp->bar2) {
6674 pci_iounmap(pdev, bp->bar2);
6675 bp->bar2 = NULL;
6676 }
6677
6678 if (bp->bar1) {
6679 pci_iounmap(pdev, bp->bar1);
6680 bp->bar1 = NULL;
6681 }
6682
6683 if (bp->bar0) {
6684 pci_iounmap(pdev, bp->bar0);
6685 bp->bar0 = NULL;
6686 }
6687
6688 pci_release_regions(pdev);
6689
6690init_err_disable:
6691 pci_disable_device(pdev);
6692
6693init_err:
6694 return rc;
6695}
6696
6697/* rtnl_lock held */
6698static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6699{
6700 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006701 struct bnxt *bp = netdev_priv(dev);
6702 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006703
6704 if (!is_valid_ether_addr(addr->sa_data))
6705 return -EADDRNOTAVAIL;
6706
Michael Chan84c33dd2016-04-11 04:11:13 -04006707 rc = bnxt_approve_mac(bp, addr->sa_data);
6708 if (rc)
6709 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006710
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006711 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6712 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006713
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006714 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6715 if (netif_running(dev)) {
6716 bnxt_close_nic(bp, false, false);
6717 rc = bnxt_open_nic(bp, false, false);
6718 }
6719
6720 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006721}
6722
6723/* rtnl_lock held */
6724static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6725{
6726 struct bnxt *bp = netdev_priv(dev);
6727
Michael Chanc0c050c2015-10-22 16:01:17 -04006728 if (netif_running(dev))
6729 bnxt_close_nic(bp, false, false);
6730
6731 dev->mtu = new_mtu;
6732 bnxt_set_ring_params(bp);
6733
6734 if (netif_running(dev))
6735 return bnxt_open_nic(bp, false, false);
6736
6737 return 0;
6738}
6739
Michael Chanc5e3deb2016-12-02 21:17:15 -05006740int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006741{
6742 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006743 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006744
Michael Chanc0c050c2015-10-22 16:01:17 -04006745 if (tc > bp->max_tc) {
6746 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6747 tc, bp->max_tc);
6748 return -EINVAL;
6749 }
6750
6751 if (netdev_get_num_tc(dev) == tc)
6752 return 0;
6753
Michael Chan3ffb6a32016-11-11 00:11:42 -05006754 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6755 sh = true;
6756
Michael Chanc0c050c2015-10-22 16:01:17 -04006757 if (tc) {
Michael Chan391be5c2016-12-29 12:13:41 -05006758 int max_rx_rings, max_tx_rings, req_tx_rings, rsv_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006759
Michael Chan391be5c2016-12-29 12:13:41 -05006760 req_tx_rings = bp->tx_nr_rings_per_tc * tc;
Michael Chan01657bc2016-01-02 23:45:03 -05006761 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan391be5c2016-12-29 12:13:41 -05006762 if (rc || req_tx_rings > max_tx_rings)
6763 return -ENOMEM;
6764
6765 rsv_tx_rings = req_tx_rings;
6766 if (bnxt_hwrm_reserve_tx_rings(bp, &rsv_tx_rings) ||
6767 rsv_tx_rings < req_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006768 return -ENOMEM;
6769 }
6770
6771 /* Needs to close the device and do hw resource re-allocations */
6772 if (netif_running(bp->dev))
6773 bnxt_close_nic(bp, true, false);
6774
6775 if (tc) {
6776 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6777 netdev_set_num_tc(dev, tc);
6778 } else {
6779 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6780 netdev_reset_tc(dev);
6781 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006782 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6783 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006784 bp->num_stat_ctxs = bp->cp_nr_rings;
6785
6786 if (netif_running(bp->dev))
6787 return bnxt_open_nic(bp, true, false);
6788
6789 return 0;
6790}
6791
Michael Chanc5e3deb2016-12-02 21:17:15 -05006792static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6793 struct tc_to_netdev *ntc)
6794{
6795 if (ntc->type != TC_SETUP_MQPRIO)
6796 return -EINVAL;
6797
6798 return bnxt_setup_mq_tc(dev, ntc->tc);
6799}
6800
Michael Chanc0c050c2015-10-22 16:01:17 -04006801#ifdef CONFIG_RFS_ACCEL
6802static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6803 struct bnxt_ntuple_filter *f2)
6804{
6805 struct flow_keys *keys1 = &f1->fkeys;
6806 struct flow_keys *keys2 = &f2->fkeys;
6807
6808 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6809 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6810 keys1->ports.ports == keys2->ports.ports &&
6811 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6812 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006813 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6814 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006815 return true;
6816
6817 return false;
6818}
6819
6820static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6821 u16 rxq_index, u32 flow_id)
6822{
6823 struct bnxt *bp = netdev_priv(dev);
6824 struct bnxt_ntuple_filter *fltr, *new_fltr;
6825 struct flow_keys *fkeys;
6826 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006827 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006828 struct hlist_head *head;
6829
6830 if (skb->encapsulation)
6831 return -EPROTONOSUPPORT;
6832
Michael Chana54c4d72016-07-25 12:33:35 -04006833 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6834 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6835 int off = 0, j;
6836
6837 netif_addr_lock_bh(dev);
6838 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6839 if (ether_addr_equal(eth->h_dest,
6840 vnic->uc_list + off)) {
6841 l2_idx = j + 1;
6842 break;
6843 }
6844 }
6845 netif_addr_unlock_bh(dev);
6846 if (!l2_idx)
6847 return -EINVAL;
6848 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006849 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6850 if (!new_fltr)
6851 return -ENOMEM;
6852
6853 fkeys = &new_fltr->fkeys;
6854 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6855 rc = -EPROTONOSUPPORT;
6856 goto err_free;
6857 }
6858
Michael Chandda0e742016-12-29 12:13:40 -05006859 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
6860 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04006861 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6862 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6863 rc = -EPROTONOSUPPORT;
6864 goto err_free;
6865 }
Michael Chandda0e742016-12-29 12:13:40 -05006866 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
6867 bp->hwrm_spec_code < 0x10601) {
6868 rc = -EPROTONOSUPPORT;
6869 goto err_free;
6870 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006871
Michael Chana54c4d72016-07-25 12:33:35 -04006872 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006873 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6874
6875 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6876 head = &bp->ntp_fltr_hash_tbl[idx];
6877 rcu_read_lock();
6878 hlist_for_each_entry_rcu(fltr, head, hash) {
6879 if (bnxt_fltr_match(fltr, new_fltr)) {
6880 rcu_read_unlock();
6881 rc = 0;
6882 goto err_free;
6883 }
6884 }
6885 rcu_read_unlock();
6886
6887 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006888 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6889 BNXT_NTP_FLTR_MAX_FLTR, 0);
6890 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006891 spin_unlock_bh(&bp->ntp_fltr_lock);
6892 rc = -ENOMEM;
6893 goto err_free;
6894 }
6895
Michael Chan84e86b92015-11-05 16:25:50 -05006896 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006897 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006898 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006899 new_fltr->rxq = rxq_index;
6900 hlist_add_head_rcu(&new_fltr->hash, head);
6901 bp->ntp_fltr_count++;
6902 spin_unlock_bh(&bp->ntp_fltr_lock);
6903
6904 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6905 schedule_work(&bp->sp_task);
6906
6907 return new_fltr->sw_id;
6908
6909err_free:
6910 kfree(new_fltr);
6911 return rc;
6912}
6913
6914static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6915{
6916 int i;
6917
6918 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6919 struct hlist_head *head;
6920 struct hlist_node *tmp;
6921 struct bnxt_ntuple_filter *fltr;
6922 int rc;
6923
6924 head = &bp->ntp_fltr_hash_tbl[i];
6925 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6926 bool del = false;
6927
6928 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6929 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6930 fltr->flow_id,
6931 fltr->sw_id)) {
6932 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6933 fltr);
6934 del = true;
6935 }
6936 } else {
6937 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6938 fltr);
6939 if (rc)
6940 del = true;
6941 else
6942 set_bit(BNXT_FLTR_VALID, &fltr->state);
6943 }
6944
6945 if (del) {
6946 spin_lock_bh(&bp->ntp_fltr_lock);
6947 hlist_del_rcu(&fltr->hash);
6948 bp->ntp_fltr_count--;
6949 spin_unlock_bh(&bp->ntp_fltr_lock);
6950 synchronize_rcu();
6951 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6952 kfree(fltr);
6953 }
6954 }
6955 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006956 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6957 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006958}
6959
6960#else
6961
6962static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6963{
6964}
6965
6966#endif /* CONFIG_RFS_ACCEL */
6967
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006968static void bnxt_udp_tunnel_add(struct net_device *dev,
6969 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006970{
6971 struct bnxt *bp = netdev_priv(dev);
6972
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006973 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6974 return;
6975
Michael Chanc0c050c2015-10-22 16:01:17 -04006976 if (!netif_running(dev))
6977 return;
6978
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006979 switch (ti->type) {
6980 case UDP_TUNNEL_TYPE_VXLAN:
6981 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6982 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006983
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006984 bp->vxlan_port_cnt++;
6985 if (bp->vxlan_port_cnt == 1) {
6986 bp->vxlan_port = ti->port;
6987 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006988 schedule_work(&bp->sp_task);
6989 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006990 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006991 case UDP_TUNNEL_TYPE_GENEVE:
6992 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6993 return;
6994
6995 bp->nge_port_cnt++;
6996 if (bp->nge_port_cnt == 1) {
6997 bp->nge_port = ti->port;
6998 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6999 }
7000 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007001 default:
7002 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04007003 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007004
7005 schedule_work(&bp->sp_task);
7006}
7007
7008static void bnxt_udp_tunnel_del(struct net_device *dev,
7009 struct udp_tunnel_info *ti)
7010{
7011 struct bnxt *bp = netdev_priv(dev);
7012
7013 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7014 return;
7015
7016 if (!netif_running(dev))
7017 return;
7018
7019 switch (ti->type) {
7020 case UDP_TUNNEL_TYPE_VXLAN:
7021 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7022 return;
7023 bp->vxlan_port_cnt--;
7024
7025 if (bp->vxlan_port_cnt != 0)
7026 return;
7027
7028 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7029 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07007030 case UDP_TUNNEL_TYPE_GENEVE:
7031 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7032 return;
7033 bp->nge_port_cnt--;
7034
7035 if (bp->nge_port_cnt != 0)
7036 return;
7037
7038 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7039 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007040 default:
7041 return;
7042 }
7043
7044 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04007045}
7046
7047static const struct net_device_ops bnxt_netdev_ops = {
7048 .ndo_open = bnxt_open,
7049 .ndo_start_xmit = bnxt_start_xmit,
7050 .ndo_stop = bnxt_close,
7051 .ndo_get_stats64 = bnxt_get_stats64,
7052 .ndo_set_rx_mode = bnxt_set_rx_mode,
7053 .ndo_do_ioctl = bnxt_ioctl,
7054 .ndo_validate_addr = eth_validate_addr,
7055 .ndo_set_mac_address = bnxt_change_mac_addr,
7056 .ndo_change_mtu = bnxt_change_mtu,
7057 .ndo_fix_features = bnxt_fix_features,
7058 .ndo_set_features = bnxt_set_features,
7059 .ndo_tx_timeout = bnxt_tx_timeout,
7060#ifdef CONFIG_BNXT_SRIOV
7061 .ndo_get_vf_config = bnxt_get_vf_config,
7062 .ndo_set_vf_mac = bnxt_set_vf_mac,
7063 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7064 .ndo_set_vf_rate = bnxt_set_vf_bw,
7065 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7066 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7067#endif
7068#ifdef CONFIG_NET_POLL_CONTROLLER
7069 .ndo_poll_controller = bnxt_poll_controller,
7070#endif
7071 .ndo_setup_tc = bnxt_setup_tc,
7072#ifdef CONFIG_RFS_ACCEL
7073 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7074#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07007075 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7076 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04007077};
7078
7079static void bnxt_remove_one(struct pci_dev *pdev)
7080{
7081 struct net_device *dev = pci_get_drvdata(pdev);
7082 struct bnxt *bp = netdev_priv(dev);
7083
7084 if (BNXT_PF(bp))
7085 bnxt_sriov_disable(bp);
7086
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007087 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04007088 unregister_netdev(dev);
7089 cancel_work_sync(&bp->sp_task);
7090 bp->sp_event = 0;
7091
Michael Chan78095922016-12-07 00:26:16 -05007092 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05007093 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007094 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05007095 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007096 pci_iounmap(pdev, bp->bar2);
7097 pci_iounmap(pdev, bp->bar1);
7098 pci_iounmap(pdev, bp->bar0);
Michael Chana588e452016-12-07 00:26:21 -05007099 kfree(bp->edev);
7100 bp->edev = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04007101 free_netdev(dev);
7102
7103 pci_release_regions(pdev);
7104 pci_disable_device(pdev);
7105}
7106
7107static int bnxt_probe_phy(struct bnxt *bp)
7108{
7109 int rc = 0;
7110 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007111
Michael Chan170ce012016-04-05 14:08:57 -04007112 rc = bnxt_hwrm_phy_qcaps(bp);
7113 if (rc) {
7114 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7115 rc);
7116 return rc;
7117 }
7118
Michael Chanc0c050c2015-10-22 16:01:17 -04007119 rc = bnxt_update_link(bp, false);
7120 if (rc) {
7121 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7122 rc);
7123 return rc;
7124 }
7125
Michael Chan93ed8112016-06-13 02:25:37 -04007126 /* Older firmware does not have supported_auto_speeds, so assume
7127 * that all supported speeds can be autonegotiated.
7128 */
7129 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7130 link_info->support_auto_speeds = link_info->support_speeds;
7131
Michael Chanc0c050c2015-10-22 16:01:17 -04007132 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007133 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007134 link_info->autoneg = BNXT_AUTONEG_SPEED;
7135 if (bp->hwrm_spec_code >= 0x10201) {
7136 if (link_info->auto_pause_setting &
7137 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7138 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7139 } else {
7140 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7141 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007142 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007143 } else {
7144 link_info->req_link_speed = link_info->force_link_speed;
7145 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007146 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007147 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7148 link_info->req_flow_ctrl =
7149 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7150 else
7151 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007152 return rc;
7153}
7154
7155static int bnxt_get_max_irq(struct pci_dev *pdev)
7156{
7157 u16 ctrl;
7158
7159 if (!pdev->msix_cap)
7160 return 1;
7161
7162 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7163 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7164}
7165
Michael Chan6e6c5a52016-01-02 23:45:02 -05007166static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7167 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007168{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007169 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007170
Michael Chan379a80a2015-10-23 15:06:19 -04007171#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007172 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007173 *max_tx = bp->vf.max_tx_rings;
7174 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007175 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7176 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007177 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007178 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007179#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007180 {
7181 *max_tx = bp->pf.max_tx_rings;
7182 *max_rx = bp->pf.max_rx_rings;
7183 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7184 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7185 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007186 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007187 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7188 *max_cp -= 1;
7189 *max_rx -= 2;
7190 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007191 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7192 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007193 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007194}
7195
7196int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7197{
7198 int rx, tx, cp;
7199
7200 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7201 if (!rx || !tx || !cp)
7202 return -ENOMEM;
7203
7204 *max_rx = rx;
7205 *max_tx = tx;
7206 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7207}
7208
Michael Chane4060d32016-12-07 00:26:19 -05007209static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7210 bool shared)
7211{
7212 int rc;
7213
7214 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007215 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7216 /* Not enough rings, try disabling agg rings. */
7217 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7218 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7219 if (rc)
7220 return rc;
7221 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7222 bp->dev->hw_features &= ~NETIF_F_LRO;
7223 bp->dev->features &= ~NETIF_F_LRO;
7224 bnxt_set_ring_params(bp);
7225 }
Michael Chane4060d32016-12-07 00:26:19 -05007226
7227 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7228 int max_cp, max_stat, max_irq;
7229
7230 /* Reserve minimum resources for RoCE */
7231 max_cp = bnxt_get_max_func_cp_rings(bp);
7232 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7233 max_irq = bnxt_get_max_func_irqs(bp);
7234 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7235 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7236 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7237 return 0;
7238
7239 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7240 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7241 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7242 max_cp = min_t(int, max_cp, max_irq);
7243 max_cp = min_t(int, max_cp, max_stat);
7244 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7245 if (rc)
7246 rc = 0;
7247 }
7248 return rc;
7249}
7250
Michael Chan6e6c5a52016-01-02 23:45:02 -05007251static int bnxt_set_dflt_rings(struct bnxt *bp)
7252{
7253 int dflt_rings, max_rx_rings, max_tx_rings, rc;
7254 bool sh = true;
7255
7256 if (sh)
7257 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7258 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007259 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007260 if (rc)
7261 return rc;
7262 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7263 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007264
7265 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7266 if (rc)
7267 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7268
Michael Chan6e6c5a52016-01-02 23:45:02 -05007269 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7270 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7271 bp->tx_nr_rings + bp->rx_nr_rings;
7272 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007273 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7274 bp->rx_nr_rings++;
7275 bp->cp_nr_rings++;
7276 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007277 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007278}
7279
Michael Chan7b08f662016-12-07 00:26:18 -05007280void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7281{
7282 ASSERT_RTNL();
7283 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007284 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007285}
7286
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007287static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7288{
7289 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7290 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7291
7292 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7293 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7294 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7295 else
7296 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7297 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7298 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7299 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7300 "Unknown", width);
7301}
7302
Michael Chanc0c050c2015-10-22 16:01:17 -04007303static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7304{
7305 static int version_printed;
7306 struct net_device *dev;
7307 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007308 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007309
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007310 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
7311 return -ENODEV;
7312
Michael Chanc0c050c2015-10-22 16:01:17 -04007313 if (version_printed++ == 0)
7314 pr_info("%s", version);
7315
7316 max_irqs = bnxt_get_max_irq(pdev);
7317 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7318 if (!dev)
7319 return -ENOMEM;
7320
7321 bp = netdev_priv(dev);
7322
7323 if (bnxt_vf_pciid(ent->driver_data))
7324 bp->flags |= BNXT_FLAG_VF;
7325
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007326 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007327 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007328
7329 rc = bnxt_init_board(pdev, dev);
7330 if (rc < 0)
7331 goto init_err_free;
7332
7333 dev->netdev_ops = &bnxt_netdev_ops;
7334 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7335 dev->ethtool_ops = &bnxt_ethtool_ops;
7336
7337 pci_set_drvdata(pdev, dev);
7338
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007339 rc = bnxt_alloc_hwrm_resources(bp);
7340 if (rc)
7341 goto init_err;
7342
7343 mutex_init(&bp->hwrm_cmd_lock);
7344 rc = bnxt_hwrm_ver_get(bp);
7345 if (rc)
7346 goto init_err;
7347
Rob Swindell5ac67d82016-09-19 03:58:03 -04007348 bnxt_hwrm_fw_set_time(bp);
7349
Michael Chanc0c050c2015-10-22 16:01:17 -04007350 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7351 NETIF_F_TSO | NETIF_F_TSO6 |
7352 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007353 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007354 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7355 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007356 NETIF_F_RXCSUM | NETIF_F_GRO;
7357
7358 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7359 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007360
Michael Chanc0c050c2015-10-22 16:01:17 -04007361 dev->hw_enc_features =
7362 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7363 NETIF_F_TSO | NETIF_F_TSO6 |
7364 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007365 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007366 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007367 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7368 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007369 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7370 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7371 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7372 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7373 dev->priv_flags |= IFF_UNICAST_FLT;
7374
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007375 /* MTU range: 60 - 9500 */
7376 dev->min_mtu = ETH_ZLEN;
Michael Chanc61fb992017-02-06 16:55:36 -05007377 dev->max_mtu = BNXT_MAX_MTU;
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007378
Michael Chan7df4ae92016-12-02 21:17:17 -05007379 bnxt_dcb_init(bp);
7380
Michael Chanc0c050c2015-10-22 16:01:17 -04007381#ifdef CONFIG_BNXT_SRIOV
7382 init_waitqueue_head(&bp->sriov_cfg_wait);
7383#endif
Michael Chan309369c2016-06-13 02:25:34 -04007384 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007385 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7386 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007387
Michael Chanc0c050c2015-10-22 16:01:17 -04007388 rc = bnxt_hwrm_func_drv_rgtr(bp);
7389 if (rc)
7390 goto init_err;
7391
Michael Chana1653b12016-12-07 00:26:20 -05007392 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7393 if (rc)
7394 goto init_err;
7395
Michael Chana588e452016-12-07 00:26:21 -05007396 bp->ulp_probe = bnxt_ulp_probe;
7397
Michael Chanc0c050c2015-10-22 16:01:17 -04007398 /* Get the MAX capabilities for this function */
7399 rc = bnxt_hwrm_func_qcaps(bp);
7400 if (rc) {
7401 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7402 rc);
7403 rc = -1;
7404 goto init_err;
7405 }
7406
7407 rc = bnxt_hwrm_queue_qportcfg(bp);
7408 if (rc) {
7409 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7410 rc);
7411 rc = -1;
7412 goto init_err;
7413 }
7414
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007415 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007416 bnxt_hwrm_port_led_qcaps(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007417
Michael Chanc61fb992017-02-06 16:55:36 -05007418 bnxt_set_rx_skb_mode(bp, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04007419 bnxt_set_tpa_flags(bp);
7420 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007421 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007422 rc = bnxt_set_dflt_rings(bp);
7423 if (rc) {
7424 netdev_err(bp->dev, "Not enough rings available.\n");
7425 rc = -ENOMEM;
7426 goto init_err;
7427 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007428
Michael Chan87da7f72016-11-16 21:13:09 -05007429 /* Default RSS hash cfg. */
7430 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7431 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7432 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7433 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7434 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7435 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7436 bp->hwrm_spec_code >= 0x10501) {
7437 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7438 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7439 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7440 }
7441
Michael Chan8fdefd62016-12-29 12:13:36 -05007442 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007443 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007444 dev->hw_features |= NETIF_F_NTUPLE;
7445 if (bnxt_rfs_capable(bp)) {
7446 bp->flags |= BNXT_FLAG_RFS;
7447 dev->features |= NETIF_F_NTUPLE;
7448 }
7449 }
7450
Michael Chanc0c050c2015-10-22 16:01:17 -04007451 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7452 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7453
7454 rc = bnxt_probe_phy(bp);
7455 if (rc)
7456 goto init_err;
7457
Michael Chanaa8ed022016-12-07 00:26:17 -05007458 rc = bnxt_hwrm_func_reset(bp);
7459 if (rc)
7460 goto init_err;
7461
Michael Chan78095922016-12-07 00:26:16 -05007462 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007463 if (rc)
7464 goto init_err;
7465
Michael Chan78095922016-12-07 00:26:16 -05007466 rc = register_netdev(dev);
7467 if (rc)
7468 goto init_err_clr_int;
7469
Michael Chanc0c050c2015-10-22 16:01:17 -04007470 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7471 board_info[ent->driver_data].name,
7472 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7473
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007474 bnxt_parse_log_pcie_link(bp);
7475
Michael Chanc0c050c2015-10-22 16:01:17 -04007476 return 0;
7477
Michael Chan78095922016-12-07 00:26:16 -05007478init_err_clr_int:
7479 bnxt_clear_int_mode(bp);
7480
Michael Chanc0c050c2015-10-22 16:01:17 -04007481init_err:
7482 pci_iounmap(pdev, bp->bar0);
7483 pci_release_regions(pdev);
7484 pci_disable_device(pdev);
7485
7486init_err_free:
7487 free_netdev(dev);
7488 return rc;
7489}
7490
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007491/**
7492 * bnxt_io_error_detected - called when PCI error is detected
7493 * @pdev: Pointer to PCI device
7494 * @state: The current pci connection state
7495 *
7496 * This function is called after a PCI bus error affecting
7497 * this device has been detected.
7498 */
7499static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7500 pci_channel_state_t state)
7501{
7502 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007503 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007504
7505 netdev_info(netdev, "PCI I/O error detected\n");
7506
7507 rtnl_lock();
7508 netif_device_detach(netdev);
7509
Michael Chana588e452016-12-07 00:26:21 -05007510 bnxt_ulp_stop(bp);
7511
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007512 if (state == pci_channel_io_perm_failure) {
7513 rtnl_unlock();
7514 return PCI_ERS_RESULT_DISCONNECT;
7515 }
7516
7517 if (netif_running(netdev))
7518 bnxt_close(netdev);
7519
7520 pci_disable_device(pdev);
7521 rtnl_unlock();
7522
7523 /* Request a slot slot reset. */
7524 return PCI_ERS_RESULT_NEED_RESET;
7525}
7526
7527/**
7528 * bnxt_io_slot_reset - called after the pci bus has been reset.
7529 * @pdev: Pointer to PCI device
7530 *
7531 * Restart the card from scratch, as if from a cold-boot.
7532 * At this point, the card has exprienced a hard reset,
7533 * followed by fixups by BIOS, and has its config space
7534 * set up identically to what it was at cold boot.
7535 */
7536static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7537{
7538 struct net_device *netdev = pci_get_drvdata(pdev);
7539 struct bnxt *bp = netdev_priv(netdev);
7540 int err = 0;
7541 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7542
7543 netdev_info(bp->dev, "PCI Slot Reset\n");
7544
7545 rtnl_lock();
7546
7547 if (pci_enable_device(pdev)) {
7548 dev_err(&pdev->dev,
7549 "Cannot re-enable PCI device after reset.\n");
7550 } else {
7551 pci_set_master(pdev);
7552
Michael Chanaa8ed022016-12-07 00:26:17 -05007553 err = bnxt_hwrm_func_reset(bp);
7554 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007555 err = bnxt_open(netdev);
7556
Michael Chana588e452016-12-07 00:26:21 -05007557 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007558 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007559 bnxt_ulp_start(bp);
7560 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007561 }
7562
7563 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7564 dev_close(netdev);
7565
7566 rtnl_unlock();
7567
7568 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7569 if (err) {
7570 dev_err(&pdev->dev,
7571 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7572 err); /* non-fatal, continue */
7573 }
7574
7575 return PCI_ERS_RESULT_RECOVERED;
7576}
7577
7578/**
7579 * bnxt_io_resume - called when traffic can start flowing again.
7580 * @pdev: Pointer to PCI device
7581 *
7582 * This callback is called when the error recovery driver tells
7583 * us that its OK to resume normal operation.
7584 */
7585static void bnxt_io_resume(struct pci_dev *pdev)
7586{
7587 struct net_device *netdev = pci_get_drvdata(pdev);
7588
7589 rtnl_lock();
7590
7591 netif_device_attach(netdev);
7592
7593 rtnl_unlock();
7594}
7595
7596static const struct pci_error_handlers bnxt_err_handler = {
7597 .error_detected = bnxt_io_error_detected,
7598 .slot_reset = bnxt_io_slot_reset,
7599 .resume = bnxt_io_resume
7600};
7601
Michael Chanc0c050c2015-10-22 16:01:17 -04007602static struct pci_driver bnxt_pci_driver = {
7603 .name = DRV_MODULE_NAME,
7604 .id_table = bnxt_pci_tbl,
7605 .probe = bnxt_init_one,
7606 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007607 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007608#if defined(CONFIG_BNXT_SRIOV)
7609 .sriov_configure = bnxt_sriov_configure,
7610#endif
7611};
7612
7613module_pci_driver(bnxt_pci_driver);