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