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 static const char version[] =
74 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
76 MODULE_LICENSE("GPL");
77 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
78 MODULE_VERSION(DRV_MODULE_VERSION);
80 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
81 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
82 #define BNXT_RX_COPY_THRESH 256
84 #define BNXT_TX_PUSH_THRESH 164
131 /* indexed by enum above */
132 static const struct {
135 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
136 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
137 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
138 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
139 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
140 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
141 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
142 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
143 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
144 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
145 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
146 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
147 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
148 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
149 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
150 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
151 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
152 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
153 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
154 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
155 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
156 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
157 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
158 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
159 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
160 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
161 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
162 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
163 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
164 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
165 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
166 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
167 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
168 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
169 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
170 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
171 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
172 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
173 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
174 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
175 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
176 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
179 static const struct pci_device_id bnxt_pci_tbl[] = {
180 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
181 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
182 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
183 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
184 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
185 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
186 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
187 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
188 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
189 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
190 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
191 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
192 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
193 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
194 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
195 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
196 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
197 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
198 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
199 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
200 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
201 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
202 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
203 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
204 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
205 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
206 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
207 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
208 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
209 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
210 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
211 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
212 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
213 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
214 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
215 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
216 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
217 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
218 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR },
219 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
220 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR },
221 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR },
222 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
223 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR },
224 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
225 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
226 #ifdef CONFIG_BNXT_SRIOV
227 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
228 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
229 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
230 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
231 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
232 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
233 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
234 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
235 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
236 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
237 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
242 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
244 static const u16 bnxt_vf_req_snif[] = {
248 HWRM_CFA_L2_FILTER_ALLOC,
251 static const u16 bnxt_async_events_arr[] = {
252 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
253 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
254 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
255 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
256 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
257 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
258 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
261 static struct workqueue_struct *bnxt_pf_wq;
263 static bool bnxt_vf_pciid(enum board_idx idx)
265 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
266 idx == NETXTREME_S_VF || idx == NETXTREME_E_P5_VF);
269 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
270 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
271 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
273 #define BNXT_CP_DB_IRQ_DIS(db) \
274 writel(DB_CP_IRQ_DIS_FLAGS, db)
276 #define BNXT_DB_CQ(db, idx) \
277 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
279 #define BNXT_DB_NQ_P5(db, idx) \
280 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
282 #define BNXT_DB_CQ_ARM(db, idx) \
283 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
285 #define BNXT_DB_NQ_ARM_P5(db, idx) \
286 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
288 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
290 if (bp->flags & BNXT_FLAG_CHIP_P5)
291 BNXT_DB_NQ_P5(db, idx);
296 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
298 if (bp->flags & BNXT_FLAG_CHIP_P5)
299 BNXT_DB_NQ_ARM_P5(db, idx);
301 BNXT_DB_CQ_ARM(db, idx);
304 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
306 if (bp->flags & BNXT_FLAG_CHIP_P5)
307 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
313 const u16 bnxt_lhint_arr[] = {
314 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
315 TX_BD_FLAGS_LHINT_512_TO_1023,
316 TX_BD_FLAGS_LHINT_1024_TO_2047,
317 TX_BD_FLAGS_LHINT_1024_TO_2047,
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,
331 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
332 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
335 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
337 struct metadata_dst *md_dst = skb_metadata_dst(skb);
339 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
342 return md_dst->u.port_info.port_id;
345 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
347 struct bnxt *bp = netdev_priv(dev);
349 struct tx_bd_ext *txbd1;
350 struct netdev_queue *txq;
353 unsigned int length, pad = 0;
354 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
356 struct pci_dev *pdev = bp->pdev;
357 struct bnxt_tx_ring_info *txr;
358 struct bnxt_sw_tx_bd *tx_buf;
360 i = skb_get_queue_mapping(skb);
361 if (unlikely(i >= bp->tx_nr_rings)) {
362 dev_kfree_skb_any(skb);
366 txq = netdev_get_tx_queue(dev, i);
367 txr = &bp->tx_ring[bp->tx_ring_map[i]];
370 free_size = bnxt_tx_avail(bp, txr);
371 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
372 netif_tx_stop_queue(txq);
373 return NETDEV_TX_BUSY;
377 len = skb_headlen(skb);
378 last_frag = skb_shinfo(skb)->nr_frags;
380 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
382 txbd->tx_bd_opaque = prod;
384 tx_buf = &txr->tx_buf_ring[prod];
386 tx_buf->nr_frags = last_frag;
389 cfa_action = bnxt_xmit_get_cfa_action(skb);
390 if (skb_vlan_tag_present(skb)) {
391 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
392 skb_vlan_tag_get(skb);
393 /* Currently supports 8021Q, 8021AD vlan offloads
394 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
396 if (skb->vlan_proto == htons(ETH_P_8021Q))
397 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
400 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
401 struct tx_push_buffer *tx_push_buf = txr->tx_push;
402 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
403 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
404 void __iomem *db = txr->tx_db.doorbell;
405 void *pdata = tx_push_buf->data;
409 /* Set COAL_NOW to be ready quickly for the next push */
410 tx_push->tx_bd_len_flags_type =
411 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
412 TX_BD_TYPE_LONG_TX_BD |
413 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
414 TX_BD_FLAGS_COAL_NOW |
415 TX_BD_FLAGS_PACKET_END |
416 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
418 if (skb->ip_summed == CHECKSUM_PARTIAL)
419 tx_push1->tx_bd_hsize_lflags =
420 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
422 tx_push1->tx_bd_hsize_lflags = 0;
424 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
425 tx_push1->tx_bd_cfa_action =
426 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
428 end = pdata + length;
429 end = PTR_ALIGN(end, 8) - 1;
432 skb_copy_from_linear_data(skb, pdata, len);
434 for (j = 0; j < last_frag; j++) {
435 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
438 fptr = skb_frag_address_safe(frag);
442 memcpy(pdata, fptr, skb_frag_size(frag));
443 pdata += skb_frag_size(frag);
446 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
447 txbd->tx_bd_haddr = txr->data_mapping;
448 prod = NEXT_TX(prod);
449 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
450 memcpy(txbd, tx_push1, sizeof(*txbd));
451 prod = NEXT_TX(prod);
453 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
457 netdev_tx_sent_queue(txq, skb->len);
458 wmb(); /* Sync is_push and byte queue before pushing data */
460 push_len = (length + sizeof(*tx_push) + 7) / 8;
462 __iowrite64_copy(db, tx_push_buf, 16);
463 __iowrite32_copy(db + 4, tx_push_buf + 1,
464 (push_len - 16) << 1);
466 __iowrite64_copy(db, tx_push_buf, push_len);
473 if (length < BNXT_MIN_PKT_SIZE) {
474 pad = BNXT_MIN_PKT_SIZE - length;
475 if (skb_pad(skb, pad)) {
476 /* SKB already freed. */
480 length = BNXT_MIN_PKT_SIZE;
483 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
485 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
486 dev_kfree_skb_any(skb);
491 dma_unmap_addr_set(tx_buf, mapping, mapping);
492 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
493 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
495 txbd->tx_bd_haddr = cpu_to_le64(mapping);
497 prod = NEXT_TX(prod);
498 txbd1 = (struct tx_bd_ext *)
499 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
501 txbd1->tx_bd_hsize_lflags = 0;
502 if (skb_is_gso(skb)) {
505 if (skb->encapsulation)
506 hdr_len = skb_inner_network_offset(skb) +
507 skb_inner_network_header_len(skb) +
508 inner_tcp_hdrlen(skb);
510 hdr_len = skb_transport_offset(skb) +
513 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
515 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
516 length = skb_shinfo(skb)->gso_size;
517 txbd1->tx_bd_mss = cpu_to_le32(length);
519 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
520 txbd1->tx_bd_hsize_lflags =
521 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
522 txbd1->tx_bd_mss = 0;
526 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
527 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
532 flags |= bnxt_lhint_arr[length];
533 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
535 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
536 txbd1->tx_bd_cfa_action =
537 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
538 for (i = 0; i < last_frag; i++) {
539 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
541 prod = NEXT_TX(prod);
542 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
544 len = skb_frag_size(frag);
545 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
548 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
551 tx_buf = &txr->tx_buf_ring[prod];
552 dma_unmap_addr_set(tx_buf, mapping, mapping);
554 txbd->tx_bd_haddr = cpu_to_le64(mapping);
556 flags = len << TX_BD_LEN_SHIFT;
557 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
561 txbd->tx_bd_len_flags_type =
562 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
563 TX_BD_FLAGS_PACKET_END);
565 netdev_tx_sent_queue(txq, skb->len);
567 /* Sync BD data before updating doorbell */
570 prod = NEXT_TX(prod);
573 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
574 bnxt_db_write(bp, &txr->tx_db, prod);
578 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
579 if (netdev_xmit_more() && !tx_buf->is_push)
580 bnxt_db_write(bp, &txr->tx_db, prod);
582 netif_tx_stop_queue(txq);
584 /* netif_tx_stop_queue() must be done before checking
585 * tx index in bnxt_tx_avail() below, because in
586 * bnxt_tx_int(), we update tx index before checking for
587 * netif_tx_queue_stopped().
590 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
591 netif_tx_wake_queue(txq);
598 /* start back at beginning and unmap skb */
600 tx_buf = &txr->tx_buf_ring[prod];
602 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
603 skb_headlen(skb), PCI_DMA_TODEVICE);
604 prod = NEXT_TX(prod);
606 /* unmap remaining mapped pages */
607 for (i = 0; i < last_frag; i++) {
608 prod = NEXT_TX(prod);
609 tx_buf = &txr->tx_buf_ring[prod];
610 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
611 skb_frag_size(&skb_shinfo(skb)->frags[i]),
615 dev_kfree_skb_any(skb);
619 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
621 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
622 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
623 u16 cons = txr->tx_cons;
624 struct pci_dev *pdev = bp->pdev;
626 unsigned int tx_bytes = 0;
628 for (i = 0; i < nr_pkts; i++) {
629 struct bnxt_sw_tx_bd *tx_buf;
633 tx_buf = &txr->tx_buf_ring[cons];
634 cons = NEXT_TX(cons);
638 if (tx_buf->is_push) {
643 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
644 skb_headlen(skb), PCI_DMA_TODEVICE);
645 last = tx_buf->nr_frags;
647 for (j = 0; j < last; j++) {
648 cons = NEXT_TX(cons);
649 tx_buf = &txr->tx_buf_ring[cons];
652 dma_unmap_addr(tx_buf, mapping),
653 skb_frag_size(&skb_shinfo(skb)->frags[j]),
658 cons = NEXT_TX(cons);
660 tx_bytes += skb->len;
661 dev_kfree_skb_any(skb);
664 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
667 /* Need to make the tx_cons update visible to bnxt_start_xmit()
668 * before checking for netif_tx_queue_stopped(). Without the
669 * memory barrier, there is a small possibility that bnxt_start_xmit()
670 * will miss it and cause the queue to be stopped forever.
674 if (unlikely(netif_tx_queue_stopped(txq)) &&
675 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
676 __netif_tx_lock(txq, smp_processor_id());
677 if (netif_tx_queue_stopped(txq) &&
678 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
679 txr->dev_state != BNXT_DEV_STATE_CLOSING)
680 netif_tx_wake_queue(txq);
681 __netif_tx_unlock(txq);
685 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
686 struct bnxt_rx_ring_info *rxr,
689 struct device *dev = &bp->pdev->dev;
692 page = page_pool_dev_alloc_pages(rxr->page_pool);
696 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
697 DMA_ATTR_WEAK_ORDERING);
698 if (dma_mapping_error(dev, *mapping)) {
699 page_pool_recycle_direct(rxr->page_pool, page);
702 *mapping += bp->rx_dma_offset;
706 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
710 struct pci_dev *pdev = bp->pdev;
712 data = kmalloc(bp->rx_buf_size, gfp);
716 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
717 bp->rx_buf_use_size, bp->rx_dir,
718 DMA_ATTR_WEAK_ORDERING);
720 if (dma_mapping_error(&pdev->dev, *mapping)) {
727 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
730 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
731 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
734 if (BNXT_RX_PAGE_MODE(bp)) {
736 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
742 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
744 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
750 rx_buf->data_ptr = data + bp->rx_offset;
752 rx_buf->mapping = mapping;
754 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
758 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
760 u16 prod = rxr->rx_prod;
761 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
762 struct rx_bd *cons_bd, *prod_bd;
764 prod_rx_buf = &rxr->rx_buf_ring[prod];
765 cons_rx_buf = &rxr->rx_buf_ring[cons];
767 prod_rx_buf->data = data;
768 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
770 prod_rx_buf->mapping = cons_rx_buf->mapping;
772 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
773 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
775 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
778 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
780 u16 next, max = rxr->rx_agg_bmap_size;
782 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
784 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
788 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
789 struct bnxt_rx_ring_info *rxr,
793 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
794 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
795 struct pci_dev *pdev = bp->pdev;
798 u16 sw_prod = rxr->rx_sw_agg_prod;
799 unsigned int offset = 0;
801 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
804 page = alloc_page(gfp);
808 rxr->rx_page_offset = 0;
810 offset = rxr->rx_page_offset;
811 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
812 if (rxr->rx_page_offset == PAGE_SIZE)
817 page = alloc_page(gfp);
822 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
823 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
824 DMA_ATTR_WEAK_ORDERING);
825 if (dma_mapping_error(&pdev->dev, mapping)) {
830 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
831 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
833 __set_bit(sw_prod, rxr->rx_agg_bmap);
834 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
835 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
837 rx_agg_buf->page = page;
838 rx_agg_buf->offset = offset;
839 rx_agg_buf->mapping = mapping;
840 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
841 rxbd->rx_bd_opaque = sw_prod;
845 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
846 struct bnxt_cp_ring_info *cpr,
847 u16 cp_cons, u16 curr)
849 struct rx_agg_cmp *agg;
851 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
852 agg = (struct rx_agg_cmp *)
853 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
857 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
858 struct bnxt_rx_ring_info *rxr,
859 u16 agg_id, u16 curr)
861 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
863 return &tpa_info->agg_arr[curr];
866 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
867 u16 start, u32 agg_bufs, bool tpa)
869 struct bnxt_napi *bnapi = cpr->bnapi;
870 struct bnxt *bp = bnapi->bp;
871 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
872 u16 prod = rxr->rx_agg_prod;
873 u16 sw_prod = rxr->rx_sw_agg_prod;
877 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
880 for (i = 0; i < agg_bufs; i++) {
882 struct rx_agg_cmp *agg;
883 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
884 struct rx_bd *prod_bd;
888 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
890 agg = bnxt_get_agg(bp, cpr, idx, start + i);
891 cons = agg->rx_agg_cmp_opaque;
892 __clear_bit(cons, rxr->rx_agg_bmap);
894 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
895 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
897 __set_bit(sw_prod, rxr->rx_agg_bmap);
898 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
899 cons_rx_buf = &rxr->rx_agg_ring[cons];
901 /* It is possible for sw_prod to be equal to cons, so
902 * set cons_rx_buf->page to NULL first.
904 page = cons_rx_buf->page;
905 cons_rx_buf->page = NULL;
906 prod_rx_buf->page = page;
907 prod_rx_buf->offset = cons_rx_buf->offset;
909 prod_rx_buf->mapping = cons_rx_buf->mapping;
911 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
913 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
914 prod_bd->rx_bd_opaque = sw_prod;
916 prod = NEXT_RX_AGG(prod);
917 sw_prod = NEXT_RX_AGG(sw_prod);
919 rxr->rx_agg_prod = prod;
920 rxr->rx_sw_agg_prod = sw_prod;
923 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
924 struct bnxt_rx_ring_info *rxr,
925 u16 cons, void *data, u8 *data_ptr,
927 unsigned int offset_and_len)
929 unsigned int payload = offset_and_len >> 16;
930 unsigned int len = offset_and_len & 0xffff;
932 struct page *page = data;
933 u16 prod = rxr->rx_prod;
937 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
939 bnxt_reuse_rx_data(rxr, cons, data);
942 dma_addr -= bp->rx_dma_offset;
943 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
944 DMA_ATTR_WEAK_ORDERING);
946 if (unlikely(!payload))
947 payload = eth_get_headlen(bp->dev, data_ptr, len);
949 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
955 off = (void *)data_ptr - page_address(page);
956 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
957 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
958 payload + NET_IP_ALIGN);
960 frag = &skb_shinfo(skb)->frags[0];
961 skb_frag_size_sub(frag, payload);
962 skb_frag_off_add(frag, payload);
963 skb->data_len -= payload;
964 skb->tail += payload;
969 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
970 struct bnxt_rx_ring_info *rxr, u16 cons,
971 void *data, u8 *data_ptr,
973 unsigned int offset_and_len)
975 u16 prod = rxr->rx_prod;
979 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
981 bnxt_reuse_rx_data(rxr, cons, data);
985 skb = build_skb(data, 0);
986 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
987 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
993 skb_reserve(skb, bp->rx_offset);
994 skb_put(skb, offset_and_len & 0xffff);
998 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
999 struct bnxt_cp_ring_info *cpr,
1000 struct sk_buff *skb, u16 idx,
1001 u32 agg_bufs, bool tpa)
1003 struct bnxt_napi *bnapi = cpr->bnapi;
1004 struct pci_dev *pdev = bp->pdev;
1005 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1006 u16 prod = rxr->rx_agg_prod;
1007 bool p5_tpa = false;
1010 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1013 for (i = 0; i < agg_bufs; i++) {
1015 struct rx_agg_cmp *agg;
1016 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1021 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1023 agg = bnxt_get_agg(bp, cpr, idx, i);
1024 cons = agg->rx_agg_cmp_opaque;
1025 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1026 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1028 cons_rx_buf = &rxr->rx_agg_ring[cons];
1029 skb_fill_page_desc(skb, i, cons_rx_buf->page,
1030 cons_rx_buf->offset, frag_len);
1031 __clear_bit(cons, rxr->rx_agg_bmap);
1033 /* It is possible for bnxt_alloc_rx_page() to allocate
1034 * a sw_prod index that equals the cons index, so we
1035 * need to clear the cons entry now.
1037 mapping = cons_rx_buf->mapping;
1038 page = cons_rx_buf->page;
1039 cons_rx_buf->page = NULL;
1041 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1042 struct skb_shared_info *shinfo;
1043 unsigned int nr_frags;
1045 shinfo = skb_shinfo(skb);
1046 nr_frags = --shinfo->nr_frags;
1047 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1051 cons_rx_buf->page = page;
1053 /* Update prod since possibly some pages have been
1054 * allocated already.
1056 rxr->rx_agg_prod = prod;
1057 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1061 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1063 DMA_ATTR_WEAK_ORDERING);
1065 skb->data_len += frag_len;
1066 skb->len += frag_len;
1067 skb->truesize += PAGE_SIZE;
1069 prod = NEXT_RX_AGG(prod);
1071 rxr->rx_agg_prod = prod;
1075 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1076 u8 agg_bufs, u32 *raw_cons)
1079 struct rx_agg_cmp *agg;
1081 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1082 last = RING_CMP(*raw_cons);
1083 agg = (struct rx_agg_cmp *)
1084 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1085 return RX_AGG_CMP_VALID(agg, *raw_cons);
1088 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1092 struct bnxt *bp = bnapi->bp;
1093 struct pci_dev *pdev = bp->pdev;
1094 struct sk_buff *skb;
1096 skb = napi_alloc_skb(&bnapi->napi, len);
1100 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1103 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1104 len + NET_IP_ALIGN);
1106 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1113 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1114 u32 *raw_cons, void *cmp)
1116 struct rx_cmp *rxcmp = cmp;
1117 u32 tmp_raw_cons = *raw_cons;
1118 u8 cmp_type, agg_bufs = 0;
1120 cmp_type = RX_CMP_TYPE(rxcmp);
1122 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1123 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1125 RX_CMP_AGG_BUFS_SHIFT;
1126 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1127 struct rx_tpa_end_cmp *tpa_end = cmp;
1129 if (bp->flags & BNXT_FLAG_CHIP_P5)
1132 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1136 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1139 *raw_cons = tmp_raw_cons;
1143 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1146 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1148 schedule_delayed_work(&bp->fw_reset_task, delay);
1151 static void bnxt_queue_sp_work(struct bnxt *bp)
1154 queue_work(bnxt_pf_wq, &bp->sp_task);
1156 schedule_work(&bp->sp_task);
1159 static void bnxt_cancel_sp_work(struct bnxt *bp)
1162 flush_workqueue(bnxt_pf_wq);
1164 cancel_work_sync(&bp->sp_task);
1167 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1169 if (!rxr->bnapi->in_reset) {
1170 rxr->bnapi->in_reset = true;
1171 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1172 bnxt_queue_sp_work(bp);
1174 rxr->rx_next_cons = 0xffff;
1177 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1179 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1180 u16 idx = agg_id & MAX_TPA_P5_MASK;
1182 if (test_bit(idx, map->agg_idx_bmap))
1183 idx = find_first_zero_bit(map->agg_idx_bmap,
1184 BNXT_AGG_IDX_BMAP_SIZE);
1185 __set_bit(idx, map->agg_idx_bmap);
1186 map->agg_id_tbl[agg_id] = idx;
1190 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1192 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1194 __clear_bit(idx, map->agg_idx_bmap);
1197 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1199 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1201 return map->agg_id_tbl[agg_id];
1204 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1205 struct rx_tpa_start_cmp *tpa_start,
1206 struct rx_tpa_start_cmp_ext *tpa_start1)
1208 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1209 struct bnxt_tpa_info *tpa_info;
1210 u16 cons, prod, agg_id;
1211 struct rx_bd *prod_bd;
1214 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1215 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1216 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1218 agg_id = TPA_START_AGG_ID(tpa_start);
1220 cons = tpa_start->rx_tpa_start_cmp_opaque;
1221 prod = rxr->rx_prod;
1222 cons_rx_buf = &rxr->rx_buf_ring[cons];
1223 prod_rx_buf = &rxr->rx_buf_ring[prod];
1224 tpa_info = &rxr->rx_tpa[agg_id];
1226 if (unlikely(cons != rxr->rx_next_cons ||
1227 TPA_START_ERROR(tpa_start))) {
1228 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1229 cons, rxr->rx_next_cons,
1230 TPA_START_ERROR_CODE(tpa_start1));
1231 bnxt_sched_reset(bp, rxr);
1234 /* Store cfa_code in tpa_info to use in tpa_end
1235 * completion processing.
1237 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1238 prod_rx_buf->data = tpa_info->data;
1239 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1241 mapping = tpa_info->mapping;
1242 prod_rx_buf->mapping = mapping;
1244 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1246 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1248 tpa_info->data = cons_rx_buf->data;
1249 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1250 cons_rx_buf->data = NULL;
1251 tpa_info->mapping = cons_rx_buf->mapping;
1254 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1255 RX_TPA_START_CMP_LEN_SHIFT;
1256 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1257 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1259 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1260 tpa_info->gso_type = SKB_GSO_TCPV4;
1261 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1262 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1263 tpa_info->gso_type = SKB_GSO_TCPV6;
1264 tpa_info->rss_hash =
1265 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1267 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1268 tpa_info->gso_type = 0;
1269 if (netif_msg_rx_err(bp))
1270 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1272 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1273 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1274 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1275 tpa_info->agg_count = 0;
1277 rxr->rx_prod = NEXT_RX(prod);
1278 cons = NEXT_RX(cons);
1279 rxr->rx_next_cons = NEXT_RX(cons);
1280 cons_rx_buf = &rxr->rx_buf_ring[cons];
1282 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1283 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1284 cons_rx_buf->data = NULL;
1287 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1290 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1294 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1296 struct udphdr *uh = NULL;
1298 if (ip_proto == htons(ETH_P_IP)) {
1299 struct iphdr *iph = (struct iphdr *)skb->data;
1301 if (iph->protocol == IPPROTO_UDP)
1302 uh = (struct udphdr *)(iph + 1);
1304 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1306 if (iph->nexthdr == IPPROTO_UDP)
1307 uh = (struct udphdr *)(iph + 1);
1311 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1313 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1318 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1319 int payload_off, int tcp_ts,
1320 struct sk_buff *skb)
1325 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1326 u32 hdr_info = tpa_info->hdr_info;
1327 bool loopback = false;
1329 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1330 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1331 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1333 /* If the packet is an internal loopback packet, the offsets will
1334 * have an extra 4 bytes.
1336 if (inner_mac_off == 4) {
1338 } else if (inner_mac_off > 4) {
1339 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1342 /* We only support inner iPv4/ipv6. If we don't see the
1343 * correct protocol ID, it must be a loopback packet where
1344 * the offsets are off by 4.
1346 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1350 /* internal loopback packet, subtract all offsets by 4 */
1356 nw_off = inner_ip_off - ETH_HLEN;
1357 skb_set_network_header(skb, nw_off);
1358 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1359 struct ipv6hdr *iph = ipv6_hdr(skb);
1361 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1362 len = skb->len - skb_transport_offset(skb);
1364 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1366 struct iphdr *iph = ip_hdr(skb);
1368 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1369 len = skb->len - skb_transport_offset(skb);
1371 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1374 if (inner_mac_off) { /* tunnel */
1375 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1378 bnxt_gro_tunnel(skb, proto);
1384 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1385 int payload_off, int tcp_ts,
1386 struct sk_buff *skb)
1389 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1390 u32 hdr_info = tpa_info->hdr_info;
1391 int iphdr_len, nw_off;
1393 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1394 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1395 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1397 nw_off = inner_ip_off - ETH_HLEN;
1398 skb_set_network_header(skb, nw_off);
1399 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1400 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1401 skb_set_transport_header(skb, nw_off + iphdr_len);
1403 if (inner_mac_off) { /* tunnel */
1404 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1407 bnxt_gro_tunnel(skb, proto);
1413 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1414 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1416 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1417 int payload_off, int tcp_ts,
1418 struct sk_buff *skb)
1422 int len, nw_off, tcp_opt_len = 0;
1427 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1430 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1432 skb_set_network_header(skb, nw_off);
1434 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1435 len = skb->len - skb_transport_offset(skb);
1437 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1438 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1439 struct ipv6hdr *iph;
1441 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1443 skb_set_network_header(skb, nw_off);
1444 iph = ipv6_hdr(skb);
1445 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1446 len = skb->len - skb_transport_offset(skb);
1448 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1450 dev_kfree_skb_any(skb);
1454 if (nw_off) /* tunnel */
1455 bnxt_gro_tunnel(skb, skb->protocol);
1460 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1461 struct bnxt_tpa_info *tpa_info,
1462 struct rx_tpa_end_cmp *tpa_end,
1463 struct rx_tpa_end_cmp_ext *tpa_end1,
1464 struct sk_buff *skb)
1470 segs = TPA_END_TPA_SEGS(tpa_end);
1474 NAPI_GRO_CB(skb)->count = segs;
1475 skb_shinfo(skb)->gso_size =
1476 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1477 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1478 if (bp->flags & BNXT_FLAG_CHIP_P5)
1479 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1481 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1482 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1484 tcp_gro_complete(skb);
1489 /* Given the cfa_code of a received packet determine which
1490 * netdev (vf-rep or PF) the packet is destined to.
1492 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1494 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1496 /* if vf-rep dev is NULL, the must belongs to the PF */
1497 return dev ? dev : bp->dev;
1500 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1501 struct bnxt_cp_ring_info *cpr,
1503 struct rx_tpa_end_cmp *tpa_end,
1504 struct rx_tpa_end_cmp_ext *tpa_end1,
1507 struct bnxt_napi *bnapi = cpr->bnapi;
1508 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1509 u8 *data_ptr, agg_bufs;
1511 struct bnxt_tpa_info *tpa_info;
1513 struct sk_buff *skb;
1514 u16 idx = 0, agg_id;
1518 if (unlikely(bnapi->in_reset)) {
1519 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1522 return ERR_PTR(-EBUSY);
1526 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1527 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1528 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1529 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1530 tpa_info = &rxr->rx_tpa[agg_id];
1531 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1532 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1533 agg_bufs, tpa_info->agg_count);
1534 agg_bufs = tpa_info->agg_count;
1536 tpa_info->agg_count = 0;
1537 *event |= BNXT_AGG_EVENT;
1538 bnxt_free_agg_idx(rxr, agg_id);
1540 gro = !!(bp->flags & BNXT_FLAG_GRO);
1542 agg_id = TPA_END_AGG_ID(tpa_end);
1543 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1544 tpa_info = &rxr->rx_tpa[agg_id];
1545 idx = RING_CMP(*raw_cons);
1547 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1548 return ERR_PTR(-EBUSY);
1550 *event |= BNXT_AGG_EVENT;
1551 idx = NEXT_CMP(idx);
1553 gro = !!TPA_END_GRO(tpa_end);
1555 data = tpa_info->data;
1556 data_ptr = tpa_info->data_ptr;
1558 len = tpa_info->len;
1559 mapping = tpa_info->mapping;
1561 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1562 bnxt_abort_tpa(cpr, idx, agg_bufs);
1563 if (agg_bufs > MAX_SKB_FRAGS)
1564 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1565 agg_bufs, (int)MAX_SKB_FRAGS);
1569 if (len <= bp->rx_copy_thresh) {
1570 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1572 bnxt_abort_tpa(cpr, idx, agg_bufs);
1577 dma_addr_t new_mapping;
1579 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1581 bnxt_abort_tpa(cpr, idx, agg_bufs);
1585 tpa_info->data = new_data;
1586 tpa_info->data_ptr = new_data + bp->rx_offset;
1587 tpa_info->mapping = new_mapping;
1589 skb = build_skb(data, 0);
1590 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1591 bp->rx_buf_use_size, bp->rx_dir,
1592 DMA_ATTR_WEAK_ORDERING);
1596 bnxt_abort_tpa(cpr, idx, agg_bufs);
1599 skb_reserve(skb, bp->rx_offset);
1604 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1606 /* Page reuse already handled by bnxt_rx_pages(). */
1612 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1614 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1615 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1617 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1618 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1619 u16 vlan_proto = tpa_info->metadata >>
1620 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1621 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1623 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1626 skb_checksum_none_assert(skb);
1627 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1628 skb->ip_summed = CHECKSUM_UNNECESSARY;
1630 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1634 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1639 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1640 struct rx_agg_cmp *rx_agg)
1642 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1643 struct bnxt_tpa_info *tpa_info;
1645 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1646 tpa_info = &rxr->rx_tpa[agg_id];
1647 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1648 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1651 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1652 struct sk_buff *skb)
1654 if (skb->dev != bp->dev) {
1655 /* this packet belongs to a vf-rep */
1656 bnxt_vf_rep_rx(bp, skb);
1659 skb_record_rx_queue(skb, bnapi->index);
1660 napi_gro_receive(&bnapi->napi, skb);
1663 /* returns the following:
1664 * 1 - 1 packet successfully received
1665 * 0 - successful TPA_START, packet not completed yet
1666 * -EBUSY - completion ring does not have all the agg buffers yet
1667 * -ENOMEM - packet aborted due to out of memory
1668 * -EIO - packet aborted due to hw error indicated in BD
1670 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1671 u32 *raw_cons, u8 *event)
1673 struct bnxt_napi *bnapi = cpr->bnapi;
1674 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1675 struct net_device *dev = bp->dev;
1676 struct rx_cmp *rxcmp;
1677 struct rx_cmp_ext *rxcmp1;
1678 u32 tmp_raw_cons = *raw_cons;
1679 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1680 struct bnxt_sw_rx_bd *rx_buf;
1682 u8 *data_ptr, agg_bufs, cmp_type;
1683 dma_addr_t dma_addr;
1684 struct sk_buff *skb;
1689 rxcmp = (struct rx_cmp *)
1690 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1692 cmp_type = RX_CMP_TYPE(rxcmp);
1694 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1695 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1696 goto next_rx_no_prod_no_len;
1699 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1700 cp_cons = RING_CMP(tmp_raw_cons);
1701 rxcmp1 = (struct rx_cmp_ext *)
1702 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1704 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1707 prod = rxr->rx_prod;
1709 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1710 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1711 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1713 *event |= BNXT_RX_EVENT;
1714 goto next_rx_no_prod_no_len;
1716 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1717 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1718 (struct rx_tpa_end_cmp *)rxcmp,
1719 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1726 bnxt_deliver_skb(bp, bnapi, skb);
1729 *event |= BNXT_RX_EVENT;
1730 goto next_rx_no_prod_no_len;
1733 cons = rxcmp->rx_cmp_opaque;
1734 if (unlikely(cons != rxr->rx_next_cons)) {
1735 int rc1 = bnxt_discard_rx(bp, cpr, raw_cons, rxcmp);
1737 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1738 cons, rxr->rx_next_cons);
1739 bnxt_sched_reset(bp, rxr);
1742 rx_buf = &rxr->rx_buf_ring[cons];
1743 data = rx_buf->data;
1744 data_ptr = rx_buf->data_ptr;
1747 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1748 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1751 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1754 cp_cons = NEXT_CMP(cp_cons);
1755 *event |= BNXT_AGG_EVENT;
1757 *event |= BNXT_RX_EVENT;
1759 rx_buf->data = NULL;
1760 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1761 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1763 bnxt_reuse_rx_data(rxr, cons, data);
1765 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1769 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1770 bnapi->cp_ring.rx_buf_errors++;
1771 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
1772 netdev_warn(bp->dev, "RX buffer error %x\n",
1774 bnxt_sched_reset(bp, rxr);
1777 goto next_rx_no_len;
1780 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1781 dma_addr = rx_buf->mapping;
1783 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1788 if (len <= bp->rx_copy_thresh) {
1789 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1790 bnxt_reuse_rx_data(rxr, cons, data);
1793 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1801 if (rx_buf->data_ptr == data_ptr)
1802 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1805 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1814 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1821 if (RX_CMP_HASH_VALID(rxcmp)) {
1822 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1823 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1825 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1826 if (hash_type != 1 && hash_type != 3)
1827 type = PKT_HASH_TYPE_L3;
1828 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1831 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1832 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1834 if ((rxcmp1->rx_cmp_flags2 &
1835 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1836 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1837 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1838 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1839 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1841 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1844 skb_checksum_none_assert(skb);
1845 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1846 if (dev->features & NETIF_F_RXCSUM) {
1847 skb->ip_summed = CHECKSUM_UNNECESSARY;
1848 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1851 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1852 if (dev->features & NETIF_F_RXCSUM)
1853 bnapi->cp_ring.rx_l4_csum_errors++;
1857 bnxt_deliver_skb(bp, bnapi, skb);
1861 cpr->rx_packets += 1;
1862 cpr->rx_bytes += len;
1865 rxr->rx_prod = NEXT_RX(prod);
1866 rxr->rx_next_cons = NEXT_RX(cons);
1868 next_rx_no_prod_no_len:
1869 *raw_cons = tmp_raw_cons;
1874 /* In netpoll mode, if we are using a combined completion ring, we need to
1875 * discard the rx packets and recycle the buffers.
1877 static int bnxt_force_rx_discard(struct bnxt *bp,
1878 struct bnxt_cp_ring_info *cpr,
1879 u32 *raw_cons, u8 *event)
1881 u32 tmp_raw_cons = *raw_cons;
1882 struct rx_cmp_ext *rxcmp1;
1883 struct rx_cmp *rxcmp;
1887 cp_cons = RING_CMP(tmp_raw_cons);
1888 rxcmp = (struct rx_cmp *)
1889 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1891 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1892 cp_cons = RING_CMP(tmp_raw_cons);
1893 rxcmp1 = (struct rx_cmp_ext *)
1894 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1896 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1899 cmp_type = RX_CMP_TYPE(rxcmp);
1900 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1901 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1902 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1903 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1904 struct rx_tpa_end_cmp_ext *tpa_end1;
1906 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1907 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1908 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1910 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1913 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
1915 struct bnxt_fw_health *fw_health = bp->fw_health;
1916 u32 reg = fw_health->regs[reg_idx];
1917 u32 reg_type, reg_off, val = 0;
1919 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
1920 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
1922 case BNXT_FW_HEALTH_REG_TYPE_CFG:
1923 pci_read_config_dword(bp->pdev, reg_off, &val);
1925 case BNXT_FW_HEALTH_REG_TYPE_GRC:
1926 reg_off = fw_health->mapped_regs[reg_idx];
1928 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
1929 val = readl(bp->bar0 + reg_off);
1931 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
1932 val = readl(bp->bar1 + reg_off);
1935 if (reg_idx == BNXT_FW_RESET_INPROG_REG)
1936 val &= fw_health->fw_reset_inprog_reg_mask;
1940 #define BNXT_GET_EVENT_PORT(data) \
1942 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1944 static int bnxt_async_event_process(struct bnxt *bp,
1945 struct hwrm_async_event_cmpl *cmpl)
1947 u16 event_id = le16_to_cpu(cmpl->event_id);
1949 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1951 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1952 u32 data1 = le32_to_cpu(cmpl->event_data1);
1953 struct bnxt_link_info *link_info = &bp->link_info;
1956 goto async_event_process_exit;
1958 /* print unsupported speed warning in forced speed mode only */
1959 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1960 (data1 & 0x20000)) {
1961 u16 fw_speed = link_info->force_link_speed;
1962 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1964 if (speed != SPEED_UNKNOWN)
1965 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1968 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1971 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1972 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1974 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1975 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1977 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1978 u32 data1 = le32_to_cpu(cmpl->event_data1);
1979 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1984 if (bp->pf.port_id != port_id)
1987 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1990 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1992 goto async_event_process_exit;
1993 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1995 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
1996 u32 data1 = le32_to_cpu(cmpl->event_data1);
1998 bp->fw_reset_timestamp = jiffies;
1999 bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2000 if (!bp->fw_reset_min_dsecs)
2001 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2002 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2003 if (!bp->fw_reset_max_dsecs)
2004 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2005 if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2006 netdev_warn(bp->dev, "Firmware fatal reset event received\n");
2007 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2009 netdev_warn(bp->dev, "Firmware non-fatal reset event received, max wait time %d msec\n",
2010 bp->fw_reset_max_dsecs * 100);
2012 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2015 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2016 struct bnxt_fw_health *fw_health = bp->fw_health;
2017 u32 data1 = le32_to_cpu(cmpl->event_data1);
2020 goto async_event_process_exit;
2022 fw_health->enabled = EVENT_DATA1_RECOVERY_ENABLED(data1);
2023 fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2024 if (!fw_health->enabled)
2027 if (netif_msg_drv(bp))
2028 netdev_info(bp->dev, "Error recovery info: error recovery[%d], master[%d], reset count[0x%x], health status: 0x%x\n",
2029 fw_health->enabled, fw_health->master,
2030 bnxt_fw_health_readl(bp,
2031 BNXT_FW_RESET_CNT_REG),
2032 bnxt_fw_health_readl(bp,
2033 BNXT_FW_HEALTH_REG));
2034 fw_health->tmr_multiplier =
2035 DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2036 bp->current_interval * 10);
2037 fw_health->tmr_counter = fw_health->tmr_multiplier;
2038 fw_health->last_fw_heartbeat =
2039 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2040 fw_health->last_fw_reset_cnt =
2041 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2042 goto async_event_process_exit;
2045 goto async_event_process_exit;
2047 bnxt_queue_sp_work(bp);
2048 async_event_process_exit:
2049 bnxt_ulp_async_events(bp, cmpl);
2053 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2055 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2056 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2057 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2058 (struct hwrm_fwd_req_cmpl *)txcmp;
2060 switch (cmpl_type) {
2061 case CMPL_BASE_TYPE_HWRM_DONE:
2062 seq_id = le16_to_cpu(h_cmpl->sequence_id);
2063 if (seq_id == bp->hwrm_intr_seq_id)
2064 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2066 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2069 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2070 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2072 if ((vf_id < bp->pf.first_vf_id) ||
2073 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2074 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2079 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2080 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2081 bnxt_queue_sp_work(bp);
2084 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2085 bnxt_async_event_process(bp,
2086 (struct hwrm_async_event_cmpl *)txcmp);
2095 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2097 struct bnxt_napi *bnapi = dev_instance;
2098 struct bnxt *bp = bnapi->bp;
2099 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2100 u32 cons = RING_CMP(cpr->cp_raw_cons);
2103 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2104 napi_schedule(&bnapi->napi);
2108 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2110 u32 raw_cons = cpr->cp_raw_cons;
2111 u16 cons = RING_CMP(raw_cons);
2112 struct tx_cmp *txcmp;
2114 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2116 return TX_CMP_VALID(txcmp, raw_cons);
2119 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2121 struct bnxt_napi *bnapi = dev_instance;
2122 struct bnxt *bp = bnapi->bp;
2123 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2124 u32 cons = RING_CMP(cpr->cp_raw_cons);
2127 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2129 if (!bnxt_has_work(bp, cpr)) {
2130 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2131 /* return if erroneous interrupt */
2132 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2136 /* disable ring IRQ */
2137 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2139 /* Return here if interrupt is shared and is disabled. */
2140 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2143 napi_schedule(&bnapi->napi);
2147 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2150 struct bnxt_napi *bnapi = cpr->bnapi;
2151 u32 raw_cons = cpr->cp_raw_cons;
2156 struct tx_cmp *txcmp;
2158 cpr->has_more_work = 0;
2162 cons = RING_CMP(raw_cons);
2163 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2165 if (!TX_CMP_VALID(txcmp, raw_cons))
2168 /* The valid test of the entry must be done first before
2169 * reading any further.
2172 cpr->had_work_done = 1;
2173 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2175 /* return full budget so NAPI will complete. */
2176 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
2178 raw_cons = NEXT_RAW_CMP(raw_cons);
2180 cpr->has_more_work = 1;
2183 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2185 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2187 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2189 if (likely(rc >= 0))
2191 /* Increment rx_pkts when rc is -ENOMEM to count towards
2192 * the NAPI budget. Otherwise, we may potentially loop
2193 * here forever if we consistently cannot allocate
2196 else if (rc == -ENOMEM && budget)
2198 else if (rc == -EBUSY) /* partial completion */
2200 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2201 CMPL_BASE_TYPE_HWRM_DONE) ||
2202 (TX_CMP_TYPE(txcmp) ==
2203 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2204 (TX_CMP_TYPE(txcmp) ==
2205 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2206 bnxt_hwrm_handler(bp, txcmp);
2208 raw_cons = NEXT_RAW_CMP(raw_cons);
2210 if (rx_pkts && rx_pkts == budget) {
2211 cpr->has_more_work = 1;
2216 if (event & BNXT_REDIRECT_EVENT)
2219 if (event & BNXT_TX_EVENT) {
2220 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2221 u16 prod = txr->tx_prod;
2223 /* Sync BD data before updating doorbell */
2226 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2229 cpr->cp_raw_cons = raw_cons;
2230 bnapi->tx_pkts += tx_pkts;
2231 bnapi->events |= event;
2235 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2237 if (bnapi->tx_pkts) {
2238 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2242 if (bnapi->events & BNXT_RX_EVENT) {
2243 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2245 if (bnapi->events & BNXT_AGG_EVENT)
2246 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2247 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2252 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2255 struct bnxt_napi *bnapi = cpr->bnapi;
2258 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2260 /* ACK completion ring before freeing tx ring and producing new
2261 * buffers in rx/agg rings to prevent overflowing the completion
2264 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2266 __bnxt_poll_work_done(bp, bnapi);
2270 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2272 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2273 struct bnxt *bp = bnapi->bp;
2274 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2275 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2276 struct tx_cmp *txcmp;
2277 struct rx_cmp_ext *rxcmp1;
2278 u32 cp_cons, tmp_raw_cons;
2279 u32 raw_cons = cpr->cp_raw_cons;
2286 cp_cons = RING_CMP(raw_cons);
2287 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2289 if (!TX_CMP_VALID(txcmp, raw_cons))
2292 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2293 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2294 cp_cons = RING_CMP(tmp_raw_cons);
2295 rxcmp1 = (struct rx_cmp_ext *)
2296 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2298 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2301 /* force an error to recycle the buffer */
2302 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2303 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2305 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2306 if (likely(rc == -EIO) && budget)
2308 else if (rc == -EBUSY) /* partial completion */
2310 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2311 CMPL_BASE_TYPE_HWRM_DONE)) {
2312 bnxt_hwrm_handler(bp, txcmp);
2315 "Invalid completion received on special ring\n");
2317 raw_cons = NEXT_RAW_CMP(raw_cons);
2319 if (rx_pkts == budget)
2323 cpr->cp_raw_cons = raw_cons;
2324 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2325 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2327 if (event & BNXT_AGG_EVENT)
2328 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2330 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2331 napi_complete_done(napi, rx_pkts);
2332 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2337 static int bnxt_poll(struct napi_struct *napi, int budget)
2339 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2340 struct bnxt *bp = bnapi->bp;
2341 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2345 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2347 if (work_done >= budget) {
2349 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2353 if (!bnxt_has_work(bp, cpr)) {
2354 if (napi_complete_done(napi, work_done))
2355 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2359 if (bp->flags & BNXT_FLAG_DIM) {
2360 struct dim_sample dim_sample = {};
2362 dim_update_sample(cpr->event_ctr,
2366 net_dim(&cpr->dim, dim_sample);
2371 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2373 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2374 int i, work_done = 0;
2376 for (i = 0; i < 2; i++) {
2377 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2380 work_done += __bnxt_poll_work(bp, cpr2,
2381 budget - work_done);
2382 cpr->has_more_work |= cpr2->has_more_work;
2388 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2389 u64 dbr_type, bool all)
2391 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2394 for (i = 0; i < 2; i++) {
2395 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2396 struct bnxt_db_info *db;
2398 if (cpr2 && (all || cpr2->had_work_done)) {
2400 writeq(db->db_key64 | dbr_type |
2401 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2402 cpr2->had_work_done = 0;
2405 __bnxt_poll_work_done(bp, bnapi);
2408 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2410 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2411 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2412 u32 raw_cons = cpr->cp_raw_cons;
2413 struct bnxt *bp = bnapi->bp;
2414 struct nqe_cn *nqcmp;
2418 if (cpr->has_more_work) {
2419 cpr->has_more_work = 0;
2420 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2421 if (cpr->has_more_work) {
2422 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, false);
2425 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, true);
2426 if (napi_complete_done(napi, work_done))
2427 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, cpr->cp_raw_cons);
2431 cons = RING_CMP(raw_cons);
2432 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2434 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2435 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
2437 cpr->cp_raw_cons = raw_cons;
2438 if (napi_complete_done(napi, work_done))
2439 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2444 /* The valid test of the entry must be done first before
2445 * reading any further.
2449 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2450 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2451 struct bnxt_cp_ring_info *cpr2;
2453 cpr2 = cpr->cp_ring_arr[idx];
2454 work_done += __bnxt_poll_work(bp, cpr2,
2455 budget - work_done);
2456 cpr->has_more_work = cpr2->has_more_work;
2458 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2460 raw_cons = NEXT_RAW_CMP(raw_cons);
2461 if (cpr->has_more_work)
2464 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, true);
2465 cpr->cp_raw_cons = raw_cons;
2469 static void bnxt_free_tx_skbs(struct bnxt *bp)
2472 struct pci_dev *pdev = bp->pdev;
2477 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2478 for (i = 0; i < bp->tx_nr_rings; i++) {
2479 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2482 for (j = 0; j < max_idx;) {
2483 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2484 struct sk_buff *skb;
2487 if (i < bp->tx_nr_rings_xdp &&
2488 tx_buf->action == XDP_REDIRECT) {
2489 dma_unmap_single(&pdev->dev,
2490 dma_unmap_addr(tx_buf, mapping),
2491 dma_unmap_len(tx_buf, len),
2493 xdp_return_frame(tx_buf->xdpf);
2495 tx_buf->xdpf = NULL;
2508 if (tx_buf->is_push) {
2514 dma_unmap_single(&pdev->dev,
2515 dma_unmap_addr(tx_buf, mapping),
2519 last = tx_buf->nr_frags;
2521 for (k = 0; k < last; k++, j++) {
2522 int ring_idx = j & bp->tx_ring_mask;
2523 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2525 tx_buf = &txr->tx_buf_ring[ring_idx];
2528 dma_unmap_addr(tx_buf, mapping),
2529 skb_frag_size(frag), PCI_DMA_TODEVICE);
2533 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2537 static void bnxt_free_rx_skbs(struct bnxt *bp)
2539 int i, max_idx, max_agg_idx;
2540 struct pci_dev *pdev = bp->pdev;
2545 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2546 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2547 for (i = 0; i < bp->rx_nr_rings; i++) {
2548 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2549 struct bnxt_tpa_idx_map *map;
2553 for (j = 0; j < bp->max_tpa; j++) {
2554 struct bnxt_tpa_info *tpa_info =
2556 u8 *data = tpa_info->data;
2561 dma_unmap_single_attrs(&pdev->dev,
2563 bp->rx_buf_use_size,
2565 DMA_ATTR_WEAK_ORDERING);
2567 tpa_info->data = NULL;
2573 for (j = 0; j < max_idx; j++) {
2574 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2575 dma_addr_t mapping = rx_buf->mapping;
2576 void *data = rx_buf->data;
2581 rx_buf->data = NULL;
2583 if (BNXT_RX_PAGE_MODE(bp)) {
2584 mapping -= bp->rx_dma_offset;
2585 dma_unmap_page_attrs(&pdev->dev, mapping,
2586 PAGE_SIZE, bp->rx_dir,
2587 DMA_ATTR_WEAK_ORDERING);
2588 page_pool_recycle_direct(rxr->page_pool, data);
2590 dma_unmap_single_attrs(&pdev->dev, mapping,
2591 bp->rx_buf_use_size,
2593 DMA_ATTR_WEAK_ORDERING);
2598 for (j = 0; j < max_agg_idx; j++) {
2599 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2600 &rxr->rx_agg_ring[j];
2601 struct page *page = rx_agg_buf->page;
2606 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2609 DMA_ATTR_WEAK_ORDERING);
2611 rx_agg_buf->page = NULL;
2612 __clear_bit(j, rxr->rx_agg_bmap);
2617 __free_page(rxr->rx_page);
2618 rxr->rx_page = NULL;
2620 map = rxr->rx_tpa_idx_map;
2622 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2626 static void bnxt_free_skbs(struct bnxt *bp)
2628 bnxt_free_tx_skbs(bp);
2629 bnxt_free_rx_skbs(bp);
2632 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2634 struct pci_dev *pdev = bp->pdev;
2637 for (i = 0; i < rmem->nr_pages; i++) {
2638 if (!rmem->pg_arr[i])
2641 dma_free_coherent(&pdev->dev, rmem->page_size,
2642 rmem->pg_arr[i], rmem->dma_arr[i]);
2644 rmem->pg_arr[i] = NULL;
2647 size_t pg_tbl_size = rmem->nr_pages * 8;
2649 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2650 pg_tbl_size = rmem->page_size;
2651 dma_free_coherent(&pdev->dev, pg_tbl_size,
2652 rmem->pg_tbl, rmem->pg_tbl_map);
2653 rmem->pg_tbl = NULL;
2655 if (rmem->vmem_size && *rmem->vmem) {
2661 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2663 struct pci_dev *pdev = bp->pdev;
2667 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2668 valid_bit = PTU_PTE_VALID;
2669 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2670 size_t pg_tbl_size = rmem->nr_pages * 8;
2672 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2673 pg_tbl_size = rmem->page_size;
2674 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2681 for (i = 0; i < rmem->nr_pages; i++) {
2682 u64 extra_bits = valid_bit;
2684 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2688 if (!rmem->pg_arr[i])
2691 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2692 if (i == rmem->nr_pages - 2 &&
2693 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2694 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2695 else if (i == rmem->nr_pages - 1 &&
2696 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2697 extra_bits |= PTU_PTE_LAST;
2699 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2703 if (rmem->vmem_size) {
2704 *rmem->vmem = vzalloc(rmem->vmem_size);
2711 static void bnxt_free_tpa_info(struct bnxt *bp)
2715 for (i = 0; i < bp->rx_nr_rings; i++) {
2716 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2718 kfree(rxr->rx_tpa_idx_map);
2719 rxr->rx_tpa_idx_map = NULL;
2721 kfree(rxr->rx_tpa[0].agg_arr);
2722 rxr->rx_tpa[0].agg_arr = NULL;
2729 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2731 int i, j, total_aggs = 0;
2733 bp->max_tpa = MAX_TPA;
2734 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2735 if (!bp->max_tpa_v2)
2737 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2738 total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2741 for (i = 0; i < bp->rx_nr_rings; i++) {
2742 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2743 struct rx_agg_cmp *agg;
2745 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2750 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2752 agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2753 rxr->rx_tpa[0].agg_arr = agg;
2756 for (j = 1; j < bp->max_tpa; j++)
2757 rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2758 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2760 if (!rxr->rx_tpa_idx_map)
2766 static void bnxt_free_rx_rings(struct bnxt *bp)
2773 bnxt_free_tpa_info(bp);
2774 for (i = 0; i < bp->rx_nr_rings; i++) {
2775 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2776 struct bnxt_ring_struct *ring;
2779 bpf_prog_put(rxr->xdp_prog);
2781 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2782 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2784 page_pool_destroy(rxr->page_pool);
2785 rxr->page_pool = NULL;
2787 kfree(rxr->rx_agg_bmap);
2788 rxr->rx_agg_bmap = NULL;
2790 ring = &rxr->rx_ring_struct;
2791 bnxt_free_ring(bp, &ring->ring_mem);
2793 ring = &rxr->rx_agg_ring_struct;
2794 bnxt_free_ring(bp, &ring->ring_mem);
2798 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2799 struct bnxt_rx_ring_info *rxr)
2801 struct page_pool_params pp = { 0 };
2803 pp.pool_size = bp->rx_ring_size;
2804 pp.nid = dev_to_node(&bp->pdev->dev);
2805 pp.dev = &bp->pdev->dev;
2806 pp.dma_dir = DMA_BIDIRECTIONAL;
2808 rxr->page_pool = page_pool_create(&pp);
2809 if (IS_ERR(rxr->page_pool)) {
2810 int err = PTR_ERR(rxr->page_pool);
2812 rxr->page_pool = NULL;
2818 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2820 int i, rc = 0, agg_rings = 0;
2825 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2828 for (i = 0; i < bp->rx_nr_rings; i++) {
2829 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2830 struct bnxt_ring_struct *ring;
2832 ring = &rxr->rx_ring_struct;
2834 rc = bnxt_alloc_rx_page_pool(bp, rxr);
2838 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2842 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2846 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2850 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2858 ring = &rxr->rx_agg_ring_struct;
2859 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2864 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2865 mem_size = rxr->rx_agg_bmap_size / 8;
2866 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2867 if (!rxr->rx_agg_bmap)
2871 if (bp->flags & BNXT_FLAG_TPA)
2872 rc = bnxt_alloc_tpa_info(bp);
2876 static void bnxt_free_tx_rings(struct bnxt *bp)
2879 struct pci_dev *pdev = bp->pdev;
2884 for (i = 0; i < bp->tx_nr_rings; i++) {
2885 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2886 struct bnxt_ring_struct *ring;
2889 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2890 txr->tx_push, txr->tx_push_mapping);
2891 txr->tx_push = NULL;
2894 ring = &txr->tx_ring_struct;
2896 bnxt_free_ring(bp, &ring->ring_mem);
2900 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2903 struct pci_dev *pdev = bp->pdev;
2905 bp->tx_push_size = 0;
2906 if (bp->tx_push_thresh) {
2909 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2910 bp->tx_push_thresh);
2912 if (push_size > 256) {
2914 bp->tx_push_thresh = 0;
2917 bp->tx_push_size = push_size;
2920 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2921 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2922 struct bnxt_ring_struct *ring;
2925 ring = &txr->tx_ring_struct;
2927 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2931 ring->grp_idx = txr->bnapi->index;
2932 if (bp->tx_push_size) {
2935 /* One pre-allocated DMA buffer to backup
2938 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2940 &txr->tx_push_mapping,
2946 mapping = txr->tx_push_mapping +
2947 sizeof(struct tx_push_bd);
2948 txr->data_mapping = cpu_to_le64(mapping);
2950 qidx = bp->tc_to_qidx[j];
2951 ring->queue_id = bp->q_info[qidx].queue_id;
2952 if (i < bp->tx_nr_rings_xdp)
2954 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2960 static void bnxt_free_cp_rings(struct bnxt *bp)
2967 for (i = 0; i < bp->cp_nr_rings; i++) {
2968 struct bnxt_napi *bnapi = bp->bnapi[i];
2969 struct bnxt_cp_ring_info *cpr;
2970 struct bnxt_ring_struct *ring;
2976 cpr = &bnapi->cp_ring;
2977 ring = &cpr->cp_ring_struct;
2979 bnxt_free_ring(bp, &ring->ring_mem);
2981 for (j = 0; j < 2; j++) {
2982 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2985 ring = &cpr2->cp_ring_struct;
2986 bnxt_free_ring(bp, &ring->ring_mem);
2988 cpr->cp_ring_arr[j] = NULL;
2994 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
2996 struct bnxt_ring_mem_info *rmem;
2997 struct bnxt_ring_struct *ring;
2998 struct bnxt_cp_ring_info *cpr;
3001 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3005 ring = &cpr->cp_ring_struct;
3006 rmem = &ring->ring_mem;
3007 rmem->nr_pages = bp->cp_nr_pages;
3008 rmem->page_size = HW_CMPD_RING_SIZE;
3009 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3010 rmem->dma_arr = cpr->cp_desc_mapping;
3011 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3012 rc = bnxt_alloc_ring(bp, rmem);
3014 bnxt_free_ring(bp, rmem);
3021 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3023 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3024 int i, rc, ulp_base_vec, ulp_msix;
3026 ulp_msix = bnxt_get_ulp_msix_num(bp);
3027 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3028 for (i = 0; i < bp->cp_nr_rings; i++) {
3029 struct bnxt_napi *bnapi = bp->bnapi[i];
3030 struct bnxt_cp_ring_info *cpr;
3031 struct bnxt_ring_struct *ring;
3036 cpr = &bnapi->cp_ring;
3038 ring = &cpr->cp_ring_struct;
3040 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3044 if (ulp_msix && i >= ulp_base_vec)
3045 ring->map_idx = i + ulp_msix;
3049 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3052 if (i < bp->rx_nr_rings) {
3053 struct bnxt_cp_ring_info *cpr2 =
3054 bnxt_alloc_cp_sub_ring(bp);
3056 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3059 cpr2->bnapi = bnapi;
3061 if ((sh && i < bp->tx_nr_rings) ||
3062 (!sh && i >= bp->rx_nr_rings)) {
3063 struct bnxt_cp_ring_info *cpr2 =
3064 bnxt_alloc_cp_sub_ring(bp);
3066 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3069 cpr2->bnapi = bnapi;
3075 static void bnxt_init_ring_struct(struct bnxt *bp)
3079 for (i = 0; i < bp->cp_nr_rings; i++) {
3080 struct bnxt_napi *bnapi = bp->bnapi[i];
3081 struct bnxt_ring_mem_info *rmem;
3082 struct bnxt_cp_ring_info *cpr;
3083 struct bnxt_rx_ring_info *rxr;
3084 struct bnxt_tx_ring_info *txr;
3085 struct bnxt_ring_struct *ring;
3090 cpr = &bnapi->cp_ring;
3091 ring = &cpr->cp_ring_struct;
3092 rmem = &ring->ring_mem;
3093 rmem->nr_pages = bp->cp_nr_pages;
3094 rmem->page_size = HW_CMPD_RING_SIZE;
3095 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3096 rmem->dma_arr = cpr->cp_desc_mapping;
3097 rmem->vmem_size = 0;
3099 rxr = bnapi->rx_ring;
3103 ring = &rxr->rx_ring_struct;
3104 rmem = &ring->ring_mem;
3105 rmem->nr_pages = bp->rx_nr_pages;
3106 rmem->page_size = HW_RXBD_RING_SIZE;
3107 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3108 rmem->dma_arr = rxr->rx_desc_mapping;
3109 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3110 rmem->vmem = (void **)&rxr->rx_buf_ring;
3112 ring = &rxr->rx_agg_ring_struct;
3113 rmem = &ring->ring_mem;
3114 rmem->nr_pages = bp->rx_agg_nr_pages;
3115 rmem->page_size = HW_RXBD_RING_SIZE;
3116 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3117 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3118 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3119 rmem->vmem = (void **)&rxr->rx_agg_ring;
3122 txr = bnapi->tx_ring;
3126 ring = &txr->tx_ring_struct;
3127 rmem = &ring->ring_mem;
3128 rmem->nr_pages = bp->tx_nr_pages;
3129 rmem->page_size = HW_RXBD_RING_SIZE;
3130 rmem->pg_arr = (void **)txr->tx_desc_ring;
3131 rmem->dma_arr = txr->tx_desc_mapping;
3132 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3133 rmem->vmem = (void **)&txr->tx_buf_ring;
3137 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3141 struct rx_bd **rx_buf_ring;
3143 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3144 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3148 rxbd = rx_buf_ring[i];
3152 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3153 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3154 rxbd->rx_bd_opaque = prod;
3159 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3161 struct net_device *dev = bp->dev;
3162 struct bnxt_rx_ring_info *rxr;
3163 struct bnxt_ring_struct *ring;
3167 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3168 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3170 if (NET_IP_ALIGN == 2)
3171 type |= RX_BD_FLAGS_SOP;
3173 rxr = &bp->rx_ring[ring_nr];
3174 ring = &rxr->rx_ring_struct;
3175 bnxt_init_rxbd_pages(ring, type);
3177 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3178 bpf_prog_add(bp->xdp_prog, 1);
3179 rxr->xdp_prog = bp->xdp_prog;
3181 prod = rxr->rx_prod;
3182 for (i = 0; i < bp->rx_ring_size; i++) {
3183 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
3184 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3185 ring_nr, i, bp->rx_ring_size);
3188 prod = NEXT_RX(prod);
3190 rxr->rx_prod = prod;
3191 ring->fw_ring_id = INVALID_HW_RING_ID;
3193 ring = &rxr->rx_agg_ring_struct;
3194 ring->fw_ring_id = INVALID_HW_RING_ID;
3196 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3199 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3200 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3202 bnxt_init_rxbd_pages(ring, type);
3204 prod = rxr->rx_agg_prod;
3205 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3206 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
3207 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3208 ring_nr, i, bp->rx_ring_size);
3211 prod = NEXT_RX_AGG(prod);
3213 rxr->rx_agg_prod = prod;
3215 if (bp->flags & BNXT_FLAG_TPA) {
3220 for (i = 0; i < bp->max_tpa; i++) {
3221 data = __bnxt_alloc_rx_data(bp, &mapping,
3226 rxr->rx_tpa[i].data = data;
3227 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3228 rxr->rx_tpa[i].mapping = mapping;
3231 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
3239 static void bnxt_init_cp_rings(struct bnxt *bp)
3243 for (i = 0; i < bp->cp_nr_rings; i++) {
3244 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3245 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3247 ring->fw_ring_id = INVALID_HW_RING_ID;
3248 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3249 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3250 for (j = 0; j < 2; j++) {
3251 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3256 ring = &cpr2->cp_ring_struct;
3257 ring->fw_ring_id = INVALID_HW_RING_ID;
3258 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3259 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3264 static int bnxt_init_rx_rings(struct bnxt *bp)
3268 if (BNXT_RX_PAGE_MODE(bp)) {
3269 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3270 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3272 bp->rx_offset = BNXT_RX_OFFSET;
3273 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3276 for (i = 0; i < bp->rx_nr_rings; i++) {
3277 rc = bnxt_init_one_rx_ring(bp, i);
3285 static int bnxt_init_tx_rings(struct bnxt *bp)
3289 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3292 for (i = 0; i < bp->tx_nr_rings; i++) {
3293 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3294 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3296 ring->fw_ring_id = INVALID_HW_RING_ID;
3302 static void bnxt_free_ring_grps(struct bnxt *bp)
3304 kfree(bp->grp_info);
3305 bp->grp_info = NULL;
3308 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3313 bp->grp_info = kcalloc(bp->cp_nr_rings,
3314 sizeof(struct bnxt_ring_grp_info),
3319 for (i = 0; i < bp->cp_nr_rings; i++) {
3321 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3322 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3323 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3324 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3325 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3330 static void bnxt_free_vnics(struct bnxt *bp)
3332 kfree(bp->vnic_info);
3333 bp->vnic_info = NULL;
3337 static int bnxt_alloc_vnics(struct bnxt *bp)
3341 #ifdef CONFIG_RFS_ACCEL
3342 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3343 num_vnics += bp->rx_nr_rings;
3346 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3349 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3354 bp->nr_vnics = num_vnics;
3358 static void bnxt_init_vnics(struct bnxt *bp)
3362 for (i = 0; i < bp->nr_vnics; i++) {
3363 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3366 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3367 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3368 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3370 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3372 if (bp->vnic_info[i].rss_hash_key) {
3374 prandom_bytes(vnic->rss_hash_key,
3377 memcpy(vnic->rss_hash_key,
3378 bp->vnic_info[0].rss_hash_key,
3384 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3388 pages = ring_size / desc_per_pg;
3395 while (pages & (pages - 1))
3401 void bnxt_set_tpa_flags(struct bnxt *bp)
3403 bp->flags &= ~BNXT_FLAG_TPA;
3404 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3406 if (bp->dev->features & NETIF_F_LRO)
3407 bp->flags |= BNXT_FLAG_LRO;
3408 else if (bp->dev->features & NETIF_F_GRO_HW)
3409 bp->flags |= BNXT_FLAG_GRO;
3412 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3415 void bnxt_set_ring_params(struct bnxt *bp)
3417 u32 ring_size, rx_size, rx_space;
3418 u32 agg_factor = 0, agg_ring_size = 0;
3420 /* 8 for CRC and VLAN */
3421 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3423 rx_space = rx_size + NET_SKB_PAD +
3424 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3426 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3427 ring_size = bp->rx_ring_size;
3428 bp->rx_agg_ring_size = 0;
3429 bp->rx_agg_nr_pages = 0;
3431 if (bp->flags & BNXT_FLAG_TPA)
3432 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3434 bp->flags &= ~BNXT_FLAG_JUMBO;
3435 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3438 bp->flags |= BNXT_FLAG_JUMBO;
3439 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3440 if (jumbo_factor > agg_factor)
3441 agg_factor = jumbo_factor;
3443 agg_ring_size = ring_size * agg_factor;
3445 if (agg_ring_size) {
3446 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3448 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3449 u32 tmp = agg_ring_size;
3451 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3452 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3453 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3454 tmp, agg_ring_size);
3456 bp->rx_agg_ring_size = agg_ring_size;
3457 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3458 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3459 rx_space = rx_size + NET_SKB_PAD +
3460 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3463 bp->rx_buf_use_size = rx_size;
3464 bp->rx_buf_size = rx_space;
3466 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3467 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3469 ring_size = bp->tx_ring_size;
3470 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3471 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3473 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
3474 bp->cp_ring_size = ring_size;
3476 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3477 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3478 bp->cp_nr_pages = MAX_CP_PAGES;
3479 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3480 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3481 ring_size, bp->cp_ring_size);
3483 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3484 bp->cp_ring_mask = bp->cp_bit - 1;
3487 /* Changing allocation mode of RX rings.
3488 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3490 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3493 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3496 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3497 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3498 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3499 bp->rx_dir = DMA_BIDIRECTIONAL;
3500 bp->rx_skb_func = bnxt_rx_page_skb;
3501 /* Disable LRO or GRO_HW */
3502 netdev_update_features(bp->dev);
3504 bp->dev->max_mtu = bp->max_mtu;
3505 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3506 bp->rx_dir = DMA_FROM_DEVICE;
3507 bp->rx_skb_func = bnxt_rx_skb;
3512 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3515 struct bnxt_vnic_info *vnic;
3516 struct pci_dev *pdev = bp->pdev;
3521 for (i = 0; i < bp->nr_vnics; i++) {
3522 vnic = &bp->vnic_info[i];
3524 kfree(vnic->fw_grp_ids);
3525 vnic->fw_grp_ids = NULL;
3527 kfree(vnic->uc_list);
3528 vnic->uc_list = NULL;
3530 if (vnic->mc_list) {
3531 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3532 vnic->mc_list, vnic->mc_list_mapping);
3533 vnic->mc_list = NULL;
3536 if (vnic->rss_table) {
3537 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3539 vnic->rss_table_dma_addr);
3540 vnic->rss_table = NULL;
3543 vnic->rss_hash_key = NULL;
3548 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3550 int i, rc = 0, size;
3551 struct bnxt_vnic_info *vnic;
3552 struct pci_dev *pdev = bp->pdev;
3555 for (i = 0; i < bp->nr_vnics; i++) {
3556 vnic = &bp->vnic_info[i];
3558 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3559 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3562 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3563 if (!vnic->uc_list) {
3570 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3571 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3573 dma_alloc_coherent(&pdev->dev,
3575 &vnic->mc_list_mapping,
3577 if (!vnic->mc_list) {
3583 if (bp->flags & BNXT_FLAG_CHIP_P5)
3584 goto vnic_skip_grps;
3586 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3587 max_rings = bp->rx_nr_rings;
3591 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3592 if (!vnic->fw_grp_ids) {
3597 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3598 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3601 /* Allocate rss table and hash key */
3602 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3603 &vnic->rss_table_dma_addr,
3605 if (!vnic->rss_table) {
3610 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3612 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3613 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3621 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3623 struct pci_dev *pdev = bp->pdev;
3625 if (bp->hwrm_cmd_resp_addr) {
3626 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3627 bp->hwrm_cmd_resp_dma_addr);
3628 bp->hwrm_cmd_resp_addr = NULL;
3631 if (bp->hwrm_cmd_kong_resp_addr) {
3632 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3633 bp->hwrm_cmd_kong_resp_addr,
3634 bp->hwrm_cmd_kong_resp_dma_addr);
3635 bp->hwrm_cmd_kong_resp_addr = NULL;
3639 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3641 struct pci_dev *pdev = bp->pdev;
3643 if (bp->hwrm_cmd_kong_resp_addr)
3646 bp->hwrm_cmd_kong_resp_addr =
3647 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3648 &bp->hwrm_cmd_kong_resp_dma_addr,
3650 if (!bp->hwrm_cmd_kong_resp_addr)
3656 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3658 struct pci_dev *pdev = bp->pdev;
3660 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3661 &bp->hwrm_cmd_resp_dma_addr,
3663 if (!bp->hwrm_cmd_resp_addr)
3669 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3671 if (bp->hwrm_short_cmd_req_addr) {
3672 struct pci_dev *pdev = bp->pdev;
3674 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3675 bp->hwrm_short_cmd_req_addr,
3676 bp->hwrm_short_cmd_req_dma_addr);
3677 bp->hwrm_short_cmd_req_addr = NULL;
3681 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3683 struct pci_dev *pdev = bp->pdev;
3685 if (bp->hwrm_short_cmd_req_addr)
3688 bp->hwrm_short_cmd_req_addr =
3689 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3690 &bp->hwrm_short_cmd_req_dma_addr,
3692 if (!bp->hwrm_short_cmd_req_addr)
3698 static void bnxt_free_port_stats(struct bnxt *bp)
3700 struct pci_dev *pdev = bp->pdev;
3702 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3703 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3705 if (bp->hw_rx_port_stats) {
3706 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3707 bp->hw_rx_port_stats,
3708 bp->hw_rx_port_stats_map);
3709 bp->hw_rx_port_stats = NULL;
3712 if (bp->hw_tx_port_stats_ext) {
3713 dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
3714 bp->hw_tx_port_stats_ext,
3715 bp->hw_tx_port_stats_ext_map);
3716 bp->hw_tx_port_stats_ext = NULL;
3719 if (bp->hw_rx_port_stats_ext) {
3720 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3721 bp->hw_rx_port_stats_ext,
3722 bp->hw_rx_port_stats_ext_map);
3723 bp->hw_rx_port_stats_ext = NULL;
3726 if (bp->hw_pcie_stats) {
3727 dma_free_coherent(&pdev->dev, sizeof(struct pcie_ctx_hw_stats),
3728 bp->hw_pcie_stats, bp->hw_pcie_stats_map);
3729 bp->hw_pcie_stats = NULL;
3733 static void bnxt_free_ring_stats(struct bnxt *bp)
3735 struct pci_dev *pdev = bp->pdev;
3741 size = bp->hw_ring_stats_size;
3743 for (i = 0; i < bp->cp_nr_rings; i++) {
3744 struct bnxt_napi *bnapi = bp->bnapi[i];
3745 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3747 if (cpr->hw_stats) {
3748 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3750 cpr->hw_stats = NULL;
3755 static int bnxt_alloc_stats(struct bnxt *bp)
3758 struct pci_dev *pdev = bp->pdev;
3760 size = bp->hw_ring_stats_size;
3762 for (i = 0; i < bp->cp_nr_rings; i++) {
3763 struct bnxt_napi *bnapi = bp->bnapi[i];
3764 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3766 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3772 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3775 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
3778 if (bp->hw_rx_port_stats)
3779 goto alloc_ext_stats;
3781 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3782 sizeof(struct tx_port_stats) + 1024;
3784 bp->hw_rx_port_stats =
3785 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3786 &bp->hw_rx_port_stats_map,
3788 if (!bp->hw_rx_port_stats)
3791 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) + 512;
3792 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3793 sizeof(struct rx_port_stats) + 512;
3794 bp->flags |= BNXT_FLAG_PORT_STATS;
3797 /* Display extended statistics only if FW supports it */
3798 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
3799 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
3802 if (bp->hw_rx_port_stats_ext)
3803 goto alloc_tx_ext_stats;
3805 bp->hw_rx_port_stats_ext =
3806 dma_alloc_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3807 &bp->hw_rx_port_stats_ext_map, GFP_KERNEL);
3808 if (!bp->hw_rx_port_stats_ext)
3812 if (bp->hw_tx_port_stats_ext)
3813 goto alloc_pcie_stats;
3815 if (bp->hwrm_spec_code >= 0x10902 ||
3816 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
3817 bp->hw_tx_port_stats_ext =
3818 dma_alloc_coherent(&pdev->dev,
3819 sizeof(struct tx_port_stats_ext),
3820 &bp->hw_tx_port_stats_ext_map,
3823 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3826 if (bp->hw_pcie_stats ||
3827 !(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
3831 dma_alloc_coherent(&pdev->dev, sizeof(struct pcie_ctx_hw_stats),
3832 &bp->hw_pcie_stats_map, GFP_KERNEL);
3833 if (!bp->hw_pcie_stats)
3836 bp->flags |= BNXT_FLAG_PCIE_STATS;
3840 static void bnxt_clear_ring_indices(struct bnxt *bp)
3847 for (i = 0; i < bp->cp_nr_rings; i++) {
3848 struct bnxt_napi *bnapi = bp->bnapi[i];
3849 struct bnxt_cp_ring_info *cpr;
3850 struct bnxt_rx_ring_info *rxr;
3851 struct bnxt_tx_ring_info *txr;
3856 cpr = &bnapi->cp_ring;
3857 cpr->cp_raw_cons = 0;
3859 txr = bnapi->tx_ring;
3865 rxr = bnapi->rx_ring;
3868 rxr->rx_agg_prod = 0;
3869 rxr->rx_sw_agg_prod = 0;
3870 rxr->rx_next_cons = 0;
3875 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3877 #ifdef CONFIG_RFS_ACCEL
3880 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3881 * safe to delete the hash table.
3883 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3884 struct hlist_head *head;
3885 struct hlist_node *tmp;
3886 struct bnxt_ntuple_filter *fltr;
3888 head = &bp->ntp_fltr_hash_tbl[i];
3889 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3890 hlist_del(&fltr->hash);
3895 kfree(bp->ntp_fltr_bmap);
3896 bp->ntp_fltr_bmap = NULL;
3898 bp->ntp_fltr_count = 0;
3902 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3904 #ifdef CONFIG_RFS_ACCEL
3907 if (!(bp->flags & BNXT_FLAG_RFS))
3910 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3911 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3913 bp->ntp_fltr_count = 0;
3914 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3918 if (!bp->ntp_fltr_bmap)
3927 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3929 bnxt_free_vnic_attributes(bp);
3930 bnxt_free_tx_rings(bp);
3931 bnxt_free_rx_rings(bp);
3932 bnxt_free_cp_rings(bp);
3933 bnxt_free_ntp_fltrs(bp, irq_re_init);
3935 bnxt_free_ring_stats(bp);
3936 bnxt_free_ring_grps(bp);
3937 bnxt_free_vnics(bp);
3938 kfree(bp->tx_ring_map);
3939 bp->tx_ring_map = NULL;
3947 bnxt_clear_ring_indices(bp);
3951 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3953 int i, j, rc, size, arr_size;
3957 /* Allocate bnapi mem pointer array and mem block for
3960 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3962 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3963 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3969 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3970 bp->bnapi[i] = bnapi;
3971 bp->bnapi[i]->index = i;
3972 bp->bnapi[i]->bp = bp;
3973 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3974 struct bnxt_cp_ring_info *cpr =
3975 &bp->bnapi[i]->cp_ring;
3977 cpr->cp_ring_struct.ring_mem.flags =
3978 BNXT_RMEM_RING_PTE_FLAG;
3982 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3983 sizeof(struct bnxt_rx_ring_info),
3988 for (i = 0; i < bp->rx_nr_rings; i++) {
3989 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3991 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3992 rxr->rx_ring_struct.ring_mem.flags =
3993 BNXT_RMEM_RING_PTE_FLAG;
3994 rxr->rx_agg_ring_struct.ring_mem.flags =
3995 BNXT_RMEM_RING_PTE_FLAG;
3997 rxr->bnapi = bp->bnapi[i];
3998 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4001 bp->tx_ring = kcalloc(bp->tx_nr_rings,
4002 sizeof(struct bnxt_tx_ring_info),
4007 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4010 if (!bp->tx_ring_map)
4013 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4016 j = bp->rx_nr_rings;
4018 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4019 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4021 if (bp->flags & BNXT_FLAG_CHIP_P5)
4022 txr->tx_ring_struct.ring_mem.flags =
4023 BNXT_RMEM_RING_PTE_FLAG;
4024 txr->bnapi = bp->bnapi[j];
4025 bp->bnapi[j]->tx_ring = txr;
4026 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4027 if (i >= bp->tx_nr_rings_xdp) {
4028 txr->txq_index = i - bp->tx_nr_rings_xdp;
4029 bp->bnapi[j]->tx_int = bnxt_tx_int;
4031 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4032 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4036 rc = bnxt_alloc_stats(bp);
4040 rc = bnxt_alloc_ntp_fltrs(bp);
4044 rc = bnxt_alloc_vnics(bp);
4049 bnxt_init_ring_struct(bp);
4051 rc = bnxt_alloc_rx_rings(bp);
4055 rc = bnxt_alloc_tx_rings(bp);
4059 rc = bnxt_alloc_cp_rings(bp);
4063 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4064 BNXT_VNIC_UCAST_FLAG;
4065 rc = bnxt_alloc_vnic_attributes(bp);
4071 bnxt_free_mem(bp, true);
4075 static void bnxt_disable_int(struct bnxt *bp)
4082 for (i = 0; i < bp->cp_nr_rings; i++) {
4083 struct bnxt_napi *bnapi = bp->bnapi[i];
4084 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4085 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4087 if (ring->fw_ring_id != INVALID_HW_RING_ID)
4088 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4092 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4094 struct bnxt_napi *bnapi = bp->bnapi[n];
4095 struct bnxt_cp_ring_info *cpr;
4097 cpr = &bnapi->cp_ring;
4098 return cpr->cp_ring_struct.map_idx;
4101 static void bnxt_disable_int_sync(struct bnxt *bp)
4105 atomic_inc(&bp->intr_sem);
4107 bnxt_disable_int(bp);
4108 for (i = 0; i < bp->cp_nr_rings; i++) {
4109 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4111 synchronize_irq(bp->irq_tbl[map_idx].vector);
4115 static void bnxt_enable_int(struct bnxt *bp)
4119 atomic_set(&bp->intr_sem, 0);
4120 for (i = 0; i < bp->cp_nr_rings; i++) {
4121 struct bnxt_napi *bnapi = bp->bnapi[i];
4122 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4124 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4128 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4129 u16 cmpl_ring, u16 target_id)
4131 struct input *req = request;
4133 req->req_type = cpu_to_le16(req_type);
4134 req->cmpl_ring = cpu_to_le16(cmpl_ring);
4135 req->target_id = cpu_to_le16(target_id);
4136 if (bnxt_kong_hwrm_message(bp, req))
4137 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4139 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4142 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4145 case HWRM_ERR_CODE_SUCCESS:
4147 case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4149 case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4151 case HWRM_ERR_CODE_INVALID_PARAMS:
4152 case HWRM_ERR_CODE_INVALID_FLAGS:
4153 case HWRM_ERR_CODE_INVALID_ENABLES:
4154 case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4155 case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4157 case HWRM_ERR_CODE_NO_BUFFER:
4159 case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4161 case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4168 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4169 int timeout, bool silent)
4171 int i, intr_process, rc, tmo_count;
4172 struct input *req = msg;
4176 u16 cp_ring_id, len = 0;
4177 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4178 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4179 struct hwrm_short_input short_input = {0};
4180 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4181 u8 *resp_addr = (u8 *)bp->hwrm_cmd_resp_addr;
4182 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4183 u16 dst = BNXT_HWRM_CHNL_CHIMP;
4185 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4188 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4189 if (msg_len > bp->hwrm_max_ext_req_len ||
4190 !bp->hwrm_short_cmd_req_addr)
4194 if (bnxt_hwrm_kong_chnl(bp, req)) {
4195 dst = BNXT_HWRM_CHNL_KONG;
4196 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4197 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4198 resp = bp->hwrm_cmd_kong_resp_addr;
4199 resp_addr = (u8 *)bp->hwrm_cmd_kong_resp_addr;
4202 memset(resp, 0, PAGE_SIZE);
4203 cp_ring_id = le16_to_cpu(req->cmpl_ring);
4204 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4206 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4207 /* currently supports only one outstanding message */
4209 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4211 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4212 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4213 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4216 /* Set boundary for maximum extended request length for short
4217 * cmd format. If passed up from device use the max supported
4218 * internal req length.
4220 max_msg_len = bp->hwrm_max_ext_req_len;
4222 memcpy(short_cmd_req, req, msg_len);
4223 if (msg_len < max_msg_len)
4224 memset(short_cmd_req + msg_len, 0,
4225 max_msg_len - msg_len);
4227 short_input.req_type = req->req_type;
4228 short_input.signature =
4229 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4230 short_input.size = cpu_to_le16(msg_len);
4231 short_input.req_addr =
4232 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4234 data = (u32 *)&short_input;
4235 msg_len = sizeof(short_input);
4237 /* Sync memory write before updating doorbell */
4240 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4243 /* Write request msg to hwrm channel */
4244 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4246 for (i = msg_len; i < max_req_len; i += 4)
4247 writel(0, bp->bar0 + bar_offset + i);
4249 /* Ring channel doorbell */
4250 writel(1, bp->bar0 + doorbell_offset);
4252 if (!pci_is_enabled(bp->pdev))
4256 timeout = DFLT_HWRM_CMD_TIMEOUT;
4257 /* convert timeout to usec */
4261 /* Short timeout for the first few iterations:
4262 * number of loops = number of loops for short timeout +
4263 * number of loops for standard timeout.
4265 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4266 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4267 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4268 resp_len = (__le32 *)(resp_addr + HWRM_RESP_LEN_OFFSET);
4271 u16 seq_id = bp->hwrm_intr_seq_id;
4273 /* Wait until hwrm response cmpl interrupt is processed */
4274 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4276 /* Abort the wait for completion if the FW health
4279 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4281 /* on first few passes, just barely sleep */
4282 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4283 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4284 HWRM_SHORT_MAX_TIMEOUT);
4286 usleep_range(HWRM_MIN_TIMEOUT,
4290 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4292 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4293 le16_to_cpu(req->req_type));
4296 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
4298 valid = resp_addr + len - 1;
4302 /* Check if response len is updated */
4303 for (i = 0; i < tmo_count; i++) {
4304 /* Abort the wait for completion if the FW health
4307 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4309 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
4313 /* on first few passes, just barely sleep */
4314 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4315 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4316 HWRM_SHORT_MAX_TIMEOUT);
4318 usleep_range(HWRM_MIN_TIMEOUT,
4322 if (i >= tmo_count) {
4324 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4325 HWRM_TOTAL_TIMEOUT(i),
4326 le16_to_cpu(req->req_type),
4327 le16_to_cpu(req->seq_id), len);
4331 /* Last byte of resp contains valid bit */
4332 valid = resp_addr + len - 1;
4333 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4334 /* make sure we read from updated DMA memory */
4341 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4343 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4344 HWRM_TOTAL_TIMEOUT(i),
4345 le16_to_cpu(req->req_type),
4346 le16_to_cpu(req->seq_id), len,
4352 /* Zero valid bit for compatibility. Valid bit in an older spec
4353 * may become a new field in a newer spec. We must make sure that
4354 * a new field not implemented by old spec will read zero.
4357 rc = le16_to_cpu(resp->error_code);
4359 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4360 le16_to_cpu(resp->req_type),
4361 le16_to_cpu(resp->seq_id), rc);
4362 return bnxt_hwrm_to_stderr(rc);
4365 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4367 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4370 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4373 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4376 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4380 mutex_lock(&bp->hwrm_cmd_lock);
4381 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4382 mutex_unlock(&bp->hwrm_cmd_lock);
4386 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4391 mutex_lock(&bp->hwrm_cmd_lock);
4392 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4393 mutex_unlock(&bp->hwrm_cmd_lock);
4397 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
4400 struct hwrm_func_drv_rgtr_input req = {0};
4401 DECLARE_BITMAP(async_events_bmap, 256);
4402 u32 *events = (u32 *)async_events_bmap;
4405 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4408 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4410 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4411 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4412 u16 event_id = bnxt_async_events_arr[i];
4414 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4415 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4417 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4419 if (bmap && bmap_size) {
4420 for (i = 0; i < bmap_size; i++) {
4421 if (test_bit(i, bmap))
4422 __set_bit(i, async_events_bmap);
4426 for (i = 0; i < 8; i++)
4427 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4429 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4432 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
4434 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4435 struct hwrm_func_drv_rgtr_input req = {0};
4439 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4442 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4443 FUNC_DRV_RGTR_REQ_ENABLES_VER);
4445 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4446 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE |
4447 FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4448 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4449 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4450 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4451 req.flags = cpu_to_le32(flags);
4452 req.ver_maj_8b = DRV_VER_MAJ;
4453 req.ver_min_8b = DRV_VER_MIN;
4454 req.ver_upd_8b = DRV_VER_UPD;
4455 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4456 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4457 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4463 memset(data, 0, sizeof(data));
4464 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4465 u16 cmd = bnxt_vf_req_snif[i];
4466 unsigned int bit, idx;
4470 data[idx] |= 1 << bit;
4473 for (i = 0; i < 8; i++)
4474 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4477 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4480 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4481 req.flags |= cpu_to_le32(
4482 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4484 mutex_lock(&bp->hwrm_cmd_lock);
4485 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4486 if (!rc && (resp->flags &
4487 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)))
4488 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4489 mutex_unlock(&bp->hwrm_cmd_lock);
4493 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4495 struct hwrm_func_drv_unrgtr_input req = {0};
4497 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4498 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4501 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4504 struct hwrm_tunnel_dst_port_free_input req = {0};
4506 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4507 req.tunnel_type = tunnel_type;
4509 switch (tunnel_type) {
4510 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4511 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
4513 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4514 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
4520 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4522 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4527 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4531 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4532 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4534 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4536 req.tunnel_type = tunnel_type;
4537 req.tunnel_dst_port_val = port;
4539 mutex_lock(&bp->hwrm_cmd_lock);
4540 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4542 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4547 switch (tunnel_type) {
4548 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4549 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
4551 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4552 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
4559 mutex_unlock(&bp->hwrm_cmd_lock);
4563 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4565 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4566 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4568 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4569 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4571 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4572 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4573 req.mask = cpu_to_le32(vnic->rx_mask);
4574 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4577 #ifdef CONFIG_RFS_ACCEL
4578 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4579 struct bnxt_ntuple_filter *fltr)
4581 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4583 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4584 req.ntuple_filter_id = fltr->filter_id;
4585 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4588 #define BNXT_NTP_FLTR_FLAGS \
4589 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4590 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4591 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4592 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4593 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4594 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4595 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4596 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4597 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4598 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4599 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4600 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4601 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4602 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4604 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4605 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4607 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4608 struct bnxt_ntuple_filter *fltr)
4610 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4611 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4612 struct flow_keys *keys = &fltr->fkeys;
4613 struct bnxt_vnic_info *vnic;
4617 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4618 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4620 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4621 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4622 req.dst_id = cpu_to_le16(fltr->rxq);
4624 vnic = &bp->vnic_info[fltr->rxq + 1];
4625 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4627 req.flags = cpu_to_le32(flags);
4628 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4630 req.ethertype = htons(ETH_P_IP);
4631 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4632 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4633 req.ip_protocol = keys->basic.ip_proto;
4635 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4638 req.ethertype = htons(ETH_P_IPV6);
4640 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4641 *(struct in6_addr *)&req.src_ipaddr[0] =
4642 keys->addrs.v6addrs.src;
4643 *(struct in6_addr *)&req.dst_ipaddr[0] =
4644 keys->addrs.v6addrs.dst;
4645 for (i = 0; i < 4; i++) {
4646 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4647 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4650 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4651 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4652 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4653 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4655 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4656 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4658 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4661 req.src_port = keys->ports.src;
4662 req.src_port_mask = cpu_to_be16(0xffff);
4663 req.dst_port = keys->ports.dst;
4664 req.dst_port_mask = cpu_to_be16(0xffff);
4666 mutex_lock(&bp->hwrm_cmd_lock);
4667 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4669 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4670 fltr->filter_id = resp->ntuple_filter_id;
4672 mutex_unlock(&bp->hwrm_cmd_lock);
4677 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4681 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4682 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4684 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4685 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4686 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4688 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4689 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4691 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4692 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4693 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4694 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4695 req.l2_addr_mask[0] = 0xff;
4696 req.l2_addr_mask[1] = 0xff;
4697 req.l2_addr_mask[2] = 0xff;
4698 req.l2_addr_mask[3] = 0xff;
4699 req.l2_addr_mask[4] = 0xff;
4700 req.l2_addr_mask[5] = 0xff;
4702 mutex_lock(&bp->hwrm_cmd_lock);
4703 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4705 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4707 mutex_unlock(&bp->hwrm_cmd_lock);
4711 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4713 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4716 /* Any associated ntuple filters will also be cleared by firmware. */
4717 mutex_lock(&bp->hwrm_cmd_lock);
4718 for (i = 0; i < num_of_vnics; i++) {
4719 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4721 for (j = 0; j < vnic->uc_filter_count; j++) {
4722 struct hwrm_cfa_l2_filter_free_input req = {0};
4724 bnxt_hwrm_cmd_hdr_init(bp, &req,
4725 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4727 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4729 rc = _hwrm_send_message(bp, &req, sizeof(req),
4732 vnic->uc_filter_count = 0;
4734 mutex_unlock(&bp->hwrm_cmd_lock);
4739 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4741 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4742 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
4743 struct hwrm_vnic_tpa_cfg_input req = {0};
4745 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4748 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4751 u16 mss = bp->dev->mtu - 40;
4752 u32 nsegs, n, segs = 0, flags;
4754 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4755 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4756 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4757 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4758 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4759 if (tpa_flags & BNXT_FLAG_GRO)
4760 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4762 req.flags = cpu_to_le32(flags);
4765 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
4766 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4767 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
4769 /* Number of segs are log2 units, and first packet is not
4770 * included as part of this units.
4772 if (mss <= BNXT_RX_PAGE_SIZE) {
4773 n = BNXT_RX_PAGE_SIZE / mss;
4774 nsegs = (MAX_SKB_FRAGS - 1) * n;
4776 n = mss / BNXT_RX_PAGE_SIZE;
4777 if (mss & (BNXT_RX_PAGE_SIZE - 1))
4779 nsegs = (MAX_SKB_FRAGS - n) / n;
4782 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4783 segs = MAX_TPA_SEGS_P5;
4784 max_aggs = bp->max_tpa;
4786 segs = ilog2(nsegs);
4788 req.max_agg_segs = cpu_to_le16(segs);
4789 req.max_aggs = cpu_to_le16(max_aggs);
4791 req.min_agg_len = cpu_to_le32(512);
4793 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4795 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4798 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4800 struct bnxt_ring_grp_info *grp_info;
4802 grp_info = &bp->grp_info[ring->grp_idx];
4803 return grp_info->cp_fw_ring_id;
4806 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4808 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4809 struct bnxt_napi *bnapi = rxr->bnapi;
4810 struct bnxt_cp_ring_info *cpr;
4812 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4813 return cpr->cp_ring_struct.fw_ring_id;
4815 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4819 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4821 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4822 struct bnxt_napi *bnapi = txr->bnapi;
4823 struct bnxt_cp_ring_info *cpr;
4825 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4826 return cpr->cp_ring_struct.fw_ring_id;
4828 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4832 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
4834 u32 i, j, max_rings;
4835 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4836 struct hwrm_vnic_rss_cfg_input req = {0};
4838 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
4839 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
4842 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4844 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4845 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4846 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
4847 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4848 max_rings = bp->rx_nr_rings - 1;
4850 max_rings = bp->rx_nr_rings;
4855 /* Fill the RSS indirection table with ring group ids */
4856 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4859 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4862 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4863 req.hash_key_tbl_addr =
4864 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4866 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4867 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4870 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
4872 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4873 u32 i, j, k, nr_ctxs, max_rings = bp->rx_nr_rings;
4874 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4875 struct hwrm_vnic_rss_cfg_input req = {0};
4877 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4878 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4880 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4883 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4884 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4885 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4886 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
4887 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
4888 for (i = 0, k = 0; i < nr_ctxs; i++) {
4889 __le16 *ring_tbl = vnic->rss_table;
4892 req.ring_table_pair_index = i;
4893 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
4894 for (j = 0; j < 64; j++) {
4897 ring_id = rxr->rx_ring_struct.fw_ring_id;
4898 *ring_tbl++ = cpu_to_le16(ring_id);
4899 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
4900 *ring_tbl++ = cpu_to_le16(ring_id);
4903 if (k == max_rings) {
4905 rxr = &bp->rx_ring[0];
4908 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4915 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4917 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4918 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4920 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4921 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4922 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4923 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4925 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4926 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4927 /* thresholds not implemented in firmware yet */
4928 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4929 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4930 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4931 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4934 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4937 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4939 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4940 req.rss_cos_lb_ctx_id =
4941 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
4943 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4944 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
4947 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4951 for (i = 0; i < bp->nr_vnics; i++) {
4952 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4954 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4955 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4956 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4959 bp->rsscos_nr_ctxs = 0;
4962 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
4965 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4966 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4967 bp->hwrm_cmd_resp_addr;
4969 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4972 mutex_lock(&bp->hwrm_cmd_lock);
4973 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4975 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
4976 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4977 mutex_unlock(&bp->hwrm_cmd_lock);
4982 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4984 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4985 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4986 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4989 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
4991 unsigned int ring = 0, grp_idx;
4992 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4993 struct hwrm_vnic_cfg_input req = {0};
4996 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
4998 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4999 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5001 req.default_rx_ring_id =
5002 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5003 req.default_cmpl_ring_id =
5004 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5006 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5007 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5010 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5011 /* Only RSS support for now TBD: COS & LB */
5012 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5013 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5014 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5015 VNIC_CFG_REQ_ENABLES_MRU);
5016 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5018 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5019 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5020 VNIC_CFG_REQ_ENABLES_MRU);
5021 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5023 req.rss_rule = cpu_to_le16(0xffff);
5026 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5027 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5028 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5029 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5031 req.cos_rule = cpu_to_le16(0xffff);
5034 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5036 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5038 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5039 ring = bp->rx_nr_rings - 1;
5041 grp_idx = bp->rx_ring[ring].bnapi->index;
5042 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5043 req.lb_rule = cpu_to_le16(0xffff);
5045 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
5048 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5049 #ifdef CONFIG_BNXT_SRIOV
5051 def_vlan = bp->vf.vlan;
5053 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5054 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5055 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5056 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5058 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5061 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5065 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5066 struct hwrm_vnic_free_input req = {0};
5068 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5070 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5072 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5073 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5078 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5082 for (i = 0; i < bp->nr_vnics; i++)
5083 bnxt_hwrm_vnic_free_one(bp, i);
5086 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5087 unsigned int start_rx_ring_idx,
5088 unsigned int nr_rings)
5091 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5092 struct hwrm_vnic_alloc_input req = {0};
5093 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5094 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5096 if (bp->flags & BNXT_FLAG_CHIP_P5)
5097 goto vnic_no_ring_grps;
5099 /* map ring groups to this vnic */
5100 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5101 grp_idx = bp->rx_ring[i].bnapi->index;
5102 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5103 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5107 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5111 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5112 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5114 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5116 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5118 mutex_lock(&bp->hwrm_cmd_lock);
5119 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5121 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5122 mutex_unlock(&bp->hwrm_cmd_lock);
5126 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5128 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5129 struct hwrm_vnic_qcaps_input req = {0};
5132 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5133 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5134 if (bp->hwrm_spec_code < 0x10600)
5137 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5138 mutex_lock(&bp->hwrm_cmd_lock);
5139 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5141 u32 flags = le32_to_cpu(resp->flags);
5143 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5144 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5145 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5147 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5148 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5149 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5151 bp->hw_ring_stats_size =
5152 sizeof(struct ctx_hw_stats_ext);
5154 mutex_unlock(&bp->hwrm_cmd_lock);
5158 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5163 if (bp->flags & BNXT_FLAG_CHIP_P5)
5166 mutex_lock(&bp->hwrm_cmd_lock);
5167 for (i = 0; i < bp->rx_nr_rings; i++) {
5168 struct hwrm_ring_grp_alloc_input req = {0};
5169 struct hwrm_ring_grp_alloc_output *resp =
5170 bp->hwrm_cmd_resp_addr;
5171 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5173 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5175 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5176 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5177 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5178 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5180 rc = _hwrm_send_message(bp, &req, sizeof(req),
5185 bp->grp_info[grp_idx].fw_grp_id =
5186 le32_to_cpu(resp->ring_group_id);
5188 mutex_unlock(&bp->hwrm_cmd_lock);
5192 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5196 struct hwrm_ring_grp_free_input req = {0};
5198 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5201 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5203 mutex_lock(&bp->hwrm_cmd_lock);
5204 for (i = 0; i < bp->cp_nr_rings; i++) {
5205 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5208 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5210 rc = _hwrm_send_message(bp, &req, sizeof(req),
5212 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5214 mutex_unlock(&bp->hwrm_cmd_lock);
5218 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5219 struct bnxt_ring_struct *ring,
5220 u32 ring_type, u32 map_index)
5222 int rc = 0, err = 0;
5223 struct hwrm_ring_alloc_input req = {0};
5224 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5225 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5226 struct bnxt_ring_grp_info *grp_info;
5229 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5232 if (rmem->nr_pages > 1) {
5233 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5234 /* Page size is in log2 units */
5235 req.page_size = BNXT_PAGE_SHIFT;
5236 req.page_tbl_depth = 1;
5238 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5241 /* Association of ring index with doorbell index and MSIX number */
5242 req.logical_id = cpu_to_le16(map_index);
5244 switch (ring_type) {
5245 case HWRM_RING_ALLOC_TX: {
5246 struct bnxt_tx_ring_info *txr;
5248 txr = container_of(ring, struct bnxt_tx_ring_info,
5250 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5251 /* Association of transmit ring with completion ring */
5252 grp_info = &bp->grp_info[ring->grp_idx];
5253 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5254 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5255 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5256 req.queue_id = cpu_to_le16(ring->queue_id);
5259 case HWRM_RING_ALLOC_RX:
5260 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5261 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5262 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5265 /* Association of rx ring with stats context */
5266 grp_info = &bp->grp_info[ring->grp_idx];
5267 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5268 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5269 req.enables |= cpu_to_le32(
5270 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5271 if (NET_IP_ALIGN == 2)
5272 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5273 req.flags = cpu_to_le16(flags);
5276 case HWRM_RING_ALLOC_AGG:
5277 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5278 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5279 /* Association of agg ring with rx ring */
5280 grp_info = &bp->grp_info[ring->grp_idx];
5281 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5282 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5283 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5284 req.enables |= cpu_to_le32(
5285 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5286 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5288 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5290 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5292 case HWRM_RING_ALLOC_CMPL:
5293 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5294 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5295 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5296 /* Association of cp ring with nq */
5297 grp_info = &bp->grp_info[map_index];
5298 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5299 req.cq_handle = cpu_to_le64(ring->handle);
5300 req.enables |= cpu_to_le32(
5301 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5302 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5303 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5306 case HWRM_RING_ALLOC_NQ:
5307 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5308 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5309 if (bp->flags & BNXT_FLAG_USING_MSIX)
5310 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5313 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5318 mutex_lock(&bp->hwrm_cmd_lock);
5319 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5320 err = le16_to_cpu(resp->error_code);
5321 ring_id = le16_to_cpu(resp->ring_id);
5322 mutex_unlock(&bp->hwrm_cmd_lock);
5325 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5326 ring_type, rc, err);
5329 ring->fw_ring_id = ring_id;
5333 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5338 struct hwrm_func_cfg_input req = {0};
5340 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5341 req.fid = cpu_to_le16(0xffff);
5342 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5343 req.async_event_cr = cpu_to_le16(idx);
5344 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5346 struct hwrm_func_vf_cfg_input req = {0};
5348 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5350 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5351 req.async_event_cr = cpu_to_le16(idx);
5352 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5357 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5358 u32 map_idx, u32 xid)
5360 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5362 db->doorbell = bp->bar1 + 0x10000;
5364 db->doorbell = bp->bar1 + 0x4000;
5365 switch (ring_type) {
5366 case HWRM_RING_ALLOC_TX:
5367 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5369 case HWRM_RING_ALLOC_RX:
5370 case HWRM_RING_ALLOC_AGG:
5371 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5373 case HWRM_RING_ALLOC_CMPL:
5374 db->db_key64 = DBR_PATH_L2;
5376 case HWRM_RING_ALLOC_NQ:
5377 db->db_key64 = DBR_PATH_L2;
5380 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5382 db->doorbell = bp->bar1 + map_idx * 0x80;
5383 switch (ring_type) {
5384 case HWRM_RING_ALLOC_TX:
5385 db->db_key32 = DB_KEY_TX;
5387 case HWRM_RING_ALLOC_RX:
5388 case HWRM_RING_ALLOC_AGG:
5389 db->db_key32 = DB_KEY_RX;
5391 case HWRM_RING_ALLOC_CMPL:
5392 db->db_key32 = DB_KEY_CP;
5398 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5400 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5404 if (bp->flags & BNXT_FLAG_CHIP_P5)
5405 type = HWRM_RING_ALLOC_NQ;
5407 type = HWRM_RING_ALLOC_CMPL;
5408 for (i = 0; i < bp->cp_nr_rings; i++) {
5409 struct bnxt_napi *bnapi = bp->bnapi[i];
5410 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5411 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5412 u32 map_idx = ring->map_idx;
5413 unsigned int vector;
5415 vector = bp->irq_tbl[map_idx].vector;
5416 disable_irq_nosync(vector);
5417 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5422 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5423 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5425 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5428 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5430 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5434 type = HWRM_RING_ALLOC_TX;
5435 for (i = 0; i < bp->tx_nr_rings; i++) {
5436 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5437 struct bnxt_ring_struct *ring;
5440 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5441 struct bnxt_napi *bnapi = txr->bnapi;
5442 struct bnxt_cp_ring_info *cpr, *cpr2;
5443 u32 type2 = HWRM_RING_ALLOC_CMPL;
5445 cpr = &bnapi->cp_ring;
5446 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5447 ring = &cpr2->cp_ring_struct;
5448 ring->handle = BNXT_TX_HDL;
5449 map_idx = bnapi->index;
5450 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5453 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5455 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5457 ring = &txr->tx_ring_struct;
5459 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5462 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5465 type = HWRM_RING_ALLOC_RX;
5466 for (i = 0; i < bp->rx_nr_rings; i++) {
5467 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5468 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5469 struct bnxt_napi *bnapi = rxr->bnapi;
5470 u32 map_idx = bnapi->index;
5472 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5475 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5476 /* If we have agg rings, post agg buffers first. */
5478 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5479 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5480 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5481 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5482 u32 type2 = HWRM_RING_ALLOC_CMPL;
5483 struct bnxt_cp_ring_info *cpr2;
5485 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5486 ring = &cpr2->cp_ring_struct;
5487 ring->handle = BNXT_RX_HDL;
5488 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5491 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5493 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5498 type = HWRM_RING_ALLOC_AGG;
5499 for (i = 0; i < bp->rx_nr_rings; i++) {
5500 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5501 struct bnxt_ring_struct *ring =
5502 &rxr->rx_agg_ring_struct;
5503 u32 grp_idx = ring->grp_idx;
5504 u32 map_idx = grp_idx + bp->rx_nr_rings;
5506 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5510 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5512 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5513 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5514 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5521 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5522 struct bnxt_ring_struct *ring,
5523 u32 ring_type, int cmpl_ring_id)
5526 struct hwrm_ring_free_input req = {0};
5527 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5530 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
5533 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5534 req.ring_type = ring_type;
5535 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5537 mutex_lock(&bp->hwrm_cmd_lock);
5538 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5539 error_code = le16_to_cpu(resp->error_code);
5540 mutex_unlock(&bp->hwrm_cmd_lock);
5542 if (rc || error_code) {
5543 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5544 ring_type, rc, error_code);
5550 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5558 for (i = 0; i < bp->tx_nr_rings; i++) {
5559 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5560 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5562 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5563 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5565 hwrm_ring_free_send_msg(bp, ring,
5566 RING_FREE_REQ_RING_TYPE_TX,
5567 close_path ? cmpl_ring_id :
5568 INVALID_HW_RING_ID);
5569 ring->fw_ring_id = INVALID_HW_RING_ID;
5573 for (i = 0; i < bp->rx_nr_rings; i++) {
5574 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5575 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5576 u32 grp_idx = rxr->bnapi->index;
5578 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5579 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5581 hwrm_ring_free_send_msg(bp, ring,
5582 RING_FREE_REQ_RING_TYPE_RX,
5583 close_path ? cmpl_ring_id :
5584 INVALID_HW_RING_ID);
5585 ring->fw_ring_id = INVALID_HW_RING_ID;
5586 bp->grp_info[grp_idx].rx_fw_ring_id =
5591 if (bp->flags & BNXT_FLAG_CHIP_P5)
5592 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5594 type = RING_FREE_REQ_RING_TYPE_RX;
5595 for (i = 0; i < bp->rx_nr_rings; i++) {
5596 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5597 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5598 u32 grp_idx = rxr->bnapi->index;
5600 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5601 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5603 hwrm_ring_free_send_msg(bp, ring, type,
5604 close_path ? cmpl_ring_id :
5605 INVALID_HW_RING_ID);
5606 ring->fw_ring_id = INVALID_HW_RING_ID;
5607 bp->grp_info[grp_idx].agg_fw_ring_id =
5612 /* The completion rings are about to be freed. After that the
5613 * IRQ doorbell will not work anymore. So we need to disable
5616 bnxt_disable_int_sync(bp);
5618 if (bp->flags & BNXT_FLAG_CHIP_P5)
5619 type = RING_FREE_REQ_RING_TYPE_NQ;
5621 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5622 for (i = 0; i < bp->cp_nr_rings; i++) {
5623 struct bnxt_napi *bnapi = bp->bnapi[i];
5624 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5625 struct bnxt_ring_struct *ring;
5628 for (j = 0; j < 2; j++) {
5629 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5632 ring = &cpr2->cp_ring_struct;
5633 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5635 hwrm_ring_free_send_msg(bp, ring,
5636 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5637 INVALID_HW_RING_ID);
5638 ring->fw_ring_id = INVALID_HW_RING_ID;
5641 ring = &cpr->cp_ring_struct;
5642 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5643 hwrm_ring_free_send_msg(bp, ring, type,
5644 INVALID_HW_RING_ID);
5645 ring->fw_ring_id = INVALID_HW_RING_ID;
5646 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5651 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5654 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5656 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5657 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5658 struct hwrm_func_qcfg_input req = {0};
5661 if (bp->hwrm_spec_code < 0x10601)
5664 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5665 req.fid = cpu_to_le16(0xffff);
5666 mutex_lock(&bp->hwrm_cmd_lock);
5667 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5669 mutex_unlock(&bp->hwrm_cmd_lock);
5673 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5674 if (BNXT_NEW_RM(bp)) {
5677 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5678 hw_resc->resv_hw_ring_grps =
5679 le32_to_cpu(resp->alloc_hw_ring_grps);
5680 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5681 cp = le16_to_cpu(resp->alloc_cmpl_rings);
5682 stats = le16_to_cpu(resp->alloc_stat_ctx);
5683 hw_resc->resv_irqs = cp;
5684 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5685 int rx = hw_resc->resv_rx_rings;
5686 int tx = hw_resc->resv_tx_rings;
5688 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5690 if (cp < (rx + tx)) {
5691 bnxt_trim_rings(bp, &rx, &tx, cp, false);
5692 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5694 hw_resc->resv_rx_rings = rx;
5695 hw_resc->resv_tx_rings = tx;
5697 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
5698 hw_resc->resv_hw_ring_grps = rx;
5700 hw_resc->resv_cp_rings = cp;
5701 hw_resc->resv_stat_ctxs = stats;
5703 mutex_unlock(&bp->hwrm_cmd_lock);
5707 /* Caller must hold bp->hwrm_cmd_lock */
5708 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5710 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5711 struct hwrm_func_qcfg_input req = {0};
5714 if (bp->hwrm_spec_code < 0x10601)
5717 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5718 req.fid = cpu_to_le16(fid);
5719 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5721 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5726 static bool bnxt_rfs_supported(struct bnxt *bp);
5729 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5730 int tx_rings, int rx_rings, int ring_grps,
5731 int cp_rings, int stats, int vnics)
5735 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5736 req->fid = cpu_to_le16(0xffff);
5737 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5738 req->num_tx_rings = cpu_to_le16(tx_rings);
5739 if (BNXT_NEW_RM(bp)) {
5740 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
5741 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5742 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5743 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5744 enables |= tx_rings + ring_grps ?
5745 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5746 enables |= rx_rings ?
5747 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5749 enables |= cp_rings ?
5750 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5751 enables |= ring_grps ?
5752 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5753 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5755 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
5757 req->num_rx_rings = cpu_to_le16(rx_rings);
5758 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5759 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5760 req->num_msix = cpu_to_le16(cp_rings);
5761 req->num_rsscos_ctxs =
5762 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5764 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5765 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5766 req->num_rsscos_ctxs = cpu_to_le16(1);
5767 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5768 bnxt_rfs_supported(bp))
5769 req->num_rsscos_ctxs =
5770 cpu_to_le16(ring_grps + 1);
5772 req->num_stat_ctxs = cpu_to_le16(stats);
5773 req->num_vnics = cpu_to_le16(vnics);
5775 req->enables = cpu_to_le32(enables);
5779 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5780 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5781 int rx_rings, int ring_grps, int cp_rings,
5782 int stats, int vnics)
5786 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5787 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5788 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5789 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5790 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5791 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5792 enables |= tx_rings + ring_grps ?
5793 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5795 enables |= cp_rings ?
5796 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5797 enables |= ring_grps ?
5798 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5800 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
5801 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
5803 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
5804 req->num_tx_rings = cpu_to_le16(tx_rings);
5805 req->num_rx_rings = cpu_to_le16(rx_rings);
5806 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5807 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5808 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5810 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5811 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5812 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
5814 req->num_stat_ctxs = cpu_to_le16(stats);
5815 req->num_vnics = cpu_to_le16(vnics);
5817 req->enables = cpu_to_le32(enables);
5821 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5822 int ring_grps, int cp_rings, int stats, int vnics)
5824 struct hwrm_func_cfg_input req = {0};
5827 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5828 cp_rings, stats, vnics);
5832 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5836 if (bp->hwrm_spec_code < 0x10601)
5837 bp->hw_resc.resv_tx_rings = tx_rings;
5839 rc = bnxt_hwrm_get_rings(bp);
5844 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5845 int ring_grps, int cp_rings, int stats, int vnics)
5847 struct hwrm_func_vf_cfg_input req = {0};
5850 if (!BNXT_NEW_RM(bp)) {
5851 bp->hw_resc.resv_tx_rings = tx_rings;
5855 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5856 cp_rings, stats, vnics);
5857 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5861 rc = bnxt_hwrm_get_rings(bp);
5865 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
5866 int cp, int stat, int vnic)
5869 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
5872 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
5876 int bnxt_nq_rings_in_use(struct bnxt *bp)
5878 int cp = bp->cp_nr_rings;
5879 int ulp_msix, ulp_base;
5881 ulp_msix = bnxt_get_ulp_msix_num(bp);
5883 ulp_base = bnxt_get_ulp_msix_base(bp);
5885 if ((ulp_base + ulp_msix) > cp)
5886 cp = ulp_base + ulp_msix;
5891 static int bnxt_cp_rings_in_use(struct bnxt *bp)
5895 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5896 return bnxt_nq_rings_in_use(bp);
5898 cp = bp->tx_nr_rings + bp->rx_nr_rings;
5902 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
5904 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
5905 int cp = bp->cp_nr_rings;
5910 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
5911 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
5913 return cp + ulp_stat;
5916 static bool bnxt_need_reserve_rings(struct bnxt *bp)
5918 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5919 int cp = bnxt_cp_rings_in_use(bp);
5920 int nq = bnxt_nq_rings_in_use(bp);
5921 int rx = bp->rx_nr_rings, stat;
5922 int vnic = 1, grp = rx;
5924 if (bp->hwrm_spec_code < 0x10601)
5927 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
5930 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5932 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5934 stat = bnxt_get_func_stat_ctxs(bp);
5935 if (BNXT_NEW_RM(bp) &&
5936 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
5937 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
5938 (hw_resc->resv_hw_ring_grps != grp &&
5939 !(bp->flags & BNXT_FLAG_CHIP_P5))))
5941 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
5942 hw_resc->resv_irqs != nq)
5947 static int __bnxt_reserve_rings(struct bnxt *bp)
5949 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5950 int cp = bnxt_nq_rings_in_use(bp);
5951 int tx = bp->tx_nr_rings;
5952 int rx = bp->rx_nr_rings;
5953 int grp, rx_rings, rc;
5957 if (!bnxt_need_reserve_rings(bp))
5960 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5962 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5964 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5966 grp = bp->rx_nr_rings;
5967 stat = bnxt_get_func_stat_ctxs(bp);
5969 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
5973 tx = hw_resc->resv_tx_rings;
5974 if (BNXT_NEW_RM(bp)) {
5975 rx = hw_resc->resv_rx_rings;
5976 cp = hw_resc->resv_irqs;
5977 grp = hw_resc->resv_hw_ring_grps;
5978 vnic = hw_resc->resv_vnics;
5979 stat = hw_resc->resv_stat_ctxs;
5983 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5987 if (netif_running(bp->dev))
5990 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
5991 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5992 bp->dev->hw_features &= ~NETIF_F_LRO;
5993 bp->dev->features &= ~NETIF_F_LRO;
5994 bnxt_set_ring_params(bp);
5997 rx_rings = min_t(int, rx_rings, grp);
5998 cp = min_t(int, cp, bp->cp_nr_rings);
5999 if (stat > bnxt_get_ulp_stat_ctxs(bp))
6000 stat -= bnxt_get_ulp_stat_ctxs(bp);
6001 cp = min_t(int, cp, stat);
6002 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6003 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6005 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6006 bp->tx_nr_rings = tx;
6007 bp->rx_nr_rings = rx_rings;
6008 bp->cp_nr_rings = cp;
6010 if (!tx || !rx || !cp || !grp || !vnic || !stat)
6016 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6017 int ring_grps, int cp_rings, int stats,
6020 struct hwrm_func_vf_cfg_input req = {0};
6024 if (!BNXT_NEW_RM(bp))
6027 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6028 cp_rings, stats, vnics);
6029 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6030 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6031 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6032 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6033 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6034 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6035 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6036 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6038 req.flags = cpu_to_le32(flags);
6039 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6043 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6044 int ring_grps, int cp_rings, int stats,
6047 struct hwrm_func_cfg_input req = {0};
6051 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6052 cp_rings, stats, vnics);
6053 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6054 if (BNXT_NEW_RM(bp)) {
6055 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6056 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6057 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6058 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6059 if (bp->flags & BNXT_FLAG_CHIP_P5)
6060 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6061 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6063 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6066 req.flags = cpu_to_le32(flags);
6067 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6071 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6072 int ring_grps, int cp_rings, int stats,
6075 if (bp->hwrm_spec_code < 0x10801)
6079 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6080 ring_grps, cp_rings, stats,
6083 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6084 cp_rings, stats, vnics);
6087 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6089 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6090 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6091 struct hwrm_ring_aggint_qcaps_input req = {0};
6094 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6095 coal_cap->num_cmpl_dma_aggr_max = 63;
6096 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6097 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6098 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6099 coal_cap->int_lat_tmr_min_max = 65535;
6100 coal_cap->int_lat_tmr_max_max = 65535;
6101 coal_cap->num_cmpl_aggr_int_max = 65535;
6102 coal_cap->timer_units = 80;
6104 if (bp->hwrm_spec_code < 0x10902)
6107 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6108 mutex_lock(&bp->hwrm_cmd_lock);
6109 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6111 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6112 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6113 coal_cap->num_cmpl_dma_aggr_max =
6114 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6115 coal_cap->num_cmpl_dma_aggr_during_int_max =
6116 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6117 coal_cap->cmpl_aggr_dma_tmr_max =
6118 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6119 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6120 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6121 coal_cap->int_lat_tmr_min_max =
6122 le16_to_cpu(resp->int_lat_tmr_min_max);
6123 coal_cap->int_lat_tmr_max_max =
6124 le16_to_cpu(resp->int_lat_tmr_max_max);
6125 coal_cap->num_cmpl_aggr_int_max =
6126 le16_to_cpu(resp->num_cmpl_aggr_int_max);
6127 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6129 mutex_unlock(&bp->hwrm_cmd_lock);
6132 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6134 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6136 return usec * 1000 / coal_cap->timer_units;
6139 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6140 struct bnxt_coal *hw_coal,
6141 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6143 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6144 u32 cmpl_params = coal_cap->cmpl_params;
6145 u16 val, tmr, max, flags = 0;
6147 max = hw_coal->bufs_per_record * 128;
6148 if (hw_coal->budget)
6149 max = hw_coal->bufs_per_record * hw_coal->budget;
6150 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6152 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6153 req->num_cmpl_aggr_int = cpu_to_le16(val);
6155 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6156 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6158 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6159 coal_cap->num_cmpl_dma_aggr_during_int_max);
6160 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6162 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6163 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6164 req->int_lat_tmr_max = cpu_to_le16(tmr);
6166 /* min timer set to 1/2 of interrupt timer */
6167 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6169 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6170 req->int_lat_tmr_min = cpu_to_le16(val);
6171 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6174 /* buf timer set to 1/4 of interrupt timer */
6175 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6176 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6179 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6180 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6181 val = clamp_t(u16, tmr, 1,
6182 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6183 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
6185 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6188 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6189 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6190 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6191 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6192 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6193 req->flags = cpu_to_le16(flags);
6194 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6197 /* Caller holds bp->hwrm_cmd_lock */
6198 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6199 struct bnxt_coal *hw_coal)
6201 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6202 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6203 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6204 u32 nq_params = coal_cap->nq_params;
6207 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6210 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6212 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6214 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6216 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6217 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6218 req.int_lat_tmr_min = cpu_to_le16(tmr);
6219 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6220 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6223 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6225 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6226 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6227 struct bnxt_coal coal;
6229 /* Tick values in micro seconds.
6230 * 1 coal_buf x bufs_per_record = 1 completion record.
6232 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6234 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6235 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6237 if (!bnapi->rx_ring)
6240 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6241 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6243 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6245 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6247 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6251 int bnxt_hwrm_set_coal(struct bnxt *bp)
6254 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6257 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6258 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6259 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6260 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6262 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6263 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6265 mutex_lock(&bp->hwrm_cmd_lock);
6266 for (i = 0; i < bp->cp_nr_rings; i++) {
6267 struct bnxt_napi *bnapi = bp->bnapi[i];
6268 struct bnxt_coal *hw_coal;
6272 if (!bnapi->rx_ring) {
6273 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6276 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6278 req->ring_id = cpu_to_le16(ring_id);
6280 rc = _hwrm_send_message(bp, req, sizeof(*req),
6285 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6288 if (bnapi->rx_ring && bnapi->tx_ring) {
6290 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6291 req->ring_id = cpu_to_le16(ring_id);
6292 rc = _hwrm_send_message(bp, req, sizeof(*req),
6298 hw_coal = &bp->rx_coal;
6300 hw_coal = &bp->tx_coal;
6301 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6303 mutex_unlock(&bp->hwrm_cmd_lock);
6307 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6310 struct hwrm_stat_ctx_free_input req = {0};
6315 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6318 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6320 mutex_lock(&bp->hwrm_cmd_lock);
6321 for (i = 0; i < bp->cp_nr_rings; i++) {
6322 struct bnxt_napi *bnapi = bp->bnapi[i];
6323 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6325 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6326 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6328 rc = _hwrm_send_message(bp, &req, sizeof(req),
6331 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6334 mutex_unlock(&bp->hwrm_cmd_lock);
6338 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6341 struct hwrm_stat_ctx_alloc_input req = {0};
6342 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6344 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6347 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6349 req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6350 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6352 mutex_lock(&bp->hwrm_cmd_lock);
6353 for (i = 0; i < bp->cp_nr_rings; i++) {
6354 struct bnxt_napi *bnapi = bp->bnapi[i];
6355 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6357 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
6359 rc = _hwrm_send_message(bp, &req, sizeof(req),
6364 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6366 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6368 mutex_unlock(&bp->hwrm_cmd_lock);
6372 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6374 struct hwrm_func_qcfg_input req = {0};
6375 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6379 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6380 req.fid = cpu_to_le16(0xffff);
6381 mutex_lock(&bp->hwrm_cmd_lock);
6382 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6384 goto func_qcfg_exit;
6386 #ifdef CONFIG_BNXT_SRIOV
6388 struct bnxt_vf_info *vf = &bp->vf;
6390 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6392 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6395 flags = le16_to_cpu(resp->flags);
6396 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6397 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6398 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6399 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6400 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6402 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6403 bp->flags |= BNXT_FLAG_MULTI_HOST;
6405 switch (resp->port_partition_type) {
6406 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6407 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6408 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6409 bp->port_partition_type = resp->port_partition_type;
6412 if (bp->hwrm_spec_code < 0x10707 ||
6413 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6414 bp->br_mode = BRIDGE_MODE_VEB;
6415 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6416 bp->br_mode = BRIDGE_MODE_VEPA;
6418 bp->br_mode = BRIDGE_MODE_UNDEF;
6420 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6422 bp->max_mtu = BNXT_MAX_MTU;
6425 mutex_unlock(&bp->hwrm_cmd_lock);
6429 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6431 struct hwrm_func_backing_store_qcaps_input req = {0};
6432 struct hwrm_func_backing_store_qcaps_output *resp =
6433 bp->hwrm_cmd_resp_addr;
6436 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6439 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6440 mutex_lock(&bp->hwrm_cmd_lock);
6441 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6443 struct bnxt_ctx_pg_info *ctx_pg;
6444 struct bnxt_ctx_mem_info *ctx;
6447 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6452 ctx_pg = kzalloc(sizeof(*ctx_pg) * (bp->max_q + 1), GFP_KERNEL);
6458 for (i = 0; i < bp->max_q + 1; i++, ctx_pg++)
6459 ctx->tqm_mem[i] = ctx_pg;
6462 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6463 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6464 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6465 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6466 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6467 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6468 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6469 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6470 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6471 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6472 ctx->vnic_max_vnic_entries =
6473 le16_to_cpu(resp->vnic_max_vnic_entries);
6474 ctx->vnic_max_ring_table_entries =
6475 le16_to_cpu(resp->vnic_max_ring_table_entries);
6476 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6477 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6478 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6479 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6480 ctx->tqm_min_entries_per_ring =
6481 le32_to_cpu(resp->tqm_min_entries_per_ring);
6482 ctx->tqm_max_entries_per_ring =
6483 le32_to_cpu(resp->tqm_max_entries_per_ring);
6484 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6485 if (!ctx->tqm_entries_multiple)
6486 ctx->tqm_entries_multiple = 1;
6487 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6488 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6489 ctx->mrav_num_entries_units =
6490 le16_to_cpu(resp->mrav_num_entries_units);
6491 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6492 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6497 mutex_unlock(&bp->hwrm_cmd_lock);
6501 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6506 if (BNXT_PAGE_SHIFT == 13)
6508 else if (BNXT_PAGE_SIZE == 16)
6512 if (rmem->depth >= 1) {
6513 if (rmem->depth == 2)
6517 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6519 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6523 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6524 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6525 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6526 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6527 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6528 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6530 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6532 struct hwrm_func_backing_store_cfg_input req = {0};
6533 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6534 struct bnxt_ctx_pg_info *ctx_pg;
6535 __le32 *num_entries;
6545 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6546 req.enables = cpu_to_le32(enables);
6548 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6549 ctx_pg = &ctx->qp_mem;
6550 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6551 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6552 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6553 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6554 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6555 &req.qpc_pg_size_qpc_lvl,
6558 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6559 ctx_pg = &ctx->srq_mem;
6560 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6561 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6562 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6563 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6564 &req.srq_pg_size_srq_lvl,
6567 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6568 ctx_pg = &ctx->cq_mem;
6569 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6570 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6571 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6572 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6575 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6576 ctx_pg = &ctx->vnic_mem;
6577 req.vnic_num_vnic_entries =
6578 cpu_to_le16(ctx->vnic_max_vnic_entries);
6579 req.vnic_num_ring_table_entries =
6580 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6581 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6582 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6583 &req.vnic_pg_size_vnic_lvl,
6584 &req.vnic_page_dir);
6586 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6587 ctx_pg = &ctx->stat_mem;
6588 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6589 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6590 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6591 &req.stat_pg_size_stat_lvl,
6592 &req.stat_page_dir);
6594 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6595 ctx_pg = &ctx->mrav_mem;
6596 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6597 if (ctx->mrav_num_entries_units)
6599 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
6600 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
6601 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6602 &req.mrav_pg_size_mrav_lvl,
6603 &req.mrav_page_dir);
6605 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
6606 ctx_pg = &ctx->tim_mem;
6607 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
6608 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
6609 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6610 &req.tim_pg_size_tim_lvl,
6613 for (i = 0, num_entries = &req.tqm_sp_num_entries,
6614 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
6615 pg_dir = &req.tqm_sp_page_dir,
6616 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
6617 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
6618 if (!(enables & ena))
6621 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
6622 ctx_pg = ctx->tqm_mem[i];
6623 *num_entries = cpu_to_le32(ctx_pg->entries);
6624 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
6626 req.flags = cpu_to_le32(flags);
6627 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6631 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
6632 struct bnxt_ctx_pg_info *ctx_pg)
6634 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6636 rmem->page_size = BNXT_PAGE_SIZE;
6637 rmem->pg_arr = ctx_pg->ctx_pg_arr;
6638 rmem->dma_arr = ctx_pg->ctx_dma_arr;
6639 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
6640 if (rmem->depth >= 1)
6641 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
6642 return bnxt_alloc_ring(bp, rmem);
6645 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
6646 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
6649 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6655 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6656 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
6657 ctx_pg->nr_pages = 0;
6660 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
6664 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
6666 if (!ctx_pg->ctx_pg_tbl)
6668 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
6669 rmem->nr_pages = nr_tbls;
6670 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6673 for (i = 0; i < nr_tbls; i++) {
6674 struct bnxt_ctx_pg_info *pg_tbl;
6676 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
6679 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
6680 rmem = &pg_tbl->ring_mem;
6681 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
6682 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
6684 rmem->nr_pages = MAX_CTX_PAGES;
6685 if (i == (nr_tbls - 1)) {
6686 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
6689 rmem->nr_pages = rem;
6691 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
6696 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6697 if (rmem->nr_pages > 1 || depth)
6699 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6704 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
6705 struct bnxt_ctx_pg_info *ctx_pg)
6707 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6709 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
6710 ctx_pg->ctx_pg_tbl) {
6711 int i, nr_tbls = rmem->nr_pages;
6713 for (i = 0; i < nr_tbls; i++) {
6714 struct bnxt_ctx_pg_info *pg_tbl;
6715 struct bnxt_ring_mem_info *rmem2;
6717 pg_tbl = ctx_pg->ctx_pg_tbl[i];
6720 rmem2 = &pg_tbl->ring_mem;
6721 bnxt_free_ring(bp, rmem2);
6722 ctx_pg->ctx_pg_arr[i] = NULL;
6724 ctx_pg->ctx_pg_tbl[i] = NULL;
6726 kfree(ctx_pg->ctx_pg_tbl);
6727 ctx_pg->ctx_pg_tbl = NULL;
6729 bnxt_free_ring(bp, rmem);
6730 ctx_pg->nr_pages = 0;
6733 static void bnxt_free_ctx_mem(struct bnxt *bp)
6735 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6741 if (ctx->tqm_mem[0]) {
6742 for (i = 0; i < bp->max_q + 1; i++)
6743 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
6744 kfree(ctx->tqm_mem[0]);
6745 ctx->tqm_mem[0] = NULL;
6748 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
6749 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
6750 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
6751 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
6752 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
6753 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
6754 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
6755 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
6758 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
6760 struct bnxt_ctx_pg_info *ctx_pg;
6761 struct bnxt_ctx_mem_info *ctx;
6762 u32 mem_size, ena, entries;
6769 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
6771 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
6776 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
6779 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
6785 ctx_pg = &ctx->qp_mem;
6786 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
6788 mem_size = ctx->qp_entry_size * ctx_pg->entries;
6789 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6793 ctx_pg = &ctx->srq_mem;
6794 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
6795 mem_size = ctx->srq_entry_size * ctx_pg->entries;
6796 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6800 ctx_pg = &ctx->cq_mem;
6801 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
6802 mem_size = ctx->cq_entry_size * ctx_pg->entries;
6803 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6807 ctx_pg = &ctx->vnic_mem;
6808 ctx_pg->entries = ctx->vnic_max_vnic_entries +
6809 ctx->vnic_max_ring_table_entries;
6810 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
6811 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6815 ctx_pg = &ctx->stat_mem;
6816 ctx_pg->entries = ctx->stat_max_entries;
6817 mem_size = ctx->stat_entry_size * ctx_pg->entries;
6818 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6823 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
6826 ctx_pg = &ctx->mrav_mem;
6827 /* 128K extra is needed to accommodate static AH context
6828 * allocation by f/w.
6830 num_mr = 1024 * 256;
6831 num_ah = 1024 * 128;
6832 ctx_pg->entries = num_mr + num_ah;
6833 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
6834 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2);
6837 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
6838 if (ctx->mrav_num_entries_units)
6840 ((num_mr / ctx->mrav_num_entries_units) << 16) |
6841 (num_ah / ctx->mrav_num_entries_units);
6843 ctx_pg = &ctx->tim_mem;
6844 ctx_pg->entries = ctx->qp_mem.entries;
6845 mem_size = ctx->tim_entry_size * ctx_pg->entries;
6846 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6849 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
6852 entries = ctx->qp_max_l2_entries + extra_qps;
6853 entries = roundup(entries, ctx->tqm_entries_multiple);
6854 entries = clamp_t(u32, entries, ctx->tqm_min_entries_per_ring,
6855 ctx->tqm_max_entries_per_ring);
6856 for (i = 0; i < bp->max_q + 1; i++) {
6857 ctx_pg = ctx->tqm_mem[i];
6858 ctx_pg->entries = entries;
6859 mem_size = ctx->tqm_entry_size * entries;
6860 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6863 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
6865 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
6866 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
6868 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
6871 ctx->flags |= BNXT_CTX_FLAG_INITED;
6876 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
6878 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6879 struct hwrm_func_resource_qcaps_input req = {0};
6880 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6883 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
6884 req.fid = cpu_to_le16(0xffff);
6886 mutex_lock(&bp->hwrm_cmd_lock);
6887 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
6890 goto hwrm_func_resc_qcaps_exit;
6892 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
6894 goto hwrm_func_resc_qcaps_exit;
6896 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
6897 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6898 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
6899 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6900 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
6901 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6902 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
6903 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6904 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
6905 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
6906 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
6907 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6908 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
6909 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6910 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
6911 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6913 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6914 u16 max_msix = le16_to_cpu(resp->max_msix);
6916 hw_resc->max_nqs = max_msix;
6917 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
6921 struct bnxt_pf_info *pf = &bp->pf;
6923 pf->vf_resv_strategy =
6924 le16_to_cpu(resp->vf_reservation_strategy);
6925 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
6926 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
6928 hwrm_func_resc_qcaps_exit:
6929 mutex_unlock(&bp->hwrm_cmd_lock);
6933 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
6936 struct hwrm_func_qcaps_input req = {0};
6937 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6938 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6941 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
6942 req.fid = cpu_to_le16(0xffff);
6944 mutex_lock(&bp->hwrm_cmd_lock);
6945 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6947 goto hwrm_func_qcaps_exit;
6949 flags = le32_to_cpu(resp->flags);
6950 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
6951 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
6952 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
6953 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
6954 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
6955 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
6956 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
6957 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
6958 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
6959 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
6960 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
6961 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
6962 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
6963 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
6965 bp->tx_push_thresh = 0;
6966 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
6967 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
6969 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6970 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6971 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6972 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6973 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
6974 if (!hw_resc->max_hw_ring_grps)
6975 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
6976 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6977 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6978 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6981 struct bnxt_pf_info *pf = &bp->pf;
6983 pf->fw_fid = le16_to_cpu(resp->fid);
6984 pf->port_id = le16_to_cpu(resp->port_id);
6985 bp->dev->dev_port = pf->port_id;
6986 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
6987 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
6988 pf->max_vfs = le16_to_cpu(resp->max_vfs);
6989 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
6990 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
6991 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
6992 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
6993 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
6994 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
6995 bp->flags &= ~BNXT_FLAG_WOL_CAP;
6996 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
6997 bp->flags |= BNXT_FLAG_WOL_CAP;
6999 #ifdef CONFIG_BNXT_SRIOV
7000 struct bnxt_vf_info *vf = &bp->vf;
7002 vf->fw_fid = le16_to_cpu(resp->fid);
7003 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7007 hwrm_func_qcaps_exit:
7008 mutex_unlock(&bp->hwrm_cmd_lock);
7012 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7014 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7018 rc = __bnxt_hwrm_func_qcaps(bp);
7021 rc = bnxt_hwrm_queue_qportcfg(bp);
7023 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7026 if (bp->hwrm_spec_code >= 0x10803) {
7027 rc = bnxt_alloc_ctx_mem(bp);
7030 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7032 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7037 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7039 struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7040 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7044 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7047 resp = bp->hwrm_cmd_resp_addr;
7048 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7050 mutex_lock(&bp->hwrm_cmd_lock);
7051 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7053 goto hwrm_cfa_adv_qcaps_exit;
7055 flags = le32_to_cpu(resp->flags);
7057 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7058 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7060 hwrm_cfa_adv_qcaps_exit:
7061 mutex_unlock(&bp->hwrm_cmd_lock);
7065 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7067 struct bnxt_fw_health *fw_health = bp->fw_health;
7068 u32 reg_base = 0xffffffff;
7071 /* Only pre-map the monitoring GRC registers using window 3 */
7072 for (i = 0; i < 4; i++) {
7073 u32 reg = fw_health->regs[i];
7075 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7077 if (reg_base == 0xffffffff)
7078 reg_base = reg & BNXT_GRC_BASE_MASK;
7079 if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7081 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_BASE +
7082 (reg & BNXT_GRC_OFFSET_MASK);
7084 if (reg_base == 0xffffffff)
7087 writel(reg_base, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7088 BNXT_FW_HEALTH_WIN_MAP_OFF);
7092 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7094 struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7095 struct bnxt_fw_health *fw_health = bp->fw_health;
7096 struct hwrm_error_recovery_qcfg_input req = {0};
7099 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7102 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7103 mutex_lock(&bp->hwrm_cmd_lock);
7104 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7106 goto err_recovery_out;
7108 fw_health = kzalloc(sizeof(*fw_health), GFP_KERNEL);
7109 bp->fw_health = fw_health;
7112 goto err_recovery_out;
7115 fw_health->flags = le32_to_cpu(resp->flags);
7116 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7117 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7119 goto err_recovery_out;
7121 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7122 fw_health->master_func_wait_dsecs =
7123 le32_to_cpu(resp->master_func_wait_period);
7124 fw_health->normal_func_wait_dsecs =
7125 le32_to_cpu(resp->normal_func_wait_period);
7126 fw_health->post_reset_wait_dsecs =
7127 le32_to_cpu(resp->master_func_wait_period_after_reset);
7128 fw_health->post_reset_max_wait_dsecs =
7129 le32_to_cpu(resp->max_bailout_time_after_reset);
7130 fw_health->regs[BNXT_FW_HEALTH_REG] =
7131 le32_to_cpu(resp->fw_health_status_reg);
7132 fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7133 le32_to_cpu(resp->fw_heartbeat_reg);
7134 fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7135 le32_to_cpu(resp->fw_reset_cnt_reg);
7136 fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7137 le32_to_cpu(resp->reset_inprogress_reg);
7138 fw_health->fw_reset_inprog_reg_mask =
7139 le32_to_cpu(resp->reset_inprogress_reg_mask);
7140 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7141 if (fw_health->fw_reset_seq_cnt >= 16) {
7143 goto err_recovery_out;
7145 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7146 fw_health->fw_reset_seq_regs[i] =
7147 le32_to_cpu(resp->reset_reg[i]);
7148 fw_health->fw_reset_seq_vals[i] =
7149 le32_to_cpu(resp->reset_reg_val[i]);
7150 fw_health->fw_reset_seq_delay_msec[i] =
7151 resp->delay_after_reset[i];
7154 mutex_unlock(&bp->hwrm_cmd_lock);
7156 rc = bnxt_map_fw_health_regs(bp);
7158 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7162 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7164 struct hwrm_func_reset_input req = {0};
7166 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7169 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7172 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7175 struct hwrm_queue_qportcfg_input req = {0};
7176 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7180 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7182 mutex_lock(&bp->hwrm_cmd_lock);
7183 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7187 if (!resp->max_configurable_queues) {
7191 bp->max_tc = resp->max_configurable_queues;
7192 bp->max_lltc = resp->max_configurable_lossless_queues;
7193 if (bp->max_tc > BNXT_MAX_QUEUE)
7194 bp->max_tc = BNXT_MAX_QUEUE;
7196 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7197 qptr = &resp->queue_id0;
7198 for (i = 0, j = 0; i < bp->max_tc; i++) {
7199 bp->q_info[j].queue_id = *qptr;
7200 bp->q_ids[i] = *qptr++;
7201 bp->q_info[j].queue_profile = *qptr++;
7202 bp->tc_to_qidx[j] = j;
7203 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7204 (no_rdma && BNXT_PF(bp)))
7207 bp->max_q = bp->max_tc;
7208 bp->max_tc = max_t(u8, j, 1);
7210 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7213 if (bp->max_lltc > bp->max_tc)
7214 bp->max_lltc = bp->max_tc;
7217 mutex_unlock(&bp->hwrm_cmd_lock);
7221 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7223 struct hwrm_ver_get_input req = {0};
7226 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7227 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7228 req.hwrm_intf_min = HWRM_VERSION_MINOR;
7229 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7231 rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7236 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7238 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7242 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7243 mutex_lock(&bp->hwrm_cmd_lock);
7244 rc = __bnxt_hwrm_ver_get(bp, false);
7246 goto hwrm_ver_get_exit;
7248 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7250 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7251 resp->hwrm_intf_min_8b << 8 |
7252 resp->hwrm_intf_upd_8b;
7253 if (resp->hwrm_intf_maj_8b < 1) {
7254 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7255 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7256 resp->hwrm_intf_upd_8b);
7257 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7259 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
7260 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
7261 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
7263 if (strlen(resp->active_pkg_name)) {
7264 int fw_ver_len = strlen(bp->fw_ver_str);
7266 snprintf(bp->fw_ver_str + fw_ver_len,
7267 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7268 resp->active_pkg_name);
7269 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7272 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7273 if (!bp->hwrm_cmd_timeout)
7274 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7276 if (resp->hwrm_intf_maj_8b >= 1) {
7277 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7278 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7280 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7281 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7283 bp->chip_num = le16_to_cpu(resp->chip_num);
7284 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7286 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7288 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7289 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7290 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7291 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7293 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7294 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7297 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7298 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7301 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7302 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7305 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7306 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7309 mutex_unlock(&bp->hwrm_cmd_lock);
7313 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7315 struct hwrm_fw_set_time_input req = {0};
7317 time64_t now = ktime_get_real_seconds();
7319 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7320 bp->hwrm_spec_code < 0x10400)
7323 time64_to_tm(now, 0, &tm);
7324 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7325 req.year = cpu_to_le16(1900 + tm.tm_year);
7326 req.month = 1 + tm.tm_mon;
7327 req.day = tm.tm_mday;
7328 req.hour = tm.tm_hour;
7329 req.minute = tm.tm_min;
7330 req.second = tm.tm_sec;
7331 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7334 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
7337 struct bnxt_pf_info *pf = &bp->pf;
7338 struct hwrm_port_qstats_input req = {0};
7340 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7343 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7344 req.port_id = cpu_to_le16(pf->port_id);
7345 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
7346 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
7347 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7351 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
7353 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7354 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7355 struct hwrm_port_qstats_ext_input req = {0};
7356 struct bnxt_pf_info *pf = &bp->pf;
7360 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7363 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7364 req.port_id = cpu_to_le16(pf->port_id);
7365 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7366 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
7367 tx_stat_size = bp->hw_tx_port_stats_ext ?
7368 sizeof(*bp->hw_tx_port_stats_ext) : 0;
7369 req.tx_stat_size = cpu_to_le16(tx_stat_size);
7370 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
7371 mutex_lock(&bp->hwrm_cmd_lock);
7372 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7374 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7375 bp->fw_tx_stats_ext_size = tx_stat_size ?
7376 le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7378 bp->fw_rx_stats_ext_size = 0;
7379 bp->fw_tx_stats_ext_size = 0;
7381 if (bp->fw_tx_stats_ext_size <=
7382 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7383 mutex_unlock(&bp->hwrm_cmd_lock);
7384 bp->pri2cos_valid = 0;
7388 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
7389 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
7391 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
7393 struct hwrm_queue_pri2cos_qcfg_output *resp2;
7397 resp2 = bp->hwrm_cmd_resp_addr;
7398 pri2cos = &resp2->pri0_cos_queue_id;
7399 for (i = 0; i < 8; i++) {
7400 u8 queue_id = pri2cos[i];
7402 for (j = 0; j < bp->max_q; j++) {
7403 if (bp->q_ids[j] == queue_id)
7407 bp->pri2cos_valid = 1;
7409 mutex_unlock(&bp->hwrm_cmd_lock);
7413 static int bnxt_hwrm_pcie_qstats(struct bnxt *bp)
7415 struct hwrm_pcie_qstats_input req = {0};
7417 if (!(bp->flags & BNXT_FLAG_PCIE_STATS))
7420 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
7421 req.pcie_stat_size = cpu_to_le16(sizeof(struct pcie_ctx_hw_stats));
7422 req.pcie_stat_host_addr = cpu_to_le64(bp->hw_pcie_stats_map);
7423 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7426 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
7428 if (bp->vxlan_port_cnt) {
7429 bnxt_hwrm_tunnel_dst_port_free(
7430 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7432 bp->vxlan_port_cnt = 0;
7433 if (bp->nge_port_cnt) {
7434 bnxt_hwrm_tunnel_dst_port_free(
7435 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7437 bp->nge_port_cnt = 0;
7440 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
7446 tpa_flags = bp->flags & BNXT_FLAG_TPA;
7447 else if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
7449 for (i = 0; i < bp->nr_vnics; i++) {
7450 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
7452 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
7460 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
7464 for (i = 0; i < bp->nr_vnics; i++)
7465 bnxt_hwrm_vnic_set_rss(bp, i, false);
7468 static void bnxt_clear_vnic(struct bnxt *bp)
7473 bnxt_hwrm_clear_vnic_filter(bp);
7474 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
7475 /* clear all RSS setting before free vnic ctx */
7476 bnxt_hwrm_clear_vnic_rss(bp);
7477 bnxt_hwrm_vnic_ctx_free(bp);
7479 /* before free the vnic, undo the vnic tpa settings */
7480 if (bp->flags & BNXT_FLAG_TPA)
7481 bnxt_set_tpa(bp, false);
7482 bnxt_hwrm_vnic_free(bp);
7483 if (bp->flags & BNXT_FLAG_CHIP_P5)
7484 bnxt_hwrm_vnic_ctx_free(bp);
7487 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
7490 bnxt_clear_vnic(bp);
7491 bnxt_hwrm_ring_free(bp, close_path);
7492 bnxt_hwrm_ring_grp_free(bp);
7494 bnxt_hwrm_stat_ctx_free(bp);
7495 bnxt_hwrm_free_tunnel_ports(bp);
7499 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
7501 struct hwrm_func_cfg_input req = {0};
7504 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7505 req.fid = cpu_to_le16(0xffff);
7506 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
7507 if (br_mode == BRIDGE_MODE_VEB)
7508 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
7509 else if (br_mode == BRIDGE_MODE_VEPA)
7510 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
7513 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7517 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
7519 struct hwrm_func_cfg_input req = {0};
7522 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
7525 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7526 req.fid = cpu_to_le16(0xffff);
7527 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
7528 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
7530 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
7532 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7536 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
7538 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
7541 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
7544 /* allocate context for vnic */
7545 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
7547 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7549 goto vnic_setup_err;
7551 bp->rsscos_nr_ctxs++;
7553 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7554 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
7556 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
7558 goto vnic_setup_err;
7560 bp->rsscos_nr_ctxs++;
7564 /* configure default vnic, ring grp */
7565 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7567 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7569 goto vnic_setup_err;
7572 /* Enable RSS hashing on vnic */
7573 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
7575 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
7577 goto vnic_setup_err;
7580 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7581 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7583 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
7592 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
7596 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
7597 for (i = 0; i < nr_ctxs; i++) {
7598 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
7600 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
7604 bp->rsscos_nr_ctxs++;
7609 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
7611 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
7615 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7617 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7621 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7622 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7624 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
7631 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
7633 if (bp->flags & BNXT_FLAG_CHIP_P5)
7634 return __bnxt_setup_vnic_p5(bp, vnic_id);
7636 return __bnxt_setup_vnic(bp, vnic_id);
7639 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
7641 #ifdef CONFIG_RFS_ACCEL
7644 if (bp->flags & BNXT_FLAG_CHIP_P5)
7647 for (i = 0; i < bp->rx_nr_rings; i++) {
7648 struct bnxt_vnic_info *vnic;
7649 u16 vnic_id = i + 1;
7652 if (vnic_id >= bp->nr_vnics)
7655 vnic = &bp->vnic_info[vnic_id];
7656 vnic->flags |= BNXT_VNIC_RFS_FLAG;
7657 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7658 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
7659 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
7661 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7665 rc = bnxt_setup_vnic(bp, vnic_id);
7675 /* Allow PF and VF with default VLAN to be in promiscuous mode */
7676 static bool bnxt_promisc_ok(struct bnxt *bp)
7678 #ifdef CONFIG_BNXT_SRIOV
7679 if (BNXT_VF(bp) && !bp->vf.vlan)
7685 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
7687 unsigned int rc = 0;
7689 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
7691 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7696 rc = bnxt_hwrm_vnic_cfg(bp, 1);
7698 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7705 static int bnxt_cfg_rx_mode(struct bnxt *);
7706 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
7708 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
7710 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7712 unsigned int rx_nr_rings = bp->rx_nr_rings;
7715 rc = bnxt_hwrm_stat_ctx_alloc(bp);
7717 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
7723 rc = bnxt_hwrm_ring_alloc(bp);
7725 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
7729 rc = bnxt_hwrm_ring_grp_alloc(bp);
7731 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
7735 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7738 /* default vnic 0 */
7739 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
7741 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
7745 rc = bnxt_setup_vnic(bp, 0);
7749 if (bp->flags & BNXT_FLAG_RFS) {
7750 rc = bnxt_alloc_rfs_vnics(bp);
7755 if (bp->flags & BNXT_FLAG_TPA) {
7756 rc = bnxt_set_tpa(bp, true);
7762 bnxt_update_vf_mac(bp);
7764 /* Filter for default vnic 0 */
7765 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
7767 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
7770 vnic->uc_filter_count = 1;
7773 if (bp->dev->flags & IFF_BROADCAST)
7774 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
7776 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
7777 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7779 if (bp->dev->flags & IFF_ALLMULTI) {
7780 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7781 vnic->mc_list_count = 0;
7785 bnxt_mc_list_updated(bp, &mask);
7786 vnic->rx_mask |= mask;
7789 rc = bnxt_cfg_rx_mode(bp);
7793 rc = bnxt_hwrm_set_coal(bp);
7795 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
7798 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7799 rc = bnxt_setup_nitroa0_vnic(bp);
7801 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
7806 bnxt_hwrm_func_qcfg(bp);
7807 netdev_update_features(bp->dev);
7813 bnxt_hwrm_resource_free(bp, 0, true);
7818 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
7820 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
7824 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
7826 bnxt_init_cp_rings(bp);
7827 bnxt_init_rx_rings(bp);
7828 bnxt_init_tx_rings(bp);
7829 bnxt_init_ring_grps(bp, irq_re_init);
7830 bnxt_init_vnics(bp);
7832 return bnxt_init_chip(bp, irq_re_init);
7835 static int bnxt_set_real_num_queues(struct bnxt *bp)
7838 struct net_device *dev = bp->dev;
7840 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
7841 bp->tx_nr_rings_xdp);
7845 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
7849 #ifdef CONFIG_RFS_ACCEL
7850 if (bp->flags & BNXT_FLAG_RFS)
7851 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
7857 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7860 int _rx = *rx, _tx = *tx;
7863 *rx = min_t(int, _rx, max);
7864 *tx = min_t(int, _tx, max);
7869 while (_rx + _tx > max) {
7870 if (_rx > _tx && _rx > 1)
7881 static void bnxt_setup_msix(struct bnxt *bp)
7883 const int len = sizeof(bp->irq_tbl[0].name);
7884 struct net_device *dev = bp->dev;
7887 tcs = netdev_get_num_tc(dev);
7891 for (i = 0; i < tcs; i++) {
7892 count = bp->tx_nr_rings_per_tc;
7894 netdev_set_tc_queue(dev, i, count, off);
7898 for (i = 0; i < bp->cp_nr_rings; i++) {
7899 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7902 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7904 else if (i < bp->rx_nr_rings)
7909 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
7911 bp->irq_tbl[map_idx].handler = bnxt_msix;
7915 static void bnxt_setup_inta(struct bnxt *bp)
7917 const int len = sizeof(bp->irq_tbl[0].name);
7919 if (netdev_get_num_tc(bp->dev))
7920 netdev_reset_tc(bp->dev);
7922 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
7924 bp->irq_tbl[0].handler = bnxt_inta;
7927 static int bnxt_setup_int_mode(struct bnxt *bp)
7931 if (bp->flags & BNXT_FLAG_USING_MSIX)
7932 bnxt_setup_msix(bp);
7934 bnxt_setup_inta(bp);
7936 rc = bnxt_set_real_num_queues(bp);
7940 #ifdef CONFIG_RFS_ACCEL
7941 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
7943 return bp->hw_resc.max_rsscos_ctxs;
7946 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
7948 return bp->hw_resc.max_vnics;
7952 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
7954 return bp->hw_resc.max_stat_ctxs;
7957 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
7959 return bp->hw_resc.max_cp_rings;
7962 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
7964 unsigned int cp = bp->hw_resc.max_cp_rings;
7966 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7967 cp -= bnxt_get_ulp_msix_num(bp);
7972 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
7974 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7976 if (bp->flags & BNXT_FLAG_CHIP_P5)
7977 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
7979 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
7982 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
7984 bp->hw_resc.max_irqs = max_irqs;
7987 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
7991 cp = bnxt_get_max_func_cp_rings_for_en(bp);
7992 if (bp->flags & BNXT_FLAG_CHIP_P5)
7993 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
7995 return cp - bp->cp_nr_rings;
7998 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8000 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8003 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8005 int max_cp = bnxt_get_max_func_cp_rings(bp);
8006 int max_irq = bnxt_get_max_func_irqs(bp);
8007 int total_req = bp->cp_nr_rings + num;
8008 int max_idx, avail_msix;
8010 max_idx = bp->total_irqs;
8011 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8012 max_idx = min_t(int, bp->total_irqs, max_cp);
8013 avail_msix = max_idx - bp->cp_nr_rings;
8014 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8017 if (max_irq < total_req) {
8018 num = max_irq - bp->cp_nr_rings;
8025 static int bnxt_get_num_msix(struct bnxt *bp)
8027 if (!BNXT_NEW_RM(bp))
8028 return bnxt_get_max_func_irqs(bp);
8030 return bnxt_nq_rings_in_use(bp);
8033 static int bnxt_init_msix(struct bnxt *bp)
8035 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8036 struct msix_entry *msix_ent;
8038 total_vecs = bnxt_get_num_msix(bp);
8039 max = bnxt_get_max_func_irqs(bp);
8040 if (total_vecs > max)
8046 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8050 for (i = 0; i < total_vecs; i++) {
8051 msix_ent[i].entry = i;
8052 msix_ent[i].vector = 0;
8055 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8058 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8059 ulp_msix = bnxt_get_ulp_msix_num(bp);
8060 if (total_vecs < 0 || total_vecs < ulp_msix) {
8062 goto msix_setup_exit;
8065 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8067 for (i = 0; i < total_vecs; i++)
8068 bp->irq_tbl[i].vector = msix_ent[i].vector;
8070 bp->total_irqs = total_vecs;
8071 /* Trim rings based upon num of vectors allocated */
8072 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8073 total_vecs - ulp_msix, min == 1);
8075 goto msix_setup_exit;
8077 bp->cp_nr_rings = (min == 1) ?
8078 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8079 bp->tx_nr_rings + bp->rx_nr_rings;
8083 goto msix_setup_exit;
8085 bp->flags |= BNXT_FLAG_USING_MSIX;
8090 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8093 pci_disable_msix(bp->pdev);
8098 static int bnxt_init_inta(struct bnxt *bp)
8100 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
8105 bp->rx_nr_rings = 1;
8106 bp->tx_nr_rings = 1;
8107 bp->cp_nr_rings = 1;
8108 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8109 bp->irq_tbl[0].vector = bp->pdev->irq;
8113 static int bnxt_init_int_mode(struct bnxt *bp)
8117 if (bp->flags & BNXT_FLAG_MSIX_CAP)
8118 rc = bnxt_init_msix(bp);
8120 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8121 /* fallback to INTA */
8122 rc = bnxt_init_inta(bp);
8127 static void bnxt_clear_int_mode(struct bnxt *bp)
8129 if (bp->flags & BNXT_FLAG_USING_MSIX)
8130 pci_disable_msix(bp->pdev);
8134 bp->flags &= ~BNXT_FLAG_USING_MSIX;
8137 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8139 int tcs = netdev_get_num_tc(bp->dev);
8140 bool irq_cleared = false;
8143 if (!bnxt_need_reserve_rings(bp))
8146 if (irq_re_init && BNXT_NEW_RM(bp) &&
8147 bnxt_get_num_msix(bp) != bp->total_irqs) {
8148 bnxt_ulp_irq_stop(bp);
8149 bnxt_clear_int_mode(bp);
8152 rc = __bnxt_reserve_rings(bp);
8155 rc = bnxt_init_int_mode(bp);
8156 bnxt_ulp_irq_restart(bp, rc);
8159 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8162 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
8163 netdev_err(bp->dev, "tx ring reservation failure\n");
8164 netdev_reset_tc(bp->dev);
8165 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8171 static void bnxt_free_irq(struct bnxt *bp)
8173 struct bnxt_irq *irq;
8176 #ifdef CONFIG_RFS_ACCEL
8177 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
8178 bp->dev->rx_cpu_rmap = NULL;
8180 if (!bp->irq_tbl || !bp->bnapi)
8183 for (i = 0; i < bp->cp_nr_rings; i++) {
8184 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8186 irq = &bp->irq_tbl[map_idx];
8187 if (irq->requested) {
8188 if (irq->have_cpumask) {
8189 irq_set_affinity_hint(irq->vector, NULL);
8190 free_cpumask_var(irq->cpu_mask);
8191 irq->have_cpumask = 0;
8193 free_irq(irq->vector, bp->bnapi[i]);
8200 static int bnxt_request_irq(struct bnxt *bp)
8203 unsigned long flags = 0;
8204 #ifdef CONFIG_RFS_ACCEL
8205 struct cpu_rmap *rmap;
8208 rc = bnxt_setup_int_mode(bp);
8210 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
8214 #ifdef CONFIG_RFS_ACCEL
8215 rmap = bp->dev->rx_cpu_rmap;
8217 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
8218 flags = IRQF_SHARED;
8220 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
8221 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8222 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
8224 #ifdef CONFIG_RFS_ACCEL
8225 if (rmap && bp->bnapi[i]->rx_ring) {
8226 rc = irq_cpu_rmap_add(rmap, irq->vector);
8228 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
8233 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
8240 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
8241 int numa_node = dev_to_node(&bp->pdev->dev);
8243 irq->have_cpumask = 1;
8244 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
8246 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
8248 netdev_warn(bp->dev,
8249 "Set affinity failed, IRQ = %d\n",
8258 static void bnxt_del_napi(struct bnxt *bp)
8265 for (i = 0; i < bp->cp_nr_rings; i++) {
8266 struct bnxt_napi *bnapi = bp->bnapi[i];
8268 napi_hash_del(&bnapi->napi);
8269 netif_napi_del(&bnapi->napi);
8271 /* We called napi_hash_del() before netif_napi_del(), we need
8272 * to respect an RCU grace period before freeing napi structures.
8277 static void bnxt_init_napi(struct bnxt *bp)
8280 unsigned int cp_nr_rings = bp->cp_nr_rings;
8281 struct bnxt_napi *bnapi;
8283 if (bp->flags & BNXT_FLAG_USING_MSIX) {
8284 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8286 if (bp->flags & BNXT_FLAG_CHIP_P5)
8287 poll_fn = bnxt_poll_p5;
8288 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8290 for (i = 0; i < cp_nr_rings; i++) {
8291 bnapi = bp->bnapi[i];
8292 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8294 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8295 bnapi = bp->bnapi[cp_nr_rings];
8296 netif_napi_add(bp->dev, &bnapi->napi,
8297 bnxt_poll_nitroa0, 64);
8300 bnapi = bp->bnapi[0];
8301 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8305 static void bnxt_disable_napi(struct bnxt *bp)
8312 for (i = 0; i < bp->cp_nr_rings; i++) {
8313 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8315 if (bp->bnapi[i]->rx_ring)
8316 cancel_work_sync(&cpr->dim.work);
8318 napi_disable(&bp->bnapi[i]->napi);
8322 static void bnxt_enable_napi(struct bnxt *bp)
8326 for (i = 0; i < bp->cp_nr_rings; i++) {
8327 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8328 bp->bnapi[i]->in_reset = false;
8330 if (bp->bnapi[i]->rx_ring) {
8331 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8332 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8334 napi_enable(&bp->bnapi[i]->napi);
8338 void bnxt_tx_disable(struct bnxt *bp)
8341 struct bnxt_tx_ring_info *txr;
8344 for (i = 0; i < bp->tx_nr_rings; i++) {
8345 txr = &bp->tx_ring[i];
8346 txr->dev_state = BNXT_DEV_STATE_CLOSING;
8349 /* Stop all TX queues */
8350 netif_tx_disable(bp->dev);
8351 netif_carrier_off(bp->dev);
8354 void bnxt_tx_enable(struct bnxt *bp)
8357 struct bnxt_tx_ring_info *txr;
8359 for (i = 0; i < bp->tx_nr_rings; i++) {
8360 txr = &bp->tx_ring[i];
8363 netif_tx_wake_all_queues(bp->dev);
8364 if (bp->link_info.link_up)
8365 netif_carrier_on(bp->dev);
8368 static void bnxt_report_link(struct bnxt *bp)
8370 if (bp->link_info.link_up) {
8372 const char *flow_ctrl;
8376 netif_carrier_on(bp->dev);
8377 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
8381 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
8382 flow_ctrl = "ON - receive & transmit";
8383 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
8384 flow_ctrl = "ON - transmit";
8385 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
8386 flow_ctrl = "ON - receive";
8389 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
8390 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
8391 speed, duplex, flow_ctrl);
8392 if (bp->flags & BNXT_FLAG_EEE_CAP)
8393 netdev_info(bp->dev, "EEE is %s\n",
8394 bp->eee.eee_active ? "active" :
8396 fec = bp->link_info.fec_cfg;
8397 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
8398 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
8399 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
8400 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
8401 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
8403 netif_carrier_off(bp->dev);
8404 netdev_err(bp->dev, "NIC Link is Down\n");
8408 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
8411 struct hwrm_port_phy_qcaps_input req = {0};
8412 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8413 struct bnxt_link_info *link_info = &bp->link_info;
8415 bp->flags &= ~BNXT_FLAG_EEE_CAP;
8417 bp->test_info->flags &= ~BNXT_TEST_FL_EXT_LPBK;
8418 if (bp->hwrm_spec_code < 0x10201)
8421 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
8423 mutex_lock(&bp->hwrm_cmd_lock);
8424 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8426 goto hwrm_phy_qcaps_exit;
8428 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
8429 struct ethtool_eee *eee = &bp->eee;
8430 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
8432 bp->flags |= BNXT_FLAG_EEE_CAP;
8433 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8434 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
8435 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
8436 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
8437 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
8439 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
8441 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
8443 if (resp->supported_speeds_auto_mode)
8444 link_info->support_auto_speeds =
8445 le16_to_cpu(resp->supported_speeds_auto_mode);
8447 bp->port_count = resp->port_cnt;
8449 hwrm_phy_qcaps_exit:
8450 mutex_unlock(&bp->hwrm_cmd_lock);
8454 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
8457 struct bnxt_link_info *link_info = &bp->link_info;
8458 struct hwrm_port_phy_qcfg_input req = {0};
8459 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8460 u8 link_up = link_info->link_up;
8463 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
8465 mutex_lock(&bp->hwrm_cmd_lock);
8466 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8468 mutex_unlock(&bp->hwrm_cmd_lock);
8472 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
8473 link_info->phy_link_status = resp->link;
8474 link_info->duplex = resp->duplex_cfg;
8475 if (bp->hwrm_spec_code >= 0x10800)
8476 link_info->duplex = resp->duplex_state;
8477 link_info->pause = resp->pause;
8478 link_info->auto_mode = resp->auto_mode;
8479 link_info->auto_pause_setting = resp->auto_pause;
8480 link_info->lp_pause = resp->link_partner_adv_pause;
8481 link_info->force_pause_setting = resp->force_pause;
8482 link_info->duplex_setting = resp->duplex_cfg;
8483 if (link_info->phy_link_status == BNXT_LINK_LINK)
8484 link_info->link_speed = le16_to_cpu(resp->link_speed);
8486 link_info->link_speed = 0;
8487 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
8488 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
8489 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
8490 link_info->lp_auto_link_speeds =
8491 le16_to_cpu(resp->link_partner_adv_speeds);
8492 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
8493 link_info->phy_ver[0] = resp->phy_maj;
8494 link_info->phy_ver[1] = resp->phy_min;
8495 link_info->phy_ver[2] = resp->phy_bld;
8496 link_info->media_type = resp->media_type;
8497 link_info->phy_type = resp->phy_type;
8498 link_info->transceiver = resp->xcvr_pkg_type;
8499 link_info->phy_addr = resp->eee_config_phy_addr &
8500 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
8501 link_info->module_status = resp->module_status;
8503 if (bp->flags & BNXT_FLAG_EEE_CAP) {
8504 struct ethtool_eee *eee = &bp->eee;
8507 eee->eee_active = 0;
8508 if (resp->eee_config_phy_addr &
8509 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
8510 eee->eee_active = 1;
8511 fw_speeds = le16_to_cpu(
8512 resp->link_partner_adv_eee_link_speed_mask);
8513 eee->lp_advertised =
8514 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8517 /* Pull initial EEE config */
8518 if (!chng_link_state) {
8519 if (resp->eee_config_phy_addr &
8520 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
8521 eee->eee_enabled = 1;
8523 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
8525 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8527 if (resp->eee_config_phy_addr &
8528 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
8531 eee->tx_lpi_enabled = 1;
8532 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
8533 eee->tx_lpi_timer = le32_to_cpu(tmr) &
8534 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
8539 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
8540 if (bp->hwrm_spec_code >= 0x10504)
8541 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
8543 /* TODO: need to add more logic to report VF link */
8544 if (chng_link_state) {
8545 if (link_info->phy_link_status == BNXT_LINK_LINK)
8546 link_info->link_up = 1;
8548 link_info->link_up = 0;
8549 if (link_up != link_info->link_up)
8550 bnxt_report_link(bp);
8552 /* alwasy link down if not require to update link state */
8553 link_info->link_up = 0;
8555 mutex_unlock(&bp->hwrm_cmd_lock);
8557 if (!BNXT_SINGLE_PF(bp))
8560 diff = link_info->support_auto_speeds ^ link_info->advertising;
8561 if ((link_info->support_auto_speeds | diff) !=
8562 link_info->support_auto_speeds) {
8563 /* An advertised speed is no longer supported, so we need to
8564 * update the advertisement settings. Caller holds RTNL
8565 * so we can modify link settings.
8567 link_info->advertising = link_info->support_auto_speeds;
8568 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
8569 bnxt_hwrm_set_link_setting(bp, true, false);
8574 static void bnxt_get_port_module_status(struct bnxt *bp)
8576 struct bnxt_link_info *link_info = &bp->link_info;
8577 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
8580 if (bnxt_update_link(bp, true))
8583 module_status = link_info->module_status;
8584 switch (module_status) {
8585 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
8586 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
8587 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
8588 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
8590 if (bp->hwrm_spec_code >= 0x10201) {
8591 netdev_warn(bp->dev, "Module part number %s\n",
8592 resp->phy_vendor_partnumber);
8594 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
8595 netdev_warn(bp->dev, "TX is disabled\n");
8596 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
8597 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
8602 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
8604 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
8605 if (bp->hwrm_spec_code >= 0x10201)
8607 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
8608 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8609 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
8610 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8611 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
8613 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
8615 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8616 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
8617 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8618 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
8620 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
8621 if (bp->hwrm_spec_code >= 0x10201) {
8622 req->auto_pause = req->force_pause;
8623 req->enables |= cpu_to_le32(
8624 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
8629 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
8630 struct hwrm_port_phy_cfg_input *req)
8632 u8 autoneg = bp->link_info.autoneg;
8633 u16 fw_link_speed = bp->link_info.req_link_speed;
8634 u16 advertising = bp->link_info.advertising;
8636 if (autoneg & BNXT_AUTONEG_SPEED) {
8638 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
8640 req->enables |= cpu_to_le32(
8641 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
8642 req->auto_link_speed_mask = cpu_to_le16(advertising);
8644 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
8646 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
8648 req->force_link_speed = cpu_to_le16(fw_link_speed);
8649 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
8652 /* tell chimp that the setting takes effect immediately */
8653 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
8656 int bnxt_hwrm_set_pause(struct bnxt *bp)
8658 struct hwrm_port_phy_cfg_input req = {0};
8661 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8662 bnxt_hwrm_set_pause_common(bp, &req);
8664 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
8665 bp->link_info.force_link_chng)
8666 bnxt_hwrm_set_link_common(bp, &req);
8668 mutex_lock(&bp->hwrm_cmd_lock);
8669 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8670 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
8671 /* since changing of pause setting doesn't trigger any link
8672 * change event, the driver needs to update the current pause
8673 * result upon successfully return of the phy_cfg command
8675 bp->link_info.pause =
8676 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
8677 bp->link_info.auto_pause_setting = 0;
8678 if (!bp->link_info.force_link_chng)
8679 bnxt_report_link(bp);
8681 bp->link_info.force_link_chng = false;
8682 mutex_unlock(&bp->hwrm_cmd_lock);
8686 static void bnxt_hwrm_set_eee(struct bnxt *bp,
8687 struct hwrm_port_phy_cfg_input *req)
8689 struct ethtool_eee *eee = &bp->eee;
8691 if (eee->eee_enabled) {
8693 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
8695 if (eee->tx_lpi_enabled)
8696 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
8698 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
8700 req->flags |= cpu_to_le32(flags);
8701 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
8702 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
8703 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
8705 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
8709 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
8711 struct hwrm_port_phy_cfg_input req = {0};
8713 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8715 bnxt_hwrm_set_pause_common(bp, &req);
8717 bnxt_hwrm_set_link_common(bp, &req);
8720 bnxt_hwrm_set_eee(bp, &req);
8721 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8724 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
8726 struct hwrm_port_phy_cfg_input req = {0};
8728 if (!BNXT_SINGLE_PF(bp))
8731 if (pci_num_vf(bp->pdev))
8734 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8735 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
8736 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8739 static int bnxt_fw_init_one(struct bnxt *bp);
8741 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
8743 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
8744 struct hwrm_func_drv_if_change_input req = {0};
8745 bool resc_reinit = false, fw_reset = false;
8749 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
8752 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
8754 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
8755 mutex_lock(&bp->hwrm_cmd_lock);
8756 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8758 flags = le32_to_cpu(resp->flags);
8759 mutex_unlock(&bp->hwrm_cmd_lock);
8766 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
8768 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
8771 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
8772 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
8775 if (resc_reinit || fw_reset) {
8777 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
8779 rc = bnxt_fw_init_one(bp);
8781 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
8784 bnxt_clear_int_mode(bp);
8785 rc = bnxt_init_int_mode(bp);
8787 netdev_err(bp->dev, "init int mode failed\n");
8790 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
8792 if (BNXT_NEW_RM(bp)) {
8793 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8795 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
8796 hw_resc->resv_cp_rings = 0;
8797 hw_resc->resv_stat_ctxs = 0;
8798 hw_resc->resv_irqs = 0;
8799 hw_resc->resv_tx_rings = 0;
8800 hw_resc->resv_rx_rings = 0;
8801 hw_resc->resv_hw_ring_grps = 0;
8802 hw_resc->resv_vnics = 0;
8804 bp->tx_nr_rings = 0;
8805 bp->rx_nr_rings = 0;
8812 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
8814 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8815 struct hwrm_port_led_qcaps_input req = {0};
8816 struct bnxt_pf_info *pf = &bp->pf;
8820 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
8823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
8824 req.port_id = cpu_to_le16(pf->port_id);
8825 mutex_lock(&bp->hwrm_cmd_lock);
8826 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8828 mutex_unlock(&bp->hwrm_cmd_lock);
8831 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
8834 bp->num_leds = resp->num_leds;
8835 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
8837 for (i = 0; i < bp->num_leds; i++) {
8838 struct bnxt_led_info *led = &bp->leds[i];
8839 __le16 caps = led->led_state_caps;
8841 if (!led->led_group_id ||
8842 !BNXT_LED_ALT_BLINK_CAP(caps)) {
8848 mutex_unlock(&bp->hwrm_cmd_lock);
8852 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
8854 struct hwrm_wol_filter_alloc_input req = {0};
8855 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
8858 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
8859 req.port_id = cpu_to_le16(bp->pf.port_id);
8860 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
8861 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
8862 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
8863 mutex_lock(&bp->hwrm_cmd_lock);
8864 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8866 bp->wol_filter_id = resp->wol_filter_id;
8867 mutex_unlock(&bp->hwrm_cmd_lock);
8871 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
8873 struct hwrm_wol_filter_free_input req = {0};
8876 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
8877 req.port_id = cpu_to_le16(bp->pf.port_id);
8878 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
8879 req.wol_filter_id = bp->wol_filter_id;
8880 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8884 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
8886 struct hwrm_wol_filter_qcfg_input req = {0};
8887 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8888 u16 next_handle = 0;
8891 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
8892 req.port_id = cpu_to_le16(bp->pf.port_id);
8893 req.handle = cpu_to_le16(handle);
8894 mutex_lock(&bp->hwrm_cmd_lock);
8895 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8897 next_handle = le16_to_cpu(resp->next_handle);
8898 if (next_handle != 0) {
8899 if (resp->wol_type ==
8900 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
8902 bp->wol_filter_id = resp->wol_filter_id;
8906 mutex_unlock(&bp->hwrm_cmd_lock);
8910 static void bnxt_get_wol_settings(struct bnxt *bp)
8915 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
8919 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
8920 } while (handle && handle != 0xffff);
8923 #ifdef CONFIG_BNXT_HWMON
8924 static ssize_t bnxt_show_temp(struct device *dev,
8925 struct device_attribute *devattr, char *buf)
8927 struct hwrm_temp_monitor_query_input req = {0};
8928 struct hwrm_temp_monitor_query_output *resp;
8929 struct bnxt *bp = dev_get_drvdata(dev);
8932 resp = bp->hwrm_cmd_resp_addr;
8933 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
8934 mutex_lock(&bp->hwrm_cmd_lock);
8935 if (!_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
8936 temp = resp->temp * 1000; /* display millidegree */
8937 mutex_unlock(&bp->hwrm_cmd_lock);
8939 return sprintf(buf, "%u\n", temp);
8941 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
8943 static struct attribute *bnxt_attrs[] = {
8944 &sensor_dev_attr_temp1_input.dev_attr.attr,
8947 ATTRIBUTE_GROUPS(bnxt);
8949 static void bnxt_hwmon_close(struct bnxt *bp)
8951 if (bp->hwmon_dev) {
8952 hwmon_device_unregister(bp->hwmon_dev);
8953 bp->hwmon_dev = NULL;
8957 static void bnxt_hwmon_open(struct bnxt *bp)
8959 struct pci_dev *pdev = bp->pdev;
8964 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
8965 DRV_MODULE_NAME, bp,
8967 if (IS_ERR(bp->hwmon_dev)) {
8968 bp->hwmon_dev = NULL;
8969 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
8973 static void bnxt_hwmon_close(struct bnxt *bp)
8977 static void bnxt_hwmon_open(struct bnxt *bp)
8982 static bool bnxt_eee_config_ok(struct bnxt *bp)
8984 struct ethtool_eee *eee = &bp->eee;
8985 struct bnxt_link_info *link_info = &bp->link_info;
8987 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
8990 if (eee->eee_enabled) {
8992 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
8994 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8995 eee->eee_enabled = 0;
8998 if (eee->advertised & ~advertising) {
8999 eee->advertised = advertising & eee->supported;
9006 static int bnxt_update_phy_setting(struct bnxt *bp)
9009 bool update_link = false;
9010 bool update_pause = false;
9011 bool update_eee = false;
9012 struct bnxt_link_info *link_info = &bp->link_info;
9014 rc = bnxt_update_link(bp, true);
9016 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
9020 if (!BNXT_SINGLE_PF(bp))
9023 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9024 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
9025 link_info->req_flow_ctrl)
9026 update_pause = true;
9027 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9028 link_info->force_pause_setting != link_info->req_flow_ctrl)
9029 update_pause = true;
9030 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9031 if (BNXT_AUTO_MODE(link_info->auto_mode))
9033 if (link_info->req_link_speed != link_info->force_link_speed)
9035 if (link_info->req_duplex != link_info->duplex_setting)
9038 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
9040 if (link_info->advertising != link_info->auto_link_speeds)
9044 /* The last close may have shutdown the link, so need to call
9045 * PHY_CFG to bring it back up.
9047 if (!netif_carrier_ok(bp->dev))
9050 if (!bnxt_eee_config_ok(bp))
9054 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
9055 else if (update_pause)
9056 rc = bnxt_hwrm_set_pause(bp);
9058 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
9066 /* Common routine to pre-map certain register block to different GRC window.
9067 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
9068 * in PF and 3 windows in VF that can be customized to map in different
9071 static void bnxt_preset_reg_win(struct bnxt *bp)
9074 /* CAG registers map to GRC window #4 */
9075 writel(BNXT_CAG_REG_BASE,
9076 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
9080 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
9082 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9086 bnxt_preset_reg_win(bp);
9087 netif_carrier_off(bp->dev);
9089 /* Reserve rings now if none were reserved at driver probe. */
9090 rc = bnxt_init_dflt_ring_mode(bp);
9092 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
9096 rc = bnxt_reserve_rings(bp, irq_re_init);
9099 if ((bp->flags & BNXT_FLAG_RFS) &&
9100 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
9101 /* disable RFS if falling back to INTA */
9102 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
9103 bp->flags &= ~BNXT_FLAG_RFS;
9106 rc = bnxt_alloc_mem(bp, irq_re_init);
9108 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9109 goto open_err_free_mem;
9114 rc = bnxt_request_irq(bp);
9116 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
9121 bnxt_enable_napi(bp);
9122 bnxt_debug_dev_init(bp);
9124 rc = bnxt_init_nic(bp, irq_re_init);
9126 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9131 mutex_lock(&bp->link_lock);
9132 rc = bnxt_update_phy_setting(bp);
9133 mutex_unlock(&bp->link_lock);
9135 netdev_warn(bp->dev, "failed to update phy settings\n");
9136 if (BNXT_SINGLE_PF(bp)) {
9137 bp->link_info.phy_retry = true;
9138 bp->link_info.phy_retry_expires =
9145 udp_tunnel_get_rx_info(bp->dev);
9147 set_bit(BNXT_STATE_OPEN, &bp->state);
9148 bnxt_enable_int(bp);
9149 /* Enable TX queues */
9151 mod_timer(&bp->timer, jiffies + bp->current_interval);
9152 /* Poll link status and check for SFP+ module status */
9153 bnxt_get_port_module_status(bp);
9155 /* VF-reps may need to be re-opened after the PF is re-opened */
9157 bnxt_vf_reps_open(bp);
9161 bnxt_debug_dev_exit(bp);
9162 bnxt_disable_napi(bp);
9170 bnxt_free_mem(bp, true);
9174 /* rtnl_lock held */
9175 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9179 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
9181 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
9187 /* rtnl_lock held, open the NIC half way by allocating all resources, but
9188 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
9191 int bnxt_half_open_nic(struct bnxt *bp)
9195 rc = bnxt_alloc_mem(bp, false);
9197 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9200 rc = bnxt_init_nic(bp, false);
9202 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9209 bnxt_free_mem(bp, false);
9214 /* rtnl_lock held, this call can only be made after a previous successful
9215 * call to bnxt_half_open_nic().
9217 void bnxt_half_close_nic(struct bnxt *bp)
9219 bnxt_hwrm_resource_free(bp, false, false);
9221 bnxt_free_mem(bp, false);
9224 static int bnxt_open(struct net_device *dev)
9226 struct bnxt *bp = netdev_priv(dev);
9229 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9230 netdev_err(bp->dev, "A previous firmware reset did not complete, aborting\n");
9234 rc = bnxt_hwrm_if_change(bp, true);
9237 rc = __bnxt_open_nic(bp, true, true);
9239 bnxt_hwrm_if_change(bp, false);
9241 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
9243 struct bnxt_pf_info *pf = &bp->pf;
9244 int n = pf->active_vfs;
9247 bnxt_cfg_hw_sriov(bp, &n, true);
9249 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9250 bnxt_ulp_start(bp, 0);
9252 bnxt_hwmon_open(bp);
9258 static bool bnxt_drv_busy(struct bnxt *bp)
9260 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
9261 test_bit(BNXT_STATE_READ_STATS, &bp->state));
9264 static void bnxt_get_ring_stats(struct bnxt *bp,
9265 struct rtnl_link_stats64 *stats);
9267 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
9270 /* Close the VF-reps before closing PF */
9272 bnxt_vf_reps_close(bp);
9274 /* Change device state to avoid TX queue wake up's */
9275 bnxt_tx_disable(bp);
9277 clear_bit(BNXT_STATE_OPEN, &bp->state);
9278 smp_mb__after_atomic();
9279 while (bnxt_drv_busy(bp))
9282 /* Flush rings and and disable interrupts */
9283 bnxt_shutdown_nic(bp, irq_re_init);
9285 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
9287 bnxt_debug_dev_exit(bp);
9288 bnxt_disable_napi(bp);
9289 del_timer_sync(&bp->timer);
9290 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) &&
9291 pci_is_enabled(bp->pdev))
9292 pci_disable_device(bp->pdev);
9296 /* Save ring stats before shutdown */
9298 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
9303 bnxt_free_mem(bp, irq_re_init);
9306 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9310 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9311 /* If we get here, it means firmware reset is in progress
9312 * while we are trying to close. We can safely proceed with
9313 * the close because we are holding rtnl_lock(). Some firmware
9314 * messages may fail as we proceed to close. We set the
9315 * ABORT_ERR flag here so that the FW reset thread will later
9316 * abort when it gets the rtnl_lock() and sees the flag.
9318 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
9319 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9322 #ifdef CONFIG_BNXT_SRIOV
9323 if (bp->sriov_cfg) {
9324 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
9326 BNXT_SRIOV_CFG_WAIT_TMO);
9328 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
9331 __bnxt_close_nic(bp, irq_re_init, link_re_init);
9335 static int bnxt_close(struct net_device *dev)
9337 struct bnxt *bp = netdev_priv(dev);
9339 bnxt_hwmon_close(bp);
9340 bnxt_close_nic(bp, true, true);
9341 bnxt_hwrm_shutdown_link(bp);
9342 bnxt_hwrm_if_change(bp, false);
9346 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
9349 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
9350 struct hwrm_port_phy_mdio_read_input req = {0};
9353 if (bp->hwrm_spec_code < 0x10a00)
9356 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
9357 req.port_id = cpu_to_le16(bp->pf.port_id);
9358 req.phy_addr = phy_addr;
9359 req.reg_addr = cpu_to_le16(reg & 0x1f);
9360 if (mdio_phy_id_is_c45(phy_addr)) {
9362 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9363 req.dev_addr = mdio_phy_id_devad(phy_addr);
9364 req.reg_addr = cpu_to_le16(reg);
9367 mutex_lock(&bp->hwrm_cmd_lock);
9368 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9370 *val = le16_to_cpu(resp->reg_data);
9371 mutex_unlock(&bp->hwrm_cmd_lock);
9375 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
9378 struct hwrm_port_phy_mdio_write_input req = {0};
9380 if (bp->hwrm_spec_code < 0x10a00)
9383 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
9384 req.port_id = cpu_to_le16(bp->pf.port_id);
9385 req.phy_addr = phy_addr;
9386 req.reg_addr = cpu_to_le16(reg & 0x1f);
9387 if (mdio_phy_id_is_c45(phy_addr)) {
9389 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9390 req.dev_addr = mdio_phy_id_devad(phy_addr);
9391 req.reg_addr = cpu_to_le16(reg);
9393 req.reg_data = cpu_to_le16(val);
9395 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9398 /* rtnl_lock held */
9399 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9401 struct mii_ioctl_data *mdio = if_mii(ifr);
9402 struct bnxt *bp = netdev_priv(dev);
9407 mdio->phy_id = bp->link_info.phy_addr;
9413 if (!netif_running(dev))
9416 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
9418 mdio->val_out = mii_regval;
9423 if (!netif_running(dev))
9426 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
9436 static void bnxt_get_ring_stats(struct bnxt *bp,
9437 struct rtnl_link_stats64 *stats)
9442 for (i = 0; i < bp->cp_nr_rings; i++) {
9443 struct bnxt_napi *bnapi = bp->bnapi[i];
9444 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9445 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
9447 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
9448 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
9449 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
9451 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
9452 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
9453 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
9455 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
9456 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
9457 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
9459 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
9460 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
9461 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
9463 stats->rx_missed_errors +=
9464 le64_to_cpu(hw_stats->rx_discard_pkts);
9466 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
9468 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
9472 static void bnxt_add_prev_stats(struct bnxt *bp,
9473 struct rtnl_link_stats64 *stats)
9475 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
9477 stats->rx_packets += prev_stats->rx_packets;
9478 stats->tx_packets += prev_stats->tx_packets;
9479 stats->rx_bytes += prev_stats->rx_bytes;
9480 stats->tx_bytes += prev_stats->tx_bytes;
9481 stats->rx_missed_errors += prev_stats->rx_missed_errors;
9482 stats->multicast += prev_stats->multicast;
9483 stats->tx_dropped += prev_stats->tx_dropped;
9487 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
9489 struct bnxt *bp = netdev_priv(dev);
9491 set_bit(BNXT_STATE_READ_STATS, &bp->state);
9492 /* Make sure bnxt_close_nic() sees that we are reading stats before
9493 * we check the BNXT_STATE_OPEN flag.
9495 smp_mb__after_atomic();
9496 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9497 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9498 *stats = bp->net_stats_prev;
9502 bnxt_get_ring_stats(bp, stats);
9503 bnxt_add_prev_stats(bp, stats);
9505 if (bp->flags & BNXT_FLAG_PORT_STATS) {
9506 struct rx_port_stats *rx = bp->hw_rx_port_stats;
9507 struct tx_port_stats *tx = bp->hw_tx_port_stats;
9509 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
9510 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
9511 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
9512 le64_to_cpu(rx->rx_ovrsz_frames) +
9513 le64_to_cpu(rx->rx_runt_frames);
9514 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
9515 le64_to_cpu(rx->rx_jbr_frames);
9516 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
9517 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
9518 stats->tx_errors = le64_to_cpu(tx->tx_err);
9520 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9523 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
9525 struct net_device *dev = bp->dev;
9526 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9527 struct netdev_hw_addr *ha;
9530 bool update = false;
9533 netdev_for_each_mc_addr(ha, dev) {
9534 if (mc_count >= BNXT_MAX_MC_ADDRS) {
9535 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9536 vnic->mc_list_count = 0;
9540 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
9541 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
9548 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
9550 if (mc_count != vnic->mc_list_count) {
9551 vnic->mc_list_count = mc_count;
9557 static bool bnxt_uc_list_updated(struct bnxt *bp)
9559 struct net_device *dev = bp->dev;
9560 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9561 struct netdev_hw_addr *ha;
9564 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
9567 netdev_for_each_uc_addr(ha, dev) {
9568 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
9576 static void bnxt_set_rx_mode(struct net_device *dev)
9578 struct bnxt *bp = netdev_priv(dev);
9579 struct bnxt_vnic_info *vnic;
9580 bool mc_update = false;
9584 if (!test_bit(BNXT_STATE_OPEN, &bp->state))
9587 vnic = &bp->vnic_info[0];
9588 mask = vnic->rx_mask;
9589 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
9590 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
9591 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
9592 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
9594 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
9595 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
9597 uc_update = bnxt_uc_list_updated(bp);
9599 if (dev->flags & IFF_BROADCAST)
9600 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
9601 if (dev->flags & IFF_ALLMULTI) {
9602 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9603 vnic->mc_list_count = 0;
9605 mc_update = bnxt_mc_list_updated(bp, &mask);
9608 if (mask != vnic->rx_mask || uc_update || mc_update) {
9609 vnic->rx_mask = mask;
9611 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
9612 bnxt_queue_sp_work(bp);
9616 static int bnxt_cfg_rx_mode(struct bnxt *bp)
9618 struct net_device *dev = bp->dev;
9619 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9620 struct netdev_hw_addr *ha;
9624 netif_addr_lock_bh(dev);
9625 uc_update = bnxt_uc_list_updated(bp);
9626 netif_addr_unlock_bh(dev);
9631 mutex_lock(&bp->hwrm_cmd_lock);
9632 for (i = 1; i < vnic->uc_filter_count; i++) {
9633 struct hwrm_cfa_l2_filter_free_input req = {0};
9635 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
9638 req.l2_filter_id = vnic->fw_l2_filter_id[i];
9640 rc = _hwrm_send_message(bp, &req, sizeof(req),
9643 mutex_unlock(&bp->hwrm_cmd_lock);
9645 vnic->uc_filter_count = 1;
9647 netif_addr_lock_bh(dev);
9648 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
9649 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
9651 netdev_for_each_uc_addr(ha, dev) {
9652 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
9654 vnic->uc_filter_count++;
9657 netif_addr_unlock_bh(dev);
9659 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
9660 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
9662 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
9664 vnic->uc_filter_count = i;
9670 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
9671 if (rc && vnic->mc_list_count) {
9672 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
9674 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9675 vnic->mc_list_count = 0;
9676 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
9679 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
9685 static bool bnxt_can_reserve_rings(struct bnxt *bp)
9687 #ifdef CONFIG_BNXT_SRIOV
9688 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
9689 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9691 /* No minimum rings were provisioned by the PF. Don't
9692 * reserve rings by default when device is down.
9694 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
9697 if (!netif_running(bp->dev))
9704 /* If the chip and firmware supports RFS */
9705 static bool bnxt_rfs_supported(struct bnxt *bp)
9707 if (bp->flags & BNXT_FLAG_CHIP_P5) {
9708 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
9712 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
9714 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
9719 /* If runtime conditions support RFS */
9720 static bool bnxt_rfs_capable(struct bnxt *bp)
9722 #ifdef CONFIG_RFS_ACCEL
9723 int vnics, max_vnics, max_rss_ctxs;
9725 if (bp->flags & BNXT_FLAG_CHIP_P5)
9726 return bnxt_rfs_supported(bp);
9727 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
9730 vnics = 1 + bp->rx_nr_rings;
9731 max_vnics = bnxt_get_max_func_vnics(bp);
9732 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
9734 /* RSS contexts not a limiting factor */
9735 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
9736 max_rss_ctxs = max_vnics;
9737 if (vnics > max_vnics || vnics > max_rss_ctxs) {
9738 if (bp->rx_nr_rings > 1)
9739 netdev_warn(bp->dev,
9740 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
9741 min(max_rss_ctxs - 1, max_vnics - 1));
9745 if (!BNXT_NEW_RM(bp))
9748 if (vnics == bp->hw_resc.resv_vnics)
9751 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
9752 if (vnics <= bp->hw_resc.resv_vnics)
9755 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
9756 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
9763 static netdev_features_t bnxt_fix_features(struct net_device *dev,
9764 netdev_features_t features)
9766 struct bnxt *bp = netdev_priv(dev);
9768 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
9769 features &= ~NETIF_F_NTUPLE;
9771 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9772 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
9774 if (!(features & NETIF_F_GRO))
9775 features &= ~NETIF_F_GRO_HW;
9777 if (features & NETIF_F_GRO_HW)
9778 features &= ~NETIF_F_LRO;
9780 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
9781 * turned on or off together.
9783 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
9784 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
9785 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
9786 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9787 NETIF_F_HW_VLAN_STAG_RX);
9789 features |= NETIF_F_HW_VLAN_CTAG_RX |
9790 NETIF_F_HW_VLAN_STAG_RX;
9792 #ifdef CONFIG_BNXT_SRIOV
9795 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9796 NETIF_F_HW_VLAN_STAG_RX);
9803 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
9805 struct bnxt *bp = netdev_priv(dev);
9806 u32 flags = bp->flags;
9809 bool re_init = false;
9810 bool update_tpa = false;
9812 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
9813 if (features & NETIF_F_GRO_HW)
9814 flags |= BNXT_FLAG_GRO;
9815 else if (features & NETIF_F_LRO)
9816 flags |= BNXT_FLAG_LRO;
9818 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9819 flags &= ~BNXT_FLAG_TPA;
9821 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9822 flags |= BNXT_FLAG_STRIP_VLAN;
9824 if (features & NETIF_F_NTUPLE)
9825 flags |= BNXT_FLAG_RFS;
9827 changes = flags ^ bp->flags;
9828 if (changes & BNXT_FLAG_TPA) {
9830 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
9831 (flags & BNXT_FLAG_TPA) == 0 ||
9832 (bp->flags & BNXT_FLAG_CHIP_P5))
9836 if (changes & ~BNXT_FLAG_TPA)
9839 if (flags != bp->flags) {
9840 u32 old_flags = bp->flags;
9842 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9845 bnxt_set_ring_params(bp);
9850 bnxt_close_nic(bp, false, false);
9853 bnxt_set_ring_params(bp);
9855 return bnxt_open_nic(bp, false, false);
9859 rc = bnxt_set_tpa(bp,
9860 (flags & BNXT_FLAG_TPA) ?
9863 bp->flags = old_flags;
9869 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
9870 u32 ring_id, u32 *prod, u32 *cons)
9872 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
9873 struct hwrm_dbg_ring_info_get_input req = {0};
9876 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
9877 req.ring_type = ring_type;
9878 req.fw_ring_id = cpu_to_le32(ring_id);
9879 mutex_lock(&bp->hwrm_cmd_lock);
9880 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9882 *prod = le32_to_cpu(resp->producer_index);
9883 *cons = le32_to_cpu(resp->consumer_index);
9885 mutex_unlock(&bp->hwrm_cmd_lock);
9889 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
9891 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
9892 int i = bnapi->index;
9897 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
9898 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
9902 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
9904 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
9905 int i = bnapi->index;
9910 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",
9911 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
9912 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
9913 rxr->rx_sw_agg_prod);
9916 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
9918 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9919 int i = bnapi->index;
9921 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
9922 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
9925 static void bnxt_dbg_dump_states(struct bnxt *bp)
9928 struct bnxt_napi *bnapi;
9930 for (i = 0; i < bp->cp_nr_rings; i++) {
9931 bnapi = bp->bnapi[i];
9932 if (netif_msg_drv(bp)) {
9933 bnxt_dump_tx_sw_state(bnapi);
9934 bnxt_dump_rx_sw_state(bnapi);
9935 bnxt_dump_cp_sw_state(bnapi);
9940 static void bnxt_reset_task(struct bnxt *bp, bool silent)
9943 bnxt_dbg_dump_states(bp);
9944 if (netif_running(bp->dev)) {
9948 bnxt_close_nic(bp, false, false);
9949 bnxt_open_nic(bp, false, false);
9952 bnxt_close_nic(bp, true, false);
9953 rc = bnxt_open_nic(bp, true, false);
9954 bnxt_ulp_start(bp, rc);
9959 static void bnxt_tx_timeout(struct net_device *dev)
9961 struct bnxt *bp = netdev_priv(dev);
9963 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
9964 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
9965 bnxt_queue_sp_work(bp);
9968 static void bnxt_fw_health_check(struct bnxt *bp)
9970 struct bnxt_fw_health *fw_health = bp->fw_health;
9973 if (!fw_health || !fw_health->enabled ||
9974 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9977 if (fw_health->tmr_counter) {
9978 fw_health->tmr_counter--;
9982 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
9983 if (val == fw_health->last_fw_heartbeat)
9986 fw_health->last_fw_heartbeat = val;
9988 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
9989 if (val != fw_health->last_fw_reset_cnt)
9992 fw_health->tmr_counter = fw_health->tmr_multiplier;
9996 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
9997 bnxt_queue_sp_work(bp);
10000 static void bnxt_timer(struct timer_list *t)
10002 struct bnxt *bp = from_timer(bp, t, timer);
10003 struct net_device *dev = bp->dev;
10005 if (!netif_running(dev))
10008 if (atomic_read(&bp->intr_sem) != 0)
10009 goto bnxt_restart_timer;
10011 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
10012 bnxt_fw_health_check(bp);
10014 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
10015 bp->stats_coal_ticks) {
10016 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
10017 bnxt_queue_sp_work(bp);
10020 if (bnxt_tc_flower_enabled(bp)) {
10021 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
10022 bnxt_queue_sp_work(bp);
10025 if (bp->link_info.phy_retry) {
10026 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
10027 bp->link_info.phy_retry = false;
10028 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
10030 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
10031 bnxt_queue_sp_work(bp);
10035 if ((bp->flags & BNXT_FLAG_CHIP_P5) && netif_carrier_ok(dev)) {
10036 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
10037 bnxt_queue_sp_work(bp);
10039 bnxt_restart_timer:
10040 mod_timer(&bp->timer, jiffies + bp->current_interval);
10043 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
10045 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
10046 * set. If the device is being closed, bnxt_close() may be holding
10047 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
10048 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
10050 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10054 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
10056 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10060 /* Only called from bnxt_sp_task() */
10061 static void bnxt_reset(struct bnxt *bp, bool silent)
10063 bnxt_rtnl_lock_sp(bp);
10064 if (test_bit(BNXT_STATE_OPEN, &bp->state))
10065 bnxt_reset_task(bp, silent);
10066 bnxt_rtnl_unlock_sp(bp);
10069 static void bnxt_fw_reset_close(struct bnxt *bp)
10072 __bnxt_close_nic(bp, true, false);
10073 bnxt_clear_int_mode(bp);
10074 bnxt_hwrm_func_drv_unrgtr(bp);
10075 bnxt_free_ctx_mem(bp);
10080 static bool is_bnxt_fw_ok(struct bnxt *bp)
10082 struct bnxt_fw_health *fw_health = bp->fw_health;
10083 bool no_heartbeat = false, has_reset = false;
10086 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10087 if (val == fw_health->last_fw_heartbeat)
10088 no_heartbeat = true;
10090 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10091 if (val != fw_health->last_fw_reset_cnt)
10094 if (!no_heartbeat && has_reset)
10100 /* rtnl_lock is acquired before calling this function */
10101 static void bnxt_force_fw_reset(struct bnxt *bp)
10103 struct bnxt_fw_health *fw_health = bp->fw_health;
10106 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
10107 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10110 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10111 bnxt_fw_reset_close(bp);
10112 wait_dsecs = fw_health->master_func_wait_dsecs;
10113 if (fw_health->master) {
10114 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
10116 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10118 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
10119 wait_dsecs = fw_health->normal_func_wait_dsecs;
10120 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10123 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
10124 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
10125 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10128 void bnxt_fw_exception(struct bnxt *bp)
10130 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
10131 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10132 bnxt_rtnl_lock_sp(bp);
10133 bnxt_force_fw_reset(bp);
10134 bnxt_rtnl_unlock_sp(bp);
10137 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
10140 static int bnxt_get_registered_vfs(struct bnxt *bp)
10142 #ifdef CONFIG_BNXT_SRIOV
10148 rc = bnxt_hwrm_func_qcfg(bp);
10150 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
10153 if (bp->pf.registered_vfs)
10154 return bp->pf.registered_vfs;
10161 void bnxt_fw_reset(struct bnxt *bp)
10163 bnxt_rtnl_lock_sp(bp);
10164 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
10165 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10168 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10169 if (bp->pf.active_vfs &&
10170 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10171 n = bnxt_get_registered_vfs(bp);
10173 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
10175 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10176 dev_close(bp->dev);
10177 goto fw_reset_exit;
10178 } else if (n > 0) {
10179 u16 vf_tmo_dsecs = n * 10;
10181 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
10182 bp->fw_reset_max_dsecs = vf_tmo_dsecs;
10183 bp->fw_reset_state =
10184 BNXT_FW_RESET_STATE_POLL_VF;
10185 bnxt_queue_fw_reset_work(bp, HZ / 10);
10186 goto fw_reset_exit;
10188 bnxt_fw_reset_close(bp);
10189 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
10190 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
10193 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10194 tmo = bp->fw_reset_min_dsecs * HZ / 10;
10196 bnxt_queue_fw_reset_work(bp, tmo);
10199 bnxt_rtnl_unlock_sp(bp);
10202 static void bnxt_chk_missed_irq(struct bnxt *bp)
10206 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
10209 for (i = 0; i < bp->cp_nr_rings; i++) {
10210 struct bnxt_napi *bnapi = bp->bnapi[i];
10211 struct bnxt_cp_ring_info *cpr;
10218 cpr = &bnapi->cp_ring;
10219 for (j = 0; j < 2; j++) {
10220 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
10223 if (!cpr2 || cpr2->has_more_work ||
10224 !bnxt_has_work(bp, cpr2))
10227 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
10228 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
10231 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
10232 bnxt_dbg_hwrm_ring_info_get(bp,
10233 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
10234 fw_ring_id, &val[0], &val[1]);
10235 cpr->missed_irqs++;
10240 static void bnxt_cfg_ntp_filters(struct bnxt *);
10242 static void bnxt_sp_task(struct work_struct *work)
10244 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
10246 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10247 smp_mb__after_atomic();
10248 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10249 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10253 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
10254 bnxt_cfg_rx_mode(bp);
10256 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
10257 bnxt_cfg_ntp_filters(bp);
10258 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
10259 bnxt_hwrm_exec_fwd_req(bp);
10260 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
10261 bnxt_hwrm_tunnel_dst_port_alloc(
10262 bp, bp->vxlan_port,
10263 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10265 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
10266 bnxt_hwrm_tunnel_dst_port_free(
10267 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10269 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
10270 bnxt_hwrm_tunnel_dst_port_alloc(
10272 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10274 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
10275 bnxt_hwrm_tunnel_dst_port_free(
10276 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10278 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
10279 bnxt_hwrm_port_qstats(bp);
10280 bnxt_hwrm_port_qstats_ext(bp);
10281 bnxt_hwrm_pcie_qstats(bp);
10284 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
10287 mutex_lock(&bp->link_lock);
10288 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
10290 bnxt_hwrm_phy_qcaps(bp);
10292 rc = bnxt_update_link(bp, true);
10293 mutex_unlock(&bp->link_lock);
10295 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
10298 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
10301 mutex_lock(&bp->link_lock);
10302 rc = bnxt_update_phy_setting(bp);
10303 mutex_unlock(&bp->link_lock);
10305 netdev_warn(bp->dev, "update phy settings retry failed\n");
10307 bp->link_info.phy_retry = false;
10308 netdev_info(bp->dev, "update phy settings retry succeeded\n");
10311 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
10312 mutex_lock(&bp->link_lock);
10313 bnxt_get_port_module_status(bp);
10314 mutex_unlock(&bp->link_lock);
10317 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
10318 bnxt_tc_flow_stats_work(bp);
10320 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
10321 bnxt_chk_missed_irq(bp);
10323 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
10324 * must be the last functions to be called before exiting.
10326 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
10327 bnxt_reset(bp, false);
10329 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
10330 bnxt_reset(bp, true);
10332 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
10333 bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
10335 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
10336 if (!is_bnxt_fw_ok(bp))
10337 bnxt_devlink_health_report(bp,
10338 BNXT_FW_EXCEPTION_SP_EVENT);
10341 smp_mb__before_atomic();
10342 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10345 /* Under rtnl_lock */
10346 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
10349 int max_rx, max_tx, tx_sets = 1;
10350 int tx_rings_needed, stats;
10357 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
10364 tx_rings_needed = tx * tx_sets + tx_xdp;
10365 if (max_tx < tx_rings_needed)
10369 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
10372 if (bp->flags & BNXT_FLAG_AGG_RINGS)
10374 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
10376 if (BNXT_NEW_RM(bp)) {
10377 cp += bnxt_get_ulp_msix_num(bp);
10378 stats += bnxt_get_ulp_stat_ctxs(bp);
10380 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
10384 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
10387 pci_iounmap(pdev, bp->bar2);
10392 pci_iounmap(pdev, bp->bar1);
10397 pci_iounmap(pdev, bp->bar0);
10402 static void bnxt_cleanup_pci(struct bnxt *bp)
10404 bnxt_unmap_bars(bp, bp->pdev);
10405 pci_release_regions(bp->pdev);
10406 if (pci_is_enabled(bp->pdev))
10407 pci_disable_device(bp->pdev);
10410 static void bnxt_init_dflt_coal(struct bnxt *bp)
10412 struct bnxt_coal *coal;
10414 /* Tick values in micro seconds.
10415 * 1 coal_buf x bufs_per_record = 1 completion record.
10417 coal = &bp->rx_coal;
10418 coal->coal_ticks = 10;
10419 coal->coal_bufs = 30;
10420 coal->coal_ticks_irq = 1;
10421 coal->coal_bufs_irq = 2;
10422 coal->idle_thresh = 50;
10423 coal->bufs_per_record = 2;
10424 coal->budget = 64; /* NAPI budget */
10426 coal = &bp->tx_coal;
10427 coal->coal_ticks = 28;
10428 coal->coal_bufs = 30;
10429 coal->coal_ticks_irq = 2;
10430 coal->coal_bufs_irq = 2;
10431 coal->bufs_per_record = 1;
10433 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
10436 static int bnxt_fw_init_one_p1(struct bnxt *bp)
10441 rc = bnxt_hwrm_ver_get(bp);
10445 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
10446 rc = bnxt_alloc_kong_hwrm_resources(bp);
10448 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
10451 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
10452 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
10453 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
10457 rc = bnxt_hwrm_func_reset(bp);
10461 bnxt_hwrm_fw_set_time(bp);
10465 static int bnxt_fw_init_one_p2(struct bnxt *bp)
10469 /* Get the MAX capabilities for this function */
10470 rc = bnxt_hwrm_func_qcaps(bp);
10472 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
10477 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
10479 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
10482 rc = bnxt_hwrm_error_recovery_qcfg(bp);
10484 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
10487 rc = bnxt_hwrm_func_drv_rgtr(bp);
10491 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
10495 bnxt_hwrm_func_qcfg(bp);
10496 bnxt_hwrm_vnic_qcaps(bp);
10497 bnxt_hwrm_port_led_qcaps(bp);
10498 bnxt_ethtool_init(bp);
10503 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
10505 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
10506 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
10507 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
10508 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
10509 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
10510 if (BNXT_CHIP_P4(bp) && bp->hwrm_spec_code >= 0x10501) {
10511 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
10512 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
10513 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
10517 static void bnxt_set_dflt_rfs(struct bnxt *bp)
10519 struct net_device *dev = bp->dev;
10521 dev->hw_features &= ~NETIF_F_NTUPLE;
10522 dev->features &= ~NETIF_F_NTUPLE;
10523 bp->flags &= ~BNXT_FLAG_RFS;
10524 if (bnxt_rfs_supported(bp)) {
10525 dev->hw_features |= NETIF_F_NTUPLE;
10526 if (bnxt_rfs_capable(bp)) {
10527 bp->flags |= BNXT_FLAG_RFS;
10528 dev->features |= NETIF_F_NTUPLE;
10533 static void bnxt_fw_init_one_p3(struct bnxt *bp)
10535 struct pci_dev *pdev = bp->pdev;
10537 bnxt_set_dflt_rss_hash_type(bp);
10538 bnxt_set_dflt_rfs(bp);
10540 bnxt_get_wol_settings(bp);
10541 if (bp->flags & BNXT_FLAG_WOL_CAP)
10542 device_set_wakeup_enable(&pdev->dev, bp->wol);
10544 device_set_wakeup_capable(&pdev->dev, false);
10546 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
10547 bnxt_hwrm_coal_params_qcaps(bp);
10550 static int bnxt_fw_init_one(struct bnxt *bp)
10554 rc = bnxt_fw_init_one_p1(bp);
10556 netdev_err(bp->dev, "Firmware init phase 1 failed\n");
10559 rc = bnxt_fw_init_one_p2(bp);
10561 netdev_err(bp->dev, "Firmware init phase 2 failed\n");
10564 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
10567 bnxt_fw_init_one_p3(bp);
10571 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
10573 struct bnxt_fw_health *fw_health = bp->fw_health;
10574 u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
10575 u32 val = fw_health->fw_reset_seq_vals[reg_idx];
10576 u32 reg_type, reg_off, delay_msecs;
10578 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
10579 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
10580 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
10581 switch (reg_type) {
10582 case BNXT_FW_HEALTH_REG_TYPE_CFG:
10583 pci_write_config_dword(bp->pdev, reg_off, val);
10585 case BNXT_FW_HEALTH_REG_TYPE_GRC:
10586 writel(reg_off & BNXT_GRC_BASE_MASK,
10587 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
10588 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
10590 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
10591 writel(val, bp->bar0 + reg_off);
10593 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
10594 writel(val, bp->bar1 + reg_off);
10598 pci_read_config_dword(bp->pdev, 0, &val);
10599 msleep(delay_msecs);
10603 static void bnxt_reset_all(struct bnxt *bp)
10605 struct bnxt_fw_health *fw_health = bp->fw_health;
10608 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
10609 #ifdef CONFIG_TEE_BNXT_FW
10610 rc = tee_bnxt_fw_load();
10612 netdev_err(bp->dev, "Unable to reset FW rc=%d\n", rc);
10613 bp->fw_reset_timestamp = jiffies;
10618 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
10619 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
10620 bnxt_fw_reset_writel(bp, i);
10621 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
10622 struct hwrm_fw_reset_input req = {0};
10624 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
10625 req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
10626 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
10627 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
10628 req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
10629 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10631 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
10633 bp->fw_reset_timestamp = jiffies;
10636 static void bnxt_fw_reset_task(struct work_struct *work)
10638 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
10641 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10642 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
10646 switch (bp->fw_reset_state) {
10647 case BNXT_FW_RESET_STATE_POLL_VF: {
10648 int n = bnxt_get_registered_vfs(bp);
10652 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
10653 n, jiffies_to_msecs(jiffies -
10654 bp->fw_reset_timestamp));
10655 goto fw_reset_abort;
10656 } else if (n > 0) {
10657 if (time_after(jiffies, bp->fw_reset_timestamp +
10658 (bp->fw_reset_max_dsecs * HZ / 10))) {
10659 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10660 bp->fw_reset_state = 0;
10661 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
10665 bnxt_queue_fw_reset_work(bp, HZ / 10);
10668 bp->fw_reset_timestamp = jiffies;
10670 bnxt_fw_reset_close(bp);
10671 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
10672 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
10675 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10676 tmo = bp->fw_reset_min_dsecs * HZ / 10;
10679 bnxt_queue_fw_reset_work(bp, tmo);
10682 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
10685 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
10686 if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
10687 !time_after(jiffies, bp->fw_reset_timestamp +
10688 (bp->fw_reset_max_dsecs * HZ / 10))) {
10689 bnxt_queue_fw_reset_work(bp, HZ / 5);
10693 if (!bp->fw_health->master) {
10694 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
10696 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10697 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10700 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10703 case BNXT_FW_RESET_STATE_RESET_FW:
10704 bnxt_reset_all(bp);
10705 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10706 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
10708 case BNXT_FW_RESET_STATE_ENABLE_DEV:
10709 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
10713 val = bnxt_fw_health_readl(bp,
10714 BNXT_FW_RESET_INPROG_REG);
10716 netdev_warn(bp->dev, "FW reset inprog %x after min wait time.\n",
10719 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10720 if (pci_enable_device(bp->pdev)) {
10721 netdev_err(bp->dev, "Cannot re-enable PCI device\n");
10722 goto fw_reset_abort;
10724 pci_set_master(bp->pdev);
10725 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
10727 case BNXT_FW_RESET_STATE_POLL_FW:
10728 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
10729 rc = __bnxt_hwrm_ver_get(bp, true);
10731 if (time_after(jiffies, bp->fw_reset_timestamp +
10732 (bp->fw_reset_max_dsecs * HZ / 10))) {
10733 netdev_err(bp->dev, "Firmware reset aborted\n");
10734 goto fw_reset_abort;
10736 bnxt_queue_fw_reset_work(bp, HZ / 5);
10739 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10740 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
10742 case BNXT_FW_RESET_STATE_OPENING:
10743 while (!rtnl_trylock()) {
10744 bnxt_queue_fw_reset_work(bp, HZ / 10);
10747 rc = bnxt_open(bp->dev);
10749 netdev_err(bp->dev, "bnxt_open_nic() failed\n");
10750 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10751 dev_close(bp->dev);
10754 bp->fw_reset_state = 0;
10755 /* Make sure fw_reset_state is 0 before clearing the flag */
10756 smp_mb__before_atomic();
10757 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10758 bnxt_ulp_start(bp, rc);
10759 bnxt_dl_health_status_update(bp, true);
10766 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10767 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
10768 bnxt_dl_health_status_update(bp, false);
10769 bp->fw_reset_state = 0;
10771 dev_close(bp->dev);
10775 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
10778 struct bnxt *bp = netdev_priv(dev);
10780 SET_NETDEV_DEV(dev, &pdev->dev);
10782 /* enable device (incl. PCI PM wakeup), and bus-mastering */
10783 rc = pci_enable_device(pdev);
10785 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
10789 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
10790 dev_err(&pdev->dev,
10791 "Cannot find PCI device base address, aborting\n");
10793 goto init_err_disable;
10796 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
10798 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
10799 goto init_err_disable;
10802 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
10803 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
10804 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
10805 goto init_err_disable;
10808 pci_set_master(pdev);
10813 bp->bar0 = pci_ioremap_bar(pdev, 0);
10815 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
10817 goto init_err_release;
10820 bp->bar1 = pci_ioremap_bar(pdev, 2);
10822 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
10824 goto init_err_release;
10827 bp->bar2 = pci_ioremap_bar(pdev, 4);
10829 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
10831 goto init_err_release;
10834 pci_enable_pcie_error_reporting(pdev);
10836 INIT_WORK(&bp->sp_task, bnxt_sp_task);
10837 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
10839 spin_lock_init(&bp->ntp_fltr_lock);
10840 #if BITS_PER_LONG == 32
10841 spin_lock_init(&bp->db_lock);
10844 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
10845 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
10847 bnxt_init_dflt_coal(bp);
10849 timer_setup(&bp->timer, bnxt_timer, 0);
10850 bp->current_interval = BNXT_TIMER_INTERVAL;
10852 clear_bit(BNXT_STATE_OPEN, &bp->state);
10856 bnxt_unmap_bars(bp, pdev);
10857 pci_release_regions(pdev);
10860 pci_disable_device(pdev);
10866 /* rtnl_lock held */
10867 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
10869 struct sockaddr *addr = p;
10870 struct bnxt *bp = netdev_priv(dev);
10873 if (!is_valid_ether_addr(addr->sa_data))
10874 return -EADDRNOTAVAIL;
10876 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
10879 rc = bnxt_approve_mac(bp, addr->sa_data, true);
10883 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
10884 if (netif_running(dev)) {
10885 bnxt_close_nic(bp, false, false);
10886 rc = bnxt_open_nic(bp, false, false);
10892 /* rtnl_lock held */
10893 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
10895 struct bnxt *bp = netdev_priv(dev);
10897 if (netif_running(dev))
10898 bnxt_close_nic(bp, false, false);
10900 dev->mtu = new_mtu;
10901 bnxt_set_ring_params(bp);
10903 if (netif_running(dev))
10904 return bnxt_open_nic(bp, false, false);
10909 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
10911 struct bnxt *bp = netdev_priv(dev);
10915 if (tc > bp->max_tc) {
10916 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
10921 if (netdev_get_num_tc(dev) == tc)
10924 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10927 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
10928 sh, tc, bp->tx_nr_rings_xdp);
10932 /* Needs to close the device and do hw resource re-allocations */
10933 if (netif_running(bp->dev))
10934 bnxt_close_nic(bp, true, false);
10937 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
10938 netdev_set_num_tc(dev, tc);
10940 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10941 netdev_reset_tc(dev);
10943 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
10944 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
10945 bp->tx_nr_rings + bp->rx_nr_rings;
10947 if (netif_running(bp->dev))
10948 return bnxt_open_nic(bp, true, false);
10953 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
10956 struct bnxt *bp = cb_priv;
10958 if (!bnxt_tc_flower_enabled(bp) ||
10959 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
10960 return -EOPNOTSUPP;
10963 case TC_SETUP_CLSFLOWER:
10964 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
10966 return -EOPNOTSUPP;
10970 LIST_HEAD(bnxt_block_cb_list);
10972 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
10975 struct bnxt *bp = netdev_priv(dev);
10978 case TC_SETUP_BLOCK:
10979 return flow_block_cb_setup_simple(type_data,
10980 &bnxt_block_cb_list,
10981 bnxt_setup_tc_block_cb,
10983 case TC_SETUP_QDISC_MQPRIO: {
10984 struct tc_mqprio_qopt *mqprio = type_data;
10986 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
10988 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
10991 return -EOPNOTSUPP;
10995 #ifdef CONFIG_RFS_ACCEL
10996 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
10997 struct bnxt_ntuple_filter *f2)
10999 struct flow_keys *keys1 = &f1->fkeys;
11000 struct flow_keys *keys2 = &f2->fkeys;
11002 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
11003 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
11004 keys1->ports.ports == keys2->ports.ports &&
11005 keys1->basic.ip_proto == keys2->basic.ip_proto &&
11006 keys1->basic.n_proto == keys2->basic.n_proto &&
11007 keys1->control.flags == keys2->control.flags &&
11008 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
11009 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
11015 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
11016 u16 rxq_index, u32 flow_id)
11018 struct bnxt *bp = netdev_priv(dev);
11019 struct bnxt_ntuple_filter *fltr, *new_fltr;
11020 struct flow_keys *fkeys;
11021 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
11022 int rc = 0, idx, bit_id, l2_idx = 0;
11023 struct hlist_head *head;
11025 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
11026 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11029 netif_addr_lock_bh(dev);
11030 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
11031 if (ether_addr_equal(eth->h_dest,
11032 vnic->uc_list + off)) {
11037 netif_addr_unlock_bh(dev);
11041 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
11045 fkeys = &new_fltr->fkeys;
11046 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
11047 rc = -EPROTONOSUPPORT;
11051 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
11052 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
11053 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
11054 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
11055 rc = -EPROTONOSUPPORT;
11058 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
11059 bp->hwrm_spec_code < 0x10601) {
11060 rc = -EPROTONOSUPPORT;
11063 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
11064 bp->hwrm_spec_code < 0x10601) {
11065 rc = -EPROTONOSUPPORT;
11069 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
11070 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
11072 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
11073 head = &bp->ntp_fltr_hash_tbl[idx];
11075 hlist_for_each_entry_rcu(fltr, head, hash) {
11076 if (bnxt_fltr_match(fltr, new_fltr)) {
11084 spin_lock_bh(&bp->ntp_fltr_lock);
11085 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
11086 BNXT_NTP_FLTR_MAX_FLTR, 0);
11088 spin_unlock_bh(&bp->ntp_fltr_lock);
11093 new_fltr->sw_id = (u16)bit_id;
11094 new_fltr->flow_id = flow_id;
11095 new_fltr->l2_fltr_idx = l2_idx;
11096 new_fltr->rxq = rxq_index;
11097 hlist_add_head_rcu(&new_fltr->hash, head);
11098 bp->ntp_fltr_count++;
11099 spin_unlock_bh(&bp->ntp_fltr_lock);
11101 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
11102 bnxt_queue_sp_work(bp);
11104 return new_fltr->sw_id;
11111 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
11115 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
11116 struct hlist_head *head;
11117 struct hlist_node *tmp;
11118 struct bnxt_ntuple_filter *fltr;
11121 head = &bp->ntp_fltr_hash_tbl[i];
11122 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
11125 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
11126 if (rps_may_expire_flow(bp->dev, fltr->rxq,
11129 bnxt_hwrm_cfa_ntuple_filter_free(bp,
11134 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
11139 set_bit(BNXT_FLTR_VALID, &fltr->state);
11143 spin_lock_bh(&bp->ntp_fltr_lock);
11144 hlist_del_rcu(&fltr->hash);
11145 bp->ntp_fltr_count--;
11146 spin_unlock_bh(&bp->ntp_fltr_lock);
11148 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
11153 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
11154 netdev_info(bp->dev, "Receive PF driver unload event!");
11159 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
11163 #endif /* CONFIG_RFS_ACCEL */
11165 static void bnxt_udp_tunnel_add(struct net_device *dev,
11166 struct udp_tunnel_info *ti)
11168 struct bnxt *bp = netdev_priv(dev);
11170 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
11173 if (!netif_running(dev))
11176 switch (ti->type) {
11177 case UDP_TUNNEL_TYPE_VXLAN:
11178 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
11181 bp->vxlan_port_cnt++;
11182 if (bp->vxlan_port_cnt == 1) {
11183 bp->vxlan_port = ti->port;
11184 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
11185 bnxt_queue_sp_work(bp);
11188 case UDP_TUNNEL_TYPE_GENEVE:
11189 if (bp->nge_port_cnt && bp->nge_port != ti->port)
11192 bp->nge_port_cnt++;
11193 if (bp->nge_port_cnt == 1) {
11194 bp->nge_port = ti->port;
11195 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
11202 bnxt_queue_sp_work(bp);
11205 static void bnxt_udp_tunnel_del(struct net_device *dev,
11206 struct udp_tunnel_info *ti)
11208 struct bnxt *bp = netdev_priv(dev);
11210 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
11213 if (!netif_running(dev))
11216 switch (ti->type) {
11217 case UDP_TUNNEL_TYPE_VXLAN:
11218 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
11220 bp->vxlan_port_cnt--;
11222 if (bp->vxlan_port_cnt != 0)
11225 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
11227 case UDP_TUNNEL_TYPE_GENEVE:
11228 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
11230 bp->nge_port_cnt--;
11232 if (bp->nge_port_cnt != 0)
11235 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
11241 bnxt_queue_sp_work(bp);
11244 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
11245 struct net_device *dev, u32 filter_mask,
11248 struct bnxt *bp = netdev_priv(dev);
11250 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
11251 nlflags, filter_mask, NULL);
11254 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
11255 u16 flags, struct netlink_ext_ack *extack)
11257 struct bnxt *bp = netdev_priv(dev);
11258 struct nlattr *attr, *br_spec;
11261 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
11262 return -EOPNOTSUPP;
11264 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
11268 nla_for_each_nested(attr, br_spec, rem) {
11271 if (nla_type(attr) != IFLA_BRIDGE_MODE)
11274 if (nla_len(attr) < sizeof(mode))
11277 mode = nla_get_u16(attr);
11278 if (mode == bp->br_mode)
11281 rc = bnxt_hwrm_set_br_mode(bp, mode);
11283 bp->br_mode = mode;
11289 int bnxt_get_port_parent_id(struct net_device *dev,
11290 struct netdev_phys_item_id *ppid)
11292 struct bnxt *bp = netdev_priv(dev);
11294 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
11295 return -EOPNOTSUPP;
11297 /* The PF and it's VF-reps only support the switchdev framework */
11299 return -EOPNOTSUPP;
11301 ppid->id_len = sizeof(bp->switch_id);
11302 memcpy(ppid->id, bp->switch_id, ppid->id_len);
11307 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
11309 struct bnxt *bp = netdev_priv(dev);
11311 return &bp->dl_port;
11314 static const struct net_device_ops bnxt_netdev_ops = {
11315 .ndo_open = bnxt_open,
11316 .ndo_start_xmit = bnxt_start_xmit,
11317 .ndo_stop = bnxt_close,
11318 .ndo_get_stats64 = bnxt_get_stats64,
11319 .ndo_set_rx_mode = bnxt_set_rx_mode,
11320 .ndo_do_ioctl = bnxt_ioctl,
11321 .ndo_validate_addr = eth_validate_addr,
11322 .ndo_set_mac_address = bnxt_change_mac_addr,
11323 .ndo_change_mtu = bnxt_change_mtu,
11324 .ndo_fix_features = bnxt_fix_features,
11325 .ndo_set_features = bnxt_set_features,
11326 .ndo_tx_timeout = bnxt_tx_timeout,
11327 #ifdef CONFIG_BNXT_SRIOV
11328 .ndo_get_vf_config = bnxt_get_vf_config,
11329 .ndo_set_vf_mac = bnxt_set_vf_mac,
11330 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
11331 .ndo_set_vf_rate = bnxt_set_vf_bw,
11332 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
11333 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
11334 .ndo_set_vf_trust = bnxt_set_vf_trust,
11336 .ndo_setup_tc = bnxt_setup_tc,
11337 #ifdef CONFIG_RFS_ACCEL
11338 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
11340 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
11341 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
11342 .ndo_bpf = bnxt_xdp,
11343 .ndo_xdp_xmit = bnxt_xdp_xmit,
11344 .ndo_bridge_getlink = bnxt_bridge_getlink,
11345 .ndo_bridge_setlink = bnxt_bridge_setlink,
11346 .ndo_get_devlink_port = bnxt_get_devlink_port,
11349 static void bnxt_remove_one(struct pci_dev *pdev)
11351 struct net_device *dev = pci_get_drvdata(pdev);
11352 struct bnxt *bp = netdev_priv(dev);
11355 bnxt_sriov_disable(bp);
11356 bnxt_dl_unregister(bp);
11359 pci_disable_pcie_error_reporting(pdev);
11360 unregister_netdev(dev);
11361 bnxt_shutdown_tc(bp);
11362 bnxt_cancel_sp_work(bp);
11365 bnxt_clear_int_mode(bp);
11366 bnxt_hwrm_func_drv_unrgtr(bp);
11367 bnxt_free_hwrm_resources(bp);
11368 bnxt_free_hwrm_short_cmd_req(bp);
11369 bnxt_ethtool_free(bp);
11373 bnxt_cleanup_pci(bp);
11374 bnxt_free_ctx_mem(bp);
11377 bnxt_free_port_stats(bp);
11381 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
11384 struct bnxt_link_info *link_info = &bp->link_info;
11386 rc = bnxt_hwrm_phy_qcaps(bp);
11388 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
11392 rc = bnxt_update_link(bp, false);
11394 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
11399 /* Older firmware does not have supported_auto_speeds, so assume
11400 * that all supported speeds can be autonegotiated.
11402 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
11403 link_info->support_auto_speeds = link_info->support_speeds;
11408 /*initialize the ethool setting copy with NVM settings */
11409 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11410 link_info->autoneg = BNXT_AUTONEG_SPEED;
11411 if (bp->hwrm_spec_code >= 0x10201) {
11412 if (link_info->auto_pause_setting &
11413 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11414 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11416 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11418 link_info->advertising = link_info->auto_link_speeds;
11420 link_info->req_link_speed = link_info->force_link_speed;
11421 link_info->req_duplex = link_info->duplex_setting;
11423 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11424 link_info->req_flow_ctrl =
11425 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11427 link_info->req_flow_ctrl = link_info->force_pause_setting;
11431 static int bnxt_get_max_irq(struct pci_dev *pdev)
11435 if (!pdev->msix_cap)
11438 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
11439 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
11442 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
11445 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11446 int max_ring_grps = 0, max_irq;
11448 *max_tx = hw_resc->max_tx_rings;
11449 *max_rx = hw_resc->max_rx_rings;
11450 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
11451 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
11452 bnxt_get_ulp_msix_num(bp),
11453 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
11454 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11455 *max_cp = min_t(int, *max_cp, max_irq);
11456 max_ring_grps = hw_resc->max_hw_ring_grps;
11457 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
11461 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11463 if (bp->flags & BNXT_FLAG_CHIP_P5) {
11464 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
11465 /* On P5 chips, max_cp output param should be available NQs */
11468 *max_rx = min_t(int, *max_rx, max_ring_grps);
11471 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
11475 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
11478 if (!rx || !tx || !cp)
11481 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
11484 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
11489 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
11490 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
11491 /* Not enough rings, try disabling agg rings. */
11492 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
11493 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
11495 /* set BNXT_FLAG_AGG_RINGS back for consistency */
11496 bp->flags |= BNXT_FLAG_AGG_RINGS;
11499 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
11500 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11501 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
11502 bnxt_set_ring_params(bp);
11505 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
11506 int max_cp, max_stat, max_irq;
11508 /* Reserve minimum resources for RoCE */
11509 max_cp = bnxt_get_max_func_cp_rings(bp);
11510 max_stat = bnxt_get_max_func_stat_ctxs(bp);
11511 max_irq = bnxt_get_max_func_irqs(bp);
11512 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
11513 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
11514 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
11517 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
11518 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
11519 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
11520 max_cp = min_t(int, max_cp, max_irq);
11521 max_cp = min_t(int, max_cp, max_stat);
11522 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
11529 /* In initial default shared ring setting, each shared ring must have a
11532 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
11534 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
11535 bp->rx_nr_rings = bp->cp_nr_rings;
11536 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
11537 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11540 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
11542 int dflt_rings, max_rx_rings, max_tx_rings, rc;
11544 if (!bnxt_can_reserve_rings(bp))
11548 bp->flags |= BNXT_FLAG_SHARED_RINGS;
11549 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
11550 /* Reduce default rings on multi-port cards so that total default
11551 * rings do not exceed CPU count.
11553 if (bp->port_count > 1) {
11555 max_t(int, num_online_cpus() / bp->port_count, 1);
11557 dflt_rings = min_t(int, dflt_rings, max_rings);
11559 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
11562 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
11563 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
11565 bnxt_trim_dflt_sh_rings(bp);
11567 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
11568 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11570 rc = __bnxt_reserve_rings(bp);
11572 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
11573 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11575 bnxt_trim_dflt_sh_rings(bp);
11577 /* Rings may have been trimmed, re-reserve the trimmed rings. */
11578 if (bnxt_need_reserve_rings(bp)) {
11579 rc = __bnxt_reserve_rings(bp);
11581 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
11582 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11584 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11591 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
11595 if (bp->tx_nr_rings)
11598 bnxt_ulp_irq_stop(bp);
11599 bnxt_clear_int_mode(bp);
11600 rc = bnxt_set_dflt_rings(bp, true);
11602 netdev_err(bp->dev, "Not enough rings available.\n");
11603 goto init_dflt_ring_err;
11605 rc = bnxt_init_int_mode(bp);
11607 goto init_dflt_ring_err;
11609 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11610 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
11611 bp->flags |= BNXT_FLAG_RFS;
11612 bp->dev->features |= NETIF_F_NTUPLE;
11614 init_dflt_ring_err:
11615 bnxt_ulp_irq_restart(bp, rc);
11619 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
11624 bnxt_hwrm_func_qcaps(bp);
11626 if (netif_running(bp->dev))
11627 __bnxt_close_nic(bp, true, false);
11629 bnxt_ulp_irq_stop(bp);
11630 bnxt_clear_int_mode(bp);
11631 rc = bnxt_init_int_mode(bp);
11632 bnxt_ulp_irq_restart(bp, rc);
11634 if (netif_running(bp->dev)) {
11636 dev_close(bp->dev);
11638 rc = bnxt_open_nic(bp, true, false);
11644 static int bnxt_init_mac_addr(struct bnxt *bp)
11649 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
11651 #ifdef CONFIG_BNXT_SRIOV
11652 struct bnxt_vf_info *vf = &bp->vf;
11653 bool strict_approval = true;
11655 if (is_valid_ether_addr(vf->mac_addr)) {
11656 /* overwrite netdev dev_addr with admin VF MAC */
11657 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
11658 /* Older PF driver or firmware may not approve this
11661 strict_approval = false;
11663 eth_hw_addr_random(bp->dev);
11665 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
11671 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
11673 struct pci_dev *pdev = bp->pdev;
11674 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
11678 netdev_info(bp->dev, "Unable do read adapter's DSN");
11679 return -EOPNOTSUPP;
11682 /* DSN (two dw) is at an offset of 4 from the cap pos */
11684 pci_read_config_dword(pdev, pos, &dw);
11685 put_unaligned_le32(dw, &dsn[0]);
11686 pci_read_config_dword(pdev, pos + 4, &dw);
11687 put_unaligned_le32(dw, &dsn[4]);
11691 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
11693 static int version_printed;
11694 struct net_device *dev;
11698 if (pci_is_bridge(pdev))
11701 if (version_printed++ == 0)
11702 pr_info("%s", version);
11704 max_irqs = bnxt_get_max_irq(pdev);
11705 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
11709 bp = netdev_priv(dev);
11710 bnxt_set_max_func_irqs(bp, max_irqs);
11712 if (bnxt_vf_pciid(ent->driver_data))
11713 bp->flags |= BNXT_FLAG_VF;
11715 if (pdev->msix_cap)
11716 bp->flags |= BNXT_FLAG_MSIX_CAP;
11718 rc = bnxt_init_board(pdev, dev);
11720 goto init_err_free;
11722 dev->netdev_ops = &bnxt_netdev_ops;
11723 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
11724 dev->ethtool_ops = &bnxt_ethtool_ops;
11725 pci_set_drvdata(pdev, dev);
11727 rc = bnxt_alloc_hwrm_resources(bp);
11729 goto init_err_pci_clean;
11731 mutex_init(&bp->hwrm_cmd_lock);
11732 mutex_init(&bp->link_lock);
11734 rc = bnxt_fw_init_one_p1(bp);
11736 goto init_err_pci_clean;
11738 if (BNXT_CHIP_P5(bp))
11739 bp->flags |= BNXT_FLAG_CHIP_P5;
11741 rc = bnxt_fw_init_one_p2(bp);
11743 goto init_err_pci_clean;
11745 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
11746 NETIF_F_TSO | NETIF_F_TSO6 |
11747 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
11748 NETIF_F_GSO_IPXIP4 |
11749 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
11750 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
11751 NETIF_F_RXCSUM | NETIF_F_GRO;
11753 if (BNXT_SUPPORTS_TPA(bp))
11754 dev->hw_features |= NETIF_F_LRO;
11756 dev->hw_enc_features =
11757 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
11758 NETIF_F_TSO | NETIF_F_TSO6 |
11759 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
11760 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
11761 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
11762 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
11763 NETIF_F_GSO_GRE_CSUM;
11764 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
11765 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
11766 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
11767 if (BNXT_SUPPORTS_TPA(bp))
11768 dev->hw_features |= NETIF_F_GRO_HW;
11769 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
11770 if (dev->features & NETIF_F_GRO_HW)
11771 dev->features &= ~NETIF_F_LRO;
11772 dev->priv_flags |= IFF_UNICAST_FLT;
11774 #ifdef CONFIG_BNXT_SRIOV
11775 init_waitqueue_head(&bp->sriov_cfg_wait);
11776 mutex_init(&bp->sriov_lock);
11778 if (BNXT_SUPPORTS_TPA(bp)) {
11779 bp->gro_func = bnxt_gro_func_5730x;
11780 if (BNXT_CHIP_P4(bp))
11781 bp->gro_func = bnxt_gro_func_5731x;
11782 else if (BNXT_CHIP_P5(bp))
11783 bp->gro_func = bnxt_gro_func_5750x;
11785 if (!BNXT_CHIP_P4_PLUS(bp))
11786 bp->flags |= BNXT_FLAG_DOUBLE_DB;
11788 bp->ulp_probe = bnxt_ulp_probe;
11790 rc = bnxt_init_mac_addr(bp);
11792 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
11793 rc = -EADDRNOTAVAIL;
11794 goto init_err_pci_clean;
11798 /* Read the adapter's DSN to use as the eswitch switch_id */
11799 rc = bnxt_pcie_dsn_get(bp, bp->switch_id);
11801 goto init_err_pci_clean;
11804 /* MTU range: 60 - FW defined max */
11805 dev->min_mtu = ETH_ZLEN;
11806 dev->max_mtu = bp->max_mtu;
11808 rc = bnxt_probe_phy(bp, true);
11810 goto init_err_pci_clean;
11812 bnxt_set_rx_skb_mode(bp, false);
11813 bnxt_set_tpa_flags(bp);
11814 bnxt_set_ring_params(bp);
11815 rc = bnxt_set_dflt_rings(bp, true);
11817 netdev_err(bp->dev, "Not enough rings available.\n");
11819 goto init_err_pci_clean;
11822 bnxt_fw_init_one_p3(bp);
11824 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
11825 bp->flags |= BNXT_FLAG_STRIP_VLAN;
11827 rc = bnxt_init_int_mode(bp);
11829 goto init_err_pci_clean;
11831 /* No TC has been set yet and rings may have been trimmed due to
11832 * limited MSIX, so we re-initialize the TX rings per TC.
11834 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11839 create_singlethread_workqueue("bnxt_pf_wq");
11841 dev_err(&pdev->dev, "Unable to create workqueue.\n");
11842 goto init_err_pci_clean;
11848 rc = register_netdev(dev);
11850 goto init_err_cleanup_tc;
11853 bnxt_dl_register(bp);
11855 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
11856 board_info[ent->driver_data].name,
11857 (long)pci_resource_start(pdev, 0), dev->dev_addr);
11858 pcie_print_link_status(pdev);
11862 init_err_cleanup_tc:
11863 bnxt_shutdown_tc(bp);
11864 bnxt_clear_int_mode(bp);
11866 init_err_pci_clean:
11867 bnxt_free_hwrm_short_cmd_req(bp);
11868 bnxt_free_hwrm_resources(bp);
11869 bnxt_free_ctx_mem(bp);
11872 kfree(bp->fw_health);
11873 bp->fw_health = NULL;
11874 bnxt_cleanup_pci(bp);
11881 static void bnxt_shutdown(struct pci_dev *pdev)
11883 struct net_device *dev = pci_get_drvdata(pdev);
11890 bp = netdev_priv(dev);
11892 goto shutdown_exit;
11894 if (netif_running(dev))
11897 bnxt_ulp_shutdown(bp);
11899 if (system_state == SYSTEM_POWER_OFF) {
11900 bnxt_clear_int_mode(bp);
11901 pci_disable_device(pdev);
11902 pci_wake_from_d3(pdev, bp->wol);
11903 pci_set_power_state(pdev, PCI_D3hot);
11910 #ifdef CONFIG_PM_SLEEP
11911 static int bnxt_suspend(struct device *device)
11913 struct net_device *dev = dev_get_drvdata(device);
11914 struct bnxt *bp = netdev_priv(dev);
11919 if (netif_running(dev)) {
11920 netif_device_detach(dev);
11921 rc = bnxt_close(dev);
11923 bnxt_hwrm_func_drv_unrgtr(bp);
11924 pci_disable_device(bp->pdev);
11929 static int bnxt_resume(struct device *device)
11931 struct net_device *dev = dev_get_drvdata(device);
11932 struct bnxt *bp = netdev_priv(dev);
11936 rc = pci_enable_device(bp->pdev);
11938 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
11942 pci_set_master(bp->pdev);
11943 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
11947 rc = bnxt_hwrm_func_reset(bp);
11952 bnxt_get_wol_settings(bp);
11953 if (netif_running(dev)) {
11954 rc = bnxt_open(dev);
11956 netif_device_attach(dev);
11960 bnxt_ulp_start(bp, rc);
11965 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
11966 #define BNXT_PM_OPS (&bnxt_pm_ops)
11970 #define BNXT_PM_OPS NULL
11972 #endif /* CONFIG_PM_SLEEP */
11975 * bnxt_io_error_detected - called when PCI error is detected
11976 * @pdev: Pointer to PCI device
11977 * @state: The current pci connection state
11979 * This function is called after a PCI bus error affecting
11980 * this device has been detected.
11982 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
11983 pci_channel_state_t state)
11985 struct net_device *netdev = pci_get_drvdata(pdev);
11986 struct bnxt *bp = netdev_priv(netdev);
11988 netdev_info(netdev, "PCI I/O error detected\n");
11991 netif_device_detach(netdev);
11995 if (state == pci_channel_io_perm_failure) {
11997 return PCI_ERS_RESULT_DISCONNECT;
12000 if (netif_running(netdev))
12001 bnxt_close(netdev);
12003 pci_disable_device(pdev);
12006 /* Request a slot slot reset. */
12007 return PCI_ERS_RESULT_NEED_RESET;
12011 * bnxt_io_slot_reset - called after the pci bus has been reset.
12012 * @pdev: Pointer to PCI device
12014 * Restart the card from scratch, as if from a cold-boot.
12015 * At this point, the card has exprienced a hard reset,
12016 * followed by fixups by BIOS, and has its config space
12017 * set up identically to what it was at cold boot.
12019 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
12021 struct net_device *netdev = pci_get_drvdata(pdev);
12022 struct bnxt *bp = netdev_priv(netdev);
12024 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
12026 netdev_info(bp->dev, "PCI Slot Reset\n");
12030 if (pci_enable_device(pdev)) {
12031 dev_err(&pdev->dev,
12032 "Cannot re-enable PCI device after reset.\n");
12034 pci_set_master(pdev);
12036 err = bnxt_hwrm_func_reset(bp);
12037 if (!err && netif_running(netdev))
12038 err = bnxt_open(netdev);
12041 result = PCI_ERS_RESULT_RECOVERED;
12042 bnxt_ulp_start(bp, err);
12045 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
12050 return PCI_ERS_RESULT_RECOVERED;
12054 * bnxt_io_resume - called when traffic can start flowing again.
12055 * @pdev: Pointer to PCI device
12057 * This callback is called when the error recovery driver tells
12058 * us that its OK to resume normal operation.
12060 static void bnxt_io_resume(struct pci_dev *pdev)
12062 struct net_device *netdev = pci_get_drvdata(pdev);
12066 netif_device_attach(netdev);
12071 static const struct pci_error_handlers bnxt_err_handler = {
12072 .error_detected = bnxt_io_error_detected,
12073 .slot_reset = bnxt_io_slot_reset,
12074 .resume = bnxt_io_resume
12077 static struct pci_driver bnxt_pci_driver = {
12078 .name = DRV_MODULE_NAME,
12079 .id_table = bnxt_pci_tbl,
12080 .probe = bnxt_init_one,
12081 .remove = bnxt_remove_one,
12082 .shutdown = bnxt_shutdown,
12083 .driver.pm = BNXT_PM_OPS,
12084 .err_handler = &bnxt_err_handler,
12085 #if defined(CONFIG_BNXT_SRIOV)
12086 .sriov_configure = bnxt_sriov_configure,
12090 static int __init bnxt_init(void)
12093 return pci_register_driver(&bnxt_pci_driver);
12096 static void __exit bnxt_exit(void)
12098 pci_unregister_driver(&bnxt_pci_driver);
12100 destroy_workqueue(bnxt_pf_wq);
12104 module_init(bnxt_init);
12105 module_exit(bnxt_exit);