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>
61 #include "bnxt_sriov.h"
62 #include "bnxt_ethtool.h"
67 #include "bnxt_devlink.h"
68 #include "bnxt_debugfs.h"
70 #define BNXT_TX_TIMEOUT (5 * HZ)
72 static const char version[] =
73 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
75 MODULE_LICENSE("GPL");
76 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
77 MODULE_VERSION(DRV_MODULE_VERSION);
79 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
80 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
81 #define BNXT_RX_COPY_THRESH 256
83 #define BNXT_TX_PUSH_THRESH 164
126 /* indexed by enum above */
127 static const struct {
130 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
131 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
132 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
133 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
134 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
135 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
136 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
137 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
138 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
139 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
140 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
141 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
142 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
143 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
144 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
145 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
146 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
147 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
148 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
149 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
150 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
151 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
152 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
153 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
154 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
155 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
156 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
157 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
158 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
159 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
160 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
161 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
162 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
163 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
164 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
165 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
166 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
167 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
170 static const struct pci_device_id bnxt_pci_tbl[] = {
171 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
173 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
174 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
175 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
176 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
177 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
178 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
179 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
180 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
181 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
182 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
183 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
184 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
185 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
186 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
187 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
188 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
189 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
190 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
191 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
192 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
193 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
194 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
195 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
196 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
197 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
198 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
199 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
200 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
201 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
202 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
203 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
204 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
205 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
206 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
207 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
208 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
209 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
210 #ifdef CONFIG_BNXT_SRIOV
211 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
212 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
213 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
214 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
215 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
216 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
217 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
218 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
219 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
227 static const u16 bnxt_vf_req_snif[] = {
231 HWRM_CFA_L2_FILTER_ALLOC,
234 static const u16 bnxt_async_events_arr[] = {
235 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
236 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
237 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
238 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
239 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
242 static struct workqueue_struct *bnxt_pf_wq;
244 static bool bnxt_vf_pciid(enum board_idx idx)
246 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
247 idx == NETXTREME_S_VF || idx == NETXTREME_E_P5_VF);
250 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
251 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
252 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
254 #define BNXT_CP_DB_IRQ_DIS(db) \
255 writel(DB_CP_IRQ_DIS_FLAGS, db)
257 #define BNXT_DB_CQ(db, idx) \
258 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
260 #define BNXT_DB_NQ_P5(db, idx) \
261 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
263 #define BNXT_DB_CQ_ARM(db, idx) \
264 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
266 #define BNXT_DB_NQ_ARM_P5(db, idx) \
267 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
269 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
271 if (bp->flags & BNXT_FLAG_CHIP_P5)
272 BNXT_DB_NQ_P5(db, idx);
277 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
279 if (bp->flags & BNXT_FLAG_CHIP_P5)
280 BNXT_DB_NQ_ARM_P5(db, idx);
282 BNXT_DB_CQ_ARM(db, idx);
285 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
287 if (bp->flags & BNXT_FLAG_CHIP_P5)
288 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
294 const u16 bnxt_lhint_arr[] = {
295 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
296 TX_BD_FLAGS_LHINT_512_TO_1023,
297 TX_BD_FLAGS_LHINT_1024_TO_2047,
298 TX_BD_FLAGS_LHINT_1024_TO_2047,
299 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
300 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
301 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
302 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
303 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
304 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
305 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
306 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
307 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
308 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
309 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
310 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
311 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
312 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
313 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
316 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
318 struct metadata_dst *md_dst = skb_metadata_dst(skb);
320 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
323 return md_dst->u.port_info.port_id;
326 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
328 struct bnxt *bp = netdev_priv(dev);
330 struct tx_bd_ext *txbd1;
331 struct netdev_queue *txq;
334 unsigned int length, pad = 0;
335 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
337 struct pci_dev *pdev = bp->pdev;
338 struct bnxt_tx_ring_info *txr;
339 struct bnxt_sw_tx_bd *tx_buf;
341 i = skb_get_queue_mapping(skb);
342 if (unlikely(i >= bp->tx_nr_rings)) {
343 dev_kfree_skb_any(skb);
347 txq = netdev_get_tx_queue(dev, i);
348 txr = &bp->tx_ring[bp->tx_ring_map[i]];
351 free_size = bnxt_tx_avail(bp, txr);
352 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
353 netif_tx_stop_queue(txq);
354 return NETDEV_TX_BUSY;
358 len = skb_headlen(skb);
359 last_frag = skb_shinfo(skb)->nr_frags;
361 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
363 txbd->tx_bd_opaque = prod;
365 tx_buf = &txr->tx_buf_ring[prod];
367 tx_buf->nr_frags = last_frag;
370 cfa_action = bnxt_xmit_get_cfa_action(skb);
371 if (skb_vlan_tag_present(skb)) {
372 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
373 skb_vlan_tag_get(skb);
374 /* Currently supports 8021Q, 8021AD vlan offloads
375 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
377 if (skb->vlan_proto == htons(ETH_P_8021Q))
378 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
381 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
382 struct tx_push_buffer *tx_push_buf = txr->tx_push;
383 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
384 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
385 void __iomem *db = txr->tx_db.doorbell;
386 void *pdata = tx_push_buf->data;
390 /* Set COAL_NOW to be ready quickly for the next push */
391 tx_push->tx_bd_len_flags_type =
392 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
393 TX_BD_TYPE_LONG_TX_BD |
394 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
395 TX_BD_FLAGS_COAL_NOW |
396 TX_BD_FLAGS_PACKET_END |
397 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
399 if (skb->ip_summed == CHECKSUM_PARTIAL)
400 tx_push1->tx_bd_hsize_lflags =
401 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
403 tx_push1->tx_bd_hsize_lflags = 0;
405 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
406 tx_push1->tx_bd_cfa_action =
407 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
409 end = pdata + length;
410 end = PTR_ALIGN(end, 8) - 1;
413 skb_copy_from_linear_data(skb, pdata, len);
415 for (j = 0; j < last_frag; j++) {
416 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
419 fptr = skb_frag_address_safe(frag);
423 memcpy(pdata, fptr, skb_frag_size(frag));
424 pdata += skb_frag_size(frag);
427 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
428 txbd->tx_bd_haddr = txr->data_mapping;
429 prod = NEXT_TX(prod);
430 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
431 memcpy(txbd, tx_push1, sizeof(*txbd));
432 prod = NEXT_TX(prod);
434 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
438 netdev_tx_sent_queue(txq, skb->len);
439 wmb(); /* Sync is_push and byte queue before pushing data */
441 push_len = (length + sizeof(*tx_push) + 7) / 8;
443 __iowrite64_copy(db, tx_push_buf, 16);
444 __iowrite32_copy(db + 4, tx_push_buf + 1,
445 (push_len - 16) << 1);
447 __iowrite64_copy(db, tx_push_buf, push_len);
454 if (length < BNXT_MIN_PKT_SIZE) {
455 pad = BNXT_MIN_PKT_SIZE - length;
456 if (skb_pad(skb, pad)) {
457 /* SKB already freed. */
461 length = BNXT_MIN_PKT_SIZE;
464 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
466 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
467 dev_kfree_skb_any(skb);
472 dma_unmap_addr_set(tx_buf, mapping, mapping);
473 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
474 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
476 txbd->tx_bd_haddr = cpu_to_le64(mapping);
478 prod = NEXT_TX(prod);
479 txbd1 = (struct tx_bd_ext *)
480 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
482 txbd1->tx_bd_hsize_lflags = 0;
483 if (skb_is_gso(skb)) {
486 if (skb->encapsulation)
487 hdr_len = skb_inner_network_offset(skb) +
488 skb_inner_network_header_len(skb) +
489 inner_tcp_hdrlen(skb);
491 hdr_len = skb_transport_offset(skb) +
494 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
496 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
497 length = skb_shinfo(skb)->gso_size;
498 txbd1->tx_bd_mss = cpu_to_le32(length);
500 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
501 txbd1->tx_bd_hsize_lflags =
502 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
503 txbd1->tx_bd_mss = 0;
507 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
508 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
513 flags |= bnxt_lhint_arr[length];
514 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
516 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
517 txbd1->tx_bd_cfa_action =
518 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
519 for (i = 0; i < last_frag; i++) {
520 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
522 prod = NEXT_TX(prod);
523 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
525 len = skb_frag_size(frag);
526 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
529 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
532 tx_buf = &txr->tx_buf_ring[prod];
533 dma_unmap_addr_set(tx_buf, mapping, mapping);
535 txbd->tx_bd_haddr = cpu_to_le64(mapping);
537 flags = len << TX_BD_LEN_SHIFT;
538 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
542 txbd->tx_bd_len_flags_type =
543 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
544 TX_BD_FLAGS_PACKET_END);
546 netdev_tx_sent_queue(txq, skb->len);
548 /* Sync BD data before updating doorbell */
551 prod = NEXT_TX(prod);
554 if (!skb->xmit_more || netif_xmit_stopped(txq))
555 bnxt_db_write(bp, &txr->tx_db, prod);
559 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
560 if (skb->xmit_more && !tx_buf->is_push)
561 bnxt_db_write(bp, &txr->tx_db, prod);
563 netif_tx_stop_queue(txq);
565 /* netif_tx_stop_queue() must be done before checking
566 * tx index in bnxt_tx_avail() below, because in
567 * bnxt_tx_int(), we update tx index before checking for
568 * netif_tx_queue_stopped().
571 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
572 netif_tx_wake_queue(txq);
579 /* start back at beginning and unmap skb */
581 tx_buf = &txr->tx_buf_ring[prod];
583 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
584 skb_headlen(skb), PCI_DMA_TODEVICE);
585 prod = NEXT_TX(prod);
587 /* unmap remaining mapped pages */
588 for (i = 0; i < last_frag; i++) {
589 prod = NEXT_TX(prod);
590 tx_buf = &txr->tx_buf_ring[prod];
591 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
592 skb_frag_size(&skb_shinfo(skb)->frags[i]),
596 dev_kfree_skb_any(skb);
600 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
602 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
603 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
604 u16 cons = txr->tx_cons;
605 struct pci_dev *pdev = bp->pdev;
607 unsigned int tx_bytes = 0;
609 for (i = 0; i < nr_pkts; i++) {
610 struct bnxt_sw_tx_bd *tx_buf;
614 tx_buf = &txr->tx_buf_ring[cons];
615 cons = NEXT_TX(cons);
619 if (tx_buf->is_push) {
624 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
625 skb_headlen(skb), PCI_DMA_TODEVICE);
626 last = tx_buf->nr_frags;
628 for (j = 0; j < last; j++) {
629 cons = NEXT_TX(cons);
630 tx_buf = &txr->tx_buf_ring[cons];
633 dma_unmap_addr(tx_buf, mapping),
634 skb_frag_size(&skb_shinfo(skb)->frags[j]),
639 cons = NEXT_TX(cons);
641 tx_bytes += skb->len;
642 dev_kfree_skb_any(skb);
645 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
648 /* Need to make the tx_cons update visible to bnxt_start_xmit()
649 * before checking for netif_tx_queue_stopped(). Without the
650 * memory barrier, there is a small possibility that bnxt_start_xmit()
651 * will miss it and cause the queue to be stopped forever.
655 if (unlikely(netif_tx_queue_stopped(txq)) &&
656 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
657 __netif_tx_lock(txq, smp_processor_id());
658 if (netif_tx_queue_stopped(txq) &&
659 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
660 txr->dev_state != BNXT_DEV_STATE_CLOSING)
661 netif_tx_wake_queue(txq);
662 __netif_tx_unlock(txq);
666 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
669 struct device *dev = &bp->pdev->dev;
672 page = alloc_page(gfp);
676 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
677 DMA_ATTR_WEAK_ORDERING);
678 if (dma_mapping_error(dev, *mapping)) {
682 *mapping += bp->rx_dma_offset;
686 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
690 struct pci_dev *pdev = bp->pdev;
692 data = kmalloc(bp->rx_buf_size, gfp);
696 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
697 bp->rx_buf_use_size, bp->rx_dir,
698 DMA_ATTR_WEAK_ORDERING);
700 if (dma_mapping_error(&pdev->dev, *mapping)) {
707 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
710 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
711 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
714 if (BNXT_RX_PAGE_MODE(bp)) {
715 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
721 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
723 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
729 rx_buf->data_ptr = data + bp->rx_offset;
731 rx_buf->mapping = mapping;
733 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
737 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
739 u16 prod = rxr->rx_prod;
740 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
741 struct rx_bd *cons_bd, *prod_bd;
743 prod_rx_buf = &rxr->rx_buf_ring[prod];
744 cons_rx_buf = &rxr->rx_buf_ring[cons];
746 prod_rx_buf->data = data;
747 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
749 prod_rx_buf->mapping = cons_rx_buf->mapping;
751 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
752 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
754 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
757 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
759 u16 next, max = rxr->rx_agg_bmap_size;
761 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
763 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
767 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
768 struct bnxt_rx_ring_info *rxr,
772 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
773 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
774 struct pci_dev *pdev = bp->pdev;
777 u16 sw_prod = rxr->rx_sw_agg_prod;
778 unsigned int offset = 0;
780 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
783 page = alloc_page(gfp);
787 rxr->rx_page_offset = 0;
789 offset = rxr->rx_page_offset;
790 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
791 if (rxr->rx_page_offset == PAGE_SIZE)
796 page = alloc_page(gfp);
801 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
802 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
803 DMA_ATTR_WEAK_ORDERING);
804 if (dma_mapping_error(&pdev->dev, mapping)) {
809 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
810 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
812 __set_bit(sw_prod, rxr->rx_agg_bmap);
813 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
814 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
816 rx_agg_buf->page = page;
817 rx_agg_buf->offset = offset;
818 rx_agg_buf->mapping = mapping;
819 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
820 rxbd->rx_bd_opaque = sw_prod;
824 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 cp_cons,
827 struct bnxt_napi *bnapi = cpr->bnapi;
828 struct bnxt *bp = bnapi->bp;
829 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
830 u16 prod = rxr->rx_agg_prod;
831 u16 sw_prod = rxr->rx_sw_agg_prod;
834 for (i = 0; i < agg_bufs; i++) {
836 struct rx_agg_cmp *agg;
837 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
838 struct rx_bd *prod_bd;
841 agg = (struct rx_agg_cmp *)
842 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
843 cons = agg->rx_agg_cmp_opaque;
844 __clear_bit(cons, rxr->rx_agg_bmap);
846 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
847 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
849 __set_bit(sw_prod, rxr->rx_agg_bmap);
850 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
851 cons_rx_buf = &rxr->rx_agg_ring[cons];
853 /* It is possible for sw_prod to be equal to cons, so
854 * set cons_rx_buf->page to NULL first.
856 page = cons_rx_buf->page;
857 cons_rx_buf->page = NULL;
858 prod_rx_buf->page = page;
859 prod_rx_buf->offset = cons_rx_buf->offset;
861 prod_rx_buf->mapping = cons_rx_buf->mapping;
863 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
865 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
866 prod_bd->rx_bd_opaque = sw_prod;
868 prod = NEXT_RX_AGG(prod);
869 sw_prod = NEXT_RX_AGG(sw_prod);
870 cp_cons = NEXT_CMP(cp_cons);
872 rxr->rx_agg_prod = prod;
873 rxr->rx_sw_agg_prod = sw_prod;
876 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
877 struct bnxt_rx_ring_info *rxr,
878 u16 cons, void *data, u8 *data_ptr,
880 unsigned int offset_and_len)
882 unsigned int payload = offset_and_len >> 16;
883 unsigned int len = offset_and_len & 0xffff;
884 struct skb_frag_struct *frag;
885 struct page *page = data;
886 u16 prod = rxr->rx_prod;
890 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
892 bnxt_reuse_rx_data(rxr, cons, data);
895 dma_addr -= bp->rx_dma_offset;
896 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
897 DMA_ATTR_WEAK_ORDERING);
899 if (unlikely(!payload))
900 payload = eth_get_headlen(data_ptr, len);
902 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
908 off = (void *)data_ptr - page_address(page);
909 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
910 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
911 payload + NET_IP_ALIGN);
913 frag = &skb_shinfo(skb)->frags[0];
914 skb_frag_size_sub(frag, payload);
915 frag->page_offset += payload;
916 skb->data_len -= payload;
917 skb->tail += payload;
922 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
923 struct bnxt_rx_ring_info *rxr, u16 cons,
924 void *data, u8 *data_ptr,
926 unsigned int offset_and_len)
928 u16 prod = rxr->rx_prod;
932 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
934 bnxt_reuse_rx_data(rxr, cons, data);
938 skb = build_skb(data, 0);
939 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
940 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
946 skb_reserve(skb, bp->rx_offset);
947 skb_put(skb, offset_and_len & 0xffff);
951 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
952 struct bnxt_cp_ring_info *cpr,
953 struct sk_buff *skb, u16 cp_cons,
956 struct bnxt_napi *bnapi = cpr->bnapi;
957 struct pci_dev *pdev = bp->pdev;
958 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
959 u16 prod = rxr->rx_agg_prod;
962 for (i = 0; i < agg_bufs; i++) {
964 struct rx_agg_cmp *agg;
965 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
969 agg = (struct rx_agg_cmp *)
970 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
971 cons = agg->rx_agg_cmp_opaque;
972 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
973 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
975 cons_rx_buf = &rxr->rx_agg_ring[cons];
976 skb_fill_page_desc(skb, i, cons_rx_buf->page,
977 cons_rx_buf->offset, frag_len);
978 __clear_bit(cons, rxr->rx_agg_bmap);
980 /* It is possible for bnxt_alloc_rx_page() to allocate
981 * a sw_prod index that equals the cons index, so we
982 * need to clear the cons entry now.
984 mapping = cons_rx_buf->mapping;
985 page = cons_rx_buf->page;
986 cons_rx_buf->page = NULL;
988 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
989 struct skb_shared_info *shinfo;
990 unsigned int nr_frags;
992 shinfo = skb_shinfo(skb);
993 nr_frags = --shinfo->nr_frags;
994 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
998 cons_rx_buf->page = page;
1000 /* Update prod since possibly some pages have been
1001 * allocated already.
1003 rxr->rx_agg_prod = prod;
1004 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs - i);
1008 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1010 DMA_ATTR_WEAK_ORDERING);
1012 skb->data_len += frag_len;
1013 skb->len += frag_len;
1014 skb->truesize += PAGE_SIZE;
1016 prod = NEXT_RX_AGG(prod);
1017 cp_cons = NEXT_CMP(cp_cons);
1019 rxr->rx_agg_prod = prod;
1023 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1024 u8 agg_bufs, u32 *raw_cons)
1027 struct rx_agg_cmp *agg;
1029 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1030 last = RING_CMP(*raw_cons);
1031 agg = (struct rx_agg_cmp *)
1032 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1033 return RX_AGG_CMP_VALID(agg, *raw_cons);
1036 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1040 struct bnxt *bp = bnapi->bp;
1041 struct pci_dev *pdev = bp->pdev;
1042 struct sk_buff *skb;
1044 skb = napi_alloc_skb(&bnapi->napi, len);
1048 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1051 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1052 len + NET_IP_ALIGN);
1054 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1061 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1062 u32 *raw_cons, void *cmp)
1064 struct rx_cmp *rxcmp = cmp;
1065 u32 tmp_raw_cons = *raw_cons;
1066 u8 cmp_type, agg_bufs = 0;
1068 cmp_type = RX_CMP_TYPE(rxcmp);
1070 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1071 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1073 RX_CMP_AGG_BUFS_SHIFT;
1074 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1075 struct rx_tpa_end_cmp *tpa_end = cmp;
1077 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1078 RX_TPA_END_CMP_AGG_BUFS) >>
1079 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1083 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1086 *raw_cons = tmp_raw_cons;
1090 static void bnxt_queue_sp_work(struct bnxt *bp)
1093 queue_work(bnxt_pf_wq, &bp->sp_task);
1095 schedule_work(&bp->sp_task);
1098 static void bnxt_cancel_sp_work(struct bnxt *bp)
1101 flush_workqueue(bnxt_pf_wq);
1103 cancel_work_sync(&bp->sp_task);
1106 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1108 if (!rxr->bnapi->in_reset) {
1109 rxr->bnapi->in_reset = true;
1110 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1111 bnxt_queue_sp_work(bp);
1113 rxr->rx_next_cons = 0xffff;
1116 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1117 struct rx_tpa_start_cmp *tpa_start,
1118 struct rx_tpa_start_cmp_ext *tpa_start1)
1120 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1122 struct bnxt_tpa_info *tpa_info;
1123 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1124 struct rx_bd *prod_bd;
1127 cons = tpa_start->rx_tpa_start_cmp_opaque;
1128 prod = rxr->rx_prod;
1129 cons_rx_buf = &rxr->rx_buf_ring[cons];
1130 prod_rx_buf = &rxr->rx_buf_ring[prod];
1131 tpa_info = &rxr->rx_tpa[agg_id];
1133 if (unlikely(cons != rxr->rx_next_cons)) {
1134 bnxt_sched_reset(bp, rxr);
1137 /* Store cfa_code in tpa_info to use in tpa_end
1138 * completion processing.
1140 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1141 prod_rx_buf->data = tpa_info->data;
1142 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1144 mapping = tpa_info->mapping;
1145 prod_rx_buf->mapping = mapping;
1147 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1149 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1151 tpa_info->data = cons_rx_buf->data;
1152 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1153 cons_rx_buf->data = NULL;
1154 tpa_info->mapping = cons_rx_buf->mapping;
1157 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1158 RX_TPA_START_CMP_LEN_SHIFT;
1159 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1160 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1162 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1163 tpa_info->gso_type = SKB_GSO_TCPV4;
1164 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1165 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1166 tpa_info->gso_type = SKB_GSO_TCPV6;
1167 tpa_info->rss_hash =
1168 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1170 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1171 tpa_info->gso_type = 0;
1172 if (netif_msg_rx_err(bp))
1173 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1175 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1176 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1177 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1179 rxr->rx_prod = NEXT_RX(prod);
1180 cons = NEXT_RX(cons);
1181 rxr->rx_next_cons = NEXT_RX(cons);
1182 cons_rx_buf = &rxr->rx_buf_ring[cons];
1184 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1185 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1186 cons_rx_buf->data = NULL;
1189 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 cp_cons,
1193 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs);
1196 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1197 int payload_off, int tcp_ts,
1198 struct sk_buff *skb)
1203 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1204 u32 hdr_info = tpa_info->hdr_info;
1205 bool loopback = false;
1207 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1208 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1209 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1211 /* If the packet is an internal loopback packet, the offsets will
1212 * have an extra 4 bytes.
1214 if (inner_mac_off == 4) {
1216 } else if (inner_mac_off > 4) {
1217 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1220 /* We only support inner iPv4/ipv6. If we don't see the
1221 * correct protocol ID, it must be a loopback packet where
1222 * the offsets are off by 4.
1224 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1228 /* internal loopback packet, subtract all offsets by 4 */
1234 nw_off = inner_ip_off - ETH_HLEN;
1235 skb_set_network_header(skb, nw_off);
1236 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1237 struct ipv6hdr *iph = ipv6_hdr(skb);
1239 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1240 len = skb->len - skb_transport_offset(skb);
1242 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1244 struct iphdr *iph = ip_hdr(skb);
1246 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1247 len = skb->len - skb_transport_offset(skb);
1249 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1252 if (inner_mac_off) { /* tunnel */
1253 struct udphdr *uh = NULL;
1254 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1257 if (proto == htons(ETH_P_IP)) {
1258 struct iphdr *iph = (struct iphdr *)skb->data;
1260 if (iph->protocol == IPPROTO_UDP)
1261 uh = (struct udphdr *)(iph + 1);
1263 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1265 if (iph->nexthdr == IPPROTO_UDP)
1266 uh = (struct udphdr *)(iph + 1);
1270 skb_shinfo(skb)->gso_type |=
1271 SKB_GSO_UDP_TUNNEL_CSUM;
1273 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1280 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1281 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1283 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1284 int payload_off, int tcp_ts,
1285 struct sk_buff *skb)
1289 int len, nw_off, tcp_opt_len = 0;
1294 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1297 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1299 skb_set_network_header(skb, nw_off);
1301 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1302 len = skb->len - skb_transport_offset(skb);
1304 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1305 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1306 struct ipv6hdr *iph;
1308 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1310 skb_set_network_header(skb, nw_off);
1311 iph = ipv6_hdr(skb);
1312 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1313 len = skb->len - skb_transport_offset(skb);
1315 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1317 dev_kfree_skb_any(skb);
1321 if (nw_off) { /* tunnel */
1322 struct udphdr *uh = NULL;
1324 if (skb->protocol == htons(ETH_P_IP)) {
1325 struct iphdr *iph = (struct iphdr *)skb->data;
1327 if (iph->protocol == IPPROTO_UDP)
1328 uh = (struct udphdr *)(iph + 1);
1330 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1332 if (iph->nexthdr == IPPROTO_UDP)
1333 uh = (struct udphdr *)(iph + 1);
1337 skb_shinfo(skb)->gso_type |=
1338 SKB_GSO_UDP_TUNNEL_CSUM;
1340 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1347 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1348 struct bnxt_tpa_info *tpa_info,
1349 struct rx_tpa_end_cmp *tpa_end,
1350 struct rx_tpa_end_cmp_ext *tpa_end1,
1351 struct sk_buff *skb)
1357 segs = TPA_END_TPA_SEGS(tpa_end);
1361 NAPI_GRO_CB(skb)->count = segs;
1362 skb_shinfo(skb)->gso_size =
1363 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1364 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1365 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1366 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1367 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1368 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1370 tcp_gro_complete(skb);
1375 /* Given the cfa_code of a received packet determine which
1376 * netdev (vf-rep or PF) the packet is destined to.
1378 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1380 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1382 /* if vf-rep dev is NULL, the must belongs to the PF */
1383 return dev ? dev : bp->dev;
1386 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1387 struct bnxt_cp_ring_info *cpr,
1389 struct rx_tpa_end_cmp *tpa_end,
1390 struct rx_tpa_end_cmp_ext *tpa_end1,
1393 struct bnxt_napi *bnapi = cpr->bnapi;
1394 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1395 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1396 u8 *data_ptr, agg_bufs;
1397 u16 cp_cons = RING_CMP(*raw_cons);
1399 struct bnxt_tpa_info *tpa_info;
1401 struct sk_buff *skb;
1404 if (unlikely(bnapi->in_reset)) {
1405 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1408 return ERR_PTR(-EBUSY);
1412 tpa_info = &rxr->rx_tpa[agg_id];
1413 data = tpa_info->data;
1414 data_ptr = tpa_info->data_ptr;
1416 len = tpa_info->len;
1417 mapping = tpa_info->mapping;
1419 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1420 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1423 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1424 return ERR_PTR(-EBUSY);
1426 *event |= BNXT_AGG_EVENT;
1427 cp_cons = NEXT_CMP(cp_cons);
1430 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1431 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
1432 if (agg_bufs > MAX_SKB_FRAGS)
1433 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1434 agg_bufs, (int)MAX_SKB_FRAGS);
1438 if (len <= bp->rx_copy_thresh) {
1439 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1441 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
1446 dma_addr_t new_mapping;
1448 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1450 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
1454 tpa_info->data = new_data;
1455 tpa_info->data_ptr = new_data + bp->rx_offset;
1456 tpa_info->mapping = new_mapping;
1458 skb = build_skb(data, 0);
1459 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1460 bp->rx_buf_use_size, bp->rx_dir,
1461 DMA_ATTR_WEAK_ORDERING);
1465 bnxt_abort_tpa(cpr, cp_cons, agg_bufs);
1468 skb_reserve(skb, bp->rx_offset);
1473 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs);
1475 /* Page reuse already handled by bnxt_rx_pages(). */
1481 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1483 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1484 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1486 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1487 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1488 u16 vlan_proto = tpa_info->metadata >>
1489 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1490 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1492 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1495 skb_checksum_none_assert(skb);
1496 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1497 skb->ip_summed = CHECKSUM_UNNECESSARY;
1499 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1502 if (TPA_END_GRO(tpa_end))
1503 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1508 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1509 struct sk_buff *skb)
1511 if (skb->dev != bp->dev) {
1512 /* this packet belongs to a vf-rep */
1513 bnxt_vf_rep_rx(bp, skb);
1516 skb_record_rx_queue(skb, bnapi->index);
1517 napi_gro_receive(&bnapi->napi, skb);
1520 /* returns the following:
1521 * 1 - 1 packet successfully received
1522 * 0 - successful TPA_START, packet not completed yet
1523 * -EBUSY - completion ring does not have all the agg buffers yet
1524 * -ENOMEM - packet aborted due to out of memory
1525 * -EIO - packet aborted due to hw error indicated in BD
1527 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1528 u32 *raw_cons, u8 *event)
1530 struct bnxt_napi *bnapi = cpr->bnapi;
1531 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1532 struct net_device *dev = bp->dev;
1533 struct rx_cmp *rxcmp;
1534 struct rx_cmp_ext *rxcmp1;
1535 u32 tmp_raw_cons = *raw_cons;
1536 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1537 struct bnxt_sw_rx_bd *rx_buf;
1539 u8 *data_ptr, agg_bufs, cmp_type;
1540 dma_addr_t dma_addr;
1541 struct sk_buff *skb;
1546 rxcmp = (struct rx_cmp *)
1547 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1549 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1550 cp_cons = RING_CMP(tmp_raw_cons);
1551 rxcmp1 = (struct rx_cmp_ext *)
1552 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1554 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1557 cmp_type = RX_CMP_TYPE(rxcmp);
1559 prod = rxr->rx_prod;
1561 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1562 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1563 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1565 *event |= BNXT_RX_EVENT;
1566 goto next_rx_no_prod_no_len;
1568 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1569 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1570 (struct rx_tpa_end_cmp *)rxcmp,
1571 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1578 bnxt_deliver_skb(bp, bnapi, skb);
1581 *event |= BNXT_RX_EVENT;
1582 goto next_rx_no_prod_no_len;
1585 cons = rxcmp->rx_cmp_opaque;
1586 rx_buf = &rxr->rx_buf_ring[cons];
1587 data = rx_buf->data;
1588 data_ptr = rx_buf->data_ptr;
1589 if (unlikely(cons != rxr->rx_next_cons)) {
1590 int rc1 = bnxt_discard_rx(bp, cpr, raw_cons, rxcmp);
1592 bnxt_sched_reset(bp, rxr);
1597 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1598 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1601 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1604 cp_cons = NEXT_CMP(cp_cons);
1605 *event |= BNXT_AGG_EVENT;
1607 *event |= BNXT_RX_EVENT;
1609 rx_buf->data = NULL;
1610 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1611 bnxt_reuse_rx_data(rxr, cons, data);
1613 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, agg_bufs);
1619 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1620 dma_addr = rx_buf->mapping;
1622 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1627 if (len <= bp->rx_copy_thresh) {
1628 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1629 bnxt_reuse_rx_data(rxr, cons, data);
1637 if (rx_buf->data_ptr == data_ptr)
1638 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1641 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1650 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs);
1657 if (RX_CMP_HASH_VALID(rxcmp)) {
1658 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1659 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1661 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1662 if (hash_type != 1 && hash_type != 3)
1663 type = PKT_HASH_TYPE_L3;
1664 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1667 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1668 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1670 if ((rxcmp1->rx_cmp_flags2 &
1671 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1672 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1673 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1674 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1675 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1677 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1680 skb_checksum_none_assert(skb);
1681 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1682 if (dev->features & NETIF_F_RXCSUM) {
1683 skb->ip_summed = CHECKSUM_UNNECESSARY;
1684 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1687 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1688 if (dev->features & NETIF_F_RXCSUM)
1689 bnapi->cp_ring.rx_l4_csum_errors++;
1693 bnxt_deliver_skb(bp, bnapi, skb);
1697 rxr->rx_prod = NEXT_RX(prod);
1698 rxr->rx_next_cons = NEXT_RX(cons);
1700 cpr->rx_packets += 1;
1701 cpr->rx_bytes += len;
1703 next_rx_no_prod_no_len:
1704 *raw_cons = tmp_raw_cons;
1709 /* In netpoll mode, if we are using a combined completion ring, we need to
1710 * discard the rx packets and recycle the buffers.
1712 static int bnxt_force_rx_discard(struct bnxt *bp,
1713 struct bnxt_cp_ring_info *cpr,
1714 u32 *raw_cons, u8 *event)
1716 u32 tmp_raw_cons = *raw_cons;
1717 struct rx_cmp_ext *rxcmp1;
1718 struct rx_cmp *rxcmp;
1722 cp_cons = RING_CMP(tmp_raw_cons);
1723 rxcmp = (struct rx_cmp *)
1724 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1726 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1727 cp_cons = RING_CMP(tmp_raw_cons);
1728 rxcmp1 = (struct rx_cmp_ext *)
1729 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1731 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1734 cmp_type = RX_CMP_TYPE(rxcmp);
1735 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1736 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1737 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1738 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1739 struct rx_tpa_end_cmp_ext *tpa_end1;
1741 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1742 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1743 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1745 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1748 #define BNXT_GET_EVENT_PORT(data) \
1750 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1752 static int bnxt_async_event_process(struct bnxt *bp,
1753 struct hwrm_async_event_cmpl *cmpl)
1755 u16 event_id = le16_to_cpu(cmpl->event_id);
1757 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1759 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1760 u32 data1 = le32_to_cpu(cmpl->event_data1);
1761 struct bnxt_link_info *link_info = &bp->link_info;
1764 goto async_event_process_exit;
1766 /* print unsupported speed warning in forced speed mode only */
1767 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
1768 (data1 & 0x20000)) {
1769 u16 fw_speed = link_info->force_link_speed;
1770 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1772 if (speed != SPEED_UNKNOWN)
1773 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1776 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1779 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1780 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1782 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1783 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1785 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1786 u32 data1 = le32_to_cpu(cmpl->event_data1);
1787 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1792 if (bp->pf.port_id != port_id)
1795 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1798 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1800 goto async_event_process_exit;
1801 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1804 goto async_event_process_exit;
1806 bnxt_queue_sp_work(bp);
1807 async_event_process_exit:
1808 bnxt_ulp_async_events(bp, cmpl);
1812 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1814 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1815 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1816 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1817 (struct hwrm_fwd_req_cmpl *)txcmp;
1819 switch (cmpl_type) {
1820 case CMPL_BASE_TYPE_HWRM_DONE:
1821 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1822 if (seq_id == bp->hwrm_intr_seq_id)
1823 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
1825 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1828 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1829 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1831 if ((vf_id < bp->pf.first_vf_id) ||
1832 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1833 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1838 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1839 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1840 bnxt_queue_sp_work(bp);
1843 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1844 bnxt_async_event_process(bp,
1845 (struct hwrm_async_event_cmpl *)txcmp);
1854 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1856 struct bnxt_napi *bnapi = dev_instance;
1857 struct bnxt *bp = bnapi->bp;
1858 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1859 u32 cons = RING_CMP(cpr->cp_raw_cons);
1862 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1863 napi_schedule(&bnapi->napi);
1867 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1869 u32 raw_cons = cpr->cp_raw_cons;
1870 u16 cons = RING_CMP(raw_cons);
1871 struct tx_cmp *txcmp;
1873 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1875 return TX_CMP_VALID(txcmp, raw_cons);
1878 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1880 struct bnxt_napi *bnapi = dev_instance;
1881 struct bnxt *bp = bnapi->bp;
1882 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1883 u32 cons = RING_CMP(cpr->cp_raw_cons);
1886 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1888 if (!bnxt_has_work(bp, cpr)) {
1889 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1890 /* return if erroneous interrupt */
1891 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1895 /* disable ring IRQ */
1896 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
1898 /* Return here if interrupt is shared and is disabled. */
1899 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1902 napi_schedule(&bnapi->napi);
1906 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1909 struct bnxt_napi *bnapi = cpr->bnapi;
1910 u32 raw_cons = cpr->cp_raw_cons;
1915 struct tx_cmp *txcmp;
1917 cpr->has_more_work = 0;
1921 cons = RING_CMP(raw_cons);
1922 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1924 if (!TX_CMP_VALID(txcmp, raw_cons))
1927 /* The valid test of the entry must be done first before
1928 * reading any further.
1931 cpr->had_work_done = 1;
1932 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1934 /* return full budget so NAPI will complete. */
1935 if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
1937 raw_cons = NEXT_RAW_CMP(raw_cons);
1939 cpr->has_more_work = 1;
1942 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1944 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
1946 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
1948 if (likely(rc >= 0))
1950 /* Increment rx_pkts when rc is -ENOMEM to count towards
1951 * the NAPI budget. Otherwise, we may potentially loop
1952 * here forever if we consistently cannot allocate
1955 else if (rc == -ENOMEM && budget)
1957 else if (rc == -EBUSY) /* partial completion */
1959 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1960 CMPL_BASE_TYPE_HWRM_DONE) ||
1961 (TX_CMP_TYPE(txcmp) ==
1962 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1963 (TX_CMP_TYPE(txcmp) ==
1964 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1965 bnxt_hwrm_handler(bp, txcmp);
1967 raw_cons = NEXT_RAW_CMP(raw_cons);
1969 if (rx_pkts && rx_pkts == budget) {
1970 cpr->has_more_work = 1;
1975 if (event & BNXT_TX_EVENT) {
1976 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1977 u16 prod = txr->tx_prod;
1979 /* Sync BD data before updating doorbell */
1982 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
1985 cpr->cp_raw_cons = raw_cons;
1986 bnapi->tx_pkts += tx_pkts;
1987 bnapi->events |= event;
1991 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
1993 if (bnapi->tx_pkts) {
1994 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
1998 if (bnapi->events & BNXT_RX_EVENT) {
1999 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2001 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2002 if (bnapi->events & BNXT_AGG_EVENT)
2003 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2008 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2011 struct bnxt_napi *bnapi = cpr->bnapi;
2014 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2016 /* ACK completion ring before freeing tx ring and producing new
2017 * buffers in rx/agg rings to prevent overflowing the completion
2020 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2022 __bnxt_poll_work_done(bp, bnapi);
2026 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2028 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2029 struct bnxt *bp = bnapi->bp;
2030 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2031 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2032 struct tx_cmp *txcmp;
2033 struct rx_cmp_ext *rxcmp1;
2034 u32 cp_cons, tmp_raw_cons;
2035 u32 raw_cons = cpr->cp_raw_cons;
2042 cp_cons = RING_CMP(raw_cons);
2043 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2045 if (!TX_CMP_VALID(txcmp, raw_cons))
2048 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2049 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2050 cp_cons = RING_CMP(tmp_raw_cons);
2051 rxcmp1 = (struct rx_cmp_ext *)
2052 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2054 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2057 /* force an error to recycle the buffer */
2058 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2059 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2061 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2062 if (likely(rc == -EIO) && budget)
2064 else if (rc == -EBUSY) /* partial completion */
2066 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2067 CMPL_BASE_TYPE_HWRM_DONE)) {
2068 bnxt_hwrm_handler(bp, txcmp);
2071 "Invalid completion received on special ring\n");
2073 raw_cons = NEXT_RAW_CMP(raw_cons);
2075 if (rx_pkts == budget)
2079 cpr->cp_raw_cons = raw_cons;
2080 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2081 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2083 if (event & BNXT_AGG_EVENT)
2084 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2086 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2087 napi_complete_done(napi, rx_pkts);
2088 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2093 static int bnxt_poll(struct napi_struct *napi, int budget)
2095 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2096 struct bnxt *bp = bnapi->bp;
2097 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2101 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2103 if (work_done >= budget) {
2105 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2109 if (!bnxt_has_work(bp, cpr)) {
2110 if (napi_complete_done(napi, work_done))
2111 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2115 if (bp->flags & BNXT_FLAG_DIM) {
2116 struct net_dim_sample dim_sample;
2118 net_dim_sample(cpr->event_ctr,
2122 net_dim(&cpr->dim, dim_sample);
2127 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2129 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2130 int i, work_done = 0;
2132 for (i = 0; i < 2; i++) {
2133 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2136 work_done += __bnxt_poll_work(bp, cpr2,
2137 budget - work_done);
2138 cpr->has_more_work |= cpr2->has_more_work;
2144 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2145 u64 dbr_type, bool all)
2147 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2150 for (i = 0; i < 2; i++) {
2151 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2152 struct bnxt_db_info *db;
2154 if (cpr2 && (all || cpr2->had_work_done)) {
2156 writeq(db->db_key64 | dbr_type |
2157 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2158 cpr2->had_work_done = 0;
2161 __bnxt_poll_work_done(bp, bnapi);
2164 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2166 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2167 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2168 u32 raw_cons = cpr->cp_raw_cons;
2169 struct bnxt *bp = bnapi->bp;
2170 struct nqe_cn *nqcmp;
2174 if (cpr->has_more_work) {
2175 cpr->has_more_work = 0;
2176 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2177 if (cpr->has_more_work) {
2178 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, false);
2181 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, true);
2182 if (napi_complete_done(napi, work_done))
2183 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, cpr->cp_raw_cons);
2187 cons = RING_CMP(raw_cons);
2188 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2190 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2191 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
2193 cpr->cp_raw_cons = raw_cons;
2194 if (napi_complete_done(napi, work_done))
2195 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2200 /* The valid test of the entry must be done first before
2201 * reading any further.
2205 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2206 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2207 struct bnxt_cp_ring_info *cpr2;
2209 cpr2 = cpr->cp_ring_arr[idx];
2210 work_done += __bnxt_poll_work(bp, cpr2,
2211 budget - work_done);
2212 cpr->has_more_work = cpr2->has_more_work;
2214 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2216 raw_cons = NEXT_RAW_CMP(raw_cons);
2217 if (cpr->has_more_work)
2220 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, true);
2221 cpr->cp_raw_cons = raw_cons;
2225 static void bnxt_free_tx_skbs(struct bnxt *bp)
2228 struct pci_dev *pdev = bp->pdev;
2233 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2234 for (i = 0; i < bp->tx_nr_rings; i++) {
2235 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2238 for (j = 0; j < max_idx;) {
2239 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2240 struct sk_buff *skb = tx_buf->skb;
2250 if (tx_buf->is_push) {
2256 dma_unmap_single(&pdev->dev,
2257 dma_unmap_addr(tx_buf, mapping),
2261 last = tx_buf->nr_frags;
2263 for (k = 0; k < last; k++, j++) {
2264 int ring_idx = j & bp->tx_ring_mask;
2265 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2267 tx_buf = &txr->tx_buf_ring[ring_idx];
2270 dma_unmap_addr(tx_buf, mapping),
2271 skb_frag_size(frag), PCI_DMA_TODEVICE);
2275 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2279 static void bnxt_free_rx_skbs(struct bnxt *bp)
2281 int i, max_idx, max_agg_idx;
2282 struct pci_dev *pdev = bp->pdev;
2287 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2288 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2289 for (i = 0; i < bp->rx_nr_rings; i++) {
2290 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2294 for (j = 0; j < MAX_TPA; j++) {
2295 struct bnxt_tpa_info *tpa_info =
2297 u8 *data = tpa_info->data;
2302 dma_unmap_single_attrs(&pdev->dev,
2304 bp->rx_buf_use_size,
2306 DMA_ATTR_WEAK_ORDERING);
2308 tpa_info->data = NULL;
2314 for (j = 0; j < max_idx; j++) {
2315 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
2316 dma_addr_t mapping = rx_buf->mapping;
2317 void *data = rx_buf->data;
2322 rx_buf->data = NULL;
2324 if (BNXT_RX_PAGE_MODE(bp)) {
2325 mapping -= bp->rx_dma_offset;
2326 dma_unmap_page_attrs(&pdev->dev, mapping,
2327 PAGE_SIZE, bp->rx_dir,
2328 DMA_ATTR_WEAK_ORDERING);
2331 dma_unmap_single_attrs(&pdev->dev, mapping,
2332 bp->rx_buf_use_size,
2334 DMA_ATTR_WEAK_ORDERING);
2339 for (j = 0; j < max_agg_idx; j++) {
2340 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2341 &rxr->rx_agg_ring[j];
2342 struct page *page = rx_agg_buf->page;
2347 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2350 DMA_ATTR_WEAK_ORDERING);
2352 rx_agg_buf->page = NULL;
2353 __clear_bit(j, rxr->rx_agg_bmap);
2358 __free_page(rxr->rx_page);
2359 rxr->rx_page = NULL;
2364 static void bnxt_free_skbs(struct bnxt *bp)
2366 bnxt_free_tx_skbs(bp);
2367 bnxt_free_rx_skbs(bp);
2370 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2372 struct pci_dev *pdev = bp->pdev;
2375 for (i = 0; i < rmem->nr_pages; i++) {
2376 if (!rmem->pg_arr[i])
2379 dma_free_coherent(&pdev->dev, rmem->page_size,
2380 rmem->pg_arr[i], rmem->dma_arr[i]);
2382 rmem->pg_arr[i] = NULL;
2385 size_t pg_tbl_size = rmem->nr_pages * 8;
2387 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2388 pg_tbl_size = rmem->page_size;
2389 dma_free_coherent(&pdev->dev, pg_tbl_size,
2390 rmem->pg_tbl, rmem->pg_tbl_map);
2391 rmem->pg_tbl = NULL;
2393 if (rmem->vmem_size && *rmem->vmem) {
2399 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2401 struct pci_dev *pdev = bp->pdev;
2405 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2406 valid_bit = PTU_PTE_VALID;
2407 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2408 size_t pg_tbl_size = rmem->nr_pages * 8;
2410 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2411 pg_tbl_size = rmem->page_size;
2412 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2419 for (i = 0; i < rmem->nr_pages; i++) {
2420 u64 extra_bits = valid_bit;
2422 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2426 if (!rmem->pg_arr[i])
2429 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2430 if (i == rmem->nr_pages - 2 &&
2431 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2432 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2433 else if (i == rmem->nr_pages - 1 &&
2434 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2435 extra_bits |= PTU_PTE_LAST;
2437 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2441 if (rmem->vmem_size) {
2442 *rmem->vmem = vzalloc(rmem->vmem_size);
2449 static void bnxt_free_rx_rings(struct bnxt *bp)
2456 for (i = 0; i < bp->rx_nr_rings; i++) {
2457 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2458 struct bnxt_ring_struct *ring;
2461 bpf_prog_put(rxr->xdp_prog);
2463 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2464 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2469 kfree(rxr->rx_agg_bmap);
2470 rxr->rx_agg_bmap = NULL;
2472 ring = &rxr->rx_ring_struct;
2473 bnxt_free_ring(bp, &ring->ring_mem);
2475 ring = &rxr->rx_agg_ring_struct;
2476 bnxt_free_ring(bp, &ring->ring_mem);
2480 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2482 int i, rc, agg_rings = 0, tpa_rings = 0;
2487 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2490 if (bp->flags & BNXT_FLAG_TPA)
2493 for (i = 0; i < bp->rx_nr_rings; i++) {
2494 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2495 struct bnxt_ring_struct *ring;
2497 ring = &rxr->rx_ring_struct;
2499 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2503 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2511 ring = &rxr->rx_agg_ring_struct;
2512 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2517 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2518 mem_size = rxr->rx_agg_bmap_size / 8;
2519 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2520 if (!rxr->rx_agg_bmap)
2524 rxr->rx_tpa = kcalloc(MAX_TPA,
2525 sizeof(struct bnxt_tpa_info),
2535 static void bnxt_free_tx_rings(struct bnxt *bp)
2538 struct pci_dev *pdev = bp->pdev;
2543 for (i = 0; i < bp->tx_nr_rings; i++) {
2544 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2545 struct bnxt_ring_struct *ring;
2548 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2549 txr->tx_push, txr->tx_push_mapping);
2550 txr->tx_push = NULL;
2553 ring = &txr->tx_ring_struct;
2555 bnxt_free_ring(bp, &ring->ring_mem);
2559 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2562 struct pci_dev *pdev = bp->pdev;
2564 bp->tx_push_size = 0;
2565 if (bp->tx_push_thresh) {
2568 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2569 bp->tx_push_thresh);
2571 if (push_size > 256) {
2573 bp->tx_push_thresh = 0;
2576 bp->tx_push_size = push_size;
2579 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2580 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2581 struct bnxt_ring_struct *ring;
2584 ring = &txr->tx_ring_struct;
2586 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2590 ring->grp_idx = txr->bnapi->index;
2591 if (bp->tx_push_size) {
2594 /* One pre-allocated DMA buffer to backup
2597 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2599 &txr->tx_push_mapping,
2605 mapping = txr->tx_push_mapping +
2606 sizeof(struct tx_push_bd);
2607 txr->data_mapping = cpu_to_le64(mapping);
2609 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2611 qidx = bp->tc_to_qidx[j];
2612 ring->queue_id = bp->q_info[qidx].queue_id;
2613 if (i < bp->tx_nr_rings_xdp)
2615 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2621 static void bnxt_free_cp_rings(struct bnxt *bp)
2628 for (i = 0; i < bp->cp_nr_rings; i++) {
2629 struct bnxt_napi *bnapi = bp->bnapi[i];
2630 struct bnxt_cp_ring_info *cpr;
2631 struct bnxt_ring_struct *ring;
2637 cpr = &bnapi->cp_ring;
2638 ring = &cpr->cp_ring_struct;
2640 bnxt_free_ring(bp, &ring->ring_mem);
2642 for (j = 0; j < 2; j++) {
2643 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2646 ring = &cpr2->cp_ring_struct;
2647 bnxt_free_ring(bp, &ring->ring_mem);
2649 cpr->cp_ring_arr[j] = NULL;
2655 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
2657 struct bnxt_ring_mem_info *rmem;
2658 struct bnxt_ring_struct *ring;
2659 struct bnxt_cp_ring_info *cpr;
2662 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
2666 ring = &cpr->cp_ring_struct;
2667 rmem = &ring->ring_mem;
2668 rmem->nr_pages = bp->cp_nr_pages;
2669 rmem->page_size = HW_CMPD_RING_SIZE;
2670 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2671 rmem->dma_arr = cpr->cp_desc_mapping;
2672 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
2673 rc = bnxt_alloc_ring(bp, rmem);
2675 bnxt_free_ring(bp, rmem);
2682 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2684 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
2685 int i, rc, ulp_base_vec, ulp_msix;
2687 ulp_msix = bnxt_get_ulp_msix_num(bp);
2688 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
2689 for (i = 0; i < bp->cp_nr_rings; i++) {
2690 struct bnxt_napi *bnapi = bp->bnapi[i];
2691 struct bnxt_cp_ring_info *cpr;
2692 struct bnxt_ring_struct *ring;
2697 cpr = &bnapi->cp_ring;
2699 ring = &cpr->cp_ring_struct;
2701 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
2705 if (ulp_msix && i >= ulp_base_vec)
2706 ring->map_idx = i + ulp_msix;
2710 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2713 if (i < bp->rx_nr_rings) {
2714 struct bnxt_cp_ring_info *cpr2 =
2715 bnxt_alloc_cp_sub_ring(bp);
2717 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
2720 cpr2->bnapi = bnapi;
2722 if ((sh && i < bp->tx_nr_rings) ||
2723 (!sh && i >= bp->rx_nr_rings)) {
2724 struct bnxt_cp_ring_info *cpr2 =
2725 bnxt_alloc_cp_sub_ring(bp);
2727 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
2730 cpr2->bnapi = bnapi;
2736 static void bnxt_init_ring_struct(struct bnxt *bp)
2740 for (i = 0; i < bp->cp_nr_rings; i++) {
2741 struct bnxt_napi *bnapi = bp->bnapi[i];
2742 struct bnxt_ring_mem_info *rmem;
2743 struct bnxt_cp_ring_info *cpr;
2744 struct bnxt_rx_ring_info *rxr;
2745 struct bnxt_tx_ring_info *txr;
2746 struct bnxt_ring_struct *ring;
2751 cpr = &bnapi->cp_ring;
2752 ring = &cpr->cp_ring_struct;
2753 rmem = &ring->ring_mem;
2754 rmem->nr_pages = bp->cp_nr_pages;
2755 rmem->page_size = HW_CMPD_RING_SIZE;
2756 rmem->pg_arr = (void **)cpr->cp_desc_ring;
2757 rmem->dma_arr = cpr->cp_desc_mapping;
2758 rmem->vmem_size = 0;
2760 rxr = bnapi->rx_ring;
2764 ring = &rxr->rx_ring_struct;
2765 rmem = &ring->ring_mem;
2766 rmem->nr_pages = bp->rx_nr_pages;
2767 rmem->page_size = HW_RXBD_RING_SIZE;
2768 rmem->pg_arr = (void **)rxr->rx_desc_ring;
2769 rmem->dma_arr = rxr->rx_desc_mapping;
2770 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2771 rmem->vmem = (void **)&rxr->rx_buf_ring;
2773 ring = &rxr->rx_agg_ring_struct;
2774 rmem = &ring->ring_mem;
2775 rmem->nr_pages = bp->rx_agg_nr_pages;
2776 rmem->page_size = HW_RXBD_RING_SIZE;
2777 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
2778 rmem->dma_arr = rxr->rx_agg_desc_mapping;
2779 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2780 rmem->vmem = (void **)&rxr->rx_agg_ring;
2783 txr = bnapi->tx_ring;
2787 ring = &txr->tx_ring_struct;
2788 rmem = &ring->ring_mem;
2789 rmem->nr_pages = bp->tx_nr_pages;
2790 rmem->page_size = HW_RXBD_RING_SIZE;
2791 rmem->pg_arr = (void **)txr->tx_desc_ring;
2792 rmem->dma_arr = txr->tx_desc_mapping;
2793 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2794 rmem->vmem = (void **)&txr->tx_buf_ring;
2798 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2802 struct rx_bd **rx_buf_ring;
2804 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
2805 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
2809 rxbd = rx_buf_ring[i];
2813 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2814 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2815 rxbd->rx_bd_opaque = prod;
2820 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2822 struct net_device *dev = bp->dev;
2823 struct bnxt_rx_ring_info *rxr;
2824 struct bnxt_ring_struct *ring;
2828 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2829 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2831 if (NET_IP_ALIGN == 2)
2832 type |= RX_BD_FLAGS_SOP;
2834 rxr = &bp->rx_ring[ring_nr];
2835 ring = &rxr->rx_ring_struct;
2836 bnxt_init_rxbd_pages(ring, type);
2838 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2839 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2840 if (IS_ERR(rxr->xdp_prog)) {
2841 int rc = PTR_ERR(rxr->xdp_prog);
2843 rxr->xdp_prog = NULL;
2847 prod = rxr->rx_prod;
2848 for (i = 0; i < bp->rx_ring_size; i++) {
2849 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2850 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2851 ring_nr, i, bp->rx_ring_size);
2854 prod = NEXT_RX(prod);
2856 rxr->rx_prod = prod;
2857 ring->fw_ring_id = INVALID_HW_RING_ID;
2859 ring = &rxr->rx_agg_ring_struct;
2860 ring->fw_ring_id = INVALID_HW_RING_ID;
2862 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2865 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2866 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2868 bnxt_init_rxbd_pages(ring, type);
2870 prod = rxr->rx_agg_prod;
2871 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2872 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2873 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2874 ring_nr, i, bp->rx_ring_size);
2877 prod = NEXT_RX_AGG(prod);
2879 rxr->rx_agg_prod = prod;
2881 if (bp->flags & BNXT_FLAG_TPA) {
2886 for (i = 0; i < MAX_TPA; i++) {
2887 data = __bnxt_alloc_rx_data(bp, &mapping,
2892 rxr->rx_tpa[i].data = data;
2893 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
2894 rxr->rx_tpa[i].mapping = mapping;
2897 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2905 static void bnxt_init_cp_rings(struct bnxt *bp)
2909 for (i = 0; i < bp->cp_nr_rings; i++) {
2910 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2911 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2913 ring->fw_ring_id = INVALID_HW_RING_ID;
2914 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2915 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2916 for (j = 0; j < 2; j++) {
2917 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
2922 ring = &cpr2->cp_ring_struct;
2923 ring->fw_ring_id = INVALID_HW_RING_ID;
2924 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
2925 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
2930 static int bnxt_init_rx_rings(struct bnxt *bp)
2934 if (BNXT_RX_PAGE_MODE(bp)) {
2935 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2936 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
2938 bp->rx_offset = BNXT_RX_OFFSET;
2939 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2942 for (i = 0; i < bp->rx_nr_rings; i++) {
2943 rc = bnxt_init_one_rx_ring(bp, i);
2951 static int bnxt_init_tx_rings(struct bnxt *bp)
2955 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2958 for (i = 0; i < bp->tx_nr_rings; i++) {
2959 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2960 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2962 ring->fw_ring_id = INVALID_HW_RING_ID;
2968 static void bnxt_free_ring_grps(struct bnxt *bp)
2970 kfree(bp->grp_info);
2971 bp->grp_info = NULL;
2974 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2979 bp->grp_info = kcalloc(bp->cp_nr_rings,
2980 sizeof(struct bnxt_ring_grp_info),
2985 for (i = 0; i < bp->cp_nr_rings; i++) {
2987 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2988 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2989 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2990 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2991 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2996 static void bnxt_free_vnics(struct bnxt *bp)
2998 kfree(bp->vnic_info);
2999 bp->vnic_info = NULL;
3003 static int bnxt_alloc_vnics(struct bnxt *bp)
3007 #ifdef CONFIG_RFS_ACCEL
3008 if (bp->flags & BNXT_FLAG_RFS)
3009 num_vnics += bp->rx_nr_rings;
3012 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3015 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3020 bp->nr_vnics = num_vnics;
3024 static void bnxt_init_vnics(struct bnxt *bp)
3028 for (i = 0; i < bp->nr_vnics; i++) {
3029 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3032 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3033 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3034 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3036 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3038 if (bp->vnic_info[i].rss_hash_key) {
3040 prandom_bytes(vnic->rss_hash_key,
3043 memcpy(vnic->rss_hash_key,
3044 bp->vnic_info[0].rss_hash_key,
3050 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3054 pages = ring_size / desc_per_pg;
3061 while (pages & (pages - 1))
3067 void bnxt_set_tpa_flags(struct bnxt *bp)
3069 bp->flags &= ~BNXT_FLAG_TPA;
3070 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3072 if (bp->dev->features & NETIF_F_LRO)
3073 bp->flags |= BNXT_FLAG_LRO;
3074 else if (bp->dev->features & NETIF_F_GRO_HW)
3075 bp->flags |= BNXT_FLAG_GRO;
3078 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3081 void bnxt_set_ring_params(struct bnxt *bp)
3083 u32 ring_size, rx_size, rx_space;
3084 u32 agg_factor = 0, agg_ring_size = 0;
3086 /* 8 for CRC and VLAN */
3087 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3089 rx_space = rx_size + NET_SKB_PAD +
3090 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3092 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3093 ring_size = bp->rx_ring_size;
3094 bp->rx_agg_ring_size = 0;
3095 bp->rx_agg_nr_pages = 0;
3097 if (bp->flags & BNXT_FLAG_TPA)
3098 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3100 bp->flags &= ~BNXT_FLAG_JUMBO;
3101 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3104 bp->flags |= BNXT_FLAG_JUMBO;
3105 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3106 if (jumbo_factor > agg_factor)
3107 agg_factor = jumbo_factor;
3109 agg_ring_size = ring_size * agg_factor;
3111 if (agg_ring_size) {
3112 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3114 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3115 u32 tmp = agg_ring_size;
3117 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3118 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3119 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3120 tmp, agg_ring_size);
3122 bp->rx_agg_ring_size = agg_ring_size;
3123 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3124 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3125 rx_space = rx_size + NET_SKB_PAD +
3126 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3129 bp->rx_buf_use_size = rx_size;
3130 bp->rx_buf_size = rx_space;
3132 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3133 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3135 ring_size = bp->tx_ring_size;
3136 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3137 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3139 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
3140 bp->cp_ring_size = ring_size;
3142 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3143 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3144 bp->cp_nr_pages = MAX_CP_PAGES;
3145 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3146 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3147 ring_size, bp->cp_ring_size);
3149 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3150 bp->cp_ring_mask = bp->cp_bit - 1;
3153 /* Changing allocation mode of RX rings.
3154 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3156 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3159 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3162 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3163 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3164 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3165 bp->rx_dir = DMA_BIDIRECTIONAL;
3166 bp->rx_skb_func = bnxt_rx_page_skb;
3167 /* Disable LRO or GRO_HW */
3168 netdev_update_features(bp->dev);
3170 bp->dev->max_mtu = bp->max_mtu;
3171 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3172 bp->rx_dir = DMA_FROM_DEVICE;
3173 bp->rx_skb_func = bnxt_rx_skb;
3178 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3181 struct bnxt_vnic_info *vnic;
3182 struct pci_dev *pdev = bp->pdev;
3187 for (i = 0; i < bp->nr_vnics; i++) {
3188 vnic = &bp->vnic_info[i];
3190 kfree(vnic->fw_grp_ids);
3191 vnic->fw_grp_ids = NULL;
3193 kfree(vnic->uc_list);
3194 vnic->uc_list = NULL;
3196 if (vnic->mc_list) {
3197 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3198 vnic->mc_list, vnic->mc_list_mapping);
3199 vnic->mc_list = NULL;
3202 if (vnic->rss_table) {
3203 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3205 vnic->rss_table_dma_addr);
3206 vnic->rss_table = NULL;
3209 vnic->rss_hash_key = NULL;
3214 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3216 int i, rc = 0, size;
3217 struct bnxt_vnic_info *vnic;
3218 struct pci_dev *pdev = bp->pdev;
3221 for (i = 0; i < bp->nr_vnics; i++) {
3222 vnic = &bp->vnic_info[i];
3224 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3225 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3228 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3229 if (!vnic->uc_list) {
3236 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3237 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3239 dma_alloc_coherent(&pdev->dev,
3241 &vnic->mc_list_mapping,
3243 if (!vnic->mc_list) {
3249 if (bp->flags & BNXT_FLAG_CHIP_P5)
3250 goto vnic_skip_grps;
3252 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3253 max_rings = bp->rx_nr_rings;
3257 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3258 if (!vnic->fw_grp_ids) {
3263 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3264 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3267 /* Allocate rss table and hash key */
3268 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3269 &vnic->rss_table_dma_addr,
3271 if (!vnic->rss_table) {
3276 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3278 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3279 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3287 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3289 struct pci_dev *pdev = bp->pdev;
3291 if (bp->hwrm_cmd_resp_addr) {
3292 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3293 bp->hwrm_cmd_resp_dma_addr);
3294 bp->hwrm_cmd_resp_addr = NULL;
3297 if (bp->hwrm_cmd_kong_resp_addr) {
3298 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3299 bp->hwrm_cmd_kong_resp_addr,
3300 bp->hwrm_cmd_kong_resp_dma_addr);
3301 bp->hwrm_cmd_kong_resp_addr = NULL;
3305 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3307 struct pci_dev *pdev = bp->pdev;
3309 bp->hwrm_cmd_kong_resp_addr =
3310 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3311 &bp->hwrm_cmd_kong_resp_dma_addr,
3313 if (!bp->hwrm_cmd_kong_resp_addr)
3319 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3321 struct pci_dev *pdev = bp->pdev;
3323 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3324 &bp->hwrm_cmd_resp_dma_addr,
3326 if (!bp->hwrm_cmd_resp_addr)
3332 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3334 if (bp->hwrm_short_cmd_req_addr) {
3335 struct pci_dev *pdev = bp->pdev;
3337 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3338 bp->hwrm_short_cmd_req_addr,
3339 bp->hwrm_short_cmd_req_dma_addr);
3340 bp->hwrm_short_cmd_req_addr = NULL;
3344 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3346 struct pci_dev *pdev = bp->pdev;
3348 bp->hwrm_short_cmd_req_addr =
3349 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3350 &bp->hwrm_short_cmd_req_dma_addr,
3352 if (!bp->hwrm_short_cmd_req_addr)
3358 static void bnxt_free_port_stats(struct bnxt *bp)
3360 struct pci_dev *pdev = bp->pdev;
3362 bp->flags &= ~BNXT_FLAG_PORT_STATS;
3363 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
3365 if (bp->hw_rx_port_stats) {
3366 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
3367 bp->hw_rx_port_stats,
3368 bp->hw_rx_port_stats_map);
3369 bp->hw_rx_port_stats = NULL;
3372 if (bp->hw_tx_port_stats_ext) {
3373 dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
3374 bp->hw_tx_port_stats_ext,
3375 bp->hw_tx_port_stats_ext_map);
3376 bp->hw_tx_port_stats_ext = NULL;
3379 if (bp->hw_rx_port_stats_ext) {
3380 dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
3381 bp->hw_rx_port_stats_ext,
3382 bp->hw_rx_port_stats_ext_map);
3383 bp->hw_rx_port_stats_ext = NULL;
3387 static void bnxt_free_ring_stats(struct bnxt *bp)
3389 struct pci_dev *pdev = bp->pdev;
3395 size = sizeof(struct ctx_hw_stats);
3397 for (i = 0; i < bp->cp_nr_rings; i++) {
3398 struct bnxt_napi *bnapi = bp->bnapi[i];
3399 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3401 if (cpr->hw_stats) {
3402 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
3404 cpr->hw_stats = NULL;
3409 static int bnxt_alloc_stats(struct bnxt *bp)
3412 struct pci_dev *pdev = bp->pdev;
3414 size = sizeof(struct ctx_hw_stats);
3416 for (i = 0; i < bp->cp_nr_rings; i++) {
3417 struct bnxt_napi *bnapi = bp->bnapi[i];
3418 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3420 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
3426 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3429 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
3430 if (bp->hw_rx_port_stats)
3431 goto alloc_ext_stats;
3433 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
3434 sizeof(struct tx_port_stats) + 1024;
3436 bp->hw_rx_port_stats =
3437 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
3438 &bp->hw_rx_port_stats_map,
3440 if (!bp->hw_rx_port_stats)
3443 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
3445 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
3446 sizeof(struct rx_port_stats) + 512;
3447 bp->flags |= BNXT_FLAG_PORT_STATS;
3450 /* Display extended statistics only if FW supports it */
3451 if (bp->hwrm_spec_code < 0x10804 ||
3452 bp->hwrm_spec_code == 0x10900)
3455 if (bp->hw_rx_port_stats_ext)
3456 goto alloc_tx_ext_stats;
3458 bp->hw_rx_port_stats_ext =
3459 dma_alloc_coherent(&pdev->dev,
3460 sizeof(struct rx_port_stats_ext),
3461 &bp->hw_rx_port_stats_ext_map,
3463 if (!bp->hw_rx_port_stats_ext)
3467 if (bp->hw_tx_port_stats_ext)
3470 if (bp->hwrm_spec_code >= 0x10902) {
3471 bp->hw_tx_port_stats_ext =
3472 dma_alloc_coherent(&pdev->dev,
3473 sizeof(struct tx_port_stats_ext),
3474 &bp->hw_tx_port_stats_ext_map,
3477 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
3482 static void bnxt_clear_ring_indices(struct bnxt *bp)
3489 for (i = 0; i < bp->cp_nr_rings; i++) {
3490 struct bnxt_napi *bnapi = bp->bnapi[i];
3491 struct bnxt_cp_ring_info *cpr;
3492 struct bnxt_rx_ring_info *rxr;
3493 struct bnxt_tx_ring_info *txr;
3498 cpr = &bnapi->cp_ring;
3499 cpr->cp_raw_cons = 0;
3501 txr = bnapi->tx_ring;
3507 rxr = bnapi->rx_ring;
3510 rxr->rx_agg_prod = 0;
3511 rxr->rx_sw_agg_prod = 0;
3512 rxr->rx_next_cons = 0;
3517 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3519 #ifdef CONFIG_RFS_ACCEL
3522 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3523 * safe to delete the hash table.
3525 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3526 struct hlist_head *head;
3527 struct hlist_node *tmp;
3528 struct bnxt_ntuple_filter *fltr;
3530 head = &bp->ntp_fltr_hash_tbl[i];
3531 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3532 hlist_del(&fltr->hash);
3537 kfree(bp->ntp_fltr_bmap);
3538 bp->ntp_fltr_bmap = NULL;
3540 bp->ntp_fltr_count = 0;
3544 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3546 #ifdef CONFIG_RFS_ACCEL
3549 if (!(bp->flags & BNXT_FLAG_RFS))
3552 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3553 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3555 bp->ntp_fltr_count = 0;
3556 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3560 if (!bp->ntp_fltr_bmap)
3569 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3571 bnxt_free_vnic_attributes(bp);
3572 bnxt_free_tx_rings(bp);
3573 bnxt_free_rx_rings(bp);
3574 bnxt_free_cp_rings(bp);
3575 bnxt_free_ntp_fltrs(bp, irq_re_init);
3577 bnxt_free_ring_stats(bp);
3578 bnxt_free_ring_grps(bp);
3579 bnxt_free_vnics(bp);
3580 kfree(bp->tx_ring_map);
3581 bp->tx_ring_map = NULL;
3589 bnxt_clear_ring_indices(bp);
3593 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3595 int i, j, rc, size, arr_size;
3599 /* Allocate bnapi mem pointer array and mem block for
3602 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3604 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3605 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3611 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3612 bp->bnapi[i] = bnapi;
3613 bp->bnapi[i]->index = i;
3614 bp->bnapi[i]->bp = bp;
3615 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3616 struct bnxt_cp_ring_info *cpr =
3617 &bp->bnapi[i]->cp_ring;
3619 cpr->cp_ring_struct.ring_mem.flags =
3620 BNXT_RMEM_RING_PTE_FLAG;
3624 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3625 sizeof(struct bnxt_rx_ring_info),
3630 for (i = 0; i < bp->rx_nr_rings; i++) {
3631 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3633 if (bp->flags & BNXT_FLAG_CHIP_P5) {
3634 rxr->rx_ring_struct.ring_mem.flags =
3635 BNXT_RMEM_RING_PTE_FLAG;
3636 rxr->rx_agg_ring_struct.ring_mem.flags =
3637 BNXT_RMEM_RING_PTE_FLAG;
3639 rxr->bnapi = bp->bnapi[i];
3640 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3643 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3644 sizeof(struct bnxt_tx_ring_info),
3649 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3652 if (!bp->tx_ring_map)
3655 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3658 j = bp->rx_nr_rings;
3660 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3661 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3663 if (bp->flags & BNXT_FLAG_CHIP_P5)
3664 txr->tx_ring_struct.ring_mem.flags =
3665 BNXT_RMEM_RING_PTE_FLAG;
3666 txr->bnapi = bp->bnapi[j];
3667 bp->bnapi[j]->tx_ring = txr;
3668 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
3669 if (i >= bp->tx_nr_rings_xdp) {
3670 txr->txq_index = i - bp->tx_nr_rings_xdp;
3671 bp->bnapi[j]->tx_int = bnxt_tx_int;
3673 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
3674 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3678 rc = bnxt_alloc_stats(bp);
3682 rc = bnxt_alloc_ntp_fltrs(bp);
3686 rc = bnxt_alloc_vnics(bp);
3691 bnxt_init_ring_struct(bp);
3693 rc = bnxt_alloc_rx_rings(bp);
3697 rc = bnxt_alloc_tx_rings(bp);
3701 rc = bnxt_alloc_cp_rings(bp);
3705 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3706 BNXT_VNIC_UCAST_FLAG;
3707 rc = bnxt_alloc_vnic_attributes(bp);
3713 bnxt_free_mem(bp, true);
3717 static void bnxt_disable_int(struct bnxt *bp)
3724 for (i = 0; i < bp->cp_nr_rings; i++) {
3725 struct bnxt_napi *bnapi = bp->bnapi[i];
3726 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3727 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3729 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3730 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3734 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
3736 struct bnxt_napi *bnapi = bp->bnapi[n];
3737 struct bnxt_cp_ring_info *cpr;
3739 cpr = &bnapi->cp_ring;
3740 return cpr->cp_ring_struct.map_idx;
3743 static void bnxt_disable_int_sync(struct bnxt *bp)
3747 atomic_inc(&bp->intr_sem);
3749 bnxt_disable_int(bp);
3750 for (i = 0; i < bp->cp_nr_rings; i++) {
3751 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
3753 synchronize_irq(bp->irq_tbl[map_idx].vector);
3757 static void bnxt_enable_int(struct bnxt *bp)
3761 atomic_set(&bp->intr_sem, 0);
3762 for (i = 0; i < bp->cp_nr_rings; i++) {
3763 struct bnxt_napi *bnapi = bp->bnapi[i];
3764 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3766 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
3770 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3771 u16 cmpl_ring, u16 target_id)
3773 struct input *req = request;
3775 req->req_type = cpu_to_le16(req_type);
3776 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3777 req->target_id = cpu_to_le16(target_id);
3778 if (bnxt_kong_hwrm_message(bp, req))
3779 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
3781 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3784 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3785 int timeout, bool silent)
3787 int i, intr_process, rc, tmo_count;
3788 struct input *req = msg;
3792 u16 cp_ring_id, len = 0;
3793 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3794 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
3795 struct hwrm_short_input short_input = {0};
3796 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
3797 u8 *resp_addr = (u8 *)bp->hwrm_cmd_resp_addr;
3798 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
3799 u16 dst = BNXT_HWRM_CHNL_CHIMP;
3801 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
3802 if (msg_len > bp->hwrm_max_ext_req_len ||
3803 !bp->hwrm_short_cmd_req_addr)
3807 if (bnxt_hwrm_kong_chnl(bp, req)) {
3808 dst = BNXT_HWRM_CHNL_KONG;
3809 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
3810 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
3811 resp = bp->hwrm_cmd_kong_resp_addr;
3812 resp_addr = (u8 *)bp->hwrm_cmd_kong_resp_addr;
3815 memset(resp, 0, PAGE_SIZE);
3816 cp_ring_id = le16_to_cpu(req->cmpl_ring);
3817 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3819 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
3820 /* currently supports only one outstanding message */
3822 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
3824 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
3825 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
3826 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3829 /* Set boundary for maximum extended request length for short
3830 * cmd format. If passed up from device use the max supported
3831 * internal req length.
3833 max_msg_len = bp->hwrm_max_ext_req_len;
3835 memcpy(short_cmd_req, req, msg_len);
3836 if (msg_len < max_msg_len)
3837 memset(short_cmd_req + msg_len, 0,
3838 max_msg_len - msg_len);
3840 short_input.req_type = req->req_type;
3841 short_input.signature =
3842 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3843 short_input.size = cpu_to_le16(msg_len);
3844 short_input.req_addr =
3845 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3847 data = (u32 *)&short_input;
3848 msg_len = sizeof(short_input);
3850 /* Sync memory write before updating doorbell */
3853 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3856 /* Write request msg to hwrm channel */
3857 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
3859 for (i = msg_len; i < max_req_len; i += 4)
3860 writel(0, bp->bar0 + bar_offset + i);
3862 /* Ring channel doorbell */
3863 writel(1, bp->bar0 + doorbell_offset);
3866 timeout = DFLT_HWRM_CMD_TIMEOUT;
3867 /* convert timeout to usec */
3871 /* Short timeout for the first few iterations:
3872 * number of loops = number of loops for short timeout +
3873 * number of loops for standard timeout.
3875 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
3876 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
3877 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
3878 resp_len = (__le32 *)(resp_addr + HWRM_RESP_LEN_OFFSET);
3881 u16 seq_id = bp->hwrm_intr_seq_id;
3883 /* Wait until hwrm response cmpl interrupt is processed */
3884 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
3886 /* on first few passes, just barely sleep */
3887 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3888 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3889 HWRM_SHORT_MAX_TIMEOUT);
3891 usleep_range(HWRM_MIN_TIMEOUT,
3895 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
3896 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
3897 le16_to_cpu(req->req_type));
3900 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3902 valid = resp_addr + len - 1;
3906 /* Check if response len is updated */
3907 for (i = 0; i < tmo_count; i++) {
3908 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3912 /* on first few passes, just barely sleep */
3913 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
3914 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
3915 HWRM_SHORT_MAX_TIMEOUT);
3917 usleep_range(HWRM_MIN_TIMEOUT,
3921 if (i >= tmo_count) {
3922 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
3923 HWRM_TOTAL_TIMEOUT(i),
3924 le16_to_cpu(req->req_type),
3925 le16_to_cpu(req->seq_id), len);
3929 /* Last byte of resp contains valid bit */
3930 valid = resp_addr + len - 1;
3931 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
3932 /* make sure we read from updated DMA memory */
3939 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
3940 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
3941 HWRM_TOTAL_TIMEOUT(i),
3942 le16_to_cpu(req->req_type),
3943 le16_to_cpu(req->seq_id), len, *valid);
3948 /* Zero valid bit for compatibility. Valid bit in an older spec
3949 * may become a new field in a newer spec. We must make sure that
3950 * a new field not implemented by old spec will read zero.
3953 rc = le16_to_cpu(resp->error_code);
3955 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3956 le16_to_cpu(resp->req_type),
3957 le16_to_cpu(resp->seq_id), rc);
3961 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3963 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
3966 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3969 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3972 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3976 mutex_lock(&bp->hwrm_cmd_lock);
3977 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3978 mutex_unlock(&bp->hwrm_cmd_lock);
3982 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3987 mutex_lock(&bp->hwrm_cmd_lock);
3988 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3989 mutex_unlock(&bp->hwrm_cmd_lock);
3993 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3996 struct hwrm_func_drv_rgtr_input req = {0};
3997 DECLARE_BITMAP(async_events_bmap, 256);
3998 u32 *events = (u32 *)async_events_bmap;
4001 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4004 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4006 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4007 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
4008 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4010 if (bmap && bmap_size) {
4011 for (i = 0; i < bmap_size; i++) {
4012 if (test_bit(i, bmap))
4013 __set_bit(i, async_events_bmap);
4017 for (i = 0; i < 8; i++)
4018 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4020 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4023 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
4025 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4026 struct hwrm_func_drv_rgtr_input req = {0};
4029 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4032 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4033 FUNC_DRV_RGTR_REQ_ENABLES_VER);
4035 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4036 req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
4037 req.ver_maj_8b = DRV_VER_MAJ;
4038 req.ver_min_8b = DRV_VER_MIN;
4039 req.ver_upd_8b = DRV_VER_UPD;
4040 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4041 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4042 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4048 memset(data, 0, sizeof(data));
4049 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4050 u16 cmd = bnxt_vf_req_snif[i];
4051 unsigned int bit, idx;
4055 data[idx] |= 1 << bit;
4058 for (i = 0; i < 8; i++)
4059 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4062 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4065 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4066 req.flags |= cpu_to_le32(
4067 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4069 mutex_lock(&bp->hwrm_cmd_lock);
4070 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4073 else if (resp->flags &
4074 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4075 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4076 mutex_unlock(&bp->hwrm_cmd_lock);
4080 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4082 struct hwrm_func_drv_unrgtr_input req = {0};
4084 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4085 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4088 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4091 struct hwrm_tunnel_dst_port_free_input req = {0};
4093 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4094 req.tunnel_type = tunnel_type;
4096 switch (tunnel_type) {
4097 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4098 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
4100 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4101 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
4107 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4109 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4114 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4118 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4119 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4121 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4123 req.tunnel_type = tunnel_type;
4124 req.tunnel_dst_port_val = port;
4126 mutex_lock(&bp->hwrm_cmd_lock);
4127 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4129 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4134 switch (tunnel_type) {
4135 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4136 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
4138 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4139 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
4146 mutex_unlock(&bp->hwrm_cmd_lock);
4150 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4152 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4153 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4155 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4156 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4158 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4159 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4160 req.mask = cpu_to_le32(vnic->rx_mask);
4161 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4164 #ifdef CONFIG_RFS_ACCEL
4165 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4166 struct bnxt_ntuple_filter *fltr)
4168 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4170 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4171 req.ntuple_filter_id = fltr->filter_id;
4172 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4175 #define BNXT_NTP_FLTR_FLAGS \
4176 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4177 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4178 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4179 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4180 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4181 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4182 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4183 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4184 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4185 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4186 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4187 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4188 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4189 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4191 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4192 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4194 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4195 struct bnxt_ntuple_filter *fltr)
4197 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
4198 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4199 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4200 struct flow_keys *keys = &fltr->fkeys;
4203 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4204 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4206 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4208 req.ethertype = htons(ETH_P_IP);
4209 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4210 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4211 req.ip_protocol = keys->basic.ip_proto;
4213 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4216 req.ethertype = htons(ETH_P_IPV6);
4218 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4219 *(struct in6_addr *)&req.src_ipaddr[0] =
4220 keys->addrs.v6addrs.src;
4221 *(struct in6_addr *)&req.dst_ipaddr[0] =
4222 keys->addrs.v6addrs.dst;
4223 for (i = 0; i < 4; i++) {
4224 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4225 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4228 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4229 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4230 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4231 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4233 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4234 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4236 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4239 req.src_port = keys->ports.src;
4240 req.src_port_mask = cpu_to_be16(0xffff);
4241 req.dst_port = keys->ports.dst;
4242 req.dst_port_mask = cpu_to_be16(0xffff);
4244 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4245 mutex_lock(&bp->hwrm_cmd_lock);
4246 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4248 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4249 fltr->filter_id = resp->ntuple_filter_id;
4251 mutex_unlock(&bp->hwrm_cmd_lock);
4256 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4260 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4261 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4263 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4264 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4265 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4267 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4268 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4270 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4271 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4272 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4273 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4274 req.l2_addr_mask[0] = 0xff;
4275 req.l2_addr_mask[1] = 0xff;
4276 req.l2_addr_mask[2] = 0xff;
4277 req.l2_addr_mask[3] = 0xff;
4278 req.l2_addr_mask[4] = 0xff;
4279 req.l2_addr_mask[5] = 0xff;
4281 mutex_lock(&bp->hwrm_cmd_lock);
4282 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4284 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4286 mutex_unlock(&bp->hwrm_cmd_lock);
4290 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4292 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4295 /* Any associated ntuple filters will also be cleared by firmware. */
4296 mutex_lock(&bp->hwrm_cmd_lock);
4297 for (i = 0; i < num_of_vnics; i++) {
4298 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4300 for (j = 0; j < vnic->uc_filter_count; j++) {
4301 struct hwrm_cfa_l2_filter_free_input req = {0};
4303 bnxt_hwrm_cmd_hdr_init(bp, &req,
4304 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4306 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4308 rc = _hwrm_send_message(bp, &req, sizeof(req),
4311 vnic->uc_filter_count = 0;
4313 mutex_unlock(&bp->hwrm_cmd_lock);
4318 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
4320 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4321 struct hwrm_vnic_tpa_cfg_input req = {0};
4323 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
4326 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
4329 u16 mss = bp->dev->mtu - 40;
4330 u32 nsegs, n, segs = 0, flags;
4332 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
4333 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
4334 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
4335 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
4336 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
4337 if (tpa_flags & BNXT_FLAG_GRO)
4338 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
4340 req.flags = cpu_to_le32(flags);
4343 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
4344 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
4345 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
4347 /* Number of segs are log2 units, and first packet is not
4348 * included as part of this units.
4350 if (mss <= BNXT_RX_PAGE_SIZE) {
4351 n = BNXT_RX_PAGE_SIZE / mss;
4352 nsegs = (MAX_SKB_FRAGS - 1) * n;
4354 n = mss / BNXT_RX_PAGE_SIZE;
4355 if (mss & (BNXT_RX_PAGE_SIZE - 1))
4357 nsegs = (MAX_SKB_FRAGS - n) / n;
4360 segs = ilog2(nsegs);
4361 req.max_agg_segs = cpu_to_le16(segs);
4362 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
4364 req.min_agg_len = cpu_to_le32(512);
4366 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4368 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4371 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
4373 struct bnxt_ring_grp_info *grp_info;
4375 grp_info = &bp->grp_info[ring->grp_idx];
4376 return grp_info->cp_fw_ring_id;
4379 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
4381 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4382 struct bnxt_napi *bnapi = rxr->bnapi;
4383 struct bnxt_cp_ring_info *cpr;
4385 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
4386 return cpr->cp_ring_struct.fw_ring_id;
4388 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
4392 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
4394 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4395 struct bnxt_napi *bnapi = txr->bnapi;
4396 struct bnxt_cp_ring_info *cpr;
4398 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
4399 return cpr->cp_ring_struct.fw_ring_id;
4401 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
4405 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
4407 u32 i, j, max_rings;
4408 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4409 struct hwrm_vnic_rss_cfg_input req = {0};
4411 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
4412 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
4415 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4417 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4418 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4419 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
4420 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4421 max_rings = bp->rx_nr_rings - 1;
4423 max_rings = bp->rx_nr_rings;
4428 /* Fill the RSS indirection table with ring group ids */
4429 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
4432 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
4435 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4436 req.hash_key_tbl_addr =
4437 cpu_to_le64(vnic->rss_hash_key_dma_addr);
4439 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4440 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4443 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
4445 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4446 u32 i, j, k, nr_ctxs, max_rings = bp->rx_nr_rings;
4447 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4448 struct hwrm_vnic_rss_cfg_input req = {0};
4450 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
4451 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4453 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4456 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
4457 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
4458 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
4459 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
4460 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
4461 for (i = 0, k = 0; i < nr_ctxs; i++) {
4462 __le16 *ring_tbl = vnic->rss_table;
4465 req.ring_table_pair_index = i;
4466 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
4467 for (j = 0; j < 64; j++) {
4470 ring_id = rxr->rx_ring_struct.fw_ring_id;
4471 *ring_tbl++ = cpu_to_le16(ring_id);
4472 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
4473 *ring_tbl++ = cpu_to_le16(ring_id);
4476 if (k == max_rings) {
4478 rxr = &bp->rx_ring[0];
4481 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4488 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
4490 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4491 struct hwrm_vnic_plcmodes_cfg_input req = {0};
4493 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
4494 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
4495 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
4496 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
4498 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
4499 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
4500 /* thresholds not implemented in firmware yet */
4501 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
4502 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
4503 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4504 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4507 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
4510 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
4512 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
4513 req.rss_cos_lb_ctx_id =
4514 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
4516 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4517 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
4520 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
4524 for (i = 0; i < bp->nr_vnics; i++) {
4525 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4527 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
4528 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
4529 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
4532 bp->rsscos_nr_ctxs = 0;
4535 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
4538 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
4539 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
4540 bp->hwrm_cmd_resp_addr;
4542 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
4545 mutex_lock(&bp->hwrm_cmd_lock);
4546 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4548 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
4549 le16_to_cpu(resp->rss_cos_lb_ctx_id);
4550 mutex_unlock(&bp->hwrm_cmd_lock);
4555 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
4557 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
4558 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
4559 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
4562 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
4564 unsigned int ring = 0, grp_idx;
4565 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4566 struct hwrm_vnic_cfg_input req = {0};
4569 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
4571 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4572 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
4574 req.default_rx_ring_id =
4575 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
4576 req.default_cmpl_ring_id =
4577 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
4579 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
4580 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
4583 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
4584 /* Only RSS support for now TBD: COS & LB */
4585 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
4586 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
4587 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4588 VNIC_CFG_REQ_ENABLES_MRU);
4589 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
4591 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
4592 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
4593 VNIC_CFG_REQ_ENABLES_MRU);
4594 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
4596 req.rss_rule = cpu_to_le16(0xffff);
4599 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
4600 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
4601 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
4602 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
4604 req.cos_rule = cpu_to_le16(0xffff);
4607 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4609 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
4611 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
4612 ring = bp->rx_nr_rings - 1;
4614 grp_idx = bp->rx_ring[ring].bnapi->index;
4615 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
4616 req.lb_rule = cpu_to_le16(0xffff);
4618 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
4621 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
4622 #ifdef CONFIG_BNXT_SRIOV
4624 def_vlan = bp->vf.vlan;
4626 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
4627 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
4628 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
4629 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
4631 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4634 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
4638 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
4639 struct hwrm_vnic_free_input req = {0};
4641 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
4643 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
4645 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4648 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
4653 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
4657 for (i = 0; i < bp->nr_vnics; i++)
4658 bnxt_hwrm_vnic_free_one(bp, i);
4661 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
4662 unsigned int start_rx_ring_idx,
4663 unsigned int nr_rings)
4666 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
4667 struct hwrm_vnic_alloc_input req = {0};
4668 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4669 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4671 if (bp->flags & BNXT_FLAG_CHIP_P5)
4672 goto vnic_no_ring_grps;
4674 /* map ring groups to this vnic */
4675 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
4676 grp_idx = bp->rx_ring[i].bnapi->index;
4677 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
4678 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
4682 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
4686 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
4687 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
4689 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
4691 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
4693 mutex_lock(&bp->hwrm_cmd_lock);
4694 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4696 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
4697 mutex_unlock(&bp->hwrm_cmd_lock);
4701 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
4703 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4704 struct hwrm_vnic_qcaps_input req = {0};
4707 if (bp->hwrm_spec_code < 0x10600)
4710 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
4711 mutex_lock(&bp->hwrm_cmd_lock);
4712 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4714 u32 flags = le32_to_cpu(resp->flags);
4716 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
4717 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
4718 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
4720 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
4721 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
4723 mutex_unlock(&bp->hwrm_cmd_lock);
4727 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
4732 if (bp->flags & BNXT_FLAG_CHIP_P5)
4735 mutex_lock(&bp->hwrm_cmd_lock);
4736 for (i = 0; i < bp->rx_nr_rings; i++) {
4737 struct hwrm_ring_grp_alloc_input req = {0};
4738 struct hwrm_ring_grp_alloc_output *resp =
4739 bp->hwrm_cmd_resp_addr;
4740 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
4742 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
4744 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
4745 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
4746 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
4747 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
4749 rc = _hwrm_send_message(bp, &req, sizeof(req),
4754 bp->grp_info[grp_idx].fw_grp_id =
4755 le32_to_cpu(resp->ring_group_id);
4757 mutex_unlock(&bp->hwrm_cmd_lock);
4761 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4765 struct hwrm_ring_grp_free_input req = {0};
4767 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
4770 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4772 mutex_lock(&bp->hwrm_cmd_lock);
4773 for (i = 0; i < bp->cp_nr_rings; i++) {
4774 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4777 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4779 rc = _hwrm_send_message(bp, &req, sizeof(req),
4783 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4785 mutex_unlock(&bp->hwrm_cmd_lock);
4789 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4790 struct bnxt_ring_struct *ring,
4791 u32 ring_type, u32 map_index)
4793 int rc = 0, err = 0;
4794 struct hwrm_ring_alloc_input req = {0};
4795 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4796 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
4797 struct bnxt_ring_grp_info *grp_info;
4800 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4803 if (rmem->nr_pages > 1) {
4804 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
4805 /* Page size is in log2 units */
4806 req.page_size = BNXT_PAGE_SHIFT;
4807 req.page_tbl_depth = 1;
4809 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
4812 /* Association of ring index with doorbell index and MSIX number */
4813 req.logical_id = cpu_to_le16(map_index);
4815 switch (ring_type) {
4816 case HWRM_RING_ALLOC_TX: {
4817 struct bnxt_tx_ring_info *txr;
4819 txr = container_of(ring, struct bnxt_tx_ring_info,
4821 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4822 /* Association of transmit ring with completion ring */
4823 grp_info = &bp->grp_info[ring->grp_idx];
4824 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
4825 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4826 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4827 req.queue_id = cpu_to_le16(ring->queue_id);
4830 case HWRM_RING_ALLOC_RX:
4831 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4832 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4833 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4836 /* Association of rx ring with stats context */
4837 grp_info = &bp->grp_info[ring->grp_idx];
4838 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
4839 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4840 req.enables |= cpu_to_le32(
4841 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4842 if (NET_IP_ALIGN == 2)
4843 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
4844 req.flags = cpu_to_le16(flags);
4847 case HWRM_RING_ALLOC_AGG:
4848 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4849 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
4850 /* Association of agg ring with rx ring */
4851 grp_info = &bp->grp_info[ring->grp_idx];
4852 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
4853 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
4854 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
4855 req.enables |= cpu_to_le32(
4856 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
4857 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
4859 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4861 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4863 case HWRM_RING_ALLOC_CMPL:
4864 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
4865 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4866 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4867 /* Association of cp ring with nq */
4868 grp_info = &bp->grp_info[map_index];
4869 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
4870 req.cq_handle = cpu_to_le64(ring->handle);
4871 req.enables |= cpu_to_le32(
4872 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
4873 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
4874 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4877 case HWRM_RING_ALLOC_NQ:
4878 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
4879 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4880 if (bp->flags & BNXT_FLAG_USING_MSIX)
4881 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4884 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4889 mutex_lock(&bp->hwrm_cmd_lock);
4890 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4891 err = le16_to_cpu(resp->error_code);
4892 ring_id = le16_to_cpu(resp->ring_id);
4893 mutex_unlock(&bp->hwrm_cmd_lock);
4896 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
4897 ring_type, rc, err);
4900 ring->fw_ring_id = ring_id;
4904 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4909 struct hwrm_func_cfg_input req = {0};
4911 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4912 req.fid = cpu_to_le16(0xffff);
4913 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4914 req.async_event_cr = cpu_to_le16(idx);
4915 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4917 struct hwrm_func_vf_cfg_input req = {0};
4919 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4921 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4922 req.async_event_cr = cpu_to_le16(idx);
4923 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4928 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
4929 u32 map_idx, u32 xid)
4931 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4933 db->doorbell = bp->bar1 + 0x10000;
4935 db->doorbell = bp->bar1 + 0x4000;
4936 switch (ring_type) {
4937 case HWRM_RING_ALLOC_TX:
4938 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
4940 case HWRM_RING_ALLOC_RX:
4941 case HWRM_RING_ALLOC_AGG:
4942 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
4944 case HWRM_RING_ALLOC_CMPL:
4945 db->db_key64 = DBR_PATH_L2;
4947 case HWRM_RING_ALLOC_NQ:
4948 db->db_key64 = DBR_PATH_L2;
4951 db->db_key64 |= (u64)xid << DBR_XID_SFT;
4953 db->doorbell = bp->bar1 + map_idx * 0x80;
4954 switch (ring_type) {
4955 case HWRM_RING_ALLOC_TX:
4956 db->db_key32 = DB_KEY_TX;
4958 case HWRM_RING_ALLOC_RX:
4959 case HWRM_RING_ALLOC_AGG:
4960 db->db_key32 = DB_KEY_RX;
4962 case HWRM_RING_ALLOC_CMPL:
4963 db->db_key32 = DB_KEY_CP;
4969 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4974 if (bp->flags & BNXT_FLAG_CHIP_P5)
4975 type = HWRM_RING_ALLOC_NQ;
4977 type = HWRM_RING_ALLOC_CMPL;
4978 for (i = 0; i < bp->cp_nr_rings; i++) {
4979 struct bnxt_napi *bnapi = bp->bnapi[i];
4980 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4981 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4982 u32 map_idx = ring->map_idx;
4983 unsigned int vector;
4985 vector = bp->irq_tbl[map_idx].vector;
4986 disable_irq_nosync(vector);
4987 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
4992 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
4993 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4995 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
4998 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5000 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5004 type = HWRM_RING_ALLOC_TX;
5005 for (i = 0; i < bp->tx_nr_rings; i++) {
5006 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5007 struct bnxt_ring_struct *ring;
5010 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5011 struct bnxt_napi *bnapi = txr->bnapi;
5012 struct bnxt_cp_ring_info *cpr, *cpr2;
5013 u32 type2 = HWRM_RING_ALLOC_CMPL;
5015 cpr = &bnapi->cp_ring;
5016 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5017 ring = &cpr2->cp_ring_struct;
5018 ring->handle = BNXT_TX_HDL;
5019 map_idx = bnapi->index;
5020 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5023 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5025 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5027 ring = &txr->tx_ring_struct;
5029 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5032 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5035 type = HWRM_RING_ALLOC_RX;
5036 for (i = 0; i < bp->rx_nr_rings; i++) {
5037 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5038 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5039 struct bnxt_napi *bnapi = rxr->bnapi;
5040 u32 map_idx = bnapi->index;
5042 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5045 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5046 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5047 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5048 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5049 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5050 u32 type2 = HWRM_RING_ALLOC_CMPL;
5051 struct bnxt_cp_ring_info *cpr2;
5053 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5054 ring = &cpr2->cp_ring_struct;
5055 ring->handle = BNXT_RX_HDL;
5056 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5059 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5061 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5065 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5066 type = HWRM_RING_ALLOC_AGG;
5067 for (i = 0; i < bp->rx_nr_rings; i++) {
5068 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5069 struct bnxt_ring_struct *ring =
5070 &rxr->rx_agg_ring_struct;
5071 u32 grp_idx = ring->grp_idx;
5072 u32 map_idx = grp_idx + bp->rx_nr_rings;
5074 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5078 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5080 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5081 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5088 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5089 struct bnxt_ring_struct *ring,
5090 u32 ring_type, int cmpl_ring_id)
5093 struct hwrm_ring_free_input req = {0};
5094 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5097 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5098 req.ring_type = ring_type;
5099 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5101 mutex_lock(&bp->hwrm_cmd_lock);
5102 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5103 error_code = le16_to_cpu(resp->error_code);
5104 mutex_unlock(&bp->hwrm_cmd_lock);
5106 if (rc || error_code) {
5107 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5108 ring_type, rc, error_code);
5114 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5122 for (i = 0; i < bp->tx_nr_rings; i++) {
5123 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5124 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5127 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5128 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5129 hwrm_ring_free_send_msg(bp, ring,
5130 RING_FREE_REQ_RING_TYPE_TX,
5131 close_path ? cmpl_ring_id :
5132 INVALID_HW_RING_ID);
5133 ring->fw_ring_id = INVALID_HW_RING_ID;
5137 for (i = 0; i < bp->rx_nr_rings; i++) {
5138 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5139 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5140 u32 grp_idx = rxr->bnapi->index;
5143 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5144 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5145 hwrm_ring_free_send_msg(bp, ring,
5146 RING_FREE_REQ_RING_TYPE_RX,
5147 close_path ? cmpl_ring_id :
5148 INVALID_HW_RING_ID);
5149 ring->fw_ring_id = INVALID_HW_RING_ID;
5150 bp->grp_info[grp_idx].rx_fw_ring_id =
5155 if (bp->flags & BNXT_FLAG_CHIP_P5)
5156 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5158 type = RING_FREE_REQ_RING_TYPE_RX;
5159 for (i = 0; i < bp->rx_nr_rings; i++) {
5160 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5161 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5162 u32 grp_idx = rxr->bnapi->index;
5165 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5166 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5167 hwrm_ring_free_send_msg(bp, ring, type,
5168 close_path ? cmpl_ring_id :
5169 INVALID_HW_RING_ID);
5170 ring->fw_ring_id = INVALID_HW_RING_ID;
5171 bp->grp_info[grp_idx].agg_fw_ring_id =
5176 /* The completion rings are about to be freed. After that the
5177 * IRQ doorbell will not work anymore. So we need to disable
5180 bnxt_disable_int_sync(bp);
5182 if (bp->flags & BNXT_FLAG_CHIP_P5)
5183 type = RING_FREE_REQ_RING_TYPE_NQ;
5185 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5186 for (i = 0; i < bp->cp_nr_rings; i++) {
5187 struct bnxt_napi *bnapi = bp->bnapi[i];
5188 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5189 struct bnxt_ring_struct *ring;
5192 for (j = 0; j < 2; j++) {
5193 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5196 ring = &cpr2->cp_ring_struct;
5197 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5199 hwrm_ring_free_send_msg(bp, ring,
5200 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5201 INVALID_HW_RING_ID);
5202 ring->fw_ring_id = INVALID_HW_RING_ID;
5205 ring = &cpr->cp_ring_struct;
5206 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5207 hwrm_ring_free_send_msg(bp, ring, type,
5208 INVALID_HW_RING_ID);
5209 ring->fw_ring_id = INVALID_HW_RING_ID;
5210 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
5215 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5218 static int bnxt_hwrm_get_rings(struct bnxt *bp)
5220 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5221 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5222 struct hwrm_func_qcfg_input req = {0};
5225 if (bp->hwrm_spec_code < 0x10601)
5228 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5229 req.fid = cpu_to_le16(0xffff);
5230 mutex_lock(&bp->hwrm_cmd_lock);
5231 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5233 mutex_unlock(&bp->hwrm_cmd_lock);
5237 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5238 if (BNXT_NEW_RM(bp)) {
5241 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
5242 hw_resc->resv_hw_ring_grps =
5243 le32_to_cpu(resp->alloc_hw_ring_grps);
5244 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
5245 cp = le16_to_cpu(resp->alloc_cmpl_rings);
5246 stats = le16_to_cpu(resp->alloc_stat_ctx);
5247 hw_resc->resv_irqs = cp;
5248 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5249 int rx = hw_resc->resv_rx_rings;
5250 int tx = hw_resc->resv_tx_rings;
5252 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5254 if (cp < (rx + tx)) {
5255 bnxt_trim_rings(bp, &rx, &tx, cp, false);
5256 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5258 hw_resc->resv_rx_rings = rx;
5259 hw_resc->resv_tx_rings = tx;
5261 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
5262 hw_resc->resv_hw_ring_grps = rx;
5264 hw_resc->resv_cp_rings = cp;
5265 hw_resc->resv_stat_ctxs = stats;
5267 mutex_unlock(&bp->hwrm_cmd_lock);
5271 /* Caller must hold bp->hwrm_cmd_lock */
5272 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
5274 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5275 struct hwrm_func_qcfg_input req = {0};
5278 if (bp->hwrm_spec_code < 0x10601)
5281 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5282 req.fid = cpu_to_le16(fid);
5283 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5285 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
5290 static bool bnxt_rfs_supported(struct bnxt *bp);
5293 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
5294 int tx_rings, int rx_rings, int ring_grps,
5295 int cp_rings, int stats, int vnics)
5299 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
5300 req->fid = cpu_to_le16(0xffff);
5301 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5302 req->num_tx_rings = cpu_to_le16(tx_rings);
5303 if (BNXT_NEW_RM(bp)) {
5304 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
5305 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5306 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
5307 enables |= tx_rings + ring_grps ?
5308 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5309 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5310 enables |= rx_rings ?
5311 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5313 enables |= cp_rings ?
5314 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5315 FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5316 enables |= ring_grps ?
5317 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
5318 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5320 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
5322 req->num_rx_rings = cpu_to_le16(rx_rings);
5323 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5324 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5325 req->num_msix = cpu_to_le16(cp_rings);
5326 req->num_rsscos_ctxs =
5327 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5329 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5330 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5331 req->num_rsscos_ctxs = cpu_to_le16(1);
5332 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
5333 bnxt_rfs_supported(bp))
5334 req->num_rsscos_ctxs =
5335 cpu_to_le16(ring_grps + 1);
5337 req->num_stat_ctxs = cpu_to_le16(stats);
5338 req->num_vnics = cpu_to_le16(vnics);
5340 req->enables = cpu_to_le32(enables);
5344 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
5345 struct hwrm_func_vf_cfg_input *req, int tx_rings,
5346 int rx_rings, int ring_grps, int cp_rings,
5347 int stats, int vnics)
5351 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
5352 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
5353 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
5354 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
5355 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5356 enables |= tx_rings + ring_grps ?
5357 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5358 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5360 enables |= cp_rings ?
5361 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
5362 FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
5363 enables |= ring_grps ?
5364 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
5366 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
5367 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
5369 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
5370 req->num_tx_rings = cpu_to_le16(tx_rings);
5371 req->num_rx_rings = cpu_to_le16(rx_rings);
5372 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5373 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
5374 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
5376 req->num_cmpl_rings = cpu_to_le16(cp_rings);
5377 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
5378 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
5380 req->num_stat_ctxs = cpu_to_le16(stats);
5381 req->num_vnics = cpu_to_le16(vnics);
5383 req->enables = cpu_to_le32(enables);
5387 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5388 int ring_grps, int cp_rings, int stats, int vnics)
5390 struct hwrm_func_cfg_input req = {0};
5393 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5394 cp_rings, stats, vnics);
5398 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5402 if (bp->hwrm_spec_code < 0x10601)
5403 bp->hw_resc.resv_tx_rings = tx_rings;
5405 rc = bnxt_hwrm_get_rings(bp);
5410 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5411 int ring_grps, int cp_rings, int stats, int vnics)
5413 struct hwrm_func_vf_cfg_input req = {0};
5416 if (!BNXT_NEW_RM(bp)) {
5417 bp->hw_resc.resv_tx_rings = tx_rings;
5421 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5422 cp_rings, stats, vnics);
5423 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5427 rc = bnxt_hwrm_get_rings(bp);
5431 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
5432 int cp, int stat, int vnic)
5435 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
5438 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
5442 int bnxt_nq_rings_in_use(struct bnxt *bp)
5444 int cp = bp->cp_nr_rings;
5445 int ulp_msix, ulp_base;
5447 ulp_msix = bnxt_get_ulp_msix_num(bp);
5449 ulp_base = bnxt_get_ulp_msix_base(bp);
5451 if ((ulp_base + ulp_msix) > cp)
5452 cp = ulp_base + ulp_msix;
5457 static int bnxt_cp_rings_in_use(struct bnxt *bp)
5461 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5462 return bnxt_nq_rings_in_use(bp);
5464 cp = bp->tx_nr_rings + bp->rx_nr_rings;
5468 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
5470 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
5473 static bool bnxt_need_reserve_rings(struct bnxt *bp)
5475 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5476 int cp = bnxt_cp_rings_in_use(bp);
5477 int nq = bnxt_nq_rings_in_use(bp);
5478 int rx = bp->rx_nr_rings, stat;
5479 int vnic = 1, grp = rx;
5481 if (bp->hwrm_spec_code < 0x10601)
5484 if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
5487 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5489 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5491 stat = bnxt_get_func_stat_ctxs(bp);
5492 if (BNXT_NEW_RM(bp) &&
5493 (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
5494 hw_resc->resv_irqs < nq || hw_resc->resv_vnics != vnic ||
5495 hw_resc->resv_stat_ctxs != stat ||
5496 (hw_resc->resv_hw_ring_grps != grp &&
5497 !(bp->flags & BNXT_FLAG_CHIP_P5))))
5502 static int __bnxt_reserve_rings(struct bnxt *bp)
5504 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
5505 int cp = bnxt_nq_rings_in_use(bp);
5506 int tx = bp->tx_nr_rings;
5507 int rx = bp->rx_nr_rings;
5508 int grp, rx_rings, rc;
5512 if (!bnxt_need_reserve_rings(bp))
5515 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5517 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
5519 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5521 grp = bp->rx_nr_rings;
5522 stat = bnxt_get_func_stat_ctxs(bp);
5524 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
5528 tx = hw_resc->resv_tx_rings;
5529 if (BNXT_NEW_RM(bp)) {
5530 rx = hw_resc->resv_rx_rings;
5531 cp = hw_resc->resv_irqs;
5532 grp = hw_resc->resv_hw_ring_grps;
5533 vnic = hw_resc->resv_vnics;
5534 stat = hw_resc->resv_stat_ctxs;
5538 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
5542 if (netif_running(bp->dev))
5545 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
5546 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5547 bp->dev->hw_features &= ~NETIF_F_LRO;
5548 bp->dev->features &= ~NETIF_F_LRO;
5549 bnxt_set_ring_params(bp);
5552 rx_rings = min_t(int, rx_rings, grp);
5553 cp = min_t(int, cp, bp->cp_nr_rings);
5554 if (stat > bnxt_get_ulp_stat_ctxs(bp))
5555 stat -= bnxt_get_ulp_stat_ctxs(bp);
5556 cp = min_t(int, cp, stat);
5557 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
5558 if (bp->flags & BNXT_FLAG_AGG_RINGS)
5560 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
5561 bp->tx_nr_rings = tx;
5562 bp->rx_nr_rings = rx_rings;
5563 bp->cp_nr_rings = cp;
5565 if (!tx || !rx || !cp || !grp || !vnic || !stat)
5571 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5572 int ring_grps, int cp_rings, int stats,
5575 struct hwrm_func_vf_cfg_input req = {0};
5579 if (!BNXT_NEW_RM(bp))
5582 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5583 cp_rings, stats, vnics);
5584 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
5585 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5586 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
5587 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5588 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
5589 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
5590 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5591 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5593 req.flags = cpu_to_le32(flags);
5594 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5600 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5601 int ring_grps, int cp_rings, int stats,
5604 struct hwrm_func_cfg_input req = {0};
5608 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
5609 cp_rings, stats, vnics);
5610 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
5611 if (BNXT_NEW_RM(bp)) {
5612 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
5613 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
5614 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
5615 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
5616 if (bp->flags & BNXT_FLAG_CHIP_P5)
5617 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
5618 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
5620 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
5623 req.flags = cpu_to_le32(flags);
5624 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5630 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
5631 int ring_grps, int cp_rings, int stats,
5634 if (bp->hwrm_spec_code < 0x10801)
5638 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
5639 ring_grps, cp_rings, stats,
5642 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
5643 cp_rings, stats, vnics);
5646 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
5648 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5649 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5650 struct hwrm_ring_aggint_qcaps_input req = {0};
5653 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
5654 coal_cap->num_cmpl_dma_aggr_max = 63;
5655 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
5656 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
5657 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
5658 coal_cap->int_lat_tmr_min_max = 65535;
5659 coal_cap->int_lat_tmr_max_max = 65535;
5660 coal_cap->num_cmpl_aggr_int_max = 65535;
5661 coal_cap->timer_units = 80;
5663 if (bp->hwrm_spec_code < 0x10902)
5666 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
5667 mutex_lock(&bp->hwrm_cmd_lock);
5668 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5670 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
5671 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
5672 coal_cap->num_cmpl_dma_aggr_max =
5673 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
5674 coal_cap->num_cmpl_dma_aggr_during_int_max =
5675 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
5676 coal_cap->cmpl_aggr_dma_tmr_max =
5677 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
5678 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
5679 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
5680 coal_cap->int_lat_tmr_min_max =
5681 le16_to_cpu(resp->int_lat_tmr_min_max);
5682 coal_cap->int_lat_tmr_max_max =
5683 le16_to_cpu(resp->int_lat_tmr_max_max);
5684 coal_cap->num_cmpl_aggr_int_max =
5685 le16_to_cpu(resp->num_cmpl_aggr_int_max);
5686 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
5688 mutex_unlock(&bp->hwrm_cmd_lock);
5691 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
5693 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5695 return usec * 1000 / coal_cap->timer_units;
5698 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
5699 struct bnxt_coal *hw_coal,
5700 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
5702 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5703 u32 cmpl_params = coal_cap->cmpl_params;
5704 u16 val, tmr, max, flags = 0;
5706 max = hw_coal->bufs_per_record * 128;
5707 if (hw_coal->budget)
5708 max = hw_coal->bufs_per_record * hw_coal->budget;
5709 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
5711 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
5712 req->num_cmpl_aggr_int = cpu_to_le16(val);
5714 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
5715 req->num_cmpl_dma_aggr = cpu_to_le16(val);
5717 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
5718 coal_cap->num_cmpl_dma_aggr_during_int_max);
5719 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
5721 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
5722 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
5723 req->int_lat_tmr_max = cpu_to_le16(tmr);
5725 /* min timer set to 1/2 of interrupt timer */
5726 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
5728 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
5729 req->int_lat_tmr_min = cpu_to_le16(val);
5730 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
5733 /* buf timer set to 1/4 of interrupt timer */
5734 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
5735 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
5738 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
5739 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
5740 val = clamp_t(u16, tmr, 1,
5741 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
5742 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
5744 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
5747 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
5748 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
5749 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
5750 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
5751 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
5752 req->flags = cpu_to_le16(flags);
5753 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
5756 /* Caller holds bp->hwrm_cmd_lock */
5757 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
5758 struct bnxt_coal *hw_coal)
5760 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
5761 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5762 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
5763 u32 nq_params = coal_cap->nq_params;
5766 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
5769 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
5771 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
5773 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
5775 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
5776 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
5777 req.int_lat_tmr_min = cpu_to_le16(tmr);
5778 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
5779 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5782 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
5784 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
5785 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5786 struct bnxt_coal coal;
5788 /* Tick values in micro seconds.
5789 * 1 coal_buf x bufs_per_record = 1 completion record.
5791 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
5793 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
5794 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
5796 if (!bnapi->rx_ring)
5799 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5800 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5802 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
5804 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
5806 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
5810 int bnxt_hwrm_set_coal(struct bnxt *bp)
5813 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
5816 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
5817 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5818 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
5819 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
5821 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
5822 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
5824 mutex_lock(&bp->hwrm_cmd_lock);
5825 for (i = 0; i < bp->cp_nr_rings; i++) {
5826 struct bnxt_napi *bnapi = bp->bnapi[i];
5827 struct bnxt_coal *hw_coal;
5831 if (!bnapi->rx_ring) {
5832 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
5835 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
5837 req->ring_id = cpu_to_le16(ring_id);
5839 rc = _hwrm_send_message(bp, req, sizeof(*req),
5844 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
5847 if (bnapi->rx_ring && bnapi->tx_ring) {
5849 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
5850 req->ring_id = cpu_to_le16(ring_id);
5851 rc = _hwrm_send_message(bp, req, sizeof(*req),
5857 hw_coal = &bp->rx_coal;
5859 hw_coal = &bp->tx_coal;
5860 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
5862 mutex_unlock(&bp->hwrm_cmd_lock);
5866 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
5869 struct hwrm_stat_ctx_free_input req = {0};
5874 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5877 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
5879 mutex_lock(&bp->hwrm_cmd_lock);
5880 for (i = 0; i < bp->cp_nr_rings; i++) {
5881 struct bnxt_napi *bnapi = bp->bnapi[i];
5882 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5884 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
5885 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
5887 rc = _hwrm_send_message(bp, &req, sizeof(req),
5892 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5895 mutex_unlock(&bp->hwrm_cmd_lock);
5899 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
5902 struct hwrm_stat_ctx_alloc_input req = {0};
5903 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5905 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5908 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
5910 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
5912 mutex_lock(&bp->hwrm_cmd_lock);
5913 for (i = 0; i < bp->cp_nr_rings; i++) {
5914 struct bnxt_napi *bnapi = bp->bnapi[i];
5915 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5917 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
5919 rc = _hwrm_send_message(bp, &req, sizeof(req),
5924 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
5926 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
5928 mutex_unlock(&bp->hwrm_cmd_lock);
5932 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
5934 struct hwrm_func_qcfg_input req = {0};
5935 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5939 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
5940 req.fid = cpu_to_le16(0xffff);
5941 mutex_lock(&bp->hwrm_cmd_lock);
5942 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5944 goto func_qcfg_exit;
5946 #ifdef CONFIG_BNXT_SRIOV
5948 struct bnxt_vf_info *vf = &bp->vf;
5950 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
5953 flags = le16_to_cpu(resp->flags);
5954 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
5955 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
5956 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
5957 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
5958 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
5960 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
5961 bp->flags |= BNXT_FLAG_MULTI_HOST;
5963 switch (resp->port_partition_type) {
5964 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
5965 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
5966 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
5967 bp->port_partition_type = resp->port_partition_type;
5970 if (bp->hwrm_spec_code < 0x10707 ||
5971 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
5972 bp->br_mode = BRIDGE_MODE_VEB;
5973 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
5974 bp->br_mode = BRIDGE_MODE_VEPA;
5976 bp->br_mode = BRIDGE_MODE_UNDEF;
5978 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
5980 bp->max_mtu = BNXT_MAX_MTU;
5983 mutex_unlock(&bp->hwrm_cmd_lock);
5987 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
5989 struct hwrm_func_backing_store_qcaps_input req = {0};
5990 struct hwrm_func_backing_store_qcaps_output *resp =
5991 bp->hwrm_cmd_resp_addr;
5994 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
5997 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
5998 mutex_lock(&bp->hwrm_cmd_lock);
5999 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6001 struct bnxt_ctx_pg_info *ctx_pg;
6002 struct bnxt_ctx_mem_info *ctx;
6005 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6010 ctx_pg = kzalloc(sizeof(*ctx_pg) * (bp->max_q + 1), GFP_KERNEL);
6016 for (i = 0; i < bp->max_q + 1; i++, ctx_pg++)
6017 ctx->tqm_mem[i] = ctx_pg;
6020 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6021 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6022 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6023 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6024 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6025 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6026 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6027 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6028 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6029 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6030 ctx->vnic_max_vnic_entries =
6031 le16_to_cpu(resp->vnic_max_vnic_entries);
6032 ctx->vnic_max_ring_table_entries =
6033 le16_to_cpu(resp->vnic_max_ring_table_entries);
6034 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6035 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6036 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6037 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6038 ctx->tqm_min_entries_per_ring =
6039 le32_to_cpu(resp->tqm_min_entries_per_ring);
6040 ctx->tqm_max_entries_per_ring =
6041 le32_to_cpu(resp->tqm_max_entries_per_ring);
6042 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6043 if (!ctx->tqm_entries_multiple)
6044 ctx->tqm_entries_multiple = 1;
6045 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6046 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6047 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6048 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6053 mutex_unlock(&bp->hwrm_cmd_lock);
6057 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6062 if (BNXT_PAGE_SHIFT == 13)
6064 else if (BNXT_PAGE_SIZE == 16)
6068 if (rmem->depth >= 1) {
6069 if (rmem->depth == 2)
6073 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6075 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6079 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6080 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6081 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6082 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6083 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6084 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6086 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6088 struct hwrm_func_backing_store_cfg_input req = {0};
6089 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6090 struct bnxt_ctx_pg_info *ctx_pg;
6091 __le32 *num_entries;
6100 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6101 req.enables = cpu_to_le32(enables);
6103 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6104 ctx_pg = &ctx->qp_mem;
6105 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6106 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6107 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6108 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6109 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6110 &req.qpc_pg_size_qpc_lvl,
6113 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6114 ctx_pg = &ctx->srq_mem;
6115 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6116 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6117 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6118 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6119 &req.srq_pg_size_srq_lvl,
6122 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6123 ctx_pg = &ctx->cq_mem;
6124 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6125 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6126 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6127 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6130 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6131 ctx_pg = &ctx->vnic_mem;
6132 req.vnic_num_vnic_entries =
6133 cpu_to_le16(ctx->vnic_max_vnic_entries);
6134 req.vnic_num_ring_table_entries =
6135 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6136 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6137 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6138 &req.vnic_pg_size_vnic_lvl,
6139 &req.vnic_page_dir);
6141 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6142 ctx_pg = &ctx->stat_mem;
6143 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6144 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6145 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6146 &req.stat_pg_size_stat_lvl,
6147 &req.stat_page_dir);
6149 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
6150 ctx_pg = &ctx->mrav_mem;
6151 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
6152 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
6153 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6154 &req.mrav_pg_size_mrav_lvl,
6155 &req.mrav_page_dir);
6157 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
6158 ctx_pg = &ctx->tim_mem;
6159 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
6160 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
6161 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6162 &req.tim_pg_size_tim_lvl,
6165 for (i = 0, num_entries = &req.tqm_sp_num_entries,
6166 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
6167 pg_dir = &req.tqm_sp_page_dir,
6168 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
6169 i < 9; i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
6170 if (!(enables & ena))
6173 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
6174 ctx_pg = ctx->tqm_mem[i];
6175 *num_entries = cpu_to_le32(ctx_pg->entries);
6176 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
6178 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6184 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
6185 struct bnxt_ctx_pg_info *ctx_pg)
6187 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6189 rmem->page_size = BNXT_PAGE_SIZE;
6190 rmem->pg_arr = ctx_pg->ctx_pg_arr;
6191 rmem->dma_arr = ctx_pg->ctx_dma_arr;
6192 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
6193 if (rmem->depth >= 1)
6194 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
6195 return bnxt_alloc_ring(bp, rmem);
6198 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
6199 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
6202 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6208 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6209 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
6210 ctx_pg->nr_pages = 0;
6213 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
6217 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
6219 if (!ctx_pg->ctx_pg_tbl)
6221 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
6222 rmem->nr_pages = nr_tbls;
6223 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6226 for (i = 0; i < nr_tbls; i++) {
6227 struct bnxt_ctx_pg_info *pg_tbl;
6229 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
6232 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
6233 rmem = &pg_tbl->ring_mem;
6234 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
6235 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
6237 rmem->nr_pages = MAX_CTX_PAGES;
6238 if (i == (nr_tbls - 1)) {
6239 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
6242 rmem->nr_pages = rem;
6244 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
6249 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
6250 if (rmem->nr_pages > 1 || depth)
6252 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
6257 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
6258 struct bnxt_ctx_pg_info *ctx_pg)
6260 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
6262 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
6263 ctx_pg->ctx_pg_tbl) {
6264 int i, nr_tbls = rmem->nr_pages;
6266 for (i = 0; i < nr_tbls; i++) {
6267 struct bnxt_ctx_pg_info *pg_tbl;
6268 struct bnxt_ring_mem_info *rmem2;
6270 pg_tbl = ctx_pg->ctx_pg_tbl[i];
6273 rmem2 = &pg_tbl->ring_mem;
6274 bnxt_free_ring(bp, rmem2);
6275 ctx_pg->ctx_pg_arr[i] = NULL;
6277 ctx_pg->ctx_pg_tbl[i] = NULL;
6279 kfree(ctx_pg->ctx_pg_tbl);
6280 ctx_pg->ctx_pg_tbl = NULL;
6282 bnxt_free_ring(bp, rmem);
6283 ctx_pg->nr_pages = 0;
6286 static void bnxt_free_ctx_mem(struct bnxt *bp)
6288 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6294 if (ctx->tqm_mem[0]) {
6295 for (i = 0; i < bp->max_q + 1; i++)
6296 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
6297 kfree(ctx->tqm_mem[0]);
6298 ctx->tqm_mem[0] = NULL;
6301 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
6302 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
6303 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
6304 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
6305 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
6306 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
6307 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
6308 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
6311 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
6313 struct bnxt_ctx_pg_info *ctx_pg;
6314 struct bnxt_ctx_mem_info *ctx;
6315 u32 mem_size, ena, entries;
6321 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
6323 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
6328 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
6331 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
6337 ctx_pg = &ctx->qp_mem;
6338 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
6340 mem_size = ctx->qp_entry_size * ctx_pg->entries;
6341 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6345 ctx_pg = &ctx->srq_mem;
6346 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
6347 mem_size = ctx->srq_entry_size * ctx_pg->entries;
6348 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6352 ctx_pg = &ctx->cq_mem;
6353 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
6354 mem_size = ctx->cq_entry_size * ctx_pg->entries;
6355 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl);
6359 ctx_pg = &ctx->vnic_mem;
6360 ctx_pg->entries = ctx->vnic_max_vnic_entries +
6361 ctx->vnic_max_ring_table_entries;
6362 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
6363 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6367 ctx_pg = &ctx->stat_mem;
6368 ctx_pg->entries = ctx->stat_max_entries;
6369 mem_size = ctx->stat_entry_size * ctx_pg->entries;
6370 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6375 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
6378 ctx_pg = &ctx->mrav_mem;
6379 ctx_pg->entries = extra_qps * 4;
6380 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
6381 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2);
6384 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
6386 ctx_pg = &ctx->tim_mem;
6387 ctx_pg->entries = ctx->qp_mem.entries;
6388 mem_size = ctx->tim_entry_size * ctx_pg->entries;
6389 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6392 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
6395 entries = ctx->qp_max_l2_entries + extra_qps;
6396 entries = roundup(entries, ctx->tqm_entries_multiple);
6397 entries = clamp_t(u32, entries, ctx->tqm_min_entries_per_ring,
6398 ctx->tqm_max_entries_per_ring);
6399 for (i = 0; i < bp->max_q + 1; i++) {
6400 ctx_pg = ctx->tqm_mem[i];
6401 ctx_pg->entries = entries;
6402 mem_size = ctx->tqm_entry_size * entries;
6403 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1);
6406 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
6408 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
6409 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
6411 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
6414 ctx->flags |= BNXT_CTX_FLAG_INITED;
6419 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
6421 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6422 struct hwrm_func_resource_qcaps_input req = {0};
6423 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6426 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
6427 req.fid = cpu_to_le16(0xffff);
6429 mutex_lock(&bp->hwrm_cmd_lock);
6430 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
6434 goto hwrm_func_resc_qcaps_exit;
6437 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
6439 goto hwrm_func_resc_qcaps_exit;
6441 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
6442 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6443 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
6444 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6445 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
6446 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6447 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
6448 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6449 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
6450 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
6451 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
6452 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6453 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
6454 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6455 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
6456 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6458 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6459 u16 max_msix = le16_to_cpu(resp->max_msix);
6461 hw_resc->max_nqs = max_msix;
6462 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
6466 struct bnxt_pf_info *pf = &bp->pf;
6468 pf->vf_resv_strategy =
6469 le16_to_cpu(resp->vf_reservation_strategy);
6470 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
6471 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
6473 hwrm_func_resc_qcaps_exit:
6474 mutex_unlock(&bp->hwrm_cmd_lock);
6478 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
6481 struct hwrm_func_qcaps_input req = {0};
6482 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6483 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6486 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
6487 req.fid = cpu_to_le16(0xffff);
6489 mutex_lock(&bp->hwrm_cmd_lock);
6490 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6492 goto hwrm_func_qcaps_exit;
6494 flags = le32_to_cpu(resp->flags);
6495 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
6496 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
6497 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
6498 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
6500 bp->tx_push_thresh = 0;
6501 if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
6502 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
6504 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
6505 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
6506 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
6507 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
6508 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
6509 if (!hw_resc->max_hw_ring_grps)
6510 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
6511 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
6512 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
6513 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
6516 struct bnxt_pf_info *pf = &bp->pf;
6518 pf->fw_fid = le16_to_cpu(resp->fid);
6519 pf->port_id = le16_to_cpu(resp->port_id);
6520 bp->dev->dev_port = pf->port_id;
6521 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
6522 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
6523 pf->max_vfs = le16_to_cpu(resp->max_vfs);
6524 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
6525 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
6526 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
6527 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
6528 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
6529 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
6530 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
6531 bp->flags |= BNXT_FLAG_WOL_CAP;
6533 #ifdef CONFIG_BNXT_SRIOV
6534 struct bnxt_vf_info *vf = &bp->vf;
6536 vf->fw_fid = le16_to_cpu(resp->fid);
6537 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
6541 hwrm_func_qcaps_exit:
6542 mutex_unlock(&bp->hwrm_cmd_lock);
6546 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
6548 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
6552 rc = __bnxt_hwrm_func_qcaps(bp);
6555 rc = bnxt_hwrm_queue_qportcfg(bp);
6557 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
6560 if (bp->hwrm_spec_code >= 0x10803) {
6561 rc = bnxt_alloc_ctx_mem(bp);
6564 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
6566 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
6571 static int bnxt_hwrm_func_reset(struct bnxt *bp)
6573 struct hwrm_func_reset_input req = {0};
6575 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
6578 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
6581 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
6584 struct hwrm_queue_qportcfg_input req = {0};
6585 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
6589 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
6591 mutex_lock(&bp->hwrm_cmd_lock);
6592 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6596 if (!resp->max_configurable_queues) {
6600 bp->max_tc = resp->max_configurable_queues;
6601 bp->max_lltc = resp->max_configurable_lossless_queues;
6602 if (bp->max_tc > BNXT_MAX_QUEUE)
6603 bp->max_tc = BNXT_MAX_QUEUE;
6605 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
6606 qptr = &resp->queue_id0;
6607 for (i = 0, j = 0; i < bp->max_tc; i++) {
6608 bp->q_info[j].queue_id = *qptr;
6609 bp->q_ids[i] = *qptr++;
6610 bp->q_info[j].queue_profile = *qptr++;
6611 bp->tc_to_qidx[j] = j;
6612 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
6613 (no_rdma && BNXT_PF(bp)))
6616 bp->max_q = bp->max_tc;
6617 bp->max_tc = max_t(u8, j, 1);
6619 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
6622 if (bp->max_lltc > bp->max_tc)
6623 bp->max_lltc = bp->max_tc;
6626 mutex_unlock(&bp->hwrm_cmd_lock);
6630 static int bnxt_hwrm_ver_get(struct bnxt *bp)
6633 struct hwrm_ver_get_input req = {0};
6634 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
6637 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
6638 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
6639 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
6640 req.hwrm_intf_min = HWRM_VERSION_MINOR;
6641 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
6642 mutex_lock(&bp->hwrm_cmd_lock);
6643 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6645 goto hwrm_ver_get_exit;
6647 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
6649 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
6650 resp->hwrm_intf_min_8b << 8 |
6651 resp->hwrm_intf_upd_8b;
6652 if (resp->hwrm_intf_maj_8b < 1) {
6653 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
6654 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
6655 resp->hwrm_intf_upd_8b);
6656 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
6658 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
6659 resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
6660 resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
6662 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
6663 if (!bp->hwrm_cmd_timeout)
6664 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
6666 if (resp->hwrm_intf_maj_8b >= 1) {
6667 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
6668 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
6670 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
6671 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
6673 bp->chip_num = le16_to_cpu(resp->chip_num);
6674 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
6676 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
6678 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
6679 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
6680 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
6681 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
6683 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
6684 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
6687 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
6688 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
6691 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
6692 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
6695 mutex_unlock(&bp->hwrm_cmd_lock);
6699 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
6701 struct hwrm_fw_set_time_input req = {0};
6703 time64_t now = ktime_get_real_seconds();
6705 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
6706 bp->hwrm_spec_code < 0x10400)
6709 time64_to_tm(now, 0, &tm);
6710 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
6711 req.year = cpu_to_le16(1900 + tm.tm_year);
6712 req.month = 1 + tm.tm_mon;
6713 req.day = tm.tm_mday;
6714 req.hour = tm.tm_hour;
6715 req.minute = tm.tm_min;
6716 req.second = tm.tm_sec;
6717 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6720 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
6723 struct bnxt_pf_info *pf = &bp->pf;
6724 struct hwrm_port_qstats_input req = {0};
6726 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
6729 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
6730 req.port_id = cpu_to_le16(pf->port_id);
6731 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
6732 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
6733 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6737 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
6739 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
6740 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
6741 struct hwrm_port_qstats_ext_input req = {0};
6742 struct bnxt_pf_info *pf = &bp->pf;
6745 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
6748 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
6749 req.port_id = cpu_to_le16(pf->port_id);
6750 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
6751 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
6752 req.tx_stat_size = cpu_to_le16(sizeof(struct tx_port_stats_ext));
6753 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
6754 mutex_lock(&bp->hwrm_cmd_lock);
6755 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6757 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
6758 bp->fw_tx_stats_ext_size = le16_to_cpu(resp->tx_stat_size) / 8;
6760 bp->fw_rx_stats_ext_size = 0;
6761 bp->fw_tx_stats_ext_size = 0;
6763 if (bp->fw_tx_stats_ext_size <=
6764 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
6765 mutex_unlock(&bp->hwrm_cmd_lock);
6766 bp->pri2cos_valid = 0;
6770 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
6771 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
6773 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
6775 struct hwrm_queue_pri2cos_qcfg_output *resp2;
6779 resp2 = bp->hwrm_cmd_resp_addr;
6780 pri2cos = &resp2->pri0_cos_queue_id;
6781 for (i = 0; i < 8; i++) {
6782 u8 queue_id = pri2cos[i];
6784 for (j = 0; j < bp->max_q; j++) {
6785 if (bp->q_ids[j] == queue_id)
6789 bp->pri2cos_valid = 1;
6791 mutex_unlock(&bp->hwrm_cmd_lock);
6795 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
6797 if (bp->vxlan_port_cnt) {
6798 bnxt_hwrm_tunnel_dst_port_free(
6799 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6801 bp->vxlan_port_cnt = 0;
6802 if (bp->nge_port_cnt) {
6803 bnxt_hwrm_tunnel_dst_port_free(
6804 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6806 bp->nge_port_cnt = 0;
6809 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
6815 tpa_flags = bp->flags & BNXT_FLAG_TPA;
6816 for (i = 0; i < bp->nr_vnics; i++) {
6817 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
6819 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
6827 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
6831 for (i = 0; i < bp->nr_vnics; i++)
6832 bnxt_hwrm_vnic_set_rss(bp, i, false);
6835 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
6838 if (bp->vnic_info) {
6839 bnxt_hwrm_clear_vnic_filter(bp);
6840 /* clear all RSS setting before free vnic ctx */
6841 bnxt_hwrm_clear_vnic_rss(bp);
6842 bnxt_hwrm_vnic_ctx_free(bp);
6843 /* before free the vnic, undo the vnic tpa settings */
6844 if (bp->flags & BNXT_FLAG_TPA)
6845 bnxt_set_tpa(bp, false);
6846 bnxt_hwrm_vnic_free(bp);
6848 bnxt_hwrm_ring_free(bp, close_path);
6849 bnxt_hwrm_ring_grp_free(bp);
6851 bnxt_hwrm_stat_ctx_free(bp);
6852 bnxt_hwrm_free_tunnel_ports(bp);
6856 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
6858 struct hwrm_func_cfg_input req = {0};
6861 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6862 req.fid = cpu_to_le16(0xffff);
6863 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
6864 if (br_mode == BRIDGE_MODE_VEB)
6865 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
6866 else if (br_mode == BRIDGE_MODE_VEPA)
6867 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
6870 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6876 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
6878 struct hwrm_func_cfg_input req = {0};
6881 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
6884 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
6885 req.fid = cpu_to_le16(0xffff);
6886 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
6887 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
6889 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
6891 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6897 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
6899 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6902 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
6905 /* allocate context for vnic */
6906 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
6908 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
6910 goto vnic_setup_err;
6912 bp->rsscos_nr_ctxs++;
6914 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6915 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
6917 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
6919 goto vnic_setup_err;
6921 bp->rsscos_nr_ctxs++;
6925 /* configure default vnic, ring grp */
6926 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
6928 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
6930 goto vnic_setup_err;
6933 /* Enable RSS hashing on vnic */
6934 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
6936 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
6938 goto vnic_setup_err;
6941 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6942 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
6944 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
6953 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
6957 nr_ctxs = DIV_ROUND_UP(bp->rx_nr_rings, 64);
6958 for (i = 0; i < nr_ctxs; i++) {
6959 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
6961 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
6965 bp->rsscos_nr_ctxs++;
6970 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
6972 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
6976 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
6978 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
6982 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6983 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
6985 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
6992 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
6994 if (bp->flags & BNXT_FLAG_CHIP_P5)
6995 return __bnxt_setup_vnic_p5(bp, vnic_id);
6997 return __bnxt_setup_vnic(bp, vnic_id);
7000 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
7002 #ifdef CONFIG_RFS_ACCEL
7005 for (i = 0; i < bp->rx_nr_rings; i++) {
7006 struct bnxt_vnic_info *vnic;
7007 u16 vnic_id = i + 1;
7010 if (vnic_id >= bp->nr_vnics)
7013 vnic = &bp->vnic_info[vnic_id];
7014 vnic->flags |= BNXT_VNIC_RFS_FLAG;
7015 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
7016 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
7017 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
7019 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
7023 rc = bnxt_setup_vnic(bp, vnic_id);
7033 /* Allow PF and VF with default VLAN to be in promiscuous mode */
7034 static bool bnxt_promisc_ok(struct bnxt *bp)
7036 #ifdef CONFIG_BNXT_SRIOV
7037 if (BNXT_VF(bp) && !bp->vf.vlan)
7043 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
7045 unsigned int rc = 0;
7047 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
7049 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7054 rc = bnxt_hwrm_vnic_cfg(bp, 1);
7056 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
7063 static int bnxt_cfg_rx_mode(struct bnxt *);
7064 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
7066 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
7068 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7070 unsigned int rx_nr_rings = bp->rx_nr_rings;
7073 rc = bnxt_hwrm_stat_ctx_alloc(bp);
7075 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
7081 rc = bnxt_hwrm_ring_alloc(bp);
7083 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
7087 rc = bnxt_hwrm_ring_grp_alloc(bp);
7089 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
7093 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7096 /* default vnic 0 */
7097 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
7099 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
7103 rc = bnxt_setup_vnic(bp, 0);
7107 if (bp->flags & BNXT_FLAG_RFS) {
7108 rc = bnxt_alloc_rfs_vnics(bp);
7113 if (bp->flags & BNXT_FLAG_TPA) {
7114 rc = bnxt_set_tpa(bp, true);
7120 bnxt_update_vf_mac(bp);
7122 /* Filter for default vnic 0 */
7123 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
7125 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
7128 vnic->uc_filter_count = 1;
7131 if (bp->dev->flags & IFF_BROADCAST)
7132 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
7134 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
7135 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
7137 if (bp->dev->flags & IFF_ALLMULTI) {
7138 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
7139 vnic->mc_list_count = 0;
7143 bnxt_mc_list_updated(bp, &mask);
7144 vnic->rx_mask |= mask;
7147 rc = bnxt_cfg_rx_mode(bp);
7151 rc = bnxt_hwrm_set_coal(bp);
7153 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
7156 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7157 rc = bnxt_setup_nitroa0_vnic(bp);
7159 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
7164 bnxt_hwrm_func_qcfg(bp);
7165 netdev_update_features(bp->dev);
7171 bnxt_hwrm_resource_free(bp, 0, true);
7176 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
7178 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
7182 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
7184 bnxt_init_cp_rings(bp);
7185 bnxt_init_rx_rings(bp);
7186 bnxt_init_tx_rings(bp);
7187 bnxt_init_ring_grps(bp, irq_re_init);
7188 bnxt_init_vnics(bp);
7190 return bnxt_init_chip(bp, irq_re_init);
7193 static int bnxt_set_real_num_queues(struct bnxt *bp)
7196 struct net_device *dev = bp->dev;
7198 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
7199 bp->tx_nr_rings_xdp);
7203 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
7207 #ifdef CONFIG_RFS_ACCEL
7208 if (bp->flags & BNXT_FLAG_RFS)
7209 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
7215 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7218 int _rx = *rx, _tx = *tx;
7221 *rx = min_t(int, _rx, max);
7222 *tx = min_t(int, _tx, max);
7227 while (_rx + _tx > max) {
7228 if (_rx > _tx && _rx > 1)
7239 static void bnxt_setup_msix(struct bnxt *bp)
7241 const int len = sizeof(bp->irq_tbl[0].name);
7242 struct net_device *dev = bp->dev;
7245 tcs = netdev_get_num_tc(dev);
7249 for (i = 0; i < tcs; i++) {
7250 count = bp->tx_nr_rings_per_tc;
7252 netdev_set_tc_queue(dev, i, count, off);
7256 for (i = 0; i < bp->cp_nr_rings; i++) {
7257 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7260 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7262 else if (i < bp->rx_nr_rings)
7267 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
7269 bp->irq_tbl[map_idx].handler = bnxt_msix;
7273 static void bnxt_setup_inta(struct bnxt *bp)
7275 const int len = sizeof(bp->irq_tbl[0].name);
7277 if (netdev_get_num_tc(bp->dev))
7278 netdev_reset_tc(bp->dev);
7280 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
7282 bp->irq_tbl[0].handler = bnxt_inta;
7285 static int bnxt_setup_int_mode(struct bnxt *bp)
7289 if (bp->flags & BNXT_FLAG_USING_MSIX)
7290 bnxt_setup_msix(bp);
7292 bnxt_setup_inta(bp);
7294 rc = bnxt_set_real_num_queues(bp);
7298 #ifdef CONFIG_RFS_ACCEL
7299 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
7301 return bp->hw_resc.max_rsscos_ctxs;
7304 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
7306 return bp->hw_resc.max_vnics;
7310 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
7312 return bp->hw_resc.max_stat_ctxs;
7315 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
7317 return bp->hw_resc.max_cp_rings;
7320 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
7322 unsigned int cp = bp->hw_resc.max_cp_rings;
7324 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7325 cp -= bnxt_get_ulp_msix_num(bp);
7330 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
7332 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7334 if (bp->flags & BNXT_FLAG_CHIP_P5)
7335 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
7337 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
7340 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
7342 bp->hw_resc.max_irqs = max_irqs;
7345 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
7349 cp = bnxt_get_max_func_cp_rings_for_en(bp);
7350 if (bp->flags & BNXT_FLAG_CHIP_P5)
7351 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
7353 return cp - bp->cp_nr_rings;
7356 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
7360 stat = bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_ulp_stat_ctxs(bp);
7361 stat -= bp->cp_nr_rings;
7365 int bnxt_get_avail_msix(struct bnxt *bp, int num)
7367 int max_cp = bnxt_get_max_func_cp_rings(bp);
7368 int max_irq = bnxt_get_max_func_irqs(bp);
7369 int total_req = bp->cp_nr_rings + num;
7370 int max_idx, avail_msix;
7372 max_idx = bp->total_irqs;
7373 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
7374 max_idx = min_t(int, bp->total_irqs, max_cp);
7375 avail_msix = max_idx - bp->cp_nr_rings;
7376 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
7379 if (max_irq < total_req) {
7380 num = max_irq - bp->cp_nr_rings;
7387 static int bnxt_get_num_msix(struct bnxt *bp)
7389 if (!BNXT_NEW_RM(bp))
7390 return bnxt_get_max_func_irqs(bp);
7392 return bnxt_nq_rings_in_use(bp);
7395 static int bnxt_init_msix(struct bnxt *bp)
7397 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
7398 struct msix_entry *msix_ent;
7400 total_vecs = bnxt_get_num_msix(bp);
7401 max = bnxt_get_max_func_irqs(bp);
7402 if (total_vecs > max)
7408 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
7412 for (i = 0; i < total_vecs; i++) {
7413 msix_ent[i].entry = i;
7414 msix_ent[i].vector = 0;
7417 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
7420 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
7421 ulp_msix = bnxt_get_ulp_msix_num(bp);
7422 if (total_vecs < 0 || total_vecs < ulp_msix) {
7424 goto msix_setup_exit;
7427 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
7429 for (i = 0; i < total_vecs; i++)
7430 bp->irq_tbl[i].vector = msix_ent[i].vector;
7432 bp->total_irqs = total_vecs;
7433 /* Trim rings based upon num of vectors allocated */
7434 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
7435 total_vecs - ulp_msix, min == 1);
7437 goto msix_setup_exit;
7439 bp->cp_nr_rings = (min == 1) ?
7440 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7441 bp->tx_nr_rings + bp->rx_nr_rings;
7445 goto msix_setup_exit;
7447 bp->flags |= BNXT_FLAG_USING_MSIX;
7452 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
7455 pci_disable_msix(bp->pdev);
7460 static int bnxt_init_inta(struct bnxt *bp)
7462 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
7467 bp->rx_nr_rings = 1;
7468 bp->tx_nr_rings = 1;
7469 bp->cp_nr_rings = 1;
7470 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7471 bp->irq_tbl[0].vector = bp->pdev->irq;
7475 static int bnxt_init_int_mode(struct bnxt *bp)
7479 if (bp->flags & BNXT_FLAG_MSIX_CAP)
7480 rc = bnxt_init_msix(bp);
7482 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
7483 /* fallback to INTA */
7484 rc = bnxt_init_inta(bp);
7489 static void bnxt_clear_int_mode(struct bnxt *bp)
7491 if (bp->flags & BNXT_FLAG_USING_MSIX)
7492 pci_disable_msix(bp->pdev);
7496 bp->flags &= ~BNXT_FLAG_USING_MSIX;
7499 int bnxt_reserve_rings(struct bnxt *bp)
7501 int tcs = netdev_get_num_tc(bp->dev);
7502 bool reinit_irq = false;
7505 if (!bnxt_need_reserve_rings(bp))
7508 if (BNXT_NEW_RM(bp) && (bnxt_get_num_msix(bp) != bp->total_irqs)) {
7509 bnxt_ulp_irq_stop(bp);
7510 bnxt_clear_int_mode(bp);
7513 rc = __bnxt_reserve_rings(bp);
7516 rc = bnxt_init_int_mode(bp);
7517 bnxt_ulp_irq_restart(bp, rc);
7520 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
7523 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
7524 netdev_err(bp->dev, "tx ring reservation failure\n");
7525 netdev_reset_tc(bp->dev);
7526 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
7532 static void bnxt_free_irq(struct bnxt *bp)
7534 struct bnxt_irq *irq;
7537 #ifdef CONFIG_RFS_ACCEL
7538 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
7539 bp->dev->rx_cpu_rmap = NULL;
7541 if (!bp->irq_tbl || !bp->bnapi)
7544 for (i = 0; i < bp->cp_nr_rings; i++) {
7545 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7547 irq = &bp->irq_tbl[map_idx];
7548 if (irq->requested) {
7549 if (irq->have_cpumask) {
7550 irq_set_affinity_hint(irq->vector, NULL);
7551 free_cpumask_var(irq->cpu_mask);
7552 irq->have_cpumask = 0;
7554 free_irq(irq->vector, bp->bnapi[i]);
7561 static int bnxt_request_irq(struct bnxt *bp)
7564 unsigned long flags = 0;
7565 #ifdef CONFIG_RFS_ACCEL
7566 struct cpu_rmap *rmap;
7569 rc = bnxt_setup_int_mode(bp);
7571 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
7575 #ifdef CONFIG_RFS_ACCEL
7576 rmap = bp->dev->rx_cpu_rmap;
7578 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
7579 flags = IRQF_SHARED;
7581 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
7582 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
7583 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
7585 #ifdef CONFIG_RFS_ACCEL
7586 if (rmap && bp->bnapi[i]->rx_ring) {
7587 rc = irq_cpu_rmap_add(rmap, irq->vector);
7589 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
7594 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
7601 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
7602 int numa_node = dev_to_node(&bp->pdev->dev);
7604 irq->have_cpumask = 1;
7605 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
7607 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
7609 netdev_warn(bp->dev,
7610 "Set affinity failed, IRQ = %d\n",
7619 static void bnxt_del_napi(struct bnxt *bp)
7626 for (i = 0; i < bp->cp_nr_rings; i++) {
7627 struct bnxt_napi *bnapi = bp->bnapi[i];
7629 napi_hash_del(&bnapi->napi);
7630 netif_napi_del(&bnapi->napi);
7632 /* We called napi_hash_del() before netif_napi_del(), we need
7633 * to respect an RCU grace period before freeing napi structures.
7638 static void bnxt_init_napi(struct bnxt *bp)
7641 unsigned int cp_nr_rings = bp->cp_nr_rings;
7642 struct bnxt_napi *bnapi;
7644 if (bp->flags & BNXT_FLAG_USING_MSIX) {
7645 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
7647 if (bp->flags & BNXT_FLAG_CHIP_P5)
7648 poll_fn = bnxt_poll_p5;
7649 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
7651 for (i = 0; i < cp_nr_rings; i++) {
7652 bnapi = bp->bnapi[i];
7653 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
7655 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7656 bnapi = bp->bnapi[cp_nr_rings];
7657 netif_napi_add(bp->dev, &bnapi->napi,
7658 bnxt_poll_nitroa0, 64);
7661 bnapi = bp->bnapi[0];
7662 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
7666 static void bnxt_disable_napi(struct bnxt *bp)
7673 for (i = 0; i < bp->cp_nr_rings; i++) {
7674 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
7676 if (bp->bnapi[i]->rx_ring)
7677 cancel_work_sync(&cpr->dim.work);
7679 napi_disable(&bp->bnapi[i]->napi);
7683 static void bnxt_enable_napi(struct bnxt *bp)
7687 for (i = 0; i < bp->cp_nr_rings; i++) {
7688 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
7689 bp->bnapi[i]->in_reset = false;
7691 if (bp->bnapi[i]->rx_ring) {
7692 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
7693 cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
7695 napi_enable(&bp->bnapi[i]->napi);
7699 void bnxt_tx_disable(struct bnxt *bp)
7702 struct bnxt_tx_ring_info *txr;
7705 for (i = 0; i < bp->tx_nr_rings; i++) {
7706 txr = &bp->tx_ring[i];
7707 txr->dev_state = BNXT_DEV_STATE_CLOSING;
7710 /* Stop all TX queues */
7711 netif_tx_disable(bp->dev);
7712 netif_carrier_off(bp->dev);
7715 void bnxt_tx_enable(struct bnxt *bp)
7718 struct bnxt_tx_ring_info *txr;
7720 for (i = 0; i < bp->tx_nr_rings; i++) {
7721 txr = &bp->tx_ring[i];
7724 netif_tx_wake_all_queues(bp->dev);
7725 if (bp->link_info.link_up)
7726 netif_carrier_on(bp->dev);
7729 static void bnxt_report_link(struct bnxt *bp)
7731 if (bp->link_info.link_up) {
7733 const char *flow_ctrl;
7737 netif_carrier_on(bp->dev);
7738 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
7742 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
7743 flow_ctrl = "ON - receive & transmit";
7744 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
7745 flow_ctrl = "ON - transmit";
7746 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
7747 flow_ctrl = "ON - receive";
7750 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
7751 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
7752 speed, duplex, flow_ctrl);
7753 if (bp->flags & BNXT_FLAG_EEE_CAP)
7754 netdev_info(bp->dev, "EEE is %s\n",
7755 bp->eee.eee_active ? "active" :
7757 fec = bp->link_info.fec_cfg;
7758 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
7759 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
7760 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
7761 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
7762 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
7764 netif_carrier_off(bp->dev);
7765 netdev_err(bp->dev, "NIC Link is Down\n");
7769 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
7772 struct hwrm_port_phy_qcaps_input req = {0};
7773 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7774 struct bnxt_link_info *link_info = &bp->link_info;
7776 if (bp->hwrm_spec_code < 0x10201)
7779 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
7781 mutex_lock(&bp->hwrm_cmd_lock);
7782 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7784 goto hwrm_phy_qcaps_exit;
7786 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
7787 struct ethtool_eee *eee = &bp->eee;
7788 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
7790 bp->flags |= BNXT_FLAG_EEE_CAP;
7791 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7792 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
7793 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
7794 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
7795 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
7797 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
7799 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
7801 if (resp->supported_speeds_auto_mode)
7802 link_info->support_auto_speeds =
7803 le16_to_cpu(resp->supported_speeds_auto_mode);
7805 bp->port_count = resp->port_cnt;
7807 hwrm_phy_qcaps_exit:
7808 mutex_unlock(&bp->hwrm_cmd_lock);
7812 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
7815 struct bnxt_link_info *link_info = &bp->link_info;
7816 struct hwrm_port_phy_qcfg_input req = {0};
7817 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7818 u8 link_up = link_info->link_up;
7821 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
7823 mutex_lock(&bp->hwrm_cmd_lock);
7824 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7826 mutex_unlock(&bp->hwrm_cmd_lock);
7830 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
7831 link_info->phy_link_status = resp->link;
7832 link_info->duplex = resp->duplex_cfg;
7833 if (bp->hwrm_spec_code >= 0x10800)
7834 link_info->duplex = resp->duplex_state;
7835 link_info->pause = resp->pause;
7836 link_info->auto_mode = resp->auto_mode;
7837 link_info->auto_pause_setting = resp->auto_pause;
7838 link_info->lp_pause = resp->link_partner_adv_pause;
7839 link_info->force_pause_setting = resp->force_pause;
7840 link_info->duplex_setting = resp->duplex_cfg;
7841 if (link_info->phy_link_status == BNXT_LINK_LINK)
7842 link_info->link_speed = le16_to_cpu(resp->link_speed);
7844 link_info->link_speed = 0;
7845 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
7846 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
7847 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
7848 link_info->lp_auto_link_speeds =
7849 le16_to_cpu(resp->link_partner_adv_speeds);
7850 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
7851 link_info->phy_ver[0] = resp->phy_maj;
7852 link_info->phy_ver[1] = resp->phy_min;
7853 link_info->phy_ver[2] = resp->phy_bld;
7854 link_info->media_type = resp->media_type;
7855 link_info->phy_type = resp->phy_type;
7856 link_info->transceiver = resp->xcvr_pkg_type;
7857 link_info->phy_addr = resp->eee_config_phy_addr &
7858 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
7859 link_info->module_status = resp->module_status;
7861 if (bp->flags & BNXT_FLAG_EEE_CAP) {
7862 struct ethtool_eee *eee = &bp->eee;
7865 eee->eee_active = 0;
7866 if (resp->eee_config_phy_addr &
7867 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
7868 eee->eee_active = 1;
7869 fw_speeds = le16_to_cpu(
7870 resp->link_partner_adv_eee_link_speed_mask);
7871 eee->lp_advertised =
7872 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7875 /* Pull initial EEE config */
7876 if (!chng_link_state) {
7877 if (resp->eee_config_phy_addr &
7878 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
7879 eee->eee_enabled = 1;
7881 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
7883 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
7885 if (resp->eee_config_phy_addr &
7886 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
7889 eee->tx_lpi_enabled = 1;
7890 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
7891 eee->tx_lpi_timer = le32_to_cpu(tmr) &
7892 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
7897 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
7898 if (bp->hwrm_spec_code >= 0x10504)
7899 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
7901 /* TODO: need to add more logic to report VF link */
7902 if (chng_link_state) {
7903 if (link_info->phy_link_status == BNXT_LINK_LINK)
7904 link_info->link_up = 1;
7906 link_info->link_up = 0;
7907 if (link_up != link_info->link_up)
7908 bnxt_report_link(bp);
7910 /* alwasy link down if not require to update link state */
7911 link_info->link_up = 0;
7913 mutex_unlock(&bp->hwrm_cmd_lock);
7915 if (!BNXT_SINGLE_PF(bp))
7918 diff = link_info->support_auto_speeds ^ link_info->advertising;
7919 if ((link_info->support_auto_speeds | diff) !=
7920 link_info->support_auto_speeds) {
7921 /* An advertised speed is no longer supported, so we need to
7922 * update the advertisement settings. Caller holds RTNL
7923 * so we can modify link settings.
7925 link_info->advertising = link_info->support_auto_speeds;
7926 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
7927 bnxt_hwrm_set_link_setting(bp, true, false);
7932 static void bnxt_get_port_module_status(struct bnxt *bp)
7934 struct bnxt_link_info *link_info = &bp->link_info;
7935 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
7938 if (bnxt_update_link(bp, true))
7941 module_status = link_info->module_status;
7942 switch (module_status) {
7943 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
7944 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
7945 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
7946 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
7948 if (bp->hwrm_spec_code >= 0x10201) {
7949 netdev_warn(bp->dev, "Module part number %s\n",
7950 resp->phy_vendor_partnumber);
7952 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
7953 netdev_warn(bp->dev, "TX is disabled\n");
7954 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
7955 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
7960 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
7962 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
7963 if (bp->hwrm_spec_code >= 0x10201)
7965 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
7966 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7967 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
7968 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
7969 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
7971 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7973 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
7974 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
7975 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
7976 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
7978 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
7979 if (bp->hwrm_spec_code >= 0x10201) {
7980 req->auto_pause = req->force_pause;
7981 req->enables |= cpu_to_le32(
7982 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
7987 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
7988 struct hwrm_port_phy_cfg_input *req)
7990 u8 autoneg = bp->link_info.autoneg;
7991 u16 fw_link_speed = bp->link_info.req_link_speed;
7992 u16 advertising = bp->link_info.advertising;
7994 if (autoneg & BNXT_AUTONEG_SPEED) {
7996 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
7998 req->enables |= cpu_to_le32(
7999 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
8000 req->auto_link_speed_mask = cpu_to_le16(advertising);
8002 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
8004 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
8006 req->force_link_speed = cpu_to_le16(fw_link_speed);
8007 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
8010 /* tell chimp that the setting takes effect immediately */
8011 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
8014 int bnxt_hwrm_set_pause(struct bnxt *bp)
8016 struct hwrm_port_phy_cfg_input req = {0};
8019 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8020 bnxt_hwrm_set_pause_common(bp, &req);
8022 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
8023 bp->link_info.force_link_chng)
8024 bnxt_hwrm_set_link_common(bp, &req);
8026 mutex_lock(&bp->hwrm_cmd_lock);
8027 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8028 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
8029 /* since changing of pause setting doesn't trigger any link
8030 * change event, the driver needs to update the current pause
8031 * result upon successfully return of the phy_cfg command
8033 bp->link_info.pause =
8034 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
8035 bp->link_info.auto_pause_setting = 0;
8036 if (!bp->link_info.force_link_chng)
8037 bnxt_report_link(bp);
8039 bp->link_info.force_link_chng = false;
8040 mutex_unlock(&bp->hwrm_cmd_lock);
8044 static void bnxt_hwrm_set_eee(struct bnxt *bp,
8045 struct hwrm_port_phy_cfg_input *req)
8047 struct ethtool_eee *eee = &bp->eee;
8049 if (eee->eee_enabled) {
8051 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
8053 if (eee->tx_lpi_enabled)
8054 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
8056 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
8058 req->flags |= cpu_to_le32(flags);
8059 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
8060 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
8061 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
8063 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
8067 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
8069 struct hwrm_port_phy_cfg_input req = {0};
8071 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8073 bnxt_hwrm_set_pause_common(bp, &req);
8075 bnxt_hwrm_set_link_common(bp, &req);
8078 bnxt_hwrm_set_eee(bp, &req);
8079 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8082 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
8084 struct hwrm_port_phy_cfg_input req = {0};
8086 if (!BNXT_SINGLE_PF(bp))
8089 if (pci_num_vf(bp->pdev))
8092 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
8093 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
8094 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8097 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
8099 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
8100 struct hwrm_func_drv_if_change_input req = {0};
8101 bool resc_reinit = false;
8104 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
8107 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
8109 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
8110 mutex_lock(&bp->hwrm_cmd_lock);
8111 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8112 if (!rc && (resp->flags &
8113 cpu_to_le32(FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)))
8115 mutex_unlock(&bp->hwrm_cmd_lock);
8117 if (up && resc_reinit && BNXT_NEW_RM(bp)) {
8118 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8120 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
8121 hw_resc->resv_cp_rings = 0;
8122 hw_resc->resv_stat_ctxs = 0;
8123 hw_resc->resv_irqs = 0;
8124 hw_resc->resv_tx_rings = 0;
8125 hw_resc->resv_rx_rings = 0;
8126 hw_resc->resv_hw_ring_grps = 0;
8127 hw_resc->resv_vnics = 0;
8128 bp->tx_nr_rings = 0;
8129 bp->rx_nr_rings = 0;
8134 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
8136 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
8137 struct hwrm_port_led_qcaps_input req = {0};
8138 struct bnxt_pf_info *pf = &bp->pf;
8141 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
8144 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
8145 req.port_id = cpu_to_le16(pf->port_id);
8146 mutex_lock(&bp->hwrm_cmd_lock);
8147 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8149 mutex_unlock(&bp->hwrm_cmd_lock);
8152 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
8155 bp->num_leds = resp->num_leds;
8156 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
8158 for (i = 0; i < bp->num_leds; i++) {
8159 struct bnxt_led_info *led = &bp->leds[i];
8160 __le16 caps = led->led_state_caps;
8162 if (!led->led_group_id ||
8163 !BNXT_LED_ALT_BLINK_CAP(caps)) {
8169 mutex_unlock(&bp->hwrm_cmd_lock);
8173 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
8175 struct hwrm_wol_filter_alloc_input req = {0};
8176 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
8179 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
8180 req.port_id = cpu_to_le16(bp->pf.port_id);
8181 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
8182 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
8183 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
8184 mutex_lock(&bp->hwrm_cmd_lock);
8185 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8187 bp->wol_filter_id = resp->wol_filter_id;
8188 mutex_unlock(&bp->hwrm_cmd_lock);
8192 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
8194 struct hwrm_wol_filter_free_input req = {0};
8197 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
8198 req.port_id = cpu_to_le16(bp->pf.port_id);
8199 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
8200 req.wol_filter_id = bp->wol_filter_id;
8201 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8205 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
8207 struct hwrm_wol_filter_qcfg_input req = {0};
8208 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
8209 u16 next_handle = 0;
8212 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
8213 req.port_id = cpu_to_le16(bp->pf.port_id);
8214 req.handle = cpu_to_le16(handle);
8215 mutex_lock(&bp->hwrm_cmd_lock);
8216 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8218 next_handle = le16_to_cpu(resp->next_handle);
8219 if (next_handle != 0) {
8220 if (resp->wol_type ==
8221 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
8223 bp->wol_filter_id = resp->wol_filter_id;
8227 mutex_unlock(&bp->hwrm_cmd_lock);
8231 static void bnxt_get_wol_settings(struct bnxt *bp)
8235 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
8239 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
8240 } while (handle && handle != 0xffff);
8243 #ifdef CONFIG_BNXT_HWMON
8244 static ssize_t bnxt_show_temp(struct device *dev,
8245 struct device_attribute *devattr, char *buf)
8247 struct hwrm_temp_monitor_query_input req = {0};
8248 struct hwrm_temp_monitor_query_output *resp;
8249 struct bnxt *bp = dev_get_drvdata(dev);
8252 resp = bp->hwrm_cmd_resp_addr;
8253 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
8254 mutex_lock(&bp->hwrm_cmd_lock);
8255 if (!_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
8256 temp = resp->temp * 1000; /* display millidegree */
8257 mutex_unlock(&bp->hwrm_cmd_lock);
8259 return sprintf(buf, "%u\n", temp);
8261 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
8263 static struct attribute *bnxt_attrs[] = {
8264 &sensor_dev_attr_temp1_input.dev_attr.attr,
8267 ATTRIBUTE_GROUPS(bnxt);
8269 static void bnxt_hwmon_close(struct bnxt *bp)
8271 if (bp->hwmon_dev) {
8272 hwmon_device_unregister(bp->hwmon_dev);
8273 bp->hwmon_dev = NULL;
8277 static void bnxt_hwmon_open(struct bnxt *bp)
8279 struct pci_dev *pdev = bp->pdev;
8281 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
8282 DRV_MODULE_NAME, bp,
8284 if (IS_ERR(bp->hwmon_dev)) {
8285 bp->hwmon_dev = NULL;
8286 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
8290 static void bnxt_hwmon_close(struct bnxt *bp)
8294 static void bnxt_hwmon_open(struct bnxt *bp)
8299 static bool bnxt_eee_config_ok(struct bnxt *bp)
8301 struct ethtool_eee *eee = &bp->eee;
8302 struct bnxt_link_info *link_info = &bp->link_info;
8304 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
8307 if (eee->eee_enabled) {
8309 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
8311 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8312 eee->eee_enabled = 0;
8315 if (eee->advertised & ~advertising) {
8316 eee->advertised = advertising & eee->supported;
8323 static int bnxt_update_phy_setting(struct bnxt *bp)
8326 bool update_link = false;
8327 bool update_pause = false;
8328 bool update_eee = false;
8329 struct bnxt_link_info *link_info = &bp->link_info;
8331 rc = bnxt_update_link(bp, true);
8333 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
8337 if (!BNXT_SINGLE_PF(bp))
8340 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
8341 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
8342 link_info->req_flow_ctrl)
8343 update_pause = true;
8344 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
8345 link_info->force_pause_setting != link_info->req_flow_ctrl)
8346 update_pause = true;
8347 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
8348 if (BNXT_AUTO_MODE(link_info->auto_mode))
8350 if (link_info->req_link_speed != link_info->force_link_speed)
8352 if (link_info->req_duplex != link_info->duplex_setting)
8355 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
8357 if (link_info->advertising != link_info->auto_link_speeds)
8361 /* The last close may have shutdown the link, so need to call
8362 * PHY_CFG to bring it back up.
8364 if (!netif_carrier_ok(bp->dev))
8367 if (!bnxt_eee_config_ok(bp))
8371 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
8372 else if (update_pause)
8373 rc = bnxt_hwrm_set_pause(bp);
8375 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
8383 /* Common routine to pre-map certain register block to different GRC window.
8384 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
8385 * in PF and 3 windows in VF that can be customized to map in different
8388 static void bnxt_preset_reg_win(struct bnxt *bp)
8391 /* CAG registers map to GRC window #4 */
8392 writel(BNXT_CAG_REG_BASE,
8393 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
8397 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
8399 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8403 bnxt_preset_reg_win(bp);
8404 netif_carrier_off(bp->dev);
8406 /* Reserve rings now if none were reserved at driver probe. */
8407 rc = bnxt_init_dflt_ring_mode(bp);
8409 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
8413 rc = bnxt_reserve_rings(bp);
8416 if ((bp->flags & BNXT_FLAG_RFS) &&
8417 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
8418 /* disable RFS if falling back to INTA */
8419 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
8420 bp->flags &= ~BNXT_FLAG_RFS;
8423 rc = bnxt_alloc_mem(bp, irq_re_init);
8425 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8426 goto open_err_free_mem;
8431 rc = bnxt_request_irq(bp);
8433 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
8438 bnxt_enable_napi(bp);
8439 bnxt_debug_dev_init(bp);
8441 rc = bnxt_init_nic(bp, irq_re_init);
8443 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8448 mutex_lock(&bp->link_lock);
8449 rc = bnxt_update_phy_setting(bp);
8450 mutex_unlock(&bp->link_lock);
8452 netdev_warn(bp->dev, "failed to update phy settings\n");
8453 if (BNXT_SINGLE_PF(bp)) {
8454 bp->link_info.phy_retry = true;
8455 bp->link_info.phy_retry_expires =
8462 udp_tunnel_get_rx_info(bp->dev);
8464 set_bit(BNXT_STATE_OPEN, &bp->state);
8465 bnxt_enable_int(bp);
8466 /* Enable TX queues */
8468 mod_timer(&bp->timer, jiffies + bp->current_interval);
8469 /* Poll link status and check for SFP+ module status */
8470 bnxt_get_port_module_status(bp);
8472 /* VF-reps may need to be re-opened after the PF is re-opened */
8474 bnxt_vf_reps_open(bp);
8478 bnxt_debug_dev_exit(bp);
8479 bnxt_disable_napi(bp);
8487 bnxt_free_mem(bp, true);
8491 /* rtnl_lock held */
8492 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8496 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
8498 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
8504 /* rtnl_lock held, open the NIC half way by allocating all resources, but
8505 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
8508 int bnxt_half_open_nic(struct bnxt *bp)
8512 rc = bnxt_alloc_mem(bp, false);
8514 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
8517 rc = bnxt_init_nic(bp, false);
8519 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
8526 bnxt_free_mem(bp, false);
8531 /* rtnl_lock held, this call can only be made after a previous successful
8532 * call to bnxt_half_open_nic().
8534 void bnxt_half_close_nic(struct bnxt *bp)
8536 bnxt_hwrm_resource_free(bp, false, false);
8538 bnxt_free_mem(bp, false);
8541 static int bnxt_open(struct net_device *dev)
8543 struct bnxt *bp = netdev_priv(dev);
8546 bnxt_hwrm_if_change(bp, true);
8547 rc = __bnxt_open_nic(bp, true, true);
8549 bnxt_hwrm_if_change(bp, false);
8551 bnxt_hwmon_open(bp);
8556 static bool bnxt_drv_busy(struct bnxt *bp)
8558 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
8559 test_bit(BNXT_STATE_READ_STATS, &bp->state));
8562 static void bnxt_get_ring_stats(struct bnxt *bp,
8563 struct rtnl_link_stats64 *stats);
8565 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
8568 /* Close the VF-reps before closing PF */
8570 bnxt_vf_reps_close(bp);
8572 /* Change device state to avoid TX queue wake up's */
8573 bnxt_tx_disable(bp);
8575 clear_bit(BNXT_STATE_OPEN, &bp->state);
8576 smp_mb__after_atomic();
8577 while (bnxt_drv_busy(bp))
8580 /* Flush rings and and disable interrupts */
8581 bnxt_shutdown_nic(bp, irq_re_init);
8583 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
8585 bnxt_debug_dev_exit(bp);
8586 bnxt_disable_napi(bp);
8587 del_timer_sync(&bp->timer);
8590 /* Save ring stats before shutdown */
8592 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
8597 bnxt_free_mem(bp, irq_re_init);
8600 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
8604 #ifdef CONFIG_BNXT_SRIOV
8605 if (bp->sriov_cfg) {
8606 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
8608 BNXT_SRIOV_CFG_WAIT_TMO);
8610 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
8613 __bnxt_close_nic(bp, irq_re_init, link_re_init);
8617 static int bnxt_close(struct net_device *dev)
8619 struct bnxt *bp = netdev_priv(dev);
8621 bnxt_hwmon_close(bp);
8622 bnxt_close_nic(bp, true, true);
8623 bnxt_hwrm_shutdown_link(bp);
8624 bnxt_hwrm_if_change(bp, false);
8628 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
8631 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
8632 struct hwrm_port_phy_mdio_read_input req = {0};
8635 if (bp->hwrm_spec_code < 0x10a00)
8638 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
8639 req.port_id = cpu_to_le16(bp->pf.port_id);
8640 req.phy_addr = phy_addr;
8641 req.reg_addr = cpu_to_le16(reg & 0x1f);
8642 if (bp->link_info.support_speeds & BNXT_LINK_SPEED_MSK_10GB) {
8644 req.phy_addr = mdio_phy_id_prtad(phy_addr);
8645 req.dev_addr = mdio_phy_id_devad(phy_addr);
8646 req.reg_addr = cpu_to_le16(reg);
8649 mutex_lock(&bp->hwrm_cmd_lock);
8650 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8652 *val = le16_to_cpu(resp->reg_data);
8653 mutex_unlock(&bp->hwrm_cmd_lock);
8657 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
8660 struct hwrm_port_phy_mdio_write_input req = {0};
8662 if (bp->hwrm_spec_code < 0x10a00)
8665 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
8666 req.port_id = cpu_to_le16(bp->pf.port_id);
8667 req.phy_addr = phy_addr;
8668 req.reg_addr = cpu_to_le16(reg & 0x1f);
8669 if (bp->link_info.support_speeds & BNXT_LINK_SPEED_MSK_10GB) {
8671 req.phy_addr = mdio_phy_id_prtad(phy_addr);
8672 req.dev_addr = mdio_phy_id_devad(phy_addr);
8673 req.reg_addr = cpu_to_le16(reg);
8675 req.reg_data = cpu_to_le16(val);
8677 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8680 /* rtnl_lock held */
8681 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
8683 struct mii_ioctl_data *mdio = if_mii(ifr);
8684 struct bnxt *bp = netdev_priv(dev);
8689 mdio->phy_id = bp->link_info.phy_addr;
8695 if (!netif_running(dev))
8698 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
8700 mdio->val_out = mii_regval;
8705 if (!netif_running(dev))
8708 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
8718 static void bnxt_get_ring_stats(struct bnxt *bp,
8719 struct rtnl_link_stats64 *stats)
8724 for (i = 0; i < bp->cp_nr_rings; i++) {
8725 struct bnxt_napi *bnapi = bp->bnapi[i];
8726 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8727 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
8729 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
8730 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
8731 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
8733 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
8734 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
8735 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
8737 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
8738 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
8739 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
8741 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
8742 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
8743 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
8745 stats->rx_missed_errors +=
8746 le64_to_cpu(hw_stats->rx_discard_pkts);
8748 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
8750 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
8754 static void bnxt_add_prev_stats(struct bnxt *bp,
8755 struct rtnl_link_stats64 *stats)
8757 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
8759 stats->rx_packets += prev_stats->rx_packets;
8760 stats->tx_packets += prev_stats->tx_packets;
8761 stats->rx_bytes += prev_stats->rx_bytes;
8762 stats->tx_bytes += prev_stats->tx_bytes;
8763 stats->rx_missed_errors += prev_stats->rx_missed_errors;
8764 stats->multicast += prev_stats->multicast;
8765 stats->tx_dropped += prev_stats->tx_dropped;
8769 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
8771 struct bnxt *bp = netdev_priv(dev);
8773 set_bit(BNXT_STATE_READ_STATS, &bp->state);
8774 /* Make sure bnxt_close_nic() sees that we are reading stats before
8775 * we check the BNXT_STATE_OPEN flag.
8777 smp_mb__after_atomic();
8778 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
8779 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
8780 *stats = bp->net_stats_prev;
8784 bnxt_get_ring_stats(bp, stats);
8785 bnxt_add_prev_stats(bp, stats);
8787 if (bp->flags & BNXT_FLAG_PORT_STATS) {
8788 struct rx_port_stats *rx = bp->hw_rx_port_stats;
8789 struct tx_port_stats *tx = bp->hw_tx_port_stats;
8791 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
8792 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
8793 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
8794 le64_to_cpu(rx->rx_ovrsz_frames) +
8795 le64_to_cpu(rx->rx_runt_frames);
8796 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
8797 le64_to_cpu(rx->rx_jbr_frames);
8798 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
8799 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
8800 stats->tx_errors = le64_to_cpu(tx->tx_err);
8802 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
8805 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
8807 struct net_device *dev = bp->dev;
8808 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8809 struct netdev_hw_addr *ha;
8812 bool update = false;
8815 netdev_for_each_mc_addr(ha, dev) {
8816 if (mc_count >= BNXT_MAX_MC_ADDRS) {
8817 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8818 vnic->mc_list_count = 0;
8822 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
8823 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
8830 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
8832 if (mc_count != vnic->mc_list_count) {
8833 vnic->mc_list_count = mc_count;
8839 static bool bnxt_uc_list_updated(struct bnxt *bp)
8841 struct net_device *dev = bp->dev;
8842 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8843 struct netdev_hw_addr *ha;
8846 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
8849 netdev_for_each_uc_addr(ha, dev) {
8850 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
8858 static void bnxt_set_rx_mode(struct net_device *dev)
8860 struct bnxt *bp = netdev_priv(dev);
8861 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8862 u32 mask = vnic->rx_mask;
8863 bool mc_update = false;
8866 if (!netif_running(dev))
8869 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
8870 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
8871 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
8872 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
8874 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
8875 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8877 uc_update = bnxt_uc_list_updated(bp);
8879 if (dev->flags & IFF_BROADCAST)
8880 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8881 if (dev->flags & IFF_ALLMULTI) {
8882 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8883 vnic->mc_list_count = 0;
8885 mc_update = bnxt_mc_list_updated(bp, &mask);
8888 if (mask != vnic->rx_mask || uc_update || mc_update) {
8889 vnic->rx_mask = mask;
8891 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
8892 bnxt_queue_sp_work(bp);
8896 static int bnxt_cfg_rx_mode(struct bnxt *bp)
8898 struct net_device *dev = bp->dev;
8899 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8900 struct netdev_hw_addr *ha;
8904 netif_addr_lock_bh(dev);
8905 uc_update = bnxt_uc_list_updated(bp);
8906 netif_addr_unlock_bh(dev);
8911 mutex_lock(&bp->hwrm_cmd_lock);
8912 for (i = 1; i < vnic->uc_filter_count; i++) {
8913 struct hwrm_cfa_l2_filter_free_input req = {0};
8915 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
8918 req.l2_filter_id = vnic->fw_l2_filter_id[i];
8920 rc = _hwrm_send_message(bp, &req, sizeof(req),
8923 mutex_unlock(&bp->hwrm_cmd_lock);
8925 vnic->uc_filter_count = 1;
8927 netif_addr_lock_bh(dev);
8928 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
8929 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8931 netdev_for_each_uc_addr(ha, dev) {
8932 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
8934 vnic->uc_filter_count++;
8937 netif_addr_unlock_bh(dev);
8939 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
8940 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
8942 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
8944 vnic->uc_filter_count = i;
8950 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
8952 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
8958 static bool bnxt_can_reserve_rings(struct bnxt *bp)
8960 #ifdef CONFIG_BNXT_SRIOV
8961 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
8962 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8964 /* No minimum rings were provisioned by the PF. Don't
8965 * reserve rings by default when device is down.
8967 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
8970 if (!netif_running(bp->dev))
8977 /* If the chip and firmware supports RFS */
8978 static bool bnxt_rfs_supported(struct bnxt *bp)
8980 if (bp->flags & BNXT_FLAG_CHIP_P5)
8982 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
8984 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8989 /* If runtime conditions support RFS */
8990 static bool bnxt_rfs_capable(struct bnxt *bp)
8992 #ifdef CONFIG_RFS_ACCEL
8993 int vnics, max_vnics, max_rss_ctxs;
8995 if (bp->flags & BNXT_FLAG_CHIP_P5)
8997 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
9000 vnics = 1 + bp->rx_nr_rings;
9001 max_vnics = bnxt_get_max_func_vnics(bp);
9002 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
9004 /* RSS contexts not a limiting factor */
9005 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
9006 max_rss_ctxs = max_vnics;
9007 if (vnics > max_vnics || vnics > max_rss_ctxs) {
9008 if (bp->rx_nr_rings > 1)
9009 netdev_warn(bp->dev,
9010 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
9011 min(max_rss_ctxs - 1, max_vnics - 1));
9015 if (!BNXT_NEW_RM(bp))
9018 if (vnics == bp->hw_resc.resv_vnics)
9021 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
9022 if (vnics <= bp->hw_resc.resv_vnics)
9025 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
9026 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
9033 static netdev_features_t bnxt_fix_features(struct net_device *dev,
9034 netdev_features_t features)
9036 struct bnxt *bp = netdev_priv(dev);
9038 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
9039 features &= ~NETIF_F_NTUPLE;
9041 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9042 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
9044 if (!(features & NETIF_F_GRO))
9045 features &= ~NETIF_F_GRO_HW;
9047 if (features & NETIF_F_GRO_HW)
9048 features &= ~NETIF_F_LRO;
9050 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
9051 * turned on or off together.
9053 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
9054 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
9055 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
9056 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9057 NETIF_F_HW_VLAN_STAG_RX);
9059 features |= NETIF_F_HW_VLAN_CTAG_RX |
9060 NETIF_F_HW_VLAN_STAG_RX;
9062 #ifdef CONFIG_BNXT_SRIOV
9065 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
9066 NETIF_F_HW_VLAN_STAG_RX);
9073 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
9075 struct bnxt *bp = netdev_priv(dev);
9076 u32 flags = bp->flags;
9079 bool re_init = false;
9080 bool update_tpa = false;
9082 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
9083 if (features & NETIF_F_GRO_HW)
9084 flags |= BNXT_FLAG_GRO;
9085 else if (features & NETIF_F_LRO)
9086 flags |= BNXT_FLAG_LRO;
9088 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
9089 flags &= ~BNXT_FLAG_TPA;
9091 if (features & NETIF_F_HW_VLAN_CTAG_RX)
9092 flags |= BNXT_FLAG_STRIP_VLAN;
9094 if (features & NETIF_F_NTUPLE)
9095 flags |= BNXT_FLAG_RFS;
9097 changes = flags ^ bp->flags;
9098 if (changes & BNXT_FLAG_TPA) {
9100 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
9101 (flags & BNXT_FLAG_TPA) == 0)
9105 if (changes & ~BNXT_FLAG_TPA)
9108 if (flags != bp->flags) {
9109 u32 old_flags = bp->flags;
9113 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9115 bnxt_set_ring_params(bp);
9120 bnxt_close_nic(bp, false, false);
9122 bnxt_set_ring_params(bp);
9124 return bnxt_open_nic(bp, false, false);
9127 rc = bnxt_set_tpa(bp,
9128 (flags & BNXT_FLAG_TPA) ?
9131 bp->flags = old_flags;
9137 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
9138 u32 ring_id, u32 *prod, u32 *cons)
9140 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
9141 struct hwrm_dbg_ring_info_get_input req = {0};
9144 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
9145 req.ring_type = ring_type;
9146 req.fw_ring_id = cpu_to_le32(ring_id);
9147 mutex_lock(&bp->hwrm_cmd_lock);
9148 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9150 *prod = le32_to_cpu(resp->producer_index);
9151 *cons = le32_to_cpu(resp->consumer_index);
9153 mutex_unlock(&bp->hwrm_cmd_lock);
9157 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
9159 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
9160 int i = bnapi->index;
9165 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
9166 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
9170 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
9172 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
9173 int i = bnapi->index;
9178 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",
9179 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
9180 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
9181 rxr->rx_sw_agg_prod);
9184 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
9186 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
9187 int i = bnapi->index;
9189 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
9190 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
9193 static void bnxt_dbg_dump_states(struct bnxt *bp)
9196 struct bnxt_napi *bnapi;
9198 for (i = 0; i < bp->cp_nr_rings; i++) {
9199 bnapi = bp->bnapi[i];
9200 if (netif_msg_drv(bp)) {
9201 bnxt_dump_tx_sw_state(bnapi);
9202 bnxt_dump_rx_sw_state(bnapi);
9203 bnxt_dump_cp_sw_state(bnapi);
9208 static void bnxt_reset_task(struct bnxt *bp, bool silent)
9211 bnxt_dbg_dump_states(bp);
9212 if (netif_running(bp->dev)) {
9217 bnxt_close_nic(bp, false, false);
9218 rc = bnxt_open_nic(bp, false, false);
9224 static void bnxt_tx_timeout(struct net_device *dev)
9226 struct bnxt *bp = netdev_priv(dev);
9228 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
9229 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
9230 bnxt_queue_sp_work(bp);
9233 static void bnxt_timer(struct timer_list *t)
9235 struct bnxt *bp = from_timer(bp, t, timer);
9236 struct net_device *dev = bp->dev;
9238 if (!netif_running(dev))
9241 if (atomic_read(&bp->intr_sem) != 0)
9242 goto bnxt_restart_timer;
9244 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
9245 bp->stats_coal_ticks) {
9246 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
9247 bnxt_queue_sp_work(bp);
9250 if (bnxt_tc_flower_enabled(bp)) {
9251 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
9252 bnxt_queue_sp_work(bp);
9255 if (bp->link_info.phy_retry) {
9256 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
9257 bp->link_info.phy_retry = 0;
9258 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
9260 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
9261 bnxt_queue_sp_work(bp);
9265 if ((bp->flags & BNXT_FLAG_CHIP_P5) && netif_carrier_ok(dev)) {
9266 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
9267 bnxt_queue_sp_work(bp);
9270 mod_timer(&bp->timer, jiffies + bp->current_interval);
9273 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
9275 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
9276 * set. If the device is being closed, bnxt_close() may be holding
9277 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
9278 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
9280 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9284 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
9286 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9290 /* Only called from bnxt_sp_task() */
9291 static void bnxt_reset(struct bnxt *bp, bool silent)
9293 bnxt_rtnl_lock_sp(bp);
9294 if (test_bit(BNXT_STATE_OPEN, &bp->state))
9295 bnxt_reset_task(bp, silent);
9296 bnxt_rtnl_unlock_sp(bp);
9299 static void bnxt_chk_missed_irq(struct bnxt *bp)
9303 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
9306 for (i = 0; i < bp->cp_nr_rings; i++) {
9307 struct bnxt_napi *bnapi = bp->bnapi[i];
9308 struct bnxt_cp_ring_info *cpr;
9315 cpr = &bnapi->cp_ring;
9316 for (j = 0; j < 2; j++) {
9317 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
9320 if (!cpr2 || cpr2->has_more_work ||
9321 !bnxt_has_work(bp, cpr2))
9324 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
9325 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
9328 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
9329 bnxt_dbg_hwrm_ring_info_get(bp,
9330 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
9331 fw_ring_id, &val[0], &val[1]);
9337 static void bnxt_cfg_ntp_filters(struct bnxt *);
9339 static void bnxt_sp_task(struct work_struct *work)
9341 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
9343 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9344 smp_mb__after_atomic();
9345 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
9346 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9350 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
9351 bnxt_cfg_rx_mode(bp);
9353 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
9354 bnxt_cfg_ntp_filters(bp);
9355 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
9356 bnxt_hwrm_exec_fwd_req(bp);
9357 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9358 bnxt_hwrm_tunnel_dst_port_alloc(
9360 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9362 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9363 bnxt_hwrm_tunnel_dst_port_free(
9364 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
9366 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
9367 bnxt_hwrm_tunnel_dst_port_alloc(
9369 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9371 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
9372 bnxt_hwrm_tunnel_dst_port_free(
9373 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
9375 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
9376 bnxt_hwrm_port_qstats(bp);
9377 bnxt_hwrm_port_qstats_ext(bp);
9380 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
9383 mutex_lock(&bp->link_lock);
9384 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
9386 bnxt_hwrm_phy_qcaps(bp);
9388 rc = bnxt_update_link(bp, true);
9389 mutex_unlock(&bp->link_lock);
9391 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
9394 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
9397 mutex_lock(&bp->link_lock);
9398 rc = bnxt_update_phy_setting(bp);
9399 mutex_unlock(&bp->link_lock);
9401 netdev_warn(bp->dev, "update phy settings retry failed\n");
9403 bp->link_info.phy_retry = false;
9404 netdev_info(bp->dev, "update phy settings retry succeeded\n");
9407 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
9408 mutex_lock(&bp->link_lock);
9409 bnxt_get_port_module_status(bp);
9410 mutex_unlock(&bp->link_lock);
9413 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
9414 bnxt_tc_flow_stats_work(bp);
9416 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
9417 bnxt_chk_missed_irq(bp);
9419 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
9420 * must be the last functions to be called before exiting.
9422 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
9423 bnxt_reset(bp, false);
9425 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
9426 bnxt_reset(bp, true);
9428 smp_mb__before_atomic();
9429 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
9432 /* Under rtnl_lock */
9433 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
9436 int max_rx, max_tx, tx_sets = 1;
9437 int tx_rings_needed, stats;
9444 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
9451 tx_rings_needed = tx * tx_sets + tx_xdp;
9452 if (max_tx < tx_rings_needed)
9456 if (bp->flags & BNXT_FLAG_RFS)
9459 if (bp->flags & BNXT_FLAG_AGG_RINGS)
9461 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
9463 if (BNXT_NEW_RM(bp)) {
9464 cp += bnxt_get_ulp_msix_num(bp);
9465 stats += bnxt_get_ulp_stat_ctxs(bp);
9467 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
9471 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
9474 pci_iounmap(pdev, bp->bar2);
9479 pci_iounmap(pdev, bp->bar1);
9484 pci_iounmap(pdev, bp->bar0);
9489 static void bnxt_cleanup_pci(struct bnxt *bp)
9491 bnxt_unmap_bars(bp, bp->pdev);
9492 pci_release_regions(bp->pdev);
9493 pci_disable_device(bp->pdev);
9496 static void bnxt_init_dflt_coal(struct bnxt *bp)
9498 struct bnxt_coal *coal;
9500 /* Tick values in micro seconds.
9501 * 1 coal_buf x bufs_per_record = 1 completion record.
9503 coal = &bp->rx_coal;
9504 coal->coal_ticks = 10;
9505 coal->coal_bufs = 30;
9506 coal->coal_ticks_irq = 1;
9507 coal->coal_bufs_irq = 2;
9508 coal->idle_thresh = 50;
9509 coal->bufs_per_record = 2;
9510 coal->budget = 64; /* NAPI budget */
9512 coal = &bp->tx_coal;
9513 coal->coal_ticks = 28;
9514 coal->coal_bufs = 30;
9515 coal->coal_ticks_irq = 2;
9516 coal->coal_bufs_irq = 2;
9517 coal->bufs_per_record = 1;
9519 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
9522 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
9525 struct bnxt *bp = netdev_priv(dev);
9527 SET_NETDEV_DEV(dev, &pdev->dev);
9529 /* enable device (incl. PCI PM wakeup), and bus-mastering */
9530 rc = pci_enable_device(pdev);
9532 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
9536 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
9538 "Cannot find PCI device base address, aborting\n");
9540 goto init_err_disable;
9543 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
9545 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
9546 goto init_err_disable;
9549 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
9550 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
9551 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
9552 goto init_err_disable;
9555 pci_set_master(pdev);
9560 bp->bar0 = pci_ioremap_bar(pdev, 0);
9562 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
9564 goto init_err_release;
9567 bp->bar1 = pci_ioremap_bar(pdev, 2);
9569 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
9571 goto init_err_release;
9574 bp->bar2 = pci_ioremap_bar(pdev, 4);
9576 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
9578 goto init_err_release;
9581 pci_enable_pcie_error_reporting(pdev);
9583 INIT_WORK(&bp->sp_task, bnxt_sp_task);
9585 spin_lock_init(&bp->ntp_fltr_lock);
9586 #if BITS_PER_LONG == 32
9587 spin_lock_init(&bp->db_lock);
9590 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
9591 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
9593 bnxt_init_dflt_coal(bp);
9595 timer_setup(&bp->timer, bnxt_timer, 0);
9596 bp->current_interval = BNXT_TIMER_INTERVAL;
9598 clear_bit(BNXT_STATE_OPEN, &bp->state);
9602 bnxt_unmap_bars(bp, pdev);
9603 pci_release_regions(pdev);
9606 pci_disable_device(pdev);
9612 /* rtnl_lock held */
9613 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
9615 struct sockaddr *addr = p;
9616 struct bnxt *bp = netdev_priv(dev);
9619 if (!is_valid_ether_addr(addr->sa_data))
9620 return -EADDRNOTAVAIL;
9622 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
9625 rc = bnxt_approve_mac(bp, addr->sa_data, true);
9629 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9630 if (netif_running(dev)) {
9631 bnxt_close_nic(bp, false, false);
9632 rc = bnxt_open_nic(bp, false, false);
9638 /* rtnl_lock held */
9639 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
9641 struct bnxt *bp = netdev_priv(dev);
9643 if (netif_running(dev))
9644 bnxt_close_nic(bp, false, false);
9647 bnxt_set_ring_params(bp);
9649 if (netif_running(dev))
9650 return bnxt_open_nic(bp, false, false);
9655 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
9657 struct bnxt *bp = netdev_priv(dev);
9661 if (tc > bp->max_tc) {
9662 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
9667 if (netdev_get_num_tc(dev) == tc)
9670 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
9673 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
9674 sh, tc, bp->tx_nr_rings_xdp);
9678 /* Needs to close the device and do hw resource re-allocations */
9679 if (netif_running(bp->dev))
9680 bnxt_close_nic(bp, true, false);
9683 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
9684 netdev_set_num_tc(dev, tc);
9686 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
9687 netdev_reset_tc(dev);
9689 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
9690 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
9691 bp->tx_nr_rings + bp->rx_nr_rings;
9693 if (netif_running(bp->dev))
9694 return bnxt_open_nic(bp, true, false);
9699 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
9702 struct bnxt *bp = cb_priv;
9704 if (!bnxt_tc_flower_enabled(bp) ||
9705 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
9709 case TC_SETUP_CLSFLOWER:
9710 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
9716 static int bnxt_setup_tc_block(struct net_device *dev,
9717 struct tc_block_offload *f)
9719 struct bnxt *bp = netdev_priv(dev);
9721 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9724 switch (f->command) {
9726 return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
9728 case TC_BLOCK_UNBIND:
9729 tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
9736 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
9740 case TC_SETUP_BLOCK:
9741 return bnxt_setup_tc_block(dev, type_data);
9742 case TC_SETUP_QDISC_MQPRIO: {
9743 struct tc_mqprio_qopt *mqprio = type_data;
9745 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
9747 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
9754 #ifdef CONFIG_RFS_ACCEL
9755 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
9756 struct bnxt_ntuple_filter *f2)
9758 struct flow_keys *keys1 = &f1->fkeys;
9759 struct flow_keys *keys2 = &f2->fkeys;
9761 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
9762 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
9763 keys1->ports.ports == keys2->ports.ports &&
9764 keys1->basic.ip_proto == keys2->basic.ip_proto &&
9765 keys1->basic.n_proto == keys2->basic.n_proto &&
9766 keys1->control.flags == keys2->control.flags &&
9767 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
9768 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
9774 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
9775 u16 rxq_index, u32 flow_id)
9777 struct bnxt *bp = netdev_priv(dev);
9778 struct bnxt_ntuple_filter *fltr, *new_fltr;
9779 struct flow_keys *fkeys;
9780 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
9781 int rc = 0, idx, bit_id, l2_idx = 0;
9782 struct hlist_head *head;
9784 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
9785 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
9788 netif_addr_lock_bh(dev);
9789 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
9790 if (ether_addr_equal(eth->h_dest,
9791 vnic->uc_list + off)) {
9796 netif_addr_unlock_bh(dev);
9800 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
9804 fkeys = &new_fltr->fkeys;
9805 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
9806 rc = -EPROTONOSUPPORT;
9810 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
9811 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
9812 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
9813 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
9814 rc = -EPROTONOSUPPORT;
9817 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
9818 bp->hwrm_spec_code < 0x10601) {
9819 rc = -EPROTONOSUPPORT;
9822 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
9823 bp->hwrm_spec_code < 0x10601) {
9824 rc = -EPROTONOSUPPORT;
9828 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
9829 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
9831 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
9832 head = &bp->ntp_fltr_hash_tbl[idx];
9834 hlist_for_each_entry_rcu(fltr, head, hash) {
9835 if (bnxt_fltr_match(fltr, new_fltr)) {
9843 spin_lock_bh(&bp->ntp_fltr_lock);
9844 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
9845 BNXT_NTP_FLTR_MAX_FLTR, 0);
9847 spin_unlock_bh(&bp->ntp_fltr_lock);
9852 new_fltr->sw_id = (u16)bit_id;
9853 new_fltr->flow_id = flow_id;
9854 new_fltr->l2_fltr_idx = l2_idx;
9855 new_fltr->rxq = rxq_index;
9856 hlist_add_head_rcu(&new_fltr->hash, head);
9857 bp->ntp_fltr_count++;
9858 spin_unlock_bh(&bp->ntp_fltr_lock);
9860 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
9861 bnxt_queue_sp_work(bp);
9863 return new_fltr->sw_id;
9870 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9874 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
9875 struct hlist_head *head;
9876 struct hlist_node *tmp;
9877 struct bnxt_ntuple_filter *fltr;
9880 head = &bp->ntp_fltr_hash_tbl[i];
9881 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
9884 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
9885 if (rps_may_expire_flow(bp->dev, fltr->rxq,
9888 bnxt_hwrm_cfa_ntuple_filter_free(bp,
9893 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
9898 set_bit(BNXT_FLTR_VALID, &fltr->state);
9902 spin_lock_bh(&bp->ntp_fltr_lock);
9903 hlist_del_rcu(&fltr->hash);
9904 bp->ntp_fltr_count--;
9905 spin_unlock_bh(&bp->ntp_fltr_lock);
9907 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
9912 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
9913 netdev_info(bp->dev, "Receive PF driver unload event!");
9918 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
9922 #endif /* CONFIG_RFS_ACCEL */
9924 static void bnxt_udp_tunnel_add(struct net_device *dev,
9925 struct udp_tunnel_info *ti)
9927 struct bnxt *bp = netdev_priv(dev);
9929 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9932 if (!netif_running(dev))
9936 case UDP_TUNNEL_TYPE_VXLAN:
9937 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
9940 bp->vxlan_port_cnt++;
9941 if (bp->vxlan_port_cnt == 1) {
9942 bp->vxlan_port = ti->port;
9943 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
9944 bnxt_queue_sp_work(bp);
9947 case UDP_TUNNEL_TYPE_GENEVE:
9948 if (bp->nge_port_cnt && bp->nge_port != ti->port)
9952 if (bp->nge_port_cnt == 1) {
9953 bp->nge_port = ti->port;
9954 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
9961 bnxt_queue_sp_work(bp);
9964 static void bnxt_udp_tunnel_del(struct net_device *dev,
9965 struct udp_tunnel_info *ti)
9967 struct bnxt *bp = netdev_priv(dev);
9969 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
9972 if (!netif_running(dev))
9976 case UDP_TUNNEL_TYPE_VXLAN:
9977 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
9979 bp->vxlan_port_cnt--;
9981 if (bp->vxlan_port_cnt != 0)
9984 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
9986 case UDP_TUNNEL_TYPE_GENEVE:
9987 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
9991 if (bp->nge_port_cnt != 0)
9994 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
10000 bnxt_queue_sp_work(bp);
10003 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
10004 struct net_device *dev, u32 filter_mask,
10007 struct bnxt *bp = netdev_priv(dev);
10009 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
10010 nlflags, filter_mask, NULL);
10013 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
10014 u16 flags, struct netlink_ext_ack *extack)
10016 struct bnxt *bp = netdev_priv(dev);
10017 struct nlattr *attr, *br_spec;
10020 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
10021 return -EOPNOTSUPP;
10023 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
10027 nla_for_each_nested(attr, br_spec, rem) {
10030 if (nla_type(attr) != IFLA_BRIDGE_MODE)
10033 if (nla_len(attr) < sizeof(mode))
10036 mode = nla_get_u16(attr);
10037 if (mode == bp->br_mode)
10040 rc = bnxt_hwrm_set_br_mode(bp, mode);
10042 bp->br_mode = mode;
10048 static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
10051 struct bnxt *bp = netdev_priv(dev);
10054 /* The PF and it's VF-reps only support the switchdev framework */
10056 return -EOPNOTSUPP;
10058 rc = snprintf(buf, len, "p%d", bp->pf.port_id);
10061 return -EOPNOTSUPP;
10065 int bnxt_get_port_parent_id(struct net_device *dev,
10066 struct netdev_phys_item_id *ppid)
10068 struct bnxt *bp = netdev_priv(dev);
10070 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
10071 return -EOPNOTSUPP;
10073 /* The PF and it's VF-reps only support the switchdev framework */
10075 return -EOPNOTSUPP;
10077 ppid->id_len = sizeof(bp->switch_id);
10078 memcpy(ppid->id, bp->switch_id, ppid->id_len);
10083 static const struct net_device_ops bnxt_netdev_ops = {
10084 .ndo_open = bnxt_open,
10085 .ndo_start_xmit = bnxt_start_xmit,
10086 .ndo_stop = bnxt_close,
10087 .ndo_get_stats64 = bnxt_get_stats64,
10088 .ndo_set_rx_mode = bnxt_set_rx_mode,
10089 .ndo_do_ioctl = bnxt_ioctl,
10090 .ndo_validate_addr = eth_validate_addr,
10091 .ndo_set_mac_address = bnxt_change_mac_addr,
10092 .ndo_change_mtu = bnxt_change_mtu,
10093 .ndo_fix_features = bnxt_fix_features,
10094 .ndo_set_features = bnxt_set_features,
10095 .ndo_tx_timeout = bnxt_tx_timeout,
10096 #ifdef CONFIG_BNXT_SRIOV
10097 .ndo_get_vf_config = bnxt_get_vf_config,
10098 .ndo_set_vf_mac = bnxt_set_vf_mac,
10099 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
10100 .ndo_set_vf_rate = bnxt_set_vf_bw,
10101 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
10102 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
10103 .ndo_set_vf_trust = bnxt_set_vf_trust,
10105 .ndo_setup_tc = bnxt_setup_tc,
10106 #ifdef CONFIG_RFS_ACCEL
10107 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
10109 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
10110 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
10111 .ndo_bpf = bnxt_xdp,
10112 .ndo_bridge_getlink = bnxt_bridge_getlink,
10113 .ndo_bridge_setlink = bnxt_bridge_setlink,
10114 .ndo_get_port_parent_id = bnxt_get_port_parent_id,
10115 .ndo_get_phys_port_name = bnxt_get_phys_port_name
10118 static void bnxt_remove_one(struct pci_dev *pdev)
10120 struct net_device *dev = pci_get_drvdata(pdev);
10121 struct bnxt *bp = netdev_priv(dev);
10124 bnxt_sriov_disable(bp);
10125 bnxt_dl_unregister(bp);
10128 pci_disable_pcie_error_reporting(pdev);
10129 unregister_netdev(dev);
10130 bnxt_shutdown_tc(bp);
10131 bnxt_cancel_sp_work(bp);
10134 bnxt_clear_int_mode(bp);
10135 bnxt_hwrm_func_drv_unrgtr(bp);
10136 bnxt_free_hwrm_resources(bp);
10137 bnxt_free_hwrm_short_cmd_req(bp);
10138 bnxt_ethtool_free(bp);
10142 bnxt_free_ctx_mem(bp);
10145 bnxt_cleanup_pci(bp);
10146 bnxt_free_port_stats(bp);
10150 static int bnxt_probe_phy(struct bnxt *bp)
10153 struct bnxt_link_info *link_info = &bp->link_info;
10155 rc = bnxt_hwrm_phy_qcaps(bp);
10157 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
10161 mutex_init(&bp->link_lock);
10163 rc = bnxt_update_link(bp, false);
10165 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
10170 /* Older firmware does not have supported_auto_speeds, so assume
10171 * that all supported speeds can be autonegotiated.
10173 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
10174 link_info->support_auto_speeds = link_info->support_speeds;
10176 /*initialize the ethool setting copy with NVM settings */
10177 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
10178 link_info->autoneg = BNXT_AUTONEG_SPEED;
10179 if (bp->hwrm_spec_code >= 0x10201) {
10180 if (link_info->auto_pause_setting &
10181 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
10182 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10184 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
10186 link_info->advertising = link_info->auto_link_speeds;
10188 link_info->req_link_speed = link_info->force_link_speed;
10189 link_info->req_duplex = link_info->duplex_setting;
10191 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
10192 link_info->req_flow_ctrl =
10193 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
10195 link_info->req_flow_ctrl = link_info->force_pause_setting;
10199 static int bnxt_get_max_irq(struct pci_dev *pdev)
10203 if (!pdev->msix_cap)
10206 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
10207 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
10210 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
10213 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10214 int max_ring_grps = 0, max_irq;
10216 *max_tx = hw_resc->max_tx_rings;
10217 *max_rx = hw_resc->max_rx_rings;
10218 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
10219 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
10220 bnxt_get_ulp_msix_num(bp),
10221 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
10222 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
10223 *max_cp = min_t(int, *max_cp, max_irq);
10224 max_ring_grps = hw_resc->max_hw_ring_grps;
10225 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
10229 if (bp->flags & BNXT_FLAG_AGG_RINGS)
10231 if (bp->flags & BNXT_FLAG_CHIP_P5) {
10232 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
10233 /* On P5 chips, max_cp output param should be available NQs */
10236 *max_rx = min_t(int, *max_rx, max_ring_grps);
10239 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
10243 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
10246 if (!rx || !tx || !cp)
10249 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
10252 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
10257 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
10258 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
10259 /* Not enough rings, try disabling agg rings. */
10260 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
10261 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
10263 /* set BNXT_FLAG_AGG_RINGS back for consistency */
10264 bp->flags |= BNXT_FLAG_AGG_RINGS;
10267 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
10268 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10269 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10270 bnxt_set_ring_params(bp);
10273 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
10274 int max_cp, max_stat, max_irq;
10276 /* Reserve minimum resources for RoCE */
10277 max_cp = bnxt_get_max_func_cp_rings(bp);
10278 max_stat = bnxt_get_max_func_stat_ctxs(bp);
10279 max_irq = bnxt_get_max_func_irqs(bp);
10280 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
10281 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
10282 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
10285 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
10286 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
10287 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
10288 max_cp = min_t(int, max_cp, max_irq);
10289 max_cp = min_t(int, max_cp, max_stat);
10290 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
10297 /* In initial default shared ring setting, each shared ring must have a
10300 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
10302 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
10303 bp->rx_nr_rings = bp->cp_nr_rings;
10304 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
10305 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10308 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
10310 int dflt_rings, max_rx_rings, max_tx_rings, rc;
10312 if (!bnxt_can_reserve_rings(bp))
10316 bp->flags |= BNXT_FLAG_SHARED_RINGS;
10317 dflt_rings = netif_get_num_default_rss_queues();
10318 /* Reduce default rings on multi-port cards so that total default
10319 * rings do not exceed CPU count.
10321 if (bp->port_count > 1) {
10323 max_t(int, num_online_cpus() / bp->port_count, 1);
10325 dflt_rings = min_t(int, dflt_rings, max_rings);
10327 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
10330 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
10331 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
10333 bnxt_trim_dflt_sh_rings(bp);
10335 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
10336 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
10338 rc = __bnxt_reserve_rings(bp);
10340 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
10341 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10343 bnxt_trim_dflt_sh_rings(bp);
10345 /* Rings may have been trimmed, re-reserve the trimmed rings. */
10346 if (bnxt_need_reserve_rings(bp)) {
10347 rc = __bnxt_reserve_rings(bp);
10349 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
10350 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10352 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10359 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
10363 if (bp->tx_nr_rings)
10366 bnxt_ulp_irq_stop(bp);
10367 bnxt_clear_int_mode(bp);
10368 rc = bnxt_set_dflt_rings(bp, true);
10370 netdev_err(bp->dev, "Not enough rings available.\n");
10371 goto init_dflt_ring_err;
10373 rc = bnxt_init_int_mode(bp);
10375 goto init_dflt_ring_err;
10377 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10378 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
10379 bp->flags |= BNXT_FLAG_RFS;
10380 bp->dev->features |= NETIF_F_NTUPLE;
10382 init_dflt_ring_err:
10383 bnxt_ulp_irq_restart(bp, rc);
10387 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
10392 bnxt_hwrm_func_qcaps(bp);
10394 if (netif_running(bp->dev))
10395 __bnxt_close_nic(bp, true, false);
10397 bnxt_ulp_irq_stop(bp);
10398 bnxt_clear_int_mode(bp);
10399 rc = bnxt_init_int_mode(bp);
10400 bnxt_ulp_irq_restart(bp, rc);
10402 if (netif_running(bp->dev)) {
10404 dev_close(bp->dev);
10406 rc = bnxt_open_nic(bp, true, false);
10412 static int bnxt_init_mac_addr(struct bnxt *bp)
10417 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
10419 #ifdef CONFIG_BNXT_SRIOV
10420 struct bnxt_vf_info *vf = &bp->vf;
10421 bool strict_approval = true;
10423 if (is_valid_ether_addr(vf->mac_addr)) {
10424 /* overwrite netdev dev_addr with admin VF MAC */
10425 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
10426 /* Older PF driver or firmware may not approve this
10429 strict_approval = false;
10431 eth_hw_addr_random(bp->dev);
10433 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
10439 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
10441 static int version_printed;
10442 struct net_device *dev;
10446 if (pci_is_bridge(pdev))
10449 if (version_printed++ == 0)
10450 pr_info("%s", version);
10452 max_irqs = bnxt_get_max_irq(pdev);
10453 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
10457 bp = netdev_priv(dev);
10458 bnxt_set_max_func_irqs(bp, max_irqs);
10460 if (bnxt_vf_pciid(ent->driver_data))
10461 bp->flags |= BNXT_FLAG_VF;
10463 if (pdev->msix_cap)
10464 bp->flags |= BNXT_FLAG_MSIX_CAP;
10466 rc = bnxt_init_board(pdev, dev);
10468 goto init_err_free;
10470 dev->netdev_ops = &bnxt_netdev_ops;
10471 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
10472 dev->ethtool_ops = &bnxt_ethtool_ops;
10473 pci_set_drvdata(pdev, dev);
10475 rc = bnxt_alloc_hwrm_resources(bp);
10477 goto init_err_pci_clean;
10479 mutex_init(&bp->hwrm_cmd_lock);
10480 rc = bnxt_hwrm_ver_get(bp);
10482 goto init_err_pci_clean;
10484 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
10485 rc = bnxt_alloc_kong_hwrm_resources(bp);
10487 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
10490 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
10491 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
10492 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
10494 goto init_err_pci_clean;
10497 if (BNXT_CHIP_P5(bp))
10498 bp->flags |= BNXT_FLAG_CHIP_P5;
10500 rc = bnxt_hwrm_func_reset(bp);
10502 goto init_err_pci_clean;
10504 bnxt_hwrm_fw_set_time(bp);
10506 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10507 NETIF_F_TSO | NETIF_F_TSO6 |
10508 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
10509 NETIF_F_GSO_IPXIP4 |
10510 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
10511 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
10512 NETIF_F_RXCSUM | NETIF_F_GRO;
10514 if (BNXT_SUPPORTS_TPA(bp))
10515 dev->hw_features |= NETIF_F_LRO;
10517 dev->hw_enc_features =
10518 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
10519 NETIF_F_TSO | NETIF_F_TSO6 |
10520 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
10521 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
10522 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
10523 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
10524 NETIF_F_GSO_GRE_CSUM;
10525 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
10526 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
10527 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
10528 if (BNXT_SUPPORTS_TPA(bp))
10529 dev->hw_features |= NETIF_F_GRO_HW;
10530 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
10531 if (dev->features & NETIF_F_GRO_HW)
10532 dev->features &= ~NETIF_F_LRO;
10533 dev->priv_flags |= IFF_UNICAST_FLT;
10535 #ifdef CONFIG_BNXT_SRIOV
10536 init_waitqueue_head(&bp->sriov_cfg_wait);
10537 mutex_init(&bp->sriov_lock);
10539 if (BNXT_SUPPORTS_TPA(bp)) {
10540 bp->gro_func = bnxt_gro_func_5730x;
10541 if (BNXT_CHIP_P4(bp))
10542 bp->gro_func = bnxt_gro_func_5731x;
10544 if (!BNXT_CHIP_P4_PLUS(bp))
10545 bp->flags |= BNXT_FLAG_DOUBLE_DB;
10547 rc = bnxt_hwrm_func_drv_rgtr(bp);
10549 goto init_err_pci_clean;
10551 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
10553 goto init_err_pci_clean;
10555 bp->ulp_probe = bnxt_ulp_probe;
10557 rc = bnxt_hwrm_queue_qportcfg(bp);
10559 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
10562 goto init_err_pci_clean;
10564 /* Get the MAX capabilities for this function */
10565 rc = bnxt_hwrm_func_qcaps(bp);
10567 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
10570 goto init_err_pci_clean;
10572 rc = bnxt_init_mac_addr(bp);
10574 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
10575 rc = -EADDRNOTAVAIL;
10576 goto init_err_pci_clean;
10579 bnxt_hwrm_func_qcfg(bp);
10580 bnxt_hwrm_vnic_qcaps(bp);
10581 bnxt_hwrm_port_led_qcaps(bp);
10582 bnxt_ethtool_init(bp);
10585 /* MTU range: 60 - FW defined max */
10586 dev->min_mtu = ETH_ZLEN;
10587 dev->max_mtu = bp->max_mtu;
10589 rc = bnxt_probe_phy(bp);
10591 goto init_err_pci_clean;
10593 bnxt_set_rx_skb_mode(bp, false);
10594 bnxt_set_tpa_flags(bp);
10595 bnxt_set_ring_params(bp);
10596 rc = bnxt_set_dflt_rings(bp, true);
10598 netdev_err(bp->dev, "Not enough rings available.\n");
10600 goto init_err_pci_clean;
10603 /* Default RSS hash cfg. */
10604 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
10605 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
10606 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
10607 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
10608 if (BNXT_CHIP_P4(bp) && bp->hwrm_spec_code >= 0x10501) {
10609 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
10610 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
10611 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
10614 if (bnxt_rfs_supported(bp)) {
10615 dev->hw_features |= NETIF_F_NTUPLE;
10616 if (bnxt_rfs_capable(bp)) {
10617 bp->flags |= BNXT_FLAG_RFS;
10618 dev->features |= NETIF_F_NTUPLE;
10622 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
10623 bp->flags |= BNXT_FLAG_STRIP_VLAN;
10625 rc = bnxt_init_int_mode(bp);
10627 goto init_err_pci_clean;
10629 /* No TC has been set yet and rings may have been trimmed due to
10630 * limited MSIX, so we re-initialize the TX rings per TC.
10632 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
10634 bnxt_get_wol_settings(bp);
10635 if (bp->flags & BNXT_FLAG_WOL_CAP)
10636 device_set_wakeup_enable(&pdev->dev, bp->wol);
10638 device_set_wakeup_capable(&pdev->dev, false);
10640 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
10642 bnxt_hwrm_coal_params_qcaps(bp);
10647 create_singlethread_workqueue("bnxt_pf_wq");
10649 dev_err(&pdev->dev, "Unable to create workqueue.\n");
10650 goto init_err_pci_clean;
10656 rc = register_netdev(dev);
10658 goto init_err_cleanup_tc;
10661 bnxt_dl_register(bp);
10663 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
10664 board_info[ent->driver_data].name,
10665 (long)pci_resource_start(pdev, 0), dev->dev_addr);
10666 pcie_print_link_status(pdev);
10670 init_err_cleanup_tc:
10671 bnxt_shutdown_tc(bp);
10672 bnxt_clear_int_mode(bp);
10674 init_err_pci_clean:
10675 bnxt_free_hwrm_resources(bp);
10676 bnxt_free_ctx_mem(bp);
10679 bnxt_cleanup_pci(bp);
10686 static void bnxt_shutdown(struct pci_dev *pdev)
10688 struct net_device *dev = pci_get_drvdata(pdev);
10695 bp = netdev_priv(dev);
10697 goto shutdown_exit;
10699 if (netif_running(dev))
10702 bnxt_ulp_shutdown(bp);
10704 if (system_state == SYSTEM_POWER_OFF) {
10705 bnxt_clear_int_mode(bp);
10706 pci_wake_from_d3(pdev, bp->wol);
10707 pci_set_power_state(pdev, PCI_D3hot);
10714 #ifdef CONFIG_PM_SLEEP
10715 static int bnxt_suspend(struct device *device)
10717 struct pci_dev *pdev = to_pci_dev(device);
10718 struct net_device *dev = pci_get_drvdata(pdev);
10719 struct bnxt *bp = netdev_priv(dev);
10723 if (netif_running(dev)) {
10724 netif_device_detach(dev);
10725 rc = bnxt_close(dev);
10727 bnxt_hwrm_func_drv_unrgtr(bp);
10732 static int bnxt_resume(struct device *device)
10734 struct pci_dev *pdev = to_pci_dev(device);
10735 struct net_device *dev = pci_get_drvdata(pdev);
10736 struct bnxt *bp = netdev_priv(dev);
10740 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
10744 rc = bnxt_hwrm_func_reset(bp);
10749 bnxt_get_wol_settings(bp);
10750 if (netif_running(dev)) {
10751 rc = bnxt_open(dev);
10753 netif_device_attach(dev);
10761 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
10762 #define BNXT_PM_OPS (&bnxt_pm_ops)
10766 #define BNXT_PM_OPS NULL
10768 #endif /* CONFIG_PM_SLEEP */
10771 * bnxt_io_error_detected - called when PCI error is detected
10772 * @pdev: Pointer to PCI device
10773 * @state: The current pci connection state
10775 * This function is called after a PCI bus error affecting
10776 * this device has been detected.
10778 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
10779 pci_channel_state_t state)
10781 struct net_device *netdev = pci_get_drvdata(pdev);
10782 struct bnxt *bp = netdev_priv(netdev);
10784 netdev_info(netdev, "PCI I/O error detected\n");
10787 netif_device_detach(netdev);
10791 if (state == pci_channel_io_perm_failure) {
10793 return PCI_ERS_RESULT_DISCONNECT;
10796 if (netif_running(netdev))
10797 bnxt_close(netdev);
10799 pci_disable_device(pdev);
10802 /* Request a slot slot reset. */
10803 return PCI_ERS_RESULT_NEED_RESET;
10807 * bnxt_io_slot_reset - called after the pci bus has been reset.
10808 * @pdev: Pointer to PCI device
10810 * Restart the card from scratch, as if from a cold-boot.
10811 * At this point, the card has exprienced a hard reset,
10812 * followed by fixups by BIOS, and has its config space
10813 * set up identically to what it was at cold boot.
10815 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
10817 struct net_device *netdev = pci_get_drvdata(pdev);
10818 struct bnxt *bp = netdev_priv(netdev);
10820 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
10822 netdev_info(bp->dev, "PCI Slot Reset\n");
10826 if (pci_enable_device(pdev)) {
10827 dev_err(&pdev->dev,
10828 "Cannot re-enable PCI device after reset.\n");
10830 pci_set_master(pdev);
10832 err = bnxt_hwrm_func_reset(bp);
10833 if (!err && netif_running(netdev))
10834 err = bnxt_open(netdev);
10837 result = PCI_ERS_RESULT_RECOVERED;
10838 bnxt_ulp_start(bp);
10842 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
10847 return PCI_ERS_RESULT_RECOVERED;
10851 * bnxt_io_resume - called when traffic can start flowing again.
10852 * @pdev: Pointer to PCI device
10854 * This callback is called when the error recovery driver tells
10855 * us that its OK to resume normal operation.
10857 static void bnxt_io_resume(struct pci_dev *pdev)
10859 struct net_device *netdev = pci_get_drvdata(pdev);
10863 netif_device_attach(netdev);
10868 static const struct pci_error_handlers bnxt_err_handler = {
10869 .error_detected = bnxt_io_error_detected,
10870 .slot_reset = bnxt_io_slot_reset,
10871 .resume = bnxt_io_resume
10874 static struct pci_driver bnxt_pci_driver = {
10875 .name = DRV_MODULE_NAME,
10876 .id_table = bnxt_pci_tbl,
10877 .probe = bnxt_init_one,
10878 .remove = bnxt_remove_one,
10879 .shutdown = bnxt_shutdown,
10880 .driver.pm = BNXT_PM_OPS,
10881 .err_handler = &bnxt_err_handler,
10882 #if defined(CONFIG_BNXT_SRIOV)
10883 .sriov_configure = bnxt_sriov_configure,
10887 static int __init bnxt_init(void)
10890 return pci_register_driver(&bnxt_pci_driver);
10893 static void __exit bnxt_exit(void)
10895 pci_unregister_driver(&bnxt_pci_driver);
10897 destroy_workqueue(bnxt_pf_wq);
10901 module_init(bnxt_init);
10902 module_exit(bnxt_exit);