blob: 916832684ad56c5b2ff18f8fa6c691b072cbf9e5 [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanc0c050c2015-10-22 16:01:17 -04004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
Rob Swindell5ac67d82016-09-19 03:58:03 -040035#include <linux/rtc.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040036#include <net/ip.h>
37#include <net/tcp.h>
38#include <net/udp.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070041#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040042#include <linux/workqueue.h>
43#include <linux/prefetch.h>
44#include <linux/cache.h>
45#include <linux/log2.h>
46#include <linux/aer.h>
47#include <linux/bitmap.h>
48#include <linux/cpu_rmap.h>
49
50#include "bnxt_hsi.h"
51#include "bnxt.h"
Michael Chana588e452016-12-07 00:26:21 -050052#include "bnxt_ulp.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040053#include "bnxt_sriov.h"
54#include "bnxt_ethtool.h"
Michael Chan7df4ae92016-12-02 21:17:17 -050055#include "bnxt_dcb.h"
Michael Chanc0c050c2015-10-22 16:01:17 -040056
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040076 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040077 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040078 BCM57311,
79 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050080 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040081 BCM57404,
82 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040083 BCM57402_NPAR,
84 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040085 BCM57412,
86 BCM57414,
87 BCM57416,
88 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040089 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040090 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57417_SFP,
92 BCM57416_SFP,
93 BCM57404_NPAR,
94 BCM57406_NPAR,
95 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040096 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040097 BCM57414_NPAR,
98 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -040099 NETXTREME_E_VF,
100 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400101};
102
103/* indexed by enum above */
104static const struct {
105 char *name;
106} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400107 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
108 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400110 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400111 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
112 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
115 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
119 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400127 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
128 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
130 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400133 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135};
136
137static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400138 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500139 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
141 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400142 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400144 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
145 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
148 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
152 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
153 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
154 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400155 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400156 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
158 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
160 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400162 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400168#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
170 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400175#endif
176 { 0 }
177};
178
179MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
180
181static const u16 bnxt_vf_req_snif[] = {
182 HWRM_FUNC_CFG,
183 HWRM_PORT_PHY_QCFG,
184 HWRM_CFA_L2_FILTER_ALLOC,
185};
186
Michael Chan25be8622016-04-05 14:09:00 -0400187static const u16 bnxt_async_events_arr[] = {
Michael Chan87c374d2016-12-02 21:17:16 -0500188 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
189 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
190 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
191 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
192 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400193};
194
Michael Chanc0c050c2015-10-22 16:01:17 -0400195static bool bnxt_vf_pciid(enum board_idx idx)
196{
Michael Chanadbc8302016-09-19 03:58:01 -0400197 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400198}
199
200#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
201#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
202#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
203
204#define BNXT_CP_DB_REARM(db, raw_cons) \
205 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
206
207#define BNXT_CP_DB(db, raw_cons) \
208 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
209
210#define BNXT_CP_DB_IRQ_DIS(db) \
211 writel(DB_CP_IRQ_DIS_FLAGS, db)
212
213static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
214{
215 /* Tell compiler to fetch tx indices from memory. */
216 barrier();
217
218 return bp->tx_ring_size -
219 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
220}
221
222static const u16 bnxt_lhint_arr[] = {
223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanb6ab4b02016-01-02 23:44:59 -0500265 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400266 txq = netdev_get_tx_queue(dev, i);
267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500512 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
514 u16 cons = txr->tx_cons;
515 struct pci_dev *pdev = bp->pdev;
516 int i;
517 unsigned int tx_bytes = 0;
518
519 for (i = 0; i < nr_pkts; i++) {
520 struct bnxt_sw_tx_bd *tx_buf;
521 struct sk_buff *skb;
522 int j, last;
523
524 tx_buf = &txr->tx_buf_ring[cons];
525 cons = NEXT_TX(cons);
526 skb = tx_buf->skb;
527 tx_buf->skb = NULL;
528
529 if (tx_buf->is_push) {
530 tx_buf->is_push = 0;
531 goto next_tx_int;
532 }
533
534 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
535 skb_headlen(skb), PCI_DMA_TODEVICE);
536 last = tx_buf->nr_frags;
537
538 for (j = 0; j < last; j++) {
539 cons = NEXT_TX(cons);
540 tx_buf = &txr->tx_buf_ring[cons];
541 dma_unmap_page(
542 &pdev->dev,
543 dma_unmap_addr(tx_buf, mapping),
544 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 PCI_DMA_TODEVICE);
546 }
547
548next_tx_int:
549 cons = NEXT_TX(cons);
550
551 tx_bytes += skb->len;
552 dev_kfree_skb_any(skb);
553 }
554
555 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
556 txr->tx_cons = cons;
557
558 /* Need to make the tx_cons update visible to bnxt_start_xmit()
559 * before checking for netif_tx_queue_stopped(). Without the
560 * memory barrier, there is a small possibility that bnxt_start_xmit()
561 * will miss it and cause the queue to be stopped forever.
562 */
563 smp_mb();
564
565 if (unlikely(netif_tx_queue_stopped(txq)) &&
566 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
567 __netif_tx_lock(txq, smp_processor_id());
568 if (netif_tx_queue_stopped(txq) &&
569 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
570 txr->dev_state != BNXT_DEV_STATE_CLOSING)
571 netif_tx_wake_queue(txq);
572 __netif_tx_unlock(txq);
573 }
574}
575
576static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 u8 *data;
580 struct pci_dev *pdev = bp->pdev;
581
582 data = kmalloc(bp->rx_buf_size, gfp);
583 if (!data)
584 return NULL;
585
586 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
587 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
588
589 if (dma_mapping_error(&pdev->dev, *mapping)) {
590 kfree(data);
591 data = NULL;
592 }
593 return data;
594}
595
596static inline int bnxt_alloc_rx_data(struct bnxt *bp,
597 struct bnxt_rx_ring_info *rxr,
598 u16 prod, gfp_t gfp)
599{
600 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
601 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
602 u8 *data;
603 dma_addr_t mapping;
604
605 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 if (!data)
607 return -ENOMEM;
608
609 rx_buf->data = data;
610 dma_unmap_addr_set(rx_buf, mapping, mapping);
611
612 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
613
614 return 0;
615}
616
617static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
618 u8 *data)
619{
620 u16 prod = rxr->rx_prod;
621 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
622 struct rx_bd *cons_bd, *prod_bd;
623
624 prod_rx_buf = &rxr->rx_buf_ring[prod];
625 cons_rx_buf = &rxr->rx_buf_ring[cons];
626
627 prod_rx_buf->data = data;
628
629 dma_unmap_addr_set(prod_rx_buf, mapping,
630 dma_unmap_addr(cons_rx_buf, mapping));
631
632 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
633 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
634
635 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
636}
637
638static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
639{
640 u16 next, max = rxr->rx_agg_bmap_size;
641
642 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
643 if (next >= max)
644 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
645 return next;
646}
647
648static inline int bnxt_alloc_rx_page(struct bnxt *bp,
649 struct bnxt_rx_ring_info *rxr,
650 u16 prod, gfp_t gfp)
651{
652 struct rx_bd *rxbd =
653 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
654 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
655 struct pci_dev *pdev = bp->pdev;
656 struct page *page;
657 dma_addr_t mapping;
658 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400659 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400660
Michael Chan89d0a062016-04-25 02:30:51 -0400661 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
662 page = rxr->rx_page;
663 if (!page) {
664 page = alloc_page(gfp);
665 if (!page)
666 return -ENOMEM;
667 rxr->rx_page = page;
668 rxr->rx_page_offset = 0;
669 }
670 offset = rxr->rx_page_offset;
671 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
672 if (rxr->rx_page_offset == PAGE_SIZE)
673 rxr->rx_page = NULL;
674 else
675 get_page(page);
676 } else {
677 page = alloc_page(gfp);
678 if (!page)
679 return -ENOMEM;
680 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400681
Michael Chan89d0a062016-04-25 02:30:51 -0400682 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400683 PCI_DMA_FROMDEVICE);
684 if (dma_mapping_error(&pdev->dev, mapping)) {
685 __free_page(page);
686 return -EIO;
687 }
688
689 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
690 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
691
692 __set_bit(sw_prod, rxr->rx_agg_bmap);
693 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
694 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
695
696 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400697 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400698 rx_agg_buf->mapping = mapping;
699 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
700 rxbd->rx_bd_opaque = sw_prod;
701 return 0;
702}
703
704static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
705 u32 agg_bufs)
706{
707 struct bnxt *bp = bnapi->bp;
708 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500709 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400710 u16 prod = rxr->rx_agg_prod;
711 u16 sw_prod = rxr->rx_sw_agg_prod;
712 u32 i;
713
714 for (i = 0; i < agg_bufs; i++) {
715 u16 cons;
716 struct rx_agg_cmp *agg;
717 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
718 struct rx_bd *prod_bd;
719 struct page *page;
720
721 agg = (struct rx_agg_cmp *)
722 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
723 cons = agg->rx_agg_cmp_opaque;
724 __clear_bit(cons, rxr->rx_agg_bmap);
725
726 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
727 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
728
729 __set_bit(sw_prod, rxr->rx_agg_bmap);
730 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
731 cons_rx_buf = &rxr->rx_agg_ring[cons];
732
733 /* It is possible for sw_prod to be equal to cons, so
734 * set cons_rx_buf->page to NULL first.
735 */
736 page = cons_rx_buf->page;
737 cons_rx_buf->page = NULL;
738 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400739 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400740
741 prod_rx_buf->mapping = cons_rx_buf->mapping;
742
743 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
744
745 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
746 prod_bd->rx_bd_opaque = sw_prod;
747
748 prod = NEXT_RX_AGG(prod);
749 sw_prod = NEXT_RX_AGG(sw_prod);
750 cp_cons = NEXT_CMP(cp_cons);
751 }
752 rxr->rx_agg_prod = prod;
753 rxr->rx_sw_agg_prod = sw_prod;
754}
755
756static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
757 struct bnxt_rx_ring_info *rxr, u16 cons,
758 u16 prod, u8 *data, dma_addr_t dma_addr,
759 unsigned int len)
760{
761 int err;
762 struct sk_buff *skb;
763
764 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
765 if (unlikely(err)) {
766 bnxt_reuse_rx_data(rxr, cons, data);
767 return NULL;
768 }
769
770 skb = build_skb(data, 0);
771 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
772 PCI_DMA_FROMDEVICE);
773 if (!skb) {
774 kfree(data);
775 return NULL;
776 }
777
778 skb_reserve(skb, BNXT_RX_OFFSET);
779 skb_put(skb, len);
780 return skb;
781}
782
783static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
784 struct sk_buff *skb, u16 cp_cons,
785 u32 agg_bufs)
786{
787 struct pci_dev *pdev = bp->pdev;
788 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500789 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400790 u16 prod = rxr->rx_agg_prod;
791 u32 i;
792
793 for (i = 0; i < agg_bufs; i++) {
794 u16 cons, frag_len;
795 struct rx_agg_cmp *agg;
796 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
797 struct page *page;
798 dma_addr_t mapping;
799
800 agg = (struct rx_agg_cmp *)
801 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
802 cons = agg->rx_agg_cmp_opaque;
803 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
804 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
805
806 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400807 skb_fill_page_desc(skb, i, cons_rx_buf->page,
808 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400809 __clear_bit(cons, rxr->rx_agg_bmap);
810
811 /* It is possible for bnxt_alloc_rx_page() to allocate
812 * a sw_prod index that equals the cons index, so we
813 * need to clear the cons entry now.
814 */
815 mapping = dma_unmap_addr(cons_rx_buf, mapping);
816 page = cons_rx_buf->page;
817 cons_rx_buf->page = NULL;
818
819 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
820 struct skb_shared_info *shinfo;
821 unsigned int nr_frags;
822
823 shinfo = skb_shinfo(skb);
824 nr_frags = --shinfo->nr_frags;
825 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
826
827 dev_kfree_skb(skb);
828
829 cons_rx_buf->page = page;
830
831 /* Update prod since possibly some pages have been
832 * allocated already.
833 */
834 rxr->rx_agg_prod = prod;
835 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
836 return NULL;
837 }
838
Michael Chan2839f282016-04-25 02:30:50 -0400839 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400840 PCI_DMA_FROMDEVICE);
841
842 skb->data_len += frag_len;
843 skb->len += frag_len;
844 skb->truesize += PAGE_SIZE;
845
846 prod = NEXT_RX_AGG(prod);
847 cp_cons = NEXT_CMP(cp_cons);
848 }
849 rxr->rx_agg_prod = prod;
850 return skb;
851}
852
853static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
854 u8 agg_bufs, u32 *raw_cons)
855{
856 u16 last;
857 struct rx_agg_cmp *agg;
858
859 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
860 last = RING_CMP(*raw_cons);
861 agg = (struct rx_agg_cmp *)
862 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
863 return RX_AGG_CMP_VALID(agg, *raw_cons);
864}
865
866static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
867 unsigned int len,
868 dma_addr_t mapping)
869{
870 struct bnxt *bp = bnapi->bp;
871 struct pci_dev *pdev = bp->pdev;
872 struct sk_buff *skb;
873
874 skb = napi_alloc_skb(&bnapi->napi, len);
875 if (!skb)
876 return NULL;
877
878 dma_sync_single_for_cpu(&pdev->dev, mapping,
879 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
880
881 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
882
883 dma_sync_single_for_device(&pdev->dev, mapping,
884 bp->rx_copy_thresh,
885 PCI_DMA_FROMDEVICE);
886
887 skb_put(skb, len);
888 return skb;
889}
890
Michael Chanfa7e2812016-05-10 19:18:00 -0400891static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
892 u32 *raw_cons, void *cmp)
893{
894 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
895 struct rx_cmp *rxcmp = cmp;
896 u32 tmp_raw_cons = *raw_cons;
897 u8 cmp_type, agg_bufs = 0;
898
899 cmp_type = RX_CMP_TYPE(rxcmp);
900
901 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
902 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
903 RX_CMP_AGG_BUFS) >>
904 RX_CMP_AGG_BUFS_SHIFT;
905 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
906 struct rx_tpa_end_cmp *tpa_end = cmp;
907
908 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
909 RX_TPA_END_CMP_AGG_BUFS) >>
910 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
911 }
912
913 if (agg_bufs) {
914 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
915 return -EBUSY;
916 }
917 *raw_cons = tmp_raw_cons;
918 return 0;
919}
920
921static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
922{
923 if (!rxr->bnapi->in_reset) {
924 rxr->bnapi->in_reset = true;
925 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
926 schedule_work(&bp->sp_task);
927 }
928 rxr->rx_next_cons = 0xffff;
929}
930
Michael Chanc0c050c2015-10-22 16:01:17 -0400931static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
932 struct rx_tpa_start_cmp *tpa_start,
933 struct rx_tpa_start_cmp_ext *tpa_start1)
934{
935 u8 agg_id = TPA_START_AGG_ID(tpa_start);
936 u16 cons, prod;
937 struct bnxt_tpa_info *tpa_info;
938 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
939 struct rx_bd *prod_bd;
940 dma_addr_t mapping;
941
942 cons = tpa_start->rx_tpa_start_cmp_opaque;
943 prod = rxr->rx_prod;
944 cons_rx_buf = &rxr->rx_buf_ring[cons];
945 prod_rx_buf = &rxr->rx_buf_ring[prod];
946 tpa_info = &rxr->rx_tpa[agg_id];
947
Michael Chanfa7e2812016-05-10 19:18:00 -0400948 if (unlikely(cons != rxr->rx_next_cons)) {
949 bnxt_sched_reset(bp, rxr);
950 return;
951 }
952
Michael Chanc0c050c2015-10-22 16:01:17 -0400953 prod_rx_buf->data = tpa_info->data;
954
955 mapping = tpa_info->mapping;
956 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
957
958 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
959
960 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
961
962 tpa_info->data = cons_rx_buf->data;
963 cons_rx_buf->data = NULL;
964 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
965
966 tpa_info->len =
967 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
968 RX_TPA_START_CMP_LEN_SHIFT;
969 if (likely(TPA_START_HASH_VALID(tpa_start))) {
970 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
971
972 tpa_info->hash_type = PKT_HASH_TYPE_L4;
973 tpa_info->gso_type = SKB_GSO_TCPV4;
974 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
975 if (hash_type == 3)
976 tpa_info->gso_type = SKB_GSO_TCPV6;
977 tpa_info->rss_hash =
978 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
979 } else {
980 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
981 tpa_info->gso_type = 0;
982 if (netif_msg_rx_err(bp))
983 netdev_warn(bp->dev, "TPA packet without valid hash\n");
984 }
985 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
986 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400987 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400988
989 rxr->rx_prod = NEXT_RX(prod);
990 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400991 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400992 cons_rx_buf = &rxr->rx_buf_ring[cons];
993
994 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
995 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
996 cons_rx_buf->data = NULL;
997}
998
999static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1000 u16 cp_cons, u32 agg_bufs)
1001{
1002 if (agg_bufs)
1003 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1004}
1005
Michael Chan94758f82016-06-13 02:25:35 -04001006static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1007 int payload_off, int tcp_ts,
1008 struct sk_buff *skb)
1009{
1010#ifdef CONFIG_INET
1011 struct tcphdr *th;
1012 int len, nw_off;
1013 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1014 u32 hdr_info = tpa_info->hdr_info;
1015 bool loopback = false;
1016
1017 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1018 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1019 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1020
1021 /* If the packet is an internal loopback packet, the offsets will
1022 * have an extra 4 bytes.
1023 */
1024 if (inner_mac_off == 4) {
1025 loopback = true;
1026 } else if (inner_mac_off > 4) {
1027 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1028 ETH_HLEN - 2));
1029
1030 /* We only support inner iPv4/ipv6. If we don't see the
1031 * correct protocol ID, it must be a loopback packet where
1032 * the offsets are off by 4.
1033 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001034 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001035 loopback = true;
1036 }
1037 if (loopback) {
1038 /* internal loopback packet, subtract all offsets by 4 */
1039 inner_ip_off -= 4;
1040 inner_mac_off -= 4;
1041 outer_ip_off -= 4;
1042 }
1043
1044 nw_off = inner_ip_off - ETH_HLEN;
1045 skb_set_network_header(skb, nw_off);
1046 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1047 struct ipv6hdr *iph = ipv6_hdr(skb);
1048
1049 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1050 len = skb->len - skb_transport_offset(skb);
1051 th = tcp_hdr(skb);
1052 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1053 } else {
1054 struct iphdr *iph = ip_hdr(skb);
1055
1056 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1057 len = skb->len - skb_transport_offset(skb);
1058 th = tcp_hdr(skb);
1059 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1060 }
1061
1062 if (inner_mac_off) { /* tunnel */
1063 struct udphdr *uh = NULL;
1064 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1065 ETH_HLEN - 2));
1066
1067 if (proto == htons(ETH_P_IP)) {
1068 struct iphdr *iph = (struct iphdr *)skb->data;
1069
1070 if (iph->protocol == IPPROTO_UDP)
1071 uh = (struct udphdr *)(iph + 1);
1072 } else {
1073 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1074
1075 if (iph->nexthdr == IPPROTO_UDP)
1076 uh = (struct udphdr *)(iph + 1);
1077 }
1078 if (uh) {
1079 if (uh->check)
1080 skb_shinfo(skb)->gso_type |=
1081 SKB_GSO_UDP_TUNNEL_CSUM;
1082 else
1083 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1084 }
1085 }
1086#endif
1087 return skb;
1088}
1089
Michael Chanc0c050c2015-10-22 16:01:17 -04001090#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1091#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1092
Michael Chan309369c2016-06-13 02:25:34 -04001093static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1094 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001095 struct sk_buff *skb)
1096{
Michael Chand1611c32015-10-25 22:27:57 -04001097#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001098 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001099 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001100
Michael Chan309369c2016-06-13 02:25:34 -04001101 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001102 tcp_opt_len = 12;
1103
Michael Chanc0c050c2015-10-22 16:01:17 -04001104 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1105 struct iphdr *iph;
1106
1107 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1108 ETH_HLEN;
1109 skb_set_network_header(skb, nw_off);
1110 iph = ip_hdr(skb);
1111 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1112 len = skb->len - skb_transport_offset(skb);
1113 th = tcp_hdr(skb);
1114 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1115 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1116 struct ipv6hdr *iph;
1117
1118 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1119 ETH_HLEN;
1120 skb_set_network_header(skb, nw_off);
1121 iph = ipv6_hdr(skb);
1122 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1123 len = skb->len - skb_transport_offset(skb);
1124 th = tcp_hdr(skb);
1125 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1126 } else {
1127 dev_kfree_skb_any(skb);
1128 return NULL;
1129 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001130
1131 if (nw_off) { /* tunnel */
1132 struct udphdr *uh = NULL;
1133
1134 if (skb->protocol == htons(ETH_P_IP)) {
1135 struct iphdr *iph = (struct iphdr *)skb->data;
1136
1137 if (iph->protocol == IPPROTO_UDP)
1138 uh = (struct udphdr *)(iph + 1);
1139 } else {
1140 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1141
1142 if (iph->nexthdr == IPPROTO_UDP)
1143 uh = (struct udphdr *)(iph + 1);
1144 }
1145 if (uh) {
1146 if (uh->check)
1147 skb_shinfo(skb)->gso_type |=
1148 SKB_GSO_UDP_TUNNEL_CSUM;
1149 else
1150 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1151 }
1152 }
1153#endif
1154 return skb;
1155}
1156
Michael Chan309369c2016-06-13 02:25:34 -04001157static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1158 struct bnxt_tpa_info *tpa_info,
1159 struct rx_tpa_end_cmp *tpa_end,
1160 struct rx_tpa_end_cmp_ext *tpa_end1,
1161 struct sk_buff *skb)
1162{
1163#ifdef CONFIG_INET
1164 int payload_off;
1165 u16 segs;
1166
1167 segs = TPA_END_TPA_SEGS(tpa_end);
1168 if (segs == 1)
1169 return skb;
1170
1171 NAPI_GRO_CB(skb)->count = segs;
1172 skb_shinfo(skb)->gso_size =
1173 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1174 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1175 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1176 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1177 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1178 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
Michael Chan59109062016-12-29 12:13:35 -05001179 if (likely(skb))
1180 tcp_gro_complete(skb);
Michael Chan309369c2016-06-13 02:25:34 -04001181#endif
1182 return skb;
1183}
1184
Michael Chanc0c050c2015-10-22 16:01:17 -04001185static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1186 struct bnxt_napi *bnapi,
1187 u32 *raw_cons,
1188 struct rx_tpa_end_cmp *tpa_end,
1189 struct rx_tpa_end_cmp_ext *tpa_end1,
1190 bool *agg_event)
1191{
1192 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001193 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001194 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1195 u8 *data, agg_bufs;
1196 u16 cp_cons = RING_CMP(*raw_cons);
1197 unsigned int len;
1198 struct bnxt_tpa_info *tpa_info;
1199 dma_addr_t mapping;
1200 struct sk_buff *skb;
1201
Michael Chanfa7e2812016-05-10 19:18:00 -04001202 if (unlikely(bnapi->in_reset)) {
1203 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1204
1205 if (rc < 0)
1206 return ERR_PTR(-EBUSY);
1207 return NULL;
1208 }
1209
Michael Chanc0c050c2015-10-22 16:01:17 -04001210 tpa_info = &rxr->rx_tpa[agg_id];
1211 data = tpa_info->data;
1212 prefetch(data);
1213 len = tpa_info->len;
1214 mapping = tpa_info->mapping;
1215
1216 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1217 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1218
1219 if (agg_bufs) {
1220 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1221 return ERR_PTR(-EBUSY);
1222
1223 *agg_event = true;
1224 cp_cons = NEXT_CMP(cp_cons);
1225 }
1226
1227 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1228 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1229 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1230 agg_bufs, (int)MAX_SKB_FRAGS);
1231 return NULL;
1232 }
1233
1234 if (len <= bp->rx_copy_thresh) {
1235 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1236 if (!skb) {
1237 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1238 return NULL;
1239 }
1240 } else {
1241 u8 *new_data;
1242 dma_addr_t new_mapping;
1243
1244 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1245 if (!new_data) {
1246 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1247 return NULL;
1248 }
1249
1250 tpa_info->data = new_data;
1251 tpa_info->mapping = new_mapping;
1252
1253 skb = build_skb(data, 0);
1254 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1255 PCI_DMA_FROMDEVICE);
1256
1257 if (!skb) {
1258 kfree(data);
1259 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1260 return NULL;
1261 }
1262 skb_reserve(skb, BNXT_RX_OFFSET);
1263 skb_put(skb, len);
1264 }
1265
1266 if (agg_bufs) {
1267 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1268 if (!skb) {
1269 /* Page reuse already handled by bnxt_rx_pages(). */
1270 return NULL;
1271 }
1272 }
1273 skb->protocol = eth_type_trans(skb, bp->dev);
1274
1275 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1276 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1277
Michael Chan8852ddb2016-06-06 02:37:16 -04001278 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1279 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001280 u16 vlan_proto = tpa_info->metadata >>
1281 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001282 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001283
Michael Chan8852ddb2016-06-06 02:37:16 -04001284 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001285 }
1286
1287 skb_checksum_none_assert(skb);
1288 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1289 skb->ip_summed = CHECKSUM_UNNECESSARY;
1290 skb->csum_level =
1291 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1292 }
1293
1294 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001295 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001296
1297 return skb;
1298}
1299
1300/* returns the following:
1301 * 1 - 1 packet successfully received
1302 * 0 - successful TPA_START, packet not completed yet
1303 * -EBUSY - completion ring does not have all the agg buffers yet
1304 * -ENOMEM - packet aborted due to out of memory
1305 * -EIO - packet aborted due to hw error indicated in BD
1306 */
1307static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1308 bool *agg_event)
1309{
1310 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001311 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001312 struct net_device *dev = bp->dev;
1313 struct rx_cmp *rxcmp;
1314 struct rx_cmp_ext *rxcmp1;
1315 u32 tmp_raw_cons = *raw_cons;
1316 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1317 struct bnxt_sw_rx_bd *rx_buf;
1318 unsigned int len;
1319 u8 *data, agg_bufs, cmp_type;
1320 dma_addr_t dma_addr;
1321 struct sk_buff *skb;
1322 int rc = 0;
1323
1324 rxcmp = (struct rx_cmp *)
1325 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1326
1327 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1328 cp_cons = RING_CMP(tmp_raw_cons);
1329 rxcmp1 = (struct rx_cmp_ext *)
1330 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1331
1332 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1333 return -EBUSY;
1334
1335 cmp_type = RX_CMP_TYPE(rxcmp);
1336
1337 prod = rxr->rx_prod;
1338
1339 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1340 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1341 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1342
1343 goto next_rx_no_prod;
1344
1345 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1346 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1347 (struct rx_tpa_end_cmp *)rxcmp,
1348 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1349 agg_event);
1350
1351 if (unlikely(IS_ERR(skb)))
1352 return -EBUSY;
1353
1354 rc = -ENOMEM;
1355 if (likely(skb)) {
1356 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001357 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001358 rc = 1;
1359 }
1360 goto next_rx_no_prod;
1361 }
1362
1363 cons = rxcmp->rx_cmp_opaque;
1364 rx_buf = &rxr->rx_buf_ring[cons];
1365 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001366 if (unlikely(cons != rxr->rx_next_cons)) {
1367 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1368
1369 bnxt_sched_reset(bp, rxr);
1370 return rc1;
1371 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001372 prefetch(data);
1373
1374 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1375 RX_CMP_AGG_BUFS_SHIFT;
1376
1377 if (agg_bufs) {
1378 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1379 return -EBUSY;
1380
1381 cp_cons = NEXT_CMP(cp_cons);
1382 *agg_event = true;
1383 }
1384
1385 rx_buf->data = NULL;
1386 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1387 bnxt_reuse_rx_data(rxr, cons, data);
1388 if (agg_bufs)
1389 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1390
1391 rc = -EIO;
1392 goto next_rx;
1393 }
1394
1395 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1396 dma_addr = dma_unmap_addr(rx_buf, mapping);
1397
1398 if (len <= bp->rx_copy_thresh) {
1399 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1400 bnxt_reuse_rx_data(rxr, cons, data);
1401 if (!skb) {
1402 rc = -ENOMEM;
1403 goto next_rx;
1404 }
1405 } else {
1406 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1407 if (!skb) {
1408 rc = -ENOMEM;
1409 goto next_rx;
1410 }
1411 }
1412
1413 if (agg_bufs) {
1414 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1415 if (!skb) {
1416 rc = -ENOMEM;
1417 goto next_rx;
1418 }
1419 }
1420
1421 if (RX_CMP_HASH_VALID(rxcmp)) {
1422 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1423 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1424
1425 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1426 if (hash_type != 1 && hash_type != 3)
1427 type = PKT_HASH_TYPE_L3;
1428 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1429 }
1430
1431 skb->protocol = eth_type_trans(skb, dev);
1432
Michael Chan8852ddb2016-06-06 02:37:16 -04001433 if ((rxcmp1->rx_cmp_flags2 &
1434 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1435 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001436 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001437 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001438 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1439
Michael Chan8852ddb2016-06-06 02:37:16 -04001440 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001441 }
1442
1443 skb_checksum_none_assert(skb);
1444 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1445 if (dev->features & NETIF_F_RXCSUM) {
1446 skb->ip_summed = CHECKSUM_UNNECESSARY;
1447 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1448 }
1449 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001450 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1451 if (dev->features & NETIF_F_RXCSUM)
1452 cpr->rx_l4_csum_errors++;
1453 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001454 }
1455
1456 skb_record_rx_queue(skb, bnapi->index);
Michael Chanb356a2e2016-12-29 12:13:31 -05001457 napi_gro_receive(&bnapi->napi, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001458 rc = 1;
1459
1460next_rx:
1461 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001462 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001463
1464next_rx_no_prod:
1465 *raw_cons = tmp_raw_cons;
1466
1467 return rc;
1468}
1469
Michael Chan4bb13ab2016-04-05 14:09:01 -04001470#define BNXT_GET_EVENT_PORT(data) \
Michael Chan87c374d2016-12-02 21:17:16 -05001471 ((data) & \
1472 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
Michael Chan4bb13ab2016-04-05 14:09:01 -04001473
Michael Chanc0c050c2015-10-22 16:01:17 -04001474static int bnxt_async_event_process(struct bnxt *bp,
1475 struct hwrm_async_event_cmpl *cmpl)
1476{
1477 u16 event_id = le16_to_cpu(cmpl->event_id);
1478
1479 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1480 switch (event_id) {
Michael Chan87c374d2016-12-02 21:17:16 -05001481 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
Michael Chan8cbde112016-04-11 04:11:14 -04001482 u32 data1 = le32_to_cpu(cmpl->event_data1);
1483 struct bnxt_link_info *link_info = &bp->link_info;
1484
1485 if (BNXT_VF(bp))
1486 goto async_event_process_exit;
1487 if (data1 & 0x20000) {
1488 u16 fw_speed = link_info->force_link_speed;
1489 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1490
1491 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1492 speed);
1493 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001494 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001495 /* fall thru */
1496 }
Michael Chan87c374d2016-12-02 21:17:16 -05001497 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
Michael Chanc0c050c2015-10-22 16:01:17 -04001498 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001499 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001500 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
Jeffrey Huang19241362016-02-26 04:00:00 -05001501 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001502 break;
Michael Chan87c374d2016-12-02 21:17:16 -05001503 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
Michael Chan4bb13ab2016-04-05 14:09:01 -04001504 u32 data1 = le32_to_cpu(cmpl->event_data1);
1505 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1506
1507 if (BNXT_VF(bp))
1508 break;
1509
1510 if (bp->pf.port_id != port_id)
1511 break;
1512
Michael Chan4bb13ab2016-04-05 14:09:01 -04001513 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1514 break;
1515 }
Michael Chan87c374d2016-12-02 21:17:16 -05001516 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
Michael Chanfc0f1922016-06-13 02:25:30 -04001517 if (BNXT_PF(bp))
1518 goto async_event_process_exit;
1519 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1520 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001521 default:
Jeffrey Huang19241362016-02-26 04:00:00 -05001522 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001523 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001524 schedule_work(&bp->sp_task);
1525async_event_process_exit:
Michael Chana588e452016-12-07 00:26:21 -05001526 bnxt_ulp_async_events(bp, cmpl);
Michael Chanc0c050c2015-10-22 16:01:17 -04001527 return 0;
1528}
1529
1530static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1531{
1532 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1533 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1534 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1535 (struct hwrm_fwd_req_cmpl *)txcmp;
1536
1537 switch (cmpl_type) {
1538 case CMPL_BASE_TYPE_HWRM_DONE:
1539 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1540 if (seq_id == bp->hwrm_intr_seq_id)
1541 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1542 else
1543 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1544 break;
1545
1546 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1547 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1548
1549 if ((vf_id < bp->pf.first_vf_id) ||
1550 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1551 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1552 vf_id);
1553 return -EINVAL;
1554 }
1555
1556 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1557 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1558 schedule_work(&bp->sp_task);
1559 break;
1560
1561 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1562 bnxt_async_event_process(bp,
1563 (struct hwrm_async_event_cmpl *)txcmp);
1564
1565 default:
1566 break;
1567 }
1568
1569 return 0;
1570}
1571
1572static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1573{
1574 struct bnxt_napi *bnapi = dev_instance;
1575 struct bnxt *bp = bnapi->bp;
1576 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1577 u32 cons = RING_CMP(cpr->cp_raw_cons);
1578
1579 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1580 napi_schedule(&bnapi->napi);
1581 return IRQ_HANDLED;
1582}
1583
1584static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1585{
1586 u32 raw_cons = cpr->cp_raw_cons;
1587 u16 cons = RING_CMP(raw_cons);
1588 struct tx_cmp *txcmp;
1589
1590 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1591
1592 return TX_CMP_VALID(txcmp, raw_cons);
1593}
1594
Michael Chanc0c050c2015-10-22 16:01:17 -04001595static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1596{
1597 struct bnxt_napi *bnapi = dev_instance;
1598 struct bnxt *bp = bnapi->bp;
1599 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1600 u32 cons = RING_CMP(cpr->cp_raw_cons);
1601 u32 int_status;
1602
1603 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1604
1605 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001606 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001607 /* return if erroneous interrupt */
1608 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1609 return IRQ_NONE;
1610 }
1611
1612 /* disable ring IRQ */
1613 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1614
1615 /* Return here if interrupt is shared and is disabled. */
1616 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1617 return IRQ_HANDLED;
1618
1619 napi_schedule(&bnapi->napi);
1620 return IRQ_HANDLED;
1621}
1622
1623static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1624{
1625 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1626 u32 raw_cons = cpr->cp_raw_cons;
1627 u32 cons;
1628 int tx_pkts = 0;
1629 int rx_pkts = 0;
1630 bool rx_event = false;
1631 bool agg_event = false;
1632 struct tx_cmp *txcmp;
1633
1634 while (1) {
1635 int rc;
1636
1637 cons = RING_CMP(raw_cons);
1638 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1639
1640 if (!TX_CMP_VALID(txcmp, raw_cons))
1641 break;
1642
Michael Chan67a95e22016-05-04 16:56:43 -04001643 /* The valid test of the entry must be done first before
1644 * reading any further.
1645 */
Michael Chanb67daab2016-05-15 03:04:51 -04001646 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001647 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1648 tx_pkts++;
1649 /* return full budget so NAPI will complete. */
1650 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1651 rx_pkts = budget;
1652 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1653 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1654 if (likely(rc >= 0))
1655 rx_pkts += rc;
1656 else if (rc == -EBUSY) /* partial completion */
1657 break;
1658 rx_event = true;
1659 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1660 CMPL_BASE_TYPE_HWRM_DONE) ||
1661 (TX_CMP_TYPE(txcmp) ==
1662 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1663 (TX_CMP_TYPE(txcmp) ==
1664 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1665 bnxt_hwrm_handler(bp, txcmp);
1666 }
1667 raw_cons = NEXT_RAW_CMP(raw_cons);
1668
1669 if (rx_pkts == budget)
1670 break;
1671 }
1672
1673 cpr->cp_raw_cons = raw_cons;
1674 /* ACK completion ring before freeing tx ring and producing new
1675 * buffers in rx/agg rings to prevent overflowing the completion
1676 * ring.
1677 */
1678 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1679
1680 if (tx_pkts)
1681 bnxt_tx_int(bp, bnapi, tx_pkts);
1682
1683 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001684 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001685
1686 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1687 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1688 if (agg_event) {
1689 writel(DB_KEY_RX | rxr->rx_agg_prod,
1690 rxr->rx_agg_doorbell);
1691 writel(DB_KEY_RX | rxr->rx_agg_prod,
1692 rxr->rx_agg_doorbell);
1693 }
1694 }
1695 return rx_pkts;
1696}
1697
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001698static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1699{
1700 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1701 struct bnxt *bp = bnapi->bp;
1702 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1703 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1704 struct tx_cmp *txcmp;
1705 struct rx_cmp_ext *rxcmp1;
1706 u32 cp_cons, tmp_raw_cons;
1707 u32 raw_cons = cpr->cp_raw_cons;
1708 u32 rx_pkts = 0;
1709 bool agg_event = false;
1710
1711 while (1) {
1712 int rc;
1713
1714 cp_cons = RING_CMP(raw_cons);
1715 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1716
1717 if (!TX_CMP_VALID(txcmp, raw_cons))
1718 break;
1719
1720 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1721 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1722 cp_cons = RING_CMP(tmp_raw_cons);
1723 rxcmp1 = (struct rx_cmp_ext *)
1724 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1725
1726 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1727 break;
1728
1729 /* force an error to recycle the buffer */
1730 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1731 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1732
1733 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1734 if (likely(rc == -EIO))
1735 rx_pkts++;
1736 else if (rc == -EBUSY) /* partial completion */
1737 break;
1738 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1739 CMPL_BASE_TYPE_HWRM_DONE)) {
1740 bnxt_hwrm_handler(bp, txcmp);
1741 } else {
1742 netdev_err(bp->dev,
1743 "Invalid completion received on special ring\n");
1744 }
1745 raw_cons = NEXT_RAW_CMP(raw_cons);
1746
1747 if (rx_pkts == budget)
1748 break;
1749 }
1750
1751 cpr->cp_raw_cons = raw_cons;
1752 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1753 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1754 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1755
1756 if (agg_event) {
1757 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1758 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1759 }
1760
1761 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1762 napi_complete(napi);
1763 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1764 }
1765 return rx_pkts;
1766}
1767
Michael Chanc0c050c2015-10-22 16:01:17 -04001768static int bnxt_poll(struct napi_struct *napi, int budget)
1769{
1770 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1771 struct bnxt *bp = bnapi->bp;
1772 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1773 int work_done = 0;
1774
Michael Chanc0c050c2015-10-22 16:01:17 -04001775 while (1) {
1776 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1777
1778 if (work_done >= budget)
1779 break;
1780
1781 if (!bnxt_has_work(bp, cpr)) {
Michael Chane7b95692016-12-29 12:13:32 -05001782 if (napi_complete_done(napi, work_done))
1783 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1784 cpr->cp_raw_cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001785 break;
1786 }
1787 }
1788 mmiowb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001789 return work_done;
1790}
1791
Michael Chanc0c050c2015-10-22 16:01:17 -04001792static void bnxt_free_tx_skbs(struct bnxt *bp)
1793{
1794 int i, max_idx;
1795 struct pci_dev *pdev = bp->pdev;
1796
Michael Chanb6ab4b02016-01-02 23:44:59 -05001797 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001798 return;
1799
1800 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1801 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001802 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001803 int j;
1804
Michael Chanc0c050c2015-10-22 16:01:17 -04001805 for (j = 0; j < max_idx;) {
1806 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1807 struct sk_buff *skb = tx_buf->skb;
1808 int k, last;
1809
1810 if (!skb) {
1811 j++;
1812 continue;
1813 }
1814
1815 tx_buf->skb = NULL;
1816
1817 if (tx_buf->is_push) {
1818 dev_kfree_skb(skb);
1819 j += 2;
1820 continue;
1821 }
1822
1823 dma_unmap_single(&pdev->dev,
1824 dma_unmap_addr(tx_buf, mapping),
1825 skb_headlen(skb),
1826 PCI_DMA_TODEVICE);
1827
1828 last = tx_buf->nr_frags;
1829 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001830 for (k = 0; k < last; k++, j++) {
1831 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001832 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1833
Michael Chand612a572016-01-28 03:11:22 -05001834 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001835 dma_unmap_page(
1836 &pdev->dev,
1837 dma_unmap_addr(tx_buf, mapping),
1838 skb_frag_size(frag), PCI_DMA_TODEVICE);
1839 }
1840 dev_kfree_skb(skb);
1841 }
1842 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1843 }
1844}
1845
1846static void bnxt_free_rx_skbs(struct bnxt *bp)
1847{
1848 int i, max_idx, max_agg_idx;
1849 struct pci_dev *pdev = bp->pdev;
1850
Michael Chanb6ab4b02016-01-02 23:44:59 -05001851 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001852 return;
1853
1854 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1855 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1856 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001857 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001858 int j;
1859
Michael Chanc0c050c2015-10-22 16:01:17 -04001860 if (rxr->rx_tpa) {
1861 for (j = 0; j < MAX_TPA; j++) {
1862 struct bnxt_tpa_info *tpa_info =
1863 &rxr->rx_tpa[j];
1864 u8 *data = tpa_info->data;
1865
1866 if (!data)
1867 continue;
1868
1869 dma_unmap_single(
1870 &pdev->dev,
1871 dma_unmap_addr(tpa_info, mapping),
1872 bp->rx_buf_use_size,
1873 PCI_DMA_FROMDEVICE);
1874
1875 tpa_info->data = NULL;
1876
1877 kfree(data);
1878 }
1879 }
1880
1881 for (j = 0; j < max_idx; j++) {
1882 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1883 u8 *data = rx_buf->data;
1884
1885 if (!data)
1886 continue;
1887
1888 dma_unmap_single(&pdev->dev,
1889 dma_unmap_addr(rx_buf, mapping),
1890 bp->rx_buf_use_size,
1891 PCI_DMA_FROMDEVICE);
1892
1893 rx_buf->data = NULL;
1894
1895 kfree(data);
1896 }
1897
1898 for (j = 0; j < max_agg_idx; j++) {
1899 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1900 &rxr->rx_agg_ring[j];
1901 struct page *page = rx_agg_buf->page;
1902
1903 if (!page)
1904 continue;
1905
1906 dma_unmap_page(&pdev->dev,
1907 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001908 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001909
1910 rx_agg_buf->page = NULL;
1911 __clear_bit(j, rxr->rx_agg_bmap);
1912
1913 __free_page(page);
1914 }
Michael Chan89d0a062016-04-25 02:30:51 -04001915 if (rxr->rx_page) {
1916 __free_page(rxr->rx_page);
1917 rxr->rx_page = NULL;
1918 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001919 }
1920}
1921
1922static void bnxt_free_skbs(struct bnxt *bp)
1923{
1924 bnxt_free_tx_skbs(bp);
1925 bnxt_free_rx_skbs(bp);
1926}
1927
1928static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1929{
1930 struct pci_dev *pdev = bp->pdev;
1931 int i;
1932
1933 for (i = 0; i < ring->nr_pages; i++) {
1934 if (!ring->pg_arr[i])
1935 continue;
1936
1937 dma_free_coherent(&pdev->dev, ring->page_size,
1938 ring->pg_arr[i], ring->dma_arr[i]);
1939
1940 ring->pg_arr[i] = NULL;
1941 }
1942 if (ring->pg_tbl) {
1943 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1944 ring->pg_tbl, ring->pg_tbl_map);
1945 ring->pg_tbl = NULL;
1946 }
1947 if (ring->vmem_size && *ring->vmem) {
1948 vfree(*ring->vmem);
1949 *ring->vmem = NULL;
1950 }
1951}
1952
1953static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1954{
1955 int i;
1956 struct pci_dev *pdev = bp->pdev;
1957
1958 if (ring->nr_pages > 1) {
1959 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1960 ring->nr_pages * 8,
1961 &ring->pg_tbl_map,
1962 GFP_KERNEL);
1963 if (!ring->pg_tbl)
1964 return -ENOMEM;
1965 }
1966
1967 for (i = 0; i < ring->nr_pages; i++) {
1968 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1969 ring->page_size,
1970 &ring->dma_arr[i],
1971 GFP_KERNEL);
1972 if (!ring->pg_arr[i])
1973 return -ENOMEM;
1974
1975 if (ring->nr_pages > 1)
1976 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1977 }
1978
1979 if (ring->vmem_size) {
1980 *ring->vmem = vzalloc(ring->vmem_size);
1981 if (!(*ring->vmem))
1982 return -ENOMEM;
1983 }
1984 return 0;
1985}
1986
1987static void bnxt_free_rx_rings(struct bnxt *bp)
1988{
1989 int i;
1990
Michael Chanb6ab4b02016-01-02 23:44:59 -05001991 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001992 return;
1993
1994 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001995 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001996 struct bnxt_ring_struct *ring;
1997
Michael Chanc0c050c2015-10-22 16:01:17 -04001998 kfree(rxr->rx_tpa);
1999 rxr->rx_tpa = NULL;
2000
2001 kfree(rxr->rx_agg_bmap);
2002 rxr->rx_agg_bmap = NULL;
2003
2004 ring = &rxr->rx_ring_struct;
2005 bnxt_free_ring(bp, ring);
2006
2007 ring = &rxr->rx_agg_ring_struct;
2008 bnxt_free_ring(bp, ring);
2009 }
2010}
2011
2012static int bnxt_alloc_rx_rings(struct bnxt *bp)
2013{
2014 int i, rc, agg_rings = 0, tpa_rings = 0;
2015
Michael Chanb6ab4b02016-01-02 23:44:59 -05002016 if (!bp->rx_ring)
2017 return -ENOMEM;
2018
Michael Chanc0c050c2015-10-22 16:01:17 -04002019 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2020 agg_rings = 1;
2021
2022 if (bp->flags & BNXT_FLAG_TPA)
2023 tpa_rings = 1;
2024
2025 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002026 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002027 struct bnxt_ring_struct *ring;
2028
Michael Chanc0c050c2015-10-22 16:01:17 -04002029 ring = &rxr->rx_ring_struct;
2030
2031 rc = bnxt_alloc_ring(bp, ring);
2032 if (rc)
2033 return rc;
2034
2035 if (agg_rings) {
2036 u16 mem_size;
2037
2038 ring = &rxr->rx_agg_ring_struct;
2039 rc = bnxt_alloc_ring(bp, ring);
2040 if (rc)
2041 return rc;
2042
2043 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2044 mem_size = rxr->rx_agg_bmap_size / 8;
2045 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2046 if (!rxr->rx_agg_bmap)
2047 return -ENOMEM;
2048
2049 if (tpa_rings) {
2050 rxr->rx_tpa = kcalloc(MAX_TPA,
2051 sizeof(struct bnxt_tpa_info),
2052 GFP_KERNEL);
2053 if (!rxr->rx_tpa)
2054 return -ENOMEM;
2055 }
2056 }
2057 }
2058 return 0;
2059}
2060
2061static void bnxt_free_tx_rings(struct bnxt *bp)
2062{
2063 int i;
2064 struct pci_dev *pdev = bp->pdev;
2065
Michael Chanb6ab4b02016-01-02 23:44:59 -05002066 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002067 return;
2068
2069 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002070 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002071 struct bnxt_ring_struct *ring;
2072
Michael Chanc0c050c2015-10-22 16:01:17 -04002073 if (txr->tx_push) {
2074 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2075 txr->tx_push, txr->tx_push_mapping);
2076 txr->tx_push = NULL;
2077 }
2078
2079 ring = &txr->tx_ring_struct;
2080
2081 bnxt_free_ring(bp, ring);
2082 }
2083}
2084
2085static int bnxt_alloc_tx_rings(struct bnxt *bp)
2086{
2087 int i, j, rc;
2088 struct pci_dev *pdev = bp->pdev;
2089
2090 bp->tx_push_size = 0;
2091 if (bp->tx_push_thresh) {
2092 int push_size;
2093
2094 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2095 bp->tx_push_thresh);
2096
Michael Chan4419dbe2016-02-10 17:33:49 -05002097 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002098 push_size = 0;
2099 bp->tx_push_thresh = 0;
2100 }
2101
2102 bp->tx_push_size = push_size;
2103 }
2104
2105 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002106 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002107 struct bnxt_ring_struct *ring;
2108
Michael Chanc0c050c2015-10-22 16:01:17 -04002109 ring = &txr->tx_ring_struct;
2110
2111 rc = bnxt_alloc_ring(bp, ring);
2112 if (rc)
2113 return rc;
2114
2115 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002116 dma_addr_t mapping;
2117
2118 /* One pre-allocated DMA buffer to backup
2119 * TX push operation
2120 */
2121 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2122 bp->tx_push_size,
2123 &txr->tx_push_mapping,
2124 GFP_KERNEL);
2125
2126 if (!txr->tx_push)
2127 return -ENOMEM;
2128
Michael Chanc0c050c2015-10-22 16:01:17 -04002129 mapping = txr->tx_push_mapping +
2130 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002131 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002132
Michael Chan4419dbe2016-02-10 17:33:49 -05002133 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002134 }
2135 ring->queue_id = bp->q_info[j].queue_id;
2136 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2137 j++;
2138 }
2139 return 0;
2140}
2141
2142static void bnxt_free_cp_rings(struct bnxt *bp)
2143{
2144 int i;
2145
2146 if (!bp->bnapi)
2147 return;
2148
2149 for (i = 0; i < bp->cp_nr_rings; i++) {
2150 struct bnxt_napi *bnapi = bp->bnapi[i];
2151 struct bnxt_cp_ring_info *cpr;
2152 struct bnxt_ring_struct *ring;
2153
2154 if (!bnapi)
2155 continue;
2156
2157 cpr = &bnapi->cp_ring;
2158 ring = &cpr->cp_ring_struct;
2159
2160 bnxt_free_ring(bp, ring);
2161 }
2162}
2163
2164static int bnxt_alloc_cp_rings(struct bnxt *bp)
2165{
2166 int i, rc;
2167
2168 for (i = 0; i < bp->cp_nr_rings; i++) {
2169 struct bnxt_napi *bnapi = bp->bnapi[i];
2170 struct bnxt_cp_ring_info *cpr;
2171 struct bnxt_ring_struct *ring;
2172
2173 if (!bnapi)
2174 continue;
2175
2176 cpr = &bnapi->cp_ring;
2177 ring = &cpr->cp_ring_struct;
2178
2179 rc = bnxt_alloc_ring(bp, ring);
2180 if (rc)
2181 return rc;
2182 }
2183 return 0;
2184}
2185
2186static void bnxt_init_ring_struct(struct bnxt *bp)
2187{
2188 int i;
2189
2190 for (i = 0; i < bp->cp_nr_rings; i++) {
2191 struct bnxt_napi *bnapi = bp->bnapi[i];
2192 struct bnxt_cp_ring_info *cpr;
2193 struct bnxt_rx_ring_info *rxr;
2194 struct bnxt_tx_ring_info *txr;
2195 struct bnxt_ring_struct *ring;
2196
2197 if (!bnapi)
2198 continue;
2199
2200 cpr = &bnapi->cp_ring;
2201 ring = &cpr->cp_ring_struct;
2202 ring->nr_pages = bp->cp_nr_pages;
2203 ring->page_size = HW_CMPD_RING_SIZE;
2204 ring->pg_arr = (void **)cpr->cp_desc_ring;
2205 ring->dma_arr = cpr->cp_desc_mapping;
2206 ring->vmem_size = 0;
2207
Michael Chanb6ab4b02016-01-02 23:44:59 -05002208 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002209 if (!rxr)
2210 goto skip_rx;
2211
Michael Chanc0c050c2015-10-22 16:01:17 -04002212 ring = &rxr->rx_ring_struct;
2213 ring->nr_pages = bp->rx_nr_pages;
2214 ring->page_size = HW_RXBD_RING_SIZE;
2215 ring->pg_arr = (void **)rxr->rx_desc_ring;
2216 ring->dma_arr = rxr->rx_desc_mapping;
2217 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2218 ring->vmem = (void **)&rxr->rx_buf_ring;
2219
2220 ring = &rxr->rx_agg_ring_struct;
2221 ring->nr_pages = bp->rx_agg_nr_pages;
2222 ring->page_size = HW_RXBD_RING_SIZE;
2223 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2224 ring->dma_arr = rxr->rx_agg_desc_mapping;
2225 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2226 ring->vmem = (void **)&rxr->rx_agg_ring;
2227
Michael Chan3b2b7d92016-01-02 23:45:00 -05002228skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002229 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002230 if (!txr)
2231 continue;
2232
Michael Chanc0c050c2015-10-22 16:01:17 -04002233 ring = &txr->tx_ring_struct;
2234 ring->nr_pages = bp->tx_nr_pages;
2235 ring->page_size = HW_RXBD_RING_SIZE;
2236 ring->pg_arr = (void **)txr->tx_desc_ring;
2237 ring->dma_arr = txr->tx_desc_mapping;
2238 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2239 ring->vmem = (void **)&txr->tx_buf_ring;
2240 }
2241}
2242
2243static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2244{
2245 int i;
2246 u32 prod;
2247 struct rx_bd **rx_buf_ring;
2248
2249 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2250 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2251 int j;
2252 struct rx_bd *rxbd;
2253
2254 rxbd = rx_buf_ring[i];
2255 if (!rxbd)
2256 continue;
2257
2258 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2259 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2260 rxbd->rx_bd_opaque = prod;
2261 }
2262 }
2263}
2264
2265static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2266{
2267 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002268 struct bnxt_rx_ring_info *rxr;
2269 struct bnxt_ring_struct *ring;
2270 u32 prod, type;
2271 int i;
2272
Michael Chanc0c050c2015-10-22 16:01:17 -04002273 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2274 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2275
2276 if (NET_IP_ALIGN == 2)
2277 type |= RX_BD_FLAGS_SOP;
2278
Michael Chanb6ab4b02016-01-02 23:44:59 -05002279 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002280 ring = &rxr->rx_ring_struct;
2281 bnxt_init_rxbd_pages(ring, type);
2282
2283 prod = rxr->rx_prod;
2284 for (i = 0; i < bp->rx_ring_size; i++) {
2285 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2286 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2287 ring_nr, i, bp->rx_ring_size);
2288 break;
2289 }
2290 prod = NEXT_RX(prod);
2291 }
2292 rxr->rx_prod = prod;
2293 ring->fw_ring_id = INVALID_HW_RING_ID;
2294
Michael Chanedd0c2c2015-12-27 18:19:19 -05002295 ring = &rxr->rx_agg_ring_struct;
2296 ring->fw_ring_id = INVALID_HW_RING_ID;
2297
Michael Chanc0c050c2015-10-22 16:01:17 -04002298 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2299 return 0;
2300
Michael Chan2839f282016-04-25 02:30:50 -04002301 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002302 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2303
2304 bnxt_init_rxbd_pages(ring, type);
2305
2306 prod = rxr->rx_agg_prod;
2307 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2308 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2309 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2310 ring_nr, i, bp->rx_ring_size);
2311 break;
2312 }
2313 prod = NEXT_RX_AGG(prod);
2314 }
2315 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002316
2317 if (bp->flags & BNXT_FLAG_TPA) {
2318 if (rxr->rx_tpa) {
2319 u8 *data;
2320 dma_addr_t mapping;
2321
2322 for (i = 0; i < MAX_TPA; i++) {
2323 data = __bnxt_alloc_rx_data(bp, &mapping,
2324 GFP_KERNEL);
2325 if (!data)
2326 return -ENOMEM;
2327
2328 rxr->rx_tpa[i].data = data;
2329 rxr->rx_tpa[i].mapping = mapping;
2330 }
2331 } else {
2332 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2333 return -ENOMEM;
2334 }
2335 }
2336
2337 return 0;
2338}
2339
2340static int bnxt_init_rx_rings(struct bnxt *bp)
2341{
2342 int i, rc = 0;
2343
2344 for (i = 0; i < bp->rx_nr_rings; i++) {
2345 rc = bnxt_init_one_rx_ring(bp, i);
2346 if (rc)
2347 break;
2348 }
2349
2350 return rc;
2351}
2352
2353static int bnxt_init_tx_rings(struct bnxt *bp)
2354{
2355 u16 i;
2356
2357 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2358 MAX_SKB_FRAGS + 1);
2359
2360 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002361 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002362 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2363
2364 ring->fw_ring_id = INVALID_HW_RING_ID;
2365 }
2366
2367 return 0;
2368}
2369
2370static void bnxt_free_ring_grps(struct bnxt *bp)
2371{
2372 kfree(bp->grp_info);
2373 bp->grp_info = NULL;
2374}
2375
2376static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2377{
2378 int i;
2379
2380 if (irq_re_init) {
2381 bp->grp_info = kcalloc(bp->cp_nr_rings,
2382 sizeof(struct bnxt_ring_grp_info),
2383 GFP_KERNEL);
2384 if (!bp->grp_info)
2385 return -ENOMEM;
2386 }
2387 for (i = 0; i < bp->cp_nr_rings; i++) {
2388 if (irq_re_init)
2389 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2390 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2391 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2392 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2393 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2394 }
2395 return 0;
2396}
2397
2398static void bnxt_free_vnics(struct bnxt *bp)
2399{
2400 kfree(bp->vnic_info);
2401 bp->vnic_info = NULL;
2402 bp->nr_vnics = 0;
2403}
2404
2405static int bnxt_alloc_vnics(struct bnxt *bp)
2406{
2407 int num_vnics = 1;
2408
2409#ifdef CONFIG_RFS_ACCEL
2410 if (bp->flags & BNXT_FLAG_RFS)
2411 num_vnics += bp->rx_nr_rings;
2412#endif
2413
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002414 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2415 num_vnics++;
2416
Michael Chanc0c050c2015-10-22 16:01:17 -04002417 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2418 GFP_KERNEL);
2419 if (!bp->vnic_info)
2420 return -ENOMEM;
2421
2422 bp->nr_vnics = num_vnics;
2423 return 0;
2424}
2425
2426static void bnxt_init_vnics(struct bnxt *bp)
2427{
2428 int i;
2429
2430 for (i = 0; i < bp->nr_vnics; i++) {
2431 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2432
2433 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002434 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2435 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002436 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2437
2438 if (bp->vnic_info[i].rss_hash_key) {
2439 if (i == 0)
2440 prandom_bytes(vnic->rss_hash_key,
2441 HW_HASH_KEY_SIZE);
2442 else
2443 memcpy(vnic->rss_hash_key,
2444 bp->vnic_info[0].rss_hash_key,
2445 HW_HASH_KEY_SIZE);
2446 }
2447 }
2448}
2449
2450static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2451{
2452 int pages;
2453
2454 pages = ring_size / desc_per_pg;
2455
2456 if (!pages)
2457 return 1;
2458
2459 pages++;
2460
2461 while (pages & (pages - 1))
2462 pages++;
2463
2464 return pages;
2465}
2466
2467static void bnxt_set_tpa_flags(struct bnxt *bp)
2468{
2469 bp->flags &= ~BNXT_FLAG_TPA;
2470 if (bp->dev->features & NETIF_F_LRO)
2471 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002472 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002473 bp->flags |= BNXT_FLAG_GRO;
2474}
2475
2476/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2477 * be set on entry.
2478 */
2479void bnxt_set_ring_params(struct bnxt *bp)
2480{
2481 u32 ring_size, rx_size, rx_space;
2482 u32 agg_factor = 0, agg_ring_size = 0;
2483
2484 /* 8 for CRC and VLAN */
2485 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2486
2487 rx_space = rx_size + NET_SKB_PAD +
2488 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2489
2490 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2491 ring_size = bp->rx_ring_size;
2492 bp->rx_agg_ring_size = 0;
2493 bp->rx_agg_nr_pages = 0;
2494
2495 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002496 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002497
2498 bp->flags &= ~BNXT_FLAG_JUMBO;
2499 if (rx_space > PAGE_SIZE) {
2500 u32 jumbo_factor;
2501
2502 bp->flags |= BNXT_FLAG_JUMBO;
2503 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2504 if (jumbo_factor > agg_factor)
2505 agg_factor = jumbo_factor;
2506 }
2507 agg_ring_size = ring_size * agg_factor;
2508
2509 if (agg_ring_size) {
2510 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2511 RX_DESC_CNT);
2512 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2513 u32 tmp = agg_ring_size;
2514
2515 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2516 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2517 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2518 tmp, agg_ring_size);
2519 }
2520 bp->rx_agg_ring_size = agg_ring_size;
2521 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2522 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2523 rx_space = rx_size + NET_SKB_PAD +
2524 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2525 }
2526
2527 bp->rx_buf_use_size = rx_size;
2528 bp->rx_buf_size = rx_space;
2529
2530 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2531 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2532
2533 ring_size = bp->tx_ring_size;
2534 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2535 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2536
2537 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2538 bp->cp_ring_size = ring_size;
2539
2540 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2541 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2542 bp->cp_nr_pages = MAX_CP_PAGES;
2543 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2544 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2545 ring_size, bp->cp_ring_size);
2546 }
2547 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2548 bp->cp_ring_mask = bp->cp_bit - 1;
2549}
2550
2551static void bnxt_free_vnic_attributes(struct bnxt *bp)
2552{
2553 int i;
2554 struct bnxt_vnic_info *vnic;
2555 struct pci_dev *pdev = bp->pdev;
2556
2557 if (!bp->vnic_info)
2558 return;
2559
2560 for (i = 0; i < bp->nr_vnics; i++) {
2561 vnic = &bp->vnic_info[i];
2562
2563 kfree(vnic->fw_grp_ids);
2564 vnic->fw_grp_ids = NULL;
2565
2566 kfree(vnic->uc_list);
2567 vnic->uc_list = NULL;
2568
2569 if (vnic->mc_list) {
2570 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2571 vnic->mc_list, vnic->mc_list_mapping);
2572 vnic->mc_list = NULL;
2573 }
2574
2575 if (vnic->rss_table) {
2576 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2577 vnic->rss_table,
2578 vnic->rss_table_dma_addr);
2579 vnic->rss_table = NULL;
2580 }
2581
2582 vnic->rss_hash_key = NULL;
2583 vnic->flags = 0;
2584 }
2585}
2586
2587static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2588{
2589 int i, rc = 0, size;
2590 struct bnxt_vnic_info *vnic;
2591 struct pci_dev *pdev = bp->pdev;
2592 int max_rings;
2593
2594 for (i = 0; i < bp->nr_vnics; i++) {
2595 vnic = &bp->vnic_info[i];
2596
2597 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2598 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2599
2600 if (mem_size > 0) {
2601 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2602 if (!vnic->uc_list) {
2603 rc = -ENOMEM;
2604 goto out;
2605 }
2606 }
2607 }
2608
2609 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2610 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2611 vnic->mc_list =
2612 dma_alloc_coherent(&pdev->dev,
2613 vnic->mc_list_size,
2614 &vnic->mc_list_mapping,
2615 GFP_KERNEL);
2616 if (!vnic->mc_list) {
2617 rc = -ENOMEM;
2618 goto out;
2619 }
2620 }
2621
2622 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2623 max_rings = bp->rx_nr_rings;
2624 else
2625 max_rings = 1;
2626
2627 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2628 if (!vnic->fw_grp_ids) {
2629 rc = -ENOMEM;
2630 goto out;
2631 }
2632
2633 /* Allocate rss table and hash key */
2634 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2635 &vnic->rss_table_dma_addr,
2636 GFP_KERNEL);
2637 if (!vnic->rss_table) {
2638 rc = -ENOMEM;
2639 goto out;
2640 }
2641
2642 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2643
2644 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2645 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2646 }
2647 return 0;
2648
2649out:
2650 return rc;
2651}
2652
2653static void bnxt_free_hwrm_resources(struct bnxt *bp)
2654{
2655 struct pci_dev *pdev = bp->pdev;
2656
2657 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2658 bp->hwrm_cmd_resp_dma_addr);
2659
2660 bp->hwrm_cmd_resp_addr = NULL;
2661 if (bp->hwrm_dbg_resp_addr) {
2662 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2663 bp->hwrm_dbg_resp_addr,
2664 bp->hwrm_dbg_resp_dma_addr);
2665
2666 bp->hwrm_dbg_resp_addr = NULL;
2667 }
2668}
2669
2670static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2671{
2672 struct pci_dev *pdev = bp->pdev;
2673
2674 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2675 &bp->hwrm_cmd_resp_dma_addr,
2676 GFP_KERNEL);
2677 if (!bp->hwrm_cmd_resp_addr)
2678 return -ENOMEM;
2679 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2680 HWRM_DBG_REG_BUF_SIZE,
2681 &bp->hwrm_dbg_resp_dma_addr,
2682 GFP_KERNEL);
2683 if (!bp->hwrm_dbg_resp_addr)
2684 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2685
2686 return 0;
2687}
2688
2689static void bnxt_free_stats(struct bnxt *bp)
2690{
2691 u32 size, i;
2692 struct pci_dev *pdev = bp->pdev;
2693
Michael Chan3bdf56c2016-03-07 15:38:45 -05002694 if (bp->hw_rx_port_stats) {
2695 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2696 bp->hw_rx_port_stats,
2697 bp->hw_rx_port_stats_map);
2698 bp->hw_rx_port_stats = NULL;
2699 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2700 }
2701
Michael Chanc0c050c2015-10-22 16:01:17 -04002702 if (!bp->bnapi)
2703 return;
2704
2705 size = sizeof(struct ctx_hw_stats);
2706
2707 for (i = 0; i < bp->cp_nr_rings; i++) {
2708 struct bnxt_napi *bnapi = bp->bnapi[i];
2709 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2710
2711 if (cpr->hw_stats) {
2712 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2713 cpr->hw_stats_map);
2714 cpr->hw_stats = NULL;
2715 }
2716 }
2717}
2718
2719static int bnxt_alloc_stats(struct bnxt *bp)
2720{
2721 u32 size, i;
2722 struct pci_dev *pdev = bp->pdev;
2723
2724 size = sizeof(struct ctx_hw_stats);
2725
2726 for (i = 0; i < bp->cp_nr_rings; i++) {
2727 struct bnxt_napi *bnapi = bp->bnapi[i];
2728 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2729
2730 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2731 &cpr->hw_stats_map,
2732 GFP_KERNEL);
2733 if (!cpr->hw_stats)
2734 return -ENOMEM;
2735
2736 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2737 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002738
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002739 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002740 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2741 sizeof(struct tx_port_stats) + 1024;
2742
2743 bp->hw_rx_port_stats =
2744 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2745 &bp->hw_rx_port_stats_map,
2746 GFP_KERNEL);
2747 if (!bp->hw_rx_port_stats)
2748 return -ENOMEM;
2749
2750 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2751 512;
2752 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2753 sizeof(struct rx_port_stats) + 512;
2754 bp->flags |= BNXT_FLAG_PORT_STATS;
2755 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002756 return 0;
2757}
2758
2759static void bnxt_clear_ring_indices(struct bnxt *bp)
2760{
2761 int i;
2762
2763 if (!bp->bnapi)
2764 return;
2765
2766 for (i = 0; i < bp->cp_nr_rings; i++) {
2767 struct bnxt_napi *bnapi = bp->bnapi[i];
2768 struct bnxt_cp_ring_info *cpr;
2769 struct bnxt_rx_ring_info *rxr;
2770 struct bnxt_tx_ring_info *txr;
2771
2772 if (!bnapi)
2773 continue;
2774
2775 cpr = &bnapi->cp_ring;
2776 cpr->cp_raw_cons = 0;
2777
Michael Chanb6ab4b02016-01-02 23:44:59 -05002778 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002779 if (txr) {
2780 txr->tx_prod = 0;
2781 txr->tx_cons = 0;
2782 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002783
Michael Chanb6ab4b02016-01-02 23:44:59 -05002784 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002785 if (rxr) {
2786 rxr->rx_prod = 0;
2787 rxr->rx_agg_prod = 0;
2788 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002789 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002790 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002791 }
2792}
2793
2794static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2795{
2796#ifdef CONFIG_RFS_ACCEL
2797 int i;
2798
2799 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2800 * safe to delete the hash table.
2801 */
2802 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2803 struct hlist_head *head;
2804 struct hlist_node *tmp;
2805 struct bnxt_ntuple_filter *fltr;
2806
2807 head = &bp->ntp_fltr_hash_tbl[i];
2808 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2809 hlist_del(&fltr->hash);
2810 kfree(fltr);
2811 }
2812 }
2813 if (irq_reinit) {
2814 kfree(bp->ntp_fltr_bmap);
2815 bp->ntp_fltr_bmap = NULL;
2816 }
2817 bp->ntp_fltr_count = 0;
2818#endif
2819}
2820
2821static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2822{
2823#ifdef CONFIG_RFS_ACCEL
2824 int i, rc = 0;
2825
2826 if (!(bp->flags & BNXT_FLAG_RFS))
2827 return 0;
2828
2829 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2830 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2831
2832 bp->ntp_fltr_count = 0;
2833 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2834 GFP_KERNEL);
2835
2836 if (!bp->ntp_fltr_bmap)
2837 rc = -ENOMEM;
2838
2839 return rc;
2840#else
2841 return 0;
2842#endif
2843}
2844
2845static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2846{
2847 bnxt_free_vnic_attributes(bp);
2848 bnxt_free_tx_rings(bp);
2849 bnxt_free_rx_rings(bp);
2850 bnxt_free_cp_rings(bp);
2851 bnxt_free_ntp_fltrs(bp, irq_re_init);
2852 if (irq_re_init) {
2853 bnxt_free_stats(bp);
2854 bnxt_free_ring_grps(bp);
2855 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002856 kfree(bp->tx_ring);
2857 bp->tx_ring = NULL;
2858 kfree(bp->rx_ring);
2859 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002860 kfree(bp->bnapi);
2861 bp->bnapi = NULL;
2862 } else {
2863 bnxt_clear_ring_indices(bp);
2864 }
2865}
2866
2867static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2868{
Michael Chan01657bc2016-01-02 23:45:03 -05002869 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002870 void *bnapi;
2871
2872 if (irq_re_init) {
2873 /* Allocate bnapi mem pointer array and mem block for
2874 * all queues
2875 */
2876 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2877 bp->cp_nr_rings);
2878 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2879 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2880 if (!bnapi)
2881 return -ENOMEM;
2882
2883 bp->bnapi = bnapi;
2884 bnapi += arr_size;
2885 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2886 bp->bnapi[i] = bnapi;
2887 bp->bnapi[i]->index = i;
2888 bp->bnapi[i]->bp = bp;
2889 }
2890
Michael Chanb6ab4b02016-01-02 23:44:59 -05002891 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2892 sizeof(struct bnxt_rx_ring_info),
2893 GFP_KERNEL);
2894 if (!bp->rx_ring)
2895 return -ENOMEM;
2896
2897 for (i = 0; i < bp->rx_nr_rings; i++) {
2898 bp->rx_ring[i].bnapi = bp->bnapi[i];
2899 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2900 }
2901
2902 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2903 sizeof(struct bnxt_tx_ring_info),
2904 GFP_KERNEL);
2905 if (!bp->tx_ring)
2906 return -ENOMEM;
2907
Michael Chan01657bc2016-01-02 23:45:03 -05002908 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2909 j = 0;
2910 else
2911 j = bp->rx_nr_rings;
2912
2913 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2914 bp->tx_ring[i].bnapi = bp->bnapi[j];
2915 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002916 }
2917
Michael Chanc0c050c2015-10-22 16:01:17 -04002918 rc = bnxt_alloc_stats(bp);
2919 if (rc)
2920 goto alloc_mem_err;
2921
2922 rc = bnxt_alloc_ntp_fltrs(bp);
2923 if (rc)
2924 goto alloc_mem_err;
2925
2926 rc = bnxt_alloc_vnics(bp);
2927 if (rc)
2928 goto alloc_mem_err;
2929 }
2930
2931 bnxt_init_ring_struct(bp);
2932
2933 rc = bnxt_alloc_rx_rings(bp);
2934 if (rc)
2935 goto alloc_mem_err;
2936
2937 rc = bnxt_alloc_tx_rings(bp);
2938 if (rc)
2939 goto alloc_mem_err;
2940
2941 rc = bnxt_alloc_cp_rings(bp);
2942 if (rc)
2943 goto alloc_mem_err;
2944
2945 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2946 BNXT_VNIC_UCAST_FLAG;
2947 rc = bnxt_alloc_vnic_attributes(bp);
2948 if (rc)
2949 goto alloc_mem_err;
2950 return 0;
2951
2952alloc_mem_err:
2953 bnxt_free_mem(bp, true);
2954 return rc;
2955}
2956
Michael Chan9d8bc092016-12-29 12:13:33 -05002957static void bnxt_disable_int(struct bnxt *bp)
2958{
2959 int i;
2960
2961 if (!bp->bnapi)
2962 return;
2963
2964 for (i = 0; i < bp->cp_nr_rings; i++) {
2965 struct bnxt_napi *bnapi = bp->bnapi[i];
2966 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2967
2968 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2969 }
2970}
2971
2972static void bnxt_disable_int_sync(struct bnxt *bp)
2973{
2974 int i;
2975
2976 atomic_inc(&bp->intr_sem);
2977
2978 bnxt_disable_int(bp);
2979 for (i = 0; i < bp->cp_nr_rings; i++)
2980 synchronize_irq(bp->irq_tbl[i].vector);
2981}
2982
2983static void bnxt_enable_int(struct bnxt *bp)
2984{
2985 int i;
2986
2987 atomic_set(&bp->intr_sem, 0);
2988 for (i = 0; i < bp->cp_nr_rings; i++) {
2989 struct bnxt_napi *bnapi = bp->bnapi[i];
2990 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2991
2992 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2993 }
2994}
2995
Michael Chanc0c050c2015-10-22 16:01:17 -04002996void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2997 u16 cmpl_ring, u16 target_id)
2998{
Michael Chana8643e12016-02-26 04:00:05 -05002999 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04003000
Michael Chana8643e12016-02-26 04:00:05 -05003001 req->req_type = cpu_to_le16(req_type);
3002 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3003 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003004 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3005}
3006
Michael Chanfbfbc482016-02-26 04:00:07 -05003007static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3008 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003009{
Michael Chana11fa2b2016-05-15 03:04:47 -04003010 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003011 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003012 u32 *data = msg;
3013 __le32 *resp_len, *valid;
3014 u16 cp_ring_id, len = 0;
3015 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3016
Michael Chana8643e12016-02-26 04:00:05 -05003017 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003018 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003019 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003020 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3021
3022 /* Write request msg to hwrm channel */
3023 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3024
Michael Chane6ef2692016-03-28 19:46:05 -04003025 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003026 writel(0, bp->bar0 + i);
3027
Michael Chanc0c050c2015-10-22 16:01:17 -04003028 /* currently supports only one outstanding message */
3029 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003030 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003031
3032 /* Ring channel doorbell */
3033 writel(1, bp->bar0 + 0x100);
3034
Michael Chanff4fe812016-02-26 04:00:04 -05003035 if (!timeout)
3036 timeout = DFLT_HWRM_CMD_TIMEOUT;
3037
Michael Chanc0c050c2015-10-22 16:01:17 -04003038 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003039 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003040 if (intr_process) {
3041 /* Wait until hwrm response cmpl interrupt is processed */
3042 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003043 i++ < tmo_count) {
3044 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003045 }
3046
3047 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3048 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003049 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003050 return -1;
3051 }
3052 } else {
3053 /* Check if response len is updated */
3054 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003055 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003056 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3057 HWRM_RESP_LEN_SFT;
3058 if (len)
3059 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003060 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003061 }
3062
Michael Chana11fa2b2016-05-15 03:04:47 -04003063 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003064 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003065 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003066 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003067 return -1;
3068 }
3069
3070 /* Last word of resp contains valid bit */
3071 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003072 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003073 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3074 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003075 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003076 }
3077
Michael Chana11fa2b2016-05-15 03:04:47 -04003078 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003079 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003080 timeout, le16_to_cpu(req->req_type),
3081 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003082 return -1;
3083 }
3084 }
3085
3086 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003087 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003088 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3089 le16_to_cpu(resp->req_type),
3090 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003091 return rc;
3092}
3093
3094int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3095{
3096 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003097}
3098
3099int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3100{
3101 int rc;
3102
3103 mutex_lock(&bp->hwrm_cmd_lock);
3104 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3105 mutex_unlock(&bp->hwrm_cmd_lock);
3106 return rc;
3107}
3108
Michael Chan90e209212016-02-26 04:00:08 -05003109int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3110 int timeout)
3111{
3112 int rc;
3113
3114 mutex_lock(&bp->hwrm_cmd_lock);
3115 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3116 mutex_unlock(&bp->hwrm_cmd_lock);
3117 return rc;
3118}
3119
Michael Chana1653b12016-12-07 00:26:20 -05003120int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3121 int bmap_size)
Michael Chanc0c050c2015-10-22 16:01:17 -04003122{
3123 struct hwrm_func_drv_rgtr_input req = {0};
Michael Chan25be8622016-04-05 14:09:00 -04003124 DECLARE_BITMAP(async_events_bmap, 256);
3125 u32 *events = (u32 *)async_events_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003126 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003127
3128 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3129
3130 req.enables =
Michael Chana1653b12016-12-07 00:26:20 -05003131 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
Michael Chanc0c050c2015-10-22 16:01:17 -04003132
Michael Chan25be8622016-04-05 14:09:00 -04003133 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3134 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3135 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3136
Michael Chana1653b12016-12-07 00:26:20 -05003137 if (bmap && bmap_size) {
3138 for (i = 0; i < bmap_size; i++) {
3139 if (test_bit(i, bmap))
3140 __set_bit(i, async_events_bmap);
3141 }
3142 }
3143
Michael Chan25be8622016-04-05 14:09:00 -04003144 for (i = 0; i < 8; i++)
3145 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3146
Michael Chana1653b12016-12-07 00:26:20 -05003147 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3148}
3149
3150static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3151{
3152 struct hwrm_func_drv_rgtr_input req = {0};
3153
3154 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3155
3156 req.enables =
3157 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3158 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3159
Michael Chan11f15ed2016-04-05 14:08:55 -04003160 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003161 req.ver_maj = DRV_VER_MAJ;
3162 req.ver_min = DRV_VER_MIN;
3163 req.ver_upd = DRV_VER_UPD;
3164
3165 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003166 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003167 u32 *data = (u32 *)vf_req_snif_bmap;
Michael Chana1653b12016-12-07 00:26:20 -05003168 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003169
Michael Chande68f5de2015-12-09 19:35:41 -05003170 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003171 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3172 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3173
Michael Chande68f5de2015-12-09 19:35:41 -05003174 for (i = 0; i < 8; i++)
3175 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3176
Michael Chanc0c050c2015-10-22 16:01:17 -04003177 req.enables |=
3178 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3179 }
3180
3181 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3182}
3183
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003184static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3185{
3186 struct hwrm_func_drv_unrgtr_input req = {0};
3187
3188 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3189 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3190}
3191
Michael Chanc0c050c2015-10-22 16:01:17 -04003192static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3193{
3194 u32 rc = 0;
3195 struct hwrm_tunnel_dst_port_free_input req = {0};
3196
3197 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3198 req.tunnel_type = tunnel_type;
3199
3200 switch (tunnel_type) {
3201 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3202 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3203 break;
3204 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3205 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3206 break;
3207 default:
3208 break;
3209 }
3210
3211 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3212 if (rc)
3213 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3214 rc);
3215 return rc;
3216}
3217
3218static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3219 u8 tunnel_type)
3220{
3221 u32 rc = 0;
3222 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3223 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3224
3225 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3226
3227 req.tunnel_type = tunnel_type;
3228 req.tunnel_dst_port_val = port;
3229
3230 mutex_lock(&bp->hwrm_cmd_lock);
3231 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3232 if (rc) {
3233 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3234 rc);
3235 goto err_out;
3236 }
3237
Christophe Jaillet57aac712016-11-22 06:14:40 +01003238 switch (tunnel_type) {
3239 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
Michael Chanc0c050c2015-10-22 16:01:17 -04003240 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003241 break;
3242 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
Michael Chanc0c050c2015-10-22 16:01:17 -04003243 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
Christophe Jaillet57aac712016-11-22 06:14:40 +01003244 break;
3245 default:
3246 break;
3247 }
3248
Michael Chanc0c050c2015-10-22 16:01:17 -04003249err_out:
3250 mutex_unlock(&bp->hwrm_cmd_lock);
3251 return rc;
3252}
3253
3254static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3255{
3256 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3257 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3258
3259 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003260 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003261
3262 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3263 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3264 req.mask = cpu_to_le32(vnic->rx_mask);
3265 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3266}
3267
3268#ifdef CONFIG_RFS_ACCEL
3269static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3270 struct bnxt_ntuple_filter *fltr)
3271{
3272 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3273
3274 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3275 req.ntuple_filter_id = fltr->filter_id;
3276 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3277}
3278
3279#define BNXT_NTP_FLTR_FLAGS \
3280 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3281 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3282 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3283 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3284 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3285 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3286 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3287 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3288 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3289 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3290 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3291 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3292 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003293 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003294
3295static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3296 struct bnxt_ntuple_filter *fltr)
3297{
3298 int rc = 0;
3299 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3300 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3301 bp->hwrm_cmd_resp_addr;
3302 struct flow_keys *keys = &fltr->fkeys;
3303 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3304
3305 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003306 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003307
3308 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3309
3310 req.ethertype = htons(ETH_P_IP);
3311 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003312 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003313 req.ip_protocol = keys->basic.ip_proto;
3314
3315 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3316 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3317 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3318 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3319
3320 req.src_port = keys->ports.src;
3321 req.src_port_mask = cpu_to_be16(0xffff);
3322 req.dst_port = keys->ports.dst;
3323 req.dst_port_mask = cpu_to_be16(0xffff);
3324
Michael Chanc1935542015-12-27 18:19:28 -05003325 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003326 mutex_lock(&bp->hwrm_cmd_lock);
3327 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3328 if (!rc)
3329 fltr->filter_id = resp->ntuple_filter_id;
3330 mutex_unlock(&bp->hwrm_cmd_lock);
3331 return rc;
3332}
3333#endif
3334
3335static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3336 u8 *mac_addr)
3337{
3338 u32 rc = 0;
3339 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3340 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3341
3342 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003343 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3344 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3345 req.flags |=
3346 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003347 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003348 req.enables =
3349 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003350 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003351 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3352 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3353 req.l2_addr_mask[0] = 0xff;
3354 req.l2_addr_mask[1] = 0xff;
3355 req.l2_addr_mask[2] = 0xff;
3356 req.l2_addr_mask[3] = 0xff;
3357 req.l2_addr_mask[4] = 0xff;
3358 req.l2_addr_mask[5] = 0xff;
3359
3360 mutex_lock(&bp->hwrm_cmd_lock);
3361 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3362 if (!rc)
3363 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3364 resp->l2_filter_id;
3365 mutex_unlock(&bp->hwrm_cmd_lock);
3366 return rc;
3367}
3368
3369static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3370{
3371 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3372 int rc = 0;
3373
3374 /* Any associated ntuple filters will also be cleared by firmware. */
3375 mutex_lock(&bp->hwrm_cmd_lock);
3376 for (i = 0; i < num_of_vnics; i++) {
3377 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3378
3379 for (j = 0; j < vnic->uc_filter_count; j++) {
3380 struct hwrm_cfa_l2_filter_free_input req = {0};
3381
3382 bnxt_hwrm_cmd_hdr_init(bp, &req,
3383 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3384
3385 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3386
3387 rc = _hwrm_send_message(bp, &req, sizeof(req),
3388 HWRM_CMD_TIMEOUT);
3389 }
3390 vnic->uc_filter_count = 0;
3391 }
3392 mutex_unlock(&bp->hwrm_cmd_lock);
3393
3394 return rc;
3395}
3396
3397static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3398{
3399 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3400 struct hwrm_vnic_tpa_cfg_input req = {0};
3401
3402 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3403
3404 if (tpa_flags) {
3405 u16 mss = bp->dev->mtu - 40;
3406 u32 nsegs, n, segs = 0, flags;
3407
3408 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3409 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3410 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3411 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3412 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3413 if (tpa_flags & BNXT_FLAG_GRO)
3414 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3415
3416 req.flags = cpu_to_le32(flags);
3417
3418 req.enables =
3419 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003420 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3421 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003422
3423 /* Number of segs are log2 units, and first packet is not
3424 * included as part of this units.
3425 */
Michael Chan2839f282016-04-25 02:30:50 -04003426 if (mss <= BNXT_RX_PAGE_SIZE) {
3427 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003428 nsegs = (MAX_SKB_FRAGS - 1) * n;
3429 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003430 n = mss / BNXT_RX_PAGE_SIZE;
3431 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003432 n++;
3433 nsegs = (MAX_SKB_FRAGS - n) / n;
3434 }
3435
3436 segs = ilog2(nsegs);
3437 req.max_agg_segs = cpu_to_le16(segs);
3438 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003439
3440 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003441 }
3442 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3443
3444 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3445}
3446
3447static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3448{
3449 u32 i, j, max_rings;
3450 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3451 struct hwrm_vnic_rss_cfg_input req = {0};
3452
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003453 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003454 return 0;
3455
3456 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3457 if (set_rss) {
Michael Chan87da7f72016-11-16 21:13:09 -05003458 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003459 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3460 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3461 max_rings = bp->rx_nr_rings - 1;
3462 else
3463 max_rings = bp->rx_nr_rings;
3464 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003465 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003466 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003467
3468 /* Fill the RSS indirection table with ring group ids */
3469 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3470 if (j == max_rings)
3471 j = 0;
3472 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3473 }
3474
3475 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3476 req.hash_key_tbl_addr =
3477 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3478 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003479 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003480 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3481}
3482
3483static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3484{
3485 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3486 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3487
3488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3489 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3490 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3491 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3492 req.enables =
3493 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3494 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3495 /* thresholds not implemented in firmware yet */
3496 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3497 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3498 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3499 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3500}
3501
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003502static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3503 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003504{
3505 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3506
3507 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3508 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003509 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003510
3511 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003512 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003513}
3514
3515static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3516{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003517 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003518
3519 for (i = 0; i < bp->nr_vnics; i++) {
3520 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3521
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003522 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3523 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3524 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3525 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003526 }
3527 bp->rsscos_nr_ctxs = 0;
3528}
3529
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003530static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003531{
3532 int rc;
3533 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3534 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3535 bp->hwrm_cmd_resp_addr;
3536
3537 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3538 -1);
3539
3540 mutex_lock(&bp->hwrm_cmd_lock);
3541 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3542 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003543 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003544 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3545 mutex_unlock(&bp->hwrm_cmd_lock);
3546
3547 return rc;
3548}
3549
Michael Chana588e452016-12-07 00:26:21 -05003550int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
Michael Chanc0c050c2015-10-22 16:01:17 -04003551{
Michael Chanb81a90d2016-01-02 23:45:01 -05003552 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003553 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3554 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003555 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003556
3557 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003558
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003559 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3560 /* Only RSS support for now TBD: COS & LB */
3561 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3562 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3563 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3564 VNIC_CFG_REQ_ENABLES_MRU);
3565 } else {
3566 req.rss_rule = cpu_to_le16(0xffff);
3567 }
3568
3569 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3570 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003571 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3572 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3573 } else {
3574 req.cos_rule = cpu_to_le16(0xffff);
3575 }
3576
Michael Chanc0c050c2015-10-22 16:01:17 -04003577 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003578 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003579 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003580 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003581 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3582 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003583
Michael Chanb81a90d2016-01-02 23:45:01 -05003584 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003585 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3586 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3587
3588 req.lb_rule = cpu_to_le16(0xffff);
3589 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3590 VLAN_HLEN);
3591
Michael Chancf6645f2016-06-13 02:25:28 -04003592#ifdef CONFIG_BNXT_SRIOV
3593 if (BNXT_VF(bp))
3594 def_vlan = bp->vf.vlan;
3595#endif
3596 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003597 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
Michael Chana588e452016-12-07 00:26:21 -05003598 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3599 req.flags |=
3600 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
Michael Chanc0c050c2015-10-22 16:01:17 -04003601
3602 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3603}
3604
3605static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3606{
3607 u32 rc = 0;
3608
3609 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3610 struct hwrm_vnic_free_input req = {0};
3611
3612 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3613 req.vnic_id =
3614 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3615
3616 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3617 if (rc)
3618 return rc;
3619 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3620 }
3621 return rc;
3622}
3623
3624static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3625{
3626 u16 i;
3627
3628 for (i = 0; i < bp->nr_vnics; i++)
3629 bnxt_hwrm_vnic_free_one(bp, i);
3630}
3631
Michael Chanb81a90d2016-01-02 23:45:01 -05003632static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3633 unsigned int start_rx_ring_idx,
3634 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003635{
Michael Chanb81a90d2016-01-02 23:45:01 -05003636 int rc = 0;
3637 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003638 struct hwrm_vnic_alloc_input req = {0};
3639 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3640
3641 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003642 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3643 grp_idx = bp->rx_ring[i].bnapi->index;
3644 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003645 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003646 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003647 break;
3648 }
3649 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003650 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003651 }
3652
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003653 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3654 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003655 if (vnic_id == 0)
3656 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3657
3658 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3659
3660 mutex_lock(&bp->hwrm_cmd_lock);
3661 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3662 if (!rc)
3663 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3664 mutex_unlock(&bp->hwrm_cmd_lock);
3665 return rc;
3666}
3667
Michael Chan8fdefd62016-12-29 12:13:36 -05003668static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3669{
3670 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3671 struct hwrm_vnic_qcaps_input req = {0};
3672 int rc;
3673
3674 if (bp->hwrm_spec_code < 0x10600)
3675 return 0;
3676
3677 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3678 mutex_lock(&bp->hwrm_cmd_lock);
3679 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3680 if (!rc) {
3681 if (resp->flags &
3682 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3683 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3684 }
3685 mutex_unlock(&bp->hwrm_cmd_lock);
3686 return rc;
3687}
3688
Michael Chanc0c050c2015-10-22 16:01:17 -04003689static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3690{
3691 u16 i;
3692 u32 rc = 0;
3693
3694 mutex_lock(&bp->hwrm_cmd_lock);
3695 for (i = 0; i < bp->rx_nr_rings; i++) {
3696 struct hwrm_ring_grp_alloc_input req = {0};
3697 struct hwrm_ring_grp_alloc_output *resp =
3698 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003699 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003700
3701 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3702
Michael Chanb81a90d2016-01-02 23:45:01 -05003703 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3704 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3705 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3706 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003707
3708 rc = _hwrm_send_message(bp, &req, sizeof(req),
3709 HWRM_CMD_TIMEOUT);
3710 if (rc)
3711 break;
3712
Michael Chanb81a90d2016-01-02 23:45:01 -05003713 bp->grp_info[grp_idx].fw_grp_id =
3714 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003715 }
3716 mutex_unlock(&bp->hwrm_cmd_lock);
3717 return rc;
3718}
3719
3720static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3721{
3722 u16 i;
3723 u32 rc = 0;
3724 struct hwrm_ring_grp_free_input req = {0};
3725
3726 if (!bp->grp_info)
3727 return 0;
3728
3729 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3730
3731 mutex_lock(&bp->hwrm_cmd_lock);
3732 for (i = 0; i < bp->cp_nr_rings; i++) {
3733 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3734 continue;
3735 req.ring_group_id =
3736 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3737
3738 rc = _hwrm_send_message(bp, &req, sizeof(req),
3739 HWRM_CMD_TIMEOUT);
3740 if (rc)
3741 break;
3742 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3743 }
3744 mutex_unlock(&bp->hwrm_cmd_lock);
3745 return rc;
3746}
3747
3748static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3749 struct bnxt_ring_struct *ring,
3750 u32 ring_type, u32 map_index,
3751 u32 stats_ctx_id)
3752{
3753 int rc = 0, err = 0;
3754 struct hwrm_ring_alloc_input req = {0};
3755 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3756 u16 ring_id;
3757
3758 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3759
3760 req.enables = 0;
3761 if (ring->nr_pages > 1) {
3762 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3763 /* Page size is in log2 units */
3764 req.page_size = BNXT_PAGE_SHIFT;
3765 req.page_tbl_depth = 1;
3766 } else {
3767 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3768 }
3769 req.fbo = 0;
3770 /* Association of ring index with doorbell index and MSIX number */
3771 req.logical_id = cpu_to_le16(map_index);
3772
3773 switch (ring_type) {
3774 case HWRM_RING_ALLOC_TX:
3775 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3776 /* Association of transmit ring with completion ring */
3777 req.cmpl_ring_id =
3778 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3779 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3780 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3781 req.queue_id = cpu_to_le16(ring->queue_id);
3782 break;
3783 case HWRM_RING_ALLOC_RX:
3784 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3785 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3786 break;
3787 case HWRM_RING_ALLOC_AGG:
3788 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3789 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3790 break;
3791 case HWRM_RING_ALLOC_CMPL:
3792 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3793 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3794 if (bp->flags & BNXT_FLAG_USING_MSIX)
3795 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3796 break;
3797 default:
3798 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3799 ring_type);
3800 return -1;
3801 }
3802
3803 mutex_lock(&bp->hwrm_cmd_lock);
3804 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3805 err = le16_to_cpu(resp->error_code);
3806 ring_id = le16_to_cpu(resp->ring_id);
3807 mutex_unlock(&bp->hwrm_cmd_lock);
3808
3809 if (rc || err) {
3810 switch (ring_type) {
3811 case RING_FREE_REQ_RING_TYPE_CMPL:
3812 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3813 rc, err);
3814 return -1;
3815
3816 case RING_FREE_REQ_RING_TYPE_RX:
3817 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3818 rc, err);
3819 return -1;
3820
3821 case RING_FREE_REQ_RING_TYPE_TX:
3822 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3823 rc, err);
3824 return -1;
3825
3826 default:
3827 netdev_err(bp->dev, "Invalid ring\n");
3828 return -1;
3829 }
3830 }
3831 ring->fw_ring_id = ring_id;
3832 return rc;
3833}
3834
3835static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3836{
3837 int i, rc = 0;
3838
Michael Chanedd0c2c2015-12-27 18:19:19 -05003839 for (i = 0; i < bp->cp_nr_rings; i++) {
3840 struct bnxt_napi *bnapi = bp->bnapi[i];
3841 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3842 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003843
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003844 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003845 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3846 INVALID_STATS_CTX_ID);
3847 if (rc)
3848 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003849 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3850 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003851 }
3852
Michael Chanedd0c2c2015-12-27 18:19:19 -05003853 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003854 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003855 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003856 u32 map_idx = txr->bnapi->index;
3857 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003858
Michael Chanb81a90d2016-01-02 23:45:01 -05003859 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3860 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003861 if (rc)
3862 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003863 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003864 }
3865
Michael Chanedd0c2c2015-12-27 18:19:19 -05003866 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003867 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003868 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003869 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003870
Michael Chanb81a90d2016-01-02 23:45:01 -05003871 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3872 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003873 if (rc)
3874 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003875 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003876 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003877 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003878 }
3879
3880 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3881 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003882 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003883 struct bnxt_ring_struct *ring =
3884 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003885 u32 grp_idx = rxr->bnapi->index;
3886 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003887
3888 rc = hwrm_ring_alloc_send_msg(bp, ring,
3889 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003890 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003891 INVALID_STATS_CTX_ID);
3892 if (rc)
3893 goto err_out;
3894
Michael Chanb81a90d2016-01-02 23:45:01 -05003895 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003896 writel(DB_KEY_RX | rxr->rx_agg_prod,
3897 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003898 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003899 }
3900 }
3901err_out:
3902 return rc;
3903}
3904
3905static int hwrm_ring_free_send_msg(struct bnxt *bp,
3906 struct bnxt_ring_struct *ring,
3907 u32 ring_type, int cmpl_ring_id)
3908{
3909 int rc;
3910 struct hwrm_ring_free_input req = {0};
3911 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3912 u16 error_code;
3913
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003914 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003915 req.ring_type = ring_type;
3916 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3917
3918 mutex_lock(&bp->hwrm_cmd_lock);
3919 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3920 error_code = le16_to_cpu(resp->error_code);
3921 mutex_unlock(&bp->hwrm_cmd_lock);
3922
3923 if (rc || error_code) {
3924 switch (ring_type) {
3925 case RING_FREE_REQ_RING_TYPE_CMPL:
3926 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3927 rc);
3928 return rc;
3929 case RING_FREE_REQ_RING_TYPE_RX:
3930 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3931 rc);
3932 return rc;
3933 case RING_FREE_REQ_RING_TYPE_TX:
3934 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3935 rc);
3936 return rc;
3937 default:
3938 netdev_err(bp->dev, "Invalid ring\n");
3939 return -1;
3940 }
3941 }
3942 return 0;
3943}
3944
Michael Chanedd0c2c2015-12-27 18:19:19 -05003945static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003946{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003947 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003948
3949 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003950 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003951
Michael Chanedd0c2c2015-12-27 18:19:19 -05003952 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003953 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003954 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003955 u32 grp_idx = txr->bnapi->index;
3956 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003957
Michael Chanedd0c2c2015-12-27 18:19:19 -05003958 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3959 hwrm_ring_free_send_msg(bp, ring,
3960 RING_FREE_REQ_RING_TYPE_TX,
3961 close_path ? cmpl_ring_id :
3962 INVALID_HW_RING_ID);
3963 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003964 }
3965 }
3966
Michael Chanedd0c2c2015-12-27 18:19:19 -05003967 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003968 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003969 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003970 u32 grp_idx = rxr->bnapi->index;
3971 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003972
Michael Chanedd0c2c2015-12-27 18:19:19 -05003973 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3974 hwrm_ring_free_send_msg(bp, ring,
3975 RING_FREE_REQ_RING_TYPE_RX,
3976 close_path ? cmpl_ring_id :
3977 INVALID_HW_RING_ID);
3978 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003979 bp->grp_info[grp_idx].rx_fw_ring_id =
3980 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003981 }
3982 }
3983
Michael Chanedd0c2c2015-12-27 18:19:19 -05003984 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003985 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003986 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003987 u32 grp_idx = rxr->bnapi->index;
3988 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003989
Michael Chanedd0c2c2015-12-27 18:19:19 -05003990 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3991 hwrm_ring_free_send_msg(bp, ring,
3992 RING_FREE_REQ_RING_TYPE_RX,
3993 close_path ? cmpl_ring_id :
3994 INVALID_HW_RING_ID);
3995 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003996 bp->grp_info[grp_idx].agg_fw_ring_id =
3997 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003998 }
3999 }
4000
Michael Chan9d8bc092016-12-29 12:13:33 -05004001 /* The completion rings are about to be freed. After that the
4002 * IRQ doorbell will not work anymore. So we need to disable
4003 * IRQ here.
4004 */
4005 bnxt_disable_int_sync(bp);
4006
Michael Chanedd0c2c2015-12-27 18:19:19 -05004007 for (i = 0; i < bp->cp_nr_rings; i++) {
4008 struct bnxt_napi *bnapi = bp->bnapi[i];
4009 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4010 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04004011
Michael Chanedd0c2c2015-12-27 18:19:19 -05004012 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4013 hwrm_ring_free_send_msg(bp, ring,
4014 RING_FREE_REQ_RING_TYPE_CMPL,
4015 INVALID_HW_RING_ID);
4016 ring->fw_ring_id = INVALID_HW_RING_ID;
4017 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04004018 }
4019 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004020}
4021
Michael Chanbb053f52016-02-26 04:00:02 -05004022static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4023 u32 buf_tmrs, u16 flags,
4024 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4025{
4026 req->flags = cpu_to_le16(flags);
4027 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4028 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4029 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4030 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4031 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4032 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4033 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4034 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4035}
4036
Michael Chanc0c050c2015-10-22 16:01:17 -04004037int bnxt_hwrm_set_coal(struct bnxt *bp)
4038{
4039 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05004040 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4041 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04004042 u16 max_buf, max_buf_irq;
4043 u16 buf_tmr, buf_tmr_irq;
4044 u32 flags;
4045
Michael Chandfc9c942016-02-26 04:00:03 -05004046 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4047 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4048 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4049 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004050
Michael Chandfb5b892016-02-26 04:00:01 -05004051 /* Each rx completion (2 records) should be DMAed immediately.
4052 * DMA 1/4 of the completion buffers at a time.
4053 */
4054 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004055 /* max_buf must not be zero */
4056 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004057 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4058 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4059 /* buf timer set to 1/4 of interrupt timer */
4060 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4061 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4062 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004063
4064 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4065
4066 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4067 * if coal_ticks is less than 25 us.
4068 */
Michael Chandfb5b892016-02-26 04:00:01 -05004069 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004070 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4071
Michael Chanbb053f52016-02-26 04:00:02 -05004072 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004073 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4074
4075 /* max_buf must not be zero */
4076 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4077 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4078 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4079 /* buf timer set to 1/4 of interrupt timer */
4080 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4081 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4082 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4083
4084 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4085 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4086 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004087
4088 mutex_lock(&bp->hwrm_cmd_lock);
4089 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004090 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004091
Michael Chandfc9c942016-02-26 04:00:03 -05004092 req = &req_rx;
4093 if (!bnapi->rx_ring)
4094 req = &req_tx;
4095 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4096
4097 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004098 HWRM_CMD_TIMEOUT);
4099 if (rc)
4100 break;
4101 }
4102 mutex_unlock(&bp->hwrm_cmd_lock);
4103 return rc;
4104}
4105
4106static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4107{
4108 int rc = 0, i;
4109 struct hwrm_stat_ctx_free_input req = {0};
4110
4111 if (!bp->bnapi)
4112 return 0;
4113
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004114 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4115 return 0;
4116
Michael Chanc0c050c2015-10-22 16:01:17 -04004117 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4118
4119 mutex_lock(&bp->hwrm_cmd_lock);
4120 for (i = 0; i < bp->cp_nr_rings; i++) {
4121 struct bnxt_napi *bnapi = bp->bnapi[i];
4122 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4123
4124 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4125 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4126
4127 rc = _hwrm_send_message(bp, &req, sizeof(req),
4128 HWRM_CMD_TIMEOUT);
4129 if (rc)
4130 break;
4131
4132 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4133 }
4134 }
4135 mutex_unlock(&bp->hwrm_cmd_lock);
4136 return rc;
4137}
4138
4139static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4140{
4141 int rc = 0, i;
4142 struct hwrm_stat_ctx_alloc_input req = {0};
4143 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4144
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004145 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4146 return 0;
4147
Michael Chanc0c050c2015-10-22 16:01:17 -04004148 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4149
Michael Chan51f30782016-07-01 18:46:29 -04004150 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004151
4152 mutex_lock(&bp->hwrm_cmd_lock);
4153 for (i = 0; i < bp->cp_nr_rings; i++) {
4154 struct bnxt_napi *bnapi = bp->bnapi[i];
4155 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4156
4157 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4158
4159 rc = _hwrm_send_message(bp, &req, sizeof(req),
4160 HWRM_CMD_TIMEOUT);
4161 if (rc)
4162 break;
4163
4164 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4165
4166 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4167 }
4168 mutex_unlock(&bp->hwrm_cmd_lock);
Pan Bian89aa8442016-12-03 17:56:17 +08004169 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04004170}
4171
Michael Chancf6645f2016-06-13 02:25:28 -04004172static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4173{
4174 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004175 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004176 int rc;
4177
4178 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4179 req.fid = cpu_to_le16(0xffff);
4180 mutex_lock(&bp->hwrm_cmd_lock);
4181 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4182 if (rc)
4183 goto func_qcfg_exit;
4184
4185#ifdef CONFIG_BNXT_SRIOV
4186 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004187 struct bnxt_vf_info *vf = &bp->vf;
4188
4189 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4190 }
4191#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004192 switch (resp->port_partition_type) {
4193 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4194 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4195 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4196 bp->port_partition_type = resp->port_partition_type;
4197 break;
4198 }
Michael Chancf6645f2016-06-13 02:25:28 -04004199
4200func_qcfg_exit:
4201 mutex_unlock(&bp->hwrm_cmd_lock);
4202 return rc;
4203}
4204
Michael Chan7b08f662016-12-07 00:26:18 -05004205static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004206{
4207 int rc = 0;
4208 struct hwrm_func_qcaps_input req = {0};
4209 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4210
4211 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4212 req.fid = cpu_to_le16(0xffff);
4213
4214 mutex_lock(&bp->hwrm_cmd_lock);
4215 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4216 if (rc)
4217 goto hwrm_func_qcaps_exit;
4218
Michael Chane4060d32016-12-07 00:26:19 -05004219 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4220 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4221 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4222 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4223
Michael Chan7cc5a202016-09-19 03:58:05 -04004224 bp->tx_push_thresh = 0;
4225 if (resp->flags &
4226 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4227 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4228
Michael Chanc0c050c2015-10-22 16:01:17 -04004229 if (BNXT_PF(bp)) {
4230 struct bnxt_pf_info *pf = &bp->pf;
4231
4232 pf->fw_fid = le16_to_cpu(resp->fid);
4233 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004234 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004235 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004236 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004237 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4238 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4239 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004240 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004241 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4242 if (!pf->max_hw_ring_grps)
4243 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004244 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4245 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4246 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4247 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4248 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4249 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4250 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4251 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4252 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4253 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4254 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4255 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004256#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004257 struct bnxt_vf_info *vf = &bp->vf;
4258
4259 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004260
4261 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4262 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4263 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4264 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004265 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4266 if (!vf->max_hw_ring_grps)
4267 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004268 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4269 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4270 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004271
4272 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004273 mutex_unlock(&bp->hwrm_cmd_lock);
4274
4275 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004276 /* overwrite netdev dev_adr with admin VF MAC */
4277 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004278 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004279 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004280 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4281 }
4282 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004283#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004284 }
4285
Michael Chanc0c050c2015-10-22 16:01:17 -04004286hwrm_func_qcaps_exit:
4287 mutex_unlock(&bp->hwrm_cmd_lock);
4288 return rc;
4289}
4290
4291static int bnxt_hwrm_func_reset(struct bnxt *bp)
4292{
4293 struct hwrm_func_reset_input req = {0};
4294
4295 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4296 req.enables = 0;
4297
4298 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4299}
4300
4301static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4302{
4303 int rc = 0;
4304 struct hwrm_queue_qportcfg_input req = {0};
4305 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4306 u8 i, *qptr;
4307
4308 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4309
4310 mutex_lock(&bp->hwrm_cmd_lock);
4311 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4312 if (rc)
4313 goto qportcfg_exit;
4314
4315 if (!resp->max_configurable_queues) {
4316 rc = -EINVAL;
4317 goto qportcfg_exit;
4318 }
4319 bp->max_tc = resp->max_configurable_queues;
Michael Chan87c374d2016-12-02 21:17:16 -05004320 bp->max_lltc = resp->max_configurable_lossless_queues;
Michael Chanc0c050c2015-10-22 16:01:17 -04004321 if (bp->max_tc > BNXT_MAX_QUEUE)
4322 bp->max_tc = BNXT_MAX_QUEUE;
4323
Michael Chan441cabb2016-09-19 03:58:02 -04004324 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4325 bp->max_tc = 1;
4326
Michael Chan87c374d2016-12-02 21:17:16 -05004327 if (bp->max_lltc > bp->max_tc)
4328 bp->max_lltc = bp->max_tc;
4329
Michael Chanc0c050c2015-10-22 16:01:17 -04004330 qptr = &resp->queue_id0;
4331 for (i = 0; i < bp->max_tc; i++) {
4332 bp->q_info[i].queue_id = *qptr++;
4333 bp->q_info[i].queue_profile = *qptr++;
4334 }
4335
4336qportcfg_exit:
4337 mutex_unlock(&bp->hwrm_cmd_lock);
4338 return rc;
4339}
4340
4341static int bnxt_hwrm_ver_get(struct bnxt *bp)
4342{
4343 int rc;
4344 struct hwrm_ver_get_input req = {0};
4345 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4346
Michael Chane6ef2692016-03-28 19:46:05 -04004347 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004348 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4349 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4350 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4351 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4352 mutex_lock(&bp->hwrm_cmd_lock);
4353 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4354 if (rc)
4355 goto hwrm_ver_get_exit;
4356
4357 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4358
Michael Chan11f15ed2016-04-05 14:08:55 -04004359 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4360 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004361 if (resp->hwrm_intf_maj < 1) {
4362 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004363 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004364 resp->hwrm_intf_upd);
4365 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004366 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004367 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004368 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4369 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4370
Michael Chanff4fe812016-02-26 04:00:04 -05004371 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4372 if (!bp->hwrm_cmd_timeout)
4373 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4374
Michael Chane6ef2692016-03-28 19:46:05 -04004375 if (resp->hwrm_intf_maj >= 1)
4376 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4377
Michael Chan659c8052016-06-13 02:25:33 -04004378 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004379 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4380 !resp->chip_metal)
4381 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004382
Michael Chanc0c050c2015-10-22 16:01:17 -04004383hwrm_ver_get_exit:
4384 mutex_unlock(&bp->hwrm_cmd_lock);
4385 return rc;
4386}
4387
Rob Swindell5ac67d82016-09-19 03:58:03 -04004388int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4389{
Rob Swindell878786d2016-09-20 03:36:33 -04004390#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004391 struct hwrm_fw_set_time_input req = {0};
4392 struct rtc_time tm;
4393 struct timeval tv;
4394
4395 if (bp->hwrm_spec_code < 0x10400)
4396 return -EOPNOTSUPP;
4397
4398 do_gettimeofday(&tv);
4399 rtc_time_to_tm(tv.tv_sec, &tm);
4400 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4401 req.year = cpu_to_le16(1900 + tm.tm_year);
4402 req.month = 1 + tm.tm_mon;
4403 req.day = tm.tm_mday;
4404 req.hour = tm.tm_hour;
4405 req.minute = tm.tm_min;
4406 req.second = tm.tm_sec;
4407 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004408#else
4409 return -EOPNOTSUPP;
4410#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004411}
4412
Michael Chan3bdf56c2016-03-07 15:38:45 -05004413static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4414{
4415 int rc;
4416 struct bnxt_pf_info *pf = &bp->pf;
4417 struct hwrm_port_qstats_input req = {0};
4418
4419 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4420 return 0;
4421
4422 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4423 req.port_id = cpu_to_le16(pf->port_id);
4424 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4425 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4426 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4427 return rc;
4428}
4429
Michael Chanc0c050c2015-10-22 16:01:17 -04004430static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4431{
4432 if (bp->vxlan_port_cnt) {
4433 bnxt_hwrm_tunnel_dst_port_free(
4434 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4435 }
4436 bp->vxlan_port_cnt = 0;
4437 if (bp->nge_port_cnt) {
4438 bnxt_hwrm_tunnel_dst_port_free(
4439 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4440 }
4441 bp->nge_port_cnt = 0;
4442}
4443
4444static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4445{
4446 int rc, i;
4447 u32 tpa_flags = 0;
4448
4449 if (set_tpa)
4450 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4451 for (i = 0; i < bp->nr_vnics; i++) {
4452 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4453 if (rc) {
4454 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4455 rc, i);
4456 return rc;
4457 }
4458 }
4459 return 0;
4460}
4461
4462static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4463{
4464 int i;
4465
4466 for (i = 0; i < bp->nr_vnics; i++)
4467 bnxt_hwrm_vnic_set_rss(bp, i, false);
4468}
4469
4470static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4471 bool irq_re_init)
4472{
4473 if (bp->vnic_info) {
4474 bnxt_hwrm_clear_vnic_filter(bp);
4475 /* clear all RSS setting before free vnic ctx */
4476 bnxt_hwrm_clear_vnic_rss(bp);
4477 bnxt_hwrm_vnic_ctx_free(bp);
4478 /* before free the vnic, undo the vnic tpa settings */
4479 if (bp->flags & BNXT_FLAG_TPA)
4480 bnxt_set_tpa(bp, false);
4481 bnxt_hwrm_vnic_free(bp);
4482 }
4483 bnxt_hwrm_ring_free(bp, close_path);
4484 bnxt_hwrm_ring_grp_free(bp);
4485 if (irq_re_init) {
4486 bnxt_hwrm_stat_ctx_free(bp);
4487 bnxt_hwrm_free_tunnel_ports(bp);
4488 }
4489}
4490
4491static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4492{
4493 int rc;
4494
4495 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004496 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004497 if (rc) {
4498 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4499 vnic_id, rc);
4500 goto vnic_setup_err;
4501 }
4502 bp->rsscos_nr_ctxs++;
4503
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004504 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4505 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4506 if (rc) {
4507 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4508 vnic_id, rc);
4509 goto vnic_setup_err;
4510 }
4511 bp->rsscos_nr_ctxs++;
4512 }
4513
Michael Chanc0c050c2015-10-22 16:01:17 -04004514 /* configure default vnic, ring grp */
4515 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4516 if (rc) {
4517 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4518 vnic_id, rc);
4519 goto vnic_setup_err;
4520 }
4521
4522 /* Enable RSS hashing on vnic */
4523 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4524 if (rc) {
4525 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4526 vnic_id, rc);
4527 goto vnic_setup_err;
4528 }
4529
4530 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4531 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4532 if (rc) {
4533 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4534 vnic_id, rc);
4535 }
4536 }
4537
4538vnic_setup_err:
4539 return rc;
4540}
4541
4542static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4543{
4544#ifdef CONFIG_RFS_ACCEL
4545 int i, rc = 0;
4546
4547 for (i = 0; i < bp->rx_nr_rings; i++) {
4548 u16 vnic_id = i + 1;
4549 u16 ring_id = i;
4550
4551 if (vnic_id >= bp->nr_vnics)
4552 break;
4553
4554 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004555 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004556 if (rc) {
4557 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4558 vnic_id, rc);
4559 break;
4560 }
4561 rc = bnxt_setup_vnic(bp, vnic_id);
4562 if (rc)
4563 break;
4564 }
4565 return rc;
4566#else
4567 return 0;
4568#endif
4569}
4570
Michael Chan17c71ac2016-07-01 18:46:27 -04004571/* Allow PF and VF with default VLAN to be in promiscuous mode */
4572static bool bnxt_promisc_ok(struct bnxt *bp)
4573{
4574#ifdef CONFIG_BNXT_SRIOV
4575 if (BNXT_VF(bp) && !bp->vf.vlan)
4576 return false;
4577#endif
4578 return true;
4579}
4580
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004581static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4582{
4583 unsigned int rc = 0;
4584
4585 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4586 if (rc) {
4587 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4588 rc);
4589 return rc;
4590 }
4591
4592 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4593 if (rc) {
4594 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4595 rc);
4596 return rc;
4597 }
4598 return rc;
4599}
4600
Michael Chanb664f002015-12-02 01:54:08 -05004601static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004602static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004603
Michael Chanc0c050c2015-10-22 16:01:17 -04004604static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4605{
Michael Chan7d2837d2016-05-04 16:56:44 -04004606 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004607 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004608 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004609
4610 if (irq_re_init) {
4611 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4612 if (rc) {
4613 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4614 rc);
4615 goto err_out;
4616 }
4617 }
4618
4619 rc = bnxt_hwrm_ring_alloc(bp);
4620 if (rc) {
4621 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4622 goto err_out;
4623 }
4624
4625 rc = bnxt_hwrm_ring_grp_alloc(bp);
4626 if (rc) {
4627 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4628 goto err_out;
4629 }
4630
Prashant Sreedharan76595192016-07-18 07:15:22 -04004631 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4632 rx_nr_rings--;
4633
Michael Chanc0c050c2015-10-22 16:01:17 -04004634 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004635 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004636 if (rc) {
4637 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4638 goto err_out;
4639 }
4640
4641 rc = bnxt_setup_vnic(bp, 0);
4642 if (rc)
4643 goto err_out;
4644
4645 if (bp->flags & BNXT_FLAG_RFS) {
4646 rc = bnxt_alloc_rfs_vnics(bp);
4647 if (rc)
4648 goto err_out;
4649 }
4650
4651 if (bp->flags & BNXT_FLAG_TPA) {
4652 rc = bnxt_set_tpa(bp, true);
4653 if (rc)
4654 goto err_out;
4655 }
4656
4657 if (BNXT_VF(bp))
4658 bnxt_update_vf_mac(bp);
4659
4660 /* Filter for default vnic 0 */
4661 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4662 if (rc) {
4663 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4664 goto err_out;
4665 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004666 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004667
Michael Chan7d2837d2016-05-04 16:56:44 -04004668 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004669
Michael Chan17c71ac2016-07-01 18:46:27 -04004670 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004671 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4672
4673 if (bp->dev->flags & IFF_ALLMULTI) {
4674 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4675 vnic->mc_list_count = 0;
4676 } else {
4677 u32 mask = 0;
4678
4679 bnxt_mc_list_updated(bp, &mask);
4680 vnic->rx_mask |= mask;
4681 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004682
Michael Chanb664f002015-12-02 01:54:08 -05004683 rc = bnxt_cfg_rx_mode(bp);
4684 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004685 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004686
4687 rc = bnxt_hwrm_set_coal(bp);
4688 if (rc)
4689 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004690 rc);
4691
4692 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4693 rc = bnxt_setup_nitroa0_vnic(bp);
4694 if (rc)
4695 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4696 rc);
4697 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004698
Michael Chancf6645f2016-06-13 02:25:28 -04004699 if (BNXT_VF(bp)) {
4700 bnxt_hwrm_func_qcfg(bp);
4701 netdev_update_features(bp->dev);
4702 }
4703
Michael Chanc0c050c2015-10-22 16:01:17 -04004704 return 0;
4705
4706err_out:
4707 bnxt_hwrm_resource_free(bp, 0, true);
4708
4709 return rc;
4710}
4711
4712static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4713{
4714 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4715 return 0;
4716}
4717
4718static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4719{
4720 bnxt_init_rx_rings(bp);
4721 bnxt_init_tx_rings(bp);
4722 bnxt_init_ring_grps(bp, irq_re_init);
4723 bnxt_init_vnics(bp);
4724
4725 return bnxt_init_chip(bp, irq_re_init);
4726}
4727
Michael Chanc0c050c2015-10-22 16:01:17 -04004728static int bnxt_set_real_num_queues(struct bnxt *bp)
4729{
4730 int rc;
4731 struct net_device *dev = bp->dev;
4732
4733 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4734 if (rc)
4735 return rc;
4736
4737 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4738 if (rc)
4739 return rc;
4740
4741#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004742 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004743 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004744#endif
4745
4746 return rc;
4747}
4748
Michael Chan6e6c5a52016-01-02 23:45:02 -05004749static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4750 bool shared)
4751{
4752 int _rx = *rx, _tx = *tx;
4753
4754 if (shared) {
4755 *rx = min_t(int, _rx, max);
4756 *tx = min_t(int, _tx, max);
4757 } else {
4758 if (max < 2)
4759 return -ENOMEM;
4760
4761 while (_rx + _tx > max) {
4762 if (_rx > _tx && _rx > 1)
4763 _rx--;
4764 else if (_tx > 1)
4765 _tx--;
4766 }
4767 *rx = _rx;
4768 *tx = _tx;
4769 }
4770 return 0;
4771}
4772
Michael Chan78095922016-12-07 00:26:16 -05004773static void bnxt_setup_msix(struct bnxt *bp)
4774{
4775 const int len = sizeof(bp->irq_tbl[0].name);
4776 struct net_device *dev = bp->dev;
4777 int tcs, i;
4778
4779 tcs = netdev_get_num_tc(dev);
4780 if (tcs > 1) {
4781 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4782 if (bp->tx_nr_rings_per_tc == 0) {
4783 netdev_reset_tc(dev);
4784 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4785 } else {
4786 int i, off, count;
4787
4788 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4789 for (i = 0; i < tcs; i++) {
4790 count = bp->tx_nr_rings_per_tc;
4791 off = i * count;
4792 netdev_set_tc_queue(dev, i, count, off);
4793 }
4794 }
4795 }
4796
4797 for (i = 0; i < bp->cp_nr_rings; i++) {
4798 char *attr;
4799
4800 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4801 attr = "TxRx";
4802 else if (i < bp->rx_nr_rings)
4803 attr = "rx";
4804 else
4805 attr = "tx";
4806
4807 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
4808 i);
4809 bp->irq_tbl[i].handler = bnxt_msix;
4810 }
4811}
4812
4813static void bnxt_setup_inta(struct bnxt *bp)
4814{
4815 const int len = sizeof(bp->irq_tbl[0].name);
4816
4817 if (netdev_get_num_tc(bp->dev))
4818 netdev_reset_tc(bp->dev);
4819
4820 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
4821 0);
4822 bp->irq_tbl[0].handler = bnxt_inta;
4823}
4824
4825static int bnxt_setup_int_mode(struct bnxt *bp)
4826{
4827 int rc;
4828
4829 if (bp->flags & BNXT_FLAG_USING_MSIX)
4830 bnxt_setup_msix(bp);
4831 else
4832 bnxt_setup_inta(bp);
4833
4834 rc = bnxt_set_real_num_queues(bp);
4835 return rc;
4836}
4837
Michael Chane4060d32016-12-07 00:26:19 -05004838unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
4839{
4840#if defined(CONFIG_BNXT_SRIOV)
4841 if (BNXT_VF(bp))
4842 return bp->vf.max_stat_ctxs;
4843#endif
4844 return bp->pf.max_stat_ctxs;
4845}
4846
Michael Chana588e452016-12-07 00:26:21 -05004847void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
4848{
4849#if defined(CONFIG_BNXT_SRIOV)
4850 if (BNXT_VF(bp))
4851 bp->vf.max_stat_ctxs = max;
4852 else
4853#endif
4854 bp->pf.max_stat_ctxs = max;
4855}
4856
Michael Chane4060d32016-12-07 00:26:19 -05004857unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
4858{
4859#if defined(CONFIG_BNXT_SRIOV)
4860 if (BNXT_VF(bp))
4861 return bp->vf.max_cp_rings;
4862#endif
4863 return bp->pf.max_cp_rings;
4864}
4865
Michael Chana588e452016-12-07 00:26:21 -05004866void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
4867{
4868#if defined(CONFIG_BNXT_SRIOV)
4869 if (BNXT_VF(bp))
4870 bp->vf.max_cp_rings = max;
4871 else
4872#endif
4873 bp->pf.max_cp_rings = max;
4874}
4875
Michael Chan78095922016-12-07 00:26:16 -05004876static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
4877{
4878#if defined(CONFIG_BNXT_SRIOV)
4879 if (BNXT_VF(bp))
4880 return bp->vf.max_irqs;
4881#endif
4882 return bp->pf.max_irqs;
4883}
4884
Michael Chan33c26572016-12-07 00:26:15 -05004885void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
4886{
4887#if defined(CONFIG_BNXT_SRIOV)
4888 if (BNXT_VF(bp))
4889 bp->vf.max_irqs = max_irqs;
4890 else
4891#endif
4892 bp->pf.max_irqs = max_irqs;
4893}
4894
Michael Chan78095922016-12-07 00:26:16 -05004895static int bnxt_init_msix(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004896{
Michael Chan01657bc2016-01-02 23:45:03 -05004897 int i, total_vecs, rc = 0, min = 1;
Michael Chan78095922016-12-07 00:26:16 -05004898 struct msix_entry *msix_ent;
Michael Chanc0c050c2015-10-22 16:01:17 -04004899
Michael Chan78095922016-12-07 00:26:16 -05004900 total_vecs = bnxt_get_max_func_irqs(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004901 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4902 if (!msix_ent)
4903 return -ENOMEM;
4904
4905 for (i = 0; i < total_vecs; i++) {
4906 msix_ent[i].entry = i;
4907 msix_ent[i].vector = 0;
4908 }
4909
Michael Chan01657bc2016-01-02 23:45:03 -05004910 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4911 min = 2;
4912
4913 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004914 if (total_vecs < 0) {
4915 rc = -ENODEV;
4916 goto msix_setup_exit;
4917 }
4918
4919 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4920 if (bp->irq_tbl) {
Michael Chan78095922016-12-07 00:26:16 -05004921 for (i = 0; i < total_vecs; i++)
4922 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chanc0c050c2015-10-22 16:01:17 -04004923
Michael Chan78095922016-12-07 00:26:16 -05004924 bp->total_irqs = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004925 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004926 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004927 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004928 if (rc)
4929 goto msix_setup_exit;
4930
Michael Chanc0c050c2015-10-22 16:01:17 -04004931 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan78095922016-12-07 00:26:16 -05004932 bp->cp_nr_rings = (min == 1) ?
4933 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
4934 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004935
Michael Chanc0c050c2015-10-22 16:01:17 -04004936 } else {
4937 rc = -ENOMEM;
4938 goto msix_setup_exit;
4939 }
4940 bp->flags |= BNXT_FLAG_USING_MSIX;
4941 kfree(msix_ent);
4942 return 0;
4943
4944msix_setup_exit:
Michael Chan78095922016-12-07 00:26:16 -05004945 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
4946 kfree(bp->irq_tbl);
4947 bp->irq_tbl = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04004948 pci_disable_msix(bp->pdev);
4949 kfree(msix_ent);
4950 return rc;
4951}
4952
Michael Chan78095922016-12-07 00:26:16 -05004953static int bnxt_init_inta(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004954{
Michael Chanc0c050c2015-10-22 16:01:17 -04004955 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
Michael Chan78095922016-12-07 00:26:16 -05004956 if (!bp->irq_tbl)
4957 return -ENOMEM;
4958
4959 bp->total_irqs = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004960 bp->rx_nr_rings = 1;
4961 bp->tx_nr_rings = 1;
4962 bp->cp_nr_rings = 1;
4963 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004964 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004965 bp->irq_tbl[0].vector = bp->pdev->irq;
Michael Chan78095922016-12-07 00:26:16 -05004966 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004967}
4968
Michael Chan78095922016-12-07 00:26:16 -05004969static int bnxt_init_int_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004970{
4971 int rc = 0;
4972
4973 if (bp->flags & BNXT_FLAG_MSIX_CAP)
Michael Chan78095922016-12-07 00:26:16 -05004974 rc = bnxt_init_msix(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004975
Michael Chan1fa72e22016-04-25 02:30:49 -04004976 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004977 /* fallback to INTA */
Michael Chan78095922016-12-07 00:26:16 -05004978 rc = bnxt_init_inta(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004979 }
4980 return rc;
4981}
4982
Michael Chan78095922016-12-07 00:26:16 -05004983static void bnxt_clear_int_mode(struct bnxt *bp)
4984{
4985 if (bp->flags & BNXT_FLAG_USING_MSIX)
4986 pci_disable_msix(bp->pdev);
4987
4988 kfree(bp->irq_tbl);
4989 bp->irq_tbl = NULL;
4990 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4991}
4992
Michael Chanc0c050c2015-10-22 16:01:17 -04004993static void bnxt_free_irq(struct bnxt *bp)
4994{
4995 struct bnxt_irq *irq;
4996 int i;
4997
4998#ifdef CONFIG_RFS_ACCEL
4999 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5000 bp->dev->rx_cpu_rmap = NULL;
5001#endif
5002 if (!bp->irq_tbl)
5003 return;
5004
5005 for (i = 0; i < bp->cp_nr_rings; i++) {
5006 irq = &bp->irq_tbl[i];
5007 if (irq->requested)
5008 free_irq(irq->vector, bp->bnapi[i]);
5009 irq->requested = 0;
5010 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005011}
5012
5013static int bnxt_request_irq(struct bnxt *bp)
5014{
Michael Chanb81a90d2016-01-02 23:45:01 -05005015 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005016 unsigned long flags = 0;
5017#ifdef CONFIG_RFS_ACCEL
5018 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5019#endif
5020
5021 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5022 flags = IRQF_SHARED;
5023
Michael Chanb81a90d2016-01-02 23:45:01 -05005024 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005025 struct bnxt_irq *irq = &bp->irq_tbl[i];
5026#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05005027 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005028 rc = irq_cpu_rmap_add(rmap, irq->vector);
5029 if (rc)
5030 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05005031 j);
5032 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04005033 }
5034#endif
5035 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5036 bp->bnapi[i]);
5037 if (rc)
5038 break;
5039
5040 irq->requested = 1;
5041 }
5042 return rc;
5043}
5044
5045static void bnxt_del_napi(struct bnxt *bp)
5046{
5047 int i;
5048
5049 if (!bp->bnapi)
5050 return;
5051
5052 for (i = 0; i < bp->cp_nr_rings; i++) {
5053 struct bnxt_napi *bnapi = bp->bnapi[i];
5054
5055 napi_hash_del(&bnapi->napi);
5056 netif_napi_del(&bnapi->napi);
5057 }
Eric Dumazete5f6f562016-11-16 06:31:52 -08005058 /* We called napi_hash_del() before netif_napi_del(), we need
5059 * to respect an RCU grace period before freeing napi structures.
5060 */
5061 synchronize_net();
Michael Chanc0c050c2015-10-22 16:01:17 -04005062}
5063
5064static void bnxt_init_napi(struct bnxt *bp)
5065{
5066 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005067 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04005068 struct bnxt_napi *bnapi;
5069
5070 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005071 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5072 cp_nr_rings--;
5073 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005074 bnapi = bp->bnapi[i];
5075 netif_napi_add(bp->dev, &bnapi->napi,
5076 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005077 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005078 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5079 bnapi = bp->bnapi[cp_nr_rings];
5080 netif_napi_add(bp->dev, &bnapi->napi,
5081 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04005082 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005083 } else {
5084 bnapi = bp->bnapi[0];
5085 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04005086 }
5087}
5088
5089static void bnxt_disable_napi(struct bnxt *bp)
5090{
5091 int i;
5092
5093 if (!bp->bnapi)
5094 return;
5095
Michael Chanb356a2e2016-12-29 12:13:31 -05005096 for (i = 0; i < bp->cp_nr_rings; i++)
Michael Chanc0c050c2015-10-22 16:01:17 -04005097 napi_disable(&bp->bnapi[i]->napi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005098}
5099
5100static void bnxt_enable_napi(struct bnxt *bp)
5101{
5102 int i;
5103
5104 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04005105 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005106 napi_enable(&bp->bnapi[i]->napi);
5107 }
5108}
5109
Michael Chan7df4ae92016-12-02 21:17:17 -05005110void bnxt_tx_disable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005111{
5112 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005113 struct bnxt_tx_ring_info *txr;
5114 struct netdev_queue *txq;
5115
Michael Chanb6ab4b02016-01-02 23:44:59 -05005116 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005117 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005118 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005119 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04005120 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005121 }
5122 }
5123 /* Stop all TX queues */
5124 netif_tx_disable(bp->dev);
5125 netif_carrier_off(bp->dev);
5126}
5127
Michael Chan7df4ae92016-12-02 21:17:17 -05005128void bnxt_tx_enable(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005129{
5130 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005131 struct bnxt_tx_ring_info *txr;
5132 struct netdev_queue *txq;
5133
5134 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005135 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005136 txq = netdev_get_tx_queue(bp->dev, i);
5137 txr->dev_state = 0;
5138 }
5139 netif_tx_wake_all_queues(bp->dev);
5140 if (bp->link_info.link_up)
5141 netif_carrier_on(bp->dev);
5142}
5143
5144static void bnxt_report_link(struct bnxt *bp)
5145{
5146 if (bp->link_info.link_up) {
5147 const char *duplex;
5148 const char *flow_ctrl;
5149 u16 speed;
5150
5151 netif_carrier_on(bp->dev);
5152 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5153 duplex = "full";
5154 else
5155 duplex = "half";
5156 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5157 flow_ctrl = "ON - receive & transmit";
5158 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5159 flow_ctrl = "ON - transmit";
5160 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5161 flow_ctrl = "ON - receive";
5162 else
5163 flow_ctrl = "none";
5164 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5165 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5166 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005167 if (bp->flags & BNXT_FLAG_EEE_CAP)
5168 netdev_info(bp->dev, "EEE is %s\n",
5169 bp->eee.eee_active ? "active" :
5170 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005171 } else {
5172 netif_carrier_off(bp->dev);
5173 netdev_err(bp->dev, "NIC Link is Down\n");
5174 }
5175}
5176
Michael Chan170ce012016-04-05 14:08:57 -04005177static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5178{
5179 int rc = 0;
5180 struct hwrm_port_phy_qcaps_input req = {0};
5181 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005182 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005183
5184 if (bp->hwrm_spec_code < 0x10201)
5185 return 0;
5186
5187 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5188
5189 mutex_lock(&bp->hwrm_cmd_lock);
5190 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5191 if (rc)
5192 goto hwrm_phy_qcaps_exit;
5193
5194 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5195 struct ethtool_eee *eee = &bp->eee;
5196 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5197
5198 bp->flags |= BNXT_FLAG_EEE_CAP;
5199 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5200 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5201 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5202 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5203 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5204 }
Michael Chan93ed8112016-06-13 02:25:37 -04005205 link_info->support_auto_speeds =
5206 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005207
5208hwrm_phy_qcaps_exit:
5209 mutex_unlock(&bp->hwrm_cmd_lock);
5210 return rc;
5211}
5212
Michael Chanc0c050c2015-10-22 16:01:17 -04005213static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5214{
5215 int rc = 0;
5216 struct bnxt_link_info *link_info = &bp->link_info;
5217 struct hwrm_port_phy_qcfg_input req = {0};
5218 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5219 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005220 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005221
5222 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5223
5224 mutex_lock(&bp->hwrm_cmd_lock);
5225 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5226 if (rc) {
5227 mutex_unlock(&bp->hwrm_cmd_lock);
5228 return rc;
5229 }
5230
5231 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5232 link_info->phy_link_status = resp->link;
5233 link_info->duplex = resp->duplex;
5234 link_info->pause = resp->pause;
5235 link_info->auto_mode = resp->auto_mode;
5236 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005237 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005238 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005239 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005240 if (link_info->phy_link_status == BNXT_LINK_LINK)
5241 link_info->link_speed = le16_to_cpu(resp->link_speed);
5242 else
5243 link_info->link_speed = 0;
5244 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005245 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5246 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005247 link_info->lp_auto_link_speeds =
5248 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005249 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5250 link_info->phy_ver[0] = resp->phy_maj;
5251 link_info->phy_ver[1] = resp->phy_min;
5252 link_info->phy_ver[2] = resp->phy_bld;
5253 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005254 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005255 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005256 link_info->phy_addr = resp->eee_config_phy_addr &
5257 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005258 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005259
Michael Chan170ce012016-04-05 14:08:57 -04005260 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5261 struct ethtool_eee *eee = &bp->eee;
5262 u16 fw_speeds;
5263
5264 eee->eee_active = 0;
5265 if (resp->eee_config_phy_addr &
5266 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5267 eee->eee_active = 1;
5268 fw_speeds = le16_to_cpu(
5269 resp->link_partner_adv_eee_link_speed_mask);
5270 eee->lp_advertised =
5271 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5272 }
5273
5274 /* Pull initial EEE config */
5275 if (!chng_link_state) {
5276 if (resp->eee_config_phy_addr &
5277 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5278 eee->eee_enabled = 1;
5279
5280 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5281 eee->advertised =
5282 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5283
5284 if (resp->eee_config_phy_addr &
5285 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5286 __le32 tmr;
5287
5288 eee->tx_lpi_enabled = 1;
5289 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5290 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5291 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5292 }
5293 }
5294 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005295 /* TODO: need to add more logic to report VF link */
5296 if (chng_link_state) {
5297 if (link_info->phy_link_status == BNXT_LINK_LINK)
5298 link_info->link_up = 1;
5299 else
5300 link_info->link_up = 0;
5301 if (link_up != link_info->link_up)
5302 bnxt_report_link(bp);
5303 } else {
5304 /* alwasy link down if not require to update link state */
5305 link_info->link_up = 0;
5306 }
5307 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005308
5309 diff = link_info->support_auto_speeds ^ link_info->advertising;
5310 if ((link_info->support_auto_speeds | diff) !=
5311 link_info->support_auto_speeds) {
5312 /* An advertised speed is no longer supported, so we need to
5313 * update the advertisement settings. See bnxt_reset() for
5314 * comments about the rtnl_lock() sequence below.
5315 */
5316 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5317 rtnl_lock();
5318 link_info->advertising = link_info->support_auto_speeds;
5319 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
5320 (link_info->autoneg & BNXT_AUTONEG_SPEED))
5321 bnxt_hwrm_set_link_setting(bp, true, false);
5322 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5323 rtnl_unlock();
5324 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005325 return 0;
5326}
5327
Michael Chan10289be2016-05-15 03:04:49 -04005328static void bnxt_get_port_module_status(struct bnxt *bp)
5329{
5330 struct bnxt_link_info *link_info = &bp->link_info;
5331 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5332 u8 module_status;
5333
5334 if (bnxt_update_link(bp, true))
5335 return;
5336
5337 module_status = link_info->module_status;
5338 switch (module_status) {
5339 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5340 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5341 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5342 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5343 bp->pf.port_id);
5344 if (bp->hwrm_spec_code >= 0x10201) {
5345 netdev_warn(bp->dev, "Module part number %s\n",
5346 resp->phy_vendor_partnumber);
5347 }
5348 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5349 netdev_warn(bp->dev, "TX is disabled\n");
5350 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5351 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5352 }
5353}
5354
Michael Chanc0c050c2015-10-22 16:01:17 -04005355static void
5356bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5357{
5358 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005359 if (bp->hwrm_spec_code >= 0x10201)
5360 req->auto_pause =
5361 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005362 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5363 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5364 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005365 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005366 req->enables |=
5367 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5368 } else {
5369 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5370 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5371 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5372 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5373 req->enables |=
5374 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005375 if (bp->hwrm_spec_code >= 0x10201) {
5376 req->auto_pause = req->force_pause;
5377 req->enables |= cpu_to_le32(
5378 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5379 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005380 }
5381}
5382
5383static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5384 struct hwrm_port_phy_cfg_input *req)
5385{
5386 u8 autoneg = bp->link_info.autoneg;
5387 u16 fw_link_speed = bp->link_info.req_link_speed;
Michael Chan68515a12016-12-29 12:13:34 -05005388 u16 advertising = bp->link_info.advertising;
Michael Chanc0c050c2015-10-22 16:01:17 -04005389
5390 if (autoneg & BNXT_AUTONEG_SPEED) {
5391 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005392 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005393
5394 req->enables |= cpu_to_le32(
5395 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5396 req->auto_link_speed_mask = cpu_to_le16(advertising);
5397
5398 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5399 req->flags |=
5400 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5401 } else {
5402 req->force_link_speed = cpu_to_le16(fw_link_speed);
5403 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5404 }
5405
Michael Chanc0c050c2015-10-22 16:01:17 -04005406 /* tell chimp that the setting takes effect immediately */
5407 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5408}
5409
5410int bnxt_hwrm_set_pause(struct bnxt *bp)
5411{
5412 struct hwrm_port_phy_cfg_input req = {0};
5413 int rc;
5414
5415 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5416 bnxt_hwrm_set_pause_common(bp, &req);
5417
5418 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5419 bp->link_info.force_link_chng)
5420 bnxt_hwrm_set_link_common(bp, &req);
5421
5422 mutex_lock(&bp->hwrm_cmd_lock);
5423 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5424 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5425 /* since changing of pause setting doesn't trigger any link
5426 * change event, the driver needs to update the current pause
5427 * result upon successfully return of the phy_cfg command
5428 */
5429 bp->link_info.pause =
5430 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5431 bp->link_info.auto_pause_setting = 0;
5432 if (!bp->link_info.force_link_chng)
5433 bnxt_report_link(bp);
5434 }
5435 bp->link_info.force_link_chng = false;
5436 mutex_unlock(&bp->hwrm_cmd_lock);
5437 return rc;
5438}
5439
Michael Chan939f7f02016-04-05 14:08:58 -04005440static void bnxt_hwrm_set_eee(struct bnxt *bp,
5441 struct hwrm_port_phy_cfg_input *req)
5442{
5443 struct ethtool_eee *eee = &bp->eee;
5444
5445 if (eee->eee_enabled) {
5446 u16 eee_speeds;
5447 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5448
5449 if (eee->tx_lpi_enabled)
5450 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5451 else
5452 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5453
5454 req->flags |= cpu_to_le32(flags);
5455 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5456 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5457 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5458 } else {
5459 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5460 }
5461}
5462
5463int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005464{
5465 struct hwrm_port_phy_cfg_input req = {0};
5466
5467 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5468 if (set_pause)
5469 bnxt_hwrm_set_pause_common(bp, &req);
5470
5471 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005472
5473 if (set_eee)
5474 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005475 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5476}
5477
Michael Chan33f7d552016-04-11 04:11:12 -04005478static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5479{
5480 struct hwrm_port_phy_cfg_input req = {0};
5481
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005482 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005483 return 0;
5484
5485 if (pci_num_vf(bp->pdev))
5486 return 0;
5487
5488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005489 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005490 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5491}
5492
Michael Chan939f7f02016-04-05 14:08:58 -04005493static bool bnxt_eee_config_ok(struct bnxt *bp)
5494{
5495 struct ethtool_eee *eee = &bp->eee;
5496 struct bnxt_link_info *link_info = &bp->link_info;
5497
5498 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5499 return true;
5500
5501 if (eee->eee_enabled) {
5502 u32 advertising =
5503 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5504
5505 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5506 eee->eee_enabled = 0;
5507 return false;
5508 }
5509 if (eee->advertised & ~advertising) {
5510 eee->advertised = advertising & eee->supported;
5511 return false;
5512 }
5513 }
5514 return true;
5515}
5516
Michael Chanc0c050c2015-10-22 16:01:17 -04005517static int bnxt_update_phy_setting(struct bnxt *bp)
5518{
5519 int rc;
5520 bool update_link = false;
5521 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005522 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005523 struct bnxt_link_info *link_info = &bp->link_info;
5524
5525 rc = bnxt_update_link(bp, true);
5526 if (rc) {
5527 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5528 rc);
5529 return rc;
5530 }
5531 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005532 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5533 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005534 update_pause = true;
5535 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5536 link_info->force_pause_setting != link_info->req_flow_ctrl)
5537 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005538 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5539 if (BNXT_AUTO_MODE(link_info->auto_mode))
5540 update_link = true;
5541 if (link_info->req_link_speed != link_info->force_link_speed)
5542 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005543 if (link_info->req_duplex != link_info->duplex_setting)
5544 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005545 } else {
5546 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5547 update_link = true;
5548 if (link_info->advertising != link_info->auto_link_speeds)
5549 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005550 }
5551
Michael Chan16d663a2016-11-16 21:13:07 -05005552 /* The last close may have shutdown the link, so need to call
5553 * PHY_CFG to bring it back up.
5554 */
5555 if (!netif_carrier_ok(bp->dev))
5556 update_link = true;
5557
Michael Chan939f7f02016-04-05 14:08:58 -04005558 if (!bnxt_eee_config_ok(bp))
5559 update_eee = true;
5560
Michael Chanc0c050c2015-10-22 16:01:17 -04005561 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005562 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005563 else if (update_pause)
5564 rc = bnxt_hwrm_set_pause(bp);
5565 if (rc) {
5566 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5567 rc);
5568 return rc;
5569 }
5570
5571 return rc;
5572}
5573
Jeffrey Huang11809492015-11-05 16:25:49 -05005574/* Common routine to pre-map certain register block to different GRC window.
5575 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5576 * in PF and 3 windows in VF that can be customized to map in different
5577 * register blocks.
5578 */
5579static void bnxt_preset_reg_win(struct bnxt *bp)
5580{
5581 if (BNXT_PF(bp)) {
5582 /* CAG registers map to GRC window #4 */
5583 writel(BNXT_CAG_REG_BASE,
5584 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5585 }
5586}
5587
Michael Chanc0c050c2015-10-22 16:01:17 -04005588static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5589{
5590 int rc = 0;
5591
Jeffrey Huang11809492015-11-05 16:25:49 -05005592 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005593 netif_carrier_off(bp->dev);
5594 if (irq_re_init) {
5595 rc = bnxt_setup_int_mode(bp);
5596 if (rc) {
5597 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5598 rc);
5599 return rc;
5600 }
5601 }
5602 if ((bp->flags & BNXT_FLAG_RFS) &&
5603 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5604 /* disable RFS if falling back to INTA */
5605 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5606 bp->flags &= ~BNXT_FLAG_RFS;
5607 }
5608
5609 rc = bnxt_alloc_mem(bp, irq_re_init);
5610 if (rc) {
5611 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5612 goto open_err_free_mem;
5613 }
5614
5615 if (irq_re_init) {
5616 bnxt_init_napi(bp);
5617 rc = bnxt_request_irq(bp);
5618 if (rc) {
5619 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5620 goto open_err;
5621 }
5622 }
5623
5624 bnxt_enable_napi(bp);
5625
5626 rc = bnxt_init_nic(bp, irq_re_init);
5627 if (rc) {
5628 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5629 goto open_err;
5630 }
5631
5632 if (link_re_init) {
5633 rc = bnxt_update_phy_setting(bp);
5634 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005635 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005636 }
5637
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005638 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005639 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005640
Michael Chancaefe522015-12-09 19:35:42 -05005641 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005642 bnxt_enable_int(bp);
5643 /* Enable TX queues */
5644 bnxt_tx_enable(bp);
5645 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005646 /* Poll link status and check for SFP+ module status */
5647 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005648
5649 return 0;
5650
5651open_err:
5652 bnxt_disable_napi(bp);
5653 bnxt_del_napi(bp);
5654
5655open_err_free_mem:
5656 bnxt_free_skbs(bp);
5657 bnxt_free_irq(bp);
5658 bnxt_free_mem(bp, true);
5659 return rc;
5660}
5661
5662/* rtnl_lock held */
5663int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5664{
5665 int rc = 0;
5666
5667 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5668 if (rc) {
5669 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5670 dev_close(bp->dev);
5671 }
5672 return rc;
5673}
5674
5675static int bnxt_open(struct net_device *dev)
5676{
5677 struct bnxt *bp = netdev_priv(dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005678
Michael Chanc0c050c2015-10-22 16:01:17 -04005679 return __bnxt_open_nic(bp, true, true);
5680}
5681
Michael Chanc0c050c2015-10-22 16:01:17 -04005682int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5683{
5684 int rc = 0;
5685
5686#ifdef CONFIG_BNXT_SRIOV
5687 if (bp->sriov_cfg) {
5688 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5689 !bp->sriov_cfg,
5690 BNXT_SRIOV_CFG_WAIT_TMO);
5691 if (rc)
5692 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5693 }
5694#endif
5695 /* Change device state to avoid TX queue wake up's */
5696 bnxt_tx_disable(bp);
5697
Michael Chancaefe522015-12-09 19:35:42 -05005698 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005699 smp_mb__after_atomic();
5700 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5701 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005702
Michael Chan9d8bc092016-12-29 12:13:33 -05005703 /* Flush rings and and disable interrupts */
Michael Chanc0c050c2015-10-22 16:01:17 -04005704 bnxt_shutdown_nic(bp, irq_re_init);
5705
5706 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5707
5708 bnxt_disable_napi(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005709 del_timer_sync(&bp->timer);
5710 bnxt_free_skbs(bp);
5711
5712 if (irq_re_init) {
5713 bnxt_free_irq(bp);
5714 bnxt_del_napi(bp);
5715 }
5716 bnxt_free_mem(bp, irq_re_init);
5717 return rc;
5718}
5719
5720static int bnxt_close(struct net_device *dev)
5721{
5722 struct bnxt *bp = netdev_priv(dev);
5723
5724 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005725 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005726 return 0;
5727}
5728
5729/* rtnl_lock held */
5730static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5731{
5732 switch (cmd) {
5733 case SIOCGMIIPHY:
5734 /* fallthru */
5735 case SIOCGMIIREG: {
5736 if (!netif_running(dev))
5737 return -EAGAIN;
5738
5739 return 0;
5740 }
5741
5742 case SIOCSMIIREG:
5743 if (!netif_running(dev))
5744 return -EAGAIN;
5745
5746 return 0;
5747
5748 default:
5749 /* do nothing */
5750 break;
5751 }
5752 return -EOPNOTSUPP;
5753}
5754
5755static struct rtnl_link_stats64 *
5756bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5757{
5758 u32 i;
5759 struct bnxt *bp = netdev_priv(dev);
5760
5761 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5762
5763 if (!bp->bnapi)
5764 return stats;
5765
5766 /* TODO check if we need to synchronize with bnxt_close path */
5767 for (i = 0; i < bp->cp_nr_rings; i++) {
5768 struct bnxt_napi *bnapi = bp->bnapi[i];
5769 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5770 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5771
5772 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5773 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5774 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5775
5776 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5777 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5778 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5779
5780 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5781 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5782 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5783
5784 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5785 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5786 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5787
5788 stats->rx_missed_errors +=
5789 le64_to_cpu(hw_stats->rx_discard_pkts);
5790
5791 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5792
Michael Chanc0c050c2015-10-22 16:01:17 -04005793 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5794 }
5795
Michael Chan9947f832016-03-07 15:38:46 -05005796 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5797 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5798 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5799
5800 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5801 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5802 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5803 le64_to_cpu(rx->rx_ovrsz_frames) +
5804 le64_to_cpu(rx->rx_runt_frames);
5805 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5806 le64_to_cpu(rx->rx_jbr_frames);
5807 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5808 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5809 stats->tx_errors = le64_to_cpu(tx->tx_err);
5810 }
5811
Michael Chanc0c050c2015-10-22 16:01:17 -04005812 return stats;
5813}
5814
5815static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5816{
5817 struct net_device *dev = bp->dev;
5818 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5819 struct netdev_hw_addr *ha;
5820 u8 *haddr;
5821 int mc_count = 0;
5822 bool update = false;
5823 int off = 0;
5824
5825 netdev_for_each_mc_addr(ha, dev) {
5826 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5827 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5828 vnic->mc_list_count = 0;
5829 return false;
5830 }
5831 haddr = ha->addr;
5832 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5833 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5834 update = true;
5835 }
5836 off += ETH_ALEN;
5837 mc_count++;
5838 }
5839 if (mc_count)
5840 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5841
5842 if (mc_count != vnic->mc_list_count) {
5843 vnic->mc_list_count = mc_count;
5844 update = true;
5845 }
5846 return update;
5847}
5848
5849static bool bnxt_uc_list_updated(struct bnxt *bp)
5850{
5851 struct net_device *dev = bp->dev;
5852 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5853 struct netdev_hw_addr *ha;
5854 int off = 0;
5855
5856 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5857 return true;
5858
5859 netdev_for_each_uc_addr(ha, dev) {
5860 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5861 return true;
5862
5863 off += ETH_ALEN;
5864 }
5865 return false;
5866}
5867
5868static void bnxt_set_rx_mode(struct net_device *dev)
5869{
5870 struct bnxt *bp = netdev_priv(dev);
5871 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5872 u32 mask = vnic->rx_mask;
5873 bool mc_update = false;
5874 bool uc_update;
5875
5876 if (!netif_running(dev))
5877 return;
5878
5879 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5880 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5881 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5882
Michael Chan17c71ac2016-07-01 18:46:27 -04005883 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005884 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5885
5886 uc_update = bnxt_uc_list_updated(bp);
5887
5888 if (dev->flags & IFF_ALLMULTI) {
5889 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5890 vnic->mc_list_count = 0;
5891 } else {
5892 mc_update = bnxt_mc_list_updated(bp, &mask);
5893 }
5894
5895 if (mask != vnic->rx_mask || uc_update || mc_update) {
5896 vnic->rx_mask = mask;
5897
5898 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5899 schedule_work(&bp->sp_task);
5900 }
5901}
5902
Michael Chanb664f002015-12-02 01:54:08 -05005903static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005904{
5905 struct net_device *dev = bp->dev;
5906 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5907 struct netdev_hw_addr *ha;
5908 int i, off = 0, rc;
5909 bool uc_update;
5910
5911 netif_addr_lock_bh(dev);
5912 uc_update = bnxt_uc_list_updated(bp);
5913 netif_addr_unlock_bh(dev);
5914
5915 if (!uc_update)
5916 goto skip_uc;
5917
5918 mutex_lock(&bp->hwrm_cmd_lock);
5919 for (i = 1; i < vnic->uc_filter_count; i++) {
5920 struct hwrm_cfa_l2_filter_free_input req = {0};
5921
5922 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5923 -1);
5924
5925 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5926
5927 rc = _hwrm_send_message(bp, &req, sizeof(req),
5928 HWRM_CMD_TIMEOUT);
5929 }
5930 mutex_unlock(&bp->hwrm_cmd_lock);
5931
5932 vnic->uc_filter_count = 1;
5933
5934 netif_addr_lock_bh(dev);
5935 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5936 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5937 } else {
5938 netdev_for_each_uc_addr(ha, dev) {
5939 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5940 off += ETH_ALEN;
5941 vnic->uc_filter_count++;
5942 }
5943 }
5944 netif_addr_unlock_bh(dev);
5945
5946 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5947 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5948 if (rc) {
5949 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5950 rc);
5951 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005952 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005953 }
5954 }
5955
5956skip_uc:
5957 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5958 if (rc)
5959 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5960 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005961
5962 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005963}
5964
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005965static bool bnxt_rfs_capable(struct bnxt *bp)
5966{
5967#ifdef CONFIG_RFS_ACCEL
5968 struct bnxt_pf_info *pf = &bp->pf;
5969 int vnics;
5970
5971 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5972 return false;
5973
5974 vnics = 1 + bp->rx_nr_rings;
Vasundhara Volama2304902016-07-25 12:33:36 -04005975 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5976 netdev_warn(bp->dev,
5977 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5978 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005979 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04005980 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005981
5982 return true;
5983#else
5984 return false;
5985#endif
5986}
5987
Michael Chanc0c050c2015-10-22 16:01:17 -04005988static netdev_features_t bnxt_fix_features(struct net_device *dev,
5989 netdev_features_t features)
5990{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005991 struct bnxt *bp = netdev_priv(dev);
5992
Vasundhara Volama2304902016-07-25 12:33:36 -04005993 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005994 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005995
5996 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5997 * turned on or off together.
5998 */
5999 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6000 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6001 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6002 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6003 NETIF_F_HW_VLAN_STAG_RX);
6004 else
6005 features |= NETIF_F_HW_VLAN_CTAG_RX |
6006 NETIF_F_HW_VLAN_STAG_RX;
6007 }
Michael Chancf6645f2016-06-13 02:25:28 -04006008#ifdef CONFIG_BNXT_SRIOV
6009 if (BNXT_VF(bp)) {
6010 if (bp->vf.vlan) {
6011 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6012 NETIF_F_HW_VLAN_STAG_RX);
6013 }
6014 }
6015#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04006016 return features;
6017}
6018
6019static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6020{
6021 struct bnxt *bp = netdev_priv(dev);
6022 u32 flags = bp->flags;
6023 u32 changes;
6024 int rc = 0;
6025 bool re_init = false;
6026 bool update_tpa = false;
6027
6028 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006029 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006030 flags |= BNXT_FLAG_GRO;
6031 if (features & NETIF_F_LRO)
6032 flags |= BNXT_FLAG_LRO;
6033
6034 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6035 flags |= BNXT_FLAG_STRIP_VLAN;
6036
6037 if (features & NETIF_F_NTUPLE)
6038 flags |= BNXT_FLAG_RFS;
6039
6040 changes = flags ^ bp->flags;
6041 if (changes & BNXT_FLAG_TPA) {
6042 update_tpa = true;
6043 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6044 (flags & BNXT_FLAG_TPA) == 0)
6045 re_init = true;
6046 }
6047
6048 if (changes & ~BNXT_FLAG_TPA)
6049 re_init = true;
6050
6051 if (flags != bp->flags) {
6052 u32 old_flags = bp->flags;
6053
6054 bp->flags = flags;
6055
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006056 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006057 if (update_tpa)
6058 bnxt_set_ring_params(bp);
6059 return rc;
6060 }
6061
6062 if (re_init) {
6063 bnxt_close_nic(bp, false, false);
6064 if (update_tpa)
6065 bnxt_set_ring_params(bp);
6066
6067 return bnxt_open_nic(bp, false, false);
6068 }
6069 if (update_tpa) {
6070 rc = bnxt_set_tpa(bp,
6071 (flags & BNXT_FLAG_TPA) ?
6072 true : false);
6073 if (rc)
6074 bp->flags = old_flags;
6075 }
6076 }
6077 return rc;
6078}
6079
Michael Chan9f554592016-01-02 23:44:58 -05006080static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6081{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006082 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006083 int i = bnapi->index;
6084
Michael Chan3b2b7d92016-01-02 23:45:00 -05006085 if (!txr)
6086 return;
6087
Michael Chan9f554592016-01-02 23:44:58 -05006088 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6089 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6090 txr->tx_cons);
6091}
6092
6093static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6094{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006095 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006096 int i = bnapi->index;
6097
Michael Chan3b2b7d92016-01-02 23:45:00 -05006098 if (!rxr)
6099 return;
6100
Michael Chan9f554592016-01-02 23:44:58 -05006101 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",
6102 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6103 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6104 rxr->rx_sw_agg_prod);
6105}
6106
6107static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6108{
6109 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6110 int i = bnapi->index;
6111
6112 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6113 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6114}
6115
Michael Chanc0c050c2015-10-22 16:01:17 -04006116static void bnxt_dbg_dump_states(struct bnxt *bp)
6117{
6118 int i;
6119 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006120
6121 for (i = 0; i < bp->cp_nr_rings; i++) {
6122 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006123 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006124 bnxt_dump_tx_sw_state(bnapi);
6125 bnxt_dump_rx_sw_state(bnapi);
6126 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006127 }
6128 }
6129}
6130
Michael Chan6988bd92016-06-13 02:25:29 -04006131static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006132{
Michael Chan6988bd92016-06-13 02:25:29 -04006133 if (!silent)
6134 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006135 if (netif_running(bp->dev)) {
6136 bnxt_close_nic(bp, false, false);
6137 bnxt_open_nic(bp, false, false);
6138 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006139}
6140
6141static void bnxt_tx_timeout(struct net_device *dev)
6142{
6143 struct bnxt *bp = netdev_priv(dev);
6144
6145 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6146 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6147 schedule_work(&bp->sp_task);
6148}
6149
6150#ifdef CONFIG_NET_POLL_CONTROLLER
6151static void bnxt_poll_controller(struct net_device *dev)
6152{
6153 struct bnxt *bp = netdev_priv(dev);
6154 int i;
6155
6156 for (i = 0; i < bp->cp_nr_rings; i++) {
6157 struct bnxt_irq *irq = &bp->irq_tbl[i];
6158
6159 disable_irq(irq->vector);
6160 irq->handler(irq->vector, bp->bnapi[i]);
6161 enable_irq(irq->vector);
6162 }
6163}
6164#endif
6165
6166static void bnxt_timer(unsigned long data)
6167{
6168 struct bnxt *bp = (struct bnxt *)data;
6169 struct net_device *dev = bp->dev;
6170
6171 if (!netif_running(dev))
6172 return;
6173
6174 if (atomic_read(&bp->intr_sem) != 0)
6175 goto bnxt_restart_timer;
6176
Michael Chan3bdf56c2016-03-07 15:38:45 -05006177 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6178 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6179 schedule_work(&bp->sp_task);
6180 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006181bnxt_restart_timer:
6182 mod_timer(&bp->timer, jiffies + bp->current_interval);
6183}
6184
Michael Chan6988bd92016-06-13 02:25:29 -04006185/* Only called from bnxt_sp_task() */
6186static void bnxt_reset(struct bnxt *bp, bool silent)
6187{
6188 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6189 * for BNXT_STATE_IN_SP_TASK to clear.
6190 * If there is a parallel dev_close(), bnxt_close() may be holding
6191 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6192 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6193 */
6194 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6195 rtnl_lock();
6196 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6197 bnxt_reset_task(bp, silent);
6198 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6199 rtnl_unlock();
6200}
6201
Michael Chanc0c050c2015-10-22 16:01:17 -04006202static void bnxt_cfg_ntp_filters(struct bnxt *);
6203
6204static void bnxt_sp_task(struct work_struct *work)
6205{
6206 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6207 int rc;
6208
Michael Chan4cebdce2015-12-09 19:35:43 -05006209 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6210 smp_mb__after_atomic();
6211 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6212 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006213 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006214 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006215
6216 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6217 bnxt_cfg_rx_mode(bp);
6218
6219 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6220 bnxt_cfg_ntp_filters(bp);
6221 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chan286ef9d2016-11-16 21:13:08 -05006222 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6223 &bp->sp_event))
6224 bnxt_hwrm_phy_qcaps(bp);
6225
Michael Chanc0c050c2015-10-22 16:01:17 -04006226 rc = bnxt_update_link(bp, true);
6227 if (rc)
6228 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6229 rc);
6230 }
6231 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6232 bnxt_hwrm_exec_fwd_req(bp);
6233 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6234 bnxt_hwrm_tunnel_dst_port_alloc(
6235 bp, bp->vxlan_port,
6236 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6237 }
6238 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6239 bnxt_hwrm_tunnel_dst_port_free(
6240 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6241 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006242 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6243 bnxt_hwrm_tunnel_dst_port_alloc(
6244 bp, bp->nge_port,
6245 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6246 }
6247 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6248 bnxt_hwrm_tunnel_dst_port_free(
6249 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6250 }
Michael Chan6988bd92016-06-13 02:25:29 -04006251 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6252 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006253
Michael Chanfc0f1922016-06-13 02:25:30 -04006254 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6255 bnxt_reset(bp, true);
6256
Michael Chan4bb13ab2016-04-05 14:09:01 -04006257 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006258 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006259
Michael Chan3bdf56c2016-03-07 15:38:45 -05006260 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6261 bnxt_hwrm_port_qstats(bp);
6262
Michael Chan4cebdce2015-12-09 19:35:43 -05006263 smp_mb__before_atomic();
6264 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006265}
6266
6267static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6268{
6269 int rc;
6270 struct bnxt *bp = netdev_priv(dev);
6271
6272 SET_NETDEV_DEV(dev, &pdev->dev);
6273
6274 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6275 rc = pci_enable_device(pdev);
6276 if (rc) {
6277 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6278 goto init_err;
6279 }
6280
6281 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6282 dev_err(&pdev->dev,
6283 "Cannot find PCI device base address, aborting\n");
6284 rc = -ENODEV;
6285 goto init_err_disable;
6286 }
6287
6288 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6289 if (rc) {
6290 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6291 goto init_err_disable;
6292 }
6293
6294 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6295 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6296 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6297 goto init_err_disable;
6298 }
6299
6300 pci_set_master(pdev);
6301
6302 bp->dev = dev;
6303 bp->pdev = pdev;
6304
6305 bp->bar0 = pci_ioremap_bar(pdev, 0);
6306 if (!bp->bar0) {
6307 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6308 rc = -ENOMEM;
6309 goto init_err_release;
6310 }
6311
6312 bp->bar1 = pci_ioremap_bar(pdev, 2);
6313 if (!bp->bar1) {
6314 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6315 rc = -ENOMEM;
6316 goto init_err_release;
6317 }
6318
6319 bp->bar2 = pci_ioremap_bar(pdev, 4);
6320 if (!bp->bar2) {
6321 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6322 rc = -ENOMEM;
6323 goto init_err_release;
6324 }
6325
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006326 pci_enable_pcie_error_reporting(pdev);
6327
Michael Chanc0c050c2015-10-22 16:01:17 -04006328 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6329
6330 spin_lock_init(&bp->ntp_fltr_lock);
6331
6332 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6333 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6334
Michael Chandfb5b892016-02-26 04:00:01 -05006335 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006336 bp->rx_coal_ticks = 12;
6337 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006338 bp->rx_coal_ticks_irq = 1;
6339 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006340
Michael Chandfc9c942016-02-26 04:00:03 -05006341 bp->tx_coal_ticks = 25;
6342 bp->tx_coal_bufs = 30;
6343 bp->tx_coal_ticks_irq = 2;
6344 bp->tx_coal_bufs_irq = 2;
6345
Michael Chan51f30782016-07-01 18:46:29 -04006346 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6347
Michael Chanc0c050c2015-10-22 16:01:17 -04006348 init_timer(&bp->timer);
6349 bp->timer.data = (unsigned long)bp;
6350 bp->timer.function = bnxt_timer;
6351 bp->current_interval = BNXT_TIMER_INTERVAL;
6352
Michael Chancaefe522015-12-09 19:35:42 -05006353 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006354
6355 return 0;
6356
6357init_err_release:
6358 if (bp->bar2) {
6359 pci_iounmap(pdev, bp->bar2);
6360 bp->bar2 = NULL;
6361 }
6362
6363 if (bp->bar1) {
6364 pci_iounmap(pdev, bp->bar1);
6365 bp->bar1 = NULL;
6366 }
6367
6368 if (bp->bar0) {
6369 pci_iounmap(pdev, bp->bar0);
6370 bp->bar0 = NULL;
6371 }
6372
6373 pci_release_regions(pdev);
6374
6375init_err_disable:
6376 pci_disable_device(pdev);
6377
6378init_err:
6379 return rc;
6380}
6381
6382/* rtnl_lock held */
6383static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6384{
6385 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006386 struct bnxt *bp = netdev_priv(dev);
6387 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006388
6389 if (!is_valid_ether_addr(addr->sa_data))
6390 return -EADDRNOTAVAIL;
6391
Michael Chan84c33dd2016-04-11 04:11:13 -04006392 rc = bnxt_approve_mac(bp, addr->sa_data);
6393 if (rc)
6394 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006395
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006396 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6397 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006398
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006399 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6400 if (netif_running(dev)) {
6401 bnxt_close_nic(bp, false, false);
6402 rc = bnxt_open_nic(bp, false, false);
6403 }
6404
6405 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006406}
6407
6408/* rtnl_lock held */
6409static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6410{
6411 struct bnxt *bp = netdev_priv(dev);
6412
Michael Chanc0c050c2015-10-22 16:01:17 -04006413 if (netif_running(dev))
6414 bnxt_close_nic(bp, false, false);
6415
6416 dev->mtu = new_mtu;
6417 bnxt_set_ring_params(bp);
6418
6419 if (netif_running(dev))
6420 return bnxt_open_nic(bp, false, false);
6421
6422 return 0;
6423}
6424
Michael Chanc5e3deb2016-12-02 21:17:15 -05006425int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006426{
6427 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006428 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006429
Michael Chanc0c050c2015-10-22 16:01:17 -04006430 if (tc > bp->max_tc) {
6431 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6432 tc, bp->max_tc);
6433 return -EINVAL;
6434 }
6435
6436 if (netdev_get_num_tc(dev) == tc)
6437 return 0;
6438
Michael Chan3ffb6a32016-11-11 00:11:42 -05006439 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6440 sh = true;
6441
Michael Chanc0c050c2015-10-22 16:01:17 -04006442 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006443 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006444
6445 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006446 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006447 return -ENOMEM;
6448 }
6449
6450 /* Needs to close the device and do hw resource re-allocations */
6451 if (netif_running(bp->dev))
6452 bnxt_close_nic(bp, true, false);
6453
6454 if (tc) {
6455 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6456 netdev_set_num_tc(dev, tc);
6457 } else {
6458 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6459 netdev_reset_tc(dev);
6460 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006461 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6462 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006463 bp->num_stat_ctxs = bp->cp_nr_rings;
6464
6465 if (netif_running(bp->dev))
6466 return bnxt_open_nic(bp, true, false);
6467
6468 return 0;
6469}
6470
Michael Chanc5e3deb2016-12-02 21:17:15 -05006471static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6472 struct tc_to_netdev *ntc)
6473{
6474 if (ntc->type != TC_SETUP_MQPRIO)
6475 return -EINVAL;
6476
6477 return bnxt_setup_mq_tc(dev, ntc->tc);
6478}
6479
Michael Chanc0c050c2015-10-22 16:01:17 -04006480#ifdef CONFIG_RFS_ACCEL
6481static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6482 struct bnxt_ntuple_filter *f2)
6483{
6484 struct flow_keys *keys1 = &f1->fkeys;
6485 struct flow_keys *keys2 = &f2->fkeys;
6486
6487 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6488 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6489 keys1->ports.ports == keys2->ports.ports &&
6490 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6491 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006492 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6493 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006494 return true;
6495
6496 return false;
6497}
6498
6499static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6500 u16 rxq_index, u32 flow_id)
6501{
6502 struct bnxt *bp = netdev_priv(dev);
6503 struct bnxt_ntuple_filter *fltr, *new_fltr;
6504 struct flow_keys *fkeys;
6505 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006506 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006507 struct hlist_head *head;
6508
6509 if (skb->encapsulation)
6510 return -EPROTONOSUPPORT;
6511
Michael Chana54c4d72016-07-25 12:33:35 -04006512 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6513 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6514 int off = 0, j;
6515
6516 netif_addr_lock_bh(dev);
6517 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6518 if (ether_addr_equal(eth->h_dest,
6519 vnic->uc_list + off)) {
6520 l2_idx = j + 1;
6521 break;
6522 }
6523 }
6524 netif_addr_unlock_bh(dev);
6525 if (!l2_idx)
6526 return -EINVAL;
6527 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006528 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6529 if (!new_fltr)
6530 return -ENOMEM;
6531
6532 fkeys = &new_fltr->fkeys;
6533 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6534 rc = -EPROTONOSUPPORT;
6535 goto err_free;
6536 }
6537
6538 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6539 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6540 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6541 rc = -EPROTONOSUPPORT;
6542 goto err_free;
6543 }
6544
Michael Chana54c4d72016-07-25 12:33:35 -04006545 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006546 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6547
6548 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6549 head = &bp->ntp_fltr_hash_tbl[idx];
6550 rcu_read_lock();
6551 hlist_for_each_entry_rcu(fltr, head, hash) {
6552 if (bnxt_fltr_match(fltr, new_fltr)) {
6553 rcu_read_unlock();
6554 rc = 0;
6555 goto err_free;
6556 }
6557 }
6558 rcu_read_unlock();
6559
6560 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006561 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6562 BNXT_NTP_FLTR_MAX_FLTR, 0);
6563 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006564 spin_unlock_bh(&bp->ntp_fltr_lock);
6565 rc = -ENOMEM;
6566 goto err_free;
6567 }
6568
Michael Chan84e86b92015-11-05 16:25:50 -05006569 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006570 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006571 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006572 new_fltr->rxq = rxq_index;
6573 hlist_add_head_rcu(&new_fltr->hash, head);
6574 bp->ntp_fltr_count++;
6575 spin_unlock_bh(&bp->ntp_fltr_lock);
6576
6577 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6578 schedule_work(&bp->sp_task);
6579
6580 return new_fltr->sw_id;
6581
6582err_free:
6583 kfree(new_fltr);
6584 return rc;
6585}
6586
6587static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6588{
6589 int i;
6590
6591 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6592 struct hlist_head *head;
6593 struct hlist_node *tmp;
6594 struct bnxt_ntuple_filter *fltr;
6595 int rc;
6596
6597 head = &bp->ntp_fltr_hash_tbl[i];
6598 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6599 bool del = false;
6600
6601 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6602 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6603 fltr->flow_id,
6604 fltr->sw_id)) {
6605 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6606 fltr);
6607 del = true;
6608 }
6609 } else {
6610 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6611 fltr);
6612 if (rc)
6613 del = true;
6614 else
6615 set_bit(BNXT_FLTR_VALID, &fltr->state);
6616 }
6617
6618 if (del) {
6619 spin_lock_bh(&bp->ntp_fltr_lock);
6620 hlist_del_rcu(&fltr->hash);
6621 bp->ntp_fltr_count--;
6622 spin_unlock_bh(&bp->ntp_fltr_lock);
6623 synchronize_rcu();
6624 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6625 kfree(fltr);
6626 }
6627 }
6628 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006629 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6630 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006631}
6632
6633#else
6634
6635static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6636{
6637}
6638
6639#endif /* CONFIG_RFS_ACCEL */
6640
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006641static void bnxt_udp_tunnel_add(struct net_device *dev,
6642 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006643{
6644 struct bnxt *bp = netdev_priv(dev);
6645
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006646 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6647 return;
6648
Michael Chanc0c050c2015-10-22 16:01:17 -04006649 if (!netif_running(dev))
6650 return;
6651
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006652 switch (ti->type) {
6653 case UDP_TUNNEL_TYPE_VXLAN:
6654 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6655 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006656
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006657 bp->vxlan_port_cnt++;
6658 if (bp->vxlan_port_cnt == 1) {
6659 bp->vxlan_port = ti->port;
6660 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006661 schedule_work(&bp->sp_task);
6662 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006663 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006664 case UDP_TUNNEL_TYPE_GENEVE:
6665 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6666 return;
6667
6668 bp->nge_port_cnt++;
6669 if (bp->nge_port_cnt == 1) {
6670 bp->nge_port = ti->port;
6671 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6672 }
6673 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006674 default:
6675 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006676 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006677
6678 schedule_work(&bp->sp_task);
6679}
6680
6681static void bnxt_udp_tunnel_del(struct net_device *dev,
6682 struct udp_tunnel_info *ti)
6683{
6684 struct bnxt *bp = netdev_priv(dev);
6685
6686 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6687 return;
6688
6689 if (!netif_running(dev))
6690 return;
6691
6692 switch (ti->type) {
6693 case UDP_TUNNEL_TYPE_VXLAN:
6694 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6695 return;
6696 bp->vxlan_port_cnt--;
6697
6698 if (bp->vxlan_port_cnt != 0)
6699 return;
6700
6701 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6702 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006703 case UDP_TUNNEL_TYPE_GENEVE:
6704 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6705 return;
6706 bp->nge_port_cnt--;
6707
6708 if (bp->nge_port_cnt != 0)
6709 return;
6710
6711 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6712 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006713 default:
6714 return;
6715 }
6716
6717 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006718}
6719
6720static const struct net_device_ops bnxt_netdev_ops = {
6721 .ndo_open = bnxt_open,
6722 .ndo_start_xmit = bnxt_start_xmit,
6723 .ndo_stop = bnxt_close,
6724 .ndo_get_stats64 = bnxt_get_stats64,
6725 .ndo_set_rx_mode = bnxt_set_rx_mode,
6726 .ndo_do_ioctl = bnxt_ioctl,
6727 .ndo_validate_addr = eth_validate_addr,
6728 .ndo_set_mac_address = bnxt_change_mac_addr,
6729 .ndo_change_mtu = bnxt_change_mtu,
6730 .ndo_fix_features = bnxt_fix_features,
6731 .ndo_set_features = bnxt_set_features,
6732 .ndo_tx_timeout = bnxt_tx_timeout,
6733#ifdef CONFIG_BNXT_SRIOV
6734 .ndo_get_vf_config = bnxt_get_vf_config,
6735 .ndo_set_vf_mac = bnxt_set_vf_mac,
6736 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6737 .ndo_set_vf_rate = bnxt_set_vf_bw,
6738 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6739 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6740#endif
6741#ifdef CONFIG_NET_POLL_CONTROLLER
6742 .ndo_poll_controller = bnxt_poll_controller,
6743#endif
6744 .ndo_setup_tc = bnxt_setup_tc,
6745#ifdef CONFIG_RFS_ACCEL
6746 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6747#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006748 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6749 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006750};
6751
6752static void bnxt_remove_one(struct pci_dev *pdev)
6753{
6754 struct net_device *dev = pci_get_drvdata(pdev);
6755 struct bnxt *bp = netdev_priv(dev);
6756
6757 if (BNXT_PF(bp))
6758 bnxt_sriov_disable(bp);
6759
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006760 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006761 unregister_netdev(dev);
6762 cancel_work_sync(&bp->sp_task);
6763 bp->sp_event = 0;
6764
Michael Chan78095922016-12-07 00:26:16 -05006765 bnxt_clear_int_mode(bp);
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006766 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006767 bnxt_free_hwrm_resources(bp);
Michael Chan7df4ae92016-12-02 21:17:17 -05006768 bnxt_dcb_free(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006769 pci_iounmap(pdev, bp->bar2);
6770 pci_iounmap(pdev, bp->bar1);
6771 pci_iounmap(pdev, bp->bar0);
Michael Chana588e452016-12-07 00:26:21 -05006772 kfree(bp->edev);
6773 bp->edev = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04006774 free_netdev(dev);
6775
6776 pci_release_regions(pdev);
6777 pci_disable_device(pdev);
6778}
6779
6780static int bnxt_probe_phy(struct bnxt *bp)
6781{
6782 int rc = 0;
6783 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006784
Michael Chan170ce012016-04-05 14:08:57 -04006785 rc = bnxt_hwrm_phy_qcaps(bp);
6786 if (rc) {
6787 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6788 rc);
6789 return rc;
6790 }
6791
Michael Chanc0c050c2015-10-22 16:01:17 -04006792 rc = bnxt_update_link(bp, false);
6793 if (rc) {
6794 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6795 rc);
6796 return rc;
6797 }
6798
Michael Chan93ed8112016-06-13 02:25:37 -04006799 /* Older firmware does not have supported_auto_speeds, so assume
6800 * that all supported speeds can be autonegotiated.
6801 */
6802 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6803 link_info->support_auto_speeds = link_info->support_speeds;
6804
Michael Chanc0c050c2015-10-22 16:01:17 -04006805 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006806 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006807 link_info->autoneg = BNXT_AUTONEG_SPEED;
6808 if (bp->hwrm_spec_code >= 0x10201) {
6809 if (link_info->auto_pause_setting &
6810 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6811 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6812 } else {
6813 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6814 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006815 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006816 } else {
6817 link_info->req_link_speed = link_info->force_link_speed;
6818 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006819 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006820 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6821 link_info->req_flow_ctrl =
6822 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6823 else
6824 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006825 return rc;
6826}
6827
6828static int bnxt_get_max_irq(struct pci_dev *pdev)
6829{
6830 u16 ctrl;
6831
6832 if (!pdev->msix_cap)
6833 return 1;
6834
6835 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6836 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6837}
6838
Michael Chan6e6c5a52016-01-02 23:45:02 -05006839static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6840 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006841{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006842 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006843
Michael Chan379a80a2015-10-23 15:06:19 -04006844#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006845 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006846 *max_tx = bp->vf.max_tx_rings;
6847 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006848 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6849 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006850 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006851 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006852#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006853 {
6854 *max_tx = bp->pf.max_tx_rings;
6855 *max_rx = bp->pf.max_rx_rings;
6856 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6857 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6858 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006859 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006860 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6861 *max_cp -= 1;
6862 *max_rx -= 2;
6863 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006864 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6865 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006866 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006867}
6868
6869int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6870{
6871 int rx, tx, cp;
6872
6873 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6874 if (!rx || !tx || !cp)
6875 return -ENOMEM;
6876
6877 *max_rx = rx;
6878 *max_tx = tx;
6879 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6880}
6881
Michael Chane4060d32016-12-07 00:26:19 -05006882static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6883 bool shared)
6884{
6885 int rc;
6886
6887 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
6888 if (rc)
6889 return rc;
6890
6891 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
6892 int max_cp, max_stat, max_irq;
6893
6894 /* Reserve minimum resources for RoCE */
6895 max_cp = bnxt_get_max_func_cp_rings(bp);
6896 max_stat = bnxt_get_max_func_stat_ctxs(bp);
6897 max_irq = bnxt_get_max_func_irqs(bp);
6898 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
6899 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
6900 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
6901 return 0;
6902
6903 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
6904 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
6905 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
6906 max_cp = min_t(int, max_cp, max_irq);
6907 max_cp = min_t(int, max_cp, max_stat);
6908 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
6909 if (rc)
6910 rc = 0;
6911 }
6912 return rc;
6913}
6914
Michael Chan6e6c5a52016-01-02 23:45:02 -05006915static int bnxt_set_dflt_rings(struct bnxt *bp)
6916{
6917 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6918 bool sh = true;
6919
6920 if (sh)
6921 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6922 dflt_rings = netif_get_num_default_rss_queues();
Michael Chane4060d32016-12-07 00:26:19 -05006923 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006924 if (rc)
6925 return rc;
6926 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6927 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6928 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6929 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6930 bp->tx_nr_rings + bp->rx_nr_rings;
6931 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006932 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6933 bp->rx_nr_rings++;
6934 bp->cp_nr_rings++;
6935 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006936 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006937}
6938
Michael Chan7b08f662016-12-07 00:26:18 -05006939void bnxt_restore_pf_fw_resources(struct bnxt *bp)
6940{
6941 ASSERT_RTNL();
6942 bnxt_hwrm_func_qcaps(bp);
Michael Chana588e452016-12-07 00:26:21 -05006943 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
Michael Chan7b08f662016-12-07 00:26:18 -05006944}
6945
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006946static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6947{
6948 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6949 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6950
6951 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6952 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6953 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6954 else
6955 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6956 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6957 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6958 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6959 "Unknown", width);
6960}
6961
Michael Chanc0c050c2015-10-22 16:01:17 -04006962static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6963{
6964 static int version_printed;
6965 struct net_device *dev;
6966 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006967 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006968
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04006969 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6970 return -ENODEV;
6971
Michael Chanc0c050c2015-10-22 16:01:17 -04006972 if (version_printed++ == 0)
6973 pr_info("%s", version);
6974
6975 max_irqs = bnxt_get_max_irq(pdev);
6976 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6977 if (!dev)
6978 return -ENOMEM;
6979
6980 bp = netdev_priv(dev);
6981
6982 if (bnxt_vf_pciid(ent->driver_data))
6983 bp->flags |= BNXT_FLAG_VF;
6984
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006985 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006986 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006987
6988 rc = bnxt_init_board(pdev, dev);
6989 if (rc < 0)
6990 goto init_err_free;
6991
6992 dev->netdev_ops = &bnxt_netdev_ops;
6993 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6994 dev->ethtool_ops = &bnxt_ethtool_ops;
6995
6996 pci_set_drvdata(pdev, dev);
6997
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006998 rc = bnxt_alloc_hwrm_resources(bp);
6999 if (rc)
7000 goto init_err;
7001
7002 mutex_init(&bp->hwrm_cmd_lock);
7003 rc = bnxt_hwrm_ver_get(bp);
7004 if (rc)
7005 goto init_err;
7006
Rob Swindell5ac67d82016-09-19 03:58:03 -04007007 bnxt_hwrm_fw_set_time(bp);
7008
Michael Chanc0c050c2015-10-22 16:01:17 -04007009 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7010 NETIF_F_TSO | NETIF_F_TSO6 |
7011 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07007012 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07007013 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7014 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007015 NETIF_F_RXCSUM | NETIF_F_GRO;
7016
7017 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7018 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04007019
Michael Chanc0c050c2015-10-22 16:01:17 -04007020 dev->hw_enc_features =
7021 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7022 NETIF_F_TSO | NETIF_F_TSO6 |
7023 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07007024 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07007025 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07007026 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7027 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04007028 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7029 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7030 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7031 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7032 dev->priv_flags |= IFF_UNICAST_FLT;
7033
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04007034 /* MTU range: 60 - 9500 */
7035 dev->min_mtu = ETH_ZLEN;
7036 dev->max_mtu = 9500;
7037
Michael Chan7df4ae92016-12-02 21:17:17 -05007038 bnxt_dcb_init(bp);
7039
Michael Chanc0c050c2015-10-22 16:01:17 -04007040#ifdef CONFIG_BNXT_SRIOV
7041 init_waitqueue_head(&bp->sriov_cfg_wait);
7042#endif
Michael Chan309369c2016-06-13 02:25:34 -04007043 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04007044 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
7045 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04007046
Michael Chanc0c050c2015-10-22 16:01:17 -04007047 rc = bnxt_hwrm_func_drv_rgtr(bp);
7048 if (rc)
7049 goto init_err;
7050
Michael Chana1653b12016-12-07 00:26:20 -05007051 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7052 if (rc)
7053 goto init_err;
7054
Michael Chana588e452016-12-07 00:26:21 -05007055 bp->ulp_probe = bnxt_ulp_probe;
7056
Michael Chanc0c050c2015-10-22 16:01:17 -04007057 /* Get the MAX capabilities for this function */
7058 rc = bnxt_hwrm_func_qcaps(bp);
7059 if (rc) {
7060 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7061 rc);
7062 rc = -1;
7063 goto init_err;
7064 }
7065
7066 rc = bnxt_hwrm_queue_qportcfg(bp);
7067 if (rc) {
7068 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7069 rc);
7070 rc = -1;
7071 goto init_err;
7072 }
7073
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04007074 bnxt_hwrm_func_qcfg(bp);
7075
Michael Chanc0c050c2015-10-22 16:01:17 -04007076 bnxt_set_tpa_flags(bp);
7077 bnxt_set_ring_params(bp);
Michael Chan33c26572016-12-07 00:26:15 -05007078 bnxt_set_max_func_irqs(bp, max_irqs);
Michael Chan6e6c5a52016-01-02 23:45:02 -05007079 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007080
Michael Chan87da7f72016-11-16 21:13:09 -05007081 /* Default RSS hash cfg. */
7082 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7083 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7084 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7085 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7086 if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
7087 !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7088 bp->hwrm_spec_code >= 0x10501) {
7089 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7090 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7091 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7092 }
7093
Michael Chan8fdefd62016-12-29 12:13:36 -05007094 bnxt_hwrm_vnic_qcaps(bp);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04007095 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05007096 dev->hw_features |= NETIF_F_NTUPLE;
7097 if (bnxt_rfs_capable(bp)) {
7098 bp->flags |= BNXT_FLAG_RFS;
7099 dev->features |= NETIF_F_NTUPLE;
7100 }
7101 }
7102
Michael Chanc0c050c2015-10-22 16:01:17 -04007103 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7104 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7105
7106 rc = bnxt_probe_phy(bp);
7107 if (rc)
7108 goto init_err;
7109
Michael Chanaa8ed022016-12-07 00:26:17 -05007110 rc = bnxt_hwrm_func_reset(bp);
7111 if (rc)
7112 goto init_err;
7113
Michael Chan78095922016-12-07 00:26:16 -05007114 rc = bnxt_init_int_mode(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04007115 if (rc)
7116 goto init_err;
7117
Michael Chan78095922016-12-07 00:26:16 -05007118 rc = register_netdev(dev);
7119 if (rc)
7120 goto init_err_clr_int;
7121
Michael Chanc0c050c2015-10-22 16:01:17 -04007122 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7123 board_info[ent->driver_data].name,
7124 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7125
Ajit Khaparde90c4f782016-05-15 03:04:45 -04007126 bnxt_parse_log_pcie_link(bp);
7127
Michael Chanc0c050c2015-10-22 16:01:17 -04007128 return 0;
7129
Michael Chan78095922016-12-07 00:26:16 -05007130init_err_clr_int:
7131 bnxt_clear_int_mode(bp);
7132
Michael Chanc0c050c2015-10-22 16:01:17 -04007133init_err:
7134 pci_iounmap(pdev, bp->bar0);
7135 pci_release_regions(pdev);
7136 pci_disable_device(pdev);
7137
7138init_err_free:
7139 free_netdev(dev);
7140 return rc;
7141}
7142
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007143/**
7144 * bnxt_io_error_detected - called when PCI error is detected
7145 * @pdev: Pointer to PCI device
7146 * @state: The current pci connection state
7147 *
7148 * This function is called after a PCI bus error affecting
7149 * this device has been detected.
7150 */
7151static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7152 pci_channel_state_t state)
7153{
7154 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chana588e452016-12-07 00:26:21 -05007155 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007156
7157 netdev_info(netdev, "PCI I/O error detected\n");
7158
7159 rtnl_lock();
7160 netif_device_detach(netdev);
7161
Michael Chana588e452016-12-07 00:26:21 -05007162 bnxt_ulp_stop(bp);
7163
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007164 if (state == pci_channel_io_perm_failure) {
7165 rtnl_unlock();
7166 return PCI_ERS_RESULT_DISCONNECT;
7167 }
7168
7169 if (netif_running(netdev))
7170 bnxt_close(netdev);
7171
7172 pci_disable_device(pdev);
7173 rtnl_unlock();
7174
7175 /* Request a slot slot reset. */
7176 return PCI_ERS_RESULT_NEED_RESET;
7177}
7178
7179/**
7180 * bnxt_io_slot_reset - called after the pci bus has been reset.
7181 * @pdev: Pointer to PCI device
7182 *
7183 * Restart the card from scratch, as if from a cold-boot.
7184 * At this point, the card has exprienced a hard reset,
7185 * followed by fixups by BIOS, and has its config space
7186 * set up identically to what it was at cold boot.
7187 */
7188static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7189{
7190 struct net_device *netdev = pci_get_drvdata(pdev);
7191 struct bnxt *bp = netdev_priv(netdev);
7192 int err = 0;
7193 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7194
7195 netdev_info(bp->dev, "PCI Slot Reset\n");
7196
7197 rtnl_lock();
7198
7199 if (pci_enable_device(pdev)) {
7200 dev_err(&pdev->dev,
7201 "Cannot re-enable PCI device after reset.\n");
7202 } else {
7203 pci_set_master(pdev);
7204
Michael Chanaa8ed022016-12-07 00:26:17 -05007205 err = bnxt_hwrm_func_reset(bp);
7206 if (!err && netif_running(netdev))
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007207 err = bnxt_open(netdev);
7208
Michael Chana588e452016-12-07 00:26:21 -05007209 if (!err) {
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007210 result = PCI_ERS_RESULT_RECOVERED;
Michael Chana588e452016-12-07 00:26:21 -05007211 bnxt_ulp_start(bp);
7212 }
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007213 }
7214
7215 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7216 dev_close(netdev);
7217
7218 rtnl_unlock();
7219
7220 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7221 if (err) {
7222 dev_err(&pdev->dev,
7223 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7224 err); /* non-fatal, continue */
7225 }
7226
7227 return PCI_ERS_RESULT_RECOVERED;
7228}
7229
7230/**
7231 * bnxt_io_resume - called when traffic can start flowing again.
7232 * @pdev: Pointer to PCI device
7233 *
7234 * This callback is called when the error recovery driver tells
7235 * us that its OK to resume normal operation.
7236 */
7237static void bnxt_io_resume(struct pci_dev *pdev)
7238{
7239 struct net_device *netdev = pci_get_drvdata(pdev);
7240
7241 rtnl_lock();
7242
7243 netif_device_attach(netdev);
7244
7245 rtnl_unlock();
7246}
7247
7248static const struct pci_error_handlers bnxt_err_handler = {
7249 .error_detected = bnxt_io_error_detected,
7250 .slot_reset = bnxt_io_slot_reset,
7251 .resume = bnxt_io_resume
7252};
7253
Michael Chanc0c050c2015-10-22 16:01:17 -04007254static struct pci_driver bnxt_pci_driver = {
7255 .name = DRV_MODULE_NAME,
7256 .id_table = bnxt_pci_tbl,
7257 .probe = bnxt_init_one,
7258 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007259 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007260#if defined(CONFIG_BNXT_SRIOV)
7261 .sriov_configure = bnxt_sriov_configure,
7262#endif
7263};
7264
7265module_pci_driver(bnxt_pci_driver);