blob: 05a93a7efd8c96b13f527392dc576d3ae4b37765 [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
3 * Copyright (c) 2014-2015 Broadcom Corporation
4 *
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>
40#if defined(CONFIG_VXLAN) || defined(CONFIG_VXLAN_MODULE)
41#include <net/vxlan.h>
42#endif
43#ifdef CONFIG_NET_RX_BUSY_POLL
44#include <net/busy_poll.h>
45#endif
46#include <linux/workqueue.h>
47#include <linux/prefetch.h>
48#include <linux/cache.h>
49#include <linux/log2.h>
50#include <linux/aer.h>
51#include <linux/bitmap.h>
52#include <linux/cpu_rmap.h>
53
54#include "bnxt_hsi.h"
55#include "bnxt.h"
56#include "bnxt_sriov.h"
57#include "bnxt_ethtool.h"
58
59#define BNXT_TX_TIMEOUT (5 * HZ)
60
61static const char version[] =
62 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
63
64MODULE_LICENSE("GPL");
65MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
66MODULE_VERSION(DRV_MODULE_VERSION);
67
68#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
69#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
70#define BNXT_RX_COPY_THRESH 256
71
Michael Chan4419dbe2016-02-10 17:33:49 -050072#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040073
74enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050075 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040076 BCM57302,
77 BCM57304,
David Christensenfbc9a522015-12-27 18:19:29 -050078 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040079 BCM57404,
80 BCM57406,
81 BCM57304_VF,
82 BCM57404_VF,
83};
84
85/* indexed by enum above */
86static const struct {
87 char *name;
88} board_info[] = {
David Christensenfbc9a522015-12-27 18:19:29 -050089 { "Broadcom BCM57301 NetXtreme-C Single-port 10Gb Ethernet" },
90 { "Broadcom BCM57302 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -040091 { "Broadcom BCM57304 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
David Christensenfbc9a522015-12-27 18:19:29 -050092 { "Broadcom BCM57402 NetXtreme-E Dual-port 10Gb Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -040093 { "Broadcom BCM57404 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
David Christensenfbc9a522015-12-27 18:19:29 -050094 { "Broadcom BCM57406 NetXtreme-E Dual-port 10GBase-T Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -040095 { "Broadcom BCM57304 NetXtreme-C Ethernet Virtual Function" },
96 { "Broadcom BCM57404 NetXtreme-E Ethernet Virtual Function" },
97};
98
99static const struct pci_device_id bnxt_pci_tbl[] = {
David Christensenfbc9a522015-12-27 18:19:29 -0500100 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400101 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
102 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
David Christensenfbc9a522015-12-27 18:19:29 -0500103 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400104 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
105 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
106#ifdef CONFIG_BNXT_SRIOV
107 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = BCM57304_VF },
108 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = BCM57404_VF },
109#endif
110 { 0 }
111};
112
113MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
114
115static const u16 bnxt_vf_req_snif[] = {
116 HWRM_FUNC_CFG,
117 HWRM_PORT_PHY_QCFG,
118 HWRM_CFA_L2_FILTER_ALLOC,
119};
120
121static bool bnxt_vf_pciid(enum board_idx idx)
122{
123 return (idx == BCM57304_VF || idx == BCM57404_VF);
124}
125
126#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
127#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
128#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
129
130#define BNXT_CP_DB_REARM(db, raw_cons) \
131 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
132
133#define BNXT_CP_DB(db, raw_cons) \
134 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
135
136#define BNXT_CP_DB_IRQ_DIS(db) \
137 writel(DB_CP_IRQ_DIS_FLAGS, db)
138
139static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
140{
141 /* Tell compiler to fetch tx indices from memory. */
142 barrier();
143
144 return bp->tx_ring_size -
145 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
146}
147
148static const u16 bnxt_lhint_arr[] = {
149 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
150 TX_BD_FLAGS_LHINT_512_TO_1023,
151 TX_BD_FLAGS_LHINT_1024_TO_2047,
152 TX_BD_FLAGS_LHINT_1024_TO_2047,
153 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
154 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
155 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
156 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
157 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
158 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
159 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
160 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
161 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
162 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
163 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
164 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
165 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
166 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
167 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
168};
169
170static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
171{
172 struct bnxt *bp = netdev_priv(dev);
173 struct tx_bd *txbd;
174 struct tx_bd_ext *txbd1;
175 struct netdev_queue *txq;
176 int i;
177 dma_addr_t mapping;
178 unsigned int length, pad = 0;
179 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
180 u16 prod, last_frag;
181 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400182 struct bnxt_tx_ring_info *txr;
183 struct bnxt_sw_tx_bd *tx_buf;
184
185 i = skb_get_queue_mapping(skb);
186 if (unlikely(i >= bp->tx_nr_rings)) {
187 dev_kfree_skb_any(skb);
188 return NETDEV_TX_OK;
189 }
190
Michael Chanb6ab4b02016-01-02 23:44:59 -0500191 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400192 txq = netdev_get_tx_queue(dev, i);
193 prod = txr->tx_prod;
194
195 free_size = bnxt_tx_avail(bp, txr);
196 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
197 netif_tx_stop_queue(txq);
198 return NETDEV_TX_BUSY;
199 }
200
201 length = skb->len;
202 len = skb_headlen(skb);
203 last_frag = skb_shinfo(skb)->nr_frags;
204
205 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
206
207 txbd->tx_bd_opaque = prod;
208
209 tx_buf = &txr->tx_buf_ring[prod];
210 tx_buf->skb = skb;
211 tx_buf->nr_frags = last_frag;
212
213 vlan_tag_flags = 0;
214 cfa_action = 0;
215 if (skb_vlan_tag_present(skb)) {
216 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
217 skb_vlan_tag_get(skb);
218 /* Currently supports 8021Q, 8021AD vlan offloads
219 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
220 */
221 if (skb->vlan_proto == htons(ETH_P_8021Q))
222 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
223 }
224
225 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500226 struct tx_push_buffer *tx_push_buf = txr->tx_push;
227 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
228 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
229 void *pdata = tx_push_buf->data;
230 u64 *end;
231 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400232
233 /* Set COAL_NOW to be ready quickly for the next push */
234 tx_push->tx_bd_len_flags_type =
235 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
236 TX_BD_TYPE_LONG_TX_BD |
237 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
238 TX_BD_FLAGS_COAL_NOW |
239 TX_BD_FLAGS_PACKET_END |
240 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
241
242 if (skb->ip_summed == CHECKSUM_PARTIAL)
243 tx_push1->tx_bd_hsize_lflags =
244 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
245 else
246 tx_push1->tx_bd_hsize_lflags = 0;
247
248 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
249 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
250
Michael Chan4419dbe2016-02-10 17:33:49 -0500251 end = PTR_ALIGN(pdata + length + 1, 8) - 1;
252 *end = 0;
253
Michael Chanc0c050c2015-10-22 16:01:17 -0400254 skb_copy_from_linear_data(skb, pdata, len);
255 pdata += len;
256 for (j = 0; j < last_frag; j++) {
257 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
258 void *fptr;
259
260 fptr = skb_frag_address_safe(frag);
261 if (!fptr)
262 goto normal_tx;
263
264 memcpy(pdata, fptr, skb_frag_size(frag));
265 pdata += skb_frag_size(frag);
266 }
267
Michael Chan4419dbe2016-02-10 17:33:49 -0500268 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
269 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400270 prod = NEXT_TX(prod);
271 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
272 memcpy(txbd, tx_push1, sizeof(*txbd));
273 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500274 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400275 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
276 txr->tx_prod = prod;
277
278 netdev_tx_sent_queue(txq, skb->len);
279
Michael Chan4419dbe2016-02-10 17:33:49 -0500280 push_len = (length + sizeof(*tx_push) + 7) / 8;
281 if (push_len > 16) {
282 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
283 __iowrite64_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
284 push_len - 16);
285 } else {
286 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
287 push_len);
288 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400289
290 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400291 goto tx_done;
292 }
293
294normal_tx:
295 if (length < BNXT_MIN_PKT_SIZE) {
296 pad = BNXT_MIN_PKT_SIZE - length;
297 if (skb_pad(skb, pad)) {
298 /* SKB already freed. */
299 tx_buf->skb = NULL;
300 return NETDEV_TX_OK;
301 }
302 length = BNXT_MIN_PKT_SIZE;
303 }
304
305 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
306
307 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
308 dev_kfree_skb_any(skb);
309 tx_buf->skb = NULL;
310 return NETDEV_TX_OK;
311 }
312
313 dma_unmap_addr_set(tx_buf, mapping, mapping);
314 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
315 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
316
317 txbd->tx_bd_haddr = cpu_to_le64(mapping);
318
319 prod = NEXT_TX(prod);
320 txbd1 = (struct tx_bd_ext *)
321 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
322
323 txbd1->tx_bd_hsize_lflags = 0;
324 if (skb_is_gso(skb)) {
325 u32 hdr_len;
326
327 if (skb->encapsulation)
328 hdr_len = skb_inner_network_offset(skb) +
329 skb_inner_network_header_len(skb) +
330 inner_tcp_hdrlen(skb);
331 else
332 hdr_len = skb_transport_offset(skb) +
333 tcp_hdrlen(skb);
334
335 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
336 TX_BD_FLAGS_T_IPID |
337 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
338 length = skb_shinfo(skb)->gso_size;
339 txbd1->tx_bd_mss = cpu_to_le32(length);
340 length += hdr_len;
341 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
342 txbd1->tx_bd_hsize_lflags =
343 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
344 txbd1->tx_bd_mss = 0;
345 }
346
347 length >>= 9;
348 flags |= bnxt_lhint_arr[length];
349 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
350
351 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
352 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
353 for (i = 0; i < last_frag; i++) {
354 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
355
356 prod = NEXT_TX(prod);
357 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
358
359 len = skb_frag_size(frag);
360 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
361 DMA_TO_DEVICE);
362
363 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
364 goto tx_dma_error;
365
366 tx_buf = &txr->tx_buf_ring[prod];
367 dma_unmap_addr_set(tx_buf, mapping, mapping);
368
369 txbd->tx_bd_haddr = cpu_to_le64(mapping);
370
371 flags = len << TX_BD_LEN_SHIFT;
372 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
373 }
374
375 flags &= ~TX_BD_LEN;
376 txbd->tx_bd_len_flags_type =
377 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
378 TX_BD_FLAGS_PACKET_END);
379
380 netdev_tx_sent_queue(txq, skb->len);
381
382 /* Sync BD data before updating doorbell */
383 wmb();
384
385 prod = NEXT_TX(prod);
386 txr->tx_prod = prod;
387
388 writel(DB_KEY_TX | prod, txr->tx_doorbell);
389 writel(DB_KEY_TX | prod, txr->tx_doorbell);
390
391tx_done:
392
393 mmiowb();
394
395 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
396 netif_tx_stop_queue(txq);
397
398 /* netif_tx_stop_queue() must be done before checking
399 * tx index in bnxt_tx_avail() below, because in
400 * bnxt_tx_int(), we update tx index before checking for
401 * netif_tx_queue_stopped().
402 */
403 smp_mb();
404 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
405 netif_tx_wake_queue(txq);
406 }
407 return NETDEV_TX_OK;
408
409tx_dma_error:
410 last_frag = i;
411
412 /* start back at beginning and unmap skb */
413 prod = txr->tx_prod;
414 tx_buf = &txr->tx_buf_ring[prod];
415 tx_buf->skb = NULL;
416 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
417 skb_headlen(skb), PCI_DMA_TODEVICE);
418 prod = NEXT_TX(prod);
419
420 /* unmap remaining mapped pages */
421 for (i = 0; i < last_frag; i++) {
422 prod = NEXT_TX(prod);
423 tx_buf = &txr->tx_buf_ring[prod];
424 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
425 skb_frag_size(&skb_shinfo(skb)->frags[i]),
426 PCI_DMA_TODEVICE);
427 }
428
429 dev_kfree_skb_any(skb);
430 return NETDEV_TX_OK;
431}
432
433static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
434{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500435 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500436 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400437 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
438 u16 cons = txr->tx_cons;
439 struct pci_dev *pdev = bp->pdev;
440 int i;
441 unsigned int tx_bytes = 0;
442
443 for (i = 0; i < nr_pkts; i++) {
444 struct bnxt_sw_tx_bd *tx_buf;
445 struct sk_buff *skb;
446 int j, last;
447
448 tx_buf = &txr->tx_buf_ring[cons];
449 cons = NEXT_TX(cons);
450 skb = tx_buf->skb;
451 tx_buf->skb = NULL;
452
453 if (tx_buf->is_push) {
454 tx_buf->is_push = 0;
455 goto next_tx_int;
456 }
457
458 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
459 skb_headlen(skb), PCI_DMA_TODEVICE);
460 last = tx_buf->nr_frags;
461
462 for (j = 0; j < last; j++) {
463 cons = NEXT_TX(cons);
464 tx_buf = &txr->tx_buf_ring[cons];
465 dma_unmap_page(
466 &pdev->dev,
467 dma_unmap_addr(tx_buf, mapping),
468 skb_frag_size(&skb_shinfo(skb)->frags[j]),
469 PCI_DMA_TODEVICE);
470 }
471
472next_tx_int:
473 cons = NEXT_TX(cons);
474
475 tx_bytes += skb->len;
476 dev_kfree_skb_any(skb);
477 }
478
479 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
480 txr->tx_cons = cons;
481
482 /* Need to make the tx_cons update visible to bnxt_start_xmit()
483 * before checking for netif_tx_queue_stopped(). Without the
484 * memory barrier, there is a small possibility that bnxt_start_xmit()
485 * will miss it and cause the queue to be stopped forever.
486 */
487 smp_mb();
488
489 if (unlikely(netif_tx_queue_stopped(txq)) &&
490 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
491 __netif_tx_lock(txq, smp_processor_id());
492 if (netif_tx_queue_stopped(txq) &&
493 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
494 txr->dev_state != BNXT_DEV_STATE_CLOSING)
495 netif_tx_wake_queue(txq);
496 __netif_tx_unlock(txq);
497 }
498}
499
500static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
501 gfp_t gfp)
502{
503 u8 *data;
504 struct pci_dev *pdev = bp->pdev;
505
506 data = kmalloc(bp->rx_buf_size, gfp);
507 if (!data)
508 return NULL;
509
510 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
511 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
512
513 if (dma_mapping_error(&pdev->dev, *mapping)) {
514 kfree(data);
515 data = NULL;
516 }
517 return data;
518}
519
520static inline int bnxt_alloc_rx_data(struct bnxt *bp,
521 struct bnxt_rx_ring_info *rxr,
522 u16 prod, gfp_t gfp)
523{
524 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
525 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
526 u8 *data;
527 dma_addr_t mapping;
528
529 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
530 if (!data)
531 return -ENOMEM;
532
533 rx_buf->data = data;
534 dma_unmap_addr_set(rx_buf, mapping, mapping);
535
536 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
537
538 return 0;
539}
540
541static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
542 u8 *data)
543{
544 u16 prod = rxr->rx_prod;
545 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
546 struct rx_bd *cons_bd, *prod_bd;
547
548 prod_rx_buf = &rxr->rx_buf_ring[prod];
549 cons_rx_buf = &rxr->rx_buf_ring[cons];
550
551 prod_rx_buf->data = data;
552
553 dma_unmap_addr_set(prod_rx_buf, mapping,
554 dma_unmap_addr(cons_rx_buf, mapping));
555
556 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
557 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
558
559 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
560}
561
562static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
563{
564 u16 next, max = rxr->rx_agg_bmap_size;
565
566 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
567 if (next >= max)
568 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
569 return next;
570}
571
572static inline int bnxt_alloc_rx_page(struct bnxt *bp,
573 struct bnxt_rx_ring_info *rxr,
574 u16 prod, gfp_t gfp)
575{
576 struct rx_bd *rxbd =
577 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
578 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
579 struct pci_dev *pdev = bp->pdev;
580 struct page *page;
581 dma_addr_t mapping;
582 u16 sw_prod = rxr->rx_sw_agg_prod;
583
584 page = alloc_page(gfp);
585 if (!page)
586 return -ENOMEM;
587
588 mapping = dma_map_page(&pdev->dev, page, 0, PAGE_SIZE,
589 PCI_DMA_FROMDEVICE);
590 if (dma_mapping_error(&pdev->dev, mapping)) {
591 __free_page(page);
592 return -EIO;
593 }
594
595 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
596 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
597
598 __set_bit(sw_prod, rxr->rx_agg_bmap);
599 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
600 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
601
602 rx_agg_buf->page = page;
603 rx_agg_buf->mapping = mapping;
604 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
605 rxbd->rx_bd_opaque = sw_prod;
606 return 0;
607}
608
609static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
610 u32 agg_bufs)
611{
612 struct bnxt *bp = bnapi->bp;
613 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500614 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400615 u16 prod = rxr->rx_agg_prod;
616 u16 sw_prod = rxr->rx_sw_agg_prod;
617 u32 i;
618
619 for (i = 0; i < agg_bufs; i++) {
620 u16 cons;
621 struct rx_agg_cmp *agg;
622 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
623 struct rx_bd *prod_bd;
624 struct page *page;
625
626 agg = (struct rx_agg_cmp *)
627 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
628 cons = agg->rx_agg_cmp_opaque;
629 __clear_bit(cons, rxr->rx_agg_bmap);
630
631 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
632 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
633
634 __set_bit(sw_prod, rxr->rx_agg_bmap);
635 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
636 cons_rx_buf = &rxr->rx_agg_ring[cons];
637
638 /* It is possible for sw_prod to be equal to cons, so
639 * set cons_rx_buf->page to NULL first.
640 */
641 page = cons_rx_buf->page;
642 cons_rx_buf->page = NULL;
643 prod_rx_buf->page = page;
644
645 prod_rx_buf->mapping = cons_rx_buf->mapping;
646
647 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
648
649 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
650 prod_bd->rx_bd_opaque = sw_prod;
651
652 prod = NEXT_RX_AGG(prod);
653 sw_prod = NEXT_RX_AGG(sw_prod);
654 cp_cons = NEXT_CMP(cp_cons);
655 }
656 rxr->rx_agg_prod = prod;
657 rxr->rx_sw_agg_prod = sw_prod;
658}
659
660static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
661 struct bnxt_rx_ring_info *rxr, u16 cons,
662 u16 prod, u8 *data, dma_addr_t dma_addr,
663 unsigned int len)
664{
665 int err;
666 struct sk_buff *skb;
667
668 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
669 if (unlikely(err)) {
670 bnxt_reuse_rx_data(rxr, cons, data);
671 return NULL;
672 }
673
674 skb = build_skb(data, 0);
675 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
676 PCI_DMA_FROMDEVICE);
677 if (!skb) {
678 kfree(data);
679 return NULL;
680 }
681
682 skb_reserve(skb, BNXT_RX_OFFSET);
683 skb_put(skb, len);
684 return skb;
685}
686
687static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
688 struct sk_buff *skb, u16 cp_cons,
689 u32 agg_bufs)
690{
691 struct pci_dev *pdev = bp->pdev;
692 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500693 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400694 u16 prod = rxr->rx_agg_prod;
695 u32 i;
696
697 for (i = 0; i < agg_bufs; i++) {
698 u16 cons, frag_len;
699 struct rx_agg_cmp *agg;
700 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
701 struct page *page;
702 dma_addr_t mapping;
703
704 agg = (struct rx_agg_cmp *)
705 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
706 cons = agg->rx_agg_cmp_opaque;
707 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
708 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
709
710 cons_rx_buf = &rxr->rx_agg_ring[cons];
711 skb_fill_page_desc(skb, i, cons_rx_buf->page, 0, frag_len);
712 __clear_bit(cons, rxr->rx_agg_bmap);
713
714 /* It is possible for bnxt_alloc_rx_page() to allocate
715 * a sw_prod index that equals the cons index, so we
716 * need to clear the cons entry now.
717 */
718 mapping = dma_unmap_addr(cons_rx_buf, mapping);
719 page = cons_rx_buf->page;
720 cons_rx_buf->page = NULL;
721
722 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
723 struct skb_shared_info *shinfo;
724 unsigned int nr_frags;
725
726 shinfo = skb_shinfo(skb);
727 nr_frags = --shinfo->nr_frags;
728 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
729
730 dev_kfree_skb(skb);
731
732 cons_rx_buf->page = page;
733
734 /* Update prod since possibly some pages have been
735 * allocated already.
736 */
737 rxr->rx_agg_prod = prod;
738 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
739 return NULL;
740 }
741
742 dma_unmap_page(&pdev->dev, mapping, PAGE_SIZE,
743 PCI_DMA_FROMDEVICE);
744
745 skb->data_len += frag_len;
746 skb->len += frag_len;
747 skb->truesize += PAGE_SIZE;
748
749 prod = NEXT_RX_AGG(prod);
750 cp_cons = NEXT_CMP(cp_cons);
751 }
752 rxr->rx_agg_prod = prod;
753 return skb;
754}
755
756static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
757 u8 agg_bufs, u32 *raw_cons)
758{
759 u16 last;
760 struct rx_agg_cmp *agg;
761
762 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
763 last = RING_CMP(*raw_cons);
764 agg = (struct rx_agg_cmp *)
765 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
766 return RX_AGG_CMP_VALID(agg, *raw_cons);
767}
768
769static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
770 unsigned int len,
771 dma_addr_t mapping)
772{
773 struct bnxt *bp = bnapi->bp;
774 struct pci_dev *pdev = bp->pdev;
775 struct sk_buff *skb;
776
777 skb = napi_alloc_skb(&bnapi->napi, len);
778 if (!skb)
779 return NULL;
780
781 dma_sync_single_for_cpu(&pdev->dev, mapping,
782 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
783
784 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
785
786 dma_sync_single_for_device(&pdev->dev, mapping,
787 bp->rx_copy_thresh,
788 PCI_DMA_FROMDEVICE);
789
790 skb_put(skb, len);
791 return skb;
792}
793
794static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
795 struct rx_tpa_start_cmp *tpa_start,
796 struct rx_tpa_start_cmp_ext *tpa_start1)
797{
798 u8 agg_id = TPA_START_AGG_ID(tpa_start);
799 u16 cons, prod;
800 struct bnxt_tpa_info *tpa_info;
801 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
802 struct rx_bd *prod_bd;
803 dma_addr_t mapping;
804
805 cons = tpa_start->rx_tpa_start_cmp_opaque;
806 prod = rxr->rx_prod;
807 cons_rx_buf = &rxr->rx_buf_ring[cons];
808 prod_rx_buf = &rxr->rx_buf_ring[prod];
809 tpa_info = &rxr->rx_tpa[agg_id];
810
811 prod_rx_buf->data = tpa_info->data;
812
813 mapping = tpa_info->mapping;
814 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
815
816 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
817
818 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
819
820 tpa_info->data = cons_rx_buf->data;
821 cons_rx_buf->data = NULL;
822 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
823
824 tpa_info->len =
825 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
826 RX_TPA_START_CMP_LEN_SHIFT;
827 if (likely(TPA_START_HASH_VALID(tpa_start))) {
828 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
829
830 tpa_info->hash_type = PKT_HASH_TYPE_L4;
831 tpa_info->gso_type = SKB_GSO_TCPV4;
832 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
833 if (hash_type == 3)
834 tpa_info->gso_type = SKB_GSO_TCPV6;
835 tpa_info->rss_hash =
836 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
837 } else {
838 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
839 tpa_info->gso_type = 0;
840 if (netif_msg_rx_err(bp))
841 netdev_warn(bp->dev, "TPA packet without valid hash\n");
842 }
843 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
844 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
845
846 rxr->rx_prod = NEXT_RX(prod);
847 cons = NEXT_RX(cons);
848 cons_rx_buf = &rxr->rx_buf_ring[cons];
849
850 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
851 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
852 cons_rx_buf->data = NULL;
853}
854
855static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
856 u16 cp_cons, u32 agg_bufs)
857{
858 if (agg_bufs)
859 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
860}
861
862#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
863#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
864
865static inline struct sk_buff *bnxt_gro_skb(struct bnxt_tpa_info *tpa_info,
866 struct rx_tpa_end_cmp *tpa_end,
867 struct rx_tpa_end_cmp_ext *tpa_end1,
868 struct sk_buff *skb)
869{
Michael Chand1611c32015-10-25 22:27:57 -0400870#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -0400871 struct tcphdr *th;
872 int payload_off, tcp_opt_len = 0;
873 int len, nw_off;
Michael Chan27e24182015-12-27 18:19:23 -0500874 u16 segs;
Michael Chanc0c050c2015-10-22 16:01:17 -0400875
Michael Chan27e24182015-12-27 18:19:23 -0500876 segs = TPA_END_TPA_SEGS(tpa_end);
877 if (segs == 1)
878 return skb;
879
880 NAPI_GRO_CB(skb)->count = segs;
Michael Chanc0c050c2015-10-22 16:01:17 -0400881 skb_shinfo(skb)->gso_size =
882 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
883 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
884 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
885 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
886 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
887 if (TPA_END_GRO_TS(tpa_end))
888 tcp_opt_len = 12;
889
Michael Chanc0c050c2015-10-22 16:01:17 -0400890 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
891 struct iphdr *iph;
892
893 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
894 ETH_HLEN;
895 skb_set_network_header(skb, nw_off);
896 iph = ip_hdr(skb);
897 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
898 len = skb->len - skb_transport_offset(skb);
899 th = tcp_hdr(skb);
900 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
901 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
902 struct ipv6hdr *iph;
903
904 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
905 ETH_HLEN;
906 skb_set_network_header(skb, nw_off);
907 iph = ipv6_hdr(skb);
908 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
909 len = skb->len - skb_transport_offset(skb);
910 th = tcp_hdr(skb);
911 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
912 } else {
913 dev_kfree_skb_any(skb);
914 return NULL;
915 }
916 tcp_gro_complete(skb);
917
918 if (nw_off) { /* tunnel */
919 struct udphdr *uh = NULL;
920
921 if (skb->protocol == htons(ETH_P_IP)) {
922 struct iphdr *iph = (struct iphdr *)skb->data;
923
924 if (iph->protocol == IPPROTO_UDP)
925 uh = (struct udphdr *)(iph + 1);
926 } else {
927 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
928
929 if (iph->nexthdr == IPPROTO_UDP)
930 uh = (struct udphdr *)(iph + 1);
931 }
932 if (uh) {
933 if (uh->check)
934 skb_shinfo(skb)->gso_type |=
935 SKB_GSO_UDP_TUNNEL_CSUM;
936 else
937 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
938 }
939 }
940#endif
941 return skb;
942}
943
944static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
945 struct bnxt_napi *bnapi,
946 u32 *raw_cons,
947 struct rx_tpa_end_cmp *tpa_end,
948 struct rx_tpa_end_cmp_ext *tpa_end1,
949 bool *agg_event)
950{
951 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500952 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400953 u8 agg_id = TPA_END_AGG_ID(tpa_end);
954 u8 *data, agg_bufs;
955 u16 cp_cons = RING_CMP(*raw_cons);
956 unsigned int len;
957 struct bnxt_tpa_info *tpa_info;
958 dma_addr_t mapping;
959 struct sk_buff *skb;
960
961 tpa_info = &rxr->rx_tpa[agg_id];
962 data = tpa_info->data;
963 prefetch(data);
964 len = tpa_info->len;
965 mapping = tpa_info->mapping;
966
967 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
968 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
969
970 if (agg_bufs) {
971 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
972 return ERR_PTR(-EBUSY);
973
974 *agg_event = true;
975 cp_cons = NEXT_CMP(cp_cons);
976 }
977
978 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
979 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
980 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
981 agg_bufs, (int)MAX_SKB_FRAGS);
982 return NULL;
983 }
984
985 if (len <= bp->rx_copy_thresh) {
986 skb = bnxt_copy_skb(bnapi, data, len, mapping);
987 if (!skb) {
988 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
989 return NULL;
990 }
991 } else {
992 u8 *new_data;
993 dma_addr_t new_mapping;
994
995 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
996 if (!new_data) {
997 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
998 return NULL;
999 }
1000
1001 tpa_info->data = new_data;
1002 tpa_info->mapping = new_mapping;
1003
1004 skb = build_skb(data, 0);
1005 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1006 PCI_DMA_FROMDEVICE);
1007
1008 if (!skb) {
1009 kfree(data);
1010 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1011 return NULL;
1012 }
1013 skb_reserve(skb, BNXT_RX_OFFSET);
1014 skb_put(skb, len);
1015 }
1016
1017 if (agg_bufs) {
1018 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1019 if (!skb) {
1020 /* Page reuse already handled by bnxt_rx_pages(). */
1021 return NULL;
1022 }
1023 }
1024 skb->protocol = eth_type_trans(skb, bp->dev);
1025
1026 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1027 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1028
1029 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1030 netdev_features_t features = skb->dev->features;
1031 u16 vlan_proto = tpa_info->metadata >>
1032 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1033
1034 if (((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1035 vlan_proto == ETH_P_8021Q) ||
1036 ((features & NETIF_F_HW_VLAN_STAG_RX) &&
1037 vlan_proto == ETH_P_8021AD)) {
1038 __vlan_hwaccel_put_tag(skb, htons(vlan_proto),
1039 tpa_info->metadata &
1040 RX_CMP_FLAGS2_METADATA_VID_MASK);
1041 }
1042 }
1043
1044 skb_checksum_none_assert(skb);
1045 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1046 skb->ip_summed = CHECKSUM_UNNECESSARY;
1047 skb->csum_level =
1048 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1049 }
1050
1051 if (TPA_END_GRO(tpa_end))
1052 skb = bnxt_gro_skb(tpa_info, tpa_end, tpa_end1, skb);
1053
1054 return skb;
1055}
1056
1057/* returns the following:
1058 * 1 - 1 packet successfully received
1059 * 0 - successful TPA_START, packet not completed yet
1060 * -EBUSY - completion ring does not have all the agg buffers yet
1061 * -ENOMEM - packet aborted due to out of memory
1062 * -EIO - packet aborted due to hw error indicated in BD
1063 */
1064static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1065 bool *agg_event)
1066{
1067 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001068 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001069 struct net_device *dev = bp->dev;
1070 struct rx_cmp *rxcmp;
1071 struct rx_cmp_ext *rxcmp1;
1072 u32 tmp_raw_cons = *raw_cons;
1073 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1074 struct bnxt_sw_rx_bd *rx_buf;
1075 unsigned int len;
1076 u8 *data, agg_bufs, cmp_type;
1077 dma_addr_t dma_addr;
1078 struct sk_buff *skb;
1079 int rc = 0;
1080
1081 rxcmp = (struct rx_cmp *)
1082 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1083
1084 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1085 cp_cons = RING_CMP(tmp_raw_cons);
1086 rxcmp1 = (struct rx_cmp_ext *)
1087 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1088
1089 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1090 return -EBUSY;
1091
1092 cmp_type = RX_CMP_TYPE(rxcmp);
1093
1094 prod = rxr->rx_prod;
1095
1096 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1097 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1098 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1099
1100 goto next_rx_no_prod;
1101
1102 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1103 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1104 (struct rx_tpa_end_cmp *)rxcmp,
1105 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1106 agg_event);
1107
1108 if (unlikely(IS_ERR(skb)))
1109 return -EBUSY;
1110
1111 rc = -ENOMEM;
1112 if (likely(skb)) {
1113 skb_record_rx_queue(skb, bnapi->index);
1114 skb_mark_napi_id(skb, &bnapi->napi);
1115 if (bnxt_busy_polling(bnapi))
1116 netif_receive_skb(skb);
1117 else
1118 napi_gro_receive(&bnapi->napi, skb);
1119 rc = 1;
1120 }
1121 goto next_rx_no_prod;
1122 }
1123
1124 cons = rxcmp->rx_cmp_opaque;
1125 rx_buf = &rxr->rx_buf_ring[cons];
1126 data = rx_buf->data;
1127 prefetch(data);
1128
1129 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1130 RX_CMP_AGG_BUFS_SHIFT;
1131
1132 if (agg_bufs) {
1133 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1134 return -EBUSY;
1135
1136 cp_cons = NEXT_CMP(cp_cons);
1137 *agg_event = true;
1138 }
1139
1140 rx_buf->data = NULL;
1141 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1142 bnxt_reuse_rx_data(rxr, cons, data);
1143 if (agg_bufs)
1144 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1145
1146 rc = -EIO;
1147 goto next_rx;
1148 }
1149
1150 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1151 dma_addr = dma_unmap_addr(rx_buf, mapping);
1152
1153 if (len <= bp->rx_copy_thresh) {
1154 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1155 bnxt_reuse_rx_data(rxr, cons, data);
1156 if (!skb) {
1157 rc = -ENOMEM;
1158 goto next_rx;
1159 }
1160 } else {
1161 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1162 if (!skb) {
1163 rc = -ENOMEM;
1164 goto next_rx;
1165 }
1166 }
1167
1168 if (agg_bufs) {
1169 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1170 if (!skb) {
1171 rc = -ENOMEM;
1172 goto next_rx;
1173 }
1174 }
1175
1176 if (RX_CMP_HASH_VALID(rxcmp)) {
1177 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1178 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1179
1180 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1181 if (hash_type != 1 && hash_type != 3)
1182 type = PKT_HASH_TYPE_L3;
1183 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1184 }
1185
1186 skb->protocol = eth_type_trans(skb, dev);
1187
1188 if (rxcmp1->rx_cmp_flags2 &
1189 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) {
1190 netdev_features_t features = skb->dev->features;
1191 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1192 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1193
1194 if (((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1195 vlan_proto == ETH_P_8021Q) ||
1196 ((features & NETIF_F_HW_VLAN_STAG_RX) &&
1197 vlan_proto == ETH_P_8021AD))
1198 __vlan_hwaccel_put_tag(skb, htons(vlan_proto),
1199 meta_data &
1200 RX_CMP_FLAGS2_METADATA_VID_MASK);
1201 }
1202
1203 skb_checksum_none_assert(skb);
1204 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1205 if (dev->features & NETIF_F_RXCSUM) {
1206 skb->ip_summed = CHECKSUM_UNNECESSARY;
1207 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1208 }
1209 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001210 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1211 if (dev->features & NETIF_F_RXCSUM)
1212 cpr->rx_l4_csum_errors++;
1213 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001214 }
1215
1216 skb_record_rx_queue(skb, bnapi->index);
1217 skb_mark_napi_id(skb, &bnapi->napi);
1218 if (bnxt_busy_polling(bnapi))
1219 netif_receive_skb(skb);
1220 else
1221 napi_gro_receive(&bnapi->napi, skb);
1222 rc = 1;
1223
1224next_rx:
1225 rxr->rx_prod = NEXT_RX(prod);
1226
1227next_rx_no_prod:
1228 *raw_cons = tmp_raw_cons;
1229
1230 return rc;
1231}
1232
1233static int bnxt_async_event_process(struct bnxt *bp,
1234 struct hwrm_async_event_cmpl *cmpl)
1235{
1236 u16 event_id = le16_to_cpu(cmpl->event_id);
1237
1238 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1239 switch (event_id) {
1240 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1241 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001242 break;
1243 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1244 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001245 break;
1246 default:
1247 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1248 event_id);
Jeffrey Huang19241362016-02-26 04:00:00 -05001249 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001250 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001251 schedule_work(&bp->sp_task);
1252async_event_process_exit:
Michael Chanc0c050c2015-10-22 16:01:17 -04001253 return 0;
1254}
1255
1256static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1257{
1258 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1259 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1260 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1261 (struct hwrm_fwd_req_cmpl *)txcmp;
1262
1263 switch (cmpl_type) {
1264 case CMPL_BASE_TYPE_HWRM_DONE:
1265 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1266 if (seq_id == bp->hwrm_intr_seq_id)
1267 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1268 else
1269 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1270 break;
1271
1272 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1273 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1274
1275 if ((vf_id < bp->pf.first_vf_id) ||
1276 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1277 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1278 vf_id);
1279 return -EINVAL;
1280 }
1281
1282 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1283 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1284 schedule_work(&bp->sp_task);
1285 break;
1286
1287 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1288 bnxt_async_event_process(bp,
1289 (struct hwrm_async_event_cmpl *)txcmp);
1290
1291 default:
1292 break;
1293 }
1294
1295 return 0;
1296}
1297
1298static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1299{
1300 struct bnxt_napi *bnapi = dev_instance;
1301 struct bnxt *bp = bnapi->bp;
1302 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1303 u32 cons = RING_CMP(cpr->cp_raw_cons);
1304
1305 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1306 napi_schedule(&bnapi->napi);
1307 return IRQ_HANDLED;
1308}
1309
1310static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1311{
1312 u32 raw_cons = cpr->cp_raw_cons;
1313 u16 cons = RING_CMP(raw_cons);
1314 struct tx_cmp *txcmp;
1315
1316 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1317
1318 return TX_CMP_VALID(txcmp, raw_cons);
1319}
1320
Michael Chanc0c050c2015-10-22 16:01:17 -04001321static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1322{
1323 struct bnxt_napi *bnapi = dev_instance;
1324 struct bnxt *bp = bnapi->bp;
1325 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1326 u32 cons = RING_CMP(cpr->cp_raw_cons);
1327 u32 int_status;
1328
1329 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1330
1331 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001332 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001333 /* return if erroneous interrupt */
1334 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1335 return IRQ_NONE;
1336 }
1337
1338 /* disable ring IRQ */
1339 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1340
1341 /* Return here if interrupt is shared and is disabled. */
1342 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1343 return IRQ_HANDLED;
1344
1345 napi_schedule(&bnapi->napi);
1346 return IRQ_HANDLED;
1347}
1348
1349static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1350{
1351 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1352 u32 raw_cons = cpr->cp_raw_cons;
1353 u32 cons;
1354 int tx_pkts = 0;
1355 int rx_pkts = 0;
1356 bool rx_event = false;
1357 bool agg_event = false;
1358 struct tx_cmp *txcmp;
1359
1360 while (1) {
1361 int rc;
1362
1363 cons = RING_CMP(raw_cons);
1364 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1365
1366 if (!TX_CMP_VALID(txcmp, raw_cons))
1367 break;
1368
1369 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1370 tx_pkts++;
1371 /* return full budget so NAPI will complete. */
1372 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1373 rx_pkts = budget;
1374 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1375 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1376 if (likely(rc >= 0))
1377 rx_pkts += rc;
1378 else if (rc == -EBUSY) /* partial completion */
1379 break;
1380 rx_event = true;
1381 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1382 CMPL_BASE_TYPE_HWRM_DONE) ||
1383 (TX_CMP_TYPE(txcmp) ==
1384 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1385 (TX_CMP_TYPE(txcmp) ==
1386 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1387 bnxt_hwrm_handler(bp, txcmp);
1388 }
1389 raw_cons = NEXT_RAW_CMP(raw_cons);
1390
1391 if (rx_pkts == budget)
1392 break;
1393 }
1394
1395 cpr->cp_raw_cons = raw_cons;
1396 /* ACK completion ring before freeing tx ring and producing new
1397 * buffers in rx/agg rings to prevent overflowing the completion
1398 * ring.
1399 */
1400 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1401
1402 if (tx_pkts)
1403 bnxt_tx_int(bp, bnapi, tx_pkts);
1404
1405 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001406 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001407
1408 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1409 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1410 if (agg_event) {
1411 writel(DB_KEY_RX | rxr->rx_agg_prod,
1412 rxr->rx_agg_doorbell);
1413 writel(DB_KEY_RX | rxr->rx_agg_prod,
1414 rxr->rx_agg_doorbell);
1415 }
1416 }
1417 return rx_pkts;
1418}
1419
1420static int bnxt_poll(struct napi_struct *napi, int budget)
1421{
1422 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1423 struct bnxt *bp = bnapi->bp;
1424 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1425 int work_done = 0;
1426
1427 if (!bnxt_lock_napi(bnapi))
1428 return budget;
1429
1430 while (1) {
1431 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1432
1433 if (work_done >= budget)
1434 break;
1435
1436 if (!bnxt_has_work(bp, cpr)) {
1437 napi_complete(napi);
1438 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1439 break;
1440 }
1441 }
1442 mmiowb();
1443 bnxt_unlock_napi(bnapi);
1444 return work_done;
1445}
1446
1447#ifdef CONFIG_NET_RX_BUSY_POLL
1448static int bnxt_busy_poll(struct napi_struct *napi)
1449{
1450 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1451 struct bnxt *bp = bnapi->bp;
1452 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1453 int rx_work, budget = 4;
1454
1455 if (atomic_read(&bp->intr_sem) != 0)
1456 return LL_FLUSH_FAILED;
1457
1458 if (!bnxt_lock_poll(bnapi))
1459 return LL_FLUSH_BUSY;
1460
1461 rx_work = bnxt_poll_work(bp, bnapi, budget);
1462
1463 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1464
1465 bnxt_unlock_poll(bnapi);
1466 return rx_work;
1467}
1468#endif
1469
1470static void bnxt_free_tx_skbs(struct bnxt *bp)
1471{
1472 int i, max_idx;
1473 struct pci_dev *pdev = bp->pdev;
1474
Michael Chanb6ab4b02016-01-02 23:44:59 -05001475 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001476 return;
1477
1478 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1479 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001480 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001481 int j;
1482
Michael Chanc0c050c2015-10-22 16:01:17 -04001483 for (j = 0; j < max_idx;) {
1484 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1485 struct sk_buff *skb = tx_buf->skb;
1486 int k, last;
1487
1488 if (!skb) {
1489 j++;
1490 continue;
1491 }
1492
1493 tx_buf->skb = NULL;
1494
1495 if (tx_buf->is_push) {
1496 dev_kfree_skb(skb);
1497 j += 2;
1498 continue;
1499 }
1500
1501 dma_unmap_single(&pdev->dev,
1502 dma_unmap_addr(tx_buf, mapping),
1503 skb_headlen(skb),
1504 PCI_DMA_TODEVICE);
1505
1506 last = tx_buf->nr_frags;
1507 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001508 for (k = 0; k < last; k++, j++) {
1509 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001510 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1511
Michael Chand612a572016-01-28 03:11:22 -05001512 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001513 dma_unmap_page(
1514 &pdev->dev,
1515 dma_unmap_addr(tx_buf, mapping),
1516 skb_frag_size(frag), PCI_DMA_TODEVICE);
1517 }
1518 dev_kfree_skb(skb);
1519 }
1520 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1521 }
1522}
1523
1524static void bnxt_free_rx_skbs(struct bnxt *bp)
1525{
1526 int i, max_idx, max_agg_idx;
1527 struct pci_dev *pdev = bp->pdev;
1528
Michael Chanb6ab4b02016-01-02 23:44:59 -05001529 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001530 return;
1531
1532 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1533 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1534 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001535 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001536 int j;
1537
Michael Chanc0c050c2015-10-22 16:01:17 -04001538 if (rxr->rx_tpa) {
1539 for (j = 0; j < MAX_TPA; j++) {
1540 struct bnxt_tpa_info *tpa_info =
1541 &rxr->rx_tpa[j];
1542 u8 *data = tpa_info->data;
1543
1544 if (!data)
1545 continue;
1546
1547 dma_unmap_single(
1548 &pdev->dev,
1549 dma_unmap_addr(tpa_info, mapping),
1550 bp->rx_buf_use_size,
1551 PCI_DMA_FROMDEVICE);
1552
1553 tpa_info->data = NULL;
1554
1555 kfree(data);
1556 }
1557 }
1558
1559 for (j = 0; j < max_idx; j++) {
1560 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1561 u8 *data = rx_buf->data;
1562
1563 if (!data)
1564 continue;
1565
1566 dma_unmap_single(&pdev->dev,
1567 dma_unmap_addr(rx_buf, mapping),
1568 bp->rx_buf_use_size,
1569 PCI_DMA_FROMDEVICE);
1570
1571 rx_buf->data = NULL;
1572
1573 kfree(data);
1574 }
1575
1576 for (j = 0; j < max_agg_idx; j++) {
1577 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1578 &rxr->rx_agg_ring[j];
1579 struct page *page = rx_agg_buf->page;
1580
1581 if (!page)
1582 continue;
1583
1584 dma_unmap_page(&pdev->dev,
1585 dma_unmap_addr(rx_agg_buf, mapping),
1586 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1587
1588 rx_agg_buf->page = NULL;
1589 __clear_bit(j, rxr->rx_agg_bmap);
1590
1591 __free_page(page);
1592 }
1593 }
1594}
1595
1596static void bnxt_free_skbs(struct bnxt *bp)
1597{
1598 bnxt_free_tx_skbs(bp);
1599 bnxt_free_rx_skbs(bp);
1600}
1601
1602static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1603{
1604 struct pci_dev *pdev = bp->pdev;
1605 int i;
1606
1607 for (i = 0; i < ring->nr_pages; i++) {
1608 if (!ring->pg_arr[i])
1609 continue;
1610
1611 dma_free_coherent(&pdev->dev, ring->page_size,
1612 ring->pg_arr[i], ring->dma_arr[i]);
1613
1614 ring->pg_arr[i] = NULL;
1615 }
1616 if (ring->pg_tbl) {
1617 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1618 ring->pg_tbl, ring->pg_tbl_map);
1619 ring->pg_tbl = NULL;
1620 }
1621 if (ring->vmem_size && *ring->vmem) {
1622 vfree(*ring->vmem);
1623 *ring->vmem = NULL;
1624 }
1625}
1626
1627static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1628{
1629 int i;
1630 struct pci_dev *pdev = bp->pdev;
1631
1632 if (ring->nr_pages > 1) {
1633 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1634 ring->nr_pages * 8,
1635 &ring->pg_tbl_map,
1636 GFP_KERNEL);
1637 if (!ring->pg_tbl)
1638 return -ENOMEM;
1639 }
1640
1641 for (i = 0; i < ring->nr_pages; i++) {
1642 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1643 ring->page_size,
1644 &ring->dma_arr[i],
1645 GFP_KERNEL);
1646 if (!ring->pg_arr[i])
1647 return -ENOMEM;
1648
1649 if (ring->nr_pages > 1)
1650 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1651 }
1652
1653 if (ring->vmem_size) {
1654 *ring->vmem = vzalloc(ring->vmem_size);
1655 if (!(*ring->vmem))
1656 return -ENOMEM;
1657 }
1658 return 0;
1659}
1660
1661static void bnxt_free_rx_rings(struct bnxt *bp)
1662{
1663 int i;
1664
Michael Chanb6ab4b02016-01-02 23:44:59 -05001665 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001666 return;
1667
1668 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001669 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001670 struct bnxt_ring_struct *ring;
1671
Michael Chanc0c050c2015-10-22 16:01:17 -04001672 kfree(rxr->rx_tpa);
1673 rxr->rx_tpa = NULL;
1674
1675 kfree(rxr->rx_agg_bmap);
1676 rxr->rx_agg_bmap = NULL;
1677
1678 ring = &rxr->rx_ring_struct;
1679 bnxt_free_ring(bp, ring);
1680
1681 ring = &rxr->rx_agg_ring_struct;
1682 bnxt_free_ring(bp, ring);
1683 }
1684}
1685
1686static int bnxt_alloc_rx_rings(struct bnxt *bp)
1687{
1688 int i, rc, agg_rings = 0, tpa_rings = 0;
1689
Michael Chanb6ab4b02016-01-02 23:44:59 -05001690 if (!bp->rx_ring)
1691 return -ENOMEM;
1692
Michael Chanc0c050c2015-10-22 16:01:17 -04001693 if (bp->flags & BNXT_FLAG_AGG_RINGS)
1694 agg_rings = 1;
1695
1696 if (bp->flags & BNXT_FLAG_TPA)
1697 tpa_rings = 1;
1698
1699 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001700 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001701 struct bnxt_ring_struct *ring;
1702
Michael Chanc0c050c2015-10-22 16:01:17 -04001703 ring = &rxr->rx_ring_struct;
1704
1705 rc = bnxt_alloc_ring(bp, ring);
1706 if (rc)
1707 return rc;
1708
1709 if (agg_rings) {
1710 u16 mem_size;
1711
1712 ring = &rxr->rx_agg_ring_struct;
1713 rc = bnxt_alloc_ring(bp, ring);
1714 if (rc)
1715 return rc;
1716
1717 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
1718 mem_size = rxr->rx_agg_bmap_size / 8;
1719 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
1720 if (!rxr->rx_agg_bmap)
1721 return -ENOMEM;
1722
1723 if (tpa_rings) {
1724 rxr->rx_tpa = kcalloc(MAX_TPA,
1725 sizeof(struct bnxt_tpa_info),
1726 GFP_KERNEL);
1727 if (!rxr->rx_tpa)
1728 return -ENOMEM;
1729 }
1730 }
1731 }
1732 return 0;
1733}
1734
1735static void bnxt_free_tx_rings(struct bnxt *bp)
1736{
1737 int i;
1738 struct pci_dev *pdev = bp->pdev;
1739
Michael Chanb6ab4b02016-01-02 23:44:59 -05001740 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001741 return;
1742
1743 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001744 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001745 struct bnxt_ring_struct *ring;
1746
Michael Chanc0c050c2015-10-22 16:01:17 -04001747 if (txr->tx_push) {
1748 dma_free_coherent(&pdev->dev, bp->tx_push_size,
1749 txr->tx_push, txr->tx_push_mapping);
1750 txr->tx_push = NULL;
1751 }
1752
1753 ring = &txr->tx_ring_struct;
1754
1755 bnxt_free_ring(bp, ring);
1756 }
1757}
1758
1759static int bnxt_alloc_tx_rings(struct bnxt *bp)
1760{
1761 int i, j, rc;
1762 struct pci_dev *pdev = bp->pdev;
1763
1764 bp->tx_push_size = 0;
1765 if (bp->tx_push_thresh) {
1766 int push_size;
1767
1768 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
1769 bp->tx_push_thresh);
1770
Michael Chan4419dbe2016-02-10 17:33:49 -05001771 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001772 push_size = 0;
1773 bp->tx_push_thresh = 0;
1774 }
1775
1776 bp->tx_push_size = push_size;
1777 }
1778
1779 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001780 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001781 struct bnxt_ring_struct *ring;
1782
Michael Chanc0c050c2015-10-22 16:01:17 -04001783 ring = &txr->tx_ring_struct;
1784
1785 rc = bnxt_alloc_ring(bp, ring);
1786 if (rc)
1787 return rc;
1788
1789 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001790 dma_addr_t mapping;
1791
1792 /* One pre-allocated DMA buffer to backup
1793 * TX push operation
1794 */
1795 txr->tx_push = dma_alloc_coherent(&pdev->dev,
1796 bp->tx_push_size,
1797 &txr->tx_push_mapping,
1798 GFP_KERNEL);
1799
1800 if (!txr->tx_push)
1801 return -ENOMEM;
1802
Michael Chanc0c050c2015-10-22 16:01:17 -04001803 mapping = txr->tx_push_mapping +
1804 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05001805 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04001806
Michael Chan4419dbe2016-02-10 17:33:49 -05001807 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04001808 }
1809 ring->queue_id = bp->q_info[j].queue_id;
1810 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
1811 j++;
1812 }
1813 return 0;
1814}
1815
1816static void bnxt_free_cp_rings(struct bnxt *bp)
1817{
1818 int i;
1819
1820 if (!bp->bnapi)
1821 return;
1822
1823 for (i = 0; i < bp->cp_nr_rings; i++) {
1824 struct bnxt_napi *bnapi = bp->bnapi[i];
1825 struct bnxt_cp_ring_info *cpr;
1826 struct bnxt_ring_struct *ring;
1827
1828 if (!bnapi)
1829 continue;
1830
1831 cpr = &bnapi->cp_ring;
1832 ring = &cpr->cp_ring_struct;
1833
1834 bnxt_free_ring(bp, ring);
1835 }
1836}
1837
1838static int bnxt_alloc_cp_rings(struct bnxt *bp)
1839{
1840 int i, rc;
1841
1842 for (i = 0; i < bp->cp_nr_rings; i++) {
1843 struct bnxt_napi *bnapi = bp->bnapi[i];
1844 struct bnxt_cp_ring_info *cpr;
1845 struct bnxt_ring_struct *ring;
1846
1847 if (!bnapi)
1848 continue;
1849
1850 cpr = &bnapi->cp_ring;
1851 ring = &cpr->cp_ring_struct;
1852
1853 rc = bnxt_alloc_ring(bp, ring);
1854 if (rc)
1855 return rc;
1856 }
1857 return 0;
1858}
1859
1860static void bnxt_init_ring_struct(struct bnxt *bp)
1861{
1862 int i;
1863
1864 for (i = 0; i < bp->cp_nr_rings; i++) {
1865 struct bnxt_napi *bnapi = bp->bnapi[i];
1866 struct bnxt_cp_ring_info *cpr;
1867 struct bnxt_rx_ring_info *rxr;
1868 struct bnxt_tx_ring_info *txr;
1869 struct bnxt_ring_struct *ring;
1870
1871 if (!bnapi)
1872 continue;
1873
1874 cpr = &bnapi->cp_ring;
1875 ring = &cpr->cp_ring_struct;
1876 ring->nr_pages = bp->cp_nr_pages;
1877 ring->page_size = HW_CMPD_RING_SIZE;
1878 ring->pg_arr = (void **)cpr->cp_desc_ring;
1879 ring->dma_arr = cpr->cp_desc_mapping;
1880 ring->vmem_size = 0;
1881
Michael Chanb6ab4b02016-01-02 23:44:59 -05001882 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05001883 if (!rxr)
1884 goto skip_rx;
1885
Michael Chanc0c050c2015-10-22 16:01:17 -04001886 ring = &rxr->rx_ring_struct;
1887 ring->nr_pages = bp->rx_nr_pages;
1888 ring->page_size = HW_RXBD_RING_SIZE;
1889 ring->pg_arr = (void **)rxr->rx_desc_ring;
1890 ring->dma_arr = rxr->rx_desc_mapping;
1891 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
1892 ring->vmem = (void **)&rxr->rx_buf_ring;
1893
1894 ring = &rxr->rx_agg_ring_struct;
1895 ring->nr_pages = bp->rx_agg_nr_pages;
1896 ring->page_size = HW_RXBD_RING_SIZE;
1897 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
1898 ring->dma_arr = rxr->rx_agg_desc_mapping;
1899 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
1900 ring->vmem = (void **)&rxr->rx_agg_ring;
1901
Michael Chan3b2b7d92016-01-02 23:45:00 -05001902skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05001903 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05001904 if (!txr)
1905 continue;
1906
Michael Chanc0c050c2015-10-22 16:01:17 -04001907 ring = &txr->tx_ring_struct;
1908 ring->nr_pages = bp->tx_nr_pages;
1909 ring->page_size = HW_RXBD_RING_SIZE;
1910 ring->pg_arr = (void **)txr->tx_desc_ring;
1911 ring->dma_arr = txr->tx_desc_mapping;
1912 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
1913 ring->vmem = (void **)&txr->tx_buf_ring;
1914 }
1915}
1916
1917static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
1918{
1919 int i;
1920 u32 prod;
1921 struct rx_bd **rx_buf_ring;
1922
1923 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
1924 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
1925 int j;
1926 struct rx_bd *rxbd;
1927
1928 rxbd = rx_buf_ring[i];
1929 if (!rxbd)
1930 continue;
1931
1932 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
1933 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
1934 rxbd->rx_bd_opaque = prod;
1935 }
1936 }
1937}
1938
1939static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
1940{
1941 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04001942 struct bnxt_rx_ring_info *rxr;
1943 struct bnxt_ring_struct *ring;
1944 u32 prod, type;
1945 int i;
1946
Michael Chanc0c050c2015-10-22 16:01:17 -04001947 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
1948 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
1949
1950 if (NET_IP_ALIGN == 2)
1951 type |= RX_BD_FLAGS_SOP;
1952
Michael Chanb6ab4b02016-01-02 23:44:59 -05001953 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04001954 ring = &rxr->rx_ring_struct;
1955 bnxt_init_rxbd_pages(ring, type);
1956
1957 prod = rxr->rx_prod;
1958 for (i = 0; i < bp->rx_ring_size; i++) {
1959 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
1960 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
1961 ring_nr, i, bp->rx_ring_size);
1962 break;
1963 }
1964 prod = NEXT_RX(prod);
1965 }
1966 rxr->rx_prod = prod;
1967 ring->fw_ring_id = INVALID_HW_RING_ID;
1968
Michael Chanedd0c2c2015-12-27 18:19:19 -05001969 ring = &rxr->rx_agg_ring_struct;
1970 ring->fw_ring_id = INVALID_HW_RING_ID;
1971
Michael Chanc0c050c2015-10-22 16:01:17 -04001972 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
1973 return 0;
1974
Michael Chanc0c050c2015-10-22 16:01:17 -04001975 type = ((u32)PAGE_SIZE << RX_BD_LEN_SHIFT) |
1976 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
1977
1978 bnxt_init_rxbd_pages(ring, type);
1979
1980 prod = rxr->rx_agg_prod;
1981 for (i = 0; i < bp->rx_agg_ring_size; i++) {
1982 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
1983 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
1984 ring_nr, i, bp->rx_ring_size);
1985 break;
1986 }
1987 prod = NEXT_RX_AGG(prod);
1988 }
1989 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04001990
1991 if (bp->flags & BNXT_FLAG_TPA) {
1992 if (rxr->rx_tpa) {
1993 u8 *data;
1994 dma_addr_t mapping;
1995
1996 for (i = 0; i < MAX_TPA; i++) {
1997 data = __bnxt_alloc_rx_data(bp, &mapping,
1998 GFP_KERNEL);
1999 if (!data)
2000 return -ENOMEM;
2001
2002 rxr->rx_tpa[i].data = data;
2003 rxr->rx_tpa[i].mapping = mapping;
2004 }
2005 } else {
2006 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2007 return -ENOMEM;
2008 }
2009 }
2010
2011 return 0;
2012}
2013
2014static int bnxt_init_rx_rings(struct bnxt *bp)
2015{
2016 int i, rc = 0;
2017
2018 for (i = 0; i < bp->rx_nr_rings; i++) {
2019 rc = bnxt_init_one_rx_ring(bp, i);
2020 if (rc)
2021 break;
2022 }
2023
2024 return rc;
2025}
2026
2027static int bnxt_init_tx_rings(struct bnxt *bp)
2028{
2029 u16 i;
2030
2031 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2032 MAX_SKB_FRAGS + 1);
2033
2034 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002035 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002036 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2037
2038 ring->fw_ring_id = INVALID_HW_RING_ID;
2039 }
2040
2041 return 0;
2042}
2043
2044static void bnxt_free_ring_grps(struct bnxt *bp)
2045{
2046 kfree(bp->grp_info);
2047 bp->grp_info = NULL;
2048}
2049
2050static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2051{
2052 int i;
2053
2054 if (irq_re_init) {
2055 bp->grp_info = kcalloc(bp->cp_nr_rings,
2056 sizeof(struct bnxt_ring_grp_info),
2057 GFP_KERNEL);
2058 if (!bp->grp_info)
2059 return -ENOMEM;
2060 }
2061 for (i = 0; i < bp->cp_nr_rings; i++) {
2062 if (irq_re_init)
2063 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2064 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2065 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2066 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2067 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2068 }
2069 return 0;
2070}
2071
2072static void bnxt_free_vnics(struct bnxt *bp)
2073{
2074 kfree(bp->vnic_info);
2075 bp->vnic_info = NULL;
2076 bp->nr_vnics = 0;
2077}
2078
2079static int bnxt_alloc_vnics(struct bnxt *bp)
2080{
2081 int num_vnics = 1;
2082
2083#ifdef CONFIG_RFS_ACCEL
2084 if (bp->flags & BNXT_FLAG_RFS)
2085 num_vnics += bp->rx_nr_rings;
2086#endif
2087
2088 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2089 GFP_KERNEL);
2090 if (!bp->vnic_info)
2091 return -ENOMEM;
2092
2093 bp->nr_vnics = num_vnics;
2094 return 0;
2095}
2096
2097static void bnxt_init_vnics(struct bnxt *bp)
2098{
2099 int i;
2100
2101 for (i = 0; i < bp->nr_vnics; i++) {
2102 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2103
2104 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2105 vnic->fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
2106 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2107
2108 if (bp->vnic_info[i].rss_hash_key) {
2109 if (i == 0)
2110 prandom_bytes(vnic->rss_hash_key,
2111 HW_HASH_KEY_SIZE);
2112 else
2113 memcpy(vnic->rss_hash_key,
2114 bp->vnic_info[0].rss_hash_key,
2115 HW_HASH_KEY_SIZE);
2116 }
2117 }
2118}
2119
2120static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2121{
2122 int pages;
2123
2124 pages = ring_size / desc_per_pg;
2125
2126 if (!pages)
2127 return 1;
2128
2129 pages++;
2130
2131 while (pages & (pages - 1))
2132 pages++;
2133
2134 return pages;
2135}
2136
2137static void bnxt_set_tpa_flags(struct bnxt *bp)
2138{
2139 bp->flags &= ~BNXT_FLAG_TPA;
2140 if (bp->dev->features & NETIF_F_LRO)
2141 bp->flags |= BNXT_FLAG_LRO;
2142 if ((bp->dev->features & NETIF_F_GRO) && (bp->pdev->revision > 0))
2143 bp->flags |= BNXT_FLAG_GRO;
2144}
2145
2146/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2147 * be set on entry.
2148 */
2149void bnxt_set_ring_params(struct bnxt *bp)
2150{
2151 u32 ring_size, rx_size, rx_space;
2152 u32 agg_factor = 0, agg_ring_size = 0;
2153
2154 /* 8 for CRC and VLAN */
2155 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2156
2157 rx_space = rx_size + NET_SKB_PAD +
2158 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2159
2160 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2161 ring_size = bp->rx_ring_size;
2162 bp->rx_agg_ring_size = 0;
2163 bp->rx_agg_nr_pages = 0;
2164
2165 if (bp->flags & BNXT_FLAG_TPA)
2166 agg_factor = 4;
2167
2168 bp->flags &= ~BNXT_FLAG_JUMBO;
2169 if (rx_space > PAGE_SIZE) {
2170 u32 jumbo_factor;
2171
2172 bp->flags |= BNXT_FLAG_JUMBO;
2173 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2174 if (jumbo_factor > agg_factor)
2175 agg_factor = jumbo_factor;
2176 }
2177 agg_ring_size = ring_size * agg_factor;
2178
2179 if (agg_ring_size) {
2180 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2181 RX_DESC_CNT);
2182 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2183 u32 tmp = agg_ring_size;
2184
2185 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2186 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2187 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2188 tmp, agg_ring_size);
2189 }
2190 bp->rx_agg_ring_size = agg_ring_size;
2191 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2192 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2193 rx_space = rx_size + NET_SKB_PAD +
2194 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2195 }
2196
2197 bp->rx_buf_use_size = rx_size;
2198 bp->rx_buf_size = rx_space;
2199
2200 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2201 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2202
2203 ring_size = bp->tx_ring_size;
2204 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2205 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2206
2207 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2208 bp->cp_ring_size = ring_size;
2209
2210 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2211 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2212 bp->cp_nr_pages = MAX_CP_PAGES;
2213 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2214 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2215 ring_size, bp->cp_ring_size);
2216 }
2217 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2218 bp->cp_ring_mask = bp->cp_bit - 1;
2219}
2220
2221static void bnxt_free_vnic_attributes(struct bnxt *bp)
2222{
2223 int i;
2224 struct bnxt_vnic_info *vnic;
2225 struct pci_dev *pdev = bp->pdev;
2226
2227 if (!bp->vnic_info)
2228 return;
2229
2230 for (i = 0; i < bp->nr_vnics; i++) {
2231 vnic = &bp->vnic_info[i];
2232
2233 kfree(vnic->fw_grp_ids);
2234 vnic->fw_grp_ids = NULL;
2235
2236 kfree(vnic->uc_list);
2237 vnic->uc_list = NULL;
2238
2239 if (vnic->mc_list) {
2240 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2241 vnic->mc_list, vnic->mc_list_mapping);
2242 vnic->mc_list = NULL;
2243 }
2244
2245 if (vnic->rss_table) {
2246 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2247 vnic->rss_table,
2248 vnic->rss_table_dma_addr);
2249 vnic->rss_table = NULL;
2250 }
2251
2252 vnic->rss_hash_key = NULL;
2253 vnic->flags = 0;
2254 }
2255}
2256
2257static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2258{
2259 int i, rc = 0, size;
2260 struct bnxt_vnic_info *vnic;
2261 struct pci_dev *pdev = bp->pdev;
2262 int max_rings;
2263
2264 for (i = 0; i < bp->nr_vnics; i++) {
2265 vnic = &bp->vnic_info[i];
2266
2267 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2268 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2269
2270 if (mem_size > 0) {
2271 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2272 if (!vnic->uc_list) {
2273 rc = -ENOMEM;
2274 goto out;
2275 }
2276 }
2277 }
2278
2279 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2280 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2281 vnic->mc_list =
2282 dma_alloc_coherent(&pdev->dev,
2283 vnic->mc_list_size,
2284 &vnic->mc_list_mapping,
2285 GFP_KERNEL);
2286 if (!vnic->mc_list) {
2287 rc = -ENOMEM;
2288 goto out;
2289 }
2290 }
2291
2292 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2293 max_rings = bp->rx_nr_rings;
2294 else
2295 max_rings = 1;
2296
2297 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2298 if (!vnic->fw_grp_ids) {
2299 rc = -ENOMEM;
2300 goto out;
2301 }
2302
2303 /* Allocate rss table and hash key */
2304 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2305 &vnic->rss_table_dma_addr,
2306 GFP_KERNEL);
2307 if (!vnic->rss_table) {
2308 rc = -ENOMEM;
2309 goto out;
2310 }
2311
2312 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2313
2314 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2315 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2316 }
2317 return 0;
2318
2319out:
2320 return rc;
2321}
2322
2323static void bnxt_free_hwrm_resources(struct bnxt *bp)
2324{
2325 struct pci_dev *pdev = bp->pdev;
2326
2327 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2328 bp->hwrm_cmd_resp_dma_addr);
2329
2330 bp->hwrm_cmd_resp_addr = NULL;
2331 if (bp->hwrm_dbg_resp_addr) {
2332 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2333 bp->hwrm_dbg_resp_addr,
2334 bp->hwrm_dbg_resp_dma_addr);
2335
2336 bp->hwrm_dbg_resp_addr = NULL;
2337 }
2338}
2339
2340static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2341{
2342 struct pci_dev *pdev = bp->pdev;
2343
2344 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2345 &bp->hwrm_cmd_resp_dma_addr,
2346 GFP_KERNEL);
2347 if (!bp->hwrm_cmd_resp_addr)
2348 return -ENOMEM;
2349 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2350 HWRM_DBG_REG_BUF_SIZE,
2351 &bp->hwrm_dbg_resp_dma_addr,
2352 GFP_KERNEL);
2353 if (!bp->hwrm_dbg_resp_addr)
2354 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2355
2356 return 0;
2357}
2358
2359static void bnxt_free_stats(struct bnxt *bp)
2360{
2361 u32 size, i;
2362 struct pci_dev *pdev = bp->pdev;
2363
2364 if (!bp->bnapi)
2365 return;
2366
2367 size = sizeof(struct ctx_hw_stats);
2368
2369 for (i = 0; i < bp->cp_nr_rings; i++) {
2370 struct bnxt_napi *bnapi = bp->bnapi[i];
2371 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2372
2373 if (cpr->hw_stats) {
2374 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2375 cpr->hw_stats_map);
2376 cpr->hw_stats = NULL;
2377 }
2378 }
2379}
2380
2381static int bnxt_alloc_stats(struct bnxt *bp)
2382{
2383 u32 size, i;
2384 struct pci_dev *pdev = bp->pdev;
2385
2386 size = sizeof(struct ctx_hw_stats);
2387
2388 for (i = 0; i < bp->cp_nr_rings; i++) {
2389 struct bnxt_napi *bnapi = bp->bnapi[i];
2390 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2391
2392 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2393 &cpr->hw_stats_map,
2394 GFP_KERNEL);
2395 if (!cpr->hw_stats)
2396 return -ENOMEM;
2397
2398 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2399 }
2400 return 0;
2401}
2402
2403static void bnxt_clear_ring_indices(struct bnxt *bp)
2404{
2405 int i;
2406
2407 if (!bp->bnapi)
2408 return;
2409
2410 for (i = 0; i < bp->cp_nr_rings; i++) {
2411 struct bnxt_napi *bnapi = bp->bnapi[i];
2412 struct bnxt_cp_ring_info *cpr;
2413 struct bnxt_rx_ring_info *rxr;
2414 struct bnxt_tx_ring_info *txr;
2415
2416 if (!bnapi)
2417 continue;
2418
2419 cpr = &bnapi->cp_ring;
2420 cpr->cp_raw_cons = 0;
2421
Michael Chanb6ab4b02016-01-02 23:44:59 -05002422 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002423 if (txr) {
2424 txr->tx_prod = 0;
2425 txr->tx_cons = 0;
2426 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002427
Michael Chanb6ab4b02016-01-02 23:44:59 -05002428 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002429 if (rxr) {
2430 rxr->rx_prod = 0;
2431 rxr->rx_agg_prod = 0;
2432 rxr->rx_sw_agg_prod = 0;
2433 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002434 }
2435}
2436
2437static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2438{
2439#ifdef CONFIG_RFS_ACCEL
2440 int i;
2441
2442 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2443 * safe to delete the hash table.
2444 */
2445 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2446 struct hlist_head *head;
2447 struct hlist_node *tmp;
2448 struct bnxt_ntuple_filter *fltr;
2449
2450 head = &bp->ntp_fltr_hash_tbl[i];
2451 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2452 hlist_del(&fltr->hash);
2453 kfree(fltr);
2454 }
2455 }
2456 if (irq_reinit) {
2457 kfree(bp->ntp_fltr_bmap);
2458 bp->ntp_fltr_bmap = NULL;
2459 }
2460 bp->ntp_fltr_count = 0;
2461#endif
2462}
2463
2464static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2465{
2466#ifdef CONFIG_RFS_ACCEL
2467 int i, rc = 0;
2468
2469 if (!(bp->flags & BNXT_FLAG_RFS))
2470 return 0;
2471
2472 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2473 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2474
2475 bp->ntp_fltr_count = 0;
2476 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2477 GFP_KERNEL);
2478
2479 if (!bp->ntp_fltr_bmap)
2480 rc = -ENOMEM;
2481
2482 return rc;
2483#else
2484 return 0;
2485#endif
2486}
2487
2488static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2489{
2490 bnxt_free_vnic_attributes(bp);
2491 bnxt_free_tx_rings(bp);
2492 bnxt_free_rx_rings(bp);
2493 bnxt_free_cp_rings(bp);
2494 bnxt_free_ntp_fltrs(bp, irq_re_init);
2495 if (irq_re_init) {
2496 bnxt_free_stats(bp);
2497 bnxt_free_ring_grps(bp);
2498 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002499 kfree(bp->tx_ring);
2500 bp->tx_ring = NULL;
2501 kfree(bp->rx_ring);
2502 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002503 kfree(bp->bnapi);
2504 bp->bnapi = NULL;
2505 } else {
2506 bnxt_clear_ring_indices(bp);
2507 }
2508}
2509
2510static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2511{
Michael Chan01657bc2016-01-02 23:45:03 -05002512 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002513 void *bnapi;
2514
2515 if (irq_re_init) {
2516 /* Allocate bnapi mem pointer array and mem block for
2517 * all queues
2518 */
2519 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2520 bp->cp_nr_rings);
2521 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2522 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2523 if (!bnapi)
2524 return -ENOMEM;
2525
2526 bp->bnapi = bnapi;
2527 bnapi += arr_size;
2528 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2529 bp->bnapi[i] = bnapi;
2530 bp->bnapi[i]->index = i;
2531 bp->bnapi[i]->bp = bp;
2532 }
2533
Michael Chanb6ab4b02016-01-02 23:44:59 -05002534 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2535 sizeof(struct bnxt_rx_ring_info),
2536 GFP_KERNEL);
2537 if (!bp->rx_ring)
2538 return -ENOMEM;
2539
2540 for (i = 0; i < bp->rx_nr_rings; i++) {
2541 bp->rx_ring[i].bnapi = bp->bnapi[i];
2542 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2543 }
2544
2545 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2546 sizeof(struct bnxt_tx_ring_info),
2547 GFP_KERNEL);
2548 if (!bp->tx_ring)
2549 return -ENOMEM;
2550
Michael Chan01657bc2016-01-02 23:45:03 -05002551 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2552 j = 0;
2553 else
2554 j = bp->rx_nr_rings;
2555
2556 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2557 bp->tx_ring[i].bnapi = bp->bnapi[j];
2558 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002559 }
2560
Michael Chanc0c050c2015-10-22 16:01:17 -04002561 rc = bnxt_alloc_stats(bp);
2562 if (rc)
2563 goto alloc_mem_err;
2564
2565 rc = bnxt_alloc_ntp_fltrs(bp);
2566 if (rc)
2567 goto alloc_mem_err;
2568
2569 rc = bnxt_alloc_vnics(bp);
2570 if (rc)
2571 goto alloc_mem_err;
2572 }
2573
2574 bnxt_init_ring_struct(bp);
2575
2576 rc = bnxt_alloc_rx_rings(bp);
2577 if (rc)
2578 goto alloc_mem_err;
2579
2580 rc = bnxt_alloc_tx_rings(bp);
2581 if (rc)
2582 goto alloc_mem_err;
2583
2584 rc = bnxt_alloc_cp_rings(bp);
2585 if (rc)
2586 goto alloc_mem_err;
2587
2588 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2589 BNXT_VNIC_UCAST_FLAG;
2590 rc = bnxt_alloc_vnic_attributes(bp);
2591 if (rc)
2592 goto alloc_mem_err;
2593 return 0;
2594
2595alloc_mem_err:
2596 bnxt_free_mem(bp, true);
2597 return rc;
2598}
2599
2600void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2601 u16 cmpl_ring, u16 target_id)
2602{
2603 struct hwrm_cmd_req_hdr *req = request;
2604
2605 req->cmpl_ring_req_type =
2606 cpu_to_le32(req_type | (cmpl_ring << HWRM_CMPL_RING_SFT));
2607 req->target_id_seq_id = cpu_to_le32(target_id << HWRM_TARGET_FID_SFT);
2608 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
2609}
2610
2611int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
2612{
2613 int i, intr_process, rc;
2614 struct hwrm_cmd_req_hdr *req = msg;
2615 u32 *data = msg;
2616 __le32 *resp_len, *valid;
2617 u16 cp_ring_id, len = 0;
2618 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
2619
2620 req->target_id_seq_id |= cpu_to_le32(bp->hwrm_cmd_seq++);
2621 memset(resp, 0, PAGE_SIZE);
2622 cp_ring_id = (le32_to_cpu(req->cmpl_ring_req_type) &
2623 HWRM_CMPL_RING_MASK) >>
2624 HWRM_CMPL_RING_SFT;
2625 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
2626
2627 /* Write request msg to hwrm channel */
2628 __iowrite32_copy(bp->bar0, data, msg_len / 4);
2629
Michael Chand79979a2016-01-07 19:56:57 -05002630 for (i = msg_len; i < HWRM_MAX_REQ_LEN; i += 4)
2631 writel(0, bp->bar0 + i);
2632
Michael Chanc0c050c2015-10-22 16:01:17 -04002633 /* currently supports only one outstanding message */
2634 if (intr_process)
2635 bp->hwrm_intr_seq_id = le32_to_cpu(req->target_id_seq_id) &
2636 HWRM_SEQ_ID_MASK;
2637
2638 /* Ring channel doorbell */
2639 writel(1, bp->bar0 + 0x100);
2640
Michael Chanff4fe812016-02-26 04:00:04 -05002641 if (!timeout)
2642 timeout = DFLT_HWRM_CMD_TIMEOUT;
2643
Michael Chanc0c050c2015-10-22 16:01:17 -04002644 i = 0;
2645 if (intr_process) {
2646 /* Wait until hwrm response cmpl interrupt is processed */
2647 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
2648 i++ < timeout) {
2649 usleep_range(600, 800);
2650 }
2651
2652 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
2653 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
2654 req->cmpl_ring_req_type);
2655 return -1;
2656 }
2657 } else {
2658 /* Check if response len is updated */
2659 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
2660 for (i = 0; i < timeout; i++) {
2661 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
2662 HWRM_RESP_LEN_SFT;
2663 if (len)
2664 break;
2665 usleep_range(600, 800);
2666 }
2667
2668 if (i >= timeout) {
2669 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
2670 timeout, req->cmpl_ring_req_type,
2671 req->target_id_seq_id, *resp_len);
2672 return -1;
2673 }
2674
2675 /* Last word of resp contains valid bit */
2676 valid = bp->hwrm_cmd_resp_addr + len - 4;
2677 for (i = 0; i < timeout; i++) {
2678 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
2679 break;
2680 usleep_range(600, 800);
2681 }
2682
2683 if (i >= timeout) {
2684 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
2685 timeout, req->cmpl_ring_req_type,
2686 req->target_id_seq_id, len, *valid);
2687 return -1;
2688 }
2689 }
2690
2691 rc = le16_to_cpu(resp->error_code);
2692 if (rc) {
2693 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
2694 le16_to_cpu(resp->req_type),
2695 le16_to_cpu(resp->seq_id), rc);
2696 return rc;
2697 }
2698 return 0;
2699}
2700
2701int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
2702{
2703 int rc;
2704
2705 mutex_lock(&bp->hwrm_cmd_lock);
2706 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
2707 mutex_unlock(&bp->hwrm_cmd_lock);
2708 return rc;
2709}
2710
2711static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
2712{
2713 struct hwrm_func_drv_rgtr_input req = {0};
2714 int i;
2715
2716 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
2717
2718 req.enables =
2719 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
2720 FUNC_DRV_RGTR_REQ_ENABLES_VER |
2721 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
2722
2723 /* TODO: current async event fwd bits are not defined and the firmware
2724 * only checks if it is non-zero to enable async event forwarding
2725 */
2726 req.async_event_fwd[0] |= cpu_to_le32(1);
2727 req.os_type = cpu_to_le16(1);
2728 req.ver_maj = DRV_VER_MAJ;
2729 req.ver_min = DRV_VER_MIN;
2730 req.ver_upd = DRV_VER_UPD;
2731
2732 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05002733 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04002734 u32 *data = (u32 *)vf_req_snif_bmap;
2735
Michael Chande68f5de2015-12-09 19:35:41 -05002736 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04002737 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
2738 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
2739
Michael Chande68f5de2015-12-09 19:35:41 -05002740 for (i = 0; i < 8; i++)
2741 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
2742
Michael Chanc0c050c2015-10-22 16:01:17 -04002743 req.enables |=
2744 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
2745 }
2746
2747 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2748}
2749
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05002750static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
2751{
2752 struct hwrm_func_drv_unrgtr_input req = {0};
2753
2754 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
2755 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2756}
2757
Michael Chanc0c050c2015-10-22 16:01:17 -04002758static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
2759{
2760 u32 rc = 0;
2761 struct hwrm_tunnel_dst_port_free_input req = {0};
2762
2763 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
2764 req.tunnel_type = tunnel_type;
2765
2766 switch (tunnel_type) {
2767 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
2768 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
2769 break;
2770 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
2771 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
2772 break;
2773 default:
2774 break;
2775 }
2776
2777 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2778 if (rc)
2779 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
2780 rc);
2781 return rc;
2782}
2783
2784static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
2785 u8 tunnel_type)
2786{
2787 u32 rc = 0;
2788 struct hwrm_tunnel_dst_port_alloc_input req = {0};
2789 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
2790
2791 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
2792
2793 req.tunnel_type = tunnel_type;
2794 req.tunnel_dst_port_val = port;
2795
2796 mutex_lock(&bp->hwrm_cmd_lock);
2797 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2798 if (rc) {
2799 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
2800 rc);
2801 goto err_out;
2802 }
2803
2804 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
2805 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
2806
2807 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
2808 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
2809err_out:
2810 mutex_unlock(&bp->hwrm_cmd_lock);
2811 return rc;
2812}
2813
2814static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
2815{
2816 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
2817 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
2818
2819 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05002820 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002821
2822 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
2823 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
2824 req.mask = cpu_to_le32(vnic->rx_mask);
2825 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2826}
2827
2828#ifdef CONFIG_RFS_ACCEL
2829static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
2830 struct bnxt_ntuple_filter *fltr)
2831{
2832 struct hwrm_cfa_ntuple_filter_free_input req = {0};
2833
2834 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
2835 req.ntuple_filter_id = fltr->filter_id;
2836 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2837}
2838
2839#define BNXT_NTP_FLTR_FLAGS \
2840 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
2841 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
2842 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
2843 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
2844 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
2845 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
2846 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
2847 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
2848 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
2849 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
2850 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
2851 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
2852 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05002853 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04002854
2855static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
2856 struct bnxt_ntuple_filter *fltr)
2857{
2858 int rc = 0;
2859 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
2860 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
2861 bp->hwrm_cmd_resp_addr;
2862 struct flow_keys *keys = &fltr->fkeys;
2863 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
2864
2865 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
2866 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[0];
2867
2868 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
2869
2870 req.ethertype = htons(ETH_P_IP);
2871 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05002872 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04002873 req.ip_protocol = keys->basic.ip_proto;
2874
2875 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
2876 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
2877 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
2878 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
2879
2880 req.src_port = keys->ports.src;
2881 req.src_port_mask = cpu_to_be16(0xffff);
2882 req.dst_port = keys->ports.dst;
2883 req.dst_port_mask = cpu_to_be16(0xffff);
2884
Michael Chanc1935542015-12-27 18:19:28 -05002885 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002886 mutex_lock(&bp->hwrm_cmd_lock);
2887 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2888 if (!rc)
2889 fltr->filter_id = resp->ntuple_filter_id;
2890 mutex_unlock(&bp->hwrm_cmd_lock);
2891 return rc;
2892}
2893#endif
2894
2895static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
2896 u8 *mac_addr)
2897{
2898 u32 rc = 0;
2899 struct hwrm_cfa_l2_filter_alloc_input req = {0};
2900 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
2901
2902 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
2903 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX |
2904 CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05002905 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002906 req.enables =
2907 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05002908 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04002909 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
2910 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
2911 req.l2_addr_mask[0] = 0xff;
2912 req.l2_addr_mask[1] = 0xff;
2913 req.l2_addr_mask[2] = 0xff;
2914 req.l2_addr_mask[3] = 0xff;
2915 req.l2_addr_mask[4] = 0xff;
2916 req.l2_addr_mask[5] = 0xff;
2917
2918 mutex_lock(&bp->hwrm_cmd_lock);
2919 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
2920 if (!rc)
2921 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
2922 resp->l2_filter_id;
2923 mutex_unlock(&bp->hwrm_cmd_lock);
2924 return rc;
2925}
2926
2927static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
2928{
2929 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
2930 int rc = 0;
2931
2932 /* Any associated ntuple filters will also be cleared by firmware. */
2933 mutex_lock(&bp->hwrm_cmd_lock);
2934 for (i = 0; i < num_of_vnics; i++) {
2935 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2936
2937 for (j = 0; j < vnic->uc_filter_count; j++) {
2938 struct hwrm_cfa_l2_filter_free_input req = {0};
2939
2940 bnxt_hwrm_cmd_hdr_init(bp, &req,
2941 HWRM_CFA_L2_FILTER_FREE, -1, -1);
2942
2943 req.l2_filter_id = vnic->fw_l2_filter_id[j];
2944
2945 rc = _hwrm_send_message(bp, &req, sizeof(req),
2946 HWRM_CMD_TIMEOUT);
2947 }
2948 vnic->uc_filter_count = 0;
2949 }
2950 mutex_unlock(&bp->hwrm_cmd_lock);
2951
2952 return rc;
2953}
2954
2955static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
2956{
2957 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
2958 struct hwrm_vnic_tpa_cfg_input req = {0};
2959
2960 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
2961
2962 if (tpa_flags) {
2963 u16 mss = bp->dev->mtu - 40;
2964 u32 nsegs, n, segs = 0, flags;
2965
2966 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
2967 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
2968 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
2969 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
2970 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
2971 if (tpa_flags & BNXT_FLAG_GRO)
2972 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
2973
2974 req.flags = cpu_to_le32(flags);
2975
2976 req.enables =
2977 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05002978 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
2979 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04002980
2981 /* Number of segs are log2 units, and first packet is not
2982 * included as part of this units.
2983 */
2984 if (mss <= PAGE_SIZE) {
2985 n = PAGE_SIZE / mss;
2986 nsegs = (MAX_SKB_FRAGS - 1) * n;
2987 } else {
2988 n = mss / PAGE_SIZE;
2989 if (mss & (PAGE_SIZE - 1))
2990 n++;
2991 nsegs = (MAX_SKB_FRAGS - n) / n;
2992 }
2993
2994 segs = ilog2(nsegs);
2995 req.max_agg_segs = cpu_to_le16(segs);
2996 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05002997
2998 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04002999 }
3000 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3001
3002 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3003}
3004
3005static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3006{
3007 u32 i, j, max_rings;
3008 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3009 struct hwrm_vnic_rss_cfg_input req = {0};
3010
3011 if (vnic->fw_rss_cos_lb_ctx == INVALID_HW_RING_ID)
3012 return 0;
3013
3014 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3015 if (set_rss) {
3016 vnic->hash_type = BNXT_RSS_HASH_TYPE_FLAG_IPV4 |
3017 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV4 |
3018 BNXT_RSS_HASH_TYPE_FLAG_IPV6 |
3019 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV6;
3020
3021 req.hash_type = cpu_to_le32(vnic->hash_type);
3022
3023 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3024 max_rings = bp->rx_nr_rings;
3025 else
3026 max_rings = 1;
3027
3028 /* Fill the RSS indirection table with ring group ids */
3029 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3030 if (j == max_rings)
3031 j = 0;
3032 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3033 }
3034
3035 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3036 req.hash_key_tbl_addr =
3037 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3038 }
3039 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
3040 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3041}
3042
3043static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3044{
3045 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3046 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3047
3048 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3049 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3050 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3051 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3052 req.enables =
3053 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3054 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3055 /* thresholds not implemented in firmware yet */
3056 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3057 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3058 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3059 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3060}
3061
3062static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id)
3063{
3064 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3065
3066 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3067 req.rss_cos_lb_ctx_id =
3068 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx);
3069
3070 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3071 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
3072}
3073
3074static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3075{
3076 int i;
3077
3078 for (i = 0; i < bp->nr_vnics; i++) {
3079 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3080
3081 if (vnic->fw_rss_cos_lb_ctx != INVALID_HW_RING_ID)
3082 bnxt_hwrm_vnic_ctx_free_one(bp, i);
3083 }
3084 bp->rsscos_nr_ctxs = 0;
3085}
3086
3087static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id)
3088{
3089 int rc;
3090 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3091 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3092 bp->hwrm_cmd_resp_addr;
3093
3094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3095 -1);
3096
3097 mutex_lock(&bp->hwrm_cmd_lock);
3098 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3099 if (!rc)
3100 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx =
3101 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3102 mutex_unlock(&bp->hwrm_cmd_lock);
3103
3104 return rc;
3105}
3106
3107static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3108{
Michael Chanb81a90d2016-01-02 23:45:01 -05003109 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003110 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3111 struct hwrm_vnic_cfg_input req = {0};
3112
3113 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
3114 /* Only RSS support for now TBD: COS & LB */
3115 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP |
3116 VNIC_CFG_REQ_ENABLES_RSS_RULE);
3117 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
3118 req.cos_rule = cpu_to_le16(0xffff);
3119 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003120 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003121 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003122 ring = vnic_id - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003123
Michael Chanb81a90d2016-01-02 23:45:01 -05003124 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003125 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3126 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3127
3128 req.lb_rule = cpu_to_le16(0xffff);
3129 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3130 VLAN_HLEN);
3131
3132 if (bp->flags & BNXT_FLAG_STRIP_VLAN)
3133 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3134
3135 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3136}
3137
3138static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3139{
3140 u32 rc = 0;
3141
3142 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3143 struct hwrm_vnic_free_input req = {0};
3144
3145 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3146 req.vnic_id =
3147 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3148
3149 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3150 if (rc)
3151 return rc;
3152 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3153 }
3154 return rc;
3155}
3156
3157static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3158{
3159 u16 i;
3160
3161 for (i = 0; i < bp->nr_vnics; i++)
3162 bnxt_hwrm_vnic_free_one(bp, i);
3163}
3164
Michael Chanb81a90d2016-01-02 23:45:01 -05003165static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3166 unsigned int start_rx_ring_idx,
3167 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003168{
Michael Chanb81a90d2016-01-02 23:45:01 -05003169 int rc = 0;
3170 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003171 struct hwrm_vnic_alloc_input req = {0};
3172 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3173
3174 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003175 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3176 grp_idx = bp->rx_ring[i].bnapi->index;
3177 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003178 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003179 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003180 break;
3181 }
3182 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003183 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003184 }
3185
3186 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
3187 if (vnic_id == 0)
3188 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3189
3190 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3191
3192 mutex_lock(&bp->hwrm_cmd_lock);
3193 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3194 if (!rc)
3195 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3196 mutex_unlock(&bp->hwrm_cmd_lock);
3197 return rc;
3198}
3199
3200static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3201{
3202 u16 i;
3203 u32 rc = 0;
3204
3205 mutex_lock(&bp->hwrm_cmd_lock);
3206 for (i = 0; i < bp->rx_nr_rings; i++) {
3207 struct hwrm_ring_grp_alloc_input req = {0};
3208 struct hwrm_ring_grp_alloc_output *resp =
3209 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003210 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003211
3212 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3213
Michael Chanb81a90d2016-01-02 23:45:01 -05003214 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3215 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3216 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3217 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003218
3219 rc = _hwrm_send_message(bp, &req, sizeof(req),
3220 HWRM_CMD_TIMEOUT);
3221 if (rc)
3222 break;
3223
Michael Chanb81a90d2016-01-02 23:45:01 -05003224 bp->grp_info[grp_idx].fw_grp_id =
3225 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003226 }
3227 mutex_unlock(&bp->hwrm_cmd_lock);
3228 return rc;
3229}
3230
3231static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3232{
3233 u16 i;
3234 u32 rc = 0;
3235 struct hwrm_ring_grp_free_input req = {0};
3236
3237 if (!bp->grp_info)
3238 return 0;
3239
3240 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3241
3242 mutex_lock(&bp->hwrm_cmd_lock);
3243 for (i = 0; i < bp->cp_nr_rings; i++) {
3244 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3245 continue;
3246 req.ring_group_id =
3247 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3248
3249 rc = _hwrm_send_message(bp, &req, sizeof(req),
3250 HWRM_CMD_TIMEOUT);
3251 if (rc)
3252 break;
3253 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3254 }
3255 mutex_unlock(&bp->hwrm_cmd_lock);
3256 return rc;
3257}
3258
3259static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3260 struct bnxt_ring_struct *ring,
3261 u32 ring_type, u32 map_index,
3262 u32 stats_ctx_id)
3263{
3264 int rc = 0, err = 0;
3265 struct hwrm_ring_alloc_input req = {0};
3266 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3267 u16 ring_id;
3268
3269 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3270
3271 req.enables = 0;
3272 if (ring->nr_pages > 1) {
3273 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3274 /* Page size is in log2 units */
3275 req.page_size = BNXT_PAGE_SHIFT;
3276 req.page_tbl_depth = 1;
3277 } else {
3278 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3279 }
3280 req.fbo = 0;
3281 /* Association of ring index with doorbell index and MSIX number */
3282 req.logical_id = cpu_to_le16(map_index);
3283
3284 switch (ring_type) {
3285 case HWRM_RING_ALLOC_TX:
3286 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3287 /* Association of transmit ring with completion ring */
3288 req.cmpl_ring_id =
3289 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3290 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3291 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3292 req.queue_id = cpu_to_le16(ring->queue_id);
3293 break;
3294 case HWRM_RING_ALLOC_RX:
3295 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3296 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3297 break;
3298 case HWRM_RING_ALLOC_AGG:
3299 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3300 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3301 break;
3302 case HWRM_RING_ALLOC_CMPL:
3303 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3304 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3305 if (bp->flags & BNXT_FLAG_USING_MSIX)
3306 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3307 break;
3308 default:
3309 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3310 ring_type);
3311 return -1;
3312 }
3313
3314 mutex_lock(&bp->hwrm_cmd_lock);
3315 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3316 err = le16_to_cpu(resp->error_code);
3317 ring_id = le16_to_cpu(resp->ring_id);
3318 mutex_unlock(&bp->hwrm_cmd_lock);
3319
3320 if (rc || err) {
3321 switch (ring_type) {
3322 case RING_FREE_REQ_RING_TYPE_CMPL:
3323 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3324 rc, err);
3325 return -1;
3326
3327 case RING_FREE_REQ_RING_TYPE_RX:
3328 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3329 rc, err);
3330 return -1;
3331
3332 case RING_FREE_REQ_RING_TYPE_TX:
3333 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3334 rc, err);
3335 return -1;
3336
3337 default:
3338 netdev_err(bp->dev, "Invalid ring\n");
3339 return -1;
3340 }
3341 }
3342 ring->fw_ring_id = ring_id;
3343 return rc;
3344}
3345
3346static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3347{
3348 int i, rc = 0;
3349
Michael Chanedd0c2c2015-12-27 18:19:19 -05003350 for (i = 0; i < bp->cp_nr_rings; i++) {
3351 struct bnxt_napi *bnapi = bp->bnapi[i];
3352 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3353 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003354
Michael Chanedd0c2c2015-12-27 18:19:19 -05003355 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3356 INVALID_STATS_CTX_ID);
3357 if (rc)
3358 goto err_out;
3359 cpr->cp_doorbell = bp->bar1 + i * 0x80;
3360 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3361 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003362 }
3363
Michael Chanedd0c2c2015-12-27 18:19:19 -05003364 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003365 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003366 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003367 u32 map_idx = txr->bnapi->index;
3368 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003369
Michael Chanb81a90d2016-01-02 23:45:01 -05003370 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3371 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003372 if (rc)
3373 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003374 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003375 }
3376
Michael Chanedd0c2c2015-12-27 18:19:19 -05003377 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003378 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003379 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003380 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003381
Michael Chanb81a90d2016-01-02 23:45:01 -05003382 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3383 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003384 if (rc)
3385 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003386 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003387 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003388 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003389 }
3390
3391 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3392 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003393 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003394 struct bnxt_ring_struct *ring =
3395 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003396 u32 grp_idx = rxr->bnapi->index;
3397 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003398
3399 rc = hwrm_ring_alloc_send_msg(bp, ring,
3400 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003401 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003402 INVALID_STATS_CTX_ID);
3403 if (rc)
3404 goto err_out;
3405
Michael Chanb81a90d2016-01-02 23:45:01 -05003406 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003407 writel(DB_KEY_RX | rxr->rx_agg_prod,
3408 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003409 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003410 }
3411 }
3412err_out:
3413 return rc;
3414}
3415
3416static int hwrm_ring_free_send_msg(struct bnxt *bp,
3417 struct bnxt_ring_struct *ring,
3418 u32 ring_type, int cmpl_ring_id)
3419{
3420 int rc;
3421 struct hwrm_ring_free_input req = {0};
3422 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3423 u16 error_code;
3424
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003425 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003426 req.ring_type = ring_type;
3427 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3428
3429 mutex_lock(&bp->hwrm_cmd_lock);
3430 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3431 error_code = le16_to_cpu(resp->error_code);
3432 mutex_unlock(&bp->hwrm_cmd_lock);
3433
3434 if (rc || error_code) {
3435 switch (ring_type) {
3436 case RING_FREE_REQ_RING_TYPE_CMPL:
3437 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3438 rc);
3439 return rc;
3440 case RING_FREE_REQ_RING_TYPE_RX:
3441 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3442 rc);
3443 return rc;
3444 case RING_FREE_REQ_RING_TYPE_TX:
3445 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3446 rc);
3447 return rc;
3448 default:
3449 netdev_err(bp->dev, "Invalid ring\n");
3450 return -1;
3451 }
3452 }
3453 return 0;
3454}
3455
Michael Chanedd0c2c2015-12-27 18:19:19 -05003456static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003457{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003458 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003459
3460 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003461 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003462
Michael Chanedd0c2c2015-12-27 18:19:19 -05003463 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003464 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003465 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003466 u32 grp_idx = txr->bnapi->index;
3467 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003468
Michael Chanedd0c2c2015-12-27 18:19:19 -05003469 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3470 hwrm_ring_free_send_msg(bp, ring,
3471 RING_FREE_REQ_RING_TYPE_TX,
3472 close_path ? cmpl_ring_id :
3473 INVALID_HW_RING_ID);
3474 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003475 }
3476 }
3477
Michael Chanedd0c2c2015-12-27 18:19:19 -05003478 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003479 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003480 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003481 u32 grp_idx = rxr->bnapi->index;
3482 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003483
Michael Chanedd0c2c2015-12-27 18:19:19 -05003484 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3485 hwrm_ring_free_send_msg(bp, ring,
3486 RING_FREE_REQ_RING_TYPE_RX,
3487 close_path ? cmpl_ring_id :
3488 INVALID_HW_RING_ID);
3489 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003490 bp->grp_info[grp_idx].rx_fw_ring_id =
3491 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003492 }
3493 }
3494
Michael Chanedd0c2c2015-12-27 18:19:19 -05003495 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003496 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003497 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003498 u32 grp_idx = rxr->bnapi->index;
3499 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003500
Michael Chanedd0c2c2015-12-27 18:19:19 -05003501 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3502 hwrm_ring_free_send_msg(bp, ring,
3503 RING_FREE_REQ_RING_TYPE_RX,
3504 close_path ? cmpl_ring_id :
3505 INVALID_HW_RING_ID);
3506 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003507 bp->grp_info[grp_idx].agg_fw_ring_id =
3508 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003509 }
3510 }
3511
Michael Chanedd0c2c2015-12-27 18:19:19 -05003512 for (i = 0; i < bp->cp_nr_rings; i++) {
3513 struct bnxt_napi *bnapi = bp->bnapi[i];
3514 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3515 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003516
Michael Chanedd0c2c2015-12-27 18:19:19 -05003517 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3518 hwrm_ring_free_send_msg(bp, ring,
3519 RING_FREE_REQ_RING_TYPE_CMPL,
3520 INVALID_HW_RING_ID);
3521 ring->fw_ring_id = INVALID_HW_RING_ID;
3522 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003523 }
3524 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003525}
3526
Michael Chanbb053f52016-02-26 04:00:02 -05003527static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3528 u32 buf_tmrs, u16 flags,
3529 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3530{
3531 req->flags = cpu_to_le16(flags);
3532 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3533 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3534 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3535 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3536 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3537 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3538 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3539 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3540}
3541
Michael Chanc0c050c2015-10-22 16:01:17 -04003542int bnxt_hwrm_set_coal(struct bnxt *bp)
3543{
3544 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05003545 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3546 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04003547 u16 max_buf, max_buf_irq;
3548 u16 buf_tmr, buf_tmr_irq;
3549 u32 flags;
3550
Michael Chandfc9c942016-02-26 04:00:03 -05003551 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3552 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3553 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3554 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003555
Michael Chandfb5b892016-02-26 04:00:01 -05003556 /* Each rx completion (2 records) should be DMAed immediately.
3557 * DMA 1/4 of the completion buffers at a time.
3558 */
3559 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04003560 /* max_buf must not be zero */
3561 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05003562 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
3563 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
3564 /* buf timer set to 1/4 of interrupt timer */
3565 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3566 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
3567 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003568
3569 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3570
3571 /* RING_IDLE generates more IRQs for lower latency. Enable it only
3572 * if coal_ticks is less than 25 us.
3573 */
Michael Chandfb5b892016-02-26 04:00:01 -05003574 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04003575 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
3576
Michael Chanbb053f52016-02-26 04:00:02 -05003577 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05003578 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
3579
3580 /* max_buf must not be zero */
3581 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
3582 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
3583 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
3584 /* buf timer set to 1/4 of interrupt timer */
3585 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3586 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
3587 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
3588
3589 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3590 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
3591 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003592
3593 mutex_lock(&bp->hwrm_cmd_lock);
3594 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05003595 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003596
Michael Chandfc9c942016-02-26 04:00:03 -05003597 req = &req_rx;
3598 if (!bnapi->rx_ring)
3599 req = &req_tx;
3600 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
3601
3602 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04003603 HWRM_CMD_TIMEOUT);
3604 if (rc)
3605 break;
3606 }
3607 mutex_unlock(&bp->hwrm_cmd_lock);
3608 return rc;
3609}
3610
3611static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
3612{
3613 int rc = 0, i;
3614 struct hwrm_stat_ctx_free_input req = {0};
3615
3616 if (!bp->bnapi)
3617 return 0;
3618
3619 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
3620
3621 mutex_lock(&bp->hwrm_cmd_lock);
3622 for (i = 0; i < bp->cp_nr_rings; i++) {
3623 struct bnxt_napi *bnapi = bp->bnapi[i];
3624 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3625
3626 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
3627 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
3628
3629 rc = _hwrm_send_message(bp, &req, sizeof(req),
3630 HWRM_CMD_TIMEOUT);
3631 if (rc)
3632 break;
3633
3634 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3635 }
3636 }
3637 mutex_unlock(&bp->hwrm_cmd_lock);
3638 return rc;
3639}
3640
3641static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
3642{
3643 int rc = 0, i;
3644 struct hwrm_stat_ctx_alloc_input req = {0};
3645 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3646
3647 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
3648
3649 req.update_period_ms = cpu_to_le32(1000);
3650
3651 mutex_lock(&bp->hwrm_cmd_lock);
3652 for (i = 0; i < bp->cp_nr_rings; i++) {
3653 struct bnxt_napi *bnapi = bp->bnapi[i];
3654 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3655
3656 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
3657
3658 rc = _hwrm_send_message(bp, &req, sizeof(req),
3659 HWRM_CMD_TIMEOUT);
3660 if (rc)
3661 break;
3662
3663 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
3664
3665 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
3666 }
3667 mutex_unlock(&bp->hwrm_cmd_lock);
3668 return 0;
3669}
3670
Michael Chan4a21b492015-12-27 18:19:26 -05003671int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04003672{
3673 int rc = 0;
3674 struct hwrm_func_qcaps_input req = {0};
3675 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3676
3677 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
3678 req.fid = cpu_to_le16(0xffff);
3679
3680 mutex_lock(&bp->hwrm_cmd_lock);
3681 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3682 if (rc)
3683 goto hwrm_func_qcaps_exit;
3684
3685 if (BNXT_PF(bp)) {
3686 struct bnxt_pf_info *pf = &bp->pf;
3687
3688 pf->fw_fid = le16_to_cpu(resp->fid);
3689 pf->port_id = le16_to_cpu(resp->port_id);
3690 memcpy(pf->mac_addr, resp->perm_mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05003691 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003692 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
3693 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
3694 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003695 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05003696 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
3697 if (!pf->max_hw_ring_grps)
3698 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003699 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
3700 pf->max_vnics = le16_to_cpu(resp->max_vnics);
3701 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
3702 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
3703 pf->max_vfs = le16_to_cpu(resp->max_vfs);
3704 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
3705 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
3706 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
3707 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
3708 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
3709 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
3710 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04003711#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04003712 struct bnxt_vf_info *vf = &bp->vf;
3713
3714 vf->fw_fid = le16_to_cpu(resp->fid);
3715 memcpy(vf->mac_addr, resp->perm_mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05003716 if (is_valid_ether_addr(vf->mac_addr))
3717 /* overwrite netdev dev_adr with admin VF MAC */
3718 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
3719 else
3720 random_ether_addr(bp->dev->dev_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04003721
3722 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
3723 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
3724 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
3725 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05003726 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
3727 if (!vf->max_hw_ring_grps)
3728 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003729 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
3730 vf->max_vnics = le16_to_cpu(resp->max_vnics);
3731 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan379a80a2015-10-23 15:06:19 -04003732#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04003733 }
3734
3735 bp->tx_push_thresh = 0;
3736 if (resp->flags &
3737 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
3738 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
3739
3740hwrm_func_qcaps_exit:
3741 mutex_unlock(&bp->hwrm_cmd_lock);
3742 return rc;
3743}
3744
3745static int bnxt_hwrm_func_reset(struct bnxt *bp)
3746{
3747 struct hwrm_func_reset_input req = {0};
3748
3749 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
3750 req.enables = 0;
3751
3752 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
3753}
3754
3755static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
3756{
3757 int rc = 0;
3758 struct hwrm_queue_qportcfg_input req = {0};
3759 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
3760 u8 i, *qptr;
3761
3762 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
3763
3764 mutex_lock(&bp->hwrm_cmd_lock);
3765 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3766 if (rc)
3767 goto qportcfg_exit;
3768
3769 if (!resp->max_configurable_queues) {
3770 rc = -EINVAL;
3771 goto qportcfg_exit;
3772 }
3773 bp->max_tc = resp->max_configurable_queues;
3774 if (bp->max_tc > BNXT_MAX_QUEUE)
3775 bp->max_tc = BNXT_MAX_QUEUE;
3776
3777 qptr = &resp->queue_id0;
3778 for (i = 0; i < bp->max_tc; i++) {
3779 bp->q_info[i].queue_id = *qptr++;
3780 bp->q_info[i].queue_profile = *qptr++;
3781 }
3782
3783qportcfg_exit:
3784 mutex_unlock(&bp->hwrm_cmd_lock);
3785 return rc;
3786}
3787
3788static int bnxt_hwrm_ver_get(struct bnxt *bp)
3789{
3790 int rc;
3791 struct hwrm_ver_get_input req = {0};
3792 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
3793
3794 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
3795 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
3796 req.hwrm_intf_min = HWRM_VERSION_MINOR;
3797 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
3798 mutex_lock(&bp->hwrm_cmd_lock);
3799 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3800 if (rc)
3801 goto hwrm_ver_get_exit;
3802
3803 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
3804
Michael Chanc1935542015-12-27 18:19:28 -05003805 if (resp->hwrm_intf_maj < 1) {
3806 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04003807 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05003808 resp->hwrm_intf_upd);
3809 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04003810 }
3811 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "bc %d.%d.%d rm %d.%d.%d",
3812 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
3813 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
3814
Michael Chanff4fe812016-02-26 04:00:04 -05003815 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
3816 if (!bp->hwrm_cmd_timeout)
3817 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
3818
Michael Chanc0c050c2015-10-22 16:01:17 -04003819hwrm_ver_get_exit:
3820 mutex_unlock(&bp->hwrm_cmd_lock);
3821 return rc;
3822}
3823
3824static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
3825{
3826 if (bp->vxlan_port_cnt) {
3827 bnxt_hwrm_tunnel_dst_port_free(
3828 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
3829 }
3830 bp->vxlan_port_cnt = 0;
3831 if (bp->nge_port_cnt) {
3832 bnxt_hwrm_tunnel_dst_port_free(
3833 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
3834 }
3835 bp->nge_port_cnt = 0;
3836}
3837
3838static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
3839{
3840 int rc, i;
3841 u32 tpa_flags = 0;
3842
3843 if (set_tpa)
3844 tpa_flags = bp->flags & BNXT_FLAG_TPA;
3845 for (i = 0; i < bp->nr_vnics; i++) {
3846 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
3847 if (rc) {
3848 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
3849 rc, i);
3850 return rc;
3851 }
3852 }
3853 return 0;
3854}
3855
3856static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
3857{
3858 int i;
3859
3860 for (i = 0; i < bp->nr_vnics; i++)
3861 bnxt_hwrm_vnic_set_rss(bp, i, false);
3862}
3863
3864static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
3865 bool irq_re_init)
3866{
3867 if (bp->vnic_info) {
3868 bnxt_hwrm_clear_vnic_filter(bp);
3869 /* clear all RSS setting before free vnic ctx */
3870 bnxt_hwrm_clear_vnic_rss(bp);
3871 bnxt_hwrm_vnic_ctx_free(bp);
3872 /* before free the vnic, undo the vnic tpa settings */
3873 if (bp->flags & BNXT_FLAG_TPA)
3874 bnxt_set_tpa(bp, false);
3875 bnxt_hwrm_vnic_free(bp);
3876 }
3877 bnxt_hwrm_ring_free(bp, close_path);
3878 bnxt_hwrm_ring_grp_free(bp);
3879 if (irq_re_init) {
3880 bnxt_hwrm_stat_ctx_free(bp);
3881 bnxt_hwrm_free_tunnel_ports(bp);
3882 }
3883}
3884
3885static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
3886{
3887 int rc;
3888
3889 /* allocate context for vnic */
3890 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id);
3891 if (rc) {
3892 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
3893 vnic_id, rc);
3894 goto vnic_setup_err;
3895 }
3896 bp->rsscos_nr_ctxs++;
3897
3898 /* configure default vnic, ring grp */
3899 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
3900 if (rc) {
3901 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
3902 vnic_id, rc);
3903 goto vnic_setup_err;
3904 }
3905
3906 /* Enable RSS hashing on vnic */
3907 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
3908 if (rc) {
3909 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
3910 vnic_id, rc);
3911 goto vnic_setup_err;
3912 }
3913
3914 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3915 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
3916 if (rc) {
3917 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
3918 vnic_id, rc);
3919 }
3920 }
3921
3922vnic_setup_err:
3923 return rc;
3924}
3925
3926static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
3927{
3928#ifdef CONFIG_RFS_ACCEL
3929 int i, rc = 0;
3930
3931 for (i = 0; i < bp->rx_nr_rings; i++) {
3932 u16 vnic_id = i + 1;
3933 u16 ring_id = i;
3934
3935 if (vnic_id >= bp->nr_vnics)
3936 break;
3937
3938 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05003939 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003940 if (rc) {
3941 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
3942 vnic_id, rc);
3943 break;
3944 }
3945 rc = bnxt_setup_vnic(bp, vnic_id);
3946 if (rc)
3947 break;
3948 }
3949 return rc;
3950#else
3951 return 0;
3952#endif
3953}
3954
Michael Chanb664f002015-12-02 01:54:08 -05003955static int bnxt_cfg_rx_mode(struct bnxt *);
3956
Michael Chanc0c050c2015-10-22 16:01:17 -04003957static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
3958{
3959 int rc = 0;
3960
3961 if (irq_re_init) {
3962 rc = bnxt_hwrm_stat_ctx_alloc(bp);
3963 if (rc) {
3964 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
3965 rc);
3966 goto err_out;
3967 }
3968 }
3969
3970 rc = bnxt_hwrm_ring_alloc(bp);
3971 if (rc) {
3972 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
3973 goto err_out;
3974 }
3975
3976 rc = bnxt_hwrm_ring_grp_alloc(bp);
3977 if (rc) {
3978 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
3979 goto err_out;
3980 }
3981
3982 /* default vnic 0 */
3983 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, bp->rx_nr_rings);
3984 if (rc) {
3985 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
3986 goto err_out;
3987 }
3988
3989 rc = bnxt_setup_vnic(bp, 0);
3990 if (rc)
3991 goto err_out;
3992
3993 if (bp->flags & BNXT_FLAG_RFS) {
3994 rc = bnxt_alloc_rfs_vnics(bp);
3995 if (rc)
3996 goto err_out;
3997 }
3998
3999 if (bp->flags & BNXT_FLAG_TPA) {
4000 rc = bnxt_set_tpa(bp, true);
4001 if (rc)
4002 goto err_out;
4003 }
4004
4005 if (BNXT_VF(bp))
4006 bnxt_update_vf_mac(bp);
4007
4008 /* Filter for default vnic 0 */
4009 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4010 if (rc) {
4011 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4012 goto err_out;
4013 }
4014 bp->vnic_info[0].uc_filter_count = 1;
4015
Michael Chanc1935542015-12-27 18:19:28 -05004016 bp->vnic_info[0].rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004017
4018 if ((bp->dev->flags & IFF_PROMISC) && BNXT_PF(bp))
4019 bp->vnic_info[0].rx_mask |=
4020 CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4021
Michael Chanb664f002015-12-02 01:54:08 -05004022 rc = bnxt_cfg_rx_mode(bp);
4023 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004024 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004025
4026 rc = bnxt_hwrm_set_coal(bp);
4027 if (rc)
4028 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
4029 rc);
4030
4031 return 0;
4032
4033err_out:
4034 bnxt_hwrm_resource_free(bp, 0, true);
4035
4036 return rc;
4037}
4038
4039static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4040{
4041 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4042 return 0;
4043}
4044
4045static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4046{
4047 bnxt_init_rx_rings(bp);
4048 bnxt_init_tx_rings(bp);
4049 bnxt_init_ring_grps(bp, irq_re_init);
4050 bnxt_init_vnics(bp);
4051
4052 return bnxt_init_chip(bp, irq_re_init);
4053}
4054
4055static void bnxt_disable_int(struct bnxt *bp)
4056{
4057 int i;
4058
4059 if (!bp->bnapi)
4060 return;
4061
4062 for (i = 0; i < bp->cp_nr_rings; i++) {
4063 struct bnxt_napi *bnapi = bp->bnapi[i];
4064 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4065
4066 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4067 }
4068}
4069
4070static void bnxt_enable_int(struct bnxt *bp)
4071{
4072 int i;
4073
4074 atomic_set(&bp->intr_sem, 0);
4075 for (i = 0; i < bp->cp_nr_rings; i++) {
4076 struct bnxt_napi *bnapi = bp->bnapi[i];
4077 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4078
4079 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4080 }
4081}
4082
4083static int bnxt_set_real_num_queues(struct bnxt *bp)
4084{
4085 int rc;
4086 struct net_device *dev = bp->dev;
4087
4088 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4089 if (rc)
4090 return rc;
4091
4092 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4093 if (rc)
4094 return rc;
4095
4096#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004097 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004098 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004099#endif
4100
4101 return rc;
4102}
4103
Michael Chan6e6c5a52016-01-02 23:45:02 -05004104static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4105 bool shared)
4106{
4107 int _rx = *rx, _tx = *tx;
4108
4109 if (shared) {
4110 *rx = min_t(int, _rx, max);
4111 *tx = min_t(int, _tx, max);
4112 } else {
4113 if (max < 2)
4114 return -ENOMEM;
4115
4116 while (_rx + _tx > max) {
4117 if (_rx > _tx && _rx > 1)
4118 _rx--;
4119 else if (_tx > 1)
4120 _tx--;
4121 }
4122 *rx = _rx;
4123 *tx = _tx;
4124 }
4125 return 0;
4126}
4127
Michael Chanc0c050c2015-10-22 16:01:17 -04004128static int bnxt_setup_msix(struct bnxt *bp)
4129{
4130 struct msix_entry *msix_ent;
4131 struct net_device *dev = bp->dev;
Michael Chan01657bc2016-01-02 23:45:03 -05004132 int i, total_vecs, rc = 0, min = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004133 const int len = sizeof(bp->irq_tbl[0].name);
4134
4135 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4136 total_vecs = bp->cp_nr_rings;
4137
4138 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4139 if (!msix_ent)
4140 return -ENOMEM;
4141
4142 for (i = 0; i < total_vecs; i++) {
4143 msix_ent[i].entry = i;
4144 msix_ent[i].vector = 0;
4145 }
4146
Michael Chan01657bc2016-01-02 23:45:03 -05004147 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4148 min = 2;
4149
4150 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004151 if (total_vecs < 0) {
4152 rc = -ENODEV;
4153 goto msix_setup_exit;
4154 }
4155
4156 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4157 if (bp->irq_tbl) {
4158 int tcs;
4159
4160 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004161 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004162 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004163 if (rc)
4164 goto msix_setup_exit;
4165
Michael Chanc0c050c2015-10-22 16:01:17 -04004166 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4167 tcs = netdev_get_num_tc(dev);
4168 if (tcs > 1) {
4169 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4170 if (bp->tx_nr_rings_per_tc == 0) {
4171 netdev_reset_tc(dev);
4172 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4173 } else {
4174 int i, off, count;
4175
4176 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4177 for (i = 0; i < tcs; i++) {
4178 count = bp->tx_nr_rings_per_tc;
4179 off = i * count;
4180 netdev_set_tc_queue(dev, i, count, off);
4181 }
4182 }
4183 }
Michael Chan01657bc2016-01-02 23:45:03 -05004184 bp->cp_nr_rings = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004185
4186 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chan01657bc2016-01-02 23:45:03 -05004187 char *attr;
4188
Michael Chanc0c050c2015-10-22 16:01:17 -04004189 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chan01657bc2016-01-02 23:45:03 -05004190 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4191 attr = "TxRx";
4192 else if (i < bp->rx_nr_rings)
4193 attr = "rx";
4194 else
4195 attr = "tx";
4196
Michael Chanc0c050c2015-10-22 16:01:17 -04004197 snprintf(bp->irq_tbl[i].name, len,
Michael Chan01657bc2016-01-02 23:45:03 -05004198 "%s-%s-%d", dev->name, attr, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004199 bp->irq_tbl[i].handler = bnxt_msix;
4200 }
4201 rc = bnxt_set_real_num_queues(bp);
4202 if (rc)
4203 goto msix_setup_exit;
4204 } else {
4205 rc = -ENOMEM;
4206 goto msix_setup_exit;
4207 }
4208 bp->flags |= BNXT_FLAG_USING_MSIX;
4209 kfree(msix_ent);
4210 return 0;
4211
4212msix_setup_exit:
4213 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4214 pci_disable_msix(bp->pdev);
4215 kfree(msix_ent);
4216 return rc;
4217}
4218
4219static int bnxt_setup_inta(struct bnxt *bp)
4220{
4221 int rc;
4222 const int len = sizeof(bp->irq_tbl[0].name);
4223
4224 if (netdev_get_num_tc(bp->dev))
4225 netdev_reset_tc(bp->dev);
4226
4227 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4228 if (!bp->irq_tbl) {
4229 rc = -ENOMEM;
4230 return rc;
4231 }
4232 bp->rx_nr_rings = 1;
4233 bp->tx_nr_rings = 1;
4234 bp->cp_nr_rings = 1;
4235 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004236 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004237 bp->irq_tbl[0].vector = bp->pdev->irq;
4238 snprintf(bp->irq_tbl[0].name, len,
4239 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4240 bp->irq_tbl[0].handler = bnxt_inta;
4241 rc = bnxt_set_real_num_queues(bp);
4242 return rc;
4243}
4244
4245static int bnxt_setup_int_mode(struct bnxt *bp)
4246{
4247 int rc = 0;
4248
4249 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4250 rc = bnxt_setup_msix(bp);
4251
4252 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
4253 /* fallback to INTA */
4254 rc = bnxt_setup_inta(bp);
4255 }
4256 return rc;
4257}
4258
4259static void bnxt_free_irq(struct bnxt *bp)
4260{
4261 struct bnxt_irq *irq;
4262 int i;
4263
4264#ifdef CONFIG_RFS_ACCEL
4265 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4266 bp->dev->rx_cpu_rmap = NULL;
4267#endif
4268 if (!bp->irq_tbl)
4269 return;
4270
4271 for (i = 0; i < bp->cp_nr_rings; i++) {
4272 irq = &bp->irq_tbl[i];
4273 if (irq->requested)
4274 free_irq(irq->vector, bp->bnapi[i]);
4275 irq->requested = 0;
4276 }
4277 if (bp->flags & BNXT_FLAG_USING_MSIX)
4278 pci_disable_msix(bp->pdev);
4279 kfree(bp->irq_tbl);
4280 bp->irq_tbl = NULL;
4281}
4282
4283static int bnxt_request_irq(struct bnxt *bp)
4284{
Michael Chanb81a90d2016-01-02 23:45:01 -05004285 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004286 unsigned long flags = 0;
4287#ifdef CONFIG_RFS_ACCEL
4288 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4289#endif
4290
4291 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4292 flags = IRQF_SHARED;
4293
Michael Chanb81a90d2016-01-02 23:45:01 -05004294 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004295 struct bnxt_irq *irq = &bp->irq_tbl[i];
4296#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05004297 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004298 rc = irq_cpu_rmap_add(rmap, irq->vector);
4299 if (rc)
4300 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004301 j);
4302 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04004303 }
4304#endif
4305 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4306 bp->bnapi[i]);
4307 if (rc)
4308 break;
4309
4310 irq->requested = 1;
4311 }
4312 return rc;
4313}
4314
4315static void bnxt_del_napi(struct bnxt *bp)
4316{
4317 int i;
4318
4319 if (!bp->bnapi)
4320 return;
4321
4322 for (i = 0; i < bp->cp_nr_rings; i++) {
4323 struct bnxt_napi *bnapi = bp->bnapi[i];
4324
4325 napi_hash_del(&bnapi->napi);
4326 netif_napi_del(&bnapi->napi);
4327 }
4328}
4329
4330static void bnxt_init_napi(struct bnxt *bp)
4331{
4332 int i;
4333 struct bnxt_napi *bnapi;
4334
4335 if (bp->flags & BNXT_FLAG_USING_MSIX) {
4336 for (i = 0; i < bp->cp_nr_rings; i++) {
4337 bnapi = bp->bnapi[i];
4338 netif_napi_add(bp->dev, &bnapi->napi,
4339 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004340 }
4341 } else {
4342 bnapi = bp->bnapi[0];
4343 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004344 }
4345}
4346
4347static void bnxt_disable_napi(struct bnxt *bp)
4348{
4349 int i;
4350
4351 if (!bp->bnapi)
4352 return;
4353
4354 for (i = 0; i < bp->cp_nr_rings; i++) {
4355 napi_disable(&bp->bnapi[i]->napi);
4356 bnxt_disable_poll(bp->bnapi[i]);
4357 }
4358}
4359
4360static void bnxt_enable_napi(struct bnxt *bp)
4361{
4362 int i;
4363
4364 for (i = 0; i < bp->cp_nr_rings; i++) {
4365 bnxt_enable_poll(bp->bnapi[i]);
4366 napi_enable(&bp->bnapi[i]->napi);
4367 }
4368}
4369
4370static void bnxt_tx_disable(struct bnxt *bp)
4371{
4372 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004373 struct bnxt_tx_ring_info *txr;
4374 struct netdev_queue *txq;
4375
Michael Chanb6ab4b02016-01-02 23:44:59 -05004376 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004377 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004378 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004379 txq = netdev_get_tx_queue(bp->dev, i);
4380 __netif_tx_lock(txq, smp_processor_id());
4381 txr->dev_state = BNXT_DEV_STATE_CLOSING;
4382 __netif_tx_unlock(txq);
4383 }
4384 }
4385 /* Stop all TX queues */
4386 netif_tx_disable(bp->dev);
4387 netif_carrier_off(bp->dev);
4388}
4389
4390static void bnxt_tx_enable(struct bnxt *bp)
4391{
4392 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004393 struct bnxt_tx_ring_info *txr;
4394 struct netdev_queue *txq;
4395
4396 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004397 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004398 txq = netdev_get_tx_queue(bp->dev, i);
4399 txr->dev_state = 0;
4400 }
4401 netif_tx_wake_all_queues(bp->dev);
4402 if (bp->link_info.link_up)
4403 netif_carrier_on(bp->dev);
4404}
4405
4406static void bnxt_report_link(struct bnxt *bp)
4407{
4408 if (bp->link_info.link_up) {
4409 const char *duplex;
4410 const char *flow_ctrl;
4411 u16 speed;
4412
4413 netif_carrier_on(bp->dev);
4414 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
4415 duplex = "full";
4416 else
4417 duplex = "half";
4418 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
4419 flow_ctrl = "ON - receive & transmit";
4420 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
4421 flow_ctrl = "ON - transmit";
4422 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
4423 flow_ctrl = "ON - receive";
4424 else
4425 flow_ctrl = "none";
4426 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
4427 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
4428 speed, duplex, flow_ctrl);
4429 } else {
4430 netif_carrier_off(bp->dev);
4431 netdev_err(bp->dev, "NIC Link is Down\n");
4432 }
4433}
4434
4435static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
4436{
4437 int rc = 0;
4438 struct bnxt_link_info *link_info = &bp->link_info;
4439 struct hwrm_port_phy_qcfg_input req = {0};
4440 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4441 u8 link_up = link_info->link_up;
4442
4443 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
4444
4445 mutex_lock(&bp->hwrm_cmd_lock);
4446 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4447 if (rc) {
4448 mutex_unlock(&bp->hwrm_cmd_lock);
4449 return rc;
4450 }
4451
4452 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
4453 link_info->phy_link_status = resp->link;
4454 link_info->duplex = resp->duplex;
4455 link_info->pause = resp->pause;
4456 link_info->auto_mode = resp->auto_mode;
4457 link_info->auto_pause_setting = resp->auto_pause;
4458 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05004459 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04004460 if (link_info->phy_link_status == BNXT_LINK_LINK)
4461 link_info->link_speed = le16_to_cpu(resp->link_speed);
4462 else
4463 link_info->link_speed = 0;
4464 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
4465 link_info->auto_link_speed = le16_to_cpu(resp->auto_link_speed);
4466 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
4467 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
4468 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
4469 link_info->phy_ver[0] = resp->phy_maj;
4470 link_info->phy_ver[1] = resp->phy_min;
4471 link_info->phy_ver[2] = resp->phy_bld;
4472 link_info->media_type = resp->media_type;
4473 link_info->transceiver = resp->transceiver_type;
4474 link_info->phy_addr = resp->phy_addr;
4475
4476 /* TODO: need to add more logic to report VF link */
4477 if (chng_link_state) {
4478 if (link_info->phy_link_status == BNXT_LINK_LINK)
4479 link_info->link_up = 1;
4480 else
4481 link_info->link_up = 0;
4482 if (link_up != link_info->link_up)
4483 bnxt_report_link(bp);
4484 } else {
4485 /* alwasy link down if not require to update link state */
4486 link_info->link_up = 0;
4487 }
4488 mutex_unlock(&bp->hwrm_cmd_lock);
4489 return 0;
4490}
4491
4492static void
4493bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
4494{
4495 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
4496 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
4497 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
4498 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
4499 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
4500 req->enables |=
4501 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
4502 } else {
4503 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
4504 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
4505 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
4506 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
4507 req->enables |=
4508 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
4509 }
4510}
4511
4512static void bnxt_hwrm_set_link_common(struct bnxt *bp,
4513 struct hwrm_port_phy_cfg_input *req)
4514{
4515 u8 autoneg = bp->link_info.autoneg;
4516 u16 fw_link_speed = bp->link_info.req_link_speed;
4517 u32 advertising = bp->link_info.advertising;
4518
4519 if (autoneg & BNXT_AUTONEG_SPEED) {
4520 req->auto_mode |=
4521 PORT_PHY_CFG_REQ_AUTO_MODE_MASK;
4522
4523 req->enables |= cpu_to_le32(
4524 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
4525 req->auto_link_speed_mask = cpu_to_le16(advertising);
4526
4527 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
4528 req->flags |=
4529 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
4530 } else {
4531 req->force_link_speed = cpu_to_le16(fw_link_speed);
4532 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
4533 }
4534
4535 /* currently don't support half duplex */
4536 req->auto_duplex = PORT_PHY_CFG_REQ_AUTO_DUPLEX_FULL;
4537 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_DUPLEX);
4538 /* tell chimp that the setting takes effect immediately */
4539 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
4540}
4541
4542int bnxt_hwrm_set_pause(struct bnxt *bp)
4543{
4544 struct hwrm_port_phy_cfg_input req = {0};
4545 int rc;
4546
4547 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
4548 bnxt_hwrm_set_pause_common(bp, &req);
4549
4550 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
4551 bp->link_info.force_link_chng)
4552 bnxt_hwrm_set_link_common(bp, &req);
4553
4554 mutex_lock(&bp->hwrm_cmd_lock);
4555 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4556 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
4557 /* since changing of pause setting doesn't trigger any link
4558 * change event, the driver needs to update the current pause
4559 * result upon successfully return of the phy_cfg command
4560 */
4561 bp->link_info.pause =
4562 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
4563 bp->link_info.auto_pause_setting = 0;
4564 if (!bp->link_info.force_link_chng)
4565 bnxt_report_link(bp);
4566 }
4567 bp->link_info.force_link_chng = false;
4568 mutex_unlock(&bp->hwrm_cmd_lock);
4569 return rc;
4570}
4571
4572int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause)
4573{
4574 struct hwrm_port_phy_cfg_input req = {0};
4575
4576 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
4577 if (set_pause)
4578 bnxt_hwrm_set_pause_common(bp, &req);
4579
4580 bnxt_hwrm_set_link_common(bp, &req);
4581 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4582}
4583
4584static int bnxt_update_phy_setting(struct bnxt *bp)
4585{
4586 int rc;
4587 bool update_link = false;
4588 bool update_pause = false;
4589 struct bnxt_link_info *link_info = &bp->link_info;
4590
4591 rc = bnxt_update_link(bp, true);
4592 if (rc) {
4593 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
4594 rc);
4595 return rc;
4596 }
4597 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
4598 link_info->auto_pause_setting != link_info->req_flow_ctrl)
4599 update_pause = true;
4600 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
4601 link_info->force_pause_setting != link_info->req_flow_ctrl)
4602 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04004603 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
4604 if (BNXT_AUTO_MODE(link_info->auto_mode))
4605 update_link = true;
4606 if (link_info->req_link_speed != link_info->force_link_speed)
4607 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05004608 if (link_info->req_duplex != link_info->duplex_setting)
4609 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04004610 } else {
4611 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
4612 update_link = true;
4613 if (link_info->advertising != link_info->auto_link_speeds)
4614 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04004615 }
4616
4617 if (update_link)
4618 rc = bnxt_hwrm_set_link_setting(bp, update_pause);
4619 else if (update_pause)
4620 rc = bnxt_hwrm_set_pause(bp);
4621 if (rc) {
4622 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
4623 rc);
4624 return rc;
4625 }
4626
4627 return rc;
4628}
4629
Jeffrey Huang11809492015-11-05 16:25:49 -05004630/* Common routine to pre-map certain register block to different GRC window.
4631 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
4632 * in PF and 3 windows in VF that can be customized to map in different
4633 * register blocks.
4634 */
4635static void bnxt_preset_reg_win(struct bnxt *bp)
4636{
4637 if (BNXT_PF(bp)) {
4638 /* CAG registers map to GRC window #4 */
4639 writel(BNXT_CAG_REG_BASE,
4640 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
4641 }
4642}
4643
Michael Chanc0c050c2015-10-22 16:01:17 -04004644static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
4645{
4646 int rc = 0;
4647
Jeffrey Huang11809492015-11-05 16:25:49 -05004648 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04004649 netif_carrier_off(bp->dev);
4650 if (irq_re_init) {
4651 rc = bnxt_setup_int_mode(bp);
4652 if (rc) {
4653 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
4654 rc);
4655 return rc;
4656 }
4657 }
4658 if ((bp->flags & BNXT_FLAG_RFS) &&
4659 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
4660 /* disable RFS if falling back to INTA */
4661 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
4662 bp->flags &= ~BNXT_FLAG_RFS;
4663 }
4664
4665 rc = bnxt_alloc_mem(bp, irq_re_init);
4666 if (rc) {
4667 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
4668 goto open_err_free_mem;
4669 }
4670
4671 if (irq_re_init) {
4672 bnxt_init_napi(bp);
4673 rc = bnxt_request_irq(bp);
4674 if (rc) {
4675 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
4676 goto open_err;
4677 }
4678 }
4679
4680 bnxt_enable_napi(bp);
4681
4682 rc = bnxt_init_nic(bp, irq_re_init);
4683 if (rc) {
4684 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
4685 goto open_err;
4686 }
4687
4688 if (link_re_init) {
4689 rc = bnxt_update_phy_setting(bp);
4690 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05004691 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004692 }
4693
4694 if (irq_re_init) {
4695#if defined(CONFIG_VXLAN) || defined(CONFIG_VXLAN_MODULE)
4696 vxlan_get_rx_port(bp->dev);
4697#endif
4698 if (!bnxt_hwrm_tunnel_dst_port_alloc(
4699 bp, htons(0x17c1),
4700 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE))
4701 bp->nge_port_cnt = 1;
4702 }
4703
Michael Chancaefe522015-12-09 19:35:42 -05004704 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04004705 bnxt_enable_int(bp);
4706 /* Enable TX queues */
4707 bnxt_tx_enable(bp);
4708 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan035a1532016-02-19 19:43:19 -05004709 bnxt_update_link(bp, true);
Michael Chanc0c050c2015-10-22 16:01:17 -04004710
4711 return 0;
4712
4713open_err:
4714 bnxt_disable_napi(bp);
4715 bnxt_del_napi(bp);
4716
4717open_err_free_mem:
4718 bnxt_free_skbs(bp);
4719 bnxt_free_irq(bp);
4720 bnxt_free_mem(bp, true);
4721 return rc;
4722}
4723
4724/* rtnl_lock held */
4725int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
4726{
4727 int rc = 0;
4728
4729 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
4730 if (rc) {
4731 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
4732 dev_close(bp->dev);
4733 }
4734 return rc;
4735}
4736
4737static int bnxt_open(struct net_device *dev)
4738{
4739 struct bnxt *bp = netdev_priv(dev);
4740 int rc = 0;
4741
4742 rc = bnxt_hwrm_func_reset(bp);
4743 if (rc) {
4744 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
4745 rc);
4746 rc = -1;
4747 return rc;
4748 }
4749 return __bnxt_open_nic(bp, true, true);
4750}
4751
4752static void bnxt_disable_int_sync(struct bnxt *bp)
4753{
4754 int i;
4755
4756 atomic_inc(&bp->intr_sem);
4757 if (!netif_running(bp->dev))
4758 return;
4759
4760 bnxt_disable_int(bp);
4761 for (i = 0; i < bp->cp_nr_rings; i++)
4762 synchronize_irq(bp->irq_tbl[i].vector);
4763}
4764
4765int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
4766{
4767 int rc = 0;
4768
4769#ifdef CONFIG_BNXT_SRIOV
4770 if (bp->sriov_cfg) {
4771 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
4772 !bp->sriov_cfg,
4773 BNXT_SRIOV_CFG_WAIT_TMO);
4774 if (rc)
4775 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
4776 }
4777#endif
4778 /* Change device state to avoid TX queue wake up's */
4779 bnxt_tx_disable(bp);
4780
Michael Chancaefe522015-12-09 19:35:42 -05004781 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05004782 smp_mb__after_atomic();
4783 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
4784 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04004785
4786 /* Flush rings before disabling interrupts */
4787 bnxt_shutdown_nic(bp, irq_re_init);
4788
4789 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
4790
4791 bnxt_disable_napi(bp);
4792 bnxt_disable_int_sync(bp);
4793 del_timer_sync(&bp->timer);
4794 bnxt_free_skbs(bp);
4795
4796 if (irq_re_init) {
4797 bnxt_free_irq(bp);
4798 bnxt_del_napi(bp);
4799 }
4800 bnxt_free_mem(bp, irq_re_init);
4801 return rc;
4802}
4803
4804static int bnxt_close(struct net_device *dev)
4805{
4806 struct bnxt *bp = netdev_priv(dev);
4807
4808 bnxt_close_nic(bp, true, true);
4809 return 0;
4810}
4811
4812/* rtnl_lock held */
4813static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4814{
4815 switch (cmd) {
4816 case SIOCGMIIPHY:
4817 /* fallthru */
4818 case SIOCGMIIREG: {
4819 if (!netif_running(dev))
4820 return -EAGAIN;
4821
4822 return 0;
4823 }
4824
4825 case SIOCSMIIREG:
4826 if (!netif_running(dev))
4827 return -EAGAIN;
4828
4829 return 0;
4830
4831 default:
4832 /* do nothing */
4833 break;
4834 }
4835 return -EOPNOTSUPP;
4836}
4837
4838static struct rtnl_link_stats64 *
4839bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
4840{
4841 u32 i;
4842 struct bnxt *bp = netdev_priv(dev);
4843
4844 memset(stats, 0, sizeof(struct rtnl_link_stats64));
4845
4846 if (!bp->bnapi)
4847 return stats;
4848
4849 /* TODO check if we need to synchronize with bnxt_close path */
4850 for (i = 0; i < bp->cp_nr_rings; i++) {
4851 struct bnxt_napi *bnapi = bp->bnapi[i];
4852 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4853 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
4854
4855 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
4856 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
4857 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
4858
4859 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
4860 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
4861 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
4862
4863 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
4864 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
4865 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
4866
4867 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
4868 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
4869 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
4870
4871 stats->rx_missed_errors +=
4872 le64_to_cpu(hw_stats->rx_discard_pkts);
4873
4874 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
4875
Michael Chanc0c050c2015-10-22 16:01:17 -04004876 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
4877 }
4878
4879 return stats;
4880}
4881
4882static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
4883{
4884 struct net_device *dev = bp->dev;
4885 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4886 struct netdev_hw_addr *ha;
4887 u8 *haddr;
4888 int mc_count = 0;
4889 bool update = false;
4890 int off = 0;
4891
4892 netdev_for_each_mc_addr(ha, dev) {
4893 if (mc_count >= BNXT_MAX_MC_ADDRS) {
4894 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4895 vnic->mc_list_count = 0;
4896 return false;
4897 }
4898 haddr = ha->addr;
4899 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
4900 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
4901 update = true;
4902 }
4903 off += ETH_ALEN;
4904 mc_count++;
4905 }
4906 if (mc_count)
4907 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
4908
4909 if (mc_count != vnic->mc_list_count) {
4910 vnic->mc_list_count = mc_count;
4911 update = true;
4912 }
4913 return update;
4914}
4915
4916static bool bnxt_uc_list_updated(struct bnxt *bp)
4917{
4918 struct net_device *dev = bp->dev;
4919 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4920 struct netdev_hw_addr *ha;
4921 int off = 0;
4922
4923 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
4924 return true;
4925
4926 netdev_for_each_uc_addr(ha, dev) {
4927 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
4928 return true;
4929
4930 off += ETH_ALEN;
4931 }
4932 return false;
4933}
4934
4935static void bnxt_set_rx_mode(struct net_device *dev)
4936{
4937 struct bnxt *bp = netdev_priv(dev);
4938 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4939 u32 mask = vnic->rx_mask;
4940 bool mc_update = false;
4941 bool uc_update;
4942
4943 if (!netif_running(dev))
4944 return;
4945
4946 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
4947 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
4948 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
4949
4950 /* Only allow PF to be in promiscuous mode */
4951 if ((dev->flags & IFF_PROMISC) && BNXT_PF(bp))
4952 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4953
4954 uc_update = bnxt_uc_list_updated(bp);
4955
4956 if (dev->flags & IFF_ALLMULTI) {
4957 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4958 vnic->mc_list_count = 0;
4959 } else {
4960 mc_update = bnxt_mc_list_updated(bp, &mask);
4961 }
4962
4963 if (mask != vnic->rx_mask || uc_update || mc_update) {
4964 vnic->rx_mask = mask;
4965
4966 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
4967 schedule_work(&bp->sp_task);
4968 }
4969}
4970
Michael Chanb664f002015-12-02 01:54:08 -05004971static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004972{
4973 struct net_device *dev = bp->dev;
4974 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4975 struct netdev_hw_addr *ha;
4976 int i, off = 0, rc;
4977 bool uc_update;
4978
4979 netif_addr_lock_bh(dev);
4980 uc_update = bnxt_uc_list_updated(bp);
4981 netif_addr_unlock_bh(dev);
4982
4983 if (!uc_update)
4984 goto skip_uc;
4985
4986 mutex_lock(&bp->hwrm_cmd_lock);
4987 for (i = 1; i < vnic->uc_filter_count; i++) {
4988 struct hwrm_cfa_l2_filter_free_input req = {0};
4989
4990 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
4991 -1);
4992
4993 req.l2_filter_id = vnic->fw_l2_filter_id[i];
4994
4995 rc = _hwrm_send_message(bp, &req, sizeof(req),
4996 HWRM_CMD_TIMEOUT);
4997 }
4998 mutex_unlock(&bp->hwrm_cmd_lock);
4999
5000 vnic->uc_filter_count = 1;
5001
5002 netif_addr_lock_bh(dev);
5003 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5004 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5005 } else {
5006 netdev_for_each_uc_addr(ha, dev) {
5007 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5008 off += ETH_ALEN;
5009 vnic->uc_filter_count++;
5010 }
5011 }
5012 netif_addr_unlock_bh(dev);
5013
5014 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5015 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5016 if (rc) {
5017 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5018 rc);
5019 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005020 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005021 }
5022 }
5023
5024skip_uc:
5025 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5026 if (rc)
5027 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5028 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005029
5030 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005031}
5032
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005033static bool bnxt_rfs_capable(struct bnxt *bp)
5034{
5035#ifdef CONFIG_RFS_ACCEL
5036 struct bnxt_pf_info *pf = &bp->pf;
5037 int vnics;
5038
5039 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5040 return false;
5041
5042 vnics = 1 + bp->rx_nr_rings;
5043 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics)
5044 return false;
5045
5046 return true;
5047#else
5048 return false;
5049#endif
5050}
5051
Michael Chanc0c050c2015-10-22 16:01:17 -04005052static netdev_features_t bnxt_fix_features(struct net_device *dev,
5053 netdev_features_t features)
5054{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005055 struct bnxt *bp = netdev_priv(dev);
5056
5057 if (!bnxt_rfs_capable(bp))
5058 features &= ~NETIF_F_NTUPLE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005059 return features;
5060}
5061
5062static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5063{
5064 struct bnxt *bp = netdev_priv(dev);
5065 u32 flags = bp->flags;
5066 u32 changes;
5067 int rc = 0;
5068 bool re_init = false;
5069 bool update_tpa = false;
5070
5071 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
5072 if ((features & NETIF_F_GRO) && (bp->pdev->revision > 0))
5073 flags |= BNXT_FLAG_GRO;
5074 if (features & NETIF_F_LRO)
5075 flags |= BNXT_FLAG_LRO;
5076
5077 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5078 flags |= BNXT_FLAG_STRIP_VLAN;
5079
5080 if (features & NETIF_F_NTUPLE)
5081 flags |= BNXT_FLAG_RFS;
5082
5083 changes = flags ^ bp->flags;
5084 if (changes & BNXT_FLAG_TPA) {
5085 update_tpa = true;
5086 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5087 (flags & BNXT_FLAG_TPA) == 0)
5088 re_init = true;
5089 }
5090
5091 if (changes & ~BNXT_FLAG_TPA)
5092 re_init = true;
5093
5094 if (flags != bp->flags) {
5095 u32 old_flags = bp->flags;
5096
5097 bp->flags = flags;
5098
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005099 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005100 if (update_tpa)
5101 bnxt_set_ring_params(bp);
5102 return rc;
5103 }
5104
5105 if (re_init) {
5106 bnxt_close_nic(bp, false, false);
5107 if (update_tpa)
5108 bnxt_set_ring_params(bp);
5109
5110 return bnxt_open_nic(bp, false, false);
5111 }
5112 if (update_tpa) {
5113 rc = bnxt_set_tpa(bp,
5114 (flags & BNXT_FLAG_TPA) ?
5115 true : false);
5116 if (rc)
5117 bp->flags = old_flags;
5118 }
5119 }
5120 return rc;
5121}
5122
Michael Chan9f554592016-01-02 23:44:58 -05005123static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5124{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005125 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005126 int i = bnapi->index;
5127
Michael Chan3b2b7d92016-01-02 23:45:00 -05005128 if (!txr)
5129 return;
5130
Michael Chan9f554592016-01-02 23:44:58 -05005131 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5132 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5133 txr->tx_cons);
5134}
5135
5136static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
5137{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005138 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005139 int i = bnapi->index;
5140
Michael Chan3b2b7d92016-01-02 23:45:00 -05005141 if (!rxr)
5142 return;
5143
Michael Chan9f554592016-01-02 23:44:58 -05005144 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",
5145 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
5146 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
5147 rxr->rx_sw_agg_prod);
5148}
5149
5150static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
5151{
5152 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5153 int i = bnapi->index;
5154
5155 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
5156 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
5157}
5158
Michael Chanc0c050c2015-10-22 16:01:17 -04005159static void bnxt_dbg_dump_states(struct bnxt *bp)
5160{
5161 int i;
5162 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04005163
5164 for (i = 0; i < bp->cp_nr_rings; i++) {
5165 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005166 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05005167 bnxt_dump_tx_sw_state(bnapi);
5168 bnxt_dump_rx_sw_state(bnapi);
5169 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005170 }
5171 }
5172}
5173
5174static void bnxt_reset_task(struct bnxt *bp)
5175{
5176 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05005177 if (netif_running(bp->dev)) {
5178 bnxt_close_nic(bp, false, false);
5179 bnxt_open_nic(bp, false, false);
5180 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005181}
5182
5183static void bnxt_tx_timeout(struct net_device *dev)
5184{
5185 struct bnxt *bp = netdev_priv(dev);
5186
5187 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
5188 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
5189 schedule_work(&bp->sp_task);
5190}
5191
5192#ifdef CONFIG_NET_POLL_CONTROLLER
5193static void bnxt_poll_controller(struct net_device *dev)
5194{
5195 struct bnxt *bp = netdev_priv(dev);
5196 int i;
5197
5198 for (i = 0; i < bp->cp_nr_rings; i++) {
5199 struct bnxt_irq *irq = &bp->irq_tbl[i];
5200
5201 disable_irq(irq->vector);
5202 irq->handler(irq->vector, bp->bnapi[i]);
5203 enable_irq(irq->vector);
5204 }
5205}
5206#endif
5207
5208static void bnxt_timer(unsigned long data)
5209{
5210 struct bnxt *bp = (struct bnxt *)data;
5211 struct net_device *dev = bp->dev;
5212
5213 if (!netif_running(dev))
5214 return;
5215
5216 if (atomic_read(&bp->intr_sem) != 0)
5217 goto bnxt_restart_timer;
5218
5219bnxt_restart_timer:
5220 mod_timer(&bp->timer, jiffies + bp->current_interval);
5221}
5222
5223static void bnxt_cfg_ntp_filters(struct bnxt *);
5224
5225static void bnxt_sp_task(struct work_struct *work)
5226{
5227 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
5228 int rc;
5229
Michael Chan4cebdce2015-12-09 19:35:43 -05005230 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5231 smp_mb__after_atomic();
5232 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
5233 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005234 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05005235 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005236
5237 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
5238 bnxt_cfg_rx_mode(bp);
5239
5240 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
5241 bnxt_cfg_ntp_filters(bp);
5242 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
5243 rc = bnxt_update_link(bp, true);
5244 if (rc)
5245 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
5246 rc);
5247 }
5248 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
5249 bnxt_hwrm_exec_fwd_req(bp);
5250 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
5251 bnxt_hwrm_tunnel_dst_port_alloc(
5252 bp, bp->vxlan_port,
5253 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5254 }
5255 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
5256 bnxt_hwrm_tunnel_dst_port_free(
5257 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5258 }
Michael Chan028de142015-12-09 19:35:44 -05005259 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) {
5260 /* bnxt_reset_task() calls bnxt_close_nic() which waits
5261 * for BNXT_STATE_IN_SP_TASK to clear.
5262 */
5263 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5264 rtnl_lock();
Michael Chanc0c050c2015-10-22 16:01:17 -04005265 bnxt_reset_task(bp);
Michael Chan028de142015-12-09 19:35:44 -05005266 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5267 rtnl_unlock();
5268 }
Michael Chan4cebdce2015-12-09 19:35:43 -05005269
5270 smp_mb__before_atomic();
5271 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005272}
5273
5274static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
5275{
5276 int rc;
5277 struct bnxt *bp = netdev_priv(dev);
5278
5279 SET_NETDEV_DEV(dev, &pdev->dev);
5280
5281 /* enable device (incl. PCI PM wakeup), and bus-mastering */
5282 rc = pci_enable_device(pdev);
5283 if (rc) {
5284 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
5285 goto init_err;
5286 }
5287
5288 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
5289 dev_err(&pdev->dev,
5290 "Cannot find PCI device base address, aborting\n");
5291 rc = -ENODEV;
5292 goto init_err_disable;
5293 }
5294
5295 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
5296 if (rc) {
5297 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
5298 goto init_err_disable;
5299 }
5300
5301 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
5302 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
5303 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
5304 goto init_err_disable;
5305 }
5306
5307 pci_set_master(pdev);
5308
5309 bp->dev = dev;
5310 bp->pdev = pdev;
5311
5312 bp->bar0 = pci_ioremap_bar(pdev, 0);
5313 if (!bp->bar0) {
5314 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
5315 rc = -ENOMEM;
5316 goto init_err_release;
5317 }
5318
5319 bp->bar1 = pci_ioremap_bar(pdev, 2);
5320 if (!bp->bar1) {
5321 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
5322 rc = -ENOMEM;
5323 goto init_err_release;
5324 }
5325
5326 bp->bar2 = pci_ioremap_bar(pdev, 4);
5327 if (!bp->bar2) {
5328 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
5329 rc = -ENOMEM;
5330 goto init_err_release;
5331 }
5332
5333 INIT_WORK(&bp->sp_task, bnxt_sp_task);
5334
5335 spin_lock_init(&bp->ntp_fltr_lock);
5336
5337 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
5338 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
5339
Michael Chandfb5b892016-02-26 04:00:01 -05005340 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05005341 bp->rx_coal_ticks = 12;
5342 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05005343 bp->rx_coal_ticks_irq = 1;
5344 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04005345
Michael Chandfc9c942016-02-26 04:00:03 -05005346 bp->tx_coal_ticks = 25;
5347 bp->tx_coal_bufs = 30;
5348 bp->tx_coal_ticks_irq = 2;
5349 bp->tx_coal_bufs_irq = 2;
5350
Michael Chanc0c050c2015-10-22 16:01:17 -04005351 init_timer(&bp->timer);
5352 bp->timer.data = (unsigned long)bp;
5353 bp->timer.function = bnxt_timer;
5354 bp->current_interval = BNXT_TIMER_INTERVAL;
5355
Michael Chancaefe522015-12-09 19:35:42 -05005356 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005357
5358 return 0;
5359
5360init_err_release:
5361 if (bp->bar2) {
5362 pci_iounmap(pdev, bp->bar2);
5363 bp->bar2 = NULL;
5364 }
5365
5366 if (bp->bar1) {
5367 pci_iounmap(pdev, bp->bar1);
5368 bp->bar1 = NULL;
5369 }
5370
5371 if (bp->bar0) {
5372 pci_iounmap(pdev, bp->bar0);
5373 bp->bar0 = NULL;
5374 }
5375
5376 pci_release_regions(pdev);
5377
5378init_err_disable:
5379 pci_disable_device(pdev);
5380
5381init_err:
5382 return rc;
5383}
5384
5385/* rtnl_lock held */
5386static int bnxt_change_mac_addr(struct net_device *dev, void *p)
5387{
5388 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05005389 struct bnxt *bp = netdev_priv(dev);
5390 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005391
5392 if (!is_valid_ether_addr(addr->sa_data))
5393 return -EADDRNOTAVAIL;
5394
Jeffrey Huangbdd43472015-12-02 01:54:07 -05005395#ifdef CONFIG_BNXT_SRIOV
5396 if (BNXT_VF(bp) && is_valid_ether_addr(bp->vf.mac_addr))
5397 return -EADDRNOTAVAIL;
5398#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005399
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05005400 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
5401 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005402
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05005403 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
5404 if (netif_running(dev)) {
5405 bnxt_close_nic(bp, false, false);
5406 rc = bnxt_open_nic(bp, false, false);
5407 }
5408
5409 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005410}
5411
5412/* rtnl_lock held */
5413static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
5414{
5415 struct bnxt *bp = netdev_priv(dev);
5416
5417 if (new_mtu < 60 || new_mtu > 9000)
5418 return -EINVAL;
5419
5420 if (netif_running(dev))
5421 bnxt_close_nic(bp, false, false);
5422
5423 dev->mtu = new_mtu;
5424 bnxt_set_ring_params(bp);
5425
5426 if (netif_running(dev))
5427 return bnxt_open_nic(bp, false, false);
5428
5429 return 0;
5430}
5431
John Fastabend16e5cc62016-02-16 21:16:43 -08005432static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
5433 struct tc_to_netdev *ntc)
Michael Chanc0c050c2015-10-22 16:01:17 -04005434{
5435 struct bnxt *bp = netdev_priv(dev);
John Fastabend16e5cc62016-02-16 21:16:43 -08005436 u8 tc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005437
John Fastabend16e5cc62016-02-16 21:16:43 -08005438 if (handle != TC_H_ROOT || ntc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08005439 return -EINVAL;
5440
John Fastabend16e5cc62016-02-16 21:16:43 -08005441 tc = ntc->tc;
5442
Michael Chanc0c050c2015-10-22 16:01:17 -04005443 if (tc > bp->max_tc) {
5444 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
5445 tc, bp->max_tc);
5446 return -EINVAL;
5447 }
5448
5449 if (netdev_get_num_tc(dev) == tc)
5450 return 0;
5451
5452 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05005453 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05005454 bool sh = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005455
Michael Chan01657bc2016-01-02 23:45:03 -05005456 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5457 sh = true;
5458
5459 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005460 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04005461 return -ENOMEM;
5462 }
5463
5464 /* Needs to close the device and do hw resource re-allocations */
5465 if (netif_running(bp->dev))
5466 bnxt_close_nic(bp, true, false);
5467
5468 if (tc) {
5469 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
5470 netdev_set_num_tc(dev, tc);
5471 } else {
5472 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
5473 netdev_reset_tc(dev);
5474 }
5475 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
5476 bp->num_stat_ctxs = bp->cp_nr_rings;
5477
5478 if (netif_running(bp->dev))
5479 return bnxt_open_nic(bp, true, false);
5480
5481 return 0;
5482}
5483
5484#ifdef CONFIG_RFS_ACCEL
5485static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
5486 struct bnxt_ntuple_filter *f2)
5487{
5488 struct flow_keys *keys1 = &f1->fkeys;
5489 struct flow_keys *keys2 = &f2->fkeys;
5490
5491 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
5492 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
5493 keys1->ports.ports == keys2->ports.ports &&
5494 keys1->basic.ip_proto == keys2->basic.ip_proto &&
5495 keys1->basic.n_proto == keys2->basic.n_proto &&
5496 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr))
5497 return true;
5498
5499 return false;
5500}
5501
5502static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
5503 u16 rxq_index, u32 flow_id)
5504{
5505 struct bnxt *bp = netdev_priv(dev);
5506 struct bnxt_ntuple_filter *fltr, *new_fltr;
5507 struct flow_keys *fkeys;
5508 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chan84e86b92015-11-05 16:25:50 -05005509 int rc = 0, idx, bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005510 struct hlist_head *head;
5511
5512 if (skb->encapsulation)
5513 return -EPROTONOSUPPORT;
5514
5515 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
5516 if (!new_fltr)
5517 return -ENOMEM;
5518
5519 fkeys = &new_fltr->fkeys;
5520 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
5521 rc = -EPROTONOSUPPORT;
5522 goto err_free;
5523 }
5524
5525 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
5526 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
5527 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
5528 rc = -EPROTONOSUPPORT;
5529 goto err_free;
5530 }
5531
5532 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
5533
5534 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
5535 head = &bp->ntp_fltr_hash_tbl[idx];
5536 rcu_read_lock();
5537 hlist_for_each_entry_rcu(fltr, head, hash) {
5538 if (bnxt_fltr_match(fltr, new_fltr)) {
5539 rcu_read_unlock();
5540 rc = 0;
5541 goto err_free;
5542 }
5543 }
5544 rcu_read_unlock();
5545
5546 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05005547 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5548 BNXT_NTP_FLTR_MAX_FLTR, 0);
5549 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005550 spin_unlock_bh(&bp->ntp_fltr_lock);
5551 rc = -ENOMEM;
5552 goto err_free;
5553 }
5554
Michael Chan84e86b92015-11-05 16:25:50 -05005555 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04005556 new_fltr->flow_id = flow_id;
5557 new_fltr->rxq = rxq_index;
5558 hlist_add_head_rcu(&new_fltr->hash, head);
5559 bp->ntp_fltr_count++;
5560 spin_unlock_bh(&bp->ntp_fltr_lock);
5561
5562 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
5563 schedule_work(&bp->sp_task);
5564
5565 return new_fltr->sw_id;
5566
5567err_free:
5568 kfree(new_fltr);
5569 return rc;
5570}
5571
5572static void bnxt_cfg_ntp_filters(struct bnxt *bp)
5573{
5574 int i;
5575
5576 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5577 struct hlist_head *head;
5578 struct hlist_node *tmp;
5579 struct bnxt_ntuple_filter *fltr;
5580 int rc;
5581
5582 head = &bp->ntp_fltr_hash_tbl[i];
5583 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
5584 bool del = false;
5585
5586 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
5587 if (rps_may_expire_flow(bp->dev, fltr->rxq,
5588 fltr->flow_id,
5589 fltr->sw_id)) {
5590 bnxt_hwrm_cfa_ntuple_filter_free(bp,
5591 fltr);
5592 del = true;
5593 }
5594 } else {
5595 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
5596 fltr);
5597 if (rc)
5598 del = true;
5599 else
5600 set_bit(BNXT_FLTR_VALID, &fltr->state);
5601 }
5602
5603 if (del) {
5604 spin_lock_bh(&bp->ntp_fltr_lock);
5605 hlist_del_rcu(&fltr->hash);
5606 bp->ntp_fltr_count--;
5607 spin_unlock_bh(&bp->ntp_fltr_lock);
5608 synchronize_rcu();
5609 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5610 kfree(fltr);
5611 }
5612 }
5613 }
Jeffrey Huang19241362016-02-26 04:00:00 -05005614 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
5615 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04005616}
5617
5618#else
5619
5620static void bnxt_cfg_ntp_filters(struct bnxt *bp)
5621{
5622}
5623
5624#endif /* CONFIG_RFS_ACCEL */
5625
5626static void bnxt_add_vxlan_port(struct net_device *dev, sa_family_t sa_family,
5627 __be16 port)
5628{
5629 struct bnxt *bp = netdev_priv(dev);
5630
5631 if (!netif_running(dev))
5632 return;
5633
5634 if (sa_family != AF_INET6 && sa_family != AF_INET)
5635 return;
5636
5637 if (bp->vxlan_port_cnt && bp->vxlan_port != port)
5638 return;
5639
5640 bp->vxlan_port_cnt++;
5641 if (bp->vxlan_port_cnt == 1) {
5642 bp->vxlan_port = port;
5643 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
5644 schedule_work(&bp->sp_task);
5645 }
5646}
5647
5648static void bnxt_del_vxlan_port(struct net_device *dev, sa_family_t sa_family,
5649 __be16 port)
5650{
5651 struct bnxt *bp = netdev_priv(dev);
5652
5653 if (!netif_running(dev))
5654 return;
5655
5656 if (sa_family != AF_INET6 && sa_family != AF_INET)
5657 return;
5658
5659 if (bp->vxlan_port_cnt && bp->vxlan_port == port) {
5660 bp->vxlan_port_cnt--;
5661
5662 if (bp->vxlan_port_cnt == 0) {
5663 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
5664 schedule_work(&bp->sp_task);
5665 }
5666 }
5667}
5668
5669static const struct net_device_ops bnxt_netdev_ops = {
5670 .ndo_open = bnxt_open,
5671 .ndo_start_xmit = bnxt_start_xmit,
5672 .ndo_stop = bnxt_close,
5673 .ndo_get_stats64 = bnxt_get_stats64,
5674 .ndo_set_rx_mode = bnxt_set_rx_mode,
5675 .ndo_do_ioctl = bnxt_ioctl,
5676 .ndo_validate_addr = eth_validate_addr,
5677 .ndo_set_mac_address = bnxt_change_mac_addr,
5678 .ndo_change_mtu = bnxt_change_mtu,
5679 .ndo_fix_features = bnxt_fix_features,
5680 .ndo_set_features = bnxt_set_features,
5681 .ndo_tx_timeout = bnxt_tx_timeout,
5682#ifdef CONFIG_BNXT_SRIOV
5683 .ndo_get_vf_config = bnxt_get_vf_config,
5684 .ndo_set_vf_mac = bnxt_set_vf_mac,
5685 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
5686 .ndo_set_vf_rate = bnxt_set_vf_bw,
5687 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
5688 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
5689#endif
5690#ifdef CONFIG_NET_POLL_CONTROLLER
5691 .ndo_poll_controller = bnxt_poll_controller,
5692#endif
5693 .ndo_setup_tc = bnxt_setup_tc,
5694#ifdef CONFIG_RFS_ACCEL
5695 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
5696#endif
5697 .ndo_add_vxlan_port = bnxt_add_vxlan_port,
5698 .ndo_del_vxlan_port = bnxt_del_vxlan_port,
5699#ifdef CONFIG_NET_RX_BUSY_POLL
5700 .ndo_busy_poll = bnxt_busy_poll,
5701#endif
5702};
5703
5704static void bnxt_remove_one(struct pci_dev *pdev)
5705{
5706 struct net_device *dev = pci_get_drvdata(pdev);
5707 struct bnxt *bp = netdev_priv(dev);
5708
5709 if (BNXT_PF(bp))
5710 bnxt_sriov_disable(bp);
5711
5712 unregister_netdev(dev);
5713 cancel_work_sync(&bp->sp_task);
5714 bp->sp_event = 0;
5715
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05005716 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005717 bnxt_free_hwrm_resources(bp);
5718 pci_iounmap(pdev, bp->bar2);
5719 pci_iounmap(pdev, bp->bar1);
5720 pci_iounmap(pdev, bp->bar0);
5721 free_netdev(dev);
5722
5723 pci_release_regions(pdev);
5724 pci_disable_device(pdev);
5725}
5726
5727static int bnxt_probe_phy(struct bnxt *bp)
5728{
5729 int rc = 0;
5730 struct bnxt_link_info *link_info = &bp->link_info;
5731 char phy_ver[PHY_VER_STR_LEN];
5732
5733 rc = bnxt_update_link(bp, false);
5734 if (rc) {
5735 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
5736 rc);
5737 return rc;
5738 }
5739
5740 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05005741 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
5742 link_info->autoneg = BNXT_AUTONEG_SPEED |
5743 BNXT_AUTONEG_FLOW_CTRL;
5744 link_info->advertising = link_info->auto_link_speeds;
Michael Chanc0c050c2015-10-22 16:01:17 -04005745 link_info->req_flow_ctrl = link_info->auto_pause_setting;
Michael Chan0d8abf02016-02-10 17:33:47 -05005746 } else {
5747 link_info->req_link_speed = link_info->force_link_speed;
5748 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04005749 link_info->req_flow_ctrl = link_info->force_pause_setting;
5750 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005751 snprintf(phy_ver, PHY_VER_STR_LEN, " ph %d.%d.%d",
5752 link_info->phy_ver[0],
5753 link_info->phy_ver[1],
5754 link_info->phy_ver[2]);
5755 strcat(bp->fw_ver_str, phy_ver);
5756 return rc;
5757}
5758
5759static int bnxt_get_max_irq(struct pci_dev *pdev)
5760{
5761 u16 ctrl;
5762
5763 if (!pdev->msix_cap)
5764 return 1;
5765
5766 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
5767 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
5768}
5769
Michael Chan6e6c5a52016-01-02 23:45:02 -05005770static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
5771 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005772{
Michael Chan6e6c5a52016-01-02 23:45:02 -05005773 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04005774
Michael Chan379a80a2015-10-23 15:06:19 -04005775#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01005776 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005777 *max_tx = bp->vf.max_tx_rings;
5778 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05005779 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
5780 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05005781 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01005782 } else
Michael Chan379a80a2015-10-23 15:06:19 -04005783#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01005784 {
5785 *max_tx = bp->pf.max_tx_rings;
5786 *max_rx = bp->pf.max_rx_rings;
5787 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
5788 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
5789 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04005790 }
Arnd Bergmann415b6f12016-01-12 16:05:08 +01005791
Michael Chanc0c050c2015-10-22 16:01:17 -04005792 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5793 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05005794 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05005795}
5796
5797int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
5798{
5799 int rx, tx, cp;
5800
5801 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
5802 if (!rx || !tx || !cp)
5803 return -ENOMEM;
5804
5805 *max_rx = rx;
5806 *max_tx = tx;
5807 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
5808}
5809
5810static int bnxt_set_dflt_rings(struct bnxt *bp)
5811{
5812 int dflt_rings, max_rx_rings, max_tx_rings, rc;
5813 bool sh = true;
5814
5815 if (sh)
5816 bp->flags |= BNXT_FLAG_SHARED_RINGS;
5817 dflt_rings = netif_get_num_default_rss_queues();
5818 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
5819 if (rc)
5820 return rc;
5821 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
5822 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
5823 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
5824 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5825 bp->tx_nr_rings + bp->rx_nr_rings;
5826 bp->num_stat_ctxs = bp->cp_nr_rings;
5827 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005828}
5829
5830static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5831{
5832 static int version_printed;
5833 struct net_device *dev;
5834 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05005835 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04005836
5837 if (version_printed++ == 0)
5838 pr_info("%s", version);
5839
5840 max_irqs = bnxt_get_max_irq(pdev);
5841 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
5842 if (!dev)
5843 return -ENOMEM;
5844
5845 bp = netdev_priv(dev);
5846
5847 if (bnxt_vf_pciid(ent->driver_data))
5848 bp->flags |= BNXT_FLAG_VF;
5849
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005850 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04005851 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04005852
5853 rc = bnxt_init_board(pdev, dev);
5854 if (rc < 0)
5855 goto init_err_free;
5856
5857 dev->netdev_ops = &bnxt_netdev_ops;
5858 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
5859 dev->ethtool_ops = &bnxt_ethtool_ops;
5860
5861 pci_set_drvdata(pdev, dev);
5862
5863 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
5864 NETIF_F_TSO | NETIF_F_TSO6 |
5865 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
5866 NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT |
5867 NETIF_F_RXHASH |
5868 NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_GRO;
5869
Michael Chanc0c050c2015-10-22 16:01:17 -04005870 dev->hw_enc_features =
5871 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
5872 NETIF_F_TSO | NETIF_F_TSO6 |
5873 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
5874 NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT;
5875 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
5876 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
5877 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
5878 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
5879 dev->priv_flags |= IFF_UNICAST_FLT;
5880
5881#ifdef CONFIG_BNXT_SRIOV
5882 init_waitqueue_head(&bp->sriov_cfg_wait);
5883#endif
5884 rc = bnxt_alloc_hwrm_resources(bp);
5885 if (rc)
5886 goto init_err;
5887
5888 mutex_init(&bp->hwrm_cmd_lock);
5889 bnxt_hwrm_ver_get(bp);
5890
5891 rc = bnxt_hwrm_func_drv_rgtr(bp);
5892 if (rc)
5893 goto init_err;
5894
5895 /* Get the MAX capabilities for this function */
5896 rc = bnxt_hwrm_func_qcaps(bp);
5897 if (rc) {
5898 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
5899 rc);
5900 rc = -1;
5901 goto init_err;
5902 }
5903
5904 rc = bnxt_hwrm_queue_qportcfg(bp);
5905 if (rc) {
5906 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
5907 rc);
5908 rc = -1;
5909 goto init_err;
5910 }
5911
5912 bnxt_set_tpa_flags(bp);
5913 bnxt_set_ring_params(bp);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05005914 if (BNXT_PF(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005915 bp->pf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04005916#if defined(CONFIG_BNXT_SRIOV)
Jeffrey Huangbdd43472015-12-02 01:54:07 -05005917 else
Michael Chanc0c050c2015-10-22 16:01:17 -04005918 bp->vf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04005919#endif
Michael Chan6e6c5a52016-01-02 23:45:02 -05005920 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005921
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005922 if (BNXT_PF(bp)) {
5923 dev->hw_features |= NETIF_F_NTUPLE;
5924 if (bnxt_rfs_capable(bp)) {
5925 bp->flags |= BNXT_FLAG_RFS;
5926 dev->features |= NETIF_F_NTUPLE;
5927 }
5928 }
5929
Michael Chanc0c050c2015-10-22 16:01:17 -04005930 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
5931 bp->flags |= BNXT_FLAG_STRIP_VLAN;
5932
5933 rc = bnxt_probe_phy(bp);
5934 if (rc)
5935 goto init_err;
5936
5937 rc = register_netdev(dev);
5938 if (rc)
5939 goto init_err;
5940
5941 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
5942 board_info[ent->driver_data].name,
5943 (long)pci_resource_start(pdev, 0), dev->dev_addr);
5944
5945 return 0;
5946
5947init_err:
5948 pci_iounmap(pdev, bp->bar0);
5949 pci_release_regions(pdev);
5950 pci_disable_device(pdev);
5951
5952init_err_free:
5953 free_netdev(dev);
5954 return rc;
5955}
5956
5957static struct pci_driver bnxt_pci_driver = {
5958 .name = DRV_MODULE_NAME,
5959 .id_table = bnxt_pci_tbl,
5960 .probe = bnxt_init_one,
5961 .remove = bnxt_remove_one,
5962#if defined(CONFIG_BNXT_SRIOV)
5963 .sriov_configure = bnxt_sriov_configure,
5964#endif
5965};
5966
5967module_pci_driver(bnxt_pci_driver);