blob: 4db50e831524447ab47fd73227f2614fe6b96f2b [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#ifdef CONFIG_NET_RX_BUSY_POLL
43#include <net/busy_poll.h>
44#endif
45#include <linux/workqueue.h>
46#include <linux/prefetch.h>
47#include <linux/cache.h>
48#include <linux/log2.h>
49#include <linux/aer.h>
50#include <linux/bitmap.h>
51#include <linux/cpu_rmap.h>
52
53#include "bnxt_hsi.h"
54#include "bnxt.h"
55#include "bnxt_sriov.h"
56#include "bnxt_ethtool.h"
57
58#define BNXT_TX_TIMEOUT (5 * HZ)
59
60static const char version[] =
61 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
62
63MODULE_LICENSE("GPL");
64MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
65MODULE_VERSION(DRV_MODULE_VERSION);
66
67#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
68#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
69#define BNXT_RX_COPY_THRESH 256
70
Michael Chan4419dbe2016-02-10 17:33:49 -050071#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040072
73enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050074 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040075 BCM57302,
76 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040077 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040078 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040079 BCM57311,
80 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050081 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040082 BCM57404,
83 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040084 BCM57402_NPAR,
85 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040086 BCM57412,
87 BCM57414,
88 BCM57416,
89 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040090 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040091 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040092 BCM57417_SFP,
93 BCM57416_SFP,
94 BCM57404_NPAR,
95 BCM57406_NPAR,
96 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040097 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040098 BCM57414_NPAR,
99 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -0400100 NETXTREME_E_VF,
101 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400102};
103
104/* indexed by enum above */
105static const struct {
106 char *name;
107} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400108 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
109 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
110 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400111 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400112 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
113 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
114 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
115 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
116 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
117 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400118 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400119 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
120 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
121 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
122 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
123 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400124 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400125 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
126 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
127 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400128 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
129 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400130 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
131 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400132 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400134 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400136};
137
138static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400139 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500140 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400141 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
142 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400144 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400145 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
146 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500147 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400148 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
149 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400150 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
151 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400152 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
153 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
154 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
155 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400156 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400157 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400158 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
160 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
162 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400163 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
164 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400168 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400169#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400170 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
175 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400176#endif
177 { 0 }
178};
179
180MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
181
182static const u16 bnxt_vf_req_snif[] = {
183 HWRM_FUNC_CFG,
184 HWRM_PORT_PHY_QCFG,
185 HWRM_CFA_L2_FILTER_ALLOC,
186};
187
Michael Chan25be8622016-04-05 14:09:00 -0400188static const u16 bnxt_async_events_arr[] = {
189 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
190 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
Michael Chan4bb13ab2016-04-05 14:09:01 -0400191 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
Michael Chanfc0f1922016-06-13 02:25:30 -0400192 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
Michael Chan8cbde112016-04-11 04:11:14 -0400193 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400194};
195
Michael Chanc0c050c2015-10-22 16:01:17 -0400196static bool bnxt_vf_pciid(enum board_idx idx)
197{
Michael Chanadbc8302016-09-19 03:58:01 -0400198 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400199}
200
201#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
202#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
203#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
204
205#define BNXT_CP_DB_REARM(db, raw_cons) \
206 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
207
208#define BNXT_CP_DB(db, raw_cons) \
209 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
210
211#define BNXT_CP_DB_IRQ_DIS(db) \
212 writel(DB_CP_IRQ_DIS_FLAGS, db)
213
214static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
215{
216 /* Tell compiler to fetch tx indices from memory. */
217 barrier();
218
219 return bp->tx_ring_size -
220 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
221}
222
223static const u16 bnxt_lhint_arr[] = {
224 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
225 TX_BD_FLAGS_LHINT_512_TO_1023,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_1024_TO_2047,
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 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243};
244
245static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
246{
247 struct bnxt *bp = netdev_priv(dev);
248 struct tx_bd *txbd;
249 struct tx_bd_ext *txbd1;
250 struct netdev_queue *txq;
251 int i;
252 dma_addr_t mapping;
253 unsigned int length, pad = 0;
254 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
255 u16 prod, last_frag;
256 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400257 struct bnxt_tx_ring_info *txr;
258 struct bnxt_sw_tx_bd *tx_buf;
259
260 i = skb_get_queue_mapping(skb);
261 if (unlikely(i >= bp->tx_nr_rings)) {
262 dev_kfree_skb_any(skb);
263 return NETDEV_TX_OK;
264 }
265
Michael Chanb6ab4b02016-01-02 23:44:59 -0500266 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400267 txq = netdev_get_tx_queue(dev, i);
268 prod = txr->tx_prod;
269
270 free_size = bnxt_tx_avail(bp, txr);
271 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
272 netif_tx_stop_queue(txq);
273 return NETDEV_TX_BUSY;
274 }
275
276 length = skb->len;
277 len = skb_headlen(skb);
278 last_frag = skb_shinfo(skb)->nr_frags;
279
280 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
281
282 txbd->tx_bd_opaque = prod;
283
284 tx_buf = &txr->tx_buf_ring[prod];
285 tx_buf->skb = skb;
286 tx_buf->nr_frags = last_frag;
287
288 vlan_tag_flags = 0;
289 cfa_action = 0;
290 if (skb_vlan_tag_present(skb)) {
291 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
292 skb_vlan_tag_get(skb);
293 /* Currently supports 8021Q, 8021AD vlan offloads
294 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
295 */
296 if (skb->vlan_proto == htons(ETH_P_8021Q))
297 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
298 }
299
300 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500301 struct tx_push_buffer *tx_push_buf = txr->tx_push;
302 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
303 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
304 void *pdata = tx_push_buf->data;
305 u64 *end;
306 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400307
308 /* Set COAL_NOW to be ready quickly for the next push */
309 tx_push->tx_bd_len_flags_type =
310 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
311 TX_BD_TYPE_LONG_TX_BD |
312 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
313 TX_BD_FLAGS_COAL_NOW |
314 TX_BD_FLAGS_PACKET_END |
315 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
316
317 if (skb->ip_summed == CHECKSUM_PARTIAL)
318 tx_push1->tx_bd_hsize_lflags =
319 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
320 else
321 tx_push1->tx_bd_hsize_lflags = 0;
322
323 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
324 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
325
Michael Chanfbb0fa82016-02-22 02:10:26 -0500326 end = pdata + length;
327 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500328 *end = 0;
329
Michael Chanc0c050c2015-10-22 16:01:17 -0400330 skb_copy_from_linear_data(skb, pdata, len);
331 pdata += len;
332 for (j = 0; j < last_frag; j++) {
333 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
334 void *fptr;
335
336 fptr = skb_frag_address_safe(frag);
337 if (!fptr)
338 goto normal_tx;
339
340 memcpy(pdata, fptr, skb_frag_size(frag));
341 pdata += skb_frag_size(frag);
342 }
343
Michael Chan4419dbe2016-02-10 17:33:49 -0500344 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
345 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400346 prod = NEXT_TX(prod);
347 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
348 memcpy(txbd, tx_push1, sizeof(*txbd));
349 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500350 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400351 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
352 txr->tx_prod = prod;
353
Michael Chanb9a84602016-06-06 02:37:14 -0400354 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400355 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400356 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400357
Michael Chan4419dbe2016-02-10 17:33:49 -0500358 push_len = (length + sizeof(*tx_push) + 7) / 8;
359 if (push_len > 16) {
360 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400361 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
362 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500363 } else {
364 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
365 push_len);
366 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400367
Michael Chanc0c050c2015-10-22 16:01:17 -0400368 goto tx_done;
369 }
370
371normal_tx:
372 if (length < BNXT_MIN_PKT_SIZE) {
373 pad = BNXT_MIN_PKT_SIZE - length;
374 if (skb_pad(skb, pad)) {
375 /* SKB already freed. */
376 tx_buf->skb = NULL;
377 return NETDEV_TX_OK;
378 }
379 length = BNXT_MIN_PKT_SIZE;
380 }
381
382 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
383
384 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
385 dev_kfree_skb_any(skb);
386 tx_buf->skb = NULL;
387 return NETDEV_TX_OK;
388 }
389
390 dma_unmap_addr_set(tx_buf, mapping, mapping);
391 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
392 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
393
394 txbd->tx_bd_haddr = cpu_to_le64(mapping);
395
396 prod = NEXT_TX(prod);
397 txbd1 = (struct tx_bd_ext *)
398 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
399
400 txbd1->tx_bd_hsize_lflags = 0;
401 if (skb_is_gso(skb)) {
402 u32 hdr_len;
403
404 if (skb->encapsulation)
405 hdr_len = skb_inner_network_offset(skb) +
406 skb_inner_network_header_len(skb) +
407 inner_tcp_hdrlen(skb);
408 else
409 hdr_len = skb_transport_offset(skb) +
410 tcp_hdrlen(skb);
411
412 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
413 TX_BD_FLAGS_T_IPID |
414 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
415 length = skb_shinfo(skb)->gso_size;
416 txbd1->tx_bd_mss = cpu_to_le32(length);
417 length += hdr_len;
418 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
419 txbd1->tx_bd_hsize_lflags =
420 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
421 txbd1->tx_bd_mss = 0;
422 }
423
424 length >>= 9;
425 flags |= bnxt_lhint_arr[length];
426 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
427
428 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
429 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
430 for (i = 0; i < last_frag; i++) {
431 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
432
433 prod = NEXT_TX(prod);
434 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
435
436 len = skb_frag_size(frag);
437 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
438 DMA_TO_DEVICE);
439
440 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
441 goto tx_dma_error;
442
443 tx_buf = &txr->tx_buf_ring[prod];
444 dma_unmap_addr_set(tx_buf, mapping, mapping);
445
446 txbd->tx_bd_haddr = cpu_to_le64(mapping);
447
448 flags = len << TX_BD_LEN_SHIFT;
449 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
450 }
451
452 flags &= ~TX_BD_LEN;
453 txbd->tx_bd_len_flags_type =
454 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
455 TX_BD_FLAGS_PACKET_END);
456
457 netdev_tx_sent_queue(txq, skb->len);
458
459 /* Sync BD data before updating doorbell */
460 wmb();
461
462 prod = NEXT_TX(prod);
463 txr->tx_prod = prod;
464
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466 writel(DB_KEY_TX | prod, txr->tx_doorbell);
467
468tx_done:
469
470 mmiowb();
471
472 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
473 netif_tx_stop_queue(txq);
474
475 /* netif_tx_stop_queue() must be done before checking
476 * tx index in bnxt_tx_avail() below, because in
477 * bnxt_tx_int(), we update tx index before checking for
478 * netif_tx_queue_stopped().
479 */
480 smp_mb();
481 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
482 netif_tx_wake_queue(txq);
483 }
484 return NETDEV_TX_OK;
485
486tx_dma_error:
487 last_frag = i;
488
489 /* start back at beginning and unmap skb */
490 prod = txr->tx_prod;
491 tx_buf = &txr->tx_buf_ring[prod];
492 tx_buf->skb = NULL;
493 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
494 skb_headlen(skb), PCI_DMA_TODEVICE);
495 prod = NEXT_TX(prod);
496
497 /* unmap remaining mapped pages */
498 for (i = 0; i < last_frag; i++) {
499 prod = NEXT_TX(prod);
500 tx_buf = &txr->tx_buf_ring[prod];
501 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
502 skb_frag_size(&skb_shinfo(skb)->frags[i]),
503 PCI_DMA_TODEVICE);
504 }
505
506 dev_kfree_skb_any(skb);
507 return NETDEV_TX_OK;
508}
509
510static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
511{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500512 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500513 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400514 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
515 u16 cons = txr->tx_cons;
516 struct pci_dev *pdev = bp->pdev;
517 int i;
518 unsigned int tx_bytes = 0;
519
520 for (i = 0; i < nr_pkts; i++) {
521 struct bnxt_sw_tx_bd *tx_buf;
522 struct sk_buff *skb;
523 int j, last;
524
525 tx_buf = &txr->tx_buf_ring[cons];
526 cons = NEXT_TX(cons);
527 skb = tx_buf->skb;
528 tx_buf->skb = NULL;
529
530 if (tx_buf->is_push) {
531 tx_buf->is_push = 0;
532 goto next_tx_int;
533 }
534
535 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
536 skb_headlen(skb), PCI_DMA_TODEVICE);
537 last = tx_buf->nr_frags;
538
539 for (j = 0; j < last; j++) {
540 cons = NEXT_TX(cons);
541 tx_buf = &txr->tx_buf_ring[cons];
542 dma_unmap_page(
543 &pdev->dev,
544 dma_unmap_addr(tx_buf, mapping),
545 skb_frag_size(&skb_shinfo(skb)->frags[j]),
546 PCI_DMA_TODEVICE);
547 }
548
549next_tx_int:
550 cons = NEXT_TX(cons);
551
552 tx_bytes += skb->len;
553 dev_kfree_skb_any(skb);
554 }
555
556 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
557 txr->tx_cons = cons;
558
559 /* Need to make the tx_cons update visible to bnxt_start_xmit()
560 * before checking for netif_tx_queue_stopped(). Without the
561 * memory barrier, there is a small possibility that bnxt_start_xmit()
562 * will miss it and cause the queue to be stopped forever.
563 */
564 smp_mb();
565
566 if (unlikely(netif_tx_queue_stopped(txq)) &&
567 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
568 __netif_tx_lock(txq, smp_processor_id());
569 if (netif_tx_queue_stopped(txq) &&
570 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
571 txr->dev_state != BNXT_DEV_STATE_CLOSING)
572 netif_tx_wake_queue(txq);
573 __netif_tx_unlock(txq);
574 }
575}
576
577static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
578 gfp_t gfp)
579{
580 u8 *data;
581 struct pci_dev *pdev = bp->pdev;
582
583 data = kmalloc(bp->rx_buf_size, gfp);
584 if (!data)
585 return NULL;
586
587 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
588 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
589
590 if (dma_mapping_error(&pdev->dev, *mapping)) {
591 kfree(data);
592 data = NULL;
593 }
594 return data;
595}
596
597static inline int bnxt_alloc_rx_data(struct bnxt *bp,
598 struct bnxt_rx_ring_info *rxr,
599 u16 prod, gfp_t gfp)
600{
601 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
602 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
603 u8 *data;
604 dma_addr_t mapping;
605
606 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
607 if (!data)
608 return -ENOMEM;
609
610 rx_buf->data = data;
611 dma_unmap_addr_set(rx_buf, mapping, mapping);
612
613 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
614
615 return 0;
616}
617
618static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
619 u8 *data)
620{
621 u16 prod = rxr->rx_prod;
622 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
623 struct rx_bd *cons_bd, *prod_bd;
624
625 prod_rx_buf = &rxr->rx_buf_ring[prod];
626 cons_rx_buf = &rxr->rx_buf_ring[cons];
627
628 prod_rx_buf->data = data;
629
630 dma_unmap_addr_set(prod_rx_buf, mapping,
631 dma_unmap_addr(cons_rx_buf, mapping));
632
633 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
634 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
635
636 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
637}
638
639static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
640{
641 u16 next, max = rxr->rx_agg_bmap_size;
642
643 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
644 if (next >= max)
645 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
646 return next;
647}
648
649static inline int bnxt_alloc_rx_page(struct bnxt *bp,
650 struct bnxt_rx_ring_info *rxr,
651 u16 prod, gfp_t gfp)
652{
653 struct rx_bd *rxbd =
654 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
655 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
656 struct pci_dev *pdev = bp->pdev;
657 struct page *page;
658 dma_addr_t mapping;
659 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400660 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400661
Michael Chan89d0a062016-04-25 02:30:51 -0400662 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
663 page = rxr->rx_page;
664 if (!page) {
665 page = alloc_page(gfp);
666 if (!page)
667 return -ENOMEM;
668 rxr->rx_page = page;
669 rxr->rx_page_offset = 0;
670 }
671 offset = rxr->rx_page_offset;
672 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
673 if (rxr->rx_page_offset == PAGE_SIZE)
674 rxr->rx_page = NULL;
675 else
676 get_page(page);
677 } else {
678 page = alloc_page(gfp);
679 if (!page)
680 return -ENOMEM;
681 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400682
Michael Chan89d0a062016-04-25 02:30:51 -0400683 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400684 PCI_DMA_FROMDEVICE);
685 if (dma_mapping_error(&pdev->dev, mapping)) {
686 __free_page(page);
687 return -EIO;
688 }
689
690 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
691 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
692
693 __set_bit(sw_prod, rxr->rx_agg_bmap);
694 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
695 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
696
697 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400698 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400699 rx_agg_buf->mapping = mapping;
700 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
701 rxbd->rx_bd_opaque = sw_prod;
702 return 0;
703}
704
705static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
706 u32 agg_bufs)
707{
708 struct bnxt *bp = bnapi->bp;
709 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500710 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400711 u16 prod = rxr->rx_agg_prod;
712 u16 sw_prod = rxr->rx_sw_agg_prod;
713 u32 i;
714
715 for (i = 0; i < agg_bufs; i++) {
716 u16 cons;
717 struct rx_agg_cmp *agg;
718 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
719 struct rx_bd *prod_bd;
720 struct page *page;
721
722 agg = (struct rx_agg_cmp *)
723 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
724 cons = agg->rx_agg_cmp_opaque;
725 __clear_bit(cons, rxr->rx_agg_bmap);
726
727 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
728 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
729
730 __set_bit(sw_prod, rxr->rx_agg_bmap);
731 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
732 cons_rx_buf = &rxr->rx_agg_ring[cons];
733
734 /* It is possible for sw_prod to be equal to cons, so
735 * set cons_rx_buf->page to NULL first.
736 */
737 page = cons_rx_buf->page;
738 cons_rx_buf->page = NULL;
739 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400740 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400741
742 prod_rx_buf->mapping = cons_rx_buf->mapping;
743
744 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
745
746 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
747 prod_bd->rx_bd_opaque = sw_prod;
748
749 prod = NEXT_RX_AGG(prod);
750 sw_prod = NEXT_RX_AGG(sw_prod);
751 cp_cons = NEXT_CMP(cp_cons);
752 }
753 rxr->rx_agg_prod = prod;
754 rxr->rx_sw_agg_prod = sw_prod;
755}
756
757static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
758 struct bnxt_rx_ring_info *rxr, u16 cons,
759 u16 prod, u8 *data, dma_addr_t dma_addr,
760 unsigned int len)
761{
762 int err;
763 struct sk_buff *skb;
764
765 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
766 if (unlikely(err)) {
767 bnxt_reuse_rx_data(rxr, cons, data);
768 return NULL;
769 }
770
771 skb = build_skb(data, 0);
772 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
773 PCI_DMA_FROMDEVICE);
774 if (!skb) {
775 kfree(data);
776 return NULL;
777 }
778
779 skb_reserve(skb, BNXT_RX_OFFSET);
780 skb_put(skb, len);
781 return skb;
782}
783
784static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
785 struct sk_buff *skb, u16 cp_cons,
786 u32 agg_bufs)
787{
788 struct pci_dev *pdev = bp->pdev;
789 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500790 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400791 u16 prod = rxr->rx_agg_prod;
792 u32 i;
793
794 for (i = 0; i < agg_bufs; i++) {
795 u16 cons, frag_len;
796 struct rx_agg_cmp *agg;
797 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
798 struct page *page;
799 dma_addr_t mapping;
800
801 agg = (struct rx_agg_cmp *)
802 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
803 cons = agg->rx_agg_cmp_opaque;
804 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
805 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
806
807 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400808 skb_fill_page_desc(skb, i, cons_rx_buf->page,
809 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400810 __clear_bit(cons, rxr->rx_agg_bmap);
811
812 /* It is possible for bnxt_alloc_rx_page() to allocate
813 * a sw_prod index that equals the cons index, so we
814 * need to clear the cons entry now.
815 */
816 mapping = dma_unmap_addr(cons_rx_buf, mapping);
817 page = cons_rx_buf->page;
818 cons_rx_buf->page = NULL;
819
820 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
821 struct skb_shared_info *shinfo;
822 unsigned int nr_frags;
823
824 shinfo = skb_shinfo(skb);
825 nr_frags = --shinfo->nr_frags;
826 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
827
828 dev_kfree_skb(skb);
829
830 cons_rx_buf->page = page;
831
832 /* Update prod since possibly some pages have been
833 * allocated already.
834 */
835 rxr->rx_agg_prod = prod;
836 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
837 return NULL;
838 }
839
Michael Chan2839f282016-04-25 02:30:50 -0400840 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400841 PCI_DMA_FROMDEVICE);
842
843 skb->data_len += frag_len;
844 skb->len += frag_len;
845 skb->truesize += PAGE_SIZE;
846
847 prod = NEXT_RX_AGG(prod);
848 cp_cons = NEXT_CMP(cp_cons);
849 }
850 rxr->rx_agg_prod = prod;
851 return skb;
852}
853
854static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
855 u8 agg_bufs, u32 *raw_cons)
856{
857 u16 last;
858 struct rx_agg_cmp *agg;
859
860 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
861 last = RING_CMP(*raw_cons);
862 agg = (struct rx_agg_cmp *)
863 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
864 return RX_AGG_CMP_VALID(agg, *raw_cons);
865}
866
867static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
868 unsigned int len,
869 dma_addr_t mapping)
870{
871 struct bnxt *bp = bnapi->bp;
872 struct pci_dev *pdev = bp->pdev;
873 struct sk_buff *skb;
874
875 skb = napi_alloc_skb(&bnapi->napi, len);
876 if (!skb)
877 return NULL;
878
879 dma_sync_single_for_cpu(&pdev->dev, mapping,
880 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
881
882 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
883
884 dma_sync_single_for_device(&pdev->dev, mapping,
885 bp->rx_copy_thresh,
886 PCI_DMA_FROMDEVICE);
887
888 skb_put(skb, len);
889 return skb;
890}
891
Michael Chanfa7e2812016-05-10 19:18:00 -0400892static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
893 u32 *raw_cons, void *cmp)
894{
895 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
896 struct rx_cmp *rxcmp = cmp;
897 u32 tmp_raw_cons = *raw_cons;
898 u8 cmp_type, agg_bufs = 0;
899
900 cmp_type = RX_CMP_TYPE(rxcmp);
901
902 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
903 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
904 RX_CMP_AGG_BUFS) >>
905 RX_CMP_AGG_BUFS_SHIFT;
906 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
907 struct rx_tpa_end_cmp *tpa_end = cmp;
908
909 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
910 RX_TPA_END_CMP_AGG_BUFS) >>
911 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
912 }
913
914 if (agg_bufs) {
915 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
916 return -EBUSY;
917 }
918 *raw_cons = tmp_raw_cons;
919 return 0;
920}
921
922static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
923{
924 if (!rxr->bnapi->in_reset) {
925 rxr->bnapi->in_reset = true;
926 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
927 schedule_work(&bp->sp_task);
928 }
929 rxr->rx_next_cons = 0xffff;
930}
931
Michael Chanc0c050c2015-10-22 16:01:17 -0400932static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
933 struct rx_tpa_start_cmp *tpa_start,
934 struct rx_tpa_start_cmp_ext *tpa_start1)
935{
936 u8 agg_id = TPA_START_AGG_ID(tpa_start);
937 u16 cons, prod;
938 struct bnxt_tpa_info *tpa_info;
939 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
940 struct rx_bd *prod_bd;
941 dma_addr_t mapping;
942
943 cons = tpa_start->rx_tpa_start_cmp_opaque;
944 prod = rxr->rx_prod;
945 cons_rx_buf = &rxr->rx_buf_ring[cons];
946 prod_rx_buf = &rxr->rx_buf_ring[prod];
947 tpa_info = &rxr->rx_tpa[agg_id];
948
Michael Chanfa7e2812016-05-10 19:18:00 -0400949 if (unlikely(cons != rxr->rx_next_cons)) {
950 bnxt_sched_reset(bp, rxr);
951 return;
952 }
953
Michael Chanc0c050c2015-10-22 16:01:17 -0400954 prod_rx_buf->data = tpa_info->data;
955
956 mapping = tpa_info->mapping;
957 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
958
959 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
960
961 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
962
963 tpa_info->data = cons_rx_buf->data;
964 cons_rx_buf->data = NULL;
965 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
966
967 tpa_info->len =
968 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
969 RX_TPA_START_CMP_LEN_SHIFT;
970 if (likely(TPA_START_HASH_VALID(tpa_start))) {
971 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
972
973 tpa_info->hash_type = PKT_HASH_TYPE_L4;
974 tpa_info->gso_type = SKB_GSO_TCPV4;
975 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
976 if (hash_type == 3)
977 tpa_info->gso_type = SKB_GSO_TCPV6;
978 tpa_info->rss_hash =
979 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
980 } else {
981 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
982 tpa_info->gso_type = 0;
983 if (netif_msg_rx_err(bp))
984 netdev_warn(bp->dev, "TPA packet without valid hash\n");
985 }
986 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
987 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400988 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400989
990 rxr->rx_prod = NEXT_RX(prod);
991 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400992 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400993 cons_rx_buf = &rxr->rx_buf_ring[cons];
994
995 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
996 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
997 cons_rx_buf->data = NULL;
998}
999
1000static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1001 u16 cp_cons, u32 agg_bufs)
1002{
1003 if (agg_bufs)
1004 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1005}
1006
Michael Chan94758f82016-06-13 02:25:35 -04001007static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1008 int payload_off, int tcp_ts,
1009 struct sk_buff *skb)
1010{
1011#ifdef CONFIG_INET
1012 struct tcphdr *th;
1013 int len, nw_off;
1014 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1015 u32 hdr_info = tpa_info->hdr_info;
1016 bool loopback = false;
1017
1018 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1019 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1020 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1021
1022 /* If the packet is an internal loopback packet, the offsets will
1023 * have an extra 4 bytes.
1024 */
1025 if (inner_mac_off == 4) {
1026 loopback = true;
1027 } else if (inner_mac_off > 4) {
1028 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1029 ETH_HLEN - 2));
1030
1031 /* We only support inner iPv4/ipv6. If we don't see the
1032 * correct protocol ID, it must be a loopback packet where
1033 * the offsets are off by 4.
1034 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001035 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001036 loopback = true;
1037 }
1038 if (loopback) {
1039 /* internal loopback packet, subtract all offsets by 4 */
1040 inner_ip_off -= 4;
1041 inner_mac_off -= 4;
1042 outer_ip_off -= 4;
1043 }
1044
1045 nw_off = inner_ip_off - ETH_HLEN;
1046 skb_set_network_header(skb, nw_off);
1047 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1048 struct ipv6hdr *iph = ipv6_hdr(skb);
1049
1050 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1051 len = skb->len - skb_transport_offset(skb);
1052 th = tcp_hdr(skb);
1053 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1054 } else {
1055 struct iphdr *iph = ip_hdr(skb);
1056
1057 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1058 len = skb->len - skb_transport_offset(skb);
1059 th = tcp_hdr(skb);
1060 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1061 }
1062
1063 if (inner_mac_off) { /* tunnel */
1064 struct udphdr *uh = NULL;
1065 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1066 ETH_HLEN - 2));
1067
1068 if (proto == htons(ETH_P_IP)) {
1069 struct iphdr *iph = (struct iphdr *)skb->data;
1070
1071 if (iph->protocol == IPPROTO_UDP)
1072 uh = (struct udphdr *)(iph + 1);
1073 } else {
1074 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1075
1076 if (iph->nexthdr == IPPROTO_UDP)
1077 uh = (struct udphdr *)(iph + 1);
1078 }
1079 if (uh) {
1080 if (uh->check)
1081 skb_shinfo(skb)->gso_type |=
1082 SKB_GSO_UDP_TUNNEL_CSUM;
1083 else
1084 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1085 }
1086 }
1087#endif
1088 return skb;
1089}
1090
Michael Chanc0c050c2015-10-22 16:01:17 -04001091#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1092#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1093
Michael Chan309369c2016-06-13 02:25:34 -04001094static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1095 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001096 struct sk_buff *skb)
1097{
Michael Chand1611c32015-10-25 22:27:57 -04001098#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001099 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001100 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001101
Michael Chan309369c2016-06-13 02:25:34 -04001102 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001103 tcp_opt_len = 12;
1104
Michael Chanc0c050c2015-10-22 16:01:17 -04001105 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1106 struct iphdr *iph;
1107
1108 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1109 ETH_HLEN;
1110 skb_set_network_header(skb, nw_off);
1111 iph = ip_hdr(skb);
1112 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1113 len = skb->len - skb_transport_offset(skb);
1114 th = tcp_hdr(skb);
1115 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1116 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1117 struct ipv6hdr *iph;
1118
1119 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1120 ETH_HLEN;
1121 skb_set_network_header(skb, nw_off);
1122 iph = ipv6_hdr(skb);
1123 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1124 len = skb->len - skb_transport_offset(skb);
1125 th = tcp_hdr(skb);
1126 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1127 } else {
1128 dev_kfree_skb_any(skb);
1129 return NULL;
1130 }
1131 tcp_gro_complete(skb);
1132
1133 if (nw_off) { /* tunnel */
1134 struct udphdr *uh = NULL;
1135
1136 if (skb->protocol == htons(ETH_P_IP)) {
1137 struct iphdr *iph = (struct iphdr *)skb->data;
1138
1139 if (iph->protocol == IPPROTO_UDP)
1140 uh = (struct udphdr *)(iph + 1);
1141 } else {
1142 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1143
1144 if (iph->nexthdr == IPPROTO_UDP)
1145 uh = (struct udphdr *)(iph + 1);
1146 }
1147 if (uh) {
1148 if (uh->check)
1149 skb_shinfo(skb)->gso_type |=
1150 SKB_GSO_UDP_TUNNEL_CSUM;
1151 else
1152 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1153 }
1154 }
1155#endif
1156 return skb;
1157}
1158
Michael Chan309369c2016-06-13 02:25:34 -04001159static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1160 struct bnxt_tpa_info *tpa_info,
1161 struct rx_tpa_end_cmp *tpa_end,
1162 struct rx_tpa_end_cmp_ext *tpa_end1,
1163 struct sk_buff *skb)
1164{
1165#ifdef CONFIG_INET
1166 int payload_off;
1167 u16 segs;
1168
1169 segs = TPA_END_TPA_SEGS(tpa_end);
1170 if (segs == 1)
1171 return skb;
1172
1173 NAPI_GRO_CB(skb)->count = segs;
1174 skb_shinfo(skb)->gso_size =
1175 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1176 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1177 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1178 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1179 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1180 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1181#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);
1357 skb_mark_napi_id(skb, &bnapi->napi);
1358 if (bnxt_busy_polling(bnapi))
1359 netif_receive_skb(skb);
1360 else
1361 napi_gro_receive(&bnapi->napi, skb);
1362 rc = 1;
1363 }
1364 goto next_rx_no_prod;
1365 }
1366
1367 cons = rxcmp->rx_cmp_opaque;
1368 rx_buf = &rxr->rx_buf_ring[cons];
1369 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001370 if (unlikely(cons != rxr->rx_next_cons)) {
1371 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1372
1373 bnxt_sched_reset(bp, rxr);
1374 return rc1;
1375 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001376 prefetch(data);
1377
1378 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1379 RX_CMP_AGG_BUFS_SHIFT;
1380
1381 if (agg_bufs) {
1382 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1383 return -EBUSY;
1384
1385 cp_cons = NEXT_CMP(cp_cons);
1386 *agg_event = true;
1387 }
1388
1389 rx_buf->data = NULL;
1390 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1391 bnxt_reuse_rx_data(rxr, cons, data);
1392 if (agg_bufs)
1393 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1394
1395 rc = -EIO;
1396 goto next_rx;
1397 }
1398
1399 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1400 dma_addr = dma_unmap_addr(rx_buf, mapping);
1401
1402 if (len <= bp->rx_copy_thresh) {
1403 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1404 bnxt_reuse_rx_data(rxr, cons, data);
1405 if (!skb) {
1406 rc = -ENOMEM;
1407 goto next_rx;
1408 }
1409 } else {
1410 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1411 if (!skb) {
1412 rc = -ENOMEM;
1413 goto next_rx;
1414 }
1415 }
1416
1417 if (agg_bufs) {
1418 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1419 if (!skb) {
1420 rc = -ENOMEM;
1421 goto next_rx;
1422 }
1423 }
1424
1425 if (RX_CMP_HASH_VALID(rxcmp)) {
1426 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1427 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1428
1429 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1430 if (hash_type != 1 && hash_type != 3)
1431 type = PKT_HASH_TYPE_L3;
1432 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1433 }
1434
1435 skb->protocol = eth_type_trans(skb, dev);
1436
Michael Chan8852ddb2016-06-06 02:37:16 -04001437 if ((rxcmp1->rx_cmp_flags2 &
1438 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1439 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001440 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001441 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001442 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1443
Michael Chan8852ddb2016-06-06 02:37:16 -04001444 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001445 }
1446
1447 skb_checksum_none_assert(skb);
1448 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1449 if (dev->features & NETIF_F_RXCSUM) {
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1451 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1452 }
1453 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001454 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1455 if (dev->features & NETIF_F_RXCSUM)
1456 cpr->rx_l4_csum_errors++;
1457 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001458 }
1459
1460 skb_record_rx_queue(skb, bnapi->index);
1461 skb_mark_napi_id(skb, &bnapi->napi);
1462 if (bnxt_busy_polling(bnapi))
1463 netif_receive_skb(skb);
1464 else
1465 napi_gro_receive(&bnapi->napi, skb);
1466 rc = 1;
1467
1468next_rx:
1469 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001470 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001471
1472next_rx_no_prod:
1473 *raw_cons = tmp_raw_cons;
1474
1475 return rc;
1476}
1477
Michael Chan4bb13ab2016-04-05 14:09:01 -04001478#define BNXT_GET_EVENT_PORT(data) \
1479 ((data) & \
1480 HWRM_ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1481
Michael Chanc0c050c2015-10-22 16:01:17 -04001482static int bnxt_async_event_process(struct bnxt *bp,
1483 struct hwrm_async_event_cmpl *cmpl)
1484{
1485 u16 event_id = le16_to_cpu(cmpl->event_id);
1486
1487 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1488 switch (event_id) {
Michael Chan8cbde112016-04-11 04:11:14 -04001489 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1490 u32 data1 = le32_to_cpu(cmpl->event_data1);
1491 struct bnxt_link_info *link_info = &bp->link_info;
1492
1493 if (BNXT_VF(bp))
1494 goto async_event_process_exit;
1495 if (data1 & 0x20000) {
1496 u16 fw_speed = link_info->force_link_speed;
1497 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1498
1499 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1500 speed);
1501 }
Michael Chan286ef9d2016-11-16 21:13:08 -05001502 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
Michael Chan8cbde112016-04-11 04:11:14 -04001503 /* fall thru */
1504 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001505 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1506 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001507 break;
1508 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1509 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001510 break;
Michael Chan4bb13ab2016-04-05 14:09:01 -04001511 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1512 u32 data1 = le32_to_cpu(cmpl->event_data1);
1513 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1514
1515 if (BNXT_VF(bp))
1516 break;
1517
1518 if (bp->pf.port_id != port_id)
1519 break;
1520
Michael Chan4bb13ab2016-04-05 14:09:01 -04001521 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1522 break;
1523 }
Michael Chanfc0f1922016-06-13 02:25:30 -04001524 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1525 if (BNXT_PF(bp))
1526 goto async_event_process_exit;
1527 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1528 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001529 default:
1530 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1531 event_id);
Jeffrey Huang19241362016-02-26 04:00:00 -05001532 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001533 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001534 schedule_work(&bp->sp_task);
1535async_event_process_exit:
Michael Chanc0c050c2015-10-22 16:01:17 -04001536 return 0;
1537}
1538
1539static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1540{
1541 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1542 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1543 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1544 (struct hwrm_fwd_req_cmpl *)txcmp;
1545
1546 switch (cmpl_type) {
1547 case CMPL_BASE_TYPE_HWRM_DONE:
1548 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1549 if (seq_id == bp->hwrm_intr_seq_id)
1550 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1551 else
1552 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1553 break;
1554
1555 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1556 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1557
1558 if ((vf_id < bp->pf.first_vf_id) ||
1559 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1560 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1561 vf_id);
1562 return -EINVAL;
1563 }
1564
1565 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1566 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1567 schedule_work(&bp->sp_task);
1568 break;
1569
1570 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1571 bnxt_async_event_process(bp,
1572 (struct hwrm_async_event_cmpl *)txcmp);
1573
1574 default:
1575 break;
1576 }
1577
1578 return 0;
1579}
1580
1581static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1582{
1583 struct bnxt_napi *bnapi = dev_instance;
1584 struct bnxt *bp = bnapi->bp;
1585 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1586 u32 cons = RING_CMP(cpr->cp_raw_cons);
1587
1588 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1589 napi_schedule(&bnapi->napi);
1590 return IRQ_HANDLED;
1591}
1592
1593static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1594{
1595 u32 raw_cons = cpr->cp_raw_cons;
1596 u16 cons = RING_CMP(raw_cons);
1597 struct tx_cmp *txcmp;
1598
1599 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1600
1601 return TX_CMP_VALID(txcmp, raw_cons);
1602}
1603
Michael Chanc0c050c2015-10-22 16:01:17 -04001604static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1605{
1606 struct bnxt_napi *bnapi = dev_instance;
1607 struct bnxt *bp = bnapi->bp;
1608 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1609 u32 cons = RING_CMP(cpr->cp_raw_cons);
1610 u32 int_status;
1611
1612 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1613
1614 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001615 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001616 /* return if erroneous interrupt */
1617 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1618 return IRQ_NONE;
1619 }
1620
1621 /* disable ring IRQ */
1622 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1623
1624 /* Return here if interrupt is shared and is disabled. */
1625 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1626 return IRQ_HANDLED;
1627
1628 napi_schedule(&bnapi->napi);
1629 return IRQ_HANDLED;
1630}
1631
1632static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1633{
1634 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1635 u32 raw_cons = cpr->cp_raw_cons;
1636 u32 cons;
1637 int tx_pkts = 0;
1638 int rx_pkts = 0;
1639 bool rx_event = false;
1640 bool agg_event = false;
1641 struct tx_cmp *txcmp;
1642
1643 while (1) {
1644 int rc;
1645
1646 cons = RING_CMP(raw_cons);
1647 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1648
1649 if (!TX_CMP_VALID(txcmp, raw_cons))
1650 break;
1651
Michael Chan67a95e22016-05-04 16:56:43 -04001652 /* The valid test of the entry must be done first before
1653 * reading any further.
1654 */
Michael Chanb67daab2016-05-15 03:04:51 -04001655 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001656 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1657 tx_pkts++;
1658 /* return full budget so NAPI will complete. */
1659 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1660 rx_pkts = budget;
1661 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1662 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1663 if (likely(rc >= 0))
1664 rx_pkts += rc;
1665 else if (rc == -EBUSY) /* partial completion */
1666 break;
1667 rx_event = true;
1668 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1669 CMPL_BASE_TYPE_HWRM_DONE) ||
1670 (TX_CMP_TYPE(txcmp) ==
1671 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1672 (TX_CMP_TYPE(txcmp) ==
1673 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1674 bnxt_hwrm_handler(bp, txcmp);
1675 }
1676 raw_cons = NEXT_RAW_CMP(raw_cons);
1677
1678 if (rx_pkts == budget)
1679 break;
1680 }
1681
1682 cpr->cp_raw_cons = raw_cons;
1683 /* ACK completion ring before freeing tx ring and producing new
1684 * buffers in rx/agg rings to prevent overflowing the completion
1685 * ring.
1686 */
1687 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1688
1689 if (tx_pkts)
1690 bnxt_tx_int(bp, bnapi, tx_pkts);
1691
1692 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001693 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001694
1695 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1696 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1697 if (agg_event) {
1698 writel(DB_KEY_RX | rxr->rx_agg_prod,
1699 rxr->rx_agg_doorbell);
1700 writel(DB_KEY_RX | rxr->rx_agg_prod,
1701 rxr->rx_agg_doorbell);
1702 }
1703 }
1704 return rx_pkts;
1705}
1706
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001707static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1708{
1709 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1710 struct bnxt *bp = bnapi->bp;
1711 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1712 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1713 struct tx_cmp *txcmp;
1714 struct rx_cmp_ext *rxcmp1;
1715 u32 cp_cons, tmp_raw_cons;
1716 u32 raw_cons = cpr->cp_raw_cons;
1717 u32 rx_pkts = 0;
1718 bool agg_event = false;
1719
1720 while (1) {
1721 int rc;
1722
1723 cp_cons = RING_CMP(raw_cons);
1724 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1725
1726 if (!TX_CMP_VALID(txcmp, raw_cons))
1727 break;
1728
1729 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1730 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1731 cp_cons = RING_CMP(tmp_raw_cons);
1732 rxcmp1 = (struct rx_cmp_ext *)
1733 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1734
1735 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1736 break;
1737
1738 /* force an error to recycle the buffer */
1739 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1740 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1741
1742 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1743 if (likely(rc == -EIO))
1744 rx_pkts++;
1745 else if (rc == -EBUSY) /* partial completion */
1746 break;
1747 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1748 CMPL_BASE_TYPE_HWRM_DONE)) {
1749 bnxt_hwrm_handler(bp, txcmp);
1750 } else {
1751 netdev_err(bp->dev,
1752 "Invalid completion received on special ring\n");
1753 }
1754 raw_cons = NEXT_RAW_CMP(raw_cons);
1755
1756 if (rx_pkts == budget)
1757 break;
1758 }
1759
1760 cpr->cp_raw_cons = raw_cons;
1761 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1762 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1763 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1764
1765 if (agg_event) {
1766 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1767 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1768 }
1769
1770 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1771 napi_complete(napi);
1772 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1773 }
1774 return rx_pkts;
1775}
1776
Michael Chanc0c050c2015-10-22 16:01:17 -04001777static int bnxt_poll(struct napi_struct *napi, int budget)
1778{
1779 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1780 struct bnxt *bp = bnapi->bp;
1781 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1782 int work_done = 0;
1783
1784 if (!bnxt_lock_napi(bnapi))
1785 return budget;
1786
1787 while (1) {
1788 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1789
1790 if (work_done >= budget)
1791 break;
1792
1793 if (!bnxt_has_work(bp, cpr)) {
1794 napi_complete(napi);
1795 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1796 break;
1797 }
1798 }
1799 mmiowb();
1800 bnxt_unlock_napi(bnapi);
1801 return work_done;
1802}
1803
1804#ifdef CONFIG_NET_RX_BUSY_POLL
1805static int bnxt_busy_poll(struct napi_struct *napi)
1806{
1807 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1808 struct bnxt *bp = bnapi->bp;
1809 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1810 int rx_work, budget = 4;
1811
1812 if (atomic_read(&bp->intr_sem) != 0)
1813 return LL_FLUSH_FAILED;
1814
1815 if (!bnxt_lock_poll(bnapi))
1816 return LL_FLUSH_BUSY;
1817
1818 rx_work = bnxt_poll_work(bp, bnapi, budget);
1819
1820 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1821
1822 bnxt_unlock_poll(bnapi);
1823 return rx_work;
1824}
1825#endif
1826
1827static void bnxt_free_tx_skbs(struct bnxt *bp)
1828{
1829 int i, max_idx;
1830 struct pci_dev *pdev = bp->pdev;
1831
Michael Chanb6ab4b02016-01-02 23:44:59 -05001832 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001833 return;
1834
1835 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1836 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001837 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001838 int j;
1839
Michael Chanc0c050c2015-10-22 16:01:17 -04001840 for (j = 0; j < max_idx;) {
1841 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1842 struct sk_buff *skb = tx_buf->skb;
1843 int k, last;
1844
1845 if (!skb) {
1846 j++;
1847 continue;
1848 }
1849
1850 tx_buf->skb = NULL;
1851
1852 if (tx_buf->is_push) {
1853 dev_kfree_skb(skb);
1854 j += 2;
1855 continue;
1856 }
1857
1858 dma_unmap_single(&pdev->dev,
1859 dma_unmap_addr(tx_buf, mapping),
1860 skb_headlen(skb),
1861 PCI_DMA_TODEVICE);
1862
1863 last = tx_buf->nr_frags;
1864 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001865 for (k = 0; k < last; k++, j++) {
1866 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001867 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1868
Michael Chand612a572016-01-28 03:11:22 -05001869 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001870 dma_unmap_page(
1871 &pdev->dev,
1872 dma_unmap_addr(tx_buf, mapping),
1873 skb_frag_size(frag), PCI_DMA_TODEVICE);
1874 }
1875 dev_kfree_skb(skb);
1876 }
1877 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1878 }
1879}
1880
1881static void bnxt_free_rx_skbs(struct bnxt *bp)
1882{
1883 int i, max_idx, max_agg_idx;
1884 struct pci_dev *pdev = bp->pdev;
1885
Michael Chanb6ab4b02016-01-02 23:44:59 -05001886 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001887 return;
1888
1889 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1890 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1891 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001892 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001893 int j;
1894
Michael Chanc0c050c2015-10-22 16:01:17 -04001895 if (rxr->rx_tpa) {
1896 for (j = 0; j < MAX_TPA; j++) {
1897 struct bnxt_tpa_info *tpa_info =
1898 &rxr->rx_tpa[j];
1899 u8 *data = tpa_info->data;
1900
1901 if (!data)
1902 continue;
1903
1904 dma_unmap_single(
1905 &pdev->dev,
1906 dma_unmap_addr(tpa_info, mapping),
1907 bp->rx_buf_use_size,
1908 PCI_DMA_FROMDEVICE);
1909
1910 tpa_info->data = NULL;
1911
1912 kfree(data);
1913 }
1914 }
1915
1916 for (j = 0; j < max_idx; j++) {
1917 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1918 u8 *data = rx_buf->data;
1919
1920 if (!data)
1921 continue;
1922
1923 dma_unmap_single(&pdev->dev,
1924 dma_unmap_addr(rx_buf, mapping),
1925 bp->rx_buf_use_size,
1926 PCI_DMA_FROMDEVICE);
1927
1928 rx_buf->data = NULL;
1929
1930 kfree(data);
1931 }
1932
1933 for (j = 0; j < max_agg_idx; j++) {
1934 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1935 &rxr->rx_agg_ring[j];
1936 struct page *page = rx_agg_buf->page;
1937
1938 if (!page)
1939 continue;
1940
1941 dma_unmap_page(&pdev->dev,
1942 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001943 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001944
1945 rx_agg_buf->page = NULL;
1946 __clear_bit(j, rxr->rx_agg_bmap);
1947
1948 __free_page(page);
1949 }
Michael Chan89d0a062016-04-25 02:30:51 -04001950 if (rxr->rx_page) {
1951 __free_page(rxr->rx_page);
1952 rxr->rx_page = NULL;
1953 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001954 }
1955}
1956
1957static void bnxt_free_skbs(struct bnxt *bp)
1958{
1959 bnxt_free_tx_skbs(bp);
1960 bnxt_free_rx_skbs(bp);
1961}
1962
1963static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1964{
1965 struct pci_dev *pdev = bp->pdev;
1966 int i;
1967
1968 for (i = 0; i < ring->nr_pages; i++) {
1969 if (!ring->pg_arr[i])
1970 continue;
1971
1972 dma_free_coherent(&pdev->dev, ring->page_size,
1973 ring->pg_arr[i], ring->dma_arr[i]);
1974
1975 ring->pg_arr[i] = NULL;
1976 }
1977 if (ring->pg_tbl) {
1978 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1979 ring->pg_tbl, ring->pg_tbl_map);
1980 ring->pg_tbl = NULL;
1981 }
1982 if (ring->vmem_size && *ring->vmem) {
1983 vfree(*ring->vmem);
1984 *ring->vmem = NULL;
1985 }
1986}
1987
1988static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1989{
1990 int i;
1991 struct pci_dev *pdev = bp->pdev;
1992
1993 if (ring->nr_pages > 1) {
1994 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1995 ring->nr_pages * 8,
1996 &ring->pg_tbl_map,
1997 GFP_KERNEL);
1998 if (!ring->pg_tbl)
1999 return -ENOMEM;
2000 }
2001
2002 for (i = 0; i < ring->nr_pages; i++) {
2003 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2004 ring->page_size,
2005 &ring->dma_arr[i],
2006 GFP_KERNEL);
2007 if (!ring->pg_arr[i])
2008 return -ENOMEM;
2009
2010 if (ring->nr_pages > 1)
2011 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2012 }
2013
2014 if (ring->vmem_size) {
2015 *ring->vmem = vzalloc(ring->vmem_size);
2016 if (!(*ring->vmem))
2017 return -ENOMEM;
2018 }
2019 return 0;
2020}
2021
2022static void bnxt_free_rx_rings(struct bnxt *bp)
2023{
2024 int i;
2025
Michael Chanb6ab4b02016-01-02 23:44:59 -05002026 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002027 return;
2028
2029 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002030 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002031 struct bnxt_ring_struct *ring;
2032
Michael Chanc0c050c2015-10-22 16:01:17 -04002033 kfree(rxr->rx_tpa);
2034 rxr->rx_tpa = NULL;
2035
2036 kfree(rxr->rx_agg_bmap);
2037 rxr->rx_agg_bmap = NULL;
2038
2039 ring = &rxr->rx_ring_struct;
2040 bnxt_free_ring(bp, ring);
2041
2042 ring = &rxr->rx_agg_ring_struct;
2043 bnxt_free_ring(bp, ring);
2044 }
2045}
2046
2047static int bnxt_alloc_rx_rings(struct bnxt *bp)
2048{
2049 int i, rc, agg_rings = 0, tpa_rings = 0;
2050
Michael Chanb6ab4b02016-01-02 23:44:59 -05002051 if (!bp->rx_ring)
2052 return -ENOMEM;
2053
Michael Chanc0c050c2015-10-22 16:01:17 -04002054 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2055 agg_rings = 1;
2056
2057 if (bp->flags & BNXT_FLAG_TPA)
2058 tpa_rings = 1;
2059
2060 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002061 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002062 struct bnxt_ring_struct *ring;
2063
Michael Chanc0c050c2015-10-22 16:01:17 -04002064 ring = &rxr->rx_ring_struct;
2065
2066 rc = bnxt_alloc_ring(bp, ring);
2067 if (rc)
2068 return rc;
2069
2070 if (agg_rings) {
2071 u16 mem_size;
2072
2073 ring = &rxr->rx_agg_ring_struct;
2074 rc = bnxt_alloc_ring(bp, ring);
2075 if (rc)
2076 return rc;
2077
2078 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2079 mem_size = rxr->rx_agg_bmap_size / 8;
2080 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2081 if (!rxr->rx_agg_bmap)
2082 return -ENOMEM;
2083
2084 if (tpa_rings) {
2085 rxr->rx_tpa = kcalloc(MAX_TPA,
2086 sizeof(struct bnxt_tpa_info),
2087 GFP_KERNEL);
2088 if (!rxr->rx_tpa)
2089 return -ENOMEM;
2090 }
2091 }
2092 }
2093 return 0;
2094}
2095
2096static void bnxt_free_tx_rings(struct bnxt *bp)
2097{
2098 int i;
2099 struct pci_dev *pdev = bp->pdev;
2100
Michael Chanb6ab4b02016-01-02 23:44:59 -05002101 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002102 return;
2103
2104 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002105 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002106 struct bnxt_ring_struct *ring;
2107
Michael Chanc0c050c2015-10-22 16:01:17 -04002108 if (txr->tx_push) {
2109 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2110 txr->tx_push, txr->tx_push_mapping);
2111 txr->tx_push = NULL;
2112 }
2113
2114 ring = &txr->tx_ring_struct;
2115
2116 bnxt_free_ring(bp, ring);
2117 }
2118}
2119
2120static int bnxt_alloc_tx_rings(struct bnxt *bp)
2121{
2122 int i, j, rc;
2123 struct pci_dev *pdev = bp->pdev;
2124
2125 bp->tx_push_size = 0;
2126 if (bp->tx_push_thresh) {
2127 int push_size;
2128
2129 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2130 bp->tx_push_thresh);
2131
Michael Chan4419dbe2016-02-10 17:33:49 -05002132 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002133 push_size = 0;
2134 bp->tx_push_thresh = 0;
2135 }
2136
2137 bp->tx_push_size = push_size;
2138 }
2139
2140 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002141 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002142 struct bnxt_ring_struct *ring;
2143
Michael Chanc0c050c2015-10-22 16:01:17 -04002144 ring = &txr->tx_ring_struct;
2145
2146 rc = bnxt_alloc_ring(bp, ring);
2147 if (rc)
2148 return rc;
2149
2150 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002151 dma_addr_t mapping;
2152
2153 /* One pre-allocated DMA buffer to backup
2154 * TX push operation
2155 */
2156 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2157 bp->tx_push_size,
2158 &txr->tx_push_mapping,
2159 GFP_KERNEL);
2160
2161 if (!txr->tx_push)
2162 return -ENOMEM;
2163
Michael Chanc0c050c2015-10-22 16:01:17 -04002164 mapping = txr->tx_push_mapping +
2165 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002166 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002167
Michael Chan4419dbe2016-02-10 17:33:49 -05002168 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002169 }
2170 ring->queue_id = bp->q_info[j].queue_id;
2171 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2172 j++;
2173 }
2174 return 0;
2175}
2176
2177static void bnxt_free_cp_rings(struct bnxt *bp)
2178{
2179 int i;
2180
2181 if (!bp->bnapi)
2182 return;
2183
2184 for (i = 0; i < bp->cp_nr_rings; i++) {
2185 struct bnxt_napi *bnapi = bp->bnapi[i];
2186 struct bnxt_cp_ring_info *cpr;
2187 struct bnxt_ring_struct *ring;
2188
2189 if (!bnapi)
2190 continue;
2191
2192 cpr = &bnapi->cp_ring;
2193 ring = &cpr->cp_ring_struct;
2194
2195 bnxt_free_ring(bp, ring);
2196 }
2197}
2198
2199static int bnxt_alloc_cp_rings(struct bnxt *bp)
2200{
2201 int i, rc;
2202
2203 for (i = 0; i < bp->cp_nr_rings; i++) {
2204 struct bnxt_napi *bnapi = bp->bnapi[i];
2205 struct bnxt_cp_ring_info *cpr;
2206 struct bnxt_ring_struct *ring;
2207
2208 if (!bnapi)
2209 continue;
2210
2211 cpr = &bnapi->cp_ring;
2212 ring = &cpr->cp_ring_struct;
2213
2214 rc = bnxt_alloc_ring(bp, ring);
2215 if (rc)
2216 return rc;
2217 }
2218 return 0;
2219}
2220
2221static void bnxt_init_ring_struct(struct bnxt *bp)
2222{
2223 int i;
2224
2225 for (i = 0; i < bp->cp_nr_rings; i++) {
2226 struct bnxt_napi *bnapi = bp->bnapi[i];
2227 struct bnxt_cp_ring_info *cpr;
2228 struct bnxt_rx_ring_info *rxr;
2229 struct bnxt_tx_ring_info *txr;
2230 struct bnxt_ring_struct *ring;
2231
2232 if (!bnapi)
2233 continue;
2234
2235 cpr = &bnapi->cp_ring;
2236 ring = &cpr->cp_ring_struct;
2237 ring->nr_pages = bp->cp_nr_pages;
2238 ring->page_size = HW_CMPD_RING_SIZE;
2239 ring->pg_arr = (void **)cpr->cp_desc_ring;
2240 ring->dma_arr = cpr->cp_desc_mapping;
2241 ring->vmem_size = 0;
2242
Michael Chanb6ab4b02016-01-02 23:44:59 -05002243 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002244 if (!rxr)
2245 goto skip_rx;
2246
Michael Chanc0c050c2015-10-22 16:01:17 -04002247 ring = &rxr->rx_ring_struct;
2248 ring->nr_pages = bp->rx_nr_pages;
2249 ring->page_size = HW_RXBD_RING_SIZE;
2250 ring->pg_arr = (void **)rxr->rx_desc_ring;
2251 ring->dma_arr = rxr->rx_desc_mapping;
2252 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2253 ring->vmem = (void **)&rxr->rx_buf_ring;
2254
2255 ring = &rxr->rx_agg_ring_struct;
2256 ring->nr_pages = bp->rx_agg_nr_pages;
2257 ring->page_size = HW_RXBD_RING_SIZE;
2258 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2259 ring->dma_arr = rxr->rx_agg_desc_mapping;
2260 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2261 ring->vmem = (void **)&rxr->rx_agg_ring;
2262
Michael Chan3b2b7d92016-01-02 23:45:00 -05002263skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002264 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002265 if (!txr)
2266 continue;
2267
Michael Chanc0c050c2015-10-22 16:01:17 -04002268 ring = &txr->tx_ring_struct;
2269 ring->nr_pages = bp->tx_nr_pages;
2270 ring->page_size = HW_RXBD_RING_SIZE;
2271 ring->pg_arr = (void **)txr->tx_desc_ring;
2272 ring->dma_arr = txr->tx_desc_mapping;
2273 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2274 ring->vmem = (void **)&txr->tx_buf_ring;
2275 }
2276}
2277
2278static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2279{
2280 int i;
2281 u32 prod;
2282 struct rx_bd **rx_buf_ring;
2283
2284 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2285 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2286 int j;
2287 struct rx_bd *rxbd;
2288
2289 rxbd = rx_buf_ring[i];
2290 if (!rxbd)
2291 continue;
2292
2293 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2294 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2295 rxbd->rx_bd_opaque = prod;
2296 }
2297 }
2298}
2299
2300static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2301{
2302 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002303 struct bnxt_rx_ring_info *rxr;
2304 struct bnxt_ring_struct *ring;
2305 u32 prod, type;
2306 int i;
2307
Michael Chanc0c050c2015-10-22 16:01:17 -04002308 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2309 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2310
2311 if (NET_IP_ALIGN == 2)
2312 type |= RX_BD_FLAGS_SOP;
2313
Michael Chanb6ab4b02016-01-02 23:44:59 -05002314 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002315 ring = &rxr->rx_ring_struct;
2316 bnxt_init_rxbd_pages(ring, type);
2317
2318 prod = rxr->rx_prod;
2319 for (i = 0; i < bp->rx_ring_size; i++) {
2320 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2321 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2322 ring_nr, i, bp->rx_ring_size);
2323 break;
2324 }
2325 prod = NEXT_RX(prod);
2326 }
2327 rxr->rx_prod = prod;
2328 ring->fw_ring_id = INVALID_HW_RING_ID;
2329
Michael Chanedd0c2c2015-12-27 18:19:19 -05002330 ring = &rxr->rx_agg_ring_struct;
2331 ring->fw_ring_id = INVALID_HW_RING_ID;
2332
Michael Chanc0c050c2015-10-22 16:01:17 -04002333 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2334 return 0;
2335
Michael Chan2839f282016-04-25 02:30:50 -04002336 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002337 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2338
2339 bnxt_init_rxbd_pages(ring, type);
2340
2341 prod = rxr->rx_agg_prod;
2342 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2343 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2344 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2345 ring_nr, i, bp->rx_ring_size);
2346 break;
2347 }
2348 prod = NEXT_RX_AGG(prod);
2349 }
2350 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002351
2352 if (bp->flags & BNXT_FLAG_TPA) {
2353 if (rxr->rx_tpa) {
2354 u8 *data;
2355 dma_addr_t mapping;
2356
2357 for (i = 0; i < MAX_TPA; i++) {
2358 data = __bnxt_alloc_rx_data(bp, &mapping,
2359 GFP_KERNEL);
2360 if (!data)
2361 return -ENOMEM;
2362
2363 rxr->rx_tpa[i].data = data;
2364 rxr->rx_tpa[i].mapping = mapping;
2365 }
2366 } else {
2367 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2368 return -ENOMEM;
2369 }
2370 }
2371
2372 return 0;
2373}
2374
2375static int bnxt_init_rx_rings(struct bnxt *bp)
2376{
2377 int i, rc = 0;
2378
2379 for (i = 0; i < bp->rx_nr_rings; i++) {
2380 rc = bnxt_init_one_rx_ring(bp, i);
2381 if (rc)
2382 break;
2383 }
2384
2385 return rc;
2386}
2387
2388static int bnxt_init_tx_rings(struct bnxt *bp)
2389{
2390 u16 i;
2391
2392 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2393 MAX_SKB_FRAGS + 1);
2394
2395 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002396 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002397 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2398
2399 ring->fw_ring_id = INVALID_HW_RING_ID;
2400 }
2401
2402 return 0;
2403}
2404
2405static void bnxt_free_ring_grps(struct bnxt *bp)
2406{
2407 kfree(bp->grp_info);
2408 bp->grp_info = NULL;
2409}
2410
2411static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2412{
2413 int i;
2414
2415 if (irq_re_init) {
2416 bp->grp_info = kcalloc(bp->cp_nr_rings,
2417 sizeof(struct bnxt_ring_grp_info),
2418 GFP_KERNEL);
2419 if (!bp->grp_info)
2420 return -ENOMEM;
2421 }
2422 for (i = 0; i < bp->cp_nr_rings; i++) {
2423 if (irq_re_init)
2424 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2425 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2426 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2427 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2428 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2429 }
2430 return 0;
2431}
2432
2433static void bnxt_free_vnics(struct bnxt *bp)
2434{
2435 kfree(bp->vnic_info);
2436 bp->vnic_info = NULL;
2437 bp->nr_vnics = 0;
2438}
2439
2440static int bnxt_alloc_vnics(struct bnxt *bp)
2441{
2442 int num_vnics = 1;
2443
2444#ifdef CONFIG_RFS_ACCEL
2445 if (bp->flags & BNXT_FLAG_RFS)
2446 num_vnics += bp->rx_nr_rings;
2447#endif
2448
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002449 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2450 num_vnics++;
2451
Michael Chanc0c050c2015-10-22 16:01:17 -04002452 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2453 GFP_KERNEL);
2454 if (!bp->vnic_info)
2455 return -ENOMEM;
2456
2457 bp->nr_vnics = num_vnics;
2458 return 0;
2459}
2460
2461static void bnxt_init_vnics(struct bnxt *bp)
2462{
2463 int i;
2464
2465 for (i = 0; i < bp->nr_vnics; i++) {
2466 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2467
2468 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002469 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2470 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002471 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2472
2473 if (bp->vnic_info[i].rss_hash_key) {
2474 if (i == 0)
2475 prandom_bytes(vnic->rss_hash_key,
2476 HW_HASH_KEY_SIZE);
2477 else
2478 memcpy(vnic->rss_hash_key,
2479 bp->vnic_info[0].rss_hash_key,
2480 HW_HASH_KEY_SIZE);
2481 }
2482 }
2483}
2484
2485static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2486{
2487 int pages;
2488
2489 pages = ring_size / desc_per_pg;
2490
2491 if (!pages)
2492 return 1;
2493
2494 pages++;
2495
2496 while (pages & (pages - 1))
2497 pages++;
2498
2499 return pages;
2500}
2501
2502static void bnxt_set_tpa_flags(struct bnxt *bp)
2503{
2504 bp->flags &= ~BNXT_FLAG_TPA;
2505 if (bp->dev->features & NETIF_F_LRO)
2506 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002507 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002508 bp->flags |= BNXT_FLAG_GRO;
2509}
2510
2511/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2512 * be set on entry.
2513 */
2514void bnxt_set_ring_params(struct bnxt *bp)
2515{
2516 u32 ring_size, rx_size, rx_space;
2517 u32 agg_factor = 0, agg_ring_size = 0;
2518
2519 /* 8 for CRC and VLAN */
2520 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2521
2522 rx_space = rx_size + NET_SKB_PAD +
2523 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2524
2525 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2526 ring_size = bp->rx_ring_size;
2527 bp->rx_agg_ring_size = 0;
2528 bp->rx_agg_nr_pages = 0;
2529
2530 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002531 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002532
2533 bp->flags &= ~BNXT_FLAG_JUMBO;
2534 if (rx_space > PAGE_SIZE) {
2535 u32 jumbo_factor;
2536
2537 bp->flags |= BNXT_FLAG_JUMBO;
2538 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2539 if (jumbo_factor > agg_factor)
2540 agg_factor = jumbo_factor;
2541 }
2542 agg_ring_size = ring_size * agg_factor;
2543
2544 if (agg_ring_size) {
2545 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2546 RX_DESC_CNT);
2547 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2548 u32 tmp = agg_ring_size;
2549
2550 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2551 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2552 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2553 tmp, agg_ring_size);
2554 }
2555 bp->rx_agg_ring_size = agg_ring_size;
2556 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2557 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2558 rx_space = rx_size + NET_SKB_PAD +
2559 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2560 }
2561
2562 bp->rx_buf_use_size = rx_size;
2563 bp->rx_buf_size = rx_space;
2564
2565 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2566 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2567
2568 ring_size = bp->tx_ring_size;
2569 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2570 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2571
2572 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2573 bp->cp_ring_size = ring_size;
2574
2575 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2576 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2577 bp->cp_nr_pages = MAX_CP_PAGES;
2578 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2579 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2580 ring_size, bp->cp_ring_size);
2581 }
2582 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2583 bp->cp_ring_mask = bp->cp_bit - 1;
2584}
2585
2586static void bnxt_free_vnic_attributes(struct bnxt *bp)
2587{
2588 int i;
2589 struct bnxt_vnic_info *vnic;
2590 struct pci_dev *pdev = bp->pdev;
2591
2592 if (!bp->vnic_info)
2593 return;
2594
2595 for (i = 0; i < bp->nr_vnics; i++) {
2596 vnic = &bp->vnic_info[i];
2597
2598 kfree(vnic->fw_grp_ids);
2599 vnic->fw_grp_ids = NULL;
2600
2601 kfree(vnic->uc_list);
2602 vnic->uc_list = NULL;
2603
2604 if (vnic->mc_list) {
2605 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2606 vnic->mc_list, vnic->mc_list_mapping);
2607 vnic->mc_list = NULL;
2608 }
2609
2610 if (vnic->rss_table) {
2611 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2612 vnic->rss_table,
2613 vnic->rss_table_dma_addr);
2614 vnic->rss_table = NULL;
2615 }
2616
2617 vnic->rss_hash_key = NULL;
2618 vnic->flags = 0;
2619 }
2620}
2621
2622static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2623{
2624 int i, rc = 0, size;
2625 struct bnxt_vnic_info *vnic;
2626 struct pci_dev *pdev = bp->pdev;
2627 int max_rings;
2628
2629 for (i = 0; i < bp->nr_vnics; i++) {
2630 vnic = &bp->vnic_info[i];
2631
2632 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2633 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2634
2635 if (mem_size > 0) {
2636 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2637 if (!vnic->uc_list) {
2638 rc = -ENOMEM;
2639 goto out;
2640 }
2641 }
2642 }
2643
2644 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2645 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2646 vnic->mc_list =
2647 dma_alloc_coherent(&pdev->dev,
2648 vnic->mc_list_size,
2649 &vnic->mc_list_mapping,
2650 GFP_KERNEL);
2651 if (!vnic->mc_list) {
2652 rc = -ENOMEM;
2653 goto out;
2654 }
2655 }
2656
2657 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2658 max_rings = bp->rx_nr_rings;
2659 else
2660 max_rings = 1;
2661
2662 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2663 if (!vnic->fw_grp_ids) {
2664 rc = -ENOMEM;
2665 goto out;
2666 }
2667
2668 /* Allocate rss table and hash key */
2669 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2670 &vnic->rss_table_dma_addr,
2671 GFP_KERNEL);
2672 if (!vnic->rss_table) {
2673 rc = -ENOMEM;
2674 goto out;
2675 }
2676
2677 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2678
2679 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2680 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2681 }
2682 return 0;
2683
2684out:
2685 return rc;
2686}
2687
2688static void bnxt_free_hwrm_resources(struct bnxt *bp)
2689{
2690 struct pci_dev *pdev = bp->pdev;
2691
2692 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2693 bp->hwrm_cmd_resp_dma_addr);
2694
2695 bp->hwrm_cmd_resp_addr = NULL;
2696 if (bp->hwrm_dbg_resp_addr) {
2697 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2698 bp->hwrm_dbg_resp_addr,
2699 bp->hwrm_dbg_resp_dma_addr);
2700
2701 bp->hwrm_dbg_resp_addr = NULL;
2702 }
2703}
2704
2705static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2706{
2707 struct pci_dev *pdev = bp->pdev;
2708
2709 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2710 &bp->hwrm_cmd_resp_dma_addr,
2711 GFP_KERNEL);
2712 if (!bp->hwrm_cmd_resp_addr)
2713 return -ENOMEM;
2714 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2715 HWRM_DBG_REG_BUF_SIZE,
2716 &bp->hwrm_dbg_resp_dma_addr,
2717 GFP_KERNEL);
2718 if (!bp->hwrm_dbg_resp_addr)
2719 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2720
2721 return 0;
2722}
2723
2724static void bnxt_free_stats(struct bnxt *bp)
2725{
2726 u32 size, i;
2727 struct pci_dev *pdev = bp->pdev;
2728
Michael Chan3bdf56c2016-03-07 15:38:45 -05002729 if (bp->hw_rx_port_stats) {
2730 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2731 bp->hw_rx_port_stats,
2732 bp->hw_rx_port_stats_map);
2733 bp->hw_rx_port_stats = NULL;
2734 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2735 }
2736
Michael Chanc0c050c2015-10-22 16:01:17 -04002737 if (!bp->bnapi)
2738 return;
2739
2740 size = sizeof(struct ctx_hw_stats);
2741
2742 for (i = 0; i < bp->cp_nr_rings; i++) {
2743 struct bnxt_napi *bnapi = bp->bnapi[i];
2744 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2745
2746 if (cpr->hw_stats) {
2747 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2748 cpr->hw_stats_map);
2749 cpr->hw_stats = NULL;
2750 }
2751 }
2752}
2753
2754static int bnxt_alloc_stats(struct bnxt *bp)
2755{
2756 u32 size, i;
2757 struct pci_dev *pdev = bp->pdev;
2758
2759 size = sizeof(struct ctx_hw_stats);
2760
2761 for (i = 0; i < bp->cp_nr_rings; i++) {
2762 struct bnxt_napi *bnapi = bp->bnapi[i];
2763 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2764
2765 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2766 &cpr->hw_stats_map,
2767 GFP_KERNEL);
2768 if (!cpr->hw_stats)
2769 return -ENOMEM;
2770
2771 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2772 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002773
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002774 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002775 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2776 sizeof(struct tx_port_stats) + 1024;
2777
2778 bp->hw_rx_port_stats =
2779 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2780 &bp->hw_rx_port_stats_map,
2781 GFP_KERNEL);
2782 if (!bp->hw_rx_port_stats)
2783 return -ENOMEM;
2784
2785 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2786 512;
2787 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2788 sizeof(struct rx_port_stats) + 512;
2789 bp->flags |= BNXT_FLAG_PORT_STATS;
2790 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002791 return 0;
2792}
2793
2794static void bnxt_clear_ring_indices(struct bnxt *bp)
2795{
2796 int i;
2797
2798 if (!bp->bnapi)
2799 return;
2800
2801 for (i = 0; i < bp->cp_nr_rings; i++) {
2802 struct bnxt_napi *bnapi = bp->bnapi[i];
2803 struct bnxt_cp_ring_info *cpr;
2804 struct bnxt_rx_ring_info *rxr;
2805 struct bnxt_tx_ring_info *txr;
2806
2807 if (!bnapi)
2808 continue;
2809
2810 cpr = &bnapi->cp_ring;
2811 cpr->cp_raw_cons = 0;
2812
Michael Chanb6ab4b02016-01-02 23:44:59 -05002813 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002814 if (txr) {
2815 txr->tx_prod = 0;
2816 txr->tx_cons = 0;
2817 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002818
Michael Chanb6ab4b02016-01-02 23:44:59 -05002819 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002820 if (rxr) {
2821 rxr->rx_prod = 0;
2822 rxr->rx_agg_prod = 0;
2823 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002824 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002825 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002826 }
2827}
2828
2829static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2830{
2831#ifdef CONFIG_RFS_ACCEL
2832 int i;
2833
2834 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2835 * safe to delete the hash table.
2836 */
2837 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2838 struct hlist_head *head;
2839 struct hlist_node *tmp;
2840 struct bnxt_ntuple_filter *fltr;
2841
2842 head = &bp->ntp_fltr_hash_tbl[i];
2843 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2844 hlist_del(&fltr->hash);
2845 kfree(fltr);
2846 }
2847 }
2848 if (irq_reinit) {
2849 kfree(bp->ntp_fltr_bmap);
2850 bp->ntp_fltr_bmap = NULL;
2851 }
2852 bp->ntp_fltr_count = 0;
2853#endif
2854}
2855
2856static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2857{
2858#ifdef CONFIG_RFS_ACCEL
2859 int i, rc = 0;
2860
2861 if (!(bp->flags & BNXT_FLAG_RFS))
2862 return 0;
2863
2864 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2865 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2866
2867 bp->ntp_fltr_count = 0;
2868 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2869 GFP_KERNEL);
2870
2871 if (!bp->ntp_fltr_bmap)
2872 rc = -ENOMEM;
2873
2874 return rc;
2875#else
2876 return 0;
2877#endif
2878}
2879
2880static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2881{
2882 bnxt_free_vnic_attributes(bp);
2883 bnxt_free_tx_rings(bp);
2884 bnxt_free_rx_rings(bp);
2885 bnxt_free_cp_rings(bp);
2886 bnxt_free_ntp_fltrs(bp, irq_re_init);
2887 if (irq_re_init) {
2888 bnxt_free_stats(bp);
2889 bnxt_free_ring_grps(bp);
2890 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002891 kfree(bp->tx_ring);
2892 bp->tx_ring = NULL;
2893 kfree(bp->rx_ring);
2894 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002895 kfree(bp->bnapi);
2896 bp->bnapi = NULL;
2897 } else {
2898 bnxt_clear_ring_indices(bp);
2899 }
2900}
2901
2902static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2903{
Michael Chan01657bc2016-01-02 23:45:03 -05002904 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002905 void *bnapi;
2906
2907 if (irq_re_init) {
2908 /* Allocate bnapi mem pointer array and mem block for
2909 * all queues
2910 */
2911 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2912 bp->cp_nr_rings);
2913 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2914 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2915 if (!bnapi)
2916 return -ENOMEM;
2917
2918 bp->bnapi = bnapi;
2919 bnapi += arr_size;
2920 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2921 bp->bnapi[i] = bnapi;
2922 bp->bnapi[i]->index = i;
2923 bp->bnapi[i]->bp = bp;
2924 }
2925
Michael Chanb6ab4b02016-01-02 23:44:59 -05002926 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2927 sizeof(struct bnxt_rx_ring_info),
2928 GFP_KERNEL);
2929 if (!bp->rx_ring)
2930 return -ENOMEM;
2931
2932 for (i = 0; i < bp->rx_nr_rings; i++) {
2933 bp->rx_ring[i].bnapi = bp->bnapi[i];
2934 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2935 }
2936
2937 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2938 sizeof(struct bnxt_tx_ring_info),
2939 GFP_KERNEL);
2940 if (!bp->tx_ring)
2941 return -ENOMEM;
2942
Michael Chan01657bc2016-01-02 23:45:03 -05002943 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2944 j = 0;
2945 else
2946 j = bp->rx_nr_rings;
2947
2948 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2949 bp->tx_ring[i].bnapi = bp->bnapi[j];
2950 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002951 }
2952
Michael Chanc0c050c2015-10-22 16:01:17 -04002953 rc = bnxt_alloc_stats(bp);
2954 if (rc)
2955 goto alloc_mem_err;
2956
2957 rc = bnxt_alloc_ntp_fltrs(bp);
2958 if (rc)
2959 goto alloc_mem_err;
2960
2961 rc = bnxt_alloc_vnics(bp);
2962 if (rc)
2963 goto alloc_mem_err;
2964 }
2965
2966 bnxt_init_ring_struct(bp);
2967
2968 rc = bnxt_alloc_rx_rings(bp);
2969 if (rc)
2970 goto alloc_mem_err;
2971
2972 rc = bnxt_alloc_tx_rings(bp);
2973 if (rc)
2974 goto alloc_mem_err;
2975
2976 rc = bnxt_alloc_cp_rings(bp);
2977 if (rc)
2978 goto alloc_mem_err;
2979
2980 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2981 BNXT_VNIC_UCAST_FLAG;
2982 rc = bnxt_alloc_vnic_attributes(bp);
2983 if (rc)
2984 goto alloc_mem_err;
2985 return 0;
2986
2987alloc_mem_err:
2988 bnxt_free_mem(bp, true);
2989 return rc;
2990}
2991
2992void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2993 u16 cmpl_ring, u16 target_id)
2994{
Michael Chana8643e12016-02-26 04:00:05 -05002995 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04002996
Michael Chana8643e12016-02-26 04:00:05 -05002997 req->req_type = cpu_to_le16(req_type);
2998 req->cmpl_ring = cpu_to_le16(cmpl_ring);
2999 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003000 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3001}
3002
Michael Chanfbfbc482016-02-26 04:00:07 -05003003static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3004 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003005{
Michael Chana11fa2b2016-05-15 03:04:47 -04003006 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003007 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003008 u32 *data = msg;
3009 __le32 *resp_len, *valid;
3010 u16 cp_ring_id, len = 0;
3011 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3012
Michael Chana8643e12016-02-26 04:00:05 -05003013 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003014 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003015 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003016 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3017
3018 /* Write request msg to hwrm channel */
3019 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3020
Michael Chane6ef2692016-03-28 19:46:05 -04003021 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003022 writel(0, bp->bar0 + i);
3023
Michael Chanc0c050c2015-10-22 16:01:17 -04003024 /* currently supports only one outstanding message */
3025 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003026 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003027
3028 /* Ring channel doorbell */
3029 writel(1, bp->bar0 + 0x100);
3030
Michael Chanff4fe812016-02-26 04:00:04 -05003031 if (!timeout)
3032 timeout = DFLT_HWRM_CMD_TIMEOUT;
3033
Michael Chanc0c050c2015-10-22 16:01:17 -04003034 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003035 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003036 if (intr_process) {
3037 /* Wait until hwrm response cmpl interrupt is processed */
3038 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003039 i++ < tmo_count) {
3040 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003041 }
3042
3043 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3044 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003045 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003046 return -1;
3047 }
3048 } else {
3049 /* Check if response len is updated */
3050 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003051 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003052 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3053 HWRM_RESP_LEN_SFT;
3054 if (len)
3055 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003056 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003057 }
3058
Michael Chana11fa2b2016-05-15 03:04:47 -04003059 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003060 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003061 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003062 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003063 return -1;
3064 }
3065
3066 /* Last word of resp contains valid bit */
3067 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003068 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003069 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3070 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003071 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003072 }
3073
Michael Chana11fa2b2016-05-15 03:04:47 -04003074 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003075 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003076 timeout, le16_to_cpu(req->req_type),
3077 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003078 return -1;
3079 }
3080 }
3081
3082 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003083 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003084 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3085 le16_to_cpu(resp->req_type),
3086 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003087 return rc;
3088}
3089
3090int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3091{
3092 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003093}
3094
3095int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3096{
3097 int rc;
3098
3099 mutex_lock(&bp->hwrm_cmd_lock);
3100 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3101 mutex_unlock(&bp->hwrm_cmd_lock);
3102 return rc;
3103}
3104
Michael Chan90e209212016-02-26 04:00:08 -05003105int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3106 int timeout)
3107{
3108 int rc;
3109
3110 mutex_lock(&bp->hwrm_cmd_lock);
3111 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3112 mutex_unlock(&bp->hwrm_cmd_lock);
3113 return rc;
3114}
3115
Michael Chanc0c050c2015-10-22 16:01:17 -04003116static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3117{
3118 struct hwrm_func_drv_rgtr_input req = {0};
3119 int i;
Michael Chan25be8622016-04-05 14:09:00 -04003120 DECLARE_BITMAP(async_events_bmap, 256);
3121 u32 *events = (u32 *)async_events_bmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04003122
3123 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3124
3125 req.enables =
3126 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3127 FUNC_DRV_RGTR_REQ_ENABLES_VER |
3128 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3129
Michael Chan25be8622016-04-05 14:09:00 -04003130 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3131 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3132 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3133
3134 for (i = 0; i < 8; i++)
3135 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3136
Michael Chan11f15ed2016-04-05 14:08:55 -04003137 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003138 req.ver_maj = DRV_VER_MAJ;
3139 req.ver_min = DRV_VER_MIN;
3140 req.ver_upd = DRV_VER_UPD;
3141
3142 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003143 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003144 u32 *data = (u32 *)vf_req_snif_bmap;
3145
Michael Chande68f5de2015-12-09 19:35:41 -05003146 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003147 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3148 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3149
Michael Chande68f5de2015-12-09 19:35:41 -05003150 for (i = 0; i < 8; i++)
3151 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3152
Michael Chanc0c050c2015-10-22 16:01:17 -04003153 req.enables |=
3154 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3155 }
3156
3157 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3158}
3159
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003160static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3161{
3162 struct hwrm_func_drv_unrgtr_input req = {0};
3163
3164 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3165 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3166}
3167
Michael Chanc0c050c2015-10-22 16:01:17 -04003168static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3169{
3170 u32 rc = 0;
3171 struct hwrm_tunnel_dst_port_free_input req = {0};
3172
3173 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3174 req.tunnel_type = tunnel_type;
3175
3176 switch (tunnel_type) {
3177 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3178 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3179 break;
3180 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3181 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3182 break;
3183 default:
3184 break;
3185 }
3186
3187 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3188 if (rc)
3189 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3190 rc);
3191 return rc;
3192}
3193
3194static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3195 u8 tunnel_type)
3196{
3197 u32 rc = 0;
3198 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3199 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3200
3201 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3202
3203 req.tunnel_type = tunnel_type;
3204 req.tunnel_dst_port_val = port;
3205
3206 mutex_lock(&bp->hwrm_cmd_lock);
3207 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3208 if (rc) {
3209 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3210 rc);
3211 goto err_out;
3212 }
3213
3214 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
3215 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3216
3217 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
3218 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3219err_out:
3220 mutex_unlock(&bp->hwrm_cmd_lock);
3221 return rc;
3222}
3223
3224static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3225{
3226 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3227 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3228
3229 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003230 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003231
3232 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3233 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3234 req.mask = cpu_to_le32(vnic->rx_mask);
3235 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3236}
3237
3238#ifdef CONFIG_RFS_ACCEL
3239static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3240 struct bnxt_ntuple_filter *fltr)
3241{
3242 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3243
3244 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3245 req.ntuple_filter_id = fltr->filter_id;
3246 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3247}
3248
3249#define BNXT_NTP_FLTR_FLAGS \
3250 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3251 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3252 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3253 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3254 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3255 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3256 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3257 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3258 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3259 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3260 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3261 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3262 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003263 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003264
3265static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3266 struct bnxt_ntuple_filter *fltr)
3267{
3268 int rc = 0;
3269 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3270 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3271 bp->hwrm_cmd_resp_addr;
3272 struct flow_keys *keys = &fltr->fkeys;
3273 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3274
3275 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003276 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003277
3278 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3279
3280 req.ethertype = htons(ETH_P_IP);
3281 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003282 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003283 req.ip_protocol = keys->basic.ip_proto;
3284
3285 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3286 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3287 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3288 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3289
3290 req.src_port = keys->ports.src;
3291 req.src_port_mask = cpu_to_be16(0xffff);
3292 req.dst_port = keys->ports.dst;
3293 req.dst_port_mask = cpu_to_be16(0xffff);
3294
Michael Chanc1935542015-12-27 18:19:28 -05003295 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003296 mutex_lock(&bp->hwrm_cmd_lock);
3297 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3298 if (!rc)
3299 fltr->filter_id = resp->ntuple_filter_id;
3300 mutex_unlock(&bp->hwrm_cmd_lock);
3301 return rc;
3302}
3303#endif
3304
3305static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3306 u8 *mac_addr)
3307{
3308 u32 rc = 0;
3309 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3310 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3311
3312 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003313 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3314 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3315 req.flags |=
3316 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003317 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003318 req.enables =
3319 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003320 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003321 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3322 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3323 req.l2_addr_mask[0] = 0xff;
3324 req.l2_addr_mask[1] = 0xff;
3325 req.l2_addr_mask[2] = 0xff;
3326 req.l2_addr_mask[3] = 0xff;
3327 req.l2_addr_mask[4] = 0xff;
3328 req.l2_addr_mask[5] = 0xff;
3329
3330 mutex_lock(&bp->hwrm_cmd_lock);
3331 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3332 if (!rc)
3333 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3334 resp->l2_filter_id;
3335 mutex_unlock(&bp->hwrm_cmd_lock);
3336 return rc;
3337}
3338
3339static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3340{
3341 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3342 int rc = 0;
3343
3344 /* Any associated ntuple filters will also be cleared by firmware. */
3345 mutex_lock(&bp->hwrm_cmd_lock);
3346 for (i = 0; i < num_of_vnics; i++) {
3347 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3348
3349 for (j = 0; j < vnic->uc_filter_count; j++) {
3350 struct hwrm_cfa_l2_filter_free_input req = {0};
3351
3352 bnxt_hwrm_cmd_hdr_init(bp, &req,
3353 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3354
3355 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3356
3357 rc = _hwrm_send_message(bp, &req, sizeof(req),
3358 HWRM_CMD_TIMEOUT);
3359 }
3360 vnic->uc_filter_count = 0;
3361 }
3362 mutex_unlock(&bp->hwrm_cmd_lock);
3363
3364 return rc;
3365}
3366
3367static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3368{
3369 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3370 struct hwrm_vnic_tpa_cfg_input req = {0};
3371
3372 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3373
3374 if (tpa_flags) {
3375 u16 mss = bp->dev->mtu - 40;
3376 u32 nsegs, n, segs = 0, flags;
3377
3378 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3379 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3380 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3381 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3382 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3383 if (tpa_flags & BNXT_FLAG_GRO)
3384 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3385
3386 req.flags = cpu_to_le32(flags);
3387
3388 req.enables =
3389 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003390 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3391 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003392
3393 /* Number of segs are log2 units, and first packet is not
3394 * included as part of this units.
3395 */
Michael Chan2839f282016-04-25 02:30:50 -04003396 if (mss <= BNXT_RX_PAGE_SIZE) {
3397 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003398 nsegs = (MAX_SKB_FRAGS - 1) * n;
3399 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003400 n = mss / BNXT_RX_PAGE_SIZE;
3401 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003402 n++;
3403 nsegs = (MAX_SKB_FRAGS - n) / n;
3404 }
3405
3406 segs = ilog2(nsegs);
3407 req.max_agg_segs = cpu_to_le16(segs);
3408 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003409
3410 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003411 }
3412 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3413
3414 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3415}
3416
3417static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3418{
3419 u32 i, j, max_rings;
3420 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3421 struct hwrm_vnic_rss_cfg_input req = {0};
3422
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003423 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003424 return 0;
3425
3426 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3427 if (set_rss) {
Michael Chan8d6be8b2016-09-19 03:58:00 -04003428 vnic->hash_type = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
3429 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
3430 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
3431 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chanc0c050c2015-10-22 16:01:17 -04003432
3433 req.hash_type = cpu_to_le32(vnic->hash_type);
3434
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003435 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3436 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3437 max_rings = bp->rx_nr_rings - 1;
3438 else
3439 max_rings = bp->rx_nr_rings;
3440 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003441 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003442 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003443
3444 /* Fill the RSS indirection table with ring group ids */
3445 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3446 if (j == max_rings)
3447 j = 0;
3448 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3449 }
3450
3451 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3452 req.hash_key_tbl_addr =
3453 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3454 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003455 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003456 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3457}
3458
3459static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3460{
3461 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3462 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3463
3464 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3465 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3466 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3467 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3468 req.enables =
3469 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3470 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3471 /* thresholds not implemented in firmware yet */
3472 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3473 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3474 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3475 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3476}
3477
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003478static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3479 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003480{
3481 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3482
3483 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3484 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003485 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003486
3487 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003488 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003489}
3490
3491static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3492{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003493 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003494
3495 for (i = 0; i < bp->nr_vnics; i++) {
3496 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3497
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003498 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3499 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3500 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3501 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003502 }
3503 bp->rsscos_nr_ctxs = 0;
3504}
3505
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003506static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003507{
3508 int rc;
3509 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3510 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3511 bp->hwrm_cmd_resp_addr;
3512
3513 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3514 -1);
3515
3516 mutex_lock(&bp->hwrm_cmd_lock);
3517 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3518 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003519 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003520 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3521 mutex_unlock(&bp->hwrm_cmd_lock);
3522
3523 return rc;
3524}
3525
3526static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3527{
Michael Chanb81a90d2016-01-02 23:45:01 -05003528 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003529 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3530 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003531 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003532
3533 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003534
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003535 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3536 /* Only RSS support for now TBD: COS & LB */
3537 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3538 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3539 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3540 VNIC_CFG_REQ_ENABLES_MRU);
3541 } else {
3542 req.rss_rule = cpu_to_le16(0xffff);
3543 }
3544
3545 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3546 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003547 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3548 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3549 } else {
3550 req.cos_rule = cpu_to_le16(0xffff);
3551 }
3552
Michael Chanc0c050c2015-10-22 16:01:17 -04003553 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003554 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003555 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003556 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003557 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3558 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003559
Michael Chanb81a90d2016-01-02 23:45:01 -05003560 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003561 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3562 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3563
3564 req.lb_rule = cpu_to_le16(0xffff);
3565 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3566 VLAN_HLEN);
3567
Michael Chancf6645f2016-06-13 02:25:28 -04003568#ifdef CONFIG_BNXT_SRIOV
3569 if (BNXT_VF(bp))
3570 def_vlan = bp->vf.vlan;
3571#endif
3572 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003573 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3574
3575 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3576}
3577
3578static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3579{
3580 u32 rc = 0;
3581
3582 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3583 struct hwrm_vnic_free_input req = {0};
3584
3585 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3586 req.vnic_id =
3587 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3588
3589 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3590 if (rc)
3591 return rc;
3592 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3593 }
3594 return rc;
3595}
3596
3597static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3598{
3599 u16 i;
3600
3601 for (i = 0; i < bp->nr_vnics; i++)
3602 bnxt_hwrm_vnic_free_one(bp, i);
3603}
3604
Michael Chanb81a90d2016-01-02 23:45:01 -05003605static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3606 unsigned int start_rx_ring_idx,
3607 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003608{
Michael Chanb81a90d2016-01-02 23:45:01 -05003609 int rc = 0;
3610 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003611 struct hwrm_vnic_alloc_input req = {0};
3612 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3613
3614 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003615 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3616 grp_idx = bp->rx_ring[i].bnapi->index;
3617 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003618 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003619 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003620 break;
3621 }
3622 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003623 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003624 }
3625
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003626 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3627 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003628 if (vnic_id == 0)
3629 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3630
3631 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3632
3633 mutex_lock(&bp->hwrm_cmd_lock);
3634 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3635 if (!rc)
3636 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3637 mutex_unlock(&bp->hwrm_cmd_lock);
3638 return rc;
3639}
3640
3641static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3642{
3643 u16 i;
3644 u32 rc = 0;
3645
3646 mutex_lock(&bp->hwrm_cmd_lock);
3647 for (i = 0; i < bp->rx_nr_rings; i++) {
3648 struct hwrm_ring_grp_alloc_input req = {0};
3649 struct hwrm_ring_grp_alloc_output *resp =
3650 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003651 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003652
3653 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3654
Michael Chanb81a90d2016-01-02 23:45:01 -05003655 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3656 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3657 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3658 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003659
3660 rc = _hwrm_send_message(bp, &req, sizeof(req),
3661 HWRM_CMD_TIMEOUT);
3662 if (rc)
3663 break;
3664
Michael Chanb81a90d2016-01-02 23:45:01 -05003665 bp->grp_info[grp_idx].fw_grp_id =
3666 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003667 }
3668 mutex_unlock(&bp->hwrm_cmd_lock);
3669 return rc;
3670}
3671
3672static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3673{
3674 u16 i;
3675 u32 rc = 0;
3676 struct hwrm_ring_grp_free_input req = {0};
3677
3678 if (!bp->grp_info)
3679 return 0;
3680
3681 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3682
3683 mutex_lock(&bp->hwrm_cmd_lock);
3684 for (i = 0; i < bp->cp_nr_rings; i++) {
3685 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3686 continue;
3687 req.ring_group_id =
3688 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3689
3690 rc = _hwrm_send_message(bp, &req, sizeof(req),
3691 HWRM_CMD_TIMEOUT);
3692 if (rc)
3693 break;
3694 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3695 }
3696 mutex_unlock(&bp->hwrm_cmd_lock);
3697 return rc;
3698}
3699
3700static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3701 struct bnxt_ring_struct *ring,
3702 u32 ring_type, u32 map_index,
3703 u32 stats_ctx_id)
3704{
3705 int rc = 0, err = 0;
3706 struct hwrm_ring_alloc_input req = {0};
3707 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3708 u16 ring_id;
3709
3710 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3711
3712 req.enables = 0;
3713 if (ring->nr_pages > 1) {
3714 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3715 /* Page size is in log2 units */
3716 req.page_size = BNXT_PAGE_SHIFT;
3717 req.page_tbl_depth = 1;
3718 } else {
3719 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3720 }
3721 req.fbo = 0;
3722 /* Association of ring index with doorbell index and MSIX number */
3723 req.logical_id = cpu_to_le16(map_index);
3724
3725 switch (ring_type) {
3726 case HWRM_RING_ALLOC_TX:
3727 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3728 /* Association of transmit ring with completion ring */
3729 req.cmpl_ring_id =
3730 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3731 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3732 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3733 req.queue_id = cpu_to_le16(ring->queue_id);
3734 break;
3735 case HWRM_RING_ALLOC_RX:
3736 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3737 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3738 break;
3739 case HWRM_RING_ALLOC_AGG:
3740 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3741 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3742 break;
3743 case HWRM_RING_ALLOC_CMPL:
3744 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3745 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3746 if (bp->flags & BNXT_FLAG_USING_MSIX)
3747 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3748 break;
3749 default:
3750 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3751 ring_type);
3752 return -1;
3753 }
3754
3755 mutex_lock(&bp->hwrm_cmd_lock);
3756 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3757 err = le16_to_cpu(resp->error_code);
3758 ring_id = le16_to_cpu(resp->ring_id);
3759 mutex_unlock(&bp->hwrm_cmd_lock);
3760
3761 if (rc || err) {
3762 switch (ring_type) {
3763 case RING_FREE_REQ_RING_TYPE_CMPL:
3764 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3765 rc, err);
3766 return -1;
3767
3768 case RING_FREE_REQ_RING_TYPE_RX:
3769 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3770 rc, err);
3771 return -1;
3772
3773 case RING_FREE_REQ_RING_TYPE_TX:
3774 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3775 rc, err);
3776 return -1;
3777
3778 default:
3779 netdev_err(bp->dev, "Invalid ring\n");
3780 return -1;
3781 }
3782 }
3783 ring->fw_ring_id = ring_id;
3784 return rc;
3785}
3786
3787static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3788{
3789 int i, rc = 0;
3790
Michael Chanedd0c2c2015-12-27 18:19:19 -05003791 for (i = 0; i < bp->cp_nr_rings; i++) {
3792 struct bnxt_napi *bnapi = bp->bnapi[i];
3793 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3794 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003795
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003796 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003797 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3798 INVALID_STATS_CTX_ID);
3799 if (rc)
3800 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003801 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3802 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003803 }
3804
Michael Chanedd0c2c2015-12-27 18:19:19 -05003805 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003806 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003807 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003808 u32 map_idx = txr->bnapi->index;
3809 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003810
Michael Chanb81a90d2016-01-02 23:45:01 -05003811 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3812 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003813 if (rc)
3814 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003815 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003816 }
3817
Michael Chanedd0c2c2015-12-27 18:19:19 -05003818 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003819 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003820 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003821 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003822
Michael Chanb81a90d2016-01-02 23:45:01 -05003823 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3824 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003825 if (rc)
3826 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003827 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003828 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003829 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003830 }
3831
3832 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3833 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003834 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003835 struct bnxt_ring_struct *ring =
3836 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003837 u32 grp_idx = rxr->bnapi->index;
3838 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003839
3840 rc = hwrm_ring_alloc_send_msg(bp, ring,
3841 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003842 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003843 INVALID_STATS_CTX_ID);
3844 if (rc)
3845 goto err_out;
3846
Michael Chanb81a90d2016-01-02 23:45:01 -05003847 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003848 writel(DB_KEY_RX | rxr->rx_agg_prod,
3849 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003850 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003851 }
3852 }
3853err_out:
3854 return rc;
3855}
3856
3857static int hwrm_ring_free_send_msg(struct bnxt *bp,
3858 struct bnxt_ring_struct *ring,
3859 u32 ring_type, int cmpl_ring_id)
3860{
3861 int rc;
3862 struct hwrm_ring_free_input req = {0};
3863 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3864 u16 error_code;
3865
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003866 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003867 req.ring_type = ring_type;
3868 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3869
3870 mutex_lock(&bp->hwrm_cmd_lock);
3871 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3872 error_code = le16_to_cpu(resp->error_code);
3873 mutex_unlock(&bp->hwrm_cmd_lock);
3874
3875 if (rc || error_code) {
3876 switch (ring_type) {
3877 case RING_FREE_REQ_RING_TYPE_CMPL:
3878 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3879 rc);
3880 return rc;
3881 case RING_FREE_REQ_RING_TYPE_RX:
3882 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3883 rc);
3884 return rc;
3885 case RING_FREE_REQ_RING_TYPE_TX:
3886 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3887 rc);
3888 return rc;
3889 default:
3890 netdev_err(bp->dev, "Invalid ring\n");
3891 return -1;
3892 }
3893 }
3894 return 0;
3895}
3896
Michael Chanedd0c2c2015-12-27 18:19:19 -05003897static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003898{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003899 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003900
3901 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003902 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003903
Michael Chanedd0c2c2015-12-27 18:19:19 -05003904 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003905 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003906 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003907 u32 grp_idx = txr->bnapi->index;
3908 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003909
Michael Chanedd0c2c2015-12-27 18:19:19 -05003910 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3911 hwrm_ring_free_send_msg(bp, ring,
3912 RING_FREE_REQ_RING_TYPE_TX,
3913 close_path ? cmpl_ring_id :
3914 INVALID_HW_RING_ID);
3915 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003916 }
3917 }
3918
Michael Chanedd0c2c2015-12-27 18:19:19 -05003919 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003920 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003921 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003922 u32 grp_idx = rxr->bnapi->index;
3923 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003924
Michael Chanedd0c2c2015-12-27 18:19:19 -05003925 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3926 hwrm_ring_free_send_msg(bp, ring,
3927 RING_FREE_REQ_RING_TYPE_RX,
3928 close_path ? cmpl_ring_id :
3929 INVALID_HW_RING_ID);
3930 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003931 bp->grp_info[grp_idx].rx_fw_ring_id =
3932 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003933 }
3934 }
3935
Michael Chanedd0c2c2015-12-27 18:19:19 -05003936 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003937 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003938 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003939 u32 grp_idx = rxr->bnapi->index;
3940 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003941
Michael Chanedd0c2c2015-12-27 18:19:19 -05003942 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3943 hwrm_ring_free_send_msg(bp, ring,
3944 RING_FREE_REQ_RING_TYPE_RX,
3945 close_path ? cmpl_ring_id :
3946 INVALID_HW_RING_ID);
3947 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003948 bp->grp_info[grp_idx].agg_fw_ring_id =
3949 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003950 }
3951 }
3952
Michael Chanedd0c2c2015-12-27 18:19:19 -05003953 for (i = 0; i < bp->cp_nr_rings; i++) {
3954 struct bnxt_napi *bnapi = bp->bnapi[i];
3955 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3956 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
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_CMPL,
3961 INVALID_HW_RING_ID);
3962 ring->fw_ring_id = INVALID_HW_RING_ID;
3963 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003964 }
3965 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003966}
3967
Michael Chanbb053f52016-02-26 04:00:02 -05003968static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3969 u32 buf_tmrs, u16 flags,
3970 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3971{
3972 req->flags = cpu_to_le16(flags);
3973 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3974 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3975 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3976 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3977 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3978 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3979 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3980 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3981}
3982
Michael Chanc0c050c2015-10-22 16:01:17 -04003983int bnxt_hwrm_set_coal(struct bnxt *bp)
3984{
3985 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05003986 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3987 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04003988 u16 max_buf, max_buf_irq;
3989 u16 buf_tmr, buf_tmr_irq;
3990 u32 flags;
3991
Michael Chandfc9c942016-02-26 04:00:03 -05003992 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3993 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3994 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3995 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003996
Michael Chandfb5b892016-02-26 04:00:01 -05003997 /* Each rx completion (2 records) should be DMAed immediately.
3998 * DMA 1/4 of the completion buffers at a time.
3999 */
4000 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04004001 /* max_buf must not be zero */
4002 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004003 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4004 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4005 /* buf timer set to 1/4 of interrupt timer */
4006 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4007 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4008 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004009
4010 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4011
4012 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4013 * if coal_ticks is less than 25 us.
4014 */
Michael Chandfb5b892016-02-26 04:00:01 -05004015 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004016 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4017
Michael Chanbb053f52016-02-26 04:00:02 -05004018 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004019 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4020
4021 /* max_buf must not be zero */
4022 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4023 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4024 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4025 /* buf timer set to 1/4 of interrupt timer */
4026 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4027 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4028 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4029
4030 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4031 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4032 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004033
4034 mutex_lock(&bp->hwrm_cmd_lock);
4035 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004036 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004037
Michael Chandfc9c942016-02-26 04:00:03 -05004038 req = &req_rx;
4039 if (!bnapi->rx_ring)
4040 req = &req_tx;
4041 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4042
4043 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004044 HWRM_CMD_TIMEOUT);
4045 if (rc)
4046 break;
4047 }
4048 mutex_unlock(&bp->hwrm_cmd_lock);
4049 return rc;
4050}
4051
4052static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4053{
4054 int rc = 0, i;
4055 struct hwrm_stat_ctx_free_input req = {0};
4056
4057 if (!bp->bnapi)
4058 return 0;
4059
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004060 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4061 return 0;
4062
Michael Chanc0c050c2015-10-22 16:01:17 -04004063 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4064
4065 mutex_lock(&bp->hwrm_cmd_lock);
4066 for (i = 0; i < bp->cp_nr_rings; i++) {
4067 struct bnxt_napi *bnapi = bp->bnapi[i];
4068 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4069
4070 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4071 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4072
4073 rc = _hwrm_send_message(bp, &req, sizeof(req),
4074 HWRM_CMD_TIMEOUT);
4075 if (rc)
4076 break;
4077
4078 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4079 }
4080 }
4081 mutex_unlock(&bp->hwrm_cmd_lock);
4082 return rc;
4083}
4084
4085static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4086{
4087 int rc = 0, i;
4088 struct hwrm_stat_ctx_alloc_input req = {0};
4089 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4090
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004091 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4092 return 0;
4093
Michael Chanc0c050c2015-10-22 16:01:17 -04004094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4095
Michael Chan51f30782016-07-01 18:46:29 -04004096 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004097
4098 mutex_lock(&bp->hwrm_cmd_lock);
4099 for (i = 0; i < bp->cp_nr_rings; i++) {
4100 struct bnxt_napi *bnapi = bp->bnapi[i];
4101 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4102
4103 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4104
4105 rc = _hwrm_send_message(bp, &req, sizeof(req),
4106 HWRM_CMD_TIMEOUT);
4107 if (rc)
4108 break;
4109
4110 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4111
4112 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4113 }
4114 mutex_unlock(&bp->hwrm_cmd_lock);
4115 return 0;
4116}
4117
Michael Chancf6645f2016-06-13 02:25:28 -04004118static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4119{
4120 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004121 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004122 int rc;
4123
4124 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4125 req.fid = cpu_to_le16(0xffff);
4126 mutex_lock(&bp->hwrm_cmd_lock);
4127 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4128 if (rc)
4129 goto func_qcfg_exit;
4130
4131#ifdef CONFIG_BNXT_SRIOV
4132 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004133 struct bnxt_vf_info *vf = &bp->vf;
4134
4135 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4136 }
4137#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004138 switch (resp->port_partition_type) {
4139 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4140 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4141 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4142 bp->port_partition_type = resp->port_partition_type;
4143 break;
4144 }
Michael Chancf6645f2016-06-13 02:25:28 -04004145
4146func_qcfg_exit:
4147 mutex_unlock(&bp->hwrm_cmd_lock);
4148 return rc;
4149}
4150
Michael Chan4a21b492015-12-27 18:19:26 -05004151int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004152{
4153 int rc = 0;
4154 struct hwrm_func_qcaps_input req = {0};
4155 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4156
4157 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4158 req.fid = cpu_to_le16(0xffff);
4159
4160 mutex_lock(&bp->hwrm_cmd_lock);
4161 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4162 if (rc)
4163 goto hwrm_func_qcaps_exit;
4164
Michael Chan7cc5a202016-09-19 03:58:05 -04004165 bp->tx_push_thresh = 0;
4166 if (resp->flags &
4167 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4168 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4169
Michael Chanc0c050c2015-10-22 16:01:17 -04004170 if (BNXT_PF(bp)) {
4171 struct bnxt_pf_info *pf = &bp->pf;
4172
4173 pf->fw_fid = le16_to_cpu(resp->fid);
4174 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004175 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004176 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004177 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004178 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4179 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4180 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004181 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004182 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4183 if (!pf->max_hw_ring_grps)
4184 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004185 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4186 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4187 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4188 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4189 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4190 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4191 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4192 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4193 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4194 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4195 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4196 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004197#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004198 struct bnxt_vf_info *vf = &bp->vf;
4199
4200 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chanc0c050c2015-10-22 16:01:17 -04004201
4202 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4203 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4204 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4205 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004206 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4207 if (!vf->max_hw_ring_grps)
4208 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004209 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4210 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4211 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan7cc5a202016-09-19 03:58:05 -04004212
4213 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004214 mutex_unlock(&bp->hwrm_cmd_lock);
4215
4216 if (is_valid_ether_addr(vf->mac_addr)) {
Michael Chan7cc5a202016-09-19 03:58:05 -04004217 /* overwrite netdev dev_adr with admin VF MAC */
4218 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
Michael Chan001154e2016-09-19 03:58:06 -04004219 } else {
Michael Chan7cc5a202016-09-19 03:58:05 -04004220 random_ether_addr(bp->dev->dev_addr);
Michael Chan001154e2016-09-19 03:58:06 -04004221 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4222 }
4223 return rc;
Michael Chan379a80a2015-10-23 15:06:19 -04004224#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004225 }
4226
Michael Chanc0c050c2015-10-22 16:01:17 -04004227hwrm_func_qcaps_exit:
4228 mutex_unlock(&bp->hwrm_cmd_lock);
4229 return rc;
4230}
4231
4232static int bnxt_hwrm_func_reset(struct bnxt *bp)
4233{
4234 struct hwrm_func_reset_input req = {0};
4235
4236 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4237 req.enables = 0;
4238
4239 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4240}
4241
4242static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4243{
4244 int rc = 0;
4245 struct hwrm_queue_qportcfg_input req = {0};
4246 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4247 u8 i, *qptr;
4248
4249 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4250
4251 mutex_lock(&bp->hwrm_cmd_lock);
4252 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4253 if (rc)
4254 goto qportcfg_exit;
4255
4256 if (!resp->max_configurable_queues) {
4257 rc = -EINVAL;
4258 goto qportcfg_exit;
4259 }
4260 bp->max_tc = resp->max_configurable_queues;
4261 if (bp->max_tc > BNXT_MAX_QUEUE)
4262 bp->max_tc = BNXT_MAX_QUEUE;
4263
Michael Chan441cabb2016-09-19 03:58:02 -04004264 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4265 bp->max_tc = 1;
4266
Michael Chanc0c050c2015-10-22 16:01:17 -04004267 qptr = &resp->queue_id0;
4268 for (i = 0; i < bp->max_tc; i++) {
4269 bp->q_info[i].queue_id = *qptr++;
4270 bp->q_info[i].queue_profile = *qptr++;
4271 }
4272
4273qportcfg_exit:
4274 mutex_unlock(&bp->hwrm_cmd_lock);
4275 return rc;
4276}
4277
4278static int bnxt_hwrm_ver_get(struct bnxt *bp)
4279{
4280 int rc;
4281 struct hwrm_ver_get_input req = {0};
4282 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4283
Michael Chane6ef2692016-03-28 19:46:05 -04004284 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004285 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4286 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4287 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4288 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4289 mutex_lock(&bp->hwrm_cmd_lock);
4290 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4291 if (rc)
4292 goto hwrm_ver_get_exit;
4293
4294 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4295
Michael Chan11f15ed2016-04-05 14:08:55 -04004296 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4297 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004298 if (resp->hwrm_intf_maj < 1) {
4299 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004300 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004301 resp->hwrm_intf_upd);
4302 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004303 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004304 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004305 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4306 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4307
Michael Chanff4fe812016-02-26 04:00:04 -05004308 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4309 if (!bp->hwrm_cmd_timeout)
4310 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4311
Michael Chane6ef2692016-03-28 19:46:05 -04004312 if (resp->hwrm_intf_maj >= 1)
4313 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4314
Michael Chan659c8052016-06-13 02:25:33 -04004315 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004316 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4317 !resp->chip_metal)
4318 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004319
Michael Chanc0c050c2015-10-22 16:01:17 -04004320hwrm_ver_get_exit:
4321 mutex_unlock(&bp->hwrm_cmd_lock);
4322 return rc;
4323}
4324
Rob Swindell5ac67d82016-09-19 03:58:03 -04004325int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4326{
Rob Swindell878786d2016-09-20 03:36:33 -04004327#if IS_ENABLED(CONFIG_RTC_LIB)
Rob Swindell5ac67d82016-09-19 03:58:03 -04004328 struct hwrm_fw_set_time_input req = {0};
4329 struct rtc_time tm;
4330 struct timeval tv;
4331
4332 if (bp->hwrm_spec_code < 0x10400)
4333 return -EOPNOTSUPP;
4334
4335 do_gettimeofday(&tv);
4336 rtc_time_to_tm(tv.tv_sec, &tm);
4337 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4338 req.year = cpu_to_le16(1900 + tm.tm_year);
4339 req.month = 1 + tm.tm_mon;
4340 req.day = tm.tm_mday;
4341 req.hour = tm.tm_hour;
4342 req.minute = tm.tm_min;
4343 req.second = tm.tm_sec;
4344 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Rob Swindell878786d2016-09-20 03:36:33 -04004345#else
4346 return -EOPNOTSUPP;
4347#endif
Rob Swindell5ac67d82016-09-19 03:58:03 -04004348}
4349
Michael Chan3bdf56c2016-03-07 15:38:45 -05004350static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4351{
4352 int rc;
4353 struct bnxt_pf_info *pf = &bp->pf;
4354 struct hwrm_port_qstats_input req = {0};
4355
4356 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4357 return 0;
4358
4359 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4360 req.port_id = cpu_to_le16(pf->port_id);
4361 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4362 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4363 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4364 return rc;
4365}
4366
Michael Chanc0c050c2015-10-22 16:01:17 -04004367static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4368{
4369 if (bp->vxlan_port_cnt) {
4370 bnxt_hwrm_tunnel_dst_port_free(
4371 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4372 }
4373 bp->vxlan_port_cnt = 0;
4374 if (bp->nge_port_cnt) {
4375 bnxt_hwrm_tunnel_dst_port_free(
4376 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4377 }
4378 bp->nge_port_cnt = 0;
4379}
4380
4381static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4382{
4383 int rc, i;
4384 u32 tpa_flags = 0;
4385
4386 if (set_tpa)
4387 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4388 for (i = 0; i < bp->nr_vnics; i++) {
4389 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4390 if (rc) {
4391 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4392 rc, i);
4393 return rc;
4394 }
4395 }
4396 return 0;
4397}
4398
4399static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4400{
4401 int i;
4402
4403 for (i = 0; i < bp->nr_vnics; i++)
4404 bnxt_hwrm_vnic_set_rss(bp, i, false);
4405}
4406
4407static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4408 bool irq_re_init)
4409{
4410 if (bp->vnic_info) {
4411 bnxt_hwrm_clear_vnic_filter(bp);
4412 /* clear all RSS setting before free vnic ctx */
4413 bnxt_hwrm_clear_vnic_rss(bp);
4414 bnxt_hwrm_vnic_ctx_free(bp);
4415 /* before free the vnic, undo the vnic tpa settings */
4416 if (bp->flags & BNXT_FLAG_TPA)
4417 bnxt_set_tpa(bp, false);
4418 bnxt_hwrm_vnic_free(bp);
4419 }
4420 bnxt_hwrm_ring_free(bp, close_path);
4421 bnxt_hwrm_ring_grp_free(bp);
4422 if (irq_re_init) {
4423 bnxt_hwrm_stat_ctx_free(bp);
4424 bnxt_hwrm_free_tunnel_ports(bp);
4425 }
4426}
4427
4428static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4429{
4430 int rc;
4431
4432 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004433 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004434 if (rc) {
4435 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4436 vnic_id, rc);
4437 goto vnic_setup_err;
4438 }
4439 bp->rsscos_nr_ctxs++;
4440
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004441 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4442 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4443 if (rc) {
4444 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4445 vnic_id, rc);
4446 goto vnic_setup_err;
4447 }
4448 bp->rsscos_nr_ctxs++;
4449 }
4450
Michael Chanc0c050c2015-10-22 16:01:17 -04004451 /* configure default vnic, ring grp */
4452 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4453 if (rc) {
4454 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4455 vnic_id, rc);
4456 goto vnic_setup_err;
4457 }
4458
4459 /* Enable RSS hashing on vnic */
4460 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4461 if (rc) {
4462 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4463 vnic_id, rc);
4464 goto vnic_setup_err;
4465 }
4466
4467 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4468 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4469 if (rc) {
4470 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4471 vnic_id, rc);
4472 }
4473 }
4474
4475vnic_setup_err:
4476 return rc;
4477}
4478
4479static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4480{
4481#ifdef CONFIG_RFS_ACCEL
4482 int i, rc = 0;
4483
4484 for (i = 0; i < bp->rx_nr_rings; i++) {
4485 u16 vnic_id = i + 1;
4486 u16 ring_id = i;
4487
4488 if (vnic_id >= bp->nr_vnics)
4489 break;
4490
4491 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004492 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004493 if (rc) {
4494 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4495 vnic_id, rc);
4496 break;
4497 }
4498 rc = bnxt_setup_vnic(bp, vnic_id);
4499 if (rc)
4500 break;
4501 }
4502 return rc;
4503#else
4504 return 0;
4505#endif
4506}
4507
Michael Chan17c71ac2016-07-01 18:46:27 -04004508/* Allow PF and VF with default VLAN to be in promiscuous mode */
4509static bool bnxt_promisc_ok(struct bnxt *bp)
4510{
4511#ifdef CONFIG_BNXT_SRIOV
4512 if (BNXT_VF(bp) && !bp->vf.vlan)
4513 return false;
4514#endif
4515 return true;
4516}
4517
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004518static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4519{
4520 unsigned int rc = 0;
4521
4522 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4523 if (rc) {
4524 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4525 rc);
4526 return rc;
4527 }
4528
4529 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4530 if (rc) {
4531 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4532 rc);
4533 return rc;
4534 }
4535 return rc;
4536}
4537
Michael Chanb664f002015-12-02 01:54:08 -05004538static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004539static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004540
Michael Chanc0c050c2015-10-22 16:01:17 -04004541static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4542{
Michael Chan7d2837d2016-05-04 16:56:44 -04004543 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004544 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004545 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004546
4547 if (irq_re_init) {
4548 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4549 if (rc) {
4550 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4551 rc);
4552 goto err_out;
4553 }
4554 }
4555
4556 rc = bnxt_hwrm_ring_alloc(bp);
4557 if (rc) {
4558 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4559 goto err_out;
4560 }
4561
4562 rc = bnxt_hwrm_ring_grp_alloc(bp);
4563 if (rc) {
4564 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4565 goto err_out;
4566 }
4567
Prashant Sreedharan76595192016-07-18 07:15:22 -04004568 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4569 rx_nr_rings--;
4570
Michael Chanc0c050c2015-10-22 16:01:17 -04004571 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004572 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004573 if (rc) {
4574 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4575 goto err_out;
4576 }
4577
4578 rc = bnxt_setup_vnic(bp, 0);
4579 if (rc)
4580 goto err_out;
4581
4582 if (bp->flags & BNXT_FLAG_RFS) {
4583 rc = bnxt_alloc_rfs_vnics(bp);
4584 if (rc)
4585 goto err_out;
4586 }
4587
4588 if (bp->flags & BNXT_FLAG_TPA) {
4589 rc = bnxt_set_tpa(bp, true);
4590 if (rc)
4591 goto err_out;
4592 }
4593
4594 if (BNXT_VF(bp))
4595 bnxt_update_vf_mac(bp);
4596
4597 /* Filter for default vnic 0 */
4598 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4599 if (rc) {
4600 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4601 goto err_out;
4602 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004603 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004604
Michael Chan7d2837d2016-05-04 16:56:44 -04004605 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004606
Michael Chan17c71ac2016-07-01 18:46:27 -04004607 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004608 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4609
4610 if (bp->dev->flags & IFF_ALLMULTI) {
4611 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4612 vnic->mc_list_count = 0;
4613 } else {
4614 u32 mask = 0;
4615
4616 bnxt_mc_list_updated(bp, &mask);
4617 vnic->rx_mask |= mask;
4618 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004619
Michael Chanb664f002015-12-02 01:54:08 -05004620 rc = bnxt_cfg_rx_mode(bp);
4621 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004622 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004623
4624 rc = bnxt_hwrm_set_coal(bp);
4625 if (rc)
4626 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004627 rc);
4628
4629 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4630 rc = bnxt_setup_nitroa0_vnic(bp);
4631 if (rc)
4632 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4633 rc);
4634 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004635
Michael Chancf6645f2016-06-13 02:25:28 -04004636 if (BNXT_VF(bp)) {
4637 bnxt_hwrm_func_qcfg(bp);
4638 netdev_update_features(bp->dev);
4639 }
4640
Michael Chanc0c050c2015-10-22 16:01:17 -04004641 return 0;
4642
4643err_out:
4644 bnxt_hwrm_resource_free(bp, 0, true);
4645
4646 return rc;
4647}
4648
4649static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4650{
4651 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4652 return 0;
4653}
4654
4655static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4656{
4657 bnxt_init_rx_rings(bp);
4658 bnxt_init_tx_rings(bp);
4659 bnxt_init_ring_grps(bp, irq_re_init);
4660 bnxt_init_vnics(bp);
4661
4662 return bnxt_init_chip(bp, irq_re_init);
4663}
4664
4665static void bnxt_disable_int(struct bnxt *bp)
4666{
4667 int i;
4668
4669 if (!bp->bnapi)
4670 return;
4671
4672 for (i = 0; i < bp->cp_nr_rings; i++) {
4673 struct bnxt_napi *bnapi = bp->bnapi[i];
4674 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4675
4676 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4677 }
4678}
4679
4680static void bnxt_enable_int(struct bnxt *bp)
4681{
4682 int i;
4683
4684 atomic_set(&bp->intr_sem, 0);
4685 for (i = 0; i < bp->cp_nr_rings; i++) {
4686 struct bnxt_napi *bnapi = bp->bnapi[i];
4687 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4688
4689 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4690 }
4691}
4692
4693static int bnxt_set_real_num_queues(struct bnxt *bp)
4694{
4695 int rc;
4696 struct net_device *dev = bp->dev;
4697
4698 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4699 if (rc)
4700 return rc;
4701
4702 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4703 if (rc)
4704 return rc;
4705
4706#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004707 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004708 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004709#endif
4710
4711 return rc;
4712}
4713
Michael Chan6e6c5a52016-01-02 23:45:02 -05004714static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4715 bool shared)
4716{
4717 int _rx = *rx, _tx = *tx;
4718
4719 if (shared) {
4720 *rx = min_t(int, _rx, max);
4721 *tx = min_t(int, _tx, max);
4722 } else {
4723 if (max < 2)
4724 return -ENOMEM;
4725
4726 while (_rx + _tx > max) {
4727 if (_rx > _tx && _rx > 1)
4728 _rx--;
4729 else if (_tx > 1)
4730 _tx--;
4731 }
4732 *rx = _rx;
4733 *tx = _tx;
4734 }
4735 return 0;
4736}
4737
Michael Chanc0c050c2015-10-22 16:01:17 -04004738static int bnxt_setup_msix(struct bnxt *bp)
4739{
4740 struct msix_entry *msix_ent;
4741 struct net_device *dev = bp->dev;
Michael Chan01657bc2016-01-02 23:45:03 -05004742 int i, total_vecs, rc = 0, min = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004743 const int len = sizeof(bp->irq_tbl[0].name);
4744
4745 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4746 total_vecs = bp->cp_nr_rings;
4747
4748 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4749 if (!msix_ent)
4750 return -ENOMEM;
4751
4752 for (i = 0; i < total_vecs; i++) {
4753 msix_ent[i].entry = i;
4754 msix_ent[i].vector = 0;
4755 }
4756
Michael Chan01657bc2016-01-02 23:45:03 -05004757 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4758 min = 2;
4759
4760 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004761 if (total_vecs < 0) {
4762 rc = -ENODEV;
4763 goto msix_setup_exit;
4764 }
4765
4766 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4767 if (bp->irq_tbl) {
4768 int tcs;
4769
4770 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004771 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004772 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004773 if (rc)
4774 goto msix_setup_exit;
4775
Michael Chanc0c050c2015-10-22 16:01:17 -04004776 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4777 tcs = netdev_get_num_tc(dev);
4778 if (tcs > 1) {
4779 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4780 if (bp->tx_nr_rings_per_tc == 0) {
4781 netdev_reset_tc(dev);
4782 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4783 } else {
4784 int i, off, count;
4785
4786 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4787 for (i = 0; i < tcs; i++) {
4788 count = bp->tx_nr_rings_per_tc;
4789 off = i * count;
4790 netdev_set_tc_queue(dev, i, count, off);
4791 }
4792 }
4793 }
Michael Chan01657bc2016-01-02 23:45:03 -05004794 bp->cp_nr_rings = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004795
4796 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chan01657bc2016-01-02 23:45:03 -05004797 char *attr;
4798
Michael Chanc0c050c2015-10-22 16:01:17 -04004799 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chan01657bc2016-01-02 23:45:03 -05004800 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
Michael Chanc0c050c2015-10-22 16:01:17 -04004807 snprintf(bp->irq_tbl[i].name, len,
Michael Chan01657bc2016-01-02 23:45:03 -05004808 "%s-%s-%d", dev->name, attr, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004809 bp->irq_tbl[i].handler = bnxt_msix;
4810 }
4811 rc = bnxt_set_real_num_queues(bp);
4812 if (rc)
4813 goto msix_setup_exit;
4814 } else {
4815 rc = -ENOMEM;
4816 goto msix_setup_exit;
4817 }
4818 bp->flags |= BNXT_FLAG_USING_MSIX;
4819 kfree(msix_ent);
4820 return 0;
4821
4822msix_setup_exit:
4823 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4824 pci_disable_msix(bp->pdev);
4825 kfree(msix_ent);
4826 return rc;
4827}
4828
4829static int bnxt_setup_inta(struct bnxt *bp)
4830{
4831 int rc;
4832 const int len = sizeof(bp->irq_tbl[0].name);
4833
4834 if (netdev_get_num_tc(bp->dev))
4835 netdev_reset_tc(bp->dev);
4836
4837 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4838 if (!bp->irq_tbl) {
4839 rc = -ENOMEM;
4840 return rc;
4841 }
4842 bp->rx_nr_rings = 1;
4843 bp->tx_nr_rings = 1;
4844 bp->cp_nr_rings = 1;
4845 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004846 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004847 bp->irq_tbl[0].vector = bp->pdev->irq;
4848 snprintf(bp->irq_tbl[0].name, len,
4849 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4850 bp->irq_tbl[0].handler = bnxt_inta;
4851 rc = bnxt_set_real_num_queues(bp);
4852 return rc;
4853}
4854
4855static int bnxt_setup_int_mode(struct bnxt *bp)
4856{
4857 int rc = 0;
4858
4859 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4860 rc = bnxt_setup_msix(bp);
4861
Michael Chan1fa72e22016-04-25 02:30:49 -04004862 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004863 /* fallback to INTA */
4864 rc = bnxt_setup_inta(bp);
4865 }
4866 return rc;
4867}
4868
4869static void bnxt_free_irq(struct bnxt *bp)
4870{
4871 struct bnxt_irq *irq;
4872 int i;
4873
4874#ifdef CONFIG_RFS_ACCEL
4875 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4876 bp->dev->rx_cpu_rmap = NULL;
4877#endif
4878 if (!bp->irq_tbl)
4879 return;
4880
4881 for (i = 0; i < bp->cp_nr_rings; i++) {
4882 irq = &bp->irq_tbl[i];
4883 if (irq->requested)
4884 free_irq(irq->vector, bp->bnapi[i]);
4885 irq->requested = 0;
4886 }
4887 if (bp->flags & BNXT_FLAG_USING_MSIX)
4888 pci_disable_msix(bp->pdev);
4889 kfree(bp->irq_tbl);
4890 bp->irq_tbl = NULL;
4891}
4892
4893static int bnxt_request_irq(struct bnxt *bp)
4894{
Michael Chanb81a90d2016-01-02 23:45:01 -05004895 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004896 unsigned long flags = 0;
4897#ifdef CONFIG_RFS_ACCEL
4898 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4899#endif
4900
4901 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4902 flags = IRQF_SHARED;
4903
Michael Chanb81a90d2016-01-02 23:45:01 -05004904 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004905 struct bnxt_irq *irq = &bp->irq_tbl[i];
4906#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05004907 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004908 rc = irq_cpu_rmap_add(rmap, irq->vector);
4909 if (rc)
4910 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004911 j);
4912 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04004913 }
4914#endif
4915 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4916 bp->bnapi[i]);
4917 if (rc)
4918 break;
4919
4920 irq->requested = 1;
4921 }
4922 return rc;
4923}
4924
4925static void bnxt_del_napi(struct bnxt *bp)
4926{
4927 int i;
4928
4929 if (!bp->bnapi)
4930 return;
4931
4932 for (i = 0; i < bp->cp_nr_rings; i++) {
4933 struct bnxt_napi *bnapi = bp->bnapi[i];
4934
4935 napi_hash_del(&bnapi->napi);
4936 netif_napi_del(&bnapi->napi);
4937 }
4938}
4939
4940static void bnxt_init_napi(struct bnxt *bp)
4941{
4942 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004943 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004944 struct bnxt_napi *bnapi;
4945
4946 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004947 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4948 cp_nr_rings--;
4949 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004950 bnapi = bp->bnapi[i];
4951 netif_napi_add(bp->dev, &bnapi->napi,
4952 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004953 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004954 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4955 bnapi = bp->bnapi[cp_nr_rings];
4956 netif_napi_add(bp->dev, &bnapi->napi,
4957 bnxt_poll_nitroa0, 64);
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004958 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004959 } else {
4960 bnapi = bp->bnapi[0];
4961 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004962 }
4963}
4964
4965static void bnxt_disable_napi(struct bnxt *bp)
4966{
4967 int i;
4968
4969 if (!bp->bnapi)
4970 return;
4971
4972 for (i = 0; i < bp->cp_nr_rings; i++) {
4973 napi_disable(&bp->bnapi[i]->napi);
4974 bnxt_disable_poll(bp->bnapi[i]);
4975 }
4976}
4977
4978static void bnxt_enable_napi(struct bnxt *bp)
4979{
4980 int i;
4981
4982 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04004983 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04004984 bnxt_enable_poll(bp->bnapi[i]);
4985 napi_enable(&bp->bnapi[i]->napi);
4986 }
4987}
4988
4989static void bnxt_tx_disable(struct bnxt *bp)
4990{
4991 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004992 struct bnxt_tx_ring_info *txr;
4993 struct netdev_queue *txq;
4994
Michael Chanb6ab4b02016-01-02 23:44:59 -05004995 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004996 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004997 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004998 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004999 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04005000 }
5001 }
5002 /* Stop all TX queues */
5003 netif_tx_disable(bp->dev);
5004 netif_carrier_off(bp->dev);
5005}
5006
5007static void bnxt_tx_enable(struct bnxt *bp)
5008{
5009 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04005010 struct bnxt_tx_ring_info *txr;
5011 struct netdev_queue *txq;
5012
5013 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05005014 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005015 txq = netdev_get_tx_queue(bp->dev, i);
5016 txr->dev_state = 0;
5017 }
5018 netif_tx_wake_all_queues(bp->dev);
5019 if (bp->link_info.link_up)
5020 netif_carrier_on(bp->dev);
5021}
5022
5023static void bnxt_report_link(struct bnxt *bp)
5024{
5025 if (bp->link_info.link_up) {
5026 const char *duplex;
5027 const char *flow_ctrl;
5028 u16 speed;
5029
5030 netif_carrier_on(bp->dev);
5031 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5032 duplex = "full";
5033 else
5034 duplex = "half";
5035 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5036 flow_ctrl = "ON - receive & transmit";
5037 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5038 flow_ctrl = "ON - transmit";
5039 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5040 flow_ctrl = "ON - receive";
5041 else
5042 flow_ctrl = "none";
5043 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5044 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5045 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005046 if (bp->flags & BNXT_FLAG_EEE_CAP)
5047 netdev_info(bp->dev, "EEE is %s\n",
5048 bp->eee.eee_active ? "active" :
5049 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005050 } else {
5051 netif_carrier_off(bp->dev);
5052 netdev_err(bp->dev, "NIC Link is Down\n");
5053 }
5054}
5055
Michael Chan170ce012016-04-05 14:08:57 -04005056static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5057{
5058 int rc = 0;
5059 struct hwrm_port_phy_qcaps_input req = {0};
5060 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005061 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005062
5063 if (bp->hwrm_spec_code < 0x10201)
5064 return 0;
5065
5066 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5067
5068 mutex_lock(&bp->hwrm_cmd_lock);
5069 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5070 if (rc)
5071 goto hwrm_phy_qcaps_exit;
5072
5073 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5074 struct ethtool_eee *eee = &bp->eee;
5075 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5076
5077 bp->flags |= BNXT_FLAG_EEE_CAP;
5078 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5079 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5080 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5081 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5082 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5083 }
Michael Chan93ed8112016-06-13 02:25:37 -04005084 link_info->support_auto_speeds =
5085 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005086
5087hwrm_phy_qcaps_exit:
5088 mutex_unlock(&bp->hwrm_cmd_lock);
5089 return rc;
5090}
5091
Michael Chanc0c050c2015-10-22 16:01:17 -04005092static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5093{
5094 int rc = 0;
5095 struct bnxt_link_info *link_info = &bp->link_info;
5096 struct hwrm_port_phy_qcfg_input req = {0};
5097 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5098 u8 link_up = link_info->link_up;
Michael Chan286ef9d2016-11-16 21:13:08 -05005099 u16 diff;
Michael Chanc0c050c2015-10-22 16:01:17 -04005100
5101 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5102
5103 mutex_lock(&bp->hwrm_cmd_lock);
5104 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5105 if (rc) {
5106 mutex_unlock(&bp->hwrm_cmd_lock);
5107 return rc;
5108 }
5109
5110 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5111 link_info->phy_link_status = resp->link;
5112 link_info->duplex = resp->duplex;
5113 link_info->pause = resp->pause;
5114 link_info->auto_mode = resp->auto_mode;
5115 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005116 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005117 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005118 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005119 if (link_info->phy_link_status == BNXT_LINK_LINK)
5120 link_info->link_speed = le16_to_cpu(resp->link_speed);
5121 else
5122 link_info->link_speed = 0;
5123 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005124 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5125 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005126 link_info->lp_auto_link_speeds =
5127 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005128 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5129 link_info->phy_ver[0] = resp->phy_maj;
5130 link_info->phy_ver[1] = resp->phy_min;
5131 link_info->phy_ver[2] = resp->phy_bld;
5132 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005133 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005134 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005135 link_info->phy_addr = resp->eee_config_phy_addr &
5136 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005137 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005138
Michael Chan170ce012016-04-05 14:08:57 -04005139 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5140 struct ethtool_eee *eee = &bp->eee;
5141 u16 fw_speeds;
5142
5143 eee->eee_active = 0;
5144 if (resp->eee_config_phy_addr &
5145 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5146 eee->eee_active = 1;
5147 fw_speeds = le16_to_cpu(
5148 resp->link_partner_adv_eee_link_speed_mask);
5149 eee->lp_advertised =
5150 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5151 }
5152
5153 /* Pull initial EEE config */
5154 if (!chng_link_state) {
5155 if (resp->eee_config_phy_addr &
5156 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5157 eee->eee_enabled = 1;
5158
5159 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5160 eee->advertised =
5161 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5162
5163 if (resp->eee_config_phy_addr &
5164 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5165 __le32 tmr;
5166
5167 eee->tx_lpi_enabled = 1;
5168 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5169 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5170 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5171 }
5172 }
5173 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005174 /* TODO: need to add more logic to report VF link */
5175 if (chng_link_state) {
5176 if (link_info->phy_link_status == BNXT_LINK_LINK)
5177 link_info->link_up = 1;
5178 else
5179 link_info->link_up = 0;
5180 if (link_up != link_info->link_up)
5181 bnxt_report_link(bp);
5182 } else {
5183 /* alwasy link down if not require to update link state */
5184 link_info->link_up = 0;
5185 }
5186 mutex_unlock(&bp->hwrm_cmd_lock);
Michael Chan286ef9d2016-11-16 21:13:08 -05005187
5188 diff = link_info->support_auto_speeds ^ link_info->advertising;
5189 if ((link_info->support_auto_speeds | diff) !=
5190 link_info->support_auto_speeds) {
5191 /* An advertised speed is no longer supported, so we need to
5192 * update the advertisement settings. See bnxt_reset() for
5193 * comments about the rtnl_lock() sequence below.
5194 */
5195 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5196 rtnl_lock();
5197 link_info->advertising = link_info->support_auto_speeds;
5198 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
5199 (link_info->autoneg & BNXT_AUTONEG_SPEED))
5200 bnxt_hwrm_set_link_setting(bp, true, false);
5201 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5202 rtnl_unlock();
5203 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005204 return 0;
5205}
5206
Michael Chan10289be2016-05-15 03:04:49 -04005207static void bnxt_get_port_module_status(struct bnxt *bp)
5208{
5209 struct bnxt_link_info *link_info = &bp->link_info;
5210 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5211 u8 module_status;
5212
5213 if (bnxt_update_link(bp, true))
5214 return;
5215
5216 module_status = link_info->module_status;
5217 switch (module_status) {
5218 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5219 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5220 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5221 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5222 bp->pf.port_id);
5223 if (bp->hwrm_spec_code >= 0x10201) {
5224 netdev_warn(bp->dev, "Module part number %s\n",
5225 resp->phy_vendor_partnumber);
5226 }
5227 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5228 netdev_warn(bp->dev, "TX is disabled\n");
5229 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5230 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5231 }
5232}
5233
Michael Chanc0c050c2015-10-22 16:01:17 -04005234static void
5235bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5236{
5237 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005238 if (bp->hwrm_spec_code >= 0x10201)
5239 req->auto_pause =
5240 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005241 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5242 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5243 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005244 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005245 req->enables |=
5246 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5247 } else {
5248 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5249 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5250 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5251 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5252 req->enables |=
5253 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005254 if (bp->hwrm_spec_code >= 0x10201) {
5255 req->auto_pause = req->force_pause;
5256 req->enables |= cpu_to_le32(
5257 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5258 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005259 }
5260}
5261
5262static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5263 struct hwrm_port_phy_cfg_input *req)
5264{
5265 u8 autoneg = bp->link_info.autoneg;
5266 u16 fw_link_speed = bp->link_info.req_link_speed;
5267 u32 advertising = bp->link_info.advertising;
5268
5269 if (autoneg & BNXT_AUTONEG_SPEED) {
5270 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005271 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005272
5273 req->enables |= cpu_to_le32(
5274 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5275 req->auto_link_speed_mask = cpu_to_le16(advertising);
5276
5277 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5278 req->flags |=
5279 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5280 } else {
5281 req->force_link_speed = cpu_to_le16(fw_link_speed);
5282 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5283 }
5284
Michael Chanc0c050c2015-10-22 16:01:17 -04005285 /* tell chimp that the setting takes effect immediately */
5286 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5287}
5288
5289int bnxt_hwrm_set_pause(struct bnxt *bp)
5290{
5291 struct hwrm_port_phy_cfg_input req = {0};
5292 int rc;
5293
5294 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5295 bnxt_hwrm_set_pause_common(bp, &req);
5296
5297 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5298 bp->link_info.force_link_chng)
5299 bnxt_hwrm_set_link_common(bp, &req);
5300
5301 mutex_lock(&bp->hwrm_cmd_lock);
5302 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5303 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5304 /* since changing of pause setting doesn't trigger any link
5305 * change event, the driver needs to update the current pause
5306 * result upon successfully return of the phy_cfg command
5307 */
5308 bp->link_info.pause =
5309 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5310 bp->link_info.auto_pause_setting = 0;
5311 if (!bp->link_info.force_link_chng)
5312 bnxt_report_link(bp);
5313 }
5314 bp->link_info.force_link_chng = false;
5315 mutex_unlock(&bp->hwrm_cmd_lock);
5316 return rc;
5317}
5318
Michael Chan939f7f02016-04-05 14:08:58 -04005319static void bnxt_hwrm_set_eee(struct bnxt *bp,
5320 struct hwrm_port_phy_cfg_input *req)
5321{
5322 struct ethtool_eee *eee = &bp->eee;
5323
5324 if (eee->eee_enabled) {
5325 u16 eee_speeds;
5326 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5327
5328 if (eee->tx_lpi_enabled)
5329 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5330 else
5331 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5332
5333 req->flags |= cpu_to_le32(flags);
5334 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5335 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5336 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5337 } else {
5338 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5339 }
5340}
5341
5342int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005343{
5344 struct hwrm_port_phy_cfg_input req = {0};
5345
5346 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5347 if (set_pause)
5348 bnxt_hwrm_set_pause_common(bp, &req);
5349
5350 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005351
5352 if (set_eee)
5353 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005354 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5355}
5356
Michael Chan33f7d552016-04-11 04:11:12 -04005357static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5358{
5359 struct hwrm_port_phy_cfg_input req = {0};
5360
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005361 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005362 return 0;
5363
5364 if (pci_num_vf(bp->pdev))
5365 return 0;
5366
5367 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
Michael Chan16d663a2016-11-16 21:13:07 -05005368 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
Michael Chan33f7d552016-04-11 04:11:12 -04005369 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5370}
5371
Michael Chan939f7f02016-04-05 14:08:58 -04005372static bool bnxt_eee_config_ok(struct bnxt *bp)
5373{
5374 struct ethtool_eee *eee = &bp->eee;
5375 struct bnxt_link_info *link_info = &bp->link_info;
5376
5377 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5378 return true;
5379
5380 if (eee->eee_enabled) {
5381 u32 advertising =
5382 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5383
5384 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5385 eee->eee_enabled = 0;
5386 return false;
5387 }
5388 if (eee->advertised & ~advertising) {
5389 eee->advertised = advertising & eee->supported;
5390 return false;
5391 }
5392 }
5393 return true;
5394}
5395
Michael Chanc0c050c2015-10-22 16:01:17 -04005396static int bnxt_update_phy_setting(struct bnxt *bp)
5397{
5398 int rc;
5399 bool update_link = false;
5400 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005401 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005402 struct bnxt_link_info *link_info = &bp->link_info;
5403
5404 rc = bnxt_update_link(bp, true);
5405 if (rc) {
5406 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5407 rc);
5408 return rc;
5409 }
5410 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005411 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5412 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005413 update_pause = true;
5414 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5415 link_info->force_pause_setting != link_info->req_flow_ctrl)
5416 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005417 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5418 if (BNXT_AUTO_MODE(link_info->auto_mode))
5419 update_link = true;
5420 if (link_info->req_link_speed != link_info->force_link_speed)
5421 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005422 if (link_info->req_duplex != link_info->duplex_setting)
5423 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005424 } else {
5425 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5426 update_link = true;
5427 if (link_info->advertising != link_info->auto_link_speeds)
5428 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005429 }
5430
Michael Chan16d663a2016-11-16 21:13:07 -05005431 /* The last close may have shutdown the link, so need to call
5432 * PHY_CFG to bring it back up.
5433 */
5434 if (!netif_carrier_ok(bp->dev))
5435 update_link = true;
5436
Michael Chan939f7f02016-04-05 14:08:58 -04005437 if (!bnxt_eee_config_ok(bp))
5438 update_eee = true;
5439
Michael Chanc0c050c2015-10-22 16:01:17 -04005440 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005441 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005442 else if (update_pause)
5443 rc = bnxt_hwrm_set_pause(bp);
5444 if (rc) {
5445 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5446 rc);
5447 return rc;
5448 }
5449
5450 return rc;
5451}
5452
Jeffrey Huang11809492015-11-05 16:25:49 -05005453/* Common routine to pre-map certain register block to different GRC window.
5454 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5455 * in PF and 3 windows in VF that can be customized to map in different
5456 * register blocks.
5457 */
5458static void bnxt_preset_reg_win(struct bnxt *bp)
5459{
5460 if (BNXT_PF(bp)) {
5461 /* CAG registers map to GRC window #4 */
5462 writel(BNXT_CAG_REG_BASE,
5463 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5464 }
5465}
5466
Michael Chanc0c050c2015-10-22 16:01:17 -04005467static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5468{
5469 int rc = 0;
5470
Jeffrey Huang11809492015-11-05 16:25:49 -05005471 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005472 netif_carrier_off(bp->dev);
5473 if (irq_re_init) {
5474 rc = bnxt_setup_int_mode(bp);
5475 if (rc) {
5476 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5477 rc);
5478 return rc;
5479 }
5480 }
5481 if ((bp->flags & BNXT_FLAG_RFS) &&
5482 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5483 /* disable RFS if falling back to INTA */
5484 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5485 bp->flags &= ~BNXT_FLAG_RFS;
5486 }
5487
5488 rc = bnxt_alloc_mem(bp, irq_re_init);
5489 if (rc) {
5490 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5491 goto open_err_free_mem;
5492 }
5493
5494 if (irq_re_init) {
5495 bnxt_init_napi(bp);
5496 rc = bnxt_request_irq(bp);
5497 if (rc) {
5498 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5499 goto open_err;
5500 }
5501 }
5502
5503 bnxt_enable_napi(bp);
5504
5505 rc = bnxt_init_nic(bp, irq_re_init);
5506 if (rc) {
5507 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5508 goto open_err;
5509 }
5510
5511 if (link_re_init) {
5512 rc = bnxt_update_phy_setting(bp);
5513 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005514 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005515 }
5516
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005517 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005518 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005519
Michael Chancaefe522015-12-09 19:35:42 -05005520 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005521 bnxt_enable_int(bp);
5522 /* Enable TX queues */
5523 bnxt_tx_enable(bp);
5524 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005525 /* Poll link status and check for SFP+ module status */
5526 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005527
5528 return 0;
5529
5530open_err:
5531 bnxt_disable_napi(bp);
5532 bnxt_del_napi(bp);
5533
5534open_err_free_mem:
5535 bnxt_free_skbs(bp);
5536 bnxt_free_irq(bp);
5537 bnxt_free_mem(bp, true);
5538 return rc;
5539}
5540
5541/* rtnl_lock held */
5542int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5543{
5544 int rc = 0;
5545
5546 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5547 if (rc) {
5548 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5549 dev_close(bp->dev);
5550 }
5551 return rc;
5552}
5553
5554static int bnxt_open(struct net_device *dev)
5555{
5556 struct bnxt *bp = netdev_priv(dev);
5557 int rc = 0;
5558
Michael Chan2a5bedf2016-07-01 18:46:21 -04005559 if (!test_bit(BNXT_STATE_FN_RST_DONE, &bp->state)) {
5560 rc = bnxt_hwrm_func_reset(bp);
5561 if (rc) {
5562 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
5563 rc);
5564 rc = -EBUSY;
5565 return rc;
5566 }
5567 /* Do func_reset during the 1st PF open only to prevent killing
5568 * the VFs when the PF is brought down and up.
5569 */
5570 if (BNXT_PF(bp))
5571 set_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005572 }
5573 return __bnxt_open_nic(bp, true, true);
5574}
5575
5576static void bnxt_disable_int_sync(struct bnxt *bp)
5577{
5578 int i;
5579
5580 atomic_inc(&bp->intr_sem);
5581 if (!netif_running(bp->dev))
5582 return;
5583
5584 bnxt_disable_int(bp);
5585 for (i = 0; i < bp->cp_nr_rings; i++)
5586 synchronize_irq(bp->irq_tbl[i].vector);
5587}
5588
5589int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5590{
5591 int rc = 0;
5592
5593#ifdef CONFIG_BNXT_SRIOV
5594 if (bp->sriov_cfg) {
5595 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5596 !bp->sriov_cfg,
5597 BNXT_SRIOV_CFG_WAIT_TMO);
5598 if (rc)
5599 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5600 }
5601#endif
5602 /* Change device state to avoid TX queue wake up's */
5603 bnxt_tx_disable(bp);
5604
Michael Chancaefe522015-12-09 19:35:42 -05005605 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005606 smp_mb__after_atomic();
5607 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5608 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005609
5610 /* Flush rings before disabling interrupts */
5611 bnxt_shutdown_nic(bp, irq_re_init);
5612
5613 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5614
5615 bnxt_disable_napi(bp);
5616 bnxt_disable_int_sync(bp);
5617 del_timer_sync(&bp->timer);
5618 bnxt_free_skbs(bp);
5619
5620 if (irq_re_init) {
5621 bnxt_free_irq(bp);
5622 bnxt_del_napi(bp);
5623 }
5624 bnxt_free_mem(bp, irq_re_init);
5625 return rc;
5626}
5627
5628static int bnxt_close(struct net_device *dev)
5629{
5630 struct bnxt *bp = netdev_priv(dev);
5631
5632 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005633 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005634 return 0;
5635}
5636
5637/* rtnl_lock held */
5638static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5639{
5640 switch (cmd) {
5641 case SIOCGMIIPHY:
5642 /* fallthru */
5643 case SIOCGMIIREG: {
5644 if (!netif_running(dev))
5645 return -EAGAIN;
5646
5647 return 0;
5648 }
5649
5650 case SIOCSMIIREG:
5651 if (!netif_running(dev))
5652 return -EAGAIN;
5653
5654 return 0;
5655
5656 default:
5657 /* do nothing */
5658 break;
5659 }
5660 return -EOPNOTSUPP;
5661}
5662
5663static struct rtnl_link_stats64 *
5664bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5665{
5666 u32 i;
5667 struct bnxt *bp = netdev_priv(dev);
5668
5669 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5670
5671 if (!bp->bnapi)
5672 return stats;
5673
5674 /* TODO check if we need to synchronize with bnxt_close path */
5675 for (i = 0; i < bp->cp_nr_rings; i++) {
5676 struct bnxt_napi *bnapi = bp->bnapi[i];
5677 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5678 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5679
5680 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5681 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5682 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5683
5684 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5685 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5686 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5687
5688 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5689 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5690 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5691
5692 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5693 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5694 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5695
5696 stats->rx_missed_errors +=
5697 le64_to_cpu(hw_stats->rx_discard_pkts);
5698
5699 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5700
Michael Chanc0c050c2015-10-22 16:01:17 -04005701 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5702 }
5703
Michael Chan9947f832016-03-07 15:38:46 -05005704 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5705 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5706 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5707
5708 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5709 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5710 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5711 le64_to_cpu(rx->rx_ovrsz_frames) +
5712 le64_to_cpu(rx->rx_runt_frames);
5713 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5714 le64_to_cpu(rx->rx_jbr_frames);
5715 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5716 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5717 stats->tx_errors = le64_to_cpu(tx->tx_err);
5718 }
5719
Michael Chanc0c050c2015-10-22 16:01:17 -04005720 return stats;
5721}
5722
5723static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5724{
5725 struct net_device *dev = bp->dev;
5726 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5727 struct netdev_hw_addr *ha;
5728 u8 *haddr;
5729 int mc_count = 0;
5730 bool update = false;
5731 int off = 0;
5732
5733 netdev_for_each_mc_addr(ha, dev) {
5734 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5735 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5736 vnic->mc_list_count = 0;
5737 return false;
5738 }
5739 haddr = ha->addr;
5740 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5741 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5742 update = true;
5743 }
5744 off += ETH_ALEN;
5745 mc_count++;
5746 }
5747 if (mc_count)
5748 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5749
5750 if (mc_count != vnic->mc_list_count) {
5751 vnic->mc_list_count = mc_count;
5752 update = true;
5753 }
5754 return update;
5755}
5756
5757static bool bnxt_uc_list_updated(struct bnxt *bp)
5758{
5759 struct net_device *dev = bp->dev;
5760 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5761 struct netdev_hw_addr *ha;
5762 int off = 0;
5763
5764 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5765 return true;
5766
5767 netdev_for_each_uc_addr(ha, dev) {
5768 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5769 return true;
5770
5771 off += ETH_ALEN;
5772 }
5773 return false;
5774}
5775
5776static void bnxt_set_rx_mode(struct net_device *dev)
5777{
5778 struct bnxt *bp = netdev_priv(dev);
5779 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5780 u32 mask = vnic->rx_mask;
5781 bool mc_update = false;
5782 bool uc_update;
5783
5784 if (!netif_running(dev))
5785 return;
5786
5787 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5788 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5789 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5790
Michael Chan17c71ac2016-07-01 18:46:27 -04005791 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005792 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5793
5794 uc_update = bnxt_uc_list_updated(bp);
5795
5796 if (dev->flags & IFF_ALLMULTI) {
5797 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5798 vnic->mc_list_count = 0;
5799 } else {
5800 mc_update = bnxt_mc_list_updated(bp, &mask);
5801 }
5802
5803 if (mask != vnic->rx_mask || uc_update || mc_update) {
5804 vnic->rx_mask = mask;
5805
5806 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5807 schedule_work(&bp->sp_task);
5808 }
5809}
5810
Michael Chanb664f002015-12-02 01:54:08 -05005811static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005812{
5813 struct net_device *dev = bp->dev;
5814 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5815 struct netdev_hw_addr *ha;
5816 int i, off = 0, rc;
5817 bool uc_update;
5818
5819 netif_addr_lock_bh(dev);
5820 uc_update = bnxt_uc_list_updated(bp);
5821 netif_addr_unlock_bh(dev);
5822
5823 if (!uc_update)
5824 goto skip_uc;
5825
5826 mutex_lock(&bp->hwrm_cmd_lock);
5827 for (i = 1; i < vnic->uc_filter_count; i++) {
5828 struct hwrm_cfa_l2_filter_free_input req = {0};
5829
5830 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5831 -1);
5832
5833 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5834
5835 rc = _hwrm_send_message(bp, &req, sizeof(req),
5836 HWRM_CMD_TIMEOUT);
5837 }
5838 mutex_unlock(&bp->hwrm_cmd_lock);
5839
5840 vnic->uc_filter_count = 1;
5841
5842 netif_addr_lock_bh(dev);
5843 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5844 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5845 } else {
5846 netdev_for_each_uc_addr(ha, dev) {
5847 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5848 off += ETH_ALEN;
5849 vnic->uc_filter_count++;
5850 }
5851 }
5852 netif_addr_unlock_bh(dev);
5853
5854 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5855 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5856 if (rc) {
5857 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5858 rc);
5859 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005860 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005861 }
5862 }
5863
5864skip_uc:
5865 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5866 if (rc)
5867 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5868 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005869
5870 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005871}
5872
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005873static bool bnxt_rfs_capable(struct bnxt *bp)
5874{
5875#ifdef CONFIG_RFS_ACCEL
5876 struct bnxt_pf_info *pf = &bp->pf;
5877 int vnics;
5878
5879 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5880 return false;
5881
5882 vnics = 1 + bp->rx_nr_rings;
Vasundhara Volama2304902016-07-25 12:33:36 -04005883 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5884 netdev_warn(bp->dev,
5885 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5886 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005887 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04005888 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005889
5890 return true;
5891#else
5892 return false;
5893#endif
5894}
5895
Michael Chanc0c050c2015-10-22 16:01:17 -04005896static netdev_features_t bnxt_fix_features(struct net_device *dev,
5897 netdev_features_t features)
5898{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005899 struct bnxt *bp = netdev_priv(dev);
5900
Vasundhara Volama2304902016-07-25 12:33:36 -04005901 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005902 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005903
5904 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5905 * turned on or off together.
5906 */
5907 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5908 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5909 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5910 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5911 NETIF_F_HW_VLAN_STAG_RX);
5912 else
5913 features |= NETIF_F_HW_VLAN_CTAG_RX |
5914 NETIF_F_HW_VLAN_STAG_RX;
5915 }
Michael Chancf6645f2016-06-13 02:25:28 -04005916#ifdef CONFIG_BNXT_SRIOV
5917 if (BNXT_VF(bp)) {
5918 if (bp->vf.vlan) {
5919 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5920 NETIF_F_HW_VLAN_STAG_RX);
5921 }
5922 }
5923#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005924 return features;
5925}
5926
5927static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5928{
5929 struct bnxt *bp = netdev_priv(dev);
5930 u32 flags = bp->flags;
5931 u32 changes;
5932 int rc = 0;
5933 bool re_init = false;
5934 bool update_tpa = false;
5935
5936 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005937 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005938 flags |= BNXT_FLAG_GRO;
5939 if (features & NETIF_F_LRO)
5940 flags |= BNXT_FLAG_LRO;
5941
5942 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5943 flags |= BNXT_FLAG_STRIP_VLAN;
5944
5945 if (features & NETIF_F_NTUPLE)
5946 flags |= BNXT_FLAG_RFS;
5947
5948 changes = flags ^ bp->flags;
5949 if (changes & BNXT_FLAG_TPA) {
5950 update_tpa = true;
5951 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5952 (flags & BNXT_FLAG_TPA) == 0)
5953 re_init = true;
5954 }
5955
5956 if (changes & ~BNXT_FLAG_TPA)
5957 re_init = true;
5958
5959 if (flags != bp->flags) {
5960 u32 old_flags = bp->flags;
5961
5962 bp->flags = flags;
5963
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005964 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005965 if (update_tpa)
5966 bnxt_set_ring_params(bp);
5967 return rc;
5968 }
5969
5970 if (re_init) {
5971 bnxt_close_nic(bp, false, false);
5972 if (update_tpa)
5973 bnxt_set_ring_params(bp);
5974
5975 return bnxt_open_nic(bp, false, false);
5976 }
5977 if (update_tpa) {
5978 rc = bnxt_set_tpa(bp,
5979 (flags & BNXT_FLAG_TPA) ?
5980 true : false);
5981 if (rc)
5982 bp->flags = old_flags;
5983 }
5984 }
5985 return rc;
5986}
5987
Michael Chan9f554592016-01-02 23:44:58 -05005988static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5989{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005990 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005991 int i = bnapi->index;
5992
Michael Chan3b2b7d92016-01-02 23:45:00 -05005993 if (!txr)
5994 return;
5995
Michael Chan9f554592016-01-02 23:44:58 -05005996 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5997 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5998 txr->tx_cons);
5999}
6000
6001static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6002{
Michael Chanb6ab4b02016-01-02 23:44:59 -05006003 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05006004 int i = bnapi->index;
6005
Michael Chan3b2b7d92016-01-02 23:45:00 -05006006 if (!rxr)
6007 return;
6008
Michael Chan9f554592016-01-02 23:44:58 -05006009 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",
6010 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6011 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6012 rxr->rx_sw_agg_prod);
6013}
6014
6015static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6016{
6017 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6018 int i = bnapi->index;
6019
6020 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6021 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6022}
6023
Michael Chanc0c050c2015-10-22 16:01:17 -04006024static void bnxt_dbg_dump_states(struct bnxt *bp)
6025{
6026 int i;
6027 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04006028
6029 for (i = 0; i < bp->cp_nr_rings; i++) {
6030 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04006031 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05006032 bnxt_dump_tx_sw_state(bnapi);
6033 bnxt_dump_rx_sw_state(bnapi);
6034 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04006035 }
6036 }
6037}
6038
Michael Chan6988bd92016-06-13 02:25:29 -04006039static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04006040{
Michael Chan6988bd92016-06-13 02:25:29 -04006041 if (!silent)
6042 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05006043 if (netif_running(bp->dev)) {
6044 bnxt_close_nic(bp, false, false);
6045 bnxt_open_nic(bp, false, false);
6046 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006047}
6048
6049static void bnxt_tx_timeout(struct net_device *dev)
6050{
6051 struct bnxt *bp = netdev_priv(dev);
6052
6053 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6054 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6055 schedule_work(&bp->sp_task);
6056}
6057
6058#ifdef CONFIG_NET_POLL_CONTROLLER
6059static void bnxt_poll_controller(struct net_device *dev)
6060{
6061 struct bnxt *bp = netdev_priv(dev);
6062 int i;
6063
6064 for (i = 0; i < bp->cp_nr_rings; i++) {
6065 struct bnxt_irq *irq = &bp->irq_tbl[i];
6066
6067 disable_irq(irq->vector);
6068 irq->handler(irq->vector, bp->bnapi[i]);
6069 enable_irq(irq->vector);
6070 }
6071}
6072#endif
6073
6074static void bnxt_timer(unsigned long data)
6075{
6076 struct bnxt *bp = (struct bnxt *)data;
6077 struct net_device *dev = bp->dev;
6078
6079 if (!netif_running(dev))
6080 return;
6081
6082 if (atomic_read(&bp->intr_sem) != 0)
6083 goto bnxt_restart_timer;
6084
Michael Chan3bdf56c2016-03-07 15:38:45 -05006085 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6086 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6087 schedule_work(&bp->sp_task);
6088 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006089bnxt_restart_timer:
6090 mod_timer(&bp->timer, jiffies + bp->current_interval);
6091}
6092
Michael Chan6988bd92016-06-13 02:25:29 -04006093/* Only called from bnxt_sp_task() */
6094static void bnxt_reset(struct bnxt *bp, bool silent)
6095{
6096 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6097 * for BNXT_STATE_IN_SP_TASK to clear.
6098 * If there is a parallel dev_close(), bnxt_close() may be holding
6099 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6100 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6101 */
6102 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6103 rtnl_lock();
6104 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6105 bnxt_reset_task(bp, silent);
6106 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6107 rtnl_unlock();
6108}
6109
Michael Chanc0c050c2015-10-22 16:01:17 -04006110static void bnxt_cfg_ntp_filters(struct bnxt *);
6111
6112static void bnxt_sp_task(struct work_struct *work)
6113{
6114 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6115 int rc;
6116
Michael Chan4cebdce2015-12-09 19:35:43 -05006117 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6118 smp_mb__after_atomic();
6119 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6120 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006121 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006122 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006123
6124 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6125 bnxt_cfg_rx_mode(bp);
6126
6127 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6128 bnxt_cfg_ntp_filters(bp);
6129 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
Michael Chan286ef9d2016-11-16 21:13:08 -05006130 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6131 &bp->sp_event))
6132 bnxt_hwrm_phy_qcaps(bp);
6133
Michael Chanc0c050c2015-10-22 16:01:17 -04006134 rc = bnxt_update_link(bp, true);
6135 if (rc)
6136 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6137 rc);
6138 }
6139 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6140 bnxt_hwrm_exec_fwd_req(bp);
6141 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6142 bnxt_hwrm_tunnel_dst_port_alloc(
6143 bp, bp->vxlan_port,
6144 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6145 }
6146 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6147 bnxt_hwrm_tunnel_dst_port_free(
6148 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6149 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006150 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6151 bnxt_hwrm_tunnel_dst_port_alloc(
6152 bp, bp->nge_port,
6153 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6154 }
6155 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6156 bnxt_hwrm_tunnel_dst_port_free(
6157 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6158 }
Michael Chan6988bd92016-06-13 02:25:29 -04006159 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6160 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006161
Michael Chanfc0f1922016-06-13 02:25:30 -04006162 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6163 bnxt_reset(bp, true);
6164
Michael Chan4bb13ab2016-04-05 14:09:01 -04006165 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006166 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006167
Michael Chan3bdf56c2016-03-07 15:38:45 -05006168 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6169 bnxt_hwrm_port_qstats(bp);
6170
Michael Chan4cebdce2015-12-09 19:35:43 -05006171 smp_mb__before_atomic();
6172 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006173}
6174
6175static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6176{
6177 int rc;
6178 struct bnxt *bp = netdev_priv(dev);
6179
6180 SET_NETDEV_DEV(dev, &pdev->dev);
6181
6182 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6183 rc = pci_enable_device(pdev);
6184 if (rc) {
6185 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6186 goto init_err;
6187 }
6188
6189 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6190 dev_err(&pdev->dev,
6191 "Cannot find PCI device base address, aborting\n");
6192 rc = -ENODEV;
6193 goto init_err_disable;
6194 }
6195
6196 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6197 if (rc) {
6198 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6199 goto init_err_disable;
6200 }
6201
6202 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6203 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6204 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6205 goto init_err_disable;
6206 }
6207
6208 pci_set_master(pdev);
6209
6210 bp->dev = dev;
6211 bp->pdev = pdev;
6212
6213 bp->bar0 = pci_ioremap_bar(pdev, 0);
6214 if (!bp->bar0) {
6215 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6216 rc = -ENOMEM;
6217 goto init_err_release;
6218 }
6219
6220 bp->bar1 = pci_ioremap_bar(pdev, 2);
6221 if (!bp->bar1) {
6222 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6223 rc = -ENOMEM;
6224 goto init_err_release;
6225 }
6226
6227 bp->bar2 = pci_ioremap_bar(pdev, 4);
6228 if (!bp->bar2) {
6229 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6230 rc = -ENOMEM;
6231 goto init_err_release;
6232 }
6233
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006234 pci_enable_pcie_error_reporting(pdev);
6235
Michael Chanc0c050c2015-10-22 16:01:17 -04006236 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6237
6238 spin_lock_init(&bp->ntp_fltr_lock);
6239
6240 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6241 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6242
Michael Chandfb5b892016-02-26 04:00:01 -05006243 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006244 bp->rx_coal_ticks = 12;
6245 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006246 bp->rx_coal_ticks_irq = 1;
6247 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006248
Michael Chandfc9c942016-02-26 04:00:03 -05006249 bp->tx_coal_ticks = 25;
6250 bp->tx_coal_bufs = 30;
6251 bp->tx_coal_ticks_irq = 2;
6252 bp->tx_coal_bufs_irq = 2;
6253
Michael Chan51f30782016-07-01 18:46:29 -04006254 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6255
Michael Chanc0c050c2015-10-22 16:01:17 -04006256 init_timer(&bp->timer);
6257 bp->timer.data = (unsigned long)bp;
6258 bp->timer.function = bnxt_timer;
6259 bp->current_interval = BNXT_TIMER_INTERVAL;
6260
Michael Chancaefe522015-12-09 19:35:42 -05006261 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006262
6263 return 0;
6264
6265init_err_release:
6266 if (bp->bar2) {
6267 pci_iounmap(pdev, bp->bar2);
6268 bp->bar2 = NULL;
6269 }
6270
6271 if (bp->bar1) {
6272 pci_iounmap(pdev, bp->bar1);
6273 bp->bar1 = NULL;
6274 }
6275
6276 if (bp->bar0) {
6277 pci_iounmap(pdev, bp->bar0);
6278 bp->bar0 = NULL;
6279 }
6280
6281 pci_release_regions(pdev);
6282
6283init_err_disable:
6284 pci_disable_device(pdev);
6285
6286init_err:
6287 return rc;
6288}
6289
6290/* rtnl_lock held */
6291static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6292{
6293 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006294 struct bnxt *bp = netdev_priv(dev);
6295 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006296
6297 if (!is_valid_ether_addr(addr->sa_data))
6298 return -EADDRNOTAVAIL;
6299
Michael Chan84c33dd2016-04-11 04:11:13 -04006300 rc = bnxt_approve_mac(bp, addr->sa_data);
6301 if (rc)
6302 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006303
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006304 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6305 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006306
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006307 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6308 if (netif_running(dev)) {
6309 bnxt_close_nic(bp, false, false);
6310 rc = bnxt_open_nic(bp, false, false);
6311 }
6312
6313 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006314}
6315
6316/* rtnl_lock held */
6317static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6318{
6319 struct bnxt *bp = netdev_priv(dev);
6320
Michael Chanc0c050c2015-10-22 16:01:17 -04006321 if (netif_running(dev))
6322 bnxt_close_nic(bp, false, false);
6323
6324 dev->mtu = new_mtu;
6325 bnxt_set_ring_params(bp);
6326
6327 if (netif_running(dev))
6328 return bnxt_open_nic(bp, false, false);
6329
6330 return 0;
6331}
6332
John Fastabend16e5cc62016-02-16 21:16:43 -08006333static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6334 struct tc_to_netdev *ntc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006335{
6336 struct bnxt *bp = netdev_priv(dev);
Michael Chan3ffb6a32016-11-11 00:11:42 -05006337 bool sh = false;
John Fastabend16e5cc62016-02-16 21:16:43 -08006338 u8 tc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006339
John Fastabend5eb4dce2016-02-29 11:26:13 -08006340 if (ntc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08006341 return -EINVAL;
6342
John Fastabend16e5cc62016-02-16 21:16:43 -08006343 tc = ntc->tc;
6344
Michael Chanc0c050c2015-10-22 16:01:17 -04006345 if (tc > bp->max_tc) {
6346 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6347 tc, bp->max_tc);
6348 return -EINVAL;
6349 }
6350
6351 if (netdev_get_num_tc(dev) == tc)
6352 return 0;
6353
Michael Chan3ffb6a32016-11-11 00:11:42 -05006354 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6355 sh = true;
6356
Michael Chanc0c050c2015-10-22 16:01:17 -04006357 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006358 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006359
6360 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006361 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006362 return -ENOMEM;
6363 }
6364
6365 /* Needs to close the device and do hw resource re-allocations */
6366 if (netif_running(bp->dev))
6367 bnxt_close_nic(bp, true, false);
6368
6369 if (tc) {
6370 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6371 netdev_set_num_tc(dev, tc);
6372 } else {
6373 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6374 netdev_reset_tc(dev);
6375 }
Michael Chan3ffb6a32016-11-11 00:11:42 -05006376 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6377 bp->tx_nr_rings + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04006378 bp->num_stat_ctxs = bp->cp_nr_rings;
6379
6380 if (netif_running(bp->dev))
6381 return bnxt_open_nic(bp, true, false);
6382
6383 return 0;
6384}
6385
6386#ifdef CONFIG_RFS_ACCEL
6387static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6388 struct bnxt_ntuple_filter *f2)
6389{
6390 struct flow_keys *keys1 = &f1->fkeys;
6391 struct flow_keys *keys2 = &f2->fkeys;
6392
6393 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6394 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6395 keys1->ports.ports == keys2->ports.ports &&
6396 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6397 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006398 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6399 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006400 return true;
6401
6402 return false;
6403}
6404
6405static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6406 u16 rxq_index, u32 flow_id)
6407{
6408 struct bnxt *bp = netdev_priv(dev);
6409 struct bnxt_ntuple_filter *fltr, *new_fltr;
6410 struct flow_keys *fkeys;
6411 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006412 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006413 struct hlist_head *head;
6414
6415 if (skb->encapsulation)
6416 return -EPROTONOSUPPORT;
6417
Michael Chana54c4d72016-07-25 12:33:35 -04006418 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6419 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6420 int off = 0, j;
6421
6422 netif_addr_lock_bh(dev);
6423 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6424 if (ether_addr_equal(eth->h_dest,
6425 vnic->uc_list + off)) {
6426 l2_idx = j + 1;
6427 break;
6428 }
6429 }
6430 netif_addr_unlock_bh(dev);
6431 if (!l2_idx)
6432 return -EINVAL;
6433 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006434 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6435 if (!new_fltr)
6436 return -ENOMEM;
6437
6438 fkeys = &new_fltr->fkeys;
6439 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6440 rc = -EPROTONOSUPPORT;
6441 goto err_free;
6442 }
6443
6444 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6445 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6446 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6447 rc = -EPROTONOSUPPORT;
6448 goto err_free;
6449 }
6450
Michael Chana54c4d72016-07-25 12:33:35 -04006451 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006452 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6453
6454 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6455 head = &bp->ntp_fltr_hash_tbl[idx];
6456 rcu_read_lock();
6457 hlist_for_each_entry_rcu(fltr, head, hash) {
6458 if (bnxt_fltr_match(fltr, new_fltr)) {
6459 rcu_read_unlock();
6460 rc = 0;
6461 goto err_free;
6462 }
6463 }
6464 rcu_read_unlock();
6465
6466 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006467 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6468 BNXT_NTP_FLTR_MAX_FLTR, 0);
6469 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006470 spin_unlock_bh(&bp->ntp_fltr_lock);
6471 rc = -ENOMEM;
6472 goto err_free;
6473 }
6474
Michael Chan84e86b92015-11-05 16:25:50 -05006475 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006476 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006477 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006478 new_fltr->rxq = rxq_index;
6479 hlist_add_head_rcu(&new_fltr->hash, head);
6480 bp->ntp_fltr_count++;
6481 spin_unlock_bh(&bp->ntp_fltr_lock);
6482
6483 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6484 schedule_work(&bp->sp_task);
6485
6486 return new_fltr->sw_id;
6487
6488err_free:
6489 kfree(new_fltr);
6490 return rc;
6491}
6492
6493static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6494{
6495 int i;
6496
6497 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6498 struct hlist_head *head;
6499 struct hlist_node *tmp;
6500 struct bnxt_ntuple_filter *fltr;
6501 int rc;
6502
6503 head = &bp->ntp_fltr_hash_tbl[i];
6504 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6505 bool del = false;
6506
6507 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6508 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6509 fltr->flow_id,
6510 fltr->sw_id)) {
6511 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6512 fltr);
6513 del = true;
6514 }
6515 } else {
6516 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6517 fltr);
6518 if (rc)
6519 del = true;
6520 else
6521 set_bit(BNXT_FLTR_VALID, &fltr->state);
6522 }
6523
6524 if (del) {
6525 spin_lock_bh(&bp->ntp_fltr_lock);
6526 hlist_del_rcu(&fltr->hash);
6527 bp->ntp_fltr_count--;
6528 spin_unlock_bh(&bp->ntp_fltr_lock);
6529 synchronize_rcu();
6530 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6531 kfree(fltr);
6532 }
6533 }
6534 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006535 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6536 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006537}
6538
6539#else
6540
6541static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6542{
6543}
6544
6545#endif /* CONFIG_RFS_ACCEL */
6546
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006547static void bnxt_udp_tunnel_add(struct net_device *dev,
6548 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006549{
6550 struct bnxt *bp = netdev_priv(dev);
6551
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006552 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6553 return;
6554
Michael Chanc0c050c2015-10-22 16:01:17 -04006555 if (!netif_running(dev))
6556 return;
6557
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006558 switch (ti->type) {
6559 case UDP_TUNNEL_TYPE_VXLAN:
6560 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6561 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006562
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006563 bp->vxlan_port_cnt++;
6564 if (bp->vxlan_port_cnt == 1) {
6565 bp->vxlan_port = ti->port;
6566 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006567 schedule_work(&bp->sp_task);
6568 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006569 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006570 case UDP_TUNNEL_TYPE_GENEVE:
6571 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6572 return;
6573
6574 bp->nge_port_cnt++;
6575 if (bp->nge_port_cnt == 1) {
6576 bp->nge_port = ti->port;
6577 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6578 }
6579 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006580 default:
6581 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006582 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006583
6584 schedule_work(&bp->sp_task);
6585}
6586
6587static void bnxt_udp_tunnel_del(struct net_device *dev,
6588 struct udp_tunnel_info *ti)
6589{
6590 struct bnxt *bp = netdev_priv(dev);
6591
6592 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6593 return;
6594
6595 if (!netif_running(dev))
6596 return;
6597
6598 switch (ti->type) {
6599 case UDP_TUNNEL_TYPE_VXLAN:
6600 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6601 return;
6602 bp->vxlan_port_cnt--;
6603
6604 if (bp->vxlan_port_cnt != 0)
6605 return;
6606
6607 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6608 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006609 case UDP_TUNNEL_TYPE_GENEVE:
6610 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6611 return;
6612 bp->nge_port_cnt--;
6613
6614 if (bp->nge_port_cnt != 0)
6615 return;
6616
6617 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6618 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006619 default:
6620 return;
6621 }
6622
6623 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006624}
6625
6626static const struct net_device_ops bnxt_netdev_ops = {
6627 .ndo_open = bnxt_open,
6628 .ndo_start_xmit = bnxt_start_xmit,
6629 .ndo_stop = bnxt_close,
6630 .ndo_get_stats64 = bnxt_get_stats64,
6631 .ndo_set_rx_mode = bnxt_set_rx_mode,
6632 .ndo_do_ioctl = bnxt_ioctl,
6633 .ndo_validate_addr = eth_validate_addr,
6634 .ndo_set_mac_address = bnxt_change_mac_addr,
6635 .ndo_change_mtu = bnxt_change_mtu,
6636 .ndo_fix_features = bnxt_fix_features,
6637 .ndo_set_features = bnxt_set_features,
6638 .ndo_tx_timeout = bnxt_tx_timeout,
6639#ifdef CONFIG_BNXT_SRIOV
6640 .ndo_get_vf_config = bnxt_get_vf_config,
6641 .ndo_set_vf_mac = bnxt_set_vf_mac,
6642 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6643 .ndo_set_vf_rate = bnxt_set_vf_bw,
6644 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6645 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6646#endif
6647#ifdef CONFIG_NET_POLL_CONTROLLER
6648 .ndo_poll_controller = bnxt_poll_controller,
6649#endif
6650 .ndo_setup_tc = bnxt_setup_tc,
6651#ifdef CONFIG_RFS_ACCEL
6652 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6653#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006654 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6655 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006656#ifdef CONFIG_NET_RX_BUSY_POLL
6657 .ndo_busy_poll = bnxt_busy_poll,
6658#endif
6659};
6660
6661static void bnxt_remove_one(struct pci_dev *pdev)
6662{
6663 struct net_device *dev = pci_get_drvdata(pdev);
6664 struct bnxt *bp = netdev_priv(dev);
6665
6666 if (BNXT_PF(bp))
6667 bnxt_sriov_disable(bp);
6668
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006669 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006670 unregister_netdev(dev);
6671 cancel_work_sync(&bp->sp_task);
6672 bp->sp_event = 0;
6673
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006674 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006675 bnxt_free_hwrm_resources(bp);
6676 pci_iounmap(pdev, bp->bar2);
6677 pci_iounmap(pdev, bp->bar1);
6678 pci_iounmap(pdev, bp->bar0);
6679 free_netdev(dev);
6680
6681 pci_release_regions(pdev);
6682 pci_disable_device(pdev);
6683}
6684
6685static int bnxt_probe_phy(struct bnxt *bp)
6686{
6687 int rc = 0;
6688 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006689
Michael Chan170ce012016-04-05 14:08:57 -04006690 rc = bnxt_hwrm_phy_qcaps(bp);
6691 if (rc) {
6692 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6693 rc);
6694 return rc;
6695 }
6696
Michael Chanc0c050c2015-10-22 16:01:17 -04006697 rc = bnxt_update_link(bp, false);
6698 if (rc) {
6699 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6700 rc);
6701 return rc;
6702 }
6703
Michael Chan93ed8112016-06-13 02:25:37 -04006704 /* Older firmware does not have supported_auto_speeds, so assume
6705 * that all supported speeds can be autonegotiated.
6706 */
6707 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6708 link_info->support_auto_speeds = link_info->support_speeds;
6709
Michael Chanc0c050c2015-10-22 16:01:17 -04006710 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006711 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006712 link_info->autoneg = BNXT_AUTONEG_SPEED;
6713 if (bp->hwrm_spec_code >= 0x10201) {
6714 if (link_info->auto_pause_setting &
6715 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6716 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6717 } else {
6718 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6719 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006720 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006721 } else {
6722 link_info->req_link_speed = link_info->force_link_speed;
6723 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006724 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006725 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6726 link_info->req_flow_ctrl =
6727 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6728 else
6729 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006730 return rc;
6731}
6732
6733static int bnxt_get_max_irq(struct pci_dev *pdev)
6734{
6735 u16 ctrl;
6736
6737 if (!pdev->msix_cap)
6738 return 1;
6739
6740 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6741 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6742}
6743
Michael Chan6e6c5a52016-01-02 23:45:02 -05006744static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6745 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006746{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006747 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006748
Michael Chan379a80a2015-10-23 15:06:19 -04006749#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006750 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006751 *max_tx = bp->vf.max_tx_rings;
6752 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006753 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6754 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006755 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006756 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006757#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006758 {
6759 *max_tx = bp->pf.max_tx_rings;
6760 *max_rx = bp->pf.max_rx_rings;
6761 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6762 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6763 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006764 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006765 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6766 *max_cp -= 1;
6767 *max_rx -= 2;
6768 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006769 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6770 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006771 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006772}
6773
6774int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6775{
6776 int rx, tx, cp;
6777
6778 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6779 if (!rx || !tx || !cp)
6780 return -ENOMEM;
6781
6782 *max_rx = rx;
6783 *max_tx = tx;
6784 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6785}
6786
6787static int bnxt_set_dflt_rings(struct bnxt *bp)
6788{
6789 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6790 bool sh = true;
6791
6792 if (sh)
6793 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6794 dflt_rings = netif_get_num_default_rss_queues();
6795 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6796 if (rc)
6797 return rc;
6798 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6799 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6800 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6801 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6802 bp->tx_nr_rings + bp->rx_nr_rings;
6803 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006804 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6805 bp->rx_nr_rings++;
6806 bp->cp_nr_rings++;
6807 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006808 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006809}
6810
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006811static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6812{
6813 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6814 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6815
6816 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6817 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6818 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6819 else
6820 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6821 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6822 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6823 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6824 "Unknown", width);
6825}
6826
Michael Chanc0c050c2015-10-22 16:01:17 -04006827static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6828{
6829 static int version_printed;
6830 struct net_device *dev;
6831 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006832 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006833
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04006834 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6835 return -ENODEV;
6836
Michael Chanc0c050c2015-10-22 16:01:17 -04006837 if (version_printed++ == 0)
6838 pr_info("%s", version);
6839
6840 max_irqs = bnxt_get_max_irq(pdev);
6841 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6842 if (!dev)
6843 return -ENOMEM;
6844
6845 bp = netdev_priv(dev);
6846
6847 if (bnxt_vf_pciid(ent->driver_data))
6848 bp->flags |= BNXT_FLAG_VF;
6849
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006850 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006851 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006852
6853 rc = bnxt_init_board(pdev, dev);
6854 if (rc < 0)
6855 goto init_err_free;
6856
6857 dev->netdev_ops = &bnxt_netdev_ops;
6858 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6859 dev->ethtool_ops = &bnxt_ethtool_ops;
6860
6861 pci_set_drvdata(pdev, dev);
6862
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006863 rc = bnxt_alloc_hwrm_resources(bp);
6864 if (rc)
6865 goto init_err;
6866
6867 mutex_init(&bp->hwrm_cmd_lock);
6868 rc = bnxt_hwrm_ver_get(bp);
6869 if (rc)
6870 goto init_err;
6871
Rob Swindell5ac67d82016-09-19 03:58:03 -04006872 bnxt_hwrm_fw_set_time(bp);
6873
Michael Chanc0c050c2015-10-22 16:01:17 -04006874 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6875 NETIF_F_TSO | NETIF_F_TSO6 |
6876 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07006877 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07006878 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6879 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006880 NETIF_F_RXCSUM | NETIF_F_GRO;
6881
6882 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6883 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04006884
Michael Chanc0c050c2015-10-22 16:01:17 -04006885 dev->hw_enc_features =
6886 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6887 NETIF_F_TSO | NETIF_F_TSO6 |
6888 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07006889 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07006890 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07006891 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
6892 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04006893 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
6894 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6895 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
6896 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
6897 dev->priv_flags |= IFF_UNICAST_FLT;
6898
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -04006899 /* MTU range: 60 - 9500 */
6900 dev->min_mtu = ETH_ZLEN;
6901 dev->max_mtu = 9500;
6902
Michael Chanc0c050c2015-10-22 16:01:17 -04006903#ifdef CONFIG_BNXT_SRIOV
6904 init_waitqueue_head(&bp->sriov_cfg_wait);
6905#endif
Michael Chan309369c2016-06-13 02:25:34 -04006906 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04006907 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
6908 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04006909
Michael Chanc0c050c2015-10-22 16:01:17 -04006910 rc = bnxt_hwrm_func_drv_rgtr(bp);
6911 if (rc)
6912 goto init_err;
6913
6914 /* Get the MAX capabilities for this function */
6915 rc = bnxt_hwrm_func_qcaps(bp);
6916 if (rc) {
6917 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
6918 rc);
6919 rc = -1;
6920 goto init_err;
6921 }
6922
6923 rc = bnxt_hwrm_queue_qportcfg(bp);
6924 if (rc) {
6925 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
6926 rc);
6927 rc = -1;
6928 goto init_err;
6929 }
6930
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006931 bnxt_hwrm_func_qcfg(bp);
6932
Michael Chanc0c050c2015-10-22 16:01:17 -04006933 bnxt_set_tpa_flags(bp);
6934 bnxt_set_ring_params(bp);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006935 if (BNXT_PF(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006936 bp->pf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006937#if defined(CONFIG_BNXT_SRIOV)
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006938 else
Michael Chanc0c050c2015-10-22 16:01:17 -04006939 bp->vf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006940#endif
Michael Chan6e6c5a52016-01-02 23:45:02 -05006941 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006942
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006943 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006944 dev->hw_features |= NETIF_F_NTUPLE;
6945 if (bnxt_rfs_capable(bp)) {
6946 bp->flags |= BNXT_FLAG_RFS;
6947 dev->features |= NETIF_F_NTUPLE;
6948 }
6949 }
6950
Michael Chanc0c050c2015-10-22 16:01:17 -04006951 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
6952 bp->flags |= BNXT_FLAG_STRIP_VLAN;
6953
6954 rc = bnxt_probe_phy(bp);
6955 if (rc)
6956 goto init_err;
6957
6958 rc = register_netdev(dev);
6959 if (rc)
6960 goto init_err;
6961
6962 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
6963 board_info[ent->driver_data].name,
6964 (long)pci_resource_start(pdev, 0), dev->dev_addr);
6965
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006966 bnxt_parse_log_pcie_link(bp);
6967
Michael Chanc0c050c2015-10-22 16:01:17 -04006968 return 0;
6969
6970init_err:
6971 pci_iounmap(pdev, bp->bar0);
6972 pci_release_regions(pdev);
6973 pci_disable_device(pdev);
6974
6975init_err_free:
6976 free_netdev(dev);
6977 return rc;
6978}
6979
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006980/**
6981 * bnxt_io_error_detected - called when PCI error is detected
6982 * @pdev: Pointer to PCI device
6983 * @state: The current pci connection state
6984 *
6985 * This function is called after a PCI bus error affecting
6986 * this device has been detected.
6987 */
6988static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
6989 pci_channel_state_t state)
6990{
6991 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chan2a5bedf2016-07-01 18:46:21 -04006992 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006993
6994 netdev_info(netdev, "PCI I/O error detected\n");
6995
6996 rtnl_lock();
6997 netif_device_detach(netdev);
6998
6999 if (state == pci_channel_io_perm_failure) {
7000 rtnl_unlock();
7001 return PCI_ERS_RESULT_DISCONNECT;
7002 }
7003
7004 if (netif_running(netdev))
7005 bnxt_close(netdev);
7006
Michael Chan2a5bedf2016-07-01 18:46:21 -04007007 /* So that func_reset will be done during slot_reset */
7008 clear_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007009 pci_disable_device(pdev);
7010 rtnl_unlock();
7011
7012 /* Request a slot slot reset. */
7013 return PCI_ERS_RESULT_NEED_RESET;
7014}
7015
7016/**
7017 * bnxt_io_slot_reset - called after the pci bus has been reset.
7018 * @pdev: Pointer to PCI device
7019 *
7020 * Restart the card from scratch, as if from a cold-boot.
7021 * At this point, the card has exprienced a hard reset,
7022 * followed by fixups by BIOS, and has its config space
7023 * set up identically to what it was at cold boot.
7024 */
7025static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7026{
7027 struct net_device *netdev = pci_get_drvdata(pdev);
7028 struct bnxt *bp = netdev_priv(netdev);
7029 int err = 0;
7030 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7031
7032 netdev_info(bp->dev, "PCI Slot Reset\n");
7033
7034 rtnl_lock();
7035
7036 if (pci_enable_device(pdev)) {
7037 dev_err(&pdev->dev,
7038 "Cannot re-enable PCI device after reset.\n");
7039 } else {
7040 pci_set_master(pdev);
7041
7042 if (netif_running(netdev))
7043 err = bnxt_open(netdev);
7044
7045 if (!err)
7046 result = PCI_ERS_RESULT_RECOVERED;
7047 }
7048
7049 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7050 dev_close(netdev);
7051
7052 rtnl_unlock();
7053
7054 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7055 if (err) {
7056 dev_err(&pdev->dev,
7057 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7058 err); /* non-fatal, continue */
7059 }
7060
7061 return PCI_ERS_RESULT_RECOVERED;
7062}
7063
7064/**
7065 * bnxt_io_resume - called when traffic can start flowing again.
7066 * @pdev: Pointer to PCI device
7067 *
7068 * This callback is called when the error recovery driver tells
7069 * us that its OK to resume normal operation.
7070 */
7071static void bnxt_io_resume(struct pci_dev *pdev)
7072{
7073 struct net_device *netdev = pci_get_drvdata(pdev);
7074
7075 rtnl_lock();
7076
7077 netif_device_attach(netdev);
7078
7079 rtnl_unlock();
7080}
7081
7082static const struct pci_error_handlers bnxt_err_handler = {
7083 .error_detected = bnxt_io_error_detected,
7084 .slot_reset = bnxt_io_slot_reset,
7085 .resume = bnxt_io_resume
7086};
7087
Michael Chanc0c050c2015-10-22 16:01:17 -04007088static struct pci_driver bnxt_pci_driver = {
7089 .name = DRV_MODULE_NAME,
7090 .id_table = bnxt_pci_tbl,
7091 .probe = bnxt_init_one,
7092 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007093 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007094#if defined(CONFIG_BNXT_SRIOV)
7095 .sriov_configure = bnxt_sriov_configure,
7096#endif
7097};
7098
7099module_pci_driver(bnxt_pci_driver);