1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2019 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>
34 #include <linux/mdio.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/udp_tunnel.h>
46 #include <linux/workqueue.h>
47 #include <linux/prefetch.h>
48 #include <linux/cache.h>
49 #include <linux/log2.h>
50 #include <linux/aer.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <linux/hwmon.h>
56 #include <linux/hwmon-sysfs.h>
57 #include <net/page_pool.h>
62 #include "bnxt_sriov.h"
63 #include "bnxt_ethtool.h"
68 #include "bnxt_devlink.h"
69 #include "bnxt_debugfs.h"
71 #define BNXT_TX_TIMEOUT (5 * HZ)
73 MODULE_LICENSE("GPL");
74 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
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
127 /* indexed by enum above */
128 static const struct {
131 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
132 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
133 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
134 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
135 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
136 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
137 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
138 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
139 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
140 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
141 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
142 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
143 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
144 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
145 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
146 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
147 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
148 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
149 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
150 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
151 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
152 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
153 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
154 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
155 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
156 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
157 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
158 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
159 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
160 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
161 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
162 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
163 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
164 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
165 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
166 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
167 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
168 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
169 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
170 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
171 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
172 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
175 static const struct pci_device_id bnxt_pci_tbl[] = {
176 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
177 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
178 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
179 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
180 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
181 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
182 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
183 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
184 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
185 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
186 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
187 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
188 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
189 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
190 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
191 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
192 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
193 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
194 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
195 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
196 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
197 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
198 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
199 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
200 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
201 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
202 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
203 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
204 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
205 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
206 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
207 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
208 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
209 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
210 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
211 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
212 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
213 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
214 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
215 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
216 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
217 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
218 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
219 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
220 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
221 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
222 #ifdef CONFIG_BNXT_SRIOV
223 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
224 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
225 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
226 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
227 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
228 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
229 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
230 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
231 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
232 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
233 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
238 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
240 static const u16 bnxt_vf_req_snif[] = {
244 HWRM_CFA_L2_FILTER_ALLOC,
247 static const u16 bnxt_async_events_arr[] = {
248 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
249 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
250 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
251 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
252 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
253 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
254 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
255 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
256 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
259 static struct workqueue_struct *bnxt_pf_wq;
261 static bool bnxt_vf_pciid(enum board_idx idx)
263 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
264 idx == NETXTREME_S_VF || idx == NETXTREME_E_P5_VF);
267 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
268 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
269 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
271 #define BNXT_CP_DB_IRQ_DIS(db) \
272 writel(DB_CP_IRQ_DIS_FLAGS, db)
274 #define BNXT_DB_CQ(db, idx) \
275 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
277 #define BNXT_DB_NQ_P5(db, idx) \
278 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
280 #define BNXT_DB_CQ_ARM(db, idx) \
281 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
283 #define BNXT_DB_NQ_ARM_P5(db, idx) \
284 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
286 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
288 if (bp->flags & BNXT_FLAG_CHIP_P5)
289 BNXT_DB_NQ_P5(db, idx);
294 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
296 if (bp->flags & BNXT_FLAG_CHIP_P5)
297 BNXT_DB_NQ_ARM_P5(db, idx);
299 BNXT_DB_CQ_ARM(db, idx);
302 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
304 if (bp->flags & BNXT_FLAG_CHIP_P5)
305 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
311 const u16 bnxt_lhint_arr[] = {
312 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
313 TX_BD_FLAGS_LHINT_512_TO_1023,
314 TX_BD_FLAGS_LHINT_1024_TO_2047,
315 TX_BD_FLAGS_LHINT_1024_TO_2047,
316 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
317 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
318 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
319 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
320 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
321 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
322 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
323 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
324 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
325 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
326 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
327 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
328 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
329 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
330 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
333 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
335 struct metadata_dst *md_dst = skb_metadata_dst(skb);
337 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
340 return md_dst->u.port_info.port_id;
343 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
345 struct bnxt *bp = netdev_priv(dev);
347 struct tx_bd_ext *txbd1;
348 struct netdev_queue *txq;
351 unsigned int length, pad = 0;
352 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
354 struct pci_dev *pdev = bp->pdev;
355 struct bnxt_tx_ring_info *txr;
356 struct bnxt_sw_tx_bd *tx_buf;
358 i = skb_get_queue_mapping(skb);
359 if (unlikely(i >= bp->tx_nr_rings)) {
360 dev_kfree_skb_any(skb);
364 txq = netdev_get_tx_queue(dev, i);
365 txr = &bp->tx_ring[bp->tx_ring_map[i]];
368 free_size = bnxt_tx_avail(bp, txr);
369 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
370 netif_tx_stop_queue(txq);
371 return NETDEV_TX_BUSY;
375 len = skb_headlen(skb);
376 last_frag = skb_shinfo(skb)->nr_frags;
378 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
380 txbd->tx_bd_opaque = prod;
382 tx_buf = &txr->tx_buf_ring[prod];
384 tx_buf->nr_frags = last_frag;
387 cfa_action = bnxt_xmit_get_cfa_action(skb);
388 if (skb_vlan_tag_present(skb)) {
389 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
390 skb_vlan_tag_get(skb);
391 /* Currently supports 8021Q, 8021AD vlan offloads
392 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
394 if (skb->vlan_proto == htons(ETH_P_8021Q))
395 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
398 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
399 struct tx_push_buffer *tx_push_buf = txr->tx_push;
400 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
401 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
402 void __iomem *db = txr->tx_db.doorbell;
403 void *pdata = tx_push_buf->data;
407 /* Set COAL_NOW to be ready quickly for the next push */
408 tx_push->tx_bd_len_flags_type =
409 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
410 TX_BD_TYPE_LONG_TX_BD |
411 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
412 TX_BD_FLAGS_COAL_NOW |
413 TX_BD_FLAGS_PACKET_END |
414 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
416 if (skb->ip_summed == CHECKSUM_PARTIAL)
417 tx_push1->tx_bd_hsize_lflags =
418 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 tx_push1->tx_bd_hsize_lflags = 0;
422 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
423 tx_push1->tx_bd_cfa_action =
424 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
426 end = pdata + length;
427 end = PTR_ALIGN(end, 8) - 1;
430 skb_copy_from_linear_data(skb, pdata, len);
432 for (j = 0; j < last_frag; j++) {
433 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
436 fptr = skb_frag_address_safe(frag);
440 memcpy(pdata, fptr, skb_frag_size(frag));
441 pdata += skb_frag_size(frag);
444 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
445 txbd->tx_bd_haddr = txr->data_mapping;
446 prod = NEXT_TX(prod);
447 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
448 memcpy(txbd, tx_push1, sizeof(*txbd));
449 prod = NEXT_TX(prod);
451 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
455 netdev_tx_sent_queue(txq, skb->len);
456 wmb(); /* Sync is_push and byte queue before pushing data */
458 push_len = (length + sizeof(*tx_push) + 7) / 8;
460 __iowrite64_copy(db, tx_push_buf, 16);
461 __iowrite32_copy(db + 4, tx_push_buf + 1,
462 (push_len - 16) << 1);
464 __iowrite64_copy(db, tx_push_buf, push_len);
471 if (length < BNXT_MIN_PKT_SIZE) {
472 pad = BNXT_MIN_PKT_SIZE - length;
473 if (skb_pad(skb, pad)) {
474 /* SKB already freed. */
478 length = BNXT_MIN_PKT_SIZE;
481 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
483 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
484 dev_kfree_skb_any(skb);
489 dma_unmap_addr_set(tx_buf, mapping, mapping);
490 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
491 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
493 txbd->tx_bd_haddr = cpu_to_le64(mapping);
495 prod = NEXT_TX(prod);
496 txbd1 = (struct tx_bd_ext *)
497 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
499 txbd1->tx_bd_hsize_lflags = 0;
500 if (skb_is_gso(skb)) {
503 if (skb->encapsulation)
504 hdr_len = skb_inner_network_offset(skb) +
505 skb_inner_network_header_len(skb) +
506 inner_tcp_hdrlen(skb);
508 hdr_len = skb_transport_offset(skb) +
511 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
513 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
514 length = skb_shinfo(skb)->gso_size;
515 txbd1->tx_bd_mss = cpu_to_le32(length);
517 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
518 txbd1->tx_bd_hsize_lflags =
519 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
520 txbd1->tx_bd_mss = 0;
524 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
525 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
530 flags |= bnxt_lhint_arr[length];
531 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
533 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
534 txbd1->tx_bd_cfa_action =
535 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
536 for (i = 0; i < last_frag; i++) {
537 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
539 prod = NEXT_TX(prod);
540 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
542 len = skb_frag_size(frag);
543 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
546 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
549 tx_buf = &txr->tx_buf_ring[prod];
550 dma_unmap_addr_set(tx_buf, mapping, mapping);
552 txbd->tx_bd_haddr = cpu_to_le64(mapping);
554 flags = len << TX_BD_LEN_SHIFT;
555 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
559 txbd->tx_bd_len_flags_type =
560 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
561 TX_BD_FLAGS_PACKET_END);
563 netdev_tx_sent_queue(txq, skb->len);
565 /* Sync BD data before updating doorbell */
568 prod = NEXT_TX(prod);
571 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
572 bnxt_db_write(bp, &txr->tx_db, prod);
576 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
577 if (netdev_xmit_more() && !tx_buf->is_push)
578 bnxt_db_write(bp, &txr->tx_db, prod);
580 netif_tx_stop_queue(txq);
582 /* netif_tx_stop_queue() must be done before checking
583 * tx index in bnxt_tx_avail() below, because in
584 * bnxt_tx_int(), we update tx index before checking for
585 * netif_tx_queue_stopped().
588 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
589 netif_tx_wake_queue(txq);
596 /* start back at beginning and unmap skb */
598 tx_buf = &txr->tx_buf_ring[prod];
600 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
601 skb_headlen(skb), PCI_DMA_TODEVICE);
602 prod = NEXT_TX(prod);
604 /* unmap remaining mapped pages */
605 for (i = 0; i < last_frag; i++) {
606 prod = NEXT_TX(prod);
607 tx_buf = &txr->tx_buf_ring[prod];
608 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
609 skb_frag_size(&skb_shinfo(skb)->frags[i]),
613 dev_kfree_skb_any(skb);
617 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
619 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
620 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
621 u16 cons = txr->tx_cons;
622 struct pci_dev *pdev = bp->pdev;
624 unsigned int tx_bytes = 0;
626 for (i = 0; i < nr_pkts; i++) {
627 struct bnxt_sw_tx_bd *tx_buf;
631 tx_buf = &txr->tx_buf_ring[cons];
632 cons = NEXT_TX(cons);
636 if (tx_buf->is_push) {
641 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
642 skb_headlen(skb), PCI_DMA_TODEVICE);
643 last = tx_buf->nr_frags;
645 for (j = 0; j < last; j++) {
646 cons = NEXT_TX(cons);
647 tx_buf = &txr->tx_buf_ring[cons];
650 dma_unmap_addr(tx_buf, mapping),
651 skb_frag_size(&skb_shinfo(skb)->frags[j]),
656 cons = NEXT_TX(cons);
658 tx_bytes += skb->len;
659 dev_kfree_skb_any(skb);
662 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
665 /* Need to make the tx_cons update visible to bnxt_start_xmit()
666 * before checking for netif_tx_queue_stopped(). Without the
667 * memory barrier, there is a small possibility that bnxt_start_xmit()
668 * will miss it and cause the queue to be stopped forever.
672 if (unlikely(netif_tx_queue_stopped(txq)) &&
673 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
674 __netif_tx_lock(txq, smp_processor_id());
675 if (netif_tx_queue_stopped(txq) &&
676 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
677 txr->dev_state != BNXT_DEV_STATE_CLOSING)
678 netif_tx_wake_queue(txq);
679 __netif_tx_unlock(txq);
683 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
684 struct bnxt_rx_ring_info *rxr,
687 struct device *dev = &bp->pdev->dev;
690 page = page_pool_dev_alloc_pages(rxr->page_pool);
694 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
695 DMA_ATTR_WEAK_ORDERING);
696 if (dma_mapping_error(dev, *mapping)) {
697 page_pool_recycle_direct(rxr->page_pool, page);
700 *mapping += bp->rx_dma_offset;
704 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
708 struct pci_dev *pdev = bp->pdev;
710 data = kmalloc(bp->rx_buf_size, gfp);
714 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
715 bp->rx_buf_use_size, bp->rx_dir,
716 DMA_ATTR_WEAK_ORDERING);
718 if (dma_mapping_error(&pdev->dev, *mapping)) {
725 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
728 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
729 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
732 if (BNXT_RX_PAGE_MODE(bp)) {
734 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
740 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
742 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
748 rx_buf->data_ptr = data + bp->rx_offset;
750 rx_buf->mapping = mapping;
752 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
756 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
758 u16 prod = rxr->rx_prod;
759 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
760 struct rx_bd *cons_bd, *prod_bd;
762 prod_rx_buf = &rxr->rx_buf_ring[prod];
763 cons_rx_buf = &rxr->rx_buf_ring[cons];
765 prod_rx_buf->data = data;
766 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
768 prod_rx_buf->mapping = cons_rx_buf->mapping;
770 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
771 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
773 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
776 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
778 u16 next, max = rxr->rx_agg_bmap_size;
780 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
782 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
786 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
787 struct bnxt_rx_ring_info *rxr,
791 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
792 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
793 struct pci_dev *pdev = bp->pdev;
796 u16 sw_prod = rxr->rx_sw_agg_prod;
797 unsigned int offset = 0;
799 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
802 page = alloc_page(gfp);
806 rxr->rx_page_offset = 0;
808 offset = rxr->rx_page_offset;
809 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
810 if (rxr->rx_page_offset == PAGE_SIZE)
815 page = alloc_page(gfp);
820 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
821 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
822 DMA_ATTR_WEAK_ORDERING);
823 if (dma_mapping_error(&pdev->dev, mapping)) {
828 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
829 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
831 __set_bit(sw_prod, rxr->rx_agg_bmap);
832 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
833 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
835 rx_agg_buf->page = page;
836 rx_agg_buf->offset = offset;
837 rx_agg_buf->mapping = mapping;
838 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
839 rxbd->rx_bd_opaque = sw_prod;
843 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
844 struct bnxt_cp_ring_info *cpr,
845 u16 cp_cons, u16 curr)
847 struct rx_agg_cmp *agg;
849 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
850 agg = (struct rx_agg_cmp *)
851 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
855 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
856 struct bnxt_rx_ring_info *rxr,
857 u16 agg_id, u16 curr)
859 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
861 return &tpa_info->agg_arr[curr];
864 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
865 u16 start, u32 agg_bufs, bool tpa)
867 struct bnxt_napi *bnapi = cpr->bnapi;
868 struct bnxt *bp = bnapi->bp;
869 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
870 u16 prod = rxr->rx_agg_prod;
871 u16 sw_prod = rxr->rx_sw_agg_prod;
875 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
878 for (i = 0; i < agg_bufs; i++) {
880 struct rx_agg_cmp *agg;
881 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
882 struct rx_bd *prod_bd;
886 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
888 agg = bnxt_get_agg(bp, cpr, idx, start + i);
889 cons = agg->rx_agg_cmp_opaque;
890 __clear_bit(cons, rxr->rx_agg_bmap);
892 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
893 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
895 __set_bit(sw_prod, rxr->rx_agg_bmap);
896 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
897 cons_rx_buf = &rxr->rx_agg_ring[cons];
899 /* It is possible for sw_prod to be equal to cons, so
900 * set cons_rx_buf->page to NULL first.
902 page = cons_rx_buf->page;
903 cons_rx_buf->page = NULL;
904 prod_rx_buf->page = page;
905 prod_rx_buf->offset = cons_rx_buf->offset;
907 prod_rx_buf->mapping = cons_rx_buf->mapping;
909 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
911 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
912 prod_bd->rx_bd_opaque = sw_prod;
914 prod = NEXT_RX_AGG(prod);
915 sw_prod = NEXT_RX_AGG(sw_prod);
917 rxr->rx_agg_prod = prod;
918 rxr->rx_sw_agg_prod = sw_prod;
921 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
922 struct bnxt_rx_ring_info *rxr,
923 u16 cons, void *data, u8 *data_ptr,
925 unsigned int offset_and_len)
927 unsigned int payload = offset_and_len >> 16;
928 unsigned int len = offset_and_len & 0xffff;
930 struct page *page = data;
931 u16 prod = rxr->rx_prod;
935 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
937 bnxt_reuse_rx_data(rxr, cons, data);
940 dma_addr -= bp->rx_dma_offset;
941 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
942 DMA_ATTR_WEAK_ORDERING);
943 page_pool_release_page(rxr->page_pool, page);
945 if (unlikely(!payload))
946 payload = eth_get_headlen(bp->dev, data_ptr, len);
948 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
954 off = (void *)data_ptr - page_address(page);
955 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
956 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
957 payload + NET_IP_ALIGN);
959 frag = &skb_shinfo(skb)->frags[0];
960 skb_frag_size_sub(frag, payload);
961 skb_frag_off_add(frag, payload);
962 skb->data_len -= payload;
963 skb->tail += payload;
968 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
969 struct bnxt_rx_ring_info *rxr, u16 cons,
970 void *data, u8 *data_ptr,
972 unsigned int offset_and_len)
974 u16 prod = rxr->rx_prod;
978 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
980 bnxt_reuse_rx_data(rxr, cons, data);
984 skb = build_skb(data, 0);
985 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
986 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
992 skb_reserve(skb, bp->rx_offset);
993 skb_put(skb, offset_and_len & 0xffff);
997 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
998 struct bnxt_cp_ring_info *cpr,
999 struct sk_buff *skb, u16 idx,
1000 u32 agg_bufs, bool tpa)
1002 struct bnxt_napi *bnapi = cpr->bnapi;
1003 struct pci_dev *pdev = bp->pdev;
1004 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1005 u16 prod = rxr->rx_agg_prod;
1006 bool p5_tpa = false;
1009 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1012 for (i = 0; i < agg_bufs; i++) {
1014 struct rx_agg_cmp *agg;
1015 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1020 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1022 agg = bnxt_get_agg(bp, cpr, idx, i);
1023 cons = agg->rx_agg_cmp_opaque;
1024 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1025 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1027 cons_rx_buf = &rxr->rx_agg_ring[cons];
1028 skb_fill_page_desc(skb, i, cons_rx_buf->page,
1029 cons_rx_buf->offset, frag_len);
1030 __clear_bit(cons, rxr->rx_agg_bmap);
1032 /* It is possible for bnxt_alloc_rx_page() to allocate
1033 * a sw_prod index that equals the cons index, so we
1034 * need to clear the cons entry now.
1036 mapping = cons_rx_buf->mapping;
1037 page = cons_rx_buf->page;
1038 cons_rx_buf->page = NULL;
1040 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1041 struct skb_shared_info *shinfo;
1042 unsigned int nr_frags;
1044 shinfo = skb_shinfo(skb);
1045 nr_frags = --shinfo->nr_frags;
1046 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1050 cons_rx_buf->page = page;
1052 /* Update prod since possibly some pages have been
1053 * allocated already.
1055 rxr->rx_agg_prod = prod;
1056 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1060 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1062 DMA_ATTR_WEAK_ORDERING);
1064 skb->data_len += frag_len;
1065 skb->len += frag_len;
1066 skb->truesize += PAGE_SIZE;
1068 prod = NEXT_RX_AGG(prod);
1070 rxr->rx_agg_prod = prod;
1074 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1075 u8 agg_bufs, u32 *raw_cons)
1078 struct rx_agg_cmp *agg;
1080 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1081 last = RING_CMP(*raw_cons);
1082 agg = (struct rx_agg_cmp *)
1083 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1084 return RX_AGG_CMP_VALID(agg, *raw_cons);
1087 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1091 struct bnxt *bp = bnapi->bp;
1092 struct pci_dev *pdev = bp->pdev;
1093 struct sk_buff *skb;
1095 skb = napi_alloc_skb(&bnapi->napi, len);
1099 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1102 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1103 len + NET_IP_ALIGN);
1105 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1112 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1113 u32 *raw_cons, void *cmp)
1115 struct rx_cmp *rxcmp = cmp;
1116 u32 tmp_raw_cons = *raw_cons;
1117 u8 cmp_type, agg_bufs = 0;
1119 cmp_type = RX_CMP_TYPE(rxcmp);
1121 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1122 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1124 RX_CMP_AGG_BUFS_SHIFT;
1125 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1126 struct rx_tpa_end_cmp *tpa_end = cmp;
1128 if (bp->flags & BNXT_FLAG_CHIP_P5)
1131 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1135 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1138 *raw_cons = tmp_raw_cons;
1142 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1144 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1148 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1150 schedule_delayed_work(&bp->fw_reset_task, delay);
1153 static void bnxt_queue_sp_work(struct bnxt *bp)
1156 queue_work(bnxt_pf_wq, &bp->sp_task);
1158 schedule_work(&bp->sp_task);
1161 static void bnxt_cancel_sp_work(struct bnxt *bp)
1164 flush_workqueue(bnxt_pf_wq);
1166 cancel_work_sync(&bp->sp_task);
1167 cancel_delayed_work_sync(&bp->fw_reset_task);
1171 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1173 if (!rxr->bnapi->in_reset) {
1174 rxr->bnapi->in_reset = true;
1175 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1176 bnxt_queue_sp_work(bp);
1178 rxr->rx_next_cons = 0xffff;
1181 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1183 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1184 u16 idx = agg_id & MAX_TPA_P5_MASK;
1186 if (test_bit(idx, map->agg_idx_bmap))
1187 idx = find_first_zero_bit(map->agg_idx_bmap,
1188 BNXT_AGG_IDX_BMAP_SIZE);
1189 __set_bit(idx, map->agg_idx_bmap);
1190 map->agg_id_tbl[agg_id] = idx;
1194 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1196 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1198 __clear_bit(idx, map->agg_idx_bmap);
1201 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1203 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1205 return map->agg_id_tbl[agg_id];
1208 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1209 struct rx_tpa_start_cmp *tpa_start,
1210 struct rx_tpa_start_cmp_ext *tpa_start1)
1212 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1213 struct bnxt_tpa_info *tpa_info;
1214 u16 cons, prod, agg_id;
1215 struct rx_bd *prod_bd;
1218 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1219 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1220 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1222 agg_id = TPA_START_AGG_ID(tpa_start);
1224 cons = tpa_start->rx_tpa_start_cmp_opaque;
1225 prod = rxr->rx_prod;
1226 cons_rx_buf = &rxr->rx_buf_ring[cons];
1227 prod_rx_buf = &rxr->rx_buf_ring[prod];
1228 tpa_info = &rxr->rx_tpa[agg_id];
1230 if (unlikely(cons != rxr->rx_next_cons ||
1231 TPA_START_ERROR(tpa_start))) {
1232 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1233 cons, rxr->rx_next_cons,
1234 TPA_START_ERROR_CODE(tpa_start1));
1235 bnxt_sched_reset(bp, rxr);
1238 /* Store cfa_code in tpa_info to use in tpa_end
1239 * completion processing.
1241 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1242 prod_rx_buf->data = tpa_info->data;
1243 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1245 mapping = tpa_info->mapping;
1246 prod_rx_buf->mapping = mapping;
1248 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1250 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1252 tpa_info->data = cons_rx_buf->data;
1253 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1254 cons_rx_buf->data = NULL;
1255 tpa_info->mapping = cons_rx_buf->mapping;
1258 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1259 RX_TPA_START_CMP_LEN_SHIFT;
1260 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1261 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1263 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1264 tpa_info->gso_type = SKB_GSO_TCPV4;
1265 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1266 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1267 tpa_info->gso_type = SKB_GSO_TCPV6;
1268 tpa_info->rss_hash =
1269 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1271 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1272 tpa_info->gso_type = 0;
1273 if (netif_msg_rx_err(bp))
1274 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1276 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1277 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1278 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1279 tpa_info->agg_count = 0;
1281 rxr->rx_prod = NEXT_RX(prod);
1282 cons = NEXT_RX(cons);
1283 rxr->rx_next_cons = NEXT_RX(cons);
1284 cons_rx_buf = &rxr->rx_buf_ring[cons];
1286 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1287 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1288 cons_rx_buf->data = NULL;
1291 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1294 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1298 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1300 struct udphdr *uh = NULL;
1302 if (ip_proto == htons(ETH_P_IP)) {
1303 struct iphdr *iph = (struct iphdr *)skb->data;
1305 if (iph->protocol == IPPROTO_UDP)
1306 uh = (struct udphdr *)(iph + 1);
1308 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1310 if (iph->nexthdr == IPPROTO_UDP)
1311 uh = (struct udphdr *)(iph + 1);
1315 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1317 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1322 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1323 int payload_off, int tcp_ts,
1324 struct sk_buff *skb)
1329 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1330 u32 hdr_info = tpa_info->hdr_info;
1331 bool loopback = false;
1333 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1334 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1335 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1337 /* If the packet is an internal loopback packet, the offsets will
1338 * have an extra 4 bytes.
1340 if (inner_mac_off == 4) {
1342 } else if (inner_mac_off > 4) {
1343 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1346 /* We only support inner iPv4/ipv6. If we don't see the
1347 * correct protocol ID, it must be a loopback packet where
1348 * the offsets are off by 4.
1350 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1354 /* internal loopback packet, subtract all offsets by 4 */
1360 nw_off = inner_ip_off - ETH_HLEN;
1361 skb_set_network_header(skb, nw_off);
1362 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1363 struct ipv6hdr *iph = ipv6_hdr(skb);
1365 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1366 len = skb->len - skb_transport_offset(skb);
1368 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1370 struct iphdr *iph = ip_hdr(skb);
1372 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1373 len = skb->len - skb_transport_offset(skb);
1375 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1378 if (inner_mac_off) { /* tunnel */
1379 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1382 bnxt_gro_tunnel(skb, proto);
1388 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1389 int payload_off, int tcp_ts,
1390 struct sk_buff *skb)
1393 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1394 u32 hdr_info = tpa_info->hdr_info;
1395 int iphdr_len, nw_off;
1397 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1398 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1399 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1401 nw_off = inner_ip_off - ETH_HLEN;
1402 skb_set_network_header(skb, nw_off);
1403 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1404 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1405 skb_set_transport_header(skb, nw_off + iphdr_len);
1407 if (inner_mac_off) { /* tunnel */
1408 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1411 bnxt_gro_tunnel(skb, proto);
1417 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1418 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1420 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1421 int payload_off, int tcp_ts,
1422 struct sk_buff *skb)
1426 int len, nw_off, tcp_opt_len = 0;
1431 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1434 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1436 skb_set_network_header(skb, nw_off);
1438 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1439 len = skb->len - skb_transport_offset(skb);
1441 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1442 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1443 struct ipv6hdr *iph;
1445 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1447 skb_set_network_header(skb, nw_off);
1448 iph = ipv6_hdr(skb);
1449 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1450 len = skb->len - skb_transport_offset(skb);
1452 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1454 dev_kfree_skb_any(skb);
1458 if (nw_off) /* tunnel */
1459 bnxt_gro_tunnel(skb, skb->protocol);
1464 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1465 struct bnxt_tpa_info *tpa_info,
1466 struct rx_tpa_end_cmp *tpa_end,
1467 struct rx_tpa_end_cmp_ext *tpa_end1,
1468 struct sk_buff *skb)
1474 segs = TPA_END_TPA_SEGS(tpa_end);
1478 NAPI_GRO_CB(skb)->count = segs;
1479 skb_shinfo(skb)->gso_size =
1480 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1481 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1482 if (bp->flags & BNXT_FLAG_CHIP_P5)
1483 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1485 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1486 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1488 tcp_gro_complete(skb);
1493 /* Given the cfa_code of a received packet determine which
1494 * netdev (vf-rep or PF) the packet is destined to.
1496 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1498 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1500 /* if vf-rep dev is NULL, the must belongs to the PF */
1501 return dev ? dev : bp->dev;
1504 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1505 struct bnxt_cp_ring_info *cpr,
1507 struct rx_tpa_end_cmp *tpa_end,
1508 struct rx_tpa_end_cmp_ext *tpa_end1,
1511 struct bnxt_napi *bnapi = cpr->bnapi;
1512 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1513 u8 *data_ptr, agg_bufs;
1515 struct bnxt_tpa_info *tpa_info;
1517 struct sk_buff *skb;
1518 u16 idx = 0, agg_id;
1522 if (unlikely(bnapi->in_reset)) {
1523 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1526 return ERR_PTR(-EBUSY);
1530 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1531 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1532 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1533 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1534 tpa_info = &rxr->rx_tpa[agg_id];
1535 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1536 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1537 agg_bufs, tpa_info->agg_count);
1538 agg_bufs = tpa_info->agg_count;
1540 tpa_info->agg_count = 0;
1541 *event |= BNXT_AGG_EVENT;
1542 bnxt_free_agg_idx(rxr, agg_id);
1544 gro = !!(bp->flags & BNXT_FLAG_GRO);
1546 agg_id = TPA_END_AGG_ID(tpa_end);
1547 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1548 tpa_info = &rxr->rx_tpa[agg_id];
1549 idx = RING_CMP(*raw_cons);
1551 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1552 return ERR_PTR(-EBUSY);
1554 *event |= BNXT_AGG_EVENT;
1555 idx = NEXT_CMP(idx);
1557 gro = !!TPA_END_GRO(tpa_end);
1559 data = tpa_info->data;
1560 data_ptr = tpa_info->data_ptr;
1562 len = tpa_info->len;
1563 mapping = tpa_info->mapping;
1565 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1566 bnxt_abort_tpa(cpr, idx, agg_bufs);
1567 if (agg_bufs > MAX_SKB_FRAGS)
1568 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1569 agg_bufs, (int)MAX_SKB_FRAGS);
1573 if (len <= bp->rx_copy_thresh) {
1574 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1576 bnxt_abort_tpa(cpr, idx, agg_bufs);
1581 dma_addr_t new_mapping;
1583 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1585 bnxt_abort_tpa(cpr, idx, agg_bufs);
1589 tpa_info->data = new_data;
1590 tpa_info->data_ptr = new_data + bp->rx_offset;
1591 tpa_info->mapping = new_mapping;
1593 skb = build_skb(data, 0);
1594 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1595 bp->rx_buf_use_size, bp->rx_dir,
1596 DMA_ATTR_WEAK_ORDERING);
1600 bnxt_abort_tpa(cpr, idx, agg_bufs);
1603 skb_reserve(skb, bp->rx_offset);
1608 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1610 /* Page reuse already handled by bnxt_rx_pages(). */
1616 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1618 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1619 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1621 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1622 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1623 u16 vlan_proto = tpa_info->metadata >>
1624 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1625 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1627 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1630 skb_checksum_none_assert(skb);
1631 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1632 skb->ip_summed = CHECKSUM_UNNECESSARY;
1634 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1638 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1643 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1644 struct rx_agg_cmp *rx_agg)
1646 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1647 struct bnxt_tpa_info *tpa_info;
1649 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1650 tpa_info = &rxr->rx_tpa[agg_id];
1651 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1652 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1655 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1656 struct sk_buff *skb)
1658 if (skb->dev != bp->dev) {
1659 /* this packet belongs to a vf-rep */
1660 bnxt_vf_rep_rx(bp, skb);
1663 skb_record_rx_queue(skb, bnapi->index);
1664 napi_gro_receive(&bnapi->napi, skb);
1667 /* returns the following:
1668 * 1 - 1 packet successfully received
1669 * 0 - successful TPA_START, packet not completed yet
1670 * -EBUSY - completion ring does not have all the agg buffers yet
1671 * -ENOMEM - packet aborted due to out of memory
1672 * -EIO - packet aborted due to hw error indicated in BD
1674 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1675 u32 *raw_cons, u8 *event)
1677 struct bnxt_napi *bnapi = cpr->bnapi;
1678 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1679 struct net_device *dev = bp->dev;
1680 struct rx_cmp *rxcmp;
1681 struct rx_cmp_ext *rxcmp1;
1682 u32 tmp_raw_cons = *raw_cons;
1683 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1684 struct bnxt_sw_rx_bd *rx_buf;
1686 u8 *data_ptr, agg_bufs, cmp_type;
1687 dma_addr_t dma_addr;
1688 struct sk_buff *skb;
1693 rxcmp = (struct rx_cmp *)
1694 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1696 cmp_type = RX_CMP_TYPE(rxcmp);
1698 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1699 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1700 goto next_rx_no_prod_no_len;
1703 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1704 cp_cons = RING_CMP(tmp_raw_cons);
1705 rxcmp1 = (struct rx_cmp_ext *)
1706 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1708 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1711 prod = rxr->rx_prod;
1713 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1714 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1715 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1717 *event |= BNXT_RX_EVENT;
1718 goto next_rx_no_prod_no_len;
1720 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1721 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1722 (struct rx_tpa_end_cmp *)rxcmp,
1723 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1730 bnxt_deliver_skb(bp, bnapi, skb);
1733 *event |= BNXT_RX_EVENT;
1734 goto next_rx_no_prod_no_len;
1737 cons = rxcmp->rx_cmp_opaque;
1738 if (unlikely(cons != rxr->rx_next_cons)) {
1739 int rc1 = bnxt_discard_rx(bp, cpr, raw_cons, rxcmp);
1741 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1742 cons, rxr->rx_next_cons);
1743 bnxt_sched_reset(bp, rxr);
1746 rx_buf = &rxr->rx_buf_ring[cons];
1747 data = rx_buf->data;
1748 data_ptr = rx_buf->data_ptr;
1751 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1752 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1755 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1758 cp_cons = NEXT_CMP(cp_cons);
1759 *event |= BNXT_AGG_EVENT;
1761 *event |= BNXT_RX_EVENT;
1763 rx_buf->data = NULL;
1764 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1765 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1767 bnxt_reuse_rx_data(rxr, cons, data);
1769 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1773 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1774 bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1775 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
1776 netdev_warn(bp->dev, "RX buffer error %x\n",
1778 bnxt_sched_reset(bp, rxr);
1781 goto next_rx_no_len;
1784 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1785 dma_addr = rx_buf->mapping;
1787 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1792 if (len <= bp->rx_copy_thresh) {
1793 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1794 bnxt_reuse_rx_data(rxr, cons, data);
1797 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1805 if (rx_buf->data_ptr == data_ptr)
1806 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1809 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1818 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1825 if (RX_CMP_HASH_VALID(rxcmp)) {
1826 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1827 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1829 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1830 if (hash_type != 1 && hash_type != 3)
1831 type = PKT_HASH_TYPE_L3;
1832 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1835 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1836 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1838 if ((rxcmp1->rx_cmp_flags2 &
1839 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1840 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1841 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1842 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1843 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1845 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1848 skb_checksum_none_assert(skb);
1849 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1850 if (dev->features & NETIF_F_RXCSUM) {
1851 skb->ip_summed = CHECKSUM_UNNECESSARY;
1852 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1855 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1856 if (dev->features & NETIF_F_RXCSUM)
1857 bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1861 bnxt_deliver_skb(bp, bnapi, skb);
1865 cpr->rx_packets += 1;
1866 cpr->rx_bytes += len;
1869 rxr->rx_prod = NEXT_RX(prod);
1870 rxr->rx_next_cons = NEXT_RX(cons);
1872 next_rx_no_prod_no_len:
1873 *raw_cons = tmp_raw_cons;
1878 /* In netpoll mode, if we are using a combined completion ring, we need to
1879 * discard the rx packets and recycle the buffers.
1881 static int bnxt_force_rx_discard(struct bnxt *bp,
1882 struct bnxt_cp_ring_info *cpr,
1883 u32 *raw_cons, u8 *event)
1885 u32 tmp_raw_cons = *raw_cons;
1886 struct rx_cmp_ext *rxcmp1;
1887 struct rx_cmp *rxcmp;
1891 cp_cons = RING_CMP(tmp_raw_cons);
1892 rxcmp = (struct rx_cmp *)
1893 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1895 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1896 cp_cons = RING_CMP(tmp_raw_cons);
1897 rxcmp1 = (struct rx_cmp_ext *)
1898 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1900 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1903 cmp_type = RX_CMP_TYPE(rxcmp);
1904 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1905 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1906 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1907 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1908 struct rx_tpa_end_cmp_ext *tpa_end1;
1910 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1911 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1912 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1914 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1917 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
1919 struct bnxt_fw_health *fw_health = bp->fw_health;
1920 u32 reg = fw_health->regs[reg_idx];
1921 u32 reg_type, reg_off, val = 0;
1923 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
1924 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
1926 case BNXT_FW_HEALTH_REG_TYPE_CFG:
1927 pci_read_config_dword(bp->pdev, reg_off, &val);
1929 case BNXT_FW_HEALTH_REG_TYPE_GRC:
1930 reg_off = fw_health->mapped_regs[reg_idx];
1932 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
1933 val = readl(bp->bar0 + reg_off);
1935 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
1936 val = readl(bp->bar1 + reg_off);
1939 if (reg_idx == BNXT_FW_RESET_INPROG_REG)
1940 val &= fw_health->fw_reset_inprog_reg_mask;
1944 #define BNXT_GET_EVENT_PORT(data) \
1946 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1948 static int bnxt_async_event_process(struct bnxt *bp,
1949 struct hwrm_async_event_cmpl *cmpl)
1951 u16 event_id = le16_to_cpu(cmpl->event_id);
1953 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1955 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1956 u32 data1 = le32_to_cpu(cmpl->event_data1);
1957 struct bnxt_link_info *link_info = &bp->link_info;
1960 goto async_event_process_exit;
1962 /* print unsupported speed warning in forced speed mode only */
1963 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1964 (data1 & 0x20000)) {
1965 u16 fw_speed = link_info->force_link_speed;
1966 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1968 if (speed != SPEED_UNKNOWN)
1969 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1972 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1975 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
1976 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
1977 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
1979 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1980 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1982 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1983 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1985 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1986 u32 data1 = le32_to_cpu(cmpl->event_data1);
1987 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1992 if (bp->pf.port_id != port_id)
1995 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1998 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2000 goto async_event_process_exit;
2001 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2003 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2004 u32 data1 = le32_to_cpu(cmpl->event_data1);
2007 goto async_event_process_exit;
2009 bp->fw_reset_timestamp = jiffies;
2010 bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2011 if (!bp->fw_reset_min_dsecs)
2012 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2013 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2014 if (!bp->fw_reset_max_dsecs)
2015 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2016 if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2017 netdev_warn(bp->dev, "Firmware fatal reset event received\n");
2018 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2020 netdev_warn(bp->dev, "Firmware non-fatal reset event received, max wait time %d msec\n",
2021 bp->fw_reset_max_dsecs * 100);
2023 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2026 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2027 struct bnxt_fw_health *fw_health = bp->fw_health;
2028 u32 data1 = le32_to_cpu(cmpl->event_data1);
2031 goto async_event_process_exit;
2033 fw_health->enabled = EVENT_DATA1_RECOVERY_ENABLED(data1);
2034 fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2035 if (!fw_health->enabled)
2038 if (netif_msg_drv(bp))
2039 netdev_info(bp->dev, "Error recovery info: error recovery[%d], master[%d], reset count[0x%x], health status: 0x%x\n",
2040 fw_health->enabled, fw_health->master,
2041 bnxt_fw_health_readl(bp,
2042 BNXT_FW_RESET_CNT_REG),
2043 bnxt_fw_health_readl(bp,
2044 BNXT_FW_HEALTH_REG));
2045 fw_health->tmr_multiplier =
2046 DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2047 bp->current_interval * 10);
2048 fw_health->tmr_counter = fw_health->tmr_multiplier;
2049 fw_health->last_fw_heartbeat =
2050 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2051 fw_health->last_fw_reset_cnt =
2052 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2053 goto async_event_process_exit;
2056 goto async_event_process_exit;
2058 bnxt_queue_sp_work(bp);
2059 async_event_process_exit:
2060 bnxt_ulp_async_events(bp, cmpl);
2064 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2066 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2067 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2068 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2069 (struct hwrm_fwd_req_cmpl *)txcmp;
2071 switch (cmpl_type) {
2072 case CMPL_BASE_TYPE_HWRM_DONE:
2073 seq_id = le16_to_cpu(h_cmpl->sequence_id);
2074 if (seq_id == bp->hwrm_intr_seq_id)
2075 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2077 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2080 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2081 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2083 if ((vf_id < bp->pf.first_vf_id) ||
2084 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2085 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2090 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2091 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2092 bnxt_queue_sp_work(bp);
2095 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2096 bnxt_async_event_process(bp,
2097 (struct hwrm_async_event_cmpl *)txcmp);
2106 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2108 struct bnxt_napi *bnapi = dev_instance;
2109 struct bnxt *bp = bnapi->bp;
2110 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2111 u32 cons = RING_CMP(cpr->cp_raw_cons);
2114 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2115 napi_schedule(&bnapi->napi);
2119 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2121 u32 raw_cons = cpr->cp_raw_cons;
2122 u16 cons = RING_CMP(raw_cons);
2123 struct tx_cmp *txcmp;
2125 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2127 return TX_CMP_VALID(txcmp, raw_cons);
2130 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2132 struct bnxt_napi *bnapi = dev_instance;
2133 struct bnxt *bp = bnapi->bp;
2134 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2135 u32 cons = RING_CMP(cpr->cp_raw_cons);
2138 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2140 if (!bnxt_has_work(bp, cpr)) {
2141 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2142 /* return if erroneous interrupt */
2143 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2147 /* disable ring IRQ */
2148 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2150 /* Return here if interrupt is shared and is disabled. */
2151 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2154 napi_schedule(&bnapi->napi);
2158 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2161 struct bnxt_napi *bnapi = cpr->bnapi;
2162 u32 raw_cons = cpr->cp_raw_cons;
2167 struct tx_cmp *txcmp;
2169 cpr->has_more_work = 0;
2170 cpr->had_work_done = 1;
2174 cons = RING_CMP(raw_cons);
2175 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2177 if (!TX_CMP_VALID(txcmp, raw_cons))
2180 /* The valid test of the entry must be done first before
2181 * reading any further.
2184 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2186 /* return full budget so NAPI will complete. */
2187 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
2189 raw_cons = NEXT_RAW_CMP(raw_cons);
2191 cpr->has_more_work = 1;
2194 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2196 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2198 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2200 if (likely(rc >= 0))
2202 /* Increment rx_pkts when rc is -ENOMEM to count towards
2203 * the NAPI budget. Otherwise, we may potentially loop
2204 * here forever if we consistently cannot allocate
2207 else if (rc == -ENOMEM && budget)
2209 else if (rc == -EBUSY) /* partial completion */
2211 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2212 CMPL_BASE_TYPE_HWRM_DONE) ||
2213 (TX_CMP_TYPE(txcmp) ==
2214 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2215 (TX_CMP_TYPE(txcmp) ==
2216 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2217 bnxt_hwrm_handler(bp, txcmp);
2219 raw_cons = NEXT_RAW_CMP(raw_cons);
2221 if (rx_pkts && rx_pkts == budget) {
2222 cpr->has_more_work = 1;
2227 if (event & BNXT_REDIRECT_EVENT)
2230 if (event & BNXT_TX_EVENT) {
2231 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2232 u16 prod = txr->tx_prod;
2234 /* Sync BD data before updating doorbell */
2237 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2240 cpr->cp_raw_cons = raw_cons;
2241 bnapi->tx_pkts += tx_pkts;
2242 bnapi->events |= event;
2246 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2248 if (bnapi->tx_pkts) {
2249 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2253 if (bnapi->events & BNXT_RX_EVENT) {
2254 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2256 if (bnapi->events & BNXT_AGG_EVENT)
2257 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2258 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2263 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2266 struct bnxt_napi *bnapi = cpr->bnapi;
2269 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2271 /* ACK completion ring before freeing tx ring and producing new
2272 * buffers in rx/agg rings to prevent overflowing the completion
2275 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2277 __bnxt_poll_work_done(bp, bnapi);
2281 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2283 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2284 struct bnxt *bp = bnapi->bp;
2285 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2286 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2287 struct tx_cmp *txcmp;
2288 struct rx_cmp_ext *rxcmp1;
2289 u32 cp_cons, tmp_raw_cons;
2290 u32 raw_cons = cpr->cp_raw_cons;
2297 cp_cons = RING_CMP(raw_cons);
2298 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2300 if (!TX_CMP_VALID(txcmp, raw_cons))
2303 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2304 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2305 cp_cons = RING_CMP(tmp_raw_cons);
2306 rxcmp1 = (struct rx_cmp_ext *)
2307 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2309 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2312 /* force an error to recycle the buffer */
2313 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2314 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2316 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2317 if (likely(rc == -EIO) && budget)
2319 else if (rc == -EBUSY) /* partial completion */
2321 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2322 CMPL_BASE_TYPE_HWRM_DONE)) {
2323 bnxt_hwrm_handler(bp, txcmp);
2326 "Invalid completion received on special ring\n");
2328 raw_cons = NEXT_RAW_CMP(raw_cons);
2330 if (rx_pkts == budget)
2334 cpr->cp_raw_cons = raw_cons;
2335 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2336 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2338 if (event & BNXT_AGG_EVENT)
2339 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2341 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2342 napi_complete_done(napi, rx_pkts);
2343 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2348 static int bnxt_poll(struct napi_struct *napi, int budget)
2350 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2351 struct bnxt *bp = bnapi->bp;
2352 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2356 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2358 if (work_done >= budget) {
2360 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2364 if (!bnxt_has_work(bp, cpr)) {
2365 if (napi_complete_done(napi, work_done))
2366 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2370 if (bp->flags & BNXT_FLAG_DIM) {
2371 struct dim_sample dim_sample = {};
2373 dim_update_sample(cpr->event_ctr,
2377 net_dim(&cpr->dim, dim_sample);
2382 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2384 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2385 int i, work_done = 0;
2387 for (i = 0; i < 2; i++) {
2388 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2391 work_done += __bnxt_poll_work(bp, cpr2,
2392 budget - work_done);
2393 cpr->has_more_work |= cpr2->has_more_work;
2399 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2402 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2405 for (i = 0; i < 2; i++) {
2406 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2407 struct bnxt_db_info *db;
2409 if (cpr2 && cpr2->had_work_done) {
2411 writeq(db->db_key64 | dbr_type |
2412 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2413 cpr2->had_work_done = 0;
2416 __bnxt_poll_work_done(bp, bnapi);
2419 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2421 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2422 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2423 u32 raw_cons = cpr->cp_raw_cons;
2424 struct bnxt *bp = bnapi->bp;
2425 struct nqe_cn *nqcmp;
2429 if (cpr->has_more_work) {
2430 cpr->has_more_work = 0;
2431 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2434 cons = RING_CMP(raw_cons);
2435 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2437 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2438 if (cpr->has_more_work)
2441 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2442 cpr->cp_raw_cons = raw_cons;
2443 if (napi_complete_done(napi, work_done))
2444 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2449 /* The valid test of the entry must be done first before
2450 * reading any further.
2454 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2455 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2456 struct bnxt_cp_ring_info *cpr2;
2458 cpr2 = cpr->cp_ring_arr[idx];
2459 work_done += __bnxt_poll_work(bp, cpr2,
2460 budget - work_done);
2461 cpr->has_more_work |= cpr2->has_more_work;
2463 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2465 raw_cons = NEXT_RAW_CMP(raw_cons);
2467 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2468 if (raw_cons != cpr->cp_raw_cons) {
2469 cpr->cp_raw_cons = raw_cons;
2470 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2475 static void bnxt_free_tx_skbs(struct bnxt *bp)
2478 struct pci_dev *pdev = bp->pdev;
2483 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2484 for (i = 0; i < bp->tx_nr_rings; i++) {
2485 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2488 for (j = 0; j < max_idx;) {
2489 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2490 struct sk_buff *skb;
2493 if (i < bp->tx_nr_rings_xdp &&
2494 tx_buf->action == XDP_REDIRECT) {
2495 dma_unmap_single(&pdev->dev,
2496 dma_unmap_addr(tx_buf, mapping),
2497 dma_unmap_len(tx_buf, len),
2499 xdp_return_frame(tx_buf->xdpf);
2501 tx_buf->xdpf = NULL;
2514 if (tx_buf->is_push) {
2520 dma_unmap_single(&pdev->dev,
2521 dma_unmap_addr(tx_buf, mapping),
2525 last = tx_buf->nr_frags;
2527 for (k = 0; k < last; k++, j++) {
2528 int ring_idx = j & bp->tx_ring_mask;
2529 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2531 tx_buf = &txr->tx_buf_ring[ring_idx];
2534 dma_unmap_addr(tx_buf, mapping),
2535 skb_frag_size(frag), PCI_DMA_TODEVICE);
2539 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2543 static void bnxt_free_rx_skbs(struct bnxt *bp)
2545 int i, max_idx, max_agg_idx;
2546 struct pci_dev *pdev = bp->pdev;
2551 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2552 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2553 for (i = 0; i < bp->rx_nr_rings; i++) {
2554 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2555 struct bnxt_tpa_idx_map *map;
2559 for (j = 0; j < bp->max_tpa; j++) {
2560 struct bnxt_tpa_info *tpa_info =
2562 u8 *data = tpa_info->data;
2567 dma_unmap_single_attrs(&pdev->dev,
2569 bp->rx_buf_use_size,
2571 DMA_ATTR_WEAK_ORDERING);
2573 tpa_info->data = NULL;
2579 for (j = 0; j < max_idx; j++) {
2580 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2581 dma_addr_t mapping = rx_buf->mapping;
2582 void *data = rx_buf->data;
2587 rx_buf->data = NULL;
2589 if (BNXT_RX_PAGE_MODE(bp)) {
2590 mapping -= bp->rx_dma_offset;
2591 dma_unmap_page_attrs(&pdev->dev, mapping,
2592 PAGE_SIZE, bp->rx_dir,
2593 DMA_ATTR_WEAK_ORDERING);
2594 page_pool_recycle_direct(rxr->page_pool, data);
2596 dma_unmap_single_attrs(&pdev->dev, mapping,
2597 bp->rx_buf_use_size,
2599 DMA_ATTR_WEAK_ORDERING);
2604 for (j = 0; j < max_agg_idx; j++) {
2605 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2606 &rxr->rx_agg_ring[j];
2607 struct page *page = rx_agg_buf->page;
2612 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2615 DMA_ATTR_WEAK_ORDERING);
2617 rx_agg_buf->page = NULL;
2618 __clear_bit(j, rxr->rx_agg_bmap);
2623 __free_page(rxr->rx_page);
2624 rxr->rx_page = NULL;
2626 map = rxr->rx_tpa_idx_map;
2628 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2632 static void bnxt_free_skbs(struct bnxt *bp)
2634 bnxt_free_tx_skbs(bp);
2635 bnxt_free_rx_skbs(bp);
2638 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2640 struct pci_dev *pdev = bp->pdev;
2643 for (i = 0; i < rmem->nr_pages; i++) {
2644 if (!rmem->pg_arr[i])
2647 dma_free_coherent(&pdev->dev, rmem->page_size,
2648 rmem->pg_arr[i], rmem->dma_arr[i]);
2650 rmem->pg_arr[i] = NULL;
2653 size_t pg_tbl_size = rmem->nr_pages * 8;
2655 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2656 pg_tbl_size = rmem->page_size;
2657 dma_free_coherent(&pdev->dev, pg_tbl_size,
2658 rmem->pg_tbl, rmem->pg_tbl_map);
2659 rmem->pg_tbl = NULL;
2661 if (rmem->vmem_size && *rmem->vmem) {
2667 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2669 struct pci_dev *pdev = bp->pdev;
2673 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2674 valid_bit = PTU_PTE_VALID;
2675 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2676 size_t pg_tbl_size = rmem->nr_pages * 8;
2678 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2679 pg_tbl_size = rmem->page_size;
2680 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2687 for (i = 0; i < rmem->nr_pages; i++) {
2688 u64 extra_bits = valid_bit;
2690 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2694 if (!rmem->pg_arr[i])
2698 memset(rmem->pg_arr[i], rmem->init_val,
2700 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2701 if (i == rmem->nr_pages - 2 &&
2702 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2703 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2704 else if (i == rmem->nr_pages - 1 &&
2705 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2706 extra_bits |= PTU_PTE_LAST;
2708 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2712 if (rmem->vmem_size) {
2713 *rmem->vmem = vzalloc(rmem->vmem_size);
2720 static void bnxt_free_tpa_info(struct bnxt *bp)
2724 for (i = 0; i < bp->rx_nr_rings; i++) {
2725 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2727 kfree(rxr->rx_tpa_idx_map);
2728 rxr->rx_tpa_idx_map = NULL;
2730 kfree(rxr->rx_tpa[0].agg_arr);
2731 rxr->rx_tpa[0].agg_arr = NULL;
2738 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2740 int i, j, total_aggs = 0;
2742 bp->max_tpa = MAX_TPA;
2743 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2744 if (!bp->max_tpa_v2)
2746 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2747 total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2750 for (i = 0; i < bp->rx_nr_rings; i++) {
2751 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2752 struct rx_agg_cmp *agg;
2754 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2759 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2761 agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2762 rxr->rx_tpa[0].agg_arr = agg;
2765 for (j = 1; j < bp->max_tpa; j++)
2766 rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2767 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2769 if (!rxr->rx_tpa_idx_map)
2775 static void bnxt_free_rx_rings(struct bnxt *bp)
2782 bnxt_free_tpa_info(bp);
2783 for (i = 0; i < bp->rx_nr_rings; i++) {
2784 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2785 struct bnxt_ring_struct *ring;
2788 bpf_prog_put(rxr->xdp_prog);
2790 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2791 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2793 page_pool_destroy(rxr->page_pool);
2794 rxr->page_pool = NULL;
2796 kfree(rxr->rx_agg_bmap);
2797 rxr->rx_agg_bmap = NULL;
2799 ring = &rxr->rx_ring_struct;
2800 bnxt_free_ring(bp, &ring->ring_mem);
2802 ring = &rxr->rx_agg_ring_struct;
2803 bnxt_free_ring(bp, &ring->ring_mem);
2807 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2808 struct bnxt_rx_ring_info *rxr)
2810 struct page_pool_params pp = { 0 };
2812 pp.pool_size = bp->rx_ring_size;
2813 pp.nid = dev_to_node(&bp->pdev->dev);
2814 pp.dev = &bp->pdev->dev;
2815 pp.dma_dir = DMA_BIDIRECTIONAL;
2817 rxr->page_pool = page_pool_create(&pp);
2818 if (IS_ERR(rxr->page_pool)) {
2819 int err = PTR_ERR(rxr->page_pool);
2821 rxr->page_pool = NULL;
2827 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2829 int i, rc = 0, agg_rings = 0;
2834 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2837 for (i = 0; i < bp->rx_nr_rings; i++) {
2838 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2839 struct bnxt_ring_struct *ring;
2841 ring = &rxr->rx_ring_struct;
2843 rc = bnxt_alloc_rx_page_pool(bp, rxr);
2847 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2851 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2855 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2859 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2867 ring = &rxr->rx_agg_ring_struct;
2868 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2873 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2874 mem_size = rxr->rx_agg_bmap_size / 8;
2875 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2876 if (!rxr->rx_agg_bmap)
2880 if (bp->flags & BNXT_FLAG_TPA)
2881 rc = bnxt_alloc_tpa_info(bp);
2885 static void bnxt_free_tx_rings(struct bnxt *bp)
2888 struct pci_dev *pdev = bp->pdev;
2893 for (i = 0; i < bp->tx_nr_rings; i++) {
2894 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2895 struct bnxt_ring_struct *ring;
2898 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2899 txr->tx_push, txr->tx_push_mapping);
2900 txr->tx_push = NULL;
2903 ring = &txr->tx_ring_struct;
2905 bnxt_free_ring(bp, &ring->ring_mem);
2909 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2912 struct pci_dev *pdev = bp->pdev;
2914 bp->tx_push_size = 0;
2915 if (bp->tx_push_thresh) {
2918 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2919 bp->tx_push_thresh);
2921 if (push_size > 256) {
2923 bp->tx_push_thresh = 0;
2926 bp->tx_push_size = push_size;
2929 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2930 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2931 struct bnxt_ring_struct *ring;
2934 ring = &txr->tx_ring_struct;
2936 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2940 ring->grp_idx = txr->bnapi->index;
2941 if (bp->tx_push_size) {
2944 /* One pre-allocated DMA buffer to backup
2947 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2949 &txr->tx_push_mapping,
2955 mapping = txr->tx_push_mapping +
2956 sizeof(struct tx_push_bd);
2957 txr->data_mapping = cpu_to_le64(mapping);
2959 qidx = bp->tc_to_qidx[j];
2960 ring->queue_id = bp->q_info[qidx].queue_id;
2961 if (i < bp->tx_nr_rings_xdp)
2963 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2969 static void bnxt_free_cp_rings(struct bnxt *bp)
2976 for (i = 0; i < bp->cp_nr_rings; i++) {
2977 struct bnxt_napi *bnapi = bp->bnapi[i];
2978 struct bnxt_cp_ring_info *cpr;
2979 struct bnxt_ring_struct *ring;
2985 cpr = &bnapi->cp_ring;
2986 ring = &cpr->cp_ring_struct;
2988 bnxt_free_ring(bp, &ring->ring_mem);
2990 for (j = 0; j < 2; j++) {
2991 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2994 ring = &cpr2->cp_ring_struct;
2995 bnxt_free_ring(bp, &ring->ring_mem);
2997 cpr->cp_ring_arr[j] = NULL;
3003 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3005 struct bnxt_ring_mem_info *rmem;
3006 struct bnxt_ring_struct *ring;
3007 struct bnxt_cp_ring_info *cpr;
3010 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3014 ring = &cpr->cp_ring_struct;
3015 rmem = &ring->ring_mem;
3016 rmem->nr_pages = bp->cp_nr_pages;
3017 rmem->page_size = HW_CMPD_RING_SIZE;
3018 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3019 rmem->dma_arr = cpr->cp_desc_mapping;
3020 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3021 rc = bnxt_alloc_ring(bp, rmem);
3023 bnxt_free_ring(bp, rmem);
3030 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3032 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3033 int i, rc, ulp_base_vec, ulp_msix;
3035 ulp_msix = bnxt_get_ulp_msix_num(bp);
3036 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3037 for (i = 0; i < bp->cp_nr_rings; i++) {
3038 struct bnxt_napi *bnapi = bp->bnapi[i];
3039 struct bnxt_cp_ring_info *cpr;
3040 struct bnxt_ring_struct *ring;
3045 cpr = &bnapi->cp_ring;
3047 ring = &cpr->cp_ring_struct;
3049 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3053 if (ulp_msix && i >= ulp_base_vec)
3054 ring->map_idx = i + ulp_msix;
3058 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3061 if (i < bp->rx_nr_rings) {
3062 struct bnxt_cp_ring_info *cpr2 =
3063 bnxt_alloc_cp_sub_ring(bp);
3065 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3068 cpr2->bnapi = bnapi;
3070 if ((sh && i < bp->tx_nr_rings) ||
3071 (!sh && i >= bp->rx_nr_rings)) {
3072 struct bnxt_cp_ring_info *cpr2 =
3073 bnxt_alloc_cp_sub_ring(bp);
3075 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3078 cpr2->bnapi = bnapi;
3084 static void bnxt_init_ring_struct(struct bnxt *bp)
3088 for (i = 0; i < bp->cp_nr_rings; i++) {
3089 struct bnxt_napi *bnapi = bp->bnapi[i];
3090 struct bnxt_ring_mem_info *rmem;
3091 struct bnxt_cp_ring_info *cpr;
3092 struct bnxt_rx_ring_info *rxr;
3093 struct bnxt_tx_ring_info *txr;
3094 struct bnxt_ring_struct *ring;
3099 cpr = &bnapi->cp_ring;
3100 ring = &cpr->cp_ring_struct;
3101 rmem = &ring->ring_mem;
3102 rmem->nr_pages = bp->cp_nr_pages;
3103 rmem->page_size = HW_CMPD_RING_SIZE;
3104 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3105 rmem->dma_arr = cpr->cp_desc_mapping;
3106 rmem->vmem_size = 0;
3108 rxr = bnapi->rx_ring;
3112 ring = &rxr->rx_ring_struct;
3113 rmem = &ring->ring_mem;
3114 rmem->nr_pages = bp->rx_nr_pages;
3115 rmem->page_size = HW_RXBD_RING_SIZE;
3116 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3117 rmem->dma_arr = rxr->rx_desc_mapping;
3118 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3119 rmem->vmem = (void **)&rxr->rx_buf_ring;
3121 ring = &rxr->rx_agg_ring_struct;
3122 rmem = &ring->ring_mem;
3123 rmem->nr_pages = bp->rx_agg_nr_pages;
3124 rmem->page_size = HW_RXBD_RING_SIZE;
3125 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3126 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3127 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3128 rmem->vmem = (void **)&rxr->rx_agg_ring;
3131 txr = bnapi->tx_ring;
3135 ring = &txr->tx_ring_struct;
3136 rmem = &ring->ring_mem;
3137 rmem->nr_pages = bp->tx_nr_pages;
3138 rmem->page_size = HW_RXBD_RING_SIZE;
3139 rmem->pg_arr = (void **)txr->tx_desc_ring;
3140 rmem->dma_arr = txr->tx_desc_mapping;
3141 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3142 rmem->vmem = (void **)&txr->tx_buf_ring;
3146 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3150 struct rx_bd **rx_buf_ring;
3152 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3153 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3157 rxbd = rx_buf_ring[i];
3161 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3162 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3163 rxbd->rx_bd_opaque = prod;
3168 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3170 struct net_device *dev = bp->dev;
3171 struct bnxt_rx_ring_info *rxr;
3172 struct bnxt_ring_struct *ring;
3176 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3177 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3179 if (NET_IP_ALIGN == 2)
3180 type |= RX_BD_FLAGS_SOP;
3182 rxr = &bp->rx_ring[ring_nr];
3183 ring = &rxr->rx_ring_struct;
3184 bnxt_init_rxbd_pages(ring, type);
3186 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3187 bpf_prog_add(bp->xdp_prog, 1);
3188 rxr->xdp_prog = bp->xdp_prog;
3190 prod = rxr->rx_prod;
3191 for (i = 0; i < bp->rx_ring_size; i++) {
3192 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
3193 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3194 ring_nr, i, bp->rx_ring_size);
3197 prod = NEXT_RX(prod);
3199 rxr->rx_prod = prod;
3200 ring->fw_ring_id = INVALID_HW_RING_ID;
3202 ring = &rxr->rx_agg_ring_struct;
3203 ring->fw_ring_id = INVALID_HW_RING_ID;
3205 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3208 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3209 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3211 bnxt_init_rxbd_pages(ring, type);
3213 prod = rxr->rx_agg_prod;
3214 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3215 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
3216 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3217 ring_nr, i, bp->rx_ring_size);
3220 prod = NEXT_RX_AGG(prod);
3222 rxr->rx_agg_prod = prod;
3224 if (bp->flags & BNXT_FLAG_TPA) {
3229 for (i = 0; i < bp->max_tpa; i++) {
3230 data = __bnxt_alloc_rx_data(bp, &mapping,
3235 rxr->rx_tpa[i].data = data;
3236 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3237 rxr->rx_tpa[i].mapping = mapping;
3240 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
3248 static void bnxt_init_cp_rings(struct bnxt *bp)
3252 for (i = 0; i < bp->cp_nr_rings; i++) {
3253 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3254 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3256 ring->fw_ring_id = INVALID_HW_RING_ID;
3257 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3258 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3259 for (j = 0; j < 2; j++) {
3260 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3265 ring = &cpr2->cp_ring_struct;
3266 ring->fw_ring_id = INVALID_HW_RING_ID;
3267 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3268 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3273 static int bnxt_init_rx_rings(struct bnxt *bp)
3277 if (BNXT_RX_PAGE_MODE(bp)) {
3278 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3279 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3281 bp->rx_offset = BNXT_RX_OFFSET;
3282 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3285 for (i = 0; i < bp->rx_nr_rings; i++) {
3286 rc = bnxt_init_one_rx_ring(bp, i);
3294 static int bnxt_init_tx_rings(struct bnxt *bp)
3298 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3301 for (i = 0; i < bp->tx_nr_rings; i++) {
3302 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3303 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3305 ring->fw_ring_id = INVALID_HW_RING_ID;
3311 static void bnxt_free_ring_grps(struct bnxt *bp)
3313 kfree(bp->grp_info);
3314 bp->grp_info = NULL;
3317 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3322 bp->grp_info = kcalloc(bp->cp_nr_rings,
3323 sizeof(struct bnxt_ring_grp_info),
3328 for (i = 0; i < bp->cp_nr_rings; i++) {
3330 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3331 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3332 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3333 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3334 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3339 static void bnxt_free_vnics(struct bnxt *bp)
3341 kfree(bp->vnic_info);
3342 bp->vnic_info = NULL;
3346 static int bnxt_alloc_vnics(struct bnxt *bp)
3350 #ifdef CONFIG_RFS_ACCEL
3351 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3352 num_vnics += bp->rx_nr_rings;
3355 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3358 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3363 bp->nr_vnics = num_vnics;
3367 static void bnxt_init_vnics(struct bnxt *bp)
3371 for (i = 0; i < bp->nr_vnics; i++) {
3372 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3375 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3376 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3377 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3379 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3381 if (bp->vnic_info[i].rss_hash_key) {
3383 prandom_bytes(vnic->rss_hash_key,
3386 memcpy(vnic->rss_hash_key,
3387 bp->vnic_info[0].rss_hash_key,
3393 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3397 pages = ring_size / desc_per_pg;
3404 while (pages & (pages - 1))
3410 void bnxt_set_tpa_flags(struct bnxt *bp)
3412 bp->flags &= ~BNXT_FLAG_TPA;
3413 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3415 if (bp->dev->features & NETIF_F_LRO)
3416 bp->flags |= BNXT_FLAG_LRO;
3417 else if (bp->dev->features & NETIF_F_GRO_HW)
3418 bp->flags |= BNXT_FLAG_GRO;
3421 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3424 void bnxt_set_ring_params(struct bnxt *bp)
3426 u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3427 u32 agg_factor = 0, agg_ring_size = 0;
3429 /* 8 for CRC and VLAN */
3430 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3432 rx_space = rx_size + NET_SKB_PAD +
3433 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3435 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3436 ring_size = bp->rx_ring_size;
3437 bp->rx_agg_ring_size = 0;
3438 bp->rx_agg_nr_pages = 0;
3440 if (bp->flags & BNXT_FLAG_TPA)
3441 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3443 bp->flags &= ~BNXT_FLAG_JUMBO;
3444 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3447 bp->flags |= BNXT_FLAG_JUMBO;
3448 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3449 if (jumbo_factor > agg_factor)
3450 agg_factor = jumbo_factor;
3452 agg_ring_size = ring_size * agg_factor;
3454 if (agg_ring_size) {
3455 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3457 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3458 u32 tmp = agg_ring_size;
3460 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3461 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3462 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3463 tmp, agg_ring_size);
3465 bp->rx_agg_ring_size = agg_ring_size;
3466 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3467 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3468 rx_space = rx_size + NET_SKB_PAD +
3469 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3472 bp->rx_buf_use_size = rx_size;
3473 bp->rx_buf_size = rx_space;
3475 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3476 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3478 ring_size = bp->tx_ring_size;
3479 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3480 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3482 max_rx_cmpl = bp->rx_ring_size;
3483 /* MAX TPA needs to be added because TPA_START completions are
3484 * immediately recycled, so the TPA completions are not bound by
3487 if (bp->flags & BNXT_FLAG_TPA)
3488 max_rx_cmpl += bp->max_tpa;
3489 /* RX and TPA completions are 32-byte, all others are 16-byte */
3490 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3491 bp->cp_ring_size = ring_size;
3493 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3494 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3495 bp->cp_nr_pages = MAX_CP_PAGES;
3496 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3497 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3498 ring_size, bp->cp_ring_size);
3500 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3501 bp->cp_ring_mask = bp->cp_bit - 1;
3504 /* Changing allocation mode of RX rings.
3505 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3507 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3510 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3513 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3514 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3515 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3516 bp->rx_dir = DMA_BIDIRECTIONAL;
3517 bp->rx_skb_func = bnxt_rx_page_skb;
3518 /* Disable LRO or GRO_HW */
3519 netdev_update_features(bp->dev);
3521 bp->dev->max_mtu = bp->max_mtu;
3522 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3523 bp->rx_dir = DMA_FROM_DEVICE;
3524 bp->rx_skb_func = bnxt_rx_skb;
3529 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3532 struct bnxt_vnic_info *vnic;
3533 struct pci_dev *pdev = bp->pdev;
3538 for (i = 0; i < bp->nr_vnics; i++) {
3539 vnic = &bp->vnic_info[i];
3541 kfree(vnic->fw_grp_ids);
3542 vnic->fw_grp_ids = NULL;
3544 kfree(vnic->uc_list);
3545 vnic->uc_list = NULL;
3547 if (vnic->mc_list) {
3548 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3549 vnic->mc_list, vnic->mc_list_mapping);
3550 vnic->mc_list = NULL;
3553 if (vnic->rss_table) {
3554 dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3556 vnic->rss_table_dma_addr);
3557 vnic->rss_table = NULL;
3560 vnic->rss_hash_key = NULL;
3565 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3567 int i, rc = 0, size;
3568 struct bnxt_vnic_info *vnic;
3569 struct pci_dev *pdev = bp->pdev;
3572 for (i = 0; i < bp->nr_vnics; i++) {
3573 vnic = &bp->vnic_info[i];
3575 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3576 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3579 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3580 if (!vnic->uc_list) {
3587 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3588 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3590 dma_alloc_coherent(&pdev->dev,
3592 &vnic->mc_list_mapping,
3594 if (!vnic->mc_list) {
3600 if (bp->flags & BNXT_FLAG_CHIP_P5)
3601 goto vnic_skip_grps;
3603 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3604 max_rings = bp->rx_nr_rings;
3608 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3609 if (!vnic->fw_grp_ids) {
3614 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3615 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3618 /* Allocate rss table and hash key */
3619 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3620 if (bp->flags & BNXT_FLAG_CHIP_P5)
3621 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3623 vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3624 vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3625 vnic->rss_table_size,
3626 &vnic->rss_table_dma_addr,
3628 if (!vnic->rss_table) {
3633 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3634 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3642 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3644 struct pci_dev *pdev = bp->pdev;
3646 if (bp->hwrm_cmd_resp_addr) {
3647 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3648 bp->hwrm_cmd_resp_dma_addr);
3649 bp->hwrm_cmd_resp_addr = NULL;
3652 if (bp->hwrm_cmd_kong_resp_addr) {
3653 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3654 bp->hwrm_cmd_kong_resp_addr,
3655 bp->hwrm_cmd_kong_resp_dma_addr);
3656 bp->hwrm_cmd_kong_resp_addr = NULL;
3660 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3662 struct pci_dev *pdev = bp->pdev;
3664 if (bp->hwrm_cmd_kong_resp_addr)
3667 bp->hwrm_cmd_kong_resp_addr =
3668 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3669 &bp->hwrm_cmd_kong_resp_dma_addr,
3671 if (!bp->hwrm_cmd_kong_resp_addr)
3677 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3679 struct pci_dev *pdev = bp->pdev;
3681 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3682 &bp->hwrm_cmd_resp_dma_addr,
3684 if (!bp->hwrm_cmd_resp_addr)
3690 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3692 if (bp->hwrm_short_cmd_req_addr) {
3693 struct pci_dev *pdev = bp->pdev;
3695 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3696 bp->hwrm_short_cmd_req_addr,
3697 bp->hwrm_short_cmd_req_dma_addr);
3698 bp->hwrm_short_cmd_req_addr = NULL;
3702 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3704 struct pci_dev *pdev = bp->pdev;
3706 if (bp->hwrm_short_cmd_req_addr)
3709 bp->hwrm_short_cmd_req_addr =
3710 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3711 &bp->hwrm_short_cmd_req_dma_addr,
3713 if (!bp->hwrm_short_cmd_req_addr)
3719 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3721 kfree(stats->hw_masks);
3722 stats->hw_masks = NULL;
3723 kfree(stats->sw_stats);
3724 stats->sw_stats = NULL;
3725 if (stats->hw_stats) {
3726 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3727 stats->hw_stats_map);
3728 stats->hw_stats = NULL;
3732 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3735 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3736 &stats->hw_stats_map, GFP_KERNEL);
3737 if (!stats->hw_stats)
3740 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
3741 if (!stats->sw_stats)
3745 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
3746 if (!stats->hw_masks)
3752 bnxt_free_stats_mem(bp, stats);
3756 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
3760 for (i = 0; i < count; i++)
3764 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
3768 for (i = 0; i < count; i++)
3769 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
3772 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
3773 struct bnxt_stats_mem *stats)
3775 struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3776 struct hwrm_func_qstats_ext_input req = {0};
3780 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
3781 !(bp->flags & BNXT_FLAG_CHIP_P5))
3784 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
3785 req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3786 mutex_lock(&bp->hwrm_cmd_lock);
3787 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3791 hw_masks = &resp->rx_ucast_pkts;
3792 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
3795 mutex_unlock(&bp->hwrm_cmd_lock);
3799 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
3800 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
3802 static void bnxt_init_stats(struct bnxt *bp)
3804 struct bnxt_napi *bnapi = bp->bnapi[0];
3805 struct bnxt_cp_ring_info *cpr;
3806 struct bnxt_stats_mem *stats;
3807 __le64 *rx_stats, *tx_stats;
3808 int rc, rx_count, tx_count;
3809 u64 *rx_masks, *tx_masks;
3813 cpr = &bnapi->cp_ring;
3814 stats = &cpr->stats;
3815 rc = bnxt_hwrm_func_qstat_ext(bp, stats);
3817 if (bp->flags & BNXT_FLAG_CHIP_P5)
3818 mask = (1ULL << 48) - 1;
3821 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
3823 if (bp->flags & BNXT_FLAG_PORT_STATS) {
3824 stats = &bp->port_stats;
3825 rx_stats = stats->hw_stats;
3826 rx_masks = stats->hw_masks;
3827 rx_count = sizeof(struct rx_port_stats) / 8;
3828 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3829 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3830 tx_count = sizeof(struct tx_port_stats) / 8;
3832 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
3833 rc = bnxt_hwrm_port_qstats(bp, flags);
3835 mask = (1ULL << 40) - 1;
3837 bnxt_fill_masks(rx_masks, mask, rx_count);
3838 bnxt_fill_masks(tx_masks, mask, tx_count);
3840 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
3841 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
3842 bnxt_hwrm_port_qstats(bp, 0);
3845 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
3846 stats = &bp->rx_port_stats_ext;
3847 rx_stats = stats->hw_stats;
3848 rx_masks = stats->hw_masks;
3849 rx_count = sizeof(struct rx_port_stats_ext) / 8;
3850 stats = &bp->tx_port_stats_ext;
3851 tx_stats = stats->hw_stats;
3852 tx_masks = stats->hw_masks;
3853 tx_count = sizeof(struct tx_port_stats_ext) / 8;
3855 flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3856 rc = bnxt_hwrm_port_qstats_ext(bp, flags);
3858 mask = (1ULL << 40) - 1;
3860 bnxt_fill_masks(rx_masks, mask, rx_count);
3862 bnxt_fill_masks(tx_masks, mask, tx_count);
3864 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
3866 bnxt_copy_hw_masks(tx_masks, tx_stats,
3868 bnxt_hwrm_port_qstats_ext(bp, 0);
3873 static void bnxt_free_port_stats(struct bnxt *bp)
3875 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3876 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3878 bnxt_free_stats_mem(bp, &bp->port_stats);
3879 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
3880 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
3883 static void bnxt_free_ring_stats(struct bnxt *bp)
3890 for (i = 0; i < bp->cp_nr_rings; i++) {
3891 struct bnxt_napi *bnapi = bp->bnapi[i];
3892 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3894 bnxt_free_stats_mem(bp, &cpr->stats);
3898 static int bnxt_alloc_stats(struct bnxt *bp)
3903 size = bp->hw_ring_stats_size;
3905 for (i = 0; i < bp->cp_nr_rings; i++) {
3906 struct bnxt_napi *bnapi = bp->bnapi[i];
3907 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3909 cpr->stats.len = size;
3910 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
3914 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3917 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
3920 if (bp->port_stats.hw_stats)
3921 goto alloc_ext_stats;
3923 bp->port_stats.len = BNXT_PORT_STATS_SIZE;
3924 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
3928 bp->flags |= BNXT_FLAG_PORT_STATS;
3931 /* Display extended statistics only if FW supports it */
3932 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
3933 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
3936 if (bp->rx_port_stats_ext.hw_stats)
3937 goto alloc_tx_ext_stats;
3939 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
3940 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
3941 /* Extended stats are optional */
3946 if (bp->tx_port_stats_ext.hw_stats)
3949 if (bp->hwrm_spec_code >= 0x10902 ||
3950 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
3951 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
3952 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
3953 /* Extended stats are optional */
3957 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3961 static void bnxt_clear_ring_indices(struct bnxt *bp)
3968 for (i = 0; i < bp->cp_nr_rings; i++) {
3969 struct bnxt_napi *bnapi = bp->bnapi[i];
3970 struct bnxt_cp_ring_info *cpr;
3971 struct bnxt_rx_ring_info *rxr;
3972 struct bnxt_tx_ring_info *txr;
3977 cpr = &bnapi->cp_ring;
3978 cpr->cp_raw_cons = 0;
3980 txr = bnapi->tx_ring;
3986 rxr = bnapi->rx_ring;
3989 rxr->rx_agg_prod = 0;
3990 rxr->rx_sw_agg_prod = 0;
3991 rxr->rx_next_cons = 0;
3996 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3998 #ifdef CONFIG_RFS_ACCEL
4001 /* Under rtnl_lock and all our NAPIs have been disabled. It's
4002 * safe to delete the hash table.
4004 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4005 struct hlist_head *head;
4006 struct hlist_node *tmp;
4007 struct bnxt_ntuple_filter *fltr;
4009 head = &bp->ntp_fltr_hash_tbl[i];
4010 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4011 hlist_del(&fltr->hash);
4016 kfree(bp->ntp_fltr_bmap);
4017 bp->ntp_fltr_bmap = NULL;
4019 bp->ntp_fltr_count = 0;
4023 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4025 #ifdef CONFIG_RFS_ACCEL
4028 if (!(bp->flags & BNXT_FLAG_RFS))
4031 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4032 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4034 bp->ntp_fltr_count = 0;
4035 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4039 if (!bp->ntp_fltr_bmap)
4048 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4050 bnxt_free_vnic_attributes(bp);
4051 bnxt_free_tx_rings(bp);
4052 bnxt_free_rx_rings(bp);
4053 bnxt_free_cp_rings(bp);
4054 bnxt_free_ntp_fltrs(bp, irq_re_init);
4056 bnxt_free_ring_stats(bp);
4057 if (!(bp->fw_cap & BNXT_FW_CAP_PORT_STATS_NO_RESET))
4058 bnxt_free_port_stats(bp);
4059 bnxt_free_ring_grps(bp);
4060 bnxt_free_vnics(bp);
4061 kfree(bp->tx_ring_map);
4062 bp->tx_ring_map = NULL;
4070 bnxt_clear_ring_indices(bp);
4074 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4076 int i, j, rc, size, arr_size;
4080 /* Allocate bnapi mem pointer array and mem block for
4083 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4085 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4086 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4092 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4093 bp->bnapi[i] = bnapi;
4094 bp->bnapi[i]->index = i;
4095 bp->bnapi[i]->bp = bp;
4096 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4097 struct bnxt_cp_ring_info *cpr =
4098 &bp->bnapi[i]->cp_ring;
4100 cpr->cp_ring_struct.ring_mem.flags =
4101 BNXT_RMEM_RING_PTE_FLAG;
4105 bp->rx_ring = kcalloc(bp->rx_nr_rings,
4106 sizeof(struct bnxt_rx_ring_info),
4111 for (i = 0; i < bp->rx_nr_rings; i++) {
4112 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4114 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4115 rxr->rx_ring_struct.ring_mem.flags =
4116 BNXT_RMEM_RING_PTE_FLAG;
4117 rxr->rx_agg_ring_struct.ring_mem.flags =
4118 BNXT_RMEM_RING_PTE_FLAG;
4120 rxr->bnapi = bp->bnapi[i];
4121 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4124 bp->tx_ring = kcalloc(bp->tx_nr_rings,
4125 sizeof(struct bnxt_tx_ring_info),
4130 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4133 if (!bp->tx_ring_map)
4136 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4139 j = bp->rx_nr_rings;
4141 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4142 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4144 if (bp->flags & BNXT_FLAG_CHIP_P5)
4145 txr->tx_ring_struct.ring_mem.flags =
4146 BNXT_RMEM_RING_PTE_FLAG;
4147 txr->bnapi = bp->bnapi[j];
4148 bp->bnapi[j]->tx_ring = txr;
4149 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4150 if (i >= bp->tx_nr_rings_xdp) {
4151 txr->txq_index = i - bp->tx_nr_rings_xdp;
4152 bp->bnapi[j]->tx_int = bnxt_tx_int;
4154 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4155 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4159 rc = bnxt_alloc_stats(bp);
4162 bnxt_init_stats(bp);
4164 rc = bnxt_alloc_ntp_fltrs(bp);
4168 rc = bnxt_alloc_vnics(bp);
4173 bnxt_init_ring_struct(bp);
4175 rc = bnxt_alloc_rx_rings(bp);
4179 rc = bnxt_alloc_tx_rings(bp);
4183 rc = bnxt_alloc_cp_rings(bp);
4187 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4188 BNXT_VNIC_UCAST_FLAG;
4189 rc = bnxt_alloc_vnic_attributes(bp);
4195 bnxt_free_mem(bp, true);
4199 static void bnxt_disable_int(struct bnxt *bp)
4206 for (i = 0; i < bp->cp_nr_rings; i++) {
4207 struct bnxt_napi *bnapi = bp->bnapi[i];
4208 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4209 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4211 if (ring->fw_ring_id != INVALID_HW_RING_ID)
4212 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4216 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4218 struct bnxt_napi *bnapi = bp->bnapi[n];
4219 struct bnxt_cp_ring_info *cpr;
4221 cpr = &bnapi->cp_ring;
4222 return cpr->cp_ring_struct.map_idx;
4225 static void bnxt_disable_int_sync(struct bnxt *bp)
4229 atomic_inc(&bp->intr_sem);
4231 bnxt_disable_int(bp);
4232 for (i = 0; i < bp->cp_nr_rings; i++) {
4233 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4235 synchronize_irq(bp->irq_tbl[map_idx].vector);
4239 static void bnxt_enable_int(struct bnxt *bp)
4243 atomic_set(&bp->intr_sem, 0);
4244 for (i = 0; i < bp->cp_nr_rings; i++) {
4245 struct bnxt_napi *bnapi = bp->bnapi[i];
4246 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4248 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4252 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4253 u16 cmpl_ring, u16 target_id)
4255 struct input *req = request;
4257 req->req_type = cpu_to_le16(req_type);
4258 req->cmpl_ring = cpu_to_le16(cmpl_ring);
4259 req->target_id = cpu_to_le16(target_id);
4260 if (bnxt_kong_hwrm_message(bp, req))
4261 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4263 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4266 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4269 case HWRM_ERR_CODE_SUCCESS:
4271 case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4273 case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4275 case HWRM_ERR_CODE_INVALID_PARAMS:
4276 case HWRM_ERR_CODE_INVALID_FLAGS:
4277 case HWRM_ERR_CODE_INVALID_ENABLES:
4278 case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4279 case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4281 case HWRM_ERR_CODE_NO_BUFFER:
4283 case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4284 case HWRM_ERR_CODE_BUSY:
4286 case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4293 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4294 int timeout, bool silent)
4296 int i, intr_process, rc, tmo_count;
4297 struct input *req = msg;
4300 u16 cp_ring_id, len = 0;
4301 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4302 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4303 struct hwrm_short_input short_input = {0};
4304 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4305 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4306 u16 dst = BNXT_HWRM_CHNL_CHIMP;
4308 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4311 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4312 if (msg_len > bp->hwrm_max_ext_req_len ||
4313 !bp->hwrm_short_cmd_req_addr)
4317 if (bnxt_hwrm_kong_chnl(bp, req)) {
4318 dst = BNXT_HWRM_CHNL_KONG;
4319 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4320 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4321 resp = bp->hwrm_cmd_kong_resp_addr;
4324 memset(resp, 0, PAGE_SIZE);
4325 cp_ring_id = le16_to_cpu(req->cmpl_ring);
4326 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4328 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4329 /* currently supports only one outstanding message */
4331 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4333 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4334 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4335 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4338 /* Set boundary for maximum extended request length for short
4339 * cmd format. If passed up from device use the max supported
4340 * internal req length.
4342 max_msg_len = bp->hwrm_max_ext_req_len;
4344 memcpy(short_cmd_req, req, msg_len);
4345 if (msg_len < max_msg_len)
4346 memset(short_cmd_req + msg_len, 0,
4347 max_msg_len - msg_len);
4349 short_input.req_type = req->req_type;
4350 short_input.signature =
4351 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4352 short_input.size = cpu_to_le16(msg_len);
4353 short_input.req_addr =
4354 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4356 data = (u32 *)&short_input;
4357 msg_len = sizeof(short_input);
4359 /* Sync memory write before updating doorbell */
4362 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4365 /* Write request msg to hwrm channel */
4366 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4368 for (i = msg_len; i < max_req_len; i += 4)
4369 writel(0, bp->bar0 + bar_offset + i);
4371 /* Ring channel doorbell */
4372 writel(1, bp->bar0 + doorbell_offset);
4374 if (!pci_is_enabled(bp->pdev))
4378 timeout = DFLT_HWRM_CMD_TIMEOUT;
4379 /* convert timeout to usec */
4383 /* Short timeout for the first few iterations:
4384 * number of loops = number of loops for short timeout +
4385 * number of loops for standard timeout.
4387 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4388 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4389 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4392 u16 seq_id = bp->hwrm_intr_seq_id;
4394 /* Wait until hwrm response cmpl interrupt is processed */
4395 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4397 /* Abort the wait for completion if the FW health
4400 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4402 /* on first few passes, just barely sleep */
4403 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4404 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4405 HWRM_SHORT_MAX_TIMEOUT);
4407 usleep_range(HWRM_MIN_TIMEOUT,
4411 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4413 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4414 le16_to_cpu(req->req_type));
4417 len = le16_to_cpu(resp->resp_len);
4418 valid = ((u8 *)resp) + len - 1;
4422 /* Check if response len is updated */
4423 for (i = 0; i < tmo_count; i++) {
4424 /* Abort the wait for completion if the FW health
4427 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4429 len = le16_to_cpu(resp->resp_len);
4432 /* on first few passes, just barely sleep */
4433 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4434 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4435 HWRM_SHORT_MAX_TIMEOUT);
4437 usleep_range(HWRM_MIN_TIMEOUT,
4441 if (i >= tmo_count) {
4443 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4444 HWRM_TOTAL_TIMEOUT(i),
4445 le16_to_cpu(req->req_type),
4446 le16_to_cpu(req->seq_id), len);
4450 /* Last byte of resp contains valid bit */
4451 valid = ((u8 *)resp) + len - 1;
4452 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4453 /* make sure we read from updated DMA memory */
4460 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4462 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4463 HWRM_TOTAL_TIMEOUT(i),
4464 le16_to_cpu(req->req_type),
4465 le16_to_cpu(req->seq_id), len,
4471 /* Zero valid bit for compatibility. Valid bit in an older spec
4472 * may become a new field in a newer spec. We must make sure that
4473 * a new field not implemented by old spec will read zero.
4476 rc = le16_to_cpu(resp->error_code);
4478 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4479 le16_to_cpu(resp->req_type),
4480 le16_to_cpu(resp->seq_id), rc);
4481 return bnxt_hwrm_to_stderr(rc);
4484 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4486 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4489 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4492 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4495 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4499 mutex_lock(&bp->hwrm_cmd_lock);
4500 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4501 mutex_unlock(&bp->hwrm_cmd_lock);
4505 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4510 mutex_lock(&bp->hwrm_cmd_lock);
4511 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4512 mutex_unlock(&bp->hwrm_cmd_lock);
4516 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4519 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4520 struct hwrm_func_drv_rgtr_input req = {0};
4521 DECLARE_BITMAP(async_events_bmap, 256);
4522 u32 *events = (u32 *)async_events_bmap;
4526 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4529 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4530 FUNC_DRV_RGTR_REQ_ENABLES_VER |
4531 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4533 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4534 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4535 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4536 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4537 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4538 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4539 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4540 req.flags = cpu_to_le32(flags);
4541 req.ver_maj_8b = DRV_VER_MAJ;
4542 req.ver_min_8b = DRV_VER_MIN;
4543 req.ver_upd_8b = DRV_VER_UPD;
4544 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4545 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4546 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4552 memset(data, 0, sizeof(data));
4553 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4554 u16 cmd = bnxt_vf_req_snif[i];
4555 unsigned int bit, idx;
4559 data[idx] |= 1 << bit;
4562 for (i = 0; i < 8; i++)
4563 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4566 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4569 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4570 req.flags |= cpu_to_le32(
4571 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4573 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4574 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4575 u16 event_id = bnxt_async_events_arr[i];
4577 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4578 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4580 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4582 if (bmap && bmap_size) {
4583 for (i = 0; i < bmap_size; i++) {
4584 if (test_bit(i, bmap))
4585 __set_bit(i, async_events_bmap);
4588 for (i = 0; i < 8; i++)
4589 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4593 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4595 mutex_lock(&bp->hwrm_cmd_lock);
4596 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4598 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4600 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4601 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4603 mutex_unlock(&bp->hwrm_cmd_lock);
4607 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4609 struct hwrm_func_drv_unrgtr_input req = {0};
4611 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4614 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4615 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4618 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4621 struct hwrm_tunnel_dst_port_free_input req = {0};
4623 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4624 req.tunnel_type = tunnel_type;
4626 switch (tunnel_type) {
4627 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4628 req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4629 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4631 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4632 req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4633 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4639 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4641 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4646 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4650 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4651 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4653 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4655 req.tunnel_type = tunnel_type;
4656 req.tunnel_dst_port_val = port;
4658 mutex_lock(&bp->hwrm_cmd_lock);
4659 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4661 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4666 switch (tunnel_type) {
4667 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4668 bp->vxlan_fw_dst_port_id =
4669 le16_to_cpu(resp->tunnel_dst_port_id);
4671 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4672 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4679 mutex_unlock(&bp->hwrm_cmd_lock);
4683 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4685 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4686 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4688 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4689 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4691 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4692 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4693 req.mask = cpu_to_le32(vnic->rx_mask);
4694 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4697 #ifdef CONFIG_RFS_ACCEL
4698 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4699 struct bnxt_ntuple_filter *fltr)
4701 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4703 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4704 req.ntuple_filter_id = fltr->filter_id;
4705 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4708 #define BNXT_NTP_FLTR_FLAGS \
4709 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4710 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4711 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4712 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4713 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4714 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4715 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4716 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4717 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4718 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4719 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4720 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4721 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4722 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4724 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4725 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4727 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4728 struct bnxt_ntuple_filter *fltr)
4730 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4731 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4732 struct flow_keys *keys = &fltr->fkeys;
4733 struct bnxt_vnic_info *vnic;
4737 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4738 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4740 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4741 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4742 req.dst_id = cpu_to_le16(fltr->rxq);
4744 vnic = &bp->vnic_info[fltr->rxq + 1];
4745 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4747 req.flags = cpu_to_le32(flags);
4748 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4750 req.ethertype = htons(ETH_P_IP);
4751 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4752 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4753 req.ip_protocol = keys->basic.ip_proto;
4755 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4758 req.ethertype = htons(ETH_P_IPV6);
4760 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4761 *(struct in6_addr *)&req.src_ipaddr[0] =
4762 keys->addrs.v6addrs.src;
4763 *(struct in6_addr *)&req.dst_ipaddr[0] =
4764 keys->addrs.v6addrs.dst;
4765 for (i = 0; i < 4; i++) {
4766 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4767 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4770 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4771 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4772 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4773 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4775 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4776 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4778 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4781 req.src_port = keys->ports.src;
4782 req.src_port_mask = cpu_to_be16(0xffff);
4783 req.dst_port = keys->ports.dst;
4784 req.dst_port_mask = cpu_to_be16(0xffff);
4786 mutex_lock(&bp->hwrm_cmd_lock);
4787 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4789 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4790 fltr->filter_id = resp->ntuple_filter_id;
4792 mutex_unlock(&bp->hwrm_cmd_lock);
4797 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4801 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4802 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4804 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4805 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4806 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4808 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4809 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4811 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4812 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4813 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4814 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4815 req.l2_addr_mask[0] = 0xff;
4816 req.l2_addr_mask[1] = 0xff;
4817 req.l2_addr_mask[2] = 0xff;
4818 req.l2_addr_mask[3] = 0xff;
4819 req.l2_addr_mask[4] = 0xff;
4820 req.l2_addr_mask[5] = 0xff;
4822 mutex_lock(&bp->hwrm_cmd_lock);
4823 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4825 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4827 mutex_unlock(&bp->hwrm_cmd_lock);
4831 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4833 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4836 /* Any associated ntuple filters will also be cleared by firmware. */
4837 mutex_lock(&bp->hwrm_cmd_lock);
4838 for (i = 0; i < num_of_vnics; i++) {
4839 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4841 for (j = 0; j < vnic->uc_filter_count; j++) {
4842 struct hwrm_cfa_l2_filter_free_input req = {0};
4844 bnxt_hwrm_cmd_hdr_init(bp, &req,
4845 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4847 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4849 rc = _hwrm_send_message(bp, &req, sizeof(req),
4852 vnic->uc_filter_count = 0;
4854 mutex_unlock(&bp->hwrm_cmd_lock);
4859 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4861 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4862 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
4863 struct hwrm_vnic_tpa_cfg_input req = {0};
4865 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4868 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4871 u16 mss = bp->dev->mtu - 40;
4872 u32 nsegs, n, segs = 0, flags;
4874 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4875 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4876 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4877 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4878 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4879 if (tpa_flags & BNXT_FLAG_GRO)
4880 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4882 req.flags = cpu_to_le32(flags);
4885 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
4886 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4887 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
4889 /* Number of segs are log2 units, and first packet is not
4890 * included as part of this units.
4892 if (mss <= BNXT_RX_PAGE_SIZE) {
4893 n = BNXT_RX_PAGE_SIZE / mss;
4894 nsegs = (MAX_SKB_FRAGS - 1) * n;
4896 n = mss / BNXT_RX_PAGE_SIZE;
4897 if (mss & (BNXT_RX_PAGE_SIZE - 1))
4899 nsegs = (MAX_SKB_FRAGS - n) / n;
4902 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4903 segs = MAX_TPA_SEGS_P5;
4904 max_aggs = bp->max_tpa;
4906 segs = ilog2(nsegs);
4908 req.max_agg_segs = cpu_to_le16(segs);
4909 req.max_aggs = cpu_to_le16(max_aggs);
4911 req.min_agg_len = cpu_to_le32(512);
4913 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4915 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4918 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4920 struct bnxt_ring_grp_info *grp_info;
4922 grp_info = &bp->grp_info[ring->grp_idx];
4923 return grp_info->cp_fw_ring_id;
4926 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4928 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4929 struct bnxt_napi *bnapi = rxr->bnapi;
4930 struct bnxt_cp_ring_info *cpr;
4932 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4933 return cpr->cp_ring_struct.fw_ring_id;
4935 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4939 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4941 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4942 struct bnxt_napi *bnapi = txr->bnapi;
4943 struct bnxt_cp_ring_info *cpr;
4945 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4946 return cpr->cp_ring_struct.fw_ring_id;
4948 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4952 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
4956 if (bp->flags & BNXT_FLAG_CHIP_P5)
4957 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
4959 entries = HW_HASH_INDEX_SIZE;
4961 bp->rss_indir_tbl_entries = entries;
4962 bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
4964 if (!bp->rss_indir_tbl)
4969 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
4971 u16 max_rings, max_entries, pad, i;
4973 if (!bp->rx_nr_rings)
4976 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4977 max_rings = bp->rx_nr_rings - 1;
4979 max_rings = bp->rx_nr_rings;
4981 max_entries = bnxt_get_rxfh_indir_size(bp->dev);
4983 for (i = 0; i < max_entries; i++)
4984 bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
4986 pad = bp->rss_indir_tbl_entries - max_entries;
4988 memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
4991 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
4993 u16 i, tbl_size, max_ring = 0;
4995 if (!bp->rss_indir_tbl)
4998 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
4999 for (i = 0; i < tbl_size; i++)
5000 max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5004 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5006 if (bp->flags & BNXT_FLAG_CHIP_P5)
5007 return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5008 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5013 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5015 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5018 /* Fill the RSS indirection table with ring group ids */
5019 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5021 j = bp->rss_indir_tbl[i];
5022 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5026 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5027 struct bnxt_vnic_info *vnic)
5029 __le16 *ring_tbl = vnic->rss_table;
5030 struct bnxt_rx_ring_info *rxr;
5033 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5035 for (i = 0; i < tbl_size; i++) {
5038 j = bp->rss_indir_tbl[i];
5039 rxr = &bp->rx_ring[j];
5041 ring_id = rxr->rx_ring_struct.fw_ring_id;
5042 *ring_tbl++ = cpu_to_le16(ring_id);
5043 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5044 *ring_tbl++ = cpu_to_le16(ring_id);
5048 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5050 if (bp->flags & BNXT_FLAG_CHIP_P5)
5051 __bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5053 __bnxt_fill_hw_rss_tbl(bp, vnic);
5056 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5058 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5059 struct hwrm_vnic_rss_cfg_input req = {0};
5061 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5062 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5065 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5067 bnxt_fill_hw_rss_tbl(bp, vnic);
5068 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5069 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5070 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5071 req.hash_key_tbl_addr =
5072 cpu_to_le64(vnic->rss_hash_key_dma_addr);
5074 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5075 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5078 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5080 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5081 struct hwrm_vnic_rss_cfg_input req = {0};
5082 dma_addr_t ring_tbl_map;
5085 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5086 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5088 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5091 bnxt_fill_hw_rss_tbl(bp, vnic);
5092 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5093 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5094 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5095 ring_tbl_map = vnic->rss_table_dma_addr;
5096 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5097 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5100 req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5101 req.ring_table_pair_index = i;
5102 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5103 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5110 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5112 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5113 struct hwrm_vnic_plcmodes_cfg_input req = {0};
5115 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5116 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5117 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5118 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5120 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5121 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5122 /* thresholds not implemented in firmware yet */
5123 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5124 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5125 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5126 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5129 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5132 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5134 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5135 req.rss_cos_lb_ctx_id =
5136 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5138 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5139 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5142 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5146 for (i = 0; i < bp->nr_vnics; i++) {
5147 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5149 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5150 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5151 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5154 bp->rsscos_nr_ctxs = 0;
5157 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5160 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5161 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5162 bp->hwrm_cmd_resp_addr;
5164 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5167 mutex_lock(&bp->hwrm_cmd_lock);
5168 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5170 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5171 le16_to_cpu(resp->rss_cos_lb_ctx_id);
5172 mutex_unlock(&bp->hwrm_cmd_lock);
5177 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5179 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5180 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5181 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5184 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5186 unsigned int ring = 0, grp_idx;
5187 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5188 struct hwrm_vnic_cfg_input req = {0};
5191 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5193 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5194 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5196 req.default_rx_ring_id =
5197 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5198 req.default_cmpl_ring_id =
5199 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5201 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5202 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5205 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5206 /* Only RSS support for now TBD: COS & LB */
5207 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5208 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5209 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5210 VNIC_CFG_REQ_ENABLES_MRU);
5211 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5213 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5214 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5215 VNIC_CFG_REQ_ENABLES_MRU);
5216 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5218 req.rss_rule = cpu_to_le16(0xffff);
5221 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5222 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5223 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5224 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5226 req.cos_rule = cpu_to_le16(0xffff);
5229 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5231 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5233 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5234 ring = bp->rx_nr_rings - 1;
5236 grp_idx = bp->rx_ring[ring].bnapi->index;
5237 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5238 req.lb_rule = cpu_to_le16(0xffff);
5240 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5242 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5243 #ifdef CONFIG_BNXT_SRIOV
5245 def_vlan = bp->vf.vlan;
5247 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5248 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5249 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5250 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5252 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5255 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5257 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5258 struct hwrm_vnic_free_input req = {0};
5260 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5262 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5264 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5265 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5269 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5273 for (i = 0; i < bp->nr_vnics; i++)
5274 bnxt_hwrm_vnic_free_one(bp, i);
5277 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5278 unsigned int start_rx_ring_idx,
5279 unsigned int nr_rings)
5282 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5283 struct hwrm_vnic_alloc_input req = {0};
5284 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5285 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5287 if (bp->flags & BNXT_FLAG_CHIP_P5)
5288 goto vnic_no_ring_grps;
5290 /* map ring groups to this vnic */
5291 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5292 grp_idx = bp->rx_ring[i].bnapi->index;
5293 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5294 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5298 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5302 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5303 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5305 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5307 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5309 mutex_lock(&bp->hwrm_cmd_lock);
5310 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5312 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5313 mutex_unlock(&bp->hwrm_cmd_lock);
5317 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5319 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5320 struct hwrm_vnic_qcaps_input req = {0};
5323 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5324 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5325 if (bp->hwrm_spec_code < 0x10600)
5328 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5329 mutex_lock(&bp->hwrm_cmd_lock);
5330 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5332 u32 flags = le32_to_cpu(resp->flags);
5334 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5335 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5336 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5338 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5339 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5341 /* Older P5 fw before EXT_HW_STATS support did not set
5342 * VLAN_STRIP_CAP properly.
5344 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5345 ((bp->flags & BNXT_FLAG_CHIP_P5) &&
5346 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5347 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5348 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5350 bp->hw_ring_stats_size =
5351 sizeof(struct ctx_hw_stats_ext);
5353 mutex_unlock(&bp->hwrm_cmd_lock);
5357 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5362 if (bp->flags & BNXT_FLAG_CHIP_P5)
5365 mutex_lock(&bp->hwrm_cmd_lock);
5366 for (i = 0; i < bp->rx_nr_rings; i++) {
5367 struct hwrm_ring_grp_alloc_input req = {0};
5368 struct hwrm_ring_grp_alloc_output *resp =
5369 bp->hwrm_cmd_resp_addr;
5370 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5372 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5374 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5375 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5376 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5377 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5379 rc = _hwrm_send_message(bp, &req, sizeof(req),
5384 bp->grp_info[grp_idx].fw_grp_id =
5385 le32_to_cpu(resp->ring_group_id);
5387 mutex_unlock(&bp->hwrm_cmd_lock);
5391 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5394 struct hwrm_ring_grp_free_input req = {0};
5396 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5399 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5401 mutex_lock(&bp->hwrm_cmd_lock);
5402 for (i = 0; i < bp->cp_nr_rings; i++) {
5403 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5406 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5408 _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5409 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5411 mutex_unlock(&bp->hwrm_cmd_lock);
5414 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5415 struct bnxt_ring_struct *ring,
5416 u32 ring_type, u32 map_index)
5418 int rc = 0, err = 0;
5419 struct hwrm_ring_alloc_input req = {0};
5420 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5421 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5422 struct bnxt_ring_grp_info *grp_info;
5425 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5428 if (rmem->nr_pages > 1) {
5429 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5430 /* Page size is in log2 units */
5431 req.page_size = BNXT_PAGE_SHIFT;
5432 req.page_tbl_depth = 1;
5434 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5437 /* Association of ring index with doorbell index and MSIX number */
5438 req.logical_id = cpu_to_le16(map_index);
5440 switch (ring_type) {
5441 case HWRM_RING_ALLOC_TX: {
5442 struct bnxt_tx_ring_info *txr;
5444 txr = container_of(ring, struct bnxt_tx_ring_info,
5446 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5447 /* Association of transmit ring with completion ring */
5448 grp_info = &bp->grp_info[ring->grp_idx];
5449 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5450 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5451 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5452 req.queue_id = cpu_to_le16(ring->queue_id);
5455 case HWRM_RING_ALLOC_RX:
5456 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5457 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5458 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5461 /* Association of rx ring with stats context */
5462 grp_info = &bp->grp_info[ring->grp_idx];
5463 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5464 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5465 req.enables |= cpu_to_le32(
5466 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5467 if (NET_IP_ALIGN == 2)
5468 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5469 req.flags = cpu_to_le16(flags);
5472 case HWRM_RING_ALLOC_AGG:
5473 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5474 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5475 /* Association of agg ring with rx ring */
5476 grp_info = &bp->grp_info[ring->grp_idx];
5477 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5478 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5479 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5480 req.enables |= cpu_to_le32(
5481 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5482 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5484 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5486 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5488 case HWRM_RING_ALLOC_CMPL:
5489 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5490 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5491 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5492 /* Association of cp ring with nq */
5493 grp_info = &bp->grp_info[map_index];
5494 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5495 req.cq_handle = cpu_to_le64(ring->handle);
5496 req.enables |= cpu_to_le32(
5497 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5498 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5499 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5502 case HWRM_RING_ALLOC_NQ:
5503 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5504 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5505 if (bp->flags & BNXT_FLAG_USING_MSIX)
5506 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5509 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5514 mutex_lock(&bp->hwrm_cmd_lock);
5515 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5516 err = le16_to_cpu(resp->error_code);
5517 ring_id = le16_to_cpu(resp->ring_id);
5518 mutex_unlock(&bp->hwrm_cmd_lock);
5521 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5522 ring_type, rc, err);
5525 ring->fw_ring_id = ring_id;
5529 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5534 struct hwrm_func_cfg_input req = {0};
5536 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5537 req.fid = cpu_to_le16(0xffff);
5538 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5539 req.async_event_cr = cpu_to_le16(idx);
5540 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5542 struct hwrm_func_vf_cfg_input req = {0};
5544 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5546 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5547 req.async_event_cr = cpu_to_le16(idx);
5548 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5553 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5554 u32 map_idx, u32 xid)
5556 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5558 db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5560 db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5561 switch (ring_type) {
5562 case HWRM_RING_ALLOC_TX:
5563 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5565 case HWRM_RING_ALLOC_RX:
5566 case HWRM_RING_ALLOC_AGG:
5567 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5569 case HWRM_RING_ALLOC_CMPL:
5570 db->db_key64 = DBR_PATH_L2;
5572 case HWRM_RING_ALLOC_NQ:
5573 db->db_key64 = DBR_PATH_L2;
5576 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5578 db->doorbell = bp->bar1 + map_idx * 0x80;
5579 switch (ring_type) {
5580 case HWRM_RING_ALLOC_TX:
5581 db->db_key32 = DB_KEY_TX;
5583 case HWRM_RING_ALLOC_RX:
5584 case HWRM_RING_ALLOC_AGG:
5585 db->db_key32 = DB_KEY_RX;
5587 case HWRM_RING_ALLOC_CMPL:
5588 db->db_key32 = DB_KEY_CP;
5594 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5596 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5600 if (bp->flags & BNXT_FLAG_CHIP_P5)
5601 type = HWRM_RING_ALLOC_NQ;
5603 type = HWRM_RING_ALLOC_CMPL;
5604 for (i = 0; i < bp->cp_nr_rings; i++) {
5605 struct bnxt_napi *bnapi = bp->bnapi[i];
5606 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5607 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5608 u32 map_idx = ring->map_idx;
5609 unsigned int vector;
5611 vector = bp->irq_tbl[map_idx].vector;
5612 disable_irq_nosync(vector);
5613 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5618 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5619 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5621 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5624 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5626 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5630 type = HWRM_RING_ALLOC_TX;
5631 for (i = 0; i < bp->tx_nr_rings; i++) {
5632 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5633 struct bnxt_ring_struct *ring;
5636 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5637 struct bnxt_napi *bnapi = txr->bnapi;
5638 struct bnxt_cp_ring_info *cpr, *cpr2;
5639 u32 type2 = HWRM_RING_ALLOC_CMPL;
5641 cpr = &bnapi->cp_ring;
5642 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5643 ring = &cpr2->cp_ring_struct;
5644 ring->handle = BNXT_TX_HDL;
5645 map_idx = bnapi->index;
5646 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5649 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5651 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5653 ring = &txr->tx_ring_struct;
5655 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5658 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5661 type = HWRM_RING_ALLOC_RX;
5662 for (i = 0; i < bp->rx_nr_rings; i++) {
5663 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5664 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5665 struct bnxt_napi *bnapi = rxr->bnapi;
5666 u32 map_idx = bnapi->index;
5668 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5671 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5672 /* If we have agg rings, post agg buffers first. */
5674 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5675 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5676 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5677 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5678 u32 type2 = HWRM_RING_ALLOC_CMPL;
5679 struct bnxt_cp_ring_info *cpr2;
5681 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5682 ring = &cpr2->cp_ring_struct;
5683 ring->handle = BNXT_RX_HDL;
5684 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5687 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5689 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5694 type = HWRM_RING_ALLOC_AGG;
5695 for (i = 0; i < bp->rx_nr_rings; i++) {
5696 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5697 struct bnxt_ring_struct *ring =
5698 &rxr->rx_agg_ring_struct;
5699 u32 grp_idx = ring->grp_idx;
5700 u32 map_idx = grp_idx + bp->rx_nr_rings;
5702 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5706 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5708 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5709 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5710 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5717 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5718 struct bnxt_ring_struct *ring,
5719 u32 ring_type, int cmpl_ring_id)
5722 struct hwrm_ring_free_input req = {0};
5723 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5726 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
5729 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5730 req.ring_type = ring_type;
5731 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5733 mutex_lock(&bp->hwrm_cmd_lock);
5734 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5735 error_code = le16_to_cpu(resp->error_code);
5736 mutex_unlock(&bp->hwrm_cmd_lock);
5738 if (rc || error_code) {
5739 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5740 ring_type, rc, error_code);
5746 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5754 for (i = 0; i < bp->tx_nr_rings; i++) {
5755 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5756 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5758 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5759 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5761 hwrm_ring_free_send_msg(bp, ring,
5762 RING_FREE_REQ_RING_TYPE_TX,
5763 close_path ? cmpl_ring_id :
5764 INVALID_HW_RING_ID);
5765 ring->fw_ring_id = INVALID_HW_RING_ID;
5769 for (i = 0; i < bp->rx_nr_rings; i++) {
5770 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5771 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5772 u32 grp_idx = rxr->bnapi->index;
5774 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5775 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5777 hwrm_ring_free_send_msg(bp, ring,
5778 RING_FREE_REQ_RING_TYPE_RX,
5779 close_path ? cmpl_ring_id :
5780 INVALID_HW_RING_ID);
5781 ring->fw_ring_id = INVALID_HW_RING_ID;
5782 bp->grp_info[grp_idx].rx_fw_ring_id =
5787 if (bp->flags & BNXT_FLAG_CHIP_P5)
5788 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5790 type = RING_FREE_REQ_RING_TYPE_RX;
5791 for (i = 0; i < bp->rx_nr_rings; i++) {
5792 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5793 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5794 u32 grp_idx = rxr->bnapi->index;
5796 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5797 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5799 hwrm_ring_free_send_msg(bp, ring, type,
5800 close_path ? cmpl_ring_id :
5801 INVALID_HW_RING_ID);
5802 ring->fw_ring_id = INVALID_HW_RING_ID;
5803 bp->grp_info[grp_idx].agg_fw_ring_id =
5808 /* The completion rings are about to be freed. After that the
5809 * IRQ doorbell will not work anymore. So we need to disable
5812 bnxt_disable_int_sync(bp);
5814 if (bp->flags & BNXT_FLAG_CHIP_P5)
5815 type = RING_FREE_REQ_RING_TYPE_NQ;
5817 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5818 for (i = 0; i < bp->cp_nr_rings; i++) {
5819 struct bnxt_napi *bnapi = bp->bnapi[i];
5820 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5821 struct bnxt_ring_struct *ring;
5824 for (j = 0; j < 2; j++) {
5825 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5828 ring = &cpr2->cp_ring_struct;
5829 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5831 hwrm_ring_free_send_msg(bp, ring,
5832 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5833 INVALID_HW_RING_ID);
5834 ring->fw_ring_id = INVALID_HW_RING_ID;
5837 ring = &cpr->cp_ring_struct;
5838 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5839 hwrm_ring_free_send_msg(bp, ring, type,
5840 INVALID_HW_RING_ID);
5841 ring->fw_ring_id = INVALID_HW_RING_ID;
5842 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5847 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5850 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5852 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5853 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5854 struct hwrm_func_qcfg_input req = {0};
5857 if (bp->hwrm_spec_code < 0x10601)
5860 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5861 req.fid = cpu_to_le16(0xffff);
5862 mutex_lock(&bp->hwrm_cmd_lock);
5863 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5865 mutex_unlock(&bp->hwrm_cmd_lock);
5869 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5870 if (BNXT_NEW_RM(bp)) {
5873 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5874 hw_resc->resv_hw_ring_grps =
5875 le32_to_cpu(resp->alloc_hw_ring_grps);
5876 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5877 cp = le16_to_cpu(resp->alloc_cmpl_rings);
5878 stats = le16_to_cpu(resp->alloc_stat_ctx);
5879 hw_resc->resv_irqs = cp;
5880 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5881 int rx = hw_resc->resv_rx_rings;
5882 int tx = hw_resc->resv_tx_rings;
5884 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5886 if (cp < (rx + tx)) {
5887 bnxt_trim_rings(bp, &rx, &tx, cp, false);
5888 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5890 hw_resc->resv_rx_rings = rx;
5891 hw_resc->resv_tx_rings = tx;
5893 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
5894 hw_resc->resv_hw_ring_grps = rx;
5896 hw_resc->resv_cp_rings = cp;
5897 hw_resc->resv_stat_ctxs = stats;
5899 mutex_unlock(&bp->hwrm_cmd_lock);
5903 /* Caller must hold bp->hwrm_cmd_lock */
5904 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5906 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5907 struct hwrm_func_qcfg_input req = {0};
5910 if (bp->hwrm_spec_code < 0x10601)
5913 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5914 req.fid = cpu_to_le16(fid);
5915 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5917 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5922 static bool bnxt_rfs_supported(struct bnxt *bp);
5925 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5926 int tx_rings, int rx_rings, int ring_grps,
5927 int cp_rings, int stats, int vnics)
5931 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5932 req->fid = cpu_to_le16(0xffff);
5933 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5934 req->num_tx_rings = cpu_to_le16(tx_rings);
5935 if (BNXT_NEW_RM(bp)) {
5936 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
5937 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5938 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5939 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5940 enables |= tx_rings + ring_grps ?
5941 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5942 enables |= rx_rings ?
5943 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5945 enables |= cp_rings ?
5946 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5947 enables |= ring_grps ?
5948 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5949 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5951 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
5953 req->num_rx_rings = cpu_to_le16(rx_rings);
5954 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5955 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5956 req->num_msix = cpu_to_le16(cp_rings);
5957 req->num_rsscos_ctxs =
5958 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5960 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5961 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5962 req->num_rsscos_ctxs = cpu_to_le16(1);
5963 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5964 bnxt_rfs_supported(bp))
5965 req->num_rsscos_ctxs =
5966 cpu_to_le16(ring_grps + 1);
5968 req->num_stat_ctxs = cpu_to_le16(stats);
5969 req->num_vnics = cpu_to_le16(vnics);
5971 req->enables = cpu_to_le32(enables);
5975 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5976 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5977 int rx_rings, int ring_grps, int cp_rings,
5978 int stats, int vnics)
5982 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5983 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5984 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5985 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5986 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5987 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5988 enables |= tx_rings + ring_grps ?
5989 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5991 enables |= cp_rings ?
5992 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5993 enables |= ring_grps ?
5994 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5996 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
5997 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
5999 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6000 req->num_tx_rings = cpu_to_le16(tx_rings);
6001 req->num_rx_rings = cpu_to_le16(rx_rings);
6002 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6003 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6004 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6006 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6007 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6008 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6010 req->num_stat_ctxs = cpu_to_le16(stats);
6011 req->num_vnics = cpu_to_le16(vnics);
6013 req->enables = cpu_to_le32(enables);
6017 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6018 int ring_grps, int cp_rings, int stats, int vnics)
6020 struct hwrm_func_cfg_input req = {0};
6023 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6024 cp_rings, stats, vnics);
6028 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6032 if (bp->hwrm_spec_code < 0x10601)
6033 bp->hw_resc.resv_tx_rings = tx_rings;
6035 return bnxt_hwrm_get_rings(bp);
6039 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6040 int ring_grps, int cp_rings, int stats, int vnics)
6042 struct hwrm_func_vf_cfg_input req = {0};
6045 if (!BNXT_NEW_RM(bp)) {
6046 bp->hw_resc.resv_tx_rings = tx_rings;
6050 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6051 cp_rings, stats, vnics);
6052 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6056 return bnxt_hwrm_get_rings(bp);
6059 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6060 int cp, int stat, int vnic)
6063 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6066 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6070 int bnxt_nq_rings_in_use(struct bnxt *bp)
6072 int cp = bp->cp_nr_rings;
6073 int ulp_msix, ulp_base;
6075 ulp_msix = bnxt_get_ulp_msix_num(bp);
6077 ulp_base = bnxt_get_ulp_msix_base(bp);
6079 if ((ulp_base + ulp_msix) > cp)
6080 cp = ulp_base + ulp_msix;
6085 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6089 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6090 return bnxt_nq_rings_in_use(bp);
6092 cp = bp->tx_nr_rings + bp->rx_nr_rings;
6096 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6098 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6099 int cp = bp->cp_nr_rings;
6104 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6105 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6107 return cp + ulp_stat;
6110 /* Check if a default RSS map needs to be setup. This function is only
6111 * used on older firmware that does not require reserving RX rings.
6113 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6115 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6117 /* The RSS map is valid for RX rings set to resv_rx_rings */
6118 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6119 hw_resc->resv_rx_rings = bp->rx_nr_rings;
6120 if (!netif_is_rxfh_configured(bp->dev))
6121 bnxt_set_dflt_rss_indir_tbl(bp);
6125 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6127 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6128 int cp = bnxt_cp_rings_in_use(bp);
6129 int nq = bnxt_nq_rings_in_use(bp);
6130 int rx = bp->rx_nr_rings, stat;
6131 int vnic = 1, grp = rx;
6133 if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6134 bp->hwrm_spec_code >= 0x10601)
6137 /* Old firmware does not need RX ring reservations but we still
6138 * need to setup a default RSS map when needed. With new firmware
6139 * we go through RX ring reservations first and then set up the
6140 * RSS map for the successfully reserved RX rings when needed.
6142 if (!BNXT_NEW_RM(bp)) {
6143 bnxt_check_rss_tbl_no_rmgr(bp);
6146 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6148 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6150 stat = bnxt_get_func_stat_ctxs(bp);
6151 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6152 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6153 (hw_resc->resv_hw_ring_grps != grp &&
6154 !(bp->flags & BNXT_FLAG_CHIP_P5)))
6156 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6157 hw_resc->resv_irqs != nq)
6162 static int __bnxt_reserve_rings(struct bnxt *bp)
6164 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6165 int cp = bnxt_nq_rings_in_use(bp);
6166 int tx = bp->tx_nr_rings;
6167 int rx = bp->rx_nr_rings;
6168 int grp, rx_rings, rc;
6172 if (!bnxt_need_reserve_rings(bp))
6175 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6177 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6179 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6181 grp = bp->rx_nr_rings;
6182 stat = bnxt_get_func_stat_ctxs(bp);
6184 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6188 tx = hw_resc->resv_tx_rings;
6189 if (BNXT_NEW_RM(bp)) {
6190 rx = hw_resc->resv_rx_rings;
6191 cp = hw_resc->resv_irqs;
6192 grp = hw_resc->resv_hw_ring_grps;
6193 vnic = hw_resc->resv_vnics;
6194 stat = hw_resc->resv_stat_ctxs;
6198 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6202 if (netif_running(bp->dev))
6205 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6206 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6207 bp->dev->hw_features &= ~NETIF_F_LRO;
6208 bp->dev->features &= ~NETIF_F_LRO;
6209 bnxt_set_ring_params(bp);
6212 rx_rings = min_t(int, rx_rings, grp);
6213 cp = min_t(int, cp, bp->cp_nr_rings);
6214 if (stat > bnxt_get_ulp_stat_ctxs(bp))
6215 stat -= bnxt_get_ulp_stat_ctxs(bp);
6216 cp = min_t(int, cp, stat);
6217 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6218 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6220 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6221 bp->tx_nr_rings = tx;
6223 /* If we cannot reserve all the RX rings, reset the RSS map only
6224 * if absolutely necessary
6226 if (rx_rings != bp->rx_nr_rings) {
6227 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6228 rx_rings, bp->rx_nr_rings);
6229 if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6230 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6231 bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6232 bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6233 netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6234 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6237 bp->rx_nr_rings = rx_rings;
6238 bp->cp_nr_rings = cp;
6240 if (!tx || !rx || !cp || !grp || !vnic || !stat)
6243 if (!netif_is_rxfh_configured(bp->dev))
6244 bnxt_set_dflt_rss_indir_tbl(bp);
6249 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6250 int ring_grps, int cp_rings, int stats,
6253 struct hwrm_func_vf_cfg_input req = {0};
6256 if (!BNXT_NEW_RM(bp))
6259 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6260 cp_rings, stats, vnics);
6261 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6262 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6263 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6264 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6265 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6266 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6267 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6268 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6270 req.flags = cpu_to_le32(flags);
6271 return hwrm_send_message_silent(bp, &req, sizeof(req),
6275 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6276 int ring_grps, int cp_rings, int stats,
6279 struct hwrm_func_cfg_input req = {0};
6282 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6283 cp_rings, stats, vnics);
6284 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6285 if (BNXT_NEW_RM(bp)) {
6286 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6287 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6288 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6289 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6290 if (bp->flags & BNXT_FLAG_CHIP_P5)
6291 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6292 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6294 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6297 req.flags = cpu_to_le32(flags);
6298 return hwrm_send_message_silent(bp, &req, sizeof(req),
6302 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6303 int ring_grps, int cp_rings, int stats,
6306 if (bp->hwrm_spec_code < 0x10801)
6310 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6311 ring_grps, cp_rings, stats,
6314 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6315 cp_rings, stats, vnics);
6318 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6320 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6321 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6322 struct hwrm_ring_aggint_qcaps_input req = {0};
6325 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6326 coal_cap->num_cmpl_dma_aggr_max = 63;
6327 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6328 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6329 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6330 coal_cap->int_lat_tmr_min_max = 65535;
6331 coal_cap->int_lat_tmr_max_max = 65535;
6332 coal_cap->num_cmpl_aggr_int_max = 65535;
6333 coal_cap->timer_units = 80;
6335 if (bp->hwrm_spec_code < 0x10902)
6338 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6339 mutex_lock(&bp->hwrm_cmd_lock);
6340 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6342 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6343 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6344 coal_cap->num_cmpl_dma_aggr_max =
6345 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6346 coal_cap->num_cmpl_dma_aggr_during_int_max =
6347 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6348 coal_cap->cmpl_aggr_dma_tmr_max =
6349 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6350 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6351 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6352 coal_cap->int_lat_tmr_min_max =
6353 le16_to_cpu(resp->int_lat_tmr_min_max);
6354 coal_cap->int_lat_tmr_max_max =
6355 le16_to_cpu(resp->int_lat_tmr_max_max);
6356 coal_cap->num_cmpl_aggr_int_max =
6357 le16_to_cpu(resp->num_cmpl_aggr_int_max);
6358 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6360 mutex_unlock(&bp->hwrm_cmd_lock);
6363 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6365 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6367 return usec * 1000 / coal_cap->timer_units;
6370 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6371 struct bnxt_coal *hw_coal,
6372 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6374 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6375 u32 cmpl_params = coal_cap->cmpl_params;
6376 u16 val, tmr, max, flags = 0;
6378 max = hw_coal->bufs_per_record * 128;
6379 if (hw_coal->budget)
6380 max = hw_coal->bufs_per_record * hw_coal->budget;
6381 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6383 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6384 req->num_cmpl_aggr_int = cpu_to_le16(val);
6386 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6387 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6389 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6390 coal_cap->num_cmpl_dma_aggr_during_int_max);
6391 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6393 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6394 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6395 req->int_lat_tmr_max = cpu_to_le16(tmr);
6397 /* min timer set to 1/2 of interrupt timer */
6398 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6400 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6401 req->int_lat_tmr_min = cpu_to_le16(val);
6402 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6405 /* buf timer set to 1/4 of interrupt timer */
6406 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6407 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6410 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6411 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6412 val = clamp_t(u16, tmr, 1,
6413 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6414 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6416 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6419 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6420 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6421 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6422 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6423 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6424 req->flags = cpu_to_le16(flags);
6425 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6428 /* Caller holds bp->hwrm_cmd_lock */
6429 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6430 struct bnxt_coal *hw_coal)
6432 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6433 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6434 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6435 u32 nq_params = coal_cap->nq_params;
6438 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6441 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6443 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6445 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6447 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6448 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6449 req.int_lat_tmr_min = cpu_to_le16(tmr);
6450 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6451 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6454 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6456 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6457 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6458 struct bnxt_coal coal;
6460 /* Tick values in micro seconds.
6461 * 1 coal_buf x bufs_per_record = 1 completion record.
6463 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6465 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6466 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6468 if (!bnapi->rx_ring)
6471 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6472 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6474 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6476 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6478 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6482 int bnxt_hwrm_set_coal(struct bnxt *bp)
6485 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6488 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6489 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6490 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6491 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6493 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6494 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6496 mutex_lock(&bp->hwrm_cmd_lock);
6497 for (i = 0; i < bp->cp_nr_rings; i++) {
6498 struct bnxt_napi *bnapi = bp->bnapi[i];
6499 struct bnxt_coal *hw_coal;
6503 if (!bnapi->rx_ring) {
6504 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6507 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6509 req->ring_id = cpu_to_le16(ring_id);
6511 rc = _hwrm_send_message(bp, req, sizeof(*req),
6516 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6519 if (bnapi->rx_ring && bnapi->tx_ring) {
6521 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6522 req->ring_id = cpu_to_le16(ring_id);
6523 rc = _hwrm_send_message(bp, req, sizeof(*req),
6529 hw_coal = &bp->rx_coal;
6531 hw_coal = &bp->tx_coal;
6532 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6534 mutex_unlock(&bp->hwrm_cmd_lock);
6538 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6540 struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6541 struct hwrm_stat_ctx_free_input req = {0};
6547 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6550 bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6551 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6553 mutex_lock(&bp->hwrm_cmd_lock);
6554 for (i = 0; i < bp->cp_nr_rings; i++) {
6555 struct bnxt_napi *bnapi = bp->bnapi[i];
6556 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6558 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6559 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6560 if (BNXT_FW_MAJ(bp) <= 20) {
6561 req0.stat_ctx_id = req.stat_ctx_id;
6562 _hwrm_send_message(bp, &req0, sizeof(req0),
6565 _hwrm_send_message(bp, &req, sizeof(req),
6568 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6571 mutex_unlock(&bp->hwrm_cmd_lock);
6574 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6577 struct hwrm_stat_ctx_alloc_input req = {0};
6578 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6580 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6583 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6585 req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6586 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6588 mutex_lock(&bp->hwrm_cmd_lock);
6589 for (i = 0; i < bp->cp_nr_rings; i++) {
6590 struct bnxt_napi *bnapi = bp->bnapi[i];
6591 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6593 req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6595 rc = _hwrm_send_message(bp, &req, sizeof(req),
6600 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6602 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6604 mutex_unlock(&bp->hwrm_cmd_lock);
6608 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6610 struct hwrm_func_qcfg_input req = {0};
6611 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6612 u32 min_db_offset = 0;
6616 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6617 req.fid = cpu_to_le16(0xffff);
6618 mutex_lock(&bp->hwrm_cmd_lock);
6619 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6621 goto func_qcfg_exit;
6623 #ifdef CONFIG_BNXT_SRIOV
6625 struct bnxt_vf_info *vf = &bp->vf;
6627 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6629 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6632 flags = le16_to_cpu(resp->flags);
6633 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6634 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6635 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6636 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6637 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6639 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6640 bp->flags |= BNXT_FLAG_MULTI_HOST;
6642 switch (resp->port_partition_type) {
6643 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6644 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6645 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6646 bp->port_partition_type = resp->port_partition_type;
6649 if (bp->hwrm_spec_code < 0x10707 ||
6650 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6651 bp->br_mode = BRIDGE_MODE_VEB;
6652 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6653 bp->br_mode = BRIDGE_MODE_VEPA;
6655 bp->br_mode = BRIDGE_MODE_UNDEF;
6657 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6659 bp->max_mtu = BNXT_MAX_MTU;
6662 goto func_qcfg_exit;
6664 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6666 min_db_offset = DB_PF_OFFSET_P5;
6668 min_db_offset = DB_VF_OFFSET_P5;
6670 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6672 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6673 bp->db_size <= min_db_offset)
6674 bp->db_size = pci_resource_len(bp->pdev, 2);
6677 mutex_unlock(&bp->hwrm_cmd_lock);
6681 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6683 struct hwrm_func_backing_store_qcaps_input req = {0};
6684 struct hwrm_func_backing_store_qcaps_output *resp =
6685 bp->hwrm_cmd_resp_addr;
6688 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6691 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6692 mutex_lock(&bp->hwrm_cmd_lock);
6693 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6695 struct bnxt_ctx_pg_info *ctx_pg;
6696 struct bnxt_ctx_mem_info *ctx;
6699 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6704 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6705 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6706 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6707 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6708 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6709 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6710 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6711 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6712 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6713 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6714 ctx->vnic_max_vnic_entries =
6715 le16_to_cpu(resp->vnic_max_vnic_entries);
6716 ctx->vnic_max_ring_table_entries =
6717 le16_to_cpu(resp->vnic_max_ring_table_entries);
6718 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6719 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6720 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6721 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6722 ctx->tqm_min_entries_per_ring =
6723 le32_to_cpu(resp->tqm_min_entries_per_ring);
6724 ctx->tqm_max_entries_per_ring =
6725 le32_to_cpu(resp->tqm_max_entries_per_ring);
6726 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6727 if (!ctx->tqm_entries_multiple)
6728 ctx->tqm_entries_multiple = 1;
6729 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6730 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6731 ctx->mrav_num_entries_units =
6732 le16_to_cpu(resp->mrav_num_entries_units);
6733 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6734 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6735 ctx->ctx_kind_initializer = resp->ctx_kind_initializer;
6736 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6737 if (!ctx->tqm_fp_rings_count)
6738 ctx->tqm_fp_rings_count = bp->max_q;
6740 tqm_rings = ctx->tqm_fp_rings_count + 1;
6741 ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6747 for (i = 0; i < tqm_rings; i++, ctx_pg++)
6748 ctx->tqm_mem[i] = ctx_pg;
6754 mutex_unlock(&bp->hwrm_cmd_lock);
6758 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6763 if (BNXT_PAGE_SHIFT == 13)
6765 else if (BNXT_PAGE_SIZE == 16)
6769 if (rmem->depth >= 1) {
6770 if (rmem->depth == 2)
6774 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6776 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6780 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6781 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6782 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6783 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6784 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6785 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6787 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6789 struct hwrm_func_backing_store_cfg_input req = {0};
6790 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6791 struct bnxt_ctx_pg_info *ctx_pg;
6792 __le32 *num_entries;
6802 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6803 req.enables = cpu_to_le32(enables);
6805 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6806 ctx_pg = &ctx->qp_mem;
6807 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6808 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6809 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6810 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6811 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6812 &req.qpc_pg_size_qpc_lvl,
6815 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6816 ctx_pg = &ctx->srq_mem;
6817 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6818 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6819 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6820 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6821 &req.srq_pg_size_srq_lvl,
6824 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6825 ctx_pg = &ctx->cq_mem;
6826 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6827 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6828 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6829 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6832 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6833 ctx_pg = &ctx->vnic_mem;
6834 req.vnic_num_vnic_entries =
6835 cpu_to_le16(ctx->vnic_max_vnic_entries);
6836 req.vnic_num_ring_table_entries =
6837 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6838 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6839 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6840 &req.vnic_pg_size_vnic_lvl,
6841 &req.vnic_page_dir);
6843 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6844 ctx_pg = &ctx->stat_mem;
6845 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6846 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6847 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6848 &req.stat_pg_size_stat_lvl,
6849 &req.stat_page_dir);
6851 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6852 ctx_pg = &ctx->mrav_mem;
6853 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6854 if (ctx->mrav_num_entries_units)
6856 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
6857 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
6858 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6859 &req.mrav_pg_size_mrav_lvl,
6860 &req.mrav_page_dir);
6862 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
6863 ctx_pg = &ctx->tim_mem;
6864 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
6865 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
6866 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6867 &req.tim_pg_size_tim_lvl,
6870 for (i = 0, num_entries = &req.tqm_sp_num_entries,
6871 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
6872 pg_dir = &req.tqm_sp_page_dir,
6873 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
6874 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
6875 if (!(enables & ena))
6878 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
6879 ctx_pg = ctx->tqm_mem[i];
6880 *num_entries = cpu_to_le32(ctx_pg->entries);
6881 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
6883 req.flags = cpu_to_le32(flags);
6884 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6887 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
6888 struct bnxt_ctx_pg_info *ctx_pg)
6890 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6892 rmem->page_size = BNXT_PAGE_SIZE;
6893 rmem->pg_arr = ctx_pg->ctx_pg_arr;
6894 rmem->dma_arr = ctx_pg->ctx_dma_arr;
6895 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
6896 if (rmem->depth >= 1)
6897 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
6898 return bnxt_alloc_ring(bp, rmem);
6901 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
6902 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
6903 u8 depth, bool use_init_val)
6905 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6911 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6912 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
6913 ctx_pg->nr_pages = 0;
6916 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
6920 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
6922 if (!ctx_pg->ctx_pg_tbl)
6924 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
6925 rmem->nr_pages = nr_tbls;
6926 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6929 for (i = 0; i < nr_tbls; i++) {
6930 struct bnxt_ctx_pg_info *pg_tbl;
6932 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
6935 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
6936 rmem = &pg_tbl->ring_mem;
6937 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
6938 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
6940 rmem->nr_pages = MAX_CTX_PAGES;
6942 rmem->init_val = bp->ctx->ctx_kind_initializer;
6943 if (i == (nr_tbls - 1)) {
6944 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
6947 rmem->nr_pages = rem;
6949 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
6954 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6955 if (rmem->nr_pages > 1 || depth)
6958 rmem->init_val = bp->ctx->ctx_kind_initializer;
6959 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6964 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
6965 struct bnxt_ctx_pg_info *ctx_pg)
6967 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6969 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
6970 ctx_pg->ctx_pg_tbl) {
6971 int i, nr_tbls = rmem->nr_pages;
6973 for (i = 0; i < nr_tbls; i++) {
6974 struct bnxt_ctx_pg_info *pg_tbl;
6975 struct bnxt_ring_mem_info *rmem2;
6977 pg_tbl = ctx_pg->ctx_pg_tbl[i];
6980 rmem2 = &pg_tbl->ring_mem;
6981 bnxt_free_ring(bp, rmem2);
6982 ctx_pg->ctx_pg_arr[i] = NULL;
6984 ctx_pg->ctx_pg_tbl[i] = NULL;
6986 kfree(ctx_pg->ctx_pg_tbl);
6987 ctx_pg->ctx_pg_tbl = NULL;
6989 bnxt_free_ring(bp, rmem);
6990 ctx_pg->nr_pages = 0;
6993 static void bnxt_free_ctx_mem(struct bnxt *bp)
6995 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7001 if (ctx->tqm_mem[0]) {
7002 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7003 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7004 kfree(ctx->tqm_mem[0]);
7005 ctx->tqm_mem[0] = NULL;
7008 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7009 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7010 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7011 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7012 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7013 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7014 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7015 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7018 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7020 struct bnxt_ctx_pg_info *ctx_pg;
7021 struct bnxt_ctx_mem_info *ctx;
7022 u32 mem_size, ena, entries;
7023 u32 entries_sp, min;
7030 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7032 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7037 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7040 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7046 ctx_pg = &ctx->qp_mem;
7047 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7049 mem_size = ctx->qp_entry_size * ctx_pg->entries;
7050 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7054 ctx_pg = &ctx->srq_mem;
7055 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7056 mem_size = ctx->srq_entry_size * ctx_pg->entries;
7057 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7061 ctx_pg = &ctx->cq_mem;
7062 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7063 mem_size = ctx->cq_entry_size * ctx_pg->entries;
7064 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7068 ctx_pg = &ctx->vnic_mem;
7069 ctx_pg->entries = ctx->vnic_max_vnic_entries +
7070 ctx->vnic_max_ring_table_entries;
7071 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7072 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7076 ctx_pg = &ctx->stat_mem;
7077 ctx_pg->entries = ctx->stat_max_entries;
7078 mem_size = ctx->stat_entry_size * ctx_pg->entries;
7079 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7084 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7087 ctx_pg = &ctx->mrav_mem;
7088 /* 128K extra is needed to accommodate static AH context
7089 * allocation by f/w.
7091 num_mr = 1024 * 256;
7092 num_ah = 1024 * 128;
7093 ctx_pg->entries = num_mr + num_ah;
7094 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7095 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, true);
7098 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7099 if (ctx->mrav_num_entries_units)
7101 ((num_mr / ctx->mrav_num_entries_units) << 16) |
7102 (num_ah / ctx->mrav_num_entries_units);
7104 ctx_pg = &ctx->tim_mem;
7105 ctx_pg->entries = ctx->qp_mem.entries;
7106 mem_size = ctx->tim_entry_size * ctx_pg->entries;
7107 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7110 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7113 min = ctx->tqm_min_entries_per_ring;
7114 entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7115 2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7116 entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7117 entries = ctx->qp_max_l2_entries + extra_qps + ctx->qp_min_qp1_entries;
7118 entries = roundup(entries, ctx->tqm_entries_multiple);
7119 entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7120 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7121 ctx_pg = ctx->tqm_mem[i];
7122 ctx_pg->entries = i ? entries : entries_sp;
7123 mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7124 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7127 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7129 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7130 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7132 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7136 ctx->flags |= BNXT_CTX_FLAG_INITED;
7140 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7142 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7143 struct hwrm_func_resource_qcaps_input req = {0};
7144 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7147 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7148 req.fid = cpu_to_le16(0xffff);
7150 mutex_lock(&bp->hwrm_cmd_lock);
7151 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7154 goto hwrm_func_resc_qcaps_exit;
7156 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7158 goto hwrm_func_resc_qcaps_exit;
7160 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7161 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7162 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7163 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7164 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7165 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7166 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7167 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7168 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7169 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7170 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7171 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7172 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7173 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7174 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7175 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7177 if (bp->flags & BNXT_FLAG_CHIP_P5) {
7178 u16 max_msix = le16_to_cpu(resp->max_msix);
7180 hw_resc->max_nqs = max_msix;
7181 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7185 struct bnxt_pf_info *pf = &bp->pf;
7187 pf->vf_resv_strategy =
7188 le16_to_cpu(resp->vf_reservation_strategy);
7189 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7190 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7192 hwrm_func_resc_qcaps_exit:
7193 mutex_unlock(&bp->hwrm_cmd_lock);
7197 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7200 struct hwrm_func_qcaps_input req = {0};
7201 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7202 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7203 u32 flags, flags_ext;
7205 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7206 req.fid = cpu_to_le16(0xffff);
7208 mutex_lock(&bp->hwrm_cmd_lock);
7209 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7211 goto hwrm_func_qcaps_exit;
7213 flags = le32_to_cpu(resp->flags);
7214 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7215 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7216 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7217 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7218 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7219 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7220 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7221 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7222 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7223 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7224 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7225 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7226 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7227 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7228 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7229 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7231 flags_ext = le32_to_cpu(resp->flags_ext);
7232 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7233 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7235 bp->tx_push_thresh = 0;
7236 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7237 BNXT_FW_MAJ(bp) > 217)
7238 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7240 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7241 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7242 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7243 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7244 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7245 if (!hw_resc->max_hw_ring_grps)
7246 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7247 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7248 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7249 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7252 struct bnxt_pf_info *pf = &bp->pf;
7254 pf->fw_fid = le16_to_cpu(resp->fid);
7255 pf->port_id = le16_to_cpu(resp->port_id);
7256 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7257 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7258 pf->max_vfs = le16_to_cpu(resp->max_vfs);
7259 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7260 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7261 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7262 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7263 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7264 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7265 bp->flags &= ~BNXT_FLAG_WOL_CAP;
7266 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7267 bp->flags |= BNXT_FLAG_WOL_CAP;
7269 #ifdef CONFIG_BNXT_SRIOV
7270 struct bnxt_vf_info *vf = &bp->vf;
7272 vf->fw_fid = le16_to_cpu(resp->fid);
7273 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7277 hwrm_func_qcaps_exit:
7278 mutex_unlock(&bp->hwrm_cmd_lock);
7282 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7284 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7288 rc = __bnxt_hwrm_func_qcaps(bp);
7291 rc = bnxt_hwrm_queue_qportcfg(bp);
7293 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7296 if (bp->hwrm_spec_code >= 0x10803) {
7297 rc = bnxt_alloc_ctx_mem(bp);
7300 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7302 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7307 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7309 struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7310 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7314 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7317 resp = bp->hwrm_cmd_resp_addr;
7318 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7320 mutex_lock(&bp->hwrm_cmd_lock);
7321 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7323 goto hwrm_cfa_adv_qcaps_exit;
7325 flags = le32_to_cpu(resp->flags);
7327 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7328 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7330 hwrm_cfa_adv_qcaps_exit:
7331 mutex_unlock(&bp->hwrm_cmd_lock);
7335 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7337 struct bnxt_fw_health *fw_health = bp->fw_health;
7338 u32 reg_base = 0xffffffff;
7341 /* Only pre-map the monitoring GRC registers using window 3 */
7342 for (i = 0; i < 4; i++) {
7343 u32 reg = fw_health->regs[i];
7345 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7347 if (reg_base == 0xffffffff)
7348 reg_base = reg & BNXT_GRC_BASE_MASK;
7349 if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7351 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_BASE +
7352 (reg & BNXT_GRC_OFFSET_MASK);
7354 if (reg_base == 0xffffffff)
7357 writel(reg_base, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7358 BNXT_FW_HEALTH_WIN_MAP_OFF);
7362 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7364 struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7365 struct bnxt_fw_health *fw_health = bp->fw_health;
7366 struct hwrm_error_recovery_qcfg_input req = {0};
7369 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7372 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7373 mutex_lock(&bp->hwrm_cmd_lock);
7374 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7376 goto err_recovery_out;
7377 fw_health->flags = le32_to_cpu(resp->flags);
7378 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7379 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7381 goto err_recovery_out;
7383 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7384 fw_health->master_func_wait_dsecs =
7385 le32_to_cpu(resp->master_func_wait_period);
7386 fw_health->normal_func_wait_dsecs =
7387 le32_to_cpu(resp->normal_func_wait_period);
7388 fw_health->post_reset_wait_dsecs =
7389 le32_to_cpu(resp->master_func_wait_period_after_reset);
7390 fw_health->post_reset_max_wait_dsecs =
7391 le32_to_cpu(resp->max_bailout_time_after_reset);
7392 fw_health->regs[BNXT_FW_HEALTH_REG] =
7393 le32_to_cpu(resp->fw_health_status_reg);
7394 fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7395 le32_to_cpu(resp->fw_heartbeat_reg);
7396 fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7397 le32_to_cpu(resp->fw_reset_cnt_reg);
7398 fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7399 le32_to_cpu(resp->reset_inprogress_reg);
7400 fw_health->fw_reset_inprog_reg_mask =
7401 le32_to_cpu(resp->reset_inprogress_reg_mask);
7402 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7403 if (fw_health->fw_reset_seq_cnt >= 16) {
7405 goto err_recovery_out;
7407 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7408 fw_health->fw_reset_seq_regs[i] =
7409 le32_to_cpu(resp->reset_reg[i]);
7410 fw_health->fw_reset_seq_vals[i] =
7411 le32_to_cpu(resp->reset_reg_val[i]);
7412 fw_health->fw_reset_seq_delay_msec[i] =
7413 resp->delay_after_reset[i];
7416 mutex_unlock(&bp->hwrm_cmd_lock);
7418 rc = bnxt_map_fw_health_regs(bp);
7420 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7424 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7426 struct hwrm_func_reset_input req = {0};
7428 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7431 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7434 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7437 struct hwrm_queue_qportcfg_input req = {0};
7438 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7442 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7444 mutex_lock(&bp->hwrm_cmd_lock);
7445 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7449 if (!resp->max_configurable_queues) {
7453 bp->max_tc = resp->max_configurable_queues;
7454 bp->max_lltc = resp->max_configurable_lossless_queues;
7455 if (bp->max_tc > BNXT_MAX_QUEUE)
7456 bp->max_tc = BNXT_MAX_QUEUE;
7458 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7459 qptr = &resp->queue_id0;
7460 for (i = 0, j = 0; i < bp->max_tc; i++) {
7461 bp->q_info[j].queue_id = *qptr;
7462 bp->q_ids[i] = *qptr++;
7463 bp->q_info[j].queue_profile = *qptr++;
7464 bp->tc_to_qidx[j] = j;
7465 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7466 (no_rdma && BNXT_PF(bp)))
7469 bp->max_q = bp->max_tc;
7470 bp->max_tc = max_t(u8, j, 1);
7472 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7475 if (bp->max_lltc > bp->max_tc)
7476 bp->max_lltc = bp->max_tc;
7479 mutex_unlock(&bp->hwrm_cmd_lock);
7483 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7485 struct hwrm_ver_get_input req = {0};
7488 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7489 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7490 req.hwrm_intf_min = HWRM_VERSION_MINOR;
7491 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7493 rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7498 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7500 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7501 u16 fw_maj, fw_min, fw_bld, fw_rsv;
7502 u32 dev_caps_cfg, hwrm_ver;
7505 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7506 mutex_lock(&bp->hwrm_cmd_lock);
7507 rc = __bnxt_hwrm_ver_get(bp, false);
7509 goto hwrm_ver_get_exit;
7511 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7513 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7514 resp->hwrm_intf_min_8b << 8 |
7515 resp->hwrm_intf_upd_8b;
7516 if (resp->hwrm_intf_maj_8b < 1) {
7517 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7518 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7519 resp->hwrm_intf_upd_8b);
7520 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7523 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7524 HWRM_VERSION_UPDATE;
7526 if (bp->hwrm_spec_code > hwrm_ver)
7527 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7528 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7529 HWRM_VERSION_UPDATE);
7531 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7532 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7533 resp->hwrm_intf_upd_8b);
7535 fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7536 if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7537 fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7538 fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7539 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7540 len = FW_VER_STR_LEN;
7542 fw_maj = resp->hwrm_fw_maj_8b;
7543 fw_min = resp->hwrm_fw_min_8b;
7544 fw_bld = resp->hwrm_fw_bld_8b;
7545 fw_rsv = resp->hwrm_fw_rsvd_8b;
7546 len = BC_HWRM_STR_LEN;
7548 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7549 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7552 if (strlen(resp->active_pkg_name)) {
7553 int fw_ver_len = strlen(bp->fw_ver_str);
7555 snprintf(bp->fw_ver_str + fw_ver_len,
7556 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7557 resp->active_pkg_name);
7558 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7561 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7562 if (!bp->hwrm_cmd_timeout)
7563 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7565 if (resp->hwrm_intf_maj_8b >= 1) {
7566 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7567 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7569 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7570 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7572 bp->chip_num = le16_to_cpu(resp->chip_num);
7573 bp->chip_rev = resp->chip_rev;
7574 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7576 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7578 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7579 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7580 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7581 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7583 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7584 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7587 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7588 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7591 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7592 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7595 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7596 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7599 mutex_unlock(&bp->hwrm_cmd_lock);
7603 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7605 struct hwrm_fw_set_time_input req = {0};
7607 time64_t now = ktime_get_real_seconds();
7609 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7610 bp->hwrm_spec_code < 0x10400)
7613 time64_to_tm(now, 0, &tm);
7614 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7615 req.year = cpu_to_le16(1900 + tm.tm_year);
7616 req.month = 1 + tm.tm_mon;
7617 req.day = tm.tm_mday;
7618 req.hour = tm.tm_hour;
7619 req.minute = tm.tm_min;
7620 req.second = tm.tm_sec;
7621 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7624 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
7628 sw_tmp = (*sw & ~mask) | hw;
7629 if (hw < (*sw & mask))
7631 WRITE_ONCE(*sw, sw_tmp);
7634 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
7635 int count, bool ignore_zero)
7639 for (i = 0; i < count; i++) {
7640 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
7642 if (ignore_zero && !hw)
7645 if (masks[i] == -1ULL)
7648 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
7652 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
7654 if (!stats->hw_stats)
7657 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7658 stats->hw_masks, stats->len / 8, false);
7661 static void bnxt_accumulate_all_stats(struct bnxt *bp)
7663 struct bnxt_stats_mem *ring0_stats;
7664 bool ignore_zero = false;
7667 /* Chip bug. Counter intermittently becomes 0. */
7668 if (bp->flags & BNXT_FLAG_CHIP_P5)
7671 for (i = 0; i < bp->cp_nr_rings; i++) {
7672 struct bnxt_napi *bnapi = bp->bnapi[i];
7673 struct bnxt_cp_ring_info *cpr;
7674 struct bnxt_stats_mem *stats;
7676 cpr = &bnapi->cp_ring;
7677 stats = &cpr->stats;
7679 ring0_stats = stats;
7680 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7681 ring0_stats->hw_masks,
7682 ring0_stats->len / 8, ignore_zero);
7684 if (bp->flags & BNXT_FLAG_PORT_STATS) {
7685 struct bnxt_stats_mem *stats = &bp->port_stats;
7686 __le64 *hw_stats = stats->hw_stats;
7687 u64 *sw_stats = stats->sw_stats;
7688 u64 *masks = stats->hw_masks;
7691 cnt = sizeof(struct rx_port_stats) / 8;
7692 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7694 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7695 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7696 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7697 cnt = sizeof(struct tx_port_stats) / 8;
7698 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7700 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
7701 bnxt_accumulate_stats(&bp->rx_port_stats_ext);
7702 bnxt_accumulate_stats(&bp->tx_port_stats_ext);
7706 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
7708 struct bnxt_pf_info *pf = &bp->pf;
7709 struct hwrm_port_qstats_input req = {0};
7711 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7714 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7718 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7719 req.port_id = cpu_to_le16(pf->port_id);
7720 req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
7721 BNXT_TX_PORT_STATS_BYTE_OFFSET);
7722 req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
7723 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7726 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
7728 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7729 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7730 struct hwrm_port_qstats_ext_input req = {0};
7731 struct bnxt_pf_info *pf = &bp->pf;
7735 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7738 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7741 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7743 req.port_id = cpu_to_le16(pf->port_id);
7744 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7745 req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
7746 tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
7747 sizeof(struct tx_port_stats_ext) : 0;
7748 req.tx_stat_size = cpu_to_le16(tx_stat_size);
7749 req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
7750 mutex_lock(&bp->hwrm_cmd_lock);
7751 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7753 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7754 bp->fw_tx_stats_ext_size = tx_stat_size ?
7755 le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7757 bp->fw_rx_stats_ext_size = 0;
7758 bp->fw_tx_stats_ext_size = 0;
7763 if (bp->fw_tx_stats_ext_size <=
7764 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7765 mutex_unlock(&bp->hwrm_cmd_lock);
7766 bp->pri2cos_valid = 0;
7770 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
7771 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
7773 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
7775 struct hwrm_queue_pri2cos_qcfg_output *resp2;
7779 resp2 = bp->hwrm_cmd_resp_addr;
7780 pri2cos = &resp2->pri0_cos_queue_id;
7781 for (i = 0; i < 8; i++) {
7782 u8 queue_id = pri2cos[i];
7785 /* Per port queue IDs start from 0, 10, 20, etc */
7786 queue_idx = queue_id % 10;
7787 if (queue_idx > BNXT_MAX_QUEUE) {
7788 bp->pri2cos_valid = false;
7791 for (j = 0; j < bp->max_q; j++) {
7792 if (bp->q_ids[j] == queue_id)
7793 bp->pri2cos_idx[i] = queue_idx;
7796 bp->pri2cos_valid = 1;
7799 mutex_unlock(&bp->hwrm_cmd_lock);
7803 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
7805 if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
7806 bnxt_hwrm_tunnel_dst_port_free(
7807 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7808 if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
7809 bnxt_hwrm_tunnel_dst_port_free(
7810 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7813 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
7819 tpa_flags = bp->flags & BNXT_FLAG_TPA;
7820 else if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
7822 for (i = 0; i < bp->nr_vnics; i++) {
7823 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
7825 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
7833 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
7837 for (i = 0; i < bp->nr_vnics; i++)
7838 bnxt_hwrm_vnic_set_rss(bp, i, false);
7841 static void bnxt_clear_vnic(struct bnxt *bp)
7846 bnxt_hwrm_clear_vnic_filter(bp);
7847 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
7848 /* clear all RSS setting before free vnic ctx */
7849 bnxt_hwrm_clear_vnic_rss(bp);
7850 bnxt_hwrm_vnic_ctx_free(bp);
7852 /* before free the vnic, undo the vnic tpa settings */
7853 if (bp->flags & BNXT_FLAG_TPA)
7854 bnxt_set_tpa(bp, false);
7855 bnxt_hwrm_vnic_free(bp);
7856 if (bp->flags & BNXT_FLAG_CHIP_P5)
7857 bnxt_hwrm_vnic_ctx_free(bp);
7860 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
7863 bnxt_clear_vnic(bp);
7864 bnxt_hwrm_ring_free(bp, close_path);
7865 bnxt_hwrm_ring_grp_free(bp);
7867 bnxt_hwrm_stat_ctx_free(bp);
7868 bnxt_hwrm_free_tunnel_ports(bp);
7872 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
7874 struct hwrm_func_cfg_input req = {0};
7876 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7877 req.fid = cpu_to_le16(0xffff);
7878 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
7879 if (br_mode == BRIDGE_MODE_VEB)
7880 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
7881 else if (br_mode == BRIDGE_MODE_VEPA)
7882 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
7885 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7888 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
7890 struct hwrm_func_cfg_input req = {0};
7892 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
7895 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7896 req.fid = cpu_to_le16(0xffff);
7897 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
7898 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
7900 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
7902 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7905 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
7907 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
7910 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
7913 /* allocate context for vnic */
7914 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
7916 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7918 goto vnic_setup_err;
7920 bp->rsscos_nr_ctxs++;
7922 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7923 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
7925 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
7927 goto vnic_setup_err;
7929 bp->rsscos_nr_ctxs++;
7933 /* configure default vnic, ring grp */
7934 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7936 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7938 goto vnic_setup_err;
7941 /* Enable RSS hashing on vnic */
7942 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
7944 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
7946 goto vnic_setup_err;
7949 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7950 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7952 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
7961 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
7965 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
7966 for (i = 0; i < nr_ctxs; i++) {
7967 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
7969 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
7973 bp->rsscos_nr_ctxs++;
7978 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
7980 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
7984 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7986 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7990 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7991 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7993 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8000 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8002 if (bp->flags & BNXT_FLAG_CHIP_P5)
8003 return __bnxt_setup_vnic_p5(bp, vnic_id);
8005 return __bnxt_setup_vnic(bp, vnic_id);
8008 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8010 #ifdef CONFIG_RFS_ACCEL
8013 if (bp->flags & BNXT_FLAG_CHIP_P5)
8016 for (i = 0; i < bp->rx_nr_rings; i++) {
8017 struct bnxt_vnic_info *vnic;
8018 u16 vnic_id = i + 1;
8021 if (vnic_id >= bp->nr_vnics)
8024 vnic = &bp->vnic_info[vnic_id];
8025 vnic->flags |= BNXT_VNIC_RFS_FLAG;
8026 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8027 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8028 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8030 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8034 rc = bnxt_setup_vnic(bp, vnic_id);
8044 /* Allow PF and VF with default VLAN to be in promiscuous mode */
8045 static bool bnxt_promisc_ok(struct bnxt *bp)
8047 #ifdef CONFIG_BNXT_SRIOV
8048 if (BNXT_VF(bp) && !bp->vf.vlan)
8054 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8056 unsigned int rc = 0;
8058 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8060 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8065 rc = bnxt_hwrm_vnic_cfg(bp, 1);
8067 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8074 static int bnxt_cfg_rx_mode(struct bnxt *);
8075 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8077 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8079 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8081 unsigned int rx_nr_rings = bp->rx_nr_rings;
8084 rc = bnxt_hwrm_stat_ctx_alloc(bp);
8086 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8092 rc = bnxt_hwrm_ring_alloc(bp);
8094 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8098 rc = bnxt_hwrm_ring_grp_alloc(bp);
8100 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8104 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8107 /* default vnic 0 */
8108 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8110 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8114 rc = bnxt_setup_vnic(bp, 0);
8118 if (bp->flags & BNXT_FLAG_RFS) {
8119 rc = bnxt_alloc_rfs_vnics(bp);
8124 if (bp->flags & BNXT_FLAG_TPA) {
8125 rc = bnxt_set_tpa(bp, true);
8131 bnxt_update_vf_mac(bp);
8133 /* Filter for default vnic 0 */
8134 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8136 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8139 vnic->uc_filter_count = 1;
8142 if (bp->dev->flags & IFF_BROADCAST)
8143 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8145 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
8146 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8148 if (bp->dev->flags & IFF_ALLMULTI) {
8149 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8150 vnic->mc_list_count = 0;
8154 bnxt_mc_list_updated(bp, &mask);
8155 vnic->rx_mask |= mask;
8158 rc = bnxt_cfg_rx_mode(bp);
8162 rc = bnxt_hwrm_set_coal(bp);
8164 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8167 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8168 rc = bnxt_setup_nitroa0_vnic(bp);
8170 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8175 bnxt_hwrm_func_qcfg(bp);
8176 netdev_update_features(bp->dev);
8182 bnxt_hwrm_resource_free(bp, 0, true);
8187 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8189 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8193 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8195 bnxt_init_cp_rings(bp);
8196 bnxt_init_rx_rings(bp);
8197 bnxt_init_tx_rings(bp);
8198 bnxt_init_ring_grps(bp, irq_re_init);
8199 bnxt_init_vnics(bp);
8201 return bnxt_init_chip(bp, irq_re_init);
8204 static int bnxt_set_real_num_queues(struct bnxt *bp)
8207 struct net_device *dev = bp->dev;
8209 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8210 bp->tx_nr_rings_xdp);
8214 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8218 #ifdef CONFIG_RFS_ACCEL
8219 if (bp->flags & BNXT_FLAG_RFS)
8220 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8226 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8229 int _rx = *rx, _tx = *tx;
8232 *rx = min_t(int, _rx, max);
8233 *tx = min_t(int, _tx, max);
8238 while (_rx + _tx > max) {
8239 if (_rx > _tx && _rx > 1)
8250 static void bnxt_setup_msix(struct bnxt *bp)
8252 const int len = sizeof(bp->irq_tbl[0].name);
8253 struct net_device *dev = bp->dev;
8256 tcs = netdev_get_num_tc(dev);
8260 for (i = 0; i < tcs; i++) {
8261 count = bp->tx_nr_rings_per_tc;
8263 netdev_set_tc_queue(dev, i, count, off);
8267 for (i = 0; i < bp->cp_nr_rings; i++) {
8268 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8271 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8273 else if (i < bp->rx_nr_rings)
8278 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8280 bp->irq_tbl[map_idx].handler = bnxt_msix;
8284 static void bnxt_setup_inta(struct bnxt *bp)
8286 const int len = sizeof(bp->irq_tbl[0].name);
8288 if (netdev_get_num_tc(bp->dev))
8289 netdev_reset_tc(bp->dev);
8291 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8293 bp->irq_tbl[0].handler = bnxt_inta;
8296 static int bnxt_setup_int_mode(struct bnxt *bp)
8300 if (bp->flags & BNXT_FLAG_USING_MSIX)
8301 bnxt_setup_msix(bp);
8303 bnxt_setup_inta(bp);
8305 rc = bnxt_set_real_num_queues(bp);
8309 #ifdef CONFIG_RFS_ACCEL
8310 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8312 return bp->hw_resc.max_rsscos_ctxs;
8315 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8317 return bp->hw_resc.max_vnics;
8321 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8323 return bp->hw_resc.max_stat_ctxs;
8326 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8328 return bp->hw_resc.max_cp_rings;
8331 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8333 unsigned int cp = bp->hw_resc.max_cp_rings;
8335 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8336 cp -= bnxt_get_ulp_msix_num(bp);
8341 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8343 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8345 if (bp->flags & BNXT_FLAG_CHIP_P5)
8346 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8348 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8351 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8353 bp->hw_resc.max_irqs = max_irqs;
8356 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8360 cp = bnxt_get_max_func_cp_rings_for_en(bp);
8361 if (bp->flags & BNXT_FLAG_CHIP_P5)
8362 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8364 return cp - bp->cp_nr_rings;
8367 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8369 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8372 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8374 int max_cp = bnxt_get_max_func_cp_rings(bp);
8375 int max_irq = bnxt_get_max_func_irqs(bp);
8376 int total_req = bp->cp_nr_rings + num;
8377 int max_idx, avail_msix;
8379 max_idx = bp->total_irqs;
8380 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8381 max_idx = min_t(int, bp->total_irqs, max_cp);
8382 avail_msix = max_idx - bp->cp_nr_rings;
8383 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8386 if (max_irq < total_req) {
8387 num = max_irq - bp->cp_nr_rings;
8394 static int bnxt_get_num_msix(struct bnxt *bp)
8396 if (!BNXT_NEW_RM(bp))
8397 return bnxt_get_max_func_irqs(bp);
8399 return bnxt_nq_rings_in_use(bp);
8402 static int bnxt_init_msix(struct bnxt *bp)
8404 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8405 struct msix_entry *msix_ent;
8407 total_vecs = bnxt_get_num_msix(bp);
8408 max = bnxt_get_max_func_irqs(bp);
8409 if (total_vecs > max)
8415 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8419 for (i = 0; i < total_vecs; i++) {
8420 msix_ent[i].entry = i;
8421 msix_ent[i].vector = 0;
8424 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8427 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8428 ulp_msix = bnxt_get_ulp_msix_num(bp);
8429 if (total_vecs < 0 || total_vecs < ulp_msix) {
8431 goto msix_setup_exit;
8434 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8436 for (i = 0; i < total_vecs; i++)
8437 bp->irq_tbl[i].vector = msix_ent[i].vector;
8439 bp->total_irqs = total_vecs;
8440 /* Trim rings based upon num of vectors allocated */
8441 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8442 total_vecs - ulp_msix, min == 1);
8444 goto msix_setup_exit;
8446 bp->cp_nr_rings = (min == 1) ?
8447 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8448 bp->tx_nr_rings + bp->rx_nr_rings;
8452 goto msix_setup_exit;
8454 bp->flags |= BNXT_FLAG_USING_MSIX;
8459 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8462 pci_disable_msix(bp->pdev);
8467 static int bnxt_init_inta(struct bnxt *bp)
8469 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
8474 bp->rx_nr_rings = 1;
8475 bp->tx_nr_rings = 1;
8476 bp->cp_nr_rings = 1;
8477 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8478 bp->irq_tbl[0].vector = bp->pdev->irq;
8482 static int bnxt_init_int_mode(struct bnxt *bp)
8486 if (bp->flags & BNXT_FLAG_MSIX_CAP)
8487 rc = bnxt_init_msix(bp);
8489 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8490 /* fallback to INTA */
8491 rc = bnxt_init_inta(bp);
8496 static void bnxt_clear_int_mode(struct bnxt *bp)
8498 if (bp->flags & BNXT_FLAG_USING_MSIX)
8499 pci_disable_msix(bp->pdev);
8503 bp->flags &= ~BNXT_FLAG_USING_MSIX;
8506 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8508 int tcs = netdev_get_num_tc(bp->dev);
8509 bool irq_cleared = false;
8512 if (!bnxt_need_reserve_rings(bp))
8515 if (irq_re_init && BNXT_NEW_RM(bp) &&
8516 bnxt_get_num_msix(bp) != bp->total_irqs) {
8517 bnxt_ulp_irq_stop(bp);
8518 bnxt_clear_int_mode(bp);
8521 rc = __bnxt_reserve_rings(bp);
8524 rc = bnxt_init_int_mode(bp);
8525 bnxt_ulp_irq_restart(bp, rc);
8528 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8531 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
8532 netdev_err(bp->dev, "tx ring reservation failure\n");
8533 netdev_reset_tc(bp->dev);
8534 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8540 static void bnxt_free_irq(struct bnxt *bp)
8542 struct bnxt_irq *irq;
8545 #ifdef CONFIG_RFS_ACCEL
8546 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
8547 bp->dev->rx_cpu_rmap = NULL;
8549 if (!bp->irq_tbl || !bp->bnapi)
8552 for (i = 0; i < bp->cp_nr_rings; i++) {
8553 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8555 irq = &bp->irq_tbl[map_idx];
8556 if (irq->requested) {
8557 if (irq->have_cpumask) {
8558 irq_set_affinity_hint(irq->vector, NULL);
8559 free_cpumask_var(irq->cpu_mask);
8560 irq->have_cpumask = 0;
8562 free_irq(irq->vector, bp->bnapi[i]);
8569 static int bnxt_request_irq(struct bnxt *bp)
8572 unsigned long flags = 0;
8573 #ifdef CONFIG_RFS_ACCEL
8574 struct cpu_rmap *rmap;
8577 rc = bnxt_setup_int_mode(bp);
8579 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
8583 #ifdef CONFIG_RFS_ACCEL
8584 rmap = bp->dev->rx_cpu_rmap;
8586 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
8587 flags = IRQF_SHARED;
8589 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
8590 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8591 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
8593 #ifdef CONFIG_RFS_ACCEL
8594 if (rmap && bp->bnapi[i]->rx_ring) {
8595 rc = irq_cpu_rmap_add(rmap, irq->vector);
8597 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
8602 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
8609 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
8610 int numa_node = dev_to_node(&bp->pdev->dev);
8612 irq->have_cpumask = 1;
8613 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
8615 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
8617 netdev_warn(bp->dev,
8618 "Set affinity failed, IRQ = %d\n",
8627 static void bnxt_del_napi(struct bnxt *bp)
8634 for (i = 0; i < bp->cp_nr_rings; i++) {
8635 struct bnxt_napi *bnapi = bp->bnapi[i];
8637 napi_hash_del(&bnapi->napi);
8638 netif_napi_del(&bnapi->napi);
8640 /* We called napi_hash_del() before netif_napi_del(), we need
8641 * to respect an RCU grace period before freeing napi structures.
8646 static void bnxt_init_napi(struct bnxt *bp)
8649 unsigned int cp_nr_rings = bp->cp_nr_rings;
8650 struct bnxt_napi *bnapi;
8652 if (bp->flags & BNXT_FLAG_USING_MSIX) {
8653 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8655 if (bp->flags & BNXT_FLAG_CHIP_P5)
8656 poll_fn = bnxt_poll_p5;
8657 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8659 for (i = 0; i < cp_nr_rings; i++) {
8660 bnapi = bp->bnapi[i];
8661 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8663 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8664 bnapi = bp->bnapi[cp_nr_rings];
8665 netif_napi_add(bp->dev, &bnapi->napi,
8666 bnxt_poll_nitroa0, 64);
8669 bnapi = bp->bnapi[0];
8670 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8674 static void bnxt_disable_napi(struct bnxt *bp)
8681 for (i = 0; i < bp->cp_nr_rings; i++) {
8682 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8684 if (bp->bnapi[i]->rx_ring)
8685 cancel_work_sync(&cpr->dim.work);
8687 napi_disable(&bp->bnapi[i]->napi);
8691 static void bnxt_enable_napi(struct bnxt *bp)
8695 for (i = 0; i < bp->cp_nr_rings; i++) {
8696 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8697 bp->bnapi[i]->in_reset = false;
8699 if (bp->bnapi[i]->rx_ring) {
8700 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8701 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8703 napi_enable(&bp->bnapi[i]->napi);
8707 void bnxt_tx_disable(struct bnxt *bp)
8710 struct bnxt_tx_ring_info *txr;
8713 for (i = 0; i < bp->tx_nr_rings; i++) {
8714 txr = &bp->tx_ring[i];
8715 txr->dev_state = BNXT_DEV_STATE_CLOSING;
8718 /* Stop all TX queues */
8719 netif_tx_disable(bp->dev);
8720 netif_carrier_off(bp->dev);
8723 void bnxt_tx_enable(struct bnxt *bp)
8726 struct bnxt_tx_ring_info *txr;
8728 for (i = 0; i < bp->tx_nr_rings; i++) {
8729 txr = &bp->tx_ring[i];
8732 netif_tx_wake_all_queues(bp->dev);
8733 if (bp->link_info.link_up)
8734 netif_carrier_on(bp->dev);
8737 static void bnxt_report_link(struct bnxt *bp)
8739 if (bp->link_info.link_up) {
8741 const char *flow_ctrl;
8745 netif_carrier_on(bp->dev);
8746 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
8750 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
8751 flow_ctrl = "ON - receive & transmit";
8752 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
8753 flow_ctrl = "ON - transmit";
8754 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
8755 flow_ctrl = "ON - receive";
8758 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
8759 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
8760 speed, duplex, flow_ctrl);
8761 if (bp->flags & BNXT_FLAG_EEE_CAP)
8762 netdev_info(bp->dev, "EEE is %s\n",
8763 bp->eee.eee_active ? "active" :
8765 fec = bp->link_info.fec_cfg;
8766 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
8767 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
8768 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
8769 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
8770 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
8772 netif_carrier_off(bp->dev);
8773 netdev_err(bp->dev, "NIC Link is Down\n");
8777 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
8780 struct hwrm_port_phy_qcaps_input req = {0};
8781 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8782 struct bnxt_link_info *link_info = &bp->link_info;
8784 bp->flags &= ~BNXT_FLAG_EEE_CAP;
8786 bp->test_info->flags &= ~(BNXT_TEST_FL_EXT_LPBK |
8787 BNXT_TEST_FL_AN_PHY_LPBK);
8788 if (bp->hwrm_spec_code < 0x10201)
8791 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
8793 mutex_lock(&bp->hwrm_cmd_lock);
8794 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8796 goto hwrm_phy_qcaps_exit;
8798 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
8799 struct ethtool_eee *eee = &bp->eee;
8800 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
8802 bp->flags |= BNXT_FLAG_EEE_CAP;
8803 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8804 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
8805 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
8806 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
8807 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
8809 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
8811 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
8813 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_AUTONEG_LPBK_SUPPORTED) {
8815 bp->test_info->flags |= BNXT_TEST_FL_AN_PHY_LPBK;
8817 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_SHARED_PHY_CFG_SUPPORTED) {
8819 bp->fw_cap |= BNXT_FW_CAP_SHARED_PORT_CFG;
8821 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_CUMULATIVE_COUNTERS_ON_RESET)
8822 bp->fw_cap |= BNXT_FW_CAP_PORT_STATS_NO_RESET;
8824 if (resp->supported_speeds_auto_mode)
8825 link_info->support_auto_speeds =
8826 le16_to_cpu(resp->supported_speeds_auto_mode);
8828 bp->port_count = resp->port_cnt;
8830 hwrm_phy_qcaps_exit:
8831 mutex_unlock(&bp->hwrm_cmd_lock);
8835 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
8838 struct bnxt_link_info *link_info = &bp->link_info;
8839 struct hwrm_port_phy_qcfg_input req = {0};
8840 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8841 u8 link_up = link_info->link_up;
8844 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
8846 mutex_lock(&bp->hwrm_cmd_lock);
8847 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8849 mutex_unlock(&bp->hwrm_cmd_lock);
8853 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
8854 link_info->phy_link_status = resp->link;
8855 link_info->duplex = resp->duplex_cfg;
8856 if (bp->hwrm_spec_code >= 0x10800)
8857 link_info->duplex = resp->duplex_state;
8858 link_info->pause = resp->pause;
8859 link_info->auto_mode = resp->auto_mode;
8860 link_info->auto_pause_setting = resp->auto_pause;
8861 link_info->lp_pause = resp->link_partner_adv_pause;
8862 link_info->force_pause_setting = resp->force_pause;
8863 link_info->duplex_setting = resp->duplex_cfg;
8864 if (link_info->phy_link_status == BNXT_LINK_LINK)
8865 link_info->link_speed = le16_to_cpu(resp->link_speed);
8867 link_info->link_speed = 0;
8868 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
8869 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
8870 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
8871 link_info->lp_auto_link_speeds =
8872 le16_to_cpu(resp->link_partner_adv_speeds);
8873 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
8874 link_info->phy_ver[0] = resp->phy_maj;
8875 link_info->phy_ver[1] = resp->phy_min;
8876 link_info->phy_ver[2] = resp->phy_bld;
8877 link_info->media_type = resp->media_type;
8878 link_info->phy_type = resp->phy_type;
8879 link_info->transceiver = resp->xcvr_pkg_type;
8880 link_info->phy_addr = resp->eee_config_phy_addr &
8881 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
8882 link_info->module_status = resp->module_status;
8884 if (bp->flags & BNXT_FLAG_EEE_CAP) {
8885 struct ethtool_eee *eee = &bp->eee;
8888 eee->eee_active = 0;
8889 if (resp->eee_config_phy_addr &
8890 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
8891 eee->eee_active = 1;
8892 fw_speeds = le16_to_cpu(
8893 resp->link_partner_adv_eee_link_speed_mask);
8894 eee->lp_advertised =
8895 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8898 /* Pull initial EEE config */
8899 if (!chng_link_state) {
8900 if (resp->eee_config_phy_addr &
8901 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
8902 eee->eee_enabled = 1;
8904 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
8906 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8908 if (resp->eee_config_phy_addr &
8909 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
8912 eee->tx_lpi_enabled = 1;
8913 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
8914 eee->tx_lpi_timer = le32_to_cpu(tmr) &
8915 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
8920 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
8921 if (bp->hwrm_spec_code >= 0x10504)
8922 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
8924 /* TODO: need to add more logic to report VF link */
8925 if (chng_link_state) {
8926 if (link_info->phy_link_status == BNXT_LINK_LINK)
8927 link_info->link_up = 1;
8929 link_info->link_up = 0;
8930 if (link_up != link_info->link_up)
8931 bnxt_report_link(bp);
8933 /* alwasy link down if not require to update link state */
8934 link_info->link_up = 0;
8936 mutex_unlock(&bp->hwrm_cmd_lock);
8938 if (!BNXT_PHY_CFG_ABLE(bp))
8941 diff = link_info->support_auto_speeds ^ link_info->advertising;
8942 if ((link_info->support_auto_speeds | diff) !=
8943 link_info->support_auto_speeds) {
8944 /* An advertised speed is no longer supported, so we need to
8945 * update the advertisement settings. Caller holds RTNL
8946 * so we can modify link settings.
8948 link_info->advertising = link_info->support_auto_speeds;
8949 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
8950 bnxt_hwrm_set_link_setting(bp, true, false);
8955 static void bnxt_get_port_module_status(struct bnxt *bp)
8957 struct bnxt_link_info *link_info = &bp->link_info;
8958 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
8961 if (bnxt_update_link(bp, true))
8964 module_status = link_info->module_status;
8965 switch (module_status) {
8966 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
8967 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
8968 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
8969 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
8971 if (bp->hwrm_spec_code >= 0x10201) {
8972 netdev_warn(bp->dev, "Module part number %s\n",
8973 resp->phy_vendor_partnumber);
8975 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
8976 netdev_warn(bp->dev, "TX is disabled\n");
8977 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
8978 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
8983 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
8985 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
8986 if (bp->hwrm_spec_code >= 0x10201)
8988 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
8989 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8990 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
8991 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8992 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
8994 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
8996 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8997 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
8998 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8999 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9001 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9002 if (bp->hwrm_spec_code >= 0x10201) {
9003 req->auto_pause = req->force_pause;
9004 req->enables |= cpu_to_le32(
9005 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9010 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
9011 struct hwrm_port_phy_cfg_input *req)
9013 u8 autoneg = bp->link_info.autoneg;
9014 u16 fw_link_speed = bp->link_info.req_link_speed;
9015 u16 advertising = bp->link_info.advertising;
9017 if (autoneg & BNXT_AUTONEG_SPEED) {
9019 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9021 req->enables |= cpu_to_le32(
9022 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9023 req->auto_link_speed_mask = cpu_to_le16(advertising);
9025 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9027 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9029 req->force_link_speed = cpu_to_le16(fw_link_speed);
9030 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9033 /* tell chimp that the setting takes effect immediately */
9034 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9037 int bnxt_hwrm_set_pause(struct bnxt *bp)
9039 struct hwrm_port_phy_cfg_input req = {0};
9042 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9043 bnxt_hwrm_set_pause_common(bp, &req);
9045 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9046 bp->link_info.force_link_chng)
9047 bnxt_hwrm_set_link_common(bp, &req);
9049 mutex_lock(&bp->hwrm_cmd_lock);
9050 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9051 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9052 /* since changing of pause setting doesn't trigger any link
9053 * change event, the driver needs to update the current pause
9054 * result upon successfully return of the phy_cfg command
9056 bp->link_info.pause =
9057 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9058 bp->link_info.auto_pause_setting = 0;
9059 if (!bp->link_info.force_link_chng)
9060 bnxt_report_link(bp);
9062 bp->link_info.force_link_chng = false;
9063 mutex_unlock(&bp->hwrm_cmd_lock);
9067 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9068 struct hwrm_port_phy_cfg_input *req)
9070 struct ethtool_eee *eee = &bp->eee;
9072 if (eee->eee_enabled) {
9074 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9076 if (eee->tx_lpi_enabled)
9077 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9079 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9081 req->flags |= cpu_to_le32(flags);
9082 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9083 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9084 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9086 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9090 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9092 struct hwrm_port_phy_cfg_input req = {0};
9094 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9096 bnxt_hwrm_set_pause_common(bp, &req);
9098 bnxt_hwrm_set_link_common(bp, &req);
9101 bnxt_hwrm_set_eee(bp, &req);
9102 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9105 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9107 struct hwrm_port_phy_cfg_input req = {0};
9109 if (!BNXT_SINGLE_PF(bp))
9112 if (pci_num_vf(bp->pdev))
9115 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9116 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9117 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9120 static int bnxt_fw_init_one(struct bnxt *bp);
9122 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9124 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9125 struct hwrm_func_drv_if_change_input req = {0};
9126 bool resc_reinit = false, fw_reset = false;
9130 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9133 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9135 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9136 mutex_lock(&bp->hwrm_cmd_lock);
9137 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9139 flags = le32_to_cpu(resp->flags);
9140 mutex_unlock(&bp->hwrm_cmd_lock);
9147 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9149 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9152 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9153 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9156 if (resc_reinit || fw_reset) {
9158 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9160 bnxt_free_ctx_mem(bp);
9164 rc = bnxt_fw_init_one(bp);
9166 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9169 bnxt_clear_int_mode(bp);
9170 rc = bnxt_init_int_mode(bp);
9172 netdev_err(bp->dev, "init int mode failed\n");
9175 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9177 if (BNXT_NEW_RM(bp)) {
9178 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9180 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9181 hw_resc->resv_cp_rings = 0;
9182 hw_resc->resv_stat_ctxs = 0;
9183 hw_resc->resv_irqs = 0;
9184 hw_resc->resv_tx_rings = 0;
9185 hw_resc->resv_rx_rings = 0;
9186 hw_resc->resv_hw_ring_grps = 0;
9187 hw_resc->resv_vnics = 0;
9189 bp->tx_nr_rings = 0;
9190 bp->rx_nr_rings = 0;
9197 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9199 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9200 struct hwrm_port_led_qcaps_input req = {0};
9201 struct bnxt_pf_info *pf = &bp->pf;
9205 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9208 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9209 req.port_id = cpu_to_le16(pf->port_id);
9210 mutex_lock(&bp->hwrm_cmd_lock);
9211 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9213 mutex_unlock(&bp->hwrm_cmd_lock);
9216 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9219 bp->num_leds = resp->num_leds;
9220 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9222 for (i = 0; i < bp->num_leds; i++) {
9223 struct bnxt_led_info *led = &bp->leds[i];
9224 __le16 caps = led->led_state_caps;
9226 if (!led->led_group_id ||
9227 !BNXT_LED_ALT_BLINK_CAP(caps)) {
9233 mutex_unlock(&bp->hwrm_cmd_lock);
9237 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9239 struct hwrm_wol_filter_alloc_input req = {0};
9240 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9243 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9244 req.port_id = cpu_to_le16(bp->pf.port_id);
9245 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9246 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9247 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9248 mutex_lock(&bp->hwrm_cmd_lock);
9249 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9251 bp->wol_filter_id = resp->wol_filter_id;
9252 mutex_unlock(&bp->hwrm_cmd_lock);
9256 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9258 struct hwrm_wol_filter_free_input req = {0};
9260 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9261 req.port_id = cpu_to_le16(bp->pf.port_id);
9262 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9263 req.wol_filter_id = bp->wol_filter_id;
9264 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9267 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9269 struct hwrm_wol_filter_qcfg_input req = {0};
9270 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9271 u16 next_handle = 0;
9274 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9275 req.port_id = cpu_to_le16(bp->pf.port_id);
9276 req.handle = cpu_to_le16(handle);
9277 mutex_lock(&bp->hwrm_cmd_lock);
9278 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9280 next_handle = le16_to_cpu(resp->next_handle);
9281 if (next_handle != 0) {
9282 if (resp->wol_type ==
9283 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9285 bp->wol_filter_id = resp->wol_filter_id;
9289 mutex_unlock(&bp->hwrm_cmd_lock);
9293 static void bnxt_get_wol_settings(struct bnxt *bp)
9298 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9302 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9303 } while (handle && handle != 0xffff);
9306 #ifdef CONFIG_BNXT_HWMON
9307 static ssize_t bnxt_show_temp(struct device *dev,
9308 struct device_attribute *devattr, char *buf)
9310 struct hwrm_temp_monitor_query_input req = {0};
9311 struct hwrm_temp_monitor_query_output *resp;
9312 struct bnxt *bp = dev_get_drvdata(dev);
9315 resp = bp->hwrm_cmd_resp_addr;
9316 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9317 mutex_lock(&bp->hwrm_cmd_lock);
9318 if (!_hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
9319 len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
9320 mutex_unlock(&bp->hwrm_cmd_lock);
9325 return sprintf(buf, "unknown\n");
9327 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
9329 static struct attribute *bnxt_attrs[] = {
9330 &sensor_dev_attr_temp1_input.dev_attr.attr,
9333 ATTRIBUTE_GROUPS(bnxt);
9335 static void bnxt_hwmon_close(struct bnxt *bp)
9337 if (bp->hwmon_dev) {
9338 hwmon_device_unregister(bp->hwmon_dev);
9339 bp->hwmon_dev = NULL;
9343 static void bnxt_hwmon_open(struct bnxt *bp)
9345 struct pci_dev *pdev = bp->pdev;
9350 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
9351 DRV_MODULE_NAME, bp,
9353 if (IS_ERR(bp->hwmon_dev)) {
9354 bp->hwmon_dev = NULL;
9355 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
9359 static void bnxt_hwmon_close(struct bnxt *bp)
9363 static void bnxt_hwmon_open(struct bnxt *bp)
9368 static bool bnxt_eee_config_ok(struct bnxt *bp)
9370 struct ethtool_eee *eee = &bp->eee;
9371 struct bnxt_link_info *link_info = &bp->link_info;
9373 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
9376 if (eee->eee_enabled) {
9378 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
9380 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9381 eee->eee_enabled = 0;
9384 if (eee->advertised & ~advertising) {
9385 eee->advertised = advertising & eee->supported;
9392 static int bnxt_update_phy_setting(struct bnxt *bp)
9395 bool update_link = false;
9396 bool update_pause = false;
9397 bool update_eee = false;
9398 struct bnxt_link_info *link_info = &bp->link_info;
9400 rc = bnxt_update_link(bp, true);
9402 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
9406 if (!BNXT_SINGLE_PF(bp))
9409 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9410 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
9411 link_info->req_flow_ctrl)
9412 update_pause = true;
9413 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9414 link_info->force_pause_setting != link_info->req_flow_ctrl)
9415 update_pause = true;
9416 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9417 if (BNXT_AUTO_MODE(link_info->auto_mode))
9419 if (link_info->req_link_speed != link_info->force_link_speed)
9421 if (link_info->req_duplex != link_info->duplex_setting)
9424 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
9426 if (link_info->advertising != link_info->auto_link_speeds)
9430 /* The last close may have shutdown the link, so need to call
9431 * PHY_CFG to bring it back up.
9433 if (!bp->link_info.link_up)
9436 if (!bnxt_eee_config_ok(bp))
9440 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
9441 else if (update_pause)
9442 rc = bnxt_hwrm_set_pause(bp);
9444 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
9452 /* Common routine to pre-map certain register block to different GRC window.
9453 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
9454 * in PF and 3 windows in VF that can be customized to map in different
9457 static void bnxt_preset_reg_win(struct bnxt *bp)
9460 /* CAG registers map to GRC window #4 */
9461 writel(BNXT_CAG_REG_BASE,
9462 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
9466 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
9468 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9472 bnxt_preset_reg_win(bp);
9473 netif_carrier_off(bp->dev);
9475 /* Reserve rings now if none were reserved at driver probe. */
9476 rc = bnxt_init_dflt_ring_mode(bp);
9478 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
9482 rc = bnxt_reserve_rings(bp, irq_re_init);
9485 if ((bp->flags & BNXT_FLAG_RFS) &&
9486 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
9487 /* disable RFS if falling back to INTA */
9488 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
9489 bp->flags &= ~BNXT_FLAG_RFS;
9492 rc = bnxt_alloc_mem(bp, irq_re_init);
9494 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9495 goto open_err_free_mem;
9500 rc = bnxt_request_irq(bp);
9502 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
9507 rc = bnxt_init_nic(bp, irq_re_init);
9509 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9513 bnxt_enable_napi(bp);
9514 bnxt_debug_dev_init(bp);
9517 mutex_lock(&bp->link_lock);
9518 rc = bnxt_update_phy_setting(bp);
9519 mutex_unlock(&bp->link_lock);
9521 netdev_warn(bp->dev, "failed to update phy settings\n");
9522 if (BNXT_SINGLE_PF(bp)) {
9523 bp->link_info.phy_retry = true;
9524 bp->link_info.phy_retry_expires =
9531 udp_tunnel_nic_reset_ntf(bp->dev);
9533 set_bit(BNXT_STATE_OPEN, &bp->state);
9534 bnxt_enable_int(bp);
9535 /* Enable TX queues */
9537 mod_timer(&bp->timer, jiffies + bp->current_interval);
9538 /* Poll link status and check for SFP+ module status */
9539 bnxt_get_port_module_status(bp);
9541 /* VF-reps may need to be re-opened after the PF is re-opened */
9543 bnxt_vf_reps_open(bp);
9552 bnxt_free_mem(bp, true);
9556 /* rtnl_lock held */
9557 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9561 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
9563 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
9569 /* rtnl_lock held, open the NIC half way by allocating all resources, but
9570 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
9573 int bnxt_half_open_nic(struct bnxt *bp)
9577 rc = bnxt_alloc_mem(bp, false);
9579 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9582 rc = bnxt_init_nic(bp, false);
9584 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9591 bnxt_free_mem(bp, false);
9596 /* rtnl_lock held, this call can only be made after a previous successful
9597 * call to bnxt_half_open_nic().
9599 void bnxt_half_close_nic(struct bnxt *bp)
9601 bnxt_hwrm_resource_free(bp, false, false);
9603 bnxt_free_mem(bp, false);
9606 static void bnxt_reenable_sriov(struct bnxt *bp)
9609 struct bnxt_pf_info *pf = &bp->pf;
9610 int n = pf->active_vfs;
9613 bnxt_cfg_hw_sriov(bp, &n, true);
9617 static int bnxt_open(struct net_device *dev)
9619 struct bnxt *bp = netdev_priv(dev);
9622 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9623 netdev_err(bp->dev, "A previous firmware reset did not complete, aborting\n");
9627 rc = bnxt_hwrm_if_change(bp, true);
9630 rc = __bnxt_open_nic(bp, true, true);
9632 bnxt_hwrm_if_change(bp, false);
9634 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
9635 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9636 bnxt_ulp_start(bp, 0);
9637 bnxt_reenable_sriov(bp);
9640 bnxt_hwmon_open(bp);
9646 static bool bnxt_drv_busy(struct bnxt *bp)
9648 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
9649 test_bit(BNXT_STATE_READ_STATS, &bp->state));
9652 static void bnxt_get_ring_stats(struct bnxt *bp,
9653 struct rtnl_link_stats64 *stats);
9655 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
9658 /* Close the VF-reps before closing PF */
9660 bnxt_vf_reps_close(bp);
9662 /* Change device state to avoid TX queue wake up's */
9663 bnxt_tx_disable(bp);
9665 clear_bit(BNXT_STATE_OPEN, &bp->state);
9666 smp_mb__after_atomic();
9667 while (bnxt_drv_busy(bp))
9670 /* Flush rings and and disable interrupts */
9671 bnxt_shutdown_nic(bp, irq_re_init);
9673 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
9675 bnxt_debug_dev_exit(bp);
9676 bnxt_disable_napi(bp);
9677 del_timer_sync(&bp->timer);
9680 /* Save ring stats before shutdown */
9681 if (bp->bnapi && irq_re_init)
9682 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
9687 bnxt_free_mem(bp, irq_re_init);
9690 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9694 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9695 /* If we get here, it means firmware reset is in progress
9696 * while we are trying to close. We can safely proceed with
9697 * the close because we are holding rtnl_lock(). Some firmware
9698 * messages may fail as we proceed to close. We set the
9699 * ABORT_ERR flag here so that the FW reset thread will later
9700 * abort when it gets the rtnl_lock() and sees the flag.
9702 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
9703 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9706 #ifdef CONFIG_BNXT_SRIOV
9707 if (bp->sriov_cfg) {
9708 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
9710 BNXT_SRIOV_CFG_WAIT_TMO);
9712 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
9715 __bnxt_close_nic(bp, irq_re_init, link_re_init);
9719 static int bnxt_close(struct net_device *dev)
9721 struct bnxt *bp = netdev_priv(dev);
9723 bnxt_hwmon_close(bp);
9724 bnxt_close_nic(bp, true, true);
9725 bnxt_hwrm_shutdown_link(bp);
9726 bnxt_hwrm_if_change(bp, false);
9730 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
9733 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
9734 struct hwrm_port_phy_mdio_read_input req = {0};
9737 if (bp->hwrm_spec_code < 0x10a00)
9740 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
9741 req.port_id = cpu_to_le16(bp->pf.port_id);
9742 req.phy_addr = phy_addr;
9743 req.reg_addr = cpu_to_le16(reg & 0x1f);
9744 if (mdio_phy_id_is_c45(phy_addr)) {
9746 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9747 req.dev_addr = mdio_phy_id_devad(phy_addr);
9748 req.reg_addr = cpu_to_le16(reg);
9751 mutex_lock(&bp->hwrm_cmd_lock);
9752 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9754 *val = le16_to_cpu(resp->reg_data);
9755 mutex_unlock(&bp->hwrm_cmd_lock);
9759 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
9762 struct hwrm_port_phy_mdio_write_input req = {0};
9764 if (bp->hwrm_spec_code < 0x10a00)
9767 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
9768 req.port_id = cpu_to_le16(bp->pf.port_id);
9769 req.phy_addr = phy_addr;
9770 req.reg_addr = cpu_to_le16(reg & 0x1f);
9771 if (mdio_phy_id_is_c45(phy_addr)) {
9773 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9774 req.dev_addr = mdio_phy_id_devad(phy_addr);
9775 req.reg_addr = cpu_to_le16(reg);
9777 req.reg_data = cpu_to_le16(val);
9779 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9782 /* rtnl_lock held */
9783 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9785 struct mii_ioctl_data *mdio = if_mii(ifr);
9786 struct bnxt *bp = netdev_priv(dev);
9791 mdio->phy_id = bp->link_info.phy_addr;
9797 if (!netif_running(dev))
9800 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
9802 mdio->val_out = mii_regval;
9807 if (!netif_running(dev))
9810 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
9820 static void bnxt_get_ring_stats(struct bnxt *bp,
9821 struct rtnl_link_stats64 *stats)
9825 for (i = 0; i < bp->cp_nr_rings; i++) {
9826 struct bnxt_napi *bnapi = bp->bnapi[i];
9827 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9828 u64 *sw = cpr->stats.sw_stats;
9830 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
9831 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
9832 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
9834 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
9835 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
9836 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
9838 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
9839 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
9840 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
9842 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
9843 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
9844 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
9846 stats->rx_missed_errors +=
9847 BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
9849 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
9851 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
9855 static void bnxt_add_prev_stats(struct bnxt *bp,
9856 struct rtnl_link_stats64 *stats)
9858 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
9860 stats->rx_packets += prev_stats->rx_packets;
9861 stats->tx_packets += prev_stats->tx_packets;
9862 stats->rx_bytes += prev_stats->rx_bytes;
9863 stats->tx_bytes += prev_stats->tx_bytes;
9864 stats->rx_missed_errors += prev_stats->rx_missed_errors;
9865 stats->multicast += prev_stats->multicast;
9866 stats->tx_dropped += prev_stats->tx_dropped;
9870 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
9872 struct bnxt *bp = netdev_priv(dev);
9874 set_bit(BNXT_STATE_READ_STATS, &bp->state);
9875 /* Make sure bnxt_close_nic() sees that we are reading stats before
9876 * we check the BNXT_STATE_OPEN flag.
9878 smp_mb__after_atomic();
9879 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9880 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9881 *stats = bp->net_stats_prev;
9885 bnxt_get_ring_stats(bp, stats);
9886 bnxt_add_prev_stats(bp, stats);
9888 if (bp->flags & BNXT_FLAG_PORT_STATS) {
9889 u64 *rx = bp->port_stats.sw_stats;
9890 u64 *tx = bp->port_stats.sw_stats +
9891 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
9893 stats->rx_crc_errors =
9894 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
9895 stats->rx_frame_errors =
9896 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
9897 stats->rx_length_errors =
9898 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
9899 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
9900 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
9902 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
9903 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
9905 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
9906 stats->tx_fifo_errors =
9907 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
9908 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
9910 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9913 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
9915 struct net_device *dev = bp->dev;
9916 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9917 struct netdev_hw_addr *ha;
9920 bool update = false;
9923 netdev_for_each_mc_addr(ha, dev) {
9924 if (mc_count >= BNXT_MAX_MC_ADDRS) {
9925 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9926 vnic->mc_list_count = 0;
9930 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
9931 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
9938 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
9940 if (mc_count != vnic->mc_list_count) {
9941 vnic->mc_list_count = mc_count;
9947 static bool bnxt_uc_list_updated(struct bnxt *bp)
9949 struct net_device *dev = bp->dev;
9950 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9951 struct netdev_hw_addr *ha;
9954 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
9957 netdev_for_each_uc_addr(ha, dev) {
9958 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
9966 static void bnxt_set_rx_mode(struct net_device *dev)
9968 struct bnxt *bp = netdev_priv(dev);
9969 struct bnxt_vnic_info *vnic;
9970 bool mc_update = false;
9974 if (!test_bit(BNXT_STATE_OPEN, &bp->state))
9977 vnic = &bp->vnic_info[0];
9978 mask = vnic->rx_mask;
9979 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
9980 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
9981 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
9982 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
9984 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
9985 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
9987 uc_update = bnxt_uc_list_updated(bp);
9989 if (dev->flags & IFF_BROADCAST)
9990 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
9991 if (dev->flags & IFF_ALLMULTI) {
9992 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9993 vnic->mc_list_count = 0;
9995 mc_update = bnxt_mc_list_updated(bp, &mask);
9998 if (mask != vnic->rx_mask || uc_update || mc_update) {
9999 vnic->rx_mask = mask;
10001 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10002 bnxt_queue_sp_work(bp);
10006 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10008 struct net_device *dev = bp->dev;
10009 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10010 struct netdev_hw_addr *ha;
10011 int i, off = 0, rc;
10014 netif_addr_lock_bh(dev);
10015 uc_update = bnxt_uc_list_updated(bp);
10016 netif_addr_unlock_bh(dev);
10021 mutex_lock(&bp->hwrm_cmd_lock);
10022 for (i = 1; i < vnic->uc_filter_count; i++) {
10023 struct hwrm_cfa_l2_filter_free_input req = {0};
10025 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10028 req.l2_filter_id = vnic->fw_l2_filter_id[i];
10030 rc = _hwrm_send_message(bp, &req, sizeof(req),
10033 mutex_unlock(&bp->hwrm_cmd_lock);
10035 vnic->uc_filter_count = 1;
10037 netif_addr_lock_bh(dev);
10038 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10039 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10041 netdev_for_each_uc_addr(ha, dev) {
10042 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10044 vnic->uc_filter_count++;
10047 netif_addr_unlock_bh(dev);
10049 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10050 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10052 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10054 vnic->uc_filter_count = i;
10060 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10061 if (rc && vnic->mc_list_count) {
10062 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10064 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10065 vnic->mc_list_count = 0;
10066 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10069 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10075 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10077 #ifdef CONFIG_BNXT_SRIOV
10078 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10079 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10081 /* No minimum rings were provisioned by the PF. Don't
10082 * reserve rings by default when device is down.
10084 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10087 if (!netif_running(bp->dev))
10094 /* If the chip and firmware supports RFS */
10095 static bool bnxt_rfs_supported(struct bnxt *bp)
10097 if (bp->flags & BNXT_FLAG_CHIP_P5) {
10098 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10102 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10104 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10109 /* If runtime conditions support RFS */
10110 static bool bnxt_rfs_capable(struct bnxt *bp)
10112 #ifdef CONFIG_RFS_ACCEL
10113 int vnics, max_vnics, max_rss_ctxs;
10115 if (bp->flags & BNXT_FLAG_CHIP_P5)
10116 return bnxt_rfs_supported(bp);
10117 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
10120 vnics = 1 + bp->rx_nr_rings;
10121 max_vnics = bnxt_get_max_func_vnics(bp);
10122 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10124 /* RSS contexts not a limiting factor */
10125 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10126 max_rss_ctxs = max_vnics;
10127 if (vnics > max_vnics || vnics > max_rss_ctxs) {
10128 if (bp->rx_nr_rings > 1)
10129 netdev_warn(bp->dev,
10130 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10131 min(max_rss_ctxs - 1, max_vnics - 1));
10135 if (!BNXT_NEW_RM(bp))
10138 if (vnics == bp->hw_resc.resv_vnics)
10141 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10142 if (vnics <= bp->hw_resc.resv_vnics)
10145 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10146 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10153 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10154 netdev_features_t features)
10156 struct bnxt *bp = netdev_priv(dev);
10157 netdev_features_t vlan_features;
10159 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10160 features &= ~NETIF_F_NTUPLE;
10162 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10163 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10165 if (!(features & NETIF_F_GRO))
10166 features &= ~NETIF_F_GRO_HW;
10168 if (features & NETIF_F_GRO_HW)
10169 features &= ~NETIF_F_LRO;
10171 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
10172 * turned on or off together.
10174 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10175 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10176 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10177 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10178 else if (vlan_features)
10179 features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10181 #ifdef CONFIG_BNXT_SRIOV
10182 if (BNXT_VF(bp) && bp->vf.vlan)
10183 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10188 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10190 struct bnxt *bp = netdev_priv(dev);
10191 u32 flags = bp->flags;
10194 bool re_init = false;
10195 bool update_tpa = false;
10197 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10198 if (features & NETIF_F_GRO_HW)
10199 flags |= BNXT_FLAG_GRO;
10200 else if (features & NETIF_F_LRO)
10201 flags |= BNXT_FLAG_LRO;
10203 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10204 flags &= ~BNXT_FLAG_TPA;
10206 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10207 flags |= BNXT_FLAG_STRIP_VLAN;
10209 if (features & NETIF_F_NTUPLE)
10210 flags |= BNXT_FLAG_RFS;
10212 changes = flags ^ bp->flags;
10213 if (changes & BNXT_FLAG_TPA) {
10215 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10216 (flags & BNXT_FLAG_TPA) == 0 ||
10217 (bp->flags & BNXT_FLAG_CHIP_P5))
10221 if (changes & ~BNXT_FLAG_TPA)
10224 if (flags != bp->flags) {
10225 u32 old_flags = bp->flags;
10227 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10230 bnxt_set_ring_params(bp);
10235 bnxt_close_nic(bp, false, false);
10238 bnxt_set_ring_params(bp);
10240 return bnxt_open_nic(bp, false, false);
10244 rc = bnxt_set_tpa(bp,
10245 (flags & BNXT_FLAG_TPA) ?
10248 bp->flags = old_flags;
10254 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
10257 struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
10258 struct hwrm_dbg_read_direct_input req = {0};
10259 __le32 *dbg_reg_buf;
10260 dma_addr_t mapping;
10263 dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
10264 &mapping, GFP_KERNEL);
10267 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
10268 req.host_dest_addr = cpu_to_le64(mapping);
10269 req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
10270 req.read_len32 = cpu_to_le32(num_words);
10271 mutex_lock(&bp->hwrm_cmd_lock);
10272 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10273 if (rc || resp->error_code) {
10275 goto dbg_rd_reg_exit;
10277 for (i = 0; i < num_words; i++)
10278 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
10281 mutex_unlock(&bp->hwrm_cmd_lock);
10282 dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
10286 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
10287 u32 ring_id, u32 *prod, u32 *cons)
10289 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
10290 struct hwrm_dbg_ring_info_get_input req = {0};
10293 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
10294 req.ring_type = ring_type;
10295 req.fw_ring_id = cpu_to_le32(ring_id);
10296 mutex_lock(&bp->hwrm_cmd_lock);
10297 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10299 *prod = le32_to_cpu(resp->producer_index);
10300 *cons = le32_to_cpu(resp->consumer_index);
10302 mutex_unlock(&bp->hwrm_cmd_lock);
10306 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
10308 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
10309 int i = bnapi->index;
10314 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
10315 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
10319 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
10321 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
10322 int i = bnapi->index;
10327 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",
10328 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
10329 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
10330 rxr->rx_sw_agg_prod);
10333 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
10335 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10336 int i = bnapi->index;
10338 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
10339 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
10342 static void bnxt_dbg_dump_states(struct bnxt *bp)
10345 struct bnxt_napi *bnapi;
10347 for (i = 0; i < bp->cp_nr_rings; i++) {
10348 bnapi = bp->bnapi[i];
10349 if (netif_msg_drv(bp)) {
10350 bnxt_dump_tx_sw_state(bnapi);
10351 bnxt_dump_rx_sw_state(bnapi);
10352 bnxt_dump_cp_sw_state(bnapi);
10357 static void bnxt_reset_task(struct bnxt *bp, bool silent)
10360 bnxt_dbg_dump_states(bp);
10361 if (netif_running(bp->dev)) {
10365 bnxt_close_nic(bp, false, false);
10366 bnxt_open_nic(bp, false, false);
10369 bnxt_close_nic(bp, true, false);
10370 rc = bnxt_open_nic(bp, true, false);
10371 bnxt_ulp_start(bp, rc);
10376 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
10378 struct bnxt *bp = netdev_priv(dev);
10380 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
10381 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
10382 bnxt_queue_sp_work(bp);
10385 static void bnxt_fw_health_check(struct bnxt *bp)
10387 struct bnxt_fw_health *fw_health = bp->fw_health;
10390 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10393 if (fw_health->tmr_counter) {
10394 fw_health->tmr_counter--;
10398 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10399 if (val == fw_health->last_fw_heartbeat)
10402 fw_health->last_fw_heartbeat = val;
10404 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10405 if (val != fw_health->last_fw_reset_cnt)
10408 fw_health->tmr_counter = fw_health->tmr_multiplier;
10412 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
10413 bnxt_queue_sp_work(bp);
10416 static void bnxt_timer(struct timer_list *t)
10418 struct bnxt *bp = from_timer(bp, t, timer);
10419 struct net_device *dev = bp->dev;
10421 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
10424 if (atomic_read(&bp->intr_sem) != 0)
10425 goto bnxt_restart_timer;
10427 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
10428 bnxt_fw_health_check(bp);
10430 if (bp->link_info.link_up && bp->stats_coal_ticks) {
10431 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
10432 bnxt_queue_sp_work(bp);
10435 if (bnxt_tc_flower_enabled(bp)) {
10436 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
10437 bnxt_queue_sp_work(bp);
10440 #ifdef CONFIG_RFS_ACCEL
10441 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
10442 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
10443 bnxt_queue_sp_work(bp);
10445 #endif /*CONFIG_RFS_ACCEL*/
10447 if (bp->link_info.phy_retry) {
10448 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
10449 bp->link_info.phy_retry = false;
10450 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
10452 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
10453 bnxt_queue_sp_work(bp);
10457 if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
10458 netif_carrier_ok(dev)) {
10459 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
10460 bnxt_queue_sp_work(bp);
10462 bnxt_restart_timer:
10463 mod_timer(&bp->timer, jiffies + bp->current_interval);
10466 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
10468 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
10469 * set. If the device is being closed, bnxt_close() may be holding
10470 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
10471 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
10473 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10477 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
10479 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10483 /* Only called from bnxt_sp_task() */
10484 static void bnxt_reset(struct bnxt *bp, bool silent)
10486 bnxt_rtnl_lock_sp(bp);
10487 if (test_bit(BNXT_STATE_OPEN, &bp->state))
10488 bnxt_reset_task(bp, silent);
10489 bnxt_rtnl_unlock_sp(bp);
10492 static void bnxt_fw_reset_close(struct bnxt *bp)
10495 /* When firmware is fatal state, disable PCI device to prevent
10496 * any potential bad DMAs before freeing kernel memory.
10498 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10499 pci_disable_device(bp->pdev);
10500 __bnxt_close_nic(bp, true, false);
10501 bnxt_clear_int_mode(bp);
10502 bnxt_hwrm_func_drv_unrgtr(bp);
10503 if (pci_is_enabled(bp->pdev))
10504 pci_disable_device(bp->pdev);
10505 bnxt_free_ctx_mem(bp);
10510 static bool is_bnxt_fw_ok(struct bnxt *bp)
10512 struct bnxt_fw_health *fw_health = bp->fw_health;
10513 bool no_heartbeat = false, has_reset = false;
10516 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10517 if (val == fw_health->last_fw_heartbeat)
10518 no_heartbeat = true;
10520 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10521 if (val != fw_health->last_fw_reset_cnt)
10524 if (!no_heartbeat && has_reset)
10530 /* rtnl_lock is acquired before calling this function */
10531 static void bnxt_force_fw_reset(struct bnxt *bp)
10533 struct bnxt_fw_health *fw_health = bp->fw_health;
10536 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
10537 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10540 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10541 bnxt_fw_reset_close(bp);
10542 wait_dsecs = fw_health->master_func_wait_dsecs;
10543 if (fw_health->master) {
10544 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
10546 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10548 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
10549 wait_dsecs = fw_health->normal_func_wait_dsecs;
10550 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10553 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
10554 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
10555 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10558 void bnxt_fw_exception(struct bnxt *bp)
10560 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
10561 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10562 bnxt_rtnl_lock_sp(bp);
10563 bnxt_force_fw_reset(bp);
10564 bnxt_rtnl_unlock_sp(bp);
10567 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
10570 static int bnxt_get_registered_vfs(struct bnxt *bp)
10572 #ifdef CONFIG_BNXT_SRIOV
10578 rc = bnxt_hwrm_func_qcfg(bp);
10580 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
10583 if (bp->pf.registered_vfs)
10584 return bp->pf.registered_vfs;
10591 void bnxt_fw_reset(struct bnxt *bp)
10593 bnxt_rtnl_lock_sp(bp);
10594 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
10595 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10598 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10599 if (bp->pf.active_vfs &&
10600 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10601 n = bnxt_get_registered_vfs(bp);
10603 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
10605 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10606 dev_close(bp->dev);
10607 goto fw_reset_exit;
10608 } else if (n > 0) {
10609 u16 vf_tmo_dsecs = n * 10;
10611 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
10612 bp->fw_reset_max_dsecs = vf_tmo_dsecs;
10613 bp->fw_reset_state =
10614 BNXT_FW_RESET_STATE_POLL_VF;
10615 bnxt_queue_fw_reset_work(bp, HZ / 10);
10616 goto fw_reset_exit;
10618 bnxt_fw_reset_close(bp);
10619 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
10620 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
10623 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10624 tmo = bp->fw_reset_min_dsecs * HZ / 10;
10626 bnxt_queue_fw_reset_work(bp, tmo);
10629 bnxt_rtnl_unlock_sp(bp);
10632 static void bnxt_chk_missed_irq(struct bnxt *bp)
10636 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
10639 for (i = 0; i < bp->cp_nr_rings; i++) {
10640 struct bnxt_napi *bnapi = bp->bnapi[i];
10641 struct bnxt_cp_ring_info *cpr;
10648 cpr = &bnapi->cp_ring;
10649 for (j = 0; j < 2; j++) {
10650 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
10653 if (!cpr2 || cpr2->has_more_work ||
10654 !bnxt_has_work(bp, cpr2))
10657 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
10658 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
10661 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
10662 bnxt_dbg_hwrm_ring_info_get(bp,
10663 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
10664 fw_ring_id, &val[0], &val[1]);
10665 cpr->sw_stats.cmn.missed_irqs++;
10670 static void bnxt_cfg_ntp_filters(struct bnxt *);
10672 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
10674 struct bnxt_link_info *link_info = &bp->link_info;
10676 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
10677 link_info->autoneg = BNXT_AUTONEG_SPEED;
10678 if (bp->hwrm_spec_code >= 0x10201) {
10679 if (link_info->auto_pause_setting &
10680 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
10681 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10683 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10685 link_info->advertising = link_info->auto_link_speeds;
10687 link_info->req_link_speed = link_info->force_link_speed;
10688 link_info->req_duplex = link_info->duplex_setting;
10690 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
10691 link_info->req_flow_ctrl =
10692 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
10694 link_info->req_flow_ctrl = link_info->force_pause_setting;
10697 static void bnxt_sp_task(struct work_struct *work)
10699 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
10701 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10702 smp_mb__after_atomic();
10703 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10704 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10708 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
10709 bnxt_cfg_rx_mode(bp);
10711 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
10712 bnxt_cfg_ntp_filters(bp);
10713 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
10714 bnxt_hwrm_exec_fwd_req(bp);
10715 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
10716 bnxt_hwrm_port_qstats(bp, 0);
10717 bnxt_hwrm_port_qstats_ext(bp, 0);
10718 bnxt_accumulate_all_stats(bp);
10721 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
10724 mutex_lock(&bp->link_lock);
10725 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
10727 bnxt_hwrm_phy_qcaps(bp);
10729 rc = bnxt_update_link(bp, true);
10731 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
10734 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
10736 bnxt_init_ethtool_link_settings(bp);
10737 mutex_unlock(&bp->link_lock);
10739 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
10742 mutex_lock(&bp->link_lock);
10743 rc = bnxt_update_phy_setting(bp);
10744 mutex_unlock(&bp->link_lock);
10746 netdev_warn(bp->dev, "update phy settings retry failed\n");
10748 bp->link_info.phy_retry = false;
10749 netdev_info(bp->dev, "update phy settings retry succeeded\n");
10752 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
10753 mutex_lock(&bp->link_lock);
10754 bnxt_get_port_module_status(bp);
10755 mutex_unlock(&bp->link_lock);
10758 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
10759 bnxt_tc_flow_stats_work(bp);
10761 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
10762 bnxt_chk_missed_irq(bp);
10764 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
10765 * must be the last functions to be called before exiting.
10767 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
10768 bnxt_reset(bp, false);
10770 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
10771 bnxt_reset(bp, true);
10773 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
10774 bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
10776 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
10777 if (!is_bnxt_fw_ok(bp))
10778 bnxt_devlink_health_report(bp,
10779 BNXT_FW_EXCEPTION_SP_EVENT);
10782 smp_mb__before_atomic();
10783 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10786 /* Under rtnl_lock */
10787 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
10790 int max_rx, max_tx, tx_sets = 1;
10791 int tx_rings_needed, stats;
10798 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
10805 tx_rings_needed = tx * tx_sets + tx_xdp;
10806 if (max_tx < tx_rings_needed)
10810 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
10813 if (bp->flags & BNXT_FLAG_AGG_RINGS)
10815 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
10817 if (BNXT_NEW_RM(bp)) {
10818 cp += bnxt_get_ulp_msix_num(bp);
10819 stats += bnxt_get_ulp_stat_ctxs(bp);
10821 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
10825 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
10828 pci_iounmap(pdev, bp->bar2);
10833 pci_iounmap(pdev, bp->bar1);
10838 pci_iounmap(pdev, bp->bar0);
10843 static void bnxt_cleanup_pci(struct bnxt *bp)
10845 bnxt_unmap_bars(bp, bp->pdev);
10846 pci_release_regions(bp->pdev);
10847 if (pci_is_enabled(bp->pdev))
10848 pci_disable_device(bp->pdev);
10851 static void bnxt_init_dflt_coal(struct bnxt *bp)
10853 struct bnxt_coal *coal;
10855 /* Tick values in micro seconds.
10856 * 1 coal_buf x bufs_per_record = 1 completion record.
10858 coal = &bp->rx_coal;
10859 coal->coal_ticks = 10;
10860 coal->coal_bufs = 30;
10861 coal->coal_ticks_irq = 1;
10862 coal->coal_bufs_irq = 2;
10863 coal->idle_thresh = 50;
10864 coal->bufs_per_record = 2;
10865 coal->budget = 64; /* NAPI budget */
10867 coal = &bp->tx_coal;
10868 coal->coal_ticks = 28;
10869 coal->coal_bufs = 30;
10870 coal->coal_ticks_irq = 2;
10871 coal->coal_bufs_irq = 2;
10872 coal->bufs_per_record = 1;
10874 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
10877 static void bnxt_alloc_fw_health(struct bnxt *bp)
10882 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10883 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10886 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
10887 if (!bp->fw_health) {
10888 netdev_warn(bp->dev, "Failed to allocate fw_health\n");
10889 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10890 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10894 static int bnxt_fw_init_one_p1(struct bnxt *bp)
10899 rc = bnxt_hwrm_ver_get(bp);
10903 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
10904 rc = bnxt_alloc_kong_hwrm_resources(bp);
10906 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
10909 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
10910 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
10911 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
10915 rc = bnxt_hwrm_func_reset(bp);
10919 bnxt_hwrm_fw_set_time(bp);
10923 static int bnxt_fw_init_one_p2(struct bnxt *bp)
10927 /* Get the MAX capabilities for this function */
10928 rc = bnxt_hwrm_func_qcaps(bp);
10930 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
10935 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
10937 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
10940 bnxt_alloc_fw_health(bp);
10941 rc = bnxt_hwrm_error_recovery_qcfg(bp);
10943 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
10946 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
10950 bnxt_hwrm_func_qcfg(bp);
10951 bnxt_hwrm_vnic_qcaps(bp);
10952 bnxt_hwrm_port_led_qcaps(bp);
10953 bnxt_ethtool_init(bp);
10958 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
10960 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
10961 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
10962 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
10963 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
10964 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
10965 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
10966 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
10967 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
10968 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
10972 static void bnxt_set_dflt_rfs(struct bnxt *bp)
10974 struct net_device *dev = bp->dev;
10976 dev->hw_features &= ~NETIF_F_NTUPLE;
10977 dev->features &= ~NETIF_F_NTUPLE;
10978 bp->flags &= ~BNXT_FLAG_RFS;
10979 if (bnxt_rfs_supported(bp)) {
10980 dev->hw_features |= NETIF_F_NTUPLE;
10981 if (bnxt_rfs_capable(bp)) {
10982 bp->flags |= BNXT_FLAG_RFS;
10983 dev->features |= NETIF_F_NTUPLE;
10988 static void bnxt_fw_init_one_p3(struct bnxt *bp)
10990 struct pci_dev *pdev = bp->pdev;
10992 bnxt_set_dflt_rss_hash_type(bp);
10993 bnxt_set_dflt_rfs(bp);
10995 bnxt_get_wol_settings(bp);
10996 if (bp->flags & BNXT_FLAG_WOL_CAP)
10997 device_set_wakeup_enable(&pdev->dev, bp->wol);
10999 device_set_wakeup_capable(&pdev->dev, false);
11001 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11002 bnxt_hwrm_coal_params_qcaps(bp);
11005 static int bnxt_fw_init_one(struct bnxt *bp)
11009 rc = bnxt_fw_init_one_p1(bp);
11011 netdev_err(bp->dev, "Firmware init phase 1 failed\n");
11014 rc = bnxt_fw_init_one_p2(bp);
11016 netdev_err(bp->dev, "Firmware init phase 2 failed\n");
11019 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
11023 /* In case fw capabilities have changed, destroy the unneeded
11024 * reporters and create newly capable ones.
11026 bnxt_dl_fw_reporters_destroy(bp, false);
11027 bnxt_dl_fw_reporters_create(bp);
11028 bnxt_fw_init_one_p3(bp);
11032 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
11034 struct bnxt_fw_health *fw_health = bp->fw_health;
11035 u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
11036 u32 val = fw_health->fw_reset_seq_vals[reg_idx];
11037 u32 reg_type, reg_off, delay_msecs;
11039 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
11040 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
11041 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
11042 switch (reg_type) {
11043 case BNXT_FW_HEALTH_REG_TYPE_CFG:
11044 pci_write_config_dword(bp->pdev, reg_off, val);
11046 case BNXT_FW_HEALTH_REG_TYPE_GRC:
11047 writel(reg_off & BNXT_GRC_BASE_MASK,
11048 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
11049 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
11051 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
11052 writel(val, bp->bar0 + reg_off);
11054 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
11055 writel(val, bp->bar1 + reg_off);
11059 pci_read_config_dword(bp->pdev, 0, &val);
11060 msleep(delay_msecs);
11064 static void bnxt_reset_all(struct bnxt *bp)
11066 struct bnxt_fw_health *fw_health = bp->fw_health;
11069 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11070 #ifdef CONFIG_TEE_BNXT_FW
11071 rc = tee_bnxt_fw_load();
11073 netdev_err(bp->dev, "Unable to reset FW rc=%d\n", rc);
11074 bp->fw_reset_timestamp = jiffies;
11079 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
11080 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
11081 bnxt_fw_reset_writel(bp, i);
11082 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
11083 struct hwrm_fw_reset_input req = {0};
11085 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
11086 req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
11087 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
11088 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
11089 req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
11090 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11092 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
11094 bp->fw_reset_timestamp = jiffies;
11097 static void bnxt_fw_reset_task(struct work_struct *work)
11099 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
11102 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11103 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
11107 switch (bp->fw_reset_state) {
11108 case BNXT_FW_RESET_STATE_POLL_VF: {
11109 int n = bnxt_get_registered_vfs(bp);
11113 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
11114 n, jiffies_to_msecs(jiffies -
11115 bp->fw_reset_timestamp));
11116 goto fw_reset_abort;
11117 } else if (n > 0) {
11118 if (time_after(jiffies, bp->fw_reset_timestamp +
11119 (bp->fw_reset_max_dsecs * HZ / 10))) {
11120 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11121 bp->fw_reset_state = 0;
11122 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
11126 bnxt_queue_fw_reset_work(bp, HZ / 10);
11129 bp->fw_reset_timestamp = jiffies;
11131 bnxt_fw_reset_close(bp);
11132 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11133 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11136 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11137 tmo = bp->fw_reset_min_dsecs * HZ / 10;
11140 bnxt_queue_fw_reset_work(bp, tmo);
11143 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
11146 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11147 if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
11148 !time_after(jiffies, bp->fw_reset_timestamp +
11149 (bp->fw_reset_max_dsecs * HZ / 10))) {
11150 bnxt_queue_fw_reset_work(bp, HZ / 5);
11154 if (!bp->fw_health->master) {
11155 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
11157 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11158 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11161 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11164 case BNXT_FW_RESET_STATE_RESET_FW:
11165 bnxt_reset_all(bp);
11166 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11167 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
11169 case BNXT_FW_RESET_STATE_ENABLE_DEV:
11170 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11173 val = bnxt_fw_health_readl(bp,
11174 BNXT_FW_RESET_INPROG_REG);
11176 netdev_warn(bp->dev, "FW reset inprog %x after min wait time.\n",
11179 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11180 if (pci_enable_device(bp->pdev)) {
11181 netdev_err(bp->dev, "Cannot re-enable PCI device\n");
11182 goto fw_reset_abort;
11184 pci_set_master(bp->pdev);
11185 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
11187 case BNXT_FW_RESET_STATE_POLL_FW:
11188 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
11189 rc = __bnxt_hwrm_ver_get(bp, true);
11191 if (time_after(jiffies, bp->fw_reset_timestamp +
11192 (bp->fw_reset_max_dsecs * HZ / 10))) {
11193 netdev_err(bp->dev, "Firmware reset aborted\n");
11194 goto fw_reset_abort;
11196 bnxt_queue_fw_reset_work(bp, HZ / 5);
11199 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
11200 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
11202 case BNXT_FW_RESET_STATE_OPENING:
11203 while (!rtnl_trylock()) {
11204 bnxt_queue_fw_reset_work(bp, HZ / 10);
11207 rc = bnxt_open(bp->dev);
11209 netdev_err(bp->dev, "bnxt_open_nic() failed\n");
11210 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11211 dev_close(bp->dev);
11214 bp->fw_reset_state = 0;
11215 /* Make sure fw_reset_state is 0 before clearing the flag */
11216 smp_mb__before_atomic();
11217 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11218 bnxt_ulp_start(bp, rc);
11220 bnxt_reenable_sriov(bp);
11221 bnxt_dl_health_recovery_done(bp);
11222 bnxt_dl_health_status_update(bp, true);
11229 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11230 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
11231 bnxt_dl_health_status_update(bp, false);
11232 bp->fw_reset_state = 0;
11234 dev_close(bp->dev);
11238 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
11241 struct bnxt *bp = netdev_priv(dev);
11243 SET_NETDEV_DEV(dev, &pdev->dev);
11245 /* enable device (incl. PCI PM wakeup), and bus-mastering */
11246 rc = pci_enable_device(pdev);
11248 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
11252 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
11253 dev_err(&pdev->dev,
11254 "Cannot find PCI device base address, aborting\n");
11256 goto init_err_disable;
11259 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
11261 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
11262 goto init_err_disable;
11265 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
11266 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
11267 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
11268 goto init_err_disable;
11271 pci_set_master(pdev);
11276 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
11277 * determines the BAR size.
11279 bp->bar0 = pci_ioremap_bar(pdev, 0);
11281 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
11283 goto init_err_release;
11286 bp->bar2 = pci_ioremap_bar(pdev, 4);
11288 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
11290 goto init_err_release;
11293 pci_enable_pcie_error_reporting(pdev);
11295 INIT_WORK(&bp->sp_task, bnxt_sp_task);
11296 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
11298 spin_lock_init(&bp->ntp_fltr_lock);
11299 #if BITS_PER_LONG == 32
11300 spin_lock_init(&bp->db_lock);
11303 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
11304 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
11306 bnxt_init_dflt_coal(bp);
11308 timer_setup(&bp->timer, bnxt_timer, 0);
11309 bp->current_interval = BNXT_TIMER_INTERVAL;
11311 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
11312 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
11314 clear_bit(BNXT_STATE_OPEN, &bp->state);
11318 bnxt_unmap_bars(bp, pdev);
11319 pci_release_regions(pdev);
11322 pci_disable_device(pdev);
11328 /* rtnl_lock held */
11329 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
11331 struct sockaddr *addr = p;
11332 struct bnxt *bp = netdev_priv(dev);
11335 if (!is_valid_ether_addr(addr->sa_data))
11336 return -EADDRNOTAVAIL;
11338 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
11341 rc = bnxt_approve_mac(bp, addr->sa_data, true);
11345 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
11346 if (netif_running(dev)) {
11347 bnxt_close_nic(bp, false, false);
11348 rc = bnxt_open_nic(bp, false, false);
11354 /* rtnl_lock held */
11355 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
11357 struct bnxt *bp = netdev_priv(dev);
11359 if (netif_running(dev))
11360 bnxt_close_nic(bp, true, false);
11362 dev->mtu = new_mtu;
11363 bnxt_set_ring_params(bp);
11365 if (netif_running(dev))
11366 return bnxt_open_nic(bp, true, false);
11371 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
11373 struct bnxt *bp = netdev_priv(dev);
11377 if (tc > bp->max_tc) {
11378 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
11383 if (netdev_get_num_tc(dev) == tc)
11386 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11389 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
11390 sh, tc, bp->tx_nr_rings_xdp);
11394 /* Needs to close the device and do hw resource re-allocations */
11395 if (netif_running(bp->dev))
11396 bnxt_close_nic(bp, true, false);
11399 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
11400 netdev_set_num_tc(dev, tc);
11402 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11403 netdev_reset_tc(dev);
11405 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
11406 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
11407 bp->tx_nr_rings + bp->rx_nr_rings;
11409 if (netif_running(bp->dev))
11410 return bnxt_open_nic(bp, true, false);
11415 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11418 struct bnxt *bp = cb_priv;
11420 if (!bnxt_tc_flower_enabled(bp) ||
11421 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
11422 return -EOPNOTSUPP;
11425 case TC_SETUP_CLSFLOWER:
11426 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
11428 return -EOPNOTSUPP;
11432 LIST_HEAD(bnxt_block_cb_list);
11434 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
11437 struct bnxt *bp = netdev_priv(dev);
11440 case TC_SETUP_BLOCK:
11441 return flow_block_cb_setup_simple(type_data,
11442 &bnxt_block_cb_list,
11443 bnxt_setup_tc_block_cb,
11445 case TC_SETUP_QDISC_MQPRIO: {
11446 struct tc_mqprio_qopt *mqprio = type_data;
11448 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
11450 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
11453 return -EOPNOTSUPP;
11457 #ifdef CONFIG_RFS_ACCEL
11458 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
11459 struct bnxt_ntuple_filter *f2)
11461 struct flow_keys *keys1 = &f1->fkeys;
11462 struct flow_keys *keys2 = &f2->fkeys;
11464 if (keys1->basic.n_proto != keys2->basic.n_proto ||
11465 keys1->basic.ip_proto != keys2->basic.ip_proto)
11468 if (keys1->basic.n_proto == htons(ETH_P_IP)) {
11469 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
11470 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
11473 if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
11474 sizeof(keys1->addrs.v6addrs.src)) ||
11475 memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
11476 sizeof(keys1->addrs.v6addrs.dst)))
11480 if (keys1->ports.ports == keys2->ports.ports &&
11481 keys1->control.flags == keys2->control.flags &&
11482 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
11483 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
11489 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
11490 u16 rxq_index, u32 flow_id)
11492 struct bnxt *bp = netdev_priv(dev);
11493 struct bnxt_ntuple_filter *fltr, *new_fltr;
11494 struct flow_keys *fkeys;
11495 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
11496 int rc = 0, idx, bit_id, l2_idx = 0;
11497 struct hlist_head *head;
11500 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
11501 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11504 netif_addr_lock_bh(dev);
11505 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
11506 if (ether_addr_equal(eth->h_dest,
11507 vnic->uc_list + off)) {
11512 netif_addr_unlock_bh(dev);
11516 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
11520 fkeys = &new_fltr->fkeys;
11521 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
11522 rc = -EPROTONOSUPPORT;
11526 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
11527 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
11528 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
11529 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
11530 rc = -EPROTONOSUPPORT;
11533 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
11534 bp->hwrm_spec_code < 0x10601) {
11535 rc = -EPROTONOSUPPORT;
11538 flags = fkeys->control.flags;
11539 if (((flags & FLOW_DIS_ENCAPSULATION) &&
11540 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
11541 rc = -EPROTONOSUPPORT;
11545 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
11546 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
11548 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
11549 head = &bp->ntp_fltr_hash_tbl[idx];
11551 hlist_for_each_entry_rcu(fltr, head, hash) {
11552 if (bnxt_fltr_match(fltr, new_fltr)) {
11560 spin_lock_bh(&bp->ntp_fltr_lock);
11561 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
11562 BNXT_NTP_FLTR_MAX_FLTR, 0);
11564 spin_unlock_bh(&bp->ntp_fltr_lock);
11569 new_fltr->sw_id = (u16)bit_id;
11570 new_fltr->flow_id = flow_id;
11571 new_fltr->l2_fltr_idx = l2_idx;
11572 new_fltr->rxq = rxq_index;
11573 hlist_add_head_rcu(&new_fltr->hash, head);
11574 bp->ntp_fltr_count++;
11575 spin_unlock_bh(&bp->ntp_fltr_lock);
11577 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11578 bnxt_queue_sp_work(bp);
11580 return new_fltr->sw_id;
11587 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
11591 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
11592 struct hlist_head *head;
11593 struct hlist_node *tmp;
11594 struct bnxt_ntuple_filter *fltr;
11597 head = &bp->ntp_fltr_hash_tbl[i];
11598 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
11601 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
11602 if (rps_may_expire_flow(bp->dev, fltr->rxq,
11605 bnxt_hwrm_cfa_ntuple_filter_free(bp,
11610 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
11615 set_bit(BNXT_FLTR_VALID, &fltr->state);
11619 spin_lock_bh(&bp->ntp_fltr_lock);
11620 hlist_del_rcu(&fltr->hash);
11621 bp->ntp_fltr_count--;
11622 spin_unlock_bh(&bp->ntp_fltr_lock);
11624 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
11629 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
11630 netdev_info(bp->dev, "Receive PF driver unload event!\n");
11635 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
11639 #endif /* CONFIG_RFS_ACCEL */
11641 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
11643 struct bnxt *bp = netdev_priv(netdev);
11644 struct udp_tunnel_info ti;
11647 udp_tunnel_nic_get_port(netdev, table, 0, &ti);
11648 if (ti.type == UDP_TUNNEL_TYPE_VXLAN)
11649 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
11651 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
11654 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd);
11656 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
11659 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
11660 .sync_table = bnxt_udp_tunnel_sync,
11661 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
11662 UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
11664 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
11665 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
11669 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11670 struct net_device *dev, u32 filter_mask,
11673 struct bnxt *bp = netdev_priv(dev);
11675 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
11676 nlflags, filter_mask, NULL);
11679 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
11680 u16 flags, struct netlink_ext_ack *extack)
11682 struct bnxt *bp = netdev_priv(dev);
11683 struct nlattr *attr, *br_spec;
11686 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
11687 return -EOPNOTSUPP;
11689 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11693 nla_for_each_nested(attr, br_spec, rem) {
11696 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11699 if (nla_len(attr) < sizeof(mode))
11702 mode = nla_get_u16(attr);
11703 if (mode == bp->br_mode)
11706 rc = bnxt_hwrm_set_br_mode(bp, mode);
11708 bp->br_mode = mode;
11714 int bnxt_get_port_parent_id(struct net_device *dev,
11715 struct netdev_phys_item_id *ppid)
11717 struct bnxt *bp = netdev_priv(dev);
11719 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
11720 return -EOPNOTSUPP;
11722 /* The PF and it's VF-reps only support the switchdev framework */
11723 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
11724 return -EOPNOTSUPP;
11726 ppid->id_len = sizeof(bp->dsn);
11727 memcpy(ppid->id, bp->dsn, ppid->id_len);
11732 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
11734 struct bnxt *bp = netdev_priv(dev);
11736 return &bp->dl_port;
11739 static const struct net_device_ops bnxt_netdev_ops = {
11740 .ndo_open = bnxt_open,
11741 .ndo_start_xmit = bnxt_start_xmit,
11742 .ndo_stop = bnxt_close,
11743 .ndo_get_stats64 = bnxt_get_stats64,
11744 .ndo_set_rx_mode = bnxt_set_rx_mode,
11745 .ndo_do_ioctl = bnxt_ioctl,
11746 .ndo_validate_addr = eth_validate_addr,
11747 .ndo_set_mac_address = bnxt_change_mac_addr,
11748 .ndo_change_mtu = bnxt_change_mtu,
11749 .ndo_fix_features = bnxt_fix_features,
11750 .ndo_set_features = bnxt_set_features,
11751 .ndo_tx_timeout = bnxt_tx_timeout,
11752 #ifdef CONFIG_BNXT_SRIOV
11753 .ndo_get_vf_config = bnxt_get_vf_config,
11754 .ndo_set_vf_mac = bnxt_set_vf_mac,
11755 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
11756 .ndo_set_vf_rate = bnxt_set_vf_bw,
11757 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
11758 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
11759 .ndo_set_vf_trust = bnxt_set_vf_trust,
11761 .ndo_setup_tc = bnxt_setup_tc,
11762 #ifdef CONFIG_RFS_ACCEL
11763 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
11765 .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
11766 .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
11767 .ndo_bpf = bnxt_xdp,
11768 .ndo_xdp_xmit = bnxt_xdp_xmit,
11769 .ndo_bridge_getlink = bnxt_bridge_getlink,
11770 .ndo_bridge_setlink = bnxt_bridge_setlink,
11771 .ndo_get_devlink_port = bnxt_get_devlink_port,
11774 static void bnxt_remove_one(struct pci_dev *pdev)
11776 struct net_device *dev = pci_get_drvdata(pdev);
11777 struct bnxt *bp = netdev_priv(dev);
11780 bnxt_sriov_disable(bp);
11782 bnxt_dl_fw_reporters_destroy(bp, true);
11784 devlink_port_type_clear(&bp->dl_port);
11785 pci_disable_pcie_error_reporting(pdev);
11786 unregister_netdev(dev);
11787 bnxt_dl_unregister(bp);
11788 bnxt_shutdown_tc(bp);
11789 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11790 bnxt_cancel_sp_work(bp);
11793 bnxt_clear_int_mode(bp);
11794 bnxt_hwrm_func_drv_unrgtr(bp);
11795 bnxt_free_hwrm_resources(bp);
11796 bnxt_free_hwrm_short_cmd_req(bp);
11797 bnxt_ethtool_free(bp);
11801 kfree(bp->fw_health);
11802 bp->fw_health = NULL;
11803 bnxt_cleanup_pci(bp);
11804 bnxt_free_ctx_mem(bp);
11807 kfree(bp->rss_indir_tbl);
11808 bp->rss_indir_tbl = NULL;
11809 bnxt_free_port_stats(bp);
11813 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
11816 struct bnxt_link_info *link_info = &bp->link_info;
11818 rc = bnxt_hwrm_phy_qcaps(bp);
11820 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
11827 rc = bnxt_update_link(bp, false);
11829 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
11834 /* Older firmware does not have supported_auto_speeds, so assume
11835 * that all supported speeds can be autonegotiated.
11837 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
11838 link_info->support_auto_speeds = link_info->support_speeds;
11840 bnxt_init_ethtool_link_settings(bp);
11844 static int bnxt_get_max_irq(struct pci_dev *pdev)
11848 if (!pdev->msix_cap)
11851 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
11852 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
11855 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
11858 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11859 int max_ring_grps = 0, max_irq;
11861 *max_tx = hw_resc->max_tx_rings;
11862 *max_rx = hw_resc->max_rx_rings;
11863 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
11864 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
11865 bnxt_get_ulp_msix_num(bp),
11866 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
11867 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11868 *max_cp = min_t(int, *max_cp, max_irq);
11869 max_ring_grps = hw_resc->max_hw_ring_grps;
11870 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
11874 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11876 if (bp->flags & BNXT_FLAG_CHIP_P5) {
11877 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
11878 /* On P5 chips, max_cp output param should be available NQs */
11881 *max_rx = min_t(int, *max_rx, max_ring_grps);
11884 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
11888 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
11891 if (!rx || !tx || !cp)
11894 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
11897 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
11902 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
11903 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
11904 /* Not enough rings, try disabling agg rings. */
11905 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
11906 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
11908 /* set BNXT_FLAG_AGG_RINGS back for consistency */
11909 bp->flags |= BNXT_FLAG_AGG_RINGS;
11912 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
11913 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11914 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11915 bnxt_set_ring_params(bp);
11918 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
11919 int max_cp, max_stat, max_irq;
11921 /* Reserve minimum resources for RoCE */
11922 max_cp = bnxt_get_max_func_cp_rings(bp);
11923 max_stat = bnxt_get_max_func_stat_ctxs(bp);
11924 max_irq = bnxt_get_max_func_irqs(bp);
11925 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
11926 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
11927 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
11930 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
11931 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
11932 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
11933 max_cp = min_t(int, max_cp, max_irq);
11934 max_cp = min_t(int, max_cp, max_stat);
11935 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
11942 /* In initial default shared ring setting, each shared ring must have a
11945 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
11947 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
11948 bp->rx_nr_rings = bp->cp_nr_rings;
11949 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
11950 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11953 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
11955 int dflt_rings, max_rx_rings, max_tx_rings, rc;
11957 if (!bnxt_can_reserve_rings(bp))
11961 bp->flags |= BNXT_FLAG_SHARED_RINGS;
11962 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
11963 /* Reduce default rings on multi-port cards so that total default
11964 * rings do not exceed CPU count.
11966 if (bp->port_count > 1) {
11968 max_t(int, num_online_cpus() / bp->port_count, 1);
11970 dflt_rings = min_t(int, dflt_rings, max_rings);
11972 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
11975 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
11976 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
11978 bnxt_trim_dflt_sh_rings(bp);
11980 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
11981 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11983 rc = __bnxt_reserve_rings(bp);
11985 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
11986 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11988 bnxt_trim_dflt_sh_rings(bp);
11990 /* Rings may have been trimmed, re-reserve the trimmed rings. */
11991 if (bnxt_need_reserve_rings(bp)) {
11992 rc = __bnxt_reserve_rings(bp);
11994 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
11995 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11997 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12002 bp->tx_nr_rings = 0;
12003 bp->rx_nr_rings = 0;
12008 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
12012 if (bp->tx_nr_rings)
12015 bnxt_ulp_irq_stop(bp);
12016 bnxt_clear_int_mode(bp);
12017 rc = bnxt_set_dflt_rings(bp, true);
12019 netdev_err(bp->dev, "Not enough rings available.\n");
12020 goto init_dflt_ring_err;
12022 rc = bnxt_init_int_mode(bp);
12024 goto init_dflt_ring_err;
12026 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12027 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
12028 bp->flags |= BNXT_FLAG_RFS;
12029 bp->dev->features |= NETIF_F_NTUPLE;
12031 init_dflt_ring_err:
12032 bnxt_ulp_irq_restart(bp, rc);
12036 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
12041 bnxt_hwrm_func_qcaps(bp);
12043 if (netif_running(bp->dev))
12044 __bnxt_close_nic(bp, true, false);
12046 bnxt_ulp_irq_stop(bp);
12047 bnxt_clear_int_mode(bp);
12048 rc = bnxt_init_int_mode(bp);
12049 bnxt_ulp_irq_restart(bp, rc);
12051 if (netif_running(bp->dev)) {
12053 dev_close(bp->dev);
12055 rc = bnxt_open_nic(bp, true, false);
12061 static int bnxt_init_mac_addr(struct bnxt *bp)
12066 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
12068 #ifdef CONFIG_BNXT_SRIOV
12069 struct bnxt_vf_info *vf = &bp->vf;
12070 bool strict_approval = true;
12072 if (is_valid_ether_addr(vf->mac_addr)) {
12073 /* overwrite netdev dev_addr with admin VF MAC */
12074 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
12075 /* Older PF driver or firmware may not approve this
12078 strict_approval = false;
12080 eth_hw_addr_random(bp->dev);
12082 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
12088 #define BNXT_VPD_LEN 512
12089 static void bnxt_vpd_read_info(struct bnxt *bp)
12091 struct pci_dev *pdev = bp->pdev;
12092 int i, len, pos, ro_size;
12096 vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
12100 vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
12101 if (vpd_size <= 0) {
12102 netdev_err(bp->dev, "Unable to read VPD\n");
12106 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
12108 netdev_err(bp->dev, "VPD READ-Only not found\n");
12112 ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
12113 i += PCI_VPD_LRDT_TAG_SIZE;
12114 if (i + ro_size > vpd_size)
12117 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12118 PCI_VPD_RO_KEYWORD_PARTNO);
12122 len = pci_vpd_info_field_size(&vpd_data[pos]);
12123 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12124 if (len + pos > vpd_size)
12127 strlcpy(bp->board_partno, &vpd_data[pos], min(len, BNXT_VPD_FLD_LEN));
12130 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12131 PCI_VPD_RO_KEYWORD_SERIALNO);
12135 len = pci_vpd_info_field_size(&vpd_data[pos]);
12136 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12137 if (len + pos > vpd_size)
12140 strlcpy(bp->board_serialno, &vpd_data[pos], min(len, BNXT_VPD_FLD_LEN));
12145 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
12147 struct pci_dev *pdev = bp->pdev;
12150 qword = pci_get_dsn(pdev);
12152 netdev_info(bp->dev, "Unable to read adapter's DSN\n");
12153 return -EOPNOTSUPP;
12156 put_unaligned_le64(qword, dsn);
12158 bp->flags |= BNXT_FLAG_DSN_VALID;
12162 static int bnxt_map_db_bar(struct bnxt *bp)
12166 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
12172 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
12174 struct net_device *dev;
12178 if (pci_is_bridge(pdev))
12181 /* Clear any pending DMA transactions from crash kernel
12182 * while loading driver in capture kernel.
12184 if (is_kdump_kernel()) {
12185 pci_clear_master(pdev);
12189 max_irqs = bnxt_get_max_irq(pdev);
12190 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
12194 bp = netdev_priv(dev);
12195 bnxt_set_max_func_irqs(bp, max_irqs);
12197 if (bnxt_vf_pciid(ent->driver_data))
12198 bp->flags |= BNXT_FLAG_VF;
12200 if (pdev->msix_cap)
12201 bp->flags |= BNXT_FLAG_MSIX_CAP;
12203 rc = bnxt_init_board(pdev, dev);
12205 goto init_err_free;
12207 dev->netdev_ops = &bnxt_netdev_ops;
12208 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
12209 dev->ethtool_ops = &bnxt_ethtool_ops;
12210 pci_set_drvdata(pdev, dev);
12213 bnxt_vpd_read_info(bp);
12215 rc = bnxt_alloc_hwrm_resources(bp);
12217 goto init_err_pci_clean;
12219 mutex_init(&bp->hwrm_cmd_lock);
12220 mutex_init(&bp->link_lock);
12222 rc = bnxt_fw_init_one_p1(bp);
12224 goto init_err_pci_clean;
12226 if (BNXT_CHIP_P5(bp))
12227 bp->flags |= BNXT_FLAG_CHIP_P5;
12229 rc = bnxt_alloc_rss_indir_tbl(bp);
12231 goto init_err_pci_clean;
12233 rc = bnxt_fw_init_one_p2(bp);
12235 goto init_err_pci_clean;
12237 rc = bnxt_map_db_bar(bp);
12239 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
12241 goto init_err_pci_clean;
12244 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12245 NETIF_F_TSO | NETIF_F_TSO6 |
12246 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12247 NETIF_F_GSO_IPXIP4 |
12248 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12249 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
12250 NETIF_F_RXCSUM | NETIF_F_GRO;
12252 if (BNXT_SUPPORTS_TPA(bp))
12253 dev->hw_features |= NETIF_F_LRO;
12255 dev->hw_enc_features =
12256 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12257 NETIF_F_TSO | NETIF_F_TSO6 |
12258 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12259 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12260 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
12261 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
12263 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
12264 NETIF_F_GSO_GRE_CSUM;
12265 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
12266 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
12267 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12268 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
12269 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
12270 if (BNXT_SUPPORTS_TPA(bp))
12271 dev->hw_features |= NETIF_F_GRO_HW;
12272 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
12273 if (dev->features & NETIF_F_GRO_HW)
12274 dev->features &= ~NETIF_F_LRO;
12275 dev->priv_flags |= IFF_UNICAST_FLT;
12277 #ifdef CONFIG_BNXT_SRIOV
12278 init_waitqueue_head(&bp->sriov_cfg_wait);
12279 mutex_init(&bp->sriov_lock);
12281 if (BNXT_SUPPORTS_TPA(bp)) {
12282 bp->gro_func = bnxt_gro_func_5730x;
12283 if (BNXT_CHIP_P4(bp))
12284 bp->gro_func = bnxt_gro_func_5731x;
12285 else if (BNXT_CHIP_P5(bp))
12286 bp->gro_func = bnxt_gro_func_5750x;
12288 if (!BNXT_CHIP_P4_PLUS(bp))
12289 bp->flags |= BNXT_FLAG_DOUBLE_DB;
12291 bp->ulp_probe = bnxt_ulp_probe;
12293 rc = bnxt_init_mac_addr(bp);
12295 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
12296 rc = -EADDRNOTAVAIL;
12297 goto init_err_pci_clean;
12301 /* Read the adapter's DSN to use as the eswitch switch_id */
12302 rc = bnxt_pcie_dsn_get(bp, bp->dsn);
12305 /* MTU range: 60 - FW defined max */
12306 dev->min_mtu = ETH_ZLEN;
12307 dev->max_mtu = bp->max_mtu;
12309 rc = bnxt_probe_phy(bp, true);
12311 goto init_err_pci_clean;
12313 bnxt_set_rx_skb_mode(bp, false);
12314 bnxt_set_tpa_flags(bp);
12315 bnxt_set_ring_params(bp);
12316 rc = bnxt_set_dflt_rings(bp, true);
12318 netdev_err(bp->dev, "Not enough rings available.\n");
12320 goto init_err_pci_clean;
12323 bnxt_fw_init_one_p3(bp);
12325 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12326 bp->flags |= BNXT_FLAG_STRIP_VLAN;
12328 rc = bnxt_init_int_mode(bp);
12330 goto init_err_pci_clean;
12332 /* No TC has been set yet and rings may have been trimmed due to
12333 * limited MSIX, so we re-initialize the TX rings per TC.
12335 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12340 create_singlethread_workqueue("bnxt_pf_wq");
12342 dev_err(&pdev->dev, "Unable to create workqueue.\n");
12343 goto init_err_pci_clean;
12346 rc = bnxt_init_tc(bp);
12348 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
12352 bnxt_dl_register(bp);
12354 rc = register_netdev(dev);
12356 goto init_err_cleanup;
12359 devlink_port_type_eth_set(&bp->dl_port, bp->dev);
12360 bnxt_dl_fw_reporters_create(bp);
12362 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
12363 board_info[ent->driver_data].name,
12364 (long)pci_resource_start(pdev, 0), dev->dev_addr);
12365 pcie_print_link_status(pdev);
12367 pci_save_state(pdev);
12371 bnxt_dl_unregister(bp);
12372 bnxt_shutdown_tc(bp);
12373 bnxt_clear_int_mode(bp);
12375 init_err_pci_clean:
12376 bnxt_hwrm_func_drv_unrgtr(bp);
12377 bnxt_free_hwrm_short_cmd_req(bp);
12378 bnxt_free_hwrm_resources(bp);
12379 kfree(bp->fw_health);
12380 bp->fw_health = NULL;
12381 bnxt_cleanup_pci(bp);
12382 bnxt_free_ctx_mem(bp);
12385 kfree(bp->rss_indir_tbl);
12386 bp->rss_indir_tbl = NULL;
12393 static void bnxt_shutdown(struct pci_dev *pdev)
12395 struct net_device *dev = pci_get_drvdata(pdev);
12402 bp = netdev_priv(dev);
12404 goto shutdown_exit;
12406 if (netif_running(dev))
12409 bnxt_ulp_shutdown(bp);
12410 bnxt_clear_int_mode(bp);
12411 pci_disable_device(pdev);
12413 if (system_state == SYSTEM_POWER_OFF) {
12414 pci_wake_from_d3(pdev, bp->wol);
12415 pci_set_power_state(pdev, PCI_D3hot);
12422 #ifdef CONFIG_PM_SLEEP
12423 static int bnxt_suspend(struct device *device)
12425 struct net_device *dev = dev_get_drvdata(device);
12426 struct bnxt *bp = netdev_priv(dev);
12431 if (netif_running(dev)) {
12432 netif_device_detach(dev);
12433 rc = bnxt_close(dev);
12435 bnxt_hwrm_func_drv_unrgtr(bp);
12436 pci_disable_device(bp->pdev);
12437 bnxt_free_ctx_mem(bp);
12444 static int bnxt_resume(struct device *device)
12446 struct net_device *dev = dev_get_drvdata(device);
12447 struct bnxt *bp = netdev_priv(dev);
12451 rc = pci_enable_device(bp->pdev);
12453 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
12457 pci_set_master(bp->pdev);
12458 if (bnxt_hwrm_ver_get(bp)) {
12462 rc = bnxt_hwrm_func_reset(bp);
12468 rc = bnxt_hwrm_func_qcaps(bp);
12472 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
12477 bnxt_get_wol_settings(bp);
12478 if (netif_running(dev)) {
12479 rc = bnxt_open(dev);
12481 netif_device_attach(dev);
12485 bnxt_ulp_start(bp, rc);
12487 bnxt_reenable_sriov(bp);
12492 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
12493 #define BNXT_PM_OPS (&bnxt_pm_ops)
12497 #define BNXT_PM_OPS NULL
12499 #endif /* CONFIG_PM_SLEEP */
12502 * bnxt_io_error_detected - called when PCI error is detected
12503 * @pdev: Pointer to PCI device
12504 * @state: The current pci connection state
12506 * This function is called after a PCI bus error affecting
12507 * this device has been detected.
12509 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
12510 pci_channel_state_t state)
12512 struct net_device *netdev = pci_get_drvdata(pdev);
12513 struct bnxt *bp = netdev_priv(netdev);
12515 netdev_info(netdev, "PCI I/O error detected\n");
12518 netif_device_detach(netdev);
12522 if (state == pci_channel_io_perm_failure) {
12524 return PCI_ERS_RESULT_DISCONNECT;
12527 if (netif_running(netdev))
12528 bnxt_close(netdev);
12530 pci_disable_device(pdev);
12531 bnxt_free_ctx_mem(bp);
12536 /* Request a slot slot reset. */
12537 return PCI_ERS_RESULT_NEED_RESET;
12541 * bnxt_io_slot_reset - called after the pci bus has been reset.
12542 * @pdev: Pointer to PCI device
12544 * Restart the card from scratch, as if from a cold-boot.
12545 * At this point, the card has exprienced a hard reset,
12546 * followed by fixups by BIOS, and has its config space
12547 * set up identically to what it was at cold boot.
12549 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
12551 struct net_device *netdev = pci_get_drvdata(pdev);
12552 struct bnxt *bp = netdev_priv(netdev);
12554 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
12556 netdev_info(bp->dev, "PCI Slot Reset\n");
12560 if (pci_enable_device(pdev)) {
12561 dev_err(&pdev->dev,
12562 "Cannot re-enable PCI device after reset.\n");
12564 pci_set_master(pdev);
12565 pci_restore_state(pdev);
12566 pci_save_state(pdev);
12568 err = bnxt_hwrm_func_reset(bp);
12570 err = bnxt_hwrm_func_qcaps(bp);
12571 if (!err && netif_running(netdev))
12572 err = bnxt_open(netdev);
12574 bnxt_ulp_start(bp, err);
12576 bnxt_reenable_sriov(bp);
12577 result = PCI_ERS_RESULT_RECOVERED;
12581 if (result != PCI_ERS_RESULT_RECOVERED) {
12582 if (netif_running(netdev))
12584 pci_disable_device(pdev);
12593 * bnxt_io_resume - called when traffic can start flowing again.
12594 * @pdev: Pointer to PCI device
12596 * This callback is called when the error recovery driver tells
12597 * us that its OK to resume normal operation.
12599 static void bnxt_io_resume(struct pci_dev *pdev)
12601 struct net_device *netdev = pci_get_drvdata(pdev);
12605 netif_device_attach(netdev);
12610 static const struct pci_error_handlers bnxt_err_handler = {
12611 .error_detected = bnxt_io_error_detected,
12612 .slot_reset = bnxt_io_slot_reset,
12613 .resume = bnxt_io_resume
12616 static struct pci_driver bnxt_pci_driver = {
12617 .name = DRV_MODULE_NAME,
12618 .id_table = bnxt_pci_tbl,
12619 .probe = bnxt_init_one,
12620 .remove = bnxt_remove_one,
12621 .shutdown = bnxt_shutdown,
12622 .driver.pm = BNXT_PM_OPS,
12623 .err_handler = &bnxt_err_handler,
12624 #if defined(CONFIG_BNXT_SRIOV)
12625 .sriov_configure = bnxt_sriov_configure,
12629 static int __init bnxt_init(void)
12632 return pci_register_driver(&bnxt_pci_driver);
12635 static void __exit bnxt_exit(void)
12637 pci_unregister_driver(&bnxt_pci_driver);
12639 destroy_workqueue(bnxt_pf_wq);
12643 module_init(bnxt_init);
12644 module_exit(bnxt_exit);