blob: f6b4f342d7b474fb4981d9f4ca131ee5ec1fde7d [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>
35#include <net/ip.h>
36#include <net/tcp.h>
37#include <net/udp.h>
38#include <net/checksum.h>
39#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070040#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040041#ifdef CONFIG_NET_RX_BUSY_POLL
42#include <net/busy_poll.h>
43#endif
44#include <linux/workqueue.h>
45#include <linux/prefetch.h>
46#include <linux/cache.h>
47#include <linux/log2.h>
48#include <linux/aer.h>
49#include <linux/bitmap.h>
50#include <linux/cpu_rmap.h>
51
52#include "bnxt_hsi.h"
53#include "bnxt.h"
54#include "bnxt_sriov.h"
55#include "bnxt_ethtool.h"
56
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chan1f681682016-07-25 12:33:37 -040076 BCM57417_NPAR,
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -040077 BCM58700,
Michael Chanb24eb6a2016-06-13 02:25:36 -040078 BCM57311,
79 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050080 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040081 BCM57404,
82 BCM57406,
Michael Chan1f681682016-07-25 12:33:37 -040083 BCM57402_NPAR,
84 BCM57407,
Michael Chanb24eb6a2016-06-13 02:25:36 -040085 BCM57412,
86 BCM57414,
87 BCM57416,
88 BCM57417,
Michael Chan1f681682016-07-25 12:33:37 -040089 BCM57412_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040090 BCM57314,
Michael Chan1f681682016-07-25 12:33:37 -040091 BCM57417_SFP,
92 BCM57416_SFP,
93 BCM57404_NPAR,
94 BCM57406_NPAR,
95 BCM57407_SFP,
Michael Chanadbc8302016-09-19 03:58:01 -040096 BCM57407_NPAR,
Michael Chan1f681682016-07-25 12:33:37 -040097 BCM57414_NPAR,
98 BCM57416_NPAR,
Michael Chanadbc8302016-09-19 03:58:01 -040099 NETXTREME_E_VF,
100 NETXTREME_C_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -0400101};
102
103/* indexed by enum above */
104static const struct {
105 char *name;
106} board_info[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400107 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
108 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400110 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400111 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
112 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
115 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
116 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400117 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400118 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
119 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400123 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400124 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
125 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
Michael Chan1f681682016-07-25 12:33:37 -0400127 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
128 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400129 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
130 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
Michael Chan1f681682016-07-25 12:33:37 -0400131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
Michael Chanadbc8302016-09-19 03:58:01 -0400133 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135};
136
137static const struct pci_device_id bnxt_pci_tbl[] = {
Michael Chanadbc8302016-09-19 03:58:01 -0400138 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
David Christensenfbc9a522015-12-27 18:19:29 -0500139 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
141 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chan1f681682016-07-25 12:33:37 -0400142 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -0400143 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400144 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
145 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500146 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400147 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
148 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chan1f681682016-07-25 12:33:37 -0400149 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400151 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
152 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
153 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
154 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
Michael Chan1f681682016-07-25 12:33:37 -0400155 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400156 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chan1f681682016-07-25 12:33:37 -0400157 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
158 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
160 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
Michael Chanadbc8302016-09-19 03:58:01 -0400162 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400164 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400165 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
Michael Chan1f681682016-07-25 12:33:37 -0400166 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
Michael Chanadbc8302016-09-19 03:58:01 -0400167 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
Michael Chanc0c050c2015-10-22 16:01:17 -0400168#ifdef CONFIG_BNXT_SRIOV
Michael Chanadbc8302016-09-19 03:58:01 -0400169 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
170 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
171 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
172 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
173 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
174 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400175#endif
176 { 0 }
177};
178
179MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
180
181static const u16 bnxt_vf_req_snif[] = {
182 HWRM_FUNC_CFG,
183 HWRM_PORT_PHY_QCFG,
184 HWRM_CFA_L2_FILTER_ALLOC,
185};
186
Michael Chan25be8622016-04-05 14:09:00 -0400187static const u16 bnxt_async_events_arr[] = {
188 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
189 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
Michael Chan4bb13ab2016-04-05 14:09:01 -0400190 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
Michael Chanfc0f1922016-06-13 02:25:30 -0400191 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
Michael Chan8cbde112016-04-11 04:11:14 -0400192 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400193};
194
Michael Chanc0c050c2015-10-22 16:01:17 -0400195static bool bnxt_vf_pciid(enum board_idx idx)
196{
Michael Chanadbc8302016-09-19 03:58:01 -0400197 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400198}
199
200#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
201#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
202#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
203
204#define BNXT_CP_DB_REARM(db, raw_cons) \
205 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
206
207#define BNXT_CP_DB(db, raw_cons) \
208 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
209
210#define BNXT_CP_DB_IRQ_DIS(db) \
211 writel(DB_CP_IRQ_DIS_FLAGS, db)
212
213static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
214{
215 /* Tell compiler to fetch tx indices from memory. */
216 barrier();
217
218 return bp->tx_ring_size -
219 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
220}
221
222static const u16 bnxt_lhint_arr[] = {
223 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
224 TX_BD_FLAGS_LHINT_512_TO_1023,
225 TX_BD_FLAGS_LHINT_1024_TO_2047,
226 TX_BD_FLAGS_LHINT_1024_TO_2047,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242};
243
244static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 struct bnxt *bp = netdev_priv(dev);
247 struct tx_bd *txbd;
248 struct tx_bd_ext *txbd1;
249 struct netdev_queue *txq;
250 int i;
251 dma_addr_t mapping;
252 unsigned int length, pad = 0;
253 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
254 u16 prod, last_frag;
255 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400256 struct bnxt_tx_ring_info *txr;
257 struct bnxt_sw_tx_bd *tx_buf;
258
259 i = skb_get_queue_mapping(skb);
260 if (unlikely(i >= bp->tx_nr_rings)) {
261 dev_kfree_skb_any(skb);
262 return NETDEV_TX_OK;
263 }
264
Michael Chanb6ab4b02016-01-02 23:44:59 -0500265 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400266 txq = netdev_get_tx_queue(dev, i);
267 prod = txr->tx_prod;
268
269 free_size = bnxt_tx_avail(bp, txr);
270 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
271 netif_tx_stop_queue(txq);
272 return NETDEV_TX_BUSY;
273 }
274
275 length = skb->len;
276 len = skb_headlen(skb);
277 last_frag = skb_shinfo(skb)->nr_frags;
278
279 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
280
281 txbd->tx_bd_opaque = prod;
282
283 tx_buf = &txr->tx_buf_ring[prod];
284 tx_buf->skb = skb;
285 tx_buf->nr_frags = last_frag;
286
287 vlan_tag_flags = 0;
288 cfa_action = 0;
289 if (skb_vlan_tag_present(skb)) {
290 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
291 skb_vlan_tag_get(skb);
292 /* Currently supports 8021Q, 8021AD vlan offloads
293 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
294 */
295 if (skb->vlan_proto == htons(ETH_P_8021Q))
296 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
297 }
298
299 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500300 struct tx_push_buffer *tx_push_buf = txr->tx_push;
301 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
302 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
303 void *pdata = tx_push_buf->data;
304 u64 *end;
305 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400306
307 /* Set COAL_NOW to be ready quickly for the next push */
308 tx_push->tx_bd_len_flags_type =
309 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
310 TX_BD_TYPE_LONG_TX_BD |
311 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
312 TX_BD_FLAGS_COAL_NOW |
313 TX_BD_FLAGS_PACKET_END |
314 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
315
316 if (skb->ip_summed == CHECKSUM_PARTIAL)
317 tx_push1->tx_bd_hsize_lflags =
318 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
319 else
320 tx_push1->tx_bd_hsize_lflags = 0;
321
322 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
323 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
324
Michael Chanfbb0fa82016-02-22 02:10:26 -0500325 end = pdata + length;
326 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500327 *end = 0;
328
Michael Chanc0c050c2015-10-22 16:01:17 -0400329 skb_copy_from_linear_data(skb, pdata, len);
330 pdata += len;
331 for (j = 0; j < last_frag; j++) {
332 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
333 void *fptr;
334
335 fptr = skb_frag_address_safe(frag);
336 if (!fptr)
337 goto normal_tx;
338
339 memcpy(pdata, fptr, skb_frag_size(frag));
340 pdata += skb_frag_size(frag);
341 }
342
Michael Chan4419dbe2016-02-10 17:33:49 -0500343 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
344 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400345 prod = NEXT_TX(prod);
346 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
347 memcpy(txbd, tx_push1, sizeof(*txbd));
348 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500349 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400350 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
351 txr->tx_prod = prod;
352
Michael Chanb9a84602016-06-06 02:37:14 -0400353 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400354 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400355 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400356
Michael Chan4419dbe2016-02-10 17:33:49 -0500357 push_len = (length + sizeof(*tx_push) + 7) / 8;
358 if (push_len > 16) {
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
Michael Chan9d137442016-09-05 01:57:35 -0400360 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
361 (push_len - 16) << 1);
Michael Chan4419dbe2016-02-10 17:33:49 -0500362 } else {
363 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
364 push_len);
365 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400366
Michael Chanc0c050c2015-10-22 16:01:17 -0400367 goto tx_done;
368 }
369
370normal_tx:
371 if (length < BNXT_MIN_PKT_SIZE) {
372 pad = BNXT_MIN_PKT_SIZE - length;
373 if (skb_pad(skb, pad)) {
374 /* SKB already freed. */
375 tx_buf->skb = NULL;
376 return NETDEV_TX_OK;
377 }
378 length = BNXT_MIN_PKT_SIZE;
379 }
380
381 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
382
383 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
384 dev_kfree_skb_any(skb);
385 tx_buf->skb = NULL;
386 return NETDEV_TX_OK;
387 }
388
389 dma_unmap_addr_set(tx_buf, mapping, mapping);
390 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
391 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
392
393 txbd->tx_bd_haddr = cpu_to_le64(mapping);
394
395 prod = NEXT_TX(prod);
396 txbd1 = (struct tx_bd_ext *)
397 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
398
399 txbd1->tx_bd_hsize_lflags = 0;
400 if (skb_is_gso(skb)) {
401 u32 hdr_len;
402
403 if (skb->encapsulation)
404 hdr_len = skb_inner_network_offset(skb) +
405 skb_inner_network_header_len(skb) +
406 inner_tcp_hdrlen(skb);
407 else
408 hdr_len = skb_transport_offset(skb) +
409 tcp_hdrlen(skb);
410
411 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
412 TX_BD_FLAGS_T_IPID |
413 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
414 length = skb_shinfo(skb)->gso_size;
415 txbd1->tx_bd_mss = cpu_to_le32(length);
416 length += hdr_len;
417 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
418 txbd1->tx_bd_hsize_lflags =
419 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 txbd1->tx_bd_mss = 0;
421 }
422
423 length >>= 9;
424 flags |= bnxt_lhint_arr[length];
425 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
426
427 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
428 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
429 for (i = 0; i < last_frag; i++) {
430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
431
432 prod = NEXT_TX(prod);
433 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434
435 len = skb_frag_size(frag);
436 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
437 DMA_TO_DEVICE);
438
439 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
440 goto tx_dma_error;
441
442 tx_buf = &txr->tx_buf_ring[prod];
443 dma_unmap_addr_set(tx_buf, mapping, mapping);
444
445 txbd->tx_bd_haddr = cpu_to_le64(mapping);
446
447 flags = len << TX_BD_LEN_SHIFT;
448 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
449 }
450
451 flags &= ~TX_BD_LEN;
452 txbd->tx_bd_len_flags_type =
453 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
454 TX_BD_FLAGS_PACKET_END);
455
456 netdev_tx_sent_queue(txq, skb->len);
457
458 /* Sync BD data before updating doorbell */
459 wmb();
460
461 prod = NEXT_TX(prod);
462 txr->tx_prod = prod;
463
464 writel(DB_KEY_TX | prod, txr->tx_doorbell);
465 writel(DB_KEY_TX | prod, txr->tx_doorbell);
466
467tx_done:
468
469 mmiowb();
470
471 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
472 netif_tx_stop_queue(txq);
473
474 /* netif_tx_stop_queue() must be done before checking
475 * tx index in bnxt_tx_avail() below, because in
476 * bnxt_tx_int(), we update tx index before checking for
477 * netif_tx_queue_stopped().
478 */
479 smp_mb();
480 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
481 netif_tx_wake_queue(txq);
482 }
483 return NETDEV_TX_OK;
484
485tx_dma_error:
486 last_frag = i;
487
488 /* start back at beginning and unmap skb */
489 prod = txr->tx_prod;
490 tx_buf = &txr->tx_buf_ring[prod];
491 tx_buf->skb = NULL;
492 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
493 skb_headlen(skb), PCI_DMA_TODEVICE);
494 prod = NEXT_TX(prod);
495
496 /* unmap remaining mapped pages */
497 for (i = 0; i < last_frag; i++) {
498 prod = NEXT_TX(prod);
499 tx_buf = &txr->tx_buf_ring[prod];
500 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_frag_size(&skb_shinfo(skb)->frags[i]),
502 PCI_DMA_TODEVICE);
503 }
504
505 dev_kfree_skb_any(skb);
506 return NETDEV_TX_OK;
507}
508
509static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
510{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500511 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500512 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400513 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
514 u16 cons = txr->tx_cons;
515 struct pci_dev *pdev = bp->pdev;
516 int i;
517 unsigned int tx_bytes = 0;
518
519 for (i = 0; i < nr_pkts; i++) {
520 struct bnxt_sw_tx_bd *tx_buf;
521 struct sk_buff *skb;
522 int j, last;
523
524 tx_buf = &txr->tx_buf_ring[cons];
525 cons = NEXT_TX(cons);
526 skb = tx_buf->skb;
527 tx_buf->skb = NULL;
528
529 if (tx_buf->is_push) {
530 tx_buf->is_push = 0;
531 goto next_tx_int;
532 }
533
534 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
535 skb_headlen(skb), PCI_DMA_TODEVICE);
536 last = tx_buf->nr_frags;
537
538 for (j = 0; j < last; j++) {
539 cons = NEXT_TX(cons);
540 tx_buf = &txr->tx_buf_ring[cons];
541 dma_unmap_page(
542 &pdev->dev,
543 dma_unmap_addr(tx_buf, mapping),
544 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 PCI_DMA_TODEVICE);
546 }
547
548next_tx_int:
549 cons = NEXT_TX(cons);
550
551 tx_bytes += skb->len;
552 dev_kfree_skb_any(skb);
553 }
554
555 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
556 txr->tx_cons = cons;
557
558 /* Need to make the tx_cons update visible to bnxt_start_xmit()
559 * before checking for netif_tx_queue_stopped(). Without the
560 * memory barrier, there is a small possibility that bnxt_start_xmit()
561 * will miss it and cause the queue to be stopped forever.
562 */
563 smp_mb();
564
565 if (unlikely(netif_tx_queue_stopped(txq)) &&
566 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
567 __netif_tx_lock(txq, smp_processor_id());
568 if (netif_tx_queue_stopped(txq) &&
569 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
570 txr->dev_state != BNXT_DEV_STATE_CLOSING)
571 netif_tx_wake_queue(txq);
572 __netif_tx_unlock(txq);
573 }
574}
575
576static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
577 gfp_t gfp)
578{
579 u8 *data;
580 struct pci_dev *pdev = bp->pdev;
581
582 data = kmalloc(bp->rx_buf_size, gfp);
583 if (!data)
584 return NULL;
585
586 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
587 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
588
589 if (dma_mapping_error(&pdev->dev, *mapping)) {
590 kfree(data);
591 data = NULL;
592 }
593 return data;
594}
595
596static inline int bnxt_alloc_rx_data(struct bnxt *bp,
597 struct bnxt_rx_ring_info *rxr,
598 u16 prod, gfp_t gfp)
599{
600 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
601 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
602 u8 *data;
603 dma_addr_t mapping;
604
605 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 if (!data)
607 return -ENOMEM;
608
609 rx_buf->data = data;
610 dma_unmap_addr_set(rx_buf, mapping, mapping);
611
612 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
613
614 return 0;
615}
616
617static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
618 u8 *data)
619{
620 u16 prod = rxr->rx_prod;
621 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
622 struct rx_bd *cons_bd, *prod_bd;
623
624 prod_rx_buf = &rxr->rx_buf_ring[prod];
625 cons_rx_buf = &rxr->rx_buf_ring[cons];
626
627 prod_rx_buf->data = data;
628
629 dma_unmap_addr_set(prod_rx_buf, mapping,
630 dma_unmap_addr(cons_rx_buf, mapping));
631
632 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
633 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
634
635 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
636}
637
638static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
639{
640 u16 next, max = rxr->rx_agg_bmap_size;
641
642 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
643 if (next >= max)
644 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
645 return next;
646}
647
648static inline int bnxt_alloc_rx_page(struct bnxt *bp,
649 struct bnxt_rx_ring_info *rxr,
650 u16 prod, gfp_t gfp)
651{
652 struct rx_bd *rxbd =
653 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
654 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
655 struct pci_dev *pdev = bp->pdev;
656 struct page *page;
657 dma_addr_t mapping;
658 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400659 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400660
Michael Chan89d0a062016-04-25 02:30:51 -0400661 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
662 page = rxr->rx_page;
663 if (!page) {
664 page = alloc_page(gfp);
665 if (!page)
666 return -ENOMEM;
667 rxr->rx_page = page;
668 rxr->rx_page_offset = 0;
669 }
670 offset = rxr->rx_page_offset;
671 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
672 if (rxr->rx_page_offset == PAGE_SIZE)
673 rxr->rx_page = NULL;
674 else
675 get_page(page);
676 } else {
677 page = alloc_page(gfp);
678 if (!page)
679 return -ENOMEM;
680 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400681
Michael Chan89d0a062016-04-25 02:30:51 -0400682 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400683 PCI_DMA_FROMDEVICE);
684 if (dma_mapping_error(&pdev->dev, mapping)) {
685 __free_page(page);
686 return -EIO;
687 }
688
689 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
690 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
691
692 __set_bit(sw_prod, rxr->rx_agg_bmap);
693 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
694 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
695
696 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400697 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400698 rx_agg_buf->mapping = mapping;
699 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
700 rxbd->rx_bd_opaque = sw_prod;
701 return 0;
702}
703
704static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
705 u32 agg_bufs)
706{
707 struct bnxt *bp = bnapi->bp;
708 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500709 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400710 u16 prod = rxr->rx_agg_prod;
711 u16 sw_prod = rxr->rx_sw_agg_prod;
712 u32 i;
713
714 for (i = 0; i < agg_bufs; i++) {
715 u16 cons;
716 struct rx_agg_cmp *agg;
717 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
718 struct rx_bd *prod_bd;
719 struct page *page;
720
721 agg = (struct rx_agg_cmp *)
722 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
723 cons = agg->rx_agg_cmp_opaque;
724 __clear_bit(cons, rxr->rx_agg_bmap);
725
726 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
727 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
728
729 __set_bit(sw_prod, rxr->rx_agg_bmap);
730 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
731 cons_rx_buf = &rxr->rx_agg_ring[cons];
732
733 /* It is possible for sw_prod to be equal to cons, so
734 * set cons_rx_buf->page to NULL first.
735 */
736 page = cons_rx_buf->page;
737 cons_rx_buf->page = NULL;
738 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400739 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400740
741 prod_rx_buf->mapping = cons_rx_buf->mapping;
742
743 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
744
745 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
746 prod_bd->rx_bd_opaque = sw_prod;
747
748 prod = NEXT_RX_AGG(prod);
749 sw_prod = NEXT_RX_AGG(sw_prod);
750 cp_cons = NEXT_CMP(cp_cons);
751 }
752 rxr->rx_agg_prod = prod;
753 rxr->rx_sw_agg_prod = sw_prod;
754}
755
756static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
757 struct bnxt_rx_ring_info *rxr, u16 cons,
758 u16 prod, u8 *data, dma_addr_t dma_addr,
759 unsigned int len)
760{
761 int err;
762 struct sk_buff *skb;
763
764 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
765 if (unlikely(err)) {
766 bnxt_reuse_rx_data(rxr, cons, data);
767 return NULL;
768 }
769
770 skb = build_skb(data, 0);
771 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
772 PCI_DMA_FROMDEVICE);
773 if (!skb) {
774 kfree(data);
775 return NULL;
776 }
777
778 skb_reserve(skb, BNXT_RX_OFFSET);
779 skb_put(skb, len);
780 return skb;
781}
782
783static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
784 struct sk_buff *skb, u16 cp_cons,
785 u32 agg_bufs)
786{
787 struct pci_dev *pdev = bp->pdev;
788 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500789 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400790 u16 prod = rxr->rx_agg_prod;
791 u32 i;
792
793 for (i = 0; i < agg_bufs; i++) {
794 u16 cons, frag_len;
795 struct rx_agg_cmp *agg;
796 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
797 struct page *page;
798 dma_addr_t mapping;
799
800 agg = (struct rx_agg_cmp *)
801 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
802 cons = agg->rx_agg_cmp_opaque;
803 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
804 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
805
806 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400807 skb_fill_page_desc(skb, i, cons_rx_buf->page,
808 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400809 __clear_bit(cons, rxr->rx_agg_bmap);
810
811 /* It is possible for bnxt_alloc_rx_page() to allocate
812 * a sw_prod index that equals the cons index, so we
813 * need to clear the cons entry now.
814 */
815 mapping = dma_unmap_addr(cons_rx_buf, mapping);
816 page = cons_rx_buf->page;
817 cons_rx_buf->page = NULL;
818
819 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
820 struct skb_shared_info *shinfo;
821 unsigned int nr_frags;
822
823 shinfo = skb_shinfo(skb);
824 nr_frags = --shinfo->nr_frags;
825 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
826
827 dev_kfree_skb(skb);
828
829 cons_rx_buf->page = page;
830
831 /* Update prod since possibly some pages have been
832 * allocated already.
833 */
834 rxr->rx_agg_prod = prod;
835 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
836 return NULL;
837 }
838
Michael Chan2839f282016-04-25 02:30:50 -0400839 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400840 PCI_DMA_FROMDEVICE);
841
842 skb->data_len += frag_len;
843 skb->len += frag_len;
844 skb->truesize += PAGE_SIZE;
845
846 prod = NEXT_RX_AGG(prod);
847 cp_cons = NEXT_CMP(cp_cons);
848 }
849 rxr->rx_agg_prod = prod;
850 return skb;
851}
852
853static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
854 u8 agg_bufs, u32 *raw_cons)
855{
856 u16 last;
857 struct rx_agg_cmp *agg;
858
859 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
860 last = RING_CMP(*raw_cons);
861 agg = (struct rx_agg_cmp *)
862 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
863 return RX_AGG_CMP_VALID(agg, *raw_cons);
864}
865
866static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
867 unsigned int len,
868 dma_addr_t mapping)
869{
870 struct bnxt *bp = bnapi->bp;
871 struct pci_dev *pdev = bp->pdev;
872 struct sk_buff *skb;
873
874 skb = napi_alloc_skb(&bnapi->napi, len);
875 if (!skb)
876 return NULL;
877
878 dma_sync_single_for_cpu(&pdev->dev, mapping,
879 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
880
881 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
882
883 dma_sync_single_for_device(&pdev->dev, mapping,
884 bp->rx_copy_thresh,
885 PCI_DMA_FROMDEVICE);
886
887 skb_put(skb, len);
888 return skb;
889}
890
Michael Chanfa7e2812016-05-10 19:18:00 -0400891static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
892 u32 *raw_cons, void *cmp)
893{
894 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
895 struct rx_cmp *rxcmp = cmp;
896 u32 tmp_raw_cons = *raw_cons;
897 u8 cmp_type, agg_bufs = 0;
898
899 cmp_type = RX_CMP_TYPE(rxcmp);
900
901 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
902 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
903 RX_CMP_AGG_BUFS) >>
904 RX_CMP_AGG_BUFS_SHIFT;
905 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
906 struct rx_tpa_end_cmp *tpa_end = cmp;
907
908 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
909 RX_TPA_END_CMP_AGG_BUFS) >>
910 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
911 }
912
913 if (agg_bufs) {
914 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
915 return -EBUSY;
916 }
917 *raw_cons = tmp_raw_cons;
918 return 0;
919}
920
921static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
922{
923 if (!rxr->bnapi->in_reset) {
924 rxr->bnapi->in_reset = true;
925 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
926 schedule_work(&bp->sp_task);
927 }
928 rxr->rx_next_cons = 0xffff;
929}
930
Michael Chanc0c050c2015-10-22 16:01:17 -0400931static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
932 struct rx_tpa_start_cmp *tpa_start,
933 struct rx_tpa_start_cmp_ext *tpa_start1)
934{
935 u8 agg_id = TPA_START_AGG_ID(tpa_start);
936 u16 cons, prod;
937 struct bnxt_tpa_info *tpa_info;
938 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
939 struct rx_bd *prod_bd;
940 dma_addr_t mapping;
941
942 cons = tpa_start->rx_tpa_start_cmp_opaque;
943 prod = rxr->rx_prod;
944 cons_rx_buf = &rxr->rx_buf_ring[cons];
945 prod_rx_buf = &rxr->rx_buf_ring[prod];
946 tpa_info = &rxr->rx_tpa[agg_id];
947
Michael Chanfa7e2812016-05-10 19:18:00 -0400948 if (unlikely(cons != rxr->rx_next_cons)) {
949 bnxt_sched_reset(bp, rxr);
950 return;
951 }
952
Michael Chanc0c050c2015-10-22 16:01:17 -0400953 prod_rx_buf->data = tpa_info->data;
954
955 mapping = tpa_info->mapping;
956 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
957
958 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
959
960 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
961
962 tpa_info->data = cons_rx_buf->data;
963 cons_rx_buf->data = NULL;
964 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
965
966 tpa_info->len =
967 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
968 RX_TPA_START_CMP_LEN_SHIFT;
969 if (likely(TPA_START_HASH_VALID(tpa_start))) {
970 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
971
972 tpa_info->hash_type = PKT_HASH_TYPE_L4;
973 tpa_info->gso_type = SKB_GSO_TCPV4;
974 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
975 if (hash_type == 3)
976 tpa_info->gso_type = SKB_GSO_TCPV6;
977 tpa_info->rss_hash =
978 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
979 } else {
980 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
981 tpa_info->gso_type = 0;
982 if (netif_msg_rx_err(bp))
983 netdev_warn(bp->dev, "TPA packet without valid hash\n");
984 }
985 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
986 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400987 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400988
989 rxr->rx_prod = NEXT_RX(prod);
990 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400991 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400992 cons_rx_buf = &rxr->rx_buf_ring[cons];
993
994 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
995 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
996 cons_rx_buf->data = NULL;
997}
998
999static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1000 u16 cp_cons, u32 agg_bufs)
1001{
1002 if (agg_bufs)
1003 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1004}
1005
Michael Chan94758f82016-06-13 02:25:35 -04001006static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1007 int payload_off, int tcp_ts,
1008 struct sk_buff *skb)
1009{
1010#ifdef CONFIG_INET
1011 struct tcphdr *th;
1012 int len, nw_off;
1013 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1014 u32 hdr_info = tpa_info->hdr_info;
1015 bool loopback = false;
1016
1017 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1018 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1019 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1020
1021 /* If the packet is an internal loopback packet, the offsets will
1022 * have an extra 4 bytes.
1023 */
1024 if (inner_mac_off == 4) {
1025 loopback = true;
1026 } else if (inner_mac_off > 4) {
1027 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1028 ETH_HLEN - 2));
1029
1030 /* We only support inner iPv4/ipv6. If we don't see the
1031 * correct protocol ID, it must be a loopback packet where
1032 * the offsets are off by 4.
1033 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001034 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001035 loopback = true;
1036 }
1037 if (loopback) {
1038 /* internal loopback packet, subtract all offsets by 4 */
1039 inner_ip_off -= 4;
1040 inner_mac_off -= 4;
1041 outer_ip_off -= 4;
1042 }
1043
1044 nw_off = inner_ip_off - ETH_HLEN;
1045 skb_set_network_header(skb, nw_off);
1046 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1047 struct ipv6hdr *iph = ipv6_hdr(skb);
1048
1049 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1050 len = skb->len - skb_transport_offset(skb);
1051 th = tcp_hdr(skb);
1052 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1053 } else {
1054 struct iphdr *iph = ip_hdr(skb);
1055
1056 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1057 len = skb->len - skb_transport_offset(skb);
1058 th = tcp_hdr(skb);
1059 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1060 }
1061
1062 if (inner_mac_off) { /* tunnel */
1063 struct udphdr *uh = NULL;
1064 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1065 ETH_HLEN - 2));
1066
1067 if (proto == htons(ETH_P_IP)) {
1068 struct iphdr *iph = (struct iphdr *)skb->data;
1069
1070 if (iph->protocol == IPPROTO_UDP)
1071 uh = (struct udphdr *)(iph + 1);
1072 } else {
1073 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1074
1075 if (iph->nexthdr == IPPROTO_UDP)
1076 uh = (struct udphdr *)(iph + 1);
1077 }
1078 if (uh) {
1079 if (uh->check)
1080 skb_shinfo(skb)->gso_type |=
1081 SKB_GSO_UDP_TUNNEL_CSUM;
1082 else
1083 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1084 }
1085 }
1086#endif
1087 return skb;
1088}
1089
Michael Chanc0c050c2015-10-22 16:01:17 -04001090#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1091#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1092
Michael Chan309369c2016-06-13 02:25:34 -04001093static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1094 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001095 struct sk_buff *skb)
1096{
Michael Chand1611c32015-10-25 22:27:57 -04001097#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001098 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001099 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001100
Michael Chan309369c2016-06-13 02:25:34 -04001101 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001102 tcp_opt_len = 12;
1103
Michael Chanc0c050c2015-10-22 16:01:17 -04001104 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1105 struct iphdr *iph;
1106
1107 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1108 ETH_HLEN;
1109 skb_set_network_header(skb, nw_off);
1110 iph = ip_hdr(skb);
1111 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1112 len = skb->len - skb_transport_offset(skb);
1113 th = tcp_hdr(skb);
1114 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1115 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1116 struct ipv6hdr *iph;
1117
1118 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1119 ETH_HLEN;
1120 skb_set_network_header(skb, nw_off);
1121 iph = ipv6_hdr(skb);
1122 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1123 len = skb->len - skb_transport_offset(skb);
1124 th = tcp_hdr(skb);
1125 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1126 } else {
1127 dev_kfree_skb_any(skb);
1128 return NULL;
1129 }
1130 tcp_gro_complete(skb);
1131
1132 if (nw_off) { /* tunnel */
1133 struct udphdr *uh = NULL;
1134
1135 if (skb->protocol == htons(ETH_P_IP)) {
1136 struct iphdr *iph = (struct iphdr *)skb->data;
1137
1138 if (iph->protocol == IPPROTO_UDP)
1139 uh = (struct udphdr *)(iph + 1);
1140 } else {
1141 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1142
1143 if (iph->nexthdr == IPPROTO_UDP)
1144 uh = (struct udphdr *)(iph + 1);
1145 }
1146 if (uh) {
1147 if (uh->check)
1148 skb_shinfo(skb)->gso_type |=
1149 SKB_GSO_UDP_TUNNEL_CSUM;
1150 else
1151 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1152 }
1153 }
1154#endif
1155 return skb;
1156}
1157
Michael Chan309369c2016-06-13 02:25:34 -04001158static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1159 struct bnxt_tpa_info *tpa_info,
1160 struct rx_tpa_end_cmp *tpa_end,
1161 struct rx_tpa_end_cmp_ext *tpa_end1,
1162 struct sk_buff *skb)
1163{
1164#ifdef CONFIG_INET
1165 int payload_off;
1166 u16 segs;
1167
1168 segs = TPA_END_TPA_SEGS(tpa_end);
1169 if (segs == 1)
1170 return skb;
1171
1172 NAPI_GRO_CB(skb)->count = segs;
1173 skb_shinfo(skb)->gso_size =
1174 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1175 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1176 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1177 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1178 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1179 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1180#endif
1181 return skb;
1182}
1183
Michael Chanc0c050c2015-10-22 16:01:17 -04001184static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1185 struct bnxt_napi *bnapi,
1186 u32 *raw_cons,
1187 struct rx_tpa_end_cmp *tpa_end,
1188 struct rx_tpa_end_cmp_ext *tpa_end1,
1189 bool *agg_event)
1190{
1191 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001192 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001193 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1194 u8 *data, agg_bufs;
1195 u16 cp_cons = RING_CMP(*raw_cons);
1196 unsigned int len;
1197 struct bnxt_tpa_info *tpa_info;
1198 dma_addr_t mapping;
1199 struct sk_buff *skb;
1200
Michael Chanfa7e2812016-05-10 19:18:00 -04001201 if (unlikely(bnapi->in_reset)) {
1202 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1203
1204 if (rc < 0)
1205 return ERR_PTR(-EBUSY);
1206 return NULL;
1207 }
1208
Michael Chanc0c050c2015-10-22 16:01:17 -04001209 tpa_info = &rxr->rx_tpa[agg_id];
1210 data = tpa_info->data;
1211 prefetch(data);
1212 len = tpa_info->len;
1213 mapping = tpa_info->mapping;
1214
1215 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1216 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1217
1218 if (agg_bufs) {
1219 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1220 return ERR_PTR(-EBUSY);
1221
1222 *agg_event = true;
1223 cp_cons = NEXT_CMP(cp_cons);
1224 }
1225
1226 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1227 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1228 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1229 agg_bufs, (int)MAX_SKB_FRAGS);
1230 return NULL;
1231 }
1232
1233 if (len <= bp->rx_copy_thresh) {
1234 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1235 if (!skb) {
1236 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1237 return NULL;
1238 }
1239 } else {
1240 u8 *new_data;
1241 dma_addr_t new_mapping;
1242
1243 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1244 if (!new_data) {
1245 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1246 return NULL;
1247 }
1248
1249 tpa_info->data = new_data;
1250 tpa_info->mapping = new_mapping;
1251
1252 skb = build_skb(data, 0);
1253 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1254 PCI_DMA_FROMDEVICE);
1255
1256 if (!skb) {
1257 kfree(data);
1258 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1259 return NULL;
1260 }
1261 skb_reserve(skb, BNXT_RX_OFFSET);
1262 skb_put(skb, len);
1263 }
1264
1265 if (agg_bufs) {
1266 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1267 if (!skb) {
1268 /* Page reuse already handled by bnxt_rx_pages(). */
1269 return NULL;
1270 }
1271 }
1272 skb->protocol = eth_type_trans(skb, bp->dev);
1273
1274 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1275 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1276
Michael Chan8852ddb2016-06-06 02:37:16 -04001277 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1278 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001279 u16 vlan_proto = tpa_info->metadata >>
1280 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001281 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001282
Michael Chan8852ddb2016-06-06 02:37:16 -04001283 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001284 }
1285
1286 skb_checksum_none_assert(skb);
1287 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1288 skb->ip_summed = CHECKSUM_UNNECESSARY;
1289 skb->csum_level =
1290 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1291 }
1292
1293 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001294 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001295
1296 return skb;
1297}
1298
1299/* returns the following:
1300 * 1 - 1 packet successfully received
1301 * 0 - successful TPA_START, packet not completed yet
1302 * -EBUSY - completion ring does not have all the agg buffers yet
1303 * -ENOMEM - packet aborted due to out of memory
1304 * -EIO - packet aborted due to hw error indicated in BD
1305 */
1306static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1307 bool *agg_event)
1308{
1309 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001310 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001311 struct net_device *dev = bp->dev;
1312 struct rx_cmp *rxcmp;
1313 struct rx_cmp_ext *rxcmp1;
1314 u32 tmp_raw_cons = *raw_cons;
1315 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1316 struct bnxt_sw_rx_bd *rx_buf;
1317 unsigned int len;
1318 u8 *data, agg_bufs, cmp_type;
1319 dma_addr_t dma_addr;
1320 struct sk_buff *skb;
1321 int rc = 0;
1322
1323 rxcmp = (struct rx_cmp *)
1324 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1325
1326 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1327 cp_cons = RING_CMP(tmp_raw_cons);
1328 rxcmp1 = (struct rx_cmp_ext *)
1329 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1330
1331 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1332 return -EBUSY;
1333
1334 cmp_type = RX_CMP_TYPE(rxcmp);
1335
1336 prod = rxr->rx_prod;
1337
1338 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1339 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1340 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1341
1342 goto next_rx_no_prod;
1343
1344 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1345 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1346 (struct rx_tpa_end_cmp *)rxcmp,
1347 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1348 agg_event);
1349
1350 if (unlikely(IS_ERR(skb)))
1351 return -EBUSY;
1352
1353 rc = -ENOMEM;
1354 if (likely(skb)) {
1355 skb_record_rx_queue(skb, bnapi->index);
1356 skb_mark_napi_id(skb, &bnapi->napi);
1357 if (bnxt_busy_polling(bnapi))
1358 netif_receive_skb(skb);
1359 else
1360 napi_gro_receive(&bnapi->napi, skb);
1361 rc = 1;
1362 }
1363 goto next_rx_no_prod;
1364 }
1365
1366 cons = rxcmp->rx_cmp_opaque;
1367 rx_buf = &rxr->rx_buf_ring[cons];
1368 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001369 if (unlikely(cons != rxr->rx_next_cons)) {
1370 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1371
1372 bnxt_sched_reset(bp, rxr);
1373 return rc1;
1374 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001375 prefetch(data);
1376
1377 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1378 RX_CMP_AGG_BUFS_SHIFT;
1379
1380 if (agg_bufs) {
1381 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1382 return -EBUSY;
1383
1384 cp_cons = NEXT_CMP(cp_cons);
1385 *agg_event = true;
1386 }
1387
1388 rx_buf->data = NULL;
1389 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1390 bnxt_reuse_rx_data(rxr, cons, data);
1391 if (agg_bufs)
1392 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1393
1394 rc = -EIO;
1395 goto next_rx;
1396 }
1397
1398 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1399 dma_addr = dma_unmap_addr(rx_buf, mapping);
1400
1401 if (len <= bp->rx_copy_thresh) {
1402 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1403 bnxt_reuse_rx_data(rxr, cons, data);
1404 if (!skb) {
1405 rc = -ENOMEM;
1406 goto next_rx;
1407 }
1408 } else {
1409 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1410 if (!skb) {
1411 rc = -ENOMEM;
1412 goto next_rx;
1413 }
1414 }
1415
1416 if (agg_bufs) {
1417 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1418 if (!skb) {
1419 rc = -ENOMEM;
1420 goto next_rx;
1421 }
1422 }
1423
1424 if (RX_CMP_HASH_VALID(rxcmp)) {
1425 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1426 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1427
1428 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1429 if (hash_type != 1 && hash_type != 3)
1430 type = PKT_HASH_TYPE_L3;
1431 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1432 }
1433
1434 skb->protocol = eth_type_trans(skb, dev);
1435
Michael Chan8852ddb2016-06-06 02:37:16 -04001436 if ((rxcmp1->rx_cmp_flags2 &
1437 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1438 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001439 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001440 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001441 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1442
Michael Chan8852ddb2016-06-06 02:37:16 -04001443 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001444 }
1445
1446 skb_checksum_none_assert(skb);
1447 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1448 if (dev->features & NETIF_F_RXCSUM) {
1449 skb->ip_summed = CHECKSUM_UNNECESSARY;
1450 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1451 }
1452 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001453 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1454 if (dev->features & NETIF_F_RXCSUM)
1455 cpr->rx_l4_csum_errors++;
1456 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001457 }
1458
1459 skb_record_rx_queue(skb, bnapi->index);
1460 skb_mark_napi_id(skb, &bnapi->napi);
1461 if (bnxt_busy_polling(bnapi))
1462 netif_receive_skb(skb);
1463 else
1464 napi_gro_receive(&bnapi->napi, skb);
1465 rc = 1;
1466
1467next_rx:
1468 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001469 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001470
1471next_rx_no_prod:
1472 *raw_cons = tmp_raw_cons;
1473
1474 return rc;
1475}
1476
Michael Chan4bb13ab2016-04-05 14:09:01 -04001477#define BNXT_GET_EVENT_PORT(data) \
1478 ((data) & \
1479 HWRM_ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1480
Michael Chanc0c050c2015-10-22 16:01:17 -04001481static int bnxt_async_event_process(struct bnxt *bp,
1482 struct hwrm_async_event_cmpl *cmpl)
1483{
1484 u16 event_id = le16_to_cpu(cmpl->event_id);
1485
1486 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1487 switch (event_id) {
Michael Chan8cbde112016-04-11 04:11:14 -04001488 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1489 u32 data1 = le32_to_cpu(cmpl->event_data1);
1490 struct bnxt_link_info *link_info = &bp->link_info;
1491
1492 if (BNXT_VF(bp))
1493 goto async_event_process_exit;
1494 if (data1 & 0x20000) {
1495 u16 fw_speed = link_info->force_link_speed;
1496 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1497
1498 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1499 speed);
1500 }
1501 /* fall thru */
1502 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001503 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1504 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001505 break;
1506 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1507 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001508 break;
Michael Chan4bb13ab2016-04-05 14:09:01 -04001509 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1510 u32 data1 = le32_to_cpu(cmpl->event_data1);
1511 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1512
1513 if (BNXT_VF(bp))
1514 break;
1515
1516 if (bp->pf.port_id != port_id)
1517 break;
1518
Michael Chan4bb13ab2016-04-05 14:09:01 -04001519 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1520 break;
1521 }
Michael Chanfc0f1922016-06-13 02:25:30 -04001522 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1523 if (BNXT_PF(bp))
1524 goto async_event_process_exit;
1525 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1526 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001527 default:
1528 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1529 event_id);
Jeffrey Huang19241362016-02-26 04:00:00 -05001530 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001531 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001532 schedule_work(&bp->sp_task);
1533async_event_process_exit:
Michael Chanc0c050c2015-10-22 16:01:17 -04001534 return 0;
1535}
1536
1537static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1538{
1539 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1540 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1541 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1542 (struct hwrm_fwd_req_cmpl *)txcmp;
1543
1544 switch (cmpl_type) {
1545 case CMPL_BASE_TYPE_HWRM_DONE:
1546 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1547 if (seq_id == bp->hwrm_intr_seq_id)
1548 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1549 else
1550 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1551 break;
1552
1553 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1554 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1555
1556 if ((vf_id < bp->pf.first_vf_id) ||
1557 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1558 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1559 vf_id);
1560 return -EINVAL;
1561 }
1562
1563 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1564 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1565 schedule_work(&bp->sp_task);
1566 break;
1567
1568 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1569 bnxt_async_event_process(bp,
1570 (struct hwrm_async_event_cmpl *)txcmp);
1571
1572 default:
1573 break;
1574 }
1575
1576 return 0;
1577}
1578
1579static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1580{
1581 struct bnxt_napi *bnapi = dev_instance;
1582 struct bnxt *bp = bnapi->bp;
1583 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1584 u32 cons = RING_CMP(cpr->cp_raw_cons);
1585
1586 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1587 napi_schedule(&bnapi->napi);
1588 return IRQ_HANDLED;
1589}
1590
1591static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1592{
1593 u32 raw_cons = cpr->cp_raw_cons;
1594 u16 cons = RING_CMP(raw_cons);
1595 struct tx_cmp *txcmp;
1596
1597 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1598
1599 return TX_CMP_VALID(txcmp, raw_cons);
1600}
1601
Michael Chanc0c050c2015-10-22 16:01:17 -04001602static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1603{
1604 struct bnxt_napi *bnapi = dev_instance;
1605 struct bnxt *bp = bnapi->bp;
1606 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1607 u32 cons = RING_CMP(cpr->cp_raw_cons);
1608 u32 int_status;
1609
1610 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1611
1612 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001613 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001614 /* return if erroneous interrupt */
1615 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1616 return IRQ_NONE;
1617 }
1618
1619 /* disable ring IRQ */
1620 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1621
1622 /* Return here if interrupt is shared and is disabled. */
1623 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1624 return IRQ_HANDLED;
1625
1626 napi_schedule(&bnapi->napi);
1627 return IRQ_HANDLED;
1628}
1629
1630static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1631{
1632 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1633 u32 raw_cons = cpr->cp_raw_cons;
1634 u32 cons;
1635 int tx_pkts = 0;
1636 int rx_pkts = 0;
1637 bool rx_event = false;
1638 bool agg_event = false;
1639 struct tx_cmp *txcmp;
1640
1641 while (1) {
1642 int rc;
1643
1644 cons = RING_CMP(raw_cons);
1645 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1646
1647 if (!TX_CMP_VALID(txcmp, raw_cons))
1648 break;
1649
Michael Chan67a95e22016-05-04 16:56:43 -04001650 /* The valid test of the entry must be done first before
1651 * reading any further.
1652 */
Michael Chanb67daab2016-05-15 03:04:51 -04001653 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001654 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1655 tx_pkts++;
1656 /* return full budget so NAPI will complete. */
1657 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1658 rx_pkts = budget;
1659 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1660 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1661 if (likely(rc >= 0))
1662 rx_pkts += rc;
1663 else if (rc == -EBUSY) /* partial completion */
1664 break;
1665 rx_event = true;
1666 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1667 CMPL_BASE_TYPE_HWRM_DONE) ||
1668 (TX_CMP_TYPE(txcmp) ==
1669 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1670 (TX_CMP_TYPE(txcmp) ==
1671 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1672 bnxt_hwrm_handler(bp, txcmp);
1673 }
1674 raw_cons = NEXT_RAW_CMP(raw_cons);
1675
1676 if (rx_pkts == budget)
1677 break;
1678 }
1679
1680 cpr->cp_raw_cons = raw_cons;
1681 /* ACK completion ring before freeing tx ring and producing new
1682 * buffers in rx/agg rings to prevent overflowing the completion
1683 * ring.
1684 */
1685 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1686
1687 if (tx_pkts)
1688 bnxt_tx_int(bp, bnapi, tx_pkts);
1689
1690 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001691 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001692
1693 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1694 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1695 if (agg_event) {
1696 writel(DB_KEY_RX | rxr->rx_agg_prod,
1697 rxr->rx_agg_doorbell);
1698 writel(DB_KEY_RX | rxr->rx_agg_prod,
1699 rxr->rx_agg_doorbell);
1700 }
1701 }
1702 return rx_pkts;
1703}
1704
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001705static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1706{
1707 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1708 struct bnxt *bp = bnapi->bp;
1709 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1710 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1711 struct tx_cmp *txcmp;
1712 struct rx_cmp_ext *rxcmp1;
1713 u32 cp_cons, tmp_raw_cons;
1714 u32 raw_cons = cpr->cp_raw_cons;
1715 u32 rx_pkts = 0;
1716 bool agg_event = false;
1717
1718 while (1) {
1719 int rc;
1720
1721 cp_cons = RING_CMP(raw_cons);
1722 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1723
1724 if (!TX_CMP_VALID(txcmp, raw_cons))
1725 break;
1726
1727 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1728 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1729 cp_cons = RING_CMP(tmp_raw_cons);
1730 rxcmp1 = (struct rx_cmp_ext *)
1731 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1732
1733 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1734 break;
1735
1736 /* force an error to recycle the buffer */
1737 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1738 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1739
1740 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1741 if (likely(rc == -EIO))
1742 rx_pkts++;
1743 else if (rc == -EBUSY) /* partial completion */
1744 break;
1745 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1746 CMPL_BASE_TYPE_HWRM_DONE)) {
1747 bnxt_hwrm_handler(bp, txcmp);
1748 } else {
1749 netdev_err(bp->dev,
1750 "Invalid completion received on special ring\n");
1751 }
1752 raw_cons = NEXT_RAW_CMP(raw_cons);
1753
1754 if (rx_pkts == budget)
1755 break;
1756 }
1757
1758 cpr->cp_raw_cons = raw_cons;
1759 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1760 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1761 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1762
1763 if (agg_event) {
1764 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1765 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1766 }
1767
1768 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1769 napi_complete(napi);
1770 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1771 }
1772 return rx_pkts;
1773}
1774
Michael Chanc0c050c2015-10-22 16:01:17 -04001775static int bnxt_poll(struct napi_struct *napi, int budget)
1776{
1777 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1778 struct bnxt *bp = bnapi->bp;
1779 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1780 int work_done = 0;
1781
1782 if (!bnxt_lock_napi(bnapi))
1783 return budget;
1784
1785 while (1) {
1786 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1787
1788 if (work_done >= budget)
1789 break;
1790
1791 if (!bnxt_has_work(bp, cpr)) {
1792 napi_complete(napi);
1793 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1794 break;
1795 }
1796 }
1797 mmiowb();
1798 bnxt_unlock_napi(bnapi);
1799 return work_done;
1800}
1801
1802#ifdef CONFIG_NET_RX_BUSY_POLL
1803static int bnxt_busy_poll(struct napi_struct *napi)
1804{
1805 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1806 struct bnxt *bp = bnapi->bp;
1807 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1808 int rx_work, budget = 4;
1809
1810 if (atomic_read(&bp->intr_sem) != 0)
1811 return LL_FLUSH_FAILED;
1812
1813 if (!bnxt_lock_poll(bnapi))
1814 return LL_FLUSH_BUSY;
1815
1816 rx_work = bnxt_poll_work(bp, bnapi, budget);
1817
1818 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1819
1820 bnxt_unlock_poll(bnapi);
1821 return rx_work;
1822}
1823#endif
1824
1825static void bnxt_free_tx_skbs(struct bnxt *bp)
1826{
1827 int i, max_idx;
1828 struct pci_dev *pdev = bp->pdev;
1829
Michael Chanb6ab4b02016-01-02 23:44:59 -05001830 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001831 return;
1832
1833 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1834 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001835 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001836 int j;
1837
Michael Chanc0c050c2015-10-22 16:01:17 -04001838 for (j = 0; j < max_idx;) {
1839 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1840 struct sk_buff *skb = tx_buf->skb;
1841 int k, last;
1842
1843 if (!skb) {
1844 j++;
1845 continue;
1846 }
1847
1848 tx_buf->skb = NULL;
1849
1850 if (tx_buf->is_push) {
1851 dev_kfree_skb(skb);
1852 j += 2;
1853 continue;
1854 }
1855
1856 dma_unmap_single(&pdev->dev,
1857 dma_unmap_addr(tx_buf, mapping),
1858 skb_headlen(skb),
1859 PCI_DMA_TODEVICE);
1860
1861 last = tx_buf->nr_frags;
1862 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001863 for (k = 0; k < last; k++, j++) {
1864 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001865 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1866
Michael Chand612a572016-01-28 03:11:22 -05001867 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001868 dma_unmap_page(
1869 &pdev->dev,
1870 dma_unmap_addr(tx_buf, mapping),
1871 skb_frag_size(frag), PCI_DMA_TODEVICE);
1872 }
1873 dev_kfree_skb(skb);
1874 }
1875 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1876 }
1877}
1878
1879static void bnxt_free_rx_skbs(struct bnxt *bp)
1880{
1881 int i, max_idx, max_agg_idx;
1882 struct pci_dev *pdev = bp->pdev;
1883
Michael Chanb6ab4b02016-01-02 23:44:59 -05001884 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001885 return;
1886
1887 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1888 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1889 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001890 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001891 int j;
1892
Michael Chanc0c050c2015-10-22 16:01:17 -04001893 if (rxr->rx_tpa) {
1894 for (j = 0; j < MAX_TPA; j++) {
1895 struct bnxt_tpa_info *tpa_info =
1896 &rxr->rx_tpa[j];
1897 u8 *data = tpa_info->data;
1898
1899 if (!data)
1900 continue;
1901
1902 dma_unmap_single(
1903 &pdev->dev,
1904 dma_unmap_addr(tpa_info, mapping),
1905 bp->rx_buf_use_size,
1906 PCI_DMA_FROMDEVICE);
1907
1908 tpa_info->data = NULL;
1909
1910 kfree(data);
1911 }
1912 }
1913
1914 for (j = 0; j < max_idx; j++) {
1915 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1916 u8 *data = rx_buf->data;
1917
1918 if (!data)
1919 continue;
1920
1921 dma_unmap_single(&pdev->dev,
1922 dma_unmap_addr(rx_buf, mapping),
1923 bp->rx_buf_use_size,
1924 PCI_DMA_FROMDEVICE);
1925
1926 rx_buf->data = NULL;
1927
1928 kfree(data);
1929 }
1930
1931 for (j = 0; j < max_agg_idx; j++) {
1932 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1933 &rxr->rx_agg_ring[j];
1934 struct page *page = rx_agg_buf->page;
1935
1936 if (!page)
1937 continue;
1938
1939 dma_unmap_page(&pdev->dev,
1940 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001941 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001942
1943 rx_agg_buf->page = NULL;
1944 __clear_bit(j, rxr->rx_agg_bmap);
1945
1946 __free_page(page);
1947 }
Michael Chan89d0a062016-04-25 02:30:51 -04001948 if (rxr->rx_page) {
1949 __free_page(rxr->rx_page);
1950 rxr->rx_page = NULL;
1951 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001952 }
1953}
1954
1955static void bnxt_free_skbs(struct bnxt *bp)
1956{
1957 bnxt_free_tx_skbs(bp);
1958 bnxt_free_rx_skbs(bp);
1959}
1960
1961static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1962{
1963 struct pci_dev *pdev = bp->pdev;
1964 int i;
1965
1966 for (i = 0; i < ring->nr_pages; i++) {
1967 if (!ring->pg_arr[i])
1968 continue;
1969
1970 dma_free_coherent(&pdev->dev, ring->page_size,
1971 ring->pg_arr[i], ring->dma_arr[i]);
1972
1973 ring->pg_arr[i] = NULL;
1974 }
1975 if (ring->pg_tbl) {
1976 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1977 ring->pg_tbl, ring->pg_tbl_map);
1978 ring->pg_tbl = NULL;
1979 }
1980 if (ring->vmem_size && *ring->vmem) {
1981 vfree(*ring->vmem);
1982 *ring->vmem = NULL;
1983 }
1984}
1985
1986static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1987{
1988 int i;
1989 struct pci_dev *pdev = bp->pdev;
1990
1991 if (ring->nr_pages > 1) {
1992 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1993 ring->nr_pages * 8,
1994 &ring->pg_tbl_map,
1995 GFP_KERNEL);
1996 if (!ring->pg_tbl)
1997 return -ENOMEM;
1998 }
1999
2000 for (i = 0; i < ring->nr_pages; i++) {
2001 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2002 ring->page_size,
2003 &ring->dma_arr[i],
2004 GFP_KERNEL);
2005 if (!ring->pg_arr[i])
2006 return -ENOMEM;
2007
2008 if (ring->nr_pages > 1)
2009 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2010 }
2011
2012 if (ring->vmem_size) {
2013 *ring->vmem = vzalloc(ring->vmem_size);
2014 if (!(*ring->vmem))
2015 return -ENOMEM;
2016 }
2017 return 0;
2018}
2019
2020static void bnxt_free_rx_rings(struct bnxt *bp)
2021{
2022 int i;
2023
Michael Chanb6ab4b02016-01-02 23:44:59 -05002024 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002025 return;
2026
2027 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002028 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002029 struct bnxt_ring_struct *ring;
2030
Michael Chanc0c050c2015-10-22 16:01:17 -04002031 kfree(rxr->rx_tpa);
2032 rxr->rx_tpa = NULL;
2033
2034 kfree(rxr->rx_agg_bmap);
2035 rxr->rx_agg_bmap = NULL;
2036
2037 ring = &rxr->rx_ring_struct;
2038 bnxt_free_ring(bp, ring);
2039
2040 ring = &rxr->rx_agg_ring_struct;
2041 bnxt_free_ring(bp, ring);
2042 }
2043}
2044
2045static int bnxt_alloc_rx_rings(struct bnxt *bp)
2046{
2047 int i, rc, agg_rings = 0, tpa_rings = 0;
2048
Michael Chanb6ab4b02016-01-02 23:44:59 -05002049 if (!bp->rx_ring)
2050 return -ENOMEM;
2051
Michael Chanc0c050c2015-10-22 16:01:17 -04002052 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2053 agg_rings = 1;
2054
2055 if (bp->flags & BNXT_FLAG_TPA)
2056 tpa_rings = 1;
2057
2058 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002059 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002060 struct bnxt_ring_struct *ring;
2061
Michael Chanc0c050c2015-10-22 16:01:17 -04002062 ring = &rxr->rx_ring_struct;
2063
2064 rc = bnxt_alloc_ring(bp, ring);
2065 if (rc)
2066 return rc;
2067
2068 if (agg_rings) {
2069 u16 mem_size;
2070
2071 ring = &rxr->rx_agg_ring_struct;
2072 rc = bnxt_alloc_ring(bp, ring);
2073 if (rc)
2074 return rc;
2075
2076 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2077 mem_size = rxr->rx_agg_bmap_size / 8;
2078 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2079 if (!rxr->rx_agg_bmap)
2080 return -ENOMEM;
2081
2082 if (tpa_rings) {
2083 rxr->rx_tpa = kcalloc(MAX_TPA,
2084 sizeof(struct bnxt_tpa_info),
2085 GFP_KERNEL);
2086 if (!rxr->rx_tpa)
2087 return -ENOMEM;
2088 }
2089 }
2090 }
2091 return 0;
2092}
2093
2094static void bnxt_free_tx_rings(struct bnxt *bp)
2095{
2096 int i;
2097 struct pci_dev *pdev = bp->pdev;
2098
Michael Chanb6ab4b02016-01-02 23:44:59 -05002099 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002100 return;
2101
2102 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002103 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002104 struct bnxt_ring_struct *ring;
2105
Michael Chanc0c050c2015-10-22 16:01:17 -04002106 if (txr->tx_push) {
2107 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2108 txr->tx_push, txr->tx_push_mapping);
2109 txr->tx_push = NULL;
2110 }
2111
2112 ring = &txr->tx_ring_struct;
2113
2114 bnxt_free_ring(bp, ring);
2115 }
2116}
2117
2118static int bnxt_alloc_tx_rings(struct bnxt *bp)
2119{
2120 int i, j, rc;
2121 struct pci_dev *pdev = bp->pdev;
2122
2123 bp->tx_push_size = 0;
2124 if (bp->tx_push_thresh) {
2125 int push_size;
2126
2127 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2128 bp->tx_push_thresh);
2129
Michael Chan4419dbe2016-02-10 17:33:49 -05002130 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002131 push_size = 0;
2132 bp->tx_push_thresh = 0;
2133 }
2134
2135 bp->tx_push_size = push_size;
2136 }
2137
2138 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002139 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002140 struct bnxt_ring_struct *ring;
2141
Michael Chanc0c050c2015-10-22 16:01:17 -04002142 ring = &txr->tx_ring_struct;
2143
2144 rc = bnxt_alloc_ring(bp, ring);
2145 if (rc)
2146 return rc;
2147
2148 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002149 dma_addr_t mapping;
2150
2151 /* One pre-allocated DMA buffer to backup
2152 * TX push operation
2153 */
2154 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2155 bp->tx_push_size,
2156 &txr->tx_push_mapping,
2157 GFP_KERNEL);
2158
2159 if (!txr->tx_push)
2160 return -ENOMEM;
2161
Michael Chanc0c050c2015-10-22 16:01:17 -04002162 mapping = txr->tx_push_mapping +
2163 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002164 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002165
Michael Chan4419dbe2016-02-10 17:33:49 -05002166 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002167 }
2168 ring->queue_id = bp->q_info[j].queue_id;
2169 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2170 j++;
2171 }
2172 return 0;
2173}
2174
2175static void bnxt_free_cp_rings(struct bnxt *bp)
2176{
2177 int i;
2178
2179 if (!bp->bnapi)
2180 return;
2181
2182 for (i = 0; i < bp->cp_nr_rings; i++) {
2183 struct bnxt_napi *bnapi = bp->bnapi[i];
2184 struct bnxt_cp_ring_info *cpr;
2185 struct bnxt_ring_struct *ring;
2186
2187 if (!bnapi)
2188 continue;
2189
2190 cpr = &bnapi->cp_ring;
2191 ring = &cpr->cp_ring_struct;
2192
2193 bnxt_free_ring(bp, ring);
2194 }
2195}
2196
2197static int bnxt_alloc_cp_rings(struct bnxt *bp)
2198{
2199 int i, rc;
2200
2201 for (i = 0; i < bp->cp_nr_rings; i++) {
2202 struct bnxt_napi *bnapi = bp->bnapi[i];
2203 struct bnxt_cp_ring_info *cpr;
2204 struct bnxt_ring_struct *ring;
2205
2206 if (!bnapi)
2207 continue;
2208
2209 cpr = &bnapi->cp_ring;
2210 ring = &cpr->cp_ring_struct;
2211
2212 rc = bnxt_alloc_ring(bp, ring);
2213 if (rc)
2214 return rc;
2215 }
2216 return 0;
2217}
2218
2219static void bnxt_init_ring_struct(struct bnxt *bp)
2220{
2221 int i;
2222
2223 for (i = 0; i < bp->cp_nr_rings; i++) {
2224 struct bnxt_napi *bnapi = bp->bnapi[i];
2225 struct bnxt_cp_ring_info *cpr;
2226 struct bnxt_rx_ring_info *rxr;
2227 struct bnxt_tx_ring_info *txr;
2228 struct bnxt_ring_struct *ring;
2229
2230 if (!bnapi)
2231 continue;
2232
2233 cpr = &bnapi->cp_ring;
2234 ring = &cpr->cp_ring_struct;
2235 ring->nr_pages = bp->cp_nr_pages;
2236 ring->page_size = HW_CMPD_RING_SIZE;
2237 ring->pg_arr = (void **)cpr->cp_desc_ring;
2238 ring->dma_arr = cpr->cp_desc_mapping;
2239 ring->vmem_size = 0;
2240
Michael Chanb6ab4b02016-01-02 23:44:59 -05002241 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002242 if (!rxr)
2243 goto skip_rx;
2244
Michael Chanc0c050c2015-10-22 16:01:17 -04002245 ring = &rxr->rx_ring_struct;
2246 ring->nr_pages = bp->rx_nr_pages;
2247 ring->page_size = HW_RXBD_RING_SIZE;
2248 ring->pg_arr = (void **)rxr->rx_desc_ring;
2249 ring->dma_arr = rxr->rx_desc_mapping;
2250 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2251 ring->vmem = (void **)&rxr->rx_buf_ring;
2252
2253 ring = &rxr->rx_agg_ring_struct;
2254 ring->nr_pages = bp->rx_agg_nr_pages;
2255 ring->page_size = HW_RXBD_RING_SIZE;
2256 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2257 ring->dma_arr = rxr->rx_agg_desc_mapping;
2258 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2259 ring->vmem = (void **)&rxr->rx_agg_ring;
2260
Michael Chan3b2b7d92016-01-02 23:45:00 -05002261skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002262 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002263 if (!txr)
2264 continue;
2265
Michael Chanc0c050c2015-10-22 16:01:17 -04002266 ring = &txr->tx_ring_struct;
2267 ring->nr_pages = bp->tx_nr_pages;
2268 ring->page_size = HW_RXBD_RING_SIZE;
2269 ring->pg_arr = (void **)txr->tx_desc_ring;
2270 ring->dma_arr = txr->tx_desc_mapping;
2271 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2272 ring->vmem = (void **)&txr->tx_buf_ring;
2273 }
2274}
2275
2276static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2277{
2278 int i;
2279 u32 prod;
2280 struct rx_bd **rx_buf_ring;
2281
2282 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2283 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2284 int j;
2285 struct rx_bd *rxbd;
2286
2287 rxbd = rx_buf_ring[i];
2288 if (!rxbd)
2289 continue;
2290
2291 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2292 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2293 rxbd->rx_bd_opaque = prod;
2294 }
2295 }
2296}
2297
2298static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2299{
2300 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002301 struct bnxt_rx_ring_info *rxr;
2302 struct bnxt_ring_struct *ring;
2303 u32 prod, type;
2304 int i;
2305
Michael Chanc0c050c2015-10-22 16:01:17 -04002306 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2307 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2308
2309 if (NET_IP_ALIGN == 2)
2310 type |= RX_BD_FLAGS_SOP;
2311
Michael Chanb6ab4b02016-01-02 23:44:59 -05002312 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002313 ring = &rxr->rx_ring_struct;
2314 bnxt_init_rxbd_pages(ring, type);
2315
2316 prod = rxr->rx_prod;
2317 for (i = 0; i < bp->rx_ring_size; i++) {
2318 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2319 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2320 ring_nr, i, bp->rx_ring_size);
2321 break;
2322 }
2323 prod = NEXT_RX(prod);
2324 }
2325 rxr->rx_prod = prod;
2326 ring->fw_ring_id = INVALID_HW_RING_ID;
2327
Michael Chanedd0c2c2015-12-27 18:19:19 -05002328 ring = &rxr->rx_agg_ring_struct;
2329 ring->fw_ring_id = INVALID_HW_RING_ID;
2330
Michael Chanc0c050c2015-10-22 16:01:17 -04002331 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2332 return 0;
2333
Michael Chan2839f282016-04-25 02:30:50 -04002334 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002335 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2336
2337 bnxt_init_rxbd_pages(ring, type);
2338
2339 prod = rxr->rx_agg_prod;
2340 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2341 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2342 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2343 ring_nr, i, bp->rx_ring_size);
2344 break;
2345 }
2346 prod = NEXT_RX_AGG(prod);
2347 }
2348 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002349
2350 if (bp->flags & BNXT_FLAG_TPA) {
2351 if (rxr->rx_tpa) {
2352 u8 *data;
2353 dma_addr_t mapping;
2354
2355 for (i = 0; i < MAX_TPA; i++) {
2356 data = __bnxt_alloc_rx_data(bp, &mapping,
2357 GFP_KERNEL);
2358 if (!data)
2359 return -ENOMEM;
2360
2361 rxr->rx_tpa[i].data = data;
2362 rxr->rx_tpa[i].mapping = mapping;
2363 }
2364 } else {
2365 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2366 return -ENOMEM;
2367 }
2368 }
2369
2370 return 0;
2371}
2372
2373static int bnxt_init_rx_rings(struct bnxt *bp)
2374{
2375 int i, rc = 0;
2376
2377 for (i = 0; i < bp->rx_nr_rings; i++) {
2378 rc = bnxt_init_one_rx_ring(bp, i);
2379 if (rc)
2380 break;
2381 }
2382
2383 return rc;
2384}
2385
2386static int bnxt_init_tx_rings(struct bnxt *bp)
2387{
2388 u16 i;
2389
2390 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2391 MAX_SKB_FRAGS + 1);
2392
2393 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002394 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002395 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2396
2397 ring->fw_ring_id = INVALID_HW_RING_ID;
2398 }
2399
2400 return 0;
2401}
2402
2403static void bnxt_free_ring_grps(struct bnxt *bp)
2404{
2405 kfree(bp->grp_info);
2406 bp->grp_info = NULL;
2407}
2408
2409static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2410{
2411 int i;
2412
2413 if (irq_re_init) {
2414 bp->grp_info = kcalloc(bp->cp_nr_rings,
2415 sizeof(struct bnxt_ring_grp_info),
2416 GFP_KERNEL);
2417 if (!bp->grp_info)
2418 return -ENOMEM;
2419 }
2420 for (i = 0; i < bp->cp_nr_rings; i++) {
2421 if (irq_re_init)
2422 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2423 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2424 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2425 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2426 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2427 }
2428 return 0;
2429}
2430
2431static void bnxt_free_vnics(struct bnxt *bp)
2432{
2433 kfree(bp->vnic_info);
2434 bp->vnic_info = NULL;
2435 bp->nr_vnics = 0;
2436}
2437
2438static int bnxt_alloc_vnics(struct bnxt *bp)
2439{
2440 int num_vnics = 1;
2441
2442#ifdef CONFIG_RFS_ACCEL
2443 if (bp->flags & BNXT_FLAG_RFS)
2444 num_vnics += bp->rx_nr_rings;
2445#endif
2446
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002447 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2448 num_vnics++;
2449
Michael Chanc0c050c2015-10-22 16:01:17 -04002450 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2451 GFP_KERNEL);
2452 if (!bp->vnic_info)
2453 return -ENOMEM;
2454
2455 bp->nr_vnics = num_vnics;
2456 return 0;
2457}
2458
2459static void bnxt_init_vnics(struct bnxt *bp)
2460{
2461 int i;
2462
2463 for (i = 0; i < bp->nr_vnics; i++) {
2464 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2465
2466 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002467 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2468 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002469 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2470
2471 if (bp->vnic_info[i].rss_hash_key) {
2472 if (i == 0)
2473 prandom_bytes(vnic->rss_hash_key,
2474 HW_HASH_KEY_SIZE);
2475 else
2476 memcpy(vnic->rss_hash_key,
2477 bp->vnic_info[0].rss_hash_key,
2478 HW_HASH_KEY_SIZE);
2479 }
2480 }
2481}
2482
2483static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2484{
2485 int pages;
2486
2487 pages = ring_size / desc_per_pg;
2488
2489 if (!pages)
2490 return 1;
2491
2492 pages++;
2493
2494 while (pages & (pages - 1))
2495 pages++;
2496
2497 return pages;
2498}
2499
2500static void bnxt_set_tpa_flags(struct bnxt *bp)
2501{
2502 bp->flags &= ~BNXT_FLAG_TPA;
2503 if (bp->dev->features & NETIF_F_LRO)
2504 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002505 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002506 bp->flags |= BNXT_FLAG_GRO;
2507}
2508
2509/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2510 * be set on entry.
2511 */
2512void bnxt_set_ring_params(struct bnxt *bp)
2513{
2514 u32 ring_size, rx_size, rx_space;
2515 u32 agg_factor = 0, agg_ring_size = 0;
2516
2517 /* 8 for CRC and VLAN */
2518 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2519
2520 rx_space = rx_size + NET_SKB_PAD +
2521 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2522
2523 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2524 ring_size = bp->rx_ring_size;
2525 bp->rx_agg_ring_size = 0;
2526 bp->rx_agg_nr_pages = 0;
2527
2528 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002529 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002530
2531 bp->flags &= ~BNXT_FLAG_JUMBO;
2532 if (rx_space > PAGE_SIZE) {
2533 u32 jumbo_factor;
2534
2535 bp->flags |= BNXT_FLAG_JUMBO;
2536 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2537 if (jumbo_factor > agg_factor)
2538 agg_factor = jumbo_factor;
2539 }
2540 agg_ring_size = ring_size * agg_factor;
2541
2542 if (agg_ring_size) {
2543 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2544 RX_DESC_CNT);
2545 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2546 u32 tmp = agg_ring_size;
2547
2548 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2549 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2550 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2551 tmp, agg_ring_size);
2552 }
2553 bp->rx_agg_ring_size = agg_ring_size;
2554 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2555 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2556 rx_space = rx_size + NET_SKB_PAD +
2557 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2558 }
2559
2560 bp->rx_buf_use_size = rx_size;
2561 bp->rx_buf_size = rx_space;
2562
2563 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2564 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2565
2566 ring_size = bp->tx_ring_size;
2567 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2568 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2569
2570 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2571 bp->cp_ring_size = ring_size;
2572
2573 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2574 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2575 bp->cp_nr_pages = MAX_CP_PAGES;
2576 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2577 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2578 ring_size, bp->cp_ring_size);
2579 }
2580 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2581 bp->cp_ring_mask = bp->cp_bit - 1;
2582}
2583
2584static void bnxt_free_vnic_attributes(struct bnxt *bp)
2585{
2586 int i;
2587 struct bnxt_vnic_info *vnic;
2588 struct pci_dev *pdev = bp->pdev;
2589
2590 if (!bp->vnic_info)
2591 return;
2592
2593 for (i = 0; i < bp->nr_vnics; i++) {
2594 vnic = &bp->vnic_info[i];
2595
2596 kfree(vnic->fw_grp_ids);
2597 vnic->fw_grp_ids = NULL;
2598
2599 kfree(vnic->uc_list);
2600 vnic->uc_list = NULL;
2601
2602 if (vnic->mc_list) {
2603 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2604 vnic->mc_list, vnic->mc_list_mapping);
2605 vnic->mc_list = NULL;
2606 }
2607
2608 if (vnic->rss_table) {
2609 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2610 vnic->rss_table,
2611 vnic->rss_table_dma_addr);
2612 vnic->rss_table = NULL;
2613 }
2614
2615 vnic->rss_hash_key = NULL;
2616 vnic->flags = 0;
2617 }
2618}
2619
2620static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2621{
2622 int i, rc = 0, size;
2623 struct bnxt_vnic_info *vnic;
2624 struct pci_dev *pdev = bp->pdev;
2625 int max_rings;
2626
2627 for (i = 0; i < bp->nr_vnics; i++) {
2628 vnic = &bp->vnic_info[i];
2629
2630 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2631 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2632
2633 if (mem_size > 0) {
2634 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2635 if (!vnic->uc_list) {
2636 rc = -ENOMEM;
2637 goto out;
2638 }
2639 }
2640 }
2641
2642 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2643 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2644 vnic->mc_list =
2645 dma_alloc_coherent(&pdev->dev,
2646 vnic->mc_list_size,
2647 &vnic->mc_list_mapping,
2648 GFP_KERNEL);
2649 if (!vnic->mc_list) {
2650 rc = -ENOMEM;
2651 goto out;
2652 }
2653 }
2654
2655 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2656 max_rings = bp->rx_nr_rings;
2657 else
2658 max_rings = 1;
2659
2660 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2661 if (!vnic->fw_grp_ids) {
2662 rc = -ENOMEM;
2663 goto out;
2664 }
2665
2666 /* Allocate rss table and hash key */
2667 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2668 &vnic->rss_table_dma_addr,
2669 GFP_KERNEL);
2670 if (!vnic->rss_table) {
2671 rc = -ENOMEM;
2672 goto out;
2673 }
2674
2675 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2676
2677 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2678 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2679 }
2680 return 0;
2681
2682out:
2683 return rc;
2684}
2685
2686static void bnxt_free_hwrm_resources(struct bnxt *bp)
2687{
2688 struct pci_dev *pdev = bp->pdev;
2689
2690 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2691 bp->hwrm_cmd_resp_dma_addr);
2692
2693 bp->hwrm_cmd_resp_addr = NULL;
2694 if (bp->hwrm_dbg_resp_addr) {
2695 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2696 bp->hwrm_dbg_resp_addr,
2697 bp->hwrm_dbg_resp_dma_addr);
2698
2699 bp->hwrm_dbg_resp_addr = NULL;
2700 }
2701}
2702
2703static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2704{
2705 struct pci_dev *pdev = bp->pdev;
2706
2707 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2708 &bp->hwrm_cmd_resp_dma_addr,
2709 GFP_KERNEL);
2710 if (!bp->hwrm_cmd_resp_addr)
2711 return -ENOMEM;
2712 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2713 HWRM_DBG_REG_BUF_SIZE,
2714 &bp->hwrm_dbg_resp_dma_addr,
2715 GFP_KERNEL);
2716 if (!bp->hwrm_dbg_resp_addr)
2717 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2718
2719 return 0;
2720}
2721
2722static void bnxt_free_stats(struct bnxt *bp)
2723{
2724 u32 size, i;
2725 struct pci_dev *pdev = bp->pdev;
2726
Michael Chan3bdf56c2016-03-07 15:38:45 -05002727 if (bp->hw_rx_port_stats) {
2728 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2729 bp->hw_rx_port_stats,
2730 bp->hw_rx_port_stats_map);
2731 bp->hw_rx_port_stats = NULL;
2732 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2733 }
2734
Michael Chanc0c050c2015-10-22 16:01:17 -04002735 if (!bp->bnapi)
2736 return;
2737
2738 size = sizeof(struct ctx_hw_stats);
2739
2740 for (i = 0; i < bp->cp_nr_rings; i++) {
2741 struct bnxt_napi *bnapi = bp->bnapi[i];
2742 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2743
2744 if (cpr->hw_stats) {
2745 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2746 cpr->hw_stats_map);
2747 cpr->hw_stats = NULL;
2748 }
2749 }
2750}
2751
2752static int bnxt_alloc_stats(struct bnxt *bp)
2753{
2754 u32 size, i;
2755 struct pci_dev *pdev = bp->pdev;
2756
2757 size = sizeof(struct ctx_hw_stats);
2758
2759 for (i = 0; i < bp->cp_nr_rings; i++) {
2760 struct bnxt_napi *bnapi = bp->bnapi[i];
2761 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2762
2763 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2764 &cpr->hw_stats_map,
2765 GFP_KERNEL);
2766 if (!cpr->hw_stats)
2767 return -ENOMEM;
2768
2769 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2770 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002771
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002772 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002773 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2774 sizeof(struct tx_port_stats) + 1024;
2775
2776 bp->hw_rx_port_stats =
2777 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2778 &bp->hw_rx_port_stats_map,
2779 GFP_KERNEL);
2780 if (!bp->hw_rx_port_stats)
2781 return -ENOMEM;
2782
2783 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2784 512;
2785 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2786 sizeof(struct rx_port_stats) + 512;
2787 bp->flags |= BNXT_FLAG_PORT_STATS;
2788 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002789 return 0;
2790}
2791
2792static void bnxt_clear_ring_indices(struct bnxt *bp)
2793{
2794 int i;
2795
2796 if (!bp->bnapi)
2797 return;
2798
2799 for (i = 0; i < bp->cp_nr_rings; i++) {
2800 struct bnxt_napi *bnapi = bp->bnapi[i];
2801 struct bnxt_cp_ring_info *cpr;
2802 struct bnxt_rx_ring_info *rxr;
2803 struct bnxt_tx_ring_info *txr;
2804
2805 if (!bnapi)
2806 continue;
2807
2808 cpr = &bnapi->cp_ring;
2809 cpr->cp_raw_cons = 0;
2810
Michael Chanb6ab4b02016-01-02 23:44:59 -05002811 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002812 if (txr) {
2813 txr->tx_prod = 0;
2814 txr->tx_cons = 0;
2815 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002816
Michael Chanb6ab4b02016-01-02 23:44:59 -05002817 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002818 if (rxr) {
2819 rxr->rx_prod = 0;
2820 rxr->rx_agg_prod = 0;
2821 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002822 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002823 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002824 }
2825}
2826
2827static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2828{
2829#ifdef CONFIG_RFS_ACCEL
2830 int i;
2831
2832 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2833 * safe to delete the hash table.
2834 */
2835 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2836 struct hlist_head *head;
2837 struct hlist_node *tmp;
2838 struct bnxt_ntuple_filter *fltr;
2839
2840 head = &bp->ntp_fltr_hash_tbl[i];
2841 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2842 hlist_del(&fltr->hash);
2843 kfree(fltr);
2844 }
2845 }
2846 if (irq_reinit) {
2847 kfree(bp->ntp_fltr_bmap);
2848 bp->ntp_fltr_bmap = NULL;
2849 }
2850 bp->ntp_fltr_count = 0;
2851#endif
2852}
2853
2854static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2855{
2856#ifdef CONFIG_RFS_ACCEL
2857 int i, rc = 0;
2858
2859 if (!(bp->flags & BNXT_FLAG_RFS))
2860 return 0;
2861
2862 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2863 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2864
2865 bp->ntp_fltr_count = 0;
2866 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2867 GFP_KERNEL);
2868
2869 if (!bp->ntp_fltr_bmap)
2870 rc = -ENOMEM;
2871
2872 return rc;
2873#else
2874 return 0;
2875#endif
2876}
2877
2878static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2879{
2880 bnxt_free_vnic_attributes(bp);
2881 bnxt_free_tx_rings(bp);
2882 bnxt_free_rx_rings(bp);
2883 bnxt_free_cp_rings(bp);
2884 bnxt_free_ntp_fltrs(bp, irq_re_init);
2885 if (irq_re_init) {
2886 bnxt_free_stats(bp);
2887 bnxt_free_ring_grps(bp);
2888 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002889 kfree(bp->tx_ring);
2890 bp->tx_ring = NULL;
2891 kfree(bp->rx_ring);
2892 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002893 kfree(bp->bnapi);
2894 bp->bnapi = NULL;
2895 } else {
2896 bnxt_clear_ring_indices(bp);
2897 }
2898}
2899
2900static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2901{
Michael Chan01657bc2016-01-02 23:45:03 -05002902 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002903 void *bnapi;
2904
2905 if (irq_re_init) {
2906 /* Allocate bnapi mem pointer array and mem block for
2907 * all queues
2908 */
2909 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2910 bp->cp_nr_rings);
2911 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2912 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2913 if (!bnapi)
2914 return -ENOMEM;
2915
2916 bp->bnapi = bnapi;
2917 bnapi += arr_size;
2918 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2919 bp->bnapi[i] = bnapi;
2920 bp->bnapi[i]->index = i;
2921 bp->bnapi[i]->bp = bp;
2922 }
2923
Michael Chanb6ab4b02016-01-02 23:44:59 -05002924 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2925 sizeof(struct bnxt_rx_ring_info),
2926 GFP_KERNEL);
2927 if (!bp->rx_ring)
2928 return -ENOMEM;
2929
2930 for (i = 0; i < bp->rx_nr_rings; i++) {
2931 bp->rx_ring[i].bnapi = bp->bnapi[i];
2932 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2933 }
2934
2935 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2936 sizeof(struct bnxt_tx_ring_info),
2937 GFP_KERNEL);
2938 if (!bp->tx_ring)
2939 return -ENOMEM;
2940
Michael Chan01657bc2016-01-02 23:45:03 -05002941 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2942 j = 0;
2943 else
2944 j = bp->rx_nr_rings;
2945
2946 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2947 bp->tx_ring[i].bnapi = bp->bnapi[j];
2948 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002949 }
2950
Michael Chanc0c050c2015-10-22 16:01:17 -04002951 rc = bnxt_alloc_stats(bp);
2952 if (rc)
2953 goto alloc_mem_err;
2954
2955 rc = bnxt_alloc_ntp_fltrs(bp);
2956 if (rc)
2957 goto alloc_mem_err;
2958
2959 rc = bnxt_alloc_vnics(bp);
2960 if (rc)
2961 goto alloc_mem_err;
2962 }
2963
2964 bnxt_init_ring_struct(bp);
2965
2966 rc = bnxt_alloc_rx_rings(bp);
2967 if (rc)
2968 goto alloc_mem_err;
2969
2970 rc = bnxt_alloc_tx_rings(bp);
2971 if (rc)
2972 goto alloc_mem_err;
2973
2974 rc = bnxt_alloc_cp_rings(bp);
2975 if (rc)
2976 goto alloc_mem_err;
2977
2978 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2979 BNXT_VNIC_UCAST_FLAG;
2980 rc = bnxt_alloc_vnic_attributes(bp);
2981 if (rc)
2982 goto alloc_mem_err;
2983 return 0;
2984
2985alloc_mem_err:
2986 bnxt_free_mem(bp, true);
2987 return rc;
2988}
2989
2990void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2991 u16 cmpl_ring, u16 target_id)
2992{
Michael Chana8643e12016-02-26 04:00:05 -05002993 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04002994
Michael Chana8643e12016-02-26 04:00:05 -05002995 req->req_type = cpu_to_le16(req_type);
2996 req->cmpl_ring = cpu_to_le16(cmpl_ring);
2997 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002998 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
2999}
3000
Michael Chanfbfbc482016-02-26 04:00:07 -05003001static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3002 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003003{
Michael Chana11fa2b2016-05-15 03:04:47 -04003004 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05003005 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04003006 u32 *data = msg;
3007 __le32 *resp_len, *valid;
3008 u16 cp_ring_id, len = 0;
3009 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3010
Michael Chana8643e12016-02-26 04:00:05 -05003011 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04003012 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05003013 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04003014 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3015
3016 /* Write request msg to hwrm channel */
3017 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3018
Michael Chane6ef2692016-03-28 19:46:05 -04003019 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05003020 writel(0, bp->bar0 + i);
3021
Michael Chanc0c050c2015-10-22 16:01:17 -04003022 /* currently supports only one outstanding message */
3023 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05003024 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003025
3026 /* Ring channel doorbell */
3027 writel(1, bp->bar0 + 0x100);
3028
Michael Chanff4fe812016-02-26 04:00:04 -05003029 if (!timeout)
3030 timeout = DFLT_HWRM_CMD_TIMEOUT;
3031
Michael Chanc0c050c2015-10-22 16:01:17 -04003032 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04003033 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003034 if (intr_process) {
3035 /* Wait until hwrm response cmpl interrupt is processed */
3036 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003037 i++ < tmo_count) {
3038 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003039 }
3040
3041 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3042 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003043 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003044 return -1;
3045 }
3046 } else {
3047 /* Check if response len is updated */
3048 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003049 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003050 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3051 HWRM_RESP_LEN_SFT;
3052 if (len)
3053 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003054 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003055 }
3056
Michael Chana11fa2b2016-05-15 03:04:47 -04003057 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003058 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003059 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003060 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003061 return -1;
3062 }
3063
3064 /* Last word of resp contains valid bit */
3065 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003066 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003067 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3068 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003069 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003070 }
3071
Michael Chana11fa2b2016-05-15 03:04:47 -04003072 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003073 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003074 timeout, le16_to_cpu(req->req_type),
3075 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003076 return -1;
3077 }
3078 }
3079
3080 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003081 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003082 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3083 le16_to_cpu(resp->req_type),
3084 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003085 return rc;
3086}
3087
3088int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3089{
3090 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003091}
3092
3093int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3094{
3095 int rc;
3096
3097 mutex_lock(&bp->hwrm_cmd_lock);
3098 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3099 mutex_unlock(&bp->hwrm_cmd_lock);
3100 return rc;
3101}
3102
Michael Chan90e209212016-02-26 04:00:08 -05003103int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3104 int timeout)
3105{
3106 int rc;
3107
3108 mutex_lock(&bp->hwrm_cmd_lock);
3109 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3110 mutex_unlock(&bp->hwrm_cmd_lock);
3111 return rc;
3112}
3113
Michael Chanc0c050c2015-10-22 16:01:17 -04003114static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3115{
3116 struct hwrm_func_drv_rgtr_input req = {0};
3117 int i;
Michael Chan25be8622016-04-05 14:09:00 -04003118 DECLARE_BITMAP(async_events_bmap, 256);
3119 u32 *events = (u32 *)async_events_bmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04003120
3121 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3122
3123 req.enables =
3124 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3125 FUNC_DRV_RGTR_REQ_ENABLES_VER |
3126 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3127
Michael Chan25be8622016-04-05 14:09:00 -04003128 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3129 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3130 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3131
3132 for (i = 0; i < 8; i++)
3133 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3134
Michael Chan11f15ed2016-04-05 14:08:55 -04003135 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003136 req.ver_maj = DRV_VER_MAJ;
3137 req.ver_min = DRV_VER_MIN;
3138 req.ver_upd = DRV_VER_UPD;
3139
3140 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003141 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003142 u32 *data = (u32 *)vf_req_snif_bmap;
3143
Michael Chande68f5de2015-12-09 19:35:41 -05003144 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003145 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3146 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3147
Michael Chande68f5de2015-12-09 19:35:41 -05003148 for (i = 0; i < 8; i++)
3149 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3150
Michael Chanc0c050c2015-10-22 16:01:17 -04003151 req.enables |=
3152 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3153 }
3154
3155 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3156}
3157
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003158static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3159{
3160 struct hwrm_func_drv_unrgtr_input req = {0};
3161
3162 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3163 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3164}
3165
Michael Chanc0c050c2015-10-22 16:01:17 -04003166static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3167{
3168 u32 rc = 0;
3169 struct hwrm_tunnel_dst_port_free_input req = {0};
3170
3171 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3172 req.tunnel_type = tunnel_type;
3173
3174 switch (tunnel_type) {
3175 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3176 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3177 break;
3178 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3179 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3180 break;
3181 default:
3182 break;
3183 }
3184
3185 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3186 if (rc)
3187 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3188 rc);
3189 return rc;
3190}
3191
3192static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3193 u8 tunnel_type)
3194{
3195 u32 rc = 0;
3196 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3197 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3198
3199 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3200
3201 req.tunnel_type = tunnel_type;
3202 req.tunnel_dst_port_val = port;
3203
3204 mutex_lock(&bp->hwrm_cmd_lock);
3205 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3206 if (rc) {
3207 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3208 rc);
3209 goto err_out;
3210 }
3211
3212 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
3213 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3214
3215 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
3216 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3217err_out:
3218 mutex_unlock(&bp->hwrm_cmd_lock);
3219 return rc;
3220}
3221
3222static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3223{
3224 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3225 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3226
3227 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003228 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003229
3230 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3231 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3232 req.mask = cpu_to_le32(vnic->rx_mask);
3233 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3234}
3235
3236#ifdef CONFIG_RFS_ACCEL
3237static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3238 struct bnxt_ntuple_filter *fltr)
3239{
3240 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3241
3242 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3243 req.ntuple_filter_id = fltr->filter_id;
3244 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3245}
3246
3247#define BNXT_NTP_FLTR_FLAGS \
3248 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3249 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3250 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3251 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3252 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3253 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3254 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3255 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3256 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3257 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3258 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3259 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3260 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003261 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003262
3263static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3264 struct bnxt_ntuple_filter *fltr)
3265{
3266 int rc = 0;
3267 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3268 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3269 bp->hwrm_cmd_resp_addr;
3270 struct flow_keys *keys = &fltr->fkeys;
3271 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3272
3273 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
Michael Chana54c4d72016-07-25 12:33:35 -04003274 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04003275
3276 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3277
3278 req.ethertype = htons(ETH_P_IP);
3279 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003280 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003281 req.ip_protocol = keys->basic.ip_proto;
3282
3283 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3284 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3285 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3286 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3287
3288 req.src_port = keys->ports.src;
3289 req.src_port_mask = cpu_to_be16(0xffff);
3290 req.dst_port = keys->ports.dst;
3291 req.dst_port_mask = cpu_to_be16(0xffff);
3292
Michael Chanc1935542015-12-27 18:19:28 -05003293 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003294 mutex_lock(&bp->hwrm_cmd_lock);
3295 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3296 if (!rc)
3297 fltr->filter_id = resp->ntuple_filter_id;
3298 mutex_unlock(&bp->hwrm_cmd_lock);
3299 return rc;
3300}
3301#endif
3302
3303static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3304 u8 *mac_addr)
3305{
3306 u32 rc = 0;
3307 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3308 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3309
3310 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003311 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3312 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3313 req.flags |=
3314 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003315 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003316 req.enables =
3317 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003318 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003319 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3320 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3321 req.l2_addr_mask[0] = 0xff;
3322 req.l2_addr_mask[1] = 0xff;
3323 req.l2_addr_mask[2] = 0xff;
3324 req.l2_addr_mask[3] = 0xff;
3325 req.l2_addr_mask[4] = 0xff;
3326 req.l2_addr_mask[5] = 0xff;
3327
3328 mutex_lock(&bp->hwrm_cmd_lock);
3329 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3330 if (!rc)
3331 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3332 resp->l2_filter_id;
3333 mutex_unlock(&bp->hwrm_cmd_lock);
3334 return rc;
3335}
3336
3337static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3338{
3339 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3340 int rc = 0;
3341
3342 /* Any associated ntuple filters will also be cleared by firmware. */
3343 mutex_lock(&bp->hwrm_cmd_lock);
3344 for (i = 0; i < num_of_vnics; i++) {
3345 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3346
3347 for (j = 0; j < vnic->uc_filter_count; j++) {
3348 struct hwrm_cfa_l2_filter_free_input req = {0};
3349
3350 bnxt_hwrm_cmd_hdr_init(bp, &req,
3351 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3352
3353 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3354
3355 rc = _hwrm_send_message(bp, &req, sizeof(req),
3356 HWRM_CMD_TIMEOUT);
3357 }
3358 vnic->uc_filter_count = 0;
3359 }
3360 mutex_unlock(&bp->hwrm_cmd_lock);
3361
3362 return rc;
3363}
3364
3365static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3366{
3367 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3368 struct hwrm_vnic_tpa_cfg_input req = {0};
3369
3370 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3371
3372 if (tpa_flags) {
3373 u16 mss = bp->dev->mtu - 40;
3374 u32 nsegs, n, segs = 0, flags;
3375
3376 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3377 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3378 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3379 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3380 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3381 if (tpa_flags & BNXT_FLAG_GRO)
3382 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3383
3384 req.flags = cpu_to_le32(flags);
3385
3386 req.enables =
3387 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003388 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3389 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003390
3391 /* Number of segs are log2 units, and first packet is not
3392 * included as part of this units.
3393 */
Michael Chan2839f282016-04-25 02:30:50 -04003394 if (mss <= BNXT_RX_PAGE_SIZE) {
3395 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003396 nsegs = (MAX_SKB_FRAGS - 1) * n;
3397 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003398 n = mss / BNXT_RX_PAGE_SIZE;
3399 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003400 n++;
3401 nsegs = (MAX_SKB_FRAGS - n) / n;
3402 }
3403
3404 segs = ilog2(nsegs);
3405 req.max_agg_segs = cpu_to_le16(segs);
3406 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003407
3408 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003409 }
3410 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3411
3412 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3413}
3414
3415static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3416{
3417 u32 i, j, max_rings;
3418 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3419 struct hwrm_vnic_rss_cfg_input req = {0};
3420
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003421 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003422 return 0;
3423
3424 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3425 if (set_rss) {
Michael Chan8d6be8b2016-09-19 03:58:00 -04003426 vnic->hash_type = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
3427 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
3428 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
3429 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
Michael Chanc0c050c2015-10-22 16:01:17 -04003430
3431 req.hash_type = cpu_to_le32(vnic->hash_type);
3432
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003433 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3434 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3435 max_rings = bp->rx_nr_rings - 1;
3436 else
3437 max_rings = bp->rx_nr_rings;
3438 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003439 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003440 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003441
3442 /* Fill the RSS indirection table with ring group ids */
3443 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3444 if (j == max_rings)
3445 j = 0;
3446 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3447 }
3448
3449 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3450 req.hash_key_tbl_addr =
3451 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3452 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003453 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003454 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3455}
3456
3457static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3458{
3459 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3460 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3461
3462 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3463 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3464 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3465 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3466 req.enables =
3467 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3468 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3469 /* thresholds not implemented in firmware yet */
3470 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3471 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3472 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3473 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3474}
3475
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003476static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3477 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003478{
3479 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3480
3481 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3482 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003483 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003484
3485 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003486 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003487}
3488
3489static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3490{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003491 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003492
3493 for (i = 0; i < bp->nr_vnics; i++) {
3494 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3495
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003496 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3497 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3498 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3499 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003500 }
3501 bp->rsscos_nr_ctxs = 0;
3502}
3503
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003504static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003505{
3506 int rc;
3507 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3508 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3509 bp->hwrm_cmd_resp_addr;
3510
3511 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3512 -1);
3513
3514 mutex_lock(&bp->hwrm_cmd_lock);
3515 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3516 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003517 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003518 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3519 mutex_unlock(&bp->hwrm_cmd_lock);
3520
3521 return rc;
3522}
3523
3524static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3525{
Michael Chanb81a90d2016-01-02 23:45:01 -05003526 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003527 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3528 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003529 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003530
3531 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003532
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003533 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3534 /* Only RSS support for now TBD: COS & LB */
3535 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3536 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3537 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3538 VNIC_CFG_REQ_ENABLES_MRU);
3539 } else {
3540 req.rss_rule = cpu_to_le16(0xffff);
3541 }
3542
3543 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3544 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003545 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3546 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3547 } else {
3548 req.cos_rule = cpu_to_le16(0xffff);
3549 }
3550
Michael Chanc0c050c2015-10-22 16:01:17 -04003551 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003552 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003553 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003554 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003555 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3556 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003557
Michael Chanb81a90d2016-01-02 23:45:01 -05003558 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003559 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3560 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3561
3562 req.lb_rule = cpu_to_le16(0xffff);
3563 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3564 VLAN_HLEN);
3565
Michael Chancf6645f2016-06-13 02:25:28 -04003566#ifdef CONFIG_BNXT_SRIOV
3567 if (BNXT_VF(bp))
3568 def_vlan = bp->vf.vlan;
3569#endif
3570 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003571 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3572
3573 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3574}
3575
3576static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3577{
3578 u32 rc = 0;
3579
3580 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3581 struct hwrm_vnic_free_input req = {0};
3582
3583 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3584 req.vnic_id =
3585 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3586
3587 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3588 if (rc)
3589 return rc;
3590 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3591 }
3592 return rc;
3593}
3594
3595static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3596{
3597 u16 i;
3598
3599 for (i = 0; i < bp->nr_vnics; i++)
3600 bnxt_hwrm_vnic_free_one(bp, i);
3601}
3602
Michael Chanb81a90d2016-01-02 23:45:01 -05003603static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3604 unsigned int start_rx_ring_idx,
3605 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003606{
Michael Chanb81a90d2016-01-02 23:45:01 -05003607 int rc = 0;
3608 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003609 struct hwrm_vnic_alloc_input req = {0};
3610 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3611
3612 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003613 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3614 grp_idx = bp->rx_ring[i].bnapi->index;
3615 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003616 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003617 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003618 break;
3619 }
3620 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003621 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003622 }
3623
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003624 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3625 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003626 if (vnic_id == 0)
3627 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3628
3629 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3630
3631 mutex_lock(&bp->hwrm_cmd_lock);
3632 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3633 if (!rc)
3634 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3635 mutex_unlock(&bp->hwrm_cmd_lock);
3636 return rc;
3637}
3638
3639static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3640{
3641 u16 i;
3642 u32 rc = 0;
3643
3644 mutex_lock(&bp->hwrm_cmd_lock);
3645 for (i = 0; i < bp->rx_nr_rings; i++) {
3646 struct hwrm_ring_grp_alloc_input req = {0};
3647 struct hwrm_ring_grp_alloc_output *resp =
3648 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003649 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003650
3651 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3652
Michael Chanb81a90d2016-01-02 23:45:01 -05003653 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3654 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3655 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3656 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003657
3658 rc = _hwrm_send_message(bp, &req, sizeof(req),
3659 HWRM_CMD_TIMEOUT);
3660 if (rc)
3661 break;
3662
Michael Chanb81a90d2016-01-02 23:45:01 -05003663 bp->grp_info[grp_idx].fw_grp_id =
3664 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003665 }
3666 mutex_unlock(&bp->hwrm_cmd_lock);
3667 return rc;
3668}
3669
3670static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3671{
3672 u16 i;
3673 u32 rc = 0;
3674 struct hwrm_ring_grp_free_input req = {0};
3675
3676 if (!bp->grp_info)
3677 return 0;
3678
3679 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3680
3681 mutex_lock(&bp->hwrm_cmd_lock);
3682 for (i = 0; i < bp->cp_nr_rings; i++) {
3683 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3684 continue;
3685 req.ring_group_id =
3686 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3687
3688 rc = _hwrm_send_message(bp, &req, sizeof(req),
3689 HWRM_CMD_TIMEOUT);
3690 if (rc)
3691 break;
3692 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3693 }
3694 mutex_unlock(&bp->hwrm_cmd_lock);
3695 return rc;
3696}
3697
3698static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3699 struct bnxt_ring_struct *ring,
3700 u32 ring_type, u32 map_index,
3701 u32 stats_ctx_id)
3702{
3703 int rc = 0, err = 0;
3704 struct hwrm_ring_alloc_input req = {0};
3705 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3706 u16 ring_id;
3707
3708 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3709
3710 req.enables = 0;
3711 if (ring->nr_pages > 1) {
3712 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3713 /* Page size is in log2 units */
3714 req.page_size = BNXT_PAGE_SHIFT;
3715 req.page_tbl_depth = 1;
3716 } else {
3717 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3718 }
3719 req.fbo = 0;
3720 /* Association of ring index with doorbell index and MSIX number */
3721 req.logical_id = cpu_to_le16(map_index);
3722
3723 switch (ring_type) {
3724 case HWRM_RING_ALLOC_TX:
3725 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3726 /* Association of transmit ring with completion ring */
3727 req.cmpl_ring_id =
3728 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3729 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3730 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3731 req.queue_id = cpu_to_le16(ring->queue_id);
3732 break;
3733 case HWRM_RING_ALLOC_RX:
3734 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3735 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3736 break;
3737 case HWRM_RING_ALLOC_AGG:
3738 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3739 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3740 break;
3741 case HWRM_RING_ALLOC_CMPL:
3742 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3743 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3744 if (bp->flags & BNXT_FLAG_USING_MSIX)
3745 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3746 break;
3747 default:
3748 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3749 ring_type);
3750 return -1;
3751 }
3752
3753 mutex_lock(&bp->hwrm_cmd_lock);
3754 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3755 err = le16_to_cpu(resp->error_code);
3756 ring_id = le16_to_cpu(resp->ring_id);
3757 mutex_unlock(&bp->hwrm_cmd_lock);
3758
3759 if (rc || err) {
3760 switch (ring_type) {
3761 case RING_FREE_REQ_RING_TYPE_CMPL:
3762 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3763 rc, err);
3764 return -1;
3765
3766 case RING_FREE_REQ_RING_TYPE_RX:
3767 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3768 rc, err);
3769 return -1;
3770
3771 case RING_FREE_REQ_RING_TYPE_TX:
3772 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3773 rc, err);
3774 return -1;
3775
3776 default:
3777 netdev_err(bp->dev, "Invalid ring\n");
3778 return -1;
3779 }
3780 }
3781 ring->fw_ring_id = ring_id;
3782 return rc;
3783}
3784
3785static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3786{
3787 int i, rc = 0;
3788
Michael Chanedd0c2c2015-12-27 18:19:19 -05003789 for (i = 0; i < bp->cp_nr_rings; i++) {
3790 struct bnxt_napi *bnapi = bp->bnapi[i];
3791 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3792 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003793
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003794 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003795 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3796 INVALID_STATS_CTX_ID);
3797 if (rc)
3798 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003799 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3800 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003801 }
3802
Michael Chanedd0c2c2015-12-27 18:19:19 -05003803 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003804 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003805 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003806 u32 map_idx = txr->bnapi->index;
3807 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003808
Michael Chanb81a90d2016-01-02 23:45:01 -05003809 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3810 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003811 if (rc)
3812 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003813 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003814 }
3815
Michael Chanedd0c2c2015-12-27 18:19:19 -05003816 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003817 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003818 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003819 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003820
Michael Chanb81a90d2016-01-02 23:45:01 -05003821 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3822 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003823 if (rc)
3824 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003825 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003826 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003827 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003828 }
3829
3830 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3831 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003832 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003833 struct bnxt_ring_struct *ring =
3834 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003835 u32 grp_idx = rxr->bnapi->index;
3836 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003837
3838 rc = hwrm_ring_alloc_send_msg(bp, ring,
3839 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003840 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003841 INVALID_STATS_CTX_ID);
3842 if (rc)
3843 goto err_out;
3844
Michael Chanb81a90d2016-01-02 23:45:01 -05003845 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003846 writel(DB_KEY_RX | rxr->rx_agg_prod,
3847 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003848 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003849 }
3850 }
3851err_out:
3852 return rc;
3853}
3854
3855static int hwrm_ring_free_send_msg(struct bnxt *bp,
3856 struct bnxt_ring_struct *ring,
3857 u32 ring_type, int cmpl_ring_id)
3858{
3859 int rc;
3860 struct hwrm_ring_free_input req = {0};
3861 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3862 u16 error_code;
3863
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003864 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003865 req.ring_type = ring_type;
3866 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3867
3868 mutex_lock(&bp->hwrm_cmd_lock);
3869 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3870 error_code = le16_to_cpu(resp->error_code);
3871 mutex_unlock(&bp->hwrm_cmd_lock);
3872
3873 if (rc || error_code) {
3874 switch (ring_type) {
3875 case RING_FREE_REQ_RING_TYPE_CMPL:
3876 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3877 rc);
3878 return rc;
3879 case RING_FREE_REQ_RING_TYPE_RX:
3880 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3881 rc);
3882 return rc;
3883 case RING_FREE_REQ_RING_TYPE_TX:
3884 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3885 rc);
3886 return rc;
3887 default:
3888 netdev_err(bp->dev, "Invalid ring\n");
3889 return -1;
3890 }
3891 }
3892 return 0;
3893}
3894
Michael Chanedd0c2c2015-12-27 18:19:19 -05003895static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003896{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003897 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003898
3899 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003900 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003901
Michael Chanedd0c2c2015-12-27 18:19:19 -05003902 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003903 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003904 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003905 u32 grp_idx = txr->bnapi->index;
3906 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003907
Michael Chanedd0c2c2015-12-27 18:19:19 -05003908 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3909 hwrm_ring_free_send_msg(bp, ring,
3910 RING_FREE_REQ_RING_TYPE_TX,
3911 close_path ? cmpl_ring_id :
3912 INVALID_HW_RING_ID);
3913 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003914 }
3915 }
3916
Michael Chanedd0c2c2015-12-27 18:19:19 -05003917 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003918 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003919 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003920 u32 grp_idx = rxr->bnapi->index;
3921 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003922
Michael Chanedd0c2c2015-12-27 18:19:19 -05003923 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3924 hwrm_ring_free_send_msg(bp, ring,
3925 RING_FREE_REQ_RING_TYPE_RX,
3926 close_path ? cmpl_ring_id :
3927 INVALID_HW_RING_ID);
3928 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003929 bp->grp_info[grp_idx].rx_fw_ring_id =
3930 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003931 }
3932 }
3933
Michael Chanedd0c2c2015-12-27 18:19:19 -05003934 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003935 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003936 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003937 u32 grp_idx = rxr->bnapi->index;
3938 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003939
Michael Chanedd0c2c2015-12-27 18:19:19 -05003940 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3941 hwrm_ring_free_send_msg(bp, ring,
3942 RING_FREE_REQ_RING_TYPE_RX,
3943 close_path ? cmpl_ring_id :
3944 INVALID_HW_RING_ID);
3945 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003946 bp->grp_info[grp_idx].agg_fw_ring_id =
3947 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003948 }
3949 }
3950
Michael Chanedd0c2c2015-12-27 18:19:19 -05003951 for (i = 0; i < bp->cp_nr_rings; i++) {
3952 struct bnxt_napi *bnapi = bp->bnapi[i];
3953 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3954 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003955
Michael Chanedd0c2c2015-12-27 18:19:19 -05003956 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3957 hwrm_ring_free_send_msg(bp, ring,
3958 RING_FREE_REQ_RING_TYPE_CMPL,
3959 INVALID_HW_RING_ID);
3960 ring->fw_ring_id = INVALID_HW_RING_ID;
3961 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003962 }
3963 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003964}
3965
Michael Chanbb053f52016-02-26 04:00:02 -05003966static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3967 u32 buf_tmrs, u16 flags,
3968 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3969{
3970 req->flags = cpu_to_le16(flags);
3971 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3972 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3973 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3974 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3975 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3976 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3977 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3978 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3979}
3980
Michael Chanc0c050c2015-10-22 16:01:17 -04003981int bnxt_hwrm_set_coal(struct bnxt *bp)
3982{
3983 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05003984 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3985 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04003986 u16 max_buf, max_buf_irq;
3987 u16 buf_tmr, buf_tmr_irq;
3988 u32 flags;
3989
Michael Chandfc9c942016-02-26 04:00:03 -05003990 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3991 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3992 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3993 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003994
Michael Chandfb5b892016-02-26 04:00:01 -05003995 /* Each rx completion (2 records) should be DMAed immediately.
3996 * DMA 1/4 of the completion buffers at a time.
3997 */
3998 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04003999 /* max_buf must not be zero */
4000 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05004001 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4002 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4003 /* buf timer set to 1/4 of interrupt timer */
4004 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4005 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4006 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004007
4008 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4009
4010 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4011 * if coal_ticks is less than 25 us.
4012 */
Michael Chandfb5b892016-02-26 04:00:01 -05004013 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04004014 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4015
Michael Chanbb053f52016-02-26 04:00:02 -05004016 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05004017 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4018
4019 /* max_buf must not be zero */
4020 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4021 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4022 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4023 /* buf timer set to 1/4 of interrupt timer */
4024 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4025 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4026 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4027
4028 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4029 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4030 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04004031
4032 mutex_lock(&bp->hwrm_cmd_lock);
4033 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004034 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004035
Michael Chandfc9c942016-02-26 04:00:03 -05004036 req = &req_rx;
4037 if (!bnapi->rx_ring)
4038 req = &req_tx;
4039 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4040
4041 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004042 HWRM_CMD_TIMEOUT);
4043 if (rc)
4044 break;
4045 }
4046 mutex_unlock(&bp->hwrm_cmd_lock);
4047 return rc;
4048}
4049
4050static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4051{
4052 int rc = 0, i;
4053 struct hwrm_stat_ctx_free_input req = {0};
4054
4055 if (!bp->bnapi)
4056 return 0;
4057
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004058 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4059 return 0;
4060
Michael Chanc0c050c2015-10-22 16:01:17 -04004061 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4062
4063 mutex_lock(&bp->hwrm_cmd_lock);
4064 for (i = 0; i < bp->cp_nr_rings; i++) {
4065 struct bnxt_napi *bnapi = bp->bnapi[i];
4066 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4067
4068 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4069 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4070
4071 rc = _hwrm_send_message(bp, &req, sizeof(req),
4072 HWRM_CMD_TIMEOUT);
4073 if (rc)
4074 break;
4075
4076 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4077 }
4078 }
4079 mutex_unlock(&bp->hwrm_cmd_lock);
4080 return rc;
4081}
4082
4083static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4084{
4085 int rc = 0, i;
4086 struct hwrm_stat_ctx_alloc_input req = {0};
4087 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4088
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004089 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4090 return 0;
4091
Michael Chanc0c050c2015-10-22 16:01:17 -04004092 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4093
Michael Chan51f30782016-07-01 18:46:29 -04004094 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004095
4096 mutex_lock(&bp->hwrm_cmd_lock);
4097 for (i = 0; i < bp->cp_nr_rings; i++) {
4098 struct bnxt_napi *bnapi = bp->bnapi[i];
4099 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4100
4101 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4102
4103 rc = _hwrm_send_message(bp, &req, sizeof(req),
4104 HWRM_CMD_TIMEOUT);
4105 if (rc)
4106 break;
4107
4108 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4109
4110 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4111 }
4112 mutex_unlock(&bp->hwrm_cmd_lock);
4113 return 0;
4114}
4115
Michael Chancf6645f2016-06-13 02:25:28 -04004116static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4117{
4118 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004119 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004120 int rc;
4121
4122 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4123 req.fid = cpu_to_le16(0xffff);
4124 mutex_lock(&bp->hwrm_cmd_lock);
4125 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4126 if (rc)
4127 goto func_qcfg_exit;
4128
4129#ifdef CONFIG_BNXT_SRIOV
4130 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004131 struct bnxt_vf_info *vf = &bp->vf;
4132
4133 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4134 }
4135#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004136 switch (resp->port_partition_type) {
4137 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4138 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4139 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4140 bp->port_partition_type = resp->port_partition_type;
4141 break;
4142 }
Michael Chancf6645f2016-06-13 02:25:28 -04004143
4144func_qcfg_exit:
4145 mutex_unlock(&bp->hwrm_cmd_lock);
4146 return rc;
4147}
4148
Michael Chan4a21b492015-12-27 18:19:26 -05004149int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004150{
4151 int rc = 0;
4152 struct hwrm_func_qcaps_input req = {0};
4153 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4154
4155 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4156 req.fid = cpu_to_le16(0xffff);
4157
4158 mutex_lock(&bp->hwrm_cmd_lock);
4159 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4160 if (rc)
4161 goto hwrm_func_qcaps_exit;
4162
4163 if (BNXT_PF(bp)) {
4164 struct bnxt_pf_info *pf = &bp->pf;
4165
4166 pf->fw_fid = le16_to_cpu(resp->fid);
4167 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004168 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004169 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004170 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004171 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4172 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4173 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004174 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004175 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4176 if (!pf->max_hw_ring_grps)
4177 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004178 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4179 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4180 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4181 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4182 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4183 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4184 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4185 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4186 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4187 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4188 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4189 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004190#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004191 struct bnxt_vf_info *vf = &bp->vf;
4192
4193 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan11f15ed2016-04-05 14:08:55 -04004194 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004195 if (is_valid_ether_addr(vf->mac_addr))
4196 /* overwrite netdev dev_adr with admin VF MAC */
4197 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
4198 else
4199 random_ether_addr(bp->dev->dev_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04004200
4201 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4202 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4203 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4204 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004205 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4206 if (!vf->max_hw_ring_grps)
4207 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004208 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4209 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4210 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan379a80a2015-10-23 15:06:19 -04004211#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004212 }
4213
4214 bp->tx_push_thresh = 0;
4215 if (resp->flags &
4216 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4217 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4218
4219hwrm_func_qcaps_exit:
4220 mutex_unlock(&bp->hwrm_cmd_lock);
4221 return rc;
4222}
4223
4224static int bnxt_hwrm_func_reset(struct bnxt *bp)
4225{
4226 struct hwrm_func_reset_input req = {0};
4227
4228 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4229 req.enables = 0;
4230
4231 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4232}
4233
4234static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4235{
4236 int rc = 0;
4237 struct hwrm_queue_qportcfg_input req = {0};
4238 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4239 u8 i, *qptr;
4240
4241 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4242
4243 mutex_lock(&bp->hwrm_cmd_lock);
4244 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4245 if (rc)
4246 goto qportcfg_exit;
4247
4248 if (!resp->max_configurable_queues) {
4249 rc = -EINVAL;
4250 goto qportcfg_exit;
4251 }
4252 bp->max_tc = resp->max_configurable_queues;
4253 if (bp->max_tc > BNXT_MAX_QUEUE)
4254 bp->max_tc = BNXT_MAX_QUEUE;
4255
Michael Chan441cabb2016-09-19 03:58:02 -04004256 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4257 bp->max_tc = 1;
4258
Michael Chanc0c050c2015-10-22 16:01:17 -04004259 qptr = &resp->queue_id0;
4260 for (i = 0; i < bp->max_tc; i++) {
4261 bp->q_info[i].queue_id = *qptr++;
4262 bp->q_info[i].queue_profile = *qptr++;
4263 }
4264
4265qportcfg_exit:
4266 mutex_unlock(&bp->hwrm_cmd_lock);
4267 return rc;
4268}
4269
4270static int bnxt_hwrm_ver_get(struct bnxt *bp)
4271{
4272 int rc;
4273 struct hwrm_ver_get_input req = {0};
4274 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4275
Michael Chane6ef2692016-03-28 19:46:05 -04004276 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004277 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4278 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4279 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4280 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4281 mutex_lock(&bp->hwrm_cmd_lock);
4282 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4283 if (rc)
4284 goto hwrm_ver_get_exit;
4285
4286 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4287
Michael Chan11f15ed2016-04-05 14:08:55 -04004288 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4289 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004290 if (resp->hwrm_intf_maj < 1) {
4291 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004292 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004293 resp->hwrm_intf_upd);
4294 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004295 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004296 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004297 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4298 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4299
Michael Chanff4fe812016-02-26 04:00:04 -05004300 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4301 if (!bp->hwrm_cmd_timeout)
4302 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4303
Michael Chane6ef2692016-03-28 19:46:05 -04004304 if (resp->hwrm_intf_maj >= 1)
4305 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4306
Michael Chan659c8052016-06-13 02:25:33 -04004307 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004308 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4309 !resp->chip_metal)
4310 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004311
Michael Chanc0c050c2015-10-22 16:01:17 -04004312hwrm_ver_get_exit:
4313 mutex_unlock(&bp->hwrm_cmd_lock);
4314 return rc;
4315}
4316
Michael Chan3bdf56c2016-03-07 15:38:45 -05004317static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4318{
4319 int rc;
4320 struct bnxt_pf_info *pf = &bp->pf;
4321 struct hwrm_port_qstats_input req = {0};
4322
4323 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4324 return 0;
4325
4326 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4327 req.port_id = cpu_to_le16(pf->port_id);
4328 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4329 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4330 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4331 return rc;
4332}
4333
Michael Chanc0c050c2015-10-22 16:01:17 -04004334static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4335{
4336 if (bp->vxlan_port_cnt) {
4337 bnxt_hwrm_tunnel_dst_port_free(
4338 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4339 }
4340 bp->vxlan_port_cnt = 0;
4341 if (bp->nge_port_cnt) {
4342 bnxt_hwrm_tunnel_dst_port_free(
4343 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4344 }
4345 bp->nge_port_cnt = 0;
4346}
4347
4348static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4349{
4350 int rc, i;
4351 u32 tpa_flags = 0;
4352
4353 if (set_tpa)
4354 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4355 for (i = 0; i < bp->nr_vnics; i++) {
4356 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4357 if (rc) {
4358 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4359 rc, i);
4360 return rc;
4361 }
4362 }
4363 return 0;
4364}
4365
4366static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4367{
4368 int i;
4369
4370 for (i = 0; i < bp->nr_vnics; i++)
4371 bnxt_hwrm_vnic_set_rss(bp, i, false);
4372}
4373
4374static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4375 bool irq_re_init)
4376{
4377 if (bp->vnic_info) {
4378 bnxt_hwrm_clear_vnic_filter(bp);
4379 /* clear all RSS setting before free vnic ctx */
4380 bnxt_hwrm_clear_vnic_rss(bp);
4381 bnxt_hwrm_vnic_ctx_free(bp);
4382 /* before free the vnic, undo the vnic tpa settings */
4383 if (bp->flags & BNXT_FLAG_TPA)
4384 bnxt_set_tpa(bp, false);
4385 bnxt_hwrm_vnic_free(bp);
4386 }
4387 bnxt_hwrm_ring_free(bp, close_path);
4388 bnxt_hwrm_ring_grp_free(bp);
4389 if (irq_re_init) {
4390 bnxt_hwrm_stat_ctx_free(bp);
4391 bnxt_hwrm_free_tunnel_ports(bp);
4392 }
4393}
4394
4395static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4396{
4397 int rc;
4398
4399 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004400 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004401 if (rc) {
4402 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4403 vnic_id, rc);
4404 goto vnic_setup_err;
4405 }
4406 bp->rsscos_nr_ctxs++;
4407
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004408 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4409 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4410 if (rc) {
4411 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4412 vnic_id, rc);
4413 goto vnic_setup_err;
4414 }
4415 bp->rsscos_nr_ctxs++;
4416 }
4417
Michael Chanc0c050c2015-10-22 16:01:17 -04004418 /* configure default vnic, ring grp */
4419 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4420 if (rc) {
4421 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4422 vnic_id, rc);
4423 goto vnic_setup_err;
4424 }
4425
4426 /* Enable RSS hashing on vnic */
4427 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4428 if (rc) {
4429 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4430 vnic_id, rc);
4431 goto vnic_setup_err;
4432 }
4433
4434 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4435 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4436 if (rc) {
4437 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4438 vnic_id, rc);
4439 }
4440 }
4441
4442vnic_setup_err:
4443 return rc;
4444}
4445
4446static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4447{
4448#ifdef CONFIG_RFS_ACCEL
4449 int i, rc = 0;
4450
4451 for (i = 0; i < bp->rx_nr_rings; i++) {
4452 u16 vnic_id = i + 1;
4453 u16 ring_id = i;
4454
4455 if (vnic_id >= bp->nr_vnics)
4456 break;
4457
4458 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004459 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004460 if (rc) {
4461 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4462 vnic_id, rc);
4463 break;
4464 }
4465 rc = bnxt_setup_vnic(bp, vnic_id);
4466 if (rc)
4467 break;
4468 }
4469 return rc;
4470#else
4471 return 0;
4472#endif
4473}
4474
Michael Chan17c71ac2016-07-01 18:46:27 -04004475/* Allow PF and VF with default VLAN to be in promiscuous mode */
4476static bool bnxt_promisc_ok(struct bnxt *bp)
4477{
4478#ifdef CONFIG_BNXT_SRIOV
4479 if (BNXT_VF(bp) && !bp->vf.vlan)
4480 return false;
4481#endif
4482 return true;
4483}
4484
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004485static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4486{
4487 unsigned int rc = 0;
4488
4489 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4490 if (rc) {
4491 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4492 rc);
4493 return rc;
4494 }
4495
4496 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4497 if (rc) {
4498 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4499 rc);
4500 return rc;
4501 }
4502 return rc;
4503}
4504
Michael Chanb664f002015-12-02 01:54:08 -05004505static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004506static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004507
Michael Chanc0c050c2015-10-22 16:01:17 -04004508static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4509{
Michael Chan7d2837d2016-05-04 16:56:44 -04004510 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004511 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004512 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004513
4514 if (irq_re_init) {
4515 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4516 if (rc) {
4517 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4518 rc);
4519 goto err_out;
4520 }
4521 }
4522
4523 rc = bnxt_hwrm_ring_alloc(bp);
4524 if (rc) {
4525 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4526 goto err_out;
4527 }
4528
4529 rc = bnxt_hwrm_ring_grp_alloc(bp);
4530 if (rc) {
4531 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4532 goto err_out;
4533 }
4534
Prashant Sreedharan76595192016-07-18 07:15:22 -04004535 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4536 rx_nr_rings--;
4537
Michael Chanc0c050c2015-10-22 16:01:17 -04004538 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004539 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004540 if (rc) {
4541 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4542 goto err_out;
4543 }
4544
4545 rc = bnxt_setup_vnic(bp, 0);
4546 if (rc)
4547 goto err_out;
4548
4549 if (bp->flags & BNXT_FLAG_RFS) {
4550 rc = bnxt_alloc_rfs_vnics(bp);
4551 if (rc)
4552 goto err_out;
4553 }
4554
4555 if (bp->flags & BNXT_FLAG_TPA) {
4556 rc = bnxt_set_tpa(bp, true);
4557 if (rc)
4558 goto err_out;
4559 }
4560
4561 if (BNXT_VF(bp))
4562 bnxt_update_vf_mac(bp);
4563
4564 /* Filter for default vnic 0 */
4565 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4566 if (rc) {
4567 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4568 goto err_out;
4569 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004570 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004571
Michael Chan7d2837d2016-05-04 16:56:44 -04004572 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004573
Michael Chan17c71ac2016-07-01 18:46:27 -04004574 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004575 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4576
4577 if (bp->dev->flags & IFF_ALLMULTI) {
4578 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4579 vnic->mc_list_count = 0;
4580 } else {
4581 u32 mask = 0;
4582
4583 bnxt_mc_list_updated(bp, &mask);
4584 vnic->rx_mask |= mask;
4585 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004586
Michael Chanb664f002015-12-02 01:54:08 -05004587 rc = bnxt_cfg_rx_mode(bp);
4588 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004589 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004590
4591 rc = bnxt_hwrm_set_coal(bp);
4592 if (rc)
4593 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004594 rc);
4595
4596 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4597 rc = bnxt_setup_nitroa0_vnic(bp);
4598 if (rc)
4599 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4600 rc);
4601 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004602
Michael Chancf6645f2016-06-13 02:25:28 -04004603 if (BNXT_VF(bp)) {
4604 bnxt_hwrm_func_qcfg(bp);
4605 netdev_update_features(bp->dev);
4606 }
4607
Michael Chanc0c050c2015-10-22 16:01:17 -04004608 return 0;
4609
4610err_out:
4611 bnxt_hwrm_resource_free(bp, 0, true);
4612
4613 return rc;
4614}
4615
4616static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4617{
4618 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4619 return 0;
4620}
4621
4622static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4623{
4624 bnxt_init_rx_rings(bp);
4625 bnxt_init_tx_rings(bp);
4626 bnxt_init_ring_grps(bp, irq_re_init);
4627 bnxt_init_vnics(bp);
4628
4629 return bnxt_init_chip(bp, irq_re_init);
4630}
4631
4632static void bnxt_disable_int(struct bnxt *bp)
4633{
4634 int i;
4635
4636 if (!bp->bnapi)
4637 return;
4638
4639 for (i = 0; i < bp->cp_nr_rings; i++) {
4640 struct bnxt_napi *bnapi = bp->bnapi[i];
4641 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4642
4643 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4644 }
4645}
4646
4647static void bnxt_enable_int(struct bnxt *bp)
4648{
4649 int i;
4650
4651 atomic_set(&bp->intr_sem, 0);
4652 for (i = 0; i < bp->cp_nr_rings; i++) {
4653 struct bnxt_napi *bnapi = bp->bnapi[i];
4654 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4655
4656 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4657 }
4658}
4659
4660static int bnxt_set_real_num_queues(struct bnxt *bp)
4661{
4662 int rc;
4663 struct net_device *dev = bp->dev;
4664
4665 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4666 if (rc)
4667 return rc;
4668
4669 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4670 if (rc)
4671 return rc;
4672
4673#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004674 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004675 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004676#endif
4677
4678 return rc;
4679}
4680
Michael Chan6e6c5a52016-01-02 23:45:02 -05004681static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4682 bool shared)
4683{
4684 int _rx = *rx, _tx = *tx;
4685
4686 if (shared) {
4687 *rx = min_t(int, _rx, max);
4688 *tx = min_t(int, _tx, max);
4689 } else {
4690 if (max < 2)
4691 return -ENOMEM;
4692
4693 while (_rx + _tx > max) {
4694 if (_rx > _tx && _rx > 1)
4695 _rx--;
4696 else if (_tx > 1)
4697 _tx--;
4698 }
4699 *rx = _rx;
4700 *tx = _tx;
4701 }
4702 return 0;
4703}
4704
Michael Chanc0c050c2015-10-22 16:01:17 -04004705static int bnxt_setup_msix(struct bnxt *bp)
4706{
4707 struct msix_entry *msix_ent;
4708 struct net_device *dev = bp->dev;
Michael Chan01657bc2016-01-02 23:45:03 -05004709 int i, total_vecs, rc = 0, min = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004710 const int len = sizeof(bp->irq_tbl[0].name);
4711
4712 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4713 total_vecs = bp->cp_nr_rings;
4714
4715 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4716 if (!msix_ent)
4717 return -ENOMEM;
4718
4719 for (i = 0; i < total_vecs; i++) {
4720 msix_ent[i].entry = i;
4721 msix_ent[i].vector = 0;
4722 }
4723
Michael Chan01657bc2016-01-02 23:45:03 -05004724 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4725 min = 2;
4726
4727 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004728 if (total_vecs < 0) {
4729 rc = -ENODEV;
4730 goto msix_setup_exit;
4731 }
4732
4733 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4734 if (bp->irq_tbl) {
4735 int tcs;
4736
4737 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004738 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004739 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004740 if (rc)
4741 goto msix_setup_exit;
4742
Michael Chanc0c050c2015-10-22 16:01:17 -04004743 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4744 tcs = netdev_get_num_tc(dev);
4745 if (tcs > 1) {
4746 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4747 if (bp->tx_nr_rings_per_tc == 0) {
4748 netdev_reset_tc(dev);
4749 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4750 } else {
4751 int i, off, count;
4752
4753 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4754 for (i = 0; i < tcs; i++) {
4755 count = bp->tx_nr_rings_per_tc;
4756 off = i * count;
4757 netdev_set_tc_queue(dev, i, count, off);
4758 }
4759 }
4760 }
Michael Chan01657bc2016-01-02 23:45:03 -05004761 bp->cp_nr_rings = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004762
4763 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chan01657bc2016-01-02 23:45:03 -05004764 char *attr;
4765
Michael Chanc0c050c2015-10-22 16:01:17 -04004766 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chan01657bc2016-01-02 23:45:03 -05004767 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4768 attr = "TxRx";
4769 else if (i < bp->rx_nr_rings)
4770 attr = "rx";
4771 else
4772 attr = "tx";
4773
Michael Chanc0c050c2015-10-22 16:01:17 -04004774 snprintf(bp->irq_tbl[i].name, len,
Michael Chan01657bc2016-01-02 23:45:03 -05004775 "%s-%s-%d", dev->name, attr, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004776 bp->irq_tbl[i].handler = bnxt_msix;
4777 }
4778 rc = bnxt_set_real_num_queues(bp);
4779 if (rc)
4780 goto msix_setup_exit;
4781 } else {
4782 rc = -ENOMEM;
4783 goto msix_setup_exit;
4784 }
4785 bp->flags |= BNXT_FLAG_USING_MSIX;
4786 kfree(msix_ent);
4787 return 0;
4788
4789msix_setup_exit:
4790 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4791 pci_disable_msix(bp->pdev);
4792 kfree(msix_ent);
4793 return rc;
4794}
4795
4796static int bnxt_setup_inta(struct bnxt *bp)
4797{
4798 int rc;
4799 const int len = sizeof(bp->irq_tbl[0].name);
4800
4801 if (netdev_get_num_tc(bp->dev))
4802 netdev_reset_tc(bp->dev);
4803
4804 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4805 if (!bp->irq_tbl) {
4806 rc = -ENOMEM;
4807 return rc;
4808 }
4809 bp->rx_nr_rings = 1;
4810 bp->tx_nr_rings = 1;
4811 bp->cp_nr_rings = 1;
4812 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004813 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004814 bp->irq_tbl[0].vector = bp->pdev->irq;
4815 snprintf(bp->irq_tbl[0].name, len,
4816 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4817 bp->irq_tbl[0].handler = bnxt_inta;
4818 rc = bnxt_set_real_num_queues(bp);
4819 return rc;
4820}
4821
4822static int bnxt_setup_int_mode(struct bnxt *bp)
4823{
4824 int rc = 0;
4825
4826 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4827 rc = bnxt_setup_msix(bp);
4828
Michael Chan1fa72e22016-04-25 02:30:49 -04004829 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004830 /* fallback to INTA */
4831 rc = bnxt_setup_inta(bp);
4832 }
4833 return rc;
4834}
4835
4836static void bnxt_free_irq(struct bnxt *bp)
4837{
4838 struct bnxt_irq *irq;
4839 int i;
4840
4841#ifdef CONFIG_RFS_ACCEL
4842 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4843 bp->dev->rx_cpu_rmap = NULL;
4844#endif
4845 if (!bp->irq_tbl)
4846 return;
4847
4848 for (i = 0; i < bp->cp_nr_rings; i++) {
4849 irq = &bp->irq_tbl[i];
4850 if (irq->requested)
4851 free_irq(irq->vector, bp->bnapi[i]);
4852 irq->requested = 0;
4853 }
4854 if (bp->flags & BNXT_FLAG_USING_MSIX)
4855 pci_disable_msix(bp->pdev);
4856 kfree(bp->irq_tbl);
4857 bp->irq_tbl = NULL;
4858}
4859
4860static int bnxt_request_irq(struct bnxt *bp)
4861{
Michael Chanb81a90d2016-01-02 23:45:01 -05004862 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004863 unsigned long flags = 0;
4864#ifdef CONFIG_RFS_ACCEL
4865 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4866#endif
4867
4868 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4869 flags = IRQF_SHARED;
4870
Michael Chanb81a90d2016-01-02 23:45:01 -05004871 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004872 struct bnxt_irq *irq = &bp->irq_tbl[i];
4873#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05004874 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004875 rc = irq_cpu_rmap_add(rmap, irq->vector);
4876 if (rc)
4877 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004878 j);
4879 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04004880 }
4881#endif
4882 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4883 bp->bnapi[i]);
4884 if (rc)
4885 break;
4886
4887 irq->requested = 1;
4888 }
4889 return rc;
4890}
4891
4892static void bnxt_del_napi(struct bnxt *bp)
4893{
4894 int i;
4895
4896 if (!bp->bnapi)
4897 return;
4898
4899 for (i = 0; i < bp->cp_nr_rings; i++) {
4900 struct bnxt_napi *bnapi = bp->bnapi[i];
4901
4902 napi_hash_del(&bnapi->napi);
4903 netif_napi_del(&bnapi->napi);
4904 }
4905}
4906
4907static void bnxt_init_napi(struct bnxt *bp)
4908{
4909 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004910 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004911 struct bnxt_napi *bnapi;
4912
4913 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004914 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4915 cp_nr_rings--;
4916 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004917 bnapi = bp->bnapi[i];
4918 netif_napi_add(bp->dev, &bnapi->napi,
4919 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004920 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004921 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4922 bnapi = bp->bnapi[cp_nr_rings];
4923 netif_napi_add(bp->dev, &bnapi->napi,
4924 bnxt_poll_nitroa0, 64);
4925 napi_hash_add(&bnapi->napi);
4926 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004927 } else {
4928 bnapi = bp->bnapi[0];
4929 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004930 }
4931}
4932
4933static void bnxt_disable_napi(struct bnxt *bp)
4934{
4935 int i;
4936
4937 if (!bp->bnapi)
4938 return;
4939
4940 for (i = 0; i < bp->cp_nr_rings; i++) {
4941 napi_disable(&bp->bnapi[i]->napi);
4942 bnxt_disable_poll(bp->bnapi[i]);
4943 }
4944}
4945
4946static void bnxt_enable_napi(struct bnxt *bp)
4947{
4948 int i;
4949
4950 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04004951 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04004952 bnxt_enable_poll(bp->bnapi[i]);
4953 napi_enable(&bp->bnapi[i]->napi);
4954 }
4955}
4956
4957static void bnxt_tx_disable(struct bnxt *bp)
4958{
4959 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004960 struct bnxt_tx_ring_info *txr;
4961 struct netdev_queue *txq;
4962
Michael Chanb6ab4b02016-01-02 23:44:59 -05004963 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004964 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004965 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004966 txq = netdev_get_tx_queue(bp->dev, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004967 txr->dev_state = BNXT_DEV_STATE_CLOSING;
Michael Chanc0c050c2015-10-22 16:01:17 -04004968 }
4969 }
4970 /* Stop all TX queues */
4971 netif_tx_disable(bp->dev);
4972 netif_carrier_off(bp->dev);
4973}
4974
4975static void bnxt_tx_enable(struct bnxt *bp)
4976{
4977 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004978 struct bnxt_tx_ring_info *txr;
4979 struct netdev_queue *txq;
4980
4981 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004982 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004983 txq = netdev_get_tx_queue(bp->dev, i);
4984 txr->dev_state = 0;
4985 }
4986 netif_tx_wake_all_queues(bp->dev);
4987 if (bp->link_info.link_up)
4988 netif_carrier_on(bp->dev);
4989}
4990
4991static void bnxt_report_link(struct bnxt *bp)
4992{
4993 if (bp->link_info.link_up) {
4994 const char *duplex;
4995 const char *flow_ctrl;
4996 u16 speed;
4997
4998 netif_carrier_on(bp->dev);
4999 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5000 duplex = "full";
5001 else
5002 duplex = "half";
5003 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5004 flow_ctrl = "ON - receive & transmit";
5005 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5006 flow_ctrl = "ON - transmit";
5007 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5008 flow_ctrl = "ON - receive";
5009 else
5010 flow_ctrl = "none";
5011 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5012 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5013 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04005014 if (bp->flags & BNXT_FLAG_EEE_CAP)
5015 netdev_info(bp->dev, "EEE is %s\n",
5016 bp->eee.eee_active ? "active" :
5017 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04005018 } else {
5019 netif_carrier_off(bp->dev);
5020 netdev_err(bp->dev, "NIC Link is Down\n");
5021 }
5022}
5023
Michael Chan170ce012016-04-05 14:08:57 -04005024static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5025{
5026 int rc = 0;
5027 struct hwrm_port_phy_qcaps_input req = {0};
5028 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04005029 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04005030
5031 if (bp->hwrm_spec_code < 0x10201)
5032 return 0;
5033
5034 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5035
5036 mutex_lock(&bp->hwrm_cmd_lock);
5037 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5038 if (rc)
5039 goto hwrm_phy_qcaps_exit;
5040
5041 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5042 struct ethtool_eee *eee = &bp->eee;
5043 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5044
5045 bp->flags |= BNXT_FLAG_EEE_CAP;
5046 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5047 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5048 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5049 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5050 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5051 }
Michael Chan93ed8112016-06-13 02:25:37 -04005052 link_info->support_auto_speeds =
5053 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005054
5055hwrm_phy_qcaps_exit:
5056 mutex_unlock(&bp->hwrm_cmd_lock);
5057 return rc;
5058}
5059
Michael Chanc0c050c2015-10-22 16:01:17 -04005060static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5061{
5062 int rc = 0;
5063 struct bnxt_link_info *link_info = &bp->link_info;
5064 struct hwrm_port_phy_qcfg_input req = {0};
5065 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5066 u8 link_up = link_info->link_up;
5067
5068 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5069
5070 mutex_lock(&bp->hwrm_cmd_lock);
5071 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5072 if (rc) {
5073 mutex_unlock(&bp->hwrm_cmd_lock);
5074 return rc;
5075 }
5076
5077 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5078 link_info->phy_link_status = resp->link;
5079 link_info->duplex = resp->duplex;
5080 link_info->pause = resp->pause;
5081 link_info->auto_mode = resp->auto_mode;
5082 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005083 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005084 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005085 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005086 if (link_info->phy_link_status == BNXT_LINK_LINK)
5087 link_info->link_speed = le16_to_cpu(resp->link_speed);
5088 else
5089 link_info->link_speed = 0;
5090 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005091 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5092 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005093 link_info->lp_auto_link_speeds =
5094 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005095 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5096 link_info->phy_ver[0] = resp->phy_maj;
5097 link_info->phy_ver[1] = resp->phy_min;
5098 link_info->phy_ver[2] = resp->phy_bld;
5099 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005100 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005101 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005102 link_info->phy_addr = resp->eee_config_phy_addr &
5103 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005104 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005105
Michael Chan170ce012016-04-05 14:08:57 -04005106 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5107 struct ethtool_eee *eee = &bp->eee;
5108 u16 fw_speeds;
5109
5110 eee->eee_active = 0;
5111 if (resp->eee_config_phy_addr &
5112 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5113 eee->eee_active = 1;
5114 fw_speeds = le16_to_cpu(
5115 resp->link_partner_adv_eee_link_speed_mask);
5116 eee->lp_advertised =
5117 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5118 }
5119
5120 /* Pull initial EEE config */
5121 if (!chng_link_state) {
5122 if (resp->eee_config_phy_addr &
5123 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5124 eee->eee_enabled = 1;
5125
5126 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5127 eee->advertised =
5128 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5129
5130 if (resp->eee_config_phy_addr &
5131 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5132 __le32 tmr;
5133
5134 eee->tx_lpi_enabled = 1;
5135 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5136 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5137 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5138 }
5139 }
5140 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005141 /* TODO: need to add more logic to report VF link */
5142 if (chng_link_state) {
5143 if (link_info->phy_link_status == BNXT_LINK_LINK)
5144 link_info->link_up = 1;
5145 else
5146 link_info->link_up = 0;
5147 if (link_up != link_info->link_up)
5148 bnxt_report_link(bp);
5149 } else {
5150 /* alwasy link down if not require to update link state */
5151 link_info->link_up = 0;
5152 }
5153 mutex_unlock(&bp->hwrm_cmd_lock);
5154 return 0;
5155}
5156
Michael Chan10289be2016-05-15 03:04:49 -04005157static void bnxt_get_port_module_status(struct bnxt *bp)
5158{
5159 struct bnxt_link_info *link_info = &bp->link_info;
5160 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5161 u8 module_status;
5162
5163 if (bnxt_update_link(bp, true))
5164 return;
5165
5166 module_status = link_info->module_status;
5167 switch (module_status) {
5168 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5169 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5170 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5171 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5172 bp->pf.port_id);
5173 if (bp->hwrm_spec_code >= 0x10201) {
5174 netdev_warn(bp->dev, "Module part number %s\n",
5175 resp->phy_vendor_partnumber);
5176 }
5177 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5178 netdev_warn(bp->dev, "TX is disabled\n");
5179 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5180 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5181 }
5182}
5183
Michael Chanc0c050c2015-10-22 16:01:17 -04005184static void
5185bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5186{
5187 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005188 if (bp->hwrm_spec_code >= 0x10201)
5189 req->auto_pause =
5190 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005191 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5192 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5193 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005194 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005195 req->enables |=
5196 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5197 } else {
5198 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5199 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5200 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5201 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5202 req->enables |=
5203 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005204 if (bp->hwrm_spec_code >= 0x10201) {
5205 req->auto_pause = req->force_pause;
5206 req->enables |= cpu_to_le32(
5207 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5208 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005209 }
5210}
5211
5212static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5213 struct hwrm_port_phy_cfg_input *req)
5214{
5215 u8 autoneg = bp->link_info.autoneg;
5216 u16 fw_link_speed = bp->link_info.req_link_speed;
5217 u32 advertising = bp->link_info.advertising;
5218
5219 if (autoneg & BNXT_AUTONEG_SPEED) {
5220 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005221 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005222
5223 req->enables |= cpu_to_le32(
5224 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5225 req->auto_link_speed_mask = cpu_to_le16(advertising);
5226
5227 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5228 req->flags |=
5229 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5230 } else {
5231 req->force_link_speed = cpu_to_le16(fw_link_speed);
5232 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5233 }
5234
Michael Chanc0c050c2015-10-22 16:01:17 -04005235 /* tell chimp that the setting takes effect immediately */
5236 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5237}
5238
5239int bnxt_hwrm_set_pause(struct bnxt *bp)
5240{
5241 struct hwrm_port_phy_cfg_input req = {0};
5242 int rc;
5243
5244 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5245 bnxt_hwrm_set_pause_common(bp, &req);
5246
5247 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5248 bp->link_info.force_link_chng)
5249 bnxt_hwrm_set_link_common(bp, &req);
5250
5251 mutex_lock(&bp->hwrm_cmd_lock);
5252 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5253 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5254 /* since changing of pause setting doesn't trigger any link
5255 * change event, the driver needs to update the current pause
5256 * result upon successfully return of the phy_cfg command
5257 */
5258 bp->link_info.pause =
5259 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5260 bp->link_info.auto_pause_setting = 0;
5261 if (!bp->link_info.force_link_chng)
5262 bnxt_report_link(bp);
5263 }
5264 bp->link_info.force_link_chng = false;
5265 mutex_unlock(&bp->hwrm_cmd_lock);
5266 return rc;
5267}
5268
Michael Chan939f7f02016-04-05 14:08:58 -04005269static void bnxt_hwrm_set_eee(struct bnxt *bp,
5270 struct hwrm_port_phy_cfg_input *req)
5271{
5272 struct ethtool_eee *eee = &bp->eee;
5273
5274 if (eee->eee_enabled) {
5275 u16 eee_speeds;
5276 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5277
5278 if (eee->tx_lpi_enabled)
5279 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5280 else
5281 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5282
5283 req->flags |= cpu_to_le32(flags);
5284 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5285 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5286 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5287 } else {
5288 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5289 }
5290}
5291
5292int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005293{
5294 struct hwrm_port_phy_cfg_input req = {0};
5295
5296 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5297 if (set_pause)
5298 bnxt_hwrm_set_pause_common(bp, &req);
5299
5300 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005301
5302 if (set_eee)
5303 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005304 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5305}
5306
Michael Chan33f7d552016-04-11 04:11:12 -04005307static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5308{
5309 struct hwrm_port_phy_cfg_input req = {0};
5310
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005311 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005312 return 0;
5313
5314 if (pci_num_vf(bp->pdev))
5315 return 0;
5316
5317 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5318 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DOWN);
5319 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5320}
5321
Michael Chan939f7f02016-04-05 14:08:58 -04005322static bool bnxt_eee_config_ok(struct bnxt *bp)
5323{
5324 struct ethtool_eee *eee = &bp->eee;
5325 struct bnxt_link_info *link_info = &bp->link_info;
5326
5327 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5328 return true;
5329
5330 if (eee->eee_enabled) {
5331 u32 advertising =
5332 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5333
5334 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5335 eee->eee_enabled = 0;
5336 return false;
5337 }
5338 if (eee->advertised & ~advertising) {
5339 eee->advertised = advertising & eee->supported;
5340 return false;
5341 }
5342 }
5343 return true;
5344}
5345
Michael Chanc0c050c2015-10-22 16:01:17 -04005346static int bnxt_update_phy_setting(struct bnxt *bp)
5347{
5348 int rc;
5349 bool update_link = false;
5350 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005351 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005352 struct bnxt_link_info *link_info = &bp->link_info;
5353
5354 rc = bnxt_update_link(bp, true);
5355 if (rc) {
5356 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5357 rc);
5358 return rc;
5359 }
5360 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005361 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5362 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005363 update_pause = true;
5364 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5365 link_info->force_pause_setting != link_info->req_flow_ctrl)
5366 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005367 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5368 if (BNXT_AUTO_MODE(link_info->auto_mode))
5369 update_link = true;
5370 if (link_info->req_link_speed != link_info->force_link_speed)
5371 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005372 if (link_info->req_duplex != link_info->duplex_setting)
5373 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005374 } else {
5375 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5376 update_link = true;
5377 if (link_info->advertising != link_info->auto_link_speeds)
5378 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005379 }
5380
Michael Chan939f7f02016-04-05 14:08:58 -04005381 if (!bnxt_eee_config_ok(bp))
5382 update_eee = true;
5383
Michael Chanc0c050c2015-10-22 16:01:17 -04005384 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005385 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005386 else if (update_pause)
5387 rc = bnxt_hwrm_set_pause(bp);
5388 if (rc) {
5389 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5390 rc);
5391 return rc;
5392 }
5393
5394 return rc;
5395}
5396
Jeffrey Huang11809492015-11-05 16:25:49 -05005397/* Common routine to pre-map certain register block to different GRC window.
5398 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5399 * in PF and 3 windows in VF that can be customized to map in different
5400 * register blocks.
5401 */
5402static void bnxt_preset_reg_win(struct bnxt *bp)
5403{
5404 if (BNXT_PF(bp)) {
5405 /* CAG registers map to GRC window #4 */
5406 writel(BNXT_CAG_REG_BASE,
5407 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5408 }
5409}
5410
Michael Chanc0c050c2015-10-22 16:01:17 -04005411static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5412{
5413 int rc = 0;
5414
Jeffrey Huang11809492015-11-05 16:25:49 -05005415 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005416 netif_carrier_off(bp->dev);
5417 if (irq_re_init) {
5418 rc = bnxt_setup_int_mode(bp);
5419 if (rc) {
5420 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5421 rc);
5422 return rc;
5423 }
5424 }
5425 if ((bp->flags & BNXT_FLAG_RFS) &&
5426 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5427 /* disable RFS if falling back to INTA */
5428 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5429 bp->flags &= ~BNXT_FLAG_RFS;
5430 }
5431
5432 rc = bnxt_alloc_mem(bp, irq_re_init);
5433 if (rc) {
5434 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5435 goto open_err_free_mem;
5436 }
5437
5438 if (irq_re_init) {
5439 bnxt_init_napi(bp);
5440 rc = bnxt_request_irq(bp);
5441 if (rc) {
5442 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5443 goto open_err;
5444 }
5445 }
5446
5447 bnxt_enable_napi(bp);
5448
5449 rc = bnxt_init_nic(bp, irq_re_init);
5450 if (rc) {
5451 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5452 goto open_err;
5453 }
5454
5455 if (link_re_init) {
5456 rc = bnxt_update_phy_setting(bp);
5457 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005458 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005459 }
5460
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005461 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005462 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005463
Michael Chancaefe522015-12-09 19:35:42 -05005464 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005465 bnxt_enable_int(bp);
5466 /* Enable TX queues */
5467 bnxt_tx_enable(bp);
5468 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005469 /* Poll link status and check for SFP+ module status */
5470 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005471
5472 return 0;
5473
5474open_err:
5475 bnxt_disable_napi(bp);
5476 bnxt_del_napi(bp);
5477
5478open_err_free_mem:
5479 bnxt_free_skbs(bp);
5480 bnxt_free_irq(bp);
5481 bnxt_free_mem(bp, true);
5482 return rc;
5483}
5484
5485/* rtnl_lock held */
5486int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5487{
5488 int rc = 0;
5489
5490 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5491 if (rc) {
5492 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5493 dev_close(bp->dev);
5494 }
5495 return rc;
5496}
5497
5498static int bnxt_open(struct net_device *dev)
5499{
5500 struct bnxt *bp = netdev_priv(dev);
5501 int rc = 0;
5502
Michael Chan2a5bedf2016-07-01 18:46:21 -04005503 if (!test_bit(BNXT_STATE_FN_RST_DONE, &bp->state)) {
5504 rc = bnxt_hwrm_func_reset(bp);
5505 if (rc) {
5506 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
5507 rc);
5508 rc = -EBUSY;
5509 return rc;
5510 }
5511 /* Do func_reset during the 1st PF open only to prevent killing
5512 * the VFs when the PF is brought down and up.
5513 */
5514 if (BNXT_PF(bp))
5515 set_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005516 }
5517 return __bnxt_open_nic(bp, true, true);
5518}
5519
5520static void bnxt_disable_int_sync(struct bnxt *bp)
5521{
5522 int i;
5523
5524 atomic_inc(&bp->intr_sem);
5525 if (!netif_running(bp->dev))
5526 return;
5527
5528 bnxt_disable_int(bp);
5529 for (i = 0; i < bp->cp_nr_rings; i++)
5530 synchronize_irq(bp->irq_tbl[i].vector);
5531}
5532
5533int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5534{
5535 int rc = 0;
5536
5537#ifdef CONFIG_BNXT_SRIOV
5538 if (bp->sriov_cfg) {
5539 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5540 !bp->sriov_cfg,
5541 BNXT_SRIOV_CFG_WAIT_TMO);
5542 if (rc)
5543 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5544 }
5545#endif
5546 /* Change device state to avoid TX queue wake up's */
5547 bnxt_tx_disable(bp);
5548
Michael Chancaefe522015-12-09 19:35:42 -05005549 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005550 smp_mb__after_atomic();
5551 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5552 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005553
5554 /* Flush rings before disabling interrupts */
5555 bnxt_shutdown_nic(bp, irq_re_init);
5556
5557 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5558
5559 bnxt_disable_napi(bp);
5560 bnxt_disable_int_sync(bp);
5561 del_timer_sync(&bp->timer);
5562 bnxt_free_skbs(bp);
5563
5564 if (irq_re_init) {
5565 bnxt_free_irq(bp);
5566 bnxt_del_napi(bp);
5567 }
5568 bnxt_free_mem(bp, irq_re_init);
5569 return rc;
5570}
5571
5572static int bnxt_close(struct net_device *dev)
5573{
5574 struct bnxt *bp = netdev_priv(dev);
5575
5576 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005577 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005578 return 0;
5579}
5580
5581/* rtnl_lock held */
5582static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5583{
5584 switch (cmd) {
5585 case SIOCGMIIPHY:
5586 /* fallthru */
5587 case SIOCGMIIREG: {
5588 if (!netif_running(dev))
5589 return -EAGAIN;
5590
5591 return 0;
5592 }
5593
5594 case SIOCSMIIREG:
5595 if (!netif_running(dev))
5596 return -EAGAIN;
5597
5598 return 0;
5599
5600 default:
5601 /* do nothing */
5602 break;
5603 }
5604 return -EOPNOTSUPP;
5605}
5606
5607static struct rtnl_link_stats64 *
5608bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5609{
5610 u32 i;
5611 struct bnxt *bp = netdev_priv(dev);
5612
5613 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5614
5615 if (!bp->bnapi)
5616 return stats;
5617
5618 /* TODO check if we need to synchronize with bnxt_close path */
5619 for (i = 0; i < bp->cp_nr_rings; i++) {
5620 struct bnxt_napi *bnapi = bp->bnapi[i];
5621 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5622 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5623
5624 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5625 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5626 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5627
5628 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5629 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5630 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5631
5632 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5633 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5634 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5635
5636 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5637 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5638 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5639
5640 stats->rx_missed_errors +=
5641 le64_to_cpu(hw_stats->rx_discard_pkts);
5642
5643 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5644
Michael Chanc0c050c2015-10-22 16:01:17 -04005645 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5646 }
5647
Michael Chan9947f832016-03-07 15:38:46 -05005648 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5649 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5650 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5651
5652 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5653 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5654 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5655 le64_to_cpu(rx->rx_ovrsz_frames) +
5656 le64_to_cpu(rx->rx_runt_frames);
5657 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5658 le64_to_cpu(rx->rx_jbr_frames);
5659 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5660 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5661 stats->tx_errors = le64_to_cpu(tx->tx_err);
5662 }
5663
Michael Chanc0c050c2015-10-22 16:01:17 -04005664 return stats;
5665}
5666
5667static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5668{
5669 struct net_device *dev = bp->dev;
5670 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5671 struct netdev_hw_addr *ha;
5672 u8 *haddr;
5673 int mc_count = 0;
5674 bool update = false;
5675 int off = 0;
5676
5677 netdev_for_each_mc_addr(ha, dev) {
5678 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5679 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5680 vnic->mc_list_count = 0;
5681 return false;
5682 }
5683 haddr = ha->addr;
5684 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5685 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5686 update = true;
5687 }
5688 off += ETH_ALEN;
5689 mc_count++;
5690 }
5691 if (mc_count)
5692 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5693
5694 if (mc_count != vnic->mc_list_count) {
5695 vnic->mc_list_count = mc_count;
5696 update = true;
5697 }
5698 return update;
5699}
5700
5701static bool bnxt_uc_list_updated(struct bnxt *bp)
5702{
5703 struct net_device *dev = bp->dev;
5704 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5705 struct netdev_hw_addr *ha;
5706 int off = 0;
5707
5708 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5709 return true;
5710
5711 netdev_for_each_uc_addr(ha, dev) {
5712 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5713 return true;
5714
5715 off += ETH_ALEN;
5716 }
5717 return false;
5718}
5719
5720static void bnxt_set_rx_mode(struct net_device *dev)
5721{
5722 struct bnxt *bp = netdev_priv(dev);
5723 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5724 u32 mask = vnic->rx_mask;
5725 bool mc_update = false;
5726 bool uc_update;
5727
5728 if (!netif_running(dev))
5729 return;
5730
5731 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5732 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5733 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5734
Michael Chan17c71ac2016-07-01 18:46:27 -04005735 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005736 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5737
5738 uc_update = bnxt_uc_list_updated(bp);
5739
5740 if (dev->flags & IFF_ALLMULTI) {
5741 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5742 vnic->mc_list_count = 0;
5743 } else {
5744 mc_update = bnxt_mc_list_updated(bp, &mask);
5745 }
5746
5747 if (mask != vnic->rx_mask || uc_update || mc_update) {
5748 vnic->rx_mask = mask;
5749
5750 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5751 schedule_work(&bp->sp_task);
5752 }
5753}
5754
Michael Chanb664f002015-12-02 01:54:08 -05005755static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005756{
5757 struct net_device *dev = bp->dev;
5758 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5759 struct netdev_hw_addr *ha;
5760 int i, off = 0, rc;
5761 bool uc_update;
5762
5763 netif_addr_lock_bh(dev);
5764 uc_update = bnxt_uc_list_updated(bp);
5765 netif_addr_unlock_bh(dev);
5766
5767 if (!uc_update)
5768 goto skip_uc;
5769
5770 mutex_lock(&bp->hwrm_cmd_lock);
5771 for (i = 1; i < vnic->uc_filter_count; i++) {
5772 struct hwrm_cfa_l2_filter_free_input req = {0};
5773
5774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5775 -1);
5776
5777 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5778
5779 rc = _hwrm_send_message(bp, &req, sizeof(req),
5780 HWRM_CMD_TIMEOUT);
5781 }
5782 mutex_unlock(&bp->hwrm_cmd_lock);
5783
5784 vnic->uc_filter_count = 1;
5785
5786 netif_addr_lock_bh(dev);
5787 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5788 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5789 } else {
5790 netdev_for_each_uc_addr(ha, dev) {
5791 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5792 off += ETH_ALEN;
5793 vnic->uc_filter_count++;
5794 }
5795 }
5796 netif_addr_unlock_bh(dev);
5797
5798 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5799 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5800 if (rc) {
5801 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5802 rc);
5803 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005804 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005805 }
5806 }
5807
5808skip_uc:
5809 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5810 if (rc)
5811 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5812 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005813
5814 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005815}
5816
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005817static bool bnxt_rfs_capable(struct bnxt *bp)
5818{
5819#ifdef CONFIG_RFS_ACCEL
5820 struct bnxt_pf_info *pf = &bp->pf;
5821 int vnics;
5822
5823 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5824 return false;
5825
5826 vnics = 1 + bp->rx_nr_rings;
Vasundhara Volama2304902016-07-25 12:33:36 -04005827 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5828 netdev_warn(bp->dev,
5829 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5830 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005831 return false;
Vasundhara Volama2304902016-07-25 12:33:36 -04005832 }
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005833
5834 return true;
5835#else
5836 return false;
5837#endif
5838}
5839
Michael Chanc0c050c2015-10-22 16:01:17 -04005840static netdev_features_t bnxt_fix_features(struct net_device *dev,
5841 netdev_features_t features)
5842{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005843 struct bnxt *bp = netdev_priv(dev);
5844
Vasundhara Volama2304902016-07-25 12:33:36 -04005845 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005846 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005847
5848 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5849 * turned on or off together.
5850 */
5851 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5852 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5853 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5854 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5855 NETIF_F_HW_VLAN_STAG_RX);
5856 else
5857 features |= NETIF_F_HW_VLAN_CTAG_RX |
5858 NETIF_F_HW_VLAN_STAG_RX;
5859 }
Michael Chancf6645f2016-06-13 02:25:28 -04005860#ifdef CONFIG_BNXT_SRIOV
5861 if (BNXT_VF(bp)) {
5862 if (bp->vf.vlan) {
5863 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5864 NETIF_F_HW_VLAN_STAG_RX);
5865 }
5866 }
5867#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005868 return features;
5869}
5870
5871static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5872{
5873 struct bnxt *bp = netdev_priv(dev);
5874 u32 flags = bp->flags;
5875 u32 changes;
5876 int rc = 0;
5877 bool re_init = false;
5878 bool update_tpa = false;
5879
5880 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005881 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005882 flags |= BNXT_FLAG_GRO;
5883 if (features & NETIF_F_LRO)
5884 flags |= BNXT_FLAG_LRO;
5885
5886 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5887 flags |= BNXT_FLAG_STRIP_VLAN;
5888
5889 if (features & NETIF_F_NTUPLE)
5890 flags |= BNXT_FLAG_RFS;
5891
5892 changes = flags ^ bp->flags;
5893 if (changes & BNXT_FLAG_TPA) {
5894 update_tpa = true;
5895 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5896 (flags & BNXT_FLAG_TPA) == 0)
5897 re_init = true;
5898 }
5899
5900 if (changes & ~BNXT_FLAG_TPA)
5901 re_init = true;
5902
5903 if (flags != bp->flags) {
5904 u32 old_flags = bp->flags;
5905
5906 bp->flags = flags;
5907
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005908 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005909 if (update_tpa)
5910 bnxt_set_ring_params(bp);
5911 return rc;
5912 }
5913
5914 if (re_init) {
5915 bnxt_close_nic(bp, false, false);
5916 if (update_tpa)
5917 bnxt_set_ring_params(bp);
5918
5919 return bnxt_open_nic(bp, false, false);
5920 }
5921 if (update_tpa) {
5922 rc = bnxt_set_tpa(bp,
5923 (flags & BNXT_FLAG_TPA) ?
5924 true : false);
5925 if (rc)
5926 bp->flags = old_flags;
5927 }
5928 }
5929 return rc;
5930}
5931
Michael Chan9f554592016-01-02 23:44:58 -05005932static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5933{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005934 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005935 int i = bnapi->index;
5936
Michael Chan3b2b7d92016-01-02 23:45:00 -05005937 if (!txr)
5938 return;
5939
Michael Chan9f554592016-01-02 23:44:58 -05005940 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5941 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5942 txr->tx_cons);
5943}
5944
5945static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
5946{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005947 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005948 int i = bnapi->index;
5949
Michael Chan3b2b7d92016-01-02 23:45:00 -05005950 if (!rxr)
5951 return;
5952
Michael Chan9f554592016-01-02 23:44:58 -05005953 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",
5954 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
5955 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
5956 rxr->rx_sw_agg_prod);
5957}
5958
5959static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
5960{
5961 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5962 int i = bnapi->index;
5963
5964 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
5965 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
5966}
5967
Michael Chanc0c050c2015-10-22 16:01:17 -04005968static void bnxt_dbg_dump_states(struct bnxt *bp)
5969{
5970 int i;
5971 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04005972
5973 for (i = 0; i < bp->cp_nr_rings; i++) {
5974 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005975 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05005976 bnxt_dump_tx_sw_state(bnapi);
5977 bnxt_dump_rx_sw_state(bnapi);
5978 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005979 }
5980 }
5981}
5982
Michael Chan6988bd92016-06-13 02:25:29 -04005983static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04005984{
Michael Chan6988bd92016-06-13 02:25:29 -04005985 if (!silent)
5986 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05005987 if (netif_running(bp->dev)) {
5988 bnxt_close_nic(bp, false, false);
5989 bnxt_open_nic(bp, false, false);
5990 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005991}
5992
5993static void bnxt_tx_timeout(struct net_device *dev)
5994{
5995 struct bnxt *bp = netdev_priv(dev);
5996
5997 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
5998 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
5999 schedule_work(&bp->sp_task);
6000}
6001
6002#ifdef CONFIG_NET_POLL_CONTROLLER
6003static void bnxt_poll_controller(struct net_device *dev)
6004{
6005 struct bnxt *bp = netdev_priv(dev);
6006 int i;
6007
6008 for (i = 0; i < bp->cp_nr_rings; i++) {
6009 struct bnxt_irq *irq = &bp->irq_tbl[i];
6010
6011 disable_irq(irq->vector);
6012 irq->handler(irq->vector, bp->bnapi[i]);
6013 enable_irq(irq->vector);
6014 }
6015}
6016#endif
6017
6018static void bnxt_timer(unsigned long data)
6019{
6020 struct bnxt *bp = (struct bnxt *)data;
6021 struct net_device *dev = bp->dev;
6022
6023 if (!netif_running(dev))
6024 return;
6025
6026 if (atomic_read(&bp->intr_sem) != 0)
6027 goto bnxt_restart_timer;
6028
Michael Chan3bdf56c2016-03-07 15:38:45 -05006029 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6030 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6031 schedule_work(&bp->sp_task);
6032 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006033bnxt_restart_timer:
6034 mod_timer(&bp->timer, jiffies + bp->current_interval);
6035}
6036
Michael Chan6988bd92016-06-13 02:25:29 -04006037/* Only called from bnxt_sp_task() */
6038static void bnxt_reset(struct bnxt *bp, bool silent)
6039{
6040 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6041 * for BNXT_STATE_IN_SP_TASK to clear.
6042 * If there is a parallel dev_close(), bnxt_close() may be holding
6043 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6044 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6045 */
6046 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6047 rtnl_lock();
6048 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6049 bnxt_reset_task(bp, silent);
6050 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6051 rtnl_unlock();
6052}
6053
Michael Chanc0c050c2015-10-22 16:01:17 -04006054static void bnxt_cfg_ntp_filters(struct bnxt *);
6055
6056static void bnxt_sp_task(struct work_struct *work)
6057{
6058 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6059 int rc;
6060
Michael Chan4cebdce2015-12-09 19:35:43 -05006061 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6062 smp_mb__after_atomic();
6063 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6064 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006065 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006066 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006067
6068 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6069 bnxt_cfg_rx_mode(bp);
6070
6071 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6072 bnxt_cfg_ntp_filters(bp);
6073 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6074 rc = bnxt_update_link(bp, true);
6075 if (rc)
6076 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6077 rc);
6078 }
6079 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6080 bnxt_hwrm_exec_fwd_req(bp);
6081 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6082 bnxt_hwrm_tunnel_dst_port_alloc(
6083 bp, bp->vxlan_port,
6084 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6085 }
6086 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6087 bnxt_hwrm_tunnel_dst_port_free(
6088 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6089 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006090 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6091 bnxt_hwrm_tunnel_dst_port_alloc(
6092 bp, bp->nge_port,
6093 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6094 }
6095 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6096 bnxt_hwrm_tunnel_dst_port_free(
6097 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6098 }
Michael Chan6988bd92016-06-13 02:25:29 -04006099 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6100 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006101
Michael Chanfc0f1922016-06-13 02:25:30 -04006102 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6103 bnxt_reset(bp, true);
6104
Michael Chan4bb13ab2016-04-05 14:09:01 -04006105 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006106 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006107
Michael Chan3bdf56c2016-03-07 15:38:45 -05006108 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6109 bnxt_hwrm_port_qstats(bp);
6110
Michael Chan4cebdce2015-12-09 19:35:43 -05006111 smp_mb__before_atomic();
6112 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006113}
6114
6115static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6116{
6117 int rc;
6118 struct bnxt *bp = netdev_priv(dev);
6119
6120 SET_NETDEV_DEV(dev, &pdev->dev);
6121
6122 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6123 rc = pci_enable_device(pdev);
6124 if (rc) {
6125 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6126 goto init_err;
6127 }
6128
6129 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6130 dev_err(&pdev->dev,
6131 "Cannot find PCI device base address, aborting\n");
6132 rc = -ENODEV;
6133 goto init_err_disable;
6134 }
6135
6136 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6137 if (rc) {
6138 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6139 goto init_err_disable;
6140 }
6141
6142 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6143 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6144 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6145 goto init_err_disable;
6146 }
6147
6148 pci_set_master(pdev);
6149
6150 bp->dev = dev;
6151 bp->pdev = pdev;
6152
6153 bp->bar0 = pci_ioremap_bar(pdev, 0);
6154 if (!bp->bar0) {
6155 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6156 rc = -ENOMEM;
6157 goto init_err_release;
6158 }
6159
6160 bp->bar1 = pci_ioremap_bar(pdev, 2);
6161 if (!bp->bar1) {
6162 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6163 rc = -ENOMEM;
6164 goto init_err_release;
6165 }
6166
6167 bp->bar2 = pci_ioremap_bar(pdev, 4);
6168 if (!bp->bar2) {
6169 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6170 rc = -ENOMEM;
6171 goto init_err_release;
6172 }
6173
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006174 pci_enable_pcie_error_reporting(pdev);
6175
Michael Chanc0c050c2015-10-22 16:01:17 -04006176 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6177
6178 spin_lock_init(&bp->ntp_fltr_lock);
6179
6180 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6181 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6182
Michael Chandfb5b892016-02-26 04:00:01 -05006183 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006184 bp->rx_coal_ticks = 12;
6185 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006186 bp->rx_coal_ticks_irq = 1;
6187 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006188
Michael Chandfc9c942016-02-26 04:00:03 -05006189 bp->tx_coal_ticks = 25;
6190 bp->tx_coal_bufs = 30;
6191 bp->tx_coal_ticks_irq = 2;
6192 bp->tx_coal_bufs_irq = 2;
6193
Michael Chan51f30782016-07-01 18:46:29 -04006194 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6195
Michael Chanc0c050c2015-10-22 16:01:17 -04006196 init_timer(&bp->timer);
6197 bp->timer.data = (unsigned long)bp;
6198 bp->timer.function = bnxt_timer;
6199 bp->current_interval = BNXT_TIMER_INTERVAL;
6200
Michael Chancaefe522015-12-09 19:35:42 -05006201 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006202
6203 return 0;
6204
6205init_err_release:
6206 if (bp->bar2) {
6207 pci_iounmap(pdev, bp->bar2);
6208 bp->bar2 = NULL;
6209 }
6210
6211 if (bp->bar1) {
6212 pci_iounmap(pdev, bp->bar1);
6213 bp->bar1 = NULL;
6214 }
6215
6216 if (bp->bar0) {
6217 pci_iounmap(pdev, bp->bar0);
6218 bp->bar0 = NULL;
6219 }
6220
6221 pci_release_regions(pdev);
6222
6223init_err_disable:
6224 pci_disable_device(pdev);
6225
6226init_err:
6227 return rc;
6228}
6229
6230/* rtnl_lock held */
6231static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6232{
6233 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006234 struct bnxt *bp = netdev_priv(dev);
6235 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006236
6237 if (!is_valid_ether_addr(addr->sa_data))
6238 return -EADDRNOTAVAIL;
6239
Michael Chan84c33dd2016-04-11 04:11:13 -04006240 rc = bnxt_approve_mac(bp, addr->sa_data);
6241 if (rc)
6242 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006243
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006244 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6245 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006246
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006247 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6248 if (netif_running(dev)) {
6249 bnxt_close_nic(bp, false, false);
6250 rc = bnxt_open_nic(bp, false, false);
6251 }
6252
6253 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006254}
6255
6256/* rtnl_lock held */
6257static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6258{
6259 struct bnxt *bp = netdev_priv(dev);
6260
Vasundhara Volamdc7aadb2016-07-01 18:46:26 -04006261 if (new_mtu < 60 || new_mtu > 9500)
Michael Chanc0c050c2015-10-22 16:01:17 -04006262 return -EINVAL;
6263
6264 if (netif_running(dev))
6265 bnxt_close_nic(bp, false, false);
6266
6267 dev->mtu = new_mtu;
6268 bnxt_set_ring_params(bp);
6269
6270 if (netif_running(dev))
6271 return bnxt_open_nic(bp, false, false);
6272
6273 return 0;
6274}
6275
John Fastabend16e5cc62016-02-16 21:16:43 -08006276static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6277 struct tc_to_netdev *ntc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006278{
6279 struct bnxt *bp = netdev_priv(dev);
John Fastabend16e5cc62016-02-16 21:16:43 -08006280 u8 tc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006281
John Fastabend5eb4dce2016-02-29 11:26:13 -08006282 if (ntc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08006283 return -EINVAL;
6284
John Fastabend16e5cc62016-02-16 21:16:43 -08006285 tc = ntc->tc;
6286
Michael Chanc0c050c2015-10-22 16:01:17 -04006287 if (tc > bp->max_tc) {
6288 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6289 tc, bp->max_tc);
6290 return -EINVAL;
6291 }
6292
6293 if (netdev_get_num_tc(dev) == tc)
6294 return 0;
6295
6296 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006297 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006298 bool sh = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006299
Michael Chan01657bc2016-01-02 23:45:03 -05006300 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6301 sh = true;
6302
6303 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006304 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006305 return -ENOMEM;
6306 }
6307
6308 /* Needs to close the device and do hw resource re-allocations */
6309 if (netif_running(bp->dev))
6310 bnxt_close_nic(bp, true, false);
6311
6312 if (tc) {
6313 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6314 netdev_set_num_tc(dev, tc);
6315 } else {
6316 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6317 netdev_reset_tc(dev);
6318 }
6319 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
6320 bp->num_stat_ctxs = bp->cp_nr_rings;
6321
6322 if (netif_running(bp->dev))
6323 return bnxt_open_nic(bp, true, false);
6324
6325 return 0;
6326}
6327
6328#ifdef CONFIG_RFS_ACCEL
6329static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6330 struct bnxt_ntuple_filter *f2)
6331{
6332 struct flow_keys *keys1 = &f1->fkeys;
6333 struct flow_keys *keys2 = &f2->fkeys;
6334
6335 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6336 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6337 keys1->ports.ports == keys2->ports.ports &&
6338 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6339 keys1->basic.n_proto == keys2->basic.n_proto &&
Michael Chana54c4d72016-07-25 12:33:35 -04006340 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6341 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
Michael Chanc0c050c2015-10-22 16:01:17 -04006342 return true;
6343
6344 return false;
6345}
6346
6347static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6348 u16 rxq_index, u32 flow_id)
6349{
6350 struct bnxt *bp = netdev_priv(dev);
6351 struct bnxt_ntuple_filter *fltr, *new_fltr;
6352 struct flow_keys *fkeys;
6353 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chana54c4d72016-07-25 12:33:35 -04006354 int rc = 0, idx, bit_id, l2_idx = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006355 struct hlist_head *head;
6356
6357 if (skb->encapsulation)
6358 return -EPROTONOSUPPORT;
6359
Michael Chana54c4d72016-07-25 12:33:35 -04006360 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6361 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6362 int off = 0, j;
6363
6364 netif_addr_lock_bh(dev);
6365 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6366 if (ether_addr_equal(eth->h_dest,
6367 vnic->uc_list + off)) {
6368 l2_idx = j + 1;
6369 break;
6370 }
6371 }
6372 netif_addr_unlock_bh(dev);
6373 if (!l2_idx)
6374 return -EINVAL;
6375 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006376 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6377 if (!new_fltr)
6378 return -ENOMEM;
6379
6380 fkeys = &new_fltr->fkeys;
6381 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6382 rc = -EPROTONOSUPPORT;
6383 goto err_free;
6384 }
6385
6386 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6387 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6388 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6389 rc = -EPROTONOSUPPORT;
6390 goto err_free;
6391 }
6392
Michael Chana54c4d72016-07-25 12:33:35 -04006393 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04006394 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6395
6396 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6397 head = &bp->ntp_fltr_hash_tbl[idx];
6398 rcu_read_lock();
6399 hlist_for_each_entry_rcu(fltr, head, hash) {
6400 if (bnxt_fltr_match(fltr, new_fltr)) {
6401 rcu_read_unlock();
6402 rc = 0;
6403 goto err_free;
6404 }
6405 }
6406 rcu_read_unlock();
6407
6408 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006409 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6410 BNXT_NTP_FLTR_MAX_FLTR, 0);
6411 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006412 spin_unlock_bh(&bp->ntp_fltr_lock);
6413 rc = -ENOMEM;
6414 goto err_free;
6415 }
6416
Michael Chan84e86b92015-11-05 16:25:50 -05006417 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006418 new_fltr->flow_id = flow_id;
Michael Chana54c4d72016-07-25 12:33:35 -04006419 new_fltr->l2_fltr_idx = l2_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04006420 new_fltr->rxq = rxq_index;
6421 hlist_add_head_rcu(&new_fltr->hash, head);
6422 bp->ntp_fltr_count++;
6423 spin_unlock_bh(&bp->ntp_fltr_lock);
6424
6425 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6426 schedule_work(&bp->sp_task);
6427
6428 return new_fltr->sw_id;
6429
6430err_free:
6431 kfree(new_fltr);
6432 return rc;
6433}
6434
6435static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6436{
6437 int i;
6438
6439 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6440 struct hlist_head *head;
6441 struct hlist_node *tmp;
6442 struct bnxt_ntuple_filter *fltr;
6443 int rc;
6444
6445 head = &bp->ntp_fltr_hash_tbl[i];
6446 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6447 bool del = false;
6448
6449 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6450 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6451 fltr->flow_id,
6452 fltr->sw_id)) {
6453 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6454 fltr);
6455 del = true;
6456 }
6457 } else {
6458 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6459 fltr);
6460 if (rc)
6461 del = true;
6462 else
6463 set_bit(BNXT_FLTR_VALID, &fltr->state);
6464 }
6465
6466 if (del) {
6467 spin_lock_bh(&bp->ntp_fltr_lock);
6468 hlist_del_rcu(&fltr->hash);
6469 bp->ntp_fltr_count--;
6470 spin_unlock_bh(&bp->ntp_fltr_lock);
6471 synchronize_rcu();
6472 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6473 kfree(fltr);
6474 }
6475 }
6476 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006477 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6478 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006479}
6480
6481#else
6482
6483static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6484{
6485}
6486
6487#endif /* CONFIG_RFS_ACCEL */
6488
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006489static void bnxt_udp_tunnel_add(struct net_device *dev,
6490 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006491{
6492 struct bnxt *bp = netdev_priv(dev);
6493
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006494 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6495 return;
6496
Michael Chanc0c050c2015-10-22 16:01:17 -04006497 if (!netif_running(dev))
6498 return;
6499
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006500 switch (ti->type) {
6501 case UDP_TUNNEL_TYPE_VXLAN:
6502 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6503 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006504
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006505 bp->vxlan_port_cnt++;
6506 if (bp->vxlan_port_cnt == 1) {
6507 bp->vxlan_port = ti->port;
6508 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006509 schedule_work(&bp->sp_task);
6510 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006511 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006512 case UDP_TUNNEL_TYPE_GENEVE:
6513 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6514 return;
6515
6516 bp->nge_port_cnt++;
6517 if (bp->nge_port_cnt == 1) {
6518 bp->nge_port = ti->port;
6519 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6520 }
6521 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006522 default:
6523 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006524 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006525
6526 schedule_work(&bp->sp_task);
6527}
6528
6529static void bnxt_udp_tunnel_del(struct net_device *dev,
6530 struct udp_tunnel_info *ti)
6531{
6532 struct bnxt *bp = netdev_priv(dev);
6533
6534 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6535 return;
6536
6537 if (!netif_running(dev))
6538 return;
6539
6540 switch (ti->type) {
6541 case UDP_TUNNEL_TYPE_VXLAN:
6542 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6543 return;
6544 bp->vxlan_port_cnt--;
6545
6546 if (bp->vxlan_port_cnt != 0)
6547 return;
6548
6549 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6550 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006551 case UDP_TUNNEL_TYPE_GENEVE:
6552 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6553 return;
6554 bp->nge_port_cnt--;
6555
6556 if (bp->nge_port_cnt != 0)
6557 return;
6558
6559 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6560 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006561 default:
6562 return;
6563 }
6564
6565 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006566}
6567
6568static const struct net_device_ops bnxt_netdev_ops = {
6569 .ndo_open = bnxt_open,
6570 .ndo_start_xmit = bnxt_start_xmit,
6571 .ndo_stop = bnxt_close,
6572 .ndo_get_stats64 = bnxt_get_stats64,
6573 .ndo_set_rx_mode = bnxt_set_rx_mode,
6574 .ndo_do_ioctl = bnxt_ioctl,
6575 .ndo_validate_addr = eth_validate_addr,
6576 .ndo_set_mac_address = bnxt_change_mac_addr,
6577 .ndo_change_mtu = bnxt_change_mtu,
6578 .ndo_fix_features = bnxt_fix_features,
6579 .ndo_set_features = bnxt_set_features,
6580 .ndo_tx_timeout = bnxt_tx_timeout,
6581#ifdef CONFIG_BNXT_SRIOV
6582 .ndo_get_vf_config = bnxt_get_vf_config,
6583 .ndo_set_vf_mac = bnxt_set_vf_mac,
6584 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6585 .ndo_set_vf_rate = bnxt_set_vf_bw,
6586 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6587 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6588#endif
6589#ifdef CONFIG_NET_POLL_CONTROLLER
6590 .ndo_poll_controller = bnxt_poll_controller,
6591#endif
6592 .ndo_setup_tc = bnxt_setup_tc,
6593#ifdef CONFIG_RFS_ACCEL
6594 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6595#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006596 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6597 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006598#ifdef CONFIG_NET_RX_BUSY_POLL
6599 .ndo_busy_poll = bnxt_busy_poll,
6600#endif
6601};
6602
6603static void bnxt_remove_one(struct pci_dev *pdev)
6604{
6605 struct net_device *dev = pci_get_drvdata(pdev);
6606 struct bnxt *bp = netdev_priv(dev);
6607
6608 if (BNXT_PF(bp))
6609 bnxt_sriov_disable(bp);
6610
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006611 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006612 unregister_netdev(dev);
6613 cancel_work_sync(&bp->sp_task);
6614 bp->sp_event = 0;
6615
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006616 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006617 bnxt_free_hwrm_resources(bp);
6618 pci_iounmap(pdev, bp->bar2);
6619 pci_iounmap(pdev, bp->bar1);
6620 pci_iounmap(pdev, bp->bar0);
6621 free_netdev(dev);
6622
6623 pci_release_regions(pdev);
6624 pci_disable_device(pdev);
6625}
6626
6627static int bnxt_probe_phy(struct bnxt *bp)
6628{
6629 int rc = 0;
6630 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006631
Michael Chan170ce012016-04-05 14:08:57 -04006632 rc = bnxt_hwrm_phy_qcaps(bp);
6633 if (rc) {
6634 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6635 rc);
6636 return rc;
6637 }
6638
Michael Chanc0c050c2015-10-22 16:01:17 -04006639 rc = bnxt_update_link(bp, false);
6640 if (rc) {
6641 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6642 rc);
6643 return rc;
6644 }
6645
Michael Chan93ed8112016-06-13 02:25:37 -04006646 /* Older firmware does not have supported_auto_speeds, so assume
6647 * that all supported speeds can be autonegotiated.
6648 */
6649 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6650 link_info->support_auto_speeds = link_info->support_speeds;
6651
Michael Chanc0c050c2015-10-22 16:01:17 -04006652 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006653 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006654 link_info->autoneg = BNXT_AUTONEG_SPEED;
6655 if (bp->hwrm_spec_code >= 0x10201) {
6656 if (link_info->auto_pause_setting &
6657 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6658 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6659 } else {
6660 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6661 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006662 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006663 } else {
6664 link_info->req_link_speed = link_info->force_link_speed;
6665 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006666 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006667 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6668 link_info->req_flow_ctrl =
6669 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6670 else
6671 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006672 return rc;
6673}
6674
6675static int bnxt_get_max_irq(struct pci_dev *pdev)
6676{
6677 u16 ctrl;
6678
6679 if (!pdev->msix_cap)
6680 return 1;
6681
6682 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6683 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6684}
6685
Michael Chan6e6c5a52016-01-02 23:45:02 -05006686static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6687 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006688{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006689 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006690
Michael Chan379a80a2015-10-23 15:06:19 -04006691#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006692 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006693 *max_tx = bp->vf.max_tx_rings;
6694 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006695 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6696 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006697 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006698 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006699#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006700 {
6701 *max_tx = bp->pf.max_tx_rings;
6702 *max_rx = bp->pf.max_rx_rings;
6703 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6704 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6705 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006706 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006707 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6708 *max_cp -= 1;
6709 *max_rx -= 2;
6710 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006711 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6712 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006713 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006714}
6715
6716int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6717{
6718 int rx, tx, cp;
6719
6720 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6721 if (!rx || !tx || !cp)
6722 return -ENOMEM;
6723
6724 *max_rx = rx;
6725 *max_tx = tx;
6726 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6727}
6728
6729static int bnxt_set_dflt_rings(struct bnxt *bp)
6730{
6731 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6732 bool sh = true;
6733
6734 if (sh)
6735 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6736 dflt_rings = netif_get_num_default_rss_queues();
6737 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6738 if (rc)
6739 return rc;
6740 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6741 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6742 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6743 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6744 bp->tx_nr_rings + bp->rx_nr_rings;
6745 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006746 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6747 bp->rx_nr_rings++;
6748 bp->cp_nr_rings++;
6749 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006750 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006751}
6752
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006753static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6754{
6755 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6756 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6757
6758 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6759 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6760 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6761 else
6762 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6763 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6764 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6765 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6766 "Unknown", width);
6767}
6768
Michael Chanc0c050c2015-10-22 16:01:17 -04006769static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6770{
6771 static int version_printed;
6772 struct net_device *dev;
6773 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006774 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006775
Prashant Sreedharanfa853dd2016-07-18 07:15:25 -04006776 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6777 return -ENODEV;
6778
Michael Chanc0c050c2015-10-22 16:01:17 -04006779 if (version_printed++ == 0)
6780 pr_info("%s", version);
6781
6782 max_irqs = bnxt_get_max_irq(pdev);
6783 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6784 if (!dev)
6785 return -ENOMEM;
6786
6787 bp = netdev_priv(dev);
6788
6789 if (bnxt_vf_pciid(ent->driver_data))
6790 bp->flags |= BNXT_FLAG_VF;
6791
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006792 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006793 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006794
6795 rc = bnxt_init_board(pdev, dev);
6796 if (rc < 0)
6797 goto init_err_free;
6798
6799 dev->netdev_ops = &bnxt_netdev_ops;
6800 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6801 dev->ethtool_ops = &bnxt_ethtool_ops;
6802
6803 pci_set_drvdata(pdev, dev);
6804
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006805 rc = bnxt_alloc_hwrm_resources(bp);
6806 if (rc)
6807 goto init_err;
6808
6809 mutex_init(&bp->hwrm_cmd_lock);
6810 rc = bnxt_hwrm_ver_get(bp);
6811 if (rc)
6812 goto init_err;
6813
Michael Chanc0c050c2015-10-22 16:01:17 -04006814 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6815 NETIF_F_TSO | NETIF_F_TSO6 |
6816 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07006817 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07006818 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6819 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006820 NETIF_F_RXCSUM | NETIF_F_GRO;
6821
6822 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6823 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04006824
Michael Chanc0c050c2015-10-22 16:01:17 -04006825 dev->hw_enc_features =
6826 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6827 NETIF_F_TSO | NETIF_F_TSO6 |
6828 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07006829 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07006830 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07006831 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
6832 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04006833 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
6834 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6835 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
6836 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
6837 dev->priv_flags |= IFF_UNICAST_FLT;
6838
6839#ifdef CONFIG_BNXT_SRIOV
6840 init_waitqueue_head(&bp->sriov_cfg_wait);
6841#endif
Michael Chan309369c2016-06-13 02:25:34 -04006842 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04006843 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
6844 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04006845
Michael Chanc0c050c2015-10-22 16:01:17 -04006846 rc = bnxt_hwrm_func_drv_rgtr(bp);
6847 if (rc)
6848 goto init_err;
6849
6850 /* Get the MAX capabilities for this function */
6851 rc = bnxt_hwrm_func_qcaps(bp);
6852 if (rc) {
6853 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
6854 rc);
6855 rc = -1;
6856 goto init_err;
6857 }
6858
6859 rc = bnxt_hwrm_queue_qportcfg(bp);
6860 if (rc) {
6861 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
6862 rc);
6863 rc = -1;
6864 goto init_err;
6865 }
6866
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006867 bnxt_hwrm_func_qcfg(bp);
6868
Michael Chanc0c050c2015-10-22 16:01:17 -04006869 bnxt_set_tpa_flags(bp);
6870 bnxt_set_ring_params(bp);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006871 if (BNXT_PF(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006872 bp->pf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006873#if defined(CONFIG_BNXT_SRIOV)
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006874 else
Michael Chanc0c050c2015-10-22 16:01:17 -04006875 bp->vf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006876#endif
Michael Chan6e6c5a52016-01-02 23:45:02 -05006877 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006878
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006879 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006880 dev->hw_features |= NETIF_F_NTUPLE;
6881 if (bnxt_rfs_capable(bp)) {
6882 bp->flags |= BNXT_FLAG_RFS;
6883 dev->features |= NETIF_F_NTUPLE;
6884 }
6885 }
6886
Michael Chanc0c050c2015-10-22 16:01:17 -04006887 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
6888 bp->flags |= BNXT_FLAG_STRIP_VLAN;
6889
6890 rc = bnxt_probe_phy(bp);
6891 if (rc)
6892 goto init_err;
6893
6894 rc = register_netdev(dev);
6895 if (rc)
6896 goto init_err;
6897
6898 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
6899 board_info[ent->driver_data].name,
6900 (long)pci_resource_start(pdev, 0), dev->dev_addr);
6901
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006902 bnxt_parse_log_pcie_link(bp);
6903
Michael Chanc0c050c2015-10-22 16:01:17 -04006904 return 0;
6905
6906init_err:
6907 pci_iounmap(pdev, bp->bar0);
6908 pci_release_regions(pdev);
6909 pci_disable_device(pdev);
6910
6911init_err_free:
6912 free_netdev(dev);
6913 return rc;
6914}
6915
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006916/**
6917 * bnxt_io_error_detected - called when PCI error is detected
6918 * @pdev: Pointer to PCI device
6919 * @state: The current pci connection state
6920 *
6921 * This function is called after a PCI bus error affecting
6922 * this device has been detected.
6923 */
6924static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
6925 pci_channel_state_t state)
6926{
6927 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chan2a5bedf2016-07-01 18:46:21 -04006928 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006929
6930 netdev_info(netdev, "PCI I/O error detected\n");
6931
6932 rtnl_lock();
6933 netif_device_detach(netdev);
6934
6935 if (state == pci_channel_io_perm_failure) {
6936 rtnl_unlock();
6937 return PCI_ERS_RESULT_DISCONNECT;
6938 }
6939
6940 if (netif_running(netdev))
6941 bnxt_close(netdev);
6942
Michael Chan2a5bedf2016-07-01 18:46:21 -04006943 /* So that func_reset will be done during slot_reset */
6944 clear_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006945 pci_disable_device(pdev);
6946 rtnl_unlock();
6947
6948 /* Request a slot slot reset. */
6949 return PCI_ERS_RESULT_NEED_RESET;
6950}
6951
6952/**
6953 * bnxt_io_slot_reset - called after the pci bus has been reset.
6954 * @pdev: Pointer to PCI device
6955 *
6956 * Restart the card from scratch, as if from a cold-boot.
6957 * At this point, the card has exprienced a hard reset,
6958 * followed by fixups by BIOS, and has its config space
6959 * set up identically to what it was at cold boot.
6960 */
6961static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
6962{
6963 struct net_device *netdev = pci_get_drvdata(pdev);
6964 struct bnxt *bp = netdev_priv(netdev);
6965 int err = 0;
6966 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
6967
6968 netdev_info(bp->dev, "PCI Slot Reset\n");
6969
6970 rtnl_lock();
6971
6972 if (pci_enable_device(pdev)) {
6973 dev_err(&pdev->dev,
6974 "Cannot re-enable PCI device after reset.\n");
6975 } else {
6976 pci_set_master(pdev);
6977
6978 if (netif_running(netdev))
6979 err = bnxt_open(netdev);
6980
6981 if (!err)
6982 result = PCI_ERS_RESULT_RECOVERED;
6983 }
6984
6985 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
6986 dev_close(netdev);
6987
6988 rtnl_unlock();
6989
6990 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6991 if (err) {
6992 dev_err(&pdev->dev,
6993 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
6994 err); /* non-fatal, continue */
6995 }
6996
6997 return PCI_ERS_RESULT_RECOVERED;
6998}
6999
7000/**
7001 * bnxt_io_resume - called when traffic can start flowing again.
7002 * @pdev: Pointer to PCI device
7003 *
7004 * This callback is called when the error recovery driver tells
7005 * us that its OK to resume normal operation.
7006 */
7007static void bnxt_io_resume(struct pci_dev *pdev)
7008{
7009 struct net_device *netdev = pci_get_drvdata(pdev);
7010
7011 rtnl_lock();
7012
7013 netif_device_attach(netdev);
7014
7015 rtnl_unlock();
7016}
7017
7018static const struct pci_error_handlers bnxt_err_handler = {
7019 .error_detected = bnxt_io_error_detected,
7020 .slot_reset = bnxt_io_slot_reset,
7021 .resume = bnxt_io_resume
7022};
7023
Michael Chanc0c050c2015-10-22 16:01:17 -04007024static struct pci_driver bnxt_pci_driver = {
7025 .name = DRV_MODULE_NAME,
7026 .id_table = bnxt_pci_tbl,
7027 .probe = bnxt_init_one,
7028 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05007029 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04007030#if defined(CONFIG_BNXT_SRIOV)
7031 .sriov_configure = bnxt_sriov_configure,
7032#endif
7033};
7034
7035module_pci_driver(bnxt_pci_driver);