1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2018 Broadcom Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
35 #include <linux/if_vlan.h>
36 #include <linux/if_bridge.h>
37 #include <linux/rtc.h>
38 #include <linux/bpf.h>
42 #include <net/checksum.h>
43 #include <net/ip6_checksum.h>
44 #include <net/udp_tunnel.h>
45 #include <linux/workqueue.h>
46 #include <linux/prefetch.h>
47 #include <linux/cache.h>
48 #include <linux/log2.h>
49 #include <linux/aer.h>
50 #include <linux/bitmap.h>
51 #include <linux/cpu_rmap.h>
52 #include <linux/cpumask.h>
53 #include <net/pkt_cls.h>
58 #include "bnxt_sriov.h"
59 #include "bnxt_ethtool.h"
64 #include "bnxt_devlink.h"
65 #include "bnxt_debugfs.h"
67 #define BNXT_TX_TIMEOUT (5 * HZ)
69 static const char version[] =
70 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
74 MODULE_VERSION(DRV_MODULE_VERSION);
76 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
77 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
78 #define BNXT_RX_COPY_THRESH 256
80 #define BNXT_TX_PUSH_THRESH 164
120 /* indexed by enum above */
121 static const struct {
124 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
125 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
126 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
127 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
128 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
129 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
130 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
131 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
132 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
133 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
134 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
135 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
136 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
137 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
138 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
139 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
140 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
141 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
142 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
143 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
144 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
145 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
146 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
147 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
148 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
149 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
150 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
151 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
152 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
153 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
154 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
155 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
156 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
157 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
158 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
161 static const struct pci_device_id bnxt_pci_tbl[] = {
162 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
164 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
165 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
166 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
167 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
168 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
169 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
171 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
172 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
173 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
174 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
175 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
176 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
177 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
178 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
179 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
180 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
181 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
182 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
183 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
184 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
185 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
186 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
187 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
188 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
189 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
190 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
191 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
192 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
193 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
194 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
195 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
196 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
197 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
202 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
203 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
204 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
205 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
206 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
207 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
208 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
213 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
215 static const u16 bnxt_vf_req_snif[] = {
219 HWRM_CFA_L2_FILTER_ALLOC,
222 static const u16 bnxt_async_events_arr[] = {
223 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
224 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
225 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
226 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
227 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
230 static struct workqueue_struct *bnxt_pf_wq;
232 static bool bnxt_vf_pciid(enum board_idx idx)
234 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
235 idx == NETXTREME_S_VF);
238 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
239 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
240 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
242 #define BNXT_CP_DB_REARM(db, raw_cons) \
243 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
245 #define BNXT_CP_DB(db, raw_cons) \
246 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
248 #define BNXT_CP_DB_IRQ_DIS(db) \
249 writel(DB_CP_IRQ_DIS_FLAGS, db)
251 const u16 bnxt_lhint_arr[] = {
252 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
253 TX_BD_FLAGS_LHINT_512_TO_1023,
254 TX_BD_FLAGS_LHINT_1024_TO_2047,
255 TX_BD_FLAGS_LHINT_1024_TO_2047,
256 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
257 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
258 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
259 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
260 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
261 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
262 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
263 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
264 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
265 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
266 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
267 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
268 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
269 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
270 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
273 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
275 struct metadata_dst *md_dst = skb_metadata_dst(skb);
277 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
280 return md_dst->u.port_info.port_id;
283 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
285 struct bnxt *bp = netdev_priv(dev);
287 struct tx_bd_ext *txbd1;
288 struct netdev_queue *txq;
291 unsigned int length, pad = 0;
292 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
294 struct pci_dev *pdev = bp->pdev;
295 struct bnxt_tx_ring_info *txr;
296 struct bnxt_sw_tx_bd *tx_buf;
298 i = skb_get_queue_mapping(skb);
299 if (unlikely(i >= bp->tx_nr_rings)) {
300 dev_kfree_skb_any(skb);
304 txq = netdev_get_tx_queue(dev, i);
305 txr = &bp->tx_ring[bp->tx_ring_map[i]];
308 free_size = bnxt_tx_avail(bp, txr);
309 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
310 netif_tx_stop_queue(txq);
311 return NETDEV_TX_BUSY;
315 len = skb_headlen(skb);
316 last_frag = skb_shinfo(skb)->nr_frags;
318 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
320 txbd->tx_bd_opaque = prod;
322 tx_buf = &txr->tx_buf_ring[prod];
324 tx_buf->nr_frags = last_frag;
327 cfa_action = bnxt_xmit_get_cfa_action(skb);
328 if (skb_vlan_tag_present(skb)) {
329 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
330 skb_vlan_tag_get(skb);
331 /* Currently supports 8021Q, 8021AD vlan offloads
332 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
334 if (skb->vlan_proto == htons(ETH_P_8021Q))
335 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
338 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
339 struct tx_push_buffer *tx_push_buf = txr->tx_push;
340 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
341 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
342 void *pdata = tx_push_buf->data;
346 /* Set COAL_NOW to be ready quickly for the next push */
347 tx_push->tx_bd_len_flags_type =
348 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
349 TX_BD_TYPE_LONG_TX_BD |
350 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
351 TX_BD_FLAGS_COAL_NOW |
352 TX_BD_FLAGS_PACKET_END |
353 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
355 if (skb->ip_summed == CHECKSUM_PARTIAL)
356 tx_push1->tx_bd_hsize_lflags =
357 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
359 tx_push1->tx_bd_hsize_lflags = 0;
361 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
362 tx_push1->tx_bd_cfa_action =
363 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
365 end = pdata + length;
366 end = PTR_ALIGN(end, 8) - 1;
369 skb_copy_from_linear_data(skb, pdata, len);
371 for (j = 0; j < last_frag; j++) {
372 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
375 fptr = skb_frag_address_safe(frag);
379 memcpy(pdata, fptr, skb_frag_size(frag));
380 pdata += skb_frag_size(frag);
383 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
384 txbd->tx_bd_haddr = txr->data_mapping;
385 prod = NEXT_TX(prod);
386 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
387 memcpy(txbd, tx_push1, sizeof(*txbd));
388 prod = NEXT_TX(prod);
390 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
394 netdev_tx_sent_queue(txq, skb->len);
395 wmb(); /* Sync is_push and byte queue before pushing data */
397 push_len = (length + sizeof(*tx_push) + 7) / 8;
399 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
400 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
401 (push_len - 16) << 1);
403 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
411 if (length < BNXT_MIN_PKT_SIZE) {
412 pad = BNXT_MIN_PKT_SIZE - length;
413 if (skb_pad(skb, pad)) {
414 /* SKB already freed. */
418 length = BNXT_MIN_PKT_SIZE;
421 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
423 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
424 dev_kfree_skb_any(skb);
429 dma_unmap_addr_set(tx_buf, mapping, mapping);
430 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
431 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
433 txbd->tx_bd_haddr = cpu_to_le64(mapping);
435 prod = NEXT_TX(prod);
436 txbd1 = (struct tx_bd_ext *)
437 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
439 txbd1->tx_bd_hsize_lflags = 0;
440 if (skb_is_gso(skb)) {
443 if (skb->encapsulation)
444 hdr_len = skb_inner_network_offset(skb) +
445 skb_inner_network_header_len(skb) +
446 inner_tcp_hdrlen(skb);
448 hdr_len = skb_transport_offset(skb) +
451 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
453 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
454 length = skb_shinfo(skb)->gso_size;
455 txbd1->tx_bd_mss = cpu_to_le32(length);
457 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
458 txbd1->tx_bd_hsize_lflags =
459 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
460 txbd1->tx_bd_mss = 0;
464 flags |= bnxt_lhint_arr[length];
465 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
467 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
468 txbd1->tx_bd_cfa_action =
469 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
470 for (i = 0; i < last_frag; i++) {
471 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
473 prod = NEXT_TX(prod);
474 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
476 len = skb_frag_size(frag);
477 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
480 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
483 tx_buf = &txr->tx_buf_ring[prod];
484 dma_unmap_addr_set(tx_buf, mapping, mapping);
486 txbd->tx_bd_haddr = cpu_to_le64(mapping);
488 flags = len << TX_BD_LEN_SHIFT;
489 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
493 txbd->tx_bd_len_flags_type =
494 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
495 TX_BD_FLAGS_PACKET_END);
497 netdev_tx_sent_queue(txq, skb->len);
499 /* Sync BD data before updating doorbell */
502 prod = NEXT_TX(prod);
505 if (!skb->xmit_more || netif_xmit_stopped(txq))
506 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
512 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
513 if (skb->xmit_more && !tx_buf->is_push)
514 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
516 netif_tx_stop_queue(txq);
518 /* netif_tx_stop_queue() must be done before checking
519 * tx index in bnxt_tx_avail() below, because in
520 * bnxt_tx_int(), we update tx index before checking for
521 * netif_tx_queue_stopped().
524 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
525 netif_tx_wake_queue(txq);
532 /* start back at beginning and unmap skb */
534 tx_buf = &txr->tx_buf_ring[prod];
536 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
537 skb_headlen(skb), PCI_DMA_TODEVICE);
538 prod = NEXT_TX(prod);
540 /* unmap remaining mapped pages */
541 for (i = 0; i < last_frag; i++) {
542 prod = NEXT_TX(prod);
543 tx_buf = &txr->tx_buf_ring[prod];
544 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
545 skb_frag_size(&skb_shinfo(skb)->frags[i]),
549 dev_kfree_skb_any(skb);
553 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
555 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
556 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
557 u16 cons = txr->tx_cons;
558 struct pci_dev *pdev = bp->pdev;
560 unsigned int tx_bytes = 0;
562 for (i = 0; i < nr_pkts; i++) {
563 struct bnxt_sw_tx_bd *tx_buf;
567 tx_buf = &txr->tx_buf_ring[cons];
568 cons = NEXT_TX(cons);
572 if (tx_buf->is_push) {
577 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
578 skb_headlen(skb), PCI_DMA_TODEVICE);
579 last = tx_buf->nr_frags;
581 for (j = 0; j < last; j++) {
582 cons = NEXT_TX(cons);
583 tx_buf = &txr->tx_buf_ring[cons];
586 dma_unmap_addr(tx_buf, mapping),
587 skb_frag_size(&skb_shinfo(skb)->frags[j]),
592 cons = NEXT_TX(cons);
594 tx_bytes += skb->len;
595 dev_kfree_skb_any(skb);
598 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
601 /* Need to make the tx_cons update visible to bnxt_start_xmit()
602 * before checking for netif_tx_queue_stopped(). Without the
603 * memory barrier, there is a small possibility that bnxt_start_xmit()
604 * will miss it and cause the queue to be stopped forever.
608 if (unlikely(netif_tx_queue_stopped(txq)) &&
609 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
610 __netif_tx_lock(txq, smp_processor_id());
611 if (netif_tx_queue_stopped(txq) &&
612 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
613 txr->dev_state != BNXT_DEV_STATE_CLOSING)
614 netif_tx_wake_queue(txq);
615 __netif_tx_unlock(txq);
619 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
622 struct device *dev = &bp->pdev->dev;
625 page = alloc_page(gfp);
629 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
630 DMA_ATTR_WEAK_ORDERING);
631 if (dma_mapping_error(dev, *mapping)) {
635 *mapping += bp->rx_dma_offset;
639 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
643 struct pci_dev *pdev = bp->pdev;
645 data = kmalloc(bp->rx_buf_size, gfp);
649 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
650 bp->rx_buf_use_size, bp->rx_dir,
651 DMA_ATTR_WEAK_ORDERING);
653 if (dma_mapping_error(&pdev->dev, *mapping)) {
660 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
663 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
664 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
667 if (BNXT_RX_PAGE_MODE(bp)) {
668 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
674 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
676 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
682 rx_buf->data_ptr = data + bp->rx_offset;
684 rx_buf->mapping = mapping;
686 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
690 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
692 u16 prod = rxr->rx_prod;
693 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
694 struct rx_bd *cons_bd, *prod_bd;
696 prod_rx_buf = &rxr->rx_buf_ring[prod];
697 cons_rx_buf = &rxr->rx_buf_ring[cons];
699 prod_rx_buf->data = data;
700 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
702 prod_rx_buf->mapping = cons_rx_buf->mapping;
704 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
705 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
707 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
710 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
712 u16 next, max = rxr->rx_agg_bmap_size;
714 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
716 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
720 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
721 struct bnxt_rx_ring_info *rxr,
725 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
726 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
727 struct pci_dev *pdev = bp->pdev;
730 u16 sw_prod = rxr->rx_sw_agg_prod;
731 unsigned int offset = 0;
733 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
736 page = alloc_page(gfp);
740 rxr->rx_page_offset = 0;
742 offset = rxr->rx_page_offset;
743 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
744 if (rxr->rx_page_offset == PAGE_SIZE)
749 page = alloc_page(gfp);
754 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
755 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
756 DMA_ATTR_WEAK_ORDERING);
757 if (dma_mapping_error(&pdev->dev, mapping)) {
762 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
763 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
765 __set_bit(sw_prod, rxr->rx_agg_bmap);
766 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
767 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
769 rx_agg_buf->page = page;
770 rx_agg_buf->offset = offset;
771 rx_agg_buf->mapping = mapping;
772 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
773 rxbd->rx_bd_opaque = sw_prod;
777 static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
780 struct bnxt *bp = bnapi->bp;
781 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
782 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
783 u16 prod = rxr->rx_agg_prod;
784 u16 sw_prod = rxr->rx_sw_agg_prod;
787 for (i = 0; i < agg_bufs; i++) {
789 struct rx_agg_cmp *agg;
790 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
791 struct rx_bd *prod_bd;
794 agg = (struct rx_agg_cmp *)
795 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
796 cons = agg->rx_agg_cmp_opaque;
797 __clear_bit(cons, rxr->rx_agg_bmap);
799 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
800 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
802 __set_bit(sw_prod, rxr->rx_agg_bmap);
803 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
804 cons_rx_buf = &rxr->rx_agg_ring[cons];
806 /* It is possible for sw_prod to be equal to cons, so
807 * set cons_rx_buf->page to NULL first.
809 page = cons_rx_buf->page;
810 cons_rx_buf->page = NULL;
811 prod_rx_buf->page = page;
812 prod_rx_buf->offset = cons_rx_buf->offset;
814 prod_rx_buf->mapping = cons_rx_buf->mapping;
816 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
818 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
819 prod_bd->rx_bd_opaque = sw_prod;
821 prod = NEXT_RX_AGG(prod);
822 sw_prod = NEXT_RX_AGG(sw_prod);
823 cp_cons = NEXT_CMP(cp_cons);
825 rxr->rx_agg_prod = prod;
826 rxr->rx_sw_agg_prod = sw_prod;
829 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
830 struct bnxt_rx_ring_info *rxr,
831 u16 cons, void *data, u8 *data_ptr,
833 unsigned int offset_and_len)
835 unsigned int payload = offset_and_len >> 16;
836 unsigned int len = offset_and_len & 0xffff;
837 struct skb_frag_struct *frag;
838 struct page *page = data;
839 u16 prod = rxr->rx_prod;
843 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
845 bnxt_reuse_rx_data(rxr, cons, data);
848 dma_addr -= bp->rx_dma_offset;
849 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
850 DMA_ATTR_WEAK_ORDERING);
852 if (unlikely(!payload))
853 payload = eth_get_headlen(data_ptr, len);
855 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
861 off = (void *)data_ptr - page_address(page);
862 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
863 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
864 payload + NET_IP_ALIGN);
866 frag = &skb_shinfo(skb)->frags[0];
867 skb_frag_size_sub(frag, payload);
868 frag->page_offset += payload;
869 skb->data_len -= payload;
870 skb->tail += payload;
875 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
876 struct bnxt_rx_ring_info *rxr, u16 cons,
877 void *data, u8 *data_ptr,
879 unsigned int offset_and_len)
881 u16 prod = rxr->rx_prod;
885 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
887 bnxt_reuse_rx_data(rxr, cons, data);
891 skb = build_skb(data, 0);
892 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
893 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
899 skb_reserve(skb, bp->rx_offset);
900 skb_put(skb, offset_and_len & 0xffff);
904 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
905 struct sk_buff *skb, u16 cp_cons,
908 struct pci_dev *pdev = bp->pdev;
909 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
910 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
911 u16 prod = rxr->rx_agg_prod;
914 for (i = 0; i < agg_bufs; i++) {
916 struct rx_agg_cmp *agg;
917 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
921 agg = (struct rx_agg_cmp *)
922 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
923 cons = agg->rx_agg_cmp_opaque;
924 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
925 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
927 cons_rx_buf = &rxr->rx_agg_ring[cons];
928 skb_fill_page_desc(skb, i, cons_rx_buf->page,
929 cons_rx_buf->offset, frag_len);
930 __clear_bit(cons, rxr->rx_agg_bmap);
932 /* It is possible for bnxt_alloc_rx_page() to allocate
933 * a sw_prod index that equals the cons index, so we
934 * need to clear the cons entry now.
936 mapping = cons_rx_buf->mapping;
937 page = cons_rx_buf->page;
938 cons_rx_buf->page = NULL;
940 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
941 struct skb_shared_info *shinfo;
942 unsigned int nr_frags;
944 shinfo = skb_shinfo(skb);
945 nr_frags = --shinfo->nr_frags;
946 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
950 cons_rx_buf->page = page;
952 /* Update prod since possibly some pages have been
955 rxr->rx_agg_prod = prod;
956 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
960 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
962 DMA_ATTR_WEAK_ORDERING);
964 skb->data_len += frag_len;
965 skb->len += frag_len;
966 skb->truesize += PAGE_SIZE;
968 prod = NEXT_RX_AGG(prod);
969 cp_cons = NEXT_CMP(cp_cons);
971 rxr->rx_agg_prod = prod;
975 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
976 u8 agg_bufs, u32 *raw_cons)
979 struct rx_agg_cmp *agg;
981 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
982 last = RING_CMP(*raw_cons);
983 agg = (struct rx_agg_cmp *)
984 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
985 return RX_AGG_CMP_VALID(agg, *raw_cons);
988 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
992 struct bnxt *bp = bnapi->bp;
993 struct pci_dev *pdev = bp->pdev;
996 skb = napi_alloc_skb(&bnapi->napi, len);
1000 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1003 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1004 len + NET_IP_ALIGN);
1006 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1013 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
1014 u32 *raw_cons, void *cmp)
1016 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1017 struct rx_cmp *rxcmp = cmp;
1018 u32 tmp_raw_cons = *raw_cons;
1019 u8 cmp_type, agg_bufs = 0;
1021 cmp_type = RX_CMP_TYPE(rxcmp);
1023 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1024 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1026 RX_CMP_AGG_BUFS_SHIFT;
1027 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1028 struct rx_tpa_end_cmp *tpa_end = cmp;
1030 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1031 RX_TPA_END_CMP_AGG_BUFS) >>
1032 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1036 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1039 *raw_cons = tmp_raw_cons;
1043 static void bnxt_queue_sp_work(struct bnxt *bp)
1046 queue_work(bnxt_pf_wq, &bp->sp_task);
1048 schedule_work(&bp->sp_task);
1051 static void bnxt_cancel_sp_work(struct bnxt *bp)
1054 flush_workqueue(bnxt_pf_wq);
1056 cancel_work_sync(&bp->sp_task);
1059 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1061 if (!rxr->bnapi->in_reset) {
1062 rxr->bnapi->in_reset = true;
1063 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1064 bnxt_queue_sp_work(bp);
1066 rxr->rx_next_cons = 0xffff;
1069 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1070 struct rx_tpa_start_cmp *tpa_start,
1071 struct rx_tpa_start_cmp_ext *tpa_start1)
1073 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1075 struct bnxt_tpa_info *tpa_info;
1076 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1077 struct rx_bd *prod_bd;
1080 cons = tpa_start->rx_tpa_start_cmp_opaque;
1081 prod = rxr->rx_prod;
1082 cons_rx_buf = &rxr->rx_buf_ring[cons];
1083 prod_rx_buf = &rxr->rx_buf_ring[prod];
1084 tpa_info = &rxr->rx_tpa[agg_id];
1086 if (unlikely(cons != rxr->rx_next_cons)) {
1087 bnxt_sched_reset(bp, rxr);
1090 /* Store cfa_code in tpa_info to use in tpa_end
1091 * completion processing.
1093 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1094 prod_rx_buf->data = tpa_info->data;
1095 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1097 mapping = tpa_info->mapping;
1098 prod_rx_buf->mapping = mapping;
1100 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1102 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1104 tpa_info->data = cons_rx_buf->data;
1105 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1106 cons_rx_buf->data = NULL;
1107 tpa_info->mapping = cons_rx_buf->mapping;
1110 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1111 RX_TPA_START_CMP_LEN_SHIFT;
1112 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1113 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1115 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1116 tpa_info->gso_type = SKB_GSO_TCPV4;
1117 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1119 tpa_info->gso_type = SKB_GSO_TCPV6;
1120 tpa_info->rss_hash =
1121 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1123 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1124 tpa_info->gso_type = 0;
1125 if (netif_msg_rx_err(bp))
1126 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1128 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1129 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1130 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1132 rxr->rx_prod = NEXT_RX(prod);
1133 cons = NEXT_RX(cons);
1134 rxr->rx_next_cons = NEXT_RX(cons);
1135 cons_rx_buf = &rxr->rx_buf_ring[cons];
1137 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1138 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1139 cons_rx_buf->data = NULL;
1142 static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1143 u16 cp_cons, u32 agg_bufs)
1146 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1149 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1150 int payload_off, int tcp_ts,
1151 struct sk_buff *skb)
1156 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1157 u32 hdr_info = tpa_info->hdr_info;
1158 bool loopback = false;
1160 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1161 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1162 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1164 /* If the packet is an internal loopback packet, the offsets will
1165 * have an extra 4 bytes.
1167 if (inner_mac_off == 4) {
1169 } else if (inner_mac_off > 4) {
1170 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1173 /* We only support inner iPv4/ipv6. If we don't see the
1174 * correct protocol ID, it must be a loopback packet where
1175 * the offsets are off by 4.
1177 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1181 /* internal loopback packet, subtract all offsets by 4 */
1187 nw_off = inner_ip_off - ETH_HLEN;
1188 skb_set_network_header(skb, nw_off);
1189 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1190 struct ipv6hdr *iph = ipv6_hdr(skb);
1192 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1193 len = skb->len - skb_transport_offset(skb);
1195 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1197 struct iphdr *iph = ip_hdr(skb);
1199 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1200 len = skb->len - skb_transport_offset(skb);
1202 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1205 if (inner_mac_off) { /* tunnel */
1206 struct udphdr *uh = NULL;
1207 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1210 if (proto == htons(ETH_P_IP)) {
1211 struct iphdr *iph = (struct iphdr *)skb->data;
1213 if (iph->protocol == IPPROTO_UDP)
1214 uh = (struct udphdr *)(iph + 1);
1216 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1218 if (iph->nexthdr == IPPROTO_UDP)
1219 uh = (struct udphdr *)(iph + 1);
1223 skb_shinfo(skb)->gso_type |=
1224 SKB_GSO_UDP_TUNNEL_CSUM;
1226 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1233 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1234 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1236 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1237 int payload_off, int tcp_ts,
1238 struct sk_buff *skb)
1242 int len, nw_off, tcp_opt_len = 0;
1247 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1250 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1252 skb_set_network_header(skb, nw_off);
1254 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1255 len = skb->len - skb_transport_offset(skb);
1257 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1258 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1259 struct ipv6hdr *iph;
1261 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1263 skb_set_network_header(skb, nw_off);
1264 iph = ipv6_hdr(skb);
1265 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1266 len = skb->len - skb_transport_offset(skb);
1268 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1270 dev_kfree_skb_any(skb);
1274 if (nw_off) { /* tunnel */
1275 struct udphdr *uh = NULL;
1277 if (skb->protocol == htons(ETH_P_IP)) {
1278 struct iphdr *iph = (struct iphdr *)skb->data;
1280 if (iph->protocol == IPPROTO_UDP)
1281 uh = (struct udphdr *)(iph + 1);
1283 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1285 if (iph->nexthdr == IPPROTO_UDP)
1286 uh = (struct udphdr *)(iph + 1);
1290 skb_shinfo(skb)->gso_type |=
1291 SKB_GSO_UDP_TUNNEL_CSUM;
1293 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1300 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1301 struct bnxt_tpa_info *tpa_info,
1302 struct rx_tpa_end_cmp *tpa_end,
1303 struct rx_tpa_end_cmp_ext *tpa_end1,
1304 struct sk_buff *skb)
1310 segs = TPA_END_TPA_SEGS(tpa_end);
1314 NAPI_GRO_CB(skb)->count = segs;
1315 skb_shinfo(skb)->gso_size =
1316 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1317 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1318 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1319 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1320 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1321 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1323 tcp_gro_complete(skb);
1328 /* Given the cfa_code of a received packet determine which
1329 * netdev (vf-rep or PF) the packet is destined to.
1331 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1333 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1335 /* if vf-rep dev is NULL, the must belongs to the PF */
1336 return dev ? dev : bp->dev;
1339 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1340 struct bnxt_napi *bnapi,
1342 struct rx_tpa_end_cmp *tpa_end,
1343 struct rx_tpa_end_cmp_ext *tpa_end1,
1346 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1347 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1348 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1349 u8 *data_ptr, agg_bufs;
1350 u16 cp_cons = RING_CMP(*raw_cons);
1352 struct bnxt_tpa_info *tpa_info;
1354 struct sk_buff *skb;
1357 if (unlikely(bnapi->in_reset)) {
1358 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1361 return ERR_PTR(-EBUSY);
1365 tpa_info = &rxr->rx_tpa[agg_id];
1366 data = tpa_info->data;
1367 data_ptr = tpa_info->data_ptr;
1369 len = tpa_info->len;
1370 mapping = tpa_info->mapping;
1372 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1373 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1376 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1377 return ERR_PTR(-EBUSY);
1379 *event |= BNXT_AGG_EVENT;
1380 cp_cons = NEXT_CMP(cp_cons);
1383 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1384 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1385 if (agg_bufs > MAX_SKB_FRAGS)
1386 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1387 agg_bufs, (int)MAX_SKB_FRAGS);
1391 if (len <= bp->rx_copy_thresh) {
1392 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1394 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1399 dma_addr_t new_mapping;
1401 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1403 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1407 tpa_info->data = new_data;
1408 tpa_info->data_ptr = new_data + bp->rx_offset;
1409 tpa_info->mapping = new_mapping;
1411 skb = build_skb(data, 0);
1412 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1413 bp->rx_buf_use_size, bp->rx_dir,
1414 DMA_ATTR_WEAK_ORDERING);
1418 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1421 skb_reserve(skb, bp->rx_offset);
1426 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1428 /* Page reuse already handled by bnxt_rx_pages(). */
1434 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1436 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1437 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1439 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1440 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1441 u16 vlan_proto = tpa_info->metadata >>
1442 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1443 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1445 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1448 skb_checksum_none_assert(skb);
1449 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1452 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1455 if (TPA_END_GRO(tpa_end))
1456 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1461 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1462 struct sk_buff *skb)
1464 if (skb->dev != bp->dev) {
1465 /* this packet belongs to a vf-rep */
1466 bnxt_vf_rep_rx(bp, skb);
1469 skb_record_rx_queue(skb, bnapi->index);
1470 napi_gro_receive(&bnapi->napi, skb);
1473 /* returns the following:
1474 * 1 - 1 packet successfully received
1475 * 0 - successful TPA_START, packet not completed yet
1476 * -EBUSY - completion ring does not have all the agg buffers yet
1477 * -ENOMEM - packet aborted due to out of memory
1478 * -EIO - packet aborted due to hw error indicated in BD
1480 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1483 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1484 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1485 struct net_device *dev = bp->dev;
1486 struct rx_cmp *rxcmp;
1487 struct rx_cmp_ext *rxcmp1;
1488 u32 tmp_raw_cons = *raw_cons;
1489 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1490 struct bnxt_sw_rx_bd *rx_buf;
1492 u8 *data_ptr, agg_bufs, cmp_type;
1493 dma_addr_t dma_addr;
1494 struct sk_buff *skb;
1499 rxcmp = (struct rx_cmp *)
1500 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1502 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1503 cp_cons = RING_CMP(tmp_raw_cons);
1504 rxcmp1 = (struct rx_cmp_ext *)
1505 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1507 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1510 cmp_type = RX_CMP_TYPE(rxcmp);
1512 prod = rxr->rx_prod;
1514 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1515 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1516 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1518 *event |= BNXT_RX_EVENT;
1519 goto next_rx_no_prod_no_len;
1521 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1522 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1523 (struct rx_tpa_end_cmp *)rxcmp,
1524 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1531 bnxt_deliver_skb(bp, bnapi, skb);
1534 *event |= BNXT_RX_EVENT;
1535 goto next_rx_no_prod_no_len;
1538 cons = rxcmp->rx_cmp_opaque;
1539 rx_buf = &rxr->rx_buf_ring[cons];
1540 data = rx_buf->data;
1541 data_ptr = rx_buf->data_ptr;
1542 if (unlikely(cons != rxr->rx_next_cons)) {
1543 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1545 bnxt_sched_reset(bp, rxr);
1550 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1551 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1554 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1557 cp_cons = NEXT_CMP(cp_cons);
1558 *event |= BNXT_AGG_EVENT;
1560 *event |= BNXT_RX_EVENT;
1562 rx_buf->data = NULL;
1563 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1564 bnxt_reuse_rx_data(rxr, cons, data);
1566 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1572 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1573 dma_addr = rx_buf->mapping;
1575 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1580 if (len <= bp->rx_copy_thresh) {
1581 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1582 bnxt_reuse_rx_data(rxr, cons, data);
1590 if (rx_buf->data_ptr == data_ptr)
1591 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1594 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1603 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1610 if (RX_CMP_HASH_VALID(rxcmp)) {
1611 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1612 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1614 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1615 if (hash_type != 1 && hash_type != 3)
1616 type = PKT_HASH_TYPE_L3;
1617 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1620 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1621 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1623 if ((rxcmp1->rx_cmp_flags2 &
1624 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1625 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1626 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1627 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1628 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1630 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1633 skb_checksum_none_assert(skb);
1634 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1635 if (dev->features & NETIF_F_RXCSUM) {
1636 skb->ip_summed = CHECKSUM_UNNECESSARY;
1637 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1640 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1641 if (dev->features & NETIF_F_RXCSUM)
1642 cpr->rx_l4_csum_errors++;
1646 bnxt_deliver_skb(bp, bnapi, skb);
1650 rxr->rx_prod = NEXT_RX(prod);
1651 rxr->rx_next_cons = NEXT_RX(cons);
1653 cpr->rx_packets += 1;
1654 cpr->rx_bytes += len;
1656 next_rx_no_prod_no_len:
1657 *raw_cons = tmp_raw_cons;
1662 /* In netpoll mode, if we are using a combined completion ring, we need to
1663 * discard the rx packets and recycle the buffers.
1665 static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
1666 u32 *raw_cons, u8 *event)
1668 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1669 u32 tmp_raw_cons = *raw_cons;
1670 struct rx_cmp_ext *rxcmp1;
1671 struct rx_cmp *rxcmp;
1675 cp_cons = RING_CMP(tmp_raw_cons);
1676 rxcmp = (struct rx_cmp *)
1677 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1679 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1680 cp_cons = RING_CMP(tmp_raw_cons);
1681 rxcmp1 = (struct rx_cmp_ext *)
1682 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1684 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1687 cmp_type = RX_CMP_TYPE(rxcmp);
1688 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1689 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1690 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1691 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1692 struct rx_tpa_end_cmp_ext *tpa_end1;
1694 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1695 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1696 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1698 return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
1701 #define BNXT_GET_EVENT_PORT(data) \
1703 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1705 static int bnxt_async_event_process(struct bnxt *bp,
1706 struct hwrm_async_event_cmpl *cmpl)
1708 u16 event_id = le16_to_cpu(cmpl->event_id);
1710 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1712 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1713 u32 data1 = le32_to_cpu(cmpl->event_data1);
1714 struct bnxt_link_info *link_info = &bp->link_info;
1717 goto async_event_process_exit;
1719 /* print unsupported speed warning in forced speed mode only */
1720 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1721 (data1 & 0x20000)) {
1722 u16 fw_speed = link_info->force_link_speed;
1723 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1725 if (speed != SPEED_UNKNOWN)
1726 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1729 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1732 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1733 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1735 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1736 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1738 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1739 u32 data1 = le32_to_cpu(cmpl->event_data1);
1740 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1745 if (bp->pf.port_id != port_id)
1748 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1751 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1753 goto async_event_process_exit;
1754 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1757 goto async_event_process_exit;
1759 bnxt_queue_sp_work(bp);
1760 async_event_process_exit:
1761 bnxt_ulp_async_events(bp, cmpl);
1765 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1767 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1768 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1769 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1770 (struct hwrm_fwd_req_cmpl *)txcmp;
1772 switch (cmpl_type) {
1773 case CMPL_BASE_TYPE_HWRM_DONE:
1774 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1775 if (seq_id == bp->hwrm_intr_seq_id)
1776 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1778 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1781 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1782 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1784 if ((vf_id < bp->pf.first_vf_id) ||
1785 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1786 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1791 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1792 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1793 bnxt_queue_sp_work(bp);
1796 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1797 bnxt_async_event_process(bp,
1798 (struct hwrm_async_event_cmpl *)txcmp);
1807 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1809 struct bnxt_napi *bnapi = dev_instance;
1810 struct bnxt *bp = bnapi->bp;
1811 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1812 u32 cons = RING_CMP(cpr->cp_raw_cons);
1815 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1816 napi_schedule(&bnapi->napi);
1820 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1822 u32 raw_cons = cpr->cp_raw_cons;
1823 u16 cons = RING_CMP(raw_cons);
1824 struct tx_cmp *txcmp;
1826 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1828 return TX_CMP_VALID(txcmp, raw_cons);
1831 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1833 struct bnxt_napi *bnapi = dev_instance;
1834 struct bnxt *bp = bnapi->bp;
1835 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1836 u32 cons = RING_CMP(cpr->cp_raw_cons);
1839 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1841 if (!bnxt_has_work(bp, cpr)) {
1842 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1843 /* return if erroneous interrupt */
1844 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1848 /* disable ring IRQ */
1849 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1851 /* Return here if interrupt is shared and is disabled. */
1852 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1855 napi_schedule(&bnapi->napi);
1859 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1861 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1862 u32 raw_cons = cpr->cp_raw_cons;
1867 struct tx_cmp *txcmp;
1872 cons = RING_CMP(raw_cons);
1873 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1875 if (!TX_CMP_VALID(txcmp, raw_cons))
1878 /* The valid test of the entry must be done first before
1879 * reading any further.
1882 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1884 /* return full budget so NAPI will complete. */
1885 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1887 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1889 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1891 rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
1893 if (likely(rc >= 0))
1895 /* Increment rx_pkts when rc is -ENOMEM to count towards
1896 * the NAPI budget. Otherwise, we may potentially loop
1897 * here forever if we consistently cannot allocate
1900 else if (rc == -ENOMEM && budget)
1902 else if (rc == -EBUSY) /* partial completion */
1904 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1905 CMPL_BASE_TYPE_HWRM_DONE) ||
1906 (TX_CMP_TYPE(txcmp) ==
1907 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1908 (TX_CMP_TYPE(txcmp) ==
1909 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1910 bnxt_hwrm_handler(bp, txcmp);
1912 raw_cons = NEXT_RAW_CMP(raw_cons);
1914 if (rx_pkts == budget)
1918 if (event & BNXT_TX_EVENT) {
1919 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1920 void __iomem *db = txr->tx_doorbell;
1921 u16 prod = txr->tx_prod;
1923 /* Sync BD data before updating doorbell */
1926 bnxt_db_write_relaxed(bp, db, DB_KEY_TX | prod);
1929 cpr->cp_raw_cons = raw_cons;
1930 /* ACK completion ring before freeing tx ring and producing new
1931 * buffers in rx/agg rings to prevent overflowing the completion
1934 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1937 bnapi->tx_int(bp, bnapi, tx_pkts);
1939 if (event & BNXT_RX_EVENT) {
1940 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1942 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1943 if (event & BNXT_AGG_EVENT)
1944 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1945 DB_KEY_RX | rxr->rx_agg_prod);
1950 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1952 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1953 struct bnxt *bp = bnapi->bp;
1954 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1955 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1956 struct tx_cmp *txcmp;
1957 struct rx_cmp_ext *rxcmp1;
1958 u32 cp_cons, tmp_raw_cons;
1959 u32 raw_cons = cpr->cp_raw_cons;
1966 cp_cons = RING_CMP(raw_cons);
1967 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1969 if (!TX_CMP_VALID(txcmp, raw_cons))
1972 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1973 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1974 cp_cons = RING_CMP(tmp_raw_cons);
1975 rxcmp1 = (struct rx_cmp_ext *)
1976 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1978 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1981 /* force an error to recycle the buffer */
1982 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1983 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1985 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1986 if (likely(rc == -EIO) && budget)
1988 else if (rc == -EBUSY) /* partial completion */
1990 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1991 CMPL_BASE_TYPE_HWRM_DONE)) {
1992 bnxt_hwrm_handler(bp, txcmp);
1995 "Invalid completion received on special ring\n");
1997 raw_cons = NEXT_RAW_CMP(raw_cons);
1999 if (rx_pkts == budget)
2003 cpr->cp_raw_cons = raw_cons;
2004 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
2005 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
2007 if (event & BNXT_AGG_EVENT)
2008 bnxt_db_write(bp, rxr->rx_agg_doorbell,
2009 DB_KEY_RX | rxr->rx_agg_prod);
2011 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2012 napi_complete_done(napi, rx_pkts);
2013 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
2018 static int bnxt_poll(struct napi_struct *napi, int budget)
2020 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2021 struct bnxt *bp = bnapi->bp;
2022 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2026 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
2028 if (work_done >= budget)
2031 if (!bnxt_has_work(bp, cpr)) {
2032 if (napi_complete_done(napi, work_done))
2033 BNXT_CP_DB_REARM(cpr->cp_doorbell,
2038 if (bp->flags & BNXT_FLAG_DIM) {
2039 struct net_dim_sample dim_sample;
2041 net_dim_sample(cpr->event_ctr,
2045 net_dim(&cpr->dim, dim_sample);
2051 static void bnxt_free_tx_skbs(struct bnxt *bp)
2054 struct pci_dev *pdev = bp->pdev;
2059 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2060 for (i = 0; i < bp->tx_nr_rings; i++) {
2061 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2064 for (j = 0; j < max_idx;) {
2065 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2066 struct sk_buff *skb = tx_buf->skb;
2076 if (tx_buf->is_push) {
2082 dma_unmap_single(&pdev->dev,
2083 dma_unmap_addr(tx_buf, mapping),
2087 last = tx_buf->nr_frags;
2089 for (k = 0; k < last; k++, j++) {
2090 int ring_idx = j & bp->tx_ring_mask;
2091 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2093 tx_buf = &txr->tx_buf_ring[ring_idx];
2096 dma_unmap_addr(tx_buf, mapping),
2097 skb_frag_size(frag), PCI_DMA_TODEVICE);
2101 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2105 static void bnxt_free_rx_skbs(struct bnxt *bp)
2107 int i, max_idx, max_agg_idx;
2108 struct pci_dev *pdev = bp->pdev;
2113 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2114 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2115 for (i = 0; i < bp->rx_nr_rings; i++) {
2116 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2120 for (j = 0; j < MAX_TPA; j++) {
2121 struct bnxt_tpa_info *tpa_info =
2123 u8 *data = tpa_info->data;
2128 dma_unmap_single_attrs(&pdev->dev,
2130 bp->rx_buf_use_size,
2132 DMA_ATTR_WEAK_ORDERING);
2134 tpa_info->data = NULL;
2140 for (j = 0; j < max_idx; j++) {
2141 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2142 dma_addr_t mapping = rx_buf->mapping;
2143 void *data = rx_buf->data;
2148 rx_buf->data = NULL;
2150 if (BNXT_RX_PAGE_MODE(bp)) {
2151 mapping -= bp->rx_dma_offset;
2152 dma_unmap_page_attrs(&pdev->dev, mapping,
2153 PAGE_SIZE, bp->rx_dir,
2154 DMA_ATTR_WEAK_ORDERING);
2157 dma_unmap_single_attrs(&pdev->dev, mapping,
2158 bp->rx_buf_use_size,
2160 DMA_ATTR_WEAK_ORDERING);
2165 for (j = 0; j < max_agg_idx; j++) {
2166 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2167 &rxr->rx_agg_ring[j];
2168 struct page *page = rx_agg_buf->page;
2173 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2176 DMA_ATTR_WEAK_ORDERING);
2178 rx_agg_buf->page = NULL;
2179 __clear_bit(j, rxr->rx_agg_bmap);
2184 __free_page(rxr->rx_page);
2185 rxr->rx_page = NULL;
2190 static void bnxt_free_skbs(struct bnxt *bp)
2192 bnxt_free_tx_skbs(bp);
2193 bnxt_free_rx_skbs(bp);
2196 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2198 struct pci_dev *pdev = bp->pdev;
2201 for (i = 0; i < ring->nr_pages; i++) {
2202 if (!ring->pg_arr[i])
2205 dma_free_coherent(&pdev->dev, ring->page_size,
2206 ring->pg_arr[i], ring->dma_arr[i]);
2208 ring->pg_arr[i] = NULL;
2211 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2212 ring->pg_tbl, ring->pg_tbl_map);
2213 ring->pg_tbl = NULL;
2215 if (ring->vmem_size && *ring->vmem) {
2221 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2224 struct pci_dev *pdev = bp->pdev;
2226 if (ring->nr_pages > 1) {
2227 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2235 for (i = 0; i < ring->nr_pages; i++) {
2236 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2240 if (!ring->pg_arr[i])
2243 if (ring->nr_pages > 1)
2244 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2247 if (ring->vmem_size) {
2248 *ring->vmem = vzalloc(ring->vmem_size);
2255 static void bnxt_free_rx_rings(struct bnxt *bp)
2262 for (i = 0; i < bp->rx_nr_rings; i++) {
2263 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2264 struct bnxt_ring_struct *ring;
2267 bpf_prog_put(rxr->xdp_prog);
2269 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2270 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2275 kfree(rxr->rx_agg_bmap);
2276 rxr->rx_agg_bmap = NULL;
2278 ring = &rxr->rx_ring_struct;
2279 bnxt_free_ring(bp, ring);
2281 ring = &rxr->rx_agg_ring_struct;
2282 bnxt_free_ring(bp, ring);
2286 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2288 int i, rc, agg_rings = 0, tpa_rings = 0;
2293 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2296 if (bp->flags & BNXT_FLAG_TPA)
2299 for (i = 0; i < bp->rx_nr_rings; i++) {
2300 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2301 struct bnxt_ring_struct *ring;
2303 ring = &rxr->rx_ring_struct;
2305 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2309 rc = bnxt_alloc_ring(bp, ring);
2316 ring = &rxr->rx_agg_ring_struct;
2317 rc = bnxt_alloc_ring(bp, ring);
2322 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2323 mem_size = rxr->rx_agg_bmap_size / 8;
2324 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2325 if (!rxr->rx_agg_bmap)
2329 rxr->rx_tpa = kcalloc(MAX_TPA,
2330 sizeof(struct bnxt_tpa_info),
2340 static void bnxt_free_tx_rings(struct bnxt *bp)
2343 struct pci_dev *pdev = bp->pdev;
2348 for (i = 0; i < bp->tx_nr_rings; i++) {
2349 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2350 struct bnxt_ring_struct *ring;
2353 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2354 txr->tx_push, txr->tx_push_mapping);
2355 txr->tx_push = NULL;
2358 ring = &txr->tx_ring_struct;
2360 bnxt_free_ring(bp, ring);
2364 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2367 struct pci_dev *pdev = bp->pdev;
2369 bp->tx_push_size = 0;
2370 if (bp->tx_push_thresh) {
2373 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2374 bp->tx_push_thresh);
2376 if (push_size > 256) {
2378 bp->tx_push_thresh = 0;
2381 bp->tx_push_size = push_size;
2384 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2385 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2386 struct bnxt_ring_struct *ring;
2389 ring = &txr->tx_ring_struct;
2391 rc = bnxt_alloc_ring(bp, ring);
2395 ring->grp_idx = txr->bnapi->index;
2396 if (bp->tx_push_size) {
2399 /* One pre-allocated DMA buffer to backup
2402 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2404 &txr->tx_push_mapping,
2410 mapping = txr->tx_push_mapping +
2411 sizeof(struct tx_push_bd);
2412 txr->data_mapping = cpu_to_le64(mapping);
2414 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2416 qidx = bp->tc_to_qidx[j];
2417 ring->queue_id = bp->q_info[qidx].queue_id;
2418 if (i < bp->tx_nr_rings_xdp)
2420 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2426 static void bnxt_free_cp_rings(struct bnxt *bp)
2433 for (i = 0; i < bp->cp_nr_rings; i++) {
2434 struct bnxt_napi *bnapi = bp->bnapi[i];
2435 struct bnxt_cp_ring_info *cpr;
2436 struct bnxt_ring_struct *ring;
2441 cpr = &bnapi->cp_ring;
2442 ring = &cpr->cp_ring_struct;
2444 bnxt_free_ring(bp, ring);
2448 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2450 int i, rc, ulp_base_vec, ulp_msix;
2452 ulp_msix = bnxt_get_ulp_msix_num(bp);
2453 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
2454 for (i = 0; i < bp->cp_nr_rings; i++) {
2455 struct bnxt_napi *bnapi = bp->bnapi[i];
2456 struct bnxt_cp_ring_info *cpr;
2457 struct bnxt_ring_struct *ring;
2462 cpr = &bnapi->cp_ring;
2463 ring = &cpr->cp_ring_struct;
2465 rc = bnxt_alloc_ring(bp, ring);
2469 if (ulp_msix && i >= ulp_base_vec)
2470 ring->map_idx = i + ulp_msix;
2477 static void bnxt_init_ring_struct(struct bnxt *bp)
2481 for (i = 0; i < bp->cp_nr_rings; i++) {
2482 struct bnxt_napi *bnapi = bp->bnapi[i];
2483 struct bnxt_cp_ring_info *cpr;
2484 struct bnxt_rx_ring_info *rxr;
2485 struct bnxt_tx_ring_info *txr;
2486 struct bnxt_ring_struct *ring;
2491 cpr = &bnapi->cp_ring;
2492 ring = &cpr->cp_ring_struct;
2493 ring->nr_pages = bp->cp_nr_pages;
2494 ring->page_size = HW_CMPD_RING_SIZE;
2495 ring->pg_arr = (void **)cpr->cp_desc_ring;
2496 ring->dma_arr = cpr->cp_desc_mapping;
2497 ring->vmem_size = 0;
2499 rxr = bnapi->rx_ring;
2503 ring = &rxr->rx_ring_struct;
2504 ring->nr_pages = bp->rx_nr_pages;
2505 ring->page_size = HW_RXBD_RING_SIZE;
2506 ring->pg_arr = (void **)rxr->rx_desc_ring;
2507 ring->dma_arr = rxr->rx_desc_mapping;
2508 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2509 ring->vmem = (void **)&rxr->rx_buf_ring;
2511 ring = &rxr->rx_agg_ring_struct;
2512 ring->nr_pages = bp->rx_agg_nr_pages;
2513 ring->page_size = HW_RXBD_RING_SIZE;
2514 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2515 ring->dma_arr = rxr->rx_agg_desc_mapping;
2516 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2517 ring->vmem = (void **)&rxr->rx_agg_ring;
2520 txr = bnapi->tx_ring;
2524 ring = &txr->tx_ring_struct;
2525 ring->nr_pages = bp->tx_nr_pages;
2526 ring->page_size = HW_RXBD_RING_SIZE;
2527 ring->pg_arr = (void **)txr->tx_desc_ring;
2528 ring->dma_arr = txr->tx_desc_mapping;
2529 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2530 ring->vmem = (void **)&txr->tx_buf_ring;
2534 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2538 struct rx_bd **rx_buf_ring;
2540 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2541 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2545 rxbd = rx_buf_ring[i];
2549 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2550 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2551 rxbd->rx_bd_opaque = prod;
2556 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2558 struct net_device *dev = bp->dev;
2559 struct bnxt_rx_ring_info *rxr;
2560 struct bnxt_ring_struct *ring;
2564 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2565 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2567 if (NET_IP_ALIGN == 2)
2568 type |= RX_BD_FLAGS_SOP;
2570 rxr = &bp->rx_ring[ring_nr];
2571 ring = &rxr->rx_ring_struct;
2572 bnxt_init_rxbd_pages(ring, type);
2574 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2575 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2576 if (IS_ERR(rxr->xdp_prog)) {
2577 int rc = PTR_ERR(rxr->xdp_prog);
2579 rxr->xdp_prog = NULL;
2583 prod = rxr->rx_prod;
2584 for (i = 0; i < bp->rx_ring_size; i++) {
2585 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2586 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2587 ring_nr, i, bp->rx_ring_size);
2590 prod = NEXT_RX(prod);
2592 rxr->rx_prod = prod;
2593 ring->fw_ring_id = INVALID_HW_RING_ID;
2595 ring = &rxr->rx_agg_ring_struct;
2596 ring->fw_ring_id = INVALID_HW_RING_ID;
2598 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2601 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2602 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2604 bnxt_init_rxbd_pages(ring, type);
2606 prod = rxr->rx_agg_prod;
2607 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2608 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2609 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2610 ring_nr, i, bp->rx_ring_size);
2613 prod = NEXT_RX_AGG(prod);
2615 rxr->rx_agg_prod = prod;
2617 if (bp->flags & BNXT_FLAG_TPA) {
2622 for (i = 0; i < MAX_TPA; i++) {
2623 data = __bnxt_alloc_rx_data(bp, &mapping,
2628 rxr->rx_tpa[i].data = data;
2629 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
2630 rxr->rx_tpa[i].mapping = mapping;
2633 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2641 static void bnxt_init_cp_rings(struct bnxt *bp)
2645 for (i = 0; i < bp->cp_nr_rings; i++) {
2646 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2647 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2649 ring->fw_ring_id = INVALID_HW_RING_ID;
2650 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2651 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2655 static int bnxt_init_rx_rings(struct bnxt *bp)
2659 if (BNXT_RX_PAGE_MODE(bp)) {
2660 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2661 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
2663 bp->rx_offset = BNXT_RX_OFFSET;
2664 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2667 for (i = 0; i < bp->rx_nr_rings; i++) {
2668 rc = bnxt_init_one_rx_ring(bp, i);
2676 static int bnxt_init_tx_rings(struct bnxt *bp)
2680 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2683 for (i = 0; i < bp->tx_nr_rings; i++) {
2684 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2685 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2687 ring->fw_ring_id = INVALID_HW_RING_ID;
2693 static void bnxt_free_ring_grps(struct bnxt *bp)
2695 kfree(bp->grp_info);
2696 bp->grp_info = NULL;
2699 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2704 bp->grp_info = kcalloc(bp->cp_nr_rings,
2705 sizeof(struct bnxt_ring_grp_info),
2710 for (i = 0; i < bp->cp_nr_rings; i++) {
2712 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2713 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2714 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2715 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2716 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2721 static void bnxt_free_vnics(struct bnxt *bp)
2723 kfree(bp->vnic_info);
2724 bp->vnic_info = NULL;
2728 static int bnxt_alloc_vnics(struct bnxt *bp)
2732 #ifdef CONFIG_RFS_ACCEL
2733 if (bp->flags & BNXT_FLAG_RFS)
2734 num_vnics += bp->rx_nr_rings;
2737 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2740 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2745 bp->nr_vnics = num_vnics;
2749 static void bnxt_init_vnics(struct bnxt *bp)
2753 for (i = 0; i < bp->nr_vnics; i++) {
2754 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2756 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2757 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2758 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
2759 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2761 if (bp->vnic_info[i].rss_hash_key) {
2763 prandom_bytes(vnic->rss_hash_key,
2766 memcpy(vnic->rss_hash_key,
2767 bp->vnic_info[0].rss_hash_key,
2773 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2777 pages = ring_size / desc_per_pg;
2784 while (pages & (pages - 1))
2790 void bnxt_set_tpa_flags(struct bnxt *bp)
2792 bp->flags &= ~BNXT_FLAG_TPA;
2793 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2795 if (bp->dev->features & NETIF_F_LRO)
2796 bp->flags |= BNXT_FLAG_LRO;
2797 else if (bp->dev->features & NETIF_F_GRO_HW)
2798 bp->flags |= BNXT_FLAG_GRO;
2801 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2804 void bnxt_set_ring_params(struct bnxt *bp)
2806 u32 ring_size, rx_size, rx_space;
2807 u32 agg_factor = 0, agg_ring_size = 0;
2809 /* 8 for CRC and VLAN */
2810 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2812 rx_space = rx_size + NET_SKB_PAD +
2813 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2815 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2816 ring_size = bp->rx_ring_size;
2817 bp->rx_agg_ring_size = 0;
2818 bp->rx_agg_nr_pages = 0;
2820 if (bp->flags & BNXT_FLAG_TPA)
2821 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
2823 bp->flags &= ~BNXT_FLAG_JUMBO;
2824 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
2827 bp->flags |= BNXT_FLAG_JUMBO;
2828 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2829 if (jumbo_factor > agg_factor)
2830 agg_factor = jumbo_factor;
2832 agg_ring_size = ring_size * agg_factor;
2834 if (agg_ring_size) {
2835 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2837 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2838 u32 tmp = agg_ring_size;
2840 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2841 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2842 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2843 tmp, agg_ring_size);
2845 bp->rx_agg_ring_size = agg_ring_size;
2846 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2847 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2848 rx_space = rx_size + NET_SKB_PAD +
2849 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2852 bp->rx_buf_use_size = rx_size;
2853 bp->rx_buf_size = rx_space;
2855 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2856 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2858 ring_size = bp->tx_ring_size;
2859 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2860 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2862 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2863 bp->cp_ring_size = ring_size;
2865 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2866 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2867 bp->cp_nr_pages = MAX_CP_PAGES;
2868 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2869 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2870 ring_size, bp->cp_ring_size);
2872 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2873 bp->cp_ring_mask = bp->cp_bit - 1;
2876 /* Changing allocation mode of RX rings.
2877 * TODO: Update when extending xdp_rxq_info to support allocation modes.
2879 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
2882 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2885 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
2886 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2887 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2888 bp->rx_dir = DMA_BIDIRECTIONAL;
2889 bp->rx_skb_func = bnxt_rx_page_skb;
2890 /* Disable LRO or GRO_HW */
2891 netdev_update_features(bp->dev);
2893 bp->dev->max_mtu = bp->max_mtu;
2894 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2895 bp->rx_dir = DMA_FROM_DEVICE;
2896 bp->rx_skb_func = bnxt_rx_skb;
2901 static void bnxt_free_vnic_attributes(struct bnxt *bp)
2904 struct bnxt_vnic_info *vnic;
2905 struct pci_dev *pdev = bp->pdev;
2910 for (i = 0; i < bp->nr_vnics; i++) {
2911 vnic = &bp->vnic_info[i];
2913 kfree(vnic->fw_grp_ids);
2914 vnic->fw_grp_ids = NULL;
2916 kfree(vnic->uc_list);
2917 vnic->uc_list = NULL;
2919 if (vnic->mc_list) {
2920 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2921 vnic->mc_list, vnic->mc_list_mapping);
2922 vnic->mc_list = NULL;
2925 if (vnic->rss_table) {
2926 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2928 vnic->rss_table_dma_addr);
2929 vnic->rss_table = NULL;
2932 vnic->rss_hash_key = NULL;
2937 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2939 int i, rc = 0, size;
2940 struct bnxt_vnic_info *vnic;
2941 struct pci_dev *pdev = bp->pdev;
2944 for (i = 0; i < bp->nr_vnics; i++) {
2945 vnic = &bp->vnic_info[i];
2947 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2948 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2951 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2952 if (!vnic->uc_list) {
2959 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2960 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2962 dma_alloc_coherent(&pdev->dev,
2964 &vnic->mc_list_mapping,
2966 if (!vnic->mc_list) {
2972 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2973 max_rings = bp->rx_nr_rings;
2977 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2978 if (!vnic->fw_grp_ids) {
2983 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2984 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2987 /* Allocate rss table and hash key */
2988 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2989 &vnic->rss_table_dma_addr,
2991 if (!vnic->rss_table) {
2996 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2998 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2999 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3007 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3009 struct pci_dev *pdev = bp->pdev;
3011 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3012 bp->hwrm_cmd_resp_dma_addr);
3014 bp->hwrm_cmd_resp_addr = NULL;
3017 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3019 struct pci_dev *pdev = bp->pdev;
3021 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3022 &bp->hwrm_cmd_resp_dma_addr,
3024 if (!bp->hwrm_cmd_resp_addr)
3030 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3032 if (bp->hwrm_short_cmd_req_addr) {
3033 struct pci_dev *pdev = bp->pdev;
3035 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3036 bp->hwrm_short_cmd_req_addr,
3037 bp->hwrm_short_cmd_req_dma_addr);
3038 bp->hwrm_short_cmd_req_addr = NULL;
3042 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3044 struct pci_dev *pdev = bp->pdev;
3046 bp->hwrm_short_cmd_req_addr =
3047 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
3048 &bp->hwrm_short_cmd_req_dma_addr,
3050 if (!bp->hwrm_short_cmd_req_addr)
3056 static void bnxt_free_stats(struct bnxt *bp)
3059 struct pci_dev *pdev = bp->pdev;
3061 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3062 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3064 if (bp->hw_rx_port_stats) {
3065 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3066 bp->hw_rx_port_stats,
3067 bp->hw_rx_port_stats_map);
3068 bp->hw_rx_port_stats = NULL;
3071 if (bp->hw_rx_port_stats_ext) {
3072 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3073 bp->hw_rx_port_stats_ext,
3074 bp->hw_rx_port_stats_ext_map);
3075 bp->hw_rx_port_stats_ext = NULL;
3081 size = sizeof(struct ctx_hw_stats);
3083 for (i = 0; i < bp->cp_nr_rings; i++) {
3084 struct bnxt_napi *bnapi = bp->bnapi[i];
3085 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3087 if (cpr->hw_stats) {
3088 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3090 cpr->hw_stats = NULL;
3095 static int bnxt_alloc_stats(struct bnxt *bp)
3098 struct pci_dev *pdev = bp->pdev;
3100 size = sizeof(struct ctx_hw_stats);
3102 for (i = 0; i < bp->cp_nr_rings; i++) {
3103 struct bnxt_napi *bnapi = bp->bnapi[i];
3104 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3106 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3112 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3115 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
3116 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3117 sizeof(struct tx_port_stats) + 1024;
3119 bp->hw_rx_port_stats =
3120 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3121 &bp->hw_rx_port_stats_map,
3123 if (!bp->hw_rx_port_stats)
3126 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3128 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3129 sizeof(struct rx_port_stats) + 512;
3130 bp->flags |= BNXT_FLAG_PORT_STATS;
3132 /* Display extended statistics only if FW supports it */
3133 if (bp->hwrm_spec_code < 0x10804 ||
3134 bp->hwrm_spec_code == 0x10900)
3137 bp->hw_rx_port_stats_ext =
3138 dma_zalloc_coherent(&pdev->dev,
3139 sizeof(struct rx_port_stats_ext),
3140 &bp->hw_rx_port_stats_ext_map,
3142 if (!bp->hw_rx_port_stats_ext)
3145 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3150 static void bnxt_clear_ring_indices(struct bnxt *bp)
3157 for (i = 0; i < bp->cp_nr_rings; i++) {
3158 struct bnxt_napi *bnapi = bp->bnapi[i];
3159 struct bnxt_cp_ring_info *cpr;
3160 struct bnxt_rx_ring_info *rxr;
3161 struct bnxt_tx_ring_info *txr;
3166 cpr = &bnapi->cp_ring;
3167 cpr->cp_raw_cons = 0;
3169 txr = bnapi->tx_ring;
3175 rxr = bnapi->rx_ring;
3178 rxr->rx_agg_prod = 0;
3179 rxr->rx_sw_agg_prod = 0;
3180 rxr->rx_next_cons = 0;
3185 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3187 #ifdef CONFIG_RFS_ACCEL
3190 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3191 * safe to delete the hash table.
3193 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3194 struct hlist_head *head;
3195 struct hlist_node *tmp;
3196 struct bnxt_ntuple_filter *fltr;
3198 head = &bp->ntp_fltr_hash_tbl[i];
3199 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3200 hlist_del(&fltr->hash);
3205 kfree(bp->ntp_fltr_bmap);
3206 bp->ntp_fltr_bmap = NULL;
3208 bp->ntp_fltr_count = 0;
3212 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3214 #ifdef CONFIG_RFS_ACCEL
3217 if (!(bp->flags & BNXT_FLAG_RFS))
3220 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3221 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3223 bp->ntp_fltr_count = 0;
3224 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3228 if (!bp->ntp_fltr_bmap)
3237 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3239 bnxt_free_vnic_attributes(bp);
3240 bnxt_free_tx_rings(bp);
3241 bnxt_free_rx_rings(bp);
3242 bnxt_free_cp_rings(bp);
3243 bnxt_free_ntp_fltrs(bp, irq_re_init);
3245 bnxt_free_stats(bp);
3246 bnxt_free_ring_grps(bp);
3247 bnxt_free_vnics(bp);
3248 kfree(bp->tx_ring_map);
3249 bp->tx_ring_map = NULL;
3257 bnxt_clear_ring_indices(bp);
3261 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3263 int i, j, rc, size, arr_size;
3267 /* Allocate bnapi mem pointer array and mem block for
3270 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3272 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3273 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3279 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3280 bp->bnapi[i] = bnapi;
3281 bp->bnapi[i]->index = i;
3282 bp->bnapi[i]->bp = bp;
3285 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3286 sizeof(struct bnxt_rx_ring_info),
3291 for (i = 0; i < bp->rx_nr_rings; i++) {
3292 bp->rx_ring[i].bnapi = bp->bnapi[i];
3293 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3296 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3297 sizeof(struct bnxt_tx_ring_info),
3302 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3305 if (!bp->tx_ring_map)
3308 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3311 j = bp->rx_nr_rings;
3313 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3314 bp->tx_ring[i].bnapi = bp->bnapi[j];
3315 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
3316 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
3317 if (i >= bp->tx_nr_rings_xdp) {
3318 bp->tx_ring[i].txq_index = i -
3319 bp->tx_nr_rings_xdp;
3320 bp->bnapi[j]->tx_int = bnxt_tx_int;
3322 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
3323 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3327 rc = bnxt_alloc_stats(bp);
3331 rc = bnxt_alloc_ntp_fltrs(bp);
3335 rc = bnxt_alloc_vnics(bp);
3340 bnxt_init_ring_struct(bp);
3342 rc = bnxt_alloc_rx_rings(bp);
3346 rc = bnxt_alloc_tx_rings(bp);
3350 rc = bnxt_alloc_cp_rings(bp);
3354 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3355 BNXT_VNIC_UCAST_FLAG;
3356 rc = bnxt_alloc_vnic_attributes(bp);
3362 bnxt_free_mem(bp, true);
3366 static void bnxt_disable_int(struct bnxt *bp)
3373 for (i = 0; i < bp->cp_nr_rings; i++) {
3374 struct bnxt_napi *bnapi = bp->bnapi[i];
3375 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3376 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3378 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3379 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3383 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3385 struct bnxt_napi *bnapi = bp->bnapi[n];
3386 struct bnxt_cp_ring_info *cpr;
3388 cpr = &bnapi->cp_ring;
3389 return cpr->cp_ring_struct.map_idx;
3392 static void bnxt_disable_int_sync(struct bnxt *bp)
3396 atomic_inc(&bp->intr_sem);
3398 bnxt_disable_int(bp);
3399 for (i = 0; i < bp->cp_nr_rings; i++) {
3400 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3402 synchronize_irq(bp->irq_tbl[map_idx].vector);
3406 static void bnxt_enable_int(struct bnxt *bp)
3410 atomic_set(&bp->intr_sem, 0);
3411 for (i = 0; i < bp->cp_nr_rings; i++) {
3412 struct bnxt_napi *bnapi = bp->bnapi[i];
3413 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3415 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3419 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3420 u16 cmpl_ring, u16 target_id)
3422 struct input *req = request;
3424 req->req_type = cpu_to_le16(req_type);
3425 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3426 req->target_id = cpu_to_le16(target_id);
3427 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3430 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3431 int timeout, bool silent)
3433 int i, intr_process, rc, tmo_count;
3434 struct input *req = msg;
3438 u16 cp_ring_id, len = 0;
3439 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3440 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
3441 struct hwrm_short_input short_input = {0};
3443 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
3444 memset(resp, 0, PAGE_SIZE);
3445 cp_ring_id = le16_to_cpu(req->cmpl_ring);
3446 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3448 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3449 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3451 memcpy(short_cmd_req, req, msg_len);
3452 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3455 short_input.req_type = req->req_type;
3456 short_input.signature =
3457 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3458 short_input.size = cpu_to_le16(msg_len);
3459 short_input.req_addr =
3460 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3462 data = (u32 *)&short_input;
3463 msg_len = sizeof(short_input);
3465 /* Sync memory write before updating doorbell */
3468 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3471 /* Write request msg to hwrm channel */
3472 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3474 for (i = msg_len; i < max_req_len; i += 4)
3475 writel(0, bp->bar0 + i);
3477 /* currently supports only one outstanding message */
3479 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
3481 /* Ring channel doorbell */
3482 writel(1, bp->bar0 + 0x100);
3485 timeout = DFLT_HWRM_CMD_TIMEOUT;
3486 /* convert timeout to usec */
3490 /* Short timeout for the first few iterations:
3491 * number of loops = number of loops for short timeout +
3492 * number of loops for standard timeout.
3494 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3495 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3496 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
3497 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
3499 /* Wait until hwrm response cmpl interrupt is processed */
3500 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
3502 /* on first few passes, just barely sleep */
3503 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3504 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3505 HWRM_SHORT_MAX_TIMEOUT);
3507 usleep_range(HWRM_MIN_TIMEOUT,
3511 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3512 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
3513 le16_to_cpu(req->req_type));
3516 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3518 valid = bp->hwrm_cmd_resp_addr + len - 1;
3522 /* Check if response len is updated */
3523 for (i = 0; i < tmo_count; i++) {
3524 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3528 /* on first few passes, just barely sleep */
3529 if (i < DFLT_HWRM_CMD_TIMEOUT)
3530 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3531 HWRM_SHORT_MAX_TIMEOUT);
3533 usleep_range(HWRM_MIN_TIMEOUT,
3537 if (i >= tmo_count) {
3538 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
3539 HWRM_TOTAL_TIMEOUT(i),
3540 le16_to_cpu(req->req_type),
3541 le16_to_cpu(req->seq_id), len);
3545 /* Last byte of resp contains valid bit */
3546 valid = bp->hwrm_cmd_resp_addr + len - 1;
3547 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
3548 /* make sure we read from updated DMA memory */
3555 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
3556 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
3557 HWRM_TOTAL_TIMEOUT(i),
3558 le16_to_cpu(req->req_type),
3559 le16_to_cpu(req->seq_id), len, *valid);
3564 /* Zero valid bit for compatibility. Valid bit in an older spec
3565 * may become a new field in a newer spec. We must make sure that
3566 * a new field not implemented by old spec will read zero.
3569 rc = le16_to_cpu(resp->error_code);
3571 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3572 le16_to_cpu(resp->req_type),
3573 le16_to_cpu(resp->seq_id), rc);
3577 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3579 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
3582 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3585 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3588 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3592 mutex_lock(&bp->hwrm_cmd_lock);
3593 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3594 mutex_unlock(&bp->hwrm_cmd_lock);
3598 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3603 mutex_lock(&bp->hwrm_cmd_lock);
3604 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3605 mutex_unlock(&bp->hwrm_cmd_lock);
3609 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3612 struct hwrm_func_drv_rgtr_input req = {0};
3613 DECLARE_BITMAP(async_events_bmap, 256);
3614 u32 *events = (u32 *)async_events_bmap;
3617 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3620 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3622 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3623 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3624 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3626 if (bmap && bmap_size) {
3627 for (i = 0; i < bmap_size; i++) {
3628 if (test_bit(i, bmap))
3629 __set_bit(i, async_events_bmap);
3633 for (i = 0; i < 8; i++)
3634 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3636 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3639 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3641 struct hwrm_func_drv_rgtr_input req = {0};
3643 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3646 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3647 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3649 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
3650 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
3651 req.ver_maj_8b = DRV_VER_MAJ;
3652 req.ver_min_8b = DRV_VER_MIN;
3653 req.ver_upd_8b = DRV_VER_UPD;
3654 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
3655 req.ver_min = cpu_to_le16(DRV_VER_MIN);
3656 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
3662 memset(data, 0, sizeof(data));
3663 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
3664 u16 cmd = bnxt_vf_req_snif[i];
3665 unsigned int bit, idx;
3669 data[idx] |= 1 << bit;
3672 for (i = 0; i < 8; i++)
3673 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3676 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3679 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3682 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3684 struct hwrm_func_drv_unrgtr_input req = {0};
3686 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3687 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3690 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3693 struct hwrm_tunnel_dst_port_free_input req = {0};
3695 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3696 req.tunnel_type = tunnel_type;
3698 switch (tunnel_type) {
3699 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3700 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3702 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3703 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3709 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3711 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3716 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3720 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3721 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3723 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3725 req.tunnel_type = tunnel_type;
3726 req.tunnel_dst_port_val = port;
3728 mutex_lock(&bp->hwrm_cmd_lock);
3729 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3731 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3736 switch (tunnel_type) {
3737 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
3738 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3740 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
3741 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3748 mutex_unlock(&bp->hwrm_cmd_lock);
3752 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3754 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3755 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3757 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
3758 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3760 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3761 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3762 req.mask = cpu_to_le32(vnic->rx_mask);
3763 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3766 #ifdef CONFIG_RFS_ACCEL
3767 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3768 struct bnxt_ntuple_filter *fltr)
3770 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3772 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3773 req.ntuple_filter_id = fltr->filter_id;
3774 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3777 #define BNXT_NTP_FLTR_FLAGS \
3778 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3779 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3780 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3781 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3782 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3783 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3784 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3785 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3786 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3787 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3788 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3789 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3790 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
3791 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
3793 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
3794 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3796 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3797 struct bnxt_ntuple_filter *fltr)
3800 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3801 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3802 bp->hwrm_cmd_resp_addr;
3803 struct flow_keys *keys = &fltr->fkeys;
3804 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3806 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3807 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
3809 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3811 req.ethertype = htons(ETH_P_IP);
3812 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
3813 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
3814 req.ip_protocol = keys->basic.ip_proto;
3816 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3819 req.ethertype = htons(ETH_P_IPV6);
3821 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3822 *(struct in6_addr *)&req.src_ipaddr[0] =
3823 keys->addrs.v6addrs.src;
3824 *(struct in6_addr *)&req.dst_ipaddr[0] =
3825 keys->addrs.v6addrs.dst;
3826 for (i = 0; i < 4; i++) {
3827 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3828 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3831 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3832 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3833 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3834 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3836 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3837 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3839 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3842 req.src_port = keys->ports.src;
3843 req.src_port_mask = cpu_to_be16(0xffff);
3844 req.dst_port = keys->ports.dst;
3845 req.dst_port_mask = cpu_to_be16(0xffff);
3847 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
3848 mutex_lock(&bp->hwrm_cmd_lock);
3849 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3851 fltr->filter_id = resp->ntuple_filter_id;
3852 mutex_unlock(&bp->hwrm_cmd_lock);
3857 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3861 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3862 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3864 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
3865 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3866 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3868 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
3869 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
3871 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
3872 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
3873 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3874 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3875 req.l2_addr_mask[0] = 0xff;
3876 req.l2_addr_mask[1] = 0xff;
3877 req.l2_addr_mask[2] = 0xff;
3878 req.l2_addr_mask[3] = 0xff;
3879 req.l2_addr_mask[4] = 0xff;
3880 req.l2_addr_mask[5] = 0xff;
3882 mutex_lock(&bp->hwrm_cmd_lock);
3883 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3885 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3887 mutex_unlock(&bp->hwrm_cmd_lock);
3891 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3893 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3896 /* Any associated ntuple filters will also be cleared by firmware. */
3897 mutex_lock(&bp->hwrm_cmd_lock);
3898 for (i = 0; i < num_of_vnics; i++) {
3899 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3901 for (j = 0; j < vnic->uc_filter_count; j++) {
3902 struct hwrm_cfa_l2_filter_free_input req = {0};
3904 bnxt_hwrm_cmd_hdr_init(bp, &req,
3905 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3907 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3909 rc = _hwrm_send_message(bp, &req, sizeof(req),
3912 vnic->uc_filter_count = 0;
3914 mutex_unlock(&bp->hwrm_cmd_lock);
3919 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3921 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3922 struct hwrm_vnic_tpa_cfg_input req = {0};
3924 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
3927 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3930 u16 mss = bp->dev->mtu - 40;
3931 u32 nsegs, n, segs = 0, flags;
3933 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3934 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3935 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3936 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3937 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3938 if (tpa_flags & BNXT_FLAG_GRO)
3939 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3941 req.flags = cpu_to_le32(flags);
3944 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
3945 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3946 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
3948 /* Number of segs are log2 units, and first packet is not
3949 * included as part of this units.
3951 if (mss <= BNXT_RX_PAGE_SIZE) {
3952 n = BNXT_RX_PAGE_SIZE / mss;
3953 nsegs = (MAX_SKB_FRAGS - 1) * n;
3955 n = mss / BNXT_RX_PAGE_SIZE;
3956 if (mss & (BNXT_RX_PAGE_SIZE - 1))
3958 nsegs = (MAX_SKB_FRAGS - n) / n;
3961 segs = ilog2(nsegs);
3962 req.max_agg_segs = cpu_to_le16(segs);
3963 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
3965 req.min_agg_len = cpu_to_le32(512);
3967 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3969 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3972 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3974 u32 i, j, max_rings;
3975 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3976 struct hwrm_vnic_rss_cfg_input req = {0};
3978 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
3981 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3983 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
3984 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3985 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3986 max_rings = bp->rx_nr_rings - 1;
3988 max_rings = bp->rx_nr_rings;
3993 /* Fill the RSS indirection table with ring group ids */
3994 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3997 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4000 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4001 req.hash_key_tbl_addr =
4002 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4004 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4005 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4008 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4010 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4011 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4013 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4014 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4015 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4016 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4018 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4019 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4020 /* thresholds not implemented in firmware yet */
4021 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4022 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4023 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4024 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4027 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4030 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4032 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4033 req.rss_cos_lb_ctx_id =
4034 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
4036 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4037 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
4040 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4044 for (i = 0; i < bp->nr_vnics; i++) {
4045 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4047 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4048 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4049 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4052 bp->rsscos_nr_ctxs = 0;
4055 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
4058 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4059 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4060 bp->hwrm_cmd_resp_addr;
4062 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4065 mutex_lock(&bp->hwrm_cmd_lock);
4066 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4068 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
4069 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4070 mutex_unlock(&bp->hwrm_cmd_lock);
4075 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4077 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4078 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4079 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4082 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
4084 unsigned int ring = 0, grp_idx;
4085 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4086 struct hwrm_vnic_cfg_input req = {0};
4089 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
4091 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4092 /* Only RSS support for now TBD: COS & LB */
4093 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4094 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4095 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4096 VNIC_CFG_REQ_ENABLES_MRU);
4097 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4099 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4100 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4101 VNIC_CFG_REQ_ENABLES_MRU);
4102 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
4104 req.rss_rule = cpu_to_le16(0xffff);
4107 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4108 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
4109 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4110 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4112 req.cos_rule = cpu_to_le16(0xffff);
4115 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4117 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
4119 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4120 ring = bp->rx_nr_rings - 1;
4122 grp_idx = bp->rx_ring[ring].bnapi->index;
4123 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4124 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4126 req.lb_rule = cpu_to_le16(0xffff);
4127 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4130 #ifdef CONFIG_BNXT_SRIOV
4132 def_vlan = bp->vf.vlan;
4134 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
4135 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
4136 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4137 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
4139 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4142 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4146 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4147 struct hwrm_vnic_free_input req = {0};
4149 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4151 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4153 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4156 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4161 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4165 for (i = 0; i < bp->nr_vnics; i++)
4166 bnxt_hwrm_vnic_free_one(bp, i);
4169 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4170 unsigned int start_rx_ring_idx,
4171 unsigned int nr_rings)
4174 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
4175 struct hwrm_vnic_alloc_input req = {0};
4176 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4178 /* map ring groups to this vnic */
4179 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4180 grp_idx = bp->rx_ring[i].bnapi->index;
4181 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
4182 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
4186 bp->vnic_info[vnic_id].fw_grp_ids[j] =
4187 bp->grp_info[grp_idx].fw_grp_id;
4190 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
4191 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
4193 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4195 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4197 mutex_lock(&bp->hwrm_cmd_lock);
4198 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4200 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
4201 mutex_unlock(&bp->hwrm_cmd_lock);
4205 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4207 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4208 struct hwrm_vnic_qcaps_input req = {0};
4211 if (bp->hwrm_spec_code < 0x10600)
4214 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4215 mutex_lock(&bp->hwrm_cmd_lock);
4216 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4218 u32 flags = le32_to_cpu(resp->flags);
4220 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)
4221 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4223 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4224 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
4226 mutex_unlock(&bp->hwrm_cmd_lock);
4230 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4235 mutex_lock(&bp->hwrm_cmd_lock);
4236 for (i = 0; i < bp->rx_nr_rings; i++) {
4237 struct hwrm_ring_grp_alloc_input req = {0};
4238 struct hwrm_ring_grp_alloc_output *resp =
4239 bp->hwrm_cmd_resp_addr;
4240 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
4242 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4244 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4245 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4246 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4247 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
4249 rc = _hwrm_send_message(bp, &req, sizeof(req),
4254 bp->grp_info[grp_idx].fw_grp_id =
4255 le32_to_cpu(resp->ring_group_id);
4257 mutex_unlock(&bp->hwrm_cmd_lock);
4261 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4265 struct hwrm_ring_grp_free_input req = {0};
4270 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4272 mutex_lock(&bp->hwrm_cmd_lock);
4273 for (i = 0; i < bp->cp_nr_rings; i++) {
4274 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4277 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4279 rc = _hwrm_send_message(bp, &req, sizeof(req),
4283 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4285 mutex_unlock(&bp->hwrm_cmd_lock);
4289 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4290 struct bnxt_ring_struct *ring,
4291 u32 ring_type, u32 map_index)
4293 int rc = 0, err = 0;
4294 struct hwrm_ring_alloc_input req = {0};
4295 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4296 struct bnxt_ring_grp_info *grp_info;
4299 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4302 if (ring->nr_pages > 1) {
4303 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4304 /* Page size is in log2 units */
4305 req.page_size = BNXT_PAGE_SHIFT;
4306 req.page_tbl_depth = 1;
4308 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4311 /* Association of ring index with doorbell index and MSIX number */
4312 req.logical_id = cpu_to_le16(map_index);
4314 switch (ring_type) {
4315 case HWRM_RING_ALLOC_TX:
4316 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4317 /* Association of transmit ring with completion ring */
4318 grp_info = &bp->grp_info[ring->grp_idx];
4319 req.cmpl_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
4320 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4321 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4322 req.queue_id = cpu_to_le16(ring->queue_id);
4324 case HWRM_RING_ALLOC_RX:
4325 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4326 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4328 case HWRM_RING_ALLOC_AGG:
4329 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4330 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4332 case HWRM_RING_ALLOC_CMPL:
4333 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
4334 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4335 if (bp->flags & BNXT_FLAG_USING_MSIX)
4336 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4339 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4344 mutex_lock(&bp->hwrm_cmd_lock);
4345 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4346 err = le16_to_cpu(resp->error_code);
4347 ring_id = le16_to_cpu(resp->ring_id);
4348 mutex_unlock(&bp->hwrm_cmd_lock);
4351 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4352 ring_type, rc, err);
4355 ring->fw_ring_id = ring_id;
4359 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4364 struct hwrm_func_cfg_input req = {0};
4366 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4367 req.fid = cpu_to_le16(0xffff);
4368 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4369 req.async_event_cr = cpu_to_le16(idx);
4370 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4372 struct hwrm_func_vf_cfg_input req = {0};
4374 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4376 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4377 req.async_event_cr = cpu_to_le16(idx);
4378 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4383 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4387 for (i = 0; i < bp->cp_nr_rings; i++) {
4388 struct bnxt_napi *bnapi = bp->bnapi[i];
4389 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4390 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4391 u32 map_idx = ring->map_idx;
4393 cpr->cp_doorbell = bp->bar1 + map_idx * 0x80;
4394 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL,
4398 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4399 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
4402 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4404 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4408 for (i = 0; i < bp->tx_nr_rings; i++) {
4409 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4410 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4413 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4417 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
4420 for (i = 0; i < bp->rx_nr_rings; i++) {
4421 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4422 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4423 u32 map_idx = rxr->bnapi->index;
4425 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4429 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
4430 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
4431 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
4434 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4435 for (i = 0; i < bp->rx_nr_rings; i++) {
4436 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4437 struct bnxt_ring_struct *ring =
4438 &rxr->rx_agg_ring_struct;
4439 u32 grp_idx = ring->grp_idx;
4440 u32 map_idx = grp_idx + bp->rx_nr_rings;
4442 rc = hwrm_ring_alloc_send_msg(bp, ring,
4443 HWRM_RING_ALLOC_AGG,
4448 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
4449 writel(DB_KEY_RX | rxr->rx_agg_prod,
4450 rxr->rx_agg_doorbell);
4451 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
4458 static int hwrm_ring_free_send_msg(struct bnxt *bp,
4459 struct bnxt_ring_struct *ring,
4460 u32 ring_type, int cmpl_ring_id)
4463 struct hwrm_ring_free_input req = {0};
4464 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4467 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
4468 req.ring_type = ring_type;
4469 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4471 mutex_lock(&bp->hwrm_cmd_lock);
4472 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4473 error_code = le16_to_cpu(resp->error_code);
4474 mutex_unlock(&bp->hwrm_cmd_lock);
4476 if (rc || error_code) {
4477 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
4478 ring_type, rc, error_code);
4484 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
4491 for (i = 0; i < bp->tx_nr_rings; i++) {
4492 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4493 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4494 u32 grp_idx = txr->bnapi->index;
4495 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4497 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4498 hwrm_ring_free_send_msg(bp, ring,
4499 RING_FREE_REQ_RING_TYPE_TX,
4500 close_path ? cmpl_ring_id :
4501 INVALID_HW_RING_ID);
4502 ring->fw_ring_id = INVALID_HW_RING_ID;
4506 for (i = 0; i < bp->rx_nr_rings; i++) {
4507 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4508 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4509 u32 grp_idx = rxr->bnapi->index;
4510 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4512 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4513 hwrm_ring_free_send_msg(bp, ring,
4514 RING_FREE_REQ_RING_TYPE_RX,
4515 close_path ? cmpl_ring_id :
4516 INVALID_HW_RING_ID);
4517 ring->fw_ring_id = INVALID_HW_RING_ID;
4518 bp->grp_info[grp_idx].rx_fw_ring_id =
4523 for (i = 0; i < bp->rx_nr_rings; i++) {
4524 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4525 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
4526 u32 grp_idx = rxr->bnapi->index;
4527 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4529 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4530 hwrm_ring_free_send_msg(bp, ring,
4531 RING_FREE_REQ_RING_TYPE_RX,
4532 close_path ? cmpl_ring_id :
4533 INVALID_HW_RING_ID);
4534 ring->fw_ring_id = INVALID_HW_RING_ID;
4535 bp->grp_info[grp_idx].agg_fw_ring_id =
4540 /* The completion rings are about to be freed. After that the
4541 * IRQ doorbell will not work anymore. So we need to disable
4544 bnxt_disable_int_sync(bp);
4546 for (i = 0; i < bp->cp_nr_rings; i++) {
4547 struct bnxt_napi *bnapi = bp->bnapi[i];
4548 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4549 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4551 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4552 hwrm_ring_free_send_msg(bp, ring,
4553 RING_FREE_REQ_RING_TYPE_L2_CMPL,
4554 INVALID_HW_RING_ID);
4555 ring->fw_ring_id = INVALID_HW_RING_ID;
4556 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4561 static int bnxt_hwrm_get_rings(struct bnxt *bp)
4563 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4564 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4565 struct hwrm_func_qcfg_input req = {0};
4568 if (bp->hwrm_spec_code < 0x10601)
4571 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4572 req.fid = cpu_to_le16(0xffff);
4573 mutex_lock(&bp->hwrm_cmd_lock);
4574 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4576 mutex_unlock(&bp->hwrm_cmd_lock);
4580 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4581 if (bp->flags & BNXT_FLAG_NEW_RM) {
4584 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
4585 hw_resc->resv_hw_ring_grps =
4586 le32_to_cpu(resp->alloc_hw_ring_grps);
4587 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
4588 cp = le16_to_cpu(resp->alloc_cmpl_rings);
4589 stats = le16_to_cpu(resp->alloc_stat_ctx);
4590 cp = min_t(u16, cp, stats);
4591 hw_resc->resv_cp_rings = cp;
4593 mutex_unlock(&bp->hwrm_cmd_lock);
4597 /* Caller must hold bp->hwrm_cmd_lock */
4598 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4600 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4601 struct hwrm_func_qcfg_input req = {0};
4604 if (bp->hwrm_spec_code < 0x10601)
4607 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4608 req.fid = cpu_to_le16(fid);
4609 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4611 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4617 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
4618 int tx_rings, int rx_rings, int ring_grps,
4619 int cp_rings, int vnics)
4623 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
4624 req->fid = cpu_to_le16(0xffff);
4625 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
4626 req->num_tx_rings = cpu_to_le16(tx_rings);
4627 if (bp->flags & BNXT_FLAG_NEW_RM) {
4628 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4629 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4630 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4631 enables |= ring_grps ?
4632 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4633 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4635 req->num_rx_rings = cpu_to_le16(rx_rings);
4636 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4637 req->num_cmpl_rings = cpu_to_le16(cp_rings);
4638 req->num_stat_ctxs = req->num_cmpl_rings;
4639 req->num_vnics = cpu_to_le16(vnics);
4641 req->enables = cpu_to_le32(enables);
4645 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
4646 struct hwrm_func_vf_cfg_input *req, int tx_rings,
4647 int rx_rings, int ring_grps, int cp_rings,
4652 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
4653 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
4654 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
4655 enables |= cp_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
4656 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
4657 enables |= ring_grps ? FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
4658 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
4660 req->num_tx_rings = cpu_to_le16(tx_rings);
4661 req->num_rx_rings = cpu_to_le16(rx_rings);
4662 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
4663 req->num_cmpl_rings = cpu_to_le16(cp_rings);
4664 req->num_stat_ctxs = req->num_cmpl_rings;
4665 req->num_vnics = cpu_to_le16(vnics);
4667 req->enables = cpu_to_le32(enables);
4671 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4672 int ring_grps, int cp_rings, int vnics)
4674 struct hwrm_func_cfg_input req = {0};
4677 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4682 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4686 if (bp->hwrm_spec_code < 0x10601)
4687 bp->hw_resc.resv_tx_rings = tx_rings;
4689 rc = bnxt_hwrm_get_rings(bp);
4694 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4695 int ring_grps, int cp_rings, int vnics)
4697 struct hwrm_func_vf_cfg_input req = {0};
4700 if (!(bp->flags & BNXT_FLAG_NEW_RM)) {
4701 bp->hw_resc.resv_tx_rings = tx_rings;
4705 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4707 req.enables |= cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
4708 FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS);
4709 req.num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
4710 req.num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
4711 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4715 rc = bnxt_hwrm_get_rings(bp);
4719 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
4723 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, vnic);
4725 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, vnic);
4728 static int bnxt_cp_rings_in_use(struct bnxt *bp)
4730 int cp = bp->cp_nr_rings;
4731 int ulp_msix, ulp_base;
4733 ulp_msix = bnxt_get_ulp_msix_num(bp);
4735 ulp_base = bnxt_get_ulp_msix_base(bp);
4737 if ((ulp_base + ulp_msix) > cp)
4738 cp = ulp_base + ulp_msix;
4743 static bool bnxt_need_reserve_rings(struct bnxt *bp)
4745 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4746 int cp = bnxt_cp_rings_in_use(bp);
4747 int rx = bp->rx_nr_rings;
4748 int vnic = 1, grp = rx;
4750 if (bp->hwrm_spec_code < 0x10601)
4753 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
4756 if (bp->flags & BNXT_FLAG_RFS)
4758 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4760 if ((bp->flags & BNXT_FLAG_NEW_RM) &&
4761 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
4762 hw_resc->resv_hw_ring_grps != grp || hw_resc->resv_vnics != vnic))
4767 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4770 static int __bnxt_reserve_rings(struct bnxt *bp)
4772 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
4773 int cp = bnxt_cp_rings_in_use(bp);
4774 int tx = bp->tx_nr_rings;
4775 int rx = bp->rx_nr_rings;
4776 int grp, rx_rings, rc;
4780 if (!bnxt_need_reserve_rings(bp))
4783 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4785 if (bp->flags & BNXT_FLAG_RFS)
4787 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4789 grp = bp->rx_nr_rings;
4791 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, vnic);
4795 tx = hw_resc->resv_tx_rings;
4796 if (bp->flags & BNXT_FLAG_NEW_RM) {
4797 rx = hw_resc->resv_rx_rings;
4798 cp = hw_resc->resv_cp_rings;
4799 grp = hw_resc->resv_hw_ring_grps;
4800 vnic = hw_resc->resv_vnics;
4804 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4808 if (netif_running(bp->dev))
4811 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
4812 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4813 bp->dev->hw_features &= ~NETIF_F_LRO;
4814 bp->dev->features &= ~NETIF_F_LRO;
4815 bnxt_set_ring_params(bp);
4818 rx_rings = min_t(int, rx_rings, grp);
4819 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
4820 if (bp->flags & BNXT_FLAG_AGG_RINGS)
4822 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
4823 bp->tx_nr_rings = tx;
4824 bp->rx_nr_rings = rx_rings;
4825 bp->cp_nr_rings = cp;
4827 if (!tx || !rx || !cp || !grp || !vnic)
4833 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4834 int ring_grps, int cp_rings, int vnics)
4836 struct hwrm_func_vf_cfg_input req = {0};
4840 if (!(bp->flags & BNXT_FLAG_NEW_RM))
4843 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4845 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
4846 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4847 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4848 FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4849 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4850 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
4852 req.flags = cpu_to_le32(flags);
4853 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4859 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4860 int ring_grps, int cp_rings, int vnics)
4862 struct hwrm_func_cfg_input req = {0};
4866 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
4868 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
4869 if (bp->flags & BNXT_FLAG_NEW_RM)
4870 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
4871 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
4872 FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
4873 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
4874 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
4876 req.flags = cpu_to_le32(flags);
4877 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4883 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
4884 int ring_grps, int cp_rings, int vnics)
4886 if (bp->hwrm_spec_code < 0x10801)
4890 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
4891 ring_grps, cp_rings, vnics);
4893 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
4897 static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
4898 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4900 u16 val, tmr, max, flags;
4902 max = hw_coal->bufs_per_record * 128;
4903 if (hw_coal->budget)
4904 max = hw_coal->bufs_per_record * hw_coal->budget;
4906 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
4907 req->num_cmpl_aggr_int = cpu_to_le16(val);
4909 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4910 val = min_t(u16, val, 63);
4911 req->num_cmpl_dma_aggr = cpu_to_le16(val);
4913 /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
4914 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 63);
4915 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
4917 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks);
4918 tmr = max_t(u16, tmr, 1);
4919 req->int_lat_tmr_max = cpu_to_le16(tmr);
4921 /* min timer set to 1/2 of interrupt timer */
4923 req->int_lat_tmr_min = cpu_to_le16(val);
4925 /* buf timer set to 1/4 of interrupt timer */
4926 val = max_t(u16, tmr / 4, 1);
4927 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
4929 tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks_irq);
4930 tmr = max_t(u16, tmr, 1);
4931 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
4933 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4934 if (hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
4935 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4936 req->flags = cpu_to_le16(flags);
4939 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
4941 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
4942 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4943 struct bnxt_coal coal;
4944 unsigned int grp_idx;
4946 /* Tick values in micro seconds.
4947 * 1 coal_buf x bufs_per_record = 1 completion record.
4949 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
4951 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
4952 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
4954 if (!bnapi->rx_ring)
4957 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4958 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4960 bnxt_hwrm_set_coal_params(&coal, &req_rx);
4962 grp_idx = bnapi->index;
4963 req_rx.ring_id = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4965 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
4969 int bnxt_hwrm_set_coal(struct bnxt *bp)
4972 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4975 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4976 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4977 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4978 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4980 bnxt_hwrm_set_coal_params(&bp->rx_coal, &req_rx);
4981 bnxt_hwrm_set_coal_params(&bp->tx_coal, &req_tx);
4983 mutex_lock(&bp->hwrm_cmd_lock);
4984 for (i = 0; i < bp->cp_nr_rings; i++) {
4985 struct bnxt_napi *bnapi = bp->bnapi[i];
4988 if (!bnapi->rx_ring)
4990 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4992 rc = _hwrm_send_message(bp, req, sizeof(*req),
4997 mutex_unlock(&bp->hwrm_cmd_lock);
5001 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5004 struct hwrm_stat_ctx_free_input req = {0};
5009 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5012 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5014 mutex_lock(&bp->hwrm_cmd_lock);
5015 for (i = 0; i < bp->cp_nr_rings; i++) {
5016 struct bnxt_napi *bnapi = bp->bnapi[i];
5017 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5019 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5020 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5022 rc = _hwrm_send_message(bp, &req, sizeof(req),
5027 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5030 mutex_unlock(&bp->hwrm_cmd_lock);
5034 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5037 struct hwrm_stat_ctx_alloc_input req = {0};
5038 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5040 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5043 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5045 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
5047 mutex_lock(&bp->hwrm_cmd_lock);
5048 for (i = 0; i < bp->cp_nr_rings; i++) {
5049 struct bnxt_napi *bnapi = bp->bnapi[i];
5050 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5052 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5054 rc = _hwrm_send_message(bp, &req, sizeof(req),
5059 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5061 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5063 mutex_unlock(&bp->hwrm_cmd_lock);
5067 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5069 struct hwrm_func_qcfg_input req = {0};
5070 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5074 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5075 req.fid = cpu_to_le16(0xffff);
5076 mutex_lock(&bp->hwrm_cmd_lock);
5077 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5079 goto func_qcfg_exit;
5081 #ifdef CONFIG_BNXT_SRIOV
5083 struct bnxt_vf_info *vf = &bp->vf;
5085 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5088 flags = le16_to_cpu(resp->flags);
5089 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5090 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
5091 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
5092 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
5093 bp->flags |= BNXT_FLAG_FW_DCBX_AGENT;
5095 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5096 bp->flags |= BNXT_FLAG_MULTI_HOST;
5098 switch (resp->port_partition_type) {
5099 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5100 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5101 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5102 bp->port_partition_type = resp->port_partition_type;
5105 if (bp->hwrm_spec_code < 0x10707 ||
5106 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5107 bp->br_mode = BRIDGE_MODE_VEB;
5108 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5109 bp->br_mode = BRIDGE_MODE_VEPA;
5111 bp->br_mode = BRIDGE_MODE_UNDEF;
5113 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5115 bp->max_mtu = BNXT_MAX_MTU;
5118 mutex_unlock(&bp->hwrm_cmd_lock);
5122 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
5124 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5125 struct hwrm_func_resource_qcaps_input req = {0};
5126 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5129 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
5130 req.fid = cpu_to_le16(0xffff);
5132 mutex_lock(&bp->hwrm_cmd_lock);
5133 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5136 goto hwrm_func_resc_qcaps_exit;
5139 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
5141 goto hwrm_func_resc_qcaps_exit;
5143 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
5144 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5145 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
5146 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5147 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
5148 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5149 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
5150 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5151 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
5152 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
5153 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
5154 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5155 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
5156 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5157 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
5158 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5161 struct bnxt_pf_info *pf = &bp->pf;
5163 pf->vf_resv_strategy =
5164 le16_to_cpu(resp->vf_reservation_strategy);
5165 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL)
5166 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
5168 hwrm_func_resc_qcaps_exit:
5169 mutex_unlock(&bp->hwrm_cmd_lock);
5173 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
5176 struct hwrm_func_qcaps_input req = {0};
5177 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5178 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5181 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
5182 req.fid = cpu_to_le16(0xffff);
5184 mutex_lock(&bp->hwrm_cmd_lock);
5185 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5187 goto hwrm_func_qcaps_exit;
5189 flags = le32_to_cpu(resp->flags);
5190 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
5191 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
5192 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
5193 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
5195 bp->tx_push_thresh = 0;
5196 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
5197 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
5199 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
5200 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
5201 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
5202 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
5203 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
5204 if (!hw_resc->max_hw_ring_grps)
5205 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
5206 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
5207 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
5208 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
5211 struct bnxt_pf_info *pf = &bp->pf;
5213 pf->fw_fid = le16_to_cpu(resp->fid);
5214 pf->port_id = le16_to_cpu(resp->port_id);
5215 bp->dev->dev_port = pf->port_id;
5216 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
5217 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
5218 pf->max_vfs = le16_to_cpu(resp->max_vfs);
5219 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
5220 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
5221 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
5222 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
5223 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
5224 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
5225 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
5226 bp->flags |= BNXT_FLAG_WOL_CAP;
5228 #ifdef CONFIG_BNXT_SRIOV
5229 struct bnxt_vf_info *vf = &bp->vf;
5231 vf->fw_fid = le16_to_cpu(resp->fid);
5232 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
5236 hwrm_func_qcaps_exit:
5237 mutex_unlock(&bp->hwrm_cmd_lock);
5241 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
5245 rc = __bnxt_hwrm_func_qcaps(bp);
5248 if (bp->hwrm_spec_code >= 0x10803) {
5249 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
5251 bp->flags |= BNXT_FLAG_NEW_RM;
5256 static int bnxt_hwrm_func_reset(struct bnxt *bp)
5258 struct hwrm_func_reset_input req = {0};
5260 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
5263 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
5266 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
5269 struct hwrm_queue_qportcfg_input req = {0};
5270 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
5273 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
5275 mutex_lock(&bp->hwrm_cmd_lock);
5276 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5280 if (!resp->max_configurable_queues) {
5284 bp->max_tc = resp->max_configurable_queues;
5285 bp->max_lltc = resp->max_configurable_lossless_queues;
5286 if (bp->max_tc > BNXT_MAX_QUEUE)
5287 bp->max_tc = BNXT_MAX_QUEUE;
5289 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
5292 if (bp->max_lltc > bp->max_tc)
5293 bp->max_lltc = bp->max_tc;
5295 qptr = &resp->queue_id0;
5296 for (i = 0; i < bp->max_tc; i++) {
5297 bp->q_info[i].queue_id = *qptr++;
5298 bp->q_info[i].queue_profile = *qptr++;
5299 bp->tc_to_qidx[i] = i;
5303 mutex_unlock(&bp->hwrm_cmd_lock);
5307 static int bnxt_hwrm_ver_get(struct bnxt *bp)
5310 struct hwrm_ver_get_input req = {0};
5311 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
5314 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
5315 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
5316 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
5317 req.hwrm_intf_min = HWRM_VERSION_MINOR;
5318 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
5319 mutex_lock(&bp->hwrm_cmd_lock);
5320 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5322 goto hwrm_ver_get_exit;
5324 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
5326 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
5327 resp->hwrm_intf_min_8b << 8 |
5328 resp->hwrm_intf_upd_8b;
5329 if (resp->hwrm_intf_maj_8b < 1) {
5330 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
5331 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
5332 resp->hwrm_intf_upd_8b);
5333 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
5335 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
5336 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
5337 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
5339 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
5340 if (!bp->hwrm_cmd_timeout)
5341 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
5343 if (resp->hwrm_intf_maj_8b >= 1)
5344 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
5346 bp->chip_num = le16_to_cpu(resp->chip_num);
5347 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
5349 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
5351 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
5352 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
5353 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
5354 bp->flags |= BNXT_FLAG_SHORT_CMD;
5357 mutex_unlock(&bp->hwrm_cmd_lock);
5361 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
5363 struct hwrm_fw_set_time_input req = {0};
5365 time64_t now = ktime_get_real_seconds();
5367 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
5368 bp->hwrm_spec_code < 0x10400)
5371 time64_to_tm(now, 0, &tm);
5372 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
5373 req.year = cpu_to_le16(1900 + tm.tm_year);
5374 req.month = 1 + tm.tm_mon;
5375 req.day = tm.tm_mday;
5376 req.hour = tm.tm_hour;
5377 req.minute = tm.tm_min;
5378 req.second = tm.tm_sec;
5379 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5382 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
5385 struct bnxt_pf_info *pf = &bp->pf;
5386 struct hwrm_port_qstats_input req = {0};
5388 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
5391 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
5392 req.port_id = cpu_to_le16(pf->port_id);
5393 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
5394 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
5395 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5399 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
5401 struct hwrm_port_qstats_ext_input req = {0};
5402 struct bnxt_pf_info *pf = &bp->pf;
5404 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
5407 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
5408 req.port_id = cpu_to_le16(pf->port_id);
5409 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
5410 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
5411 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5414 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
5416 if (bp->vxlan_port_cnt) {
5417 bnxt_hwrm_tunnel_dst_port_free(
5418 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5420 bp->vxlan_port_cnt = 0;
5421 if (bp->nge_port_cnt) {
5422 bnxt_hwrm_tunnel_dst_port_free(
5423 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5425 bp->nge_port_cnt = 0;
5428 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
5434 tpa_flags = bp->flags & BNXT_FLAG_TPA;
5435 for (i = 0; i < bp->nr_vnics; i++) {
5436 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
5438 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
5446 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
5450 for (i = 0; i < bp->nr_vnics; i++)
5451 bnxt_hwrm_vnic_set_rss(bp, i, false);
5454 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
5457 if (bp->vnic_info) {
5458 bnxt_hwrm_clear_vnic_filter(bp);
5459 /* clear all RSS setting before free vnic ctx */
5460 bnxt_hwrm_clear_vnic_rss(bp);
5461 bnxt_hwrm_vnic_ctx_free(bp);
5462 /* before free the vnic, undo the vnic tpa settings */
5463 if (bp->flags & BNXT_FLAG_TPA)
5464 bnxt_set_tpa(bp, false);
5465 bnxt_hwrm_vnic_free(bp);
5467 bnxt_hwrm_ring_free(bp, close_path);
5468 bnxt_hwrm_ring_grp_free(bp);
5470 bnxt_hwrm_stat_ctx_free(bp);
5471 bnxt_hwrm_free_tunnel_ports(bp);
5475 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
5477 struct hwrm_func_cfg_input req = {0};
5480 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5481 req.fid = cpu_to_le16(0xffff);
5482 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
5483 if (br_mode == BRIDGE_MODE_VEB)
5484 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
5485 else if (br_mode == BRIDGE_MODE_VEPA)
5486 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
5489 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5495 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
5497 struct hwrm_func_cfg_input req = {0};
5500 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
5503 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5504 req.fid = cpu_to_le16(0xffff);
5505 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
5506 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
5508 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
5510 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5516 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
5518 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5521 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
5524 /* allocate context for vnic */
5525 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
5527 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5529 goto vnic_setup_err;
5531 bp->rsscos_nr_ctxs++;
5533 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5534 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
5536 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
5538 goto vnic_setup_err;
5540 bp->rsscos_nr_ctxs++;
5544 /* configure default vnic, ring grp */
5545 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
5547 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
5549 goto vnic_setup_err;
5552 /* Enable RSS hashing on vnic */
5553 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
5555 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
5557 goto vnic_setup_err;
5560 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5561 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
5563 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
5572 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
5574 #ifdef CONFIG_RFS_ACCEL
5577 for (i = 0; i < bp->rx_nr_rings; i++) {
5578 struct bnxt_vnic_info *vnic;
5579 u16 vnic_id = i + 1;
5582 if (vnic_id >= bp->nr_vnics)
5585 vnic = &bp->vnic_info[vnic_id];
5586 vnic->flags |= BNXT_VNIC_RFS_FLAG;
5587 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
5588 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
5589 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
5591 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
5595 rc = bnxt_setup_vnic(bp, vnic_id);
5605 /* Allow PF and VF with default VLAN to be in promiscuous mode */
5606 static bool bnxt_promisc_ok(struct bnxt *bp)
5608 #ifdef CONFIG_BNXT_SRIOV
5609 if (BNXT_VF(bp) && !bp->vf.vlan)
5615 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
5617 unsigned int rc = 0;
5619 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
5621 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5626 rc = bnxt_hwrm_vnic_cfg(bp, 1);
5628 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
5635 static int bnxt_cfg_rx_mode(struct bnxt *);
5636 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
5638 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
5640 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5642 unsigned int rx_nr_rings = bp->rx_nr_rings;
5645 rc = bnxt_hwrm_stat_ctx_alloc(bp);
5647 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5653 rc = bnxt_hwrm_ring_alloc(bp);
5655 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5659 rc = bnxt_hwrm_ring_grp_alloc(bp);
5661 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5665 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5668 /* default vnic 0 */
5669 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
5671 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5675 rc = bnxt_setup_vnic(bp, 0);
5679 if (bp->flags & BNXT_FLAG_RFS) {
5680 rc = bnxt_alloc_rfs_vnics(bp);
5685 if (bp->flags & BNXT_FLAG_TPA) {
5686 rc = bnxt_set_tpa(bp, true);
5692 bnxt_update_vf_mac(bp);
5694 /* Filter for default vnic 0 */
5695 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5697 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5700 vnic->uc_filter_count = 1;
5703 if (bp->dev->flags & IFF_BROADCAST)
5704 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
5706 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
5707 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5709 if (bp->dev->flags & IFF_ALLMULTI) {
5710 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5711 vnic->mc_list_count = 0;
5715 bnxt_mc_list_updated(bp, &mask);
5716 vnic->rx_mask |= mask;
5719 rc = bnxt_cfg_rx_mode(bp);
5723 rc = bnxt_hwrm_set_coal(bp);
5725 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
5728 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5729 rc = bnxt_setup_nitroa0_vnic(bp);
5731 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5736 bnxt_hwrm_func_qcfg(bp);
5737 netdev_update_features(bp->dev);
5743 bnxt_hwrm_resource_free(bp, 0, true);
5748 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5750 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5754 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5756 bnxt_init_cp_rings(bp);
5757 bnxt_init_rx_rings(bp);
5758 bnxt_init_tx_rings(bp);
5759 bnxt_init_ring_grps(bp, irq_re_init);
5760 bnxt_init_vnics(bp);
5762 return bnxt_init_chip(bp, irq_re_init);
5765 static int bnxt_set_real_num_queues(struct bnxt *bp)
5768 struct net_device *dev = bp->dev;
5770 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5771 bp->tx_nr_rings_xdp);
5775 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5779 #ifdef CONFIG_RFS_ACCEL
5780 if (bp->flags & BNXT_FLAG_RFS)
5781 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
5787 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5790 int _rx = *rx, _tx = *tx;
5793 *rx = min_t(int, _rx, max);
5794 *tx = min_t(int, _tx, max);
5799 while (_rx + _tx > max) {
5800 if (_rx > _tx && _rx > 1)
5811 static void bnxt_setup_msix(struct bnxt *bp)
5813 const int len = sizeof(bp->irq_tbl[0].name);
5814 struct net_device *dev = bp->dev;
5817 tcs = netdev_get_num_tc(dev);
5821 for (i = 0; i < tcs; i++) {
5822 count = bp->tx_nr_rings_per_tc;
5824 netdev_set_tc_queue(dev, i, count, off);
5828 for (i = 0; i < bp->cp_nr_rings; i++) {
5829 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5832 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5834 else if (i < bp->rx_nr_rings)
5839 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
5841 bp->irq_tbl[map_idx].handler = bnxt_msix;
5845 static void bnxt_setup_inta(struct bnxt *bp)
5847 const int len = sizeof(bp->irq_tbl[0].name);
5849 if (netdev_get_num_tc(bp->dev))
5850 netdev_reset_tc(bp->dev);
5852 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5854 bp->irq_tbl[0].handler = bnxt_inta;
5857 static int bnxt_setup_int_mode(struct bnxt *bp)
5861 if (bp->flags & BNXT_FLAG_USING_MSIX)
5862 bnxt_setup_msix(bp);
5864 bnxt_setup_inta(bp);
5866 rc = bnxt_set_real_num_queues(bp);
5870 #ifdef CONFIG_RFS_ACCEL
5871 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5873 return bp->hw_resc.max_rsscos_ctxs;
5876 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5878 return bp->hw_resc.max_vnics;
5882 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5884 return bp->hw_resc.max_stat_ctxs;
5887 void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5889 bp->hw_resc.max_stat_ctxs = max;
5892 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5894 return bp->hw_resc.max_cp_rings;
5897 void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5899 bp->hw_resc.max_cp_rings = max;
5902 unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5904 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5906 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
5909 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5911 bp->hw_resc.max_irqs = max_irqs;
5914 int bnxt_get_avail_msix(struct bnxt *bp, int num)
5916 int max_cp = bnxt_get_max_func_cp_rings(bp);
5917 int max_irq = bnxt_get_max_func_irqs(bp);
5918 int total_req = bp->cp_nr_rings + num;
5919 int max_idx, avail_msix;
5921 max_idx = min_t(int, bp->total_irqs, max_cp);
5922 avail_msix = max_idx - bp->cp_nr_rings;
5923 if (!(bp->flags & BNXT_FLAG_NEW_RM) || avail_msix >= num)
5926 if (max_irq < total_req) {
5927 num = max_irq - bp->cp_nr_rings;
5934 static int bnxt_get_num_msix(struct bnxt *bp)
5936 if (!(bp->flags & BNXT_FLAG_NEW_RM))
5937 return bnxt_get_max_func_irqs(bp);
5939 return bnxt_cp_rings_in_use(bp);
5942 static int bnxt_init_msix(struct bnxt *bp)
5944 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
5945 struct msix_entry *msix_ent;
5947 total_vecs = bnxt_get_num_msix(bp);
5948 max = bnxt_get_max_func_irqs(bp);
5949 if (total_vecs > max)
5955 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5959 for (i = 0; i < total_vecs; i++) {
5960 msix_ent[i].entry = i;
5961 msix_ent[i].vector = 0;
5964 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5967 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
5968 ulp_msix = bnxt_get_ulp_msix_num(bp);
5969 if (total_vecs < 0 || total_vecs < ulp_msix) {
5971 goto msix_setup_exit;
5974 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5976 for (i = 0; i < total_vecs; i++)
5977 bp->irq_tbl[i].vector = msix_ent[i].vector;
5979 bp->total_irqs = total_vecs;
5980 /* Trim rings based upon num of vectors allocated */
5981 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
5982 total_vecs - ulp_msix, min == 1);
5984 goto msix_setup_exit;
5986 bp->cp_nr_rings = (min == 1) ?
5987 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5988 bp->tx_nr_rings + bp->rx_nr_rings;
5992 goto msix_setup_exit;
5994 bp->flags |= BNXT_FLAG_USING_MSIX;
5999 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
6002 pci_disable_msix(bp->pdev);
6007 static int bnxt_init_inta(struct bnxt *bp)
6009 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
6014 bp->rx_nr_rings = 1;
6015 bp->tx_nr_rings = 1;
6016 bp->cp_nr_rings = 1;
6017 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6018 bp->irq_tbl[0].vector = bp->pdev->irq;
6022 static int bnxt_init_int_mode(struct bnxt *bp)
6026 if (bp->flags & BNXT_FLAG_MSIX_CAP)
6027 rc = bnxt_init_msix(bp);
6029 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
6030 /* fallback to INTA */
6031 rc = bnxt_init_inta(bp);
6036 static void bnxt_clear_int_mode(struct bnxt *bp)
6038 if (bp->flags & BNXT_FLAG_USING_MSIX)
6039 pci_disable_msix(bp->pdev);
6043 bp->flags &= ~BNXT_FLAG_USING_MSIX;
6046 int bnxt_reserve_rings(struct bnxt *bp)
6048 int tcs = netdev_get_num_tc(bp->dev);
6051 if (!bnxt_need_reserve_rings(bp))
6054 rc = __bnxt_reserve_rings(bp);
6056 netdev_err(bp->dev, "ring reservation failure rc: %d\n", rc);
6059 if ((bp->flags & BNXT_FLAG_NEW_RM) &&
6060 (bnxt_get_num_msix(bp) != bp->total_irqs)) {
6061 bnxt_ulp_irq_stop(bp);
6062 bnxt_clear_int_mode(bp);
6063 rc = bnxt_init_int_mode(bp);
6064 bnxt_ulp_irq_restart(bp, rc);
6068 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
6069 netdev_err(bp->dev, "tx ring reservation failure\n");
6070 netdev_reset_tc(bp->dev);
6071 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
6074 bp->num_stat_ctxs = bp->cp_nr_rings;
6078 static void bnxt_free_irq(struct bnxt *bp)
6080 struct bnxt_irq *irq;
6083 #ifdef CONFIG_RFS_ACCEL
6084 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
6085 bp->dev->rx_cpu_rmap = NULL;
6087 if (!bp->irq_tbl || !bp->bnapi)
6090 for (i = 0; i < bp->cp_nr_rings; i++) {
6091 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6093 irq = &bp->irq_tbl[map_idx];
6094 if (irq->requested) {
6095 if (irq->have_cpumask) {
6096 irq_set_affinity_hint(irq->vector, NULL);
6097 free_cpumask_var(irq->cpu_mask);
6098 irq->have_cpumask = 0;
6100 free_irq(irq->vector, bp->bnapi[i]);
6107 static int bnxt_request_irq(struct bnxt *bp)
6110 unsigned long flags = 0;
6111 #ifdef CONFIG_RFS_ACCEL
6112 struct cpu_rmap *rmap;
6115 rc = bnxt_setup_int_mode(bp);
6117 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6121 #ifdef CONFIG_RFS_ACCEL
6122 rmap = bp->dev->rx_cpu_rmap;
6124 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
6125 flags = IRQF_SHARED;
6127 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
6128 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
6129 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
6131 #ifdef CONFIG_RFS_ACCEL
6132 if (rmap && bp->bnapi[i]->rx_ring) {
6133 rc = irq_cpu_rmap_add(rmap, irq->vector);
6135 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
6140 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
6147 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
6148 int numa_node = dev_to_node(&bp->pdev->dev);
6150 irq->have_cpumask = 1;
6151 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
6153 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
6155 netdev_warn(bp->dev,
6156 "Set affinity failed, IRQ = %d\n",
6165 static void bnxt_del_napi(struct bnxt *bp)
6172 for (i = 0; i < bp->cp_nr_rings; i++) {
6173 struct bnxt_napi *bnapi = bp->bnapi[i];
6175 napi_hash_del(&bnapi->napi);
6176 netif_napi_del(&bnapi->napi);
6178 /* We called napi_hash_del() before netif_napi_del(), we need
6179 * to respect an RCU grace period before freeing napi structures.
6184 static void bnxt_init_napi(struct bnxt *bp)
6187 unsigned int cp_nr_rings = bp->cp_nr_rings;
6188 struct bnxt_napi *bnapi;
6190 if (bp->flags & BNXT_FLAG_USING_MSIX) {
6191 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6193 for (i = 0; i < cp_nr_rings; i++) {
6194 bnapi = bp->bnapi[i];
6195 netif_napi_add(bp->dev, &bnapi->napi,
6198 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6199 bnapi = bp->bnapi[cp_nr_rings];
6200 netif_napi_add(bp->dev, &bnapi->napi,
6201 bnxt_poll_nitroa0, 64);
6204 bnapi = bp->bnapi[0];
6205 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
6209 static void bnxt_disable_napi(struct bnxt *bp)
6216 for (i = 0; i < bp->cp_nr_rings; i++) {
6217 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
6219 if (bp->bnapi[i]->rx_ring)
6220 cancel_work_sync(&cpr->dim.work);
6222 napi_disable(&bp->bnapi[i]->napi);
6226 static void bnxt_enable_napi(struct bnxt *bp)
6230 for (i = 0; i < bp->cp_nr_rings; i++) {
6231 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
6232 bp->bnapi[i]->in_reset = false;
6234 if (bp->bnapi[i]->rx_ring) {
6235 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
6236 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6238 napi_enable(&bp->bnapi[i]->napi);
6242 void bnxt_tx_disable(struct bnxt *bp)
6245 struct bnxt_tx_ring_info *txr;
6248 for (i = 0; i < bp->tx_nr_rings; i++) {
6249 txr = &bp->tx_ring[i];
6250 txr->dev_state = BNXT_DEV_STATE_CLOSING;
6253 /* Stop all TX queues */
6254 netif_tx_disable(bp->dev);
6255 netif_carrier_off(bp->dev);
6258 void bnxt_tx_enable(struct bnxt *bp)
6261 struct bnxt_tx_ring_info *txr;
6263 for (i = 0; i < bp->tx_nr_rings; i++) {
6264 txr = &bp->tx_ring[i];
6267 netif_tx_wake_all_queues(bp->dev);
6268 if (bp->link_info.link_up)
6269 netif_carrier_on(bp->dev);
6272 static void bnxt_report_link(struct bnxt *bp)
6274 if (bp->link_info.link_up) {
6276 const char *flow_ctrl;
6280 netif_carrier_on(bp->dev);
6281 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
6285 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
6286 flow_ctrl = "ON - receive & transmit";
6287 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
6288 flow_ctrl = "ON - transmit";
6289 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
6290 flow_ctrl = "ON - receive";
6293 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
6294 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
6295 speed, duplex, flow_ctrl);
6296 if (bp->flags & BNXT_FLAG_EEE_CAP)
6297 netdev_info(bp->dev, "EEE is %s\n",
6298 bp->eee.eee_active ? "active" :
6300 fec = bp->link_info.fec_cfg;
6301 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
6302 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
6303 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
6304 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
6305 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
6307 netif_carrier_off(bp->dev);
6308 netdev_err(bp->dev, "NIC Link is Down\n");
6312 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
6315 struct hwrm_port_phy_qcaps_input req = {0};
6316 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6317 struct bnxt_link_info *link_info = &bp->link_info;
6319 if (bp->hwrm_spec_code < 0x10201)
6322 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
6324 mutex_lock(&bp->hwrm_cmd_lock);
6325 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6327 goto hwrm_phy_qcaps_exit;
6329 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
6330 struct ethtool_eee *eee = &bp->eee;
6331 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
6333 bp->flags |= BNXT_FLAG_EEE_CAP;
6334 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6335 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
6336 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
6337 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
6338 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
6340 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
6342 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
6344 if (resp->supported_speeds_auto_mode)
6345 link_info->support_auto_speeds =
6346 le16_to_cpu(resp->supported_speeds_auto_mode);
6348 bp->port_count = resp->port_cnt;
6350 hwrm_phy_qcaps_exit:
6351 mutex_unlock(&bp->hwrm_cmd_lock);
6355 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
6358 struct bnxt_link_info *link_info = &bp->link_info;
6359 struct hwrm_port_phy_qcfg_input req = {0};
6360 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6361 u8 link_up = link_info->link_up;
6364 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
6366 mutex_lock(&bp->hwrm_cmd_lock);
6367 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6369 mutex_unlock(&bp->hwrm_cmd_lock);
6373 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
6374 link_info->phy_link_status = resp->link;
6375 link_info->duplex = resp->duplex_cfg;
6376 if (bp->hwrm_spec_code >= 0x10800)
6377 link_info->duplex = resp->duplex_state;
6378 link_info->pause = resp->pause;
6379 link_info->auto_mode = resp->auto_mode;
6380 link_info->auto_pause_setting = resp->auto_pause;
6381 link_info->lp_pause = resp->link_partner_adv_pause;
6382 link_info->force_pause_setting = resp->force_pause;
6383 link_info->duplex_setting = resp->duplex_cfg;
6384 if (link_info->phy_link_status == BNXT_LINK_LINK)
6385 link_info->link_speed = le16_to_cpu(resp->link_speed);
6387 link_info->link_speed = 0;
6388 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
6389 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
6390 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
6391 link_info->lp_auto_link_speeds =
6392 le16_to_cpu(resp->link_partner_adv_speeds);
6393 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
6394 link_info->phy_ver[0] = resp->phy_maj;
6395 link_info->phy_ver[1] = resp->phy_min;
6396 link_info->phy_ver[2] = resp->phy_bld;
6397 link_info->media_type = resp->media_type;
6398 link_info->phy_type = resp->phy_type;
6399 link_info->transceiver = resp->xcvr_pkg_type;
6400 link_info->phy_addr = resp->eee_config_phy_addr &
6401 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
6402 link_info->module_status = resp->module_status;
6404 if (bp->flags & BNXT_FLAG_EEE_CAP) {
6405 struct ethtool_eee *eee = &bp->eee;
6408 eee->eee_active = 0;
6409 if (resp->eee_config_phy_addr &
6410 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
6411 eee->eee_active = 1;
6412 fw_speeds = le16_to_cpu(
6413 resp->link_partner_adv_eee_link_speed_mask);
6414 eee->lp_advertised =
6415 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6418 /* Pull initial EEE config */
6419 if (!chng_link_state) {
6420 if (resp->eee_config_phy_addr &
6421 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
6422 eee->eee_enabled = 1;
6424 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
6426 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
6428 if (resp->eee_config_phy_addr &
6429 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
6432 eee->tx_lpi_enabled = 1;
6433 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
6434 eee->tx_lpi_timer = le32_to_cpu(tmr) &
6435 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
6440 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
6441 if (bp->hwrm_spec_code >= 0x10504)
6442 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
6444 /* TODO: need to add more logic to report VF link */
6445 if (chng_link_state) {
6446 if (link_info->phy_link_status == BNXT_LINK_LINK)
6447 link_info->link_up = 1;
6449 link_info->link_up = 0;
6450 if (link_up != link_info->link_up)
6451 bnxt_report_link(bp);
6453 /* alwasy link down if not require to update link state */
6454 link_info->link_up = 0;
6456 mutex_unlock(&bp->hwrm_cmd_lock);
6458 if (!BNXT_SINGLE_PF(bp))
6461 diff = link_info->support_auto_speeds ^ link_info->advertising;
6462 if ((link_info->support_auto_speeds | diff) !=
6463 link_info->support_auto_speeds) {
6464 /* An advertised speed is no longer supported, so we need to
6465 * update the advertisement settings. Caller holds RTNL
6466 * so we can modify link settings.
6468 link_info->advertising = link_info->support_auto_speeds;
6469 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
6470 bnxt_hwrm_set_link_setting(bp, true, false);
6475 static void bnxt_get_port_module_status(struct bnxt *bp)
6477 struct bnxt_link_info *link_info = &bp->link_info;
6478 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
6481 if (bnxt_update_link(bp, true))
6484 module_status = link_info->module_status;
6485 switch (module_status) {
6486 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
6487 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
6488 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
6489 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
6491 if (bp->hwrm_spec_code >= 0x10201) {
6492 netdev_warn(bp->dev, "Module part number %s\n",
6493 resp->phy_vendor_partnumber);
6495 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
6496 netdev_warn(bp->dev, "TX is disabled\n");
6497 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
6498 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
6503 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
6505 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
6506 if (bp->hwrm_spec_code >= 0x10201)
6508 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
6509 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6510 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
6511 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6512 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
6514 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6516 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
6517 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
6518 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
6519 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
6521 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
6522 if (bp->hwrm_spec_code >= 0x10201) {
6523 req->auto_pause = req->force_pause;
6524 req->enables |= cpu_to_le32(
6525 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
6530 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
6531 struct hwrm_port_phy_cfg_input *req)
6533 u8 autoneg = bp->link_info.autoneg;
6534 u16 fw_link_speed = bp->link_info.req_link_speed;
6535 u16 advertising = bp->link_info.advertising;
6537 if (autoneg & BNXT_AUTONEG_SPEED) {
6539 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
6541 req->enables |= cpu_to_le32(
6542 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
6543 req->auto_link_speed_mask = cpu_to_le16(advertising);
6545 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
6547 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
6549 req->force_link_speed = cpu_to_le16(fw_link_speed);
6550 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
6553 /* tell chimp that the setting takes effect immediately */
6554 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
6557 int bnxt_hwrm_set_pause(struct bnxt *bp)
6559 struct hwrm_port_phy_cfg_input req = {0};
6562 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6563 bnxt_hwrm_set_pause_common(bp, &req);
6565 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
6566 bp->link_info.force_link_chng)
6567 bnxt_hwrm_set_link_common(bp, &req);
6569 mutex_lock(&bp->hwrm_cmd_lock);
6570 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6571 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
6572 /* since changing of pause setting doesn't trigger any link
6573 * change event, the driver needs to update the current pause
6574 * result upon successfully return of the phy_cfg command
6576 bp->link_info.pause =
6577 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
6578 bp->link_info.auto_pause_setting = 0;
6579 if (!bp->link_info.force_link_chng)
6580 bnxt_report_link(bp);
6582 bp->link_info.force_link_chng = false;
6583 mutex_unlock(&bp->hwrm_cmd_lock);
6587 static void bnxt_hwrm_set_eee(struct bnxt *bp,
6588 struct hwrm_port_phy_cfg_input *req)
6590 struct ethtool_eee *eee = &bp->eee;
6592 if (eee->eee_enabled) {
6594 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
6596 if (eee->tx_lpi_enabled)
6597 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
6599 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
6601 req->flags |= cpu_to_le32(flags);
6602 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
6603 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
6604 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
6606 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
6610 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
6612 struct hwrm_port_phy_cfg_input req = {0};
6614 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6616 bnxt_hwrm_set_pause_common(bp, &req);
6618 bnxt_hwrm_set_link_common(bp, &req);
6621 bnxt_hwrm_set_eee(bp, &req);
6622 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6625 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
6627 struct hwrm_port_phy_cfg_input req = {0};
6629 if (!BNXT_SINGLE_PF(bp))
6632 if (pci_num_vf(bp->pdev))
6635 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
6636 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
6637 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6640 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
6642 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6643 struct hwrm_port_led_qcaps_input req = {0};
6644 struct bnxt_pf_info *pf = &bp->pf;
6647 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
6650 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
6651 req.port_id = cpu_to_le16(pf->port_id);
6652 mutex_lock(&bp->hwrm_cmd_lock);
6653 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6655 mutex_unlock(&bp->hwrm_cmd_lock);
6658 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
6661 bp->num_leds = resp->num_leds;
6662 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
6664 for (i = 0; i < bp->num_leds; i++) {
6665 struct bnxt_led_info *led = &bp->leds[i];
6666 __le16 caps = led->led_state_caps;
6668 if (!led->led_group_id ||
6669 !BNXT_LED_ALT_BLINK_CAP(caps)) {
6675 mutex_unlock(&bp->hwrm_cmd_lock);
6679 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
6681 struct hwrm_wol_filter_alloc_input req = {0};
6682 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6685 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
6686 req.port_id = cpu_to_le16(bp->pf.port_id);
6687 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
6688 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
6689 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
6690 mutex_lock(&bp->hwrm_cmd_lock);
6691 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6693 bp->wol_filter_id = resp->wol_filter_id;
6694 mutex_unlock(&bp->hwrm_cmd_lock);
6698 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
6700 struct hwrm_wol_filter_free_input req = {0};
6703 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
6704 req.port_id = cpu_to_le16(bp->pf.port_id);
6705 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
6706 req.wol_filter_id = bp->wol_filter_id;
6707 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6711 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
6713 struct hwrm_wol_filter_qcfg_input req = {0};
6714 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6715 u16 next_handle = 0;
6718 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
6719 req.port_id = cpu_to_le16(bp->pf.port_id);
6720 req.handle = cpu_to_le16(handle);
6721 mutex_lock(&bp->hwrm_cmd_lock);
6722 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6724 next_handle = le16_to_cpu(resp->next_handle);
6725 if (next_handle != 0) {
6726 if (resp->wol_type ==
6727 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
6729 bp->wol_filter_id = resp->wol_filter_id;
6733 mutex_unlock(&bp->hwrm_cmd_lock);
6737 static void bnxt_get_wol_settings(struct bnxt *bp)
6741 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6745 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6746 } while (handle && handle != 0xffff);
6749 static bool bnxt_eee_config_ok(struct bnxt *bp)
6751 struct ethtool_eee *eee = &bp->eee;
6752 struct bnxt_link_info *link_info = &bp->link_info;
6754 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6757 if (eee->eee_enabled) {
6759 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6761 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6762 eee->eee_enabled = 0;
6765 if (eee->advertised & ~advertising) {
6766 eee->advertised = advertising & eee->supported;
6773 static int bnxt_update_phy_setting(struct bnxt *bp)
6776 bool update_link = false;
6777 bool update_pause = false;
6778 bool update_eee = false;
6779 struct bnxt_link_info *link_info = &bp->link_info;
6781 rc = bnxt_update_link(bp, true);
6783 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6787 if (!BNXT_SINGLE_PF(bp))
6790 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6791 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6792 link_info->req_flow_ctrl)
6793 update_pause = true;
6794 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6795 link_info->force_pause_setting != link_info->req_flow_ctrl)
6796 update_pause = true;
6797 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6798 if (BNXT_AUTO_MODE(link_info->auto_mode))
6800 if (link_info->req_link_speed != link_info->force_link_speed)
6802 if (link_info->req_duplex != link_info->duplex_setting)
6805 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6807 if (link_info->advertising != link_info->auto_link_speeds)
6811 /* The last close may have shutdown the link, so need to call
6812 * PHY_CFG to bring it back up.
6814 if (!netif_carrier_ok(bp->dev))
6817 if (!bnxt_eee_config_ok(bp))
6821 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
6822 else if (update_pause)
6823 rc = bnxt_hwrm_set_pause(bp);
6825 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6833 /* Common routine to pre-map certain register block to different GRC window.
6834 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6835 * in PF and 3 windows in VF that can be customized to map in different
6838 static void bnxt_preset_reg_win(struct bnxt *bp)
6841 /* CAG registers map to GRC window #4 */
6842 writel(BNXT_CAG_REG_BASE,
6843 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6847 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
6849 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6853 bnxt_preset_reg_win(bp);
6854 netif_carrier_off(bp->dev);
6856 /* Reserve rings now if none were reserved at driver probe. */
6857 rc = bnxt_init_dflt_ring_mode(bp);
6859 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
6862 rc = bnxt_reserve_rings(bp);
6866 if ((bp->flags & BNXT_FLAG_RFS) &&
6867 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6868 /* disable RFS if falling back to INTA */
6869 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6870 bp->flags &= ~BNXT_FLAG_RFS;
6873 rc = bnxt_alloc_mem(bp, irq_re_init);
6875 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6876 goto open_err_free_mem;
6881 rc = bnxt_request_irq(bp);
6883 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6888 bnxt_enable_napi(bp);
6889 bnxt_debug_dev_init(bp);
6891 rc = bnxt_init_nic(bp, irq_re_init);
6893 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6898 mutex_lock(&bp->link_lock);
6899 rc = bnxt_update_phy_setting(bp);
6900 mutex_unlock(&bp->link_lock);
6902 netdev_warn(bp->dev, "failed to update phy settings\n");
6906 udp_tunnel_get_rx_info(bp->dev);
6908 set_bit(BNXT_STATE_OPEN, &bp->state);
6909 bnxt_enable_int(bp);
6910 /* Enable TX queues */
6912 mod_timer(&bp->timer, jiffies + bp->current_interval);
6913 /* Poll link status and check for SFP+ module status */
6914 bnxt_get_port_module_status(bp);
6916 /* VF-reps may need to be re-opened after the PF is re-opened */
6918 bnxt_vf_reps_open(bp);
6922 bnxt_debug_dev_exit(bp);
6923 bnxt_disable_napi(bp);
6931 bnxt_free_mem(bp, true);
6935 /* rtnl_lock held */
6936 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6940 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6942 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6948 /* rtnl_lock held, open the NIC half way by allocating all resources, but
6949 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6952 int bnxt_half_open_nic(struct bnxt *bp)
6956 rc = bnxt_alloc_mem(bp, false);
6958 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6961 rc = bnxt_init_nic(bp, false);
6963 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6970 bnxt_free_mem(bp, false);
6975 /* rtnl_lock held, this call can only be made after a previous successful
6976 * call to bnxt_half_open_nic().
6978 void bnxt_half_close_nic(struct bnxt *bp)
6980 bnxt_hwrm_resource_free(bp, false, false);
6982 bnxt_free_mem(bp, false);
6985 static int bnxt_open(struct net_device *dev)
6987 struct bnxt *bp = netdev_priv(dev);
6989 return __bnxt_open_nic(bp, true, true);
6992 static bool bnxt_drv_busy(struct bnxt *bp)
6994 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
6995 test_bit(BNXT_STATE_READ_STATS, &bp->state));
6998 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
7001 /* Close the VF-reps before closing PF */
7003 bnxt_vf_reps_close(bp);
7005 /* Change device state to avoid TX queue wake up's */
7006 bnxt_tx_disable(bp);
7008 clear_bit(BNXT_STATE_OPEN, &bp->state);
7009 smp_mb__after_atomic();
7010 while (bnxt_drv_busy(bp))
7013 /* Flush rings and and disable interrupts */
7014 bnxt_shutdown_nic(bp, irq_re_init);
7016 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
7018 bnxt_debug_dev_exit(bp);
7019 bnxt_disable_napi(bp);
7020 del_timer_sync(&bp->timer);
7027 bnxt_free_mem(bp, irq_re_init);
7030 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
7034 #ifdef CONFIG_BNXT_SRIOV
7035 if (bp->sriov_cfg) {
7036 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
7038 BNXT_SRIOV_CFG_WAIT_TMO);
7040 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
7043 __bnxt_close_nic(bp, irq_re_init, link_re_init);
7047 static int bnxt_close(struct net_device *dev)
7049 struct bnxt *bp = netdev_priv(dev);
7051 bnxt_close_nic(bp, true, true);
7052 bnxt_hwrm_shutdown_link(bp);
7056 /* rtnl_lock held */
7057 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7063 if (!netif_running(dev))
7070 if (!netif_running(dev))
7083 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7086 struct bnxt *bp = netdev_priv(dev);
7088 set_bit(BNXT_STATE_READ_STATS, &bp->state);
7089 /* Make sure bnxt_close_nic() sees that we are reading stats before
7090 * we check the BNXT_STATE_OPEN flag.
7092 smp_mb__after_atomic();
7093 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7094 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
7098 /* TODO check if we need to synchronize with bnxt_close path */
7099 for (i = 0; i < bp->cp_nr_rings; i++) {
7100 struct bnxt_napi *bnapi = bp->bnapi[i];
7101 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7102 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
7104 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
7105 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
7106 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
7108 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
7109 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
7110 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
7112 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
7113 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
7114 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
7116 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
7117 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
7118 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
7120 stats->rx_missed_errors +=
7121 le64_to_cpu(hw_stats->rx_discard_pkts);
7123 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
7125 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
7128 if (bp->flags & BNXT_FLAG_PORT_STATS) {
7129 struct rx_port_stats *rx = bp->hw_rx_port_stats;
7130 struct tx_port_stats *tx = bp->hw_tx_port_stats;
7132 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
7133 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
7134 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
7135 le64_to_cpu(rx->rx_ovrsz_frames) +
7136 le64_to_cpu(rx->rx_runt_frames);
7137 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
7138 le64_to_cpu(rx->rx_jbr_frames);
7139 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
7140 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
7141 stats->tx_errors = le64_to_cpu(tx->tx_err);
7143 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
7146 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
7148 struct net_device *dev = bp->dev;
7149 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7150 struct netdev_hw_addr *ha;
7153 bool update = false;
7156 netdev_for_each_mc_addr(ha, dev) {
7157 if (mc_count >= BNXT_MAX_MC_ADDRS) {
7158 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7159 vnic->mc_list_count = 0;
7163 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
7164 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
7171 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
7173 if (mc_count != vnic->mc_list_count) {
7174 vnic->mc_list_count = mc_count;
7180 static bool bnxt_uc_list_updated(struct bnxt *bp)
7182 struct net_device *dev = bp->dev;
7183 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7184 struct netdev_hw_addr *ha;
7187 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
7190 netdev_for_each_uc_addr(ha, dev) {
7191 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
7199 static void bnxt_set_rx_mode(struct net_device *dev)
7201 struct bnxt *bp = netdev_priv(dev);
7202 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7203 u32 mask = vnic->rx_mask;
7204 bool mc_update = false;
7207 if (!netif_running(dev))
7210 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
7211 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
7212 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
7213 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
7215 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
7216 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7218 uc_update = bnxt_uc_list_updated(bp);
7220 if (dev->flags & IFF_BROADCAST)
7221 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
7222 if (dev->flags & IFF_ALLMULTI) {
7223 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7224 vnic->mc_list_count = 0;
7226 mc_update = bnxt_mc_list_updated(bp, &mask);
7229 if (mask != vnic->rx_mask || uc_update || mc_update) {
7230 vnic->rx_mask = mask;
7232 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
7233 bnxt_queue_sp_work(bp);
7237 static int bnxt_cfg_rx_mode(struct bnxt *bp)
7239 struct net_device *dev = bp->dev;
7240 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7241 struct netdev_hw_addr *ha;
7245 netif_addr_lock_bh(dev);
7246 uc_update = bnxt_uc_list_updated(bp);
7247 netif_addr_unlock_bh(dev);
7252 mutex_lock(&bp->hwrm_cmd_lock);
7253 for (i = 1; i < vnic->uc_filter_count; i++) {
7254 struct hwrm_cfa_l2_filter_free_input req = {0};
7256 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
7259 req.l2_filter_id = vnic->fw_l2_filter_id[i];
7261 rc = _hwrm_send_message(bp, &req, sizeof(req),
7264 mutex_unlock(&bp->hwrm_cmd_lock);
7266 vnic->uc_filter_count = 1;
7268 netif_addr_lock_bh(dev);
7269 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
7270 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7272 netdev_for_each_uc_addr(ha, dev) {
7273 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
7275 vnic->uc_filter_count++;
7278 netif_addr_unlock_bh(dev);
7280 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
7281 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
7283 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
7285 vnic->uc_filter_count = i;
7291 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
7293 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
7299 static bool bnxt_can_reserve_rings(struct bnxt *bp)
7301 #ifdef CONFIG_BNXT_SRIOV
7302 if ((bp->flags & BNXT_FLAG_NEW_RM) && BNXT_VF(bp)) {
7303 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7305 /* No minimum rings were provisioned by the PF. Don't
7306 * reserve rings by default when device is down.
7308 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
7311 if (!netif_running(bp->dev))
7318 /* If the chip and firmware supports RFS */
7319 static bool bnxt_rfs_supported(struct bnxt *bp)
7321 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
7323 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7328 /* If runtime conditions support RFS */
7329 static bool bnxt_rfs_capable(struct bnxt *bp)
7331 #ifdef CONFIG_RFS_ACCEL
7332 int vnics, max_vnics, max_rss_ctxs;
7334 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
7337 vnics = 1 + bp->rx_nr_rings;
7338 max_vnics = bnxt_get_max_func_vnics(bp);
7339 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
7341 /* RSS contexts not a limiting factor */
7342 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7343 max_rss_ctxs = max_vnics;
7344 if (vnics > max_vnics || vnics > max_rss_ctxs) {
7345 if (bp->rx_nr_rings > 1)
7346 netdev_warn(bp->dev,
7347 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
7348 min(max_rss_ctxs - 1, max_vnics - 1));
7352 if (!(bp->flags & BNXT_FLAG_NEW_RM))
7355 if (vnics == bp->hw_resc.resv_vnics)
7358 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, vnics);
7359 if (vnics <= bp->hw_resc.resv_vnics)
7362 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
7363 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 1);
7370 static netdev_features_t bnxt_fix_features(struct net_device *dev,
7371 netdev_features_t features)
7373 struct bnxt *bp = netdev_priv(dev);
7375 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
7376 features &= ~NETIF_F_NTUPLE;
7378 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7379 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
7381 if (!(features & NETIF_F_GRO))
7382 features &= ~NETIF_F_GRO_HW;
7384 if (features & NETIF_F_GRO_HW)
7385 features &= ~NETIF_F_LRO;
7387 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
7388 * turned on or off together.
7390 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
7391 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
7392 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
7393 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7394 NETIF_F_HW_VLAN_STAG_RX);
7396 features |= NETIF_F_HW_VLAN_CTAG_RX |
7397 NETIF_F_HW_VLAN_STAG_RX;
7399 #ifdef CONFIG_BNXT_SRIOV
7402 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
7403 NETIF_F_HW_VLAN_STAG_RX);
7410 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
7412 struct bnxt *bp = netdev_priv(dev);
7413 u32 flags = bp->flags;
7416 bool re_init = false;
7417 bool update_tpa = false;
7419 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
7420 if (features & NETIF_F_GRO_HW)
7421 flags |= BNXT_FLAG_GRO;
7422 else if (features & NETIF_F_LRO)
7423 flags |= BNXT_FLAG_LRO;
7425 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
7426 flags &= ~BNXT_FLAG_TPA;
7428 if (features & NETIF_F_HW_VLAN_CTAG_RX)
7429 flags |= BNXT_FLAG_STRIP_VLAN;
7431 if (features & NETIF_F_NTUPLE)
7432 flags |= BNXT_FLAG_RFS;
7434 changes = flags ^ bp->flags;
7435 if (changes & BNXT_FLAG_TPA) {
7437 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
7438 (flags & BNXT_FLAG_TPA) == 0)
7442 if (changes & ~BNXT_FLAG_TPA)
7445 if (flags != bp->flags) {
7446 u32 old_flags = bp->flags;
7450 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7452 bnxt_set_ring_params(bp);
7457 bnxt_close_nic(bp, false, false);
7459 bnxt_set_ring_params(bp);
7461 return bnxt_open_nic(bp, false, false);
7464 rc = bnxt_set_tpa(bp,
7465 (flags & BNXT_FLAG_TPA) ?
7468 bp->flags = old_flags;
7474 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
7476 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
7477 int i = bnapi->index;
7482 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
7483 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
7487 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
7489 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
7490 int i = bnapi->index;
7495 netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
7496 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
7497 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
7498 rxr->rx_sw_agg_prod);
7501 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
7503 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7504 int i = bnapi->index;
7506 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
7507 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
7510 static void bnxt_dbg_dump_states(struct bnxt *bp)
7513 struct bnxt_napi *bnapi;
7515 for (i = 0; i < bp->cp_nr_rings; i++) {
7516 bnapi = bp->bnapi[i];
7517 if (netif_msg_drv(bp)) {
7518 bnxt_dump_tx_sw_state(bnapi);
7519 bnxt_dump_rx_sw_state(bnapi);
7520 bnxt_dump_cp_sw_state(bnapi);
7525 static void bnxt_reset_task(struct bnxt *bp, bool silent)
7528 bnxt_dbg_dump_states(bp);
7529 if (netif_running(bp->dev)) {
7534 bnxt_close_nic(bp, false, false);
7535 rc = bnxt_open_nic(bp, false, false);
7541 static void bnxt_tx_timeout(struct net_device *dev)
7543 struct bnxt *bp = netdev_priv(dev);
7545 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
7546 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
7547 bnxt_queue_sp_work(bp);
7550 #ifdef CONFIG_NET_POLL_CONTROLLER
7551 static void bnxt_poll_controller(struct net_device *dev)
7553 struct bnxt *bp = netdev_priv(dev);
7556 /* Only process tx rings/combined rings in netpoll mode. */
7557 for (i = 0; i < bp->tx_nr_rings; i++) {
7558 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7560 napi_schedule(&txr->bnapi->napi);
7565 static void bnxt_timer(struct timer_list *t)
7567 struct bnxt *bp = from_timer(bp, t, timer);
7568 struct net_device *dev = bp->dev;
7570 if (!netif_running(dev))
7573 if (atomic_read(&bp->intr_sem) != 0)
7574 goto bnxt_restart_timer;
7576 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
7577 bp->stats_coal_ticks) {
7578 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
7579 bnxt_queue_sp_work(bp);
7582 if (bnxt_tc_flower_enabled(bp)) {
7583 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
7584 bnxt_queue_sp_work(bp);
7587 mod_timer(&bp->timer, jiffies + bp->current_interval);
7590 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
7592 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
7593 * set. If the device is being closed, bnxt_close() may be holding
7594 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
7595 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
7597 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7601 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
7603 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7607 /* Only called from bnxt_sp_task() */
7608 static void bnxt_reset(struct bnxt *bp, bool silent)
7610 bnxt_rtnl_lock_sp(bp);
7611 if (test_bit(BNXT_STATE_OPEN, &bp->state))
7612 bnxt_reset_task(bp, silent);
7613 bnxt_rtnl_unlock_sp(bp);
7616 static void bnxt_cfg_ntp_filters(struct bnxt *);
7618 static void bnxt_sp_task(struct work_struct *work)
7620 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
7622 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7623 smp_mb__after_atomic();
7624 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
7625 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7629 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
7630 bnxt_cfg_rx_mode(bp);
7632 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
7633 bnxt_cfg_ntp_filters(bp);
7634 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
7635 bnxt_hwrm_exec_fwd_req(bp);
7636 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7637 bnxt_hwrm_tunnel_dst_port_alloc(
7639 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7641 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7642 bnxt_hwrm_tunnel_dst_port_free(
7643 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7645 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
7646 bnxt_hwrm_tunnel_dst_port_alloc(
7648 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7650 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
7651 bnxt_hwrm_tunnel_dst_port_free(
7652 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7654 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
7655 bnxt_hwrm_port_qstats(bp);
7656 bnxt_hwrm_port_qstats_ext(bp);
7659 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
7662 mutex_lock(&bp->link_lock);
7663 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
7665 bnxt_hwrm_phy_qcaps(bp);
7667 rc = bnxt_update_link(bp, true);
7668 mutex_unlock(&bp->link_lock);
7670 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
7673 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
7674 mutex_lock(&bp->link_lock);
7675 bnxt_get_port_module_status(bp);
7676 mutex_unlock(&bp->link_lock);
7679 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
7680 bnxt_tc_flow_stats_work(bp);
7682 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
7683 * must be the last functions to be called before exiting.
7685 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
7686 bnxt_reset(bp, false);
7688 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
7689 bnxt_reset(bp, true);
7691 smp_mb__before_atomic();
7692 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
7695 /* Under rtnl_lock */
7696 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
7699 int max_rx, max_tx, tx_sets = 1;
7700 int tx_rings_needed;
7707 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
7714 tx_rings_needed = tx * tx_sets + tx_xdp;
7715 if (max_tx < tx_rings_needed)
7719 if (bp->flags & BNXT_FLAG_RFS)
7722 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7724 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
7725 if (bp->flags & BNXT_FLAG_NEW_RM)
7726 cp += bnxt_get_ulp_msix_num(bp);
7727 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
7731 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
7734 pci_iounmap(pdev, bp->bar2);
7739 pci_iounmap(pdev, bp->bar1);
7744 pci_iounmap(pdev, bp->bar0);
7749 static void bnxt_cleanup_pci(struct bnxt *bp)
7751 bnxt_unmap_bars(bp, bp->pdev);
7752 pci_release_regions(bp->pdev);
7753 pci_disable_device(bp->pdev);
7756 static void bnxt_init_dflt_coal(struct bnxt *bp)
7758 struct bnxt_coal *coal;
7760 /* Tick values in micro seconds.
7761 * 1 coal_buf x bufs_per_record = 1 completion record.
7763 coal = &bp->rx_coal;
7764 coal->coal_ticks = 14;
7765 coal->coal_bufs = 30;
7766 coal->coal_ticks_irq = 1;
7767 coal->coal_bufs_irq = 2;
7768 coal->idle_thresh = 50;
7769 coal->bufs_per_record = 2;
7770 coal->budget = 64; /* NAPI budget */
7772 coal = &bp->tx_coal;
7773 coal->coal_ticks = 28;
7774 coal->coal_bufs = 30;
7775 coal->coal_ticks_irq = 2;
7776 coal->coal_bufs_irq = 2;
7777 coal->bufs_per_record = 1;
7779 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7782 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
7785 struct bnxt *bp = netdev_priv(dev);
7787 SET_NETDEV_DEV(dev, &pdev->dev);
7789 /* enable device (incl. PCI PM wakeup), and bus-mastering */
7790 rc = pci_enable_device(pdev);
7792 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
7796 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
7798 "Cannot find PCI device base address, aborting\n");
7800 goto init_err_disable;
7803 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
7805 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
7806 goto init_err_disable;
7809 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
7810 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
7811 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
7812 goto init_err_disable;
7815 pci_set_master(pdev);
7820 bp->bar0 = pci_ioremap_bar(pdev, 0);
7822 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
7824 goto init_err_release;
7827 bp->bar1 = pci_ioremap_bar(pdev, 2);
7829 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
7831 goto init_err_release;
7834 bp->bar2 = pci_ioremap_bar(pdev, 4);
7836 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
7838 goto init_err_release;
7841 pci_enable_pcie_error_reporting(pdev);
7843 INIT_WORK(&bp->sp_task, bnxt_sp_task);
7845 spin_lock_init(&bp->ntp_fltr_lock);
7847 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
7848 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
7850 bnxt_init_dflt_coal(bp);
7852 timer_setup(&bp->timer, bnxt_timer, 0);
7853 bp->current_interval = BNXT_TIMER_INTERVAL;
7855 clear_bit(BNXT_STATE_OPEN, &bp->state);
7859 bnxt_unmap_bars(bp, pdev);
7860 pci_release_regions(pdev);
7863 pci_disable_device(pdev);
7869 /* rtnl_lock held */
7870 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7872 struct sockaddr *addr = p;
7873 struct bnxt *bp = netdev_priv(dev);
7876 if (!is_valid_ether_addr(addr->sa_data))
7877 return -EADDRNOTAVAIL;
7879 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7882 rc = bnxt_approve_mac(bp, addr->sa_data);
7886 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7887 if (netif_running(dev)) {
7888 bnxt_close_nic(bp, false, false);
7889 rc = bnxt_open_nic(bp, false, false);
7895 /* rtnl_lock held */
7896 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7898 struct bnxt *bp = netdev_priv(dev);
7900 if (netif_running(dev))
7901 bnxt_close_nic(bp, false, false);
7904 bnxt_set_ring_params(bp);
7906 if (netif_running(dev))
7907 return bnxt_open_nic(bp, false, false);
7912 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
7914 struct bnxt *bp = netdev_priv(dev);
7918 if (tc > bp->max_tc) {
7919 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
7924 if (netdev_get_num_tc(dev) == tc)
7927 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7930 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7931 sh, tc, bp->tx_nr_rings_xdp);
7935 /* Needs to close the device and do hw resource re-allocations */
7936 if (netif_running(bp->dev))
7937 bnxt_close_nic(bp, true, false);
7940 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7941 netdev_set_num_tc(dev, tc);
7943 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7944 netdev_reset_tc(dev);
7946 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
7947 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7948 bp->tx_nr_rings + bp->rx_nr_rings;
7949 bp->num_stat_ctxs = bp->cp_nr_rings;
7951 if (netif_running(bp->dev))
7952 return bnxt_open_nic(bp, true, false);
7957 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
7960 struct bnxt *bp = cb_priv;
7962 if (!bnxt_tc_flower_enabled(bp) ||
7963 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
7967 case TC_SETUP_CLSFLOWER:
7968 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
7974 static int bnxt_setup_tc_block(struct net_device *dev,
7975 struct tc_block_offload *f)
7977 struct bnxt *bp = netdev_priv(dev);
7979 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
7982 switch (f->command) {
7984 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
7986 case TC_BLOCK_UNBIND:
7987 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
7994 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
7998 case TC_SETUP_BLOCK:
7999 return bnxt_setup_tc_block(dev, type_data);
8000 case TC_SETUP_QDISC_MQPRIO: {
8001 struct tc_mqprio_qopt *mqprio = type_data;
8003 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
8005 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
8012 #ifdef CONFIG_RFS_ACCEL
8013 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
8014 struct bnxt_ntuple_filter *f2)
8016 struct flow_keys *keys1 = &f1->fkeys;
8017 struct flow_keys *keys2 = &f2->fkeys;
8019 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
8020 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
8021 keys1->ports.ports == keys2->ports.ports &&
8022 keys1->basic.ip_proto == keys2->basic.ip_proto &&
8023 keys1->basic.n_proto == keys2->basic.n_proto &&
8024 keys1->control.flags == keys2->control.flags &&
8025 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
8026 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
8032 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
8033 u16 rxq_index, u32 flow_id)
8035 struct bnxt *bp = netdev_priv(dev);
8036 struct bnxt_ntuple_filter *fltr, *new_fltr;
8037 struct flow_keys *fkeys;
8038 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
8039 int rc = 0, idx, bit_id, l2_idx = 0;
8040 struct hlist_head *head;
8042 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
8043 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8046 netif_addr_lock_bh(dev);
8047 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
8048 if (ether_addr_equal(eth->h_dest,
8049 vnic->uc_list + off)) {
8054 netif_addr_unlock_bh(dev);
8058 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
8062 fkeys = &new_fltr->fkeys;
8063 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
8064 rc = -EPROTONOSUPPORT;
8068 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
8069 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
8070 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
8071 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
8072 rc = -EPROTONOSUPPORT;
8075 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
8076 bp->hwrm_spec_code < 0x10601) {
8077 rc = -EPROTONOSUPPORT;
8080 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
8081 bp->hwrm_spec_code < 0x10601) {
8082 rc = -EPROTONOSUPPORT;
8086 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
8087 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
8089 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
8090 head = &bp->ntp_fltr_hash_tbl[idx];
8092 hlist_for_each_entry_rcu(fltr, head, hash) {
8093 if (bnxt_fltr_match(fltr, new_fltr)) {
8101 spin_lock_bh(&bp->ntp_fltr_lock);
8102 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
8103 BNXT_NTP_FLTR_MAX_FLTR, 0);
8105 spin_unlock_bh(&bp->ntp_fltr_lock);
8110 new_fltr->sw_id = (u16)bit_id;
8111 new_fltr->flow_id = flow_id;
8112 new_fltr->l2_fltr_idx = l2_idx;
8113 new_fltr->rxq = rxq_index;
8114 hlist_add_head_rcu(&new_fltr->hash, head);
8115 bp->ntp_fltr_count++;
8116 spin_unlock_bh(&bp->ntp_fltr_lock);
8118 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
8119 bnxt_queue_sp_work(bp);
8121 return new_fltr->sw_id;
8128 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8132 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
8133 struct hlist_head *head;
8134 struct hlist_node *tmp;
8135 struct bnxt_ntuple_filter *fltr;
8138 head = &bp->ntp_fltr_hash_tbl[i];
8139 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
8142 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
8143 if (rps_may_expire_flow(bp->dev, fltr->rxq,
8146 bnxt_hwrm_cfa_ntuple_filter_free(bp,
8151 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
8156 set_bit(BNXT_FLTR_VALID, &fltr->state);
8160 spin_lock_bh(&bp->ntp_fltr_lock);
8161 hlist_del_rcu(&fltr->hash);
8162 bp->ntp_fltr_count--;
8163 spin_unlock_bh(&bp->ntp_fltr_lock);
8165 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
8170 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
8171 netdev_info(bp->dev, "Receive PF driver unload event!");
8176 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
8180 #endif /* CONFIG_RFS_ACCEL */
8182 static void bnxt_udp_tunnel_add(struct net_device *dev,
8183 struct udp_tunnel_info *ti)
8185 struct bnxt *bp = netdev_priv(dev);
8187 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8190 if (!netif_running(dev))
8194 case UDP_TUNNEL_TYPE_VXLAN:
8195 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
8198 bp->vxlan_port_cnt++;
8199 if (bp->vxlan_port_cnt == 1) {
8200 bp->vxlan_port = ti->port;
8201 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
8202 bnxt_queue_sp_work(bp);
8205 case UDP_TUNNEL_TYPE_GENEVE:
8206 if (bp->nge_port_cnt && bp->nge_port != ti->port)
8210 if (bp->nge_port_cnt == 1) {
8211 bp->nge_port = ti->port;
8212 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
8219 bnxt_queue_sp_work(bp);
8222 static void bnxt_udp_tunnel_del(struct net_device *dev,
8223 struct udp_tunnel_info *ti)
8225 struct bnxt *bp = netdev_priv(dev);
8227 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
8230 if (!netif_running(dev))
8234 case UDP_TUNNEL_TYPE_VXLAN:
8235 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
8237 bp->vxlan_port_cnt--;
8239 if (bp->vxlan_port_cnt != 0)
8242 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
8244 case UDP_TUNNEL_TYPE_GENEVE:
8245 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
8249 if (bp->nge_port_cnt != 0)
8252 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
8258 bnxt_queue_sp_work(bp);
8261 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8262 struct net_device *dev, u32 filter_mask,
8265 struct bnxt *bp = netdev_priv(dev);
8267 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
8268 nlflags, filter_mask, NULL);
8271 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8274 struct bnxt *bp = netdev_priv(dev);
8275 struct nlattr *attr, *br_spec;
8278 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
8281 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8285 nla_for_each_nested(attr, br_spec, rem) {
8288 if (nla_type(attr) != IFLA_BRIDGE_MODE)
8291 if (nla_len(attr) < sizeof(mode))
8294 mode = nla_get_u16(attr);
8295 if (mode == bp->br_mode)
8298 rc = bnxt_hwrm_set_br_mode(bp, mode);
8306 static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
8309 struct bnxt *bp = netdev_priv(dev);
8312 /* The PF and it's VF-reps only support the switchdev framework */
8316 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
8323 int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
8325 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
8328 /* The PF and it's VF-reps only support the switchdev framework */
8333 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
8334 attr->u.ppid.id_len = sizeof(bp->switch_id);
8335 memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
8343 static int bnxt_swdev_port_attr_get(struct net_device *dev,
8344 struct switchdev_attr *attr)
8346 return bnxt_port_attr_get(netdev_priv(dev), attr);
8349 static const struct switchdev_ops bnxt_switchdev_ops = {
8350 .switchdev_port_attr_get = bnxt_swdev_port_attr_get
8353 static const struct net_device_ops bnxt_netdev_ops = {
8354 .ndo_open = bnxt_open,
8355 .ndo_start_xmit = bnxt_start_xmit,
8356 .ndo_stop = bnxt_close,
8357 .ndo_get_stats64 = bnxt_get_stats64,
8358 .ndo_set_rx_mode = bnxt_set_rx_mode,
8359 .ndo_do_ioctl = bnxt_ioctl,
8360 .ndo_validate_addr = eth_validate_addr,
8361 .ndo_set_mac_address = bnxt_change_mac_addr,
8362 .ndo_change_mtu = bnxt_change_mtu,
8363 .ndo_fix_features = bnxt_fix_features,
8364 .ndo_set_features = bnxt_set_features,
8365 .ndo_tx_timeout = bnxt_tx_timeout,
8366 #ifdef CONFIG_BNXT_SRIOV
8367 .ndo_get_vf_config = bnxt_get_vf_config,
8368 .ndo_set_vf_mac = bnxt_set_vf_mac,
8369 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
8370 .ndo_set_vf_rate = bnxt_set_vf_bw,
8371 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
8372 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
8373 .ndo_set_vf_trust = bnxt_set_vf_trust,
8375 #ifdef CONFIG_NET_POLL_CONTROLLER
8376 .ndo_poll_controller = bnxt_poll_controller,
8378 .ndo_setup_tc = bnxt_setup_tc,
8379 #ifdef CONFIG_RFS_ACCEL
8380 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
8382 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
8383 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
8384 .ndo_bpf = bnxt_xdp,
8385 .ndo_bridge_getlink = bnxt_bridge_getlink,
8386 .ndo_bridge_setlink = bnxt_bridge_setlink,
8387 .ndo_get_phys_port_name = bnxt_get_phys_port_name
8390 static void bnxt_remove_one(struct pci_dev *pdev)
8392 struct net_device *dev = pci_get_drvdata(pdev);
8393 struct bnxt *bp = netdev_priv(dev);
8396 bnxt_sriov_disable(bp);
8397 bnxt_dl_unregister(bp);
8400 pci_disable_pcie_error_reporting(pdev);
8401 unregister_netdev(dev);
8402 bnxt_shutdown_tc(bp);
8403 bnxt_cancel_sp_work(bp);
8406 bnxt_clear_int_mode(bp);
8407 bnxt_hwrm_func_drv_unrgtr(bp);
8408 bnxt_free_hwrm_resources(bp);
8409 bnxt_free_hwrm_short_cmd_req(bp);
8410 bnxt_ethtool_free(bp);
8414 bnxt_cleanup_pci(bp);
8418 static int bnxt_probe_phy(struct bnxt *bp)
8421 struct bnxt_link_info *link_info = &bp->link_info;
8423 rc = bnxt_hwrm_phy_qcaps(bp);
8425 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
8429 mutex_init(&bp->link_lock);
8431 rc = bnxt_update_link(bp, false);
8433 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
8438 /* Older firmware does not have supported_auto_speeds, so assume
8439 * that all supported speeds can be autonegotiated.
8441 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
8442 link_info->support_auto_speeds = link_info->support_speeds;
8444 /*initialize the ethool setting copy with NVM settings */
8445 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
8446 link_info->autoneg = BNXT_AUTONEG_SPEED;
8447 if (bp->hwrm_spec_code >= 0x10201) {
8448 if (link_info->auto_pause_setting &
8449 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
8450 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8452 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
8454 link_info->advertising = link_info->auto_link_speeds;
8456 link_info->req_link_speed = link_info->force_link_speed;
8457 link_info->req_duplex = link_info->duplex_setting;
8459 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
8460 link_info->req_flow_ctrl =
8461 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
8463 link_info->req_flow_ctrl = link_info->force_pause_setting;
8467 static int bnxt_get_max_irq(struct pci_dev *pdev)
8471 if (!pdev->msix_cap)
8474 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
8475 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
8478 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8481 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8482 int max_ring_grps = 0;
8484 *max_tx = hw_resc->max_tx_rings;
8485 *max_rx = hw_resc->max_rx_rings;
8486 *max_cp = min_t(int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8487 *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
8488 max_ring_grps = hw_resc->max_hw_ring_grps;
8489 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
8493 if (bp->flags & BNXT_FLAG_AGG_RINGS)
8495 *max_rx = min_t(int, *max_rx, max_ring_grps);
8498 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
8502 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
8505 if (!rx || !tx || !cp)
8508 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
8511 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
8516 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8517 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
8518 /* Not enough rings, try disabling agg rings. */
8519 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8520 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
8522 /* set BNXT_FLAG_AGG_RINGS back for consistency */
8523 bp->flags |= BNXT_FLAG_AGG_RINGS;
8526 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8527 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8528 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
8529 bnxt_set_ring_params(bp);
8532 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
8533 int max_cp, max_stat, max_irq;
8535 /* Reserve minimum resources for RoCE */
8536 max_cp = bnxt_get_max_func_cp_rings(bp);
8537 max_stat = bnxt_get_max_func_stat_ctxs(bp);
8538 max_irq = bnxt_get_max_func_irqs(bp);
8539 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
8540 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
8541 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
8544 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
8545 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
8546 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
8547 max_cp = min_t(int, max_cp, max_irq);
8548 max_cp = min_t(int, max_cp, max_stat);
8549 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
8556 /* In initial default shared ring setting, each shared ring must have a
8559 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
8561 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
8562 bp->rx_nr_rings = bp->cp_nr_rings;
8563 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
8564 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8567 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
8569 int dflt_rings, max_rx_rings, max_tx_rings, rc;
8571 if (!bnxt_can_reserve_rings(bp))
8575 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8576 dflt_rings = netif_get_num_default_rss_queues();
8577 /* Reduce default rings on multi-port cards so that total default
8578 * rings do not exceed CPU count.
8580 if (bp->port_count > 1) {
8582 max_t(int, num_online_cpus() / bp->port_count, 1);
8584 dflt_rings = min_t(int, dflt_rings, max_rings);
8586 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
8589 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
8590 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
8592 bnxt_trim_dflt_sh_rings(bp);
8594 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
8595 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
8597 rc = __bnxt_reserve_rings(bp);
8599 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
8600 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8602 bnxt_trim_dflt_sh_rings(bp);
8604 /* Rings may have been trimmed, re-reserve the trimmed rings. */
8605 if (bnxt_need_reserve_rings(bp)) {
8606 rc = __bnxt_reserve_rings(bp);
8608 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
8609 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8611 bp->num_stat_ctxs = bp->cp_nr_rings;
8612 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8619 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
8623 if (bp->tx_nr_rings)
8626 rc = bnxt_set_dflt_rings(bp, true);
8628 netdev_err(bp->dev, "Not enough rings available.\n");
8631 rc = bnxt_init_int_mode(bp);
8634 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8635 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
8636 bp->flags |= BNXT_FLAG_RFS;
8637 bp->dev->features |= NETIF_F_NTUPLE;
8642 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
8647 bnxt_hwrm_func_qcaps(bp);
8649 if (netif_running(bp->dev))
8650 __bnxt_close_nic(bp, true, false);
8652 bnxt_ulp_irq_stop(bp);
8653 bnxt_clear_int_mode(bp);
8654 rc = bnxt_init_int_mode(bp);
8655 bnxt_ulp_irq_restart(bp, rc);
8657 if (netif_running(bp->dev)) {
8661 rc = bnxt_open_nic(bp, true, false);
8667 static int bnxt_init_mac_addr(struct bnxt *bp)
8672 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
8674 #ifdef CONFIG_BNXT_SRIOV
8675 struct bnxt_vf_info *vf = &bp->vf;
8677 if (is_valid_ether_addr(vf->mac_addr)) {
8678 /* overwrite netdev dev_addr with admin VF MAC */
8679 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
8681 eth_hw_addr_random(bp->dev);
8683 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
8689 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8691 static int version_printed;
8692 struct net_device *dev;
8696 if (pci_is_bridge(pdev))
8699 if (version_printed++ == 0)
8700 pr_info("%s", version);
8702 max_irqs = bnxt_get_max_irq(pdev);
8703 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
8707 bp = netdev_priv(dev);
8709 if (bnxt_vf_pciid(ent->driver_data))
8710 bp->flags |= BNXT_FLAG_VF;
8713 bp->flags |= BNXT_FLAG_MSIX_CAP;
8715 rc = bnxt_init_board(pdev, dev);
8719 dev->netdev_ops = &bnxt_netdev_ops;
8720 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
8721 dev->ethtool_ops = &bnxt_ethtool_ops;
8722 SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
8723 pci_set_drvdata(pdev, dev);
8725 rc = bnxt_alloc_hwrm_resources(bp);
8727 goto init_err_pci_clean;
8729 mutex_init(&bp->hwrm_cmd_lock);
8730 rc = bnxt_hwrm_ver_get(bp);
8732 goto init_err_pci_clean;
8734 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
8735 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
8737 goto init_err_pci_clean;
8740 rc = bnxt_hwrm_func_reset(bp);
8742 goto init_err_pci_clean;
8744 bnxt_hwrm_fw_set_time(bp);
8746 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8747 NETIF_F_TSO | NETIF_F_TSO6 |
8748 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
8749 NETIF_F_GSO_IPXIP4 |
8750 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8751 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
8752 NETIF_F_RXCSUM | NETIF_F_GRO;
8754 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8755 dev->hw_features |= NETIF_F_LRO;
8757 dev->hw_enc_features =
8758 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
8759 NETIF_F_TSO | NETIF_F_TSO6 |
8760 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
8761 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
8762 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
8763 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
8764 NETIF_F_GSO_GRE_CSUM;
8765 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
8766 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
8767 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
8768 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
8769 dev->hw_features |= NETIF_F_GRO_HW;
8770 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
8771 if (dev->features & NETIF_F_GRO_HW)
8772 dev->features &= ~NETIF_F_LRO;
8773 dev->priv_flags |= IFF_UNICAST_FLT;
8775 #ifdef CONFIG_BNXT_SRIOV
8776 init_waitqueue_head(&bp->sriov_cfg_wait);
8777 mutex_init(&bp->sriov_lock);
8779 bp->gro_func = bnxt_gro_func_5730x;
8780 if (BNXT_CHIP_P4_PLUS(bp))
8781 bp->gro_func = bnxt_gro_func_5731x;
8783 bp->flags |= BNXT_FLAG_DOUBLE_DB;
8785 rc = bnxt_hwrm_func_drv_rgtr(bp);
8787 goto init_err_pci_clean;
8789 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
8791 goto init_err_pci_clean;
8793 bp->ulp_probe = bnxt_ulp_probe;
8795 /* Get the MAX capabilities for this function */
8796 rc = bnxt_hwrm_func_qcaps(bp);
8798 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
8801 goto init_err_pci_clean;
8803 rc = bnxt_init_mac_addr(bp);
8805 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
8806 rc = -EADDRNOTAVAIL;
8807 goto init_err_pci_clean;
8809 rc = bnxt_hwrm_queue_qportcfg(bp);
8811 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
8814 goto init_err_pci_clean;
8817 bnxt_hwrm_func_qcfg(bp);
8818 bnxt_hwrm_port_led_qcaps(bp);
8819 bnxt_ethtool_init(bp);
8822 /* MTU range: 60 - FW defined max */
8823 dev->min_mtu = ETH_ZLEN;
8824 dev->max_mtu = bp->max_mtu;
8826 rc = bnxt_probe_phy(bp);
8828 goto init_err_pci_clean;
8830 bnxt_set_rx_skb_mode(bp, false);
8831 bnxt_set_tpa_flags(bp);
8832 bnxt_set_ring_params(bp);
8833 bnxt_set_max_func_irqs(bp, max_irqs);
8834 rc = bnxt_set_dflt_rings(bp, true);
8836 netdev_err(bp->dev, "Not enough rings available.\n");
8838 goto init_err_pci_clean;
8841 /* Default RSS hash cfg. */
8842 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
8843 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
8844 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
8845 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
8846 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
8847 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
8848 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
8849 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
8852 bnxt_hwrm_vnic_qcaps(bp);
8853 if (bnxt_rfs_supported(bp)) {
8854 dev->hw_features |= NETIF_F_NTUPLE;
8855 if (bnxt_rfs_capable(bp)) {
8856 bp->flags |= BNXT_FLAG_RFS;
8857 dev->features |= NETIF_F_NTUPLE;
8861 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
8862 bp->flags |= BNXT_FLAG_STRIP_VLAN;
8864 rc = bnxt_init_int_mode(bp);
8866 goto init_err_pci_clean;
8868 /* No TC has been set yet and rings may have been trimmed due to
8869 * limited MSIX, so we re-initialize the TX rings per TC.
8871 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8873 bnxt_get_wol_settings(bp);
8874 if (bp->flags & BNXT_FLAG_WOL_CAP)
8875 device_set_wakeup_enable(&pdev->dev, bp->wol);
8877 device_set_wakeup_capable(&pdev->dev, false);
8879 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
8884 create_singlethread_workqueue("bnxt_pf_wq");
8886 dev_err(&pdev->dev, "Unable to create workqueue.\n");
8887 goto init_err_pci_clean;
8893 rc = register_netdev(dev);
8895 goto init_err_cleanup_tc;
8898 bnxt_dl_register(bp);
8900 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
8901 board_info[ent->driver_data].name,
8902 (long)pci_resource_start(pdev, 0), dev->dev_addr);
8903 pcie_print_link_status(pdev);
8907 init_err_cleanup_tc:
8908 bnxt_shutdown_tc(bp);
8909 bnxt_clear_int_mode(bp);
8912 bnxt_cleanup_pci(bp);
8919 static void bnxt_shutdown(struct pci_dev *pdev)
8921 struct net_device *dev = pci_get_drvdata(pdev);
8928 bp = netdev_priv(dev);
8932 if (netif_running(dev))
8935 bnxt_ulp_shutdown(bp);
8937 if (system_state == SYSTEM_POWER_OFF) {
8938 bnxt_clear_int_mode(bp);
8939 pci_wake_from_d3(pdev, bp->wol);
8940 pci_set_power_state(pdev, PCI_D3hot);
8947 #ifdef CONFIG_PM_SLEEP
8948 static int bnxt_suspend(struct device *device)
8950 struct pci_dev *pdev = to_pci_dev(device);
8951 struct net_device *dev = pci_get_drvdata(pdev);
8952 struct bnxt *bp = netdev_priv(dev);
8956 if (netif_running(dev)) {
8957 netif_device_detach(dev);
8958 rc = bnxt_close(dev);
8960 bnxt_hwrm_func_drv_unrgtr(bp);
8965 static int bnxt_resume(struct device *device)
8967 struct pci_dev *pdev = to_pci_dev(device);
8968 struct net_device *dev = pci_get_drvdata(pdev);
8969 struct bnxt *bp = netdev_priv(dev);
8973 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
8977 rc = bnxt_hwrm_func_reset(bp);
8982 bnxt_get_wol_settings(bp);
8983 if (netif_running(dev)) {
8984 rc = bnxt_open(dev);
8986 netif_device_attach(dev);
8994 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
8995 #define BNXT_PM_OPS (&bnxt_pm_ops)
8999 #define BNXT_PM_OPS NULL
9001 #endif /* CONFIG_PM_SLEEP */
9004 * bnxt_io_error_detected - called when PCI error is detected
9005 * @pdev: Pointer to PCI device
9006 * @state: The current pci connection state
9008 * This function is called after a PCI bus error affecting
9009 * this device has been detected.
9011 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
9012 pci_channel_state_t state)
9014 struct net_device *netdev = pci_get_drvdata(pdev);
9015 struct bnxt *bp = netdev_priv(netdev);
9017 netdev_info(netdev, "PCI I/O error detected\n");
9020 netif_device_detach(netdev);
9024 if (state == pci_channel_io_perm_failure) {
9026 return PCI_ERS_RESULT_DISCONNECT;
9029 if (netif_running(netdev))
9032 pci_disable_device(pdev);
9035 /* Request a slot slot reset. */
9036 return PCI_ERS_RESULT_NEED_RESET;
9040 * bnxt_io_slot_reset - called after the pci bus has been reset.
9041 * @pdev: Pointer to PCI device
9043 * Restart the card from scratch, as if from a cold-boot.
9044 * At this point, the card has exprienced a hard reset,
9045 * followed by fixups by BIOS, and has its config space
9046 * set up identically to what it was at cold boot.
9048 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
9050 struct net_device *netdev = pci_get_drvdata(pdev);
9051 struct bnxt *bp = netdev_priv(netdev);
9053 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
9055 netdev_info(bp->dev, "PCI Slot Reset\n");
9059 if (pci_enable_device(pdev)) {
9061 "Cannot re-enable PCI device after reset.\n");
9063 pci_set_master(pdev);
9065 err = bnxt_hwrm_func_reset(bp);
9066 if (!err && netif_running(netdev))
9067 err = bnxt_open(netdev);
9070 result = PCI_ERS_RESULT_RECOVERED;
9075 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
9080 err = pci_cleanup_aer_uncorrect_error_status(pdev);
9083 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
9084 err); /* non-fatal, continue */
9087 return PCI_ERS_RESULT_RECOVERED;
9091 * bnxt_io_resume - called when traffic can start flowing again.
9092 * @pdev: Pointer to PCI device
9094 * This callback is called when the error recovery driver tells
9095 * us that its OK to resume normal operation.
9097 static void bnxt_io_resume(struct pci_dev *pdev)
9099 struct net_device *netdev = pci_get_drvdata(pdev);
9103 netif_device_attach(netdev);
9108 static const struct pci_error_handlers bnxt_err_handler = {
9109 .error_detected = bnxt_io_error_detected,
9110 .slot_reset = bnxt_io_slot_reset,
9111 .resume = bnxt_io_resume
9114 static struct pci_driver bnxt_pci_driver = {
9115 .name = DRV_MODULE_NAME,
9116 .id_table = bnxt_pci_tbl,
9117 .probe = bnxt_init_one,
9118 .remove = bnxt_remove_one,
9119 .shutdown = bnxt_shutdown,
9120 .driver.pm = BNXT_PM_OPS,
9121 .err_handler = &bnxt_err_handler,
9122 #if defined(CONFIG_BNXT_SRIOV)
9123 .sriov_configure = bnxt_sriov_configure,
9127 static int __init bnxt_init(void)
9130 return pci_register_driver(&bnxt_pci_driver);
9133 static void __exit bnxt_exit(void)
9135 pci_unregister_driver(&bnxt_pci_driver);
9137 destroy_workqueue(bnxt_pf_wq);
9141 module_init(bnxt_init);
9142 module_exit(bnxt_exit);