blob: 94f3d7805036e220b99ef329e41e5a68b436ea69 [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanc0c050c2015-10-22 16:01:17 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040035#include <linux/rtc.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040036#include <net/ip.h>
37#include <net/tcp.h>
38#include <net/udp.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070041#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040042#include <linux/workqueue.h>
43#include <linux/prefetch.h>
44#include <linux/cache.h>
45#include <linux/log2.h>
46#include <linux/aer.h>
47#include <linux/bitmap.h>
48#include <linux/cpu_rmap.h>
49
50#include "bnxt_hsi.h"
51#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050052#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040053#include "bnxt_sriov.h"
54#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050055#include "bnxt_dcb.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040076 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040077 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040078 BCM57311,
79 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050080 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040081 BCM57404,
82 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040083 BCM57402_NPAR,
84 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040085 BCM57412,
86 BCM57414,
87 BCM57416,
88 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040089 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040090 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57417_SFP,
92 BCM57416_SFP,
93 BCM57404_NPAR,
94 BCM57406_NPAR,
95 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040096 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040097 BCM57414_NPAR,
98 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -040099 NETXTREME_E_VF,
100 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400101};
102
103/* indexed by enum above */
104static const struct {
105 char *name;
106} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400107 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
108 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400110 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400111 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
112 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
115 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
119 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400127 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
128 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
130 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400133 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135};
136
137static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400138 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500139 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
141 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400142 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400144 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
145 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
148 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
152 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
153 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
154 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400155 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400156 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
158 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
160 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400162 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400168#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
170 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400175#endif
176 { 0 }
177};
178
179MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
180
181static const u16 bnxt_vf_req_snif[] = {
182 HWRM_FUNC_CFG,
183 HWRM_PORT_PHY_QCFG,
184 HWRM_CFA_L2_FILTER_ALLOC,
185};
186
Michael Chan25be8622016-04-05 14:09:00 -0400187static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500188 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
189 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
190 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
191 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
192 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400193};
194
Michael Chanc0c050c2015-10-22 16:01:17 -0400195static bool bnxt_vf_pciid(enum board_idx idx)
196{
Michael Chanadbc8302016-09-19 03:58:01 -0400197 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400198}
199
200#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
201#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
202#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
203
204#define BNXT_CP_DB_REARM(db, raw_cons) \
205 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
206
207#define BNXT_CP_DB(db, raw_cons) \
208 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
209
210#define BNXT_CP_DB_IRQ_DIS(db) \
211 writel(DB_CP_IRQ_DIS_FLAGS, db)
212
213static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
214{
215 /* Tell compiler to fetch tx indices from memory. */
216 barrier();
217
218 return bp->tx_ring_size -
219 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
220}
221
222static const u16 bnxt_lhint_arr[] = {
223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanb6ab4b02016-01-02 23:44:59 -0500265 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400266 txq = netdev_get_tx_queue(dev, i);
267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500512 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
514 u16 cons = txr->tx_cons;
515 struct pci_dev *pdev = bp->pdev;
516 int i;
517 unsigned int tx_bytes = 0;
518
519 for (i = 0; i < nr_pkts; i++) {
520 struct bnxt_sw_tx_bd *tx_buf;
521 struct sk_buff *skb;
522 int j, last;
523
524 tx_buf = &txr->tx_buf_ring[cons];
525 cons = NEXT_TX(cons);
526 skb = tx_buf->skb;
527 tx_buf->skb = NULL;
528
529 if (tx_buf->is_push) {
530 tx_buf->is_push = 0;
531 goto next_tx_int;
532 }
533
534 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
535 skb_headlen(skb), PCI_DMA_TODEVICE);
536 last = tx_buf->nr_frags;
537
538 for (j = 0; j < last; j++) {
539 cons = NEXT_TX(cons);
540 tx_buf = &txr->tx_buf_ring[cons];
541 dma_unmap_page(
542 &pdev->dev,
543 dma_unmap_addr(tx_buf, mapping),
544 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 PCI_DMA_TODEVICE);
546 }
547
548next_tx_int:
549 cons = NEXT_TX(cons);
550
551 tx_bytes += skb->len;
552 dev_kfree_skb_any(skb);
553 }
554
555 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
556 txr->tx_cons = cons;
557
558 /* Need to make the tx_cons update visible to bnxt_start_xmit()
559 * before checking for netif_tx_queue_stopped(). Without the
560 * memory barrier, there is a small possibility that bnxt_start_xmit()
561 * will miss it and cause the queue to be stopped forever.
562 */
563 smp_mb();
564
565 if (unlikely(netif_tx_queue_stopped(txq)) &&
566 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
567 __netif_tx_lock(txq, smp_processor_id());
568 if (netif_tx_queue_stopped(txq) &&
569 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
570 txr->dev_state != BNXT_DEV_STATE_CLOSING)
571 netif_tx_wake_queue(txq);
572 __netif_tx_unlock(txq);
573 }
574}
575
576static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 u8 *data;
580 struct pci_dev *pdev = bp->pdev;
581
582 data = kmalloc(bp->rx_buf_size, gfp);
583 if (!data)
584 return NULL;
585
586 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
587 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
588
589 if (dma_mapping_error(&pdev->dev, *mapping)) {
590 kfree(data);
591 data = NULL;
592 }
593 return data;
594}
595
596static inline int bnxt_alloc_rx_data(struct bnxt *bp,
597 struct bnxt_rx_ring_info *rxr,
598 u16 prod, gfp_t gfp)
599{
600 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
601 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
602 u8 *data;
603 dma_addr_t mapping;
604
605 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 if (!data)
607 return -ENOMEM;
608
609 rx_buf->data = data;
Michael Chan6bb19472017-02-06 16:55:32 -0500610 rx_buf->data_ptr = data + BNXT_RX_OFFSET;
Michael Chanc0c050c2015-10-22 16:01:17 -0400611 dma_unmap_addr_set(rx_buf, mapping, mapping);
612
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
631 dma_unmap_addr_set(prod_rx_buf, mapping,
632 dma_unmap_addr(cons_rx_buf, mapping));
633
634 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
635 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
636
637 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
638}
639
640static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
641{
642 u16 next, max = rxr->rx_agg_bmap_size;
643
644 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
645 if (next >= max)
646 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
647 return next;
648}
649
650static inline int bnxt_alloc_rx_page(struct bnxt *bp,
651 struct bnxt_rx_ring_info *rxr,
652 u16 prod, gfp_t gfp)
653{
654 struct rx_bd *rxbd =
655 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
656 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
657 struct pci_dev *pdev = bp->pdev;
658 struct page *page;
659 dma_addr_t mapping;
660 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400661 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400662
Michael Chan89d0a062016-04-25 02:30:51 -0400663 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
664 page = rxr->rx_page;
665 if (!page) {
666 page = alloc_page(gfp);
667 if (!page)
668 return -ENOMEM;
669 rxr->rx_page = page;
670 rxr->rx_page_offset = 0;
671 }
672 offset = rxr->rx_page_offset;
673 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
674 if (rxr->rx_page_offset == PAGE_SIZE)
675 rxr->rx_page = NULL;
676 else
677 get_page(page);
678 } else {
679 page = alloc_page(gfp);
680 if (!page)
681 return -ENOMEM;
682 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400683
Michael Chan89d0a062016-04-25 02:30:51 -0400684 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400685 PCI_DMA_FROMDEVICE);
686 if (dma_mapping_error(&pdev->dev, mapping)) {
687 __free_page(page);
688 return -EIO;
689 }
690
691 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
692 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
693
694 __set_bit(sw_prod, rxr->rx_agg_bmap);
695 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
696 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
697
698 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400699 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400700 rx_agg_buf->mapping = mapping;
701 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
702 rxbd->rx_bd_opaque = sw_prod;
703 return 0;
704}
705
706static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
707 u32 agg_bufs)
708{
709 struct bnxt *bp = bnapi->bp;
710 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500711 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400712 u16 prod = rxr->rx_agg_prod;
713 u16 sw_prod = rxr->rx_sw_agg_prod;
714 u32 i;
715
716 for (i = 0; i < agg_bufs; i++) {
717 u16 cons;
718 struct rx_agg_cmp *agg;
719 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
720 struct rx_bd *prod_bd;
721 struct page *page;
722
723 agg = (struct rx_agg_cmp *)
724 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
725 cons = agg->rx_agg_cmp_opaque;
726 __clear_bit(cons, rxr->rx_agg_bmap);
727
728 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
729 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
730
731 __set_bit(sw_prod, rxr->rx_agg_bmap);
732 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
733 cons_rx_buf = &rxr->rx_agg_ring[cons];
734
735 /* It is possible for sw_prod to be equal to cons, so
736 * set cons_rx_buf->page to NULL first.
737 */
738 page = cons_rx_buf->page;
739 cons_rx_buf->page = NULL;
740 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400741 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400742
743 prod_rx_buf->mapping = cons_rx_buf->mapping;
744
745 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
746
747 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
748 prod_bd->rx_bd_opaque = sw_prod;
749
750 prod = NEXT_RX_AGG(prod);
751 sw_prod = NEXT_RX_AGG(sw_prod);
752 cp_cons = NEXT_CMP(cp_cons);
753 }
754 rxr->rx_agg_prod = prod;
755 rxr->rx_sw_agg_prod = sw_prod;
756}
757
758static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
759 struct bnxt_rx_ring_info *rxr, u16 cons,
Michael Chan6bb19472017-02-06 16:55:32 -0500760 void *data, u8 *data_ptr,
761 dma_addr_t dma_addr,
762 unsigned int offset_and_len)
Michael Chanc0c050c2015-10-22 16:01:17 -0400763{
Michael Chan6bb19472017-02-06 16:55:32 -0500764 u16 prod = rxr->rx_prod;
Michael Chanc0c050c2015-10-22 16:01:17 -0400765 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -0500766 int err;
Michael Chanc0c050c2015-10-22 16:01:17 -0400767
768 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
769 if (unlikely(err)) {
770 bnxt_reuse_rx_data(rxr, cons, data);
771 return NULL;
772 }
773
774 skb = build_skb(data, 0);
775 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
776 PCI_DMA_FROMDEVICE);
777 if (!skb) {
778 kfree(data);
779 return NULL;
780 }
781
782 skb_reserve(skb, BNXT_RX_OFFSET);
Michael Chan6bb19472017-02-06 16:55:32 -0500783 skb_put(skb, offset_and_len & 0xffff);
Michael Chanc0c050c2015-10-22 16:01:17 -0400784 return skb;
785}
786
787static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
788 struct sk_buff *skb, u16 cp_cons,
789 u32 agg_bufs)
790{
791 struct pci_dev *pdev = bp->pdev;
792 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500793 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400794 u16 prod = rxr->rx_agg_prod;
795 u32 i;
796
797 for (i = 0; i < agg_bufs; i++) {
798 u16 cons, frag_len;
799 struct rx_agg_cmp *agg;
800 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
801 struct page *page;
802 dma_addr_t mapping;
803
804 agg = (struct rx_agg_cmp *)
805 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
806 cons = agg->rx_agg_cmp_opaque;
807 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
808 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
809
810 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400811 skb_fill_page_desc(skb, i, cons_rx_buf->page,
812 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400813 __clear_bit(cons, rxr->rx_agg_bmap);
814
815 /* It is possible for bnxt_alloc_rx_page() to allocate
816 * a sw_prod index that equals the cons index, so we
817 * need to clear the cons entry now.
818 */
819 mapping = dma_unmap_addr(cons_rx_buf, mapping);
820 page = cons_rx_buf->page;
821 cons_rx_buf->page = NULL;
822
823 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
824 struct skb_shared_info *shinfo;
825 unsigned int nr_frags;
826
827 shinfo = skb_shinfo(skb);
828 nr_frags = --shinfo->nr_frags;
829 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
830
831 dev_kfree_skb(skb);
832
833 cons_rx_buf->page = page;
834
835 /* Update prod since possibly some pages have been
836 * allocated already.
837 */
838 rxr->rx_agg_prod = prod;
839 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
840 return NULL;
841 }
842
Michael Chan2839f282016-04-25 02:30:50 -0400843 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400844 PCI_DMA_FROMDEVICE);
845
846 skb->data_len += frag_len;
847 skb->len += frag_len;
848 skb->truesize += PAGE_SIZE;
849
850 prod = NEXT_RX_AGG(prod);
851 cp_cons = NEXT_CMP(cp_cons);
852 }
853 rxr->rx_agg_prod = prod;
854 return skb;
855}
856
857static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
858 u8 agg_bufs, u32 *raw_cons)
859{
860 u16 last;
861 struct rx_agg_cmp *agg;
862
863 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
864 last = RING_CMP(*raw_cons);
865 agg = (struct rx_agg_cmp *)
866 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
867 return RX_AGG_CMP_VALID(agg, *raw_cons);
868}
869
870static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
871 unsigned int len,
872 dma_addr_t mapping)
873{
874 struct bnxt *bp = bnapi->bp;
875 struct pci_dev *pdev = bp->pdev;
876 struct sk_buff *skb;
877
878 skb = napi_alloc_skb(&bnapi->napi, len);
879 if (!skb)
880 return NULL;
881
882 dma_sync_single_for_cpu(&pdev->dev, mapping,
883 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
884
Michael Chan6bb19472017-02-06 16:55:32 -0500885 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
886 len + NET_IP_ALIGN);
Michael Chanc0c050c2015-10-22 16:01:17 -0400887
888 dma_sync_single_for_device(&pdev->dev, mapping,
889 bp->rx_copy_thresh,
890 PCI_DMA_FROMDEVICE);
891
892 skb_put(skb, len);
893 return skb;
894}
895
Michael Chanfa7e2812016-05-10 19:18:00 -0400896static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
897 u32 *raw_cons, void *cmp)
898{
899 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
900 struct rx_cmp *rxcmp = cmp;
901 u32 tmp_raw_cons = *raw_cons;
902 u8 cmp_type, agg_bufs = 0;
903
904 cmp_type = RX_CMP_TYPE(rxcmp);
905
906 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
907 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
908 RX_CMP_AGG_BUFS) >>
909 RX_CMP_AGG_BUFS_SHIFT;
910 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
911 struct rx_tpa_end_cmp *tpa_end = cmp;
912
913 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
914 RX_TPA_END_CMP_AGG_BUFS) >>
915 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
916 }
917
918 if (agg_bufs) {
919 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
920 return -EBUSY;
921 }
922 *raw_cons = tmp_raw_cons;
923 return 0;
924}
925
926static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
927{
928 if (!rxr->bnapi->in_reset) {
929 rxr->bnapi->in_reset = true;
930 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
931 schedule_work(&bp->sp_task);
932 }
933 rxr->rx_next_cons = 0xffff;
934}
935
Michael Chanc0c050c2015-10-22 16:01:17 -0400936static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
937 struct rx_tpa_start_cmp *tpa_start,
938 struct rx_tpa_start_cmp_ext *tpa_start1)
939{
940 u8 agg_id = TPA_START_AGG_ID(tpa_start);
941 u16 cons, prod;
942 struct bnxt_tpa_info *tpa_info;
943 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
944 struct rx_bd *prod_bd;
945 dma_addr_t mapping;
946
947 cons = tpa_start->rx_tpa_start_cmp_opaque;
948 prod = rxr->rx_prod;
949 cons_rx_buf = &rxr->rx_buf_ring[cons];
950 prod_rx_buf = &rxr->rx_buf_ring[prod];
951 tpa_info = &rxr->rx_tpa[agg_id];
952
Michael Chanfa7e2812016-05-10 19:18:00 -0400953 if (unlikely(cons != rxr->rx_next_cons)) {
954 bnxt_sched_reset(bp, rxr);
955 return;
956 }
957
Michael Chanc0c050c2015-10-22 16:01:17 -0400958 prod_rx_buf->data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -0500959 prod_rx_buf->data_ptr = tpa_info->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400960
961 mapping = tpa_info->mapping;
962 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
963
964 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
965
966 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
967
968 tpa_info->data = cons_rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -0500969 tpa_info->data_ptr = cons_rx_buf->data_ptr;
Michael Chanc0c050c2015-10-22 16:01:17 -0400970 cons_rx_buf->data = NULL;
971 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
972
973 tpa_info->len =
974 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
975 RX_TPA_START_CMP_LEN_SHIFT;
976 if (likely(TPA_START_HASH_VALID(tpa_start))) {
977 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
978
979 tpa_info->hash_type = PKT_HASH_TYPE_L4;
980 tpa_info->gso_type = SKB_GSO_TCPV4;
981 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
982 if (hash_type == 3)
983 tpa_info->gso_type = SKB_GSO_TCPV6;
984 tpa_info->rss_hash =
985 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
986 } else {
987 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
988 tpa_info->gso_type = 0;
989 if (netif_msg_rx_err(bp))
990 netdev_warn(bp->dev, "TPA packet without valid hash\n");
991 }
992 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
993 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400994 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400995
996 rxr->rx_prod = NEXT_RX(prod);
997 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400998 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400999 cons_rx_buf = &rxr->rx_buf_ring[cons];
1000
1001 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1002 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1003 cons_rx_buf->data = NULL;
1004}
1005
1006static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1007 u16 cp_cons, u32 agg_bufs)
1008{
1009 if (agg_bufs)
1010 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1011}
1012
Michael Chan94758f82016-06-13 02:25:35 -04001013static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1014 int payload_off, int tcp_ts,
1015 struct sk_buff *skb)
1016{
1017#ifdef CONFIG_INET
1018 struct tcphdr *th;
1019 int len, nw_off;
1020 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1021 u32 hdr_info = tpa_info->hdr_info;
1022 bool loopback = false;
1023
1024 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1025 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1026 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1027
1028 /* If the packet is an internal loopback packet, the offsets will
1029 * have an extra 4 bytes.
1030 */
1031 if (inner_mac_off == 4) {
1032 loopback = true;
1033 } else if (inner_mac_off > 4) {
1034 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1035 ETH_HLEN - 2));
1036
1037 /* We only support inner iPv4/ipv6. If we don't see the
1038 * correct protocol ID, it must be a loopback packet where
1039 * the offsets are off by 4.
1040 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001041 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001042 loopback = true;
1043 }
1044 if (loopback) {
1045 /* internal loopback packet, subtract all offsets by 4 */
1046 inner_ip_off -= 4;
1047 inner_mac_off -= 4;
1048 outer_ip_off -= 4;
1049 }
1050
1051 nw_off = inner_ip_off - ETH_HLEN;
1052 skb_set_network_header(skb, nw_off);
1053 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1054 struct ipv6hdr *iph = ipv6_hdr(skb);
1055
1056 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1057 len = skb->len - skb_transport_offset(skb);
1058 th = tcp_hdr(skb);
1059 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1060 } else {
1061 struct iphdr *iph = ip_hdr(skb);
1062
1063 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1064 len = skb->len - skb_transport_offset(skb);
1065 th = tcp_hdr(skb);
1066 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1067 }
1068
1069 if (inner_mac_off) { /* tunnel */
1070 struct udphdr *uh = NULL;
1071 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1072 ETH_HLEN - 2));
1073
1074 if (proto == htons(ETH_P_IP)) {
1075 struct iphdr *iph = (struct iphdr *)skb->data;
1076
1077 if (iph->protocol == IPPROTO_UDP)
1078 uh = (struct udphdr *)(iph + 1);
1079 } else {
1080 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1081
1082 if (iph->nexthdr == IPPROTO_UDP)
1083 uh = (struct udphdr *)(iph + 1);
1084 }
1085 if (uh) {
1086 if (uh->check)
1087 skb_shinfo(skb)->gso_type |=
1088 SKB_GSO_UDP_TUNNEL_CSUM;
1089 else
1090 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1091 }
1092 }
1093#endif
1094 return skb;
1095}
1096
Michael Chanc0c050c2015-10-22 16:01:17 -04001097#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1098#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1099
Michael Chan309369c2016-06-13 02:25:34 -04001100static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1101 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001102 struct sk_buff *skb)
1103{
Michael Chand1611c32015-10-25 22:27:57 -04001104#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001105 struct tcphdr *th;
Michael Chan719ca812017-01-17 22:07:19 -05001106 int len, nw_off, tcp_opt_len = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04001107
Michael Chan309369c2016-06-13 02:25:34 -04001108 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001109 tcp_opt_len = 12;
1110
Michael Chanc0c050c2015-10-22 16:01:17 -04001111 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1112 struct iphdr *iph;
1113
1114 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1115 ETH_HLEN;
1116 skb_set_network_header(skb, nw_off);
1117 iph = ip_hdr(skb);
1118 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1119 len = skb->len - skb_transport_offset(skb);
1120 th = tcp_hdr(skb);
1121 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1122 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1123 struct ipv6hdr *iph;
1124
1125 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1126 ETH_HLEN;
1127 skb_set_network_header(skb, nw_off);
1128 iph = ipv6_hdr(skb);
1129 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1130 len = skb->len - skb_transport_offset(skb);
1131 th = tcp_hdr(skb);
1132 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1133 } else {
1134 dev_kfree_skb_any(skb);
1135 return NULL;
1136 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001137
1138 if (nw_off) { /* tunnel */
1139 struct udphdr *uh = NULL;
1140
1141 if (skb->protocol == htons(ETH_P_IP)) {
1142 struct iphdr *iph = (struct iphdr *)skb->data;
1143
1144 if (iph->protocol == IPPROTO_UDP)
1145 uh = (struct udphdr *)(iph + 1);
1146 } else {
1147 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1148
1149 if (iph->nexthdr == IPPROTO_UDP)
1150 uh = (struct udphdr *)(iph + 1);
1151 }
1152 if (uh) {
1153 if (uh->check)
1154 skb_shinfo(skb)->gso_type |=
1155 SKB_GSO_UDP_TUNNEL_CSUM;
1156 else
1157 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1158 }
1159 }
1160#endif
1161 return skb;
1162}
1163
Michael Chan309369c2016-06-13 02:25:34 -04001164static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1165 struct bnxt_tpa_info *tpa_info,
1166 struct rx_tpa_end_cmp *tpa_end,
1167 struct rx_tpa_end_cmp_ext *tpa_end1,
1168 struct sk_buff *skb)
1169{
1170#ifdef CONFIG_INET
1171 int payload_off;
1172 u16 segs;
1173
1174 segs = TPA_END_TPA_SEGS(tpa_end);
1175 if (segs == 1)
1176 return skb;
1177
1178 NAPI_GRO_CB(skb)->count = segs;
1179 skb_shinfo(skb)->gso_size =
1180 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1181 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1182 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1183 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1184 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1185 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001186 if (likely(skb))
1187 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001188#endif
1189 return skb;
1190}
1191
Michael Chanc0c050c2015-10-22 16:01:17 -04001192static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1193 struct bnxt_napi *bnapi,
1194 u32 *raw_cons,
1195 struct rx_tpa_end_cmp *tpa_end,
1196 struct rx_tpa_end_cmp_ext *tpa_end1,
1197 bool *agg_event)
1198{
1199 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001200 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001201 u8 agg_id = TPA_END_AGG_ID(tpa_end);
Michael Chan6bb19472017-02-06 16:55:32 -05001202 u8 *data_ptr, agg_bufs;
Michael Chanc0c050c2015-10-22 16:01:17 -04001203 u16 cp_cons = RING_CMP(*raw_cons);
1204 unsigned int len;
1205 struct bnxt_tpa_info *tpa_info;
1206 dma_addr_t mapping;
1207 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001208 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001209
Michael Chanfa7e2812016-05-10 19:18:00 -04001210 if (unlikely(bnapi->in_reset)) {
1211 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1212
1213 if (rc < 0)
1214 return ERR_PTR(-EBUSY);
1215 return NULL;
1216 }
1217
Michael Chanc0c050c2015-10-22 16:01:17 -04001218 tpa_info = &rxr->rx_tpa[agg_id];
1219 data = tpa_info->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001220 data_ptr = tpa_info->data_ptr;
1221 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001222 len = tpa_info->len;
1223 mapping = tpa_info->mapping;
1224
1225 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1226 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1227
1228 if (agg_bufs) {
1229 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1230 return ERR_PTR(-EBUSY);
1231
1232 *agg_event = true;
1233 cp_cons = NEXT_CMP(cp_cons);
1234 }
1235
1236 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1237 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1238 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1239 agg_bufs, (int)MAX_SKB_FRAGS);
1240 return NULL;
1241 }
1242
1243 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001244 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001245 if (!skb) {
1246 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1247 return NULL;
1248 }
1249 } else {
1250 u8 *new_data;
1251 dma_addr_t new_mapping;
1252
1253 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1254 if (!new_data) {
1255 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1256 return NULL;
1257 }
1258
1259 tpa_info->data = new_data;
Michael Chan6bb19472017-02-06 16:55:32 -05001260 tpa_info->data_ptr = new_data + BNXT_RX_OFFSET;
Michael Chanc0c050c2015-10-22 16:01:17 -04001261 tpa_info->mapping = new_mapping;
1262
1263 skb = build_skb(data, 0);
1264 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1265 PCI_DMA_FROMDEVICE);
1266
1267 if (!skb) {
1268 kfree(data);
1269 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1270 return NULL;
1271 }
1272 skb_reserve(skb, BNXT_RX_OFFSET);
1273 skb_put(skb, len);
1274 }
1275
1276 if (agg_bufs) {
1277 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1278 if (!skb) {
1279 /* Page reuse already handled by bnxt_rx_pages(). */
1280 return NULL;
1281 }
1282 }
1283 skb->protocol = eth_type_trans(skb, bp->dev);
1284
1285 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1286 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1287
Michael Chan8852ddb2016-06-06 02:37:16 -04001288 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1289 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001290 u16 vlan_proto = tpa_info->metadata >>
1291 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001292 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001293
Michael Chan8852ddb2016-06-06 02:37:16 -04001294 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001295 }
1296
1297 skb_checksum_none_assert(skb);
1298 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1299 skb->ip_summed = CHECKSUM_UNNECESSARY;
1300 skb->csum_level =
1301 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1302 }
1303
1304 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001305 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001306
1307 return skb;
1308}
1309
1310/* returns the following:
1311 * 1 - 1 packet successfully received
1312 * 0 - successful TPA_START, packet not completed yet
1313 * -EBUSY - completion ring does not have all the agg buffers yet
1314 * -ENOMEM - packet aborted due to out of memory
1315 * -EIO - packet aborted due to hw error indicated in BD
1316 */
1317static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1318 bool *agg_event)
1319{
1320 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001321 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001322 struct net_device *dev = bp->dev;
1323 struct rx_cmp *rxcmp;
1324 struct rx_cmp_ext *rxcmp1;
1325 u32 tmp_raw_cons = *raw_cons;
1326 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1327 struct bnxt_sw_rx_bd *rx_buf;
1328 unsigned int len;
Michael Chan6bb19472017-02-06 16:55:32 -05001329 u8 *data_ptr, agg_bufs, cmp_type;
Michael Chanc0c050c2015-10-22 16:01:17 -04001330 dma_addr_t dma_addr;
1331 struct sk_buff *skb;
Michael Chan6bb19472017-02-06 16:55:32 -05001332 void *data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001333 int rc = 0;
1334
1335 rxcmp = (struct rx_cmp *)
1336 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1337
1338 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1339 cp_cons = RING_CMP(tmp_raw_cons);
1340 rxcmp1 = (struct rx_cmp_ext *)
1341 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1342
1343 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1344 return -EBUSY;
1345
1346 cmp_type = RX_CMP_TYPE(rxcmp);
1347
1348 prod = rxr->rx_prod;
1349
1350 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1351 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1352 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1353
1354 goto next_rx_no_prod;
1355
1356 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1357 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1358 (struct rx_tpa_end_cmp *)rxcmp,
1359 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1360 agg_event);
1361
1362 if (unlikely(IS_ERR(skb)))
1363 return -EBUSY;
1364
1365 rc = -ENOMEM;
1366 if (likely(skb)) {
1367 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001368 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001369 rc = 1;
1370 }
1371 goto next_rx_no_prod;
1372 }
1373
1374 cons = rxcmp->rx_cmp_opaque;
1375 rx_buf = &rxr->rx_buf_ring[cons];
1376 data = rx_buf->data;
Michael Chan6bb19472017-02-06 16:55:32 -05001377 data_ptr = rx_buf->data_ptr;
Michael Chanfa7e2812016-05-10 19:18:00 -04001378 if (unlikely(cons != rxr->rx_next_cons)) {
1379 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1380
1381 bnxt_sched_reset(bp, rxr);
1382 return rc1;
1383 }
Michael Chan6bb19472017-02-06 16:55:32 -05001384 prefetch(data_ptr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001385
1386 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1387 RX_CMP_AGG_BUFS_SHIFT;
1388
1389 if (agg_bufs) {
1390 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1391 return -EBUSY;
1392
1393 cp_cons = NEXT_CMP(cp_cons);
1394 *agg_event = true;
1395 }
1396
1397 rx_buf->data = NULL;
1398 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1399 bnxt_reuse_rx_data(rxr, cons, data);
1400 if (agg_bufs)
1401 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1402
1403 rc = -EIO;
1404 goto next_rx;
1405 }
1406
1407 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1408 dma_addr = dma_unmap_addr(rx_buf, mapping);
1409
1410 if (len <= bp->rx_copy_thresh) {
Michael Chan6bb19472017-02-06 16:55:32 -05001411 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04001412 bnxt_reuse_rx_data(rxr, cons, data);
1413 if (!skb) {
1414 rc = -ENOMEM;
1415 goto next_rx;
1416 }
1417 } else {
Michael Chan6bb19472017-02-06 16:55:32 -05001418 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1419 len);
Michael Chanc0c050c2015-10-22 16:01:17 -04001420 if (!skb) {
1421 rc = -ENOMEM;
1422 goto next_rx;
1423 }
1424 }
1425
1426 if (agg_bufs) {
1427 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1428 if (!skb) {
1429 rc = -ENOMEM;
1430 goto next_rx;
1431 }
1432 }
1433
1434 if (RX_CMP_HASH_VALID(rxcmp)) {
1435 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1436 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1437
1438 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1439 if (hash_type != 1 && hash_type != 3)
1440 type = PKT_HASH_TYPE_L3;
1441 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1442 }
1443
1444 skb->protocol = eth_type_trans(skb, dev);
1445
Michael Chan8852ddb2016-06-06 02:37:16 -04001446 if ((rxcmp1->rx_cmp_flags2 &
1447 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1448 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001449 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001450 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001451 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1452
Michael Chan8852ddb2016-06-06 02:37:16 -04001453 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001454 }
1455
1456 skb_checksum_none_assert(skb);
1457 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1458 if (dev->features & NETIF_F_RXCSUM) {
1459 skb->ip_summed = CHECKSUM_UNNECESSARY;
1460 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1461 }
1462 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001463 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1464 if (dev->features & NETIF_F_RXCSUM)
1465 cpr->rx_l4_csum_errors++;
1466 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001467 }
1468
1469 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001470 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001471 rc = 1;
1472
1473next_rx:
1474 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001475 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001476
1477next_rx_no_prod:
1478 *raw_cons = tmp_raw_cons;
1479
1480 return rc;
1481}
1482
Michael Chan4bb13ab2016-04-05 14:09:01 -04001483#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001484 ((data) & \
1485 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001486
Michael Chanc0c050c2015-10-22 16:01:17 -04001487static int bnxt_async_event_process(struct bnxt *bp,
1488 struct hwrm_async_event_cmpl *cmpl)
1489{
1490 u16 event_id = le16_to_cpu(cmpl->event_id);
1491
1492 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1493 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001494 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001495 u32 data1 = le32_to_cpu(cmpl->event_data1);
1496 struct bnxt_link_info *link_info = &bp->link_info;
1497
1498 if (BNXT_VF(bp))
1499 goto async_event_process_exit;
1500 if (data1 & 0x20000) {
1501 u16 fw_speed = link_info->force_link_speed;
1502 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1503
1504 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1505 speed);
1506 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001507 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001508 /* fall thru */
1509 }
Michael Chan87c374d2016-12-02 21:17:16 -05001510 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001511 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001512 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001513 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001514 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001515 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001516 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001517 u32 data1 = le32_to_cpu(cmpl->event_data1);
1518 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1519
1520 if (BNXT_VF(bp))
1521 break;
1522
1523 if (bp->pf.port_id != port_id)
1524 break;
1525
Michael Chan4bb13ab2016-04-05 14:09:01 -04001526 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1527 break;
1528 }
Michael Chan87c374d2016-12-02 21:17:16 -05001529 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001530 if (BNXT_PF(bp))
1531 goto async_event_process_exit;
1532 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1533 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001534 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001535 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001536 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001537 schedule_work(&bp->sp_task);
1538async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001539 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001540 return 0;
1541}
1542
1543static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1544{
1545 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1546 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1547 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1548 (struct hwrm_fwd_req_cmpl *)txcmp;
1549
1550 switch (cmpl_type) {
1551 case CMPL_BASE_TYPE_HWRM_DONE:
1552 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1553 if (seq_id == bp->hwrm_intr_seq_id)
1554 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1555 else
1556 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1557 break;
1558
1559 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1560 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1561
1562 if ((vf_id < bp->pf.first_vf_id) ||
1563 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1564 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1565 vf_id);
1566 return -EINVAL;
1567 }
1568
1569 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1570 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1571 schedule_work(&bp->sp_task);
1572 break;
1573
1574 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1575 bnxt_async_event_process(bp,
1576 (struct hwrm_async_event_cmpl *)txcmp);
1577
1578 default:
1579 break;
1580 }
1581
1582 return 0;
1583}
1584
1585static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1586{
1587 struct bnxt_napi *bnapi = dev_instance;
1588 struct bnxt *bp = bnapi->bp;
1589 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1590 u32 cons = RING_CMP(cpr->cp_raw_cons);
1591
1592 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1593 napi_schedule(&bnapi->napi);
1594 return IRQ_HANDLED;
1595}
1596
1597static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1598{
1599 u32 raw_cons = cpr->cp_raw_cons;
1600 u16 cons = RING_CMP(raw_cons);
1601 struct tx_cmp *txcmp;
1602
1603 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1604
1605 return TX_CMP_VALID(txcmp, raw_cons);
1606}
1607
Michael Chanc0c050c2015-10-22 16:01:17 -04001608static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1609{
1610 struct bnxt_napi *bnapi = dev_instance;
1611 struct bnxt *bp = bnapi->bp;
1612 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1613 u32 cons = RING_CMP(cpr->cp_raw_cons);
1614 u32 int_status;
1615
1616 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1617
1618 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001619 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001620 /* return if erroneous interrupt */
1621 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1622 return IRQ_NONE;
1623 }
1624
1625 /* disable ring IRQ */
1626 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1627
1628 /* Return here if interrupt is shared and is disabled. */
1629 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1630 return IRQ_HANDLED;
1631
1632 napi_schedule(&bnapi->napi);
1633 return IRQ_HANDLED;
1634}
1635
1636static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1637{
1638 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1639 u32 raw_cons = cpr->cp_raw_cons;
1640 u32 cons;
1641 int tx_pkts = 0;
1642 int rx_pkts = 0;
1643 bool rx_event = false;
1644 bool agg_event = false;
1645 struct tx_cmp *txcmp;
1646
1647 while (1) {
1648 int rc;
1649
1650 cons = RING_CMP(raw_cons);
1651 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1652
1653 if (!TX_CMP_VALID(txcmp, raw_cons))
1654 break;
1655
Michael Chan67a95e22016-05-04 16:56:43 -04001656 /* The valid test of the entry must be done first before
1657 * reading any further.
1658 */
Michael Chanb67daab2016-05-15 03:04:51 -04001659 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001660 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1661 tx_pkts++;
1662 /* return full budget so NAPI will complete. */
1663 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1664 rx_pkts = budget;
1665 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1666 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1667 if (likely(rc >= 0))
1668 rx_pkts += rc;
1669 else if (rc == -EBUSY) /* partial completion */
1670 break;
1671 rx_event = true;
1672 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1673 CMPL_BASE_TYPE_HWRM_DONE) ||
1674 (TX_CMP_TYPE(txcmp) ==
1675 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1676 (TX_CMP_TYPE(txcmp) ==
1677 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1678 bnxt_hwrm_handler(bp, txcmp);
1679 }
1680 raw_cons = NEXT_RAW_CMP(raw_cons);
1681
1682 if (rx_pkts == budget)
1683 break;
1684 }
1685
1686 cpr->cp_raw_cons = raw_cons;
1687 /* ACK completion ring before freeing tx ring and producing new
1688 * buffers in rx/agg rings to prevent overflowing the completion
1689 * ring.
1690 */
1691 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1692
1693 if (tx_pkts)
1694 bnxt_tx_int(bp, bnapi, tx_pkts);
1695
1696 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001697 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001698
1699 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1700 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1701 if (agg_event) {
1702 writel(DB_KEY_RX | rxr->rx_agg_prod,
1703 rxr->rx_agg_doorbell);
1704 writel(DB_KEY_RX | rxr->rx_agg_prod,
1705 rxr->rx_agg_doorbell);
1706 }
1707 }
1708 return rx_pkts;
1709}
1710
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001711static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1712{
1713 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1714 struct bnxt *bp = bnapi->bp;
1715 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1716 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1717 struct tx_cmp *txcmp;
1718 struct rx_cmp_ext *rxcmp1;
1719 u32 cp_cons, tmp_raw_cons;
1720 u32 raw_cons = cpr->cp_raw_cons;
1721 u32 rx_pkts = 0;
1722 bool agg_event = false;
1723
1724 while (1) {
1725 int rc;
1726
1727 cp_cons = RING_CMP(raw_cons);
1728 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1729
1730 if (!TX_CMP_VALID(txcmp, raw_cons))
1731 break;
1732
1733 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1734 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1735 cp_cons = RING_CMP(tmp_raw_cons);
1736 rxcmp1 = (struct rx_cmp_ext *)
1737 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1738
1739 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1740 break;
1741
1742 /* force an error to recycle the buffer */
1743 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1744 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1745
1746 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1747 if (likely(rc == -EIO))
1748 rx_pkts++;
1749 else if (rc == -EBUSY) /* partial completion */
1750 break;
1751 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1752 CMPL_BASE_TYPE_HWRM_DONE)) {
1753 bnxt_hwrm_handler(bp, txcmp);
1754 } else {
1755 netdev_err(bp->dev,
1756 "Invalid completion received on special ring\n");
1757 }
1758 raw_cons = NEXT_RAW_CMP(raw_cons);
1759
1760 if (rx_pkts == budget)
1761 break;
1762 }
1763
1764 cpr->cp_raw_cons = raw_cons;
1765 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1766 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1767 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1768
1769 if (agg_event) {
1770 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1771 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1772 }
1773
1774 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001775 napi_complete_done(napi, rx_pkts);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001776 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1777 }
1778 return rx_pkts;
1779}
1780
Michael Chanc0c050c2015-10-22 16:01:17 -04001781static int bnxt_poll(struct napi_struct *napi, int budget)
1782{
1783 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1784 struct bnxt *bp = bnapi->bp;
1785 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1786 int work_done = 0;
1787
Michael Chanc0c050c2015-10-22 16:01:17 -04001788 while (1) {
1789 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1790
1791 if (work_done >= budget)
1792 break;
1793
1794 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001795 if (napi_complete_done(napi, work_done))
1796 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1797 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001798 break;
1799 }
1800 }
1801 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001802 return work_done;
1803}
1804
Michael Chanc0c050c2015-10-22 16:01:17 -04001805static void bnxt_free_tx_skbs(struct bnxt *bp)
1806{
1807 int i, max_idx;
1808 struct pci_dev *pdev = bp->pdev;
1809
Michael Chanb6ab4b02016-01-02 23:44:59 -05001810 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001811 return;
1812
1813 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1814 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001815 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001816 int j;
1817
Michael Chanc0c050c2015-10-22 16:01:17 -04001818 for (j = 0; j < max_idx;) {
1819 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1820 struct sk_buff *skb = tx_buf->skb;
1821 int k, last;
1822
1823 if (!skb) {
1824 j++;
1825 continue;
1826 }
1827
1828 tx_buf->skb = NULL;
1829
1830 if (tx_buf->is_push) {
1831 dev_kfree_skb(skb);
1832 j += 2;
1833 continue;
1834 }
1835
1836 dma_unmap_single(&pdev->dev,
1837 dma_unmap_addr(tx_buf, mapping),
1838 skb_headlen(skb),
1839 PCI_DMA_TODEVICE);
1840
1841 last = tx_buf->nr_frags;
1842 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001843 for (k = 0; k < last; k++, j++) {
1844 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001845 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1846
Michael Chand612a572016-01-28 03:11:22 -05001847 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001848 dma_unmap_page(
1849 &pdev->dev,
1850 dma_unmap_addr(tx_buf, mapping),
1851 skb_frag_size(frag), PCI_DMA_TODEVICE);
1852 }
1853 dev_kfree_skb(skb);
1854 }
1855 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1856 }
1857}
1858
1859static void bnxt_free_rx_skbs(struct bnxt *bp)
1860{
1861 int i, max_idx, max_agg_idx;
1862 struct pci_dev *pdev = bp->pdev;
1863
Michael Chanb6ab4b02016-01-02 23:44:59 -05001864 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001865 return;
1866
1867 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1868 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1869 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001870 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001871 int j;
1872
Michael Chanc0c050c2015-10-22 16:01:17 -04001873 if (rxr->rx_tpa) {
1874 for (j = 0; j < MAX_TPA; j++) {
1875 struct bnxt_tpa_info *tpa_info =
1876 &rxr->rx_tpa[j];
1877 u8 *data = tpa_info->data;
1878
1879 if (!data)
1880 continue;
1881
1882 dma_unmap_single(
1883 &pdev->dev,
1884 dma_unmap_addr(tpa_info, mapping),
1885 bp->rx_buf_use_size,
1886 PCI_DMA_FROMDEVICE);
1887
1888 tpa_info->data = NULL;
1889
1890 kfree(data);
1891 }
1892 }
1893
1894 for (j = 0; j < max_idx; j++) {
1895 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
Michael Chan6bb19472017-02-06 16:55:32 -05001896 void *data = rx_buf->data;
Michael Chanc0c050c2015-10-22 16:01:17 -04001897
1898 if (!data)
1899 continue;
1900
1901 dma_unmap_single(&pdev->dev,
1902 dma_unmap_addr(rx_buf, mapping),
1903 bp->rx_buf_use_size,
1904 PCI_DMA_FROMDEVICE);
1905
1906 rx_buf->data = NULL;
1907
1908 kfree(data);
1909 }
1910
1911 for (j = 0; j < max_agg_idx; j++) {
1912 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1913 &rxr->rx_agg_ring[j];
1914 struct page *page = rx_agg_buf->page;
1915
1916 if (!page)
1917 continue;
1918
1919 dma_unmap_page(&pdev->dev,
1920 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001921 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001922
1923 rx_agg_buf->page = NULL;
1924 __clear_bit(j, rxr->rx_agg_bmap);
1925
1926 __free_page(page);
1927 }
Michael Chan89d0a062016-04-25 02:30:51 -04001928 if (rxr->rx_page) {
1929 __free_page(rxr->rx_page);
1930 rxr->rx_page = NULL;
1931 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001932 }
1933}
1934
1935static void bnxt_free_skbs(struct bnxt *bp)
1936{
1937 bnxt_free_tx_skbs(bp);
1938 bnxt_free_rx_skbs(bp);
1939}
1940
1941static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1942{
1943 struct pci_dev *pdev = bp->pdev;
1944 int i;
1945
1946 for (i = 0; i < ring->nr_pages; i++) {
1947 if (!ring->pg_arr[i])
1948 continue;
1949
1950 dma_free_coherent(&pdev->dev, ring->page_size,
1951 ring->pg_arr[i], ring->dma_arr[i]);
1952
1953 ring->pg_arr[i] = NULL;
1954 }
1955 if (ring->pg_tbl) {
1956 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1957 ring->pg_tbl, ring->pg_tbl_map);
1958 ring->pg_tbl = NULL;
1959 }
1960 if (ring->vmem_size && *ring->vmem) {
1961 vfree(*ring->vmem);
1962 *ring->vmem = NULL;
1963 }
1964}
1965
1966static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1967{
1968 int i;
1969 struct pci_dev *pdev = bp->pdev;
1970
1971 if (ring->nr_pages > 1) {
1972 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1973 ring->nr_pages * 8,
1974 &ring->pg_tbl_map,
1975 GFP_KERNEL);
1976 if (!ring->pg_tbl)
1977 return -ENOMEM;
1978 }
1979
1980 for (i = 0; i < ring->nr_pages; i++) {
1981 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1982 ring->page_size,
1983 &ring->dma_arr[i],
1984 GFP_KERNEL);
1985 if (!ring->pg_arr[i])
1986 return -ENOMEM;
1987
1988 if (ring->nr_pages > 1)
1989 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1990 }
1991
1992 if (ring->vmem_size) {
1993 *ring->vmem = vzalloc(ring->vmem_size);
1994 if (!(*ring->vmem))
1995 return -ENOMEM;
1996 }
1997 return 0;
1998}
1999
2000static void bnxt_free_rx_rings(struct bnxt *bp)
2001{
2002 int i;
2003
Michael Chanb6ab4b02016-01-02 23:44:59 -05002004 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002005 return;
2006
2007 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002008 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002009 struct bnxt_ring_struct *ring;
2010
Michael Chanc0c050c2015-10-22 16:01:17 -04002011 kfree(rxr->rx_tpa);
2012 rxr->rx_tpa = NULL;
2013
2014 kfree(rxr->rx_agg_bmap);
2015 rxr->rx_agg_bmap = NULL;
2016
2017 ring = &rxr->rx_ring_struct;
2018 bnxt_free_ring(bp, ring);
2019
2020 ring = &rxr->rx_agg_ring_struct;
2021 bnxt_free_ring(bp, ring);
2022 }
2023}
2024
2025static int bnxt_alloc_rx_rings(struct bnxt *bp)
2026{
2027 int i, rc, agg_rings = 0, tpa_rings = 0;
2028
Michael Chanb6ab4b02016-01-02 23:44:59 -05002029 if (!bp->rx_ring)
2030 return -ENOMEM;
2031
Michael Chanc0c050c2015-10-22 16:01:17 -04002032 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2033 agg_rings = 1;
2034
2035 if (bp->flags & BNXT_FLAG_TPA)
2036 tpa_rings = 1;
2037
2038 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002039 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002040 struct bnxt_ring_struct *ring;
2041
Michael Chanc0c050c2015-10-22 16:01:17 -04002042 ring = &rxr->rx_ring_struct;
2043
2044 rc = bnxt_alloc_ring(bp, ring);
2045 if (rc)
2046 return rc;
2047
2048 if (agg_rings) {
2049 u16 mem_size;
2050
2051 ring = &rxr->rx_agg_ring_struct;
2052 rc = bnxt_alloc_ring(bp, ring);
2053 if (rc)
2054 return rc;
2055
2056 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2057 mem_size = rxr->rx_agg_bmap_size / 8;
2058 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2059 if (!rxr->rx_agg_bmap)
2060 return -ENOMEM;
2061
2062 if (tpa_rings) {
2063 rxr->rx_tpa = kcalloc(MAX_TPA,
2064 sizeof(struct bnxt_tpa_info),
2065 GFP_KERNEL);
2066 if (!rxr->rx_tpa)
2067 return -ENOMEM;
2068 }
2069 }
2070 }
2071 return 0;
2072}
2073
2074static void bnxt_free_tx_rings(struct bnxt *bp)
2075{
2076 int i;
2077 struct pci_dev *pdev = bp->pdev;
2078
Michael Chanb6ab4b02016-01-02 23:44:59 -05002079 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002080 return;
2081
2082 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002083 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002084 struct bnxt_ring_struct *ring;
2085
Michael Chanc0c050c2015-10-22 16:01:17 -04002086 if (txr->tx_push) {
2087 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2088 txr->tx_push, txr->tx_push_mapping);
2089 txr->tx_push = NULL;
2090 }
2091
2092 ring = &txr->tx_ring_struct;
2093
2094 bnxt_free_ring(bp, ring);
2095 }
2096}
2097
2098static int bnxt_alloc_tx_rings(struct bnxt *bp)
2099{
2100 int i, j, rc;
2101 struct pci_dev *pdev = bp->pdev;
2102
2103 bp->tx_push_size = 0;
2104 if (bp->tx_push_thresh) {
2105 int push_size;
2106
2107 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2108 bp->tx_push_thresh);
2109
Michael Chan4419dbe2016-02-10 17:33:49 -05002110 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002111 push_size = 0;
2112 bp->tx_push_thresh = 0;
2113 }
2114
2115 bp->tx_push_size = push_size;
2116 }
2117
2118 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002119 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002120 struct bnxt_ring_struct *ring;
2121
Michael Chanc0c050c2015-10-22 16:01:17 -04002122 ring = &txr->tx_ring_struct;
2123
2124 rc = bnxt_alloc_ring(bp, ring);
2125 if (rc)
2126 return rc;
2127
2128 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002129 dma_addr_t mapping;
2130
2131 /* One pre-allocated DMA buffer to backup
2132 * TX push operation
2133 */
2134 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2135 bp->tx_push_size,
2136 &txr->tx_push_mapping,
2137 GFP_KERNEL);
2138
2139 if (!txr->tx_push)
2140 return -ENOMEM;
2141
Michael Chanc0c050c2015-10-22 16:01:17 -04002142 mapping = txr->tx_push_mapping +
2143 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002144 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002145
Michael Chan4419dbe2016-02-10 17:33:49 -05002146 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002147 }
2148 ring->queue_id = bp->q_info[j].queue_id;
2149 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2150 j++;
2151 }
2152 return 0;
2153}
2154
2155static void bnxt_free_cp_rings(struct bnxt *bp)
2156{
2157 int i;
2158
2159 if (!bp->bnapi)
2160 return;
2161
2162 for (i = 0; i < bp->cp_nr_rings; i++) {
2163 struct bnxt_napi *bnapi = bp->bnapi[i];
2164 struct bnxt_cp_ring_info *cpr;
2165 struct bnxt_ring_struct *ring;
2166
2167 if (!bnapi)
2168 continue;
2169
2170 cpr = &bnapi->cp_ring;
2171 ring = &cpr->cp_ring_struct;
2172
2173 bnxt_free_ring(bp, ring);
2174 }
2175}
2176
2177static int bnxt_alloc_cp_rings(struct bnxt *bp)
2178{
2179 int i, rc;
2180
2181 for (i = 0; i < bp->cp_nr_rings; i++) {
2182 struct bnxt_napi *bnapi = bp->bnapi[i];
2183 struct bnxt_cp_ring_info *cpr;
2184 struct bnxt_ring_struct *ring;
2185
2186 if (!bnapi)
2187 continue;
2188
2189 cpr = &bnapi->cp_ring;
2190 ring = &cpr->cp_ring_struct;
2191
2192 rc = bnxt_alloc_ring(bp, ring);
2193 if (rc)
2194 return rc;
2195 }
2196 return 0;
2197}
2198
2199static void bnxt_init_ring_struct(struct bnxt *bp)
2200{
2201 int i;
2202
2203 for (i = 0; i < bp->cp_nr_rings; i++) {
2204 struct bnxt_napi *bnapi = bp->bnapi[i];
2205 struct bnxt_cp_ring_info *cpr;
2206 struct bnxt_rx_ring_info *rxr;
2207 struct bnxt_tx_ring_info *txr;
2208 struct bnxt_ring_struct *ring;
2209
2210 if (!bnapi)
2211 continue;
2212
2213 cpr = &bnapi->cp_ring;
2214 ring = &cpr->cp_ring_struct;
2215 ring->nr_pages = bp->cp_nr_pages;
2216 ring->page_size = HW_CMPD_RING_SIZE;
2217 ring->pg_arr = (void **)cpr->cp_desc_ring;
2218 ring->dma_arr = cpr->cp_desc_mapping;
2219 ring->vmem_size = 0;
2220
Michael Chanb6ab4b02016-01-02 23:44:59 -05002221 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002222 if (!rxr)
2223 goto skip_rx;
2224
Michael Chanc0c050c2015-10-22 16:01:17 -04002225 ring = &rxr->rx_ring_struct;
2226 ring->nr_pages = bp->rx_nr_pages;
2227 ring->page_size = HW_RXBD_RING_SIZE;
2228 ring->pg_arr = (void **)rxr->rx_desc_ring;
2229 ring->dma_arr = rxr->rx_desc_mapping;
2230 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2231 ring->vmem = (void **)&rxr->rx_buf_ring;
2232
2233 ring = &rxr->rx_agg_ring_struct;
2234 ring->nr_pages = bp->rx_agg_nr_pages;
2235 ring->page_size = HW_RXBD_RING_SIZE;
2236 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2237 ring->dma_arr = rxr->rx_agg_desc_mapping;
2238 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2239 ring->vmem = (void **)&rxr->rx_agg_ring;
2240
Michael Chan3b2b7d92016-01-02 23:45:00 -05002241skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002242 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002243 if (!txr)
2244 continue;
2245
Michael Chanc0c050c2015-10-22 16:01:17 -04002246 ring = &txr->tx_ring_struct;
2247 ring->nr_pages = bp->tx_nr_pages;
2248 ring->page_size = HW_RXBD_RING_SIZE;
2249 ring->pg_arr = (void **)txr->tx_desc_ring;
2250 ring->dma_arr = txr->tx_desc_mapping;
2251 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2252 ring->vmem = (void **)&txr->tx_buf_ring;
2253 }
2254}
2255
2256static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2257{
2258 int i;
2259 u32 prod;
2260 struct rx_bd **rx_buf_ring;
2261
2262 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2263 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2264 int j;
2265 struct rx_bd *rxbd;
2266
2267 rxbd = rx_buf_ring[i];
2268 if (!rxbd)
2269 continue;
2270
2271 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2272 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2273 rxbd->rx_bd_opaque = prod;
2274 }
2275 }
2276}
2277
2278static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2279{
2280 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002281 struct bnxt_rx_ring_info *rxr;
2282 struct bnxt_ring_struct *ring;
2283 u32 prod, type;
2284 int i;
2285
Michael Chanc0c050c2015-10-22 16:01:17 -04002286 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2287 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2288
2289 if (NET_IP_ALIGN == 2)
2290 type |= RX_BD_FLAGS_SOP;
2291
Michael Chanb6ab4b02016-01-02 23:44:59 -05002292 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002293 ring = &rxr->rx_ring_struct;
2294 bnxt_init_rxbd_pages(ring, type);
2295
2296 prod = rxr->rx_prod;
2297 for (i = 0; i < bp->rx_ring_size; i++) {
2298 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2299 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2300 ring_nr, i, bp->rx_ring_size);
2301 break;
2302 }
2303 prod = NEXT_RX(prod);
2304 }
2305 rxr->rx_prod = prod;
2306 ring->fw_ring_id = INVALID_HW_RING_ID;
2307
Michael Chanedd0c2c2015-12-27 18:19:19 -05002308 ring = &rxr->rx_agg_ring_struct;
2309 ring->fw_ring_id = INVALID_HW_RING_ID;
2310
Michael Chanc0c050c2015-10-22 16:01:17 -04002311 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2312 return 0;
2313
Michael Chan2839f282016-04-25 02:30:50 -04002314 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002315 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2316
2317 bnxt_init_rxbd_pages(ring, type);
2318
2319 prod = rxr->rx_agg_prod;
2320 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2321 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2322 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2323 ring_nr, i, bp->rx_ring_size);
2324 break;
2325 }
2326 prod = NEXT_RX_AGG(prod);
2327 }
2328 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002329
2330 if (bp->flags & BNXT_FLAG_TPA) {
2331 if (rxr->rx_tpa) {
2332 u8 *data;
2333 dma_addr_t mapping;
2334
2335 for (i = 0; i < MAX_TPA; i++) {
2336 data = __bnxt_alloc_rx_data(bp, &mapping,
2337 GFP_KERNEL);
2338 if (!data)
2339 return -ENOMEM;
2340
2341 rxr->rx_tpa[i].data = data;
Michael Chan6bb19472017-02-06 16:55:32 -05002342 rxr->rx_tpa[i].data_ptr = data + BNXT_RX_OFFSET;
Michael Chanc0c050c2015-10-22 16:01:17 -04002343 rxr->rx_tpa[i].mapping = mapping;
2344 }
2345 } else {
2346 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2347 return -ENOMEM;
2348 }
2349 }
2350
2351 return 0;
2352}
2353
2354static int bnxt_init_rx_rings(struct bnxt *bp)
2355{
2356 int i, rc = 0;
2357
2358 for (i = 0; i < bp->rx_nr_rings; i++) {
2359 rc = bnxt_init_one_rx_ring(bp, i);
2360 if (rc)
2361 break;
2362 }
2363
2364 return rc;
2365}
2366
2367static int bnxt_init_tx_rings(struct bnxt *bp)
2368{
2369 u16 i;
2370
2371 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2372 MAX_SKB_FRAGS + 1);
2373
2374 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002375 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002376 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2377
2378 ring->fw_ring_id = INVALID_HW_RING_ID;
2379 }
2380
2381 return 0;
2382}
2383
2384static void bnxt_free_ring_grps(struct bnxt *bp)
2385{
2386 kfree(bp->grp_info);
2387 bp->grp_info = NULL;
2388}
2389
2390static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2391{
2392 int i;
2393
2394 if (irq_re_init) {
2395 bp->grp_info = kcalloc(bp->cp_nr_rings,
2396 sizeof(struct bnxt_ring_grp_info),
2397 GFP_KERNEL);
2398 if (!bp->grp_info)
2399 return -ENOMEM;
2400 }
2401 for (i = 0; i < bp->cp_nr_rings; i++) {
2402 if (irq_re_init)
2403 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2404 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2405 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2406 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2407 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2408 }
2409 return 0;
2410}
2411
2412static void bnxt_free_vnics(struct bnxt *bp)
2413{
2414 kfree(bp->vnic_info);
2415 bp->vnic_info = NULL;
2416 bp->nr_vnics = 0;
2417}
2418
2419static int bnxt_alloc_vnics(struct bnxt *bp)
2420{
2421 int num_vnics = 1;
2422
2423#ifdef CONFIG_RFS_ACCEL
2424 if (bp->flags & BNXT_FLAG_RFS)
2425 num_vnics += bp->rx_nr_rings;
2426#endif
2427
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002428 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2429 num_vnics++;
2430
Michael Chanc0c050c2015-10-22 16:01:17 -04002431 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2432 GFP_KERNEL);
2433 if (!bp->vnic_info)
2434 return -ENOMEM;
2435
2436 bp->nr_vnics = num_vnics;
2437 return 0;
2438}
2439
2440static void bnxt_init_vnics(struct bnxt *bp)
2441{
2442 int i;
2443
2444 for (i = 0; i < bp->nr_vnics; i++) {
2445 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2446
2447 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002448 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2449 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002450 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2451
2452 if (bp->vnic_info[i].rss_hash_key) {
2453 if (i == 0)
2454 prandom_bytes(vnic->rss_hash_key,
2455 HW_HASH_KEY_SIZE);
2456 else
2457 memcpy(vnic->rss_hash_key,
2458 bp->vnic_info[0].rss_hash_key,
2459 HW_HASH_KEY_SIZE);
2460 }
2461 }
2462}
2463
2464static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2465{
2466 int pages;
2467
2468 pages = ring_size / desc_per_pg;
2469
2470 if (!pages)
2471 return 1;
2472
2473 pages++;
2474
2475 while (pages & (pages - 1))
2476 pages++;
2477
2478 return pages;
2479}
2480
2481static void bnxt_set_tpa_flags(struct bnxt *bp)
2482{
2483 bp->flags &= ~BNXT_FLAG_TPA;
Michael Chan341138c2017-01-13 01:32:01 -05002484 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2485 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04002486 if (bp->dev->features & NETIF_F_LRO)
2487 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002488 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002489 bp->flags |= BNXT_FLAG_GRO;
2490}
2491
2492/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2493 * be set on entry.
2494 */
2495void bnxt_set_ring_params(struct bnxt *bp)
2496{
2497 u32 ring_size, rx_size, rx_space;
2498 u32 agg_factor = 0, agg_ring_size = 0;
2499
2500 /* 8 for CRC and VLAN */
2501 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2502
2503 rx_space = rx_size + NET_SKB_PAD +
2504 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2505
2506 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2507 ring_size = bp->rx_ring_size;
2508 bp->rx_agg_ring_size = 0;
2509 bp->rx_agg_nr_pages = 0;
2510
2511 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002512 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002513
2514 bp->flags &= ~BNXT_FLAG_JUMBO;
Michael Chanbdbd1eb2016-12-29 12:13:43 -05002515 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002516 u32 jumbo_factor;
2517
2518 bp->flags |= BNXT_FLAG_JUMBO;
2519 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2520 if (jumbo_factor > agg_factor)
2521 agg_factor = jumbo_factor;
2522 }
2523 agg_ring_size = ring_size * agg_factor;
2524
2525 if (agg_ring_size) {
2526 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2527 RX_DESC_CNT);
2528 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2529 u32 tmp = agg_ring_size;
2530
2531 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2532 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2533 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2534 tmp, agg_ring_size);
2535 }
2536 bp->rx_agg_ring_size = agg_ring_size;
2537 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2538 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2539 rx_space = rx_size + NET_SKB_PAD +
2540 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2541 }
2542
2543 bp->rx_buf_use_size = rx_size;
2544 bp->rx_buf_size = rx_space;
2545
2546 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2547 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2548
2549 ring_size = bp->tx_ring_size;
2550 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2551 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2552
2553 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2554 bp->cp_ring_size = ring_size;
2555
2556 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2557 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2558 bp->cp_nr_pages = MAX_CP_PAGES;
2559 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2560 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2561 ring_size, bp->cp_ring_size);
2562 }
2563 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2564 bp->cp_ring_mask = bp->cp_bit - 1;
2565}
2566
Michael Chan6bb19472017-02-06 16:55:32 -05002567static int bnxt_set_rx_skb_mode(struct bnxt *bp)
2568{
2569 bp->rx_skb_func = bnxt_rx_skb;
2570 return 0;
2571}
2572
Michael Chanc0c050c2015-10-22 16:01:17 -04002573static void bnxt_free_vnic_attributes(struct bnxt *bp)
2574{
2575 int i;
2576 struct bnxt_vnic_info *vnic;
2577 struct pci_dev *pdev = bp->pdev;
2578
2579 if (!bp->vnic_info)
2580 return;
2581
2582 for (i = 0; i < bp->nr_vnics; i++) {
2583 vnic = &bp->vnic_info[i];
2584
2585 kfree(vnic->fw_grp_ids);
2586 vnic->fw_grp_ids = NULL;
2587
2588 kfree(vnic->uc_list);
2589 vnic->uc_list = NULL;
2590
2591 if (vnic->mc_list) {
2592 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2593 vnic->mc_list, vnic->mc_list_mapping);
2594 vnic->mc_list = NULL;
2595 }
2596
2597 if (vnic->rss_table) {
2598 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2599 vnic->rss_table,
2600 vnic->rss_table_dma_addr);
2601 vnic->rss_table = NULL;
2602 }
2603
2604 vnic->rss_hash_key = NULL;
2605 vnic->flags = 0;
2606 }
2607}
2608
2609static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2610{
2611 int i, rc = 0, size;
2612 struct bnxt_vnic_info *vnic;
2613 struct pci_dev *pdev = bp->pdev;
2614 int max_rings;
2615
2616 for (i = 0; i < bp->nr_vnics; i++) {
2617 vnic = &bp->vnic_info[i];
2618
2619 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2620 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2621
2622 if (mem_size > 0) {
2623 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2624 if (!vnic->uc_list) {
2625 rc = -ENOMEM;
2626 goto out;
2627 }
2628 }
2629 }
2630
2631 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2632 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2633 vnic->mc_list =
2634 dma_alloc_coherent(&pdev->dev,
2635 vnic->mc_list_size,
2636 &vnic->mc_list_mapping,
2637 GFP_KERNEL);
2638 if (!vnic->mc_list) {
2639 rc = -ENOMEM;
2640 goto out;
2641 }
2642 }
2643
2644 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2645 max_rings = bp->rx_nr_rings;
2646 else
2647 max_rings = 1;
2648
2649 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2650 if (!vnic->fw_grp_ids) {
2651 rc = -ENOMEM;
2652 goto out;
2653 }
2654
Michael Chanae10ae72016-12-29 12:13:38 -05002655 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2656 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2657 continue;
2658
Michael Chanc0c050c2015-10-22 16:01:17 -04002659 /* Allocate rss table and hash key */
2660 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2661 &vnic->rss_table_dma_addr,
2662 GFP_KERNEL);
2663 if (!vnic->rss_table) {
2664 rc = -ENOMEM;
2665 goto out;
2666 }
2667
2668 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2669
2670 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2671 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2672 }
2673 return 0;
2674
2675out:
2676 return rc;
2677}
2678
2679static void bnxt_free_hwrm_resources(struct bnxt *bp)
2680{
2681 struct pci_dev *pdev = bp->pdev;
2682
2683 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2684 bp->hwrm_cmd_resp_dma_addr);
2685
2686 bp->hwrm_cmd_resp_addr = NULL;
2687 if (bp->hwrm_dbg_resp_addr) {
2688 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2689 bp->hwrm_dbg_resp_addr,
2690 bp->hwrm_dbg_resp_dma_addr);
2691
2692 bp->hwrm_dbg_resp_addr = NULL;
2693 }
2694}
2695
2696static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2697{
2698 struct pci_dev *pdev = bp->pdev;
2699
2700 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2701 &bp->hwrm_cmd_resp_dma_addr,
2702 GFP_KERNEL);
2703 if (!bp->hwrm_cmd_resp_addr)
2704 return -ENOMEM;
2705 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2706 HWRM_DBG_REG_BUF_SIZE,
2707 &bp->hwrm_dbg_resp_dma_addr,
2708 GFP_KERNEL);
2709 if (!bp->hwrm_dbg_resp_addr)
2710 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2711
2712 return 0;
2713}
2714
2715static void bnxt_free_stats(struct bnxt *bp)
2716{
2717 u32 size, i;
2718 struct pci_dev *pdev = bp->pdev;
2719
Michael Chan3bdf56c2016-03-07 15:38:45 -05002720 if (bp->hw_rx_port_stats) {
2721 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2722 bp->hw_rx_port_stats,
2723 bp->hw_rx_port_stats_map);
2724 bp->hw_rx_port_stats = NULL;
2725 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2726 }
2727
Michael Chanc0c050c2015-10-22 16:01:17 -04002728 if (!bp->bnapi)
2729 return;
2730
2731 size = sizeof(struct ctx_hw_stats);
2732
2733 for (i = 0; i < bp->cp_nr_rings; i++) {
2734 struct bnxt_napi *bnapi = bp->bnapi[i];
2735 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2736
2737 if (cpr->hw_stats) {
2738 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2739 cpr->hw_stats_map);
2740 cpr->hw_stats = NULL;
2741 }
2742 }
2743}
2744
2745static int bnxt_alloc_stats(struct bnxt *bp)
2746{
2747 u32 size, i;
2748 struct pci_dev *pdev = bp->pdev;
2749
2750 size = sizeof(struct ctx_hw_stats);
2751
2752 for (i = 0; i < bp->cp_nr_rings; i++) {
2753 struct bnxt_napi *bnapi = bp->bnapi[i];
2754 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2755
2756 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2757 &cpr->hw_stats_map,
2758 GFP_KERNEL);
2759 if (!cpr->hw_stats)
2760 return -ENOMEM;
2761
2762 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2763 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002764
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002765 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002766 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2767 sizeof(struct tx_port_stats) + 1024;
2768
2769 bp->hw_rx_port_stats =
2770 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2771 &bp->hw_rx_port_stats_map,
2772 GFP_KERNEL);
2773 if (!bp->hw_rx_port_stats)
2774 return -ENOMEM;
2775
2776 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2777 512;
2778 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2779 sizeof(struct rx_port_stats) + 512;
2780 bp->flags |= BNXT_FLAG_PORT_STATS;
2781 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002782 return 0;
2783}
2784
2785static void bnxt_clear_ring_indices(struct bnxt *bp)
2786{
2787 int i;
2788
2789 if (!bp->bnapi)
2790 return;
2791
2792 for (i = 0; i < bp->cp_nr_rings; i++) {
2793 struct bnxt_napi *bnapi = bp->bnapi[i];
2794 struct bnxt_cp_ring_info *cpr;
2795 struct bnxt_rx_ring_info *rxr;
2796 struct bnxt_tx_ring_info *txr;
2797
2798 if (!bnapi)
2799 continue;
2800
2801 cpr = &bnapi->cp_ring;
2802 cpr->cp_raw_cons = 0;
2803
Michael Chanb6ab4b02016-01-02 23:44:59 -05002804 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002805 if (txr) {
2806 txr->tx_prod = 0;
2807 txr->tx_cons = 0;
2808 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002809
Michael Chanb6ab4b02016-01-02 23:44:59 -05002810 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002811 if (rxr) {
2812 rxr->rx_prod = 0;
2813 rxr->rx_agg_prod = 0;
2814 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002815 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002816 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002817 }
2818}
2819
2820static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2821{
2822#ifdef CONFIG_RFS_ACCEL
2823 int i;
2824
2825 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2826 * safe to delete the hash table.
2827 */
2828 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2829 struct hlist_head *head;
2830 struct hlist_node *tmp;
2831 struct bnxt_ntuple_filter *fltr;
2832
2833 head = &bp->ntp_fltr_hash_tbl[i];
2834 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2835 hlist_del(&fltr->hash);
2836 kfree(fltr);
2837 }
2838 }
2839 if (irq_reinit) {
2840 kfree(bp->ntp_fltr_bmap);
2841 bp->ntp_fltr_bmap = NULL;
2842 }
2843 bp->ntp_fltr_count = 0;
2844#endif
2845}
2846
2847static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2848{
2849#ifdef CONFIG_RFS_ACCEL
2850 int i, rc = 0;
2851
2852 if (!(bp->flags & BNXT_FLAG_RFS))
2853 return 0;
2854
2855 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2856 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2857
2858 bp->ntp_fltr_count = 0;
2859 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2860 GFP_KERNEL);
2861
2862 if (!bp->ntp_fltr_bmap)
2863 rc = -ENOMEM;
2864
2865 return rc;
2866#else
2867 return 0;
2868#endif
2869}
2870
2871static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2872{
2873 bnxt_free_vnic_attributes(bp);
2874 bnxt_free_tx_rings(bp);
2875 bnxt_free_rx_rings(bp);
2876 bnxt_free_cp_rings(bp);
2877 bnxt_free_ntp_fltrs(bp, irq_re_init);
2878 if (irq_re_init) {
2879 bnxt_free_stats(bp);
2880 bnxt_free_ring_grps(bp);
2881 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002882 kfree(bp->tx_ring);
2883 bp->tx_ring = NULL;
2884 kfree(bp->rx_ring);
2885 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002886 kfree(bp->bnapi);
2887 bp->bnapi = NULL;
2888 } else {
2889 bnxt_clear_ring_indices(bp);
2890 }
2891}
2892
2893static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2894{
Michael Chan01657bc2016-01-02 23:45:03 -05002895 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002896 void *bnapi;
2897
2898 if (irq_re_init) {
2899 /* Allocate bnapi mem pointer array and mem block for
2900 * all queues
2901 */
2902 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2903 bp->cp_nr_rings);
2904 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2905 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2906 if (!bnapi)
2907 return -ENOMEM;
2908
2909 bp->bnapi = bnapi;
2910 bnapi += arr_size;
2911 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2912 bp->bnapi[i] = bnapi;
2913 bp->bnapi[i]->index = i;
2914 bp->bnapi[i]->bp = bp;
2915 }
2916
Michael Chanb6ab4b02016-01-02 23:44:59 -05002917 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2918 sizeof(struct bnxt_rx_ring_info),
2919 GFP_KERNEL);
2920 if (!bp->rx_ring)
2921 return -ENOMEM;
2922
2923 for (i = 0; i < bp->rx_nr_rings; i++) {
2924 bp->rx_ring[i].bnapi = bp->bnapi[i];
2925 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2926 }
2927
2928 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2929 sizeof(struct bnxt_tx_ring_info),
2930 GFP_KERNEL);
2931 if (!bp->tx_ring)
2932 return -ENOMEM;
2933
Michael Chan01657bc2016-01-02 23:45:03 -05002934 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2935 j = 0;
2936 else
2937 j = bp->rx_nr_rings;
2938
2939 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2940 bp->tx_ring[i].bnapi = bp->bnapi[j];
2941 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002942 }
2943
Michael Chanc0c050c2015-10-22 16:01:17 -04002944 rc = bnxt_alloc_stats(bp);
2945 if (rc)
2946 goto alloc_mem_err;
2947
2948 rc = bnxt_alloc_ntp_fltrs(bp);
2949 if (rc)
2950 goto alloc_mem_err;
2951
2952 rc = bnxt_alloc_vnics(bp);
2953 if (rc)
2954 goto alloc_mem_err;
2955 }
2956
2957 bnxt_init_ring_struct(bp);
2958
2959 rc = bnxt_alloc_rx_rings(bp);
2960 if (rc)
2961 goto alloc_mem_err;
2962
2963 rc = bnxt_alloc_tx_rings(bp);
2964 if (rc)
2965 goto alloc_mem_err;
2966
2967 rc = bnxt_alloc_cp_rings(bp);
2968 if (rc)
2969 goto alloc_mem_err;
2970
2971 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2972 BNXT_VNIC_UCAST_FLAG;
2973 rc = bnxt_alloc_vnic_attributes(bp);
2974 if (rc)
2975 goto alloc_mem_err;
2976 return 0;
2977
2978alloc_mem_err:
2979 bnxt_free_mem(bp, true);
2980 return rc;
2981}
2982
Michael Chan9d8bc092016-12-29 12:13:33 -05002983static void bnxt_disable_int(struct bnxt *bp)
2984{
2985 int i;
2986
2987 if (!bp->bnapi)
2988 return;
2989
2990 for (i = 0; i < bp->cp_nr_rings; i++) {
2991 struct bnxt_napi *bnapi = bp->bnapi[i];
2992 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2993
2994 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2995 }
2996}
2997
2998static void bnxt_disable_int_sync(struct bnxt *bp)
2999{
3000 int i;
3001
3002 atomic_inc(&bp->intr_sem);
3003
3004 bnxt_disable_int(bp);
3005 for (i = 0; i < bp->cp_nr_rings; i++)
3006 synchronize_irq(bp->irq_tbl[i].vector);
3007}
3008
3009static void bnxt_enable_int(struct bnxt *bp)
3010{
3011 int i;
3012
3013 atomic_set(&bp->intr_sem, 0);
3014 for (i = 0; i < bp->cp_nr_rings; i++) {
3015 struct bnxt_napi *bnapi = bp->bnapi[i];
3016 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3017
3018 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3019 }
3020}
3021
Michael Chanc0c050c2015-10-22 16:01:17 -04003022void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3023 u16 cmpl_ring, u16 target_id)
3024{
Michael Chana8643e12016-02-26 04:00:05 -05003025 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003026
Michael Chana8643e12016-02-26 04:00:05 -05003027 req->req_type = cpu_to_le16(req_type);
3028 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3029 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003030 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3031}
3032
Michael Chanfbfbc482016-02-26 04:00:07 -05003033static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3034 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003035{
Michael Chana11fa2b2016-05-15 03:04:47 -04003036 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003037 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003038 u32 *data = msg;
3039 __le32 *resp_len, *valid;
3040 u16 cp_ring_id, len = 0;
3041 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3042
Michael Chana8643e12016-02-26 04:00:05 -05003043 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003044 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003045 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003046 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3047
3048 /* Write request msg to hwrm channel */
3049 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3050
Michael Chane6ef2692016-03-28 19:46:05 -04003051 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003052 writel(0, bp->bar0 + i);
3053
Michael Chanc0c050c2015-10-22 16:01:17 -04003054 /* currently supports only one outstanding message */
3055 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003056 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003057
3058 /* Ring channel doorbell */
3059 writel(1, bp->bar0 + 0x100);
3060
Michael Chanff4fe812016-02-26 04:00:04 -05003061 if (!timeout)
3062 timeout = DFLT_HWRM_CMD_TIMEOUT;
3063
Michael Chanc0c050c2015-10-22 16:01:17 -04003064 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003065 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003066 if (intr_process) {
3067 /* Wait until hwrm response cmpl interrupt is processed */
3068 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003069 i++ < tmo_count) {
3070 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003071 }
3072
3073 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3074 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003075 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003076 return -1;
3077 }
3078 } else {
3079 /* Check if response len is updated */
3080 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003081 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003082 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3083 HWRM_RESP_LEN_SFT;
3084 if (len)
3085 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003086 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003087 }
3088
Michael Chana11fa2b2016-05-15 03:04:47 -04003089 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003090 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003091 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003092 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003093 return -1;
3094 }
3095
3096 /* Last word of resp contains valid bit */
3097 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003098 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003099 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3100 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003101 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003102 }
3103
Michael Chana11fa2b2016-05-15 03:04:47 -04003104 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003105 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003106 timeout, le16_to_cpu(req->req_type),
3107 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003108 return -1;
3109 }
3110 }
3111
3112 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003113 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003114 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3115 le16_to_cpu(resp->req_type),
3116 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003117 return rc;
3118}
3119
3120int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3121{
3122 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003123}
3124
3125int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3126{
3127 int rc;
3128
3129 mutex_lock(&bp->hwrm_cmd_lock);
3130 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3131 mutex_unlock(&bp->hwrm_cmd_lock);
3132 return rc;
3133}
3134
Michael Chan90e209212016-02-26 04:00:08 -05003135int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3136 int timeout)
3137{
3138 int rc;
3139
3140 mutex_lock(&bp->hwrm_cmd_lock);
3141 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3142 mutex_unlock(&bp->hwrm_cmd_lock);
3143 return rc;
3144}
3145
Michael Chana1653b12016-12-07 00:26:20 -05003146int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3147 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003148{
3149 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003150 DECLARE_BITMAP(async_events_bmap, 256);
3151 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003152 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003153
3154 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3155
3156 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003157 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003158
Michael Chan25be8622016-04-05 14:09:00 -04003159 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3160 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3161 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3162
Michael Chana1653b12016-12-07 00:26:20 -05003163 if (bmap && bmap_size) {
3164 for (i = 0; i < bmap_size; i++) {
3165 if (test_bit(i, bmap))
3166 __set_bit(i, async_events_bmap);
3167 }
3168 }
3169
Michael Chan25be8622016-04-05 14:09:00 -04003170 for (i = 0; i < 8; i++)
3171 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3172
Michael Chana1653b12016-12-07 00:26:20 -05003173 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3174}
3175
3176static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3177{
3178 struct hwrm_func_drv_rgtr_input req = {0};
3179
3180 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3181
3182 req.enables =
3183 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3184 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3185
Michael Chan11f15ed2016-04-05 14:08:55 -04003186 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003187 req.ver_maj = DRV_VER_MAJ;
3188 req.ver_min = DRV_VER_MIN;
3189 req.ver_upd = DRV_VER_UPD;
3190
3191 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003192 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003193 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003194 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003195
Michael Chande68f5de2015-12-09 19:35:41 -05003196 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003197 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3198 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3199
Michael Chande68f5de2015-12-09 19:35:41 -05003200 for (i = 0; i < 8; i++)
3201 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3202
Michael Chanc0c050c2015-10-22 16:01:17 -04003203 req.enables |=
3204 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3205 }
3206
3207 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3208}
3209
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003210static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3211{
3212 struct hwrm_func_drv_unrgtr_input req = {0};
3213
3214 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3215 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3216}
3217
Michael Chanc0c050c2015-10-22 16:01:17 -04003218static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3219{
3220 u32 rc = 0;
3221 struct hwrm_tunnel_dst_port_free_input req = {0};
3222
3223 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3224 req.tunnel_type = tunnel_type;
3225
3226 switch (tunnel_type) {
3227 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3228 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3229 break;
3230 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3231 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3232 break;
3233 default:
3234 break;
3235 }
3236
3237 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3238 if (rc)
3239 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3240 rc);
3241 return rc;
3242}
3243
3244static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3245 u8 tunnel_type)
3246{
3247 u32 rc = 0;
3248 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3249 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3250
3251 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3252
3253 req.tunnel_type = tunnel_type;
3254 req.tunnel_dst_port_val = port;
3255
3256 mutex_lock(&bp->hwrm_cmd_lock);
3257 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3258 if (rc) {
3259 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3260 rc);
3261 goto err_out;
3262 }
3263
Christophe Jaillet57aac712016-11-22 06:14:40 +01003264 switch (tunnel_type) {
3265 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003266 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003267 break;
3268 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003269 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003270 break;
3271 default:
3272 break;
3273 }
3274
Michael Chanc0c050c2015-10-22 16:01:17 -04003275err_out:
3276 mutex_unlock(&bp->hwrm_cmd_lock);
3277 return rc;
3278}
3279
3280static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3281{
3282 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3283 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3284
3285 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003286 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003287
3288 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3289 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3290 req.mask = cpu_to_le32(vnic->rx_mask);
3291 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3292}
3293
3294#ifdef CONFIG_RFS_ACCEL
3295static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3296 struct bnxt_ntuple_filter *fltr)
3297{
3298 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3299
3300 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3301 req.ntuple_filter_id = fltr->filter_id;
3302 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3303}
3304
3305#define BNXT_NTP_FLTR_FLAGS \
3306 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3307 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3308 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3309 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3310 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3311 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3312 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3313 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3314 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3315 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3316 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3317 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3318 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003319 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003320
3321static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3322 struct bnxt_ntuple_filter *fltr)
3323{
3324 int rc = 0;
3325 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3326 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3327 bp->hwrm_cmd_resp_addr;
3328 struct flow_keys *keys = &fltr->fkeys;
3329 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3330
3331 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003332 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003333
3334 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3335
3336 req.ethertype = htons(ETH_P_IP);
3337 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003338 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003339 req.ip_protocol = keys->basic.ip_proto;
3340
Michael Chandda0e742016-12-29 12:13:40 -05003341 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3342 int i;
3343
3344 req.ethertype = htons(ETH_P_IPV6);
3345 req.ip_addr_type =
3346 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3347 *(struct in6_addr *)&req.src_ipaddr[0] =
3348 keys->addrs.v6addrs.src;
3349 *(struct in6_addr *)&req.dst_ipaddr[0] =
3350 keys->addrs.v6addrs.dst;
3351 for (i = 0; i < 4; i++) {
3352 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3353 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3354 }
3355 } else {
3356 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3357 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3358 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3359 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3360 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003361
3362 req.src_port = keys->ports.src;
3363 req.src_port_mask = cpu_to_be16(0xffff);
3364 req.dst_port = keys->ports.dst;
3365 req.dst_port_mask = cpu_to_be16(0xffff);
3366
Michael Chanc1935542015-12-27 18:19:28 -05003367 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003368 mutex_lock(&bp->hwrm_cmd_lock);
3369 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3370 if (!rc)
3371 fltr->filter_id = resp->ntuple_filter_id;
3372 mutex_unlock(&bp->hwrm_cmd_lock);
3373 return rc;
3374}
3375#endif
3376
3377static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3378 u8 *mac_addr)
3379{
3380 u32 rc = 0;
3381 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3382 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3383
3384 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003385 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3386 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3387 req.flags |=
3388 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003389 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003390 req.enables =
3391 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003392 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003393 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3394 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3395 req.l2_addr_mask[0] = 0xff;
3396 req.l2_addr_mask[1] = 0xff;
3397 req.l2_addr_mask[2] = 0xff;
3398 req.l2_addr_mask[3] = 0xff;
3399 req.l2_addr_mask[4] = 0xff;
3400 req.l2_addr_mask[5] = 0xff;
3401
3402 mutex_lock(&bp->hwrm_cmd_lock);
3403 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3404 if (!rc)
3405 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3406 resp->l2_filter_id;
3407 mutex_unlock(&bp->hwrm_cmd_lock);
3408 return rc;
3409}
3410
3411static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3412{
3413 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3414 int rc = 0;
3415
3416 /* Any associated ntuple filters will also be cleared by firmware. */
3417 mutex_lock(&bp->hwrm_cmd_lock);
3418 for (i = 0; i < num_of_vnics; i++) {
3419 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3420
3421 for (j = 0; j < vnic->uc_filter_count; j++) {
3422 struct hwrm_cfa_l2_filter_free_input req = {0};
3423
3424 bnxt_hwrm_cmd_hdr_init(bp, &req,
3425 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3426
3427 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3428
3429 rc = _hwrm_send_message(bp, &req, sizeof(req),
3430 HWRM_CMD_TIMEOUT);
3431 }
3432 vnic->uc_filter_count = 0;
3433 }
3434 mutex_unlock(&bp->hwrm_cmd_lock);
3435
3436 return rc;
3437}
3438
3439static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3440{
3441 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3442 struct hwrm_vnic_tpa_cfg_input req = {0};
3443
3444 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3445
3446 if (tpa_flags) {
3447 u16 mss = bp->dev->mtu - 40;
3448 u32 nsegs, n, segs = 0, flags;
3449
3450 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3451 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3452 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3453 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3454 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3455 if (tpa_flags & BNXT_FLAG_GRO)
3456 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3457
3458 req.flags = cpu_to_le32(flags);
3459
3460 req.enables =
3461 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003462 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3463 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003464
3465 /* Number of segs are log2 units, and first packet is not
3466 * included as part of this units.
3467 */
Michael Chan2839f282016-04-25 02:30:50 -04003468 if (mss <= BNXT_RX_PAGE_SIZE) {
3469 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003470 nsegs = (MAX_SKB_FRAGS - 1) * n;
3471 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003472 n = mss / BNXT_RX_PAGE_SIZE;
3473 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003474 n++;
3475 nsegs = (MAX_SKB_FRAGS - n) / n;
3476 }
3477
3478 segs = ilog2(nsegs);
3479 req.max_agg_segs = cpu_to_le16(segs);
3480 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003481
3482 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003483 }
3484 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3485
3486 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3487}
3488
3489static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3490{
3491 u32 i, j, max_rings;
3492 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3493 struct hwrm_vnic_rss_cfg_input req = {0};
3494
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003495 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003496 return 0;
3497
3498 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3499 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003500 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003501 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3502 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3503 max_rings = bp->rx_nr_rings - 1;
3504 else
3505 max_rings = bp->rx_nr_rings;
3506 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003507 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003508 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003509
3510 /* Fill the RSS indirection table with ring group ids */
3511 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3512 if (j == max_rings)
3513 j = 0;
3514 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3515 }
3516
3517 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3518 req.hash_key_tbl_addr =
3519 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3520 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003521 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003522 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3523}
3524
3525static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3526{
3527 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3528 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3529
3530 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3531 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3532 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3533 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3534 req.enables =
3535 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3536 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3537 /* thresholds not implemented in firmware yet */
3538 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3539 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3540 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3541 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3542}
3543
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003544static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3545 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003546{
3547 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3548
3549 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3550 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003551 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003552
3553 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003554 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003555}
3556
3557static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3558{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003559 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003560
3561 for (i = 0; i < bp->nr_vnics; i++) {
3562 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3563
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003564 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3565 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3566 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3567 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003568 }
3569 bp->rsscos_nr_ctxs = 0;
3570}
3571
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003572static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003573{
3574 int rc;
3575 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3576 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3577 bp->hwrm_cmd_resp_addr;
3578
3579 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3580 -1);
3581
3582 mutex_lock(&bp->hwrm_cmd_lock);
3583 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3584 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003585 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003586 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3587 mutex_unlock(&bp->hwrm_cmd_lock);
3588
3589 return rc;
3590}
3591
Michael Chana588e452016-12-07 00:26:21 -05003592int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003593{
Michael Chanb81a90d2016-01-02 23:45:01 -05003594 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003595 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3596 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003597 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003598
3599 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003600
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003601 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3602 /* Only RSS support for now TBD: COS & LB */
3603 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3604 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3605 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3606 VNIC_CFG_REQ_ENABLES_MRU);
Michael Chanae10ae72016-12-29 12:13:38 -05003607 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3608 req.rss_rule =
3609 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3610 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3611 VNIC_CFG_REQ_ENABLES_MRU);
3612 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003613 } else {
3614 req.rss_rule = cpu_to_le16(0xffff);
3615 }
3616
3617 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3618 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003619 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3620 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3621 } else {
3622 req.cos_rule = cpu_to_le16(0xffff);
3623 }
3624
Michael Chanc0c050c2015-10-22 16:01:17 -04003625 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003626 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003627 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003628 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003629 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3630 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003631
Michael Chanb81a90d2016-01-02 23:45:01 -05003632 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003633 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3634 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3635
3636 req.lb_rule = cpu_to_le16(0xffff);
3637 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3638 VLAN_HLEN);
3639
Michael Chancf6645f2016-06-13 02:25:28 -04003640#ifdef CONFIG_BNXT_SRIOV
3641 if (BNXT_VF(bp))
3642 def_vlan = bp->vf.vlan;
3643#endif
3644 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003645 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003646 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3647 req.flags |=
3648 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003649
3650 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3651}
3652
3653static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3654{
3655 u32 rc = 0;
3656
3657 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3658 struct hwrm_vnic_free_input req = {0};
3659
3660 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3661 req.vnic_id =
3662 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3663
3664 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3665 if (rc)
3666 return rc;
3667 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3668 }
3669 return rc;
3670}
3671
3672static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3673{
3674 u16 i;
3675
3676 for (i = 0; i < bp->nr_vnics; i++)
3677 bnxt_hwrm_vnic_free_one(bp, i);
3678}
3679
Michael Chanb81a90d2016-01-02 23:45:01 -05003680static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3681 unsigned int start_rx_ring_idx,
3682 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003683{
Michael Chanb81a90d2016-01-02 23:45:01 -05003684 int rc = 0;
3685 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003686 struct hwrm_vnic_alloc_input req = {0};
3687 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3688
3689 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003690 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3691 grp_idx = bp->rx_ring[i].bnapi->index;
3692 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003693 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003694 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003695 break;
3696 }
3697 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003698 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003699 }
3700
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003701 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3702 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003703 if (vnic_id == 0)
3704 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3705
3706 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3707
3708 mutex_lock(&bp->hwrm_cmd_lock);
3709 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3710 if (!rc)
3711 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3712 mutex_unlock(&bp->hwrm_cmd_lock);
3713 return rc;
3714}
3715
Michael Chan8fdefd62016-12-29 12:13:36 -05003716static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3717{
3718 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3719 struct hwrm_vnic_qcaps_input req = {0};
3720 int rc;
3721
3722 if (bp->hwrm_spec_code < 0x10600)
3723 return 0;
3724
3725 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3726 mutex_lock(&bp->hwrm_cmd_lock);
3727 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3728 if (!rc) {
3729 if (resp->flags &
3730 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3731 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3732 }
3733 mutex_unlock(&bp->hwrm_cmd_lock);
3734 return rc;
3735}
3736
Michael Chanc0c050c2015-10-22 16:01:17 -04003737static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3738{
3739 u16 i;
3740 u32 rc = 0;
3741
3742 mutex_lock(&bp->hwrm_cmd_lock);
3743 for (i = 0; i < bp->rx_nr_rings; i++) {
3744 struct hwrm_ring_grp_alloc_input req = {0};
3745 struct hwrm_ring_grp_alloc_output *resp =
3746 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003747 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003748
3749 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3750
Michael Chanb81a90d2016-01-02 23:45:01 -05003751 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3752 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3753 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3754 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003755
3756 rc = _hwrm_send_message(bp, &req, sizeof(req),
3757 HWRM_CMD_TIMEOUT);
3758 if (rc)
3759 break;
3760
Michael Chanb81a90d2016-01-02 23:45:01 -05003761 bp->grp_info[grp_idx].fw_grp_id =
3762 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003763 }
3764 mutex_unlock(&bp->hwrm_cmd_lock);
3765 return rc;
3766}
3767
3768static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3769{
3770 u16 i;
3771 u32 rc = 0;
3772 struct hwrm_ring_grp_free_input req = {0};
3773
3774 if (!bp->grp_info)
3775 return 0;
3776
3777 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3778
3779 mutex_lock(&bp->hwrm_cmd_lock);
3780 for (i = 0; i < bp->cp_nr_rings; i++) {
3781 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3782 continue;
3783 req.ring_group_id =
3784 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3785
3786 rc = _hwrm_send_message(bp, &req, sizeof(req),
3787 HWRM_CMD_TIMEOUT);
3788 if (rc)
3789 break;
3790 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3791 }
3792 mutex_unlock(&bp->hwrm_cmd_lock);
3793 return rc;
3794}
3795
3796static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3797 struct bnxt_ring_struct *ring,
3798 u32 ring_type, u32 map_index,
3799 u32 stats_ctx_id)
3800{
3801 int rc = 0, err = 0;
3802 struct hwrm_ring_alloc_input req = {0};
3803 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3804 u16 ring_id;
3805
3806 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3807
3808 req.enables = 0;
3809 if (ring->nr_pages > 1) {
3810 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3811 /* Page size is in log2 units */
3812 req.page_size = BNXT_PAGE_SHIFT;
3813 req.page_tbl_depth = 1;
3814 } else {
3815 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3816 }
3817 req.fbo = 0;
3818 /* Association of ring index with doorbell index and MSIX number */
3819 req.logical_id = cpu_to_le16(map_index);
3820
3821 switch (ring_type) {
3822 case HWRM_RING_ALLOC_TX:
3823 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3824 /* Association of transmit ring with completion ring */
3825 req.cmpl_ring_id =
3826 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3827 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3828 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3829 req.queue_id = cpu_to_le16(ring->queue_id);
3830 break;
3831 case HWRM_RING_ALLOC_RX:
3832 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3833 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3834 break;
3835 case HWRM_RING_ALLOC_AGG:
3836 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3837 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3838 break;
3839 case HWRM_RING_ALLOC_CMPL:
3840 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3841 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3842 if (bp->flags & BNXT_FLAG_USING_MSIX)
3843 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3844 break;
3845 default:
3846 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3847 ring_type);
3848 return -1;
3849 }
3850
3851 mutex_lock(&bp->hwrm_cmd_lock);
3852 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3853 err = le16_to_cpu(resp->error_code);
3854 ring_id = le16_to_cpu(resp->ring_id);
3855 mutex_unlock(&bp->hwrm_cmd_lock);
3856
3857 if (rc || err) {
3858 switch (ring_type) {
3859 case RING_FREE_REQ_RING_TYPE_CMPL:
3860 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3861 rc, err);
3862 return -1;
3863
3864 case RING_FREE_REQ_RING_TYPE_RX:
3865 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3866 rc, err);
3867 return -1;
3868
3869 case RING_FREE_REQ_RING_TYPE_TX:
3870 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3871 rc, err);
3872 return -1;
3873
3874 default:
3875 netdev_err(bp->dev, "Invalid ring\n");
3876 return -1;
3877 }
3878 }
3879 ring->fw_ring_id = ring_id;
3880 return rc;
3881}
3882
Michael Chan486b5c22016-12-29 12:13:42 -05003883static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
3884{
3885 int rc;
3886
3887 if (BNXT_PF(bp)) {
3888 struct hwrm_func_cfg_input req = {0};
3889
3890 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
3891 req.fid = cpu_to_le16(0xffff);
3892 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3893 req.async_event_cr = cpu_to_le16(idx);
3894 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3895 } else {
3896 struct hwrm_func_vf_cfg_input req = {0};
3897
3898 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
3899 req.enables =
3900 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
3901 req.async_event_cr = cpu_to_le16(idx);
3902 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3903 }
3904 return rc;
3905}
3906
Michael Chanc0c050c2015-10-22 16:01:17 -04003907static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3908{
3909 int i, rc = 0;
3910
Michael Chanedd0c2c2015-12-27 18:19:19 -05003911 for (i = 0; i < bp->cp_nr_rings; i++) {
3912 struct bnxt_napi *bnapi = bp->bnapi[i];
3913 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3914 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003915
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003916 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003917 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3918 INVALID_STATS_CTX_ID);
3919 if (rc)
3920 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003921 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3922 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chan486b5c22016-12-29 12:13:42 -05003923
3924 if (!i) {
3925 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
3926 if (rc)
3927 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
3928 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003929 }
3930
Michael Chanedd0c2c2015-12-27 18:19:19 -05003931 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003932 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003933 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003934 u32 map_idx = txr->bnapi->index;
3935 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003936
Michael Chanb81a90d2016-01-02 23:45:01 -05003937 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3938 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003939 if (rc)
3940 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003941 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003942 }
3943
Michael Chanedd0c2c2015-12-27 18:19:19 -05003944 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003945 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003946 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003947 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003948
Michael Chanb81a90d2016-01-02 23:45:01 -05003949 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3950 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003951 if (rc)
3952 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003953 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003954 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003955 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003956 }
3957
3958 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3959 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003960 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003961 struct bnxt_ring_struct *ring =
3962 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003963 u32 grp_idx = rxr->bnapi->index;
3964 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003965
3966 rc = hwrm_ring_alloc_send_msg(bp, ring,
3967 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003968 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003969 INVALID_STATS_CTX_ID);
3970 if (rc)
3971 goto err_out;
3972
Michael Chanb81a90d2016-01-02 23:45:01 -05003973 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003974 writel(DB_KEY_RX | rxr->rx_agg_prod,
3975 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003976 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003977 }
3978 }
3979err_out:
3980 return rc;
3981}
3982
3983static int hwrm_ring_free_send_msg(struct bnxt *bp,
3984 struct bnxt_ring_struct *ring,
3985 u32 ring_type, int cmpl_ring_id)
3986{
3987 int rc;
3988 struct hwrm_ring_free_input req = {0};
3989 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3990 u16 error_code;
3991
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003992 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003993 req.ring_type = ring_type;
3994 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3995
3996 mutex_lock(&bp->hwrm_cmd_lock);
3997 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3998 error_code = le16_to_cpu(resp->error_code);
3999 mutex_unlock(&bp->hwrm_cmd_lock);
4000
4001 if (rc || error_code) {
4002 switch (ring_type) {
4003 case RING_FREE_REQ_RING_TYPE_CMPL:
4004 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4005 rc);
4006 return rc;
4007 case RING_FREE_REQ_RING_TYPE_RX:
4008 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4009 rc);
4010 return rc;
4011 case RING_FREE_REQ_RING_TYPE_TX:
4012 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4013 rc);
4014 return rc;
4015 default:
4016 netdev_err(bp->dev, "Invalid ring\n");
4017 return -1;
4018 }
4019 }
4020 return 0;
4021}
4022
Michael Chanedd0c2c2015-12-27 18:19:19 -05004023static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04004024{
Michael Chanedd0c2c2015-12-27 18:19:19 -05004025 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004026
4027 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05004028 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04004029
Michael Chanedd0c2c2015-12-27 18:19:19 -05004030 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004031 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004032 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004033 u32 grp_idx = txr->bnapi->index;
4034 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004035
Michael Chanedd0c2c2015-12-27 18:19:19 -05004036 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4037 hwrm_ring_free_send_msg(bp, ring,
4038 RING_FREE_REQ_RING_TYPE_TX,
4039 close_path ? cmpl_ring_id :
4040 INVALID_HW_RING_ID);
4041 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004042 }
4043 }
4044
Michael Chanedd0c2c2015-12-27 18:19:19 -05004045 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004046 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004047 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004048 u32 grp_idx = rxr->bnapi->index;
4049 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004050
Michael Chanedd0c2c2015-12-27 18:19:19 -05004051 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4052 hwrm_ring_free_send_msg(bp, ring,
4053 RING_FREE_REQ_RING_TYPE_RX,
4054 close_path ? cmpl_ring_id :
4055 INVALID_HW_RING_ID);
4056 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004057 bp->grp_info[grp_idx].rx_fw_ring_id =
4058 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004059 }
4060 }
4061
Michael Chanedd0c2c2015-12-27 18:19:19 -05004062 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004063 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05004064 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05004065 u32 grp_idx = rxr->bnapi->index;
4066 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04004067
Michael Chanedd0c2c2015-12-27 18:19:19 -05004068 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4069 hwrm_ring_free_send_msg(bp, ring,
4070 RING_FREE_REQ_RING_TYPE_RX,
4071 close_path ? cmpl_ring_id :
4072 INVALID_HW_RING_ID);
4073 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05004074 bp->grp_info[grp_idx].agg_fw_ring_id =
4075 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004076 }
4077 }
4078
Michael Chan9d8bc092016-12-29 12:13:33 -05004079 /* The completion rings are about to be freed. After that the
4080 * IRQ doorbell will not work anymore. So we need to disable
4081 * IRQ here.
4082 */
4083 bnxt_disable_int_sync(bp);
4084
Michael Chanedd0c2c2015-12-27 18:19:19 -05004085 for (i = 0; i < bp->cp_nr_rings; i++) {
4086 struct bnxt_napi *bnapi = bp->bnapi[i];
4087 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4088 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004089
Michael Chanedd0c2c2015-12-27 18:19:19 -05004090 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4091 hwrm_ring_free_send_msg(bp, ring,
4092 RING_FREE_REQ_RING_TYPE_CMPL,
4093 INVALID_HW_RING_ID);
4094 ring->fw_ring_id = INVALID_HW_RING_ID;
4095 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004096 }
4097 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004098}
4099
Michael Chan391be5c2016-12-29 12:13:41 -05004100/* Caller must hold bp->hwrm_cmd_lock */
4101int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4102{
4103 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4104 struct hwrm_func_qcfg_input req = {0};
4105 int rc;
4106
4107 if (bp->hwrm_spec_code < 0x10601)
4108 return 0;
4109
4110 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4111 req.fid = cpu_to_le16(fid);
4112 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4113 if (!rc)
4114 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4115
4116 return rc;
4117}
4118
4119int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
4120{
4121 struct hwrm_func_cfg_input req = {0};
4122 int rc;
4123
4124 if (bp->hwrm_spec_code < 0x10601)
4125 return 0;
4126
4127 if (BNXT_VF(bp))
4128 return 0;
4129
4130 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4131 req.fid = cpu_to_le16(0xffff);
4132 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4133 req.num_tx_rings = cpu_to_le16(*tx_rings);
4134 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4135 if (rc)
4136 return rc;
4137
4138 mutex_lock(&bp->hwrm_cmd_lock);
4139 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4140 mutex_unlock(&bp->hwrm_cmd_lock);
4141 return rc;
4142}
4143
Michael Chanbb053f52016-02-26 04:00:02 -05004144static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4145 u32 buf_tmrs, u16 flags,
4146 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4147{
4148 req->flags = cpu_to_le16(flags);
4149 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4150 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4151 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4152 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4153 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4154 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4155 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4156 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4157}
4158
Michael Chanc0c050c2015-10-22 16:01:17 -04004159int bnxt_hwrm_set_coal(struct bnxt *bp)
4160{
4161 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004162 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4163 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004164 u16 max_buf, max_buf_irq;
4165 u16 buf_tmr, buf_tmr_irq;
4166 u32 flags;
4167
Michael Chandfc9c942016-02-26 04:00:03 -05004168 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4169 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4170 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4171 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004172
Michael Chandfb5b892016-02-26 04:00:01 -05004173 /* Each rx completion (2 records) should be DMAed immediately.
4174 * DMA 1/4 of the completion buffers at a time.
4175 */
4176 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004177 /* max_buf must not be zero */
4178 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004179 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4180 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4181 /* buf timer set to 1/4 of interrupt timer */
4182 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4183 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4184 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004185
4186 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4187
4188 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4189 * if coal_ticks is less than 25 us.
4190 */
Michael Chandfb5b892016-02-26 04:00:01 -05004191 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004192 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4193
Michael Chanbb053f52016-02-26 04:00:02 -05004194 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004195 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4196
4197 /* max_buf must not be zero */
4198 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4199 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4200 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4201 /* buf timer set to 1/4 of interrupt timer */
4202 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4203 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4204 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4205
4206 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4207 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4208 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004209
4210 mutex_lock(&bp->hwrm_cmd_lock);
4211 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004212 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004213
Michael Chandfc9c942016-02-26 04:00:03 -05004214 req = &req_rx;
4215 if (!bnapi->rx_ring)
4216 req = &req_tx;
4217 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4218
4219 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004220 HWRM_CMD_TIMEOUT);
4221 if (rc)
4222 break;
4223 }
4224 mutex_unlock(&bp->hwrm_cmd_lock);
4225 return rc;
4226}
4227
4228static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4229{
4230 int rc = 0, i;
4231 struct hwrm_stat_ctx_free_input req = {0};
4232
4233 if (!bp->bnapi)
4234 return 0;
4235
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004236 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4237 return 0;
4238
Michael Chanc0c050c2015-10-22 16:01:17 -04004239 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4240
4241 mutex_lock(&bp->hwrm_cmd_lock);
4242 for (i = 0; i < bp->cp_nr_rings; i++) {
4243 struct bnxt_napi *bnapi = bp->bnapi[i];
4244 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4245
4246 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4247 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4248
4249 rc = _hwrm_send_message(bp, &req, sizeof(req),
4250 HWRM_CMD_TIMEOUT);
4251 if (rc)
4252 break;
4253
4254 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4255 }
4256 }
4257 mutex_unlock(&bp->hwrm_cmd_lock);
4258 return rc;
4259}
4260
4261static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4262{
4263 int rc = 0, i;
4264 struct hwrm_stat_ctx_alloc_input req = {0};
4265 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4266
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004267 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4268 return 0;
4269
Michael Chanc0c050c2015-10-22 16:01:17 -04004270 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4271
Michael Chan51f30782016-07-01 18:46:29 -04004272 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004273
4274 mutex_lock(&bp->hwrm_cmd_lock);
4275 for (i = 0; i < bp->cp_nr_rings; i++) {
4276 struct bnxt_napi *bnapi = bp->bnapi[i];
4277 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4278
4279 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4280
4281 rc = _hwrm_send_message(bp, &req, sizeof(req),
4282 HWRM_CMD_TIMEOUT);
4283 if (rc)
4284 break;
4285
4286 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4287
4288 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4289 }
4290 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004291 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004292}
4293
Michael Chancf6645f2016-06-13 02:25:28 -04004294static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4295{
4296 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004297 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004298 int rc;
4299
4300 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4301 req.fid = cpu_to_le16(0xffff);
4302 mutex_lock(&bp->hwrm_cmd_lock);
4303 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4304 if (rc)
4305 goto func_qcfg_exit;
4306
4307#ifdef CONFIG_BNXT_SRIOV
4308 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004309 struct bnxt_vf_info *vf = &bp->vf;
4310
4311 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4312 }
4313#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004314 switch (resp->port_partition_type) {
4315 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4316 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4317 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4318 bp->port_partition_type = resp->port_partition_type;
4319 break;
4320 }
Michael Chancf6645f2016-06-13 02:25:28 -04004321
4322func_qcfg_exit:
4323 mutex_unlock(&bp->hwrm_cmd_lock);
4324 return rc;
4325}
4326
Michael Chan7b08f662016-12-07 00:26:18 -05004327static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004328{
4329 int rc = 0;
4330 struct hwrm_func_qcaps_input req = {0};
4331 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4332
4333 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4334 req.fid = cpu_to_le16(0xffff);
4335
4336 mutex_lock(&bp->hwrm_cmd_lock);
4337 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4338 if (rc)
4339 goto hwrm_func_qcaps_exit;
4340
Michael Chane4060d32016-12-07 00:26:19 -05004341 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4342 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4343 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4344 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4345
Michael Chan7cc5a202016-09-19 03:58:05 -04004346 bp->tx_push_thresh = 0;
4347 if (resp->flags &
4348 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4349 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4350
Michael Chanc0c050c2015-10-22 16:01:17 -04004351 if (BNXT_PF(bp)) {
4352 struct bnxt_pf_info *pf = &bp->pf;
4353
4354 pf->fw_fid = le16_to_cpu(resp->fid);
4355 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004356 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004357 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004358 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004359 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4360 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4361 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004362 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004363 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4364 if (!pf->max_hw_ring_grps)
4365 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004366 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4367 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4368 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4369 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4370 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4371 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4372 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4373 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4374 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4375 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4376 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4377 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004378#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004379 struct bnxt_vf_info *vf = &bp->vf;
4380
4381 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004382
4383 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4384 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4385 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4386 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004387 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4388 if (!vf->max_hw_ring_grps)
4389 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004390 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4391 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4392 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004393
4394 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004395 mutex_unlock(&bp->hwrm_cmd_lock);
4396
4397 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004398 /* overwrite netdev dev_adr with admin VF MAC */
4399 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004400 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004401 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004402 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4403 }
4404 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004405#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004406 }
4407
Michael Chanc0c050c2015-10-22 16:01:17 -04004408hwrm_func_qcaps_exit:
4409 mutex_unlock(&bp->hwrm_cmd_lock);
4410 return rc;
4411}
4412
4413static int bnxt_hwrm_func_reset(struct bnxt *bp)
4414{
4415 struct hwrm_func_reset_input req = {0};
4416
4417 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4418 req.enables = 0;
4419
4420 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4421}
4422
4423static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4424{
4425 int rc = 0;
4426 struct hwrm_queue_qportcfg_input req = {0};
4427 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4428 u8 i, *qptr;
4429
4430 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4431
4432 mutex_lock(&bp->hwrm_cmd_lock);
4433 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4434 if (rc)
4435 goto qportcfg_exit;
4436
4437 if (!resp->max_configurable_queues) {
4438 rc = -EINVAL;
4439 goto qportcfg_exit;
4440 }
4441 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004442 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004443 if (bp->max_tc > BNXT_MAX_QUEUE)
4444 bp->max_tc = BNXT_MAX_QUEUE;
4445
Michael Chan441cabb2016-09-19 03:58:02 -04004446 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4447 bp->max_tc = 1;
4448
Michael Chan87c374d2016-12-02 21:17:16 -05004449 if (bp->max_lltc > bp->max_tc)
4450 bp->max_lltc = bp->max_tc;
4451
Michael Chanc0c050c2015-10-22 16:01:17 -04004452 qptr = &resp->queue_id0;
4453 for (i = 0; i < bp->max_tc; i++) {
4454 bp->q_info[i].queue_id = *qptr++;
4455 bp->q_info[i].queue_profile = *qptr++;
4456 }
4457
4458qportcfg_exit:
4459 mutex_unlock(&bp->hwrm_cmd_lock);
4460 return rc;
4461}
4462
4463static int bnxt_hwrm_ver_get(struct bnxt *bp)
4464{
4465 int rc;
4466 struct hwrm_ver_get_input req = {0};
4467 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4468
Michael Chane6ef2692016-03-28 19:46:05 -04004469 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004470 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4471 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4472 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4473 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4474 mutex_lock(&bp->hwrm_cmd_lock);
4475 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4476 if (rc)
4477 goto hwrm_ver_get_exit;
4478
4479 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4480
Michael Chan11f15ed2016-04-05 14:08:55 -04004481 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4482 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004483 if (resp->hwrm_intf_maj < 1) {
4484 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004485 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004486 resp->hwrm_intf_upd);
4487 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004488 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004489 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004490 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4491 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4492
Michael Chanff4fe812016-02-26 04:00:04 -05004493 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4494 if (!bp->hwrm_cmd_timeout)
4495 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4496
Michael Chane6ef2692016-03-28 19:46:05 -04004497 if (resp->hwrm_intf_maj >= 1)
4498 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4499
Michael Chan659c8052016-06-13 02:25:33 -04004500 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004501 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4502 !resp->chip_metal)
4503 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004504
Michael Chanc0c050c2015-10-22 16:01:17 -04004505hwrm_ver_get_exit:
4506 mutex_unlock(&bp->hwrm_cmd_lock);
4507 return rc;
4508}
4509
Rob Swindell5ac67d82016-09-19 03:58:03 -04004510int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4511{
Rob Swindell878786d2016-09-20 03:36:33 -04004512#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004513 struct hwrm_fw_set_time_input req = {0};
4514 struct rtc_time tm;
4515 struct timeval tv;
4516
4517 if (bp->hwrm_spec_code < 0x10400)
4518 return -EOPNOTSUPP;
4519
4520 do_gettimeofday(&tv);
4521 rtc_time_to_tm(tv.tv_sec, &tm);
4522 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4523 req.year = cpu_to_le16(1900 + tm.tm_year);
4524 req.month = 1 + tm.tm_mon;
4525 req.day = tm.tm_mday;
4526 req.hour = tm.tm_hour;
4527 req.minute = tm.tm_min;
4528 req.second = tm.tm_sec;
4529 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004530#else
4531 return -EOPNOTSUPP;
4532#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004533}
4534
Michael Chan3bdf56c2016-03-07 15:38:45 -05004535static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4536{
4537 int rc;
4538 struct bnxt_pf_info *pf = &bp->pf;
4539 struct hwrm_port_qstats_input req = {0};
4540
4541 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4542 return 0;
4543
4544 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4545 req.port_id = cpu_to_le16(pf->port_id);
4546 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4547 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4548 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4549 return rc;
4550}
4551
Michael Chanc0c050c2015-10-22 16:01:17 -04004552static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4553{
4554 if (bp->vxlan_port_cnt) {
4555 bnxt_hwrm_tunnel_dst_port_free(
4556 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4557 }
4558 bp->vxlan_port_cnt = 0;
4559 if (bp->nge_port_cnt) {
4560 bnxt_hwrm_tunnel_dst_port_free(
4561 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4562 }
4563 bp->nge_port_cnt = 0;
4564}
4565
4566static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4567{
4568 int rc, i;
4569 u32 tpa_flags = 0;
4570
4571 if (set_tpa)
4572 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4573 for (i = 0; i < bp->nr_vnics; i++) {
4574 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4575 if (rc) {
4576 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4577 rc, i);
4578 return rc;
4579 }
4580 }
4581 return 0;
4582}
4583
4584static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4585{
4586 int i;
4587
4588 for (i = 0; i < bp->nr_vnics; i++)
4589 bnxt_hwrm_vnic_set_rss(bp, i, false);
4590}
4591
4592static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4593 bool irq_re_init)
4594{
4595 if (bp->vnic_info) {
4596 bnxt_hwrm_clear_vnic_filter(bp);
4597 /* clear all RSS setting before free vnic ctx */
4598 bnxt_hwrm_clear_vnic_rss(bp);
4599 bnxt_hwrm_vnic_ctx_free(bp);
4600 /* before free the vnic, undo the vnic tpa settings */
4601 if (bp->flags & BNXT_FLAG_TPA)
4602 bnxt_set_tpa(bp, false);
4603 bnxt_hwrm_vnic_free(bp);
4604 }
4605 bnxt_hwrm_ring_free(bp, close_path);
4606 bnxt_hwrm_ring_grp_free(bp);
4607 if (irq_re_init) {
4608 bnxt_hwrm_stat_ctx_free(bp);
4609 bnxt_hwrm_free_tunnel_ports(bp);
4610 }
4611}
4612
4613static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4614{
Michael Chanae10ae72016-12-29 12:13:38 -05004615 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
Michael Chanc0c050c2015-10-22 16:01:17 -04004616 int rc;
4617
Michael Chanae10ae72016-12-29 12:13:38 -05004618 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4619 goto skip_rss_ctx;
4620
Michael Chanc0c050c2015-10-22 16:01:17 -04004621 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004622 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004623 if (rc) {
4624 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4625 vnic_id, rc);
4626 goto vnic_setup_err;
4627 }
4628 bp->rsscos_nr_ctxs++;
4629
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004630 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4631 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4632 if (rc) {
4633 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4634 vnic_id, rc);
4635 goto vnic_setup_err;
4636 }
4637 bp->rsscos_nr_ctxs++;
4638 }
4639
Michael Chanae10ae72016-12-29 12:13:38 -05004640skip_rss_ctx:
Michael Chanc0c050c2015-10-22 16:01:17 -04004641 /* configure default vnic, ring grp */
4642 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4643 if (rc) {
4644 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4645 vnic_id, rc);
4646 goto vnic_setup_err;
4647 }
4648
4649 /* Enable RSS hashing on vnic */
4650 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4651 if (rc) {
4652 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4653 vnic_id, rc);
4654 goto vnic_setup_err;
4655 }
4656
4657 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4658 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4659 if (rc) {
4660 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4661 vnic_id, rc);
4662 }
4663 }
4664
4665vnic_setup_err:
4666 return rc;
4667}
4668
4669static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4670{
4671#ifdef CONFIG_RFS_ACCEL
4672 int i, rc = 0;
4673
4674 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanae10ae72016-12-29 12:13:38 -05004675 struct bnxt_vnic_info *vnic;
Michael Chanc0c050c2015-10-22 16:01:17 -04004676 u16 vnic_id = i + 1;
4677 u16 ring_id = i;
4678
4679 if (vnic_id >= bp->nr_vnics)
4680 break;
4681
Michael Chanae10ae72016-12-29 12:13:38 -05004682 vnic = &bp->vnic_info[vnic_id];
4683 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4684 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4685 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004686 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004687 if (rc) {
4688 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4689 vnic_id, rc);
4690 break;
4691 }
4692 rc = bnxt_setup_vnic(bp, vnic_id);
4693 if (rc)
4694 break;
4695 }
4696 return rc;
4697#else
4698 return 0;
4699#endif
4700}
4701
Michael Chan17c71ac2016-07-01 18:46:27 -04004702/* Allow PF and VF with default VLAN to be in promiscuous mode */
4703static bool bnxt_promisc_ok(struct bnxt *bp)
4704{
4705#ifdef CONFIG_BNXT_SRIOV
4706 if (BNXT_VF(bp) && !bp->vf.vlan)
4707 return false;
4708#endif
4709 return true;
4710}
4711
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004712static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4713{
4714 unsigned int rc = 0;
4715
4716 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4717 if (rc) {
4718 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4719 rc);
4720 return rc;
4721 }
4722
4723 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4724 if (rc) {
4725 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4726 rc);
4727 return rc;
4728 }
4729 return rc;
4730}
4731
Michael Chanb664f002015-12-02 01:54:08 -05004732static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004733static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004734
Michael Chanc0c050c2015-10-22 16:01:17 -04004735static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4736{
Michael Chan7d2837d2016-05-04 16:56:44 -04004737 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004738 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004739 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004740
4741 if (irq_re_init) {
4742 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4743 if (rc) {
4744 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4745 rc);
4746 goto err_out;
4747 }
4748 }
4749
4750 rc = bnxt_hwrm_ring_alloc(bp);
4751 if (rc) {
4752 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4753 goto err_out;
4754 }
4755
4756 rc = bnxt_hwrm_ring_grp_alloc(bp);
4757 if (rc) {
4758 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4759 goto err_out;
4760 }
4761
Prashant Sreedharan76595192016-07-18 07:15:22 -04004762 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4763 rx_nr_rings--;
4764
Michael Chanc0c050c2015-10-22 16:01:17 -04004765 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004766 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004767 if (rc) {
4768 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4769 goto err_out;
4770 }
4771
4772 rc = bnxt_setup_vnic(bp, 0);
4773 if (rc)
4774 goto err_out;
4775
4776 if (bp->flags & BNXT_FLAG_RFS) {
4777 rc = bnxt_alloc_rfs_vnics(bp);
4778 if (rc)
4779 goto err_out;
4780 }
4781
4782 if (bp->flags & BNXT_FLAG_TPA) {
4783 rc = bnxt_set_tpa(bp, true);
4784 if (rc)
4785 goto err_out;
4786 }
4787
4788 if (BNXT_VF(bp))
4789 bnxt_update_vf_mac(bp);
4790
4791 /* Filter for default vnic 0 */
4792 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4793 if (rc) {
4794 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4795 goto err_out;
4796 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004797 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004798
Michael Chan7d2837d2016-05-04 16:56:44 -04004799 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004800
Michael Chan17c71ac2016-07-01 18:46:27 -04004801 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004802 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4803
4804 if (bp->dev->flags & IFF_ALLMULTI) {
4805 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4806 vnic->mc_list_count = 0;
4807 } else {
4808 u32 mask = 0;
4809
4810 bnxt_mc_list_updated(bp, &mask);
4811 vnic->rx_mask |= mask;
4812 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004813
Michael Chanb664f002015-12-02 01:54:08 -05004814 rc = bnxt_cfg_rx_mode(bp);
4815 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004816 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004817
4818 rc = bnxt_hwrm_set_coal(bp);
4819 if (rc)
4820 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004821 rc);
4822
4823 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4824 rc = bnxt_setup_nitroa0_vnic(bp);
4825 if (rc)
4826 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4827 rc);
4828 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004829
Michael Chancf6645f2016-06-13 02:25:28 -04004830 if (BNXT_VF(bp)) {
4831 bnxt_hwrm_func_qcfg(bp);
4832 netdev_update_features(bp->dev);
4833 }
4834
Michael Chanc0c050c2015-10-22 16:01:17 -04004835 return 0;
4836
4837err_out:
4838 bnxt_hwrm_resource_free(bp, 0, true);
4839
4840 return rc;
4841}
4842
4843static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4844{
4845 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4846 return 0;
4847}
4848
4849static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4850{
4851 bnxt_init_rx_rings(bp);
4852 bnxt_init_tx_rings(bp);
4853 bnxt_init_ring_grps(bp, irq_re_init);
4854 bnxt_init_vnics(bp);
4855
4856 return bnxt_init_chip(bp, irq_re_init);
4857}
4858
Michael Chanc0c050c2015-10-22 16:01:17 -04004859static int bnxt_set_real_num_queues(struct bnxt *bp)
4860{
4861 int rc;
4862 struct net_device *dev = bp->dev;
4863
4864 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4865 if (rc)
4866 return rc;
4867
4868 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4869 if (rc)
4870 return rc;
4871
4872#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004873 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004874 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004875#endif
4876
4877 return rc;
4878}
4879
Michael Chan6e6c5a52016-01-02 23:45:02 -05004880static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4881 bool shared)
4882{
4883 int _rx = *rx, _tx = *tx;
4884
4885 if (shared) {
4886 *rx = min_t(int, _rx, max);
4887 *tx = min_t(int, _tx, max);
4888 } else {
4889 if (max < 2)
4890 return -ENOMEM;
4891
4892 while (_rx + _tx > max) {
4893 if (_rx > _tx && _rx > 1)
4894 _rx--;
4895 else if (_tx > 1)
4896 _tx--;
4897 }
4898 *rx = _rx;
4899 *tx = _tx;
4900 }
4901 return 0;
4902}
4903
Michael Chan78095922016-12-07 00:26:16 -05004904static void bnxt_setup_msix(struct bnxt *bp)
4905{
4906 const int len = sizeof(bp->irq_tbl[0].name);
4907 struct net_device *dev = bp->dev;
4908 int tcs, i;
4909
4910 tcs = netdev_get_num_tc(dev);
4911 if (tcs > 1) {
4912 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4913 if (bp->tx_nr_rings_per_tc == 0) {
4914 netdev_reset_tc(dev);
4915 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4916 } else {
4917 int i, off, count;
4918
4919 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4920 for (i = 0; i < tcs; i++) {
4921 count = bp->tx_nr_rings_per_tc;
4922 off = i * count;
4923 netdev_set_tc_queue(dev, i, count, off);
4924 }
4925 }
4926 }
4927
4928 for (i = 0; i < bp->cp_nr_rings; i++) {
4929 char *attr;
4930
4931 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4932 attr = "TxRx";
4933 else if (i < bp->rx_nr_rings)
4934 attr = "rx";
4935 else
4936 attr = "tx";
4937
4938 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
4939 i);
4940 bp->irq_tbl[i].handler = bnxt_msix;
4941 }
4942}
4943
4944static void bnxt_setup_inta(struct bnxt *bp)
4945{
4946 const int len = sizeof(bp->irq_tbl[0].name);
4947
4948 if (netdev_get_num_tc(bp->dev))
4949 netdev_reset_tc(bp->dev);
4950
4951 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
4952 0);
4953 bp->irq_tbl[0].handler = bnxt_inta;
4954}
4955
4956static int bnxt_setup_int_mode(struct bnxt *bp)
4957{
4958 int rc;
4959
4960 if (bp->flags & BNXT_FLAG_USING_MSIX)
4961 bnxt_setup_msix(bp);
4962 else
4963 bnxt_setup_inta(bp);
4964
4965 rc = bnxt_set_real_num_queues(bp);
4966 return rc;
4967}
4968
Michael Chanb7429952017-01-13 01:32:00 -05004969#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05004970static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
4971{
4972#if defined(CONFIG_BNXT_SRIOV)
4973 if (BNXT_VF(bp))
4974 return bp->vf.max_rsscos_ctxs;
4975#endif
4976 return bp->pf.max_rsscos_ctxs;
4977}
4978
4979static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
4980{
4981#if defined(CONFIG_BNXT_SRIOV)
4982 if (BNXT_VF(bp))
4983 return bp->vf.max_vnics;
4984#endif
4985 return bp->pf.max_vnics;
4986}
Michael Chanb7429952017-01-13 01:32:00 -05004987#endif
Michael Chan8079e8f2016-12-29 12:13:37 -05004988
Michael Chane4060d32016-12-07 00:26:19 -05004989unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
4990{
4991#if defined(CONFIG_BNXT_SRIOV)
4992 if (BNXT_VF(bp))
4993 return bp->vf.max_stat_ctxs;
4994#endif
4995 return bp->pf.max_stat_ctxs;
4996}
4997
Michael Chana588e452016-12-07 00:26:21 -05004998void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
4999{
5000#if defined(CONFIG_BNXT_SRIOV)
5001 if (BNXT_VF(bp))
5002 bp->vf.max_stat_ctxs = max;
5003 else
5004#endif
5005 bp->pf.max_stat_ctxs = max;
5006}
5007
Michael Chane4060d32016-12-07 00:26:19 -05005008unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5009{
5010#if defined(CONFIG_BNXT_SRIOV)
5011 if (BNXT_VF(bp))
5012 return bp->vf.max_cp_rings;
5013#endif
5014 return bp->pf.max_cp_rings;
5015}
5016
Michael Chana588e452016-12-07 00:26:21 -05005017void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5018{
5019#if defined(CONFIG_BNXT_SRIOV)
5020 if (BNXT_VF(bp))
5021 bp->vf.max_cp_rings = max;
5022 else
5023#endif
5024 bp->pf.max_cp_rings = max;
5025}
5026
Michael Chan78095922016-12-07 00:26:16 -05005027static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5028{
5029#if defined(CONFIG_BNXT_SRIOV)
5030 if (BNXT_VF(bp))
5031 return bp->vf.max_irqs;
5032#endif
5033 return bp->pf.max_irqs;
5034}
5035
Michael Chan33c26572016-12-07 00:26:15 -05005036void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5037{
5038#if defined(CONFIG_BNXT_SRIOV)
5039 if (BNXT_VF(bp))
5040 bp->vf.max_irqs = max_irqs;
5041 else
5042#endif
5043 bp->pf.max_irqs = max_irqs;
5044}
5045
Michael Chan78095922016-12-07 00:26:16 -05005046static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005047{
Michael Chan01657bc2016-01-02 23:45:03 -05005048 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05005049 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04005050
Michael Chan78095922016-12-07 00:26:16 -05005051 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005052 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5053 if (!msix_ent)
5054 return -ENOMEM;
5055
5056 for (i = 0; i < total_vecs; i++) {
5057 msix_ent[i].entry = i;
5058 msix_ent[i].vector = 0;
5059 }
5060
Michael Chan01657bc2016-01-02 23:45:03 -05005061 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5062 min = 2;
5063
5064 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04005065 if (total_vecs < 0) {
5066 rc = -ENODEV;
5067 goto msix_setup_exit;
5068 }
5069
5070 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5071 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05005072 for (i = 0; i < total_vecs; i++)
5073 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04005074
Michael Chan78095922016-12-07 00:26:16 -05005075 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005076 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05005077 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05005078 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005079 if (rc)
5080 goto msix_setup_exit;
5081
Michael Chanc0c050c2015-10-22 16:01:17 -04005082 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05005083 bp->cp_nr_rings = (min == 1) ?
5084 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5085 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005086
Michael Chanc0c050c2015-10-22 16:01:17 -04005087 } else {
5088 rc = -ENOMEM;
5089 goto msix_setup_exit;
5090 }
5091 bp->flags |= BNXT_FLAG_USING_MSIX;
5092 kfree(msix_ent);
5093 return 0;
5094
5095msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05005096 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5097 kfree(bp->irq_tbl);
5098 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04005099 pci_disable_msix(bp->pdev);
5100 kfree(msix_ent);
5101 return rc;
5102}
5103
Michael Chan78095922016-12-07 00:26:16 -05005104static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005105{
Michael Chanc0c050c2015-10-22 16:01:17 -04005106 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05005107 if (!bp->irq_tbl)
5108 return -ENOMEM;
5109
5110 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04005111 bp->rx_nr_rings = 1;
5112 bp->tx_nr_rings = 1;
5113 bp->cp_nr_rings = 1;
5114 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05005115 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04005116 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05005117 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005118}
5119
Michael Chan78095922016-12-07 00:26:16 -05005120static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005121{
5122 int rc = 0;
5123
5124 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05005125 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005126
Michael Chan1fa72e22016-04-25 02:30:49 -04005127 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005128 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05005129 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005130 }
5131 return rc;
5132}
5133
Michael Chan78095922016-12-07 00:26:16 -05005134static void bnxt_clear_int_mode(struct bnxt *bp)
5135{
5136 if (bp->flags & BNXT_FLAG_USING_MSIX)
5137 pci_disable_msix(bp->pdev);
5138
5139 kfree(bp->irq_tbl);
5140 bp->irq_tbl = NULL;
5141 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5142}
5143
Michael Chanc0c050c2015-10-22 16:01:17 -04005144static void bnxt_free_irq(struct bnxt *bp)
5145{
5146 struct bnxt_irq *irq;
5147 int i;
5148
5149#ifdef CONFIG_RFS_ACCEL
5150 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5151 bp->dev->rx_cpu_rmap = NULL;
5152#endif
5153 if (!bp->irq_tbl)
5154 return;
5155
5156 for (i = 0; i < bp->cp_nr_rings; i++) {
5157 irq = &bp->irq_tbl[i];
5158 if (irq->requested)
5159 free_irq(irq->vector, bp->bnapi[i]);
5160 irq->requested = 0;
5161 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005162}
5163
5164static int bnxt_request_irq(struct bnxt *bp)
5165{
Michael Chanb81a90d2016-01-02 23:45:01 -05005166 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005167 unsigned long flags = 0;
5168#ifdef CONFIG_RFS_ACCEL
5169 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5170#endif
5171
5172 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5173 flags = IRQF_SHARED;
5174
Michael Chanb81a90d2016-01-02 23:45:01 -05005175 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005176 struct bnxt_irq *irq = &bp->irq_tbl[i];
5177#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005178 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005179 rc = irq_cpu_rmap_add(rmap, irq->vector);
5180 if (rc)
5181 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005182 j);
5183 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005184 }
5185#endif
5186 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5187 bp->bnapi[i]);
5188 if (rc)
5189 break;
5190
5191 irq->requested = 1;
5192 }
5193 return rc;
5194}
5195
5196static void bnxt_del_napi(struct bnxt *bp)
5197{
5198 int i;
5199
5200 if (!bp->bnapi)
5201 return;
5202
5203 for (i = 0; i < bp->cp_nr_rings; i++) {
5204 struct bnxt_napi *bnapi = bp->bnapi[i];
5205
5206 napi_hash_del(&bnapi->napi);
5207 netif_napi_del(&bnapi->napi);
5208 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005209 /* We called napi_hash_del() before netif_napi_del(), we need
5210 * to respect an RCU grace period before freeing napi structures.
5211 */
5212 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005213}
5214
5215static void bnxt_init_napi(struct bnxt *bp)
5216{
5217 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005218 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005219 struct bnxt_napi *bnapi;
5220
5221 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005222 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5223 cp_nr_rings--;
5224 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005225 bnapi = bp->bnapi[i];
5226 netif_napi_add(bp->dev, &bnapi->napi,
5227 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005228 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005229 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5230 bnapi = bp->bnapi[cp_nr_rings];
5231 netif_napi_add(bp->dev, &bnapi->napi,
5232 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005233 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005234 } else {
5235 bnapi = bp->bnapi[0];
5236 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005237 }
5238}
5239
5240static void bnxt_disable_napi(struct bnxt *bp)
5241{
5242 int i;
5243
5244 if (!bp->bnapi)
5245 return;
5246
Michael Chanb356a2e2016-12-29 12:13:31 -05005247 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005248 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005249}
5250
5251static void bnxt_enable_napi(struct bnxt *bp)
5252{
5253 int i;
5254
5255 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005256 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005257 napi_enable(&bp->bnapi[i]->napi);
5258 }
5259}
5260
Michael Chan7df4ae92016-12-02 21:17:17 -05005261void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005262{
5263 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005264 struct bnxt_tx_ring_info *txr;
5265 struct netdev_queue *txq;
5266
Michael Chanb6ab4b02016-01-02 23:44:59 -05005267 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005268 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005269 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005270 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005271 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005272 }
5273 }
5274 /* Stop all TX queues */
5275 netif_tx_disable(bp->dev);
5276 netif_carrier_off(bp->dev);
5277}
5278
Michael Chan7df4ae92016-12-02 21:17:17 -05005279void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005280{
5281 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005282 struct bnxt_tx_ring_info *txr;
5283 struct netdev_queue *txq;
5284
5285 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005286 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005287 txq = netdev_get_tx_queue(bp->dev, i);
5288 txr->dev_state = 0;
5289 }
5290 netif_tx_wake_all_queues(bp->dev);
5291 if (bp->link_info.link_up)
5292 netif_carrier_on(bp->dev);
5293}
5294
5295static void bnxt_report_link(struct bnxt *bp)
5296{
5297 if (bp->link_info.link_up) {
5298 const char *duplex;
5299 const char *flow_ctrl;
5300 u16 speed;
5301
5302 netif_carrier_on(bp->dev);
5303 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5304 duplex = "full";
5305 else
5306 duplex = "half";
5307 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5308 flow_ctrl = "ON - receive & transmit";
5309 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5310 flow_ctrl = "ON - transmit";
5311 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5312 flow_ctrl = "ON - receive";
5313 else
5314 flow_ctrl = "none";
5315 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5316 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5317 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005318 if (bp->flags & BNXT_FLAG_EEE_CAP)
5319 netdev_info(bp->dev, "EEE is %s\n",
5320 bp->eee.eee_active ? "active" :
5321 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005322 } else {
5323 netif_carrier_off(bp->dev);
5324 netdev_err(bp->dev, "NIC Link is Down\n");
5325 }
5326}
5327
Michael Chan170ce012016-04-05 14:08:57 -04005328static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5329{
5330 int rc = 0;
5331 struct hwrm_port_phy_qcaps_input req = {0};
5332 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005333 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005334
5335 if (bp->hwrm_spec_code < 0x10201)
5336 return 0;
5337
5338 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5339
5340 mutex_lock(&bp->hwrm_cmd_lock);
5341 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5342 if (rc)
5343 goto hwrm_phy_qcaps_exit;
5344
5345 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5346 struct ethtool_eee *eee = &bp->eee;
5347 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5348
5349 bp->flags |= BNXT_FLAG_EEE_CAP;
5350 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5351 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5352 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5353 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5354 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5355 }
Michael Chan93ed8112016-06-13 02:25:37 -04005356 link_info->support_auto_speeds =
5357 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005358
5359hwrm_phy_qcaps_exit:
5360 mutex_unlock(&bp->hwrm_cmd_lock);
5361 return rc;
5362}
5363
Michael Chanc0c050c2015-10-22 16:01:17 -04005364static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5365{
5366 int rc = 0;
5367 struct bnxt_link_info *link_info = &bp->link_info;
5368 struct hwrm_port_phy_qcfg_input req = {0};
5369 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5370 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005371 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005372
5373 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5374
5375 mutex_lock(&bp->hwrm_cmd_lock);
5376 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5377 if (rc) {
5378 mutex_unlock(&bp->hwrm_cmd_lock);
5379 return rc;
5380 }
5381
5382 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5383 link_info->phy_link_status = resp->link;
5384 link_info->duplex = resp->duplex;
5385 link_info->pause = resp->pause;
5386 link_info->auto_mode = resp->auto_mode;
5387 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005388 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005389 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005390 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005391 if (link_info->phy_link_status == BNXT_LINK_LINK)
5392 link_info->link_speed = le16_to_cpu(resp->link_speed);
5393 else
5394 link_info->link_speed = 0;
5395 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005396 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5397 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005398 link_info->lp_auto_link_speeds =
5399 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005400 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5401 link_info->phy_ver[0] = resp->phy_maj;
5402 link_info->phy_ver[1] = resp->phy_min;
5403 link_info->phy_ver[2] = resp->phy_bld;
5404 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005405 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005406 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005407 link_info->phy_addr = resp->eee_config_phy_addr &
5408 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005409 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005410
Michael Chan170ce012016-04-05 14:08:57 -04005411 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5412 struct ethtool_eee *eee = &bp->eee;
5413 u16 fw_speeds;
5414
5415 eee->eee_active = 0;
5416 if (resp->eee_config_phy_addr &
5417 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5418 eee->eee_active = 1;
5419 fw_speeds = le16_to_cpu(
5420 resp->link_partner_adv_eee_link_speed_mask);
5421 eee->lp_advertised =
5422 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5423 }
5424
5425 /* Pull initial EEE config */
5426 if (!chng_link_state) {
5427 if (resp->eee_config_phy_addr &
5428 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5429 eee->eee_enabled = 1;
5430
5431 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5432 eee->advertised =
5433 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5434
5435 if (resp->eee_config_phy_addr &
5436 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5437 __le32 tmr;
5438
5439 eee->tx_lpi_enabled = 1;
5440 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5441 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5442 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5443 }
5444 }
5445 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005446 /* TODO: need to add more logic to report VF link */
5447 if (chng_link_state) {
5448 if (link_info->phy_link_status == BNXT_LINK_LINK)
5449 link_info->link_up = 1;
5450 else
5451 link_info->link_up = 0;
5452 if (link_up != link_info->link_up)
5453 bnxt_report_link(bp);
5454 } else {
5455 /* alwasy link down if not require to update link state */
5456 link_info->link_up = 0;
5457 }
5458 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005459
5460 diff = link_info->support_auto_speeds ^ link_info->advertising;
5461 if ((link_info->support_auto_speeds | diff) !=
5462 link_info->support_auto_speeds) {
5463 /* An advertised speed is no longer supported, so we need to
Michael Chan0eaa24b2017-01-25 02:55:08 -05005464 * update the advertisement settings. Caller holds RTNL
5465 * so we can modify link settings.
Michael Chan286ef9d2016-11-16 21:13:08 -05005466 */
Michael Chan286ef9d2016-11-16 21:13:08 -05005467 link_info->advertising = link_info->support_auto_speeds;
Michael Chan0eaa24b2017-01-25 02:55:08 -05005468 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
Michael Chan286ef9d2016-11-16 21:13:08 -05005469 bnxt_hwrm_set_link_setting(bp, true, false);
Michael Chan286ef9d2016-11-16 21:13:08 -05005470 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005471 return 0;
5472}
5473
Michael Chan10289be2016-05-15 03:04:49 -04005474static void bnxt_get_port_module_status(struct bnxt *bp)
5475{
5476 struct bnxt_link_info *link_info = &bp->link_info;
5477 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5478 u8 module_status;
5479
5480 if (bnxt_update_link(bp, true))
5481 return;
5482
5483 module_status = link_info->module_status;
5484 switch (module_status) {
5485 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5486 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5487 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5488 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5489 bp->pf.port_id);
5490 if (bp->hwrm_spec_code >= 0x10201) {
5491 netdev_warn(bp->dev, "Module part number %s\n",
5492 resp->phy_vendor_partnumber);
5493 }
5494 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5495 netdev_warn(bp->dev, "TX is disabled\n");
5496 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5497 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5498 }
5499}
5500
Michael Chanc0c050c2015-10-22 16:01:17 -04005501static void
5502bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5503{
5504 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005505 if (bp->hwrm_spec_code >= 0x10201)
5506 req->auto_pause =
5507 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005508 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5509 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5510 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005511 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005512 req->enables |=
5513 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5514 } else {
5515 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5516 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5517 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5518 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5519 req->enables |=
5520 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005521 if (bp->hwrm_spec_code >= 0x10201) {
5522 req->auto_pause = req->force_pause;
5523 req->enables |= cpu_to_le32(
5524 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5525 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005526 }
5527}
5528
5529static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5530 struct hwrm_port_phy_cfg_input *req)
5531{
5532 u8 autoneg = bp->link_info.autoneg;
5533 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005534 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005535
5536 if (autoneg & BNXT_AUTONEG_SPEED) {
5537 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005538 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005539
5540 req->enables |= cpu_to_le32(
5541 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5542 req->auto_link_speed_mask = cpu_to_le16(advertising);
5543
5544 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5545 req->flags |=
5546 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5547 } else {
5548 req->force_link_speed = cpu_to_le16(fw_link_speed);
5549 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5550 }
5551
Michael Chanc0c050c2015-10-22 16:01:17 -04005552 /* tell chimp that the setting takes effect immediately */
5553 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5554}
5555
5556int bnxt_hwrm_set_pause(struct bnxt *bp)
5557{
5558 struct hwrm_port_phy_cfg_input req = {0};
5559 int rc;
5560
5561 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5562 bnxt_hwrm_set_pause_common(bp, &req);
5563
5564 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5565 bp->link_info.force_link_chng)
5566 bnxt_hwrm_set_link_common(bp, &req);
5567
5568 mutex_lock(&bp->hwrm_cmd_lock);
5569 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5570 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5571 /* since changing of pause setting doesn't trigger any link
5572 * change event, the driver needs to update the current pause
5573 * result upon successfully return of the phy_cfg command
5574 */
5575 bp->link_info.pause =
5576 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5577 bp->link_info.auto_pause_setting = 0;
5578 if (!bp->link_info.force_link_chng)
5579 bnxt_report_link(bp);
5580 }
5581 bp->link_info.force_link_chng = false;
5582 mutex_unlock(&bp->hwrm_cmd_lock);
5583 return rc;
5584}
5585
Michael Chan939f7f02016-04-05 14:08:58 -04005586static void bnxt_hwrm_set_eee(struct bnxt *bp,
5587 struct hwrm_port_phy_cfg_input *req)
5588{
5589 struct ethtool_eee *eee = &bp->eee;
5590
5591 if (eee->eee_enabled) {
5592 u16 eee_speeds;
5593 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5594
5595 if (eee->tx_lpi_enabled)
5596 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5597 else
5598 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5599
5600 req->flags |= cpu_to_le32(flags);
5601 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5602 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5603 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5604 } else {
5605 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5606 }
5607}
5608
5609int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005610{
5611 struct hwrm_port_phy_cfg_input req = {0};
5612
5613 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5614 if (set_pause)
5615 bnxt_hwrm_set_pause_common(bp, &req);
5616
5617 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005618
5619 if (set_eee)
5620 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005621 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5622}
5623
Michael Chan33f7d552016-04-11 04:11:12 -04005624static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5625{
5626 struct hwrm_port_phy_cfg_input req = {0};
5627
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005628 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005629 return 0;
5630
5631 if (pci_num_vf(bp->pdev))
5632 return 0;
5633
5634 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005635 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005636 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5637}
5638
Michael Chan5ad2cbe2017-01-13 01:32:03 -05005639static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5640{
5641 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5642 struct hwrm_port_led_qcaps_input req = {0};
5643 struct bnxt_pf_info *pf = &bp->pf;
5644 int rc;
5645
5646 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5647 return 0;
5648
5649 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5650 req.port_id = cpu_to_le16(pf->port_id);
5651 mutex_lock(&bp->hwrm_cmd_lock);
5652 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5653 if (rc) {
5654 mutex_unlock(&bp->hwrm_cmd_lock);
5655 return rc;
5656 }
5657 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5658 int i;
5659
5660 bp->num_leds = resp->num_leds;
5661 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5662 bp->num_leds);
5663 for (i = 0; i < bp->num_leds; i++) {
5664 struct bnxt_led_info *led = &bp->leds[i];
5665 __le16 caps = led->led_state_caps;
5666
5667 if (!led->led_group_id ||
5668 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5669 bp->num_leds = 0;
5670 break;
5671 }
5672 }
5673 }
5674 mutex_unlock(&bp->hwrm_cmd_lock);
5675 return 0;
5676}
5677
Michael Chan939f7f02016-04-05 14:08:58 -04005678static bool bnxt_eee_config_ok(struct bnxt *bp)
5679{
5680 struct ethtool_eee *eee = &bp->eee;
5681 struct bnxt_link_info *link_info = &bp->link_info;
5682
5683 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5684 return true;
5685
5686 if (eee->eee_enabled) {
5687 u32 advertising =
5688 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5689
5690 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5691 eee->eee_enabled = 0;
5692 return false;
5693 }
5694 if (eee->advertised & ~advertising) {
5695 eee->advertised = advertising & eee->supported;
5696 return false;
5697 }
5698 }
5699 return true;
5700}
5701
Michael Chanc0c050c2015-10-22 16:01:17 -04005702static int bnxt_update_phy_setting(struct bnxt *bp)
5703{
5704 int rc;
5705 bool update_link = false;
5706 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005707 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005708 struct bnxt_link_info *link_info = &bp->link_info;
5709
5710 rc = bnxt_update_link(bp, true);
5711 if (rc) {
5712 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5713 rc);
5714 return rc;
5715 }
5716 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005717 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5718 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005719 update_pause = true;
5720 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5721 link_info->force_pause_setting != link_info->req_flow_ctrl)
5722 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005723 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5724 if (BNXT_AUTO_MODE(link_info->auto_mode))
5725 update_link = true;
5726 if (link_info->req_link_speed != link_info->force_link_speed)
5727 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005728 if (link_info->req_duplex != link_info->duplex_setting)
5729 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005730 } else {
5731 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5732 update_link = true;
5733 if (link_info->advertising != link_info->auto_link_speeds)
5734 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005735 }
5736
Michael Chan16d663a2016-11-16 21:13:07 -05005737 /* The last close may have shutdown the link, so need to call
5738 * PHY_CFG to bring it back up.
5739 */
5740 if (!netif_carrier_ok(bp->dev))
5741 update_link = true;
5742
Michael Chan939f7f02016-04-05 14:08:58 -04005743 if (!bnxt_eee_config_ok(bp))
5744 update_eee = true;
5745
Michael Chanc0c050c2015-10-22 16:01:17 -04005746 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005747 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005748 else if (update_pause)
5749 rc = bnxt_hwrm_set_pause(bp);
5750 if (rc) {
5751 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5752 rc);
5753 return rc;
5754 }
5755
5756 return rc;
5757}
5758
Jeffrey Huang11809492015-11-05 16:25:49 -05005759/* Common routine to pre-map certain register block to different GRC window.
5760 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5761 * in PF and 3 windows in VF that can be customized to map in different
5762 * register blocks.
5763 */
5764static void bnxt_preset_reg_win(struct bnxt *bp)
5765{
5766 if (BNXT_PF(bp)) {
5767 /* CAG registers map to GRC window #4 */
5768 writel(BNXT_CAG_REG_BASE,
5769 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5770 }
5771}
5772
Michael Chanc0c050c2015-10-22 16:01:17 -04005773static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5774{
5775 int rc = 0;
5776
Jeffrey Huang11809492015-11-05 16:25:49 -05005777 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005778 netif_carrier_off(bp->dev);
5779 if (irq_re_init) {
5780 rc = bnxt_setup_int_mode(bp);
5781 if (rc) {
5782 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5783 rc);
5784 return rc;
5785 }
5786 }
5787 if ((bp->flags & BNXT_FLAG_RFS) &&
5788 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5789 /* disable RFS if falling back to INTA */
5790 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5791 bp->flags &= ~BNXT_FLAG_RFS;
5792 }
5793
5794 rc = bnxt_alloc_mem(bp, irq_re_init);
5795 if (rc) {
5796 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5797 goto open_err_free_mem;
5798 }
5799
5800 if (irq_re_init) {
5801 bnxt_init_napi(bp);
5802 rc = bnxt_request_irq(bp);
5803 if (rc) {
5804 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5805 goto open_err;
5806 }
5807 }
5808
5809 bnxt_enable_napi(bp);
5810
5811 rc = bnxt_init_nic(bp, irq_re_init);
5812 if (rc) {
5813 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5814 goto open_err;
5815 }
5816
5817 if (link_re_init) {
5818 rc = bnxt_update_phy_setting(bp);
5819 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005820 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005821 }
5822
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005823 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005824 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005825
Michael Chancaefe522015-12-09 19:35:42 -05005826 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005827 bnxt_enable_int(bp);
5828 /* Enable TX queues */
5829 bnxt_tx_enable(bp);
5830 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005831 /* Poll link status and check for SFP+ module status */
5832 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005833
5834 return 0;
5835
5836open_err:
5837 bnxt_disable_napi(bp);
5838 bnxt_del_napi(bp);
5839
5840open_err_free_mem:
5841 bnxt_free_skbs(bp);
5842 bnxt_free_irq(bp);
5843 bnxt_free_mem(bp, true);
5844 return rc;
5845}
5846
5847/* rtnl_lock held */
5848int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5849{
5850 int rc = 0;
5851
5852 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5853 if (rc) {
5854 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5855 dev_close(bp->dev);
5856 }
5857 return rc;
5858}
5859
5860static int bnxt_open(struct net_device *dev)
5861{
5862 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005863
Michael Chanc0c050c2015-10-22 16:01:17 -04005864 return __bnxt_open_nic(bp, true, true);
5865}
5866
Michael Chanc0c050c2015-10-22 16:01:17 -04005867int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5868{
5869 int rc = 0;
5870
5871#ifdef CONFIG_BNXT_SRIOV
5872 if (bp->sriov_cfg) {
5873 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5874 !bp->sriov_cfg,
5875 BNXT_SRIOV_CFG_WAIT_TMO);
5876 if (rc)
5877 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5878 }
5879#endif
5880 /* Change device state to avoid TX queue wake up's */
5881 bnxt_tx_disable(bp);
5882
Michael Chancaefe522015-12-09 19:35:42 -05005883 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005884 smp_mb__after_atomic();
5885 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5886 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005887
Michael Chan9d8bc092016-12-29 12:13:33 -05005888 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04005889 bnxt_shutdown_nic(bp, irq_re_init);
5890
5891 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5892
5893 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005894 del_timer_sync(&bp->timer);
5895 bnxt_free_skbs(bp);
5896
5897 if (irq_re_init) {
5898 bnxt_free_irq(bp);
5899 bnxt_del_napi(bp);
5900 }
5901 bnxt_free_mem(bp, irq_re_init);
5902 return rc;
5903}
5904
5905static int bnxt_close(struct net_device *dev)
5906{
5907 struct bnxt *bp = netdev_priv(dev);
5908
5909 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005910 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005911 return 0;
5912}
5913
5914/* rtnl_lock held */
5915static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5916{
5917 switch (cmd) {
5918 case SIOCGMIIPHY:
5919 /* fallthru */
5920 case SIOCGMIIREG: {
5921 if (!netif_running(dev))
5922 return -EAGAIN;
5923
5924 return 0;
5925 }
5926
5927 case SIOCSMIIREG:
5928 if (!netif_running(dev))
5929 return -EAGAIN;
5930
5931 return 0;
5932
5933 default:
5934 /* do nothing */
5935 break;
5936 }
5937 return -EOPNOTSUPP;
5938}
5939
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005940static void
Michael Chanc0c050c2015-10-22 16:01:17 -04005941bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5942{
5943 u32 i;
5944 struct bnxt *bp = netdev_priv(dev);
5945
Michael Chanc0c050c2015-10-22 16:01:17 -04005946 if (!bp->bnapi)
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005947 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04005948
5949 /* TODO check if we need to synchronize with bnxt_close path */
5950 for (i = 0; i < bp->cp_nr_rings; i++) {
5951 struct bnxt_napi *bnapi = bp->bnapi[i];
5952 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5953 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5954
5955 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5956 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5957 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5958
5959 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5960 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5961 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5962
5963 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5964 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5965 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5966
5967 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5968 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5969 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5970
5971 stats->rx_missed_errors +=
5972 le64_to_cpu(hw_stats->rx_discard_pkts);
5973
5974 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5975
Michael Chanc0c050c2015-10-22 16:01:17 -04005976 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5977 }
5978
Michael Chan9947f832016-03-07 15:38:46 -05005979 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5980 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5981 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5982
5983 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5984 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5985 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5986 le64_to_cpu(rx->rx_ovrsz_frames) +
5987 le64_to_cpu(rx->rx_runt_frames);
5988 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5989 le64_to_cpu(rx->rx_jbr_frames);
5990 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5991 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5992 stats->tx_errors = le64_to_cpu(tx->tx_err);
5993 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005994}
5995
5996static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5997{
5998 struct net_device *dev = bp->dev;
5999 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6000 struct netdev_hw_addr *ha;
6001 u8 *haddr;
6002 int mc_count = 0;
6003 bool update = false;
6004 int off = 0;
6005
6006 netdev_for_each_mc_addr(ha, dev) {
6007 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6008 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6009 vnic->mc_list_count = 0;
6010 return false;
6011 }
6012 haddr = ha->addr;
6013 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6014 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6015 update = true;
6016 }
6017 off += ETH_ALEN;
6018 mc_count++;
6019 }
6020 if (mc_count)
6021 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6022
6023 if (mc_count != vnic->mc_list_count) {
6024 vnic->mc_list_count = mc_count;
6025 update = true;
6026 }
6027 return update;
6028}
6029
6030static bool bnxt_uc_list_updated(struct bnxt *bp)
6031{
6032 struct net_device *dev = bp->dev;
6033 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6034 struct netdev_hw_addr *ha;
6035 int off = 0;
6036
6037 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6038 return true;
6039
6040 netdev_for_each_uc_addr(ha, dev) {
6041 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6042 return true;
6043
6044 off += ETH_ALEN;
6045 }
6046 return false;
6047}
6048
6049static void bnxt_set_rx_mode(struct net_device *dev)
6050{
6051 struct bnxt *bp = netdev_priv(dev);
6052 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6053 u32 mask = vnic->rx_mask;
6054 bool mc_update = false;
6055 bool uc_update;
6056
6057 if (!netif_running(dev))
6058 return;
6059
6060 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6061 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6062 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6063
Michael Chan17c71ac2016-07-01 18:46:27 -04006064 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006065 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6066
6067 uc_update = bnxt_uc_list_updated(bp);
6068
6069 if (dev->flags & IFF_ALLMULTI) {
6070 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6071 vnic->mc_list_count = 0;
6072 } else {
6073 mc_update = bnxt_mc_list_updated(bp, &mask);
6074 }
6075
6076 if (mask != vnic->rx_mask || uc_update || mc_update) {
6077 vnic->rx_mask = mask;
6078
6079 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6080 schedule_work(&bp->sp_task);
6081 }
6082}
6083
Michael Chanb664f002015-12-02 01:54:08 -05006084static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006085{
6086 struct net_device *dev = bp->dev;
6087 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6088 struct netdev_hw_addr *ha;
6089 int i, off = 0, rc;
6090 bool uc_update;
6091
6092 netif_addr_lock_bh(dev);
6093 uc_update = bnxt_uc_list_updated(bp);
6094 netif_addr_unlock_bh(dev);
6095
6096 if (!uc_update)
6097 goto skip_uc;
6098
6099 mutex_lock(&bp->hwrm_cmd_lock);
6100 for (i = 1; i < vnic->uc_filter_count; i++) {
6101 struct hwrm_cfa_l2_filter_free_input req = {0};
6102
6103 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6104 -1);
6105
6106 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6107
6108 rc = _hwrm_send_message(bp, &req, sizeof(req),
6109 HWRM_CMD_TIMEOUT);
6110 }
6111 mutex_unlock(&bp->hwrm_cmd_lock);
6112
6113 vnic->uc_filter_count = 1;
6114
6115 netif_addr_lock_bh(dev);
6116 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6117 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6118 } else {
6119 netdev_for_each_uc_addr(ha, dev) {
6120 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6121 off += ETH_ALEN;
6122 vnic->uc_filter_count++;
6123 }
6124 }
6125 netif_addr_unlock_bh(dev);
6126
6127 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6128 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6129 if (rc) {
6130 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6131 rc);
6132 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05006133 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006134 }
6135 }
6136
6137skip_uc:
6138 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6139 if (rc)
6140 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6141 rc);
Michael Chanb664f002015-12-02 01:54:08 -05006142
6143 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006144}
6145
Michael Chan8079e8f2016-12-29 12:13:37 -05006146/* If the chip and firmware supports RFS */
6147static bool bnxt_rfs_supported(struct bnxt *bp)
6148{
6149 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6150 return true;
Michael Chanae10ae72016-12-29 12:13:38 -05006151 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6152 return true;
Michael Chan8079e8f2016-12-29 12:13:37 -05006153 return false;
6154}
6155
6156/* If runtime conditions support RFS */
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006157static bool bnxt_rfs_capable(struct bnxt *bp)
6158{
6159#ifdef CONFIG_RFS_ACCEL
Michael Chan8079e8f2016-12-29 12:13:37 -05006160 int vnics, max_vnics, max_rss_ctxs;
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006161
6162 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
6163 return false;
6164
6165 vnics = 1 + bp->rx_nr_rings;
Michael Chan8079e8f2016-12-29 12:13:37 -05006166 max_vnics = bnxt_get_max_func_vnics(bp);
6167 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
Michael Chanae10ae72016-12-29 12:13:38 -05006168
6169 /* RSS contexts not a limiting factor */
6170 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6171 max_rss_ctxs = max_vnics;
Michael Chan8079e8f2016-12-29 12:13:37 -05006172 if (vnics > max_vnics || vnics > max_rss_ctxs) {
Vasundhara Volama2304902016-07-25 12:33:36 -04006173 netdev_warn(bp->dev,
6174 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
Michael Chan8079e8f2016-12-29 12:13:37 -05006175 min(max_rss_ctxs - 1, max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006176 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04006177 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006178
6179 return true;
6180#else
6181 return false;
6182#endif
6183}
6184
Michael Chanc0c050c2015-10-22 16:01:17 -04006185static netdev_features_t bnxt_fix_features(struct net_device *dev,
6186 netdev_features_t features)
6187{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006188 struct bnxt *bp = netdev_priv(dev);
6189
Vasundhara Volama2304902016-07-25 12:33:36 -04006190 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006191 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04006192
6193 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6194 * turned on or off together.
6195 */
6196 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6197 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6198 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6199 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6200 NETIF_F_HW_VLAN_STAG_RX);
6201 else
6202 features |= NETIF_F_HW_VLAN_CTAG_RX |
6203 NETIF_F_HW_VLAN_STAG_RX;
6204 }
Michael Chancf6645f2016-06-13 02:25:28 -04006205#ifdef CONFIG_BNXT_SRIOV
6206 if (BNXT_VF(bp)) {
6207 if (bp->vf.vlan) {
6208 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6209 NETIF_F_HW_VLAN_STAG_RX);
6210 }
6211 }
6212#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006213 return features;
6214}
6215
6216static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6217{
6218 struct bnxt *bp = netdev_priv(dev);
6219 u32 flags = bp->flags;
6220 u32 changes;
6221 int rc = 0;
6222 bool re_init = false;
6223 bool update_tpa = false;
6224
6225 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006226 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006227 flags |= BNXT_FLAG_GRO;
6228 if (features & NETIF_F_LRO)
6229 flags |= BNXT_FLAG_LRO;
6230
Michael Chanbdbd1eb2016-12-29 12:13:43 -05006231 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6232 flags &= ~BNXT_FLAG_TPA;
6233
Michael Chanc0c050c2015-10-22 16:01:17 -04006234 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6235 flags |= BNXT_FLAG_STRIP_VLAN;
6236
6237 if (features & NETIF_F_NTUPLE)
6238 flags |= BNXT_FLAG_RFS;
6239
6240 changes = flags ^ bp->flags;
6241 if (changes & BNXT_FLAG_TPA) {
6242 update_tpa = true;
6243 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6244 (flags & BNXT_FLAG_TPA) == 0)
6245 re_init = true;
6246 }
6247
6248 if (changes & ~BNXT_FLAG_TPA)
6249 re_init = true;
6250
6251 if (flags != bp->flags) {
6252 u32 old_flags = bp->flags;
6253
6254 bp->flags = flags;
6255
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006256 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006257 if (update_tpa)
6258 bnxt_set_ring_params(bp);
6259 return rc;
6260 }
6261
6262 if (re_init) {
6263 bnxt_close_nic(bp, false, false);
6264 if (update_tpa)
6265 bnxt_set_ring_params(bp);
6266
6267 return bnxt_open_nic(bp, false, false);
6268 }
6269 if (update_tpa) {
6270 rc = bnxt_set_tpa(bp,
6271 (flags & BNXT_FLAG_TPA) ?
6272 true : false);
6273 if (rc)
6274 bp->flags = old_flags;
6275 }
6276 }
6277 return rc;
6278}
6279
Michael Chan9f554592016-01-02 23:44:58 -05006280static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6281{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006282 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006283 int i = bnapi->index;
6284
Michael Chan3b2b7d92016-01-02 23:45:00 -05006285 if (!txr)
6286 return;
6287
Michael Chan9f554592016-01-02 23:44:58 -05006288 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6289 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6290 txr->tx_cons);
6291}
6292
6293static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6294{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006295 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006296 int i = bnapi->index;
6297
Michael Chan3b2b7d92016-01-02 23:45:00 -05006298 if (!rxr)
6299 return;
6300
Michael Chan9f554592016-01-02 23:44:58 -05006301 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",
6302 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6303 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6304 rxr->rx_sw_agg_prod);
6305}
6306
6307static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6308{
6309 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6310 int i = bnapi->index;
6311
6312 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6313 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6314}
6315
Michael Chanc0c050c2015-10-22 16:01:17 -04006316static void bnxt_dbg_dump_states(struct bnxt *bp)
6317{
6318 int i;
6319 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006320
6321 for (i = 0; i < bp->cp_nr_rings; i++) {
6322 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006323 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006324 bnxt_dump_tx_sw_state(bnapi);
6325 bnxt_dump_rx_sw_state(bnapi);
6326 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006327 }
6328 }
6329}
6330
Michael Chan6988bd92016-06-13 02:25:29 -04006331static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006332{
Michael Chan6988bd92016-06-13 02:25:29 -04006333 if (!silent)
6334 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006335 if (netif_running(bp->dev)) {
6336 bnxt_close_nic(bp, false, false);
6337 bnxt_open_nic(bp, false, false);
6338 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006339}
6340
6341static void bnxt_tx_timeout(struct net_device *dev)
6342{
6343 struct bnxt *bp = netdev_priv(dev);
6344
6345 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6346 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6347 schedule_work(&bp->sp_task);
6348}
6349
6350#ifdef CONFIG_NET_POLL_CONTROLLER
6351static void bnxt_poll_controller(struct net_device *dev)
6352{
6353 struct bnxt *bp = netdev_priv(dev);
6354 int i;
6355
6356 for (i = 0; i < bp->cp_nr_rings; i++) {
6357 struct bnxt_irq *irq = &bp->irq_tbl[i];
6358
6359 disable_irq(irq->vector);
6360 irq->handler(irq->vector, bp->bnapi[i]);
6361 enable_irq(irq->vector);
6362 }
6363}
6364#endif
6365
6366static void bnxt_timer(unsigned long data)
6367{
6368 struct bnxt *bp = (struct bnxt *)data;
6369 struct net_device *dev = bp->dev;
6370
6371 if (!netif_running(dev))
6372 return;
6373
6374 if (atomic_read(&bp->intr_sem) != 0)
6375 goto bnxt_restart_timer;
6376
Michael Chan3bdf56c2016-03-07 15:38:45 -05006377 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6378 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6379 schedule_work(&bp->sp_task);
6380 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006381bnxt_restart_timer:
6382 mod_timer(&bp->timer, jiffies + bp->current_interval);
6383}
6384
Michael Chana551ee92017-01-25 02:55:07 -05006385static void bnxt_rtnl_lock_sp(struct bnxt *bp)
Michael Chan6988bd92016-06-13 02:25:29 -04006386{
Michael Chana551ee92017-01-25 02:55:07 -05006387 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6388 * set. If the device is being closed, bnxt_close() may be holding
Michael Chan6988bd92016-06-13 02:25:29 -04006389 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6390 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6391 */
6392 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6393 rtnl_lock();
Michael Chana551ee92017-01-25 02:55:07 -05006394}
6395
6396static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6397{
Michael Chan6988bd92016-06-13 02:25:29 -04006398 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6399 rtnl_unlock();
6400}
6401
Michael Chana551ee92017-01-25 02:55:07 -05006402/* Only called from bnxt_sp_task() */
6403static void bnxt_reset(struct bnxt *bp, bool silent)
6404{
6405 bnxt_rtnl_lock_sp(bp);
6406 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6407 bnxt_reset_task(bp, silent);
6408 bnxt_rtnl_unlock_sp(bp);
6409}
6410
Michael Chanc0c050c2015-10-22 16:01:17 -04006411static void bnxt_cfg_ntp_filters(struct bnxt *);
6412
6413static void bnxt_sp_task(struct work_struct *work)
6414{
6415 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006416
Michael Chan4cebdce2015-12-09 19:35:43 -05006417 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6418 smp_mb__after_atomic();
6419 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6420 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006421 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006422 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006423
6424 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6425 bnxt_cfg_rx_mode(bp);
6426
6427 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6428 bnxt_cfg_ntp_filters(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006429 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6430 bnxt_hwrm_exec_fwd_req(bp);
6431 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6432 bnxt_hwrm_tunnel_dst_port_alloc(
6433 bp, bp->vxlan_port,
6434 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6435 }
6436 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6437 bnxt_hwrm_tunnel_dst_port_free(
6438 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6439 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006440 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6441 bnxt_hwrm_tunnel_dst_port_alloc(
6442 bp, bp->nge_port,
6443 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6444 }
6445 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6446 bnxt_hwrm_tunnel_dst_port_free(
6447 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6448 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05006449 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6450 bnxt_hwrm_port_qstats(bp);
6451
Michael Chana551ee92017-01-25 02:55:07 -05006452 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6453 * must be the last functions to be called before exiting.
6454 */
Michael Chan0eaa24b2017-01-25 02:55:08 -05006455 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6456 int rc = 0;
6457
6458 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6459 &bp->sp_event))
6460 bnxt_hwrm_phy_qcaps(bp);
6461
6462 bnxt_rtnl_lock_sp(bp);
6463 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6464 rc = bnxt_update_link(bp, true);
6465 bnxt_rtnl_unlock_sp(bp);
6466 if (rc)
6467 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6468 rc);
6469 }
Michael Chan90c694b2017-01-25 02:55:09 -05006470 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6471 bnxt_rtnl_lock_sp(bp);
6472 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6473 bnxt_get_port_module_status(bp);
6474 bnxt_rtnl_unlock_sp(bp);
6475 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006476 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6477 bnxt_reset(bp, false);
6478
6479 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6480 bnxt_reset(bp, true);
6481
Michael Chanc0c050c2015-10-22 16:01:17 -04006482 smp_mb__before_atomic();
6483 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6484}
6485
6486static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6487{
6488 int rc;
6489 struct bnxt *bp = netdev_priv(dev);
6490
6491 SET_NETDEV_DEV(dev, &pdev->dev);
6492
6493 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6494 rc = pci_enable_device(pdev);
6495 if (rc) {
6496 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6497 goto init_err;
6498 }
6499
6500 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6501 dev_err(&pdev->dev,
6502 "Cannot find PCI device base address, aborting\n");
6503 rc = -ENODEV;
6504 goto init_err_disable;
6505 }
6506
6507 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6508 if (rc) {
6509 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6510 goto init_err_disable;
6511 }
6512
6513 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6514 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6515 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6516 goto init_err_disable;
6517 }
6518
6519 pci_set_master(pdev);
6520
6521 bp->dev = dev;
6522 bp->pdev = pdev;
6523
6524 bp->bar0 = pci_ioremap_bar(pdev, 0);
6525 if (!bp->bar0) {
6526 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6527 rc = -ENOMEM;
6528 goto init_err_release;
6529 }
6530
6531 bp->bar1 = pci_ioremap_bar(pdev, 2);
6532 if (!bp->bar1) {
6533 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6534 rc = -ENOMEM;
6535 goto init_err_release;
6536 }
6537
6538 bp->bar2 = pci_ioremap_bar(pdev, 4);
6539 if (!bp->bar2) {
6540 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6541 rc = -ENOMEM;
6542 goto init_err_release;
6543 }
6544
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006545 pci_enable_pcie_error_reporting(pdev);
6546
Michael Chanc0c050c2015-10-22 16:01:17 -04006547 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6548
6549 spin_lock_init(&bp->ntp_fltr_lock);
6550
6551 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6552 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6553
Michael Chandfb5b892016-02-26 04:00:01 -05006554 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006555 bp->rx_coal_ticks = 12;
6556 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006557 bp->rx_coal_ticks_irq = 1;
6558 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006559
Michael Chandfc9c942016-02-26 04:00:03 -05006560 bp->tx_coal_ticks = 25;
6561 bp->tx_coal_bufs = 30;
6562 bp->tx_coal_ticks_irq = 2;
6563 bp->tx_coal_bufs_irq = 2;
6564
Michael Chan51f30782016-07-01 18:46:29 -04006565 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6566
Michael Chanc0c050c2015-10-22 16:01:17 -04006567 init_timer(&bp->timer);
6568 bp->timer.data = (unsigned long)bp;
6569 bp->timer.function = bnxt_timer;
6570 bp->current_interval = BNXT_TIMER_INTERVAL;
6571
Michael Chancaefe522015-12-09 19:35:42 -05006572 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006573
6574 return 0;
6575
6576init_err_release:
6577 if (bp->bar2) {
6578 pci_iounmap(pdev, bp->bar2);
6579 bp->bar2 = NULL;
6580 }
6581
6582 if (bp->bar1) {
6583 pci_iounmap(pdev, bp->bar1);
6584 bp->bar1 = NULL;
6585 }
6586
6587 if (bp->bar0) {
6588 pci_iounmap(pdev, bp->bar0);
6589 bp->bar0 = NULL;
6590 }
6591
6592 pci_release_regions(pdev);
6593
6594init_err_disable:
6595 pci_disable_device(pdev);
6596
6597init_err:
6598 return rc;
6599}
6600
6601/* rtnl_lock held */
6602static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6603{
6604 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006605 struct bnxt *bp = netdev_priv(dev);
6606 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006607
6608 if (!is_valid_ether_addr(addr->sa_data))
6609 return -EADDRNOTAVAIL;
6610
Michael Chan84c33dd2016-04-11 04:11:13 -04006611 rc = bnxt_approve_mac(bp, addr->sa_data);
6612 if (rc)
6613 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006614
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006615 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6616 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006617
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006618 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6619 if (netif_running(dev)) {
6620 bnxt_close_nic(bp, false, false);
6621 rc = bnxt_open_nic(bp, false, false);
6622 }
6623
6624 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006625}
6626
6627/* rtnl_lock held */
6628static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6629{
6630 struct bnxt *bp = netdev_priv(dev);
6631
Michael Chanc0c050c2015-10-22 16:01:17 -04006632 if (netif_running(dev))
6633 bnxt_close_nic(bp, false, false);
6634
6635 dev->mtu = new_mtu;
6636 bnxt_set_ring_params(bp);
6637
6638 if (netif_running(dev))
6639 return bnxt_open_nic(bp, false, false);
6640
6641 return 0;
6642}
6643
Michael Chanc5e3deb2016-12-02 21:17:15 -05006644int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006645{
6646 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006647 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006648
Michael Chanc0c050c2015-10-22 16:01:17 -04006649 if (tc > bp->max_tc) {
6650 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6651 tc, bp->max_tc);
6652 return -EINVAL;
6653 }
6654
6655 if (netdev_get_num_tc(dev) == tc)
6656 return 0;
6657
Michael Chan3ffb6a32016-11-11 00:11:42 -05006658 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6659 sh = true;
6660
Michael Chanc0c050c2015-10-22 16:01:17 -04006661 if (tc) {
Michael Chan391be5c2016-12-29 12:13:41 -05006662 int max_rx_rings, max_tx_rings, req_tx_rings, rsv_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006663
Michael Chan391be5c2016-12-29 12:13:41 -05006664 req_tx_rings = bp->tx_nr_rings_per_tc * tc;
Michael Chan01657bc2016-01-02 23:45:03 -05006665 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan391be5c2016-12-29 12:13:41 -05006666 if (rc || req_tx_rings > max_tx_rings)
6667 return -ENOMEM;
6668
6669 rsv_tx_rings = req_tx_rings;
6670 if (bnxt_hwrm_reserve_tx_rings(bp, &rsv_tx_rings) ||
6671 rsv_tx_rings < req_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006672 return -ENOMEM;
6673 }
6674
6675 /* Needs to close the device and do hw resource re-allocations */
6676 if (netif_running(bp->dev))
6677 bnxt_close_nic(bp, true, false);
6678
6679 if (tc) {
6680 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6681 netdev_set_num_tc(dev, tc);
6682 } else {
6683 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6684 netdev_reset_tc(dev);
6685 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006686 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6687 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006688 bp->num_stat_ctxs = bp->cp_nr_rings;
6689
6690 if (netif_running(bp->dev))
6691 return bnxt_open_nic(bp, true, false);
6692
6693 return 0;
6694}
6695
Michael Chanc5e3deb2016-12-02 21:17:15 -05006696static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6697 struct tc_to_netdev *ntc)
6698{
6699 if (ntc->type != TC_SETUP_MQPRIO)
6700 return -EINVAL;
6701
6702 return bnxt_setup_mq_tc(dev, ntc->tc);
6703}
6704
Michael Chanc0c050c2015-10-22 16:01:17 -04006705#ifdef CONFIG_RFS_ACCEL
6706static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6707 struct bnxt_ntuple_filter *f2)
6708{
6709 struct flow_keys *keys1 = &f1->fkeys;
6710 struct flow_keys *keys2 = &f2->fkeys;
6711
6712 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6713 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6714 keys1->ports.ports == keys2->ports.ports &&
6715 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6716 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006717 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6718 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006719 return true;
6720
6721 return false;
6722}
6723
6724static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6725 u16 rxq_index, u32 flow_id)
6726{
6727 struct bnxt *bp = netdev_priv(dev);
6728 struct bnxt_ntuple_filter *fltr, *new_fltr;
6729 struct flow_keys *fkeys;
6730 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006731 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006732 struct hlist_head *head;
6733
6734 if (skb->encapsulation)
6735 return -EPROTONOSUPPORT;
6736
Michael Chana54c4d72016-07-25 12:33:35 -04006737 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6738 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6739 int off = 0, j;
6740
6741 netif_addr_lock_bh(dev);
6742 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6743 if (ether_addr_equal(eth->h_dest,
6744 vnic->uc_list + off)) {
6745 l2_idx = j + 1;
6746 break;
6747 }
6748 }
6749 netif_addr_unlock_bh(dev);
6750 if (!l2_idx)
6751 return -EINVAL;
6752 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006753 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6754 if (!new_fltr)
6755 return -ENOMEM;
6756
6757 fkeys = &new_fltr->fkeys;
6758 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6759 rc = -EPROTONOSUPPORT;
6760 goto err_free;
6761 }
6762
Michael Chandda0e742016-12-29 12:13:40 -05006763 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
6764 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
Michael Chanc0c050c2015-10-22 16:01:17 -04006765 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6766 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6767 rc = -EPROTONOSUPPORT;
6768 goto err_free;
6769 }
Michael Chandda0e742016-12-29 12:13:40 -05006770 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
6771 bp->hwrm_spec_code < 0x10601) {
6772 rc = -EPROTONOSUPPORT;
6773 goto err_free;
6774 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006775
Michael Chana54c4d72016-07-25 12:33:35 -04006776 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006777 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6778
6779 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6780 head = &bp->ntp_fltr_hash_tbl[idx];
6781 rcu_read_lock();
6782 hlist_for_each_entry_rcu(fltr, head, hash) {
6783 if (bnxt_fltr_match(fltr, new_fltr)) {
6784 rcu_read_unlock();
6785 rc = 0;
6786 goto err_free;
6787 }
6788 }
6789 rcu_read_unlock();
6790
6791 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006792 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6793 BNXT_NTP_FLTR_MAX_FLTR, 0);
6794 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006795 spin_unlock_bh(&bp->ntp_fltr_lock);
6796 rc = -ENOMEM;
6797 goto err_free;
6798 }
6799
Michael Chan84e86b92015-11-05 16:25:50 -05006800 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006801 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006802 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006803 new_fltr->rxq = rxq_index;
6804 hlist_add_head_rcu(&new_fltr->hash, head);
6805 bp->ntp_fltr_count++;
6806 spin_unlock_bh(&bp->ntp_fltr_lock);
6807
6808 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6809 schedule_work(&bp->sp_task);
6810
6811 return new_fltr->sw_id;
6812
6813err_free:
6814 kfree(new_fltr);
6815 return rc;
6816}
6817
6818static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6819{
6820 int i;
6821
6822 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6823 struct hlist_head *head;
6824 struct hlist_node *tmp;
6825 struct bnxt_ntuple_filter *fltr;
6826 int rc;
6827
6828 head = &bp->ntp_fltr_hash_tbl[i];
6829 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6830 bool del = false;
6831
6832 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6833 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6834 fltr->flow_id,
6835 fltr->sw_id)) {
6836 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6837 fltr);
6838 del = true;
6839 }
6840 } else {
6841 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6842 fltr);
6843 if (rc)
6844 del = true;
6845 else
6846 set_bit(BNXT_FLTR_VALID, &fltr->state);
6847 }
6848
6849 if (del) {
6850 spin_lock_bh(&bp->ntp_fltr_lock);
6851 hlist_del_rcu(&fltr->hash);
6852 bp->ntp_fltr_count--;
6853 spin_unlock_bh(&bp->ntp_fltr_lock);
6854 synchronize_rcu();
6855 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6856 kfree(fltr);
6857 }
6858 }
6859 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006860 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6861 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006862}
6863
6864#else
6865
6866static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6867{
6868}
6869
6870#endif /* CONFIG_RFS_ACCEL */
6871
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006872static void bnxt_udp_tunnel_add(struct net_device *dev,
6873 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006874{
6875 struct bnxt *bp = netdev_priv(dev);
6876
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006877 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6878 return;
6879
Michael Chanc0c050c2015-10-22 16:01:17 -04006880 if (!netif_running(dev))
6881 return;
6882
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006883 switch (ti->type) {
6884 case UDP_TUNNEL_TYPE_VXLAN:
6885 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6886 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006887
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006888 bp->vxlan_port_cnt++;
6889 if (bp->vxlan_port_cnt == 1) {
6890 bp->vxlan_port = ti->port;
6891 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006892 schedule_work(&bp->sp_task);
6893 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006894 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006895 case UDP_TUNNEL_TYPE_GENEVE:
6896 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6897 return;
6898
6899 bp->nge_port_cnt++;
6900 if (bp->nge_port_cnt == 1) {
6901 bp->nge_port = ti->port;
6902 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6903 }
6904 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006905 default:
6906 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006907 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006908
6909 schedule_work(&bp->sp_task);
6910}
6911
6912static void bnxt_udp_tunnel_del(struct net_device *dev,
6913 struct udp_tunnel_info *ti)
6914{
6915 struct bnxt *bp = netdev_priv(dev);
6916
6917 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6918 return;
6919
6920 if (!netif_running(dev))
6921 return;
6922
6923 switch (ti->type) {
6924 case UDP_TUNNEL_TYPE_VXLAN:
6925 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6926 return;
6927 bp->vxlan_port_cnt--;
6928
6929 if (bp->vxlan_port_cnt != 0)
6930 return;
6931
6932 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6933 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006934 case UDP_TUNNEL_TYPE_GENEVE:
6935 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6936 return;
6937 bp->nge_port_cnt--;
6938
6939 if (bp->nge_port_cnt != 0)
6940 return;
6941
6942 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6943 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006944 default:
6945 return;
6946 }
6947
6948 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006949}
6950
6951static const struct net_device_ops bnxt_netdev_ops = {
6952 .ndo_open = bnxt_open,
6953 .ndo_start_xmit = bnxt_start_xmit,
6954 .ndo_stop = bnxt_close,
6955 .ndo_get_stats64 = bnxt_get_stats64,
6956 .ndo_set_rx_mode = bnxt_set_rx_mode,
6957 .ndo_do_ioctl = bnxt_ioctl,
6958 .ndo_validate_addr = eth_validate_addr,
6959 .ndo_set_mac_address = bnxt_change_mac_addr,
6960 .ndo_change_mtu = bnxt_change_mtu,
6961 .ndo_fix_features = bnxt_fix_features,
6962 .ndo_set_features = bnxt_set_features,
6963 .ndo_tx_timeout = bnxt_tx_timeout,
6964#ifdef CONFIG_BNXT_SRIOV
6965 .ndo_get_vf_config = bnxt_get_vf_config,
6966 .ndo_set_vf_mac = bnxt_set_vf_mac,
6967 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6968 .ndo_set_vf_rate = bnxt_set_vf_bw,
6969 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6970 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6971#endif
6972#ifdef CONFIG_NET_POLL_CONTROLLER
6973 .ndo_poll_controller = bnxt_poll_controller,
6974#endif
6975 .ndo_setup_tc = bnxt_setup_tc,
6976#ifdef CONFIG_RFS_ACCEL
6977 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6978#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006979 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6980 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006981};
6982
6983static void bnxt_remove_one(struct pci_dev *pdev)
6984{
6985 struct net_device *dev = pci_get_drvdata(pdev);
6986 struct bnxt *bp = netdev_priv(dev);
6987
6988 if (BNXT_PF(bp))
6989 bnxt_sriov_disable(bp);
6990
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006991 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006992 unregister_netdev(dev);
6993 cancel_work_sync(&bp->sp_task);
6994 bp->sp_event = 0;
6995
Michael Chan78095922016-12-07 00:26:16 -05006996 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006997 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006998 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05006999 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007000 pci_iounmap(pdev, bp->bar2);
7001 pci_iounmap(pdev, bp->bar1);
7002 pci_iounmap(pdev, bp->bar0);
Michael Chana588e452016-12-07 00:26:21 -05007003 kfree(bp->edev);
7004 bp->edev = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04007005 free_netdev(dev);
7006
7007 pci_release_regions(pdev);
7008 pci_disable_device(pdev);
7009}
7010
7011static int bnxt_probe_phy(struct bnxt *bp)
7012{
7013 int rc = 0;
7014 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04007015
Michael Chan170ce012016-04-05 14:08:57 -04007016 rc = bnxt_hwrm_phy_qcaps(bp);
7017 if (rc) {
7018 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7019 rc);
7020 return rc;
7021 }
7022
Michael Chanc0c050c2015-10-22 16:01:17 -04007023 rc = bnxt_update_link(bp, false);
7024 if (rc) {
7025 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7026 rc);
7027 return rc;
7028 }
7029
Michael Chan93ed8112016-06-13 02:25:37 -04007030 /* Older firmware does not have supported_auto_speeds, so assume
7031 * that all supported speeds can be autonegotiated.
7032 */
7033 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7034 link_info->support_auto_speeds = link_info->support_speeds;
7035
Michael Chanc0c050c2015-10-22 16:01:17 -04007036 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05007037 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04007038 link_info->autoneg = BNXT_AUTONEG_SPEED;
7039 if (bp->hwrm_spec_code >= 0x10201) {
7040 if (link_info->auto_pause_setting &
7041 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7042 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7043 } else {
7044 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7045 }
Michael Chan0d8abf02016-02-10 17:33:47 -05007046 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05007047 } else {
7048 link_info->req_link_speed = link_info->force_link_speed;
7049 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007050 }
Michael Chanc9ee9512016-04-05 14:08:56 -04007051 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7052 link_info->req_flow_ctrl =
7053 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7054 else
7055 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04007056 return rc;
7057}
7058
7059static int bnxt_get_max_irq(struct pci_dev *pdev)
7060{
7061 u16 ctrl;
7062
7063 if (!pdev->msix_cap)
7064 return 1;
7065
7066 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7067 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7068}
7069
Michael Chan6e6c5a52016-01-02 23:45:02 -05007070static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7071 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04007072{
Michael Chan6e6c5a52016-01-02 23:45:02 -05007073 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04007074
Michael Chan379a80a2015-10-23 15:06:19 -04007075#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007076 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04007077 *max_tx = bp->vf.max_tx_rings;
7078 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007079 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7080 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05007081 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007082 } else
Michael Chan379a80a2015-10-23 15:06:19 -04007083#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01007084 {
7085 *max_tx = bp->pf.max_tx_rings;
7086 *max_rx = bp->pf.max_rx_rings;
7087 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7088 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7089 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04007090 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04007091 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7092 *max_cp -= 1;
7093 *max_rx -= 2;
7094 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007095 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7096 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05007097 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007098}
7099
7100int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7101{
7102 int rx, tx, cp;
7103
7104 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7105 if (!rx || !tx || !cp)
7106 return -ENOMEM;
7107
7108 *max_rx = rx;
7109 *max_tx = tx;
7110 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7111}
7112
Michael Chane4060d32016-12-07 00:26:19 -05007113static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7114 bool shared)
7115{
7116 int rc;
7117
7118 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007119 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7120 /* Not enough rings, try disabling agg rings. */
7121 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7122 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7123 if (rc)
7124 return rc;
7125 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7126 bp->dev->hw_features &= ~NETIF_F_LRO;
7127 bp->dev->features &= ~NETIF_F_LRO;
7128 bnxt_set_ring_params(bp);
7129 }
Michael Chane4060d32016-12-07 00:26:19 -05007130
7131 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7132 int max_cp, max_stat, max_irq;
7133
7134 /* Reserve minimum resources for RoCE */
7135 max_cp = bnxt_get_max_func_cp_rings(bp);
7136 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7137 max_irq = bnxt_get_max_func_irqs(bp);
7138 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7139 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7140 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7141 return 0;
7142
7143 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7144 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7145 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7146 max_cp = min_t(int, max_cp, max_irq);
7147 max_cp = min_t(int, max_cp, max_stat);
7148 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7149 if (rc)
7150 rc = 0;
7151 }
7152 return rc;
7153}
7154
Michael Chan6e6c5a52016-01-02 23:45:02 -05007155static int bnxt_set_dflt_rings(struct bnxt *bp)
7156{
7157 int dflt_rings, max_rx_rings, max_tx_rings, rc;
7158 bool sh = true;
7159
7160 if (sh)
7161 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7162 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05007163 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007164 if (rc)
7165 return rc;
7166 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7167 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
Michael Chan391be5c2016-12-29 12:13:41 -05007168
7169 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7170 if (rc)
7171 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7172
Michael Chan6e6c5a52016-01-02 23:45:02 -05007173 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7174 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7175 bp->tx_nr_rings + bp->rx_nr_rings;
7176 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04007177 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7178 bp->rx_nr_rings++;
7179 bp->cp_nr_rings++;
7180 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05007181 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04007182}
7183
Michael Chan7b08f662016-12-07 00:26:18 -05007184void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7185{
7186 ASSERT_RTNL();
7187 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05007188 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05007189}
7190
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007191static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7192{
7193 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7194 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7195
7196 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7197 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7198 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7199 else
7200 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7201 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7202 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7203 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7204 "Unknown", width);
7205}
7206
Michael Chanc0c050c2015-10-22 16:01:17 -04007207static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7208{
7209 static int version_printed;
7210 struct net_device *dev;
7211 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05007212 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04007213
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04007214 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
7215 return -ENODEV;
7216
Michael Chanc0c050c2015-10-22 16:01:17 -04007217 if (version_printed++ == 0)
7218 pr_info("%s", version);
7219
7220 max_irqs = bnxt_get_max_irq(pdev);
7221 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7222 if (!dev)
7223 return -ENOMEM;
7224
7225 bp = netdev_priv(dev);
7226
7227 if (bnxt_vf_pciid(ent->driver_data))
7228 bp->flags |= BNXT_FLAG_VF;
7229
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007230 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04007231 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04007232
7233 rc = bnxt_init_board(pdev, dev);
7234 if (rc < 0)
7235 goto init_err_free;
7236
7237 dev->netdev_ops = &bnxt_netdev_ops;
7238 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7239 dev->ethtool_ops = &bnxt_ethtool_ops;
7240
7241 pci_set_drvdata(pdev, dev);
7242
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007243 rc = bnxt_alloc_hwrm_resources(bp);
7244 if (rc)
7245 goto init_err;
7246
7247 mutex_init(&bp->hwrm_cmd_lock);
7248 rc = bnxt_hwrm_ver_get(bp);
7249 if (rc)
7250 goto init_err;
7251
Rob Swindell5ac67d82016-09-19 03:58:03 -04007252 bnxt_hwrm_fw_set_time(bp);
7253
Michael Chanc0c050c2015-10-22 16:01:17 -04007254 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7255 NETIF_F_TSO | NETIF_F_TSO6 |
7256 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007257 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007258 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7259 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007260 NETIF_F_RXCSUM | NETIF_F_GRO;
7261
7262 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7263 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007264
Michael Chanc0c050c2015-10-22 16:01:17 -04007265 dev->hw_enc_features =
7266 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7267 NETIF_F_TSO | NETIF_F_TSO6 |
7268 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007269 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007270 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007271 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7272 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007273 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7274 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7275 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7276 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7277 dev->priv_flags |= IFF_UNICAST_FLT;
7278
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007279 /* MTU range: 60 - 9500 */
7280 dev->min_mtu = ETH_ZLEN;
7281 dev->max_mtu = 9500;
7282
Michael Chan7df4ae92016-12-02 21:17:17 -05007283 bnxt_dcb_init(bp);
7284
Michael Chanc0c050c2015-10-22 16:01:17 -04007285#ifdef CONFIG_BNXT_SRIOV
7286 init_waitqueue_head(&bp->sriov_cfg_wait);
7287#endif
Michael Chan309369c2016-06-13 02:25:34 -04007288 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007289 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7290 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007291
Michael Chanc0c050c2015-10-22 16:01:17 -04007292 rc = bnxt_hwrm_func_drv_rgtr(bp);
7293 if (rc)
7294 goto init_err;
7295
Michael Chana1653b12016-12-07 00:26:20 -05007296 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7297 if (rc)
7298 goto init_err;
7299
Michael Chana588e452016-12-07 00:26:21 -05007300 bp->ulp_probe = bnxt_ulp_probe;
7301
Michael Chanc0c050c2015-10-22 16:01:17 -04007302 /* Get the MAX capabilities for this function */
7303 rc = bnxt_hwrm_func_qcaps(bp);
7304 if (rc) {
7305 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7306 rc);
7307 rc = -1;
7308 goto init_err;
7309 }
7310
7311 rc = bnxt_hwrm_queue_qportcfg(bp);
7312 if (rc) {
7313 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7314 rc);
7315 rc = -1;
7316 goto init_err;
7317 }
7318
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007319 bnxt_hwrm_func_qcfg(bp);
Michael Chan5ad2cbe2017-01-13 01:32:03 -05007320 bnxt_hwrm_port_led_qcaps(bp);
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007321
Michael Chan6bb19472017-02-06 16:55:32 -05007322 bnxt_set_rx_skb_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007323 bnxt_set_tpa_flags(bp);
7324 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007325 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chanbdbd1eb2016-12-29 12:13:43 -05007326 rc = bnxt_set_dflt_rings(bp);
7327 if (rc) {
7328 netdev_err(bp->dev, "Not enough rings available.\n");
7329 rc = -ENOMEM;
7330 goto init_err;
7331 }
Michael Chanc0c050c2015-10-22 16:01:17 -04007332
Michael Chan87da7f72016-11-16 21:13:09 -05007333 /* Default RSS hash cfg. */
7334 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7335 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7336 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7337 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7338 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7339 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7340 bp->hwrm_spec_code >= 0x10501) {
7341 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7342 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7343 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7344 }
7345
Michael Chan8fdefd62016-12-29 12:13:36 -05007346 bnxt_hwrm_vnic_qcaps(bp);
Michael Chan8079e8f2016-12-29 12:13:37 -05007347 if (bnxt_rfs_supported(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007348 dev->hw_features |= NETIF_F_NTUPLE;
7349 if (bnxt_rfs_capable(bp)) {
7350 bp->flags |= BNXT_FLAG_RFS;
7351 dev->features |= NETIF_F_NTUPLE;
7352 }
7353 }
7354
Michael Chanc0c050c2015-10-22 16:01:17 -04007355 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7356 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7357
7358 rc = bnxt_probe_phy(bp);
7359 if (rc)
7360 goto init_err;
7361
Michael Chanaa8ed022016-12-07 00:26:17 -05007362 rc = bnxt_hwrm_func_reset(bp);
7363 if (rc)
7364 goto init_err;
7365
Michael Chan78095922016-12-07 00:26:16 -05007366 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007367 if (rc)
7368 goto init_err;
7369
Michael Chan78095922016-12-07 00:26:16 -05007370 rc = register_netdev(dev);
7371 if (rc)
7372 goto init_err_clr_int;
7373
Michael Chanc0c050c2015-10-22 16:01:17 -04007374 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7375 board_info[ent->driver_data].name,
7376 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7377
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007378 bnxt_parse_log_pcie_link(bp);
7379
Michael Chanc0c050c2015-10-22 16:01:17 -04007380 return 0;
7381
Michael Chan78095922016-12-07 00:26:16 -05007382init_err_clr_int:
7383 bnxt_clear_int_mode(bp);
7384
Michael Chanc0c050c2015-10-22 16:01:17 -04007385init_err:
7386 pci_iounmap(pdev, bp->bar0);
7387 pci_release_regions(pdev);
7388 pci_disable_device(pdev);
7389
7390init_err_free:
7391 free_netdev(dev);
7392 return rc;
7393}
7394
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007395/**
7396 * bnxt_io_error_detected - called when PCI error is detected
7397 * @pdev: Pointer to PCI device
7398 * @state: The current pci connection state
7399 *
7400 * This function is called after a PCI bus error affecting
7401 * this device has been detected.
7402 */
7403static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7404 pci_channel_state_t state)
7405{
7406 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007407 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007408
7409 netdev_info(netdev, "PCI I/O error detected\n");
7410
7411 rtnl_lock();
7412 netif_device_detach(netdev);
7413
Michael Chana588e452016-12-07 00:26:21 -05007414 bnxt_ulp_stop(bp);
7415
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007416 if (state == pci_channel_io_perm_failure) {
7417 rtnl_unlock();
7418 return PCI_ERS_RESULT_DISCONNECT;
7419 }
7420
7421 if (netif_running(netdev))
7422 bnxt_close(netdev);
7423
7424 pci_disable_device(pdev);
7425 rtnl_unlock();
7426
7427 /* Request a slot slot reset. */
7428 return PCI_ERS_RESULT_NEED_RESET;
7429}
7430
7431/**
7432 * bnxt_io_slot_reset - called after the pci bus has been reset.
7433 * @pdev: Pointer to PCI device
7434 *
7435 * Restart the card from scratch, as if from a cold-boot.
7436 * At this point, the card has exprienced a hard reset,
7437 * followed by fixups by BIOS, and has its config space
7438 * set up identically to what it was at cold boot.
7439 */
7440static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7441{
7442 struct net_device *netdev = pci_get_drvdata(pdev);
7443 struct bnxt *bp = netdev_priv(netdev);
7444 int err = 0;
7445 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7446
7447 netdev_info(bp->dev, "PCI Slot Reset\n");
7448
7449 rtnl_lock();
7450
7451 if (pci_enable_device(pdev)) {
7452 dev_err(&pdev->dev,
7453 "Cannot re-enable PCI device after reset.\n");
7454 } else {
7455 pci_set_master(pdev);
7456
Michael Chanaa8ed022016-12-07 00:26:17 -05007457 err = bnxt_hwrm_func_reset(bp);
7458 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007459 err = bnxt_open(netdev);
7460
Michael Chana588e452016-12-07 00:26:21 -05007461 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007462 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007463 bnxt_ulp_start(bp);
7464 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007465 }
7466
7467 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7468 dev_close(netdev);
7469
7470 rtnl_unlock();
7471
7472 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7473 if (err) {
7474 dev_err(&pdev->dev,
7475 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7476 err); /* non-fatal, continue */
7477 }
7478
7479 return PCI_ERS_RESULT_RECOVERED;
7480}
7481
7482/**
7483 * bnxt_io_resume - called when traffic can start flowing again.
7484 * @pdev: Pointer to PCI device
7485 *
7486 * This callback is called when the error recovery driver tells
7487 * us that its OK to resume normal operation.
7488 */
7489static void bnxt_io_resume(struct pci_dev *pdev)
7490{
7491 struct net_device *netdev = pci_get_drvdata(pdev);
7492
7493 rtnl_lock();
7494
7495 netif_device_attach(netdev);
7496
7497 rtnl_unlock();
7498}
7499
7500static const struct pci_error_handlers bnxt_err_handler = {
7501 .error_detected = bnxt_io_error_detected,
7502 .slot_reset = bnxt_io_slot_reset,
7503 .resume = bnxt_io_resume
7504};
7505
Michael Chanc0c050c2015-10-22 16:01:17 -04007506static struct pci_driver bnxt_pci_driver = {
7507 .name = DRV_MODULE_NAME,
7508 .id_table = bnxt_pci_tbl,
7509 .probe = bnxt_init_one,
7510 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007511 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007512#if defined(CONFIG_BNXT_SRIOV)
7513 .sriov_configure = bnxt_sriov_configure,
7514#endif
7515};
7516
7517module_pci_driver(bnxt_pci_driver);