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,
259 static struct workqueue_struct *bnxt_pf_wq;
261 static bool bnxt_vf_pciid(enum board_idx idx)
263 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
264 idx == NETXTREME_S_VF || idx == NETXTREME_E_P5_VF);
267 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
268 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
269 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
271 #define BNXT_CP_DB_IRQ_DIS(db) \
272 writel(DB_CP_IRQ_DIS_FLAGS, db)
274 #define BNXT_DB_CQ(db, idx) \
275 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
277 #define BNXT_DB_NQ_P5(db, idx) \
278 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
280 #define BNXT_DB_CQ_ARM(db, idx) \
281 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
283 #define BNXT_DB_NQ_ARM_P5(db, idx) \
284 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
286 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
288 if (bp->flags & BNXT_FLAG_CHIP_P5)
289 BNXT_DB_NQ_P5(db, idx);
294 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
296 if (bp->flags & BNXT_FLAG_CHIP_P5)
297 BNXT_DB_NQ_ARM_P5(db, idx);
299 BNXT_DB_CQ_ARM(db, idx);
302 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
304 if (bp->flags & BNXT_FLAG_CHIP_P5)
305 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
311 const u16 bnxt_lhint_arr[] = {
312 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
313 TX_BD_FLAGS_LHINT_512_TO_1023,
314 TX_BD_FLAGS_LHINT_1024_TO_2047,
315 TX_BD_FLAGS_LHINT_1024_TO_2047,
316 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
317 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
318 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
319 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
320 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
321 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
322 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
323 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
324 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
325 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
326 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
327 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
328 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
329 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
330 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
333 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
335 struct metadata_dst *md_dst = skb_metadata_dst(skb);
337 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
340 return md_dst->u.port_info.port_id;
343 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
345 struct bnxt *bp = netdev_priv(dev);
347 struct tx_bd_ext *txbd1;
348 struct netdev_queue *txq;
351 unsigned int length, pad = 0;
352 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
354 struct pci_dev *pdev = bp->pdev;
355 struct bnxt_tx_ring_info *txr;
356 struct bnxt_sw_tx_bd *tx_buf;
358 i = skb_get_queue_mapping(skb);
359 if (unlikely(i >= bp->tx_nr_rings)) {
360 dev_kfree_skb_any(skb);
364 txq = netdev_get_tx_queue(dev, i);
365 txr = &bp->tx_ring[bp->tx_ring_map[i]];
368 free_size = bnxt_tx_avail(bp, txr);
369 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
370 netif_tx_stop_queue(txq);
371 return NETDEV_TX_BUSY;
375 len = skb_headlen(skb);
376 last_frag = skb_shinfo(skb)->nr_frags;
378 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
380 txbd->tx_bd_opaque = prod;
382 tx_buf = &txr->tx_buf_ring[prod];
384 tx_buf->nr_frags = last_frag;
387 cfa_action = bnxt_xmit_get_cfa_action(skb);
388 if (skb_vlan_tag_present(skb)) {
389 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
390 skb_vlan_tag_get(skb);
391 /* Currently supports 8021Q, 8021AD vlan offloads
392 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
394 if (skb->vlan_proto == htons(ETH_P_8021Q))
395 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
398 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
399 struct tx_push_buffer *tx_push_buf = txr->tx_push;
400 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
401 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
402 void __iomem *db = txr->tx_db.doorbell;
403 void *pdata = tx_push_buf->data;
407 /* Set COAL_NOW to be ready quickly for the next push */
408 tx_push->tx_bd_len_flags_type =
409 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
410 TX_BD_TYPE_LONG_TX_BD |
411 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
412 TX_BD_FLAGS_COAL_NOW |
413 TX_BD_FLAGS_PACKET_END |
414 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
416 if (skb->ip_summed == CHECKSUM_PARTIAL)
417 tx_push1->tx_bd_hsize_lflags =
418 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
420 tx_push1->tx_bd_hsize_lflags = 0;
422 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
423 tx_push1->tx_bd_cfa_action =
424 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
426 end = pdata + length;
427 end = PTR_ALIGN(end, 8) - 1;
430 skb_copy_from_linear_data(skb, pdata, len);
432 for (j = 0; j < last_frag; j++) {
433 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
436 fptr = skb_frag_address_safe(frag);
440 memcpy(pdata, fptr, skb_frag_size(frag));
441 pdata += skb_frag_size(frag);
444 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
445 txbd->tx_bd_haddr = txr->data_mapping;
446 prod = NEXT_TX(prod);
447 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
448 memcpy(txbd, tx_push1, sizeof(*txbd));
449 prod = NEXT_TX(prod);
451 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
455 netdev_tx_sent_queue(txq, skb->len);
456 wmb(); /* Sync is_push and byte queue before pushing data */
458 push_len = (length + sizeof(*tx_push) + 7) / 8;
460 __iowrite64_copy(db, tx_push_buf, 16);
461 __iowrite32_copy(db + 4, tx_push_buf + 1,
462 (push_len - 16) << 1);
464 __iowrite64_copy(db, tx_push_buf, push_len);
471 if (length < BNXT_MIN_PKT_SIZE) {
472 pad = BNXT_MIN_PKT_SIZE - length;
473 if (skb_pad(skb, pad)) {
474 /* SKB already freed. */
478 length = BNXT_MIN_PKT_SIZE;
481 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
483 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
484 dev_kfree_skb_any(skb);
489 dma_unmap_addr_set(tx_buf, mapping, mapping);
490 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
491 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
493 txbd->tx_bd_haddr = cpu_to_le64(mapping);
495 prod = NEXT_TX(prod);
496 txbd1 = (struct tx_bd_ext *)
497 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
499 txbd1->tx_bd_hsize_lflags = 0;
500 if (skb_is_gso(skb)) {
503 if (skb->encapsulation)
504 hdr_len = skb_inner_network_offset(skb) +
505 skb_inner_network_header_len(skb) +
506 inner_tcp_hdrlen(skb);
508 hdr_len = skb_transport_offset(skb) +
511 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
513 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
514 length = skb_shinfo(skb)->gso_size;
515 txbd1->tx_bd_mss = cpu_to_le32(length);
517 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
518 txbd1->tx_bd_hsize_lflags =
519 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
520 txbd1->tx_bd_mss = 0;
524 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
525 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
530 flags |= bnxt_lhint_arr[length];
531 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
533 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
534 txbd1->tx_bd_cfa_action =
535 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
536 for (i = 0; i < last_frag; i++) {
537 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
539 prod = NEXT_TX(prod);
540 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
542 len = skb_frag_size(frag);
543 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
546 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
549 tx_buf = &txr->tx_buf_ring[prod];
550 dma_unmap_addr_set(tx_buf, mapping, mapping);
552 txbd->tx_bd_haddr = cpu_to_le64(mapping);
554 flags = len << TX_BD_LEN_SHIFT;
555 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
559 txbd->tx_bd_len_flags_type =
560 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
561 TX_BD_FLAGS_PACKET_END);
563 netdev_tx_sent_queue(txq, skb->len);
565 /* Sync BD data before updating doorbell */
568 prod = NEXT_TX(prod);
571 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
572 bnxt_db_write(bp, &txr->tx_db, prod);
576 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
577 if (netdev_xmit_more() && !tx_buf->is_push)
578 bnxt_db_write(bp, &txr->tx_db, prod);
580 netif_tx_stop_queue(txq);
582 /* netif_tx_stop_queue() must be done before checking
583 * tx index in bnxt_tx_avail() below, because in
584 * bnxt_tx_int(), we update tx index before checking for
585 * netif_tx_queue_stopped().
588 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
589 netif_tx_wake_queue(txq);
596 /* start back at beginning and unmap skb */
598 tx_buf = &txr->tx_buf_ring[prod];
600 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
601 skb_headlen(skb), PCI_DMA_TODEVICE);
602 prod = NEXT_TX(prod);
604 /* unmap remaining mapped pages */
605 for (i = 0; i < last_frag; i++) {
606 prod = NEXT_TX(prod);
607 tx_buf = &txr->tx_buf_ring[prod];
608 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
609 skb_frag_size(&skb_shinfo(skb)->frags[i]),
613 dev_kfree_skb_any(skb);
617 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
619 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
620 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
621 u16 cons = txr->tx_cons;
622 struct pci_dev *pdev = bp->pdev;
624 unsigned int tx_bytes = 0;
626 for (i = 0; i < nr_pkts; i++) {
627 struct bnxt_sw_tx_bd *tx_buf;
631 tx_buf = &txr->tx_buf_ring[cons];
632 cons = NEXT_TX(cons);
636 if (tx_buf->is_push) {
641 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
642 skb_headlen(skb), PCI_DMA_TODEVICE);
643 last = tx_buf->nr_frags;
645 for (j = 0; j < last; j++) {
646 cons = NEXT_TX(cons);
647 tx_buf = &txr->tx_buf_ring[cons];
650 dma_unmap_addr(tx_buf, mapping),
651 skb_frag_size(&skb_shinfo(skb)->frags[j]),
656 cons = NEXT_TX(cons);
658 tx_bytes += skb->len;
659 dev_kfree_skb_any(skb);
662 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
665 /* Need to make the tx_cons update visible to bnxt_start_xmit()
666 * before checking for netif_tx_queue_stopped(). Without the
667 * memory barrier, there is a small possibility that bnxt_start_xmit()
668 * will miss it and cause the queue to be stopped forever.
672 if (unlikely(netif_tx_queue_stopped(txq)) &&
673 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
674 __netif_tx_lock(txq, smp_processor_id());
675 if (netif_tx_queue_stopped(txq) &&
676 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
677 txr->dev_state != BNXT_DEV_STATE_CLOSING)
678 netif_tx_wake_queue(txq);
679 __netif_tx_unlock(txq);
683 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
684 struct bnxt_rx_ring_info *rxr,
687 struct device *dev = &bp->pdev->dev;
690 page = page_pool_dev_alloc_pages(rxr->page_pool);
694 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
695 DMA_ATTR_WEAK_ORDERING);
696 if (dma_mapping_error(dev, *mapping)) {
697 page_pool_recycle_direct(rxr->page_pool, page);
700 *mapping += bp->rx_dma_offset;
704 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
708 struct pci_dev *pdev = bp->pdev;
710 data = kmalloc(bp->rx_buf_size, gfp);
714 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
715 bp->rx_buf_use_size, bp->rx_dir,
716 DMA_ATTR_WEAK_ORDERING);
718 if (dma_mapping_error(&pdev->dev, *mapping)) {
725 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
728 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
729 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
732 if (BNXT_RX_PAGE_MODE(bp)) {
734 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
740 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
742 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
748 rx_buf->data_ptr = data + bp->rx_offset;
750 rx_buf->mapping = mapping;
752 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
756 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
758 u16 prod = rxr->rx_prod;
759 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
760 struct rx_bd *cons_bd, *prod_bd;
762 prod_rx_buf = &rxr->rx_buf_ring[prod];
763 cons_rx_buf = &rxr->rx_buf_ring[cons];
765 prod_rx_buf->data = data;
766 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
768 prod_rx_buf->mapping = cons_rx_buf->mapping;
770 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
771 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
773 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
776 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
778 u16 next, max = rxr->rx_agg_bmap_size;
780 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
782 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
786 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
787 struct bnxt_rx_ring_info *rxr,
791 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
792 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
793 struct pci_dev *pdev = bp->pdev;
796 u16 sw_prod = rxr->rx_sw_agg_prod;
797 unsigned int offset = 0;
799 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
802 page = alloc_page(gfp);
806 rxr->rx_page_offset = 0;
808 offset = rxr->rx_page_offset;
809 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
810 if (rxr->rx_page_offset == PAGE_SIZE)
815 page = alloc_page(gfp);
820 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
821 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
822 DMA_ATTR_WEAK_ORDERING);
823 if (dma_mapping_error(&pdev->dev, mapping)) {
828 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
829 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
831 __set_bit(sw_prod, rxr->rx_agg_bmap);
832 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
833 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
835 rx_agg_buf->page = page;
836 rx_agg_buf->offset = offset;
837 rx_agg_buf->mapping = mapping;
838 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
839 rxbd->rx_bd_opaque = sw_prod;
843 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
844 struct bnxt_cp_ring_info *cpr,
845 u16 cp_cons, u16 curr)
847 struct rx_agg_cmp *agg;
849 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
850 agg = (struct rx_agg_cmp *)
851 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
855 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
856 struct bnxt_rx_ring_info *rxr,
857 u16 agg_id, u16 curr)
859 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
861 return &tpa_info->agg_arr[curr];
864 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
865 u16 start, u32 agg_bufs, bool tpa)
867 struct bnxt_napi *bnapi = cpr->bnapi;
868 struct bnxt *bp = bnapi->bp;
869 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
870 u16 prod = rxr->rx_agg_prod;
871 u16 sw_prod = rxr->rx_sw_agg_prod;
875 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
878 for (i = 0; i < agg_bufs; i++) {
880 struct rx_agg_cmp *agg;
881 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
882 struct rx_bd *prod_bd;
886 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
888 agg = bnxt_get_agg(bp, cpr, idx, start + i);
889 cons = agg->rx_agg_cmp_opaque;
890 __clear_bit(cons, rxr->rx_agg_bmap);
892 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
893 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
895 __set_bit(sw_prod, rxr->rx_agg_bmap);
896 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
897 cons_rx_buf = &rxr->rx_agg_ring[cons];
899 /* It is possible for sw_prod to be equal to cons, so
900 * set cons_rx_buf->page to NULL first.
902 page = cons_rx_buf->page;
903 cons_rx_buf->page = NULL;
904 prod_rx_buf->page = page;
905 prod_rx_buf->offset = cons_rx_buf->offset;
907 prod_rx_buf->mapping = cons_rx_buf->mapping;
909 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
911 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
912 prod_bd->rx_bd_opaque = sw_prod;
914 prod = NEXT_RX_AGG(prod);
915 sw_prod = NEXT_RX_AGG(sw_prod);
917 rxr->rx_agg_prod = prod;
918 rxr->rx_sw_agg_prod = sw_prod;
921 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
922 struct bnxt_rx_ring_info *rxr,
923 u16 cons, void *data, u8 *data_ptr,
925 unsigned int offset_and_len)
927 unsigned int payload = offset_and_len >> 16;
928 unsigned int len = offset_and_len & 0xffff;
930 struct page *page = data;
931 u16 prod = rxr->rx_prod;
935 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
937 bnxt_reuse_rx_data(rxr, cons, data);
940 dma_addr -= bp->rx_dma_offset;
941 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
942 DMA_ATTR_WEAK_ORDERING);
944 if (unlikely(!payload))
945 payload = eth_get_headlen(bp->dev, data_ptr, len);
947 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
953 off = (void *)data_ptr - page_address(page);
954 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
955 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
956 payload + NET_IP_ALIGN);
958 frag = &skb_shinfo(skb)->frags[0];
959 skb_frag_size_sub(frag, payload);
960 skb_frag_off_add(frag, payload);
961 skb->data_len -= payload;
962 skb->tail += payload;
967 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
968 struct bnxt_rx_ring_info *rxr, u16 cons,
969 void *data, u8 *data_ptr,
971 unsigned int offset_and_len)
973 u16 prod = rxr->rx_prod;
977 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
979 bnxt_reuse_rx_data(rxr, cons, data);
983 skb = build_skb(data, 0);
984 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
985 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
991 skb_reserve(skb, bp->rx_offset);
992 skb_put(skb, offset_and_len & 0xffff);
996 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
997 struct bnxt_cp_ring_info *cpr,
998 struct sk_buff *skb, u16 idx,
999 u32 agg_bufs, bool tpa)
1001 struct bnxt_napi *bnapi = cpr->bnapi;
1002 struct pci_dev *pdev = bp->pdev;
1003 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1004 u16 prod = rxr->rx_agg_prod;
1005 bool p5_tpa = false;
1008 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1011 for (i = 0; i < agg_bufs; i++) {
1013 struct rx_agg_cmp *agg;
1014 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1019 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1021 agg = bnxt_get_agg(bp, cpr, idx, i);
1022 cons = agg->rx_agg_cmp_opaque;
1023 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1024 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1026 cons_rx_buf = &rxr->rx_agg_ring[cons];
1027 skb_fill_page_desc(skb, i, cons_rx_buf->page,
1028 cons_rx_buf->offset, frag_len);
1029 __clear_bit(cons, rxr->rx_agg_bmap);
1031 /* It is possible for bnxt_alloc_rx_page() to allocate
1032 * a sw_prod index that equals the cons index, so we
1033 * need to clear the cons entry now.
1035 mapping = cons_rx_buf->mapping;
1036 page = cons_rx_buf->page;
1037 cons_rx_buf->page = NULL;
1039 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1040 struct skb_shared_info *shinfo;
1041 unsigned int nr_frags;
1043 shinfo = skb_shinfo(skb);
1044 nr_frags = --shinfo->nr_frags;
1045 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1049 cons_rx_buf->page = page;
1051 /* Update prod since possibly some pages have been
1052 * allocated already.
1054 rxr->rx_agg_prod = prod;
1055 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1059 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1061 DMA_ATTR_WEAK_ORDERING);
1063 skb->data_len += frag_len;
1064 skb->len += frag_len;
1065 skb->truesize += PAGE_SIZE;
1067 prod = NEXT_RX_AGG(prod);
1069 rxr->rx_agg_prod = prod;
1073 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1074 u8 agg_bufs, u32 *raw_cons)
1077 struct rx_agg_cmp *agg;
1079 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1080 last = RING_CMP(*raw_cons);
1081 agg = (struct rx_agg_cmp *)
1082 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1083 return RX_AGG_CMP_VALID(agg, *raw_cons);
1086 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1090 struct bnxt *bp = bnapi->bp;
1091 struct pci_dev *pdev = bp->pdev;
1092 struct sk_buff *skb;
1094 skb = napi_alloc_skb(&bnapi->napi, len);
1098 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1101 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1102 len + NET_IP_ALIGN);
1104 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1111 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1112 u32 *raw_cons, void *cmp)
1114 struct rx_cmp *rxcmp = cmp;
1115 u32 tmp_raw_cons = *raw_cons;
1116 u8 cmp_type, agg_bufs = 0;
1118 cmp_type = RX_CMP_TYPE(rxcmp);
1120 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1121 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1123 RX_CMP_AGG_BUFS_SHIFT;
1124 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1125 struct rx_tpa_end_cmp *tpa_end = cmp;
1127 if (bp->flags & BNXT_FLAG_CHIP_P5)
1130 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1134 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1137 *raw_cons = tmp_raw_cons;
1141 static void bnxt_queue_sp_work(struct bnxt *bp)
1144 queue_work(bnxt_pf_wq, &bp->sp_task);
1146 schedule_work(&bp->sp_task);
1149 static void bnxt_cancel_sp_work(struct bnxt *bp)
1152 flush_workqueue(bnxt_pf_wq);
1154 cancel_work_sync(&bp->sp_task);
1157 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1159 if (!rxr->bnapi->in_reset) {
1160 rxr->bnapi->in_reset = true;
1161 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1162 bnxt_queue_sp_work(bp);
1164 rxr->rx_next_cons = 0xffff;
1167 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1169 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1170 u16 idx = agg_id & MAX_TPA_P5_MASK;
1172 if (test_bit(idx, map->agg_idx_bmap))
1173 idx = find_first_zero_bit(map->agg_idx_bmap,
1174 BNXT_AGG_IDX_BMAP_SIZE);
1175 __set_bit(idx, map->agg_idx_bmap);
1176 map->agg_id_tbl[agg_id] = idx;
1180 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1182 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1184 __clear_bit(idx, map->agg_idx_bmap);
1187 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1189 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1191 return map->agg_id_tbl[agg_id];
1194 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1195 struct rx_tpa_start_cmp *tpa_start,
1196 struct rx_tpa_start_cmp_ext *tpa_start1)
1198 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1199 struct bnxt_tpa_info *tpa_info;
1200 u16 cons, prod, agg_id;
1201 struct rx_bd *prod_bd;
1204 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1205 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1206 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1208 agg_id = TPA_START_AGG_ID(tpa_start);
1210 cons = tpa_start->rx_tpa_start_cmp_opaque;
1211 prod = rxr->rx_prod;
1212 cons_rx_buf = &rxr->rx_buf_ring[cons];
1213 prod_rx_buf = &rxr->rx_buf_ring[prod];
1214 tpa_info = &rxr->rx_tpa[agg_id];
1216 if (unlikely(cons != rxr->rx_next_cons ||
1217 TPA_START_ERROR(tpa_start))) {
1218 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1219 cons, rxr->rx_next_cons,
1220 TPA_START_ERROR_CODE(tpa_start1));
1221 bnxt_sched_reset(bp, rxr);
1224 /* Store cfa_code in tpa_info to use in tpa_end
1225 * completion processing.
1227 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1228 prod_rx_buf->data = tpa_info->data;
1229 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1231 mapping = tpa_info->mapping;
1232 prod_rx_buf->mapping = mapping;
1234 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1236 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1238 tpa_info->data = cons_rx_buf->data;
1239 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1240 cons_rx_buf->data = NULL;
1241 tpa_info->mapping = cons_rx_buf->mapping;
1244 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1245 RX_TPA_START_CMP_LEN_SHIFT;
1246 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1247 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1249 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1250 tpa_info->gso_type = SKB_GSO_TCPV4;
1251 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1252 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1253 tpa_info->gso_type = SKB_GSO_TCPV6;
1254 tpa_info->rss_hash =
1255 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1257 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1258 tpa_info->gso_type = 0;
1259 if (netif_msg_rx_err(bp))
1260 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1262 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1263 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1264 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1265 tpa_info->agg_count = 0;
1267 rxr->rx_prod = NEXT_RX(prod);
1268 cons = NEXT_RX(cons);
1269 rxr->rx_next_cons = NEXT_RX(cons);
1270 cons_rx_buf = &rxr->rx_buf_ring[cons];
1272 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1273 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1274 cons_rx_buf->data = NULL;
1277 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1280 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1284 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1286 struct udphdr *uh = NULL;
1288 if (ip_proto == htons(ETH_P_IP)) {
1289 struct iphdr *iph = (struct iphdr *)skb->data;
1291 if (iph->protocol == IPPROTO_UDP)
1292 uh = (struct udphdr *)(iph + 1);
1294 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1296 if (iph->nexthdr == IPPROTO_UDP)
1297 uh = (struct udphdr *)(iph + 1);
1301 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1303 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1308 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1309 int payload_off, int tcp_ts,
1310 struct sk_buff *skb)
1315 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1316 u32 hdr_info = tpa_info->hdr_info;
1317 bool loopback = false;
1319 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1320 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1321 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1323 /* If the packet is an internal loopback packet, the offsets will
1324 * have an extra 4 bytes.
1326 if (inner_mac_off == 4) {
1328 } else if (inner_mac_off > 4) {
1329 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1332 /* We only support inner iPv4/ipv6. If we don't see the
1333 * correct protocol ID, it must be a loopback packet where
1334 * the offsets are off by 4.
1336 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1340 /* internal loopback packet, subtract all offsets by 4 */
1346 nw_off = inner_ip_off - ETH_HLEN;
1347 skb_set_network_header(skb, nw_off);
1348 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1349 struct ipv6hdr *iph = ipv6_hdr(skb);
1351 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1352 len = skb->len - skb_transport_offset(skb);
1354 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1356 struct iphdr *iph = ip_hdr(skb);
1358 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1359 len = skb->len - skb_transport_offset(skb);
1361 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1364 if (inner_mac_off) { /* tunnel */
1365 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1368 bnxt_gro_tunnel(skb, proto);
1374 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1375 int payload_off, int tcp_ts,
1376 struct sk_buff *skb)
1379 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1380 u32 hdr_info = tpa_info->hdr_info;
1381 int iphdr_len, nw_off;
1383 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1384 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1385 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1387 nw_off = inner_ip_off - ETH_HLEN;
1388 skb_set_network_header(skb, nw_off);
1389 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1390 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1391 skb_set_transport_header(skb, nw_off + iphdr_len);
1393 if (inner_mac_off) { /* tunnel */
1394 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1397 bnxt_gro_tunnel(skb, proto);
1403 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1404 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1406 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1407 int payload_off, int tcp_ts,
1408 struct sk_buff *skb)
1412 int len, nw_off, tcp_opt_len = 0;
1417 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1420 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1422 skb_set_network_header(skb, nw_off);
1424 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1425 len = skb->len - skb_transport_offset(skb);
1427 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1428 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1429 struct ipv6hdr *iph;
1431 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1433 skb_set_network_header(skb, nw_off);
1434 iph = ipv6_hdr(skb);
1435 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1436 len = skb->len - skb_transport_offset(skb);
1438 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1440 dev_kfree_skb_any(skb);
1444 if (nw_off) /* tunnel */
1445 bnxt_gro_tunnel(skb, skb->protocol);
1450 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1451 struct bnxt_tpa_info *tpa_info,
1452 struct rx_tpa_end_cmp *tpa_end,
1453 struct rx_tpa_end_cmp_ext *tpa_end1,
1454 struct sk_buff *skb)
1460 segs = TPA_END_TPA_SEGS(tpa_end);
1464 NAPI_GRO_CB(skb)->count = segs;
1465 skb_shinfo(skb)->gso_size =
1466 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1467 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1468 if (bp->flags & BNXT_FLAG_CHIP_P5)
1469 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1471 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1472 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1474 tcp_gro_complete(skb);
1479 /* Given the cfa_code of a received packet determine which
1480 * netdev (vf-rep or PF) the packet is destined to.
1482 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1484 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1486 /* if vf-rep dev is NULL, the must belongs to the PF */
1487 return dev ? dev : bp->dev;
1490 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1491 struct bnxt_cp_ring_info *cpr,
1493 struct rx_tpa_end_cmp *tpa_end,
1494 struct rx_tpa_end_cmp_ext *tpa_end1,
1497 struct bnxt_napi *bnapi = cpr->bnapi;
1498 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1499 u8 *data_ptr, agg_bufs;
1501 struct bnxt_tpa_info *tpa_info;
1503 struct sk_buff *skb;
1504 u16 idx = 0, agg_id;
1508 if (unlikely(bnapi->in_reset)) {
1509 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1512 return ERR_PTR(-EBUSY);
1516 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1517 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1518 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1519 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1520 tpa_info = &rxr->rx_tpa[agg_id];
1521 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1522 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1523 agg_bufs, tpa_info->agg_count);
1524 agg_bufs = tpa_info->agg_count;
1526 tpa_info->agg_count = 0;
1527 *event |= BNXT_AGG_EVENT;
1528 bnxt_free_agg_idx(rxr, agg_id);
1530 gro = !!(bp->flags & BNXT_FLAG_GRO);
1532 agg_id = TPA_END_AGG_ID(tpa_end);
1533 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1534 tpa_info = &rxr->rx_tpa[agg_id];
1535 idx = RING_CMP(*raw_cons);
1537 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1538 return ERR_PTR(-EBUSY);
1540 *event |= BNXT_AGG_EVENT;
1541 idx = NEXT_CMP(idx);
1543 gro = !!TPA_END_GRO(tpa_end);
1545 data = tpa_info->data;
1546 data_ptr = tpa_info->data_ptr;
1548 len = tpa_info->len;
1549 mapping = tpa_info->mapping;
1551 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1552 bnxt_abort_tpa(cpr, idx, agg_bufs);
1553 if (agg_bufs > MAX_SKB_FRAGS)
1554 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1555 agg_bufs, (int)MAX_SKB_FRAGS);
1559 if (len <= bp->rx_copy_thresh) {
1560 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1562 bnxt_abort_tpa(cpr, idx, agg_bufs);
1567 dma_addr_t new_mapping;
1569 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1571 bnxt_abort_tpa(cpr, idx, agg_bufs);
1575 tpa_info->data = new_data;
1576 tpa_info->data_ptr = new_data + bp->rx_offset;
1577 tpa_info->mapping = new_mapping;
1579 skb = build_skb(data, 0);
1580 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1581 bp->rx_buf_use_size, bp->rx_dir,
1582 DMA_ATTR_WEAK_ORDERING);
1586 bnxt_abort_tpa(cpr, idx, agg_bufs);
1589 skb_reserve(skb, bp->rx_offset);
1594 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1596 /* Page reuse already handled by bnxt_rx_pages(). */
1602 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1604 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1605 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1607 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1608 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1609 u16 vlan_proto = tpa_info->metadata >>
1610 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1611 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1613 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1616 skb_checksum_none_assert(skb);
1617 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1618 skb->ip_summed = CHECKSUM_UNNECESSARY;
1620 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1624 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1629 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1630 struct rx_agg_cmp *rx_agg)
1632 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1633 struct bnxt_tpa_info *tpa_info;
1635 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1636 tpa_info = &rxr->rx_tpa[agg_id];
1637 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1638 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1641 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1642 struct sk_buff *skb)
1644 if (skb->dev != bp->dev) {
1645 /* this packet belongs to a vf-rep */
1646 bnxt_vf_rep_rx(bp, skb);
1649 skb_record_rx_queue(skb, bnapi->index);
1650 napi_gro_receive(&bnapi->napi, skb);
1653 /* returns the following:
1654 * 1 - 1 packet successfully received
1655 * 0 - successful TPA_START, packet not completed yet
1656 * -EBUSY - completion ring does not have all the agg buffers yet
1657 * -ENOMEM - packet aborted due to out of memory
1658 * -EIO - packet aborted due to hw error indicated in BD
1660 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1661 u32 *raw_cons, u8 *event)
1663 struct bnxt_napi *bnapi = cpr->bnapi;
1664 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1665 struct net_device *dev = bp->dev;
1666 struct rx_cmp *rxcmp;
1667 struct rx_cmp_ext *rxcmp1;
1668 u32 tmp_raw_cons = *raw_cons;
1669 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1670 struct bnxt_sw_rx_bd *rx_buf;
1672 u8 *data_ptr, agg_bufs, cmp_type;
1673 dma_addr_t dma_addr;
1674 struct sk_buff *skb;
1679 rxcmp = (struct rx_cmp *)
1680 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1682 cmp_type = RX_CMP_TYPE(rxcmp);
1684 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1685 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1686 goto next_rx_no_prod_no_len;
1689 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1690 cp_cons = RING_CMP(tmp_raw_cons);
1691 rxcmp1 = (struct rx_cmp_ext *)
1692 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1694 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1697 prod = rxr->rx_prod;
1699 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1700 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1701 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1703 *event |= BNXT_RX_EVENT;
1704 goto next_rx_no_prod_no_len;
1706 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1707 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1708 (struct rx_tpa_end_cmp *)rxcmp,
1709 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1716 bnxt_deliver_skb(bp, bnapi, skb);
1719 *event |= BNXT_RX_EVENT;
1720 goto next_rx_no_prod_no_len;
1723 cons = rxcmp->rx_cmp_opaque;
1724 if (unlikely(cons != rxr->rx_next_cons)) {
1725 int rc1 = bnxt_discard_rx(bp, cpr, raw_cons, rxcmp);
1727 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1728 cons, rxr->rx_next_cons);
1729 bnxt_sched_reset(bp, rxr);
1732 rx_buf = &rxr->rx_buf_ring[cons];
1733 data = rx_buf->data;
1734 data_ptr = rx_buf->data_ptr;
1737 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1738 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1741 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1744 cp_cons = NEXT_CMP(cp_cons);
1745 *event |= BNXT_AGG_EVENT;
1747 *event |= BNXT_RX_EVENT;
1749 rx_buf->data = NULL;
1750 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1751 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1753 bnxt_reuse_rx_data(rxr, cons, data);
1755 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1759 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1760 netdev_warn(bp->dev, "RX buffer error %x\n", rx_err);
1761 bnxt_sched_reset(bp, rxr);
1763 goto next_rx_no_len;
1766 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1767 dma_addr = rx_buf->mapping;
1769 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1774 if (len <= bp->rx_copy_thresh) {
1775 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1776 bnxt_reuse_rx_data(rxr, cons, data);
1779 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1787 if (rx_buf->data_ptr == data_ptr)
1788 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1791 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1800 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1807 if (RX_CMP_HASH_VALID(rxcmp)) {
1808 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1809 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1811 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1812 if (hash_type != 1 && hash_type != 3)
1813 type = PKT_HASH_TYPE_L3;
1814 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1817 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1818 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1820 if ((rxcmp1->rx_cmp_flags2 &
1821 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1822 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1823 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1824 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1825 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1827 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1830 skb_checksum_none_assert(skb);
1831 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1832 if (dev->features & NETIF_F_RXCSUM) {
1833 skb->ip_summed = CHECKSUM_UNNECESSARY;
1834 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1837 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1838 if (dev->features & NETIF_F_RXCSUM)
1839 bnapi->cp_ring.rx_l4_csum_errors++;
1843 bnxt_deliver_skb(bp, bnapi, skb);
1847 cpr->rx_packets += 1;
1848 cpr->rx_bytes += len;
1851 rxr->rx_prod = NEXT_RX(prod);
1852 rxr->rx_next_cons = NEXT_RX(cons);
1854 next_rx_no_prod_no_len:
1855 *raw_cons = tmp_raw_cons;
1860 /* In netpoll mode, if we are using a combined completion ring, we need to
1861 * discard the rx packets and recycle the buffers.
1863 static int bnxt_force_rx_discard(struct bnxt *bp,
1864 struct bnxt_cp_ring_info *cpr,
1865 u32 *raw_cons, u8 *event)
1867 u32 tmp_raw_cons = *raw_cons;
1868 struct rx_cmp_ext *rxcmp1;
1869 struct rx_cmp *rxcmp;
1873 cp_cons = RING_CMP(tmp_raw_cons);
1874 rxcmp = (struct rx_cmp *)
1875 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1877 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1878 cp_cons = RING_CMP(tmp_raw_cons);
1879 rxcmp1 = (struct rx_cmp_ext *)
1880 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1882 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1885 cmp_type = RX_CMP_TYPE(rxcmp);
1886 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1887 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1888 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1889 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1890 struct rx_tpa_end_cmp_ext *tpa_end1;
1892 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1893 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1894 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1896 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1899 #define BNXT_GET_EVENT_PORT(data) \
1901 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1903 static int bnxt_async_event_process(struct bnxt *bp,
1904 struct hwrm_async_event_cmpl *cmpl)
1906 u16 event_id = le16_to_cpu(cmpl->event_id);
1908 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1910 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1911 u32 data1 = le32_to_cpu(cmpl->event_data1);
1912 struct bnxt_link_info *link_info = &bp->link_info;
1915 goto async_event_process_exit;
1917 /* print unsupported speed warning in forced speed mode only */
1918 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1919 (data1 & 0x20000)) {
1920 u16 fw_speed = link_info->force_link_speed;
1921 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1923 if (speed != SPEED_UNKNOWN)
1924 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1927 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1930 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1931 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1933 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1934 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1936 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1937 u32 data1 = le32_to_cpu(cmpl->event_data1);
1938 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1943 if (bp->pf.port_id != port_id)
1946 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1949 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1951 goto async_event_process_exit;
1952 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1955 goto async_event_process_exit;
1957 bnxt_queue_sp_work(bp);
1958 async_event_process_exit:
1959 bnxt_ulp_async_events(bp, cmpl);
1963 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1965 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1966 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1967 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1968 (struct hwrm_fwd_req_cmpl *)txcmp;
1970 switch (cmpl_type) {
1971 case CMPL_BASE_TYPE_HWRM_DONE:
1972 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1973 if (seq_id == bp->hwrm_intr_seq_id)
1974 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
1976 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1979 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1980 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1982 if ((vf_id < bp->pf.first_vf_id) ||
1983 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1984 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1989 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1990 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1991 bnxt_queue_sp_work(bp);
1994 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1995 bnxt_async_event_process(bp,
1996 (struct hwrm_async_event_cmpl *)txcmp);
2005 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2007 struct bnxt_napi *bnapi = dev_instance;
2008 struct bnxt *bp = bnapi->bp;
2009 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2010 u32 cons = RING_CMP(cpr->cp_raw_cons);
2013 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2014 napi_schedule(&bnapi->napi);
2018 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2020 u32 raw_cons = cpr->cp_raw_cons;
2021 u16 cons = RING_CMP(raw_cons);
2022 struct tx_cmp *txcmp;
2024 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2026 return TX_CMP_VALID(txcmp, raw_cons);
2029 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2031 struct bnxt_napi *bnapi = dev_instance;
2032 struct bnxt *bp = bnapi->bp;
2033 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2034 u32 cons = RING_CMP(cpr->cp_raw_cons);
2037 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2039 if (!bnxt_has_work(bp, cpr)) {
2040 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2041 /* return if erroneous interrupt */
2042 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2046 /* disable ring IRQ */
2047 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2049 /* Return here if interrupt is shared and is disabled. */
2050 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2053 napi_schedule(&bnapi->napi);
2057 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2060 struct bnxt_napi *bnapi = cpr->bnapi;
2061 u32 raw_cons = cpr->cp_raw_cons;
2066 struct tx_cmp *txcmp;
2068 cpr->has_more_work = 0;
2072 cons = RING_CMP(raw_cons);
2073 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2075 if (!TX_CMP_VALID(txcmp, raw_cons))
2078 /* The valid test of the entry must be done first before
2079 * reading any further.
2082 cpr->had_work_done = 1;
2083 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2085 /* return full budget so NAPI will complete. */
2086 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
2088 raw_cons = NEXT_RAW_CMP(raw_cons);
2090 cpr->has_more_work = 1;
2093 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2095 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2097 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2099 if (likely(rc >= 0))
2101 /* Increment rx_pkts when rc is -ENOMEM to count towards
2102 * the NAPI budget. Otherwise, we may potentially loop
2103 * here forever if we consistently cannot allocate
2106 else if (rc == -ENOMEM && budget)
2108 else if (rc == -EBUSY) /* partial completion */
2110 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2111 CMPL_BASE_TYPE_HWRM_DONE) ||
2112 (TX_CMP_TYPE(txcmp) ==
2113 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2114 (TX_CMP_TYPE(txcmp) ==
2115 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2116 bnxt_hwrm_handler(bp, txcmp);
2118 raw_cons = NEXT_RAW_CMP(raw_cons);
2120 if (rx_pkts && rx_pkts == budget) {
2121 cpr->has_more_work = 1;
2126 if (event & BNXT_REDIRECT_EVENT)
2129 if (event & BNXT_TX_EVENT) {
2130 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2131 u16 prod = txr->tx_prod;
2133 /* Sync BD data before updating doorbell */
2136 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2139 cpr->cp_raw_cons = raw_cons;
2140 bnapi->tx_pkts += tx_pkts;
2141 bnapi->events |= event;
2145 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2147 if (bnapi->tx_pkts) {
2148 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2152 if (bnapi->events & BNXT_RX_EVENT) {
2153 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2155 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2156 if (bnapi->events & BNXT_AGG_EVENT)
2157 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2162 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2165 struct bnxt_napi *bnapi = cpr->bnapi;
2168 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2170 /* ACK completion ring before freeing tx ring and producing new
2171 * buffers in rx/agg rings to prevent overflowing the completion
2174 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2176 __bnxt_poll_work_done(bp, bnapi);
2180 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2182 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2183 struct bnxt *bp = bnapi->bp;
2184 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2185 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2186 struct tx_cmp *txcmp;
2187 struct rx_cmp_ext *rxcmp1;
2188 u32 cp_cons, tmp_raw_cons;
2189 u32 raw_cons = cpr->cp_raw_cons;
2196 cp_cons = RING_CMP(raw_cons);
2197 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2199 if (!TX_CMP_VALID(txcmp, raw_cons))
2202 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2203 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2204 cp_cons = RING_CMP(tmp_raw_cons);
2205 rxcmp1 = (struct rx_cmp_ext *)
2206 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2208 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2211 /* force an error to recycle the buffer */
2212 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2213 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2215 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2216 if (likely(rc == -EIO) && budget)
2218 else if (rc == -EBUSY) /* partial completion */
2220 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2221 CMPL_BASE_TYPE_HWRM_DONE)) {
2222 bnxt_hwrm_handler(bp, txcmp);
2225 "Invalid completion received on special ring\n");
2227 raw_cons = NEXT_RAW_CMP(raw_cons);
2229 if (rx_pkts == budget)
2233 cpr->cp_raw_cons = raw_cons;
2234 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2235 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2237 if (event & BNXT_AGG_EVENT)
2238 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2240 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2241 napi_complete_done(napi, rx_pkts);
2242 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2247 static int bnxt_poll(struct napi_struct *napi, int budget)
2249 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2250 struct bnxt *bp = bnapi->bp;
2251 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2255 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2257 if (work_done >= budget) {
2259 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2263 if (!bnxt_has_work(bp, cpr)) {
2264 if (napi_complete_done(napi, work_done))
2265 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2269 if (bp->flags & BNXT_FLAG_DIM) {
2270 struct dim_sample dim_sample = {};
2272 dim_update_sample(cpr->event_ctr,
2276 net_dim(&cpr->dim, dim_sample);
2281 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2283 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2284 int i, work_done = 0;
2286 for (i = 0; i < 2; i++) {
2287 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2290 work_done += __bnxt_poll_work(bp, cpr2,
2291 budget - work_done);
2292 cpr->has_more_work |= cpr2->has_more_work;
2298 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2299 u64 dbr_type, bool all)
2301 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2304 for (i = 0; i < 2; i++) {
2305 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2306 struct bnxt_db_info *db;
2308 if (cpr2 && (all || cpr2->had_work_done)) {
2310 writeq(db->db_key64 | dbr_type |
2311 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2312 cpr2->had_work_done = 0;
2315 __bnxt_poll_work_done(bp, bnapi);
2318 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2320 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2321 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2322 u32 raw_cons = cpr->cp_raw_cons;
2323 struct bnxt *bp = bnapi->bp;
2324 struct nqe_cn *nqcmp;
2328 if (cpr->has_more_work) {
2329 cpr->has_more_work = 0;
2330 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2331 if (cpr->has_more_work) {
2332 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, false);
2335 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, true);
2336 if (napi_complete_done(napi, work_done))
2337 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, cpr->cp_raw_cons);
2341 cons = RING_CMP(raw_cons);
2342 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2344 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2345 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
2347 cpr->cp_raw_cons = raw_cons;
2348 if (napi_complete_done(napi, work_done))
2349 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2354 /* The valid test of the entry must be done first before
2355 * reading any further.
2359 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2360 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2361 struct bnxt_cp_ring_info *cpr2;
2363 cpr2 = cpr->cp_ring_arr[idx];
2364 work_done += __bnxt_poll_work(bp, cpr2,
2365 budget - work_done);
2366 cpr->has_more_work = cpr2->has_more_work;
2368 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2370 raw_cons = NEXT_RAW_CMP(raw_cons);
2371 if (cpr->has_more_work)
2374 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, true);
2375 cpr->cp_raw_cons = raw_cons;
2379 static void bnxt_free_tx_skbs(struct bnxt *bp)
2382 struct pci_dev *pdev = bp->pdev;
2387 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2388 for (i = 0; i < bp->tx_nr_rings; i++) {
2389 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2392 for (j = 0; j < max_idx;) {
2393 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2394 struct sk_buff *skb;
2397 if (i < bp->tx_nr_rings_xdp &&
2398 tx_buf->action == XDP_REDIRECT) {
2399 dma_unmap_single(&pdev->dev,
2400 dma_unmap_addr(tx_buf, mapping),
2401 dma_unmap_len(tx_buf, len),
2403 xdp_return_frame(tx_buf->xdpf);
2405 tx_buf->xdpf = NULL;
2418 if (tx_buf->is_push) {
2424 dma_unmap_single(&pdev->dev,
2425 dma_unmap_addr(tx_buf, mapping),
2429 last = tx_buf->nr_frags;
2431 for (k = 0; k < last; k++, j++) {
2432 int ring_idx = j & bp->tx_ring_mask;
2433 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2435 tx_buf = &txr->tx_buf_ring[ring_idx];
2438 dma_unmap_addr(tx_buf, mapping),
2439 skb_frag_size(frag), PCI_DMA_TODEVICE);
2443 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2447 static void bnxt_free_rx_skbs(struct bnxt *bp)
2449 int i, max_idx, max_agg_idx;
2450 struct pci_dev *pdev = bp->pdev;
2455 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2456 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2457 for (i = 0; i < bp->rx_nr_rings; i++) {
2458 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2459 struct bnxt_tpa_idx_map *map;
2463 for (j = 0; j < bp->max_tpa; j++) {
2464 struct bnxt_tpa_info *tpa_info =
2466 u8 *data = tpa_info->data;
2471 dma_unmap_single_attrs(&pdev->dev,
2473 bp->rx_buf_use_size,
2475 DMA_ATTR_WEAK_ORDERING);
2477 tpa_info->data = NULL;
2483 for (j = 0; j < max_idx; j++) {
2484 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2485 dma_addr_t mapping = rx_buf->mapping;
2486 void *data = rx_buf->data;
2491 rx_buf->data = NULL;
2493 if (BNXT_RX_PAGE_MODE(bp)) {
2494 mapping -= bp->rx_dma_offset;
2495 dma_unmap_page_attrs(&pdev->dev, mapping,
2496 PAGE_SIZE, bp->rx_dir,
2497 DMA_ATTR_WEAK_ORDERING);
2498 page_pool_recycle_direct(rxr->page_pool, data);
2500 dma_unmap_single_attrs(&pdev->dev, mapping,
2501 bp->rx_buf_use_size,
2503 DMA_ATTR_WEAK_ORDERING);
2508 for (j = 0; j < max_agg_idx; j++) {
2509 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2510 &rxr->rx_agg_ring[j];
2511 struct page *page = rx_agg_buf->page;
2516 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2519 DMA_ATTR_WEAK_ORDERING);
2521 rx_agg_buf->page = NULL;
2522 __clear_bit(j, rxr->rx_agg_bmap);
2527 __free_page(rxr->rx_page);
2528 rxr->rx_page = NULL;
2530 map = rxr->rx_tpa_idx_map;
2532 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2536 static void bnxt_free_skbs(struct bnxt *bp)
2538 bnxt_free_tx_skbs(bp);
2539 bnxt_free_rx_skbs(bp);
2542 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2544 struct pci_dev *pdev = bp->pdev;
2547 for (i = 0; i < rmem->nr_pages; i++) {
2548 if (!rmem->pg_arr[i])
2551 dma_free_coherent(&pdev->dev, rmem->page_size,
2552 rmem->pg_arr[i], rmem->dma_arr[i]);
2554 rmem->pg_arr[i] = NULL;
2557 size_t pg_tbl_size = rmem->nr_pages * 8;
2559 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2560 pg_tbl_size = rmem->page_size;
2561 dma_free_coherent(&pdev->dev, pg_tbl_size,
2562 rmem->pg_tbl, rmem->pg_tbl_map);
2563 rmem->pg_tbl = NULL;
2565 if (rmem->vmem_size && *rmem->vmem) {
2571 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2573 struct pci_dev *pdev = bp->pdev;
2577 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2578 valid_bit = PTU_PTE_VALID;
2579 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2580 size_t pg_tbl_size = rmem->nr_pages * 8;
2582 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2583 pg_tbl_size = rmem->page_size;
2584 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2591 for (i = 0; i < rmem->nr_pages; i++) {
2592 u64 extra_bits = valid_bit;
2594 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2598 if (!rmem->pg_arr[i])
2601 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2602 if (i == rmem->nr_pages - 2 &&
2603 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2604 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2605 else if (i == rmem->nr_pages - 1 &&
2606 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2607 extra_bits |= PTU_PTE_LAST;
2609 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2613 if (rmem->vmem_size) {
2614 *rmem->vmem = vzalloc(rmem->vmem_size);
2621 static void bnxt_free_tpa_info(struct bnxt *bp)
2625 for (i = 0; i < bp->rx_nr_rings; i++) {
2626 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2628 kfree(rxr->rx_tpa_idx_map);
2629 rxr->rx_tpa_idx_map = NULL;
2631 kfree(rxr->rx_tpa[0].agg_arr);
2632 rxr->rx_tpa[0].agg_arr = NULL;
2639 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2641 int i, j, total_aggs = 0;
2643 bp->max_tpa = MAX_TPA;
2644 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2645 if (!bp->max_tpa_v2)
2647 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2648 total_aggs = bp->max_tpa * MAX_SKB_FRAGS;
2651 for (i = 0; i < bp->rx_nr_rings; i++) {
2652 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2653 struct rx_agg_cmp *agg;
2655 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2660 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2662 agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL);
2663 rxr->rx_tpa[0].agg_arr = agg;
2666 for (j = 1; j < bp->max_tpa; j++)
2667 rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS;
2668 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2670 if (!rxr->rx_tpa_idx_map)
2676 static void bnxt_free_rx_rings(struct bnxt *bp)
2683 bnxt_free_tpa_info(bp);
2684 for (i = 0; i < bp->rx_nr_rings; i++) {
2685 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2686 struct bnxt_ring_struct *ring;
2689 bpf_prog_put(rxr->xdp_prog);
2691 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2692 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2694 page_pool_destroy(rxr->page_pool);
2695 rxr->page_pool = NULL;
2697 kfree(rxr->rx_agg_bmap);
2698 rxr->rx_agg_bmap = NULL;
2700 ring = &rxr->rx_ring_struct;
2701 bnxt_free_ring(bp, &ring->ring_mem);
2703 ring = &rxr->rx_agg_ring_struct;
2704 bnxt_free_ring(bp, &ring->ring_mem);
2708 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2709 struct bnxt_rx_ring_info *rxr)
2711 struct page_pool_params pp = { 0 };
2713 pp.pool_size = bp->rx_ring_size;
2714 pp.nid = dev_to_node(&bp->pdev->dev);
2715 pp.dev = &bp->pdev->dev;
2716 pp.dma_dir = DMA_BIDIRECTIONAL;
2718 rxr->page_pool = page_pool_create(&pp);
2719 if (IS_ERR(rxr->page_pool)) {
2720 int err = PTR_ERR(rxr->page_pool);
2722 rxr->page_pool = NULL;
2728 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2730 int i, rc = 0, agg_rings = 0;
2735 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2738 for (i = 0; i < bp->rx_nr_rings; i++) {
2739 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2740 struct bnxt_ring_struct *ring;
2742 ring = &rxr->rx_ring_struct;
2744 rc = bnxt_alloc_rx_page_pool(bp, rxr);
2748 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2752 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2756 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2760 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2768 ring = &rxr->rx_agg_ring_struct;
2769 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2774 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2775 mem_size = rxr->rx_agg_bmap_size / 8;
2776 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2777 if (!rxr->rx_agg_bmap)
2781 if (bp->flags & BNXT_FLAG_TPA)
2782 rc = bnxt_alloc_tpa_info(bp);
2786 static void bnxt_free_tx_rings(struct bnxt *bp)
2789 struct pci_dev *pdev = bp->pdev;
2794 for (i = 0; i < bp->tx_nr_rings; i++) {
2795 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2796 struct bnxt_ring_struct *ring;
2799 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2800 txr->tx_push, txr->tx_push_mapping);
2801 txr->tx_push = NULL;
2804 ring = &txr->tx_ring_struct;
2806 bnxt_free_ring(bp, &ring->ring_mem);
2810 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2813 struct pci_dev *pdev = bp->pdev;
2815 bp->tx_push_size = 0;
2816 if (bp->tx_push_thresh) {
2819 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2820 bp->tx_push_thresh);
2822 if (push_size > 256) {
2824 bp->tx_push_thresh = 0;
2827 bp->tx_push_size = push_size;
2830 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2831 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2832 struct bnxt_ring_struct *ring;
2835 ring = &txr->tx_ring_struct;
2837 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2841 ring->grp_idx = txr->bnapi->index;
2842 if (bp->tx_push_size) {
2845 /* One pre-allocated DMA buffer to backup
2848 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2850 &txr->tx_push_mapping,
2856 mapping = txr->tx_push_mapping +
2857 sizeof(struct tx_push_bd);
2858 txr->data_mapping = cpu_to_le64(mapping);
2860 qidx = bp->tc_to_qidx[j];
2861 ring->queue_id = bp->q_info[qidx].queue_id;
2862 if (i < bp->tx_nr_rings_xdp)
2864 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2870 static void bnxt_free_cp_rings(struct bnxt *bp)
2877 for (i = 0; i < bp->cp_nr_rings; i++) {
2878 struct bnxt_napi *bnapi = bp->bnapi[i];
2879 struct bnxt_cp_ring_info *cpr;
2880 struct bnxt_ring_struct *ring;
2886 cpr = &bnapi->cp_ring;
2887 ring = &cpr->cp_ring_struct;
2889 bnxt_free_ring(bp, &ring->ring_mem);
2891 for (j = 0; j < 2; j++) {
2892 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2895 ring = &cpr2->cp_ring_struct;
2896 bnxt_free_ring(bp, &ring->ring_mem);
2898 cpr->cp_ring_arr[j] = NULL;
2904 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
2906 struct bnxt_ring_mem_info *rmem;
2907 struct bnxt_ring_struct *ring;
2908 struct bnxt_cp_ring_info *cpr;
2911 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
2915 ring = &cpr->cp_ring_struct;
2916 rmem = &ring->ring_mem;
2917 rmem->nr_pages = bp->cp_nr_pages;
2918 rmem->page_size = HW_CMPD_RING_SIZE;
2919 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2920 rmem->dma_arr = cpr->cp_desc_mapping;
2921 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
2922 rc = bnxt_alloc_ring(bp, rmem);
2924 bnxt_free_ring(bp, rmem);
2931 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2933 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
2934 int i, rc, ulp_base_vec, ulp_msix;
2936 ulp_msix = bnxt_get_ulp_msix_num(bp);
2937 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
2938 for (i = 0; i < bp->cp_nr_rings; i++) {
2939 struct bnxt_napi *bnapi = bp->bnapi[i];
2940 struct bnxt_cp_ring_info *cpr;
2941 struct bnxt_ring_struct *ring;
2946 cpr = &bnapi->cp_ring;
2948 ring = &cpr->cp_ring_struct;
2950 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2954 if (ulp_msix && i >= ulp_base_vec)
2955 ring->map_idx = i + ulp_msix;
2959 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2962 if (i < bp->rx_nr_rings) {
2963 struct bnxt_cp_ring_info *cpr2 =
2964 bnxt_alloc_cp_sub_ring(bp);
2966 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
2969 cpr2->bnapi = bnapi;
2971 if ((sh && i < bp->tx_nr_rings) ||
2972 (!sh && i >= bp->rx_nr_rings)) {
2973 struct bnxt_cp_ring_info *cpr2 =
2974 bnxt_alloc_cp_sub_ring(bp);
2976 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
2979 cpr2->bnapi = bnapi;
2985 static void bnxt_init_ring_struct(struct bnxt *bp)
2989 for (i = 0; i < bp->cp_nr_rings; i++) {
2990 struct bnxt_napi *bnapi = bp->bnapi[i];
2991 struct bnxt_ring_mem_info *rmem;
2992 struct bnxt_cp_ring_info *cpr;
2993 struct bnxt_rx_ring_info *rxr;
2994 struct bnxt_tx_ring_info *txr;
2995 struct bnxt_ring_struct *ring;
3000 cpr = &bnapi->cp_ring;
3001 ring = &cpr->cp_ring_struct;
3002 rmem = &ring->ring_mem;
3003 rmem->nr_pages = bp->cp_nr_pages;
3004 rmem->page_size = HW_CMPD_RING_SIZE;
3005 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3006 rmem->dma_arr = cpr->cp_desc_mapping;
3007 rmem->vmem_size = 0;
3009 rxr = bnapi->rx_ring;
3013 ring = &rxr->rx_ring_struct;
3014 rmem = &ring->ring_mem;
3015 rmem->nr_pages = bp->rx_nr_pages;
3016 rmem->page_size = HW_RXBD_RING_SIZE;
3017 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3018 rmem->dma_arr = rxr->rx_desc_mapping;
3019 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3020 rmem->vmem = (void **)&rxr->rx_buf_ring;
3022 ring = &rxr->rx_agg_ring_struct;
3023 rmem = &ring->ring_mem;
3024 rmem->nr_pages = bp->rx_agg_nr_pages;
3025 rmem->page_size = HW_RXBD_RING_SIZE;
3026 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3027 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3028 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3029 rmem->vmem = (void **)&rxr->rx_agg_ring;
3032 txr = bnapi->tx_ring;
3036 ring = &txr->tx_ring_struct;
3037 rmem = &ring->ring_mem;
3038 rmem->nr_pages = bp->tx_nr_pages;
3039 rmem->page_size = HW_RXBD_RING_SIZE;
3040 rmem->pg_arr = (void **)txr->tx_desc_ring;
3041 rmem->dma_arr = txr->tx_desc_mapping;
3042 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3043 rmem->vmem = (void **)&txr->tx_buf_ring;
3047 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3051 struct rx_bd **rx_buf_ring;
3053 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3054 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3058 rxbd = rx_buf_ring[i];
3062 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3063 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3064 rxbd->rx_bd_opaque = prod;
3069 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3071 struct net_device *dev = bp->dev;
3072 struct bnxt_rx_ring_info *rxr;
3073 struct bnxt_ring_struct *ring;
3077 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3078 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3080 if (NET_IP_ALIGN == 2)
3081 type |= RX_BD_FLAGS_SOP;
3083 rxr = &bp->rx_ring[ring_nr];
3084 ring = &rxr->rx_ring_struct;
3085 bnxt_init_rxbd_pages(ring, type);
3087 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3088 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
3089 if (IS_ERR(rxr->xdp_prog)) {
3090 int rc = PTR_ERR(rxr->xdp_prog);
3092 rxr->xdp_prog = NULL;
3096 prod = rxr->rx_prod;
3097 for (i = 0; i < bp->rx_ring_size; i++) {
3098 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
3099 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3100 ring_nr, i, bp->rx_ring_size);
3103 prod = NEXT_RX(prod);
3105 rxr->rx_prod = prod;
3106 ring->fw_ring_id = INVALID_HW_RING_ID;
3108 ring = &rxr->rx_agg_ring_struct;
3109 ring->fw_ring_id = INVALID_HW_RING_ID;
3111 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3114 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3115 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3117 bnxt_init_rxbd_pages(ring, type);
3119 prod = rxr->rx_agg_prod;
3120 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3121 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
3122 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3123 ring_nr, i, bp->rx_ring_size);
3126 prod = NEXT_RX_AGG(prod);
3128 rxr->rx_agg_prod = prod;
3130 if (bp->flags & BNXT_FLAG_TPA) {
3135 for (i = 0; i < bp->max_tpa; i++) {
3136 data = __bnxt_alloc_rx_data(bp, &mapping,
3141 rxr->rx_tpa[i].data = data;
3142 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3143 rxr->rx_tpa[i].mapping = mapping;
3146 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
3154 static void bnxt_init_cp_rings(struct bnxt *bp)
3158 for (i = 0; i < bp->cp_nr_rings; i++) {
3159 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3160 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3162 ring->fw_ring_id = INVALID_HW_RING_ID;
3163 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3164 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3165 for (j = 0; j < 2; j++) {
3166 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3171 ring = &cpr2->cp_ring_struct;
3172 ring->fw_ring_id = INVALID_HW_RING_ID;
3173 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3174 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3179 static int bnxt_init_rx_rings(struct bnxt *bp)
3183 if (BNXT_RX_PAGE_MODE(bp)) {
3184 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3185 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3187 bp->rx_offset = BNXT_RX_OFFSET;
3188 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3191 for (i = 0; i < bp->rx_nr_rings; i++) {
3192 rc = bnxt_init_one_rx_ring(bp, i);
3200 static int bnxt_init_tx_rings(struct bnxt *bp)
3204 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3207 for (i = 0; i < bp->tx_nr_rings; i++) {
3208 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3209 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3211 ring->fw_ring_id = INVALID_HW_RING_ID;
3217 static void bnxt_free_ring_grps(struct bnxt *bp)
3219 kfree(bp->grp_info);
3220 bp->grp_info = NULL;
3223 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3228 bp->grp_info = kcalloc(bp->cp_nr_rings,
3229 sizeof(struct bnxt_ring_grp_info),
3234 for (i = 0; i < bp->cp_nr_rings; i++) {
3236 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3237 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3238 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3239 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3240 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3245 static void bnxt_free_vnics(struct bnxt *bp)
3247 kfree(bp->vnic_info);
3248 bp->vnic_info = NULL;
3252 static int bnxt_alloc_vnics(struct bnxt *bp)
3256 #ifdef CONFIG_RFS_ACCEL
3257 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3258 num_vnics += bp->rx_nr_rings;
3261 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3264 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3269 bp->nr_vnics = num_vnics;
3273 static void bnxt_init_vnics(struct bnxt *bp)
3277 for (i = 0; i < bp->nr_vnics; i++) {
3278 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3281 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3282 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3283 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3285 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3287 if (bp->vnic_info[i].rss_hash_key) {
3289 prandom_bytes(vnic->rss_hash_key,
3292 memcpy(vnic->rss_hash_key,
3293 bp->vnic_info[0].rss_hash_key,
3299 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3303 pages = ring_size / desc_per_pg;
3310 while (pages & (pages - 1))
3316 void bnxt_set_tpa_flags(struct bnxt *bp)
3318 bp->flags &= ~BNXT_FLAG_TPA;
3319 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3321 if (bp->dev->features & NETIF_F_LRO)
3322 bp->flags |= BNXT_FLAG_LRO;
3323 else if (bp->dev->features & NETIF_F_GRO_HW)
3324 bp->flags |= BNXT_FLAG_GRO;
3327 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3330 void bnxt_set_ring_params(struct bnxt *bp)
3332 u32 ring_size, rx_size, rx_space;
3333 u32 agg_factor = 0, agg_ring_size = 0;
3335 /* 8 for CRC and VLAN */
3336 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3338 rx_space = rx_size + NET_SKB_PAD +
3339 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3341 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3342 ring_size = bp->rx_ring_size;
3343 bp->rx_agg_ring_size = 0;
3344 bp->rx_agg_nr_pages = 0;
3346 if (bp->flags & BNXT_FLAG_TPA)
3347 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3349 bp->flags &= ~BNXT_FLAG_JUMBO;
3350 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3353 bp->flags |= BNXT_FLAG_JUMBO;
3354 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3355 if (jumbo_factor > agg_factor)
3356 agg_factor = jumbo_factor;
3358 agg_ring_size = ring_size * agg_factor;
3360 if (agg_ring_size) {
3361 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3363 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3364 u32 tmp = agg_ring_size;
3366 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3367 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3368 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3369 tmp, agg_ring_size);
3371 bp->rx_agg_ring_size = agg_ring_size;
3372 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3373 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3374 rx_space = rx_size + NET_SKB_PAD +
3375 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3378 bp->rx_buf_use_size = rx_size;
3379 bp->rx_buf_size = rx_space;
3381 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3382 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3384 ring_size = bp->tx_ring_size;
3385 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3386 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3388 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
3389 bp->cp_ring_size = ring_size;
3391 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3392 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3393 bp->cp_nr_pages = MAX_CP_PAGES;
3394 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3395 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3396 ring_size, bp->cp_ring_size);
3398 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3399 bp->cp_ring_mask = bp->cp_bit - 1;
3402 /* Changing allocation mode of RX rings.
3403 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3405 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3408 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3411 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3412 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3413 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3414 bp->rx_dir = DMA_BIDIRECTIONAL;
3415 bp->rx_skb_func = bnxt_rx_page_skb;
3416 /* Disable LRO or GRO_HW */
3417 netdev_update_features(bp->dev);
3419 bp->dev->max_mtu = bp->max_mtu;
3420 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3421 bp->rx_dir = DMA_FROM_DEVICE;
3422 bp->rx_skb_func = bnxt_rx_skb;
3427 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3430 struct bnxt_vnic_info *vnic;
3431 struct pci_dev *pdev = bp->pdev;
3436 for (i = 0; i < bp->nr_vnics; i++) {
3437 vnic = &bp->vnic_info[i];
3439 kfree(vnic->fw_grp_ids);
3440 vnic->fw_grp_ids = NULL;
3442 kfree(vnic->uc_list);
3443 vnic->uc_list = NULL;
3445 if (vnic->mc_list) {
3446 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3447 vnic->mc_list, vnic->mc_list_mapping);
3448 vnic->mc_list = NULL;
3451 if (vnic->rss_table) {
3452 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3454 vnic->rss_table_dma_addr);
3455 vnic->rss_table = NULL;
3458 vnic->rss_hash_key = NULL;
3463 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3465 int i, rc = 0, size;
3466 struct bnxt_vnic_info *vnic;
3467 struct pci_dev *pdev = bp->pdev;
3470 for (i = 0; i < bp->nr_vnics; i++) {
3471 vnic = &bp->vnic_info[i];
3473 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3474 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3477 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3478 if (!vnic->uc_list) {
3485 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3486 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3488 dma_alloc_coherent(&pdev->dev,
3490 &vnic->mc_list_mapping,
3492 if (!vnic->mc_list) {
3498 if (bp->flags & BNXT_FLAG_CHIP_P5)
3499 goto vnic_skip_grps;
3501 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3502 max_rings = bp->rx_nr_rings;
3506 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3507 if (!vnic->fw_grp_ids) {
3512 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3513 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3516 /* Allocate rss table and hash key */
3517 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3518 &vnic->rss_table_dma_addr,
3520 if (!vnic->rss_table) {
3525 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3527 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3528 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3536 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3538 struct pci_dev *pdev = bp->pdev;
3540 if (bp->hwrm_cmd_resp_addr) {
3541 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3542 bp->hwrm_cmd_resp_dma_addr);
3543 bp->hwrm_cmd_resp_addr = NULL;
3546 if (bp->hwrm_cmd_kong_resp_addr) {
3547 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3548 bp->hwrm_cmd_kong_resp_addr,
3549 bp->hwrm_cmd_kong_resp_dma_addr);
3550 bp->hwrm_cmd_kong_resp_addr = NULL;
3554 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3556 struct pci_dev *pdev = bp->pdev;
3558 bp->hwrm_cmd_kong_resp_addr =
3559 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3560 &bp->hwrm_cmd_kong_resp_dma_addr,
3562 if (!bp->hwrm_cmd_kong_resp_addr)
3568 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3570 struct pci_dev *pdev = bp->pdev;
3572 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3573 &bp->hwrm_cmd_resp_dma_addr,
3575 if (!bp->hwrm_cmd_resp_addr)
3581 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3583 if (bp->hwrm_short_cmd_req_addr) {
3584 struct pci_dev *pdev = bp->pdev;
3586 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3587 bp->hwrm_short_cmd_req_addr,
3588 bp->hwrm_short_cmd_req_dma_addr);
3589 bp->hwrm_short_cmd_req_addr = NULL;
3593 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3595 struct pci_dev *pdev = bp->pdev;
3597 bp->hwrm_short_cmd_req_addr =
3598 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3599 &bp->hwrm_short_cmd_req_dma_addr,
3601 if (!bp->hwrm_short_cmd_req_addr)
3607 static void bnxt_free_port_stats(struct bnxt *bp)
3609 struct pci_dev *pdev = bp->pdev;
3611 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3612 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3614 if (bp->hw_rx_port_stats) {
3615 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3616 bp->hw_rx_port_stats,
3617 bp->hw_rx_port_stats_map);
3618 bp->hw_rx_port_stats = NULL;
3621 if (bp->hw_tx_port_stats_ext) {
3622 dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
3623 bp->hw_tx_port_stats_ext,
3624 bp->hw_tx_port_stats_ext_map);
3625 bp->hw_tx_port_stats_ext = NULL;
3628 if (bp->hw_rx_port_stats_ext) {
3629 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3630 bp->hw_rx_port_stats_ext,
3631 bp->hw_rx_port_stats_ext_map);
3632 bp->hw_rx_port_stats_ext = NULL;
3635 if (bp->hw_pcie_stats) {
3636 dma_free_coherent(&pdev->dev, sizeof(struct pcie_ctx_hw_stats),
3637 bp->hw_pcie_stats, bp->hw_pcie_stats_map);
3638 bp->hw_pcie_stats = NULL;
3642 static void bnxt_free_ring_stats(struct bnxt *bp)
3644 struct pci_dev *pdev = bp->pdev;
3650 size = bp->hw_ring_stats_size;
3652 for (i = 0; i < bp->cp_nr_rings; i++) {
3653 struct bnxt_napi *bnapi = bp->bnapi[i];
3654 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3656 if (cpr->hw_stats) {
3657 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3659 cpr->hw_stats = NULL;
3664 static int bnxt_alloc_stats(struct bnxt *bp)
3667 struct pci_dev *pdev = bp->pdev;
3669 size = bp->hw_ring_stats_size;
3671 for (i = 0; i < bp->cp_nr_rings; i++) {
3672 struct bnxt_napi *bnapi = bp->bnapi[i];
3673 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3675 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3681 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3684 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
3687 if (bp->hw_rx_port_stats)
3688 goto alloc_ext_stats;
3690 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3691 sizeof(struct tx_port_stats) + 1024;
3693 bp->hw_rx_port_stats =
3694 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3695 &bp->hw_rx_port_stats_map,
3697 if (!bp->hw_rx_port_stats)
3700 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) + 512;
3701 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3702 sizeof(struct rx_port_stats) + 512;
3703 bp->flags |= BNXT_FLAG_PORT_STATS;
3706 /* Display extended statistics only if FW supports it */
3707 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
3708 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
3711 if (bp->hw_rx_port_stats_ext)
3712 goto alloc_tx_ext_stats;
3714 bp->hw_rx_port_stats_ext =
3715 dma_alloc_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3716 &bp->hw_rx_port_stats_ext_map, GFP_KERNEL);
3717 if (!bp->hw_rx_port_stats_ext)
3721 if (bp->hw_tx_port_stats_ext)
3722 goto alloc_pcie_stats;
3724 if (bp->hwrm_spec_code >= 0x10902 ||
3725 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
3726 bp->hw_tx_port_stats_ext =
3727 dma_alloc_coherent(&pdev->dev,
3728 sizeof(struct tx_port_stats_ext),
3729 &bp->hw_tx_port_stats_ext_map,
3732 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3735 if (bp->hw_pcie_stats ||
3736 !(bp->fw_cap & BNXT_FW_CAP_PCIE_STATS_SUPPORTED))
3740 dma_alloc_coherent(&pdev->dev, sizeof(struct pcie_ctx_hw_stats),
3741 &bp->hw_pcie_stats_map, GFP_KERNEL);
3742 if (!bp->hw_pcie_stats)
3745 bp->flags |= BNXT_FLAG_PCIE_STATS;
3749 static void bnxt_clear_ring_indices(struct bnxt *bp)
3756 for (i = 0; i < bp->cp_nr_rings; i++) {
3757 struct bnxt_napi *bnapi = bp->bnapi[i];
3758 struct bnxt_cp_ring_info *cpr;
3759 struct bnxt_rx_ring_info *rxr;
3760 struct bnxt_tx_ring_info *txr;
3765 cpr = &bnapi->cp_ring;
3766 cpr->cp_raw_cons = 0;
3768 txr = bnapi->tx_ring;
3774 rxr = bnapi->rx_ring;
3777 rxr->rx_agg_prod = 0;
3778 rxr->rx_sw_agg_prod = 0;
3779 rxr->rx_next_cons = 0;
3784 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3786 #ifdef CONFIG_RFS_ACCEL
3789 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3790 * safe to delete the hash table.
3792 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3793 struct hlist_head *head;
3794 struct hlist_node *tmp;
3795 struct bnxt_ntuple_filter *fltr;
3797 head = &bp->ntp_fltr_hash_tbl[i];
3798 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3799 hlist_del(&fltr->hash);
3804 kfree(bp->ntp_fltr_bmap);
3805 bp->ntp_fltr_bmap = NULL;
3807 bp->ntp_fltr_count = 0;
3811 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3813 #ifdef CONFIG_RFS_ACCEL
3816 if (!(bp->flags & BNXT_FLAG_RFS))
3819 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3820 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3822 bp->ntp_fltr_count = 0;
3823 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3827 if (!bp->ntp_fltr_bmap)
3836 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3838 bnxt_free_vnic_attributes(bp);
3839 bnxt_free_tx_rings(bp);
3840 bnxt_free_rx_rings(bp);
3841 bnxt_free_cp_rings(bp);
3842 bnxt_free_ntp_fltrs(bp, irq_re_init);
3844 bnxt_free_ring_stats(bp);
3845 bnxt_free_ring_grps(bp);
3846 bnxt_free_vnics(bp);
3847 kfree(bp->tx_ring_map);
3848 bp->tx_ring_map = NULL;
3856 bnxt_clear_ring_indices(bp);
3860 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3862 int i, j, rc, size, arr_size;
3866 /* Allocate bnapi mem pointer array and mem block for
3869 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3871 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3872 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3878 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3879 bp->bnapi[i] = bnapi;
3880 bp->bnapi[i]->index = i;
3881 bp->bnapi[i]->bp = bp;
3882 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3883 struct bnxt_cp_ring_info *cpr =
3884 &bp->bnapi[i]->cp_ring;
3886 cpr->cp_ring_struct.ring_mem.flags =
3887 BNXT_RMEM_RING_PTE_FLAG;
3891 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3892 sizeof(struct bnxt_rx_ring_info),
3897 for (i = 0; i < bp->rx_nr_rings; i++) {
3898 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3900 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3901 rxr->rx_ring_struct.ring_mem.flags =
3902 BNXT_RMEM_RING_PTE_FLAG;
3903 rxr->rx_agg_ring_struct.ring_mem.flags =
3904 BNXT_RMEM_RING_PTE_FLAG;
3906 rxr->bnapi = bp->bnapi[i];
3907 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3910 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3911 sizeof(struct bnxt_tx_ring_info),
3916 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3919 if (!bp->tx_ring_map)
3922 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3925 j = bp->rx_nr_rings;
3927 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3928 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3930 if (bp->flags & BNXT_FLAG_CHIP_P5)
3931 txr->tx_ring_struct.ring_mem.flags =
3932 BNXT_RMEM_RING_PTE_FLAG;
3933 txr->bnapi = bp->bnapi[j];
3934 bp->bnapi[j]->tx_ring = txr;
3935 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
3936 if (i >= bp->tx_nr_rings_xdp) {
3937 txr->txq_index = i - bp->tx_nr_rings_xdp;
3938 bp->bnapi[j]->tx_int = bnxt_tx_int;
3940 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
3941 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3945 rc = bnxt_alloc_stats(bp);
3949 rc = bnxt_alloc_ntp_fltrs(bp);
3953 rc = bnxt_alloc_vnics(bp);
3958 bnxt_init_ring_struct(bp);
3960 rc = bnxt_alloc_rx_rings(bp);
3964 rc = bnxt_alloc_tx_rings(bp);
3968 rc = bnxt_alloc_cp_rings(bp);
3972 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3973 BNXT_VNIC_UCAST_FLAG;
3974 rc = bnxt_alloc_vnic_attributes(bp);
3980 bnxt_free_mem(bp, true);
3984 static void bnxt_disable_int(struct bnxt *bp)
3991 for (i = 0; i < bp->cp_nr_rings; i++) {
3992 struct bnxt_napi *bnapi = bp->bnapi[i];
3993 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3994 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3996 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3997 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4001 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4003 struct bnxt_napi *bnapi = bp->bnapi[n];
4004 struct bnxt_cp_ring_info *cpr;
4006 cpr = &bnapi->cp_ring;
4007 return cpr->cp_ring_struct.map_idx;
4010 static void bnxt_disable_int_sync(struct bnxt *bp)
4014 atomic_inc(&bp->intr_sem);
4016 bnxt_disable_int(bp);
4017 for (i = 0; i < bp->cp_nr_rings; i++) {
4018 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4020 synchronize_irq(bp->irq_tbl[map_idx].vector);
4024 static void bnxt_enable_int(struct bnxt *bp)
4028 atomic_set(&bp->intr_sem, 0);
4029 for (i = 0; i < bp->cp_nr_rings; i++) {
4030 struct bnxt_napi *bnapi = bp->bnapi[i];
4031 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4033 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4037 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4038 u16 cmpl_ring, u16 target_id)
4040 struct input *req = request;
4042 req->req_type = cpu_to_le16(req_type);
4043 req->cmpl_ring = cpu_to_le16(cmpl_ring);
4044 req->target_id = cpu_to_le16(target_id);
4045 if (bnxt_kong_hwrm_message(bp, req))
4046 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4048 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4051 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4052 int timeout, bool silent)
4054 int i, intr_process, rc, tmo_count;
4055 struct input *req = msg;
4059 u16 cp_ring_id, len = 0;
4060 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4061 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4062 struct hwrm_short_input short_input = {0};
4063 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4064 u8 *resp_addr = (u8 *)bp->hwrm_cmd_resp_addr;
4065 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4066 u16 dst = BNXT_HWRM_CHNL_CHIMP;
4068 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4069 if (msg_len > bp->hwrm_max_ext_req_len ||
4070 !bp->hwrm_short_cmd_req_addr)
4074 if (bnxt_hwrm_kong_chnl(bp, req)) {
4075 dst = BNXT_HWRM_CHNL_KONG;
4076 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4077 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4078 resp = bp->hwrm_cmd_kong_resp_addr;
4079 resp_addr = (u8 *)bp->hwrm_cmd_kong_resp_addr;
4082 memset(resp, 0, PAGE_SIZE);
4083 cp_ring_id = le16_to_cpu(req->cmpl_ring);
4084 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4086 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4087 /* currently supports only one outstanding message */
4089 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4091 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4092 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4093 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4096 /* Set boundary for maximum extended request length for short
4097 * cmd format. If passed up from device use the max supported
4098 * internal req length.
4100 max_msg_len = bp->hwrm_max_ext_req_len;
4102 memcpy(short_cmd_req, req, msg_len);
4103 if (msg_len < max_msg_len)
4104 memset(short_cmd_req + msg_len, 0,
4105 max_msg_len - msg_len);
4107 short_input.req_type = req->req_type;
4108 short_input.signature =
4109 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4110 short_input.size = cpu_to_le16(msg_len);
4111 short_input.req_addr =
4112 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4114 data = (u32 *)&short_input;
4115 msg_len = sizeof(short_input);
4117 /* Sync memory write before updating doorbell */
4120 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4123 /* Write request msg to hwrm channel */
4124 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4126 for (i = msg_len; i < max_req_len; i += 4)
4127 writel(0, bp->bar0 + bar_offset + i);
4129 /* Ring channel doorbell */
4130 writel(1, bp->bar0 + doorbell_offset);
4133 timeout = DFLT_HWRM_CMD_TIMEOUT;
4134 /* convert timeout to usec */
4138 /* Short timeout for the first few iterations:
4139 * number of loops = number of loops for short timeout +
4140 * number of loops for standard timeout.
4142 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4143 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4144 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4145 resp_len = (__le32 *)(resp_addr + HWRM_RESP_LEN_OFFSET);
4148 u16 seq_id = bp->hwrm_intr_seq_id;
4150 /* Wait until hwrm response cmpl interrupt is processed */
4151 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4153 /* on first few passes, just barely sleep */
4154 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4155 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4156 HWRM_SHORT_MAX_TIMEOUT);
4158 usleep_range(HWRM_MIN_TIMEOUT,
4162 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4163 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4164 le16_to_cpu(req->req_type));
4167 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
4169 valid = resp_addr + len - 1;
4173 /* Check if response len is updated */
4174 for (i = 0; i < tmo_count; i++) {
4175 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
4179 /* on first few passes, just barely sleep */
4180 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4181 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4182 HWRM_SHORT_MAX_TIMEOUT);
4184 usleep_range(HWRM_MIN_TIMEOUT,
4188 if (i >= tmo_count) {
4189 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4190 HWRM_TOTAL_TIMEOUT(i),
4191 le16_to_cpu(req->req_type),
4192 le16_to_cpu(req->seq_id), len);
4196 /* Last byte of resp contains valid bit */
4197 valid = resp_addr + len - 1;
4198 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4199 /* make sure we read from updated DMA memory */
4206 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4207 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4208 HWRM_TOTAL_TIMEOUT(i),
4209 le16_to_cpu(req->req_type),
4210 le16_to_cpu(req->seq_id), len, *valid);
4215 /* Zero valid bit for compatibility. Valid bit in an older spec
4216 * may become a new field in a newer spec. We must make sure that
4217 * a new field not implemented by old spec will read zero.
4220 rc = le16_to_cpu(resp->error_code);
4222 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4223 le16_to_cpu(resp->req_type),
4224 le16_to_cpu(resp->seq_id), rc);
4228 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4230 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4233 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4236 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4239 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4243 mutex_lock(&bp->hwrm_cmd_lock);
4244 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4245 mutex_unlock(&bp->hwrm_cmd_lock);
4249 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4254 mutex_lock(&bp->hwrm_cmd_lock);
4255 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4256 mutex_unlock(&bp->hwrm_cmd_lock);
4260 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
4263 struct hwrm_func_drv_rgtr_input req = {0};
4264 DECLARE_BITMAP(async_events_bmap, 256);
4265 u32 *events = (u32 *)async_events_bmap;
4268 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4271 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4273 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4274 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
4275 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4277 if (bmap && bmap_size) {
4278 for (i = 0; i < bmap_size; i++) {
4279 if (test_bit(i, bmap))
4280 __set_bit(i, async_events_bmap);
4284 for (i = 0; i < 8; i++)
4285 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4287 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4290 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
4292 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4293 struct hwrm_func_drv_rgtr_input req = {0};
4296 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4299 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4300 FUNC_DRV_RGTR_REQ_ENABLES_VER);
4302 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4303 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
4304 req.ver_maj_8b = DRV_VER_MAJ;
4305 req.ver_min_8b = DRV_VER_MIN;
4306 req.ver_upd_8b = DRV_VER_UPD;
4307 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4308 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4309 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4315 memset(data, 0, sizeof(data));
4316 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4317 u16 cmd = bnxt_vf_req_snif[i];
4318 unsigned int bit, idx;
4322 data[idx] |= 1 << bit;
4325 for (i = 0; i < 8; i++)
4326 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4329 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4332 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4333 req.flags |= cpu_to_le32(
4334 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4336 mutex_lock(&bp->hwrm_cmd_lock);
4337 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4340 else if (resp->flags &
4341 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4342 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4343 mutex_unlock(&bp->hwrm_cmd_lock);
4347 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4349 struct hwrm_func_drv_unrgtr_input req = {0};
4351 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4352 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4355 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4358 struct hwrm_tunnel_dst_port_free_input req = {0};
4360 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4361 req.tunnel_type = tunnel_type;
4363 switch (tunnel_type) {
4364 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4365 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
4367 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4368 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
4374 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4376 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4381 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4385 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4386 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4388 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4390 req.tunnel_type = tunnel_type;
4391 req.tunnel_dst_port_val = port;
4393 mutex_lock(&bp->hwrm_cmd_lock);
4394 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4396 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4401 switch (tunnel_type) {
4402 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4403 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
4405 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4406 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
4413 mutex_unlock(&bp->hwrm_cmd_lock);
4417 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4419 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4420 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4422 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4423 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4425 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4426 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4427 req.mask = cpu_to_le32(vnic->rx_mask);
4428 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4431 #ifdef CONFIG_RFS_ACCEL
4432 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4433 struct bnxt_ntuple_filter *fltr)
4435 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4437 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4438 req.ntuple_filter_id = fltr->filter_id;
4439 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4442 #define BNXT_NTP_FLTR_FLAGS \
4443 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4444 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4445 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4446 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4447 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4448 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4449 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4450 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4451 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4452 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4453 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4454 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4455 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4456 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4458 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4459 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4461 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4462 struct bnxt_ntuple_filter *fltr)
4464 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4465 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4466 struct flow_keys *keys = &fltr->fkeys;
4467 struct bnxt_vnic_info *vnic;
4471 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4472 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4474 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX) {
4475 dst_ena = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
4476 req.rfs_ring_tbl_idx = cpu_to_le16(fltr->rxq);
4477 vnic = &bp->vnic_info[0];
4479 vnic = &bp->vnic_info[fltr->rxq + 1];
4481 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4482 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS | dst_ena);
4484 req.ethertype = htons(ETH_P_IP);
4485 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4486 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4487 req.ip_protocol = keys->basic.ip_proto;
4489 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4492 req.ethertype = htons(ETH_P_IPV6);
4494 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4495 *(struct in6_addr *)&req.src_ipaddr[0] =
4496 keys->addrs.v6addrs.src;
4497 *(struct in6_addr *)&req.dst_ipaddr[0] =
4498 keys->addrs.v6addrs.dst;
4499 for (i = 0; i < 4; i++) {
4500 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4501 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4504 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4505 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4506 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4507 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4509 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4510 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4512 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4515 req.src_port = keys->ports.src;
4516 req.src_port_mask = cpu_to_be16(0xffff);
4517 req.dst_port = keys->ports.dst;
4518 req.dst_port_mask = cpu_to_be16(0xffff);
4520 mutex_lock(&bp->hwrm_cmd_lock);
4521 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4523 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4524 fltr->filter_id = resp->ntuple_filter_id;
4526 mutex_unlock(&bp->hwrm_cmd_lock);
4531 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4535 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4536 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4538 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4539 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4540 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4542 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4543 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4545 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4546 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4547 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4548 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4549 req.l2_addr_mask[0] = 0xff;
4550 req.l2_addr_mask[1] = 0xff;
4551 req.l2_addr_mask[2] = 0xff;
4552 req.l2_addr_mask[3] = 0xff;
4553 req.l2_addr_mask[4] = 0xff;
4554 req.l2_addr_mask[5] = 0xff;
4556 mutex_lock(&bp->hwrm_cmd_lock);
4557 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4559 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4561 mutex_unlock(&bp->hwrm_cmd_lock);
4565 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4567 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4570 /* Any associated ntuple filters will also be cleared by firmware. */
4571 mutex_lock(&bp->hwrm_cmd_lock);
4572 for (i = 0; i < num_of_vnics; i++) {
4573 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4575 for (j = 0; j < vnic->uc_filter_count; j++) {
4576 struct hwrm_cfa_l2_filter_free_input req = {0};
4578 bnxt_hwrm_cmd_hdr_init(bp, &req,
4579 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4581 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4583 rc = _hwrm_send_message(bp, &req, sizeof(req),
4586 vnic->uc_filter_count = 0;
4588 mutex_unlock(&bp->hwrm_cmd_lock);
4593 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4595 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4596 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
4597 struct hwrm_vnic_tpa_cfg_input req = {0};
4599 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4602 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4605 u16 mss = bp->dev->mtu - 40;
4606 u32 nsegs, n, segs = 0, flags;
4608 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4609 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4610 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4611 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4612 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4613 if (tpa_flags & BNXT_FLAG_GRO)
4614 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4616 req.flags = cpu_to_le32(flags);
4619 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
4620 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4621 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
4623 /* Number of segs are log2 units, and first packet is not
4624 * included as part of this units.
4626 if (mss <= BNXT_RX_PAGE_SIZE) {
4627 n = BNXT_RX_PAGE_SIZE / mss;
4628 nsegs = (MAX_SKB_FRAGS - 1) * n;
4630 n = mss / BNXT_RX_PAGE_SIZE;
4631 if (mss & (BNXT_RX_PAGE_SIZE - 1))
4633 nsegs = (MAX_SKB_FRAGS - n) / n;
4636 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4637 segs = MAX_TPA_SEGS_P5;
4638 max_aggs = bp->max_tpa;
4640 segs = ilog2(nsegs);
4642 req.max_agg_segs = cpu_to_le16(segs);
4643 req.max_aggs = cpu_to_le16(max_aggs);
4645 req.min_agg_len = cpu_to_le32(512);
4647 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4649 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4652 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4654 struct bnxt_ring_grp_info *grp_info;
4656 grp_info = &bp->grp_info[ring->grp_idx];
4657 return grp_info->cp_fw_ring_id;
4660 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4662 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4663 struct bnxt_napi *bnapi = rxr->bnapi;
4664 struct bnxt_cp_ring_info *cpr;
4666 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4667 return cpr->cp_ring_struct.fw_ring_id;
4669 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4673 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4675 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4676 struct bnxt_napi *bnapi = txr->bnapi;
4677 struct bnxt_cp_ring_info *cpr;
4679 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4680 return cpr->cp_ring_struct.fw_ring_id;
4682 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4686 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
4688 u32 i, j, max_rings;
4689 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4690 struct hwrm_vnic_rss_cfg_input req = {0};
4692 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
4693 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
4696 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4698 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4699 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4700 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
4701 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4702 max_rings = bp->rx_nr_rings - 1;
4704 max_rings = bp->rx_nr_rings;
4709 /* Fill the RSS indirection table with ring group ids */
4710 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4713 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4716 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4717 req.hash_key_tbl_addr =
4718 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4720 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4721 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4724 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
4726 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4727 u32 i, j, k, nr_ctxs, max_rings = bp->rx_nr_rings;
4728 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4729 struct hwrm_vnic_rss_cfg_input req = {0};
4731 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4732 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4734 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4737 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4738 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4739 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4740 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
4741 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
4742 for (i = 0, k = 0; i < nr_ctxs; i++) {
4743 __le16 *ring_tbl = vnic->rss_table;
4746 req.ring_table_pair_index = i;
4747 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
4748 for (j = 0; j < 64; j++) {
4751 ring_id = rxr->rx_ring_struct.fw_ring_id;
4752 *ring_tbl++ = cpu_to_le16(ring_id);
4753 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
4754 *ring_tbl++ = cpu_to_le16(ring_id);
4757 if (k == max_rings) {
4759 rxr = &bp->rx_ring[0];
4762 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4769 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4771 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4772 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4775 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4776 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4777 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4779 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4780 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4781 /* thresholds not implemented in firmware yet */
4782 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4783 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4784 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4785 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4788 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4791 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4793 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4794 req.rss_cos_lb_ctx_id =
4795 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
4797 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4798 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
4801 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4805 for (i = 0; i < bp->nr_vnics; i++) {
4806 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4808 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4809 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4810 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4813 bp->rsscos_nr_ctxs = 0;
4816 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
4819 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4820 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4821 bp->hwrm_cmd_resp_addr;
4823 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4826 mutex_lock(&bp->hwrm_cmd_lock);
4827 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4829 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
4830 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4831 mutex_unlock(&bp->hwrm_cmd_lock);
4836 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4838 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4839 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4840 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4843 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
4845 unsigned int ring = 0, grp_idx;
4846 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4847 struct hwrm_vnic_cfg_input req = {0};
4850 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
4852 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4853 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4855 req.default_rx_ring_id =
4856 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
4857 req.default_cmpl_ring_id =
4858 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
4860 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
4861 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
4864 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4865 /* Only RSS support for now TBD: COS & LB */
4866 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4867 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4868 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4869 VNIC_CFG_REQ_ENABLES_MRU);
4870 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4872 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4873 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4874 VNIC_CFG_REQ_ENABLES_MRU);
4875 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
4877 req.rss_rule = cpu_to_le16(0xffff);
4880 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4881 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
4882 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4883 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4885 req.cos_rule = cpu_to_le16(0xffff);
4888 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4890 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
4892 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4893 ring = bp->rx_nr_rings - 1;
4895 grp_idx = bp->rx_ring[ring].bnapi->index;
4896 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4897 req.lb_rule = cpu_to_le16(0xffff);
4899 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4902 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4903 #ifdef CONFIG_BNXT_SRIOV
4905 def_vlan = bp->vf.vlan;
4907 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
4908 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
4909 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4910 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
4912 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4915 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4919 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4920 struct hwrm_vnic_free_input req = {0};
4922 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4924 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4926 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4929 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4934 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4938 for (i = 0; i < bp->nr_vnics; i++)
4939 bnxt_hwrm_vnic_free_one(bp, i);
4942 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4943 unsigned int start_rx_ring_idx,
4944 unsigned int nr_rings)
4947 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
4948 struct hwrm_vnic_alloc_input req = {0};
4949 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4950 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4952 if (bp->flags & BNXT_FLAG_CHIP_P5)
4953 goto vnic_no_ring_grps;
4955 /* map ring groups to this vnic */
4956 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4957 grp_idx = bp->rx_ring[i].bnapi->index;
4958 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
4959 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
4963 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
4967 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
4968 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
4970 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4972 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4974 mutex_lock(&bp->hwrm_cmd_lock);
4975 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4977 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
4978 mutex_unlock(&bp->hwrm_cmd_lock);
4982 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4984 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4985 struct hwrm_vnic_qcaps_input req = {0};
4988 if (bp->hwrm_spec_code < 0x10600)
4991 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4992 mutex_lock(&bp->hwrm_cmd_lock);
4993 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4995 u32 flags = le32_to_cpu(resp->flags);
4997 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
4998 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4999 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5001 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5002 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5003 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5005 bp->hw_ring_stats_size =
5006 sizeof(struct ctx_hw_stats_ext);
5008 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5010 mutex_unlock(&bp->hwrm_cmd_lock);
5014 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5019 if (bp->flags & BNXT_FLAG_CHIP_P5)
5022 mutex_lock(&bp->hwrm_cmd_lock);
5023 for (i = 0; i < bp->rx_nr_rings; i++) {
5024 struct hwrm_ring_grp_alloc_input req = {0};
5025 struct hwrm_ring_grp_alloc_output *resp =
5026 bp->hwrm_cmd_resp_addr;
5027 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5029 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5031 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5032 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5033 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5034 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5036 rc = _hwrm_send_message(bp, &req, sizeof(req),
5041 bp->grp_info[grp_idx].fw_grp_id =
5042 le32_to_cpu(resp->ring_group_id);
5044 mutex_unlock(&bp->hwrm_cmd_lock);
5048 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5052 struct hwrm_ring_grp_free_input req = {0};
5054 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5057 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5059 mutex_lock(&bp->hwrm_cmd_lock);
5060 for (i = 0; i < bp->cp_nr_rings; i++) {
5061 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5064 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5066 rc = _hwrm_send_message(bp, &req, sizeof(req),
5070 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5072 mutex_unlock(&bp->hwrm_cmd_lock);
5076 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5077 struct bnxt_ring_struct *ring,
5078 u32 ring_type, u32 map_index)
5080 int rc = 0, err = 0;
5081 struct hwrm_ring_alloc_input req = {0};
5082 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5083 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5084 struct bnxt_ring_grp_info *grp_info;
5087 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5090 if (rmem->nr_pages > 1) {
5091 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5092 /* Page size is in log2 units */
5093 req.page_size = BNXT_PAGE_SHIFT;
5094 req.page_tbl_depth = 1;
5096 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5099 /* Association of ring index with doorbell index and MSIX number */
5100 req.logical_id = cpu_to_le16(map_index);
5102 switch (ring_type) {
5103 case HWRM_RING_ALLOC_TX: {
5104 struct bnxt_tx_ring_info *txr;
5106 txr = container_of(ring, struct bnxt_tx_ring_info,
5108 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5109 /* Association of transmit ring with completion ring */
5110 grp_info = &bp->grp_info[ring->grp_idx];
5111 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5112 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5113 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5114 req.queue_id = cpu_to_le16(ring->queue_id);
5117 case HWRM_RING_ALLOC_RX:
5118 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5119 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5120 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5123 /* Association of rx ring with stats context */
5124 grp_info = &bp->grp_info[ring->grp_idx];
5125 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5126 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5127 req.enables |= cpu_to_le32(
5128 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5129 if (NET_IP_ALIGN == 2)
5130 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5131 req.flags = cpu_to_le16(flags);
5134 case HWRM_RING_ALLOC_AGG:
5135 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5136 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5137 /* Association of agg ring with rx ring */
5138 grp_info = &bp->grp_info[ring->grp_idx];
5139 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5140 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5141 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5142 req.enables |= cpu_to_le32(
5143 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5144 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5146 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5148 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5150 case HWRM_RING_ALLOC_CMPL:
5151 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5152 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5153 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5154 /* Association of cp ring with nq */
5155 grp_info = &bp->grp_info[map_index];
5156 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5157 req.cq_handle = cpu_to_le64(ring->handle);
5158 req.enables |= cpu_to_le32(
5159 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5160 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5161 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5164 case HWRM_RING_ALLOC_NQ:
5165 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5166 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5167 if (bp->flags & BNXT_FLAG_USING_MSIX)
5168 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5171 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5176 mutex_lock(&bp->hwrm_cmd_lock);
5177 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5178 err = le16_to_cpu(resp->error_code);
5179 ring_id = le16_to_cpu(resp->ring_id);
5180 mutex_unlock(&bp->hwrm_cmd_lock);
5183 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5184 ring_type, rc, err);
5187 ring->fw_ring_id = ring_id;
5191 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5196 struct hwrm_func_cfg_input req = {0};
5198 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5199 req.fid = cpu_to_le16(0xffff);
5200 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5201 req.async_event_cr = cpu_to_le16(idx);
5202 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5204 struct hwrm_func_vf_cfg_input req = {0};
5206 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5208 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5209 req.async_event_cr = cpu_to_le16(idx);
5210 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5215 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5216 u32 map_idx, u32 xid)
5218 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5220 db->doorbell = bp->bar1 + 0x10000;
5222 db->doorbell = bp->bar1 + 0x4000;
5223 switch (ring_type) {
5224 case HWRM_RING_ALLOC_TX:
5225 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5227 case HWRM_RING_ALLOC_RX:
5228 case HWRM_RING_ALLOC_AGG:
5229 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5231 case HWRM_RING_ALLOC_CMPL:
5232 db->db_key64 = DBR_PATH_L2;
5234 case HWRM_RING_ALLOC_NQ:
5235 db->db_key64 = DBR_PATH_L2;
5238 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5240 db->doorbell = bp->bar1 + map_idx * 0x80;
5241 switch (ring_type) {
5242 case HWRM_RING_ALLOC_TX:
5243 db->db_key32 = DB_KEY_TX;
5245 case HWRM_RING_ALLOC_RX:
5246 case HWRM_RING_ALLOC_AGG:
5247 db->db_key32 = DB_KEY_RX;
5249 case HWRM_RING_ALLOC_CMPL:
5250 db->db_key32 = DB_KEY_CP;
5256 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5261 if (bp->flags & BNXT_FLAG_CHIP_P5)
5262 type = HWRM_RING_ALLOC_NQ;
5264 type = HWRM_RING_ALLOC_CMPL;
5265 for (i = 0; i < bp->cp_nr_rings; i++) {
5266 struct bnxt_napi *bnapi = bp->bnapi[i];
5267 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5268 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5269 u32 map_idx = ring->map_idx;
5270 unsigned int vector;
5272 vector = bp->irq_tbl[map_idx].vector;
5273 disable_irq_nosync(vector);
5274 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5279 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5280 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5282 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5285 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5287 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5291 type = HWRM_RING_ALLOC_TX;
5292 for (i = 0; i < bp->tx_nr_rings; i++) {
5293 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5294 struct bnxt_ring_struct *ring;
5297 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5298 struct bnxt_napi *bnapi = txr->bnapi;
5299 struct bnxt_cp_ring_info *cpr, *cpr2;
5300 u32 type2 = HWRM_RING_ALLOC_CMPL;
5302 cpr = &bnapi->cp_ring;
5303 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5304 ring = &cpr2->cp_ring_struct;
5305 ring->handle = BNXT_TX_HDL;
5306 map_idx = bnapi->index;
5307 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5310 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5312 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5314 ring = &txr->tx_ring_struct;
5316 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5319 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5322 type = HWRM_RING_ALLOC_RX;
5323 for (i = 0; i < bp->rx_nr_rings; i++) {
5324 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5325 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5326 struct bnxt_napi *bnapi = rxr->bnapi;
5327 u32 map_idx = bnapi->index;
5329 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5332 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5333 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5334 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5335 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5336 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5337 u32 type2 = HWRM_RING_ALLOC_CMPL;
5338 struct bnxt_cp_ring_info *cpr2;
5340 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5341 ring = &cpr2->cp_ring_struct;
5342 ring->handle = BNXT_RX_HDL;
5343 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5346 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5348 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5352 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5353 type = HWRM_RING_ALLOC_AGG;
5354 for (i = 0; i < bp->rx_nr_rings; i++) {
5355 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5356 struct bnxt_ring_struct *ring =
5357 &rxr->rx_agg_ring_struct;
5358 u32 grp_idx = ring->grp_idx;
5359 u32 map_idx = grp_idx + bp->rx_nr_rings;
5361 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5365 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5367 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5368 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5375 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5376 struct bnxt_ring_struct *ring,
5377 u32 ring_type, int cmpl_ring_id)
5380 struct hwrm_ring_free_input req = {0};
5381 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5384 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5385 req.ring_type = ring_type;
5386 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5388 mutex_lock(&bp->hwrm_cmd_lock);
5389 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5390 error_code = le16_to_cpu(resp->error_code);
5391 mutex_unlock(&bp->hwrm_cmd_lock);
5393 if (rc || error_code) {
5394 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5395 ring_type, rc, error_code);
5401 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5409 for (i = 0; i < bp->tx_nr_rings; i++) {
5410 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5411 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5413 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5414 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5416 hwrm_ring_free_send_msg(bp, ring,
5417 RING_FREE_REQ_RING_TYPE_TX,
5418 close_path ? cmpl_ring_id :
5419 INVALID_HW_RING_ID);
5420 ring->fw_ring_id = INVALID_HW_RING_ID;
5424 for (i = 0; i < bp->rx_nr_rings; i++) {
5425 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5426 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5427 u32 grp_idx = rxr->bnapi->index;
5429 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5430 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5432 hwrm_ring_free_send_msg(bp, ring,
5433 RING_FREE_REQ_RING_TYPE_RX,
5434 close_path ? cmpl_ring_id :
5435 INVALID_HW_RING_ID);
5436 ring->fw_ring_id = INVALID_HW_RING_ID;
5437 bp->grp_info[grp_idx].rx_fw_ring_id =
5442 if (bp->flags & BNXT_FLAG_CHIP_P5)
5443 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5445 type = RING_FREE_REQ_RING_TYPE_RX;
5446 for (i = 0; i < bp->rx_nr_rings; i++) {
5447 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5448 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5449 u32 grp_idx = rxr->bnapi->index;
5451 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5452 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5454 hwrm_ring_free_send_msg(bp, ring, type,
5455 close_path ? cmpl_ring_id :
5456 INVALID_HW_RING_ID);
5457 ring->fw_ring_id = INVALID_HW_RING_ID;
5458 bp->grp_info[grp_idx].agg_fw_ring_id =
5463 /* The completion rings are about to be freed. After that the
5464 * IRQ doorbell will not work anymore. So we need to disable
5467 bnxt_disable_int_sync(bp);
5469 if (bp->flags & BNXT_FLAG_CHIP_P5)
5470 type = RING_FREE_REQ_RING_TYPE_NQ;
5472 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5473 for (i = 0; i < bp->cp_nr_rings; i++) {
5474 struct bnxt_napi *bnapi = bp->bnapi[i];
5475 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5476 struct bnxt_ring_struct *ring;
5479 for (j = 0; j < 2; j++) {
5480 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5483 ring = &cpr2->cp_ring_struct;
5484 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5486 hwrm_ring_free_send_msg(bp, ring,
5487 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5488 INVALID_HW_RING_ID);
5489 ring->fw_ring_id = INVALID_HW_RING_ID;
5492 ring = &cpr->cp_ring_struct;
5493 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5494 hwrm_ring_free_send_msg(bp, ring, type,
5495 INVALID_HW_RING_ID);
5496 ring->fw_ring_id = INVALID_HW_RING_ID;
5497 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5502 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5505 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5507 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5508 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5509 struct hwrm_func_qcfg_input req = {0};
5512 if (bp->hwrm_spec_code < 0x10601)
5515 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5516 req.fid = cpu_to_le16(0xffff);
5517 mutex_lock(&bp->hwrm_cmd_lock);
5518 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5520 mutex_unlock(&bp->hwrm_cmd_lock);
5524 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5525 if (BNXT_NEW_RM(bp)) {
5528 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5529 hw_resc->resv_hw_ring_grps =
5530 le32_to_cpu(resp->alloc_hw_ring_grps);
5531 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5532 cp = le16_to_cpu(resp->alloc_cmpl_rings);
5533 stats = le16_to_cpu(resp->alloc_stat_ctx);
5534 hw_resc->resv_irqs = cp;
5535 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5536 int rx = hw_resc->resv_rx_rings;
5537 int tx = hw_resc->resv_tx_rings;
5539 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5541 if (cp < (rx + tx)) {
5542 bnxt_trim_rings(bp, &rx, &tx, cp, false);
5543 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5545 hw_resc->resv_rx_rings = rx;
5546 hw_resc->resv_tx_rings = tx;
5548 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
5549 hw_resc->resv_hw_ring_grps = rx;
5551 hw_resc->resv_cp_rings = cp;
5552 hw_resc->resv_stat_ctxs = stats;
5554 mutex_unlock(&bp->hwrm_cmd_lock);
5558 /* Caller must hold bp->hwrm_cmd_lock */
5559 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5561 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5562 struct hwrm_func_qcfg_input req = {0};
5565 if (bp->hwrm_spec_code < 0x10601)
5568 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5569 req.fid = cpu_to_le16(fid);
5570 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5572 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5577 static bool bnxt_rfs_supported(struct bnxt *bp);
5580 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5581 int tx_rings, int rx_rings, int ring_grps,
5582 int cp_rings, int stats, int vnics)
5586 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5587 req->fid = cpu_to_le16(0xffff);
5588 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5589 req->num_tx_rings = cpu_to_le16(tx_rings);
5590 if (BNXT_NEW_RM(bp)) {
5591 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
5592 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5593 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5594 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5595 enables |= tx_rings + ring_grps ?
5596 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5597 enables |= rx_rings ?
5598 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5600 enables |= cp_rings ?
5601 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5602 enables |= ring_grps ?
5603 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5604 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5606 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
5608 req->num_rx_rings = cpu_to_le16(rx_rings);
5609 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5610 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5611 req->num_msix = cpu_to_le16(cp_rings);
5612 req->num_rsscos_ctxs =
5613 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5615 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5616 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5617 req->num_rsscos_ctxs = cpu_to_le16(1);
5618 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5619 bnxt_rfs_supported(bp))
5620 req->num_rsscos_ctxs =
5621 cpu_to_le16(ring_grps + 1);
5623 req->num_stat_ctxs = cpu_to_le16(stats);
5624 req->num_vnics = cpu_to_le16(vnics);
5626 req->enables = cpu_to_le32(enables);
5630 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5631 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5632 int rx_rings, int ring_grps, int cp_rings,
5633 int stats, int vnics)
5637 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5638 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5639 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5640 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5641 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5642 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5643 enables |= tx_rings + ring_grps ?
5644 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5646 enables |= cp_rings ?
5647 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
5648 enables |= ring_grps ?
5649 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5651 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
5652 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
5654 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
5655 req->num_tx_rings = cpu_to_le16(tx_rings);
5656 req->num_rx_rings = cpu_to_le16(rx_rings);
5657 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5658 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5659 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5661 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5662 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5663 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
5665 req->num_stat_ctxs = cpu_to_le16(stats);
5666 req->num_vnics = cpu_to_le16(vnics);
5668 req->enables = cpu_to_le32(enables);
5672 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5673 int ring_grps, int cp_rings, int stats, int vnics)
5675 struct hwrm_func_cfg_input req = {0};
5678 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5679 cp_rings, stats, vnics);
5683 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5687 if (bp->hwrm_spec_code < 0x10601)
5688 bp->hw_resc.resv_tx_rings = tx_rings;
5690 rc = bnxt_hwrm_get_rings(bp);
5695 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5696 int ring_grps, int cp_rings, int stats, int vnics)
5698 struct hwrm_func_vf_cfg_input req = {0};
5701 if (!BNXT_NEW_RM(bp)) {
5702 bp->hw_resc.resv_tx_rings = tx_rings;
5706 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5707 cp_rings, stats, vnics);
5708 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5712 rc = bnxt_hwrm_get_rings(bp);
5716 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
5717 int cp, int stat, int vnic)
5720 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
5723 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
5727 int bnxt_nq_rings_in_use(struct bnxt *bp)
5729 int cp = bp->cp_nr_rings;
5730 int ulp_msix, ulp_base;
5732 ulp_msix = bnxt_get_ulp_msix_num(bp);
5734 ulp_base = bnxt_get_ulp_msix_base(bp);
5736 if ((ulp_base + ulp_msix) > cp)
5737 cp = ulp_base + ulp_msix;
5742 static int bnxt_cp_rings_in_use(struct bnxt *bp)
5746 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5747 return bnxt_nq_rings_in_use(bp);
5749 cp = bp->tx_nr_rings + bp->rx_nr_rings;
5753 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
5755 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
5756 int cp = bp->cp_nr_rings;
5761 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
5762 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
5764 return cp + ulp_stat;
5767 static bool bnxt_need_reserve_rings(struct bnxt *bp)
5769 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5770 int cp = bnxt_cp_rings_in_use(bp);
5771 int nq = bnxt_nq_rings_in_use(bp);
5772 int rx = bp->rx_nr_rings, stat;
5773 int vnic = 1, grp = rx;
5775 if (bp->hwrm_spec_code < 0x10601)
5778 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
5781 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5783 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5785 stat = bnxt_get_func_stat_ctxs(bp);
5786 if (BNXT_NEW_RM(bp) &&
5787 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
5788 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
5789 (hw_resc->resv_hw_ring_grps != grp &&
5790 !(bp->flags & BNXT_FLAG_CHIP_P5))))
5792 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
5793 hw_resc->resv_irqs != nq)
5798 static int __bnxt_reserve_rings(struct bnxt *bp)
5800 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5801 int cp = bnxt_nq_rings_in_use(bp);
5802 int tx = bp->tx_nr_rings;
5803 int rx = bp->rx_nr_rings;
5804 int grp, rx_rings, rc;
5808 if (!bnxt_need_reserve_rings(bp))
5811 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5813 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5815 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5817 grp = bp->rx_nr_rings;
5818 stat = bnxt_get_func_stat_ctxs(bp);
5820 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
5824 tx = hw_resc->resv_tx_rings;
5825 if (BNXT_NEW_RM(bp)) {
5826 rx = hw_resc->resv_rx_rings;
5827 cp = hw_resc->resv_irqs;
5828 grp = hw_resc->resv_hw_ring_grps;
5829 vnic = hw_resc->resv_vnics;
5830 stat = hw_resc->resv_stat_ctxs;
5834 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5838 if (netif_running(bp->dev))
5841 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
5842 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5843 bp->dev->hw_features &= ~NETIF_F_LRO;
5844 bp->dev->features &= ~NETIF_F_LRO;
5845 bnxt_set_ring_params(bp);
5848 rx_rings = min_t(int, rx_rings, grp);
5849 cp = min_t(int, cp, bp->cp_nr_rings);
5850 if (stat > bnxt_get_ulp_stat_ctxs(bp))
5851 stat -= bnxt_get_ulp_stat_ctxs(bp);
5852 cp = min_t(int, cp, stat);
5853 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
5854 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5856 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
5857 bp->tx_nr_rings = tx;
5858 bp->rx_nr_rings = rx_rings;
5859 bp->cp_nr_rings = cp;
5861 if (!tx || !rx || !cp || !grp || !vnic || !stat)
5867 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5868 int ring_grps, int cp_rings, int stats,
5871 struct hwrm_func_vf_cfg_input req = {0};
5875 if (!BNXT_NEW_RM(bp))
5878 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5879 cp_rings, stats, vnics);
5880 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
5881 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5882 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
5883 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5884 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
5885 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5886 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5887 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5889 req.flags = cpu_to_le32(flags);
5890 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5896 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5897 int ring_grps, int cp_rings, int stats,
5900 struct hwrm_func_cfg_input req = {0};
5904 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5905 cp_rings, stats, vnics);
5906 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
5907 if (BNXT_NEW_RM(bp)) {
5908 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5909 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
5910 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5911 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
5912 if (bp->flags & BNXT_FLAG_CHIP_P5)
5913 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
5914 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
5916 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5919 req.flags = cpu_to_le32(flags);
5920 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5926 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5927 int ring_grps, int cp_rings, int stats,
5930 if (bp->hwrm_spec_code < 0x10801)
5934 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
5935 ring_grps, cp_rings, stats,
5938 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
5939 cp_rings, stats, vnics);
5942 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
5944 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5945 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5946 struct hwrm_ring_aggint_qcaps_input req = {0};
5949 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
5950 coal_cap->num_cmpl_dma_aggr_max = 63;
5951 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
5952 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
5953 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
5954 coal_cap->int_lat_tmr_min_max = 65535;
5955 coal_cap->int_lat_tmr_max_max = 65535;
5956 coal_cap->num_cmpl_aggr_int_max = 65535;
5957 coal_cap->timer_units = 80;
5959 if (bp->hwrm_spec_code < 0x10902)
5962 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
5963 mutex_lock(&bp->hwrm_cmd_lock);
5964 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5966 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
5967 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
5968 coal_cap->num_cmpl_dma_aggr_max =
5969 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
5970 coal_cap->num_cmpl_dma_aggr_during_int_max =
5971 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
5972 coal_cap->cmpl_aggr_dma_tmr_max =
5973 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
5974 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
5975 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
5976 coal_cap->int_lat_tmr_min_max =
5977 le16_to_cpu(resp->int_lat_tmr_min_max);
5978 coal_cap->int_lat_tmr_max_max =
5979 le16_to_cpu(resp->int_lat_tmr_max_max);
5980 coal_cap->num_cmpl_aggr_int_max =
5981 le16_to_cpu(resp->num_cmpl_aggr_int_max);
5982 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
5984 mutex_unlock(&bp->hwrm_cmd_lock);
5987 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
5989 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5991 return usec * 1000 / coal_cap->timer_units;
5994 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
5995 struct bnxt_coal *hw_coal,
5996 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
5998 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5999 u32 cmpl_params = coal_cap->cmpl_params;
6000 u16 val, tmr, max, flags = 0;
6002 max = hw_coal->bufs_per_record * 128;
6003 if (hw_coal->budget)
6004 max = hw_coal->bufs_per_record * hw_coal->budget;
6005 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6007 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6008 req->num_cmpl_aggr_int = cpu_to_le16(val);
6010 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6011 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6013 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6014 coal_cap->num_cmpl_dma_aggr_during_int_max);
6015 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6017 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6018 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6019 req->int_lat_tmr_max = cpu_to_le16(tmr);
6021 /* min timer set to 1/2 of interrupt timer */
6022 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6024 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6025 req->int_lat_tmr_min = cpu_to_le16(val);
6026 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6029 /* buf timer set to 1/4 of interrupt timer */
6030 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6031 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6034 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6035 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6036 val = clamp_t(u16, tmr, 1,
6037 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6038 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
6040 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6043 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6044 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6045 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6046 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6047 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6048 req->flags = cpu_to_le16(flags);
6049 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6052 /* Caller holds bp->hwrm_cmd_lock */
6053 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6054 struct bnxt_coal *hw_coal)
6056 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6057 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6058 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6059 u32 nq_params = coal_cap->nq_params;
6062 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6065 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6067 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6069 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6071 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6072 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6073 req.int_lat_tmr_min = cpu_to_le16(tmr);
6074 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6075 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6078 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6080 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6081 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6082 struct bnxt_coal coal;
6084 /* Tick values in micro seconds.
6085 * 1 coal_buf x bufs_per_record = 1 completion record.
6087 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6089 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6090 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6092 if (!bnapi->rx_ring)
6095 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6096 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6098 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6100 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6102 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6106 int bnxt_hwrm_set_coal(struct bnxt *bp)
6109 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6112 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6113 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6114 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6115 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6117 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6118 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6120 mutex_lock(&bp->hwrm_cmd_lock);
6121 for (i = 0; i < bp->cp_nr_rings; i++) {
6122 struct bnxt_napi *bnapi = bp->bnapi[i];
6123 struct bnxt_coal *hw_coal;
6127 if (!bnapi->rx_ring) {
6128 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6131 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6133 req->ring_id = cpu_to_le16(ring_id);
6135 rc = _hwrm_send_message(bp, req, sizeof(*req),
6140 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6143 if (bnapi->rx_ring && bnapi->tx_ring) {
6145 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6146 req->ring_id = cpu_to_le16(ring_id);
6147 rc = _hwrm_send_message(bp, req, sizeof(*req),
6153 hw_coal = &bp->rx_coal;
6155 hw_coal = &bp->tx_coal;
6156 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6158 mutex_unlock(&bp->hwrm_cmd_lock);
6162 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6165 struct hwrm_stat_ctx_free_input req = {0};
6170 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6173 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6175 mutex_lock(&bp->hwrm_cmd_lock);
6176 for (i = 0; i < bp->cp_nr_rings; i++) {
6177 struct bnxt_napi *bnapi = bp->bnapi[i];
6178 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6180 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6181 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6183 rc = _hwrm_send_message(bp, &req, sizeof(req),
6188 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6191 mutex_unlock(&bp->hwrm_cmd_lock);
6195 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6198 struct hwrm_stat_ctx_alloc_input req = {0};
6199 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6201 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6204 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6206 req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6207 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6209 mutex_lock(&bp->hwrm_cmd_lock);
6210 for (i = 0; i < bp->cp_nr_rings; i++) {
6211 struct bnxt_napi *bnapi = bp->bnapi[i];
6212 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6214 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
6216 rc = _hwrm_send_message(bp, &req, sizeof(req),
6221 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6223 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6225 mutex_unlock(&bp->hwrm_cmd_lock);
6229 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6231 struct hwrm_func_qcfg_input req = {0};
6232 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6236 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6237 req.fid = cpu_to_le16(0xffff);
6238 mutex_lock(&bp->hwrm_cmd_lock);
6239 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6241 goto func_qcfg_exit;
6243 #ifdef CONFIG_BNXT_SRIOV
6245 struct bnxt_vf_info *vf = &bp->vf;
6247 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6250 flags = le16_to_cpu(resp->flags);
6251 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6252 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6253 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6254 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6255 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6257 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6258 bp->flags |= BNXT_FLAG_MULTI_HOST;
6260 switch (resp->port_partition_type) {
6261 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6262 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6263 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6264 bp->port_partition_type = resp->port_partition_type;
6267 if (bp->hwrm_spec_code < 0x10707 ||
6268 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6269 bp->br_mode = BRIDGE_MODE_VEB;
6270 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6271 bp->br_mode = BRIDGE_MODE_VEPA;
6273 bp->br_mode = BRIDGE_MODE_UNDEF;
6275 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6277 bp->max_mtu = BNXT_MAX_MTU;
6280 mutex_unlock(&bp->hwrm_cmd_lock);
6284 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6286 struct hwrm_func_backing_store_qcaps_input req = {0};
6287 struct hwrm_func_backing_store_qcaps_output *resp =
6288 bp->hwrm_cmd_resp_addr;
6291 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6294 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6295 mutex_lock(&bp->hwrm_cmd_lock);
6296 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6298 struct bnxt_ctx_pg_info *ctx_pg;
6299 struct bnxt_ctx_mem_info *ctx;
6302 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6307 ctx_pg = kzalloc(sizeof(*ctx_pg) * (bp->max_q + 1), GFP_KERNEL);
6313 for (i = 0; i < bp->max_q + 1; i++, ctx_pg++)
6314 ctx->tqm_mem[i] = ctx_pg;
6317 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6318 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6319 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6320 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6321 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6322 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6323 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6324 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6325 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6326 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6327 ctx->vnic_max_vnic_entries =
6328 le16_to_cpu(resp->vnic_max_vnic_entries);
6329 ctx->vnic_max_ring_table_entries =
6330 le16_to_cpu(resp->vnic_max_ring_table_entries);
6331 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6332 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6333 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6334 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6335 ctx->tqm_min_entries_per_ring =
6336 le32_to_cpu(resp->tqm_min_entries_per_ring);
6337 ctx->tqm_max_entries_per_ring =
6338 le32_to_cpu(resp->tqm_max_entries_per_ring);
6339 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6340 if (!ctx->tqm_entries_multiple)
6341 ctx->tqm_entries_multiple = 1;
6342 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6343 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6344 ctx->mrav_num_entries_units =
6345 le16_to_cpu(resp->mrav_num_entries_units);
6346 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6347 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6352 mutex_unlock(&bp->hwrm_cmd_lock);
6356 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6361 if (BNXT_PAGE_SHIFT == 13)
6363 else if (BNXT_PAGE_SIZE == 16)
6367 if (rmem->depth >= 1) {
6368 if (rmem->depth == 2)
6372 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6374 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6378 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6379 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6380 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6381 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6382 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6383 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6385 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6387 struct hwrm_func_backing_store_cfg_input req = {0};
6388 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6389 struct bnxt_ctx_pg_info *ctx_pg;
6390 __le32 *num_entries;
6400 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6401 req.enables = cpu_to_le32(enables);
6403 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6404 ctx_pg = &ctx->qp_mem;
6405 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6406 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6407 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6408 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6409 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6410 &req.qpc_pg_size_qpc_lvl,
6413 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6414 ctx_pg = &ctx->srq_mem;
6415 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6416 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6417 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6418 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6419 &req.srq_pg_size_srq_lvl,
6422 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6423 ctx_pg = &ctx->cq_mem;
6424 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6425 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6426 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6427 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6430 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6431 ctx_pg = &ctx->vnic_mem;
6432 req.vnic_num_vnic_entries =
6433 cpu_to_le16(ctx->vnic_max_vnic_entries);
6434 req.vnic_num_ring_table_entries =
6435 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6436 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6437 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6438 &req.vnic_pg_size_vnic_lvl,
6439 &req.vnic_page_dir);
6441 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6442 ctx_pg = &ctx->stat_mem;
6443 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6444 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6445 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6446 &req.stat_pg_size_stat_lvl,
6447 &req.stat_page_dir);
6449 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6450 ctx_pg = &ctx->mrav_mem;
6451 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6452 if (ctx->mrav_num_entries_units)
6454 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
6455 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
6456 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6457 &req.mrav_pg_size_mrav_lvl,
6458 &req.mrav_page_dir);
6460 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
6461 ctx_pg = &ctx->tim_mem;
6462 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
6463 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
6464 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6465 &req.tim_pg_size_tim_lvl,
6468 for (i = 0, num_entries = &req.tqm_sp_num_entries,
6469 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
6470 pg_dir = &req.tqm_sp_page_dir,
6471 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
6472 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
6473 if (!(enables & ena))
6476 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
6477 ctx_pg = ctx->tqm_mem[i];
6478 *num_entries = cpu_to_le32(ctx_pg->entries);
6479 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
6481 req.flags = cpu_to_le32(flags);
6482 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6488 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
6489 struct bnxt_ctx_pg_info *ctx_pg)
6491 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6493 rmem->page_size = BNXT_PAGE_SIZE;
6494 rmem->pg_arr = ctx_pg->ctx_pg_arr;
6495 rmem->dma_arr = ctx_pg->ctx_dma_arr;
6496 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
6497 if (rmem->depth >= 1)
6498 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
6499 return bnxt_alloc_ring(bp, rmem);
6502 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
6503 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
6506 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6512 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6513 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
6514 ctx_pg->nr_pages = 0;
6517 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
6521 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
6523 if (!ctx_pg->ctx_pg_tbl)
6525 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
6526 rmem->nr_pages = nr_tbls;
6527 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6530 for (i = 0; i < nr_tbls; i++) {
6531 struct bnxt_ctx_pg_info *pg_tbl;
6533 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
6536 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
6537 rmem = &pg_tbl->ring_mem;
6538 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
6539 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
6541 rmem->nr_pages = MAX_CTX_PAGES;
6542 if (i == (nr_tbls - 1)) {
6543 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
6546 rmem->nr_pages = rem;
6548 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
6553 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6554 if (rmem->nr_pages > 1 || depth)
6556 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6561 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
6562 struct bnxt_ctx_pg_info *ctx_pg)
6564 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6566 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
6567 ctx_pg->ctx_pg_tbl) {
6568 int i, nr_tbls = rmem->nr_pages;
6570 for (i = 0; i < nr_tbls; i++) {
6571 struct bnxt_ctx_pg_info *pg_tbl;
6572 struct bnxt_ring_mem_info *rmem2;
6574 pg_tbl = ctx_pg->ctx_pg_tbl[i];
6577 rmem2 = &pg_tbl->ring_mem;
6578 bnxt_free_ring(bp, rmem2);
6579 ctx_pg->ctx_pg_arr[i] = NULL;
6581 ctx_pg->ctx_pg_tbl[i] = NULL;
6583 kfree(ctx_pg->ctx_pg_tbl);
6584 ctx_pg->ctx_pg_tbl = NULL;
6586 bnxt_free_ring(bp, rmem);
6587 ctx_pg->nr_pages = 0;
6590 static void bnxt_free_ctx_mem(struct bnxt *bp)
6592 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6598 if (ctx->tqm_mem[0]) {
6599 for (i = 0; i < bp->max_q + 1; i++)
6600 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
6601 kfree(ctx->tqm_mem[0]);
6602 ctx->tqm_mem[0] = NULL;
6605 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
6606 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
6607 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
6608 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
6609 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
6610 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
6611 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
6612 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
6615 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
6617 struct bnxt_ctx_pg_info *ctx_pg;
6618 struct bnxt_ctx_mem_info *ctx;
6619 u32 mem_size, ena, entries;
6626 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
6628 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
6633 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
6636 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
6642 ctx_pg = &ctx->qp_mem;
6643 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
6645 mem_size = ctx->qp_entry_size * ctx_pg->entries;
6646 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6650 ctx_pg = &ctx->srq_mem;
6651 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
6652 mem_size = ctx->srq_entry_size * ctx_pg->entries;
6653 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6657 ctx_pg = &ctx->cq_mem;
6658 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
6659 mem_size = ctx->cq_entry_size * ctx_pg->entries;
6660 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6664 ctx_pg = &ctx->vnic_mem;
6665 ctx_pg->entries = ctx->vnic_max_vnic_entries +
6666 ctx->vnic_max_ring_table_entries;
6667 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
6668 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6672 ctx_pg = &ctx->stat_mem;
6673 ctx_pg->entries = ctx->stat_max_entries;
6674 mem_size = ctx->stat_entry_size * ctx_pg->entries;
6675 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6680 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
6683 ctx_pg = &ctx->mrav_mem;
6684 /* 128K extra is needed to accommodate static AH context
6685 * allocation by f/w.
6687 num_mr = 1024 * 256;
6688 num_ah = 1024 * 128;
6689 ctx_pg->entries = num_mr + num_ah;
6690 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
6691 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2);
6694 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
6695 if (ctx->mrav_num_entries_units)
6697 ((num_mr / ctx->mrav_num_entries_units) << 16) |
6698 (num_ah / ctx->mrav_num_entries_units);
6700 ctx_pg = &ctx->tim_mem;
6701 ctx_pg->entries = ctx->qp_mem.entries;
6702 mem_size = ctx->tim_entry_size * ctx_pg->entries;
6703 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6706 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
6709 entries = ctx->qp_max_l2_entries + extra_qps;
6710 entries = roundup(entries, ctx->tqm_entries_multiple);
6711 entries = clamp_t(u32, entries, ctx->tqm_min_entries_per_ring,
6712 ctx->tqm_max_entries_per_ring);
6713 for (i = 0; i < bp->max_q + 1; i++) {
6714 ctx_pg = ctx->tqm_mem[i];
6715 ctx_pg->entries = entries;
6716 mem_size = ctx->tqm_entry_size * entries;
6717 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6720 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
6722 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
6723 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
6725 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
6728 ctx->flags |= BNXT_CTX_FLAG_INITED;
6733 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
6735 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6736 struct hwrm_func_resource_qcaps_input req = {0};
6737 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6740 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
6741 req.fid = cpu_to_le16(0xffff);
6743 mutex_lock(&bp->hwrm_cmd_lock);
6744 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
6748 goto hwrm_func_resc_qcaps_exit;
6751 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
6753 goto hwrm_func_resc_qcaps_exit;
6755 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
6756 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6757 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
6758 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6759 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
6760 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6761 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
6762 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6763 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
6764 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
6765 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
6766 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6767 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
6768 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6769 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
6770 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6772 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6773 u16 max_msix = le16_to_cpu(resp->max_msix);
6775 hw_resc->max_nqs = max_msix;
6776 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
6780 struct bnxt_pf_info *pf = &bp->pf;
6782 pf->vf_resv_strategy =
6783 le16_to_cpu(resp->vf_reservation_strategy);
6784 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
6785 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
6787 hwrm_func_resc_qcaps_exit:
6788 mutex_unlock(&bp->hwrm_cmd_lock);
6792 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
6795 struct hwrm_func_qcaps_input req = {0};
6796 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6797 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6800 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
6801 req.fid = cpu_to_le16(0xffff);
6803 mutex_lock(&bp->hwrm_cmd_lock);
6804 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6806 goto hwrm_func_qcaps_exit;
6808 flags = le32_to_cpu(resp->flags);
6809 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
6810 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
6811 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
6812 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
6813 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
6814 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
6815 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
6816 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
6818 bp->tx_push_thresh = 0;
6819 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
6820 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
6822 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6823 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6824 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6825 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6826 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
6827 if (!hw_resc->max_hw_ring_grps)
6828 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
6829 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6830 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6831 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6834 struct bnxt_pf_info *pf = &bp->pf;
6836 pf->fw_fid = le16_to_cpu(resp->fid);
6837 pf->port_id = le16_to_cpu(resp->port_id);
6838 bp->dev->dev_port = pf->port_id;
6839 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
6840 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
6841 pf->max_vfs = le16_to_cpu(resp->max_vfs);
6842 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
6843 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
6844 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
6845 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
6846 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
6847 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
6848 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
6849 bp->flags |= BNXT_FLAG_WOL_CAP;
6851 #ifdef CONFIG_BNXT_SRIOV
6852 struct bnxt_vf_info *vf = &bp->vf;
6854 vf->fw_fid = le16_to_cpu(resp->fid);
6855 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
6859 hwrm_func_qcaps_exit:
6860 mutex_unlock(&bp->hwrm_cmd_lock);
6864 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
6866 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
6870 rc = __bnxt_hwrm_func_qcaps(bp);
6873 rc = bnxt_hwrm_queue_qportcfg(bp);
6875 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
6878 if (bp->hwrm_spec_code >= 0x10803) {
6879 rc = bnxt_alloc_ctx_mem(bp);
6882 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
6884 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
6889 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
6891 struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
6892 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
6896 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
6899 resp = bp->hwrm_cmd_resp_addr;
6900 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
6902 mutex_lock(&bp->hwrm_cmd_lock);
6903 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6905 goto hwrm_cfa_adv_qcaps_exit;
6907 flags = le32_to_cpu(resp->flags);
6909 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_SUPPORTED)
6910 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX;
6912 hwrm_cfa_adv_qcaps_exit:
6913 mutex_unlock(&bp->hwrm_cmd_lock);
6917 static int bnxt_hwrm_func_reset(struct bnxt *bp)
6919 struct hwrm_func_reset_input req = {0};
6921 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
6924 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
6927 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
6930 struct hwrm_queue_qportcfg_input req = {0};
6931 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
6935 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
6937 mutex_lock(&bp->hwrm_cmd_lock);
6938 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6942 if (!resp->max_configurable_queues) {
6946 bp->max_tc = resp->max_configurable_queues;
6947 bp->max_lltc = resp->max_configurable_lossless_queues;
6948 if (bp->max_tc > BNXT_MAX_QUEUE)
6949 bp->max_tc = BNXT_MAX_QUEUE;
6951 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
6952 qptr = &resp->queue_id0;
6953 for (i = 0, j = 0; i < bp->max_tc; i++) {
6954 bp->q_info[j].queue_id = *qptr;
6955 bp->q_ids[i] = *qptr++;
6956 bp->q_info[j].queue_profile = *qptr++;
6957 bp->tc_to_qidx[j] = j;
6958 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
6959 (no_rdma && BNXT_PF(bp)))
6962 bp->max_q = bp->max_tc;
6963 bp->max_tc = max_t(u8, j, 1);
6965 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
6968 if (bp->max_lltc > bp->max_tc)
6969 bp->max_lltc = bp->max_tc;
6972 mutex_unlock(&bp->hwrm_cmd_lock);
6976 static int bnxt_hwrm_ver_get(struct bnxt *bp)
6979 struct hwrm_ver_get_input req = {0};
6980 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
6983 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
6984 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
6985 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
6986 req.hwrm_intf_min = HWRM_VERSION_MINOR;
6987 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
6988 mutex_lock(&bp->hwrm_cmd_lock);
6989 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6991 goto hwrm_ver_get_exit;
6993 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
6995 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
6996 resp->hwrm_intf_min_8b << 8 |
6997 resp->hwrm_intf_upd_8b;
6998 if (resp->hwrm_intf_maj_8b < 1) {
6999 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7000 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7001 resp->hwrm_intf_upd_8b);
7002 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7004 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
7005 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
7006 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
7008 if (strlen(resp->active_pkg_name)) {
7009 int fw_ver_len = strlen(bp->fw_ver_str);
7011 snprintf(bp->fw_ver_str + fw_ver_len,
7012 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7013 resp->active_pkg_name);
7014 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7017 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7018 if (!bp->hwrm_cmd_timeout)
7019 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7021 if (resp->hwrm_intf_maj_8b >= 1) {
7022 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7023 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7025 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7026 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7028 bp->chip_num = le16_to_cpu(resp->chip_num);
7029 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7031 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7033 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7034 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7035 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7036 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7038 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7039 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7042 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7043 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7046 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7047 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7050 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7051 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7054 mutex_unlock(&bp->hwrm_cmd_lock);
7058 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7060 struct hwrm_fw_set_time_input req = {0};
7062 time64_t now = ktime_get_real_seconds();
7064 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7065 bp->hwrm_spec_code < 0x10400)
7068 time64_to_tm(now, 0, &tm);
7069 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7070 req.year = cpu_to_le16(1900 + tm.tm_year);
7071 req.month = 1 + tm.tm_mon;
7072 req.day = tm.tm_mday;
7073 req.hour = tm.tm_hour;
7074 req.minute = tm.tm_min;
7075 req.second = tm.tm_sec;
7076 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7079 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
7082 struct bnxt_pf_info *pf = &bp->pf;
7083 struct hwrm_port_qstats_input req = {0};
7085 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7089 req.port_id = cpu_to_le16(pf->port_id);
7090 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
7091 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
7092 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7096 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
7098 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7099 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7100 struct hwrm_port_qstats_ext_input req = {0};
7101 struct bnxt_pf_info *pf = &bp->pf;
7105 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7108 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7109 req.port_id = cpu_to_le16(pf->port_id);
7110 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7111 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
7112 tx_stat_size = bp->hw_tx_port_stats_ext ?
7113 sizeof(*bp->hw_tx_port_stats_ext) : 0;
7114 req.tx_stat_size = cpu_to_le16(tx_stat_size);
7115 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
7116 mutex_lock(&bp->hwrm_cmd_lock);
7117 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7119 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7120 bp->fw_tx_stats_ext_size = tx_stat_size ?
7121 le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7123 bp->fw_rx_stats_ext_size = 0;
7124 bp->fw_tx_stats_ext_size = 0;
7126 if (bp->fw_tx_stats_ext_size <=
7127 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7128 mutex_unlock(&bp->hwrm_cmd_lock);
7129 bp->pri2cos_valid = 0;
7133 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
7134 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
7136 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
7138 struct hwrm_queue_pri2cos_qcfg_output *resp2;
7142 resp2 = bp->hwrm_cmd_resp_addr;
7143 pri2cos = &resp2->pri0_cos_queue_id;
7144 for (i = 0; i < 8; i++) {
7145 u8 queue_id = pri2cos[i];
7147 for (j = 0; j < bp->max_q; j++) {
7148 if (bp->q_ids[j] == queue_id)
7152 bp->pri2cos_valid = 1;
7154 mutex_unlock(&bp->hwrm_cmd_lock);
7158 static int bnxt_hwrm_pcie_qstats(struct bnxt *bp)
7160 struct hwrm_pcie_qstats_input req = {0};
7162 if (!(bp->flags & BNXT_FLAG_PCIE_STATS))
7165 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PCIE_QSTATS, -1, -1);
7166 req.pcie_stat_size = cpu_to_le16(sizeof(struct pcie_ctx_hw_stats));
7167 req.pcie_stat_host_addr = cpu_to_le64(bp->hw_pcie_stats_map);
7168 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7171 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
7173 if (bp->vxlan_port_cnt) {
7174 bnxt_hwrm_tunnel_dst_port_free(
7175 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
7177 bp->vxlan_port_cnt = 0;
7178 if (bp->nge_port_cnt) {
7179 bnxt_hwrm_tunnel_dst_port_free(
7180 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
7182 bp->nge_port_cnt = 0;
7185 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
7191 tpa_flags = bp->flags & BNXT_FLAG_TPA;
7192 for (i = 0; i < bp->nr_vnics; i++) {
7193 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
7195 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
7203 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
7207 for (i = 0; i < bp->nr_vnics; i++)
7208 bnxt_hwrm_vnic_set_rss(bp, i, false);
7211 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
7214 if (bp->vnic_info) {
7215 bnxt_hwrm_clear_vnic_filter(bp);
7216 /* clear all RSS setting before free vnic ctx */
7217 bnxt_hwrm_clear_vnic_rss(bp);
7218 bnxt_hwrm_vnic_ctx_free(bp);
7219 /* before free the vnic, undo the vnic tpa settings */
7220 if (bp->flags & BNXT_FLAG_TPA)
7221 bnxt_set_tpa(bp, false);
7222 bnxt_hwrm_vnic_free(bp);
7224 bnxt_hwrm_ring_free(bp, close_path);
7225 bnxt_hwrm_ring_grp_free(bp);
7227 bnxt_hwrm_stat_ctx_free(bp);
7228 bnxt_hwrm_free_tunnel_ports(bp);
7232 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
7234 struct hwrm_func_cfg_input req = {0};
7237 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7238 req.fid = cpu_to_le16(0xffff);
7239 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
7240 if (br_mode == BRIDGE_MODE_VEB)
7241 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
7242 else if (br_mode == BRIDGE_MODE_VEPA)
7243 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
7246 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7252 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
7254 struct hwrm_func_cfg_input req = {0};
7257 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
7260 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
7261 req.fid = cpu_to_le16(0xffff);
7262 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
7263 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
7265 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
7267 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7273 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
7275 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
7278 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
7281 /* allocate context for vnic */
7282 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
7284 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7286 goto vnic_setup_err;
7288 bp->rsscos_nr_ctxs++;
7290 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7291 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
7293 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
7295 goto vnic_setup_err;
7297 bp->rsscos_nr_ctxs++;
7301 /* configure default vnic, ring grp */
7302 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7304 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7306 goto vnic_setup_err;
7309 /* Enable RSS hashing on vnic */
7310 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
7312 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
7314 goto vnic_setup_err;
7317 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7318 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7320 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
7329 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
7333 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
7334 for (i = 0; i < nr_ctxs; i++) {
7335 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
7337 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
7341 bp->rsscos_nr_ctxs++;
7346 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
7348 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
7352 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
7354 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
7358 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7359 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
7361 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
7368 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
7370 if (bp->flags & BNXT_FLAG_CHIP_P5)
7371 return __bnxt_setup_vnic_p5(bp, vnic_id);
7373 return __bnxt_setup_vnic(bp, vnic_id);
7376 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
7378 #ifdef CONFIG_RFS_ACCEL
7381 if (bp->flags & BNXT_FLAG_CHIP_P5)
7384 for (i = 0; i < bp->rx_nr_rings; i++) {
7385 struct bnxt_vnic_info *vnic;
7386 u16 vnic_id = i + 1;
7389 if (vnic_id >= bp->nr_vnics)
7392 vnic = &bp->vnic_info[vnic_id];
7393 vnic->flags |= BNXT_VNIC_RFS_FLAG;
7394 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7395 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
7396 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
7398 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7402 rc = bnxt_setup_vnic(bp, vnic_id);
7412 /* Allow PF and VF with default VLAN to be in promiscuous mode */
7413 static bool bnxt_promisc_ok(struct bnxt *bp)
7415 #ifdef CONFIG_BNXT_SRIOV
7416 if (BNXT_VF(bp) && !bp->vf.vlan)
7422 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
7424 unsigned int rc = 0;
7426 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
7428 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7433 rc = bnxt_hwrm_vnic_cfg(bp, 1);
7435 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7442 static int bnxt_cfg_rx_mode(struct bnxt *);
7443 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
7445 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
7447 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7449 unsigned int rx_nr_rings = bp->rx_nr_rings;
7452 rc = bnxt_hwrm_stat_ctx_alloc(bp);
7454 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
7460 rc = bnxt_hwrm_ring_alloc(bp);
7462 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
7466 rc = bnxt_hwrm_ring_grp_alloc(bp);
7468 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
7472 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7475 /* default vnic 0 */
7476 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
7478 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
7482 rc = bnxt_setup_vnic(bp, 0);
7486 if (bp->flags & BNXT_FLAG_RFS) {
7487 rc = bnxt_alloc_rfs_vnics(bp);
7492 if (bp->flags & BNXT_FLAG_TPA) {
7493 rc = bnxt_set_tpa(bp, true);
7499 bnxt_update_vf_mac(bp);
7501 /* Filter for default vnic 0 */
7502 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
7504 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
7507 vnic->uc_filter_count = 1;
7510 if (bp->dev->flags & IFF_BROADCAST)
7511 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
7513 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
7514 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7516 if (bp->dev->flags & IFF_ALLMULTI) {
7517 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7518 vnic->mc_list_count = 0;
7522 bnxt_mc_list_updated(bp, &mask);
7523 vnic->rx_mask |= mask;
7526 rc = bnxt_cfg_rx_mode(bp);
7530 rc = bnxt_hwrm_set_coal(bp);
7532 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
7535 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7536 rc = bnxt_setup_nitroa0_vnic(bp);
7538 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
7543 bnxt_hwrm_func_qcfg(bp);
7544 netdev_update_features(bp->dev);
7550 bnxt_hwrm_resource_free(bp, 0, true);
7555 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
7557 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
7561 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
7563 bnxt_init_cp_rings(bp);
7564 bnxt_init_rx_rings(bp);
7565 bnxt_init_tx_rings(bp);
7566 bnxt_init_ring_grps(bp, irq_re_init);
7567 bnxt_init_vnics(bp);
7569 return bnxt_init_chip(bp, irq_re_init);
7572 static int bnxt_set_real_num_queues(struct bnxt *bp)
7575 struct net_device *dev = bp->dev;
7577 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
7578 bp->tx_nr_rings_xdp);
7582 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
7586 #ifdef CONFIG_RFS_ACCEL
7587 if (bp->flags & BNXT_FLAG_RFS)
7588 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
7594 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7597 int _rx = *rx, _tx = *tx;
7600 *rx = min_t(int, _rx, max);
7601 *tx = min_t(int, _tx, max);
7606 while (_rx + _tx > max) {
7607 if (_rx > _tx && _rx > 1)
7618 static void bnxt_setup_msix(struct bnxt *bp)
7620 const int len = sizeof(bp->irq_tbl[0].name);
7621 struct net_device *dev = bp->dev;
7624 tcs = netdev_get_num_tc(dev);
7628 for (i = 0; i < tcs; i++) {
7629 count = bp->tx_nr_rings_per_tc;
7631 netdev_set_tc_queue(dev, i, count, off);
7635 for (i = 0; i < bp->cp_nr_rings; i++) {
7636 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7639 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7641 else if (i < bp->rx_nr_rings)
7646 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
7648 bp->irq_tbl[map_idx].handler = bnxt_msix;
7652 static void bnxt_setup_inta(struct bnxt *bp)
7654 const int len = sizeof(bp->irq_tbl[0].name);
7656 if (netdev_get_num_tc(bp->dev))
7657 netdev_reset_tc(bp->dev);
7659 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
7661 bp->irq_tbl[0].handler = bnxt_inta;
7664 static int bnxt_setup_int_mode(struct bnxt *bp)
7668 if (bp->flags & BNXT_FLAG_USING_MSIX)
7669 bnxt_setup_msix(bp);
7671 bnxt_setup_inta(bp);
7673 rc = bnxt_set_real_num_queues(bp);
7677 #ifdef CONFIG_RFS_ACCEL
7678 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
7680 return bp->hw_resc.max_rsscos_ctxs;
7683 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
7685 return bp->hw_resc.max_vnics;
7689 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
7691 return bp->hw_resc.max_stat_ctxs;
7694 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
7696 return bp->hw_resc.max_cp_rings;
7699 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
7701 unsigned int cp = bp->hw_resc.max_cp_rings;
7703 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7704 cp -= bnxt_get_ulp_msix_num(bp);
7709 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
7711 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7713 if (bp->flags & BNXT_FLAG_CHIP_P5)
7714 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
7716 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
7719 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
7721 bp->hw_resc.max_irqs = max_irqs;
7724 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
7728 cp = bnxt_get_max_func_cp_rings_for_en(bp);
7729 if (bp->flags & BNXT_FLAG_CHIP_P5)
7730 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
7732 return cp - bp->cp_nr_rings;
7735 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
7737 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
7740 int bnxt_get_avail_msix(struct bnxt *bp, int num)
7742 int max_cp = bnxt_get_max_func_cp_rings(bp);
7743 int max_irq = bnxt_get_max_func_irqs(bp);
7744 int total_req = bp->cp_nr_rings + num;
7745 int max_idx, avail_msix;
7747 max_idx = bp->total_irqs;
7748 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7749 max_idx = min_t(int, bp->total_irqs, max_cp);
7750 avail_msix = max_idx - bp->cp_nr_rings;
7751 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
7754 if (max_irq < total_req) {
7755 num = max_irq - bp->cp_nr_rings;
7762 static int bnxt_get_num_msix(struct bnxt *bp)
7764 if (!BNXT_NEW_RM(bp))
7765 return bnxt_get_max_func_irqs(bp);
7767 return bnxt_nq_rings_in_use(bp);
7770 static int bnxt_init_msix(struct bnxt *bp)
7772 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
7773 struct msix_entry *msix_ent;
7775 total_vecs = bnxt_get_num_msix(bp);
7776 max = bnxt_get_max_func_irqs(bp);
7777 if (total_vecs > max)
7783 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
7787 for (i = 0; i < total_vecs; i++) {
7788 msix_ent[i].entry = i;
7789 msix_ent[i].vector = 0;
7792 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
7795 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
7796 ulp_msix = bnxt_get_ulp_msix_num(bp);
7797 if (total_vecs < 0 || total_vecs < ulp_msix) {
7799 goto msix_setup_exit;
7802 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
7804 for (i = 0; i < total_vecs; i++)
7805 bp->irq_tbl[i].vector = msix_ent[i].vector;
7807 bp->total_irqs = total_vecs;
7808 /* Trim rings based upon num of vectors allocated */
7809 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
7810 total_vecs - ulp_msix, min == 1);
7812 goto msix_setup_exit;
7814 bp->cp_nr_rings = (min == 1) ?
7815 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7816 bp->tx_nr_rings + bp->rx_nr_rings;
7820 goto msix_setup_exit;
7822 bp->flags |= BNXT_FLAG_USING_MSIX;
7827 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
7830 pci_disable_msix(bp->pdev);
7835 static int bnxt_init_inta(struct bnxt *bp)
7837 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
7842 bp->rx_nr_rings = 1;
7843 bp->tx_nr_rings = 1;
7844 bp->cp_nr_rings = 1;
7845 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7846 bp->irq_tbl[0].vector = bp->pdev->irq;
7850 static int bnxt_init_int_mode(struct bnxt *bp)
7854 if (bp->flags & BNXT_FLAG_MSIX_CAP)
7855 rc = bnxt_init_msix(bp);
7857 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
7858 /* fallback to INTA */
7859 rc = bnxt_init_inta(bp);
7864 static void bnxt_clear_int_mode(struct bnxt *bp)
7866 if (bp->flags & BNXT_FLAG_USING_MSIX)
7867 pci_disable_msix(bp->pdev);
7871 bp->flags &= ~BNXT_FLAG_USING_MSIX;
7874 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
7876 int tcs = netdev_get_num_tc(bp->dev);
7877 bool irq_cleared = false;
7880 if (!bnxt_need_reserve_rings(bp))
7883 if (irq_re_init && BNXT_NEW_RM(bp) &&
7884 bnxt_get_num_msix(bp) != bp->total_irqs) {
7885 bnxt_ulp_irq_stop(bp);
7886 bnxt_clear_int_mode(bp);
7889 rc = __bnxt_reserve_rings(bp);
7892 rc = bnxt_init_int_mode(bp);
7893 bnxt_ulp_irq_restart(bp, rc);
7896 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
7899 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
7900 netdev_err(bp->dev, "tx ring reservation failure\n");
7901 netdev_reset_tc(bp->dev);
7902 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
7908 static void bnxt_free_irq(struct bnxt *bp)
7910 struct bnxt_irq *irq;
7913 #ifdef CONFIG_RFS_ACCEL
7914 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
7915 bp->dev->rx_cpu_rmap = NULL;
7917 if (!bp->irq_tbl || !bp->bnapi)
7920 for (i = 0; i < bp->cp_nr_rings; i++) {
7921 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7923 irq = &bp->irq_tbl[map_idx];
7924 if (irq->requested) {
7925 if (irq->have_cpumask) {
7926 irq_set_affinity_hint(irq->vector, NULL);
7927 free_cpumask_var(irq->cpu_mask);
7928 irq->have_cpumask = 0;
7930 free_irq(irq->vector, bp->bnapi[i]);
7937 static int bnxt_request_irq(struct bnxt *bp)
7940 unsigned long flags = 0;
7941 #ifdef CONFIG_RFS_ACCEL
7942 struct cpu_rmap *rmap;
7945 rc = bnxt_setup_int_mode(bp);
7947 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
7951 #ifdef CONFIG_RFS_ACCEL
7952 rmap = bp->dev->rx_cpu_rmap;
7954 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
7955 flags = IRQF_SHARED;
7957 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
7958 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7959 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
7961 #ifdef CONFIG_RFS_ACCEL
7962 if (rmap && bp->bnapi[i]->rx_ring) {
7963 rc = irq_cpu_rmap_add(rmap, irq->vector);
7965 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
7970 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
7977 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
7978 int numa_node = dev_to_node(&bp->pdev->dev);
7980 irq->have_cpumask = 1;
7981 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
7983 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
7985 netdev_warn(bp->dev,
7986 "Set affinity failed, IRQ = %d\n",
7995 static void bnxt_del_napi(struct bnxt *bp)
8002 for (i = 0; i < bp->cp_nr_rings; i++) {
8003 struct bnxt_napi *bnapi = bp->bnapi[i];
8005 napi_hash_del(&bnapi->napi);
8006 netif_napi_del(&bnapi->napi);
8008 /* We called napi_hash_del() before netif_napi_del(), we need
8009 * to respect an RCU grace period before freeing napi structures.
8014 static void bnxt_init_napi(struct bnxt *bp)
8017 unsigned int cp_nr_rings = bp->cp_nr_rings;
8018 struct bnxt_napi *bnapi;
8020 if (bp->flags & BNXT_FLAG_USING_MSIX) {
8021 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8023 if (bp->flags & BNXT_FLAG_CHIP_P5)
8024 poll_fn = bnxt_poll_p5;
8025 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8027 for (i = 0; i < cp_nr_rings; i++) {
8028 bnapi = bp->bnapi[i];
8029 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8031 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8032 bnapi = bp->bnapi[cp_nr_rings];
8033 netif_napi_add(bp->dev, &bnapi->napi,
8034 bnxt_poll_nitroa0, 64);
8037 bnapi = bp->bnapi[0];
8038 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8042 static void bnxt_disable_napi(struct bnxt *bp)
8049 for (i = 0; i < bp->cp_nr_rings; i++) {
8050 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8052 if (bp->bnapi[i]->rx_ring)
8053 cancel_work_sync(&cpr->dim.work);
8055 napi_disable(&bp->bnapi[i]->napi);
8059 static void bnxt_enable_napi(struct bnxt *bp)
8063 for (i = 0; i < bp->cp_nr_rings; i++) {
8064 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8065 bp->bnapi[i]->in_reset = false;
8067 if (bp->bnapi[i]->rx_ring) {
8068 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8069 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8071 napi_enable(&bp->bnapi[i]->napi);
8075 void bnxt_tx_disable(struct bnxt *bp)
8078 struct bnxt_tx_ring_info *txr;
8081 for (i = 0; i < bp->tx_nr_rings; i++) {
8082 txr = &bp->tx_ring[i];
8083 txr->dev_state = BNXT_DEV_STATE_CLOSING;
8086 /* Stop all TX queues */
8087 netif_tx_disable(bp->dev);
8088 netif_carrier_off(bp->dev);
8091 void bnxt_tx_enable(struct bnxt *bp)
8094 struct bnxt_tx_ring_info *txr;
8096 for (i = 0; i < bp->tx_nr_rings; i++) {
8097 txr = &bp->tx_ring[i];
8100 netif_tx_wake_all_queues(bp->dev);
8101 if (bp->link_info.link_up)
8102 netif_carrier_on(bp->dev);
8105 static void bnxt_report_link(struct bnxt *bp)
8107 if (bp->link_info.link_up) {
8109 const char *flow_ctrl;
8113 netif_carrier_on(bp->dev);
8114 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
8118 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
8119 flow_ctrl = "ON - receive & transmit";
8120 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
8121 flow_ctrl = "ON - transmit";
8122 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
8123 flow_ctrl = "ON - receive";
8126 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
8127 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
8128 speed, duplex, flow_ctrl);
8129 if (bp->flags & BNXT_FLAG_EEE_CAP)
8130 netdev_info(bp->dev, "EEE is %s\n",
8131 bp->eee.eee_active ? "active" :
8133 fec = bp->link_info.fec_cfg;
8134 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
8135 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
8136 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
8137 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
8138 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
8140 netif_carrier_off(bp->dev);
8141 netdev_err(bp->dev, "NIC Link is Down\n");
8145 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
8148 struct hwrm_port_phy_qcaps_input req = {0};
8149 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8150 struct bnxt_link_info *link_info = &bp->link_info;
8152 if (bp->hwrm_spec_code < 0x10201)
8155 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
8157 mutex_lock(&bp->hwrm_cmd_lock);
8158 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8160 goto hwrm_phy_qcaps_exit;
8162 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
8163 struct ethtool_eee *eee = &bp->eee;
8164 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
8166 bp->flags |= BNXT_FLAG_EEE_CAP;
8167 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8168 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
8169 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
8170 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
8171 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
8173 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
8175 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
8177 if (resp->supported_speeds_auto_mode)
8178 link_info->support_auto_speeds =
8179 le16_to_cpu(resp->supported_speeds_auto_mode);
8181 bp->port_count = resp->port_cnt;
8183 hwrm_phy_qcaps_exit:
8184 mutex_unlock(&bp->hwrm_cmd_lock);
8188 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
8191 struct bnxt_link_info *link_info = &bp->link_info;
8192 struct hwrm_port_phy_qcfg_input req = {0};
8193 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8194 u8 link_up = link_info->link_up;
8197 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
8199 mutex_lock(&bp->hwrm_cmd_lock);
8200 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8202 mutex_unlock(&bp->hwrm_cmd_lock);
8206 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
8207 link_info->phy_link_status = resp->link;
8208 link_info->duplex = resp->duplex_cfg;
8209 if (bp->hwrm_spec_code >= 0x10800)
8210 link_info->duplex = resp->duplex_state;
8211 link_info->pause = resp->pause;
8212 link_info->auto_mode = resp->auto_mode;
8213 link_info->auto_pause_setting = resp->auto_pause;
8214 link_info->lp_pause = resp->link_partner_adv_pause;
8215 link_info->force_pause_setting = resp->force_pause;
8216 link_info->duplex_setting = resp->duplex_cfg;
8217 if (link_info->phy_link_status == BNXT_LINK_LINK)
8218 link_info->link_speed = le16_to_cpu(resp->link_speed);
8220 link_info->link_speed = 0;
8221 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
8222 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
8223 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
8224 link_info->lp_auto_link_speeds =
8225 le16_to_cpu(resp->link_partner_adv_speeds);
8226 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
8227 link_info->phy_ver[0] = resp->phy_maj;
8228 link_info->phy_ver[1] = resp->phy_min;
8229 link_info->phy_ver[2] = resp->phy_bld;
8230 link_info->media_type = resp->media_type;
8231 link_info->phy_type = resp->phy_type;
8232 link_info->transceiver = resp->xcvr_pkg_type;
8233 link_info->phy_addr = resp->eee_config_phy_addr &
8234 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
8235 link_info->module_status = resp->module_status;
8237 if (bp->flags & BNXT_FLAG_EEE_CAP) {
8238 struct ethtool_eee *eee = &bp->eee;
8241 eee->eee_active = 0;
8242 if (resp->eee_config_phy_addr &
8243 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
8244 eee->eee_active = 1;
8245 fw_speeds = le16_to_cpu(
8246 resp->link_partner_adv_eee_link_speed_mask);
8247 eee->lp_advertised =
8248 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8251 /* Pull initial EEE config */
8252 if (!chng_link_state) {
8253 if (resp->eee_config_phy_addr &
8254 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
8255 eee->eee_enabled = 1;
8257 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
8259 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
8261 if (resp->eee_config_phy_addr &
8262 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
8265 eee->tx_lpi_enabled = 1;
8266 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
8267 eee->tx_lpi_timer = le32_to_cpu(tmr) &
8268 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
8273 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
8274 if (bp->hwrm_spec_code >= 0x10504)
8275 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
8277 /* TODO: need to add more logic to report VF link */
8278 if (chng_link_state) {
8279 if (link_info->phy_link_status == BNXT_LINK_LINK)
8280 link_info->link_up = 1;
8282 link_info->link_up = 0;
8283 if (link_up != link_info->link_up)
8284 bnxt_report_link(bp);
8286 /* alwasy link down if not require to update link state */
8287 link_info->link_up = 0;
8289 mutex_unlock(&bp->hwrm_cmd_lock);
8291 if (!BNXT_SINGLE_PF(bp))
8294 diff = link_info->support_auto_speeds ^ link_info->advertising;
8295 if ((link_info->support_auto_speeds | diff) !=
8296 link_info->support_auto_speeds) {
8297 /* An advertised speed is no longer supported, so we need to
8298 * update the advertisement settings. Caller holds RTNL
8299 * so we can modify link settings.
8301 link_info->advertising = link_info->support_auto_speeds;
8302 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
8303 bnxt_hwrm_set_link_setting(bp, true, false);
8308 static void bnxt_get_port_module_status(struct bnxt *bp)
8310 struct bnxt_link_info *link_info = &bp->link_info;
8311 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
8314 if (bnxt_update_link(bp, true))
8317 module_status = link_info->module_status;
8318 switch (module_status) {
8319 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
8320 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
8321 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
8322 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
8324 if (bp->hwrm_spec_code >= 0x10201) {
8325 netdev_warn(bp->dev, "Module part number %s\n",
8326 resp->phy_vendor_partnumber);
8328 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
8329 netdev_warn(bp->dev, "TX is disabled\n");
8330 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
8331 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
8336 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
8338 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
8339 if (bp->hwrm_spec_code >= 0x10201)
8341 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
8342 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8343 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
8344 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8345 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
8347 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
8349 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
8350 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
8351 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
8352 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
8354 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
8355 if (bp->hwrm_spec_code >= 0x10201) {
8356 req->auto_pause = req->force_pause;
8357 req->enables |= cpu_to_le32(
8358 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
8363 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
8364 struct hwrm_port_phy_cfg_input *req)
8366 u8 autoneg = bp->link_info.autoneg;
8367 u16 fw_link_speed = bp->link_info.req_link_speed;
8368 u16 advertising = bp->link_info.advertising;
8370 if (autoneg & BNXT_AUTONEG_SPEED) {
8372 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
8374 req->enables |= cpu_to_le32(
8375 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
8376 req->auto_link_speed_mask = cpu_to_le16(advertising);
8378 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
8380 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
8382 req->force_link_speed = cpu_to_le16(fw_link_speed);
8383 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
8386 /* tell chimp that the setting takes effect immediately */
8387 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
8390 int bnxt_hwrm_set_pause(struct bnxt *bp)
8392 struct hwrm_port_phy_cfg_input req = {0};
8395 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8396 bnxt_hwrm_set_pause_common(bp, &req);
8398 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
8399 bp->link_info.force_link_chng)
8400 bnxt_hwrm_set_link_common(bp, &req);
8402 mutex_lock(&bp->hwrm_cmd_lock);
8403 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8404 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
8405 /* since changing of pause setting doesn't trigger any link
8406 * change event, the driver needs to update the current pause
8407 * result upon successfully return of the phy_cfg command
8409 bp->link_info.pause =
8410 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
8411 bp->link_info.auto_pause_setting = 0;
8412 if (!bp->link_info.force_link_chng)
8413 bnxt_report_link(bp);
8415 bp->link_info.force_link_chng = false;
8416 mutex_unlock(&bp->hwrm_cmd_lock);
8420 static void bnxt_hwrm_set_eee(struct bnxt *bp,
8421 struct hwrm_port_phy_cfg_input *req)
8423 struct ethtool_eee *eee = &bp->eee;
8425 if (eee->eee_enabled) {
8427 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
8429 if (eee->tx_lpi_enabled)
8430 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
8432 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
8434 req->flags |= cpu_to_le32(flags);
8435 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
8436 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
8437 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
8439 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
8443 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
8445 struct hwrm_port_phy_cfg_input req = {0};
8447 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8449 bnxt_hwrm_set_pause_common(bp, &req);
8451 bnxt_hwrm_set_link_common(bp, &req);
8454 bnxt_hwrm_set_eee(bp, &req);
8455 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8458 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
8460 struct hwrm_port_phy_cfg_input req = {0};
8462 if (!BNXT_SINGLE_PF(bp))
8465 if (pci_num_vf(bp->pdev))
8468 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8469 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
8470 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8473 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
8475 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
8476 struct hwrm_func_drv_if_change_input req = {0};
8477 bool resc_reinit = false;
8480 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
8483 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
8485 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
8486 mutex_lock(&bp->hwrm_cmd_lock);
8487 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8488 if (!rc && (resp->flags &
8489 cpu_to_le32(FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)))
8491 mutex_unlock(&bp->hwrm_cmd_lock);
8493 if (up && resc_reinit && BNXT_NEW_RM(bp)) {
8494 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8496 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
8497 hw_resc->resv_cp_rings = 0;
8498 hw_resc->resv_stat_ctxs = 0;
8499 hw_resc->resv_irqs = 0;
8500 hw_resc->resv_tx_rings = 0;
8501 hw_resc->resv_rx_rings = 0;
8502 hw_resc->resv_hw_ring_grps = 0;
8503 hw_resc->resv_vnics = 0;
8504 bp->tx_nr_rings = 0;
8505 bp->rx_nr_rings = 0;
8510 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
8512 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8513 struct hwrm_port_led_qcaps_input req = {0};
8514 struct bnxt_pf_info *pf = &bp->pf;
8517 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
8520 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
8521 req.port_id = cpu_to_le16(pf->port_id);
8522 mutex_lock(&bp->hwrm_cmd_lock);
8523 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8525 mutex_unlock(&bp->hwrm_cmd_lock);
8528 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
8531 bp->num_leds = resp->num_leds;
8532 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
8534 for (i = 0; i < bp->num_leds; i++) {
8535 struct bnxt_led_info *led = &bp->leds[i];
8536 __le16 caps = led->led_state_caps;
8538 if (!led->led_group_id ||
8539 !BNXT_LED_ALT_BLINK_CAP(caps)) {
8545 mutex_unlock(&bp->hwrm_cmd_lock);
8549 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
8551 struct hwrm_wol_filter_alloc_input req = {0};
8552 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
8555 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
8556 req.port_id = cpu_to_le16(bp->pf.port_id);
8557 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
8558 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
8559 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
8560 mutex_lock(&bp->hwrm_cmd_lock);
8561 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8563 bp->wol_filter_id = resp->wol_filter_id;
8564 mutex_unlock(&bp->hwrm_cmd_lock);
8568 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
8570 struct hwrm_wol_filter_free_input req = {0};
8573 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
8574 req.port_id = cpu_to_le16(bp->pf.port_id);
8575 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
8576 req.wol_filter_id = bp->wol_filter_id;
8577 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8581 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
8583 struct hwrm_wol_filter_qcfg_input req = {0};
8584 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8585 u16 next_handle = 0;
8588 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
8589 req.port_id = cpu_to_le16(bp->pf.port_id);
8590 req.handle = cpu_to_le16(handle);
8591 mutex_lock(&bp->hwrm_cmd_lock);
8592 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8594 next_handle = le16_to_cpu(resp->next_handle);
8595 if (next_handle != 0) {
8596 if (resp->wol_type ==
8597 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
8599 bp->wol_filter_id = resp->wol_filter_id;
8603 mutex_unlock(&bp->hwrm_cmd_lock);
8607 static void bnxt_get_wol_settings(struct bnxt *bp)
8611 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
8615 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
8616 } while (handle && handle != 0xffff);
8619 #ifdef CONFIG_BNXT_HWMON
8620 static ssize_t bnxt_show_temp(struct device *dev,
8621 struct device_attribute *devattr, char *buf)
8623 struct hwrm_temp_monitor_query_input req = {0};
8624 struct hwrm_temp_monitor_query_output *resp;
8625 struct bnxt *bp = dev_get_drvdata(dev);
8628 resp = bp->hwrm_cmd_resp_addr;
8629 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
8630 mutex_lock(&bp->hwrm_cmd_lock);
8631 if (!_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
8632 temp = resp->temp * 1000; /* display millidegree */
8633 mutex_unlock(&bp->hwrm_cmd_lock);
8635 return sprintf(buf, "%u\n", temp);
8637 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
8639 static struct attribute *bnxt_attrs[] = {
8640 &sensor_dev_attr_temp1_input.dev_attr.attr,
8643 ATTRIBUTE_GROUPS(bnxt);
8645 static void bnxt_hwmon_close(struct bnxt *bp)
8647 if (bp->hwmon_dev) {
8648 hwmon_device_unregister(bp->hwmon_dev);
8649 bp->hwmon_dev = NULL;
8653 static void bnxt_hwmon_open(struct bnxt *bp)
8655 struct pci_dev *pdev = bp->pdev;
8657 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
8658 DRV_MODULE_NAME, bp,
8660 if (IS_ERR(bp->hwmon_dev)) {
8661 bp->hwmon_dev = NULL;
8662 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
8666 static void bnxt_hwmon_close(struct bnxt *bp)
8670 static void bnxt_hwmon_open(struct bnxt *bp)
8675 static bool bnxt_eee_config_ok(struct bnxt *bp)
8677 struct ethtool_eee *eee = &bp->eee;
8678 struct bnxt_link_info *link_info = &bp->link_info;
8680 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
8683 if (eee->eee_enabled) {
8685 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
8687 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8688 eee->eee_enabled = 0;
8691 if (eee->advertised & ~advertising) {
8692 eee->advertised = advertising & eee->supported;
8699 static int bnxt_update_phy_setting(struct bnxt *bp)
8702 bool update_link = false;
8703 bool update_pause = false;
8704 bool update_eee = false;
8705 struct bnxt_link_info *link_info = &bp->link_info;
8707 rc = bnxt_update_link(bp, true);
8709 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
8713 if (!BNXT_SINGLE_PF(bp))
8716 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
8717 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
8718 link_info->req_flow_ctrl)
8719 update_pause = true;
8720 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
8721 link_info->force_pause_setting != link_info->req_flow_ctrl)
8722 update_pause = true;
8723 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8724 if (BNXT_AUTO_MODE(link_info->auto_mode))
8726 if (link_info->req_link_speed != link_info->force_link_speed)
8728 if (link_info->req_duplex != link_info->duplex_setting)
8731 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
8733 if (link_info->advertising != link_info->auto_link_speeds)
8737 /* The last close may have shutdown the link, so need to call
8738 * PHY_CFG to bring it back up.
8740 if (!netif_carrier_ok(bp->dev))
8743 if (!bnxt_eee_config_ok(bp))
8747 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
8748 else if (update_pause)
8749 rc = bnxt_hwrm_set_pause(bp);
8751 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
8759 /* Common routine to pre-map certain register block to different GRC window.
8760 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
8761 * in PF and 3 windows in VF that can be customized to map in different
8764 static void bnxt_preset_reg_win(struct bnxt *bp)
8767 /* CAG registers map to GRC window #4 */
8768 writel(BNXT_CAG_REG_BASE,
8769 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
8773 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
8775 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8779 bnxt_preset_reg_win(bp);
8780 netif_carrier_off(bp->dev);
8782 /* Reserve rings now if none were reserved at driver probe. */
8783 rc = bnxt_init_dflt_ring_mode(bp);
8785 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
8789 rc = bnxt_reserve_rings(bp, irq_re_init);
8792 if ((bp->flags & BNXT_FLAG_RFS) &&
8793 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
8794 /* disable RFS if falling back to INTA */
8795 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
8796 bp->flags &= ~BNXT_FLAG_RFS;
8799 rc = bnxt_alloc_mem(bp, irq_re_init);
8801 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8802 goto open_err_free_mem;
8807 rc = bnxt_request_irq(bp);
8809 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
8814 bnxt_enable_napi(bp);
8815 bnxt_debug_dev_init(bp);
8817 rc = bnxt_init_nic(bp, irq_re_init);
8819 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8824 mutex_lock(&bp->link_lock);
8825 rc = bnxt_update_phy_setting(bp);
8826 mutex_unlock(&bp->link_lock);
8828 netdev_warn(bp->dev, "failed to update phy settings\n");
8829 if (BNXT_SINGLE_PF(bp)) {
8830 bp->link_info.phy_retry = true;
8831 bp->link_info.phy_retry_expires =
8838 udp_tunnel_get_rx_info(bp->dev);
8840 set_bit(BNXT_STATE_OPEN, &bp->state);
8841 bnxt_enable_int(bp);
8842 /* Enable TX queues */
8844 mod_timer(&bp->timer, jiffies + bp->current_interval);
8845 /* Poll link status and check for SFP+ module status */
8846 bnxt_get_port_module_status(bp);
8848 /* VF-reps may need to be re-opened after the PF is re-opened */
8850 bnxt_vf_reps_open(bp);
8854 bnxt_debug_dev_exit(bp);
8855 bnxt_disable_napi(bp);
8863 bnxt_free_mem(bp, true);
8867 /* rtnl_lock held */
8868 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8872 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
8874 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
8880 /* rtnl_lock held, open the NIC half way by allocating all resources, but
8881 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
8884 int bnxt_half_open_nic(struct bnxt *bp)
8888 rc = bnxt_alloc_mem(bp, false);
8890 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8893 rc = bnxt_init_nic(bp, false);
8895 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8902 bnxt_free_mem(bp, false);
8907 /* rtnl_lock held, this call can only be made after a previous successful
8908 * call to bnxt_half_open_nic().
8910 void bnxt_half_close_nic(struct bnxt *bp)
8912 bnxt_hwrm_resource_free(bp, false, false);
8914 bnxt_free_mem(bp, false);
8917 static int bnxt_open(struct net_device *dev)
8919 struct bnxt *bp = netdev_priv(dev);
8922 bnxt_hwrm_if_change(bp, true);
8923 rc = __bnxt_open_nic(bp, true, true);
8925 bnxt_hwrm_if_change(bp, false);
8927 bnxt_hwmon_open(bp);
8932 static bool bnxt_drv_busy(struct bnxt *bp)
8934 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
8935 test_bit(BNXT_STATE_READ_STATS, &bp->state));
8938 static void bnxt_get_ring_stats(struct bnxt *bp,
8939 struct rtnl_link_stats64 *stats);
8941 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
8944 /* Close the VF-reps before closing PF */
8946 bnxt_vf_reps_close(bp);
8948 /* Change device state to avoid TX queue wake up's */
8949 bnxt_tx_disable(bp);
8951 clear_bit(BNXT_STATE_OPEN, &bp->state);
8952 smp_mb__after_atomic();
8953 while (bnxt_drv_busy(bp))
8956 /* Flush rings and and disable interrupts */
8957 bnxt_shutdown_nic(bp, irq_re_init);
8959 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
8961 bnxt_debug_dev_exit(bp);
8962 bnxt_disable_napi(bp);
8963 del_timer_sync(&bp->timer);
8966 /* Save ring stats before shutdown */
8968 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
8973 bnxt_free_mem(bp, irq_re_init);
8976 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8980 #ifdef CONFIG_BNXT_SRIOV
8981 if (bp->sriov_cfg) {
8982 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
8984 BNXT_SRIOV_CFG_WAIT_TMO);
8986 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
8989 __bnxt_close_nic(bp, irq_re_init, link_re_init);
8993 static int bnxt_close(struct net_device *dev)
8995 struct bnxt *bp = netdev_priv(dev);
8997 bnxt_hwmon_close(bp);
8998 bnxt_close_nic(bp, true, true);
8999 bnxt_hwrm_shutdown_link(bp);
9000 bnxt_hwrm_if_change(bp, false);
9004 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
9007 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
9008 struct hwrm_port_phy_mdio_read_input req = {0};
9011 if (bp->hwrm_spec_code < 0x10a00)
9014 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
9015 req.port_id = cpu_to_le16(bp->pf.port_id);
9016 req.phy_addr = phy_addr;
9017 req.reg_addr = cpu_to_le16(reg & 0x1f);
9018 if (mdio_phy_id_is_c45(phy_addr)) {
9020 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9021 req.dev_addr = mdio_phy_id_devad(phy_addr);
9022 req.reg_addr = cpu_to_le16(reg);
9025 mutex_lock(&bp->hwrm_cmd_lock);
9026 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9028 *val = le16_to_cpu(resp->reg_data);
9029 mutex_unlock(&bp->hwrm_cmd_lock);
9033 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
9036 struct hwrm_port_phy_mdio_write_input req = {0};
9038 if (bp->hwrm_spec_code < 0x10a00)
9041 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
9042 req.port_id = cpu_to_le16(bp->pf.port_id);
9043 req.phy_addr = phy_addr;
9044 req.reg_addr = cpu_to_le16(reg & 0x1f);
9045 if (mdio_phy_id_is_c45(phy_addr)) {
9047 req.phy_addr = mdio_phy_id_prtad(phy_addr);
9048 req.dev_addr = mdio_phy_id_devad(phy_addr);
9049 req.reg_addr = cpu_to_le16(reg);
9051 req.reg_data = cpu_to_le16(val);
9053 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9056 /* rtnl_lock held */
9057 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9059 struct mii_ioctl_data *mdio = if_mii(ifr);
9060 struct bnxt *bp = netdev_priv(dev);
9065 mdio->phy_id = bp->link_info.phy_addr;
9071 if (!netif_running(dev))
9074 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
9076 mdio->val_out = mii_regval;
9081 if (!netif_running(dev))
9084 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
9094 static void bnxt_get_ring_stats(struct bnxt *bp,
9095 struct rtnl_link_stats64 *stats)
9100 for (i = 0; i < bp->cp_nr_rings; i++) {
9101 struct bnxt_napi *bnapi = bp->bnapi[i];
9102 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9103 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
9105 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
9106 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
9107 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
9109 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
9110 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
9111 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
9113 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
9114 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
9115 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
9117 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
9118 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
9119 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
9121 stats->rx_missed_errors +=
9122 le64_to_cpu(hw_stats->rx_discard_pkts);
9124 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
9126 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
9130 static void bnxt_add_prev_stats(struct bnxt *bp,
9131 struct rtnl_link_stats64 *stats)
9133 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
9135 stats->rx_packets += prev_stats->rx_packets;
9136 stats->tx_packets += prev_stats->tx_packets;
9137 stats->rx_bytes += prev_stats->rx_bytes;
9138 stats->tx_bytes += prev_stats->tx_bytes;
9139 stats->rx_missed_errors += prev_stats->rx_missed_errors;
9140 stats->multicast += prev_stats->multicast;
9141 stats->tx_dropped += prev_stats->tx_dropped;
9145 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
9147 struct bnxt *bp = netdev_priv(dev);
9149 set_bit(BNXT_STATE_READ_STATS, &bp->state);
9150 /* Make sure bnxt_close_nic() sees that we are reading stats before
9151 * we check the BNXT_STATE_OPEN flag.
9153 smp_mb__after_atomic();
9154 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9155 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9156 *stats = bp->net_stats_prev;
9160 bnxt_get_ring_stats(bp, stats);
9161 bnxt_add_prev_stats(bp, stats);
9163 if (bp->flags & BNXT_FLAG_PORT_STATS) {
9164 struct rx_port_stats *rx = bp->hw_rx_port_stats;
9165 struct tx_port_stats *tx = bp->hw_tx_port_stats;
9167 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
9168 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
9169 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
9170 le64_to_cpu(rx->rx_ovrsz_frames) +
9171 le64_to_cpu(rx->rx_runt_frames);
9172 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
9173 le64_to_cpu(rx->rx_jbr_frames);
9174 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
9175 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
9176 stats->tx_errors = le64_to_cpu(tx->tx_err);
9178 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
9181 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
9183 struct net_device *dev = bp->dev;
9184 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9185 struct netdev_hw_addr *ha;
9188 bool update = false;
9191 netdev_for_each_mc_addr(ha, dev) {
9192 if (mc_count >= BNXT_MAX_MC_ADDRS) {
9193 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9194 vnic->mc_list_count = 0;
9198 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
9199 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
9206 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
9208 if (mc_count != vnic->mc_list_count) {
9209 vnic->mc_list_count = mc_count;
9215 static bool bnxt_uc_list_updated(struct bnxt *bp)
9217 struct net_device *dev = bp->dev;
9218 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9219 struct netdev_hw_addr *ha;
9222 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
9225 netdev_for_each_uc_addr(ha, dev) {
9226 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
9234 static void bnxt_set_rx_mode(struct net_device *dev)
9236 struct bnxt *bp = netdev_priv(dev);
9237 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9238 u32 mask = vnic->rx_mask;
9239 bool mc_update = false;
9242 if (!netif_running(dev))
9245 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
9246 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
9247 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
9248 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
9250 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
9251 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
9253 uc_update = bnxt_uc_list_updated(bp);
9255 if (dev->flags & IFF_BROADCAST)
9256 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
9257 if (dev->flags & IFF_ALLMULTI) {
9258 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9259 vnic->mc_list_count = 0;
9261 mc_update = bnxt_mc_list_updated(bp, &mask);
9264 if (mask != vnic->rx_mask || uc_update || mc_update) {
9265 vnic->rx_mask = mask;
9267 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
9268 bnxt_queue_sp_work(bp);
9272 static int bnxt_cfg_rx_mode(struct bnxt *bp)
9274 struct net_device *dev = bp->dev;
9275 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9276 struct netdev_hw_addr *ha;
9280 netif_addr_lock_bh(dev);
9281 uc_update = bnxt_uc_list_updated(bp);
9282 netif_addr_unlock_bh(dev);
9287 mutex_lock(&bp->hwrm_cmd_lock);
9288 for (i = 1; i < vnic->uc_filter_count; i++) {
9289 struct hwrm_cfa_l2_filter_free_input req = {0};
9291 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
9294 req.l2_filter_id = vnic->fw_l2_filter_id[i];
9296 rc = _hwrm_send_message(bp, &req, sizeof(req),
9299 mutex_unlock(&bp->hwrm_cmd_lock);
9301 vnic->uc_filter_count = 1;
9303 netif_addr_lock_bh(dev);
9304 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
9305 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
9307 netdev_for_each_uc_addr(ha, dev) {
9308 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
9310 vnic->uc_filter_count++;
9313 netif_addr_unlock_bh(dev);
9315 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
9316 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
9318 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
9320 vnic->uc_filter_count = i;
9326 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
9327 if (rc && vnic->mc_list_count) {
9328 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
9330 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
9331 vnic->mc_list_count = 0;
9332 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
9335 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
9341 static bool bnxt_can_reserve_rings(struct bnxt *bp)
9343 #ifdef CONFIG_BNXT_SRIOV
9344 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
9345 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9347 /* No minimum rings were provisioned by the PF. Don't
9348 * reserve rings by default when device is down.
9350 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
9353 if (!netif_running(bp->dev))
9360 /* If the chip and firmware supports RFS */
9361 static bool bnxt_rfs_supported(struct bnxt *bp)
9363 if (bp->flags & BNXT_FLAG_CHIP_P5) {
9364 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX)
9368 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
9370 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
9375 /* If runtime conditions support RFS */
9376 static bool bnxt_rfs_capable(struct bnxt *bp)
9378 #ifdef CONFIG_RFS_ACCEL
9379 int vnics, max_vnics, max_rss_ctxs;
9381 if (bp->flags & BNXT_FLAG_CHIP_P5)
9382 return bnxt_rfs_supported(bp);
9383 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
9386 vnics = 1 + bp->rx_nr_rings;
9387 max_vnics = bnxt_get_max_func_vnics(bp);
9388 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
9390 /* RSS contexts not a limiting factor */
9391 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
9392 max_rss_ctxs = max_vnics;
9393 if (vnics > max_vnics || vnics > max_rss_ctxs) {
9394 if (bp->rx_nr_rings > 1)
9395 netdev_warn(bp->dev,
9396 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
9397 min(max_rss_ctxs - 1, max_vnics - 1));
9401 if (!BNXT_NEW_RM(bp))
9404 if (vnics == bp->hw_resc.resv_vnics)
9407 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
9408 if (vnics <= bp->hw_resc.resv_vnics)
9411 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
9412 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
9419 static netdev_features_t bnxt_fix_features(struct net_device *dev,
9420 netdev_features_t features)
9422 struct bnxt *bp = netdev_priv(dev);
9424 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
9425 features &= ~NETIF_F_NTUPLE;
9427 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9428 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
9430 if (!(features & NETIF_F_GRO))
9431 features &= ~NETIF_F_GRO_HW;
9433 if (features & NETIF_F_GRO_HW)
9434 features &= ~NETIF_F_LRO;
9436 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
9437 * turned on or off together.
9439 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
9440 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
9441 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
9442 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9443 NETIF_F_HW_VLAN_STAG_RX);
9445 features |= NETIF_F_HW_VLAN_CTAG_RX |
9446 NETIF_F_HW_VLAN_STAG_RX;
9448 #ifdef CONFIG_BNXT_SRIOV
9451 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9452 NETIF_F_HW_VLAN_STAG_RX);
9459 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
9461 struct bnxt *bp = netdev_priv(dev);
9462 u32 flags = bp->flags;
9465 bool re_init = false;
9466 bool update_tpa = false;
9468 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
9469 if (features & NETIF_F_GRO_HW)
9470 flags |= BNXT_FLAG_GRO;
9471 else if (features & NETIF_F_LRO)
9472 flags |= BNXT_FLAG_LRO;
9474 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9475 flags &= ~BNXT_FLAG_TPA;
9477 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9478 flags |= BNXT_FLAG_STRIP_VLAN;
9480 if (features & NETIF_F_NTUPLE)
9481 flags |= BNXT_FLAG_RFS;
9483 changes = flags ^ bp->flags;
9484 if (changes & BNXT_FLAG_TPA) {
9486 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
9487 (flags & BNXT_FLAG_TPA) == 0 ||
9488 (bp->flags & BNXT_FLAG_CHIP_P5))
9492 if (changes & ~BNXT_FLAG_TPA)
9495 if (flags != bp->flags) {
9496 u32 old_flags = bp->flags;
9498 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9501 bnxt_set_ring_params(bp);
9506 bnxt_close_nic(bp, false, false);
9509 bnxt_set_ring_params(bp);
9511 return bnxt_open_nic(bp, false, false);
9515 rc = bnxt_set_tpa(bp,
9516 (flags & BNXT_FLAG_TPA) ?
9519 bp->flags = old_flags;
9525 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
9526 u32 ring_id, u32 *prod, u32 *cons)
9528 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
9529 struct hwrm_dbg_ring_info_get_input req = {0};
9532 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
9533 req.ring_type = ring_type;
9534 req.fw_ring_id = cpu_to_le32(ring_id);
9535 mutex_lock(&bp->hwrm_cmd_lock);
9536 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9538 *prod = le32_to_cpu(resp->producer_index);
9539 *cons = le32_to_cpu(resp->consumer_index);
9541 mutex_unlock(&bp->hwrm_cmd_lock);
9545 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
9547 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
9548 int i = bnapi->index;
9553 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
9554 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
9558 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
9560 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
9561 int i = bnapi->index;
9566 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",
9567 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
9568 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
9569 rxr->rx_sw_agg_prod);
9572 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
9574 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9575 int i = bnapi->index;
9577 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
9578 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
9581 static void bnxt_dbg_dump_states(struct bnxt *bp)
9584 struct bnxt_napi *bnapi;
9586 for (i = 0; i < bp->cp_nr_rings; i++) {
9587 bnapi = bp->bnapi[i];
9588 if (netif_msg_drv(bp)) {
9589 bnxt_dump_tx_sw_state(bnapi);
9590 bnxt_dump_rx_sw_state(bnapi);
9591 bnxt_dump_cp_sw_state(bnapi);
9596 static void bnxt_reset_task(struct bnxt *bp, bool silent)
9599 bnxt_dbg_dump_states(bp);
9600 if (netif_running(bp->dev)) {
9605 bnxt_close_nic(bp, false, false);
9606 rc = bnxt_open_nic(bp, false, false);
9612 static void bnxt_tx_timeout(struct net_device *dev)
9614 struct bnxt *bp = netdev_priv(dev);
9616 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
9617 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
9618 bnxt_queue_sp_work(bp);
9621 static void bnxt_timer(struct timer_list *t)
9623 struct bnxt *bp = from_timer(bp, t, timer);
9624 struct net_device *dev = bp->dev;
9626 if (!netif_running(dev))
9629 if (atomic_read(&bp->intr_sem) != 0)
9630 goto bnxt_restart_timer;
9632 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
9633 bp->stats_coal_ticks) {
9634 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
9635 bnxt_queue_sp_work(bp);
9638 if (bnxt_tc_flower_enabled(bp)) {
9639 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
9640 bnxt_queue_sp_work(bp);
9643 if (bp->link_info.phy_retry) {
9644 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
9645 bp->link_info.phy_retry = 0;
9646 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
9648 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
9649 bnxt_queue_sp_work(bp);
9653 if ((bp->flags & BNXT_FLAG_CHIP_P5) && netif_carrier_ok(dev)) {
9654 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
9655 bnxt_queue_sp_work(bp);
9658 mod_timer(&bp->timer, jiffies + bp->current_interval);
9661 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
9663 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
9664 * set. If the device is being closed, bnxt_close() may be holding
9665 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
9666 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
9668 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9672 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
9674 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9678 /* Only called from bnxt_sp_task() */
9679 static void bnxt_reset(struct bnxt *bp, bool silent)
9681 bnxt_rtnl_lock_sp(bp);
9682 if (test_bit(BNXT_STATE_OPEN, &bp->state))
9683 bnxt_reset_task(bp, silent);
9684 bnxt_rtnl_unlock_sp(bp);
9687 static void bnxt_chk_missed_irq(struct bnxt *bp)
9691 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9694 for (i = 0; i < bp->cp_nr_rings; i++) {
9695 struct bnxt_napi *bnapi = bp->bnapi[i];
9696 struct bnxt_cp_ring_info *cpr;
9703 cpr = &bnapi->cp_ring;
9704 for (j = 0; j < 2; j++) {
9705 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
9708 if (!cpr2 || cpr2->has_more_work ||
9709 !bnxt_has_work(bp, cpr2))
9712 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
9713 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
9716 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
9717 bnxt_dbg_hwrm_ring_info_get(bp,
9718 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
9719 fw_ring_id, &val[0], &val[1]);
9725 static void bnxt_cfg_ntp_filters(struct bnxt *);
9727 static void bnxt_sp_task(struct work_struct *work)
9729 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
9731 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9732 smp_mb__after_atomic();
9733 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9734 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9738 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
9739 bnxt_cfg_rx_mode(bp);
9741 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
9742 bnxt_cfg_ntp_filters(bp);
9743 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
9744 bnxt_hwrm_exec_fwd_req(bp);
9745 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9746 bnxt_hwrm_tunnel_dst_port_alloc(
9748 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9750 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9751 bnxt_hwrm_tunnel_dst_port_free(
9752 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9754 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9755 bnxt_hwrm_tunnel_dst_port_alloc(
9757 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9759 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9760 bnxt_hwrm_tunnel_dst_port_free(
9761 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9763 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
9764 bnxt_hwrm_port_qstats(bp);
9765 bnxt_hwrm_port_qstats_ext(bp);
9766 bnxt_hwrm_pcie_qstats(bp);
9769 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
9772 mutex_lock(&bp->link_lock);
9773 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
9775 bnxt_hwrm_phy_qcaps(bp);
9777 rc = bnxt_update_link(bp, true);
9778 mutex_unlock(&bp->link_lock);
9780 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
9783 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
9786 mutex_lock(&bp->link_lock);
9787 rc = bnxt_update_phy_setting(bp);
9788 mutex_unlock(&bp->link_lock);
9790 netdev_warn(bp->dev, "update phy settings retry failed\n");
9792 bp->link_info.phy_retry = false;
9793 netdev_info(bp->dev, "update phy settings retry succeeded\n");
9796 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
9797 mutex_lock(&bp->link_lock);
9798 bnxt_get_port_module_status(bp);
9799 mutex_unlock(&bp->link_lock);
9802 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
9803 bnxt_tc_flow_stats_work(bp);
9805 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
9806 bnxt_chk_missed_irq(bp);
9808 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
9809 * must be the last functions to be called before exiting.
9811 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
9812 bnxt_reset(bp, false);
9814 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
9815 bnxt_reset(bp, true);
9817 smp_mb__before_atomic();
9818 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9821 /* Under rtnl_lock */
9822 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
9825 int max_rx, max_tx, tx_sets = 1;
9826 int tx_rings_needed, stats;
9833 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
9840 tx_rings_needed = tx * tx_sets + tx_xdp;
9841 if (max_tx < tx_rings_needed)
9845 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
9848 if (bp->flags & BNXT_FLAG_AGG_RINGS)
9850 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
9852 if (BNXT_NEW_RM(bp)) {
9853 cp += bnxt_get_ulp_msix_num(bp);
9854 stats += bnxt_get_ulp_stat_ctxs(bp);
9856 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
9860 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
9863 pci_iounmap(pdev, bp->bar2);
9868 pci_iounmap(pdev, bp->bar1);
9873 pci_iounmap(pdev, bp->bar0);
9878 static void bnxt_cleanup_pci(struct bnxt *bp)
9880 bnxt_unmap_bars(bp, bp->pdev);
9881 pci_release_regions(bp->pdev);
9882 pci_disable_device(bp->pdev);
9885 static void bnxt_init_dflt_coal(struct bnxt *bp)
9887 struct bnxt_coal *coal;
9889 /* Tick values in micro seconds.
9890 * 1 coal_buf x bufs_per_record = 1 completion record.
9892 coal = &bp->rx_coal;
9893 coal->coal_ticks = 10;
9894 coal->coal_bufs = 30;
9895 coal->coal_ticks_irq = 1;
9896 coal->coal_bufs_irq = 2;
9897 coal->idle_thresh = 50;
9898 coal->bufs_per_record = 2;
9899 coal->budget = 64; /* NAPI budget */
9901 coal = &bp->tx_coal;
9902 coal->coal_ticks = 28;
9903 coal->coal_bufs = 30;
9904 coal->coal_ticks_irq = 2;
9905 coal->coal_bufs_irq = 2;
9906 coal->bufs_per_record = 1;
9908 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
9911 static int bnxt_fw_init_one_p1(struct bnxt *bp)
9916 rc = bnxt_hwrm_ver_get(bp);
9920 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
9921 rc = bnxt_alloc_kong_hwrm_resources(bp);
9923 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
9926 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
9927 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
9928 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
9932 rc = bnxt_hwrm_func_reset(bp);
9936 bnxt_hwrm_fw_set_time(bp);
9940 static int bnxt_fw_init_one_p2(struct bnxt *bp)
9944 /* Get the MAX capabilities for this function */
9945 rc = bnxt_hwrm_func_qcaps(bp);
9947 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
9952 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
9954 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
9957 rc = bnxt_hwrm_func_drv_rgtr(bp);
9961 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
9965 bnxt_hwrm_func_qcfg(bp);
9966 bnxt_hwrm_vnic_qcaps(bp);
9967 bnxt_hwrm_port_led_qcaps(bp);
9968 bnxt_ethtool_init(bp);
9973 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
9976 struct bnxt *bp = netdev_priv(dev);
9978 SET_NETDEV_DEV(dev, &pdev->dev);
9980 /* enable device (incl. PCI PM wakeup), and bus-mastering */
9981 rc = pci_enable_device(pdev);
9983 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
9987 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
9989 "Cannot find PCI device base address, aborting\n");
9991 goto init_err_disable;
9994 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
9996 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
9997 goto init_err_disable;
10000 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
10001 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
10002 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
10003 goto init_err_disable;
10006 pci_set_master(pdev);
10011 bp->bar0 = pci_ioremap_bar(pdev, 0);
10013 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
10015 goto init_err_release;
10018 bp->bar1 = pci_ioremap_bar(pdev, 2);
10020 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
10022 goto init_err_release;
10025 bp->bar2 = pci_ioremap_bar(pdev, 4);
10027 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
10029 goto init_err_release;
10032 pci_enable_pcie_error_reporting(pdev);
10034 INIT_WORK(&bp->sp_task, bnxt_sp_task);
10036 spin_lock_init(&bp->ntp_fltr_lock);
10037 #if BITS_PER_LONG == 32
10038 spin_lock_init(&bp->db_lock);
10041 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
10042 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
10044 bnxt_init_dflt_coal(bp);
10046 timer_setup(&bp->timer, bnxt_timer, 0);
10047 bp->current_interval = BNXT_TIMER_INTERVAL;
10049 clear_bit(BNXT_STATE_OPEN, &bp->state);
10053 bnxt_unmap_bars(bp, pdev);
10054 pci_release_regions(pdev);
10057 pci_disable_device(pdev);
10063 /* rtnl_lock held */
10064 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
10066 struct sockaddr *addr = p;
10067 struct bnxt *bp = netdev_priv(dev);
10070 if (!is_valid_ether_addr(addr->sa_data))
10071 return -EADDRNOTAVAIL;
10073 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
10076 rc = bnxt_approve_mac(bp, addr->sa_data, true);
10080 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
10081 if (netif_running(dev)) {
10082 bnxt_close_nic(bp, false, false);
10083 rc = bnxt_open_nic(bp, false, false);
10089 /* rtnl_lock held */
10090 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
10092 struct bnxt *bp = netdev_priv(dev);
10094 if (netif_running(dev))
10095 bnxt_close_nic(bp, false, false);
10097 dev->mtu = new_mtu;
10098 bnxt_set_ring_params(bp);
10100 if (netif_running(dev))
10101 return bnxt_open_nic(bp, false, false);
10106 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
10108 struct bnxt *bp = netdev_priv(dev);
10112 if (tc > bp->max_tc) {
10113 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
10118 if (netdev_get_num_tc(dev) == tc)
10121 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10124 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
10125 sh, tc, bp->tx_nr_rings_xdp);
10129 /* Needs to close the device and do hw resource re-allocations */
10130 if (netif_running(bp->dev))
10131 bnxt_close_nic(bp, true, false);
10134 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
10135 netdev_set_num_tc(dev, tc);
10137 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10138 netdev_reset_tc(dev);
10140 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
10141 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
10142 bp->tx_nr_rings + bp->rx_nr_rings;
10144 if (netif_running(bp->dev))
10145 return bnxt_open_nic(bp, true, false);
10150 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
10153 struct bnxt *bp = cb_priv;
10155 if (!bnxt_tc_flower_enabled(bp) ||
10156 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
10157 return -EOPNOTSUPP;
10160 case TC_SETUP_CLSFLOWER:
10161 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
10163 return -EOPNOTSUPP;
10167 static LIST_HEAD(bnxt_block_cb_list);
10169 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
10172 struct bnxt *bp = netdev_priv(dev);
10175 case TC_SETUP_BLOCK:
10176 return flow_block_cb_setup_simple(type_data,
10177 &bnxt_block_cb_list,
10178 bnxt_setup_tc_block_cb,
10180 case TC_SETUP_QDISC_MQPRIO: {
10181 struct tc_mqprio_qopt *mqprio = type_data;
10183 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
10185 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
10188 return -EOPNOTSUPP;
10192 #ifdef CONFIG_RFS_ACCEL
10193 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
10194 struct bnxt_ntuple_filter *f2)
10196 struct flow_keys *keys1 = &f1->fkeys;
10197 struct flow_keys *keys2 = &f2->fkeys;
10199 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
10200 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
10201 keys1->ports.ports == keys2->ports.ports &&
10202 keys1->basic.ip_proto == keys2->basic.ip_proto &&
10203 keys1->basic.n_proto == keys2->basic.n_proto &&
10204 keys1->control.flags == keys2->control.flags &&
10205 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
10206 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
10212 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
10213 u16 rxq_index, u32 flow_id)
10215 struct bnxt *bp = netdev_priv(dev);
10216 struct bnxt_ntuple_filter *fltr, *new_fltr;
10217 struct flow_keys *fkeys;
10218 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
10219 int rc = 0, idx, bit_id, l2_idx = 0;
10220 struct hlist_head *head;
10222 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
10223 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10226 netif_addr_lock_bh(dev);
10227 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
10228 if (ether_addr_equal(eth->h_dest,
10229 vnic->uc_list + off)) {
10234 netif_addr_unlock_bh(dev);
10238 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
10242 fkeys = &new_fltr->fkeys;
10243 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
10244 rc = -EPROTONOSUPPORT;
10248 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
10249 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
10250 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
10251 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
10252 rc = -EPROTONOSUPPORT;
10255 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
10256 bp->hwrm_spec_code < 0x10601) {
10257 rc = -EPROTONOSUPPORT;
10260 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
10261 bp->hwrm_spec_code < 0x10601) {
10262 rc = -EPROTONOSUPPORT;
10266 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
10267 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
10269 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
10270 head = &bp->ntp_fltr_hash_tbl[idx];
10272 hlist_for_each_entry_rcu(fltr, head, hash) {
10273 if (bnxt_fltr_match(fltr, new_fltr)) {
10281 spin_lock_bh(&bp->ntp_fltr_lock);
10282 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
10283 BNXT_NTP_FLTR_MAX_FLTR, 0);
10285 spin_unlock_bh(&bp->ntp_fltr_lock);
10290 new_fltr->sw_id = (u16)bit_id;
10291 new_fltr->flow_id = flow_id;
10292 new_fltr->l2_fltr_idx = l2_idx;
10293 new_fltr->rxq = rxq_index;
10294 hlist_add_head_rcu(&new_fltr->hash, head);
10295 bp->ntp_fltr_count++;
10296 spin_unlock_bh(&bp->ntp_fltr_lock);
10298 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
10299 bnxt_queue_sp_work(bp);
10301 return new_fltr->sw_id;
10308 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
10312 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
10313 struct hlist_head *head;
10314 struct hlist_node *tmp;
10315 struct bnxt_ntuple_filter *fltr;
10318 head = &bp->ntp_fltr_hash_tbl[i];
10319 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
10322 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
10323 if (rps_may_expire_flow(bp->dev, fltr->rxq,
10326 bnxt_hwrm_cfa_ntuple_filter_free(bp,
10331 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
10336 set_bit(BNXT_FLTR_VALID, &fltr->state);
10340 spin_lock_bh(&bp->ntp_fltr_lock);
10341 hlist_del_rcu(&fltr->hash);
10342 bp->ntp_fltr_count--;
10343 spin_unlock_bh(&bp->ntp_fltr_lock);
10345 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
10350 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
10351 netdev_info(bp->dev, "Receive PF driver unload event!");
10356 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
10360 #endif /* CONFIG_RFS_ACCEL */
10362 static void bnxt_udp_tunnel_add(struct net_device *dev,
10363 struct udp_tunnel_info *ti)
10365 struct bnxt *bp = netdev_priv(dev);
10367 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
10370 if (!netif_running(dev))
10373 switch (ti->type) {
10374 case UDP_TUNNEL_TYPE_VXLAN:
10375 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
10378 bp->vxlan_port_cnt++;
10379 if (bp->vxlan_port_cnt == 1) {
10380 bp->vxlan_port = ti->port;
10381 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
10382 bnxt_queue_sp_work(bp);
10385 case UDP_TUNNEL_TYPE_GENEVE:
10386 if (bp->nge_port_cnt && bp->nge_port != ti->port)
10389 bp->nge_port_cnt++;
10390 if (bp->nge_port_cnt == 1) {
10391 bp->nge_port = ti->port;
10392 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
10399 bnxt_queue_sp_work(bp);
10402 static void bnxt_udp_tunnel_del(struct net_device *dev,
10403 struct udp_tunnel_info *ti)
10405 struct bnxt *bp = netdev_priv(dev);
10407 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
10410 if (!netif_running(dev))
10413 switch (ti->type) {
10414 case UDP_TUNNEL_TYPE_VXLAN:
10415 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
10417 bp->vxlan_port_cnt--;
10419 if (bp->vxlan_port_cnt != 0)
10422 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
10424 case UDP_TUNNEL_TYPE_GENEVE:
10425 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
10427 bp->nge_port_cnt--;
10429 if (bp->nge_port_cnt != 0)
10432 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
10438 bnxt_queue_sp_work(bp);
10441 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
10442 struct net_device *dev, u32 filter_mask,
10445 struct bnxt *bp = netdev_priv(dev);
10447 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
10448 nlflags, filter_mask, NULL);
10451 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
10452 u16 flags, struct netlink_ext_ack *extack)
10454 struct bnxt *bp = netdev_priv(dev);
10455 struct nlattr *attr, *br_spec;
10458 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
10459 return -EOPNOTSUPP;
10461 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
10465 nla_for_each_nested(attr, br_spec, rem) {
10468 if (nla_type(attr) != IFLA_BRIDGE_MODE)
10471 if (nla_len(attr) < sizeof(mode))
10474 mode = nla_get_u16(attr);
10475 if (mode == bp->br_mode)
10478 rc = bnxt_hwrm_set_br_mode(bp, mode);
10480 bp->br_mode = mode;
10486 int bnxt_get_port_parent_id(struct net_device *dev,
10487 struct netdev_phys_item_id *ppid)
10489 struct bnxt *bp = netdev_priv(dev);
10491 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
10492 return -EOPNOTSUPP;
10494 /* The PF and it's VF-reps only support the switchdev framework */
10496 return -EOPNOTSUPP;
10498 ppid->id_len = sizeof(bp->switch_id);
10499 memcpy(ppid->id, bp->switch_id, ppid->id_len);
10504 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
10506 struct bnxt *bp = netdev_priv(dev);
10508 return &bp->dl_port;
10511 static const struct net_device_ops bnxt_netdev_ops = {
10512 .ndo_open = bnxt_open,
10513 .ndo_start_xmit = bnxt_start_xmit,
10514 .ndo_stop = bnxt_close,
10515 .ndo_get_stats64 = bnxt_get_stats64,
10516 .ndo_set_rx_mode = bnxt_set_rx_mode,
10517 .ndo_do_ioctl = bnxt_ioctl,
10518 .ndo_validate_addr = eth_validate_addr,
10519 .ndo_set_mac_address = bnxt_change_mac_addr,
10520 .ndo_change_mtu = bnxt_change_mtu,
10521 .ndo_fix_features = bnxt_fix_features,
10522 .ndo_set_features = bnxt_set_features,
10523 .ndo_tx_timeout = bnxt_tx_timeout,
10524 #ifdef CONFIG_BNXT_SRIOV
10525 .ndo_get_vf_config = bnxt_get_vf_config,
10526 .ndo_set_vf_mac = bnxt_set_vf_mac,
10527 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
10528 .ndo_set_vf_rate = bnxt_set_vf_bw,
10529 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
10530 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
10531 .ndo_set_vf_trust = bnxt_set_vf_trust,
10533 .ndo_setup_tc = bnxt_setup_tc,
10534 #ifdef CONFIG_RFS_ACCEL
10535 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
10537 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
10538 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
10539 .ndo_bpf = bnxt_xdp,
10540 .ndo_xdp_xmit = bnxt_xdp_xmit,
10541 .ndo_bridge_getlink = bnxt_bridge_getlink,
10542 .ndo_bridge_setlink = bnxt_bridge_setlink,
10543 .ndo_get_devlink_port = bnxt_get_devlink_port,
10546 static void bnxt_remove_one(struct pci_dev *pdev)
10548 struct net_device *dev = pci_get_drvdata(pdev);
10549 struct bnxt *bp = netdev_priv(dev);
10552 bnxt_sriov_disable(bp);
10553 bnxt_dl_unregister(bp);
10556 pci_disable_pcie_error_reporting(pdev);
10557 unregister_netdev(dev);
10558 bnxt_shutdown_tc(bp);
10559 bnxt_cancel_sp_work(bp);
10562 bnxt_clear_int_mode(bp);
10563 bnxt_hwrm_func_drv_unrgtr(bp);
10564 bnxt_free_hwrm_resources(bp);
10565 bnxt_free_hwrm_short_cmd_req(bp);
10566 bnxt_ethtool_free(bp);
10570 bnxt_cleanup_pci(bp);
10571 bnxt_free_ctx_mem(bp);
10574 bnxt_free_port_stats(bp);
10578 static int bnxt_probe_phy(struct bnxt *bp)
10581 struct bnxt_link_info *link_info = &bp->link_info;
10583 rc = bnxt_hwrm_phy_qcaps(bp);
10585 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
10589 mutex_init(&bp->link_lock);
10591 rc = bnxt_update_link(bp, false);
10593 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
10598 /* Older firmware does not have supported_auto_speeds, so assume
10599 * that all supported speeds can be autonegotiated.
10601 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
10602 link_info->support_auto_speeds = link_info->support_speeds;
10604 /*initialize the ethool setting copy with NVM settings */
10605 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
10606 link_info->autoneg = BNXT_AUTONEG_SPEED;
10607 if (bp->hwrm_spec_code >= 0x10201) {
10608 if (link_info->auto_pause_setting &
10609 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
10610 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10612 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10614 link_info->advertising = link_info->auto_link_speeds;
10616 link_info->req_link_speed = link_info->force_link_speed;
10617 link_info->req_duplex = link_info->duplex_setting;
10619 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
10620 link_info->req_flow_ctrl =
10621 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
10623 link_info->req_flow_ctrl = link_info->force_pause_setting;
10627 static int bnxt_get_max_irq(struct pci_dev *pdev)
10631 if (!pdev->msix_cap)
10634 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
10635 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
10638 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
10641 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10642 int max_ring_grps = 0, max_irq;
10644 *max_tx = hw_resc->max_tx_rings;
10645 *max_rx = hw_resc->max_rx_rings;
10646 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
10647 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
10648 bnxt_get_ulp_msix_num(bp),
10649 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
10650 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
10651 *max_cp = min_t(int, *max_cp, max_irq);
10652 max_ring_grps = hw_resc->max_hw_ring_grps;
10653 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
10657 if (bp->flags & BNXT_FLAG_AGG_RINGS)
10659 if (bp->flags & BNXT_FLAG_CHIP_P5) {
10660 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
10661 /* On P5 chips, max_cp output param should be available NQs */
10664 *max_rx = min_t(int, *max_rx, max_ring_grps);
10667 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
10671 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
10674 if (!rx || !tx || !cp)
10677 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
10680 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
10685 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
10686 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
10687 /* Not enough rings, try disabling agg rings. */
10688 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
10689 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
10691 /* set BNXT_FLAG_AGG_RINGS back for consistency */
10692 bp->flags |= BNXT_FLAG_AGG_RINGS;
10695 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
10696 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10697 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10698 bnxt_set_ring_params(bp);
10701 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
10702 int max_cp, max_stat, max_irq;
10704 /* Reserve minimum resources for RoCE */
10705 max_cp = bnxt_get_max_func_cp_rings(bp);
10706 max_stat = bnxt_get_max_func_stat_ctxs(bp);
10707 max_irq = bnxt_get_max_func_irqs(bp);
10708 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
10709 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
10710 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
10713 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
10714 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
10715 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
10716 max_cp = min_t(int, max_cp, max_irq);
10717 max_cp = min_t(int, max_cp, max_stat);
10718 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
10725 /* In initial default shared ring setting, each shared ring must have a
10728 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
10730 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
10731 bp->rx_nr_rings = bp->cp_nr_rings;
10732 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
10733 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10736 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
10738 int dflt_rings, max_rx_rings, max_tx_rings, rc;
10740 if (!bnxt_can_reserve_rings(bp))
10744 bp->flags |= BNXT_FLAG_SHARED_RINGS;
10745 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
10746 /* Reduce default rings on multi-port cards so that total default
10747 * rings do not exceed CPU count.
10749 if (bp->port_count > 1) {
10751 max_t(int, num_online_cpus() / bp->port_count, 1);
10753 dflt_rings = min_t(int, dflt_rings, max_rings);
10755 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
10758 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
10759 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
10761 bnxt_trim_dflt_sh_rings(bp);
10763 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
10764 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10766 rc = __bnxt_reserve_rings(bp);
10768 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
10769 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10771 bnxt_trim_dflt_sh_rings(bp);
10773 /* Rings may have been trimmed, re-reserve the trimmed rings. */
10774 if (bnxt_need_reserve_rings(bp)) {
10775 rc = __bnxt_reserve_rings(bp);
10777 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
10778 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10780 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10787 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
10791 if (bp->tx_nr_rings)
10794 bnxt_ulp_irq_stop(bp);
10795 bnxt_clear_int_mode(bp);
10796 rc = bnxt_set_dflt_rings(bp, true);
10798 netdev_err(bp->dev, "Not enough rings available.\n");
10799 goto init_dflt_ring_err;
10801 rc = bnxt_init_int_mode(bp);
10803 goto init_dflt_ring_err;
10805 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10806 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
10807 bp->flags |= BNXT_FLAG_RFS;
10808 bp->dev->features |= NETIF_F_NTUPLE;
10810 init_dflt_ring_err:
10811 bnxt_ulp_irq_restart(bp, rc);
10815 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
10820 bnxt_hwrm_func_qcaps(bp);
10822 if (netif_running(bp->dev))
10823 __bnxt_close_nic(bp, true, false);
10825 bnxt_ulp_irq_stop(bp);
10826 bnxt_clear_int_mode(bp);
10827 rc = bnxt_init_int_mode(bp);
10828 bnxt_ulp_irq_restart(bp, rc);
10830 if (netif_running(bp->dev)) {
10832 dev_close(bp->dev);
10834 rc = bnxt_open_nic(bp, true, false);
10840 static int bnxt_init_mac_addr(struct bnxt *bp)
10845 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
10847 #ifdef CONFIG_BNXT_SRIOV
10848 struct bnxt_vf_info *vf = &bp->vf;
10849 bool strict_approval = true;
10851 if (is_valid_ether_addr(vf->mac_addr)) {
10852 /* overwrite netdev dev_addr with admin VF MAC */
10853 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
10854 /* Older PF driver or firmware may not approve this
10857 strict_approval = false;
10859 eth_hw_addr_random(bp->dev);
10861 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
10867 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
10869 struct pci_dev *pdev = bp->pdev;
10870 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
10874 netdev_info(bp->dev, "Unable do read adapter's DSN");
10875 return -EOPNOTSUPP;
10878 /* DSN (two dw) is at an offset of 4 from the cap pos */
10880 pci_read_config_dword(pdev, pos, &dw);
10881 put_unaligned_le32(dw, &dsn[0]);
10882 pci_read_config_dword(pdev, pos + 4, &dw);
10883 put_unaligned_le32(dw, &dsn[4]);
10887 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
10889 static int version_printed;
10890 struct net_device *dev;
10894 if (pci_is_bridge(pdev))
10897 if (version_printed++ == 0)
10898 pr_info("%s", version);
10900 max_irqs = bnxt_get_max_irq(pdev);
10901 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
10905 bp = netdev_priv(dev);
10906 bnxt_set_max_func_irqs(bp, max_irqs);
10908 if (bnxt_vf_pciid(ent->driver_data))
10909 bp->flags |= BNXT_FLAG_VF;
10911 if (pdev->msix_cap)
10912 bp->flags |= BNXT_FLAG_MSIX_CAP;
10914 rc = bnxt_init_board(pdev, dev);
10916 goto init_err_free;
10918 dev->netdev_ops = &bnxt_netdev_ops;
10919 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
10920 dev->ethtool_ops = &bnxt_ethtool_ops;
10921 pci_set_drvdata(pdev, dev);
10923 rc = bnxt_alloc_hwrm_resources(bp);
10925 goto init_err_pci_clean;
10927 mutex_init(&bp->hwrm_cmd_lock);
10929 rc = bnxt_fw_init_one_p1(bp);
10931 goto init_err_pci_clean;
10933 if (BNXT_CHIP_P5(bp))
10934 bp->flags |= BNXT_FLAG_CHIP_P5;
10936 rc = bnxt_fw_init_one_p2(bp);
10938 goto init_err_pci_clean;
10940 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10941 NETIF_F_TSO | NETIF_F_TSO6 |
10942 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
10943 NETIF_F_GSO_IPXIP4 |
10944 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
10945 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
10946 NETIF_F_RXCSUM | NETIF_F_GRO;
10948 if (BNXT_SUPPORTS_TPA(bp))
10949 dev->hw_features |= NETIF_F_LRO;
10951 dev->hw_enc_features =
10952 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10953 NETIF_F_TSO | NETIF_F_TSO6 |
10954 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
10955 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
10956 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
10957 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
10958 NETIF_F_GSO_GRE_CSUM;
10959 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
10960 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
10961 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
10962 if (BNXT_SUPPORTS_TPA(bp))
10963 dev->hw_features |= NETIF_F_GRO_HW;
10964 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
10965 if (dev->features & NETIF_F_GRO_HW)
10966 dev->features &= ~NETIF_F_LRO;
10967 dev->priv_flags |= IFF_UNICAST_FLT;
10969 #ifdef CONFIG_BNXT_SRIOV
10970 init_waitqueue_head(&bp->sriov_cfg_wait);
10971 mutex_init(&bp->sriov_lock);
10973 if (BNXT_SUPPORTS_TPA(bp)) {
10974 bp->gro_func = bnxt_gro_func_5730x;
10975 if (BNXT_CHIP_P4(bp))
10976 bp->gro_func = bnxt_gro_func_5731x;
10977 else if (BNXT_CHIP_P5(bp))
10978 bp->gro_func = bnxt_gro_func_5750x;
10980 if (!BNXT_CHIP_P4_PLUS(bp))
10981 bp->flags |= BNXT_FLAG_DOUBLE_DB;
10983 bp->ulp_probe = bnxt_ulp_probe;
10985 rc = bnxt_init_mac_addr(bp);
10987 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
10988 rc = -EADDRNOTAVAIL;
10989 goto init_err_pci_clean;
10993 /* Read the adapter's DSN to use as the eswitch switch_id */
10994 rc = bnxt_pcie_dsn_get(bp, bp->switch_id);
10996 goto init_err_pci_clean;
10999 /* MTU range: 60 - FW defined max */
11000 dev->min_mtu = ETH_ZLEN;
11001 dev->max_mtu = bp->max_mtu;
11003 rc = bnxt_probe_phy(bp);
11005 goto init_err_pci_clean;
11007 bnxt_set_rx_skb_mode(bp, false);
11008 bnxt_set_tpa_flags(bp);
11009 bnxt_set_ring_params(bp);
11010 rc = bnxt_set_dflt_rings(bp, true);
11012 netdev_err(bp->dev, "Not enough rings available.\n");
11014 goto init_err_pci_clean;
11017 /* Default RSS hash cfg. */
11018 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11019 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11020 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11021 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11022 if (BNXT_CHIP_P4(bp) && bp->hwrm_spec_code >= 0x10501) {
11023 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11024 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11025 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11028 if (bnxt_rfs_supported(bp)) {
11029 dev->hw_features |= NETIF_F_NTUPLE;
11030 if (bnxt_rfs_capable(bp)) {
11031 bp->flags |= BNXT_FLAG_RFS;
11032 dev->features |= NETIF_F_NTUPLE;
11036 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
11037 bp->flags |= BNXT_FLAG_STRIP_VLAN;
11039 rc = bnxt_init_int_mode(bp);
11041 goto init_err_pci_clean;
11043 /* No TC has been set yet and rings may have been trimmed due to
11044 * limited MSIX, so we re-initialize the TX rings per TC.
11046 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11048 bnxt_get_wol_settings(bp);
11049 if (bp->flags & BNXT_FLAG_WOL_CAP)
11050 device_set_wakeup_enable(&pdev->dev, bp->wol);
11052 device_set_wakeup_capable(&pdev->dev, false);
11054 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11056 bnxt_hwrm_coal_params_qcaps(bp);
11061 create_singlethread_workqueue("bnxt_pf_wq");
11063 dev_err(&pdev->dev, "Unable to create workqueue.\n");
11064 goto init_err_pci_clean;
11070 rc = register_netdev(dev);
11072 goto init_err_cleanup_tc;
11075 bnxt_dl_register(bp);
11077 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
11078 board_info[ent->driver_data].name,
11079 (long)pci_resource_start(pdev, 0), dev->dev_addr);
11080 pcie_print_link_status(pdev);
11084 init_err_cleanup_tc:
11085 bnxt_shutdown_tc(bp);
11086 bnxt_clear_int_mode(bp);
11088 init_err_pci_clean:
11089 bnxt_free_hwrm_short_cmd_req(bp);
11090 bnxt_free_hwrm_resources(bp);
11091 bnxt_free_ctx_mem(bp);
11094 bnxt_cleanup_pci(bp);
11101 static void bnxt_shutdown(struct pci_dev *pdev)
11103 struct net_device *dev = pci_get_drvdata(pdev);
11110 bp = netdev_priv(dev);
11112 goto shutdown_exit;
11114 if (netif_running(dev))
11117 bnxt_ulp_shutdown(bp);
11119 if (system_state == SYSTEM_POWER_OFF) {
11120 bnxt_clear_int_mode(bp);
11121 pci_disable_device(pdev);
11122 pci_wake_from_d3(pdev, bp->wol);
11123 pci_set_power_state(pdev, PCI_D3hot);
11130 #ifdef CONFIG_PM_SLEEP
11131 static int bnxt_suspend(struct device *device)
11133 struct net_device *dev = dev_get_drvdata(device);
11134 struct bnxt *bp = netdev_priv(dev);
11138 if (netif_running(dev)) {
11139 netif_device_detach(dev);
11140 rc = bnxt_close(dev);
11142 bnxt_hwrm_func_drv_unrgtr(bp);
11147 static int bnxt_resume(struct device *device)
11149 struct net_device *dev = dev_get_drvdata(device);
11150 struct bnxt *bp = netdev_priv(dev);
11154 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
11158 rc = bnxt_hwrm_func_reset(bp);
11163 bnxt_get_wol_settings(bp);
11164 if (netif_running(dev)) {
11165 rc = bnxt_open(dev);
11167 netif_device_attach(dev);
11175 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
11176 #define BNXT_PM_OPS (&bnxt_pm_ops)
11180 #define BNXT_PM_OPS NULL
11182 #endif /* CONFIG_PM_SLEEP */
11185 * bnxt_io_error_detected - called when PCI error is detected
11186 * @pdev: Pointer to PCI device
11187 * @state: The current pci connection state
11189 * This function is called after a PCI bus error affecting
11190 * this device has been detected.
11192 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
11193 pci_channel_state_t state)
11195 struct net_device *netdev = pci_get_drvdata(pdev);
11196 struct bnxt *bp = netdev_priv(netdev);
11198 netdev_info(netdev, "PCI I/O error detected\n");
11201 netif_device_detach(netdev);
11205 if (state == pci_channel_io_perm_failure) {
11207 return PCI_ERS_RESULT_DISCONNECT;
11210 if (netif_running(netdev))
11211 bnxt_close(netdev);
11213 pci_disable_device(pdev);
11216 /* Request a slot slot reset. */
11217 return PCI_ERS_RESULT_NEED_RESET;
11221 * bnxt_io_slot_reset - called after the pci bus has been reset.
11222 * @pdev: Pointer to PCI device
11224 * Restart the card from scratch, as if from a cold-boot.
11225 * At this point, the card has exprienced a hard reset,
11226 * followed by fixups by BIOS, and has its config space
11227 * set up identically to what it was at cold boot.
11229 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
11231 struct net_device *netdev = pci_get_drvdata(pdev);
11232 struct bnxt *bp = netdev_priv(netdev);
11234 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
11236 netdev_info(bp->dev, "PCI Slot Reset\n");
11240 if (pci_enable_device(pdev)) {
11241 dev_err(&pdev->dev,
11242 "Cannot re-enable PCI device after reset.\n");
11244 pci_set_master(pdev);
11246 err = bnxt_hwrm_func_reset(bp);
11247 if (!err && netif_running(netdev))
11248 err = bnxt_open(netdev);
11251 result = PCI_ERS_RESULT_RECOVERED;
11252 bnxt_ulp_start(bp);
11256 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
11261 return PCI_ERS_RESULT_RECOVERED;
11265 * bnxt_io_resume - called when traffic can start flowing again.
11266 * @pdev: Pointer to PCI device
11268 * This callback is called when the error recovery driver tells
11269 * us that its OK to resume normal operation.
11271 static void bnxt_io_resume(struct pci_dev *pdev)
11273 struct net_device *netdev = pci_get_drvdata(pdev);
11277 netif_device_attach(netdev);
11282 static const struct pci_error_handlers bnxt_err_handler = {
11283 .error_detected = bnxt_io_error_detected,
11284 .slot_reset = bnxt_io_slot_reset,
11285 .resume = bnxt_io_resume
11288 static struct pci_driver bnxt_pci_driver = {
11289 .name = DRV_MODULE_NAME,
11290 .id_table = bnxt_pci_tbl,
11291 .probe = bnxt_init_one,
11292 .remove = bnxt_remove_one,
11293 .shutdown = bnxt_shutdown,
11294 .driver.pm = BNXT_PM_OPS,
11295 .err_handler = &bnxt_err_handler,
11296 #if defined(CONFIG_BNXT_SRIOV)
11297 .sriov_configure = bnxt_sriov_configure,
11301 static int __init bnxt_init(void)
11304 return pci_register_driver(&bnxt_pci_driver);
11307 static void __exit bnxt_exit(void)
11309 pci_unregister_driver(&bnxt_pci_driver);
11311 destroy_workqueue(bnxt_pf_wq);
11315 module_init(bnxt_init);
11316 module_exit(bnxt_exit);