blob: c0f2167bbbb9fc101bfcc6d452933216b5b7ad8b [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanc0c050c2015-10-22 16:01:17 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040035#include <linux/rtc.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040036#include <net/ip.h>
37#include <net/tcp.h>
38#include <net/udp.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070041#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040042#include <linux/workqueue.h>
43#include <linux/prefetch.h>
44#include <linux/cache.h>
45#include <linux/log2.h>
46#include <linux/aer.h>
47#include <linux/bitmap.h>
48#include <linux/cpu_rmap.h>
49
50#include "bnxt_hsi.h"
51#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050052#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040053#include "bnxt_sriov.h"
54#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050055#include "bnxt_dcb.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040076 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040077 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040078 BCM57311,
79 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050080 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040081 BCM57404,
82 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040083 BCM57402_NPAR,
84 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040085 BCM57412,
86 BCM57414,
87 BCM57416,
88 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040089 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040090 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57417_SFP,
92 BCM57416_SFP,
93 BCM57404_NPAR,
94 BCM57406_NPAR,
95 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040096 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040097 BCM57414_NPAR,
98 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -040099 NETXTREME_E_VF,
100 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400101};
102
103/* indexed by enum above */
104static const struct {
105 char *name;
106} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400107 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
108 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400110 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400111 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
112 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
115 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
119 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400127 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
128 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
130 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400133 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135};
136
137static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400138 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500139 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
141 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400142 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400144 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
145 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
148 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
152 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
153 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
154 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400155 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400156 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
158 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
160 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400162 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400168#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
170 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400175#endif
176 { 0 }
177};
178
179MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
180
181static const u16 bnxt_vf_req_snif[] = {
182 HWRM_FUNC_CFG,
183 HWRM_PORT_PHY_QCFG,
184 HWRM_CFA_L2_FILTER_ALLOC,
185};
186
Michael Chan25be8622016-04-05 14:09:00 -0400187static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500188 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
189 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
190 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
191 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
192 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400193};
194
Michael Chanc0c050c2015-10-22 16:01:17 -0400195static bool bnxt_vf_pciid(enum board_idx idx)
196{
Michael Chanadbc8302016-09-19 03:58:01 -0400197 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400198}
199
200#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
201#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
202#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
203
204#define BNXT_CP_DB_REARM(db, raw_cons) \
205 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
206
207#define BNXT_CP_DB(db, raw_cons) \
208 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
209
210#define BNXT_CP_DB_IRQ_DIS(db) \
211 writel(DB_CP_IRQ_DIS_FLAGS, db)
212
213static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
214{
215 /* Tell compiler to fetch tx indices from memory. */
216 barrier();
217
218 return bp->tx_ring_size -
219 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
220}
221
222static const u16 bnxt_lhint_arr[] = {
223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanb6ab4b02016-01-02 23:44:59 -0500265 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400266 txq = netdev_get_tx_queue(dev, i);
267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500512 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
514 u16 cons = txr->tx_cons;
515 struct pci_dev *pdev = bp->pdev;
516 int i;
517 unsigned int tx_bytes = 0;
518
519 for (i = 0; i < nr_pkts; i++) {
520 struct bnxt_sw_tx_bd *tx_buf;
521 struct sk_buff *skb;
522 int j, last;
523
524 tx_buf = &txr->tx_buf_ring[cons];
525 cons = NEXT_TX(cons);
526 skb = tx_buf->skb;
527 tx_buf->skb = NULL;
528
529 if (tx_buf->is_push) {
530 tx_buf->is_push = 0;
531 goto next_tx_int;
532 }
533
534 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
535 skb_headlen(skb), PCI_DMA_TODEVICE);
536 last = tx_buf->nr_frags;
537
538 for (j = 0; j < last; j++) {
539 cons = NEXT_TX(cons);
540 tx_buf = &txr->tx_buf_ring[cons];
541 dma_unmap_page(
542 &pdev->dev,
543 dma_unmap_addr(tx_buf, mapping),
544 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 PCI_DMA_TODEVICE);
546 }
547
548next_tx_int:
549 cons = NEXT_TX(cons);
550
551 tx_bytes += skb->len;
552 dev_kfree_skb_any(skb);
553 }
554
555 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
556 txr->tx_cons = cons;
557
558 /* Need to make the tx_cons update visible to bnxt_start_xmit()
559 * before checking for netif_tx_queue_stopped(). Without the
560 * memory barrier, there is a small possibility that bnxt_start_xmit()
561 * will miss it and cause the queue to be stopped forever.
562 */
563 smp_mb();
564
565 if (unlikely(netif_tx_queue_stopped(txq)) &&
566 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
567 __netif_tx_lock(txq, smp_processor_id());
568 if (netif_tx_queue_stopped(txq) &&
569 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
570 txr->dev_state != BNXT_DEV_STATE_CLOSING)
571 netif_tx_wake_queue(txq);
572 __netif_tx_unlock(txq);
573 }
574}
575
576static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 u8 *data;
580 struct pci_dev *pdev = bp->pdev;
581
582 data = kmalloc(bp->rx_buf_size, gfp);
583 if (!data)
584 return NULL;
585
Michael Chanb3dba772017-02-06 16:55:35 -0500586 *mapping = dma_map_single(&pdev->dev, data + bp->rx_dma_offset,
Michael Chan745fc052017-02-06 16:55:34 -0500587 bp->rx_buf_use_size, bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400588
589 if (dma_mapping_error(&pdev->dev, *mapping)) {
590 kfree(data);
591 data = NULL;
592 }
593 return data;
594}
595
596static inline int bnxt_alloc_rx_data(struct bnxt *bp,
597 struct bnxt_rx_ring_info *rxr,
598 u16 prod, gfp_t gfp)
599{
600 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
601 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
602 u8 *data;
603 dma_addr_t mapping;
604
605 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 if (!data)
607 return -ENOMEM;
608
609 rx_buf->data = data;
Michael Chanb3dba772017-02-06 16:55:35 -0500610 rx_buf->data_ptr = data + bp->rx_offset;
Michael Chan11cd1192017-02-06 16:55:33 -0500611 rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400612
613 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
614
615 return 0;
616}
617
618static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500619 void *data)
Michael Chanc0c050c2015-10-22 16:01:17 -0400620{
621 u16 prod = rxr->rx_prod;
622 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
623 struct rx_bd *cons_bd, *prod_bd;
624
625 prod_rx_buf = &rxr->rx_buf_ring[prod];
626 cons_rx_buf = &rxr->rx_buf_ring[cons];
627
628 prod_rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500629 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400630
Michael Chan11cd1192017-02-06 16:55:33 -0500631 prod_rx_buf->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400632
633 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
634 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
635
636 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
637}
638
639static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
640{
641 u16 next, max = rxr->rx_agg_bmap_size;
642
643 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
644 if (next >= max)
645 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
646 return next;
647}
648
649static inline int bnxt_alloc_rx_page(struct bnxt *bp,
650 struct bnxt_rx_ring_info *rxr,
651 u16 prod, gfp_t gfp)
652{
653 struct rx_bd *rxbd =
654 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
655 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
656 struct pci_dev *pdev = bp->pdev;
657 struct page *page;
658 dma_addr_t mapping;
659 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400660 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400661
Michael Chan89d0a062016-04-25 02:30:51 -0400662 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
663 page = rxr->rx_page;
664 if (!page) {
665 page = alloc_page(gfp);
666 if (!page)
667 return -ENOMEM;
668 rxr->rx_page = page;
669 rxr->rx_page_offset = 0;
670 }
671 offset = rxr->rx_page_offset;
672 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
673 if (rxr->rx_page_offset == PAGE_SIZE)
674 rxr->rx_page = NULL;
675 else
676 get_page(page);
677 } else {
678 page = alloc_page(gfp);
679 if (!page)
680 return -ENOMEM;
681 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400682
Michael Chan89d0a062016-04-25 02:30:51 -0400683 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400684 PCI_DMA_FROMDEVICE);
685 if (dma_mapping_error(&pdev->dev, mapping)) {
686 __free_page(page);
687 return -EIO;
688 }
689
690 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
691 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
692
693 __set_bit(sw_prod, rxr->rx_agg_bmap);
694 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
695 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
696
697 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400698 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400699 rx_agg_buf->mapping = mapping;
700 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
701 rxbd->rx_bd_opaque = sw_prod;
702 return 0;
703}
704
705static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
706 u32 agg_bufs)
707{
708 struct bnxt *bp = bnapi->bp;
709 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500710 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400711 u16 prod = rxr->rx_agg_prod;
712 u16 sw_prod = rxr->rx_sw_agg_prod;
713 u32 i;
714
715 for (i = 0; i < agg_bufs; i++) {
716 u16 cons;
717 struct rx_agg_cmp *agg;
718 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
719 struct rx_bd *prod_bd;
720 struct page *page;
721
722 agg = (struct rx_agg_cmp *)
723 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
724 cons = agg->rx_agg_cmp_opaque;
725 __clear_bit(cons, rxr->rx_agg_bmap);
726
727 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
728 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
729
730 __set_bit(sw_prod, rxr->rx_agg_bmap);
731 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
732 cons_rx_buf = &rxr->rx_agg_ring[cons];
733
734 /* It is possible for sw_prod to be equal to cons, so
735 * set cons_rx_buf->page to NULL first.
736 */
737 page = cons_rx_buf->page;
738 cons_rx_buf->page = NULL;
739 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400740 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400741
742 prod_rx_buf->mapping = cons_rx_buf->mapping;
743
744 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
745
746 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
747 prod_bd->rx_bd_opaque = sw_prod;
748
749 prod = NEXT_RX_AGG(prod);
750 sw_prod = NEXT_RX_AGG(sw_prod);
751 cp_cons = NEXT_CMP(cp_cons);
752 }
753 rxr->rx_agg_prod = prod;
754 rxr->rx_sw_agg_prod = sw_prod;
755}
756
757static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
758 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500759 void *data, u8 *data_ptr,
760 dma_addr_t dma_addr,
761 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400762{
Michael Chan6bb19472017-02-06 16:55:32 -0500763 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400764 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500765 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400766
767 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
768 if (unlikely(err)) {
769 bnxt_reuse_rx_data(rxr, cons, data);
770 return NULL;
771 }
772
773 skb = build_skb(data, 0);
774 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
Michael Chan745fc052017-02-06 16:55:34 -0500775 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400776 if (!skb) {
777 kfree(data);
778 return NULL;
779 }
780
Michael Chanb3dba772017-02-06 16:55:35 -0500781 skb_reserve(skb, bp->rx_offset);
Michael Chan6bb19472017-02-06 16:55:32 -0500782 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400783 return skb;
784}
785
786static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
787 struct sk_buff *skb, u16 cp_cons,
788 u32 agg_bufs)
789{
790 struct pci_dev *pdev = bp->pdev;
791 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500792 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400793 u16 prod = rxr->rx_agg_prod;
794 u32 i;
795
796 for (i = 0; i < agg_bufs; i++) {
797 u16 cons, frag_len;
798 struct rx_agg_cmp *agg;
799 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
800 struct page *page;
801 dma_addr_t mapping;
802
803 agg = (struct rx_agg_cmp *)
804 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
805 cons = agg->rx_agg_cmp_opaque;
806 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
807 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
808
809 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400810 skb_fill_page_desc(skb, i, cons_rx_buf->page,
811 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400812 __clear_bit(cons, rxr->rx_agg_bmap);
813
814 /* It is possible for bnxt_alloc_rx_page() to allocate
815 * a sw_prod index that equals the cons index, so we
816 * need to clear the cons entry now.
817 */
Michael Chan11cd1192017-02-06 16:55:33 -0500818 mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400819 page = cons_rx_buf->page;
820 cons_rx_buf->page = NULL;
821
822 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
823 struct skb_shared_info *shinfo;
824 unsigned int nr_frags;
825
826 shinfo = skb_shinfo(skb);
827 nr_frags = --shinfo->nr_frags;
828 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
829
830 dev_kfree_skb(skb);
831
832 cons_rx_buf->page = page;
833
834 /* Update prod since possibly some pages have been
835 * allocated already.
836 */
837 rxr->rx_agg_prod = prod;
838 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
839 return NULL;
840 }
841
Michael Chan2839f282016-04-25 02:30:50 -0400842 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400843 PCI_DMA_FROMDEVICE);
844
845 skb->data_len += frag_len;
846 skb->len += frag_len;
847 skb->truesize += PAGE_SIZE;
848
849 prod = NEXT_RX_AGG(prod);
850 cp_cons = NEXT_CMP(cp_cons);
851 }
852 rxr->rx_agg_prod = prod;
853 return skb;
854}
855
856static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
857 u8 agg_bufs, u32 *raw_cons)
858{
859 u16 last;
860 struct rx_agg_cmp *agg;
861
862 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
863 last = RING_CMP(*raw_cons);
864 agg = (struct rx_agg_cmp *)
865 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
866 return RX_AGG_CMP_VALID(agg, *raw_cons);
867}
868
869static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
870 unsigned int len,
871 dma_addr_t mapping)
872{
873 struct bnxt *bp = bnapi->bp;
874 struct pci_dev *pdev = bp->pdev;
875 struct sk_buff *skb;
876
877 skb = napi_alloc_skb(&bnapi->napi, len);
878 if (!skb)
879 return NULL;
880
Michael Chan745fc052017-02-06 16:55:34 -0500881 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
882 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400883
Michael Chan6bb19472017-02-06 16:55:32 -0500884 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
885 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400886
Michael Chan745fc052017-02-06 16:55:34 -0500887 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
888 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -0400889
890 skb_put(skb, len);
891 return skb;
892}
893
Michael Chanfa7e2812016-05-10 19:18:00 -0400894static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
895 u32 *raw_cons, void *cmp)
896{
897 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
898 struct rx_cmp *rxcmp = cmp;
899 u32 tmp_raw_cons = *raw_cons;
900 u8 cmp_type, agg_bufs = 0;
901
902 cmp_type = RX_CMP_TYPE(rxcmp);
903
904 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
905 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
906 RX_CMP_AGG_BUFS) >>
907 RX_CMP_AGG_BUFS_SHIFT;
908 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
909 struct rx_tpa_end_cmp *tpa_end = cmp;
910
911 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
912 RX_TPA_END_CMP_AGG_BUFS) >>
913 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
914 }
915
916 if (agg_bufs) {
917 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
918 return -EBUSY;
919 }
920 *raw_cons = tmp_raw_cons;
921 return 0;
922}
923
924static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
925{
926 if (!rxr->bnapi->in_reset) {
927 rxr->bnapi->in_reset = true;
928 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
929 schedule_work(&bp->sp_task);
930 }
931 rxr->rx_next_cons = 0xffff;
932}
933
Michael Chanc0c050c2015-10-22 16:01:17 -0400934static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
935 struct rx_tpa_start_cmp *tpa_start,
936 struct rx_tpa_start_cmp_ext *tpa_start1)
937{
938 u8 agg_id = TPA_START_AGG_ID(tpa_start);
939 u16 cons, prod;
940 struct bnxt_tpa_info *tpa_info;
941 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
942 struct rx_bd *prod_bd;
943 dma_addr_t mapping;
944
945 cons = tpa_start->rx_tpa_start_cmp_opaque;
946 prod = rxr->rx_prod;
947 cons_rx_buf = &rxr->rx_buf_ring[cons];
948 prod_rx_buf = &rxr->rx_buf_ring[prod];
949 tpa_info = &rxr->rx_tpa[agg_id];
950
Michael Chanfa7e2812016-05-10 19:18:00 -0400951 if (unlikely(cons != rxr->rx_next_cons)) {
952 bnxt_sched_reset(bp, rxr);
953 return;
954 }
955
Michael Chanc0c050c2015-10-22 16:01:17 -0400956 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -0500957 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400958
959 mapping = tpa_info->mapping;
Michael Chan11cd1192017-02-06 16:55:33 -0500960 prod_rx_buf->mapping = mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400961
962 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
963
964 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
965
966 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -0500967 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400968 cons_rx_buf->data = NULL;
Michael Chan11cd1192017-02-06 16:55:33 -0500969 tpa_info->mapping = cons_rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400970
971 tpa_info->len =
972 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
973 RX_TPA_START_CMP_LEN_SHIFT;
974 if (likely(TPA_START_HASH_VALID(tpa_start))) {
975 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
976
977 tpa_info->hash_type = PKT_HASH_TYPE_L4;
978 tpa_info->gso_type = SKB_GSO_TCPV4;
979 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
980 if (hash_type == 3)
981 tpa_info->gso_type = SKB_GSO_TCPV6;
982 tpa_info->rss_hash =
983 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
984 } else {
985 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
986 tpa_info->gso_type = 0;
987 if (netif_msg_rx_err(bp))
988 netdev_warn(bp->dev, "TPA packet without valid hash\n");
989 }
990 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
991 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400992 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400993
994 rxr->rx_prod = NEXT_RX(prod);
995 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400996 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400997 cons_rx_buf = &rxr->rx_buf_ring[cons];
998
999 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1000 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1001 cons_rx_buf->data = NULL;
1002}
1003
1004static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1005 u16 cp_cons, u32 agg_bufs)
1006{
1007 if (agg_bufs)
1008 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1009}
1010
Michael Chan94758f82016-06-13 02:25:35 -04001011static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1012 int payload_off, int tcp_ts,
1013 struct sk_buff *skb)
1014{
1015#ifdef CONFIG_INET
1016 struct tcphdr *th;
1017 int len, nw_off;
1018 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1019 u32 hdr_info = tpa_info->hdr_info;
1020 bool loopback = false;
1021
1022 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1023 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1024 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1025
1026 /* If the packet is an internal loopback packet, the offsets will
1027 * have an extra 4 bytes.
1028 */
1029 if (inner_mac_off == 4) {
1030 loopback = true;
1031 } else if (inner_mac_off > 4) {
1032 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1033 ETH_HLEN - 2));
1034
1035 /* We only support inner iPv4/ipv6. If we don't see the
1036 * correct protocol ID, it must be a loopback packet where
1037 * the offsets are off by 4.
1038 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001039 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001040 loopback = true;
1041 }
1042 if (loopback) {
1043 /* internal loopback packet, subtract all offsets by 4 */
1044 inner_ip_off -= 4;
1045 inner_mac_off -= 4;
1046 outer_ip_off -= 4;
1047 }
1048
1049 nw_off = inner_ip_off - ETH_HLEN;
1050 skb_set_network_header(skb, nw_off);
1051 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1052 struct ipv6hdr *iph = ipv6_hdr(skb);
1053
1054 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1055 len = skb->len - skb_transport_offset(skb);
1056 th = tcp_hdr(skb);
1057 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1058 } else {
1059 struct iphdr *iph = ip_hdr(skb);
1060
1061 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1062 len = skb->len - skb_transport_offset(skb);
1063 th = tcp_hdr(skb);
1064 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1065 }
1066
1067 if (inner_mac_off) { /* tunnel */
1068 struct udphdr *uh = NULL;
1069 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1070 ETH_HLEN - 2));
1071
1072 if (proto == htons(ETH_P_IP)) {
1073 struct iphdr *iph = (struct iphdr *)skb->data;
1074
1075 if (iph->protocol == IPPROTO_UDP)
1076 uh = (struct udphdr *)(iph + 1);
1077 } else {
1078 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1079
1080 if (iph->nexthdr == IPPROTO_UDP)
1081 uh = (struct udphdr *)(iph + 1);
1082 }
1083 if (uh) {
1084 if (uh->check)
1085 skb_shinfo(skb)->gso_type |=
1086 SKB_GSO_UDP_TUNNEL_CSUM;
1087 else
1088 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1089 }
1090 }
1091#endif
1092 return skb;
1093}
1094
Michael Chanc0c050c2015-10-22 16:01:17 -04001095#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1096#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1097
Michael Chan309369c2016-06-13 02:25:34 -04001098static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1099 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001100 struct sk_buff *skb)
1101{
Michael Chand1611c32015-10-25 22:27:57 -04001102#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001103 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001104 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001105
Michael Chan309369c2016-06-13 02:25:34 -04001106 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001107 tcp_opt_len = 12;
1108
Michael Chanc0c050c2015-10-22 16:01:17 -04001109 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1110 struct iphdr *iph;
1111
1112 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1113 ETH_HLEN;
1114 skb_set_network_header(skb, nw_off);
1115 iph = ip_hdr(skb);
1116 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1117 len = skb->len - skb_transport_offset(skb);
1118 th = tcp_hdr(skb);
1119 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1120 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1121 struct ipv6hdr *iph;
1122
1123 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1124 ETH_HLEN;
1125 skb_set_network_header(skb, nw_off);
1126 iph = ipv6_hdr(skb);
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 dev_kfree_skb_any(skb);
1133 return NULL;
1134 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001135
1136 if (nw_off) { /* tunnel */
1137 struct udphdr *uh = NULL;
1138
1139 if (skb->protocol == htons(ETH_P_IP)) {
1140 struct iphdr *iph = (struct iphdr *)skb->data;
1141
1142 if (iph->protocol == IPPROTO_UDP)
1143 uh = (struct udphdr *)(iph + 1);
1144 } else {
1145 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1146
1147 if (iph->nexthdr == IPPROTO_UDP)
1148 uh = (struct udphdr *)(iph + 1);
1149 }
1150 if (uh) {
1151 if (uh->check)
1152 skb_shinfo(skb)->gso_type |=
1153 SKB_GSO_UDP_TUNNEL_CSUM;
1154 else
1155 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1156 }
1157 }
1158#endif
1159 return skb;
1160}
1161
Michael Chan309369c2016-06-13 02:25:34 -04001162static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1163 struct bnxt_tpa_info *tpa_info,
1164 struct rx_tpa_end_cmp *tpa_end,
1165 struct rx_tpa_end_cmp_ext *tpa_end1,
1166 struct sk_buff *skb)
1167{
1168#ifdef CONFIG_INET
1169 int payload_off;
1170 u16 segs;
1171
1172 segs = TPA_END_TPA_SEGS(tpa_end);
1173 if (segs == 1)
1174 return skb;
1175
1176 NAPI_GRO_CB(skb)->count = segs;
1177 skb_shinfo(skb)->gso_size =
1178 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1179 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1180 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1181 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1182 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1183 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001184 if (likely(skb))
1185 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001186#endif
1187 return skb;
1188}
1189
Michael Chanc0c050c2015-10-22 16:01:17 -04001190static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1191 struct bnxt_napi *bnapi,
1192 u32 *raw_cons,
1193 struct rx_tpa_end_cmp *tpa_end,
1194 struct rx_tpa_end_cmp_ext *tpa_end1,
1195 bool *agg_event)
1196{
1197 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001198 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001199 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001200 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001201 u16 cp_cons = RING_CMP(*raw_cons);
1202 unsigned int len;
1203 struct bnxt_tpa_info *tpa_info;
1204 dma_addr_t mapping;
1205 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001206 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001207
Michael Chanfa7e2812016-05-10 19:18:00 -04001208 if (unlikely(bnapi->in_reset)) {
1209 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1210
1211 if (rc < 0)
1212 return ERR_PTR(-EBUSY);
1213 return NULL;
1214 }
1215
Michael Chanc0c050c2015-10-22 16:01:17 -04001216 tpa_info = &rxr->rx_tpa[agg_id];
1217 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001218 data_ptr = tpa_info->data_ptr;
1219 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001220 len = tpa_info->len;
1221 mapping = tpa_info->mapping;
1222
1223 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1224 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1225
1226 if (agg_bufs) {
1227 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1228 return ERR_PTR(-EBUSY);
1229
1230 *agg_event = true;
1231 cp_cons = NEXT_CMP(cp_cons);
1232 }
1233
1234 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1235 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1236 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1237 agg_bufs, (int)MAX_SKB_FRAGS);
1238 return NULL;
1239 }
1240
1241 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001242 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001243 if (!skb) {
1244 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1245 return NULL;
1246 }
1247 } else {
1248 u8 *new_data;
1249 dma_addr_t new_mapping;
1250
1251 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1252 if (!new_data) {
1253 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1254 return NULL;
1255 }
1256
1257 tpa_info->data = new_data;
Michael Chanb3dba772017-02-06 16:55:35 -05001258 tpa_info->data_ptr = new_data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04001259 tpa_info->mapping = new_mapping;
1260
1261 skb = build_skb(data, 0);
1262 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
Michael Chan745fc052017-02-06 16:55:34 -05001263 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001264
1265 if (!skb) {
1266 kfree(data);
1267 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1268 return NULL;
1269 }
Michael Chanb3dba772017-02-06 16:55:35 -05001270 skb_reserve(skb, bp->rx_offset);
Michael Chanc0c050c2015-10-22 16:01:17 -04001271 skb_put(skb, len);
1272 }
1273
1274 if (agg_bufs) {
1275 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1276 if (!skb) {
1277 /* Page reuse already handled by bnxt_rx_pages(). */
1278 return NULL;
1279 }
1280 }
1281 skb->protocol = eth_type_trans(skb, bp->dev);
1282
1283 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1284 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1285
Michael Chan8852ddb2016-06-06 02:37:16 -04001286 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1287 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001288 u16 vlan_proto = tpa_info->metadata >>
1289 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001290 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001291
Michael Chan8852ddb2016-06-06 02:37:16 -04001292 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001293 }
1294
1295 skb_checksum_none_assert(skb);
1296 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1297 skb->ip_summed = CHECKSUM_UNNECESSARY;
1298 skb->csum_level =
1299 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1300 }
1301
1302 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001303 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001304
1305 return skb;
1306}
1307
1308/* returns the following:
1309 * 1 - 1 packet successfully received
1310 * 0 - successful TPA_START, packet not completed yet
1311 * -EBUSY - completion ring does not have all the agg buffers yet
1312 * -ENOMEM - packet aborted due to out of memory
1313 * -EIO - packet aborted due to hw error indicated in BD
1314 */
1315static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1316 bool *agg_event)
1317{
1318 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001319 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001320 struct net_device *dev = bp->dev;
1321 struct rx_cmp *rxcmp;
1322 struct rx_cmp_ext *rxcmp1;
1323 u32 tmp_raw_cons = *raw_cons;
1324 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1325 struct bnxt_sw_rx_bd *rx_buf;
1326 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001327 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001328 dma_addr_t dma_addr;
1329 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001330 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001331 int rc = 0;
1332
1333 rxcmp = (struct rx_cmp *)
1334 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1335
1336 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1337 cp_cons = RING_CMP(tmp_raw_cons);
1338 rxcmp1 = (struct rx_cmp_ext *)
1339 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1340
1341 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1342 return -EBUSY;
1343
1344 cmp_type = RX_CMP_TYPE(rxcmp);
1345
1346 prod = rxr->rx_prod;
1347
1348 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1349 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1350 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1351
1352 goto next_rx_no_prod;
1353
1354 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1355 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1356 (struct rx_tpa_end_cmp *)rxcmp,
1357 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1358 agg_event);
1359
1360 if (unlikely(IS_ERR(skb)))
1361 return -EBUSY;
1362
1363 rc = -ENOMEM;
1364 if (likely(skb)) {
1365 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001366 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001367 rc = 1;
1368 }
1369 goto next_rx_no_prod;
1370 }
1371
1372 cons = rxcmp->rx_cmp_opaque;
1373 rx_buf = &rxr->rx_buf_ring[cons];
1374 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001375 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001376 if (unlikely(cons != rxr->rx_next_cons)) {
1377 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1378
1379 bnxt_sched_reset(bp, rxr);
1380 return rc1;
1381 }
Michael Chan6bb19472017-02-06 16:55:32 -05001382 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001383
1384 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1385 RX_CMP_AGG_BUFS_SHIFT;
1386
1387 if (agg_bufs) {
1388 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1389 return -EBUSY;
1390
1391 cp_cons = NEXT_CMP(cp_cons);
1392 *agg_event = true;
1393 }
1394
1395 rx_buf->data = NULL;
1396 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1397 bnxt_reuse_rx_data(rxr, cons, data);
1398 if (agg_bufs)
1399 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1400
1401 rc = -EIO;
1402 goto next_rx;
1403 }
1404
1405 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
Michael Chan11cd1192017-02-06 16:55:33 -05001406 dma_addr = rx_buf->mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -04001407
1408 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001409 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001410 bnxt_reuse_rx_data(rxr, cons, data);
1411 if (!skb) {
1412 rc = -ENOMEM;
1413 goto next_rx;
1414 }
1415 } else {
Michael Chan6bb19472017-02-06 16:55:32 -05001416 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1417 len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001418 if (!skb) {
1419 rc = -ENOMEM;
1420 goto next_rx;
1421 }
1422 }
1423
1424 if (agg_bufs) {
1425 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1426 if (!skb) {
1427 rc = -ENOMEM;
1428 goto next_rx;
1429 }
1430 }
1431
1432 if (RX_CMP_HASH_VALID(rxcmp)) {
1433 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1434 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1435
1436 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1437 if (hash_type != 1 && hash_type != 3)
1438 type = PKT_HASH_TYPE_L3;
1439 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1440 }
1441
1442 skb->protocol = eth_type_trans(skb, dev);
1443
Michael Chan8852ddb2016-06-06 02:37:16 -04001444 if ((rxcmp1->rx_cmp_flags2 &
1445 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1446 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001447 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001448 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001449 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1450
Michael Chan8852ddb2016-06-06 02:37:16 -04001451 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001452 }
1453
1454 skb_checksum_none_assert(skb);
1455 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1456 if (dev->features & NETIF_F_RXCSUM) {
1457 skb->ip_summed = CHECKSUM_UNNECESSARY;
1458 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1459 }
1460 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001461 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1462 if (dev->features & NETIF_F_RXCSUM)
1463 cpr->rx_l4_csum_errors++;
1464 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001465 }
1466
1467 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001468 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001469 rc = 1;
1470
1471next_rx:
1472 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001473 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001474
1475next_rx_no_prod:
1476 *raw_cons = tmp_raw_cons;
1477
1478 return rc;
1479}
1480
Michael Chan4bb13ab2016-04-05 14:09:01 -04001481#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001482 ((data) & \
1483 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001484
Michael Chanc0c050c2015-10-22 16:01:17 -04001485static int bnxt_async_event_process(struct bnxt *bp,
1486 struct hwrm_async_event_cmpl *cmpl)
1487{
1488 u16 event_id = le16_to_cpu(cmpl->event_id);
1489
1490 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1491 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001492 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001493 u32 data1 = le32_to_cpu(cmpl->event_data1);
1494 struct bnxt_link_info *link_info = &bp->link_info;
1495
1496 if (BNXT_VF(bp))
1497 goto async_event_process_exit;
1498 if (data1 & 0x20000) {
1499 u16 fw_speed = link_info->force_link_speed;
1500 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1501
1502 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1503 speed);
1504 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001505 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001506 /* fall thru */
1507 }
Michael Chan87c374d2016-12-02 21:17:16 -05001508 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001509 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001510 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001511 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001512 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001513 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001514 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001515 u32 data1 = le32_to_cpu(cmpl->event_data1);
1516 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1517
1518 if (BNXT_VF(bp))
1519 break;
1520
1521 if (bp->pf.port_id != port_id)
1522 break;
1523
Michael Chan4bb13ab2016-04-05 14:09:01 -04001524 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1525 break;
1526 }
Michael Chan87c374d2016-12-02 21:17:16 -05001527 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001528 if (BNXT_PF(bp))
1529 goto async_event_process_exit;
1530 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1531 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001532 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001533 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001534 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001535 schedule_work(&bp->sp_task);
1536async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001537 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001538 return 0;
1539}
1540
1541static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1542{
1543 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1544 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1545 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1546 (struct hwrm_fwd_req_cmpl *)txcmp;
1547
1548 switch (cmpl_type) {
1549 case CMPL_BASE_TYPE_HWRM_DONE:
1550 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1551 if (seq_id == bp->hwrm_intr_seq_id)
1552 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1553 else
1554 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1555 break;
1556
1557 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1558 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1559
1560 if ((vf_id < bp->pf.first_vf_id) ||
1561 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1562 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1563 vf_id);
1564 return -EINVAL;
1565 }
1566
1567 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1568 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1569 schedule_work(&bp->sp_task);
1570 break;
1571
1572 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1573 bnxt_async_event_process(bp,
1574 (struct hwrm_async_event_cmpl *)txcmp);
1575
1576 default:
1577 break;
1578 }
1579
1580 return 0;
1581}
1582
1583static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1584{
1585 struct bnxt_napi *bnapi = dev_instance;
1586 struct bnxt *bp = bnapi->bp;
1587 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1588 u32 cons = RING_CMP(cpr->cp_raw_cons);
1589
1590 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1591 napi_schedule(&bnapi->napi);
1592 return IRQ_HANDLED;
1593}
1594
1595static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1596{
1597 u32 raw_cons = cpr->cp_raw_cons;
1598 u16 cons = RING_CMP(raw_cons);
1599 struct tx_cmp *txcmp;
1600
1601 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1602
1603 return TX_CMP_VALID(txcmp, raw_cons);
1604}
1605
Michael Chanc0c050c2015-10-22 16:01:17 -04001606static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1607{
1608 struct bnxt_napi *bnapi = dev_instance;
1609 struct bnxt *bp = bnapi->bp;
1610 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1611 u32 cons = RING_CMP(cpr->cp_raw_cons);
1612 u32 int_status;
1613
1614 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1615
1616 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001617 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001618 /* return if erroneous interrupt */
1619 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1620 return IRQ_NONE;
1621 }
1622
1623 /* disable ring IRQ */
1624 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1625
1626 /* Return here if interrupt is shared and is disabled. */
1627 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1628 return IRQ_HANDLED;
1629
1630 napi_schedule(&bnapi->napi);
1631 return IRQ_HANDLED;
1632}
1633
1634static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1635{
1636 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1637 u32 raw_cons = cpr->cp_raw_cons;
1638 u32 cons;
1639 int tx_pkts = 0;
1640 int rx_pkts = 0;
1641 bool rx_event = false;
1642 bool agg_event = false;
1643 struct tx_cmp *txcmp;
1644
1645 while (1) {
1646 int rc;
1647
1648 cons = RING_CMP(raw_cons);
1649 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1650
1651 if (!TX_CMP_VALID(txcmp, raw_cons))
1652 break;
1653
Michael Chan67a95e22016-05-04 16:56:43 -04001654 /* The valid test of the entry must be done first before
1655 * reading any further.
1656 */
Michael Chanb67daab2016-05-15 03:04:51 -04001657 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001658 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1659 tx_pkts++;
1660 /* return full budget so NAPI will complete. */
1661 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1662 rx_pkts = budget;
1663 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1664 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1665 if (likely(rc >= 0))
1666 rx_pkts += rc;
1667 else if (rc == -EBUSY) /* partial completion */
1668 break;
1669 rx_event = true;
1670 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1671 CMPL_BASE_TYPE_HWRM_DONE) ||
1672 (TX_CMP_TYPE(txcmp) ==
1673 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1674 (TX_CMP_TYPE(txcmp) ==
1675 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1676 bnxt_hwrm_handler(bp, txcmp);
1677 }
1678 raw_cons = NEXT_RAW_CMP(raw_cons);
1679
1680 if (rx_pkts == budget)
1681 break;
1682 }
1683
1684 cpr->cp_raw_cons = raw_cons;
1685 /* ACK completion ring before freeing tx ring and producing new
1686 * buffers in rx/agg rings to prevent overflowing the completion
1687 * ring.
1688 */
1689 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1690
1691 if (tx_pkts)
1692 bnxt_tx_int(bp, bnapi, tx_pkts);
1693
1694 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001695 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001696
1697 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1698 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1699 if (agg_event) {
1700 writel(DB_KEY_RX | rxr->rx_agg_prod,
1701 rxr->rx_agg_doorbell);
1702 writel(DB_KEY_RX | rxr->rx_agg_prod,
1703 rxr->rx_agg_doorbell);
1704 }
1705 }
1706 return rx_pkts;
1707}
1708
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001709static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1710{
1711 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1712 struct bnxt *bp = bnapi->bp;
1713 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1714 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1715 struct tx_cmp *txcmp;
1716 struct rx_cmp_ext *rxcmp1;
1717 u32 cp_cons, tmp_raw_cons;
1718 u32 raw_cons = cpr->cp_raw_cons;
1719 u32 rx_pkts = 0;
1720 bool agg_event = false;
1721
1722 while (1) {
1723 int rc;
1724
1725 cp_cons = RING_CMP(raw_cons);
1726 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1727
1728 if (!TX_CMP_VALID(txcmp, raw_cons))
1729 break;
1730
1731 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1732 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1733 cp_cons = RING_CMP(tmp_raw_cons);
1734 rxcmp1 = (struct rx_cmp_ext *)
1735 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1736
1737 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1738 break;
1739
1740 /* force an error to recycle the buffer */
1741 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1742 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1743
1744 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1745 if (likely(rc == -EIO))
1746 rx_pkts++;
1747 else if (rc == -EBUSY) /* partial completion */
1748 break;
1749 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1750 CMPL_BASE_TYPE_HWRM_DONE)) {
1751 bnxt_hwrm_handler(bp, txcmp);
1752 } else {
1753 netdev_err(bp->dev,
1754 "Invalid completion received on special ring\n");
1755 }
1756 raw_cons = NEXT_RAW_CMP(raw_cons);
1757
1758 if (rx_pkts == budget)
1759 break;
1760 }
1761
1762 cpr->cp_raw_cons = raw_cons;
1763 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1764 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1765 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1766
1767 if (agg_event) {
1768 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1769 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1770 }
1771
1772 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001773 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001774 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1775 }
1776 return rx_pkts;
1777}
1778
Michael Chanc0c050c2015-10-22 16:01:17 -04001779static int bnxt_poll(struct napi_struct *napi, int budget)
1780{
1781 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1782 struct bnxt *bp = bnapi->bp;
1783 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1784 int work_done = 0;
1785
Michael Chanc0c050c2015-10-22 16:01:17 -04001786 while (1) {
1787 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1788
1789 if (work_done >= budget)
1790 break;
1791
1792 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001793 if (napi_complete_done(napi, work_done))
1794 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1795 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001796 break;
1797 }
1798 }
1799 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001800 return work_done;
1801}
1802
Michael Chanc0c050c2015-10-22 16:01:17 -04001803static void bnxt_free_tx_skbs(struct bnxt *bp)
1804{
1805 int i, max_idx;
1806 struct pci_dev *pdev = bp->pdev;
1807
Michael Chanb6ab4b02016-01-02 23:44:59 -05001808 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001809 return;
1810
1811 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1812 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001813 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001814 int j;
1815
Michael Chanc0c050c2015-10-22 16:01:17 -04001816 for (j = 0; j < max_idx;) {
1817 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1818 struct sk_buff *skb = tx_buf->skb;
1819 int k, last;
1820
1821 if (!skb) {
1822 j++;
1823 continue;
1824 }
1825
1826 tx_buf->skb = NULL;
1827
1828 if (tx_buf->is_push) {
1829 dev_kfree_skb(skb);
1830 j += 2;
1831 continue;
1832 }
1833
1834 dma_unmap_single(&pdev->dev,
1835 dma_unmap_addr(tx_buf, mapping),
1836 skb_headlen(skb),
1837 PCI_DMA_TODEVICE);
1838
1839 last = tx_buf->nr_frags;
1840 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001841 for (k = 0; k < last; k++, j++) {
1842 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001843 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1844
Michael Chand612a572016-01-28 03:11:22 -05001845 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001846 dma_unmap_page(
1847 &pdev->dev,
1848 dma_unmap_addr(tx_buf, mapping),
1849 skb_frag_size(frag), PCI_DMA_TODEVICE);
1850 }
1851 dev_kfree_skb(skb);
1852 }
1853 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1854 }
1855}
1856
1857static void bnxt_free_rx_skbs(struct bnxt *bp)
1858{
1859 int i, max_idx, max_agg_idx;
1860 struct pci_dev *pdev = bp->pdev;
1861
Michael Chanb6ab4b02016-01-02 23:44:59 -05001862 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001863 return;
1864
1865 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1866 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1867 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001868 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001869 int j;
1870
Michael Chanc0c050c2015-10-22 16:01:17 -04001871 if (rxr->rx_tpa) {
1872 for (j = 0; j < MAX_TPA; j++) {
1873 struct bnxt_tpa_info *tpa_info =
1874 &rxr->rx_tpa[j];
1875 u8 *data = tpa_info->data;
1876
1877 if (!data)
1878 continue;
1879
Michael Chan745fc052017-02-06 16:55:34 -05001880 dma_unmap_single(&pdev->dev, tpa_info->mapping,
1881 bp->rx_buf_use_size,
1882 bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001883
1884 tpa_info->data = NULL;
1885
1886 kfree(data);
1887 }
1888 }
1889
1890 for (j = 0; j < max_idx; j++) {
1891 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan6bb19472017-02-06 16:55:32 -05001892 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001893
1894 if (!data)
1895 continue;
1896
Michael Chan11cd1192017-02-06 16:55:33 -05001897 dma_unmap_single(&pdev->dev, rx_buf->mapping,
Michael Chan745fc052017-02-06 16:55:34 -05001898 bp->rx_buf_use_size, bp->rx_dir);
Michael Chanc0c050c2015-10-22 16:01:17 -04001899
1900 rx_buf->data = NULL;
1901
1902 kfree(data);
1903 }
1904
1905 for (j = 0; j < max_agg_idx; j++) {
1906 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1907 &rxr->rx_agg_ring[j];
1908 struct page *page = rx_agg_buf->page;
1909
1910 if (!page)
1911 continue;
1912
Michael Chan11cd1192017-02-06 16:55:33 -05001913 dma_unmap_page(&pdev->dev, rx_agg_buf->mapping,
Michael Chan2839f282016-04-25 02:30:50 -04001914 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001915
1916 rx_agg_buf->page = NULL;
1917 __clear_bit(j, rxr->rx_agg_bmap);
1918
1919 __free_page(page);
1920 }
Michael Chan89d0a062016-04-25 02:30:51 -04001921 if (rxr->rx_page) {
1922 __free_page(rxr->rx_page);
1923 rxr->rx_page = NULL;
1924 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001925 }
1926}
1927
1928static void bnxt_free_skbs(struct bnxt *bp)
1929{
1930 bnxt_free_tx_skbs(bp);
1931 bnxt_free_rx_skbs(bp);
1932}
1933
1934static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1935{
1936 struct pci_dev *pdev = bp->pdev;
1937 int i;
1938
1939 for (i = 0; i < ring->nr_pages; i++) {
1940 if (!ring->pg_arr[i])
1941 continue;
1942
1943 dma_free_coherent(&pdev->dev, ring->page_size,
1944 ring->pg_arr[i], ring->dma_arr[i]);
1945
1946 ring->pg_arr[i] = NULL;
1947 }
1948 if (ring->pg_tbl) {
1949 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1950 ring->pg_tbl, ring->pg_tbl_map);
1951 ring->pg_tbl = NULL;
1952 }
1953 if (ring->vmem_size && *ring->vmem) {
1954 vfree(*ring->vmem);
1955 *ring->vmem = NULL;
1956 }
1957}
1958
1959static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1960{
1961 int i;
1962 struct pci_dev *pdev = bp->pdev;
1963
1964 if (ring->nr_pages > 1) {
1965 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1966 ring->nr_pages * 8,
1967 &ring->pg_tbl_map,
1968 GFP_KERNEL);
1969 if (!ring->pg_tbl)
1970 return -ENOMEM;
1971 }
1972
1973 for (i = 0; i < ring->nr_pages; i++) {
1974 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1975 ring->page_size,
1976 &ring->dma_arr[i],
1977 GFP_KERNEL);
1978 if (!ring->pg_arr[i])
1979 return -ENOMEM;
1980
1981 if (ring->nr_pages > 1)
1982 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1983 }
1984
1985 if (ring->vmem_size) {
1986 *ring->vmem = vzalloc(ring->vmem_size);
1987 if (!(*ring->vmem))
1988 return -ENOMEM;
1989 }
1990 return 0;
1991}
1992
1993static void bnxt_free_rx_rings(struct bnxt *bp)
1994{
1995 int i;
1996
Michael Chanb6ab4b02016-01-02 23:44:59 -05001997 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001998 return;
1999
2000 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002001 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002002 struct bnxt_ring_struct *ring;
2003
Michael Chanc0c050c2015-10-22 16:01:17 -04002004 kfree(rxr->rx_tpa);
2005 rxr->rx_tpa = NULL;
2006
2007 kfree(rxr->rx_agg_bmap);
2008 rxr->rx_agg_bmap = NULL;
2009
2010 ring = &rxr->rx_ring_struct;
2011 bnxt_free_ring(bp, ring);
2012
2013 ring = &rxr->rx_agg_ring_struct;
2014 bnxt_free_ring(bp, ring);
2015 }
2016}
2017
2018static int bnxt_alloc_rx_rings(struct bnxt *bp)
2019{
2020 int i, rc, agg_rings = 0, tpa_rings = 0;
2021
Michael Chanb6ab4b02016-01-02 23:44:59 -05002022 if (!bp->rx_ring)
2023 return -ENOMEM;
2024
Michael Chanc0c050c2015-10-22 16:01:17 -04002025 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2026 agg_rings = 1;
2027
2028 if (bp->flags & BNXT_FLAG_TPA)
2029 tpa_rings = 1;
2030
2031 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002032 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002033 struct bnxt_ring_struct *ring;
2034
Michael Chanc0c050c2015-10-22 16:01:17 -04002035 ring = &rxr->rx_ring_struct;
2036
2037 rc = bnxt_alloc_ring(bp, ring);
2038 if (rc)
2039 return rc;
2040
2041 if (agg_rings) {
2042 u16 mem_size;
2043
2044 ring = &rxr->rx_agg_ring_struct;
2045 rc = bnxt_alloc_ring(bp, ring);
2046 if (rc)
2047 return rc;
2048
2049 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2050 mem_size = rxr->rx_agg_bmap_size / 8;
2051 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2052 if (!rxr->rx_agg_bmap)
2053 return -ENOMEM;
2054
2055 if (tpa_rings) {
2056 rxr->rx_tpa = kcalloc(MAX_TPA,
2057 sizeof(struct bnxt_tpa_info),
2058 GFP_KERNEL);
2059 if (!rxr->rx_tpa)
2060 return -ENOMEM;
2061 }
2062 }
2063 }
2064 return 0;
2065}
2066
2067static void bnxt_free_tx_rings(struct bnxt *bp)
2068{
2069 int i;
2070 struct pci_dev *pdev = bp->pdev;
2071
Michael Chanb6ab4b02016-01-02 23:44:59 -05002072 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002073 return;
2074
2075 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002076 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002077 struct bnxt_ring_struct *ring;
2078
Michael Chanc0c050c2015-10-22 16:01:17 -04002079 if (txr->tx_push) {
2080 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2081 txr->tx_push, txr->tx_push_mapping);
2082 txr->tx_push = NULL;
2083 }
2084
2085 ring = &txr->tx_ring_struct;
2086
2087 bnxt_free_ring(bp, ring);
2088 }
2089}
2090
2091static int bnxt_alloc_tx_rings(struct bnxt *bp)
2092{
2093 int i, j, rc;
2094 struct pci_dev *pdev = bp->pdev;
2095
2096 bp->tx_push_size = 0;
2097 if (bp->tx_push_thresh) {
2098 int push_size;
2099
2100 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2101 bp->tx_push_thresh);
2102
Michael Chan4419dbe2016-02-10 17:33:49 -05002103 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002104 push_size = 0;
2105 bp->tx_push_thresh = 0;
2106 }
2107
2108 bp->tx_push_size = push_size;
2109 }
2110
2111 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002112 struct bnxt_tx_ring_info *txr = &bp->tx_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 = &txr->tx_ring_struct;
2116
2117 rc = bnxt_alloc_ring(bp, ring);
2118 if (rc)
2119 return rc;
2120
2121 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002122 dma_addr_t mapping;
2123
2124 /* One pre-allocated DMA buffer to backup
2125 * TX push operation
2126 */
2127 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2128 bp->tx_push_size,
2129 &txr->tx_push_mapping,
2130 GFP_KERNEL);
2131
2132 if (!txr->tx_push)
2133 return -ENOMEM;
2134
Michael Chanc0c050c2015-10-22 16:01:17 -04002135 mapping = txr->tx_push_mapping +
2136 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002137 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002138
Michael Chan4419dbe2016-02-10 17:33:49 -05002139 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002140 }
2141 ring->queue_id = bp->q_info[j].queue_id;
2142 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2143 j++;
2144 }
2145 return 0;
2146}
2147
2148static void bnxt_free_cp_rings(struct bnxt *bp)
2149{
2150 int i;
2151
2152 if (!bp->bnapi)
2153 return;
2154
2155 for (i = 0; i < bp->cp_nr_rings; i++) {
2156 struct bnxt_napi *bnapi = bp->bnapi[i];
2157 struct bnxt_cp_ring_info *cpr;
2158 struct bnxt_ring_struct *ring;
2159
2160 if (!bnapi)
2161 continue;
2162
2163 cpr = &bnapi->cp_ring;
2164 ring = &cpr->cp_ring_struct;
2165
2166 bnxt_free_ring(bp, ring);
2167 }
2168}
2169
2170static int bnxt_alloc_cp_rings(struct bnxt *bp)
2171{
2172 int i, rc;
2173
2174 for (i = 0; i < bp->cp_nr_rings; i++) {
2175 struct bnxt_napi *bnapi = bp->bnapi[i];
2176 struct bnxt_cp_ring_info *cpr;
2177 struct bnxt_ring_struct *ring;
2178
2179 if (!bnapi)
2180 continue;
2181
2182 cpr = &bnapi->cp_ring;
2183 ring = &cpr->cp_ring_struct;
2184
2185 rc = bnxt_alloc_ring(bp, ring);
2186 if (rc)
2187 return rc;
2188 }
2189 return 0;
2190}
2191
2192static void bnxt_init_ring_struct(struct bnxt *bp)
2193{
2194 int i;
2195
2196 for (i = 0; i < bp->cp_nr_rings; i++) {
2197 struct bnxt_napi *bnapi = bp->bnapi[i];
2198 struct bnxt_cp_ring_info *cpr;
2199 struct bnxt_rx_ring_info *rxr;
2200 struct bnxt_tx_ring_info *txr;
2201 struct bnxt_ring_struct *ring;
2202
2203 if (!bnapi)
2204 continue;
2205
2206 cpr = &bnapi->cp_ring;
2207 ring = &cpr->cp_ring_struct;
2208 ring->nr_pages = bp->cp_nr_pages;
2209 ring->page_size = HW_CMPD_RING_SIZE;
2210 ring->pg_arr = (void **)cpr->cp_desc_ring;
2211 ring->dma_arr = cpr->cp_desc_mapping;
2212 ring->vmem_size = 0;
2213
Michael Chanb6ab4b02016-01-02 23:44:59 -05002214 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002215 if (!rxr)
2216 goto skip_rx;
2217
Michael Chanc0c050c2015-10-22 16:01:17 -04002218 ring = &rxr->rx_ring_struct;
2219 ring->nr_pages = bp->rx_nr_pages;
2220 ring->page_size = HW_RXBD_RING_SIZE;
2221 ring->pg_arr = (void **)rxr->rx_desc_ring;
2222 ring->dma_arr = rxr->rx_desc_mapping;
2223 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2224 ring->vmem = (void **)&rxr->rx_buf_ring;
2225
2226 ring = &rxr->rx_agg_ring_struct;
2227 ring->nr_pages = bp->rx_agg_nr_pages;
2228 ring->page_size = HW_RXBD_RING_SIZE;
2229 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2230 ring->dma_arr = rxr->rx_agg_desc_mapping;
2231 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2232 ring->vmem = (void **)&rxr->rx_agg_ring;
2233
Michael Chan3b2b7d92016-01-02 23:45:00 -05002234skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002235 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002236 if (!txr)
2237 continue;
2238
Michael Chanc0c050c2015-10-22 16:01:17 -04002239 ring = &txr->tx_ring_struct;
2240 ring->nr_pages = bp->tx_nr_pages;
2241 ring->page_size = HW_RXBD_RING_SIZE;
2242 ring->pg_arr = (void **)txr->tx_desc_ring;
2243 ring->dma_arr = txr->tx_desc_mapping;
2244 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2245 ring->vmem = (void **)&txr->tx_buf_ring;
2246 }
2247}
2248
2249static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2250{
2251 int i;
2252 u32 prod;
2253 struct rx_bd **rx_buf_ring;
2254
2255 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2256 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2257 int j;
2258 struct rx_bd *rxbd;
2259
2260 rxbd = rx_buf_ring[i];
2261 if (!rxbd)
2262 continue;
2263
2264 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2265 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2266 rxbd->rx_bd_opaque = prod;
2267 }
2268 }
2269}
2270
2271static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2272{
2273 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002274 struct bnxt_rx_ring_info *rxr;
2275 struct bnxt_ring_struct *ring;
2276 u32 prod, type;
2277 int i;
2278
Michael Chanc0c050c2015-10-22 16:01:17 -04002279 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2280 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2281
2282 if (NET_IP_ALIGN == 2)
2283 type |= RX_BD_FLAGS_SOP;
2284
Michael Chanb6ab4b02016-01-02 23:44:59 -05002285 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002286 ring = &rxr->rx_ring_struct;
2287 bnxt_init_rxbd_pages(ring, type);
2288
2289 prod = rxr->rx_prod;
2290 for (i = 0; i < bp->rx_ring_size; i++) {
2291 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2292 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2293 ring_nr, i, bp->rx_ring_size);
2294 break;
2295 }
2296 prod = NEXT_RX(prod);
2297 }
2298 rxr->rx_prod = prod;
2299 ring->fw_ring_id = INVALID_HW_RING_ID;
2300
Michael Chanedd0c2c2015-12-27 18:19:19 -05002301 ring = &rxr->rx_agg_ring_struct;
2302 ring->fw_ring_id = INVALID_HW_RING_ID;
2303
Michael Chanc0c050c2015-10-22 16:01:17 -04002304 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2305 return 0;
2306
Michael Chan2839f282016-04-25 02:30:50 -04002307 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002308 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2309
2310 bnxt_init_rxbd_pages(ring, type);
2311
2312 prod = rxr->rx_agg_prod;
2313 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2314 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2315 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2316 ring_nr, i, bp->rx_ring_size);
2317 break;
2318 }
2319 prod = NEXT_RX_AGG(prod);
2320 }
2321 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002322
2323 if (bp->flags & BNXT_FLAG_TPA) {
2324 if (rxr->rx_tpa) {
2325 u8 *data;
2326 dma_addr_t mapping;
2327
2328 for (i = 0; i < MAX_TPA; i++) {
2329 data = __bnxt_alloc_rx_data(bp, &mapping,
2330 GFP_KERNEL);
2331 if (!data)
2332 return -ENOMEM;
2333
2334 rxr->rx_tpa[i].data = data;
Michael Chanb3dba772017-02-06 16:55:35 -05002335 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
Michael Chanc0c050c2015-10-22 16:01:17 -04002336 rxr->rx_tpa[i].mapping = mapping;
2337 }
2338 } else {
2339 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2340 return -ENOMEM;
2341 }
2342 }
2343
2344 return 0;
2345}
2346
2347static int bnxt_init_rx_rings(struct bnxt *bp)
2348{
2349 int i, rc = 0;
2350
Michael Chanb3dba772017-02-06 16:55:35 -05002351 bp->rx_offset = BNXT_RX_OFFSET;
2352 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2353
Michael Chanc0c050c2015-10-22 16:01:17 -04002354 for (i = 0; i < bp->rx_nr_rings; i++) {
2355 rc = bnxt_init_one_rx_ring(bp, i);
2356 if (rc)
2357 break;
2358 }
2359
2360 return rc;
2361}
2362
2363static int bnxt_init_tx_rings(struct bnxt *bp)
2364{
2365 u16 i;
2366
2367 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2368 MAX_SKB_FRAGS + 1);
2369
2370 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002371 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002372 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2373
2374 ring->fw_ring_id = INVALID_HW_RING_ID;
2375 }
2376
2377 return 0;
2378}
2379
2380static void bnxt_free_ring_grps(struct bnxt *bp)
2381{
2382 kfree(bp->grp_info);
2383 bp->grp_info = NULL;
2384}
2385
2386static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2387{
2388 int i;
2389
2390 if (irq_re_init) {
2391 bp->grp_info = kcalloc(bp->cp_nr_rings,
2392 sizeof(struct bnxt_ring_grp_info),
2393 GFP_KERNEL);
2394 if (!bp->grp_info)
2395 return -ENOMEM;
2396 }
2397 for (i = 0; i < bp->cp_nr_rings; i++) {
2398 if (irq_re_init)
2399 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2400 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2401 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2402 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2403 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2404 }
2405 return 0;
2406}
2407
2408static void bnxt_free_vnics(struct bnxt *bp)
2409{
2410 kfree(bp->vnic_info);
2411 bp->vnic_info = NULL;
2412 bp->nr_vnics = 0;
2413}
2414
2415static int bnxt_alloc_vnics(struct bnxt *bp)
2416{
2417 int num_vnics = 1;
2418
2419#ifdef CONFIG_RFS_ACCEL
2420 if (bp->flags & BNXT_FLAG_RFS)
2421 num_vnics += bp->rx_nr_rings;
2422#endif
2423
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002424 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2425 num_vnics++;
2426
Michael Chanc0c050c2015-10-22 16:01:17 -04002427 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2428 GFP_KERNEL);
2429 if (!bp->vnic_info)
2430 return -ENOMEM;
2431
2432 bp->nr_vnics = num_vnics;
2433 return 0;
2434}
2435
2436static void bnxt_init_vnics(struct bnxt *bp)
2437{
2438 int i;
2439
2440 for (i = 0; i < bp->nr_vnics; i++) {
2441 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2442
2443 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002444 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2445 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002446 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2447
2448 if (bp->vnic_info[i].rss_hash_key) {
2449 if (i == 0)
2450 prandom_bytes(vnic->rss_hash_key,
2451 HW_HASH_KEY_SIZE);
2452 else
2453 memcpy(vnic->rss_hash_key,
2454 bp->vnic_info[0].rss_hash_key,
2455 HW_HASH_KEY_SIZE);
2456 }
2457 }
2458}
2459
2460static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2461{
2462 int pages;
2463
2464 pages = ring_size / desc_per_pg;
2465
2466 if (!pages)
2467 return 1;
2468
2469 pages++;
2470
2471 while (pages & (pages - 1))
2472 pages++;
2473
2474 return pages;
2475}
2476
2477static void bnxt_set_tpa_flags(struct bnxt *bp)
2478{
2479 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002480 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2481 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002482 if (bp->dev->features & NETIF_F_LRO)
2483 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002484 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002485 bp->flags |= BNXT_FLAG_GRO;
2486}
2487
2488/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2489 * be set on entry.
2490 */
2491void bnxt_set_ring_params(struct bnxt *bp)
2492{
2493 u32 ring_size, rx_size, rx_space;
2494 u32 agg_factor = 0, agg_ring_size = 0;
2495
2496 /* 8 for CRC and VLAN */
2497 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2498
2499 rx_space = rx_size + NET_SKB_PAD +
2500 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2501
2502 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2503 ring_size = bp->rx_ring_size;
2504 bp->rx_agg_ring_size = 0;
2505 bp->rx_agg_nr_pages = 0;
2506
2507 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002508 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002509
2510 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002511 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002512 u32 jumbo_factor;
2513
2514 bp->flags |= BNXT_FLAG_JUMBO;
2515 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2516 if (jumbo_factor > agg_factor)
2517 agg_factor = jumbo_factor;
2518 }
2519 agg_ring_size = ring_size * agg_factor;
2520
2521 if (agg_ring_size) {
2522 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2523 RX_DESC_CNT);
2524 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2525 u32 tmp = agg_ring_size;
2526
2527 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2528 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2529 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2530 tmp, agg_ring_size);
2531 }
2532 bp->rx_agg_ring_size = agg_ring_size;
2533 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2534 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2535 rx_space = rx_size + NET_SKB_PAD +
2536 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2537 }
2538
2539 bp->rx_buf_use_size = rx_size;
2540 bp->rx_buf_size = rx_space;
2541
2542 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2543 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2544
2545 ring_size = bp->tx_ring_size;
2546 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2547 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2548
2549 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2550 bp->cp_ring_size = ring_size;
2551
2552 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2553 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2554 bp->cp_nr_pages = MAX_CP_PAGES;
2555 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2556 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2557 ring_size, bp->cp_ring_size);
2558 }
2559 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2560 bp->cp_ring_mask = bp->cp_bit - 1;
2561}
2562
Michael Chan6bb19472017-02-06 16:55:32 -05002563static int bnxt_set_rx_skb_mode(struct bnxt *bp)
2564{
Michael Chan745fc052017-02-06 16:55:34 -05002565 bp->rx_dir = DMA_FROM_DEVICE;
Michael Chan6bb19472017-02-06 16:55:32 -05002566 bp->rx_skb_func = bnxt_rx_skb;
2567 return 0;
2568}
2569
Michael Chanc0c050c2015-10-22 16:01:17 -04002570static void bnxt_free_vnic_attributes(struct bnxt *bp)
2571{
2572 int i;
2573 struct bnxt_vnic_info *vnic;
2574 struct pci_dev *pdev = bp->pdev;
2575
2576 if (!bp->vnic_info)
2577 return;
2578
2579 for (i = 0; i < bp->nr_vnics; i++) {
2580 vnic = &bp->vnic_info[i];
2581
2582 kfree(vnic->fw_grp_ids);
2583 vnic->fw_grp_ids = NULL;
2584
2585 kfree(vnic->uc_list);
2586 vnic->uc_list = NULL;
2587
2588 if (vnic->mc_list) {
2589 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2590 vnic->mc_list, vnic->mc_list_mapping);
2591 vnic->mc_list = NULL;
2592 }
2593
2594 if (vnic->rss_table) {
2595 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2596 vnic->rss_table,
2597 vnic->rss_table_dma_addr);
2598 vnic->rss_table = NULL;
2599 }
2600
2601 vnic->rss_hash_key = NULL;
2602 vnic->flags = 0;
2603 }
2604}
2605
2606static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2607{
2608 int i, rc = 0, size;
2609 struct bnxt_vnic_info *vnic;
2610 struct pci_dev *pdev = bp->pdev;
2611 int max_rings;
2612
2613 for (i = 0; i < bp->nr_vnics; i++) {
2614 vnic = &bp->vnic_info[i];
2615
2616 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2617 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2618
2619 if (mem_size > 0) {
2620 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2621 if (!vnic->uc_list) {
2622 rc = -ENOMEM;
2623 goto out;
2624 }
2625 }
2626 }
2627
2628 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2629 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2630 vnic->mc_list =
2631 dma_alloc_coherent(&pdev->dev,
2632 vnic->mc_list_size,
2633 &vnic->mc_list_mapping,
2634 GFP_KERNEL);
2635 if (!vnic->mc_list) {
2636 rc = -ENOMEM;
2637 goto out;
2638 }
2639 }
2640
2641 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2642 max_rings = bp->rx_nr_rings;
2643 else
2644 max_rings = 1;
2645
2646 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2647 if (!vnic->fw_grp_ids) {
2648 rc = -ENOMEM;
2649 goto out;
2650 }
2651
Michael Chanae10ae72016-12-29 12:13:38 -05002652 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2653 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2654 continue;
2655
Michael Chanc0c050c2015-10-22 16:01:17 -04002656 /* Allocate rss table and hash key */
2657 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2658 &vnic->rss_table_dma_addr,
2659 GFP_KERNEL);
2660 if (!vnic->rss_table) {
2661 rc = -ENOMEM;
2662 goto out;
2663 }
2664
2665 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2666
2667 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2668 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2669 }
2670 return 0;
2671
2672out:
2673 return rc;
2674}
2675
2676static void bnxt_free_hwrm_resources(struct bnxt *bp)
2677{
2678 struct pci_dev *pdev = bp->pdev;
2679
2680 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2681 bp->hwrm_cmd_resp_dma_addr);
2682
2683 bp->hwrm_cmd_resp_addr = NULL;
2684 if (bp->hwrm_dbg_resp_addr) {
2685 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2686 bp->hwrm_dbg_resp_addr,
2687 bp->hwrm_dbg_resp_dma_addr);
2688
2689 bp->hwrm_dbg_resp_addr = NULL;
2690 }
2691}
2692
2693static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2694{
2695 struct pci_dev *pdev = bp->pdev;
2696
2697 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2698 &bp->hwrm_cmd_resp_dma_addr,
2699 GFP_KERNEL);
2700 if (!bp->hwrm_cmd_resp_addr)
2701 return -ENOMEM;
2702 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2703 HWRM_DBG_REG_BUF_SIZE,
2704 &bp->hwrm_dbg_resp_dma_addr,
2705 GFP_KERNEL);
2706 if (!bp->hwrm_dbg_resp_addr)
2707 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2708
2709 return 0;
2710}
2711
2712static void bnxt_free_stats(struct bnxt *bp)
2713{
2714 u32 size, i;
2715 struct pci_dev *pdev = bp->pdev;
2716
Michael Chan3bdf56c2016-03-07 15:38:45 -05002717 if (bp->hw_rx_port_stats) {
2718 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2719 bp->hw_rx_port_stats,
2720 bp->hw_rx_port_stats_map);
2721 bp->hw_rx_port_stats = NULL;
2722 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2723 }
2724
Michael Chanc0c050c2015-10-22 16:01:17 -04002725 if (!bp->bnapi)
2726 return;
2727
2728 size = sizeof(struct ctx_hw_stats);
2729
2730 for (i = 0; i < bp->cp_nr_rings; i++) {
2731 struct bnxt_napi *bnapi = bp->bnapi[i];
2732 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2733
2734 if (cpr->hw_stats) {
2735 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2736 cpr->hw_stats_map);
2737 cpr->hw_stats = NULL;
2738 }
2739 }
2740}
2741
2742static int bnxt_alloc_stats(struct bnxt *bp)
2743{
2744 u32 size, i;
2745 struct pci_dev *pdev = bp->pdev;
2746
2747 size = sizeof(struct ctx_hw_stats);
2748
2749 for (i = 0; i < bp->cp_nr_rings; i++) {
2750 struct bnxt_napi *bnapi = bp->bnapi[i];
2751 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2752
2753 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2754 &cpr->hw_stats_map,
2755 GFP_KERNEL);
2756 if (!cpr->hw_stats)
2757 return -ENOMEM;
2758
2759 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2760 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002761
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002762 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002763 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2764 sizeof(struct tx_port_stats) + 1024;
2765
2766 bp->hw_rx_port_stats =
2767 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2768 &bp->hw_rx_port_stats_map,
2769 GFP_KERNEL);
2770 if (!bp->hw_rx_port_stats)
2771 return -ENOMEM;
2772
2773 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2774 512;
2775 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2776 sizeof(struct rx_port_stats) + 512;
2777 bp->flags |= BNXT_FLAG_PORT_STATS;
2778 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002779 return 0;
2780}
2781
2782static void bnxt_clear_ring_indices(struct bnxt *bp)
2783{
2784 int i;
2785
2786 if (!bp->bnapi)
2787 return;
2788
2789 for (i = 0; i < bp->cp_nr_rings; i++) {
2790 struct bnxt_napi *bnapi = bp->bnapi[i];
2791 struct bnxt_cp_ring_info *cpr;
2792 struct bnxt_rx_ring_info *rxr;
2793 struct bnxt_tx_ring_info *txr;
2794
2795 if (!bnapi)
2796 continue;
2797
2798 cpr = &bnapi->cp_ring;
2799 cpr->cp_raw_cons = 0;
2800
Michael Chanb6ab4b02016-01-02 23:44:59 -05002801 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002802 if (txr) {
2803 txr->tx_prod = 0;
2804 txr->tx_cons = 0;
2805 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002806
Michael Chanb6ab4b02016-01-02 23:44:59 -05002807 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002808 if (rxr) {
2809 rxr->rx_prod = 0;
2810 rxr->rx_agg_prod = 0;
2811 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002812 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002813 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002814 }
2815}
2816
2817static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2818{
2819#ifdef CONFIG_RFS_ACCEL
2820 int i;
2821
2822 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2823 * safe to delete the hash table.
2824 */
2825 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2826 struct hlist_head *head;
2827 struct hlist_node *tmp;
2828 struct bnxt_ntuple_filter *fltr;
2829
2830 head = &bp->ntp_fltr_hash_tbl[i];
2831 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2832 hlist_del(&fltr->hash);
2833 kfree(fltr);
2834 }
2835 }
2836 if (irq_reinit) {
2837 kfree(bp->ntp_fltr_bmap);
2838 bp->ntp_fltr_bmap = NULL;
2839 }
2840 bp->ntp_fltr_count = 0;
2841#endif
2842}
2843
2844static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2845{
2846#ifdef CONFIG_RFS_ACCEL
2847 int i, rc = 0;
2848
2849 if (!(bp->flags & BNXT_FLAG_RFS))
2850 return 0;
2851
2852 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2853 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2854
2855 bp->ntp_fltr_count = 0;
2856 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2857 GFP_KERNEL);
2858
2859 if (!bp->ntp_fltr_bmap)
2860 rc = -ENOMEM;
2861
2862 return rc;
2863#else
2864 return 0;
2865#endif
2866}
2867
2868static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2869{
2870 bnxt_free_vnic_attributes(bp);
2871 bnxt_free_tx_rings(bp);
2872 bnxt_free_rx_rings(bp);
2873 bnxt_free_cp_rings(bp);
2874 bnxt_free_ntp_fltrs(bp, irq_re_init);
2875 if (irq_re_init) {
2876 bnxt_free_stats(bp);
2877 bnxt_free_ring_grps(bp);
2878 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002879 kfree(bp->tx_ring);
2880 bp->tx_ring = NULL;
2881 kfree(bp->rx_ring);
2882 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002883 kfree(bp->bnapi);
2884 bp->bnapi = NULL;
2885 } else {
2886 bnxt_clear_ring_indices(bp);
2887 }
2888}
2889
2890static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2891{
Michael Chan01657bc2016-01-02 23:45:03 -05002892 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002893 void *bnapi;
2894
2895 if (irq_re_init) {
2896 /* Allocate bnapi mem pointer array and mem block for
2897 * all queues
2898 */
2899 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2900 bp->cp_nr_rings);
2901 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2902 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2903 if (!bnapi)
2904 return -ENOMEM;
2905
2906 bp->bnapi = bnapi;
2907 bnapi += arr_size;
2908 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2909 bp->bnapi[i] = bnapi;
2910 bp->bnapi[i]->index = i;
2911 bp->bnapi[i]->bp = bp;
2912 }
2913
Michael Chanb6ab4b02016-01-02 23:44:59 -05002914 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2915 sizeof(struct bnxt_rx_ring_info),
2916 GFP_KERNEL);
2917 if (!bp->rx_ring)
2918 return -ENOMEM;
2919
2920 for (i = 0; i < bp->rx_nr_rings; i++) {
2921 bp->rx_ring[i].bnapi = bp->bnapi[i];
2922 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2923 }
2924
2925 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2926 sizeof(struct bnxt_tx_ring_info),
2927 GFP_KERNEL);
2928 if (!bp->tx_ring)
2929 return -ENOMEM;
2930
Michael Chan01657bc2016-01-02 23:45:03 -05002931 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2932 j = 0;
2933 else
2934 j = bp->rx_nr_rings;
2935
2936 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2937 bp->tx_ring[i].bnapi = bp->bnapi[j];
2938 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002939 }
2940
Michael Chanc0c050c2015-10-22 16:01:17 -04002941 rc = bnxt_alloc_stats(bp);
2942 if (rc)
2943 goto alloc_mem_err;
2944
2945 rc = bnxt_alloc_ntp_fltrs(bp);
2946 if (rc)
2947 goto alloc_mem_err;
2948
2949 rc = bnxt_alloc_vnics(bp);
2950 if (rc)
2951 goto alloc_mem_err;
2952 }
2953
2954 bnxt_init_ring_struct(bp);
2955
2956 rc = bnxt_alloc_rx_rings(bp);
2957 if (rc)
2958 goto alloc_mem_err;
2959
2960 rc = bnxt_alloc_tx_rings(bp);
2961 if (rc)
2962 goto alloc_mem_err;
2963
2964 rc = bnxt_alloc_cp_rings(bp);
2965 if (rc)
2966 goto alloc_mem_err;
2967
2968 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2969 BNXT_VNIC_UCAST_FLAG;
2970 rc = bnxt_alloc_vnic_attributes(bp);
2971 if (rc)
2972 goto alloc_mem_err;
2973 return 0;
2974
2975alloc_mem_err:
2976 bnxt_free_mem(bp, true);
2977 return rc;
2978}
2979
Michael Chan9d8bc092016-12-29 12:13:33 -05002980static void bnxt_disable_int(struct bnxt *bp)
2981{
2982 int i;
2983
2984 if (!bp->bnapi)
2985 return;
2986
2987 for (i = 0; i < bp->cp_nr_rings; i++) {
2988 struct bnxt_napi *bnapi = bp->bnapi[i];
2989 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2990
2991 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2992 }
2993}
2994
2995static void bnxt_disable_int_sync(struct bnxt *bp)
2996{
2997 int i;
2998
2999 atomic_inc(&bp->intr_sem);
3000
3001 bnxt_disable_int(bp);
3002 for (i = 0; i < bp->cp_nr_rings; i++)
3003 synchronize_irq(bp->irq_tbl[i].vector);
3004}
3005
3006static void bnxt_enable_int(struct bnxt *bp)
3007{
3008 int i;
3009
3010 atomic_set(&bp->intr_sem, 0);
3011 for (i = 0; i < bp->cp_nr_rings; i++) {
3012 struct bnxt_napi *bnapi = bp->bnapi[i];
3013 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3014
3015 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3016 }
3017}
3018
Michael Chanc0c050c2015-10-22 16:01:17 -04003019void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3020 u16 cmpl_ring, u16 target_id)
3021{
Michael Chana8643e12016-02-26 04:00:05 -05003022 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003023
Michael Chana8643e12016-02-26 04:00:05 -05003024 req->req_type = cpu_to_le16(req_type);
3025 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3026 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003027 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3028}
3029
Michael Chanfbfbc482016-02-26 04:00:07 -05003030static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3031 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003032{
Michael Chana11fa2b2016-05-15 03:04:47 -04003033 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003034 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003035 u32 *data = msg;
3036 __le32 *resp_len, *valid;
3037 u16 cp_ring_id, len = 0;
3038 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3039
Michael Chana8643e12016-02-26 04:00:05 -05003040 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003041 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003042 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003043 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3044
3045 /* Write request msg to hwrm channel */
3046 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3047
Michael Chane6ef2692016-03-28 19:46:05 -04003048 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003049 writel(0, bp->bar0 + i);
3050
Michael Chanc0c050c2015-10-22 16:01:17 -04003051 /* currently supports only one outstanding message */
3052 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003053 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003054
3055 /* Ring channel doorbell */
3056 writel(1, bp->bar0 + 0x100);
3057
Michael Chanff4fe812016-02-26 04:00:04 -05003058 if (!timeout)
3059 timeout = DFLT_HWRM_CMD_TIMEOUT;
3060
Michael Chanc0c050c2015-10-22 16:01:17 -04003061 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003062 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003063 if (intr_process) {
3064 /* Wait until hwrm response cmpl interrupt is processed */
3065 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003066 i++ < tmo_count) {
3067 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003068 }
3069
3070 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3071 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003072 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003073 return -1;
3074 }
3075 } else {
3076 /* Check if response len is updated */
3077 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003078 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003079 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3080 HWRM_RESP_LEN_SFT;
3081 if (len)
3082 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003083 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003084 }
3085
Michael Chana11fa2b2016-05-15 03:04:47 -04003086 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003087 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003088 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003089 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003090 return -1;
3091 }
3092
3093 /* Last word of resp contains valid bit */
3094 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003095 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003096 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3097 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003098 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003099 }
3100
Michael Chana11fa2b2016-05-15 03:04:47 -04003101 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003102 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003103 timeout, le16_to_cpu(req->req_type),
3104 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003105 return -1;
3106 }
3107 }
3108
3109 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003110 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003111 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3112 le16_to_cpu(resp->req_type),
3113 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003114 return rc;
3115}
3116
3117int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3118{
3119 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003120}
3121
3122int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3123{
3124 int rc;
3125
3126 mutex_lock(&bp->hwrm_cmd_lock);
3127 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3128 mutex_unlock(&bp->hwrm_cmd_lock);
3129 return rc;
3130}
3131
Michael Chan90e209212016-02-26 04:00:08 -05003132int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3133 int timeout)
3134{
3135 int rc;
3136
3137 mutex_lock(&bp->hwrm_cmd_lock);
3138 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3139 mutex_unlock(&bp->hwrm_cmd_lock);
3140 return rc;
3141}
3142
Michael Chana1653b12016-12-07 00:26:20 -05003143int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3144 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003145{
3146 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003147 DECLARE_BITMAP(async_events_bmap, 256);
3148 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003149 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003150
3151 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3152
3153 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003154 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003155
Michael Chan25be8622016-04-05 14:09:00 -04003156 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3157 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3158 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3159
Michael Chana1653b12016-12-07 00:26:20 -05003160 if (bmap && bmap_size) {
3161 for (i = 0; i < bmap_size; i++) {
3162 if (test_bit(i, bmap))
3163 __set_bit(i, async_events_bmap);
3164 }
3165 }
3166
Michael Chan25be8622016-04-05 14:09:00 -04003167 for (i = 0; i < 8; i++)
3168 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3169
Michael Chana1653b12016-12-07 00:26:20 -05003170 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3171}
3172
3173static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3174{
3175 struct hwrm_func_drv_rgtr_input req = {0};
3176
3177 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3178
3179 req.enables =
3180 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3181 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3182
Michael Chan11f15ed2016-04-05 14:08:55 -04003183 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003184 req.ver_maj = DRV_VER_MAJ;
3185 req.ver_min = DRV_VER_MIN;
3186 req.ver_upd = DRV_VER_UPD;
3187
3188 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003189 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003190 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003191 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003192
Michael Chande68f5de2015-12-09 19:35:41 -05003193 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003194 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3195 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3196
Michael Chande68f5de2015-12-09 19:35:41 -05003197 for (i = 0; i < 8; i++)
3198 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3199
Michael Chanc0c050c2015-10-22 16:01:17 -04003200 req.enables |=
3201 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3202 }
3203
3204 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3205}
3206
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003207static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3208{
3209 struct hwrm_func_drv_unrgtr_input req = {0};
3210
3211 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3212 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3213}
3214
Michael Chanc0c050c2015-10-22 16:01:17 -04003215static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3216{
3217 u32 rc = 0;
3218 struct hwrm_tunnel_dst_port_free_input req = {0};
3219
3220 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3221 req.tunnel_type = tunnel_type;
3222
3223 switch (tunnel_type) {
3224 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3225 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3226 break;
3227 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3228 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3229 break;
3230 default:
3231 break;
3232 }
3233
3234 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3235 if (rc)
3236 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3237 rc);
3238 return rc;
3239}
3240
3241static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3242 u8 tunnel_type)
3243{
3244 u32 rc = 0;
3245 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3246 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3247
3248 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3249
3250 req.tunnel_type = tunnel_type;
3251 req.tunnel_dst_port_val = port;
3252
3253 mutex_lock(&bp->hwrm_cmd_lock);
3254 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3255 if (rc) {
3256 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3257 rc);
3258 goto err_out;
3259 }
3260
Christophe Jaillet57aac712016-11-22 06:14:40 +01003261 switch (tunnel_type) {
3262 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003263 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003264 break;
3265 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003266 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003267 break;
3268 default:
3269 break;
3270 }
3271
Michael Chanc0c050c2015-10-22 16:01:17 -04003272err_out:
3273 mutex_unlock(&bp->hwrm_cmd_lock);
3274 return rc;
3275}
3276
3277static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3278{
3279 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3280 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3281
3282 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003283 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003284
3285 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3286 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3287 req.mask = cpu_to_le32(vnic->rx_mask);
3288 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3289}
3290
3291#ifdef CONFIG_RFS_ACCEL
3292static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3293 struct bnxt_ntuple_filter *fltr)
3294{
3295 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3296
3297 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3298 req.ntuple_filter_id = fltr->filter_id;
3299 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3300}
3301
3302#define BNXT_NTP_FLTR_FLAGS \
3303 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3304 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3305 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3306 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3307 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3308 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3309 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3310 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3311 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3312 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3313 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3314 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3315 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003316 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003317
3318static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3319 struct bnxt_ntuple_filter *fltr)
3320{
3321 int rc = 0;
3322 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3323 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3324 bp->hwrm_cmd_resp_addr;
3325 struct flow_keys *keys = &fltr->fkeys;
3326 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3327
3328 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003329 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003330
3331 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3332
3333 req.ethertype = htons(ETH_P_IP);
3334 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003335 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003336 req.ip_protocol = keys->basic.ip_proto;
3337
Michael Chandda0e742016-12-29 12:13:40 -05003338 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3339 int i;
3340
3341 req.ethertype = htons(ETH_P_IPV6);
3342 req.ip_addr_type =
3343 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3344 *(struct in6_addr *)&req.src_ipaddr[0] =
3345 keys->addrs.v6addrs.src;
3346 *(struct in6_addr *)&req.dst_ipaddr[0] =
3347 keys->addrs.v6addrs.dst;
3348 for (i = 0; i < 4; i++) {
3349 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3350 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3351 }
3352 } else {
3353 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3354 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3355 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3356 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3357 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003358
3359 req.src_port = keys->ports.src;
3360 req.src_port_mask = cpu_to_be16(0xffff);
3361 req.dst_port = keys->ports.dst;
3362 req.dst_port_mask = cpu_to_be16(0xffff);
3363
Michael Chanc1935542015-12-27 18:19:28 -05003364 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003365 mutex_lock(&bp->hwrm_cmd_lock);
3366 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3367 if (!rc)
3368 fltr->filter_id = resp->ntuple_filter_id;
3369 mutex_unlock(&bp->hwrm_cmd_lock);
3370 return rc;
3371}
3372#endif
3373
3374static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3375 u8 *mac_addr)
3376{
3377 u32 rc = 0;
3378 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3379 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3380
3381 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003382 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3383 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3384 req.flags |=
3385 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003386 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003387 req.enables =
3388 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003389 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003390 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3391 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3392 req.l2_addr_mask[0] = 0xff;
3393 req.l2_addr_mask[1] = 0xff;
3394 req.l2_addr_mask[2] = 0xff;
3395 req.l2_addr_mask[3] = 0xff;
3396 req.l2_addr_mask[4] = 0xff;
3397 req.l2_addr_mask[5] = 0xff;
3398
3399 mutex_lock(&bp->hwrm_cmd_lock);
3400 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3401 if (!rc)
3402 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3403 resp->l2_filter_id;
3404 mutex_unlock(&bp->hwrm_cmd_lock);
3405 return rc;
3406}
3407
3408static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3409{
3410 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3411 int rc = 0;
3412
3413 /* Any associated ntuple filters will also be cleared by firmware. */
3414 mutex_lock(&bp->hwrm_cmd_lock);
3415 for (i = 0; i < num_of_vnics; i++) {
3416 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3417
3418 for (j = 0; j < vnic->uc_filter_count; j++) {
3419 struct hwrm_cfa_l2_filter_free_input req = {0};
3420
3421 bnxt_hwrm_cmd_hdr_init(bp, &req,
3422 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3423
3424 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3425
3426 rc = _hwrm_send_message(bp, &req, sizeof(req),
3427 HWRM_CMD_TIMEOUT);
3428 }
3429 vnic->uc_filter_count = 0;
3430 }
3431 mutex_unlock(&bp->hwrm_cmd_lock);
3432
3433 return rc;
3434}
3435
3436static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3437{
3438 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3439 struct hwrm_vnic_tpa_cfg_input req = {0};
3440
3441 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3442
3443 if (tpa_flags) {
3444 u16 mss = bp->dev->mtu - 40;
3445 u32 nsegs, n, segs = 0, flags;
3446
3447 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3448 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3449 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3450 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3451 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3452 if (tpa_flags & BNXT_FLAG_GRO)
3453 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3454
3455 req.flags = cpu_to_le32(flags);
3456
3457 req.enables =
3458 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003459 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3460 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003461
3462 /* Number of segs are log2 units, and first packet is not
3463 * included as part of this units.
3464 */
Michael Chan2839f282016-04-25 02:30:50 -04003465 if (mss <= BNXT_RX_PAGE_SIZE) {
3466 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003467 nsegs = (MAX_SKB_FRAGS - 1) * n;
3468 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003469 n = mss / BNXT_RX_PAGE_SIZE;
3470 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003471 n++;
3472 nsegs = (MAX_SKB_FRAGS - n) / n;
3473 }
3474
3475 segs = ilog2(nsegs);
3476 req.max_agg_segs = cpu_to_le16(segs);
3477 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003478
3479 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003480 }
3481 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3482
3483 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3484}
3485
3486static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3487{
3488 u32 i, j, max_rings;
3489 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3490 struct hwrm_vnic_rss_cfg_input req = {0};
3491
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003492 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003493 return 0;
3494
3495 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3496 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003497 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003498 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3499 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3500 max_rings = bp->rx_nr_rings - 1;
3501 else
3502 max_rings = bp->rx_nr_rings;
3503 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003504 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003505 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003506
3507 /* Fill the RSS indirection table with ring group ids */
3508 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3509 if (j == max_rings)
3510 j = 0;
3511 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3512 }
3513
3514 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3515 req.hash_key_tbl_addr =
3516 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3517 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003518 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003519 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3520}
3521
3522static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3523{
3524 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3525 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3526
3527 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3528 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3529 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3530 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3531 req.enables =
3532 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3533 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3534 /* thresholds not implemented in firmware yet */
3535 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3536 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3537 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3538 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3539}
3540
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003541static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3542 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003543{
3544 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3545
3546 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3547 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003548 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003549
3550 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003551 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003552}
3553
3554static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3555{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003556 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003557
3558 for (i = 0; i < bp->nr_vnics; i++) {
3559 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3560
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003561 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3562 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3563 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3564 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003565 }
3566 bp->rsscos_nr_ctxs = 0;
3567}
3568
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003569static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003570{
3571 int rc;
3572 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3573 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3574 bp->hwrm_cmd_resp_addr;
3575
3576 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3577 -1);
3578
3579 mutex_lock(&bp->hwrm_cmd_lock);
3580 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3581 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003582 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003583 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3584 mutex_unlock(&bp->hwrm_cmd_lock);
3585
3586 return rc;
3587}
3588
Michael Chana588e452016-12-07 00:26:21 -05003589int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003590{
Michael Chanb81a90d2016-01-02 23:45:01 -05003591 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003592 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3593 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003594 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003595
3596 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003597
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003598 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3599 /* Only RSS support for now TBD: COS & LB */
3600 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3601 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3602 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3603 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003604 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3605 req.rss_rule =
3606 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3607 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3608 VNIC_CFG_REQ_ENABLES_MRU);
3609 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003610 } else {
3611 req.rss_rule = cpu_to_le16(0xffff);
3612 }
3613
3614 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3615 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003616 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3617 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3618 } else {
3619 req.cos_rule = cpu_to_le16(0xffff);
3620 }
3621
Michael Chanc0c050c2015-10-22 16:01:17 -04003622 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003623 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003624 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003625 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003626 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3627 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003628
Michael Chanb81a90d2016-01-02 23:45:01 -05003629 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003630 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3631 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3632
3633 req.lb_rule = cpu_to_le16(0xffff);
3634 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3635 VLAN_HLEN);
3636
Michael Chancf6645f2016-06-13 02:25:28 -04003637#ifdef CONFIG_BNXT_SRIOV
3638 if (BNXT_VF(bp))
3639 def_vlan = bp->vf.vlan;
3640#endif
3641 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003642 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003643 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3644 req.flags |=
3645 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003646
3647 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3648}
3649
3650static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3651{
3652 u32 rc = 0;
3653
3654 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3655 struct hwrm_vnic_free_input req = {0};
3656
3657 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3658 req.vnic_id =
3659 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3660
3661 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3662 if (rc)
3663 return rc;
3664 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3665 }
3666 return rc;
3667}
3668
3669static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3670{
3671 u16 i;
3672
3673 for (i = 0; i < bp->nr_vnics; i++)
3674 bnxt_hwrm_vnic_free_one(bp, i);
3675}
3676
Michael Chanb81a90d2016-01-02 23:45:01 -05003677static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3678 unsigned int start_rx_ring_idx,
3679 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003680{
Michael Chanb81a90d2016-01-02 23:45:01 -05003681 int rc = 0;
3682 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003683 struct hwrm_vnic_alloc_input req = {0};
3684 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3685
3686 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003687 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3688 grp_idx = bp->rx_ring[i].bnapi->index;
3689 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003690 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003691 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003692 break;
3693 }
3694 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003695 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003696 }
3697
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003698 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3699 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003700 if (vnic_id == 0)
3701 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3702
3703 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3704
3705 mutex_lock(&bp->hwrm_cmd_lock);
3706 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3707 if (!rc)
3708 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3709 mutex_unlock(&bp->hwrm_cmd_lock);
3710 return rc;
3711}
3712
Michael Chan8fdefd62016-12-29 12:13:36 -05003713static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3714{
3715 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3716 struct hwrm_vnic_qcaps_input req = {0};
3717 int rc;
3718
3719 if (bp->hwrm_spec_code < 0x10600)
3720 return 0;
3721
3722 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3723 mutex_lock(&bp->hwrm_cmd_lock);
3724 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3725 if (!rc) {
3726 if (resp->flags &
3727 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3728 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3729 }
3730 mutex_unlock(&bp->hwrm_cmd_lock);
3731 return rc;
3732}
3733
Michael Chanc0c050c2015-10-22 16:01:17 -04003734static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3735{
3736 u16 i;
3737 u32 rc = 0;
3738
3739 mutex_lock(&bp->hwrm_cmd_lock);
3740 for (i = 0; i < bp->rx_nr_rings; i++) {
3741 struct hwrm_ring_grp_alloc_input req = {0};
3742 struct hwrm_ring_grp_alloc_output *resp =
3743 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003744 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003745
3746 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3747
Michael Chanb81a90d2016-01-02 23:45:01 -05003748 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3749 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3750 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3751 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003752
3753 rc = _hwrm_send_message(bp, &req, sizeof(req),
3754 HWRM_CMD_TIMEOUT);
3755 if (rc)
3756 break;
3757
Michael Chanb81a90d2016-01-02 23:45:01 -05003758 bp->grp_info[grp_idx].fw_grp_id =
3759 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003760 }
3761 mutex_unlock(&bp->hwrm_cmd_lock);
3762 return rc;
3763}
3764
3765static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3766{
3767 u16 i;
3768 u32 rc = 0;
3769 struct hwrm_ring_grp_free_input req = {0};
3770
3771 if (!bp->grp_info)
3772 return 0;
3773
3774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3775
3776 mutex_lock(&bp->hwrm_cmd_lock);
3777 for (i = 0; i < bp->cp_nr_rings; i++) {
3778 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3779 continue;
3780 req.ring_group_id =
3781 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3782
3783 rc = _hwrm_send_message(bp, &req, sizeof(req),
3784 HWRM_CMD_TIMEOUT);
3785 if (rc)
3786 break;
3787 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3788 }
3789 mutex_unlock(&bp->hwrm_cmd_lock);
3790 return rc;
3791}
3792
3793static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3794 struct bnxt_ring_struct *ring,
3795 u32 ring_type, u32 map_index,
3796 u32 stats_ctx_id)
3797{
3798 int rc = 0, err = 0;
3799 struct hwrm_ring_alloc_input req = {0};
3800 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3801 u16 ring_id;
3802
3803 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3804
3805 req.enables = 0;
3806 if (ring->nr_pages > 1) {
3807 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3808 /* Page size is in log2 units */
3809 req.page_size = BNXT_PAGE_SHIFT;
3810 req.page_tbl_depth = 1;
3811 } else {
3812 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3813 }
3814 req.fbo = 0;
3815 /* Association of ring index with doorbell index and MSIX number */
3816 req.logical_id = cpu_to_le16(map_index);
3817
3818 switch (ring_type) {
3819 case HWRM_RING_ALLOC_TX:
3820 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3821 /* Association of transmit ring with completion ring */
3822 req.cmpl_ring_id =
3823 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3824 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3825 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3826 req.queue_id = cpu_to_le16(ring->queue_id);
3827 break;
3828 case HWRM_RING_ALLOC_RX:
3829 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3830 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3831 break;
3832 case HWRM_RING_ALLOC_AGG:
3833 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3834 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3835 break;
3836 case HWRM_RING_ALLOC_CMPL:
3837 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3838 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3839 if (bp->flags & BNXT_FLAG_USING_MSIX)
3840 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3841 break;
3842 default:
3843 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3844 ring_type);
3845 return -1;
3846 }
3847
3848 mutex_lock(&bp->hwrm_cmd_lock);
3849 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3850 err = le16_to_cpu(resp->error_code);
3851 ring_id = le16_to_cpu(resp->ring_id);
3852 mutex_unlock(&bp->hwrm_cmd_lock);
3853
3854 if (rc || err) {
3855 switch (ring_type) {
3856 case RING_FREE_REQ_RING_TYPE_CMPL:
3857 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3858 rc, err);
3859 return -1;
3860
3861 case RING_FREE_REQ_RING_TYPE_RX:
3862 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3863 rc, err);
3864 return -1;
3865
3866 case RING_FREE_REQ_RING_TYPE_TX:
3867 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3868 rc, err);
3869 return -1;
3870
3871 default:
3872 netdev_err(bp->dev, "Invalid ring\n");
3873 return -1;
3874 }
3875 }
3876 ring->fw_ring_id = ring_id;
3877 return rc;
3878}
3879
Michael Chan486b5c22016-12-29 12:13:42 -05003880static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
3881{
3882 int rc;
3883
3884 if (BNXT_PF(bp)) {
3885 struct hwrm_func_cfg_input req = {0};
3886
3887 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
3888 req.fid = cpu_to_le16(0xffff);
3889 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3890 req.async_event_cr = cpu_to_le16(idx);
3891 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3892 } else {
3893 struct hwrm_func_vf_cfg_input req = {0};
3894
3895 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
3896 req.enables =
3897 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3898 req.async_event_cr = cpu_to_le16(idx);
3899 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3900 }
3901 return rc;
3902}
3903
Michael Chanc0c050c2015-10-22 16:01:17 -04003904static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3905{
3906 int i, rc = 0;
3907
Michael Chanedd0c2c2015-12-27 18:19:19 -05003908 for (i = 0; i < bp->cp_nr_rings; i++) {
3909 struct bnxt_napi *bnapi = bp->bnapi[i];
3910 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3911 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003912
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003913 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003914 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3915 INVALID_STATS_CTX_ID);
3916 if (rc)
3917 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003918 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3919 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05003920
3921 if (!i) {
3922 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
3923 if (rc)
3924 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
3925 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003926 }
3927
Michael Chanedd0c2c2015-12-27 18:19:19 -05003928 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003929 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003930 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003931 u32 map_idx = txr->bnapi->index;
3932 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003933
Michael Chanb81a90d2016-01-02 23:45:01 -05003934 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3935 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003936 if (rc)
3937 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003938 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003939 }
3940
Michael Chanedd0c2c2015-12-27 18:19:19 -05003941 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003942 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003943 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003944 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003945
Michael Chanb81a90d2016-01-02 23:45:01 -05003946 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3947 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003948 if (rc)
3949 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003950 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003951 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003952 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003953 }
3954
3955 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3956 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003957 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003958 struct bnxt_ring_struct *ring =
3959 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003960 u32 grp_idx = rxr->bnapi->index;
3961 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003962
3963 rc = hwrm_ring_alloc_send_msg(bp, ring,
3964 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003965 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003966 INVALID_STATS_CTX_ID);
3967 if (rc)
3968 goto err_out;
3969
Michael Chanb81a90d2016-01-02 23:45:01 -05003970 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003971 writel(DB_KEY_RX | rxr->rx_agg_prod,
3972 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003973 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003974 }
3975 }
3976err_out:
3977 return rc;
3978}
3979
3980static int hwrm_ring_free_send_msg(struct bnxt *bp,
3981 struct bnxt_ring_struct *ring,
3982 u32 ring_type, int cmpl_ring_id)
3983{
3984 int rc;
3985 struct hwrm_ring_free_input req = {0};
3986 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3987 u16 error_code;
3988
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003989 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003990 req.ring_type = ring_type;
3991 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3992
3993 mutex_lock(&bp->hwrm_cmd_lock);
3994 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3995 error_code = le16_to_cpu(resp->error_code);
3996 mutex_unlock(&bp->hwrm_cmd_lock);
3997
3998 if (rc || error_code) {
3999 switch (ring_type) {
4000 case RING_FREE_REQ_RING_TYPE_CMPL:
4001 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4002 rc);
4003 return rc;
4004 case RING_FREE_REQ_RING_TYPE_RX:
4005 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4006 rc);
4007 return rc;
4008 case RING_FREE_REQ_RING_TYPE_TX:
4009 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4010 rc);
4011 return rc;
4012 default:
4013 netdev_err(bp->dev, "Invalid ring\n");
4014 return -1;
4015 }
4016 }
4017 return 0;
4018}
4019
Michael Chanedd0c2c2015-12-27 18:19:19 -05004020static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004021{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004022 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004023
4024 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004025 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004026
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 grp_idx = txr->bnapi->index;
4031 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004032
Michael Chanedd0c2c2015-12-27 18:19:19 -05004033 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4034 hwrm_ring_free_send_msg(bp, ring,
4035 RING_FREE_REQ_RING_TYPE_TX,
4036 close_path ? cmpl_ring_id :
4037 INVALID_HW_RING_ID);
4038 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004039 }
4040 }
4041
Michael Chanedd0c2c2015-12-27 18:19:19 -05004042 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004043 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004044 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004045 u32 grp_idx = rxr->bnapi->index;
4046 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004047
Michael Chanedd0c2c2015-12-27 18:19:19 -05004048 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4049 hwrm_ring_free_send_msg(bp, ring,
4050 RING_FREE_REQ_RING_TYPE_RX,
4051 close_path ? cmpl_ring_id :
4052 INVALID_HW_RING_ID);
4053 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004054 bp->grp_info[grp_idx].rx_fw_ring_id =
4055 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004056 }
4057 }
4058
Michael Chanedd0c2c2015-12-27 18:19:19 -05004059 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004060 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004061 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004062 u32 grp_idx = rxr->bnapi->index;
4063 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004064
Michael Chanedd0c2c2015-12-27 18:19:19 -05004065 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4066 hwrm_ring_free_send_msg(bp, ring,
4067 RING_FREE_REQ_RING_TYPE_RX,
4068 close_path ? cmpl_ring_id :
4069 INVALID_HW_RING_ID);
4070 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004071 bp->grp_info[grp_idx].agg_fw_ring_id =
4072 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004073 }
4074 }
4075
Michael Chan9d8bc092016-12-29 12:13:33 -05004076 /* The completion rings are about to be freed. After that the
4077 * IRQ doorbell will not work anymore. So we need to disable
4078 * IRQ here.
4079 */
4080 bnxt_disable_int_sync(bp);
4081
Michael Chanedd0c2c2015-12-27 18:19:19 -05004082 for (i = 0; i < bp->cp_nr_rings; i++) {
4083 struct bnxt_napi *bnapi = bp->bnapi[i];
4084 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4085 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004086
Michael Chanedd0c2c2015-12-27 18:19:19 -05004087 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4088 hwrm_ring_free_send_msg(bp, ring,
4089 RING_FREE_REQ_RING_TYPE_CMPL,
4090 INVALID_HW_RING_ID);
4091 ring->fw_ring_id = INVALID_HW_RING_ID;
4092 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004093 }
4094 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004095}
4096
Michael Chan391be5c2016-12-29 12:13:41 -05004097/* Caller must hold bp->hwrm_cmd_lock */
4098int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4099{
4100 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4101 struct hwrm_func_qcfg_input req = {0};
4102 int rc;
4103
4104 if (bp->hwrm_spec_code < 0x10601)
4105 return 0;
4106
4107 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4108 req.fid = cpu_to_le16(fid);
4109 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4110 if (!rc)
4111 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4112
4113 return rc;
4114}
4115
4116int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
4117{
4118 struct hwrm_func_cfg_input req = {0};
4119 int rc;
4120
4121 if (bp->hwrm_spec_code < 0x10601)
4122 return 0;
4123
4124 if (BNXT_VF(bp))
4125 return 0;
4126
4127 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4128 req.fid = cpu_to_le16(0xffff);
4129 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4130 req.num_tx_rings = cpu_to_le16(*tx_rings);
4131 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4132 if (rc)
4133 return rc;
4134
4135 mutex_lock(&bp->hwrm_cmd_lock);
4136 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4137 mutex_unlock(&bp->hwrm_cmd_lock);
4138 return rc;
4139}
4140
Michael Chanbb053f52016-02-26 04:00:02 -05004141static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4142 u32 buf_tmrs, u16 flags,
4143 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4144{
4145 req->flags = cpu_to_le16(flags);
4146 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4147 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4148 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4149 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4150 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4151 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4152 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4153 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4154}
4155
Michael Chanc0c050c2015-10-22 16:01:17 -04004156int bnxt_hwrm_set_coal(struct bnxt *bp)
4157{
4158 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004159 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4160 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004161 u16 max_buf, max_buf_irq;
4162 u16 buf_tmr, buf_tmr_irq;
4163 u32 flags;
4164
Michael Chandfc9c942016-02-26 04:00:03 -05004165 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4166 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4167 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4168 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004169
Michael Chandfb5b892016-02-26 04:00:01 -05004170 /* Each rx completion (2 records) should be DMAed immediately.
4171 * DMA 1/4 of the completion buffers at a time.
4172 */
4173 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004174 /* max_buf must not be zero */
4175 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004176 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4177 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4178 /* buf timer set to 1/4 of interrupt timer */
4179 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4180 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4181 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004182
4183 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4184
4185 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4186 * if coal_ticks is less than 25 us.
4187 */
Michael Chandfb5b892016-02-26 04:00:01 -05004188 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004189 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4190
Michael Chanbb053f52016-02-26 04:00:02 -05004191 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004192 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4193
4194 /* max_buf must not be zero */
4195 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4196 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4197 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4198 /* buf timer set to 1/4 of interrupt timer */
4199 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4200 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4201 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4202
4203 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4204 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4205 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004206
4207 mutex_lock(&bp->hwrm_cmd_lock);
4208 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004209 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004210
Michael Chandfc9c942016-02-26 04:00:03 -05004211 req = &req_rx;
4212 if (!bnapi->rx_ring)
4213 req = &req_tx;
4214 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4215
4216 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004217 HWRM_CMD_TIMEOUT);
4218 if (rc)
4219 break;
4220 }
4221 mutex_unlock(&bp->hwrm_cmd_lock);
4222 return rc;
4223}
4224
4225static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4226{
4227 int rc = 0, i;
4228 struct hwrm_stat_ctx_free_input req = {0};
4229
4230 if (!bp->bnapi)
4231 return 0;
4232
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004233 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4234 return 0;
4235
Michael Chanc0c050c2015-10-22 16:01:17 -04004236 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4237
4238 mutex_lock(&bp->hwrm_cmd_lock);
4239 for (i = 0; i < bp->cp_nr_rings; i++) {
4240 struct bnxt_napi *bnapi = bp->bnapi[i];
4241 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4242
4243 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4244 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4245
4246 rc = _hwrm_send_message(bp, &req, sizeof(req),
4247 HWRM_CMD_TIMEOUT);
4248 if (rc)
4249 break;
4250
4251 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4252 }
4253 }
4254 mutex_unlock(&bp->hwrm_cmd_lock);
4255 return rc;
4256}
4257
4258static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4259{
4260 int rc = 0, i;
4261 struct hwrm_stat_ctx_alloc_input req = {0};
4262 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4263
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004264 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4265 return 0;
4266
Michael Chanc0c050c2015-10-22 16:01:17 -04004267 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4268
Michael Chan51f30782016-07-01 18:46:29 -04004269 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004270
4271 mutex_lock(&bp->hwrm_cmd_lock);
4272 for (i = 0; i < bp->cp_nr_rings; i++) {
4273 struct bnxt_napi *bnapi = bp->bnapi[i];
4274 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4275
4276 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4277
4278 rc = _hwrm_send_message(bp, &req, sizeof(req),
4279 HWRM_CMD_TIMEOUT);
4280 if (rc)
4281 break;
4282
4283 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4284
4285 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4286 }
4287 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004288 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004289}
4290
Michael Chancf6645f2016-06-13 02:25:28 -04004291static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4292{
4293 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004294 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004295 int rc;
4296
4297 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4298 req.fid = cpu_to_le16(0xffff);
4299 mutex_lock(&bp->hwrm_cmd_lock);
4300 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4301 if (rc)
4302 goto func_qcfg_exit;
4303
4304#ifdef CONFIG_BNXT_SRIOV
4305 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004306 struct bnxt_vf_info *vf = &bp->vf;
4307
4308 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4309 }
4310#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004311 switch (resp->port_partition_type) {
4312 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4313 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4314 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4315 bp->port_partition_type = resp->port_partition_type;
4316 break;
4317 }
Michael Chancf6645f2016-06-13 02:25:28 -04004318
4319func_qcfg_exit:
4320 mutex_unlock(&bp->hwrm_cmd_lock);
4321 return rc;
4322}
4323
Michael Chan7b08f662016-12-07 00:26:18 -05004324static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004325{
4326 int rc = 0;
4327 struct hwrm_func_qcaps_input req = {0};
4328 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4329
4330 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4331 req.fid = cpu_to_le16(0xffff);
4332
4333 mutex_lock(&bp->hwrm_cmd_lock);
4334 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4335 if (rc)
4336 goto hwrm_func_qcaps_exit;
4337
Michael Chane4060d32016-12-07 00:26:19 -05004338 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4339 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4340 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4341 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4342
Michael Chan7cc5a202016-09-19 03:58:05 -04004343 bp->tx_push_thresh = 0;
4344 if (resp->flags &
4345 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4346 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4347
Michael Chanc0c050c2015-10-22 16:01:17 -04004348 if (BNXT_PF(bp)) {
4349 struct bnxt_pf_info *pf = &bp->pf;
4350
4351 pf->fw_fid = le16_to_cpu(resp->fid);
4352 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004353 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004354 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004355 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004356 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4357 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4358 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004359 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004360 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4361 if (!pf->max_hw_ring_grps)
4362 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004363 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4364 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4365 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4366 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4367 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4368 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4369 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4370 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4371 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4372 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4373 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4374 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004375#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004376 struct bnxt_vf_info *vf = &bp->vf;
4377
4378 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004379
4380 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4381 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4382 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4383 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004384 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4385 if (!vf->max_hw_ring_grps)
4386 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004387 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4388 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4389 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004390
4391 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004392 mutex_unlock(&bp->hwrm_cmd_lock);
4393
4394 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004395 /* overwrite netdev dev_adr with admin VF MAC */
4396 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004397 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004398 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004399 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4400 }
4401 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004402#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004403 }
4404
Michael Chanc0c050c2015-10-22 16:01:17 -04004405hwrm_func_qcaps_exit:
4406 mutex_unlock(&bp->hwrm_cmd_lock);
4407 return rc;
4408}
4409
4410static int bnxt_hwrm_func_reset(struct bnxt *bp)
4411{
4412 struct hwrm_func_reset_input req = {0};
4413
4414 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4415 req.enables = 0;
4416
4417 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4418}
4419
4420static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4421{
4422 int rc = 0;
4423 struct hwrm_queue_qportcfg_input req = {0};
4424 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4425 u8 i, *qptr;
4426
4427 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4428
4429 mutex_lock(&bp->hwrm_cmd_lock);
4430 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4431 if (rc)
4432 goto qportcfg_exit;
4433
4434 if (!resp->max_configurable_queues) {
4435 rc = -EINVAL;
4436 goto qportcfg_exit;
4437 }
4438 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004439 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004440 if (bp->max_tc > BNXT_MAX_QUEUE)
4441 bp->max_tc = BNXT_MAX_QUEUE;
4442
Michael Chan441cabb2016-09-19 03:58:02 -04004443 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4444 bp->max_tc = 1;
4445
Michael Chan87c374d2016-12-02 21:17:16 -05004446 if (bp->max_lltc > bp->max_tc)
4447 bp->max_lltc = bp->max_tc;
4448
Michael Chanc0c050c2015-10-22 16:01:17 -04004449 qptr = &resp->queue_id0;
4450 for (i = 0; i < bp->max_tc; i++) {
4451 bp->q_info[i].queue_id = *qptr++;
4452 bp->q_info[i].queue_profile = *qptr++;
4453 }
4454
4455qportcfg_exit:
4456 mutex_unlock(&bp->hwrm_cmd_lock);
4457 return rc;
4458}
4459
4460static int bnxt_hwrm_ver_get(struct bnxt *bp)
4461{
4462 int rc;
4463 struct hwrm_ver_get_input req = {0};
4464 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4465
Michael Chane6ef2692016-03-28 19:46:05 -04004466 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004467 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4468 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4469 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4470 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4471 mutex_lock(&bp->hwrm_cmd_lock);
4472 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4473 if (rc)
4474 goto hwrm_ver_get_exit;
4475
4476 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4477
Michael Chan11f15ed2016-04-05 14:08:55 -04004478 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4479 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004480 if (resp->hwrm_intf_maj < 1) {
4481 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004482 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004483 resp->hwrm_intf_upd);
4484 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004485 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004486 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004487 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4488 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4489
Michael Chanff4fe812016-02-26 04:00:04 -05004490 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4491 if (!bp->hwrm_cmd_timeout)
4492 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4493
Michael Chane6ef2692016-03-28 19:46:05 -04004494 if (resp->hwrm_intf_maj >= 1)
4495 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4496
Michael Chan659c8052016-06-13 02:25:33 -04004497 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004498 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4499 !resp->chip_metal)
4500 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004501
Michael Chanc0c050c2015-10-22 16:01:17 -04004502hwrm_ver_get_exit:
4503 mutex_unlock(&bp->hwrm_cmd_lock);
4504 return rc;
4505}
4506
Rob Swindell5ac67d82016-09-19 03:58:03 -04004507int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4508{
Rob Swindell878786d2016-09-20 03:36:33 -04004509#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004510 struct hwrm_fw_set_time_input req = {0};
4511 struct rtc_time tm;
4512 struct timeval tv;
4513
4514 if (bp->hwrm_spec_code < 0x10400)
4515 return -EOPNOTSUPP;
4516
4517 do_gettimeofday(&tv);
4518 rtc_time_to_tm(tv.tv_sec, &tm);
4519 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4520 req.year = cpu_to_le16(1900 + tm.tm_year);
4521 req.month = 1 + tm.tm_mon;
4522 req.day = tm.tm_mday;
4523 req.hour = tm.tm_hour;
4524 req.minute = tm.tm_min;
4525 req.second = tm.tm_sec;
4526 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004527#else
4528 return -EOPNOTSUPP;
4529#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004530}
4531
Michael Chan3bdf56c2016-03-07 15:38:45 -05004532static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4533{
4534 int rc;
4535 struct bnxt_pf_info *pf = &bp->pf;
4536 struct hwrm_port_qstats_input req = {0};
4537
4538 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4539 return 0;
4540
4541 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4542 req.port_id = cpu_to_le16(pf->port_id);
4543 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4544 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4545 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4546 return rc;
4547}
4548
Michael Chanc0c050c2015-10-22 16:01:17 -04004549static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4550{
4551 if (bp->vxlan_port_cnt) {
4552 bnxt_hwrm_tunnel_dst_port_free(
4553 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4554 }
4555 bp->vxlan_port_cnt = 0;
4556 if (bp->nge_port_cnt) {
4557 bnxt_hwrm_tunnel_dst_port_free(
4558 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4559 }
4560 bp->nge_port_cnt = 0;
4561}
4562
4563static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4564{
4565 int rc, i;
4566 u32 tpa_flags = 0;
4567
4568 if (set_tpa)
4569 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4570 for (i = 0; i < bp->nr_vnics; i++) {
4571 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4572 if (rc) {
4573 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4574 rc, i);
4575 return rc;
4576 }
4577 }
4578 return 0;
4579}
4580
4581static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4582{
4583 int i;
4584
4585 for (i = 0; i < bp->nr_vnics; i++)
4586 bnxt_hwrm_vnic_set_rss(bp, i, false);
4587}
4588
4589static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4590 bool irq_re_init)
4591{
4592 if (bp->vnic_info) {
4593 bnxt_hwrm_clear_vnic_filter(bp);
4594 /* clear all RSS setting before free vnic ctx */
4595 bnxt_hwrm_clear_vnic_rss(bp);
4596 bnxt_hwrm_vnic_ctx_free(bp);
4597 /* before free the vnic, undo the vnic tpa settings */
4598 if (bp->flags & BNXT_FLAG_TPA)
4599 bnxt_set_tpa(bp, false);
4600 bnxt_hwrm_vnic_free(bp);
4601 }
4602 bnxt_hwrm_ring_free(bp, close_path);
4603 bnxt_hwrm_ring_grp_free(bp);
4604 if (irq_re_init) {
4605 bnxt_hwrm_stat_ctx_free(bp);
4606 bnxt_hwrm_free_tunnel_ports(bp);
4607 }
4608}
4609
4610static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4611{
Michael Chanae10ae72016-12-29 12:13:38 -05004612 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004613 int rc;
4614
Michael Chanae10ae72016-12-29 12:13:38 -05004615 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4616 goto skip_rss_ctx;
4617
Michael Chanc0c050c2015-10-22 16:01:17 -04004618 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004619 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004620 if (rc) {
4621 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4622 vnic_id, rc);
4623 goto vnic_setup_err;
4624 }
4625 bp->rsscos_nr_ctxs++;
4626
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004627 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4628 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4629 if (rc) {
4630 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4631 vnic_id, rc);
4632 goto vnic_setup_err;
4633 }
4634 bp->rsscos_nr_ctxs++;
4635 }
4636
Michael Chanae10ae72016-12-29 12:13:38 -05004637skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004638 /* configure default vnic, ring grp */
4639 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4640 if (rc) {
4641 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4642 vnic_id, rc);
4643 goto vnic_setup_err;
4644 }
4645
4646 /* Enable RSS hashing on vnic */
4647 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4648 if (rc) {
4649 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4650 vnic_id, rc);
4651 goto vnic_setup_err;
4652 }
4653
4654 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4655 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4656 if (rc) {
4657 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4658 vnic_id, rc);
4659 }
4660 }
4661
4662vnic_setup_err:
4663 return rc;
4664}
4665
4666static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4667{
4668#ifdef CONFIG_RFS_ACCEL
4669 int i, rc = 0;
4670
4671 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004672 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004673 u16 vnic_id = i + 1;
4674 u16 ring_id = i;
4675
4676 if (vnic_id >= bp->nr_vnics)
4677 break;
4678
Michael Chanae10ae72016-12-29 12:13:38 -05004679 vnic = &bp->vnic_info[vnic_id];
4680 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4681 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4682 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004683 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004684 if (rc) {
4685 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4686 vnic_id, rc);
4687 break;
4688 }
4689 rc = bnxt_setup_vnic(bp, vnic_id);
4690 if (rc)
4691 break;
4692 }
4693 return rc;
4694#else
4695 return 0;
4696#endif
4697}
4698
Michael Chan17c71ac2016-07-01 18:46:27 -04004699/* Allow PF and VF with default VLAN to be in promiscuous mode */
4700static bool bnxt_promisc_ok(struct bnxt *bp)
4701{
4702#ifdef CONFIG_BNXT_SRIOV
4703 if (BNXT_VF(bp) && !bp->vf.vlan)
4704 return false;
4705#endif
4706 return true;
4707}
4708
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004709static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4710{
4711 unsigned int rc = 0;
4712
4713 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4714 if (rc) {
4715 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4716 rc);
4717 return rc;
4718 }
4719
4720 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4721 if (rc) {
4722 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4723 rc);
4724 return rc;
4725 }
4726 return rc;
4727}
4728
Michael Chanb664f002015-12-02 01:54:08 -05004729static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004730static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004731
Michael Chanc0c050c2015-10-22 16:01:17 -04004732static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4733{
Michael Chan7d2837d2016-05-04 16:56:44 -04004734 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004735 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004736 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004737
4738 if (irq_re_init) {
4739 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4740 if (rc) {
4741 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4742 rc);
4743 goto err_out;
4744 }
4745 }
4746
4747 rc = bnxt_hwrm_ring_alloc(bp);
4748 if (rc) {
4749 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4750 goto err_out;
4751 }
4752
4753 rc = bnxt_hwrm_ring_grp_alloc(bp);
4754 if (rc) {
4755 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4756 goto err_out;
4757 }
4758
Prashant Sreedharan76595192016-07-18 07:15:22 -04004759 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4760 rx_nr_rings--;
4761
Michael Chanc0c050c2015-10-22 16:01:17 -04004762 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004763 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004764 if (rc) {
4765 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4766 goto err_out;
4767 }
4768
4769 rc = bnxt_setup_vnic(bp, 0);
4770 if (rc)
4771 goto err_out;
4772
4773 if (bp->flags & BNXT_FLAG_RFS) {
4774 rc = bnxt_alloc_rfs_vnics(bp);
4775 if (rc)
4776 goto err_out;
4777 }
4778
4779 if (bp->flags & BNXT_FLAG_TPA) {
4780 rc = bnxt_set_tpa(bp, true);
4781 if (rc)
4782 goto err_out;
4783 }
4784
4785 if (BNXT_VF(bp))
4786 bnxt_update_vf_mac(bp);
4787
4788 /* Filter for default vnic 0 */
4789 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4790 if (rc) {
4791 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4792 goto err_out;
4793 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004794 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004795
Michael Chan7d2837d2016-05-04 16:56:44 -04004796 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004797
Michael Chan17c71ac2016-07-01 18:46:27 -04004798 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004799 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4800
4801 if (bp->dev->flags & IFF_ALLMULTI) {
4802 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4803 vnic->mc_list_count = 0;
4804 } else {
4805 u32 mask = 0;
4806
4807 bnxt_mc_list_updated(bp, &mask);
4808 vnic->rx_mask |= mask;
4809 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004810
Michael Chanb664f002015-12-02 01:54:08 -05004811 rc = bnxt_cfg_rx_mode(bp);
4812 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004813 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004814
4815 rc = bnxt_hwrm_set_coal(bp);
4816 if (rc)
4817 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004818 rc);
4819
4820 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4821 rc = bnxt_setup_nitroa0_vnic(bp);
4822 if (rc)
4823 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4824 rc);
4825 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004826
Michael Chancf6645f2016-06-13 02:25:28 -04004827 if (BNXT_VF(bp)) {
4828 bnxt_hwrm_func_qcfg(bp);
4829 netdev_update_features(bp->dev);
4830 }
4831
Michael Chanc0c050c2015-10-22 16:01:17 -04004832 return 0;
4833
4834err_out:
4835 bnxt_hwrm_resource_free(bp, 0, true);
4836
4837 return rc;
4838}
4839
4840static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4841{
4842 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4843 return 0;
4844}
4845
4846static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4847{
4848 bnxt_init_rx_rings(bp);
4849 bnxt_init_tx_rings(bp);
4850 bnxt_init_ring_grps(bp, irq_re_init);
4851 bnxt_init_vnics(bp);
4852
4853 return bnxt_init_chip(bp, irq_re_init);
4854}
4855
Michael Chanc0c050c2015-10-22 16:01:17 -04004856static int bnxt_set_real_num_queues(struct bnxt *bp)
4857{
4858 int rc;
4859 struct net_device *dev = bp->dev;
4860
4861 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4862 if (rc)
4863 return rc;
4864
4865 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4866 if (rc)
4867 return rc;
4868
4869#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004870 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004871 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004872#endif
4873
4874 return rc;
4875}
4876
Michael Chan6e6c5a52016-01-02 23:45:02 -05004877static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4878 bool shared)
4879{
4880 int _rx = *rx, _tx = *tx;
4881
4882 if (shared) {
4883 *rx = min_t(int, _rx, max);
4884 *tx = min_t(int, _tx, max);
4885 } else {
4886 if (max < 2)
4887 return -ENOMEM;
4888
4889 while (_rx + _tx > max) {
4890 if (_rx > _tx && _rx > 1)
4891 _rx--;
4892 else if (_tx > 1)
4893 _tx--;
4894 }
4895 *rx = _rx;
4896 *tx = _tx;
4897 }
4898 return 0;
4899}
4900
Michael Chan78095922016-12-07 00:26:16 -05004901static void bnxt_setup_msix(struct bnxt *bp)
4902{
4903 const int len = sizeof(bp->irq_tbl[0].name);
4904 struct net_device *dev = bp->dev;
4905 int tcs, i;
4906
4907 tcs = netdev_get_num_tc(dev);
4908 if (tcs > 1) {
4909 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4910 if (bp->tx_nr_rings_per_tc == 0) {
4911 netdev_reset_tc(dev);
4912 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4913 } else {
4914 int i, off, count;
4915
4916 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4917 for (i = 0; i < tcs; i++) {
4918 count = bp->tx_nr_rings_per_tc;
4919 off = i * count;
4920 netdev_set_tc_queue(dev, i, count, off);
4921 }
4922 }
4923 }
4924
4925 for (i = 0; i < bp->cp_nr_rings; i++) {
4926 char *attr;
4927
4928 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4929 attr = "TxRx";
4930 else if (i < bp->rx_nr_rings)
4931 attr = "rx";
4932 else
4933 attr = "tx";
4934
4935 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
4936 i);
4937 bp->irq_tbl[i].handler = bnxt_msix;
4938 }
4939}
4940
4941static void bnxt_setup_inta(struct bnxt *bp)
4942{
4943 const int len = sizeof(bp->irq_tbl[0].name);
4944
4945 if (netdev_get_num_tc(bp->dev))
4946 netdev_reset_tc(bp->dev);
4947
4948 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
4949 0);
4950 bp->irq_tbl[0].handler = bnxt_inta;
4951}
4952
4953static int bnxt_setup_int_mode(struct bnxt *bp)
4954{
4955 int rc;
4956
4957 if (bp->flags & BNXT_FLAG_USING_MSIX)
4958 bnxt_setup_msix(bp);
4959 else
4960 bnxt_setup_inta(bp);
4961
4962 rc = bnxt_set_real_num_queues(bp);
4963 return rc;
4964}
4965
Michael Chanb7429952017-01-13 01:32:00 -05004966#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05004967static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
4968{
4969#if defined(CONFIG_BNXT_SRIOV)
4970 if (BNXT_VF(bp))
4971 return bp->vf.max_rsscos_ctxs;
4972#endif
4973 return bp->pf.max_rsscos_ctxs;
4974}
4975
4976static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
4977{
4978#if defined(CONFIG_BNXT_SRIOV)
4979 if (BNXT_VF(bp))
4980 return bp->vf.max_vnics;
4981#endif
4982 return bp->pf.max_vnics;
4983}
Michael Chanb7429952017-01-13 01:32:00 -05004984#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05004985
Michael Chane4060d32016-12-07 00:26:19 -05004986unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
4987{
4988#if defined(CONFIG_BNXT_SRIOV)
4989 if (BNXT_VF(bp))
4990 return bp->vf.max_stat_ctxs;
4991#endif
4992 return bp->pf.max_stat_ctxs;
4993}
4994
Michael Chana588e452016-12-07 00:26:21 -05004995void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
4996{
4997#if defined(CONFIG_BNXT_SRIOV)
4998 if (BNXT_VF(bp))
4999 bp->vf.max_stat_ctxs = max;
5000 else
5001#endif
5002 bp->pf.max_stat_ctxs = max;
5003}
5004
Michael Chane4060d32016-12-07 00:26:19 -05005005unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5006{
5007#if defined(CONFIG_BNXT_SRIOV)
5008 if (BNXT_VF(bp))
5009 return bp->vf.max_cp_rings;
5010#endif
5011 return bp->pf.max_cp_rings;
5012}
5013
Michael Chana588e452016-12-07 00:26:21 -05005014void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5015{
5016#if defined(CONFIG_BNXT_SRIOV)
5017 if (BNXT_VF(bp))
5018 bp->vf.max_cp_rings = max;
5019 else
5020#endif
5021 bp->pf.max_cp_rings = max;
5022}
5023
Michael Chan78095922016-12-07 00:26:16 -05005024static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5025{
5026#if defined(CONFIG_BNXT_SRIOV)
5027 if (BNXT_VF(bp))
5028 return bp->vf.max_irqs;
5029#endif
5030 return bp->pf.max_irqs;
5031}
5032
Michael Chan33c26572016-12-07 00:26:15 -05005033void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5034{
5035#if defined(CONFIG_BNXT_SRIOV)
5036 if (BNXT_VF(bp))
5037 bp->vf.max_irqs = max_irqs;
5038 else
5039#endif
5040 bp->pf.max_irqs = max_irqs;
5041}
5042
Michael Chan78095922016-12-07 00:26:16 -05005043static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005044{
Michael Chan01657bc2016-01-02 23:45:03 -05005045 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005046 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005047
Michael Chan78095922016-12-07 00:26:16 -05005048 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005049 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5050 if (!msix_ent)
5051 return -ENOMEM;
5052
5053 for (i = 0; i < total_vecs; i++) {
5054 msix_ent[i].entry = i;
5055 msix_ent[i].vector = 0;
5056 }
5057
Michael Chan01657bc2016-01-02 23:45:03 -05005058 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5059 min = 2;
5060
5061 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005062 if (total_vecs < 0) {
5063 rc = -ENODEV;
5064 goto msix_setup_exit;
5065 }
5066
5067 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5068 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005069 for (i = 0; i < total_vecs; i++)
5070 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005071
Michael Chan78095922016-12-07 00:26:16 -05005072 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005073 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005074 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005075 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005076 if (rc)
5077 goto msix_setup_exit;
5078
Michael Chanc0c050c2015-10-22 16:01:17 -04005079 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005080 bp->cp_nr_rings = (min == 1) ?
5081 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5082 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005083
Michael Chanc0c050c2015-10-22 16:01:17 -04005084 } else {
5085 rc = -ENOMEM;
5086 goto msix_setup_exit;
5087 }
5088 bp->flags |= BNXT_FLAG_USING_MSIX;
5089 kfree(msix_ent);
5090 return 0;
5091
5092msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005093 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5094 kfree(bp->irq_tbl);
5095 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005096 pci_disable_msix(bp->pdev);
5097 kfree(msix_ent);
5098 return rc;
5099}
5100
Michael Chan78095922016-12-07 00:26:16 -05005101static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005102{
Michael Chanc0c050c2015-10-22 16:01:17 -04005103 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005104 if (!bp->irq_tbl)
5105 return -ENOMEM;
5106
5107 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005108 bp->rx_nr_rings = 1;
5109 bp->tx_nr_rings = 1;
5110 bp->cp_nr_rings = 1;
5111 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005112 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005113 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005114 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005115}
5116
Michael Chan78095922016-12-07 00:26:16 -05005117static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005118{
5119 int rc = 0;
5120
5121 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005122 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005123
Michael Chan1fa72e22016-04-25 02:30:49 -04005124 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005125 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005126 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005127 }
5128 return rc;
5129}
5130
Michael Chan78095922016-12-07 00:26:16 -05005131static void bnxt_clear_int_mode(struct bnxt *bp)
5132{
5133 if (bp->flags & BNXT_FLAG_USING_MSIX)
5134 pci_disable_msix(bp->pdev);
5135
5136 kfree(bp->irq_tbl);
5137 bp->irq_tbl = NULL;
5138 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5139}
5140
Michael Chanc0c050c2015-10-22 16:01:17 -04005141static void bnxt_free_irq(struct bnxt *bp)
5142{
5143 struct bnxt_irq *irq;
5144 int i;
5145
5146#ifdef CONFIG_RFS_ACCEL
5147 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5148 bp->dev->rx_cpu_rmap = NULL;
5149#endif
5150 if (!bp->irq_tbl)
5151 return;
5152
5153 for (i = 0; i < bp->cp_nr_rings; i++) {
5154 irq = &bp->irq_tbl[i];
5155 if (irq->requested)
5156 free_irq(irq->vector, bp->bnapi[i]);
5157 irq->requested = 0;
5158 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005159}
5160
5161static int bnxt_request_irq(struct bnxt *bp)
5162{
Michael Chanb81a90d2016-01-02 23:45:01 -05005163 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005164 unsigned long flags = 0;
5165#ifdef CONFIG_RFS_ACCEL
5166 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5167#endif
5168
5169 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5170 flags = IRQF_SHARED;
5171
Michael Chanb81a90d2016-01-02 23:45:01 -05005172 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005173 struct bnxt_irq *irq = &bp->irq_tbl[i];
5174#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005175 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005176 rc = irq_cpu_rmap_add(rmap, irq->vector);
5177 if (rc)
5178 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005179 j);
5180 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005181 }
5182#endif
5183 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5184 bp->bnapi[i]);
5185 if (rc)
5186 break;
5187
5188 irq->requested = 1;
5189 }
5190 return rc;
5191}
5192
5193static void bnxt_del_napi(struct bnxt *bp)
5194{
5195 int i;
5196
5197 if (!bp->bnapi)
5198 return;
5199
5200 for (i = 0; i < bp->cp_nr_rings; i++) {
5201 struct bnxt_napi *bnapi = bp->bnapi[i];
5202
5203 napi_hash_del(&bnapi->napi);
5204 netif_napi_del(&bnapi->napi);
5205 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005206 /* We called napi_hash_del() before netif_napi_del(), we need
5207 * to respect an RCU grace period before freeing napi structures.
5208 */
5209 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005210}
5211
5212static void bnxt_init_napi(struct bnxt *bp)
5213{
5214 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005215 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005216 struct bnxt_napi *bnapi;
5217
5218 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005219 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5220 cp_nr_rings--;
5221 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005222 bnapi = bp->bnapi[i];
5223 netif_napi_add(bp->dev, &bnapi->napi,
5224 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005225 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005226 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5227 bnapi = bp->bnapi[cp_nr_rings];
5228 netif_napi_add(bp->dev, &bnapi->napi,
5229 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005230 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005231 } else {
5232 bnapi = bp->bnapi[0];
5233 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005234 }
5235}
5236
5237static void bnxt_disable_napi(struct bnxt *bp)
5238{
5239 int i;
5240
5241 if (!bp->bnapi)
5242 return;
5243
Michael Chanb356a2e2016-12-29 12:13:31 -05005244 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005245 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005246}
5247
5248static void bnxt_enable_napi(struct bnxt *bp)
5249{
5250 int i;
5251
5252 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005253 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005254 napi_enable(&bp->bnapi[i]->napi);
5255 }
5256}
5257
Michael Chan7df4ae92016-12-02 21:17:17 -05005258void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005259{
5260 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005261 struct bnxt_tx_ring_info *txr;
5262 struct netdev_queue *txq;
5263
Michael Chanb6ab4b02016-01-02 23:44:59 -05005264 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005265 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005266 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005267 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005268 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005269 }
5270 }
5271 /* Stop all TX queues */
5272 netif_tx_disable(bp->dev);
5273 netif_carrier_off(bp->dev);
5274}
5275
Michael Chan7df4ae92016-12-02 21:17:17 -05005276void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005277{
5278 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005279 struct bnxt_tx_ring_info *txr;
5280 struct netdev_queue *txq;
5281
5282 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005283 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005284 txq = netdev_get_tx_queue(bp->dev, i);
5285 txr->dev_state = 0;
5286 }
5287 netif_tx_wake_all_queues(bp->dev);
5288 if (bp->link_info.link_up)
5289 netif_carrier_on(bp->dev);
5290}
5291
5292static void bnxt_report_link(struct bnxt *bp)
5293{
5294 if (bp->link_info.link_up) {
5295 const char *duplex;
5296 const char *flow_ctrl;
5297 u16 speed;
5298
5299 netif_carrier_on(bp->dev);
5300 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5301 duplex = "full";
5302 else
5303 duplex = "half";
5304 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5305 flow_ctrl = "ON - receive & transmit";
5306 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5307 flow_ctrl = "ON - transmit";
5308 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5309 flow_ctrl = "ON - receive";
5310 else
5311 flow_ctrl = "none";
5312 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5313 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5314 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005315 if (bp->flags & BNXT_FLAG_EEE_CAP)
5316 netdev_info(bp->dev, "EEE is %s\n",
5317 bp->eee.eee_active ? "active" :
5318 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005319 } else {
5320 netif_carrier_off(bp->dev);
5321 netdev_err(bp->dev, "NIC Link is Down\n");
5322 }
5323}
5324
Michael Chan170ce012016-04-05 14:08:57 -04005325static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5326{
5327 int rc = 0;
5328 struct hwrm_port_phy_qcaps_input req = {0};
5329 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005330 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005331
5332 if (bp->hwrm_spec_code < 0x10201)
5333 return 0;
5334
5335 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5336
5337 mutex_lock(&bp->hwrm_cmd_lock);
5338 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5339 if (rc)
5340 goto hwrm_phy_qcaps_exit;
5341
5342 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5343 struct ethtool_eee *eee = &bp->eee;
5344 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5345
5346 bp->flags |= BNXT_FLAG_EEE_CAP;
5347 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5348 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5349 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5350 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5351 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5352 }
Michael Chan93ed8112016-06-13 02:25:37 -04005353 link_info->support_auto_speeds =
5354 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005355
5356hwrm_phy_qcaps_exit:
5357 mutex_unlock(&bp->hwrm_cmd_lock);
5358 return rc;
5359}
5360
Michael Chanc0c050c2015-10-22 16:01:17 -04005361static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5362{
5363 int rc = 0;
5364 struct bnxt_link_info *link_info = &bp->link_info;
5365 struct hwrm_port_phy_qcfg_input req = {0};
5366 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5367 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005368 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005369
5370 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5371
5372 mutex_lock(&bp->hwrm_cmd_lock);
5373 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5374 if (rc) {
5375 mutex_unlock(&bp->hwrm_cmd_lock);
5376 return rc;
5377 }
5378
5379 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5380 link_info->phy_link_status = resp->link;
5381 link_info->duplex = resp->duplex;
5382 link_info->pause = resp->pause;
5383 link_info->auto_mode = resp->auto_mode;
5384 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005385 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005386 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005387 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005388 if (link_info->phy_link_status == BNXT_LINK_LINK)
5389 link_info->link_speed = le16_to_cpu(resp->link_speed);
5390 else
5391 link_info->link_speed = 0;
5392 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005393 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5394 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005395 link_info->lp_auto_link_speeds =
5396 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005397 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5398 link_info->phy_ver[0] = resp->phy_maj;
5399 link_info->phy_ver[1] = resp->phy_min;
5400 link_info->phy_ver[2] = resp->phy_bld;
5401 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005402 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005403 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005404 link_info->phy_addr = resp->eee_config_phy_addr &
5405 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005406 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005407
Michael Chan170ce012016-04-05 14:08:57 -04005408 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5409 struct ethtool_eee *eee = &bp->eee;
5410 u16 fw_speeds;
5411
5412 eee->eee_active = 0;
5413 if (resp->eee_config_phy_addr &
5414 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5415 eee->eee_active = 1;
5416 fw_speeds = le16_to_cpu(
5417 resp->link_partner_adv_eee_link_speed_mask);
5418 eee->lp_advertised =
5419 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5420 }
5421
5422 /* Pull initial EEE config */
5423 if (!chng_link_state) {
5424 if (resp->eee_config_phy_addr &
5425 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5426 eee->eee_enabled = 1;
5427
5428 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5429 eee->advertised =
5430 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5431
5432 if (resp->eee_config_phy_addr &
5433 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5434 __le32 tmr;
5435
5436 eee->tx_lpi_enabled = 1;
5437 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5438 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5439 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5440 }
5441 }
5442 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005443 /* TODO: need to add more logic to report VF link */
5444 if (chng_link_state) {
5445 if (link_info->phy_link_status == BNXT_LINK_LINK)
5446 link_info->link_up = 1;
5447 else
5448 link_info->link_up = 0;
5449 if (link_up != link_info->link_up)
5450 bnxt_report_link(bp);
5451 } else {
5452 /* alwasy link down if not require to update link state */
5453 link_info->link_up = 0;
5454 }
5455 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005456
5457 diff = link_info->support_auto_speeds ^ link_info->advertising;
5458 if ((link_info->support_auto_speeds | diff) !=
5459 link_info->support_auto_speeds) {
5460 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005461 * update the advertisement settings. Caller holds RTNL
5462 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005463 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005464 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005465 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005466 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005467 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005468 return 0;
5469}
5470
Michael Chan10289be2016-05-15 03:04:49 -04005471static void bnxt_get_port_module_status(struct bnxt *bp)
5472{
5473 struct bnxt_link_info *link_info = &bp->link_info;
5474 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5475 u8 module_status;
5476
5477 if (bnxt_update_link(bp, true))
5478 return;
5479
5480 module_status = link_info->module_status;
5481 switch (module_status) {
5482 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5483 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5484 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5485 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5486 bp->pf.port_id);
5487 if (bp->hwrm_spec_code >= 0x10201) {
5488 netdev_warn(bp->dev, "Module part number %s\n",
5489 resp->phy_vendor_partnumber);
5490 }
5491 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5492 netdev_warn(bp->dev, "TX is disabled\n");
5493 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5494 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5495 }
5496}
5497
Michael Chanc0c050c2015-10-22 16:01:17 -04005498static void
5499bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5500{
5501 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005502 if (bp->hwrm_spec_code >= 0x10201)
5503 req->auto_pause =
5504 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005505 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5506 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5507 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005508 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005509 req->enables |=
5510 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5511 } else {
5512 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5513 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5514 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5515 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5516 req->enables |=
5517 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005518 if (bp->hwrm_spec_code >= 0x10201) {
5519 req->auto_pause = req->force_pause;
5520 req->enables |= cpu_to_le32(
5521 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5522 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005523 }
5524}
5525
5526static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5527 struct hwrm_port_phy_cfg_input *req)
5528{
5529 u8 autoneg = bp->link_info.autoneg;
5530 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005531 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005532
5533 if (autoneg & BNXT_AUTONEG_SPEED) {
5534 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005535 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005536
5537 req->enables |= cpu_to_le32(
5538 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5539 req->auto_link_speed_mask = cpu_to_le16(advertising);
5540
5541 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5542 req->flags |=
5543 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5544 } else {
5545 req->force_link_speed = cpu_to_le16(fw_link_speed);
5546 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5547 }
5548
Michael Chanc0c050c2015-10-22 16:01:17 -04005549 /* tell chimp that the setting takes effect immediately */
5550 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5551}
5552
5553int bnxt_hwrm_set_pause(struct bnxt *bp)
5554{
5555 struct hwrm_port_phy_cfg_input req = {0};
5556 int rc;
5557
5558 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5559 bnxt_hwrm_set_pause_common(bp, &req);
5560
5561 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5562 bp->link_info.force_link_chng)
5563 bnxt_hwrm_set_link_common(bp, &req);
5564
5565 mutex_lock(&bp->hwrm_cmd_lock);
5566 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5567 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5568 /* since changing of pause setting doesn't trigger any link
5569 * change event, the driver needs to update the current pause
5570 * result upon successfully return of the phy_cfg command
5571 */
5572 bp->link_info.pause =
5573 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5574 bp->link_info.auto_pause_setting = 0;
5575 if (!bp->link_info.force_link_chng)
5576 bnxt_report_link(bp);
5577 }
5578 bp->link_info.force_link_chng = false;
5579 mutex_unlock(&bp->hwrm_cmd_lock);
5580 return rc;
5581}
5582
Michael Chan939f7f02016-04-05 14:08:58 -04005583static void bnxt_hwrm_set_eee(struct bnxt *bp,
5584 struct hwrm_port_phy_cfg_input *req)
5585{
5586 struct ethtool_eee *eee = &bp->eee;
5587
5588 if (eee->eee_enabled) {
5589 u16 eee_speeds;
5590 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5591
5592 if (eee->tx_lpi_enabled)
5593 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5594 else
5595 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5596
5597 req->flags |= cpu_to_le32(flags);
5598 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5599 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5600 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5601 } else {
5602 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5603 }
5604}
5605
5606int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005607{
5608 struct hwrm_port_phy_cfg_input req = {0};
5609
5610 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5611 if (set_pause)
5612 bnxt_hwrm_set_pause_common(bp, &req);
5613
5614 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005615
5616 if (set_eee)
5617 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005618 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5619}
5620
Michael Chan33f7d552016-04-11 04:11:12 -04005621static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5622{
5623 struct hwrm_port_phy_cfg_input req = {0};
5624
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005625 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005626 return 0;
5627
5628 if (pci_num_vf(bp->pdev))
5629 return 0;
5630
5631 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005632 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005633 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5634}
5635
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005636static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5637{
5638 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5639 struct hwrm_port_led_qcaps_input req = {0};
5640 struct bnxt_pf_info *pf = &bp->pf;
5641 int rc;
5642
5643 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5644 return 0;
5645
5646 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5647 req.port_id = cpu_to_le16(pf->port_id);
5648 mutex_lock(&bp->hwrm_cmd_lock);
5649 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5650 if (rc) {
5651 mutex_unlock(&bp->hwrm_cmd_lock);
5652 return rc;
5653 }
5654 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5655 int i;
5656
5657 bp->num_leds = resp->num_leds;
5658 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5659 bp->num_leds);
5660 for (i = 0; i < bp->num_leds; i++) {
5661 struct bnxt_led_info *led = &bp->leds[i];
5662 __le16 caps = led->led_state_caps;
5663
5664 if (!led->led_group_id ||
5665 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5666 bp->num_leds = 0;
5667 break;
5668 }
5669 }
5670 }
5671 mutex_unlock(&bp->hwrm_cmd_lock);
5672 return 0;
5673}
5674
Michael Chan939f7f02016-04-05 14:08:58 -04005675static bool bnxt_eee_config_ok(struct bnxt *bp)
5676{
5677 struct ethtool_eee *eee = &bp->eee;
5678 struct bnxt_link_info *link_info = &bp->link_info;
5679
5680 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5681 return true;
5682
5683 if (eee->eee_enabled) {
5684 u32 advertising =
5685 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5686
5687 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5688 eee->eee_enabled = 0;
5689 return false;
5690 }
5691 if (eee->advertised & ~advertising) {
5692 eee->advertised = advertising & eee->supported;
5693 return false;
5694 }
5695 }
5696 return true;
5697}
5698
Michael Chanc0c050c2015-10-22 16:01:17 -04005699static int bnxt_update_phy_setting(struct bnxt *bp)
5700{
5701 int rc;
5702 bool update_link = false;
5703 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005704 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005705 struct bnxt_link_info *link_info = &bp->link_info;
5706
5707 rc = bnxt_update_link(bp, true);
5708 if (rc) {
5709 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5710 rc);
5711 return rc;
5712 }
5713 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005714 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5715 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005716 update_pause = true;
5717 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5718 link_info->force_pause_setting != link_info->req_flow_ctrl)
5719 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005720 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5721 if (BNXT_AUTO_MODE(link_info->auto_mode))
5722 update_link = true;
5723 if (link_info->req_link_speed != link_info->force_link_speed)
5724 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005725 if (link_info->req_duplex != link_info->duplex_setting)
5726 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005727 } else {
5728 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5729 update_link = true;
5730 if (link_info->advertising != link_info->auto_link_speeds)
5731 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005732 }
5733
Michael Chan16d663a2016-11-16 21:13:07 -05005734 /* The last close may have shutdown the link, so need to call
5735 * PHY_CFG to bring it back up.
5736 */
5737 if (!netif_carrier_ok(bp->dev))
5738 update_link = true;
5739
Michael Chan939f7f02016-04-05 14:08:58 -04005740 if (!bnxt_eee_config_ok(bp))
5741 update_eee = true;
5742
Michael Chanc0c050c2015-10-22 16:01:17 -04005743 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005744 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005745 else if (update_pause)
5746 rc = bnxt_hwrm_set_pause(bp);
5747 if (rc) {
5748 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5749 rc);
5750 return rc;
5751 }
5752
5753 return rc;
5754}
5755
Jeffrey Huang11809492015-11-05 16:25:49 -05005756/* Common routine to pre-map certain register block to different GRC window.
5757 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5758 * in PF and 3 windows in VF that can be customized to map in different
5759 * register blocks.
5760 */
5761static void bnxt_preset_reg_win(struct bnxt *bp)
5762{
5763 if (BNXT_PF(bp)) {
5764 /* CAG registers map to GRC window #4 */
5765 writel(BNXT_CAG_REG_BASE,
5766 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5767 }
5768}
5769
Michael Chanc0c050c2015-10-22 16:01:17 -04005770static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5771{
5772 int rc = 0;
5773
Jeffrey Huang11809492015-11-05 16:25:49 -05005774 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005775 netif_carrier_off(bp->dev);
5776 if (irq_re_init) {
5777 rc = bnxt_setup_int_mode(bp);
5778 if (rc) {
5779 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5780 rc);
5781 return rc;
5782 }
5783 }
5784 if ((bp->flags & BNXT_FLAG_RFS) &&
5785 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5786 /* disable RFS if falling back to INTA */
5787 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5788 bp->flags &= ~BNXT_FLAG_RFS;
5789 }
5790
5791 rc = bnxt_alloc_mem(bp, irq_re_init);
5792 if (rc) {
5793 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5794 goto open_err_free_mem;
5795 }
5796
5797 if (irq_re_init) {
5798 bnxt_init_napi(bp);
5799 rc = bnxt_request_irq(bp);
5800 if (rc) {
5801 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5802 goto open_err;
5803 }
5804 }
5805
5806 bnxt_enable_napi(bp);
5807
5808 rc = bnxt_init_nic(bp, irq_re_init);
5809 if (rc) {
5810 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5811 goto open_err;
5812 }
5813
5814 if (link_re_init) {
5815 rc = bnxt_update_phy_setting(bp);
5816 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005817 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005818 }
5819
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005820 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005821 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005822
Michael Chancaefe522015-12-09 19:35:42 -05005823 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005824 bnxt_enable_int(bp);
5825 /* Enable TX queues */
5826 bnxt_tx_enable(bp);
5827 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005828 /* Poll link status and check for SFP+ module status */
5829 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005830
5831 return 0;
5832
5833open_err:
5834 bnxt_disable_napi(bp);
5835 bnxt_del_napi(bp);
5836
5837open_err_free_mem:
5838 bnxt_free_skbs(bp);
5839 bnxt_free_irq(bp);
5840 bnxt_free_mem(bp, true);
5841 return rc;
5842}
5843
5844/* rtnl_lock held */
5845int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5846{
5847 int rc = 0;
5848
5849 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5850 if (rc) {
5851 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5852 dev_close(bp->dev);
5853 }
5854 return rc;
5855}
5856
5857static int bnxt_open(struct net_device *dev)
5858{
5859 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005860
Michael Chanc0c050c2015-10-22 16:01:17 -04005861 return __bnxt_open_nic(bp, true, true);
5862}
5863
Michael Chanc0c050c2015-10-22 16:01:17 -04005864int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5865{
5866 int rc = 0;
5867
5868#ifdef CONFIG_BNXT_SRIOV
5869 if (bp->sriov_cfg) {
5870 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5871 !bp->sriov_cfg,
5872 BNXT_SRIOV_CFG_WAIT_TMO);
5873 if (rc)
5874 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5875 }
5876#endif
5877 /* Change device state to avoid TX queue wake up's */
5878 bnxt_tx_disable(bp);
5879
Michael Chancaefe522015-12-09 19:35:42 -05005880 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005881 smp_mb__after_atomic();
5882 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5883 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005884
Michael Chan9d8bc092016-12-29 12:13:33 -05005885 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04005886 bnxt_shutdown_nic(bp, irq_re_init);
5887
5888 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5889
5890 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005891 del_timer_sync(&bp->timer);
5892 bnxt_free_skbs(bp);
5893
5894 if (irq_re_init) {
5895 bnxt_free_irq(bp);
5896 bnxt_del_napi(bp);
5897 }
5898 bnxt_free_mem(bp, irq_re_init);
5899 return rc;
5900}
5901
5902static int bnxt_close(struct net_device *dev)
5903{
5904 struct bnxt *bp = netdev_priv(dev);
5905
5906 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005907 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005908 return 0;
5909}
5910
5911/* rtnl_lock held */
5912static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5913{
5914 switch (cmd) {
5915 case SIOCGMIIPHY:
5916 /* fallthru */
5917 case SIOCGMIIREG: {
5918 if (!netif_running(dev))
5919 return -EAGAIN;
5920
5921 return 0;
5922 }
5923
5924 case SIOCSMIIREG:
5925 if (!netif_running(dev))
5926 return -EAGAIN;
5927
5928 return 0;
5929
5930 default:
5931 /* do nothing */
5932 break;
5933 }
5934 return -EOPNOTSUPP;
5935}
5936
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005937static void
Michael Chanc0c050c2015-10-22 16:01:17 -04005938bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5939{
5940 u32 i;
5941 struct bnxt *bp = netdev_priv(dev);
5942
Michael Chanc0c050c2015-10-22 16:01:17 -04005943 if (!bp->bnapi)
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005944 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04005945
5946 /* TODO check if we need to synchronize with bnxt_close path */
5947 for (i = 0; i < bp->cp_nr_rings; i++) {
5948 struct bnxt_napi *bnapi = bp->bnapi[i];
5949 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5950 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5951
5952 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5953 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5954 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5955
5956 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5957 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5958 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5959
5960 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5961 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5962 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5963
5964 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5965 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5966 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5967
5968 stats->rx_missed_errors +=
5969 le64_to_cpu(hw_stats->rx_discard_pkts);
5970
5971 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5972
Michael Chanc0c050c2015-10-22 16:01:17 -04005973 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5974 }
5975
Michael Chan9947f832016-03-07 15:38:46 -05005976 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5977 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5978 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5979
5980 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5981 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5982 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5983 le64_to_cpu(rx->rx_ovrsz_frames) +
5984 le64_to_cpu(rx->rx_runt_frames);
5985 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5986 le64_to_cpu(rx->rx_jbr_frames);
5987 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5988 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5989 stats->tx_errors = le64_to_cpu(tx->tx_err);
5990 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005991}
5992
5993static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5994{
5995 struct net_device *dev = bp->dev;
5996 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5997 struct netdev_hw_addr *ha;
5998 u8 *haddr;
5999 int mc_count = 0;
6000 bool update = false;
6001 int off = 0;
6002
6003 netdev_for_each_mc_addr(ha, dev) {
6004 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6005 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6006 vnic->mc_list_count = 0;
6007 return false;
6008 }
6009 haddr = ha->addr;
6010 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6011 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6012 update = true;
6013 }
6014 off += ETH_ALEN;
6015 mc_count++;
6016 }
6017 if (mc_count)
6018 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6019
6020 if (mc_count != vnic->mc_list_count) {
6021 vnic->mc_list_count = mc_count;
6022 update = true;
6023 }
6024 return update;
6025}
6026
6027static bool bnxt_uc_list_updated(struct bnxt *bp)
6028{
6029 struct net_device *dev = bp->dev;
6030 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6031 struct netdev_hw_addr *ha;
6032 int off = 0;
6033
6034 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6035 return true;
6036
6037 netdev_for_each_uc_addr(ha, dev) {
6038 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6039 return true;
6040
6041 off += ETH_ALEN;
6042 }
6043 return false;
6044}
6045
6046static void bnxt_set_rx_mode(struct net_device *dev)
6047{
6048 struct bnxt *bp = netdev_priv(dev);
6049 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6050 u32 mask = vnic->rx_mask;
6051 bool mc_update = false;
6052 bool uc_update;
6053
6054 if (!netif_running(dev))
6055 return;
6056
6057 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6058 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6059 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6060
Michael Chan17c71ac2016-07-01 18:46:27 -04006061 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006062 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6063
6064 uc_update = bnxt_uc_list_updated(bp);
6065
6066 if (dev->flags & IFF_ALLMULTI) {
6067 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6068 vnic->mc_list_count = 0;
6069 } else {
6070 mc_update = bnxt_mc_list_updated(bp, &mask);
6071 }
6072
6073 if (mask != vnic->rx_mask || uc_update || mc_update) {
6074 vnic->rx_mask = mask;
6075
6076 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6077 schedule_work(&bp->sp_task);
6078 }
6079}
6080
Michael Chanb664f002015-12-02 01:54:08 -05006081static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006082{
6083 struct net_device *dev = bp->dev;
6084 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6085 struct netdev_hw_addr *ha;
6086 int i, off = 0, rc;
6087 bool uc_update;
6088
6089 netif_addr_lock_bh(dev);
6090 uc_update = bnxt_uc_list_updated(bp);
6091 netif_addr_unlock_bh(dev);
6092
6093 if (!uc_update)
6094 goto skip_uc;
6095
6096 mutex_lock(&bp->hwrm_cmd_lock);
6097 for (i = 1; i < vnic->uc_filter_count; i++) {
6098 struct hwrm_cfa_l2_filter_free_input req = {0};
6099
6100 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6101 -1);
6102
6103 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6104
6105 rc = _hwrm_send_message(bp, &req, sizeof(req),
6106 HWRM_CMD_TIMEOUT);
6107 }
6108 mutex_unlock(&bp->hwrm_cmd_lock);
6109
6110 vnic->uc_filter_count = 1;
6111
6112 netif_addr_lock_bh(dev);
6113 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6114 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6115 } else {
6116 netdev_for_each_uc_addr(ha, dev) {
6117 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6118 off += ETH_ALEN;
6119 vnic->uc_filter_count++;
6120 }
6121 }
6122 netif_addr_unlock_bh(dev);
6123
6124 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6125 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6126 if (rc) {
6127 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6128 rc);
6129 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006130 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006131 }
6132 }
6133
6134skip_uc:
6135 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6136 if (rc)
6137 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6138 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006139
6140 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006141}
6142
Michael Chan8079e8f2016-12-29 12:13:37 -05006143/* If the chip and firmware supports RFS */
6144static bool bnxt_rfs_supported(struct bnxt *bp)
6145{
6146 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6147 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006148 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6149 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006150 return false;
6151}
6152
6153/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006154static bool bnxt_rfs_capable(struct bnxt *bp)
6155{
6156#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006157 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006158
6159 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
6160 return false;
6161
6162 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006163 max_vnics = bnxt_get_max_func_vnics(bp);
6164 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006165
6166 /* RSS contexts not a limiting factor */
6167 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6168 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006169 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006170 netdev_warn(bp->dev,
6171 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006172 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006173 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006174 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006175
6176 return true;
6177#else
6178 return false;
6179#endif
6180}
6181
Michael Chanc0c050c2015-10-22 16:01:17 -04006182static netdev_features_t bnxt_fix_features(struct net_device *dev,
6183 netdev_features_t features)
6184{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006185 struct bnxt *bp = netdev_priv(dev);
6186
Vasundhara Volama2304902016-07-25 12:33:36 -04006187 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006188 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006189
6190 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6191 * turned on or off together.
6192 */
6193 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6194 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6195 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6196 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6197 NETIF_F_HW_VLAN_STAG_RX);
6198 else
6199 features |= NETIF_F_HW_VLAN_CTAG_RX |
6200 NETIF_F_HW_VLAN_STAG_RX;
6201 }
Michael Chancf6645f2016-06-13 02:25:28 -04006202#ifdef CONFIG_BNXT_SRIOV
6203 if (BNXT_VF(bp)) {
6204 if (bp->vf.vlan) {
6205 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6206 NETIF_F_HW_VLAN_STAG_RX);
6207 }
6208 }
6209#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006210 return features;
6211}
6212
6213static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6214{
6215 struct bnxt *bp = netdev_priv(dev);
6216 u32 flags = bp->flags;
6217 u32 changes;
6218 int rc = 0;
6219 bool re_init = false;
6220 bool update_tpa = false;
6221
6222 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006223 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006224 flags |= BNXT_FLAG_GRO;
6225 if (features & NETIF_F_LRO)
6226 flags |= BNXT_FLAG_LRO;
6227
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006228 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6229 flags &= ~BNXT_FLAG_TPA;
6230
Michael Chanc0c050c2015-10-22 16:01:17 -04006231 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6232 flags |= BNXT_FLAG_STRIP_VLAN;
6233
6234 if (features & NETIF_F_NTUPLE)
6235 flags |= BNXT_FLAG_RFS;
6236
6237 changes = flags ^ bp->flags;
6238 if (changes & BNXT_FLAG_TPA) {
6239 update_tpa = true;
6240 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6241 (flags & BNXT_FLAG_TPA) == 0)
6242 re_init = true;
6243 }
6244
6245 if (changes & ~BNXT_FLAG_TPA)
6246 re_init = true;
6247
6248 if (flags != bp->flags) {
6249 u32 old_flags = bp->flags;
6250
6251 bp->flags = flags;
6252
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006253 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006254 if (update_tpa)
6255 bnxt_set_ring_params(bp);
6256 return rc;
6257 }
6258
6259 if (re_init) {
6260 bnxt_close_nic(bp, false, false);
6261 if (update_tpa)
6262 bnxt_set_ring_params(bp);
6263
6264 return bnxt_open_nic(bp, false, false);
6265 }
6266 if (update_tpa) {
6267 rc = bnxt_set_tpa(bp,
6268 (flags & BNXT_FLAG_TPA) ?
6269 true : false);
6270 if (rc)
6271 bp->flags = old_flags;
6272 }
6273 }
6274 return rc;
6275}
6276
Michael Chan9f554592016-01-02 23:44:58 -05006277static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6278{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006279 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006280 int i = bnapi->index;
6281
Michael Chan3b2b7d92016-01-02 23:45:00 -05006282 if (!txr)
6283 return;
6284
Michael Chan9f554592016-01-02 23:44:58 -05006285 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6286 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6287 txr->tx_cons);
6288}
6289
6290static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6291{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006292 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006293 int i = bnapi->index;
6294
Michael Chan3b2b7d92016-01-02 23:45:00 -05006295 if (!rxr)
6296 return;
6297
Michael Chan9f554592016-01-02 23:44:58 -05006298 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",
6299 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6300 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6301 rxr->rx_sw_agg_prod);
6302}
6303
6304static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6305{
6306 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6307 int i = bnapi->index;
6308
6309 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6310 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6311}
6312
Michael Chanc0c050c2015-10-22 16:01:17 -04006313static void bnxt_dbg_dump_states(struct bnxt *bp)
6314{
6315 int i;
6316 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006317
6318 for (i = 0; i < bp->cp_nr_rings; i++) {
6319 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006320 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006321 bnxt_dump_tx_sw_state(bnapi);
6322 bnxt_dump_rx_sw_state(bnapi);
6323 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006324 }
6325 }
6326}
6327
Michael Chan6988bd92016-06-13 02:25:29 -04006328static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006329{
Michael Chan6988bd92016-06-13 02:25:29 -04006330 if (!silent)
6331 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006332 if (netif_running(bp->dev)) {
6333 bnxt_close_nic(bp, false, false);
6334 bnxt_open_nic(bp, false, false);
6335 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006336}
6337
6338static void bnxt_tx_timeout(struct net_device *dev)
6339{
6340 struct bnxt *bp = netdev_priv(dev);
6341
6342 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6343 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6344 schedule_work(&bp->sp_task);
6345}
6346
6347#ifdef CONFIG_NET_POLL_CONTROLLER
6348static void bnxt_poll_controller(struct net_device *dev)
6349{
6350 struct bnxt *bp = netdev_priv(dev);
6351 int i;
6352
6353 for (i = 0; i < bp->cp_nr_rings; i++) {
6354 struct bnxt_irq *irq = &bp->irq_tbl[i];
6355
6356 disable_irq(irq->vector);
6357 irq->handler(irq->vector, bp->bnapi[i]);
6358 enable_irq(irq->vector);
6359 }
6360}
6361#endif
6362
6363static void bnxt_timer(unsigned long data)
6364{
6365 struct bnxt *bp = (struct bnxt *)data;
6366 struct net_device *dev = bp->dev;
6367
6368 if (!netif_running(dev))
6369 return;
6370
6371 if (atomic_read(&bp->intr_sem) != 0)
6372 goto bnxt_restart_timer;
6373
Michael Chan3bdf56c2016-03-07 15:38:45 -05006374 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6375 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6376 schedule_work(&bp->sp_task);
6377 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006378bnxt_restart_timer:
6379 mod_timer(&bp->timer, jiffies + bp->current_interval);
6380}
6381
Michael Chana551ee92017-01-25 02:55:07 -05006382static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006383{
Michael Chana551ee92017-01-25 02:55:07 -05006384 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6385 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006386 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6387 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6388 */
6389 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6390 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006391}
6392
6393static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6394{
Michael Chan6988bd92016-06-13 02:25:29 -04006395 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6396 rtnl_unlock();
6397}
6398
Michael Chana551ee92017-01-25 02:55:07 -05006399/* Only called from bnxt_sp_task() */
6400static void bnxt_reset(struct bnxt *bp, bool silent)
6401{
6402 bnxt_rtnl_lock_sp(bp);
6403 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6404 bnxt_reset_task(bp, silent);
6405 bnxt_rtnl_unlock_sp(bp);
6406}
6407
Michael Chanc0c050c2015-10-22 16:01:17 -04006408static void bnxt_cfg_ntp_filters(struct bnxt *);
6409
6410static void bnxt_sp_task(struct work_struct *work)
6411{
6412 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006413
Michael Chan4cebdce2015-12-09 19:35:43 -05006414 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6415 smp_mb__after_atomic();
6416 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6417 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006418 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006419 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006420
6421 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6422 bnxt_cfg_rx_mode(bp);
6423
6424 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6425 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006426 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6427 bnxt_hwrm_exec_fwd_req(bp);
6428 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6429 bnxt_hwrm_tunnel_dst_port_alloc(
6430 bp, bp->vxlan_port,
6431 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6432 }
6433 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6434 bnxt_hwrm_tunnel_dst_port_free(
6435 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6436 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006437 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6438 bnxt_hwrm_tunnel_dst_port_alloc(
6439 bp, bp->nge_port,
6440 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6441 }
6442 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6443 bnxt_hwrm_tunnel_dst_port_free(
6444 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6445 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006446 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6447 bnxt_hwrm_port_qstats(bp);
6448
Michael Chana551ee92017-01-25 02:55:07 -05006449 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6450 * must be the last functions to be called before exiting.
6451 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006452 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6453 int rc = 0;
6454
6455 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6456 &bp->sp_event))
6457 bnxt_hwrm_phy_qcaps(bp);
6458
6459 bnxt_rtnl_lock_sp(bp);
6460 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6461 rc = bnxt_update_link(bp, true);
6462 bnxt_rtnl_unlock_sp(bp);
6463 if (rc)
6464 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6465 rc);
6466 }
Michael Chan90c694b2017-01-25 02:55:09 -05006467 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6468 bnxt_rtnl_lock_sp(bp);
6469 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6470 bnxt_get_port_module_status(bp);
6471 bnxt_rtnl_unlock_sp(bp);
6472 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006473 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6474 bnxt_reset(bp, false);
6475
6476 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6477 bnxt_reset(bp, true);
6478
Michael Chanc0c050c2015-10-22 16:01:17 -04006479 smp_mb__before_atomic();
6480 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6481}
6482
6483static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6484{
6485 int rc;
6486 struct bnxt *bp = netdev_priv(dev);
6487
6488 SET_NETDEV_DEV(dev, &pdev->dev);
6489
6490 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6491 rc = pci_enable_device(pdev);
6492 if (rc) {
6493 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6494 goto init_err;
6495 }
6496
6497 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6498 dev_err(&pdev->dev,
6499 "Cannot find PCI device base address, aborting\n");
6500 rc = -ENODEV;
6501 goto init_err_disable;
6502 }
6503
6504 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6505 if (rc) {
6506 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6507 goto init_err_disable;
6508 }
6509
6510 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6511 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6512 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6513 goto init_err_disable;
6514 }
6515
6516 pci_set_master(pdev);
6517
6518 bp->dev = dev;
6519 bp->pdev = pdev;
6520
6521 bp->bar0 = pci_ioremap_bar(pdev, 0);
6522 if (!bp->bar0) {
6523 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6524 rc = -ENOMEM;
6525 goto init_err_release;
6526 }
6527
6528 bp->bar1 = pci_ioremap_bar(pdev, 2);
6529 if (!bp->bar1) {
6530 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6531 rc = -ENOMEM;
6532 goto init_err_release;
6533 }
6534
6535 bp->bar2 = pci_ioremap_bar(pdev, 4);
6536 if (!bp->bar2) {
6537 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6538 rc = -ENOMEM;
6539 goto init_err_release;
6540 }
6541
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006542 pci_enable_pcie_error_reporting(pdev);
6543
Michael Chanc0c050c2015-10-22 16:01:17 -04006544 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6545
6546 spin_lock_init(&bp->ntp_fltr_lock);
6547
6548 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6549 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6550
Michael Chandfb5b892016-02-26 04:00:01 -05006551 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006552 bp->rx_coal_ticks = 12;
6553 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006554 bp->rx_coal_ticks_irq = 1;
6555 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006556
Michael Chandfc9c942016-02-26 04:00:03 -05006557 bp->tx_coal_ticks = 25;
6558 bp->tx_coal_bufs = 30;
6559 bp->tx_coal_ticks_irq = 2;
6560 bp->tx_coal_bufs_irq = 2;
6561
Michael Chan51f30782016-07-01 18:46:29 -04006562 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6563
Michael Chanc0c050c2015-10-22 16:01:17 -04006564 init_timer(&bp->timer);
6565 bp->timer.data = (unsigned long)bp;
6566 bp->timer.function = bnxt_timer;
6567 bp->current_interval = BNXT_TIMER_INTERVAL;
6568
Michael Chancaefe522015-12-09 19:35:42 -05006569 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006570
6571 return 0;
6572
6573init_err_release:
6574 if (bp->bar2) {
6575 pci_iounmap(pdev, bp->bar2);
6576 bp->bar2 = NULL;
6577 }
6578
6579 if (bp->bar1) {
6580 pci_iounmap(pdev, bp->bar1);
6581 bp->bar1 = NULL;
6582 }
6583
6584 if (bp->bar0) {
6585 pci_iounmap(pdev, bp->bar0);
6586 bp->bar0 = NULL;
6587 }
6588
6589 pci_release_regions(pdev);
6590
6591init_err_disable:
6592 pci_disable_device(pdev);
6593
6594init_err:
6595 return rc;
6596}
6597
6598/* rtnl_lock held */
6599static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6600{
6601 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006602 struct bnxt *bp = netdev_priv(dev);
6603 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006604
6605 if (!is_valid_ether_addr(addr->sa_data))
6606 return -EADDRNOTAVAIL;
6607
Michael Chan84c33dd2016-04-11 04:11:13 -04006608 rc = bnxt_approve_mac(bp, addr->sa_data);
6609 if (rc)
6610 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006611
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006612 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6613 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006614
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006615 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6616 if (netif_running(dev)) {
6617 bnxt_close_nic(bp, false, false);
6618 rc = bnxt_open_nic(bp, false, false);
6619 }
6620
6621 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006622}
6623
6624/* rtnl_lock held */
6625static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6626{
6627 struct bnxt *bp = netdev_priv(dev);
6628
Michael Chanc0c050c2015-10-22 16:01:17 -04006629 if (netif_running(dev))
6630 bnxt_close_nic(bp, false, false);
6631
6632 dev->mtu = new_mtu;
6633 bnxt_set_ring_params(bp);
6634
6635 if (netif_running(dev))
6636 return bnxt_open_nic(bp, false, false);
6637
6638 return 0;
6639}
6640
Michael Chanc5e3deb2016-12-02 21:17:15 -05006641int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006642{
6643 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006644 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006645
Michael Chanc0c050c2015-10-22 16:01:17 -04006646 if (tc > bp->max_tc) {
6647 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6648 tc, bp->max_tc);
6649 return -EINVAL;
6650 }
6651
6652 if (netdev_get_num_tc(dev) == tc)
6653 return 0;
6654
Michael Chan3ffb6a32016-11-11 00:11:42 -05006655 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6656 sh = true;
6657
Michael Chanc0c050c2015-10-22 16:01:17 -04006658 if (tc) {
Michael Chan391be5c2016-12-29 12:13:41 -05006659 int max_rx_rings, max_tx_rings, req_tx_rings, rsv_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006660
Michael Chan391be5c2016-12-29 12:13:41 -05006661 req_tx_rings = bp->tx_nr_rings_per_tc * tc;
Michael Chan01657bc2016-01-02 23:45:03 -05006662 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan391be5c2016-12-29 12:13:41 -05006663 if (rc || req_tx_rings > max_tx_rings)
6664 return -ENOMEM;
6665
6666 rsv_tx_rings = req_tx_rings;
6667 if (bnxt_hwrm_reserve_tx_rings(bp, &rsv_tx_rings) ||
6668 rsv_tx_rings < req_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006669 return -ENOMEM;
6670 }
6671
6672 /* Needs to close the device and do hw resource re-allocations */
6673 if (netif_running(bp->dev))
6674 bnxt_close_nic(bp, true, false);
6675
6676 if (tc) {
6677 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6678 netdev_set_num_tc(dev, tc);
6679 } else {
6680 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6681 netdev_reset_tc(dev);
6682 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006683 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6684 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006685 bp->num_stat_ctxs = bp->cp_nr_rings;
6686
6687 if (netif_running(bp->dev))
6688 return bnxt_open_nic(bp, true, false);
6689
6690 return 0;
6691}
6692
Michael Chanc5e3deb2016-12-02 21:17:15 -05006693static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6694 struct tc_to_netdev *ntc)
6695{
6696 if (ntc->type != TC_SETUP_MQPRIO)
6697 return -EINVAL;
6698
6699 return bnxt_setup_mq_tc(dev, ntc->tc);
6700}
6701
Michael Chanc0c050c2015-10-22 16:01:17 -04006702#ifdef CONFIG_RFS_ACCEL
6703static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6704 struct bnxt_ntuple_filter *f2)
6705{
6706 struct flow_keys *keys1 = &f1->fkeys;
6707 struct flow_keys *keys2 = &f2->fkeys;
6708
6709 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6710 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6711 keys1->ports.ports == keys2->ports.ports &&
6712 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6713 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006714 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6715 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006716 return true;
6717
6718 return false;
6719}
6720
6721static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6722 u16 rxq_index, u32 flow_id)
6723{
6724 struct bnxt *bp = netdev_priv(dev);
6725 struct bnxt_ntuple_filter *fltr, *new_fltr;
6726 struct flow_keys *fkeys;
6727 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006728 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006729 struct hlist_head *head;
6730
6731 if (skb->encapsulation)
6732 return -EPROTONOSUPPORT;
6733
Michael Chana54c4d72016-07-25 12:33:35 -04006734 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6735 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6736 int off = 0, j;
6737
6738 netif_addr_lock_bh(dev);
6739 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6740 if (ether_addr_equal(eth->h_dest,
6741 vnic->uc_list + off)) {
6742 l2_idx = j + 1;
6743 break;
6744 }
6745 }
6746 netif_addr_unlock_bh(dev);
6747 if (!l2_idx)
6748 return -EINVAL;
6749 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006750 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6751 if (!new_fltr)
6752 return -ENOMEM;
6753
6754 fkeys = &new_fltr->fkeys;
6755 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6756 rc = -EPROTONOSUPPORT;
6757 goto err_free;
6758 }
6759
Michael Chandda0e742016-12-29 12:13:40 -05006760 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
6761 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04006762 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6763 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6764 rc = -EPROTONOSUPPORT;
6765 goto err_free;
6766 }
Michael Chandda0e742016-12-29 12:13:40 -05006767 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
6768 bp->hwrm_spec_code < 0x10601) {
6769 rc = -EPROTONOSUPPORT;
6770 goto err_free;
6771 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006772
Michael Chana54c4d72016-07-25 12:33:35 -04006773 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006774 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6775
6776 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6777 head = &bp->ntp_fltr_hash_tbl[idx];
6778 rcu_read_lock();
6779 hlist_for_each_entry_rcu(fltr, head, hash) {
6780 if (bnxt_fltr_match(fltr, new_fltr)) {
6781 rcu_read_unlock();
6782 rc = 0;
6783 goto err_free;
6784 }
6785 }
6786 rcu_read_unlock();
6787
6788 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006789 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6790 BNXT_NTP_FLTR_MAX_FLTR, 0);
6791 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006792 spin_unlock_bh(&bp->ntp_fltr_lock);
6793 rc = -ENOMEM;
6794 goto err_free;
6795 }
6796
Michael Chan84e86b92015-11-05 16:25:50 -05006797 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006798 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006799 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006800 new_fltr->rxq = rxq_index;
6801 hlist_add_head_rcu(&new_fltr->hash, head);
6802 bp->ntp_fltr_count++;
6803 spin_unlock_bh(&bp->ntp_fltr_lock);
6804
6805 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6806 schedule_work(&bp->sp_task);
6807
6808 return new_fltr->sw_id;
6809
6810err_free:
6811 kfree(new_fltr);
6812 return rc;
6813}
6814
6815static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6816{
6817 int i;
6818
6819 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6820 struct hlist_head *head;
6821 struct hlist_node *tmp;
6822 struct bnxt_ntuple_filter *fltr;
6823 int rc;
6824
6825 head = &bp->ntp_fltr_hash_tbl[i];
6826 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6827 bool del = false;
6828
6829 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6830 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6831 fltr->flow_id,
6832 fltr->sw_id)) {
6833 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6834 fltr);
6835 del = true;
6836 }
6837 } else {
6838 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6839 fltr);
6840 if (rc)
6841 del = true;
6842 else
6843 set_bit(BNXT_FLTR_VALID, &fltr->state);
6844 }
6845
6846 if (del) {
6847 spin_lock_bh(&bp->ntp_fltr_lock);
6848 hlist_del_rcu(&fltr->hash);
6849 bp->ntp_fltr_count--;
6850 spin_unlock_bh(&bp->ntp_fltr_lock);
6851 synchronize_rcu();
6852 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6853 kfree(fltr);
6854 }
6855 }
6856 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006857 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6858 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006859}
6860
6861#else
6862
6863static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6864{
6865}
6866
6867#endif /* CONFIG_RFS_ACCEL */
6868
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006869static void bnxt_udp_tunnel_add(struct net_device *dev,
6870 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006871{
6872 struct bnxt *bp = netdev_priv(dev);
6873
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006874 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6875 return;
6876
Michael Chanc0c050c2015-10-22 16:01:17 -04006877 if (!netif_running(dev))
6878 return;
6879
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006880 switch (ti->type) {
6881 case UDP_TUNNEL_TYPE_VXLAN:
6882 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6883 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006884
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006885 bp->vxlan_port_cnt++;
6886 if (bp->vxlan_port_cnt == 1) {
6887 bp->vxlan_port = ti->port;
6888 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006889 schedule_work(&bp->sp_task);
6890 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006891 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006892 case UDP_TUNNEL_TYPE_GENEVE:
6893 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6894 return;
6895
6896 bp->nge_port_cnt++;
6897 if (bp->nge_port_cnt == 1) {
6898 bp->nge_port = ti->port;
6899 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6900 }
6901 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006902 default:
6903 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006904 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006905
6906 schedule_work(&bp->sp_task);
6907}
6908
6909static void bnxt_udp_tunnel_del(struct net_device *dev,
6910 struct udp_tunnel_info *ti)
6911{
6912 struct bnxt *bp = netdev_priv(dev);
6913
6914 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6915 return;
6916
6917 if (!netif_running(dev))
6918 return;
6919
6920 switch (ti->type) {
6921 case UDP_TUNNEL_TYPE_VXLAN:
6922 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6923 return;
6924 bp->vxlan_port_cnt--;
6925
6926 if (bp->vxlan_port_cnt != 0)
6927 return;
6928
6929 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6930 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006931 case UDP_TUNNEL_TYPE_GENEVE:
6932 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6933 return;
6934 bp->nge_port_cnt--;
6935
6936 if (bp->nge_port_cnt != 0)
6937 return;
6938
6939 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6940 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006941 default:
6942 return;
6943 }
6944
6945 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006946}
6947
6948static const struct net_device_ops bnxt_netdev_ops = {
6949 .ndo_open = bnxt_open,
6950 .ndo_start_xmit = bnxt_start_xmit,
6951 .ndo_stop = bnxt_close,
6952 .ndo_get_stats64 = bnxt_get_stats64,
6953 .ndo_set_rx_mode = bnxt_set_rx_mode,
6954 .ndo_do_ioctl = bnxt_ioctl,
6955 .ndo_validate_addr = eth_validate_addr,
6956 .ndo_set_mac_address = bnxt_change_mac_addr,
6957 .ndo_change_mtu = bnxt_change_mtu,
6958 .ndo_fix_features = bnxt_fix_features,
6959 .ndo_set_features = bnxt_set_features,
6960 .ndo_tx_timeout = bnxt_tx_timeout,
6961#ifdef CONFIG_BNXT_SRIOV
6962 .ndo_get_vf_config = bnxt_get_vf_config,
6963 .ndo_set_vf_mac = bnxt_set_vf_mac,
6964 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6965 .ndo_set_vf_rate = bnxt_set_vf_bw,
6966 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6967 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6968#endif
6969#ifdef CONFIG_NET_POLL_CONTROLLER
6970 .ndo_poll_controller = bnxt_poll_controller,
6971#endif
6972 .ndo_setup_tc = bnxt_setup_tc,
6973#ifdef CONFIG_RFS_ACCEL
6974 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6975#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006976 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6977 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006978};
6979
6980static void bnxt_remove_one(struct pci_dev *pdev)
6981{
6982 struct net_device *dev = pci_get_drvdata(pdev);
6983 struct bnxt *bp = netdev_priv(dev);
6984
6985 if (BNXT_PF(bp))
6986 bnxt_sriov_disable(bp);
6987
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006988 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006989 unregister_netdev(dev);
6990 cancel_work_sync(&bp->sp_task);
6991 bp->sp_event = 0;
6992
Michael Chan78095922016-12-07 00:26:16 -05006993 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006994 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006995 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05006996 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006997 pci_iounmap(pdev, bp->bar2);
6998 pci_iounmap(pdev, bp->bar1);
6999 pci_iounmap(pdev, bp->bar0);
Michael Chana588e452016-12-07 00:26:21 -05007000 kfree(bp->edev);
7001 bp->edev = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04007002 free_netdev(dev);
7003
7004 pci_release_regions(pdev);
7005 pci_disable_device(pdev);
7006}
7007
7008static int bnxt_probe_phy(struct bnxt *bp)
7009{
7010 int rc = 0;
7011 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007012
Michael Chan170ce012016-04-05 14:08:57 -04007013 rc = bnxt_hwrm_phy_qcaps(bp);
7014 if (rc) {
7015 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7016 rc);
7017 return rc;
7018 }
7019
Michael Chanc0c050c2015-10-22 16:01:17 -04007020 rc = bnxt_update_link(bp, false);
7021 if (rc) {
7022 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7023 rc);
7024 return rc;
7025 }
7026
Michael Chan93ed8112016-06-13 02:25:37 -04007027 /* Older firmware does not have supported_auto_speeds, so assume
7028 * that all supported speeds can be autonegotiated.
7029 */
7030 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7031 link_info->support_auto_speeds = link_info->support_speeds;
7032
Michael Chanc0c050c2015-10-22 16:01:17 -04007033 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007034 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007035 link_info->autoneg = BNXT_AUTONEG_SPEED;
7036 if (bp->hwrm_spec_code >= 0x10201) {
7037 if (link_info->auto_pause_setting &
7038 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7039 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7040 } else {
7041 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7042 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007043 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007044 } else {
7045 link_info->req_link_speed = link_info->force_link_speed;
7046 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007047 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007048 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7049 link_info->req_flow_ctrl =
7050 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7051 else
7052 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007053 return rc;
7054}
7055
7056static int bnxt_get_max_irq(struct pci_dev *pdev)
7057{
7058 u16 ctrl;
7059
7060 if (!pdev->msix_cap)
7061 return 1;
7062
7063 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7064 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7065}
7066
Michael Chan6e6c5a52016-01-02 23:45:02 -05007067static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7068 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007069{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007070 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007071
Michael Chan379a80a2015-10-23 15:06:19 -04007072#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007073 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007074 *max_tx = bp->vf.max_tx_rings;
7075 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007076 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7077 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007078 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007079 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007080#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007081 {
7082 *max_tx = bp->pf.max_tx_rings;
7083 *max_rx = bp->pf.max_rx_rings;
7084 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7085 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7086 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007087 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007088 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7089 *max_cp -= 1;
7090 *max_rx -= 2;
7091 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007092 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7093 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007094 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007095}
7096
7097int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7098{
7099 int rx, tx, cp;
7100
7101 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7102 if (!rx || !tx || !cp)
7103 return -ENOMEM;
7104
7105 *max_rx = rx;
7106 *max_tx = tx;
7107 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7108}
7109
Michael Chane4060d32016-12-07 00:26:19 -05007110static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7111 bool shared)
7112{
7113 int rc;
7114
7115 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007116 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7117 /* Not enough rings, try disabling agg rings. */
7118 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7119 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7120 if (rc)
7121 return rc;
7122 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7123 bp->dev->hw_features &= ~NETIF_F_LRO;
7124 bp->dev->features &= ~NETIF_F_LRO;
7125 bnxt_set_ring_params(bp);
7126 }
Michael Chane4060d32016-12-07 00:26:19 -05007127
7128 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7129 int max_cp, max_stat, max_irq;
7130
7131 /* Reserve minimum resources for RoCE */
7132 max_cp = bnxt_get_max_func_cp_rings(bp);
7133 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7134 max_irq = bnxt_get_max_func_irqs(bp);
7135 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7136 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7137 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7138 return 0;
7139
7140 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7141 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7142 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7143 max_cp = min_t(int, max_cp, max_irq);
7144 max_cp = min_t(int, max_cp, max_stat);
7145 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7146 if (rc)
7147 rc = 0;
7148 }
7149 return rc;
7150}
7151
Michael Chan6e6c5a52016-01-02 23:45:02 -05007152static int bnxt_set_dflt_rings(struct bnxt *bp)
7153{
7154 int dflt_rings, max_rx_rings, max_tx_rings, rc;
7155 bool sh = true;
7156
7157 if (sh)
7158 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7159 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007160 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007161 if (rc)
7162 return rc;
7163 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7164 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007165
7166 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7167 if (rc)
7168 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7169
Michael Chan6e6c5a52016-01-02 23:45:02 -05007170 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7171 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7172 bp->tx_nr_rings + bp->rx_nr_rings;
7173 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007174 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7175 bp->rx_nr_rings++;
7176 bp->cp_nr_rings++;
7177 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007178 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007179}
7180
Michael Chan7b08f662016-12-07 00:26:18 -05007181void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7182{
7183 ASSERT_RTNL();
7184 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007185 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007186}
7187
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007188static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7189{
7190 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7191 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7192
7193 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7194 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7195 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7196 else
7197 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7198 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7199 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7200 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7201 "Unknown", width);
7202}
7203
Michael Chanc0c050c2015-10-22 16:01:17 -04007204static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7205{
7206 static int version_printed;
7207 struct net_device *dev;
7208 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007209 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007210
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007211 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
7212 return -ENODEV;
7213
Michael Chanc0c050c2015-10-22 16:01:17 -04007214 if (version_printed++ == 0)
7215 pr_info("%s", version);
7216
7217 max_irqs = bnxt_get_max_irq(pdev);
7218 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7219 if (!dev)
7220 return -ENOMEM;
7221
7222 bp = netdev_priv(dev);
7223
7224 if (bnxt_vf_pciid(ent->driver_data))
7225 bp->flags |= BNXT_FLAG_VF;
7226
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007227 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007228 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007229
7230 rc = bnxt_init_board(pdev, dev);
7231 if (rc < 0)
7232 goto init_err_free;
7233
7234 dev->netdev_ops = &bnxt_netdev_ops;
7235 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7236 dev->ethtool_ops = &bnxt_ethtool_ops;
7237
7238 pci_set_drvdata(pdev, dev);
7239
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007240 rc = bnxt_alloc_hwrm_resources(bp);
7241 if (rc)
7242 goto init_err;
7243
7244 mutex_init(&bp->hwrm_cmd_lock);
7245 rc = bnxt_hwrm_ver_get(bp);
7246 if (rc)
7247 goto init_err;
7248
Rob Swindell5ac67d82016-09-19 03:58:03 -04007249 bnxt_hwrm_fw_set_time(bp);
7250
Michael Chanc0c050c2015-10-22 16:01:17 -04007251 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7252 NETIF_F_TSO | NETIF_F_TSO6 |
7253 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007254 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007255 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7256 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007257 NETIF_F_RXCSUM | NETIF_F_GRO;
7258
7259 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7260 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007261
Michael Chanc0c050c2015-10-22 16:01:17 -04007262 dev->hw_enc_features =
7263 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7264 NETIF_F_TSO | NETIF_F_TSO6 |
7265 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007266 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007267 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007268 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7269 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007270 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7271 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7272 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7273 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7274 dev->priv_flags |= IFF_UNICAST_FLT;
7275
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007276 /* MTU range: 60 - 9500 */
7277 dev->min_mtu = ETH_ZLEN;
7278 dev->max_mtu = 9500;
7279
Michael Chan7df4ae92016-12-02 21:17:17 -05007280 bnxt_dcb_init(bp);
7281
Michael Chanc0c050c2015-10-22 16:01:17 -04007282#ifdef CONFIG_BNXT_SRIOV
7283 init_waitqueue_head(&bp->sriov_cfg_wait);
7284#endif
Michael Chan309369c2016-06-13 02:25:34 -04007285 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007286 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7287 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007288
Michael Chanc0c050c2015-10-22 16:01:17 -04007289 rc = bnxt_hwrm_func_drv_rgtr(bp);
7290 if (rc)
7291 goto init_err;
7292
Michael Chana1653b12016-12-07 00:26:20 -05007293 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7294 if (rc)
7295 goto init_err;
7296
Michael Chana588e452016-12-07 00:26:21 -05007297 bp->ulp_probe = bnxt_ulp_probe;
7298
Michael Chanc0c050c2015-10-22 16:01:17 -04007299 /* Get the MAX capabilities for this function */
7300 rc = bnxt_hwrm_func_qcaps(bp);
7301 if (rc) {
7302 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7303 rc);
7304 rc = -1;
7305 goto init_err;
7306 }
7307
7308 rc = bnxt_hwrm_queue_qportcfg(bp);
7309 if (rc) {
7310 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7311 rc);
7312 rc = -1;
7313 goto init_err;
7314 }
7315
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007316 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007317 bnxt_hwrm_port_led_qcaps(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007318
Michael Chan6bb19472017-02-06 16:55:32 -05007319 bnxt_set_rx_skb_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007320 bnxt_set_tpa_flags(bp);
7321 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007322 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007323 rc = bnxt_set_dflt_rings(bp);
7324 if (rc) {
7325 netdev_err(bp->dev, "Not enough rings available.\n");
7326 rc = -ENOMEM;
7327 goto init_err;
7328 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007329
Michael Chan87da7f72016-11-16 21:13:09 -05007330 /* Default RSS hash cfg. */
7331 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7332 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7333 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7334 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7335 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7336 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7337 bp->hwrm_spec_code >= 0x10501) {
7338 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7339 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7340 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7341 }
7342
Michael Chan8fdefd62016-12-29 12:13:36 -05007343 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007344 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007345 dev->hw_features |= NETIF_F_NTUPLE;
7346 if (bnxt_rfs_capable(bp)) {
7347 bp->flags |= BNXT_FLAG_RFS;
7348 dev->features |= NETIF_F_NTUPLE;
7349 }
7350 }
7351
Michael Chanc0c050c2015-10-22 16:01:17 -04007352 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7353 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7354
7355 rc = bnxt_probe_phy(bp);
7356 if (rc)
7357 goto init_err;
7358
Michael Chanaa8ed022016-12-07 00:26:17 -05007359 rc = bnxt_hwrm_func_reset(bp);
7360 if (rc)
7361 goto init_err;
7362
Michael Chan78095922016-12-07 00:26:16 -05007363 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007364 if (rc)
7365 goto init_err;
7366
Michael Chan78095922016-12-07 00:26:16 -05007367 rc = register_netdev(dev);
7368 if (rc)
7369 goto init_err_clr_int;
7370
Michael Chanc0c050c2015-10-22 16:01:17 -04007371 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7372 board_info[ent->driver_data].name,
7373 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7374
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007375 bnxt_parse_log_pcie_link(bp);
7376
Michael Chanc0c050c2015-10-22 16:01:17 -04007377 return 0;
7378
Michael Chan78095922016-12-07 00:26:16 -05007379init_err_clr_int:
7380 bnxt_clear_int_mode(bp);
7381
Michael Chanc0c050c2015-10-22 16:01:17 -04007382init_err:
7383 pci_iounmap(pdev, bp->bar0);
7384 pci_release_regions(pdev);
7385 pci_disable_device(pdev);
7386
7387init_err_free:
7388 free_netdev(dev);
7389 return rc;
7390}
7391
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007392/**
7393 * bnxt_io_error_detected - called when PCI error is detected
7394 * @pdev: Pointer to PCI device
7395 * @state: The current pci connection state
7396 *
7397 * This function is called after a PCI bus error affecting
7398 * this device has been detected.
7399 */
7400static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7401 pci_channel_state_t state)
7402{
7403 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007404 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007405
7406 netdev_info(netdev, "PCI I/O error detected\n");
7407
7408 rtnl_lock();
7409 netif_device_detach(netdev);
7410
Michael Chana588e452016-12-07 00:26:21 -05007411 bnxt_ulp_stop(bp);
7412
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007413 if (state == pci_channel_io_perm_failure) {
7414 rtnl_unlock();
7415 return PCI_ERS_RESULT_DISCONNECT;
7416 }
7417
7418 if (netif_running(netdev))
7419 bnxt_close(netdev);
7420
7421 pci_disable_device(pdev);
7422 rtnl_unlock();
7423
7424 /* Request a slot slot reset. */
7425 return PCI_ERS_RESULT_NEED_RESET;
7426}
7427
7428/**
7429 * bnxt_io_slot_reset - called after the pci bus has been reset.
7430 * @pdev: Pointer to PCI device
7431 *
7432 * Restart the card from scratch, as if from a cold-boot.
7433 * At this point, the card has exprienced a hard reset,
7434 * followed by fixups by BIOS, and has its config space
7435 * set up identically to what it was at cold boot.
7436 */
7437static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7438{
7439 struct net_device *netdev = pci_get_drvdata(pdev);
7440 struct bnxt *bp = netdev_priv(netdev);
7441 int err = 0;
7442 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7443
7444 netdev_info(bp->dev, "PCI Slot Reset\n");
7445
7446 rtnl_lock();
7447
7448 if (pci_enable_device(pdev)) {
7449 dev_err(&pdev->dev,
7450 "Cannot re-enable PCI device after reset.\n");
7451 } else {
7452 pci_set_master(pdev);
7453
Michael Chanaa8ed022016-12-07 00:26:17 -05007454 err = bnxt_hwrm_func_reset(bp);
7455 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007456 err = bnxt_open(netdev);
7457
Michael Chana588e452016-12-07 00:26:21 -05007458 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007459 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007460 bnxt_ulp_start(bp);
7461 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007462 }
7463
7464 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7465 dev_close(netdev);
7466
7467 rtnl_unlock();
7468
7469 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7470 if (err) {
7471 dev_err(&pdev->dev,
7472 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7473 err); /* non-fatal, continue */
7474 }
7475
7476 return PCI_ERS_RESULT_RECOVERED;
7477}
7478
7479/**
7480 * bnxt_io_resume - called when traffic can start flowing again.
7481 * @pdev: Pointer to PCI device
7482 *
7483 * This callback is called when the error recovery driver tells
7484 * us that its OK to resume normal operation.
7485 */
7486static void bnxt_io_resume(struct pci_dev *pdev)
7487{
7488 struct net_device *netdev = pci_get_drvdata(pdev);
7489
7490 rtnl_lock();
7491
7492 netif_device_attach(netdev);
7493
7494 rtnl_unlock();
7495}
7496
7497static const struct pci_error_handlers bnxt_err_handler = {
7498 .error_detected = bnxt_io_error_detected,
7499 .slot_reset = bnxt_io_slot_reset,
7500 .resume = bnxt_io_resume
7501};
7502
Michael Chanc0c050c2015-10-22 16:01:17 -04007503static struct pci_driver bnxt_pci_driver = {
7504 .name = DRV_MODULE_NAME,
7505 .id_table = bnxt_pci_tbl,
7506 .probe = bnxt_init_one,
7507 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007508 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007509#if defined(CONFIG_BNXT_SRIOV)
7510 .sriov_configure = bnxt_sriov_configure,
7511#endif
7512};
7513
7514module_pci_driver(bnxt_pci_driver);