1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2019 Broadcom Limited 5 * 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. 9 */ 10 11 #include <linux/module.h> 12 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> 27 #include <linux/io.h> 28 #include <linux/irq.h> 29 #include <linux/delay.h> 30 #include <asm/byteorder.h> 31 #include <asm/page.h> 32 #include <linux/time.h> 33 #include <linux/mii.h> 34 #include <linux/mdio.h> 35 #include <linux/if.h> 36 #include <linux/if_vlan.h> 37 #include <linux/if_bridge.h> 38 #include <linux/rtc.h> 39 #include <linux/bpf.h> 40 #include <net/gro.h> 41 #include <net/ip.h> 42 #include <net/tcp.h> 43 #include <net/udp.h> 44 #include <net/checksum.h> 45 #include <net/ip6_checksum.h> 46 #include <net/udp_tunnel.h> 47 #include <linux/workqueue.h> 48 #include <linux/prefetch.h> 49 #include <linux/cache.h> 50 #include <linux/log2.h> 51 #include <linux/aer.h> 52 #include <linux/bitmap.h> 53 #include <linux/cpu_rmap.h> 54 #include <linux/cpumask.h> 55 #include <net/pkt_cls.h> 56 #include <linux/hwmon.h> 57 #include <linux/hwmon-sysfs.h> 58 #include <net/page_pool.h> 59 60 #include "bnxt_hsi.h" 61 #include "bnxt.h" 62 #include "bnxt_hwrm.h" 63 #include "bnxt_ulp.h" 64 #include "bnxt_sriov.h" 65 #include "bnxt_ethtool.h" 66 #include "bnxt_dcb.h" 67 #include "bnxt_xdp.h" 68 #include "bnxt_ptp.h" 69 #include "bnxt_vfr.h" 70 #include "bnxt_tc.h" 71 #include "bnxt_devlink.h" 72 #include "bnxt_debugfs.h" 73 74 #define BNXT_TX_TIMEOUT (5 * HZ) 75 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 76 NETIF_MSG_TX_ERR) 77 78 MODULE_LICENSE("GPL"); 79 MODULE_DESCRIPTION("Broadcom BCM573xx network driver"); 80 81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 83 #define BNXT_RX_COPY_THRESH 256 84 85 #define BNXT_TX_PUSH_THRESH 164 86 87 /* indexed by enum board_idx */ 88 static const struct { 89 char *name; 90 } board_info[] = { 91 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 92 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 93 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 94 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 95 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 96 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 97 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 98 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 99 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 100 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 101 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 102 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 103 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 104 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 105 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 106 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 108 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 109 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 110 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 111 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 112 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 113 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 114 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 115 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 116 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 117 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 118 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 119 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 120 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 121 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 122 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 123 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 124 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 125 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 126 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 127 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 128 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 129 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 130 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 131 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 132 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 133 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 134 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 135 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 136 }; 137 138 static const struct pci_device_id bnxt_pci_tbl[] = { 139 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 140 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 141 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 142 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 143 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 144 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 145 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 146 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 147 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 148 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 149 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 150 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 151 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 152 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 153 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 154 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 155 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 156 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 157 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 158 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 159 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 160 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 161 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 162 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 163 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 164 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 165 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 166 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 167 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 168 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 169 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 170 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 171 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 172 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 173 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 174 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 175 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 176 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 177 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57508_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 179 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57502_NPAR }, 180 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57508_NPAR }, 181 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 182 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57502_NPAR }, 183 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 184 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 185 #ifdef CONFIG_BNXT_SRIOV 186 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 187 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 188 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 189 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 190 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 191 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 192 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 193 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 194 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 195 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 196 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 197 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 198 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 199 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 200 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 201 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 202 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 203 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 204 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 206 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 207 #endif 208 { 0 } 209 }; 210 211 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 212 213 static const u16 bnxt_vf_req_snif[] = { 214 HWRM_FUNC_CFG, 215 HWRM_FUNC_VF_CFG, 216 HWRM_PORT_PHY_QCFG, 217 HWRM_CFA_L2_FILTER_ALLOC, 218 }; 219 220 static const u16 bnxt_async_events_arr[] = { 221 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 222 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 223 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 224 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 225 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 226 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 227 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 228 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 229 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 230 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 231 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 232 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 233 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 234 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 235 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 236 }; 237 238 static struct workqueue_struct *bnxt_pf_wq; 239 240 static bool bnxt_vf_pciid(enum board_idx idx) 241 { 242 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 243 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 244 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 245 idx == NETXTREME_E_P5_VF_HV); 246 } 247 248 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 249 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 250 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS) 251 252 #define BNXT_CP_DB_IRQ_DIS(db) \ 253 writel(DB_CP_IRQ_DIS_FLAGS, db) 254 255 #define BNXT_DB_CQ(db, idx) \ 256 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell) 257 258 #define BNXT_DB_NQ_P5(db, idx) \ 259 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), \ 260 (db)->doorbell) 261 262 #define BNXT_DB_CQ_ARM(db, idx) \ 263 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell) 264 265 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 266 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx),\ 267 (db)->doorbell) 268 269 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 270 { 271 if (bp->flags & BNXT_FLAG_CHIP_P5) 272 BNXT_DB_NQ_P5(db, idx); 273 else 274 BNXT_DB_CQ(db, idx); 275 } 276 277 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 278 { 279 if (bp->flags & BNXT_FLAG_CHIP_P5) 280 BNXT_DB_NQ_ARM_P5(db, idx); 281 else 282 BNXT_DB_CQ_ARM(db, idx); 283 } 284 285 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 286 { 287 if (bp->flags & BNXT_FLAG_CHIP_P5) 288 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 289 RING_CMP(idx), db->doorbell); 290 else 291 BNXT_DB_CQ(db, idx); 292 } 293 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, 314 }; 315 316 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 317 { 318 struct metadata_dst *md_dst = skb_metadata_dst(skb); 319 320 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 321 return 0; 322 323 return md_dst->u.port_info.port_id; 324 } 325 326 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 327 u16 prod) 328 { 329 bnxt_db_write(bp, &txr->tx_db, prod); 330 txr->kick_pending = 0; 331 } 332 333 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp, 334 struct bnxt_tx_ring_info *txr, 335 struct netdev_queue *txq) 336 { 337 netif_tx_stop_queue(txq); 338 339 /* netif_tx_stop_queue() must be done before checking 340 * tx index in bnxt_tx_avail() below, because in 341 * bnxt_tx_int(), we update tx index before checking for 342 * netif_tx_queue_stopped(). 343 */ 344 smp_mb(); 345 if (bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh) { 346 netif_tx_wake_queue(txq); 347 return false; 348 } 349 350 return true; 351 } 352 353 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 354 { 355 struct bnxt *bp = netdev_priv(dev); 356 struct tx_bd *txbd; 357 struct tx_bd_ext *txbd1; 358 struct netdev_queue *txq; 359 int i; 360 dma_addr_t mapping; 361 unsigned int length, pad = 0; 362 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 363 u16 prod, last_frag; 364 struct pci_dev *pdev = bp->pdev; 365 struct bnxt_tx_ring_info *txr; 366 struct bnxt_sw_tx_bd *tx_buf; 367 __le32 lflags = 0; 368 369 i = skb_get_queue_mapping(skb); 370 if (unlikely(i >= bp->tx_nr_rings)) { 371 dev_kfree_skb_any(skb); 372 atomic_long_inc(&dev->tx_dropped); 373 return NETDEV_TX_OK; 374 } 375 376 txq = netdev_get_tx_queue(dev, i); 377 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 378 prod = txr->tx_prod; 379 380 free_size = bnxt_tx_avail(bp, txr); 381 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 382 /* We must have raced with NAPI cleanup */ 383 if (net_ratelimit() && txr->kick_pending) 384 netif_warn(bp, tx_err, dev, 385 "bnxt: ring busy w/ flush pending!\n"); 386 if (bnxt_txr_netif_try_stop_queue(bp, txr, txq)) 387 return NETDEV_TX_BUSY; 388 } 389 390 length = skb->len; 391 len = skb_headlen(skb); 392 last_frag = skb_shinfo(skb)->nr_frags; 393 394 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)]; 395 396 txbd->tx_bd_opaque = prod; 397 398 tx_buf = &txr->tx_buf_ring[prod]; 399 tx_buf->skb = skb; 400 tx_buf->nr_frags = last_frag; 401 402 vlan_tag_flags = 0; 403 cfa_action = bnxt_xmit_get_cfa_action(skb); 404 if (skb_vlan_tag_present(skb)) { 405 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 406 skb_vlan_tag_get(skb); 407 /* Currently supports 8021Q, 8021AD vlan offloads 408 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 409 */ 410 if (skb->vlan_proto == htons(ETH_P_8021Q)) 411 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 412 } 413 414 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) { 415 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 416 417 if (ptp && ptp->tx_tstamp_en && !skb_is_gso(skb) && 418 atomic_dec_if_positive(&ptp->tx_avail) >= 0) { 419 if (!bnxt_ptp_parse(skb, &ptp->tx_seqid, 420 &ptp->tx_hdr_off)) { 421 if (vlan_tag_flags) 422 ptp->tx_hdr_off += VLAN_HLEN; 423 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 424 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 425 } else { 426 atomic_inc(&bp->ptp_cfg->tx_avail); 427 } 428 } 429 } 430 431 if (unlikely(skb->no_fcs)) 432 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 433 434 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 435 !lflags) { 436 struct tx_push_buffer *tx_push_buf = txr->tx_push; 437 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 438 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 439 void __iomem *db = txr->tx_db.doorbell; 440 void *pdata = tx_push_buf->data; 441 u64 *end; 442 int j, push_len; 443 444 /* Set COAL_NOW to be ready quickly for the next push */ 445 tx_push->tx_bd_len_flags_type = 446 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 447 TX_BD_TYPE_LONG_TX_BD | 448 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 449 TX_BD_FLAGS_COAL_NOW | 450 TX_BD_FLAGS_PACKET_END | 451 (2 << TX_BD_FLAGS_BD_CNT_SHIFT)); 452 453 if (skb->ip_summed == CHECKSUM_PARTIAL) 454 tx_push1->tx_bd_hsize_lflags = 455 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 456 else 457 tx_push1->tx_bd_hsize_lflags = 0; 458 459 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 460 tx_push1->tx_bd_cfa_action = 461 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 462 463 end = pdata + length; 464 end = PTR_ALIGN(end, 8) - 1; 465 *end = 0; 466 467 skb_copy_from_linear_data(skb, pdata, len); 468 pdata += len; 469 for (j = 0; j < last_frag; j++) { 470 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 471 void *fptr; 472 473 fptr = skb_frag_address_safe(frag); 474 if (!fptr) 475 goto normal_tx; 476 477 memcpy(pdata, fptr, skb_frag_size(frag)); 478 pdata += skb_frag_size(frag); 479 } 480 481 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 482 txbd->tx_bd_haddr = txr->data_mapping; 483 prod = NEXT_TX(prod); 484 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)]; 485 memcpy(txbd, tx_push1, sizeof(*txbd)); 486 prod = NEXT_TX(prod); 487 tx_push->doorbell = 488 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod); 489 txr->tx_prod = prod; 490 491 tx_buf->is_push = 1; 492 netdev_tx_sent_queue(txq, skb->len); 493 wmb(); /* Sync is_push and byte queue before pushing data */ 494 495 push_len = (length + sizeof(*tx_push) + 7) / 8; 496 if (push_len > 16) { 497 __iowrite64_copy(db, tx_push_buf, 16); 498 __iowrite32_copy(db + 4, tx_push_buf + 1, 499 (push_len - 16) << 1); 500 } else { 501 __iowrite64_copy(db, tx_push_buf, push_len); 502 } 503 504 goto tx_done; 505 } 506 507 normal_tx: 508 if (length < BNXT_MIN_PKT_SIZE) { 509 pad = BNXT_MIN_PKT_SIZE - length; 510 if (skb_pad(skb, pad)) 511 /* SKB already freed. */ 512 goto tx_kick_pending; 513 length = BNXT_MIN_PKT_SIZE; 514 } 515 516 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 517 518 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 519 goto tx_free; 520 521 dma_unmap_addr_set(tx_buf, mapping, mapping); 522 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 523 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT); 524 525 txbd->tx_bd_haddr = cpu_to_le64(mapping); 526 527 prod = NEXT_TX(prod); 528 txbd1 = (struct tx_bd_ext *) 529 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)]; 530 531 txbd1->tx_bd_hsize_lflags = lflags; 532 if (skb_is_gso(skb)) { 533 u32 hdr_len; 534 535 if (skb->encapsulation) 536 hdr_len = skb_inner_network_offset(skb) + 537 skb_inner_network_header_len(skb) + 538 inner_tcp_hdrlen(skb); 539 else 540 hdr_len = skb_transport_offset(skb) + 541 tcp_hdrlen(skb); 542 543 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 544 TX_BD_FLAGS_T_IPID | 545 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 546 length = skb_shinfo(skb)->gso_size; 547 txbd1->tx_bd_mss = cpu_to_le32(length); 548 length += hdr_len; 549 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 550 txbd1->tx_bd_hsize_lflags |= 551 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 552 txbd1->tx_bd_mss = 0; 553 } 554 555 length >>= 9; 556 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 557 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 558 skb->len); 559 i = 0; 560 goto tx_dma_error; 561 } 562 flags |= bnxt_lhint_arr[length]; 563 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 564 565 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 566 txbd1->tx_bd_cfa_action = 567 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 568 for (i = 0; i < last_frag; i++) { 569 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 570 571 prod = NEXT_TX(prod); 572 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)]; 573 574 len = skb_frag_size(frag); 575 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 576 DMA_TO_DEVICE); 577 578 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 579 goto tx_dma_error; 580 581 tx_buf = &txr->tx_buf_ring[prod]; 582 dma_unmap_addr_set(tx_buf, mapping, mapping); 583 584 txbd->tx_bd_haddr = cpu_to_le64(mapping); 585 586 flags = len << TX_BD_LEN_SHIFT; 587 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 588 } 589 590 flags &= ~TX_BD_LEN; 591 txbd->tx_bd_len_flags_type = 592 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 593 TX_BD_FLAGS_PACKET_END); 594 595 netdev_tx_sent_queue(txq, skb->len); 596 597 skb_tx_timestamp(skb); 598 599 /* Sync BD data before updating doorbell */ 600 wmb(); 601 602 prod = NEXT_TX(prod); 603 txr->tx_prod = prod; 604 605 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) 606 bnxt_txr_db_kick(bp, txr, prod); 607 else 608 txr->kick_pending = 1; 609 610 tx_done: 611 612 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 613 if (netdev_xmit_more() && !tx_buf->is_push) 614 bnxt_txr_db_kick(bp, txr, prod); 615 616 bnxt_txr_netif_try_stop_queue(bp, txr, txq); 617 } 618 return NETDEV_TX_OK; 619 620 tx_dma_error: 621 if (BNXT_TX_PTP_IS_SET(lflags)) 622 atomic_inc(&bp->ptp_cfg->tx_avail); 623 624 last_frag = i; 625 626 /* start back at beginning and unmap skb */ 627 prod = txr->tx_prod; 628 tx_buf = &txr->tx_buf_ring[prod]; 629 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 630 skb_headlen(skb), DMA_TO_DEVICE); 631 prod = NEXT_TX(prod); 632 633 /* unmap remaining mapped pages */ 634 for (i = 0; i < last_frag; i++) { 635 prod = NEXT_TX(prod); 636 tx_buf = &txr->tx_buf_ring[prod]; 637 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 638 skb_frag_size(&skb_shinfo(skb)->frags[i]), 639 DMA_TO_DEVICE); 640 } 641 642 tx_free: 643 dev_kfree_skb_any(skb); 644 tx_kick_pending: 645 if (txr->kick_pending) 646 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 647 txr->tx_buf_ring[txr->tx_prod].skb = NULL; 648 atomic_long_inc(&dev->tx_dropped); 649 return NETDEV_TX_OK; 650 } 651 652 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts) 653 { 654 struct bnxt_tx_ring_info *txr = bnapi->tx_ring; 655 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 656 u16 cons = txr->tx_cons; 657 struct pci_dev *pdev = bp->pdev; 658 int i; 659 unsigned int tx_bytes = 0; 660 661 for (i = 0; i < nr_pkts; i++) { 662 struct bnxt_sw_tx_bd *tx_buf; 663 bool compl_deferred = false; 664 struct sk_buff *skb; 665 int j, last; 666 667 tx_buf = &txr->tx_buf_ring[cons]; 668 cons = NEXT_TX(cons); 669 skb = tx_buf->skb; 670 tx_buf->skb = NULL; 671 672 if (tx_buf->is_push) { 673 tx_buf->is_push = 0; 674 goto next_tx_int; 675 } 676 677 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 678 skb_headlen(skb), DMA_TO_DEVICE); 679 last = tx_buf->nr_frags; 680 681 for (j = 0; j < last; j++) { 682 cons = NEXT_TX(cons); 683 tx_buf = &txr->tx_buf_ring[cons]; 684 dma_unmap_page( 685 &pdev->dev, 686 dma_unmap_addr(tx_buf, mapping), 687 skb_frag_size(&skb_shinfo(skb)->frags[j]), 688 DMA_TO_DEVICE); 689 } 690 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) { 691 if (bp->flags & BNXT_FLAG_CHIP_P5) { 692 if (!bnxt_get_tx_ts_p5(bp, skb)) 693 compl_deferred = true; 694 else 695 atomic_inc(&bp->ptp_cfg->tx_avail); 696 } 697 } 698 699 next_tx_int: 700 cons = NEXT_TX(cons); 701 702 tx_bytes += skb->len; 703 if (!compl_deferred) 704 dev_kfree_skb_any(skb); 705 } 706 707 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes); 708 txr->tx_cons = cons; 709 710 /* Need to make the tx_cons update visible to bnxt_start_xmit() 711 * before checking for netif_tx_queue_stopped(). Without the 712 * memory barrier, there is a small possibility that bnxt_start_xmit() 713 * will miss it and cause the queue to be stopped forever. 714 */ 715 smp_mb(); 716 717 if (unlikely(netif_tx_queue_stopped(txq)) && 718 bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh && 719 READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING) 720 netif_tx_wake_queue(txq); 721 } 722 723 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 724 struct bnxt_rx_ring_info *rxr, 725 gfp_t gfp) 726 { 727 struct device *dev = &bp->pdev->dev; 728 struct page *page; 729 730 page = page_pool_dev_alloc_pages(rxr->page_pool); 731 if (!page) 732 return NULL; 733 734 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir, 735 DMA_ATTR_WEAK_ORDERING); 736 if (dma_mapping_error(dev, *mapping)) { 737 page_pool_recycle_direct(rxr->page_pool, page); 738 return NULL; 739 } 740 *mapping += bp->rx_dma_offset; 741 return page; 742 } 743 744 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 745 gfp_t gfp) 746 { 747 u8 *data; 748 struct pci_dev *pdev = bp->pdev; 749 750 if (gfp == GFP_ATOMIC) 751 data = napi_alloc_frag(bp->rx_buf_size); 752 else 753 data = netdev_alloc_frag(bp->rx_buf_size); 754 if (!data) 755 return NULL; 756 757 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset, 758 bp->rx_buf_use_size, bp->rx_dir, 759 DMA_ATTR_WEAK_ORDERING); 760 761 if (dma_mapping_error(&pdev->dev, *mapping)) { 762 skb_free_frag(data); 763 data = NULL; 764 } 765 return data; 766 } 767 768 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 769 u16 prod, gfp_t gfp) 770 { 771 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)]; 772 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod]; 773 dma_addr_t mapping; 774 775 if (BNXT_RX_PAGE_MODE(bp)) { 776 struct page *page = 777 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp); 778 779 if (!page) 780 return -ENOMEM; 781 782 rx_buf->data = page; 783 rx_buf->data_ptr = page_address(page) + bp->rx_offset; 784 } else { 785 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp); 786 787 if (!data) 788 return -ENOMEM; 789 790 rx_buf->data = data; 791 rx_buf->data_ptr = data + bp->rx_offset; 792 } 793 rx_buf->mapping = mapping; 794 795 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 796 return 0; 797 } 798 799 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 800 { 801 u16 prod = rxr->rx_prod; 802 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 803 struct rx_bd *cons_bd, *prod_bd; 804 805 prod_rx_buf = &rxr->rx_buf_ring[prod]; 806 cons_rx_buf = &rxr->rx_buf_ring[cons]; 807 808 prod_rx_buf->data = data; 809 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 810 811 prod_rx_buf->mapping = cons_rx_buf->mapping; 812 813 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)]; 814 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)]; 815 816 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 817 } 818 819 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 820 { 821 u16 next, max = rxr->rx_agg_bmap_size; 822 823 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 824 if (next >= max) 825 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 826 return next; 827 } 828 829 static inline int bnxt_alloc_rx_page(struct bnxt *bp, 830 struct bnxt_rx_ring_info *rxr, 831 u16 prod, gfp_t gfp) 832 { 833 struct rx_bd *rxbd = 834 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)]; 835 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 836 struct pci_dev *pdev = bp->pdev; 837 struct page *page; 838 dma_addr_t mapping; 839 u16 sw_prod = rxr->rx_sw_agg_prod; 840 unsigned int offset = 0; 841 842 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 843 page = rxr->rx_page; 844 if (!page) { 845 page = alloc_page(gfp); 846 if (!page) 847 return -ENOMEM; 848 rxr->rx_page = page; 849 rxr->rx_page_offset = 0; 850 } 851 offset = rxr->rx_page_offset; 852 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE; 853 if (rxr->rx_page_offset == PAGE_SIZE) 854 rxr->rx_page = NULL; 855 else 856 get_page(page); 857 } else { 858 page = alloc_page(gfp); 859 if (!page) 860 return -ENOMEM; 861 } 862 863 mapping = dma_map_page_attrs(&pdev->dev, page, offset, 864 BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE, 865 DMA_ATTR_WEAK_ORDERING); 866 if (dma_mapping_error(&pdev->dev, mapping)) { 867 __free_page(page); 868 return -EIO; 869 } 870 871 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 872 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 873 874 __set_bit(sw_prod, rxr->rx_agg_bmap); 875 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 876 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod); 877 878 rx_agg_buf->page = page; 879 rx_agg_buf->offset = offset; 880 rx_agg_buf->mapping = mapping; 881 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 882 rxbd->rx_bd_opaque = sw_prod; 883 return 0; 884 } 885 886 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 887 struct bnxt_cp_ring_info *cpr, 888 u16 cp_cons, u16 curr) 889 { 890 struct rx_agg_cmp *agg; 891 892 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 893 agg = (struct rx_agg_cmp *) 894 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 895 return agg; 896 } 897 898 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 899 struct bnxt_rx_ring_info *rxr, 900 u16 agg_id, u16 curr) 901 { 902 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 903 904 return &tpa_info->agg_arr[curr]; 905 } 906 907 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 908 u16 start, u32 agg_bufs, bool tpa) 909 { 910 struct bnxt_napi *bnapi = cpr->bnapi; 911 struct bnxt *bp = bnapi->bp; 912 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 913 u16 prod = rxr->rx_agg_prod; 914 u16 sw_prod = rxr->rx_sw_agg_prod; 915 bool p5_tpa = false; 916 u32 i; 917 918 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa) 919 p5_tpa = true; 920 921 for (i = 0; i < agg_bufs; i++) { 922 u16 cons; 923 struct rx_agg_cmp *agg; 924 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 925 struct rx_bd *prod_bd; 926 struct page *page; 927 928 if (p5_tpa) 929 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 930 else 931 agg = bnxt_get_agg(bp, cpr, idx, start + i); 932 cons = agg->rx_agg_cmp_opaque; 933 __clear_bit(cons, rxr->rx_agg_bmap); 934 935 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 936 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 937 938 __set_bit(sw_prod, rxr->rx_agg_bmap); 939 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 940 cons_rx_buf = &rxr->rx_agg_ring[cons]; 941 942 /* It is possible for sw_prod to be equal to cons, so 943 * set cons_rx_buf->page to NULL first. 944 */ 945 page = cons_rx_buf->page; 946 cons_rx_buf->page = NULL; 947 prod_rx_buf->page = page; 948 prod_rx_buf->offset = cons_rx_buf->offset; 949 950 prod_rx_buf->mapping = cons_rx_buf->mapping; 951 952 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)]; 953 954 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 955 prod_bd->rx_bd_opaque = sw_prod; 956 957 prod = NEXT_RX_AGG(prod); 958 sw_prod = NEXT_RX_AGG(sw_prod); 959 } 960 rxr->rx_agg_prod = prod; 961 rxr->rx_sw_agg_prod = sw_prod; 962 } 963 964 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 965 struct bnxt_rx_ring_info *rxr, 966 u16 cons, void *data, u8 *data_ptr, 967 dma_addr_t dma_addr, 968 unsigned int offset_and_len) 969 { 970 unsigned int payload = offset_and_len >> 16; 971 unsigned int len = offset_and_len & 0xffff; 972 skb_frag_t *frag; 973 struct page *page = data; 974 u16 prod = rxr->rx_prod; 975 struct sk_buff *skb; 976 int off, err; 977 978 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 979 if (unlikely(err)) { 980 bnxt_reuse_rx_data(rxr, cons, data); 981 return NULL; 982 } 983 dma_addr -= bp->rx_dma_offset; 984 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir, 985 DMA_ATTR_WEAK_ORDERING); 986 page_pool_release_page(rxr->page_pool, page); 987 988 if (unlikely(!payload)) 989 payload = eth_get_headlen(bp->dev, data_ptr, len); 990 991 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 992 if (!skb) { 993 __free_page(page); 994 return NULL; 995 } 996 997 off = (void *)data_ptr - page_address(page); 998 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE); 999 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1000 payload + NET_IP_ALIGN); 1001 1002 frag = &skb_shinfo(skb)->frags[0]; 1003 skb_frag_size_sub(frag, payload); 1004 skb_frag_off_add(frag, payload); 1005 skb->data_len -= payload; 1006 skb->tail += payload; 1007 1008 return skb; 1009 } 1010 1011 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1012 struct bnxt_rx_ring_info *rxr, u16 cons, 1013 void *data, u8 *data_ptr, 1014 dma_addr_t dma_addr, 1015 unsigned int offset_and_len) 1016 { 1017 u16 prod = rxr->rx_prod; 1018 struct sk_buff *skb; 1019 int err; 1020 1021 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1022 if (unlikely(err)) { 1023 bnxt_reuse_rx_data(rxr, cons, data); 1024 return NULL; 1025 } 1026 1027 skb = build_skb(data, bp->rx_buf_size); 1028 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1029 bp->rx_dir, DMA_ATTR_WEAK_ORDERING); 1030 if (!skb) { 1031 skb_free_frag(data); 1032 return NULL; 1033 } 1034 1035 skb_reserve(skb, bp->rx_offset); 1036 skb_put(skb, offset_and_len & 0xffff); 1037 return skb; 1038 } 1039 1040 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, 1041 struct bnxt_cp_ring_info *cpr, 1042 struct sk_buff *skb, u16 idx, 1043 u32 agg_bufs, bool tpa) 1044 { 1045 struct bnxt_napi *bnapi = cpr->bnapi; 1046 struct pci_dev *pdev = bp->pdev; 1047 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1048 u16 prod = rxr->rx_agg_prod; 1049 bool p5_tpa = false; 1050 u32 i; 1051 1052 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa) 1053 p5_tpa = true; 1054 1055 for (i = 0; i < agg_bufs; i++) { 1056 u16 cons, frag_len; 1057 struct rx_agg_cmp *agg; 1058 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1059 struct page *page; 1060 dma_addr_t mapping; 1061 1062 if (p5_tpa) 1063 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1064 else 1065 agg = bnxt_get_agg(bp, cpr, idx, i); 1066 cons = agg->rx_agg_cmp_opaque; 1067 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1068 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1069 1070 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1071 skb_fill_page_desc(skb, i, cons_rx_buf->page, 1072 cons_rx_buf->offset, frag_len); 1073 __clear_bit(cons, rxr->rx_agg_bmap); 1074 1075 /* It is possible for bnxt_alloc_rx_page() to allocate 1076 * a sw_prod index that equals the cons index, so we 1077 * need to clear the cons entry now. 1078 */ 1079 mapping = cons_rx_buf->mapping; 1080 page = cons_rx_buf->page; 1081 cons_rx_buf->page = NULL; 1082 1083 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) { 1084 struct skb_shared_info *shinfo; 1085 unsigned int nr_frags; 1086 1087 shinfo = skb_shinfo(skb); 1088 nr_frags = --shinfo->nr_frags; 1089 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL); 1090 1091 dev_kfree_skb(skb); 1092 1093 cons_rx_buf->page = page; 1094 1095 /* Update prod since possibly some pages have been 1096 * allocated already. 1097 */ 1098 rxr->rx_agg_prod = prod; 1099 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1100 return NULL; 1101 } 1102 1103 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE, 1104 DMA_FROM_DEVICE, 1105 DMA_ATTR_WEAK_ORDERING); 1106 1107 skb->data_len += frag_len; 1108 skb->len += frag_len; 1109 skb->truesize += PAGE_SIZE; 1110 1111 prod = NEXT_RX_AGG(prod); 1112 } 1113 rxr->rx_agg_prod = prod; 1114 return skb; 1115 } 1116 1117 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1118 u8 agg_bufs, u32 *raw_cons) 1119 { 1120 u16 last; 1121 struct rx_agg_cmp *agg; 1122 1123 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1124 last = RING_CMP(*raw_cons); 1125 agg = (struct rx_agg_cmp *) 1126 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1127 return RX_AGG_CMP_VALID(agg, *raw_cons); 1128 } 1129 1130 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1131 unsigned int len, 1132 dma_addr_t mapping) 1133 { 1134 struct bnxt *bp = bnapi->bp; 1135 struct pci_dev *pdev = bp->pdev; 1136 struct sk_buff *skb; 1137 1138 skb = napi_alloc_skb(&bnapi->napi, len); 1139 if (!skb) 1140 return NULL; 1141 1142 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh, 1143 bp->rx_dir); 1144 1145 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1146 len + NET_IP_ALIGN); 1147 1148 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh, 1149 bp->rx_dir); 1150 1151 skb_put(skb, len); 1152 return skb; 1153 } 1154 1155 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1156 u32 *raw_cons, void *cmp) 1157 { 1158 struct rx_cmp *rxcmp = cmp; 1159 u32 tmp_raw_cons = *raw_cons; 1160 u8 cmp_type, agg_bufs = 0; 1161 1162 cmp_type = RX_CMP_TYPE(rxcmp); 1163 1164 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1165 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1166 RX_CMP_AGG_BUFS) >> 1167 RX_CMP_AGG_BUFS_SHIFT; 1168 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1169 struct rx_tpa_end_cmp *tpa_end = cmp; 1170 1171 if (bp->flags & BNXT_FLAG_CHIP_P5) 1172 return 0; 1173 1174 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1175 } 1176 1177 if (agg_bufs) { 1178 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1179 return -EBUSY; 1180 } 1181 *raw_cons = tmp_raw_cons; 1182 return 0; 1183 } 1184 1185 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 1186 { 1187 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 1188 return; 1189 1190 if (BNXT_PF(bp)) 1191 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 1192 else 1193 schedule_delayed_work(&bp->fw_reset_task, delay); 1194 } 1195 1196 static void bnxt_queue_sp_work(struct bnxt *bp) 1197 { 1198 if (BNXT_PF(bp)) 1199 queue_work(bnxt_pf_wq, &bp->sp_task); 1200 else 1201 schedule_work(&bp->sp_task); 1202 } 1203 1204 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 1205 { 1206 if (!rxr->bnapi->in_reset) { 1207 rxr->bnapi->in_reset = true; 1208 if (bp->flags & BNXT_FLAG_CHIP_P5) 1209 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 1210 else 1211 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 1212 bnxt_queue_sp_work(bp); 1213 } 1214 rxr->rx_next_cons = 0xffff; 1215 } 1216 1217 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1218 { 1219 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1220 u16 idx = agg_id & MAX_TPA_P5_MASK; 1221 1222 if (test_bit(idx, map->agg_idx_bmap)) 1223 idx = find_first_zero_bit(map->agg_idx_bmap, 1224 BNXT_AGG_IDX_BMAP_SIZE); 1225 __set_bit(idx, map->agg_idx_bmap); 1226 map->agg_id_tbl[agg_id] = idx; 1227 return idx; 1228 } 1229 1230 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1231 { 1232 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1233 1234 __clear_bit(idx, map->agg_idx_bmap); 1235 } 1236 1237 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1238 { 1239 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1240 1241 return map->agg_id_tbl[agg_id]; 1242 } 1243 1244 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1245 struct rx_tpa_start_cmp *tpa_start, 1246 struct rx_tpa_start_cmp_ext *tpa_start1) 1247 { 1248 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1249 struct bnxt_tpa_info *tpa_info; 1250 u16 cons, prod, agg_id; 1251 struct rx_bd *prod_bd; 1252 dma_addr_t mapping; 1253 1254 if (bp->flags & BNXT_FLAG_CHIP_P5) { 1255 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1256 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1257 } else { 1258 agg_id = TPA_START_AGG_ID(tpa_start); 1259 } 1260 cons = tpa_start->rx_tpa_start_cmp_opaque; 1261 prod = rxr->rx_prod; 1262 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1263 prod_rx_buf = &rxr->rx_buf_ring[prod]; 1264 tpa_info = &rxr->rx_tpa[agg_id]; 1265 1266 if (unlikely(cons != rxr->rx_next_cons || 1267 TPA_START_ERROR(tpa_start))) { 1268 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1269 cons, rxr->rx_next_cons, 1270 TPA_START_ERROR_CODE(tpa_start1)); 1271 bnxt_sched_reset(bp, rxr); 1272 return; 1273 } 1274 /* Store cfa_code in tpa_info to use in tpa_end 1275 * completion processing. 1276 */ 1277 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1278 prod_rx_buf->data = tpa_info->data; 1279 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1280 1281 mapping = tpa_info->mapping; 1282 prod_rx_buf->mapping = mapping; 1283 1284 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)]; 1285 1286 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1287 1288 tpa_info->data = cons_rx_buf->data; 1289 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1290 cons_rx_buf->data = NULL; 1291 tpa_info->mapping = cons_rx_buf->mapping; 1292 1293 tpa_info->len = 1294 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1295 RX_TPA_START_CMP_LEN_SHIFT; 1296 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1297 u32 hash_type = TPA_START_HASH_TYPE(tpa_start); 1298 1299 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1300 tpa_info->gso_type = SKB_GSO_TCPV4; 1301 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1302 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1)) 1303 tpa_info->gso_type = SKB_GSO_TCPV6; 1304 tpa_info->rss_hash = 1305 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1306 } else { 1307 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1308 tpa_info->gso_type = 0; 1309 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1310 } 1311 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1312 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1313 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1314 tpa_info->agg_count = 0; 1315 1316 rxr->rx_prod = NEXT_RX(prod); 1317 cons = NEXT_RX(cons); 1318 rxr->rx_next_cons = NEXT_RX(cons); 1319 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1320 1321 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1322 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1323 cons_rx_buf->data = NULL; 1324 } 1325 1326 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1327 { 1328 if (agg_bufs) 1329 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1330 } 1331 1332 #ifdef CONFIG_INET 1333 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1334 { 1335 struct udphdr *uh = NULL; 1336 1337 if (ip_proto == htons(ETH_P_IP)) { 1338 struct iphdr *iph = (struct iphdr *)skb->data; 1339 1340 if (iph->protocol == IPPROTO_UDP) 1341 uh = (struct udphdr *)(iph + 1); 1342 } else { 1343 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1344 1345 if (iph->nexthdr == IPPROTO_UDP) 1346 uh = (struct udphdr *)(iph + 1); 1347 } 1348 if (uh) { 1349 if (uh->check) 1350 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1351 else 1352 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1353 } 1354 } 1355 #endif 1356 1357 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1358 int payload_off, int tcp_ts, 1359 struct sk_buff *skb) 1360 { 1361 #ifdef CONFIG_INET 1362 struct tcphdr *th; 1363 int len, nw_off; 1364 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1365 u32 hdr_info = tpa_info->hdr_info; 1366 bool loopback = false; 1367 1368 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1369 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1370 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1371 1372 /* If the packet is an internal loopback packet, the offsets will 1373 * have an extra 4 bytes. 1374 */ 1375 if (inner_mac_off == 4) { 1376 loopback = true; 1377 } else if (inner_mac_off > 4) { 1378 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1379 ETH_HLEN - 2)); 1380 1381 /* We only support inner iPv4/ipv6. If we don't see the 1382 * correct protocol ID, it must be a loopback packet where 1383 * the offsets are off by 4. 1384 */ 1385 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1386 loopback = true; 1387 } 1388 if (loopback) { 1389 /* internal loopback packet, subtract all offsets by 4 */ 1390 inner_ip_off -= 4; 1391 inner_mac_off -= 4; 1392 outer_ip_off -= 4; 1393 } 1394 1395 nw_off = inner_ip_off - ETH_HLEN; 1396 skb_set_network_header(skb, nw_off); 1397 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1398 struct ipv6hdr *iph = ipv6_hdr(skb); 1399 1400 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1401 len = skb->len - skb_transport_offset(skb); 1402 th = tcp_hdr(skb); 1403 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1404 } else { 1405 struct iphdr *iph = ip_hdr(skb); 1406 1407 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1408 len = skb->len - skb_transport_offset(skb); 1409 th = tcp_hdr(skb); 1410 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1411 } 1412 1413 if (inner_mac_off) { /* tunnel */ 1414 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1415 ETH_HLEN - 2)); 1416 1417 bnxt_gro_tunnel(skb, proto); 1418 } 1419 #endif 1420 return skb; 1421 } 1422 1423 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1424 int payload_off, int tcp_ts, 1425 struct sk_buff *skb) 1426 { 1427 #ifdef CONFIG_INET 1428 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1429 u32 hdr_info = tpa_info->hdr_info; 1430 int iphdr_len, nw_off; 1431 1432 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1433 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1434 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1435 1436 nw_off = inner_ip_off - ETH_HLEN; 1437 skb_set_network_header(skb, nw_off); 1438 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1439 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1440 skb_set_transport_header(skb, nw_off + iphdr_len); 1441 1442 if (inner_mac_off) { /* tunnel */ 1443 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1444 ETH_HLEN - 2)); 1445 1446 bnxt_gro_tunnel(skb, proto); 1447 } 1448 #endif 1449 return skb; 1450 } 1451 1452 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1453 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1454 1455 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1456 int payload_off, int tcp_ts, 1457 struct sk_buff *skb) 1458 { 1459 #ifdef CONFIG_INET 1460 struct tcphdr *th; 1461 int len, nw_off, tcp_opt_len = 0; 1462 1463 if (tcp_ts) 1464 tcp_opt_len = 12; 1465 1466 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1467 struct iphdr *iph; 1468 1469 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1470 ETH_HLEN; 1471 skb_set_network_header(skb, nw_off); 1472 iph = ip_hdr(skb); 1473 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1474 len = skb->len - skb_transport_offset(skb); 1475 th = tcp_hdr(skb); 1476 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1477 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1478 struct ipv6hdr *iph; 1479 1480 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1481 ETH_HLEN; 1482 skb_set_network_header(skb, nw_off); 1483 iph = ipv6_hdr(skb); 1484 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1485 len = skb->len - skb_transport_offset(skb); 1486 th = tcp_hdr(skb); 1487 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1488 } else { 1489 dev_kfree_skb_any(skb); 1490 return NULL; 1491 } 1492 1493 if (nw_off) /* tunnel */ 1494 bnxt_gro_tunnel(skb, skb->protocol); 1495 #endif 1496 return skb; 1497 } 1498 1499 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1500 struct bnxt_tpa_info *tpa_info, 1501 struct rx_tpa_end_cmp *tpa_end, 1502 struct rx_tpa_end_cmp_ext *tpa_end1, 1503 struct sk_buff *skb) 1504 { 1505 #ifdef CONFIG_INET 1506 int payload_off; 1507 u16 segs; 1508 1509 segs = TPA_END_TPA_SEGS(tpa_end); 1510 if (segs == 1) 1511 return skb; 1512 1513 NAPI_GRO_CB(skb)->count = segs; 1514 skb_shinfo(skb)->gso_size = 1515 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1516 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1517 if (bp->flags & BNXT_FLAG_CHIP_P5) 1518 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1519 else 1520 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1521 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1522 if (likely(skb)) 1523 tcp_gro_complete(skb); 1524 #endif 1525 return skb; 1526 } 1527 1528 /* Given the cfa_code of a received packet determine which 1529 * netdev (vf-rep or PF) the packet is destined to. 1530 */ 1531 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1532 { 1533 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1534 1535 /* if vf-rep dev is NULL, the must belongs to the PF */ 1536 return dev ? dev : bp->dev; 1537 } 1538 1539 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1540 struct bnxt_cp_ring_info *cpr, 1541 u32 *raw_cons, 1542 struct rx_tpa_end_cmp *tpa_end, 1543 struct rx_tpa_end_cmp_ext *tpa_end1, 1544 u8 *event) 1545 { 1546 struct bnxt_napi *bnapi = cpr->bnapi; 1547 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1548 u8 *data_ptr, agg_bufs; 1549 unsigned int len; 1550 struct bnxt_tpa_info *tpa_info; 1551 dma_addr_t mapping; 1552 struct sk_buff *skb; 1553 u16 idx = 0, agg_id; 1554 void *data; 1555 bool gro; 1556 1557 if (unlikely(bnapi->in_reset)) { 1558 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1559 1560 if (rc < 0) 1561 return ERR_PTR(-EBUSY); 1562 return NULL; 1563 } 1564 1565 if (bp->flags & BNXT_FLAG_CHIP_P5) { 1566 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1567 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1568 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1569 tpa_info = &rxr->rx_tpa[agg_id]; 1570 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1571 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1572 agg_bufs, tpa_info->agg_count); 1573 agg_bufs = tpa_info->agg_count; 1574 } 1575 tpa_info->agg_count = 0; 1576 *event |= BNXT_AGG_EVENT; 1577 bnxt_free_agg_idx(rxr, agg_id); 1578 idx = agg_id; 1579 gro = !!(bp->flags & BNXT_FLAG_GRO); 1580 } else { 1581 agg_id = TPA_END_AGG_ID(tpa_end); 1582 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1583 tpa_info = &rxr->rx_tpa[agg_id]; 1584 idx = RING_CMP(*raw_cons); 1585 if (agg_bufs) { 1586 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1587 return ERR_PTR(-EBUSY); 1588 1589 *event |= BNXT_AGG_EVENT; 1590 idx = NEXT_CMP(idx); 1591 } 1592 gro = !!TPA_END_GRO(tpa_end); 1593 } 1594 data = tpa_info->data; 1595 data_ptr = tpa_info->data_ptr; 1596 prefetch(data_ptr); 1597 len = tpa_info->len; 1598 mapping = tpa_info->mapping; 1599 1600 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1601 bnxt_abort_tpa(cpr, idx, agg_bufs); 1602 if (agg_bufs > MAX_SKB_FRAGS) 1603 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1604 agg_bufs, (int)MAX_SKB_FRAGS); 1605 return NULL; 1606 } 1607 1608 if (len <= bp->rx_copy_thresh) { 1609 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1610 if (!skb) { 1611 bnxt_abort_tpa(cpr, idx, agg_bufs); 1612 cpr->sw_stats.rx.rx_oom_discards += 1; 1613 return NULL; 1614 } 1615 } else { 1616 u8 *new_data; 1617 dma_addr_t new_mapping; 1618 1619 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC); 1620 if (!new_data) { 1621 bnxt_abort_tpa(cpr, idx, agg_bufs); 1622 cpr->sw_stats.rx.rx_oom_discards += 1; 1623 return NULL; 1624 } 1625 1626 tpa_info->data = new_data; 1627 tpa_info->data_ptr = new_data + bp->rx_offset; 1628 tpa_info->mapping = new_mapping; 1629 1630 skb = build_skb(data, bp->rx_buf_size); 1631 dma_unmap_single_attrs(&bp->pdev->dev, mapping, 1632 bp->rx_buf_use_size, bp->rx_dir, 1633 DMA_ATTR_WEAK_ORDERING); 1634 1635 if (!skb) { 1636 skb_free_frag(data); 1637 bnxt_abort_tpa(cpr, idx, agg_bufs); 1638 cpr->sw_stats.rx.rx_oom_discards += 1; 1639 return NULL; 1640 } 1641 skb_reserve(skb, bp->rx_offset); 1642 skb_put(skb, len); 1643 } 1644 1645 if (agg_bufs) { 1646 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true); 1647 if (!skb) { 1648 /* Page reuse already handled by bnxt_rx_pages(). */ 1649 cpr->sw_stats.rx.rx_oom_discards += 1; 1650 return NULL; 1651 } 1652 } 1653 1654 skb->protocol = 1655 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code)); 1656 1657 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1658 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1659 1660 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) && 1661 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1662 __be16 vlan_proto = htons(tpa_info->metadata >> 1663 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1664 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1665 1666 if (eth_type_vlan(vlan_proto)) { 1667 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1668 } else { 1669 dev_kfree_skb(skb); 1670 return NULL; 1671 } 1672 } 1673 1674 skb_checksum_none_assert(skb); 1675 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1676 skb->ip_summed = CHECKSUM_UNNECESSARY; 1677 skb->csum_level = 1678 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1679 } 1680 1681 if (gro) 1682 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1683 1684 return skb; 1685 } 1686 1687 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1688 struct rx_agg_cmp *rx_agg) 1689 { 1690 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1691 struct bnxt_tpa_info *tpa_info; 1692 1693 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1694 tpa_info = &rxr->rx_tpa[agg_id]; 1695 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1696 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1697 } 1698 1699 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1700 struct sk_buff *skb) 1701 { 1702 if (skb->dev != bp->dev) { 1703 /* this packet belongs to a vf-rep */ 1704 bnxt_vf_rep_rx(bp, skb); 1705 return; 1706 } 1707 skb_record_rx_queue(skb, bnapi->index); 1708 napi_gro_receive(&bnapi->napi, skb); 1709 } 1710 1711 /* returns the following: 1712 * 1 - 1 packet successfully received 1713 * 0 - successful TPA_START, packet not completed yet 1714 * -EBUSY - completion ring does not have all the agg buffers yet 1715 * -ENOMEM - packet aborted due to out of memory 1716 * -EIO - packet aborted due to hw error indicated in BD 1717 */ 1718 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1719 u32 *raw_cons, u8 *event) 1720 { 1721 struct bnxt_napi *bnapi = cpr->bnapi; 1722 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1723 struct net_device *dev = bp->dev; 1724 struct rx_cmp *rxcmp; 1725 struct rx_cmp_ext *rxcmp1; 1726 u32 tmp_raw_cons = *raw_cons; 1727 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 1728 struct bnxt_sw_rx_bd *rx_buf; 1729 unsigned int len; 1730 u8 *data_ptr, agg_bufs, cmp_type; 1731 dma_addr_t dma_addr; 1732 struct sk_buff *skb; 1733 u32 flags, misc; 1734 void *data; 1735 int rc = 0; 1736 1737 rxcmp = (struct rx_cmp *) 1738 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1739 1740 cmp_type = RX_CMP_TYPE(rxcmp); 1741 1742 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 1743 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 1744 goto next_rx_no_prod_no_len; 1745 } 1746 1747 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 1748 cp_cons = RING_CMP(tmp_raw_cons); 1749 rxcmp1 = (struct rx_cmp_ext *) 1750 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1751 1752 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 1753 return -EBUSY; 1754 1755 /* The valid test of the entry must be done first before 1756 * reading any further. 1757 */ 1758 dma_rmb(); 1759 prod = rxr->rx_prod; 1760 1761 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) { 1762 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp, 1763 (struct rx_tpa_start_cmp_ext *)rxcmp1); 1764 1765 *event |= BNXT_RX_EVENT; 1766 goto next_rx_no_prod_no_len; 1767 1768 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1769 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 1770 (struct rx_tpa_end_cmp *)rxcmp, 1771 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 1772 1773 if (IS_ERR(skb)) 1774 return -EBUSY; 1775 1776 rc = -ENOMEM; 1777 if (likely(skb)) { 1778 bnxt_deliver_skb(bp, bnapi, skb); 1779 rc = 1; 1780 } 1781 *event |= BNXT_RX_EVENT; 1782 goto next_rx_no_prod_no_len; 1783 } 1784 1785 cons = rxcmp->rx_cmp_opaque; 1786 if (unlikely(cons != rxr->rx_next_cons)) { 1787 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 1788 1789 /* 0xffff is forced error, don't print it */ 1790 if (rxr->rx_next_cons != 0xffff) 1791 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 1792 cons, rxr->rx_next_cons); 1793 bnxt_sched_reset(bp, rxr); 1794 if (rc1) 1795 return rc1; 1796 goto next_rx_no_prod_no_len; 1797 } 1798 rx_buf = &rxr->rx_buf_ring[cons]; 1799 data = rx_buf->data; 1800 data_ptr = rx_buf->data_ptr; 1801 prefetch(data_ptr); 1802 1803 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 1804 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 1805 1806 if (agg_bufs) { 1807 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1808 return -EBUSY; 1809 1810 cp_cons = NEXT_CMP(cp_cons); 1811 *event |= BNXT_AGG_EVENT; 1812 } 1813 *event |= BNXT_RX_EVENT; 1814 1815 rx_buf->data = NULL; 1816 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 1817 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 1818 1819 bnxt_reuse_rx_data(rxr, cons, data); 1820 if (agg_bufs) 1821 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 1822 false); 1823 1824 rc = -EIO; 1825 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 1826 bnapi->cp_ring.sw_stats.rx.rx_buf_errors++; 1827 if (!(bp->flags & BNXT_FLAG_CHIP_P5) && 1828 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 1829 netdev_warn_once(bp->dev, "RX buffer error %x\n", 1830 rx_err); 1831 bnxt_sched_reset(bp, rxr); 1832 } 1833 } 1834 goto next_rx_no_len; 1835 } 1836 1837 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 1838 len = flags >> RX_CMP_LEN_SHIFT; 1839 dma_addr = rx_buf->mapping; 1840 1841 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) { 1842 rc = 1; 1843 goto next_rx; 1844 } 1845 1846 if (len <= bp->rx_copy_thresh) { 1847 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 1848 bnxt_reuse_rx_data(rxr, cons, data); 1849 if (!skb) { 1850 if (agg_bufs) 1851 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 1852 agg_bufs, false); 1853 cpr->sw_stats.rx.rx_oom_discards += 1; 1854 rc = -ENOMEM; 1855 goto next_rx; 1856 } 1857 } else { 1858 u32 payload; 1859 1860 if (rx_buf->data_ptr == data_ptr) 1861 payload = misc & RX_CMP_PAYLOAD_OFFSET; 1862 else 1863 payload = 0; 1864 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 1865 payload | len); 1866 if (!skb) { 1867 cpr->sw_stats.rx.rx_oom_discards += 1; 1868 rc = -ENOMEM; 1869 goto next_rx; 1870 } 1871 } 1872 1873 if (agg_bufs) { 1874 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false); 1875 if (!skb) { 1876 cpr->sw_stats.rx.rx_oom_discards += 1; 1877 rc = -ENOMEM; 1878 goto next_rx; 1879 } 1880 } 1881 1882 if (RX_CMP_HASH_VALID(rxcmp)) { 1883 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp); 1884 enum pkt_hash_types type = PKT_HASH_TYPE_L4; 1885 1886 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1887 if (hash_type != 1 && hash_type != 3) 1888 type = PKT_HASH_TYPE_L3; 1889 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 1890 } 1891 1892 cfa_code = RX_CMP_CFA_CODE(rxcmp1); 1893 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code)); 1894 1895 if ((rxcmp1->rx_cmp_flags2 & 1896 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) && 1897 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1898 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 1899 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1900 __be16 vlan_proto = htons(meta_data >> 1901 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1902 1903 if (eth_type_vlan(vlan_proto)) { 1904 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1905 } else { 1906 dev_kfree_skb(skb); 1907 goto next_rx; 1908 } 1909 } 1910 1911 skb_checksum_none_assert(skb); 1912 if (RX_CMP_L4_CS_OK(rxcmp1)) { 1913 if (dev->features & NETIF_F_RXCSUM) { 1914 skb->ip_summed = CHECKSUM_UNNECESSARY; 1915 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 1916 } 1917 } else { 1918 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 1919 if (dev->features & NETIF_F_RXCSUM) 1920 bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++; 1921 } 1922 } 1923 1924 if (unlikely((flags & RX_CMP_FLAGS_ITYPES_MASK) == 1925 RX_CMP_FLAGS_ITYPE_PTP_W_TS)) { 1926 if (bp->flags & BNXT_FLAG_CHIP_P5) { 1927 u32 cmpl_ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 1928 u64 ns, ts; 1929 1930 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 1931 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 1932 1933 spin_lock_bh(&ptp->ptp_lock); 1934 ns = timecounter_cyc2time(&ptp->tc, ts); 1935 spin_unlock_bh(&ptp->ptp_lock); 1936 memset(skb_hwtstamps(skb), 0, 1937 sizeof(*skb_hwtstamps(skb))); 1938 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 1939 } 1940 } 1941 } 1942 bnxt_deliver_skb(bp, bnapi, skb); 1943 rc = 1; 1944 1945 next_rx: 1946 cpr->rx_packets += 1; 1947 cpr->rx_bytes += len; 1948 1949 next_rx_no_len: 1950 rxr->rx_prod = NEXT_RX(prod); 1951 rxr->rx_next_cons = NEXT_RX(cons); 1952 1953 next_rx_no_prod_no_len: 1954 *raw_cons = tmp_raw_cons; 1955 1956 return rc; 1957 } 1958 1959 /* In netpoll mode, if we are using a combined completion ring, we need to 1960 * discard the rx packets and recycle the buffers. 1961 */ 1962 static int bnxt_force_rx_discard(struct bnxt *bp, 1963 struct bnxt_cp_ring_info *cpr, 1964 u32 *raw_cons, u8 *event) 1965 { 1966 u32 tmp_raw_cons = *raw_cons; 1967 struct rx_cmp_ext *rxcmp1; 1968 struct rx_cmp *rxcmp; 1969 u16 cp_cons; 1970 u8 cmp_type; 1971 int rc; 1972 1973 cp_cons = RING_CMP(tmp_raw_cons); 1974 rxcmp = (struct rx_cmp *) 1975 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1976 1977 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 1978 cp_cons = RING_CMP(tmp_raw_cons); 1979 rxcmp1 = (struct rx_cmp_ext *) 1980 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1981 1982 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 1983 return -EBUSY; 1984 1985 /* The valid test of the entry must be done first before 1986 * reading any further. 1987 */ 1988 dma_rmb(); 1989 cmp_type = RX_CMP_TYPE(rxcmp); 1990 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1991 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 1992 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 1993 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1994 struct rx_tpa_end_cmp_ext *tpa_end1; 1995 1996 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 1997 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 1998 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 1999 } 2000 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2001 if (rc && rc != -EBUSY) 2002 cpr->sw_stats.rx.rx_netpoll_discards += 1; 2003 return rc; 2004 } 2005 2006 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2007 { 2008 struct bnxt_fw_health *fw_health = bp->fw_health; 2009 u32 reg = fw_health->regs[reg_idx]; 2010 u32 reg_type, reg_off, val = 0; 2011 2012 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2013 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2014 switch (reg_type) { 2015 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2016 pci_read_config_dword(bp->pdev, reg_off, &val); 2017 break; 2018 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2019 reg_off = fw_health->mapped_regs[reg_idx]; 2020 fallthrough; 2021 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2022 val = readl(bp->bar0 + reg_off); 2023 break; 2024 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2025 val = readl(bp->bar1 + reg_off); 2026 break; 2027 } 2028 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2029 val &= fw_health->fw_reset_inprog_reg_mask; 2030 return val; 2031 } 2032 2033 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2034 { 2035 int i; 2036 2037 for (i = 0; i < bp->rx_nr_rings; i++) { 2038 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2039 struct bnxt_ring_grp_info *grp_info; 2040 2041 grp_info = &bp->grp_info[grp_idx]; 2042 if (grp_info->agg_fw_ring_id == ring_id) 2043 return grp_idx; 2044 } 2045 return INVALID_HW_RING_ID; 2046 } 2047 2048 static void bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2049 { 2050 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2051 2052 switch (err_type) { 2053 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2054 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2055 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2056 break; 2057 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2058 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2059 break; 2060 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2061 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2062 break; 2063 default: 2064 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2065 err_type); 2066 break; 2067 } 2068 } 2069 2070 #define BNXT_GET_EVENT_PORT(data) \ 2071 ((data) & \ 2072 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2073 2074 #define BNXT_EVENT_RING_TYPE(data2) \ 2075 ((data2) & \ 2076 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2077 2078 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2079 (BNXT_EVENT_RING_TYPE(data2) == \ 2080 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2081 2082 static int bnxt_async_event_process(struct bnxt *bp, 2083 struct hwrm_async_event_cmpl *cmpl) 2084 { 2085 u16 event_id = le16_to_cpu(cmpl->event_id); 2086 u32 data1 = le32_to_cpu(cmpl->event_data1); 2087 u32 data2 = le32_to_cpu(cmpl->event_data2); 2088 2089 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2090 event_id, data1, data2); 2091 2092 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2093 switch (event_id) { 2094 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2095 struct bnxt_link_info *link_info = &bp->link_info; 2096 2097 if (BNXT_VF(bp)) 2098 goto async_event_process_exit; 2099 2100 /* print unsupported speed warning in forced speed mode only */ 2101 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2102 (data1 & 0x20000)) { 2103 u16 fw_speed = link_info->force_link_speed; 2104 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2105 2106 if (speed != SPEED_UNKNOWN) 2107 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2108 speed); 2109 } 2110 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2111 } 2112 fallthrough; 2113 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2114 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2115 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2116 fallthrough; 2117 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2118 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2119 break; 2120 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2121 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2122 break; 2123 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2124 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2125 2126 if (BNXT_VF(bp)) 2127 break; 2128 2129 if (bp->pf.port_id != port_id) 2130 break; 2131 2132 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2133 break; 2134 } 2135 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2136 if (BNXT_PF(bp)) 2137 goto async_event_process_exit; 2138 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2139 break; 2140 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2141 char *type_str = "Solicited"; 2142 2143 if (!bp->fw_health) 2144 goto async_event_process_exit; 2145 2146 bp->fw_reset_timestamp = jiffies; 2147 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2148 if (!bp->fw_reset_min_dsecs) 2149 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2150 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2151 if (!bp->fw_reset_max_dsecs) 2152 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2153 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2154 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2155 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2156 type_str = "Fatal"; 2157 bp->fw_health->fatalities++; 2158 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2159 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2160 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2161 type_str = "Non-fatal"; 2162 bp->fw_health->survivals++; 2163 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2164 } 2165 netif_warn(bp, hw, bp->dev, 2166 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2167 type_str, data1, data2, 2168 bp->fw_reset_min_dsecs * 100, 2169 bp->fw_reset_max_dsecs * 100); 2170 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2171 break; 2172 } 2173 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2174 struct bnxt_fw_health *fw_health = bp->fw_health; 2175 char *status_desc = "healthy"; 2176 u32 status; 2177 2178 if (!fw_health) 2179 goto async_event_process_exit; 2180 2181 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2182 fw_health->enabled = false; 2183 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2184 break; 2185 } 2186 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2187 fw_health->tmr_multiplier = 2188 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2189 bp->current_interval * 10); 2190 fw_health->tmr_counter = fw_health->tmr_multiplier; 2191 if (!fw_health->enabled) 2192 fw_health->last_fw_heartbeat = 2193 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2194 fw_health->last_fw_reset_cnt = 2195 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2196 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2197 if (status != BNXT_FW_STATUS_HEALTHY) 2198 status_desc = "unhealthy"; 2199 netif_info(bp, drv, bp->dev, 2200 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2201 fw_health->primary ? "primary" : "backup", status, 2202 status_desc, fw_health->last_fw_reset_cnt); 2203 if (!fw_health->enabled) { 2204 /* Make sure tmr_counter is set and visible to 2205 * bnxt_health_check() before setting enabled to true. 2206 */ 2207 smp_wmb(); 2208 fw_health->enabled = true; 2209 } 2210 goto async_event_process_exit; 2211 } 2212 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2213 netif_notice(bp, hw, bp->dev, 2214 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2215 data1, data2); 2216 goto async_event_process_exit; 2217 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2218 struct bnxt_rx_ring_info *rxr; 2219 u16 grp_idx; 2220 2221 if (bp->flags & BNXT_FLAG_CHIP_P5) 2222 goto async_event_process_exit; 2223 2224 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2225 BNXT_EVENT_RING_TYPE(data2), data1); 2226 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2227 goto async_event_process_exit; 2228 2229 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2230 if (grp_idx == INVALID_HW_RING_ID) { 2231 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2232 data1); 2233 goto async_event_process_exit; 2234 } 2235 rxr = bp->bnapi[grp_idx]->rx_ring; 2236 bnxt_sched_reset(bp, rxr); 2237 goto async_event_process_exit; 2238 } 2239 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2240 struct bnxt_fw_health *fw_health = bp->fw_health; 2241 2242 netif_notice(bp, hw, bp->dev, 2243 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2244 data1, data2); 2245 if (fw_health) { 2246 fw_health->echo_req_data1 = data1; 2247 fw_health->echo_req_data2 = data2; 2248 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2249 break; 2250 } 2251 goto async_event_process_exit; 2252 } 2253 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2254 bnxt_ptp_pps_event(bp, data1, data2); 2255 goto async_event_process_exit; 2256 } 2257 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2258 bnxt_event_error_report(bp, data1, data2); 2259 goto async_event_process_exit; 2260 } 2261 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2262 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2263 2264 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2265 goto async_event_process_exit; 2266 } 2267 default: 2268 goto async_event_process_exit; 2269 } 2270 bnxt_queue_sp_work(bp); 2271 async_event_process_exit: 2272 bnxt_ulp_async_events(bp, cmpl); 2273 return 0; 2274 } 2275 2276 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2277 { 2278 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2279 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2280 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2281 (struct hwrm_fwd_req_cmpl *)txcmp; 2282 2283 switch (cmpl_type) { 2284 case CMPL_BASE_TYPE_HWRM_DONE: 2285 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2286 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2287 break; 2288 2289 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2290 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2291 2292 if ((vf_id < bp->pf.first_vf_id) || 2293 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2294 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2295 vf_id); 2296 return -EINVAL; 2297 } 2298 2299 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2300 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event); 2301 bnxt_queue_sp_work(bp); 2302 break; 2303 2304 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2305 bnxt_async_event_process(bp, 2306 (struct hwrm_async_event_cmpl *)txcmp); 2307 break; 2308 2309 default: 2310 break; 2311 } 2312 2313 return 0; 2314 } 2315 2316 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2317 { 2318 struct bnxt_napi *bnapi = dev_instance; 2319 struct bnxt *bp = bnapi->bp; 2320 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2321 u32 cons = RING_CMP(cpr->cp_raw_cons); 2322 2323 cpr->event_ctr++; 2324 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2325 napi_schedule(&bnapi->napi); 2326 return IRQ_HANDLED; 2327 } 2328 2329 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2330 { 2331 u32 raw_cons = cpr->cp_raw_cons; 2332 u16 cons = RING_CMP(raw_cons); 2333 struct tx_cmp *txcmp; 2334 2335 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2336 2337 return TX_CMP_VALID(txcmp, raw_cons); 2338 } 2339 2340 static irqreturn_t bnxt_inta(int irq, void *dev_instance) 2341 { 2342 struct bnxt_napi *bnapi = dev_instance; 2343 struct bnxt *bp = bnapi->bp; 2344 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2345 u32 cons = RING_CMP(cpr->cp_raw_cons); 2346 u32 int_status; 2347 2348 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2349 2350 if (!bnxt_has_work(bp, cpr)) { 2351 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS); 2352 /* return if erroneous interrupt */ 2353 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id))) 2354 return IRQ_NONE; 2355 } 2356 2357 /* disable ring IRQ */ 2358 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell); 2359 2360 /* Return here if interrupt is shared and is disabled. */ 2361 if (unlikely(atomic_read(&bp->intr_sem) != 0)) 2362 return IRQ_HANDLED; 2363 2364 napi_schedule(&bnapi->napi); 2365 return IRQ_HANDLED; 2366 } 2367 2368 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2369 int budget) 2370 { 2371 struct bnxt_napi *bnapi = cpr->bnapi; 2372 u32 raw_cons = cpr->cp_raw_cons; 2373 u32 cons; 2374 int tx_pkts = 0; 2375 int rx_pkts = 0; 2376 u8 event = 0; 2377 struct tx_cmp *txcmp; 2378 2379 cpr->has_more_work = 0; 2380 cpr->had_work_done = 1; 2381 while (1) { 2382 int rc; 2383 2384 cons = RING_CMP(raw_cons); 2385 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2386 2387 if (!TX_CMP_VALID(txcmp, raw_cons)) 2388 break; 2389 2390 /* The valid test of the entry must be done first before 2391 * reading any further. 2392 */ 2393 dma_rmb(); 2394 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) { 2395 tx_pkts++; 2396 /* return full budget so NAPI will complete. */ 2397 if (unlikely(tx_pkts >= bp->tx_wake_thresh)) { 2398 rx_pkts = budget; 2399 raw_cons = NEXT_RAW_CMP(raw_cons); 2400 if (budget) 2401 cpr->has_more_work = 1; 2402 break; 2403 } 2404 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 2405 if (likely(budget)) 2406 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 2407 else 2408 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 2409 &event); 2410 if (likely(rc >= 0)) 2411 rx_pkts += rc; 2412 /* Increment rx_pkts when rc is -ENOMEM to count towards 2413 * the NAPI budget. Otherwise, we may potentially loop 2414 * here forever if we consistently cannot allocate 2415 * buffers. 2416 */ 2417 else if (rc == -ENOMEM && budget) 2418 rx_pkts++; 2419 else if (rc == -EBUSY) /* partial completion */ 2420 break; 2421 } else if (unlikely((TX_CMP_TYPE(txcmp) == 2422 CMPL_BASE_TYPE_HWRM_DONE) || 2423 (TX_CMP_TYPE(txcmp) == 2424 CMPL_BASE_TYPE_HWRM_FWD_REQ) || 2425 (TX_CMP_TYPE(txcmp) == 2426 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) { 2427 bnxt_hwrm_handler(bp, txcmp); 2428 } 2429 raw_cons = NEXT_RAW_CMP(raw_cons); 2430 2431 if (rx_pkts && rx_pkts == budget) { 2432 cpr->has_more_work = 1; 2433 break; 2434 } 2435 } 2436 2437 if (event & BNXT_REDIRECT_EVENT) 2438 xdp_do_flush(); 2439 2440 if (event & BNXT_TX_EVENT) { 2441 struct bnxt_tx_ring_info *txr = bnapi->tx_ring; 2442 u16 prod = txr->tx_prod; 2443 2444 /* Sync BD data before updating doorbell */ 2445 wmb(); 2446 2447 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 2448 } 2449 2450 cpr->cp_raw_cons = raw_cons; 2451 bnapi->tx_pkts += tx_pkts; 2452 bnapi->events |= event; 2453 return rx_pkts; 2454 } 2455 2456 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi) 2457 { 2458 if (bnapi->tx_pkts) { 2459 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts); 2460 bnapi->tx_pkts = 0; 2461 } 2462 2463 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 2464 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2465 2466 if (bnapi->events & BNXT_AGG_EVENT) 2467 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 2468 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 2469 } 2470 bnapi->events = 0; 2471 } 2472 2473 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2474 int budget) 2475 { 2476 struct bnxt_napi *bnapi = cpr->bnapi; 2477 int rx_pkts; 2478 2479 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 2480 2481 /* ACK completion ring before freeing tx ring and producing new 2482 * buffers in rx/agg rings to prevent overflowing the completion 2483 * ring. 2484 */ 2485 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 2486 2487 __bnxt_poll_work_done(bp, bnapi); 2488 return rx_pkts; 2489 } 2490 2491 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 2492 { 2493 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 2494 struct bnxt *bp = bnapi->bp; 2495 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2496 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2497 struct tx_cmp *txcmp; 2498 struct rx_cmp_ext *rxcmp1; 2499 u32 cp_cons, tmp_raw_cons; 2500 u32 raw_cons = cpr->cp_raw_cons; 2501 u32 rx_pkts = 0; 2502 u8 event = 0; 2503 2504 while (1) { 2505 int rc; 2506 2507 cp_cons = RING_CMP(raw_cons); 2508 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2509 2510 if (!TX_CMP_VALID(txcmp, raw_cons)) 2511 break; 2512 2513 /* The valid test of the entry must be done first before 2514 * reading any further. 2515 */ 2516 dma_rmb(); 2517 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 2518 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 2519 cp_cons = RING_CMP(tmp_raw_cons); 2520 rxcmp1 = (struct rx_cmp_ext *) 2521 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2522 2523 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2524 break; 2525 2526 /* force an error to recycle the buffer */ 2527 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2528 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2529 2530 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 2531 if (likely(rc == -EIO) && budget) 2532 rx_pkts++; 2533 else if (rc == -EBUSY) /* partial completion */ 2534 break; 2535 } else if (unlikely(TX_CMP_TYPE(txcmp) == 2536 CMPL_BASE_TYPE_HWRM_DONE)) { 2537 bnxt_hwrm_handler(bp, txcmp); 2538 } else { 2539 netdev_err(bp->dev, 2540 "Invalid completion received on special ring\n"); 2541 } 2542 raw_cons = NEXT_RAW_CMP(raw_cons); 2543 2544 if (rx_pkts == budget) 2545 break; 2546 } 2547 2548 cpr->cp_raw_cons = raw_cons; 2549 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 2550 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 2551 2552 if (event & BNXT_AGG_EVENT) 2553 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 2554 2555 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 2556 napi_complete_done(napi, rx_pkts); 2557 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 2558 } 2559 return rx_pkts; 2560 } 2561 2562 static int bnxt_poll(struct napi_struct *napi, int budget) 2563 { 2564 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 2565 struct bnxt *bp = bnapi->bp; 2566 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2567 int work_done = 0; 2568 2569 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 2570 napi_complete(napi); 2571 return 0; 2572 } 2573 while (1) { 2574 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 2575 2576 if (work_done >= budget) { 2577 if (!budget) 2578 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 2579 break; 2580 } 2581 2582 if (!bnxt_has_work(bp, cpr)) { 2583 if (napi_complete_done(napi, work_done)) 2584 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 2585 break; 2586 } 2587 } 2588 if (bp->flags & BNXT_FLAG_DIM) { 2589 struct dim_sample dim_sample = {}; 2590 2591 dim_update_sample(cpr->event_ctr, 2592 cpr->rx_packets, 2593 cpr->rx_bytes, 2594 &dim_sample); 2595 net_dim(&cpr->dim, dim_sample); 2596 } 2597 return work_done; 2598 } 2599 2600 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 2601 { 2602 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2603 int i, work_done = 0; 2604 2605 for (i = 0; i < 2; i++) { 2606 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i]; 2607 2608 if (cpr2) { 2609 work_done += __bnxt_poll_work(bp, cpr2, 2610 budget - work_done); 2611 cpr->has_more_work |= cpr2->has_more_work; 2612 } 2613 } 2614 return work_done; 2615 } 2616 2617 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 2618 u64 dbr_type) 2619 { 2620 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2621 int i; 2622 2623 for (i = 0; i < 2; i++) { 2624 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i]; 2625 struct bnxt_db_info *db; 2626 2627 if (cpr2 && cpr2->had_work_done) { 2628 db = &cpr2->cp_db; 2629 bnxt_writeq(bp, db->db_key64 | dbr_type | 2630 RING_CMP(cpr2->cp_raw_cons), db->doorbell); 2631 cpr2->had_work_done = 0; 2632 } 2633 } 2634 __bnxt_poll_work_done(bp, bnapi); 2635 } 2636 2637 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 2638 { 2639 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 2640 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2641 struct bnxt_cp_ring_info *cpr_rx; 2642 u32 raw_cons = cpr->cp_raw_cons; 2643 struct bnxt *bp = bnapi->bp; 2644 struct nqe_cn *nqcmp; 2645 int work_done = 0; 2646 u32 cons; 2647 2648 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 2649 napi_complete(napi); 2650 return 0; 2651 } 2652 if (cpr->has_more_work) { 2653 cpr->has_more_work = 0; 2654 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 2655 } 2656 while (1) { 2657 cons = RING_CMP(raw_cons); 2658 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2659 2660 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 2661 if (cpr->has_more_work) 2662 break; 2663 2664 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL); 2665 cpr->cp_raw_cons = raw_cons; 2666 if (napi_complete_done(napi, work_done)) 2667 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 2668 cpr->cp_raw_cons); 2669 goto poll_done; 2670 } 2671 2672 /* The valid test of the entry must be done first before 2673 * reading any further. 2674 */ 2675 dma_rmb(); 2676 2677 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) { 2678 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 2679 struct bnxt_cp_ring_info *cpr2; 2680 2681 cpr2 = cpr->cp_ring_arr[idx]; 2682 work_done += __bnxt_poll_work(bp, cpr2, 2683 budget - work_done); 2684 cpr->has_more_work |= cpr2->has_more_work; 2685 } else { 2686 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 2687 } 2688 raw_cons = NEXT_RAW_CMP(raw_cons); 2689 } 2690 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ); 2691 if (raw_cons != cpr->cp_raw_cons) { 2692 cpr->cp_raw_cons = raw_cons; 2693 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 2694 } 2695 poll_done: 2696 cpr_rx = cpr->cp_ring_arr[BNXT_RX_HDL]; 2697 if (cpr_rx && (bp->flags & BNXT_FLAG_DIM)) { 2698 struct dim_sample dim_sample = {}; 2699 2700 dim_update_sample(cpr->event_ctr, 2701 cpr_rx->rx_packets, 2702 cpr_rx->rx_bytes, 2703 &dim_sample); 2704 net_dim(&cpr->dim, dim_sample); 2705 } 2706 return work_done; 2707 } 2708 2709 static void bnxt_free_tx_skbs(struct bnxt *bp) 2710 { 2711 int i, max_idx; 2712 struct pci_dev *pdev = bp->pdev; 2713 2714 if (!bp->tx_ring) 2715 return; 2716 2717 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 2718 for (i = 0; i < bp->tx_nr_rings; i++) { 2719 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 2720 int j; 2721 2722 if (!txr->tx_buf_ring) 2723 continue; 2724 2725 for (j = 0; j < max_idx;) { 2726 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j]; 2727 struct sk_buff *skb; 2728 int k, last; 2729 2730 if (i < bp->tx_nr_rings_xdp && 2731 tx_buf->action == XDP_REDIRECT) { 2732 dma_unmap_single(&pdev->dev, 2733 dma_unmap_addr(tx_buf, mapping), 2734 dma_unmap_len(tx_buf, len), 2735 DMA_TO_DEVICE); 2736 xdp_return_frame(tx_buf->xdpf); 2737 tx_buf->action = 0; 2738 tx_buf->xdpf = NULL; 2739 j++; 2740 continue; 2741 } 2742 2743 skb = tx_buf->skb; 2744 if (!skb) { 2745 j++; 2746 continue; 2747 } 2748 2749 tx_buf->skb = NULL; 2750 2751 if (tx_buf->is_push) { 2752 dev_kfree_skb(skb); 2753 j += 2; 2754 continue; 2755 } 2756 2757 dma_unmap_single(&pdev->dev, 2758 dma_unmap_addr(tx_buf, mapping), 2759 skb_headlen(skb), 2760 DMA_TO_DEVICE); 2761 2762 last = tx_buf->nr_frags; 2763 j += 2; 2764 for (k = 0; k < last; k++, j++) { 2765 int ring_idx = j & bp->tx_ring_mask; 2766 skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2767 2768 tx_buf = &txr->tx_buf_ring[ring_idx]; 2769 dma_unmap_page( 2770 &pdev->dev, 2771 dma_unmap_addr(tx_buf, mapping), 2772 skb_frag_size(frag), DMA_TO_DEVICE); 2773 } 2774 dev_kfree_skb(skb); 2775 } 2776 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i)); 2777 } 2778 } 2779 2780 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr) 2781 { 2782 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 2783 struct pci_dev *pdev = bp->pdev; 2784 struct bnxt_tpa_idx_map *map; 2785 int i, max_idx, max_agg_idx; 2786 2787 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 2788 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 2789 if (!rxr->rx_tpa) 2790 goto skip_rx_tpa_free; 2791 2792 for (i = 0; i < bp->max_tpa; i++) { 2793 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 2794 u8 *data = tpa_info->data; 2795 2796 if (!data) 2797 continue; 2798 2799 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping, 2800 bp->rx_buf_use_size, bp->rx_dir, 2801 DMA_ATTR_WEAK_ORDERING); 2802 2803 tpa_info->data = NULL; 2804 2805 skb_free_frag(data); 2806 } 2807 2808 skip_rx_tpa_free: 2809 if (!rxr->rx_buf_ring) 2810 goto skip_rx_buf_free; 2811 2812 for (i = 0; i < max_idx; i++) { 2813 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 2814 dma_addr_t mapping = rx_buf->mapping; 2815 void *data = rx_buf->data; 2816 2817 if (!data) 2818 continue; 2819 2820 rx_buf->data = NULL; 2821 if (BNXT_RX_PAGE_MODE(bp)) { 2822 mapping -= bp->rx_dma_offset; 2823 dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE, 2824 bp->rx_dir, 2825 DMA_ATTR_WEAK_ORDERING); 2826 page_pool_recycle_direct(rxr->page_pool, data); 2827 } else { 2828 dma_unmap_single_attrs(&pdev->dev, mapping, 2829 bp->rx_buf_use_size, bp->rx_dir, 2830 DMA_ATTR_WEAK_ORDERING); 2831 skb_free_frag(data); 2832 } 2833 } 2834 2835 skip_rx_buf_free: 2836 if (!rxr->rx_agg_ring) 2837 goto skip_rx_agg_free; 2838 2839 for (i = 0; i < max_agg_idx; i++) { 2840 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 2841 struct page *page = rx_agg_buf->page; 2842 2843 if (!page) 2844 continue; 2845 2846 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping, 2847 BNXT_RX_PAGE_SIZE, DMA_FROM_DEVICE, 2848 DMA_ATTR_WEAK_ORDERING); 2849 2850 rx_agg_buf->page = NULL; 2851 __clear_bit(i, rxr->rx_agg_bmap); 2852 2853 __free_page(page); 2854 } 2855 2856 skip_rx_agg_free: 2857 if (rxr->rx_page) { 2858 __free_page(rxr->rx_page); 2859 rxr->rx_page = NULL; 2860 } 2861 map = rxr->rx_tpa_idx_map; 2862 if (map) 2863 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 2864 } 2865 2866 static void bnxt_free_rx_skbs(struct bnxt *bp) 2867 { 2868 int i; 2869 2870 if (!bp->rx_ring) 2871 return; 2872 2873 for (i = 0; i < bp->rx_nr_rings; i++) 2874 bnxt_free_one_rx_ring_skbs(bp, i); 2875 } 2876 2877 static void bnxt_free_skbs(struct bnxt *bp) 2878 { 2879 bnxt_free_tx_skbs(bp); 2880 bnxt_free_rx_skbs(bp); 2881 } 2882 2883 static void bnxt_init_ctx_mem(struct bnxt_mem_init *mem_init, void *p, int len) 2884 { 2885 u8 init_val = mem_init->init_val; 2886 u16 offset = mem_init->offset; 2887 u8 *p2 = p; 2888 int i; 2889 2890 if (!init_val) 2891 return; 2892 if (offset == BNXT_MEM_INVALID_OFFSET) { 2893 memset(p, init_val, len); 2894 return; 2895 } 2896 for (i = 0; i < len; i += mem_init->size) 2897 *(p2 + i + offset) = init_val; 2898 } 2899 2900 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 2901 { 2902 struct pci_dev *pdev = bp->pdev; 2903 int i; 2904 2905 if (!rmem->pg_arr) 2906 goto skip_pages; 2907 2908 for (i = 0; i < rmem->nr_pages; i++) { 2909 if (!rmem->pg_arr[i]) 2910 continue; 2911 2912 dma_free_coherent(&pdev->dev, rmem->page_size, 2913 rmem->pg_arr[i], rmem->dma_arr[i]); 2914 2915 rmem->pg_arr[i] = NULL; 2916 } 2917 skip_pages: 2918 if (rmem->pg_tbl) { 2919 size_t pg_tbl_size = rmem->nr_pages * 8; 2920 2921 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 2922 pg_tbl_size = rmem->page_size; 2923 dma_free_coherent(&pdev->dev, pg_tbl_size, 2924 rmem->pg_tbl, rmem->pg_tbl_map); 2925 rmem->pg_tbl = NULL; 2926 } 2927 if (rmem->vmem_size && *rmem->vmem) { 2928 vfree(*rmem->vmem); 2929 *rmem->vmem = NULL; 2930 } 2931 } 2932 2933 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 2934 { 2935 struct pci_dev *pdev = bp->pdev; 2936 u64 valid_bit = 0; 2937 int i; 2938 2939 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 2940 valid_bit = PTU_PTE_VALID; 2941 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 2942 size_t pg_tbl_size = rmem->nr_pages * 8; 2943 2944 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 2945 pg_tbl_size = rmem->page_size; 2946 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 2947 &rmem->pg_tbl_map, 2948 GFP_KERNEL); 2949 if (!rmem->pg_tbl) 2950 return -ENOMEM; 2951 } 2952 2953 for (i = 0; i < rmem->nr_pages; i++) { 2954 u64 extra_bits = valid_bit; 2955 2956 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 2957 rmem->page_size, 2958 &rmem->dma_arr[i], 2959 GFP_KERNEL); 2960 if (!rmem->pg_arr[i]) 2961 return -ENOMEM; 2962 2963 if (rmem->mem_init) 2964 bnxt_init_ctx_mem(rmem->mem_init, rmem->pg_arr[i], 2965 rmem->page_size); 2966 if (rmem->nr_pages > 1 || rmem->depth > 0) { 2967 if (i == rmem->nr_pages - 2 && 2968 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 2969 extra_bits |= PTU_PTE_NEXT_TO_LAST; 2970 else if (i == rmem->nr_pages - 1 && 2971 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 2972 extra_bits |= PTU_PTE_LAST; 2973 rmem->pg_tbl[i] = 2974 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 2975 } 2976 } 2977 2978 if (rmem->vmem_size) { 2979 *rmem->vmem = vzalloc(rmem->vmem_size); 2980 if (!(*rmem->vmem)) 2981 return -ENOMEM; 2982 } 2983 return 0; 2984 } 2985 2986 static void bnxt_free_tpa_info(struct bnxt *bp) 2987 { 2988 int i; 2989 2990 for (i = 0; i < bp->rx_nr_rings; i++) { 2991 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 2992 2993 kfree(rxr->rx_tpa_idx_map); 2994 rxr->rx_tpa_idx_map = NULL; 2995 if (rxr->rx_tpa) { 2996 kfree(rxr->rx_tpa[0].agg_arr); 2997 rxr->rx_tpa[0].agg_arr = NULL; 2998 } 2999 kfree(rxr->rx_tpa); 3000 rxr->rx_tpa = NULL; 3001 } 3002 } 3003 3004 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3005 { 3006 int i, j, total_aggs = 0; 3007 3008 bp->max_tpa = MAX_TPA; 3009 if (bp->flags & BNXT_FLAG_CHIP_P5) { 3010 if (!bp->max_tpa_v2) 3011 return 0; 3012 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3013 total_aggs = bp->max_tpa * MAX_SKB_FRAGS; 3014 } 3015 3016 for (i = 0; i < bp->rx_nr_rings; i++) { 3017 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3018 struct rx_agg_cmp *agg; 3019 3020 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3021 GFP_KERNEL); 3022 if (!rxr->rx_tpa) 3023 return -ENOMEM; 3024 3025 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 3026 continue; 3027 agg = kcalloc(total_aggs, sizeof(*agg), GFP_KERNEL); 3028 rxr->rx_tpa[0].agg_arr = agg; 3029 if (!agg) 3030 return -ENOMEM; 3031 for (j = 1; j < bp->max_tpa; j++) 3032 rxr->rx_tpa[j].agg_arr = agg + j * MAX_SKB_FRAGS; 3033 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3034 GFP_KERNEL); 3035 if (!rxr->rx_tpa_idx_map) 3036 return -ENOMEM; 3037 } 3038 return 0; 3039 } 3040 3041 static void bnxt_free_rx_rings(struct bnxt *bp) 3042 { 3043 int i; 3044 3045 if (!bp->rx_ring) 3046 return; 3047 3048 bnxt_free_tpa_info(bp); 3049 for (i = 0; i < bp->rx_nr_rings; i++) { 3050 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3051 struct bnxt_ring_struct *ring; 3052 3053 if (rxr->xdp_prog) 3054 bpf_prog_put(rxr->xdp_prog); 3055 3056 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3057 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3058 3059 page_pool_destroy(rxr->page_pool); 3060 rxr->page_pool = NULL; 3061 3062 kfree(rxr->rx_agg_bmap); 3063 rxr->rx_agg_bmap = NULL; 3064 3065 ring = &rxr->rx_ring_struct; 3066 bnxt_free_ring(bp, &ring->ring_mem); 3067 3068 ring = &rxr->rx_agg_ring_struct; 3069 bnxt_free_ring(bp, &ring->ring_mem); 3070 } 3071 } 3072 3073 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3074 struct bnxt_rx_ring_info *rxr) 3075 { 3076 struct page_pool_params pp = { 0 }; 3077 3078 pp.pool_size = bp->rx_ring_size; 3079 pp.nid = dev_to_node(&bp->pdev->dev); 3080 pp.dev = &bp->pdev->dev; 3081 pp.dma_dir = DMA_BIDIRECTIONAL; 3082 3083 rxr->page_pool = page_pool_create(&pp); 3084 if (IS_ERR(rxr->page_pool)) { 3085 int err = PTR_ERR(rxr->page_pool); 3086 3087 rxr->page_pool = NULL; 3088 return err; 3089 } 3090 return 0; 3091 } 3092 3093 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3094 { 3095 int i, rc = 0, agg_rings = 0; 3096 3097 if (!bp->rx_ring) 3098 return -ENOMEM; 3099 3100 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3101 agg_rings = 1; 3102 3103 for (i = 0; i < bp->rx_nr_rings; i++) { 3104 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3105 struct bnxt_ring_struct *ring; 3106 3107 ring = &rxr->rx_ring_struct; 3108 3109 rc = bnxt_alloc_rx_page_pool(bp, rxr); 3110 if (rc) 3111 return rc; 3112 3113 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3114 if (rc < 0) 3115 return rc; 3116 3117 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3118 MEM_TYPE_PAGE_POOL, 3119 rxr->page_pool); 3120 if (rc) { 3121 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3122 return rc; 3123 } 3124 3125 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3126 if (rc) 3127 return rc; 3128 3129 ring->grp_idx = i; 3130 if (agg_rings) { 3131 u16 mem_size; 3132 3133 ring = &rxr->rx_agg_ring_struct; 3134 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3135 if (rc) 3136 return rc; 3137 3138 ring->grp_idx = i; 3139 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3140 mem_size = rxr->rx_agg_bmap_size / 8; 3141 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3142 if (!rxr->rx_agg_bmap) 3143 return -ENOMEM; 3144 } 3145 } 3146 if (bp->flags & BNXT_FLAG_TPA) 3147 rc = bnxt_alloc_tpa_info(bp); 3148 return rc; 3149 } 3150 3151 static void bnxt_free_tx_rings(struct bnxt *bp) 3152 { 3153 int i; 3154 struct pci_dev *pdev = bp->pdev; 3155 3156 if (!bp->tx_ring) 3157 return; 3158 3159 for (i = 0; i < bp->tx_nr_rings; i++) { 3160 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3161 struct bnxt_ring_struct *ring; 3162 3163 if (txr->tx_push) { 3164 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3165 txr->tx_push, txr->tx_push_mapping); 3166 txr->tx_push = NULL; 3167 } 3168 3169 ring = &txr->tx_ring_struct; 3170 3171 bnxt_free_ring(bp, &ring->ring_mem); 3172 } 3173 } 3174 3175 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3176 { 3177 int i, j, rc; 3178 struct pci_dev *pdev = bp->pdev; 3179 3180 bp->tx_push_size = 0; 3181 if (bp->tx_push_thresh) { 3182 int push_size; 3183 3184 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3185 bp->tx_push_thresh); 3186 3187 if (push_size > 256) { 3188 push_size = 0; 3189 bp->tx_push_thresh = 0; 3190 } 3191 3192 bp->tx_push_size = push_size; 3193 } 3194 3195 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3196 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3197 struct bnxt_ring_struct *ring; 3198 u8 qidx; 3199 3200 ring = &txr->tx_ring_struct; 3201 3202 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3203 if (rc) 3204 return rc; 3205 3206 ring->grp_idx = txr->bnapi->index; 3207 if (bp->tx_push_size) { 3208 dma_addr_t mapping; 3209 3210 /* One pre-allocated DMA buffer to backup 3211 * TX push operation 3212 */ 3213 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3214 bp->tx_push_size, 3215 &txr->tx_push_mapping, 3216 GFP_KERNEL); 3217 3218 if (!txr->tx_push) 3219 return -ENOMEM; 3220 3221 mapping = txr->tx_push_mapping + 3222 sizeof(struct tx_push_bd); 3223 txr->data_mapping = cpu_to_le64(mapping); 3224 } 3225 qidx = bp->tc_to_qidx[j]; 3226 ring->queue_id = bp->q_info[qidx].queue_id; 3227 if (i < bp->tx_nr_rings_xdp) 3228 continue; 3229 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1)) 3230 j++; 3231 } 3232 return 0; 3233 } 3234 3235 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 3236 { 3237 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 3238 3239 kfree(cpr->cp_desc_ring); 3240 cpr->cp_desc_ring = NULL; 3241 ring->ring_mem.pg_arr = NULL; 3242 kfree(cpr->cp_desc_mapping); 3243 cpr->cp_desc_mapping = NULL; 3244 ring->ring_mem.dma_arr = NULL; 3245 } 3246 3247 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 3248 { 3249 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 3250 if (!cpr->cp_desc_ring) 3251 return -ENOMEM; 3252 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 3253 GFP_KERNEL); 3254 if (!cpr->cp_desc_mapping) 3255 return -ENOMEM; 3256 return 0; 3257 } 3258 3259 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 3260 { 3261 int i; 3262 3263 if (!bp->bnapi) 3264 return; 3265 for (i = 0; i < bp->cp_nr_rings; i++) { 3266 struct bnxt_napi *bnapi = bp->bnapi[i]; 3267 3268 if (!bnapi) 3269 continue; 3270 bnxt_free_cp_arrays(&bnapi->cp_ring); 3271 } 3272 } 3273 3274 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 3275 { 3276 int i, n = bp->cp_nr_pages; 3277 3278 for (i = 0; i < bp->cp_nr_rings; i++) { 3279 struct bnxt_napi *bnapi = bp->bnapi[i]; 3280 int rc; 3281 3282 if (!bnapi) 3283 continue; 3284 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 3285 if (rc) 3286 return rc; 3287 } 3288 return 0; 3289 } 3290 3291 static void bnxt_free_cp_rings(struct bnxt *bp) 3292 { 3293 int i; 3294 3295 if (!bp->bnapi) 3296 return; 3297 3298 for (i = 0; i < bp->cp_nr_rings; i++) { 3299 struct bnxt_napi *bnapi = bp->bnapi[i]; 3300 struct bnxt_cp_ring_info *cpr; 3301 struct bnxt_ring_struct *ring; 3302 int j; 3303 3304 if (!bnapi) 3305 continue; 3306 3307 cpr = &bnapi->cp_ring; 3308 ring = &cpr->cp_ring_struct; 3309 3310 bnxt_free_ring(bp, &ring->ring_mem); 3311 3312 for (j = 0; j < 2; j++) { 3313 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j]; 3314 3315 if (cpr2) { 3316 ring = &cpr2->cp_ring_struct; 3317 bnxt_free_ring(bp, &ring->ring_mem); 3318 bnxt_free_cp_arrays(cpr2); 3319 kfree(cpr2); 3320 cpr->cp_ring_arr[j] = NULL; 3321 } 3322 } 3323 } 3324 } 3325 3326 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp) 3327 { 3328 struct bnxt_ring_mem_info *rmem; 3329 struct bnxt_ring_struct *ring; 3330 struct bnxt_cp_ring_info *cpr; 3331 int rc; 3332 3333 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL); 3334 if (!cpr) 3335 return NULL; 3336 3337 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 3338 if (rc) { 3339 bnxt_free_cp_arrays(cpr); 3340 kfree(cpr); 3341 return NULL; 3342 } 3343 ring = &cpr->cp_ring_struct; 3344 rmem = &ring->ring_mem; 3345 rmem->nr_pages = bp->cp_nr_pages; 3346 rmem->page_size = HW_CMPD_RING_SIZE; 3347 rmem->pg_arr = (void **)cpr->cp_desc_ring; 3348 rmem->dma_arr = cpr->cp_desc_mapping; 3349 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 3350 rc = bnxt_alloc_ring(bp, rmem); 3351 if (rc) { 3352 bnxt_free_ring(bp, rmem); 3353 bnxt_free_cp_arrays(cpr); 3354 kfree(cpr); 3355 cpr = NULL; 3356 } 3357 return cpr; 3358 } 3359 3360 static int bnxt_alloc_cp_rings(struct bnxt *bp) 3361 { 3362 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 3363 int i, rc, ulp_base_vec, ulp_msix; 3364 3365 ulp_msix = bnxt_get_ulp_msix_num(bp); 3366 ulp_base_vec = bnxt_get_ulp_msix_base(bp); 3367 for (i = 0; i < bp->cp_nr_rings; i++) { 3368 struct bnxt_napi *bnapi = bp->bnapi[i]; 3369 struct bnxt_cp_ring_info *cpr; 3370 struct bnxt_ring_struct *ring; 3371 3372 if (!bnapi) 3373 continue; 3374 3375 cpr = &bnapi->cp_ring; 3376 cpr->bnapi = bnapi; 3377 ring = &cpr->cp_ring_struct; 3378 3379 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3380 if (rc) 3381 return rc; 3382 3383 if (ulp_msix && i >= ulp_base_vec) 3384 ring->map_idx = i + ulp_msix; 3385 else 3386 ring->map_idx = i; 3387 3388 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 3389 continue; 3390 3391 if (i < bp->rx_nr_rings) { 3392 struct bnxt_cp_ring_info *cpr2 = 3393 bnxt_alloc_cp_sub_ring(bp); 3394 3395 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2; 3396 if (!cpr2) 3397 return -ENOMEM; 3398 cpr2->bnapi = bnapi; 3399 } 3400 if ((sh && i < bp->tx_nr_rings) || 3401 (!sh && i >= bp->rx_nr_rings)) { 3402 struct bnxt_cp_ring_info *cpr2 = 3403 bnxt_alloc_cp_sub_ring(bp); 3404 3405 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2; 3406 if (!cpr2) 3407 return -ENOMEM; 3408 cpr2->bnapi = bnapi; 3409 } 3410 } 3411 return 0; 3412 } 3413 3414 static void bnxt_init_ring_struct(struct bnxt *bp) 3415 { 3416 int i; 3417 3418 for (i = 0; i < bp->cp_nr_rings; i++) { 3419 struct bnxt_napi *bnapi = bp->bnapi[i]; 3420 struct bnxt_ring_mem_info *rmem; 3421 struct bnxt_cp_ring_info *cpr; 3422 struct bnxt_rx_ring_info *rxr; 3423 struct bnxt_tx_ring_info *txr; 3424 struct bnxt_ring_struct *ring; 3425 3426 if (!bnapi) 3427 continue; 3428 3429 cpr = &bnapi->cp_ring; 3430 ring = &cpr->cp_ring_struct; 3431 rmem = &ring->ring_mem; 3432 rmem->nr_pages = bp->cp_nr_pages; 3433 rmem->page_size = HW_CMPD_RING_SIZE; 3434 rmem->pg_arr = (void **)cpr->cp_desc_ring; 3435 rmem->dma_arr = cpr->cp_desc_mapping; 3436 rmem->vmem_size = 0; 3437 3438 rxr = bnapi->rx_ring; 3439 if (!rxr) 3440 goto skip_rx; 3441 3442 ring = &rxr->rx_ring_struct; 3443 rmem = &ring->ring_mem; 3444 rmem->nr_pages = bp->rx_nr_pages; 3445 rmem->page_size = HW_RXBD_RING_SIZE; 3446 rmem->pg_arr = (void **)rxr->rx_desc_ring; 3447 rmem->dma_arr = rxr->rx_desc_mapping; 3448 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 3449 rmem->vmem = (void **)&rxr->rx_buf_ring; 3450 3451 ring = &rxr->rx_agg_ring_struct; 3452 rmem = &ring->ring_mem; 3453 rmem->nr_pages = bp->rx_agg_nr_pages; 3454 rmem->page_size = HW_RXBD_RING_SIZE; 3455 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 3456 rmem->dma_arr = rxr->rx_agg_desc_mapping; 3457 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 3458 rmem->vmem = (void **)&rxr->rx_agg_ring; 3459 3460 skip_rx: 3461 txr = bnapi->tx_ring; 3462 if (!txr) 3463 continue; 3464 3465 ring = &txr->tx_ring_struct; 3466 rmem = &ring->ring_mem; 3467 rmem->nr_pages = bp->tx_nr_pages; 3468 rmem->page_size = HW_RXBD_RING_SIZE; 3469 rmem->pg_arr = (void **)txr->tx_desc_ring; 3470 rmem->dma_arr = txr->tx_desc_mapping; 3471 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 3472 rmem->vmem = (void **)&txr->tx_buf_ring; 3473 } 3474 } 3475 3476 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 3477 { 3478 int i; 3479 u32 prod; 3480 struct rx_bd **rx_buf_ring; 3481 3482 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 3483 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 3484 int j; 3485 struct rx_bd *rxbd; 3486 3487 rxbd = rx_buf_ring[i]; 3488 if (!rxbd) 3489 continue; 3490 3491 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 3492 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 3493 rxbd->rx_bd_opaque = prod; 3494 } 3495 } 3496 } 3497 3498 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 3499 { 3500 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 3501 struct net_device *dev = bp->dev; 3502 u32 prod; 3503 int i; 3504 3505 prod = rxr->rx_prod; 3506 for (i = 0; i < bp->rx_ring_size; i++) { 3507 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 3508 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n", 3509 ring_nr, i, bp->rx_ring_size); 3510 break; 3511 } 3512 prod = NEXT_RX(prod); 3513 } 3514 rxr->rx_prod = prod; 3515 3516 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 3517 return 0; 3518 3519 prod = rxr->rx_agg_prod; 3520 for (i = 0; i < bp->rx_agg_ring_size; i++) { 3521 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) { 3522 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n", 3523 ring_nr, i, bp->rx_ring_size); 3524 break; 3525 } 3526 prod = NEXT_RX_AGG(prod); 3527 } 3528 rxr->rx_agg_prod = prod; 3529 3530 if (rxr->rx_tpa) { 3531 dma_addr_t mapping; 3532 u8 *data; 3533 3534 for (i = 0; i < bp->max_tpa; i++) { 3535 data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL); 3536 if (!data) 3537 return -ENOMEM; 3538 3539 rxr->rx_tpa[i].data = data; 3540 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 3541 rxr->rx_tpa[i].mapping = mapping; 3542 } 3543 } 3544 return 0; 3545 } 3546 3547 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 3548 { 3549 struct bnxt_rx_ring_info *rxr; 3550 struct bnxt_ring_struct *ring; 3551 u32 type; 3552 3553 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 3554 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 3555 3556 if (NET_IP_ALIGN == 2) 3557 type |= RX_BD_FLAGS_SOP; 3558 3559 rxr = &bp->rx_ring[ring_nr]; 3560 ring = &rxr->rx_ring_struct; 3561 bnxt_init_rxbd_pages(ring, type); 3562 3563 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 3564 bpf_prog_add(bp->xdp_prog, 1); 3565 rxr->xdp_prog = bp->xdp_prog; 3566 } 3567 ring->fw_ring_id = INVALID_HW_RING_ID; 3568 3569 ring = &rxr->rx_agg_ring_struct; 3570 ring->fw_ring_id = INVALID_HW_RING_ID; 3571 3572 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 3573 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 3574 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 3575 3576 bnxt_init_rxbd_pages(ring, type); 3577 } 3578 3579 return bnxt_alloc_one_rx_ring(bp, ring_nr); 3580 } 3581 3582 static void bnxt_init_cp_rings(struct bnxt *bp) 3583 { 3584 int i, j; 3585 3586 for (i = 0; i < bp->cp_nr_rings; i++) { 3587 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 3588 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 3589 3590 ring->fw_ring_id = INVALID_HW_RING_ID; 3591 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 3592 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 3593 for (j = 0; j < 2; j++) { 3594 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j]; 3595 3596 if (!cpr2) 3597 continue; 3598 3599 ring = &cpr2->cp_ring_struct; 3600 ring->fw_ring_id = INVALID_HW_RING_ID; 3601 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 3602 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 3603 } 3604 } 3605 } 3606 3607 static int bnxt_init_rx_rings(struct bnxt *bp) 3608 { 3609 int i, rc = 0; 3610 3611 if (BNXT_RX_PAGE_MODE(bp)) { 3612 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 3613 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 3614 } else { 3615 bp->rx_offset = BNXT_RX_OFFSET; 3616 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 3617 } 3618 3619 for (i = 0; i < bp->rx_nr_rings; i++) { 3620 rc = bnxt_init_one_rx_ring(bp, i); 3621 if (rc) 3622 break; 3623 } 3624 3625 return rc; 3626 } 3627 3628 static int bnxt_init_tx_rings(struct bnxt *bp) 3629 { 3630 u16 i; 3631 3632 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 3633 BNXT_MIN_TX_DESC_CNT); 3634 3635 for (i = 0; i < bp->tx_nr_rings; i++) { 3636 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3637 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 3638 3639 ring->fw_ring_id = INVALID_HW_RING_ID; 3640 } 3641 3642 return 0; 3643 } 3644 3645 static void bnxt_free_ring_grps(struct bnxt *bp) 3646 { 3647 kfree(bp->grp_info); 3648 bp->grp_info = NULL; 3649 } 3650 3651 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 3652 { 3653 int i; 3654 3655 if (irq_re_init) { 3656 bp->grp_info = kcalloc(bp->cp_nr_rings, 3657 sizeof(struct bnxt_ring_grp_info), 3658 GFP_KERNEL); 3659 if (!bp->grp_info) 3660 return -ENOMEM; 3661 } 3662 for (i = 0; i < bp->cp_nr_rings; i++) { 3663 if (irq_re_init) 3664 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 3665 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 3666 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 3667 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 3668 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 3669 } 3670 return 0; 3671 } 3672 3673 static void bnxt_free_vnics(struct bnxt *bp) 3674 { 3675 kfree(bp->vnic_info); 3676 bp->vnic_info = NULL; 3677 bp->nr_vnics = 0; 3678 } 3679 3680 static int bnxt_alloc_vnics(struct bnxt *bp) 3681 { 3682 int num_vnics = 1; 3683 3684 #ifdef CONFIG_RFS_ACCEL 3685 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS) 3686 num_vnics += bp->rx_nr_rings; 3687 #endif 3688 3689 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 3690 num_vnics++; 3691 3692 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 3693 GFP_KERNEL); 3694 if (!bp->vnic_info) 3695 return -ENOMEM; 3696 3697 bp->nr_vnics = num_vnics; 3698 return 0; 3699 } 3700 3701 static void bnxt_init_vnics(struct bnxt *bp) 3702 { 3703 int i; 3704 3705 for (i = 0; i < bp->nr_vnics; i++) { 3706 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 3707 int j; 3708 3709 vnic->fw_vnic_id = INVALID_HW_RING_ID; 3710 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 3711 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 3712 3713 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 3714 3715 if (bp->vnic_info[i].rss_hash_key) { 3716 if (i == 0) 3717 prandom_bytes(vnic->rss_hash_key, 3718 HW_HASH_KEY_SIZE); 3719 else 3720 memcpy(vnic->rss_hash_key, 3721 bp->vnic_info[0].rss_hash_key, 3722 HW_HASH_KEY_SIZE); 3723 } 3724 } 3725 } 3726 3727 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 3728 { 3729 int pages; 3730 3731 pages = ring_size / desc_per_pg; 3732 3733 if (!pages) 3734 return 1; 3735 3736 pages++; 3737 3738 while (pages & (pages - 1)) 3739 pages++; 3740 3741 return pages; 3742 } 3743 3744 void bnxt_set_tpa_flags(struct bnxt *bp) 3745 { 3746 bp->flags &= ~BNXT_FLAG_TPA; 3747 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 3748 return; 3749 if (bp->dev->features & NETIF_F_LRO) 3750 bp->flags |= BNXT_FLAG_LRO; 3751 else if (bp->dev->features & NETIF_F_GRO_HW) 3752 bp->flags |= BNXT_FLAG_GRO; 3753 } 3754 3755 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 3756 * be set on entry. 3757 */ 3758 void bnxt_set_ring_params(struct bnxt *bp) 3759 { 3760 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 3761 u32 agg_factor = 0, agg_ring_size = 0; 3762 3763 /* 8 for CRC and VLAN */ 3764 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 3765 3766 rx_space = rx_size + NET_SKB_PAD + 3767 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 3768 3769 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH; 3770 ring_size = bp->rx_ring_size; 3771 bp->rx_agg_ring_size = 0; 3772 bp->rx_agg_nr_pages = 0; 3773 3774 if (bp->flags & BNXT_FLAG_TPA) 3775 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 3776 3777 bp->flags &= ~BNXT_FLAG_JUMBO; 3778 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 3779 u32 jumbo_factor; 3780 3781 bp->flags |= BNXT_FLAG_JUMBO; 3782 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 3783 if (jumbo_factor > agg_factor) 3784 agg_factor = jumbo_factor; 3785 } 3786 if (agg_factor) { 3787 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 3788 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 3789 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 3790 bp->rx_ring_size, ring_size); 3791 bp->rx_ring_size = ring_size; 3792 } 3793 agg_ring_size = ring_size * agg_factor; 3794 3795 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 3796 RX_DESC_CNT); 3797 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 3798 u32 tmp = agg_ring_size; 3799 3800 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 3801 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 3802 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 3803 tmp, agg_ring_size); 3804 } 3805 bp->rx_agg_ring_size = agg_ring_size; 3806 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 3807 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN); 3808 rx_space = rx_size + NET_SKB_PAD + 3809 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 3810 } 3811 3812 bp->rx_buf_use_size = rx_size; 3813 bp->rx_buf_size = rx_space; 3814 3815 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 3816 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 3817 3818 ring_size = bp->tx_ring_size; 3819 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 3820 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 3821 3822 max_rx_cmpl = bp->rx_ring_size; 3823 /* MAX TPA needs to be added because TPA_START completions are 3824 * immediately recycled, so the TPA completions are not bound by 3825 * the RX ring size. 3826 */ 3827 if (bp->flags & BNXT_FLAG_TPA) 3828 max_rx_cmpl += bp->max_tpa; 3829 /* RX and TPA completions are 32-byte, all others are 16-byte */ 3830 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 3831 bp->cp_ring_size = ring_size; 3832 3833 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 3834 if (bp->cp_nr_pages > MAX_CP_PAGES) { 3835 bp->cp_nr_pages = MAX_CP_PAGES; 3836 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 3837 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 3838 ring_size, bp->cp_ring_size); 3839 } 3840 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 3841 bp->cp_ring_mask = bp->cp_bit - 1; 3842 } 3843 3844 /* Changing allocation mode of RX rings. 3845 * TODO: Update when extending xdp_rxq_info to support allocation modes. 3846 */ 3847 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 3848 { 3849 if (page_mode) { 3850 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) 3851 return -EOPNOTSUPP; 3852 bp->dev->max_mtu = 3853 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 3854 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 3855 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE; 3856 bp->rx_dir = DMA_BIDIRECTIONAL; 3857 bp->rx_skb_func = bnxt_rx_page_skb; 3858 /* Disable LRO or GRO_HW */ 3859 netdev_update_features(bp->dev); 3860 } else { 3861 bp->dev->max_mtu = bp->max_mtu; 3862 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 3863 bp->rx_dir = DMA_FROM_DEVICE; 3864 bp->rx_skb_func = bnxt_rx_skb; 3865 } 3866 return 0; 3867 } 3868 3869 static void bnxt_free_vnic_attributes(struct bnxt *bp) 3870 { 3871 int i; 3872 struct bnxt_vnic_info *vnic; 3873 struct pci_dev *pdev = bp->pdev; 3874 3875 if (!bp->vnic_info) 3876 return; 3877 3878 for (i = 0; i < bp->nr_vnics; i++) { 3879 vnic = &bp->vnic_info[i]; 3880 3881 kfree(vnic->fw_grp_ids); 3882 vnic->fw_grp_ids = NULL; 3883 3884 kfree(vnic->uc_list); 3885 vnic->uc_list = NULL; 3886 3887 if (vnic->mc_list) { 3888 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 3889 vnic->mc_list, vnic->mc_list_mapping); 3890 vnic->mc_list = NULL; 3891 } 3892 3893 if (vnic->rss_table) { 3894 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 3895 vnic->rss_table, 3896 vnic->rss_table_dma_addr); 3897 vnic->rss_table = NULL; 3898 } 3899 3900 vnic->rss_hash_key = NULL; 3901 vnic->flags = 0; 3902 } 3903 } 3904 3905 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 3906 { 3907 int i, rc = 0, size; 3908 struct bnxt_vnic_info *vnic; 3909 struct pci_dev *pdev = bp->pdev; 3910 int max_rings; 3911 3912 for (i = 0; i < bp->nr_vnics; i++) { 3913 vnic = &bp->vnic_info[i]; 3914 3915 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 3916 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 3917 3918 if (mem_size > 0) { 3919 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 3920 if (!vnic->uc_list) { 3921 rc = -ENOMEM; 3922 goto out; 3923 } 3924 } 3925 } 3926 3927 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 3928 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 3929 vnic->mc_list = 3930 dma_alloc_coherent(&pdev->dev, 3931 vnic->mc_list_size, 3932 &vnic->mc_list_mapping, 3933 GFP_KERNEL); 3934 if (!vnic->mc_list) { 3935 rc = -ENOMEM; 3936 goto out; 3937 } 3938 } 3939 3940 if (bp->flags & BNXT_FLAG_CHIP_P5) 3941 goto vnic_skip_grps; 3942 3943 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 3944 max_rings = bp->rx_nr_rings; 3945 else 3946 max_rings = 1; 3947 3948 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 3949 if (!vnic->fw_grp_ids) { 3950 rc = -ENOMEM; 3951 goto out; 3952 } 3953 vnic_skip_grps: 3954 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) && 3955 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 3956 continue; 3957 3958 /* Allocate rss table and hash key */ 3959 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 3960 if (bp->flags & BNXT_FLAG_CHIP_P5) 3961 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 3962 3963 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 3964 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 3965 vnic->rss_table_size, 3966 &vnic->rss_table_dma_addr, 3967 GFP_KERNEL); 3968 if (!vnic->rss_table) { 3969 rc = -ENOMEM; 3970 goto out; 3971 } 3972 3973 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 3974 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 3975 } 3976 return 0; 3977 3978 out: 3979 return rc; 3980 } 3981 3982 static void bnxt_free_hwrm_resources(struct bnxt *bp) 3983 { 3984 struct bnxt_hwrm_wait_token *token; 3985 3986 dma_pool_destroy(bp->hwrm_dma_pool); 3987 bp->hwrm_dma_pool = NULL; 3988 3989 rcu_read_lock(); 3990 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 3991 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 3992 rcu_read_unlock(); 3993 } 3994 3995 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 3996 { 3997 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 3998 BNXT_HWRM_DMA_SIZE, 3999 BNXT_HWRM_DMA_ALIGN, 0); 4000 if (!bp->hwrm_dma_pool) 4001 return -ENOMEM; 4002 4003 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4004 4005 return 0; 4006 } 4007 4008 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4009 { 4010 kfree(stats->hw_masks); 4011 stats->hw_masks = NULL; 4012 kfree(stats->sw_stats); 4013 stats->sw_stats = NULL; 4014 if (stats->hw_stats) { 4015 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 4016 stats->hw_stats_map); 4017 stats->hw_stats = NULL; 4018 } 4019 } 4020 4021 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 4022 bool alloc_masks) 4023 { 4024 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 4025 &stats->hw_stats_map, GFP_KERNEL); 4026 if (!stats->hw_stats) 4027 return -ENOMEM; 4028 4029 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 4030 if (!stats->sw_stats) 4031 goto stats_mem_err; 4032 4033 if (alloc_masks) { 4034 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 4035 if (!stats->hw_masks) 4036 goto stats_mem_err; 4037 } 4038 return 0; 4039 4040 stats_mem_err: 4041 bnxt_free_stats_mem(bp, stats); 4042 return -ENOMEM; 4043 } 4044 4045 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 4046 { 4047 int i; 4048 4049 for (i = 0; i < count; i++) 4050 mask_arr[i] = mask; 4051 } 4052 4053 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 4054 { 4055 int i; 4056 4057 for (i = 0; i < count; i++) 4058 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 4059 } 4060 4061 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 4062 struct bnxt_stats_mem *stats) 4063 { 4064 struct hwrm_func_qstats_ext_output *resp; 4065 struct hwrm_func_qstats_ext_input *req; 4066 __le64 *hw_masks; 4067 int rc; 4068 4069 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 4070 !(bp->flags & BNXT_FLAG_CHIP_P5)) 4071 return -EOPNOTSUPP; 4072 4073 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 4074 if (rc) 4075 return rc; 4076 4077 req->fid = cpu_to_le16(0xffff); 4078 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4079 4080 resp = hwrm_req_hold(bp, req); 4081 rc = hwrm_req_send(bp, req); 4082 if (!rc) { 4083 hw_masks = &resp->rx_ucast_pkts; 4084 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 4085 } 4086 hwrm_req_drop(bp, req); 4087 return rc; 4088 } 4089 4090 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 4091 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 4092 4093 static void bnxt_init_stats(struct bnxt *bp) 4094 { 4095 struct bnxt_napi *bnapi = bp->bnapi[0]; 4096 struct bnxt_cp_ring_info *cpr; 4097 struct bnxt_stats_mem *stats; 4098 __le64 *rx_stats, *tx_stats; 4099 int rc, rx_count, tx_count; 4100 u64 *rx_masks, *tx_masks; 4101 u64 mask; 4102 u8 flags; 4103 4104 cpr = &bnapi->cp_ring; 4105 stats = &cpr->stats; 4106 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 4107 if (rc) { 4108 if (bp->flags & BNXT_FLAG_CHIP_P5) 4109 mask = (1ULL << 48) - 1; 4110 else 4111 mask = -1ULL; 4112 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 4113 } 4114 if (bp->flags & BNXT_FLAG_PORT_STATS) { 4115 stats = &bp->port_stats; 4116 rx_stats = stats->hw_stats; 4117 rx_masks = stats->hw_masks; 4118 rx_count = sizeof(struct rx_port_stats) / 8; 4119 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4120 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4121 tx_count = sizeof(struct tx_port_stats) / 8; 4122 4123 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 4124 rc = bnxt_hwrm_port_qstats(bp, flags); 4125 if (rc) { 4126 mask = (1ULL << 40) - 1; 4127 4128 bnxt_fill_masks(rx_masks, mask, rx_count); 4129 bnxt_fill_masks(tx_masks, mask, tx_count); 4130 } else { 4131 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4132 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 4133 bnxt_hwrm_port_qstats(bp, 0); 4134 } 4135 } 4136 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 4137 stats = &bp->rx_port_stats_ext; 4138 rx_stats = stats->hw_stats; 4139 rx_masks = stats->hw_masks; 4140 rx_count = sizeof(struct rx_port_stats_ext) / 8; 4141 stats = &bp->tx_port_stats_ext; 4142 tx_stats = stats->hw_stats; 4143 tx_masks = stats->hw_masks; 4144 tx_count = sizeof(struct tx_port_stats_ext) / 8; 4145 4146 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4147 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 4148 if (rc) { 4149 mask = (1ULL << 40) - 1; 4150 4151 bnxt_fill_masks(rx_masks, mask, rx_count); 4152 if (tx_stats) 4153 bnxt_fill_masks(tx_masks, mask, tx_count); 4154 } else { 4155 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4156 if (tx_stats) 4157 bnxt_copy_hw_masks(tx_masks, tx_stats, 4158 tx_count); 4159 bnxt_hwrm_port_qstats_ext(bp, 0); 4160 } 4161 } 4162 } 4163 4164 static void bnxt_free_port_stats(struct bnxt *bp) 4165 { 4166 bp->flags &= ~BNXT_FLAG_PORT_STATS; 4167 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 4168 4169 bnxt_free_stats_mem(bp, &bp->port_stats); 4170 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 4171 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 4172 } 4173 4174 static void bnxt_free_ring_stats(struct bnxt *bp) 4175 { 4176 int i; 4177 4178 if (!bp->bnapi) 4179 return; 4180 4181 for (i = 0; i < bp->cp_nr_rings; i++) { 4182 struct bnxt_napi *bnapi = bp->bnapi[i]; 4183 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4184 4185 bnxt_free_stats_mem(bp, &cpr->stats); 4186 } 4187 } 4188 4189 static int bnxt_alloc_stats(struct bnxt *bp) 4190 { 4191 u32 size, i; 4192 int rc; 4193 4194 size = bp->hw_ring_stats_size; 4195 4196 for (i = 0; i < bp->cp_nr_rings; i++) { 4197 struct bnxt_napi *bnapi = bp->bnapi[i]; 4198 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4199 4200 cpr->stats.len = size; 4201 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 4202 if (rc) 4203 return rc; 4204 4205 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 4206 } 4207 4208 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 4209 return 0; 4210 4211 if (bp->port_stats.hw_stats) 4212 goto alloc_ext_stats; 4213 4214 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 4215 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 4216 if (rc) 4217 return rc; 4218 4219 bp->flags |= BNXT_FLAG_PORT_STATS; 4220 4221 alloc_ext_stats: 4222 /* Display extended statistics only if FW supports it */ 4223 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 4224 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 4225 return 0; 4226 4227 if (bp->rx_port_stats_ext.hw_stats) 4228 goto alloc_tx_ext_stats; 4229 4230 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 4231 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 4232 /* Extended stats are optional */ 4233 if (rc) 4234 return 0; 4235 4236 alloc_tx_ext_stats: 4237 if (bp->tx_port_stats_ext.hw_stats) 4238 return 0; 4239 4240 if (bp->hwrm_spec_code >= 0x10902 || 4241 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 4242 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 4243 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 4244 /* Extended stats are optional */ 4245 if (rc) 4246 return 0; 4247 } 4248 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 4249 return 0; 4250 } 4251 4252 static void bnxt_clear_ring_indices(struct bnxt *bp) 4253 { 4254 int i; 4255 4256 if (!bp->bnapi) 4257 return; 4258 4259 for (i = 0; i < bp->cp_nr_rings; i++) { 4260 struct bnxt_napi *bnapi = bp->bnapi[i]; 4261 struct bnxt_cp_ring_info *cpr; 4262 struct bnxt_rx_ring_info *rxr; 4263 struct bnxt_tx_ring_info *txr; 4264 4265 if (!bnapi) 4266 continue; 4267 4268 cpr = &bnapi->cp_ring; 4269 cpr->cp_raw_cons = 0; 4270 4271 txr = bnapi->tx_ring; 4272 if (txr) { 4273 txr->tx_prod = 0; 4274 txr->tx_cons = 0; 4275 } 4276 4277 rxr = bnapi->rx_ring; 4278 if (rxr) { 4279 rxr->rx_prod = 0; 4280 rxr->rx_agg_prod = 0; 4281 rxr->rx_sw_agg_prod = 0; 4282 rxr->rx_next_cons = 0; 4283 } 4284 } 4285 } 4286 4287 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit) 4288 { 4289 #ifdef CONFIG_RFS_ACCEL 4290 int i; 4291 4292 /* Under rtnl_lock and all our NAPIs have been disabled. It's 4293 * safe to delete the hash table. 4294 */ 4295 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 4296 struct hlist_head *head; 4297 struct hlist_node *tmp; 4298 struct bnxt_ntuple_filter *fltr; 4299 4300 head = &bp->ntp_fltr_hash_tbl[i]; 4301 hlist_for_each_entry_safe(fltr, tmp, head, hash) { 4302 hlist_del(&fltr->hash); 4303 kfree(fltr); 4304 } 4305 } 4306 if (irq_reinit) { 4307 kfree(bp->ntp_fltr_bmap); 4308 bp->ntp_fltr_bmap = NULL; 4309 } 4310 bp->ntp_fltr_count = 0; 4311 #endif 4312 } 4313 4314 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 4315 { 4316 #ifdef CONFIG_RFS_ACCEL 4317 int i, rc = 0; 4318 4319 if (!(bp->flags & BNXT_FLAG_RFS)) 4320 return 0; 4321 4322 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 4323 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 4324 4325 bp->ntp_fltr_count = 0; 4326 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR), 4327 sizeof(long), 4328 GFP_KERNEL); 4329 4330 if (!bp->ntp_fltr_bmap) 4331 rc = -ENOMEM; 4332 4333 return rc; 4334 #else 4335 return 0; 4336 #endif 4337 } 4338 4339 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 4340 { 4341 bnxt_free_vnic_attributes(bp); 4342 bnxt_free_tx_rings(bp); 4343 bnxt_free_rx_rings(bp); 4344 bnxt_free_cp_rings(bp); 4345 bnxt_free_all_cp_arrays(bp); 4346 bnxt_free_ntp_fltrs(bp, irq_re_init); 4347 if (irq_re_init) { 4348 bnxt_free_ring_stats(bp); 4349 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 4350 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 4351 bnxt_free_port_stats(bp); 4352 bnxt_free_ring_grps(bp); 4353 bnxt_free_vnics(bp); 4354 kfree(bp->tx_ring_map); 4355 bp->tx_ring_map = NULL; 4356 kfree(bp->tx_ring); 4357 bp->tx_ring = NULL; 4358 kfree(bp->rx_ring); 4359 bp->rx_ring = NULL; 4360 kfree(bp->bnapi); 4361 bp->bnapi = NULL; 4362 } else { 4363 bnxt_clear_ring_indices(bp); 4364 } 4365 } 4366 4367 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 4368 { 4369 int i, j, rc, size, arr_size; 4370 void *bnapi; 4371 4372 if (irq_re_init) { 4373 /* Allocate bnapi mem pointer array and mem block for 4374 * all queues 4375 */ 4376 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 4377 bp->cp_nr_rings); 4378 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 4379 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 4380 if (!bnapi) 4381 return -ENOMEM; 4382 4383 bp->bnapi = bnapi; 4384 bnapi += arr_size; 4385 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 4386 bp->bnapi[i] = bnapi; 4387 bp->bnapi[i]->index = i; 4388 bp->bnapi[i]->bp = bp; 4389 if (bp->flags & BNXT_FLAG_CHIP_P5) { 4390 struct bnxt_cp_ring_info *cpr = 4391 &bp->bnapi[i]->cp_ring; 4392 4393 cpr->cp_ring_struct.ring_mem.flags = 4394 BNXT_RMEM_RING_PTE_FLAG; 4395 } 4396 } 4397 4398 bp->rx_ring = kcalloc(bp->rx_nr_rings, 4399 sizeof(struct bnxt_rx_ring_info), 4400 GFP_KERNEL); 4401 if (!bp->rx_ring) 4402 return -ENOMEM; 4403 4404 for (i = 0; i < bp->rx_nr_rings; i++) { 4405 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 4406 4407 if (bp->flags & BNXT_FLAG_CHIP_P5) { 4408 rxr->rx_ring_struct.ring_mem.flags = 4409 BNXT_RMEM_RING_PTE_FLAG; 4410 rxr->rx_agg_ring_struct.ring_mem.flags = 4411 BNXT_RMEM_RING_PTE_FLAG; 4412 } 4413 rxr->bnapi = bp->bnapi[i]; 4414 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 4415 } 4416 4417 bp->tx_ring = kcalloc(bp->tx_nr_rings, 4418 sizeof(struct bnxt_tx_ring_info), 4419 GFP_KERNEL); 4420 if (!bp->tx_ring) 4421 return -ENOMEM; 4422 4423 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 4424 GFP_KERNEL); 4425 4426 if (!bp->tx_ring_map) 4427 return -ENOMEM; 4428 4429 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 4430 j = 0; 4431 else 4432 j = bp->rx_nr_rings; 4433 4434 for (i = 0; i < bp->tx_nr_rings; i++, j++) { 4435 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4436 4437 if (bp->flags & BNXT_FLAG_CHIP_P5) 4438 txr->tx_ring_struct.ring_mem.flags = 4439 BNXT_RMEM_RING_PTE_FLAG; 4440 txr->bnapi = bp->bnapi[j]; 4441 bp->bnapi[j]->tx_ring = txr; 4442 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 4443 if (i >= bp->tx_nr_rings_xdp) { 4444 txr->txq_index = i - bp->tx_nr_rings_xdp; 4445 bp->bnapi[j]->tx_int = bnxt_tx_int; 4446 } else { 4447 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP; 4448 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp; 4449 } 4450 } 4451 4452 rc = bnxt_alloc_stats(bp); 4453 if (rc) 4454 goto alloc_mem_err; 4455 bnxt_init_stats(bp); 4456 4457 rc = bnxt_alloc_ntp_fltrs(bp); 4458 if (rc) 4459 goto alloc_mem_err; 4460 4461 rc = bnxt_alloc_vnics(bp); 4462 if (rc) 4463 goto alloc_mem_err; 4464 } 4465 4466 rc = bnxt_alloc_all_cp_arrays(bp); 4467 if (rc) 4468 goto alloc_mem_err; 4469 4470 bnxt_init_ring_struct(bp); 4471 4472 rc = bnxt_alloc_rx_rings(bp); 4473 if (rc) 4474 goto alloc_mem_err; 4475 4476 rc = bnxt_alloc_tx_rings(bp); 4477 if (rc) 4478 goto alloc_mem_err; 4479 4480 rc = bnxt_alloc_cp_rings(bp); 4481 if (rc) 4482 goto alloc_mem_err; 4483 4484 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG | 4485 BNXT_VNIC_UCAST_FLAG; 4486 rc = bnxt_alloc_vnic_attributes(bp); 4487 if (rc) 4488 goto alloc_mem_err; 4489 return 0; 4490 4491 alloc_mem_err: 4492 bnxt_free_mem(bp, true); 4493 return rc; 4494 } 4495 4496 static void bnxt_disable_int(struct bnxt *bp) 4497 { 4498 int i; 4499 4500 if (!bp->bnapi) 4501 return; 4502 4503 for (i = 0; i < bp->cp_nr_rings; i++) { 4504 struct bnxt_napi *bnapi = bp->bnapi[i]; 4505 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4506 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4507 4508 if (ring->fw_ring_id != INVALID_HW_RING_ID) 4509 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 4510 } 4511 } 4512 4513 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 4514 { 4515 struct bnxt_napi *bnapi = bp->bnapi[n]; 4516 struct bnxt_cp_ring_info *cpr; 4517 4518 cpr = &bnapi->cp_ring; 4519 return cpr->cp_ring_struct.map_idx; 4520 } 4521 4522 static void bnxt_disable_int_sync(struct bnxt *bp) 4523 { 4524 int i; 4525 4526 if (!bp->irq_tbl) 4527 return; 4528 4529 atomic_inc(&bp->intr_sem); 4530 4531 bnxt_disable_int(bp); 4532 for (i = 0; i < bp->cp_nr_rings; i++) { 4533 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 4534 4535 synchronize_irq(bp->irq_tbl[map_idx].vector); 4536 } 4537 } 4538 4539 static void bnxt_enable_int(struct bnxt *bp) 4540 { 4541 int i; 4542 4543 atomic_set(&bp->intr_sem, 0); 4544 for (i = 0; i < bp->cp_nr_rings; i++) { 4545 struct bnxt_napi *bnapi = bp->bnapi[i]; 4546 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4547 4548 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 4549 } 4550 } 4551 4552 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 4553 bool async_only) 4554 { 4555 DECLARE_BITMAP(async_events_bmap, 256); 4556 u32 *events = (u32 *)async_events_bmap; 4557 struct hwrm_func_drv_rgtr_output *resp; 4558 struct hwrm_func_drv_rgtr_input *req; 4559 u32 flags; 4560 int rc, i; 4561 4562 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 4563 if (rc) 4564 return rc; 4565 4566 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 4567 FUNC_DRV_RGTR_REQ_ENABLES_VER | 4568 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 4569 4570 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 4571 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 4572 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 4573 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 4574 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 4575 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 4576 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 4577 req->flags = cpu_to_le32(flags); 4578 req->ver_maj_8b = DRV_VER_MAJ; 4579 req->ver_min_8b = DRV_VER_MIN; 4580 req->ver_upd_8b = DRV_VER_UPD; 4581 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 4582 req->ver_min = cpu_to_le16(DRV_VER_MIN); 4583 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 4584 4585 if (BNXT_PF(bp)) { 4586 u32 data[8]; 4587 int i; 4588 4589 memset(data, 0, sizeof(data)); 4590 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 4591 u16 cmd = bnxt_vf_req_snif[i]; 4592 unsigned int bit, idx; 4593 4594 idx = cmd / 32; 4595 bit = cmd % 32; 4596 data[idx] |= 1 << bit; 4597 } 4598 4599 for (i = 0; i < 8; i++) 4600 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 4601 4602 req->enables |= 4603 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 4604 } 4605 4606 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 4607 req->flags |= cpu_to_le32( 4608 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 4609 4610 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 4611 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 4612 u16 event_id = bnxt_async_events_arr[i]; 4613 4614 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 4615 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 4616 continue; 4617 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 4618 } 4619 if (bmap && bmap_size) { 4620 for (i = 0; i < bmap_size; i++) { 4621 if (test_bit(i, bmap)) 4622 __set_bit(i, async_events_bmap); 4623 } 4624 } 4625 for (i = 0; i < 8; i++) 4626 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 4627 4628 if (async_only) 4629 req->enables = 4630 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 4631 4632 resp = hwrm_req_hold(bp, req); 4633 rc = hwrm_req_send(bp, req); 4634 if (!rc) { 4635 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 4636 if (resp->flags & 4637 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 4638 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 4639 } 4640 hwrm_req_drop(bp, req); 4641 return rc; 4642 } 4643 4644 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 4645 { 4646 struct hwrm_func_drv_unrgtr_input *req; 4647 int rc; 4648 4649 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 4650 return 0; 4651 4652 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 4653 if (rc) 4654 return rc; 4655 return hwrm_req_send(bp, req); 4656 } 4657 4658 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 4659 { 4660 struct hwrm_tunnel_dst_port_free_input *req; 4661 int rc; 4662 4663 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 4664 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 4665 return 0; 4666 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 4667 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 4668 return 0; 4669 4670 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 4671 if (rc) 4672 return rc; 4673 4674 req->tunnel_type = tunnel_type; 4675 4676 switch (tunnel_type) { 4677 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 4678 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 4679 bp->vxlan_port = 0; 4680 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 4681 break; 4682 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 4683 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 4684 bp->nge_port = 0; 4685 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 4686 break; 4687 default: 4688 break; 4689 } 4690 4691 rc = hwrm_req_send(bp, req); 4692 if (rc) 4693 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 4694 rc); 4695 return rc; 4696 } 4697 4698 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 4699 u8 tunnel_type) 4700 { 4701 struct hwrm_tunnel_dst_port_alloc_output *resp; 4702 struct hwrm_tunnel_dst_port_alloc_input *req; 4703 int rc; 4704 4705 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 4706 if (rc) 4707 return rc; 4708 4709 req->tunnel_type = tunnel_type; 4710 req->tunnel_dst_port_val = port; 4711 4712 resp = hwrm_req_hold(bp, req); 4713 rc = hwrm_req_send(bp, req); 4714 if (rc) { 4715 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 4716 rc); 4717 goto err_out; 4718 } 4719 4720 switch (tunnel_type) { 4721 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 4722 bp->vxlan_port = port; 4723 bp->vxlan_fw_dst_port_id = 4724 le16_to_cpu(resp->tunnel_dst_port_id); 4725 break; 4726 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 4727 bp->nge_port = port; 4728 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 4729 break; 4730 default: 4731 break; 4732 } 4733 4734 err_out: 4735 hwrm_req_drop(bp, req); 4736 return rc; 4737 } 4738 4739 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 4740 { 4741 struct hwrm_cfa_l2_set_rx_mask_input *req; 4742 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 4743 int rc; 4744 4745 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 4746 if (rc) 4747 return rc; 4748 4749 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 4750 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 4751 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 4752 req->mask = cpu_to_le32(vnic->rx_mask); 4753 return hwrm_req_send_silent(bp, req); 4754 } 4755 4756 #ifdef CONFIG_RFS_ACCEL 4757 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 4758 struct bnxt_ntuple_filter *fltr) 4759 { 4760 struct hwrm_cfa_ntuple_filter_free_input *req; 4761 int rc; 4762 4763 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 4764 if (rc) 4765 return rc; 4766 4767 req->ntuple_filter_id = fltr->filter_id; 4768 return hwrm_req_send(bp, req); 4769 } 4770 4771 #define BNXT_NTP_FLTR_FLAGS \ 4772 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 4773 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 4774 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \ 4775 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 4776 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 4777 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 4778 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 4779 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 4780 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 4781 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 4782 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 4783 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 4784 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 4785 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 4786 4787 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 4788 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 4789 4790 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 4791 struct bnxt_ntuple_filter *fltr) 4792 { 4793 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 4794 struct hwrm_cfa_ntuple_filter_alloc_input *req; 4795 struct flow_keys *keys = &fltr->fkeys; 4796 struct bnxt_vnic_info *vnic; 4797 u32 flags = 0; 4798 int rc; 4799 4800 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 4801 if (rc) 4802 return rc; 4803 4804 req->l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx]; 4805 4806 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 4807 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 4808 req->dst_id = cpu_to_le16(fltr->rxq); 4809 } else { 4810 vnic = &bp->vnic_info[fltr->rxq + 1]; 4811 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 4812 } 4813 req->flags = cpu_to_le32(flags); 4814 req->enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 4815 4816 req->ethertype = htons(ETH_P_IP); 4817 memcpy(req->src_macaddr, fltr->src_mac_addr, ETH_ALEN); 4818 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 4819 req->ip_protocol = keys->basic.ip_proto; 4820 4821 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 4822 int i; 4823 4824 req->ethertype = htons(ETH_P_IPV6); 4825 req->ip_addr_type = 4826 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 4827 *(struct in6_addr *)&req->src_ipaddr[0] = 4828 keys->addrs.v6addrs.src; 4829 *(struct in6_addr *)&req->dst_ipaddr[0] = 4830 keys->addrs.v6addrs.dst; 4831 for (i = 0; i < 4; i++) { 4832 req->src_ipaddr_mask[i] = cpu_to_be32(0xffffffff); 4833 req->dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff); 4834 } 4835 } else { 4836 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 4837 req->src_ipaddr_mask[0] = cpu_to_be32(0xffffffff); 4838 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 4839 req->dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff); 4840 } 4841 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 4842 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 4843 req->tunnel_type = 4844 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 4845 } 4846 4847 req->src_port = keys->ports.src; 4848 req->src_port_mask = cpu_to_be16(0xffff); 4849 req->dst_port = keys->ports.dst; 4850 req->dst_port_mask = cpu_to_be16(0xffff); 4851 4852 resp = hwrm_req_hold(bp, req); 4853 rc = hwrm_req_send(bp, req); 4854 if (!rc) 4855 fltr->filter_id = resp->ntuple_filter_id; 4856 hwrm_req_drop(bp, req); 4857 return rc; 4858 } 4859 #endif 4860 4861 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 4862 const u8 *mac_addr) 4863 { 4864 struct hwrm_cfa_l2_filter_alloc_output *resp; 4865 struct hwrm_cfa_l2_filter_alloc_input *req; 4866 int rc; 4867 4868 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 4869 if (rc) 4870 return rc; 4871 4872 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 4873 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 4874 req->flags |= 4875 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 4876 req->dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id); 4877 req->enables = 4878 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 4879 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 4880 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 4881 memcpy(req->l2_addr, mac_addr, ETH_ALEN); 4882 req->l2_addr_mask[0] = 0xff; 4883 req->l2_addr_mask[1] = 0xff; 4884 req->l2_addr_mask[2] = 0xff; 4885 req->l2_addr_mask[3] = 0xff; 4886 req->l2_addr_mask[4] = 0xff; 4887 req->l2_addr_mask[5] = 0xff; 4888 4889 resp = hwrm_req_hold(bp, req); 4890 rc = hwrm_req_send(bp, req); 4891 if (!rc) 4892 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] = 4893 resp->l2_filter_id; 4894 hwrm_req_drop(bp, req); 4895 return rc; 4896 } 4897 4898 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 4899 { 4900 struct hwrm_cfa_l2_filter_free_input *req; 4901 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 4902 int rc; 4903 4904 /* Any associated ntuple filters will also be cleared by firmware. */ 4905 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 4906 if (rc) 4907 return rc; 4908 hwrm_req_hold(bp, req); 4909 for (i = 0; i < num_of_vnics; i++) { 4910 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4911 4912 for (j = 0; j < vnic->uc_filter_count; j++) { 4913 req->l2_filter_id = vnic->fw_l2_filter_id[j]; 4914 4915 rc = hwrm_req_send(bp, req); 4916 } 4917 vnic->uc_filter_count = 0; 4918 } 4919 hwrm_req_drop(bp, req); 4920 return rc; 4921 } 4922 4923 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags) 4924 { 4925 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 4926 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 4927 struct hwrm_vnic_tpa_cfg_input *req; 4928 int rc; 4929 4930 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 4931 return 0; 4932 4933 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 4934 if (rc) 4935 return rc; 4936 4937 if (tpa_flags) { 4938 u16 mss = bp->dev->mtu - 40; 4939 u32 nsegs, n, segs = 0, flags; 4940 4941 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 4942 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 4943 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 4944 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 4945 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 4946 if (tpa_flags & BNXT_FLAG_GRO) 4947 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 4948 4949 req->flags = cpu_to_le32(flags); 4950 4951 req->enables = 4952 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 4953 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 4954 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 4955 4956 /* Number of segs are log2 units, and first packet is not 4957 * included as part of this units. 4958 */ 4959 if (mss <= BNXT_RX_PAGE_SIZE) { 4960 n = BNXT_RX_PAGE_SIZE / mss; 4961 nsegs = (MAX_SKB_FRAGS - 1) * n; 4962 } else { 4963 n = mss / BNXT_RX_PAGE_SIZE; 4964 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 4965 n++; 4966 nsegs = (MAX_SKB_FRAGS - n) / n; 4967 } 4968 4969 if (bp->flags & BNXT_FLAG_CHIP_P5) { 4970 segs = MAX_TPA_SEGS_P5; 4971 max_aggs = bp->max_tpa; 4972 } else { 4973 segs = ilog2(nsegs); 4974 } 4975 req->max_agg_segs = cpu_to_le16(segs); 4976 req->max_aggs = cpu_to_le16(max_aggs); 4977 4978 req->min_agg_len = cpu_to_le32(512); 4979 } 4980 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 4981 4982 return hwrm_req_send(bp, req); 4983 } 4984 4985 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 4986 { 4987 struct bnxt_ring_grp_info *grp_info; 4988 4989 grp_info = &bp->grp_info[ring->grp_idx]; 4990 return grp_info->cp_fw_ring_id; 4991 } 4992 4993 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 4994 { 4995 if (bp->flags & BNXT_FLAG_CHIP_P5) { 4996 struct bnxt_napi *bnapi = rxr->bnapi; 4997 struct bnxt_cp_ring_info *cpr; 4998 4999 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL]; 5000 return cpr->cp_ring_struct.fw_ring_id; 5001 } else { 5002 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 5003 } 5004 } 5005 5006 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 5007 { 5008 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5009 struct bnxt_napi *bnapi = txr->bnapi; 5010 struct bnxt_cp_ring_info *cpr; 5011 5012 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL]; 5013 return cpr->cp_ring_struct.fw_ring_id; 5014 } else { 5015 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 5016 } 5017 } 5018 5019 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 5020 { 5021 int entries; 5022 5023 if (bp->flags & BNXT_FLAG_CHIP_P5) 5024 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 5025 else 5026 entries = HW_HASH_INDEX_SIZE; 5027 5028 bp->rss_indir_tbl_entries = entries; 5029 bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), 5030 GFP_KERNEL); 5031 if (!bp->rss_indir_tbl) 5032 return -ENOMEM; 5033 return 0; 5034 } 5035 5036 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp) 5037 { 5038 u16 max_rings, max_entries, pad, i; 5039 5040 if (!bp->rx_nr_rings) 5041 return; 5042 5043 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 5044 max_rings = bp->rx_nr_rings - 1; 5045 else 5046 max_rings = bp->rx_nr_rings; 5047 5048 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 5049 5050 for (i = 0; i < max_entries; i++) 5051 bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 5052 5053 pad = bp->rss_indir_tbl_entries - max_entries; 5054 if (pad) 5055 memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16)); 5056 } 5057 5058 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 5059 { 5060 u16 i, tbl_size, max_ring = 0; 5061 5062 if (!bp->rss_indir_tbl) 5063 return 0; 5064 5065 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 5066 for (i = 0; i < tbl_size; i++) 5067 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 5068 return max_ring; 5069 } 5070 5071 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 5072 { 5073 if (bp->flags & BNXT_FLAG_CHIP_P5) 5074 return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5); 5075 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 5076 return 2; 5077 return 1; 5078 } 5079 5080 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 5081 { 5082 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 5083 u16 i, j; 5084 5085 /* Fill the RSS indirection table with ring group ids */ 5086 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 5087 if (!no_rss) 5088 j = bp->rss_indir_tbl[i]; 5089 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 5090 } 5091 } 5092 5093 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 5094 struct bnxt_vnic_info *vnic) 5095 { 5096 __le16 *ring_tbl = vnic->rss_table; 5097 struct bnxt_rx_ring_info *rxr; 5098 u16 tbl_size, i; 5099 5100 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 5101 5102 for (i = 0; i < tbl_size; i++) { 5103 u16 ring_id, j; 5104 5105 j = bp->rss_indir_tbl[i]; 5106 rxr = &bp->rx_ring[j]; 5107 5108 ring_id = rxr->rx_ring_struct.fw_ring_id; 5109 *ring_tbl++ = cpu_to_le16(ring_id); 5110 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 5111 *ring_tbl++ = cpu_to_le16(ring_id); 5112 } 5113 } 5114 5115 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 5116 { 5117 if (bp->flags & BNXT_FLAG_CHIP_P5) 5118 __bnxt_fill_hw_rss_tbl_p5(bp, vnic); 5119 else 5120 __bnxt_fill_hw_rss_tbl(bp, vnic); 5121 } 5122 5123 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss) 5124 { 5125 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5126 struct hwrm_vnic_rss_cfg_input *req; 5127 int rc; 5128 5129 if ((bp->flags & BNXT_FLAG_CHIP_P5) || 5130 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 5131 return 0; 5132 5133 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 5134 if (rc) 5135 return rc; 5136 5137 if (set_rss) { 5138 bnxt_fill_hw_rss_tbl(bp, vnic); 5139 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 5140 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 5141 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 5142 req->hash_key_tbl_addr = 5143 cpu_to_le64(vnic->rss_hash_key_dma_addr); 5144 } 5145 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 5146 return hwrm_req_send(bp, req); 5147 } 5148 5149 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss) 5150 { 5151 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5152 struct hwrm_vnic_rss_cfg_input *req; 5153 dma_addr_t ring_tbl_map; 5154 u32 i, nr_ctxs; 5155 int rc; 5156 5157 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 5158 if (rc) 5159 return rc; 5160 5161 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 5162 if (!set_rss) 5163 return hwrm_req_send(bp, req); 5164 5165 bnxt_fill_hw_rss_tbl(bp, vnic); 5166 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 5167 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 5168 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 5169 ring_tbl_map = vnic->rss_table_dma_addr; 5170 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 5171 5172 hwrm_req_hold(bp, req); 5173 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 5174 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 5175 req->ring_table_pair_index = i; 5176 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 5177 rc = hwrm_req_send(bp, req); 5178 if (rc) 5179 goto exit; 5180 } 5181 5182 exit: 5183 hwrm_req_drop(bp, req); 5184 return rc; 5185 } 5186 5187 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id) 5188 { 5189 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5190 struct hwrm_vnic_plcmodes_cfg_input *req; 5191 int rc; 5192 5193 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 5194 if (rc) 5195 return rc; 5196 5197 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT | 5198 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 5199 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 5200 req->enables = 5201 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID | 5202 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 5203 /* thresholds not implemented in firmware yet */ 5204 req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh); 5205 req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh); 5206 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5207 return hwrm_req_send(bp, req); 5208 } 5209 5210 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id, 5211 u16 ctx_idx) 5212 { 5213 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 5214 5215 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 5216 return; 5217 5218 req->rss_cos_lb_ctx_id = 5219 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]); 5220 5221 hwrm_req_send(bp, req); 5222 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 5223 } 5224 5225 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 5226 { 5227 int i, j; 5228 5229 for (i = 0; i < bp->nr_vnics; i++) { 5230 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 5231 5232 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 5233 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 5234 bnxt_hwrm_vnic_ctx_free_one(bp, i, j); 5235 } 5236 } 5237 bp->rsscos_nr_ctxs = 0; 5238 } 5239 5240 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx) 5241 { 5242 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 5243 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 5244 int rc; 5245 5246 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 5247 if (rc) 5248 return rc; 5249 5250 resp = hwrm_req_hold(bp, req); 5251 rc = hwrm_req_send(bp, req); 5252 if (!rc) 5253 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = 5254 le16_to_cpu(resp->rss_cos_lb_ctx_id); 5255 hwrm_req_drop(bp, req); 5256 5257 return rc; 5258 } 5259 5260 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 5261 { 5262 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 5263 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 5264 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 5265 } 5266 5267 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id) 5268 { 5269 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5270 struct hwrm_vnic_cfg_input *req; 5271 unsigned int ring = 0, grp_idx; 5272 u16 def_vlan = 0; 5273 int rc; 5274 5275 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 5276 if (rc) 5277 return rc; 5278 5279 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5280 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 5281 5282 req->default_rx_ring_id = 5283 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 5284 req->default_cmpl_ring_id = 5285 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 5286 req->enables = 5287 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 5288 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 5289 goto vnic_mru; 5290 } 5291 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 5292 /* Only RSS support for now TBD: COS & LB */ 5293 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 5294 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 5295 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 5296 VNIC_CFG_REQ_ENABLES_MRU); 5297 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 5298 req->rss_rule = 5299 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]); 5300 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 5301 VNIC_CFG_REQ_ENABLES_MRU); 5302 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 5303 } else { 5304 req->rss_rule = cpu_to_le16(0xffff); 5305 } 5306 5307 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 5308 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 5309 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 5310 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 5311 } else { 5312 req->cos_rule = cpu_to_le16(0xffff); 5313 } 5314 5315 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 5316 ring = 0; 5317 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 5318 ring = vnic_id - 1; 5319 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 5320 ring = bp->rx_nr_rings - 1; 5321 5322 grp_idx = bp->rx_ring[ring].bnapi->index; 5323 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 5324 req->lb_rule = cpu_to_le16(0xffff); 5325 vnic_mru: 5326 req->mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN); 5327 5328 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 5329 #ifdef CONFIG_BNXT_SRIOV 5330 if (BNXT_VF(bp)) 5331 def_vlan = bp->vf.vlan; 5332 #endif 5333 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 5334 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 5335 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) 5336 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 5337 5338 return hwrm_req_send(bp, req); 5339 } 5340 5341 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id) 5342 { 5343 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) { 5344 struct hwrm_vnic_free_input *req; 5345 5346 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 5347 return; 5348 5349 req->vnic_id = 5350 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id); 5351 5352 hwrm_req_send(bp, req); 5353 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID; 5354 } 5355 } 5356 5357 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 5358 { 5359 u16 i; 5360 5361 for (i = 0; i < bp->nr_vnics; i++) 5362 bnxt_hwrm_vnic_free_one(bp, i); 5363 } 5364 5365 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id, 5366 unsigned int start_rx_ring_idx, 5367 unsigned int nr_rings) 5368 { 5369 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 5370 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5371 struct hwrm_vnic_alloc_output *resp; 5372 struct hwrm_vnic_alloc_input *req; 5373 int rc; 5374 5375 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 5376 if (rc) 5377 return rc; 5378 5379 if (bp->flags & BNXT_FLAG_CHIP_P5) 5380 goto vnic_no_ring_grps; 5381 5382 /* map ring groups to this vnic */ 5383 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 5384 grp_idx = bp->rx_ring[i].bnapi->index; 5385 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 5386 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 5387 j, nr_rings); 5388 break; 5389 } 5390 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 5391 } 5392 5393 vnic_no_ring_grps: 5394 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 5395 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 5396 if (vnic_id == 0) 5397 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 5398 5399 resp = hwrm_req_hold(bp, req); 5400 rc = hwrm_req_send(bp, req); 5401 if (!rc) 5402 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 5403 hwrm_req_drop(bp, req); 5404 return rc; 5405 } 5406 5407 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 5408 { 5409 struct hwrm_vnic_qcaps_output *resp; 5410 struct hwrm_vnic_qcaps_input *req; 5411 int rc; 5412 5413 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 5414 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP); 5415 if (bp->hwrm_spec_code < 0x10600) 5416 return 0; 5417 5418 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 5419 if (rc) 5420 return rc; 5421 5422 resp = hwrm_req_hold(bp, req); 5423 rc = hwrm_req_send(bp, req); 5424 if (!rc) { 5425 u32 flags = le32_to_cpu(resp->flags); 5426 5427 if (!(bp->flags & BNXT_FLAG_CHIP_P5) && 5428 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 5429 bp->flags |= BNXT_FLAG_NEW_RSS_CAP; 5430 if (flags & 5431 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 5432 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 5433 5434 /* Older P5 fw before EXT_HW_STATS support did not set 5435 * VLAN_STRIP_CAP properly. 5436 */ 5437 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 5438 (BNXT_CHIP_P5_THOR(bp) && 5439 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 5440 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 5441 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 5442 if (bp->max_tpa_v2) { 5443 if (BNXT_CHIP_P5_THOR(bp)) 5444 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 5445 else 5446 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2; 5447 } 5448 } 5449 hwrm_req_drop(bp, req); 5450 return rc; 5451 } 5452 5453 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 5454 { 5455 struct hwrm_ring_grp_alloc_output *resp; 5456 struct hwrm_ring_grp_alloc_input *req; 5457 int rc; 5458 u16 i; 5459 5460 if (bp->flags & BNXT_FLAG_CHIP_P5) 5461 return 0; 5462 5463 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 5464 if (rc) 5465 return rc; 5466 5467 resp = hwrm_req_hold(bp, req); 5468 for (i = 0; i < bp->rx_nr_rings; i++) { 5469 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 5470 5471 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 5472 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 5473 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 5474 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 5475 5476 rc = hwrm_req_send(bp, req); 5477 5478 if (rc) 5479 break; 5480 5481 bp->grp_info[grp_idx].fw_grp_id = 5482 le32_to_cpu(resp->ring_group_id); 5483 } 5484 hwrm_req_drop(bp, req); 5485 return rc; 5486 } 5487 5488 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 5489 { 5490 struct hwrm_ring_grp_free_input *req; 5491 u16 i; 5492 5493 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5)) 5494 return; 5495 5496 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 5497 return; 5498 5499 hwrm_req_hold(bp, req); 5500 for (i = 0; i < bp->cp_nr_rings; i++) { 5501 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 5502 continue; 5503 req->ring_group_id = 5504 cpu_to_le32(bp->grp_info[i].fw_grp_id); 5505 5506 hwrm_req_send(bp, req); 5507 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 5508 } 5509 hwrm_req_drop(bp, req); 5510 } 5511 5512 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 5513 struct bnxt_ring_struct *ring, 5514 u32 ring_type, u32 map_index) 5515 { 5516 struct hwrm_ring_alloc_output *resp; 5517 struct hwrm_ring_alloc_input *req; 5518 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 5519 struct bnxt_ring_grp_info *grp_info; 5520 int rc, err = 0; 5521 u16 ring_id; 5522 5523 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 5524 if (rc) 5525 goto exit; 5526 5527 req->enables = 0; 5528 if (rmem->nr_pages > 1) { 5529 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 5530 /* Page size is in log2 units */ 5531 req->page_size = BNXT_PAGE_SHIFT; 5532 req->page_tbl_depth = 1; 5533 } else { 5534 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 5535 } 5536 req->fbo = 0; 5537 /* Association of ring index with doorbell index and MSIX number */ 5538 req->logical_id = cpu_to_le16(map_index); 5539 5540 switch (ring_type) { 5541 case HWRM_RING_ALLOC_TX: { 5542 struct bnxt_tx_ring_info *txr; 5543 5544 txr = container_of(ring, struct bnxt_tx_ring_info, 5545 tx_ring_struct); 5546 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 5547 /* Association of transmit ring with completion ring */ 5548 grp_info = &bp->grp_info[ring->grp_idx]; 5549 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 5550 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 5551 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 5552 req->queue_id = cpu_to_le16(ring->queue_id); 5553 break; 5554 } 5555 case HWRM_RING_ALLOC_RX: 5556 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 5557 req->length = cpu_to_le32(bp->rx_ring_mask + 1); 5558 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5559 u16 flags = 0; 5560 5561 /* Association of rx ring with stats context */ 5562 grp_info = &bp->grp_info[ring->grp_idx]; 5563 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 5564 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 5565 req->enables |= cpu_to_le32( 5566 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 5567 if (NET_IP_ALIGN == 2) 5568 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD; 5569 req->flags = cpu_to_le16(flags); 5570 } 5571 break; 5572 case HWRM_RING_ALLOC_AGG: 5573 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5574 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 5575 /* Association of agg ring with rx ring */ 5576 grp_info = &bp->grp_info[ring->grp_idx]; 5577 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 5578 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 5579 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 5580 req->enables |= cpu_to_le32( 5581 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID | 5582 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 5583 } else { 5584 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 5585 } 5586 req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1); 5587 break; 5588 case HWRM_RING_ALLOC_CMPL: 5589 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 5590 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 5591 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5592 /* Association of cp ring with nq */ 5593 grp_info = &bp->grp_info[map_index]; 5594 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 5595 req->cq_handle = cpu_to_le64(ring->handle); 5596 req->enables |= cpu_to_le32( 5597 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 5598 } else if (bp->flags & BNXT_FLAG_USING_MSIX) { 5599 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 5600 } 5601 break; 5602 case HWRM_RING_ALLOC_NQ: 5603 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 5604 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 5605 if (bp->flags & BNXT_FLAG_USING_MSIX) 5606 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 5607 break; 5608 default: 5609 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 5610 ring_type); 5611 return -1; 5612 } 5613 5614 resp = hwrm_req_hold(bp, req); 5615 rc = hwrm_req_send(bp, req); 5616 err = le16_to_cpu(resp->error_code); 5617 ring_id = le16_to_cpu(resp->ring_id); 5618 hwrm_req_drop(bp, req); 5619 5620 exit: 5621 if (rc || err) { 5622 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 5623 ring_type, rc, err); 5624 return -EIO; 5625 } 5626 ring->fw_ring_id = ring_id; 5627 return rc; 5628 } 5629 5630 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 5631 { 5632 int rc; 5633 5634 if (BNXT_PF(bp)) { 5635 struct hwrm_func_cfg_input *req; 5636 5637 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG); 5638 if (rc) 5639 return rc; 5640 5641 req->fid = cpu_to_le16(0xffff); 5642 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 5643 req->async_event_cr = cpu_to_le16(idx); 5644 return hwrm_req_send(bp, req); 5645 } else { 5646 struct hwrm_func_vf_cfg_input *req; 5647 5648 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 5649 if (rc) 5650 return rc; 5651 5652 req->enables = 5653 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 5654 req->async_event_cr = cpu_to_le16(idx); 5655 return hwrm_req_send(bp, req); 5656 } 5657 } 5658 5659 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 5660 u32 map_idx, u32 xid) 5661 { 5662 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5663 if (BNXT_PF(bp)) 5664 db->doorbell = bp->bar1 + DB_PF_OFFSET_P5; 5665 else 5666 db->doorbell = bp->bar1 + DB_VF_OFFSET_P5; 5667 switch (ring_type) { 5668 case HWRM_RING_ALLOC_TX: 5669 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 5670 break; 5671 case HWRM_RING_ALLOC_RX: 5672 case HWRM_RING_ALLOC_AGG: 5673 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 5674 break; 5675 case HWRM_RING_ALLOC_CMPL: 5676 db->db_key64 = DBR_PATH_L2; 5677 break; 5678 case HWRM_RING_ALLOC_NQ: 5679 db->db_key64 = DBR_PATH_L2; 5680 break; 5681 } 5682 db->db_key64 |= (u64)xid << DBR_XID_SFT; 5683 } else { 5684 db->doorbell = bp->bar1 + map_idx * 0x80; 5685 switch (ring_type) { 5686 case HWRM_RING_ALLOC_TX: 5687 db->db_key32 = DB_KEY_TX; 5688 break; 5689 case HWRM_RING_ALLOC_RX: 5690 case HWRM_RING_ALLOC_AGG: 5691 db->db_key32 = DB_KEY_RX; 5692 break; 5693 case HWRM_RING_ALLOC_CMPL: 5694 db->db_key32 = DB_KEY_CP; 5695 break; 5696 } 5697 } 5698 } 5699 5700 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 5701 { 5702 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 5703 int i, rc = 0; 5704 u32 type; 5705 5706 if (bp->flags & BNXT_FLAG_CHIP_P5) 5707 type = HWRM_RING_ALLOC_NQ; 5708 else 5709 type = HWRM_RING_ALLOC_CMPL; 5710 for (i = 0; i < bp->cp_nr_rings; i++) { 5711 struct bnxt_napi *bnapi = bp->bnapi[i]; 5712 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5713 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5714 u32 map_idx = ring->map_idx; 5715 unsigned int vector; 5716 5717 vector = bp->irq_tbl[map_idx].vector; 5718 disable_irq_nosync(vector); 5719 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 5720 if (rc) { 5721 enable_irq(vector); 5722 goto err_out; 5723 } 5724 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 5725 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5726 enable_irq(vector); 5727 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 5728 5729 if (!i) { 5730 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 5731 if (rc) 5732 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 5733 } 5734 } 5735 5736 type = HWRM_RING_ALLOC_TX; 5737 for (i = 0; i < bp->tx_nr_rings; i++) { 5738 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5739 struct bnxt_ring_struct *ring; 5740 u32 map_idx; 5741 5742 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5743 struct bnxt_napi *bnapi = txr->bnapi; 5744 struct bnxt_cp_ring_info *cpr, *cpr2; 5745 u32 type2 = HWRM_RING_ALLOC_CMPL; 5746 5747 cpr = &bnapi->cp_ring; 5748 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL]; 5749 ring = &cpr2->cp_ring_struct; 5750 ring->handle = BNXT_TX_HDL; 5751 map_idx = bnapi->index; 5752 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 5753 if (rc) 5754 goto err_out; 5755 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 5756 ring->fw_ring_id); 5757 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 5758 } 5759 ring = &txr->tx_ring_struct; 5760 map_idx = i; 5761 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 5762 if (rc) 5763 goto err_out; 5764 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id); 5765 } 5766 5767 type = HWRM_RING_ALLOC_RX; 5768 for (i = 0; i < bp->rx_nr_rings; i++) { 5769 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5770 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 5771 struct bnxt_napi *bnapi = rxr->bnapi; 5772 u32 map_idx = bnapi->index; 5773 5774 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 5775 if (rc) 5776 goto err_out; 5777 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 5778 /* If we have agg rings, post agg buffers first. */ 5779 if (!agg_rings) 5780 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 5781 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 5782 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5783 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5784 u32 type2 = HWRM_RING_ALLOC_CMPL; 5785 struct bnxt_cp_ring_info *cpr2; 5786 5787 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL]; 5788 ring = &cpr2->cp_ring_struct; 5789 ring->handle = BNXT_RX_HDL; 5790 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 5791 if (rc) 5792 goto err_out; 5793 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 5794 ring->fw_ring_id); 5795 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 5796 } 5797 } 5798 5799 if (agg_rings) { 5800 type = HWRM_RING_ALLOC_AGG; 5801 for (i = 0; i < bp->rx_nr_rings; i++) { 5802 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5803 struct bnxt_ring_struct *ring = 5804 &rxr->rx_agg_ring_struct; 5805 u32 grp_idx = ring->grp_idx; 5806 u32 map_idx = grp_idx + bp->rx_nr_rings; 5807 5808 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 5809 if (rc) 5810 goto err_out; 5811 5812 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 5813 ring->fw_ring_id); 5814 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 5815 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 5816 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 5817 } 5818 } 5819 err_out: 5820 return rc; 5821 } 5822 5823 static int hwrm_ring_free_send_msg(struct bnxt *bp, 5824 struct bnxt_ring_struct *ring, 5825 u32 ring_type, int cmpl_ring_id) 5826 { 5827 struct hwrm_ring_free_output *resp; 5828 struct hwrm_ring_free_input *req; 5829 u16 error_code = 0; 5830 int rc; 5831 5832 if (BNXT_NO_FW_ACCESS(bp)) 5833 return 0; 5834 5835 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 5836 if (rc) 5837 goto exit; 5838 5839 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 5840 req->ring_type = ring_type; 5841 req->ring_id = cpu_to_le16(ring->fw_ring_id); 5842 5843 resp = hwrm_req_hold(bp, req); 5844 rc = hwrm_req_send(bp, req); 5845 error_code = le16_to_cpu(resp->error_code); 5846 hwrm_req_drop(bp, req); 5847 exit: 5848 if (rc || error_code) { 5849 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 5850 ring_type, rc, error_code); 5851 return -EIO; 5852 } 5853 return 0; 5854 } 5855 5856 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 5857 { 5858 u32 type; 5859 int i; 5860 5861 if (!bp->bnapi) 5862 return; 5863 5864 for (i = 0; i < bp->tx_nr_rings; i++) { 5865 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5866 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 5867 5868 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 5869 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr); 5870 5871 hwrm_ring_free_send_msg(bp, ring, 5872 RING_FREE_REQ_RING_TYPE_TX, 5873 close_path ? cmpl_ring_id : 5874 INVALID_HW_RING_ID); 5875 ring->fw_ring_id = INVALID_HW_RING_ID; 5876 } 5877 } 5878 5879 for (i = 0; i < bp->rx_nr_rings; i++) { 5880 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5881 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 5882 u32 grp_idx = rxr->bnapi->index; 5883 5884 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 5885 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 5886 5887 hwrm_ring_free_send_msg(bp, ring, 5888 RING_FREE_REQ_RING_TYPE_RX, 5889 close_path ? cmpl_ring_id : 5890 INVALID_HW_RING_ID); 5891 ring->fw_ring_id = INVALID_HW_RING_ID; 5892 bp->grp_info[grp_idx].rx_fw_ring_id = 5893 INVALID_HW_RING_ID; 5894 } 5895 } 5896 5897 if (bp->flags & BNXT_FLAG_CHIP_P5) 5898 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 5899 else 5900 type = RING_FREE_REQ_RING_TYPE_RX; 5901 for (i = 0; i < bp->rx_nr_rings; i++) { 5902 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5903 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 5904 u32 grp_idx = rxr->bnapi->index; 5905 5906 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 5907 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 5908 5909 hwrm_ring_free_send_msg(bp, ring, type, 5910 close_path ? cmpl_ring_id : 5911 INVALID_HW_RING_ID); 5912 ring->fw_ring_id = INVALID_HW_RING_ID; 5913 bp->grp_info[grp_idx].agg_fw_ring_id = 5914 INVALID_HW_RING_ID; 5915 } 5916 } 5917 5918 /* The completion rings are about to be freed. After that the 5919 * IRQ doorbell will not work anymore. So we need to disable 5920 * IRQ here. 5921 */ 5922 bnxt_disable_int_sync(bp); 5923 5924 if (bp->flags & BNXT_FLAG_CHIP_P5) 5925 type = RING_FREE_REQ_RING_TYPE_NQ; 5926 else 5927 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 5928 for (i = 0; i < bp->cp_nr_rings; i++) { 5929 struct bnxt_napi *bnapi = bp->bnapi[i]; 5930 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5931 struct bnxt_ring_struct *ring; 5932 int j; 5933 5934 for (j = 0; j < 2; j++) { 5935 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j]; 5936 5937 if (cpr2) { 5938 ring = &cpr2->cp_ring_struct; 5939 if (ring->fw_ring_id == INVALID_HW_RING_ID) 5940 continue; 5941 hwrm_ring_free_send_msg(bp, ring, 5942 RING_FREE_REQ_RING_TYPE_L2_CMPL, 5943 INVALID_HW_RING_ID); 5944 ring->fw_ring_id = INVALID_HW_RING_ID; 5945 } 5946 } 5947 ring = &cpr->cp_ring_struct; 5948 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 5949 hwrm_ring_free_send_msg(bp, ring, type, 5950 INVALID_HW_RING_ID); 5951 ring->fw_ring_id = INVALID_HW_RING_ID; 5952 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 5953 } 5954 } 5955 } 5956 5957 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 5958 bool shared); 5959 5960 static int bnxt_hwrm_get_rings(struct bnxt *bp) 5961 { 5962 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 5963 struct hwrm_func_qcfg_output *resp; 5964 struct hwrm_func_qcfg_input *req; 5965 int rc; 5966 5967 if (bp->hwrm_spec_code < 0x10601) 5968 return 0; 5969 5970 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 5971 if (rc) 5972 return rc; 5973 5974 req->fid = cpu_to_le16(0xffff); 5975 resp = hwrm_req_hold(bp, req); 5976 rc = hwrm_req_send(bp, req); 5977 if (rc) { 5978 hwrm_req_drop(bp, req); 5979 return rc; 5980 } 5981 5982 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 5983 if (BNXT_NEW_RM(bp)) { 5984 u16 cp, stats; 5985 5986 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 5987 hw_resc->resv_hw_ring_grps = 5988 le32_to_cpu(resp->alloc_hw_ring_grps); 5989 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 5990 cp = le16_to_cpu(resp->alloc_cmpl_rings); 5991 stats = le16_to_cpu(resp->alloc_stat_ctx); 5992 hw_resc->resv_irqs = cp; 5993 if (bp->flags & BNXT_FLAG_CHIP_P5) { 5994 int rx = hw_resc->resv_rx_rings; 5995 int tx = hw_resc->resv_tx_rings; 5996 5997 if (bp->flags & BNXT_FLAG_AGG_RINGS) 5998 rx >>= 1; 5999 if (cp < (rx + tx)) { 6000 bnxt_trim_rings(bp, &rx, &tx, cp, false); 6001 if (bp->flags & BNXT_FLAG_AGG_RINGS) 6002 rx <<= 1; 6003 hw_resc->resv_rx_rings = rx; 6004 hw_resc->resv_tx_rings = tx; 6005 } 6006 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 6007 hw_resc->resv_hw_ring_grps = rx; 6008 } 6009 hw_resc->resv_cp_rings = cp; 6010 hw_resc->resv_stat_ctxs = stats; 6011 } 6012 hwrm_req_drop(bp, req); 6013 return 0; 6014 } 6015 6016 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 6017 { 6018 struct hwrm_func_qcfg_output *resp; 6019 struct hwrm_func_qcfg_input *req; 6020 int rc; 6021 6022 if (bp->hwrm_spec_code < 0x10601) 6023 return 0; 6024 6025 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 6026 if (rc) 6027 return rc; 6028 6029 req->fid = cpu_to_le16(fid); 6030 resp = hwrm_req_hold(bp, req); 6031 rc = hwrm_req_send(bp, req); 6032 if (!rc) 6033 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 6034 6035 hwrm_req_drop(bp, req); 6036 return rc; 6037 } 6038 6039 static bool bnxt_rfs_supported(struct bnxt *bp); 6040 6041 static struct hwrm_func_cfg_input * 6042 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6043 int ring_grps, int cp_rings, int stats, int vnics) 6044 { 6045 struct hwrm_func_cfg_input *req; 6046 u32 enables = 0; 6047 6048 if (hwrm_req_init(bp, req, HWRM_FUNC_CFG)) 6049 return NULL; 6050 6051 req->fid = cpu_to_le16(0xffff); 6052 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 6053 req->num_tx_rings = cpu_to_le16(tx_rings); 6054 if (BNXT_NEW_RM(bp)) { 6055 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 6056 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 6057 if (bp->flags & BNXT_FLAG_CHIP_P5) { 6058 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 6059 enables |= tx_rings + ring_grps ? 6060 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 6061 enables |= rx_rings ? 6062 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 6063 } else { 6064 enables |= cp_rings ? 6065 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 6066 enables |= ring_grps ? 6067 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS | 6068 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 6069 } 6070 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 6071 6072 req->num_rx_rings = cpu_to_le16(rx_rings); 6073 if (bp->flags & BNXT_FLAG_CHIP_P5) { 6074 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps); 6075 req->num_msix = cpu_to_le16(cp_rings); 6076 req->num_rsscos_ctxs = 6077 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64)); 6078 } else { 6079 req->num_cmpl_rings = cpu_to_le16(cp_rings); 6080 req->num_hw_ring_grps = cpu_to_le16(ring_grps); 6081 req->num_rsscos_ctxs = cpu_to_le16(1); 6082 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) && 6083 bnxt_rfs_supported(bp)) 6084 req->num_rsscos_ctxs = 6085 cpu_to_le16(ring_grps + 1); 6086 } 6087 req->num_stat_ctxs = cpu_to_le16(stats); 6088 req->num_vnics = cpu_to_le16(vnics); 6089 } 6090 req->enables = cpu_to_le32(enables); 6091 return req; 6092 } 6093 6094 static struct hwrm_func_vf_cfg_input * 6095 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6096 int ring_grps, int cp_rings, int stats, int vnics) 6097 { 6098 struct hwrm_func_vf_cfg_input *req; 6099 u32 enables = 0; 6100 6101 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 6102 return NULL; 6103 6104 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 6105 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 6106 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 6107 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 6108 if (bp->flags & BNXT_FLAG_CHIP_P5) { 6109 enables |= tx_rings + ring_grps ? 6110 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 6111 } else { 6112 enables |= cp_rings ? 6113 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 6114 enables |= ring_grps ? 6115 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 6116 } 6117 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 6118 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 6119 6120 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 6121 req->num_tx_rings = cpu_to_le16(tx_rings); 6122 req->num_rx_rings = cpu_to_le16(rx_rings); 6123 if (bp->flags & BNXT_FLAG_CHIP_P5) { 6124 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps); 6125 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64)); 6126 } else { 6127 req->num_cmpl_rings = cpu_to_le16(cp_rings); 6128 req->num_hw_ring_grps = cpu_to_le16(ring_grps); 6129 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX); 6130 } 6131 req->num_stat_ctxs = cpu_to_le16(stats); 6132 req->num_vnics = cpu_to_le16(vnics); 6133 6134 req->enables = cpu_to_le32(enables); 6135 return req; 6136 } 6137 6138 static int 6139 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6140 int ring_grps, int cp_rings, int stats, int vnics) 6141 { 6142 struct hwrm_func_cfg_input *req; 6143 int rc; 6144 6145 req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps, 6146 cp_rings, stats, vnics); 6147 if (!req) 6148 return -ENOMEM; 6149 6150 if (!req->enables) { 6151 hwrm_req_drop(bp, req); 6152 return 0; 6153 } 6154 6155 rc = hwrm_req_send(bp, req); 6156 if (rc) 6157 return rc; 6158 6159 if (bp->hwrm_spec_code < 0x10601) 6160 bp->hw_resc.resv_tx_rings = tx_rings; 6161 6162 return bnxt_hwrm_get_rings(bp); 6163 } 6164 6165 static int 6166 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6167 int ring_grps, int cp_rings, int stats, int vnics) 6168 { 6169 struct hwrm_func_vf_cfg_input *req; 6170 int rc; 6171 6172 if (!BNXT_NEW_RM(bp)) { 6173 bp->hw_resc.resv_tx_rings = tx_rings; 6174 return 0; 6175 } 6176 6177 req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps, 6178 cp_rings, stats, vnics); 6179 if (!req) 6180 return -ENOMEM; 6181 6182 rc = hwrm_req_send(bp, req); 6183 if (rc) 6184 return rc; 6185 6186 return bnxt_hwrm_get_rings(bp); 6187 } 6188 6189 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp, 6190 int cp, int stat, int vnic) 6191 { 6192 if (BNXT_PF(bp)) 6193 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat, 6194 vnic); 6195 else 6196 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat, 6197 vnic); 6198 } 6199 6200 int bnxt_nq_rings_in_use(struct bnxt *bp) 6201 { 6202 int cp = bp->cp_nr_rings; 6203 int ulp_msix, ulp_base; 6204 6205 ulp_msix = bnxt_get_ulp_msix_num(bp); 6206 if (ulp_msix) { 6207 ulp_base = bnxt_get_ulp_msix_base(bp); 6208 cp += ulp_msix; 6209 if ((ulp_base + ulp_msix) > cp) 6210 cp = ulp_base + ulp_msix; 6211 } 6212 return cp; 6213 } 6214 6215 static int bnxt_cp_rings_in_use(struct bnxt *bp) 6216 { 6217 int cp; 6218 6219 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 6220 return bnxt_nq_rings_in_use(bp); 6221 6222 cp = bp->tx_nr_rings + bp->rx_nr_rings; 6223 return cp; 6224 } 6225 6226 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 6227 { 6228 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp); 6229 int cp = bp->cp_nr_rings; 6230 6231 if (!ulp_stat) 6232 return cp; 6233 6234 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp)) 6235 return bnxt_get_ulp_msix_base(bp) + ulp_stat; 6236 6237 return cp + ulp_stat; 6238 } 6239 6240 /* Check if a default RSS map needs to be setup. This function is only 6241 * used on older firmware that does not require reserving RX rings. 6242 */ 6243 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 6244 { 6245 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 6246 6247 /* The RSS map is valid for RX rings set to resv_rx_rings */ 6248 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 6249 hw_resc->resv_rx_rings = bp->rx_nr_rings; 6250 if (!netif_is_rxfh_configured(bp->dev)) 6251 bnxt_set_dflt_rss_indir_tbl(bp); 6252 } 6253 } 6254 6255 static bool bnxt_need_reserve_rings(struct bnxt *bp) 6256 { 6257 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 6258 int cp = bnxt_cp_rings_in_use(bp); 6259 int nq = bnxt_nq_rings_in_use(bp); 6260 int rx = bp->rx_nr_rings, stat; 6261 int vnic = 1, grp = rx; 6262 6263 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 6264 bp->hwrm_spec_code >= 0x10601) 6265 return true; 6266 6267 /* Old firmware does not need RX ring reservations but we still 6268 * need to setup a default RSS map when needed. With new firmware 6269 * we go through RX ring reservations first and then set up the 6270 * RSS map for the successfully reserved RX rings when needed. 6271 */ 6272 if (!BNXT_NEW_RM(bp)) { 6273 bnxt_check_rss_tbl_no_rmgr(bp); 6274 return false; 6275 } 6276 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5)) 6277 vnic = rx + 1; 6278 if (bp->flags & BNXT_FLAG_AGG_RINGS) 6279 rx <<= 1; 6280 stat = bnxt_get_func_stat_ctxs(bp); 6281 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 6282 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 6283 (hw_resc->resv_hw_ring_grps != grp && 6284 !(bp->flags & BNXT_FLAG_CHIP_P5))) 6285 return true; 6286 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) && 6287 hw_resc->resv_irqs != nq) 6288 return true; 6289 return false; 6290 } 6291 6292 static int __bnxt_reserve_rings(struct bnxt *bp) 6293 { 6294 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 6295 int cp = bnxt_nq_rings_in_use(bp); 6296 int tx = bp->tx_nr_rings; 6297 int rx = bp->rx_nr_rings; 6298 int grp, rx_rings, rc; 6299 int vnic = 1, stat; 6300 bool sh = false; 6301 6302 if (!bnxt_need_reserve_rings(bp)) 6303 return 0; 6304 6305 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 6306 sh = true; 6307 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5)) 6308 vnic = rx + 1; 6309 if (bp->flags & BNXT_FLAG_AGG_RINGS) 6310 rx <<= 1; 6311 grp = bp->rx_nr_rings; 6312 stat = bnxt_get_func_stat_ctxs(bp); 6313 6314 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic); 6315 if (rc) 6316 return rc; 6317 6318 tx = hw_resc->resv_tx_rings; 6319 if (BNXT_NEW_RM(bp)) { 6320 rx = hw_resc->resv_rx_rings; 6321 cp = hw_resc->resv_irqs; 6322 grp = hw_resc->resv_hw_ring_grps; 6323 vnic = hw_resc->resv_vnics; 6324 stat = hw_resc->resv_stat_ctxs; 6325 } 6326 6327 rx_rings = rx; 6328 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 6329 if (rx >= 2) { 6330 rx_rings = rx >> 1; 6331 } else { 6332 if (netif_running(bp->dev)) 6333 return -ENOMEM; 6334 6335 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 6336 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 6337 bp->dev->hw_features &= ~NETIF_F_LRO; 6338 bp->dev->features &= ~NETIF_F_LRO; 6339 bnxt_set_ring_params(bp); 6340 } 6341 } 6342 rx_rings = min_t(int, rx_rings, grp); 6343 cp = min_t(int, cp, bp->cp_nr_rings); 6344 if (stat > bnxt_get_ulp_stat_ctxs(bp)) 6345 stat -= bnxt_get_ulp_stat_ctxs(bp); 6346 cp = min_t(int, cp, stat); 6347 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh); 6348 if (bp->flags & BNXT_FLAG_AGG_RINGS) 6349 rx = rx_rings << 1; 6350 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings; 6351 bp->tx_nr_rings = tx; 6352 6353 /* If we cannot reserve all the RX rings, reset the RSS map only 6354 * if absolutely necessary 6355 */ 6356 if (rx_rings != bp->rx_nr_rings) { 6357 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 6358 rx_rings, bp->rx_nr_rings); 6359 if (netif_is_rxfh_configured(bp->dev) && 6360 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 6361 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 6362 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 6363 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 6364 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 6365 } 6366 } 6367 bp->rx_nr_rings = rx_rings; 6368 bp->cp_nr_rings = cp; 6369 6370 if (!tx || !rx || !cp || !grp || !vnic || !stat) 6371 return -ENOMEM; 6372 6373 if (!netif_is_rxfh_configured(bp->dev)) 6374 bnxt_set_dflt_rss_indir_tbl(bp); 6375 6376 return rc; 6377 } 6378 6379 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6380 int ring_grps, int cp_rings, int stats, 6381 int vnics) 6382 { 6383 struct hwrm_func_vf_cfg_input *req; 6384 u32 flags; 6385 6386 if (!BNXT_NEW_RM(bp)) 6387 return 0; 6388 6389 req = __bnxt_hwrm_reserve_vf_rings(bp, tx_rings, rx_rings, ring_grps, 6390 cp_rings, stats, vnics); 6391 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 6392 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 6393 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 6394 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 6395 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 6396 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 6397 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 6398 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 6399 6400 req->flags = cpu_to_le32(flags); 6401 return hwrm_req_send_silent(bp, req); 6402 } 6403 6404 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6405 int ring_grps, int cp_rings, int stats, 6406 int vnics) 6407 { 6408 struct hwrm_func_cfg_input *req; 6409 u32 flags; 6410 6411 req = __bnxt_hwrm_reserve_pf_rings(bp, tx_rings, rx_rings, ring_grps, 6412 cp_rings, stats, vnics); 6413 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 6414 if (BNXT_NEW_RM(bp)) { 6415 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 6416 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 6417 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 6418 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 6419 if (bp->flags & BNXT_FLAG_CHIP_P5) 6420 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 6421 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 6422 else 6423 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 6424 } 6425 6426 req->flags = cpu_to_le32(flags); 6427 return hwrm_req_send_silent(bp, req); 6428 } 6429 6430 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings, 6431 int ring_grps, int cp_rings, int stats, 6432 int vnics) 6433 { 6434 if (bp->hwrm_spec_code < 0x10801) 6435 return 0; 6436 6437 if (BNXT_PF(bp)) 6438 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings, 6439 ring_grps, cp_rings, stats, 6440 vnics); 6441 6442 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps, 6443 cp_rings, stats, vnics); 6444 } 6445 6446 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 6447 { 6448 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 6449 struct hwrm_ring_aggint_qcaps_output *resp; 6450 struct hwrm_ring_aggint_qcaps_input *req; 6451 int rc; 6452 6453 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 6454 coal_cap->num_cmpl_dma_aggr_max = 63; 6455 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 6456 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 6457 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 6458 coal_cap->int_lat_tmr_min_max = 65535; 6459 coal_cap->int_lat_tmr_max_max = 65535; 6460 coal_cap->num_cmpl_aggr_int_max = 65535; 6461 coal_cap->timer_units = 80; 6462 6463 if (bp->hwrm_spec_code < 0x10902) 6464 return; 6465 6466 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 6467 return; 6468 6469 resp = hwrm_req_hold(bp, req); 6470 rc = hwrm_req_send_silent(bp, req); 6471 if (!rc) { 6472 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 6473 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 6474 coal_cap->num_cmpl_dma_aggr_max = 6475 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 6476 coal_cap->num_cmpl_dma_aggr_during_int_max = 6477 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 6478 coal_cap->cmpl_aggr_dma_tmr_max = 6479 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 6480 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 6481 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 6482 coal_cap->int_lat_tmr_min_max = 6483 le16_to_cpu(resp->int_lat_tmr_min_max); 6484 coal_cap->int_lat_tmr_max_max = 6485 le16_to_cpu(resp->int_lat_tmr_max_max); 6486 coal_cap->num_cmpl_aggr_int_max = 6487 le16_to_cpu(resp->num_cmpl_aggr_int_max); 6488 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 6489 } 6490 hwrm_req_drop(bp, req); 6491 } 6492 6493 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 6494 { 6495 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 6496 6497 return usec * 1000 / coal_cap->timer_units; 6498 } 6499 6500 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 6501 struct bnxt_coal *hw_coal, 6502 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 6503 { 6504 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 6505 u16 val, tmr, max, flags = hw_coal->flags; 6506 u32 cmpl_params = coal_cap->cmpl_params; 6507 6508 max = hw_coal->bufs_per_record * 128; 6509 if (hw_coal->budget) 6510 max = hw_coal->bufs_per_record * hw_coal->budget; 6511 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 6512 6513 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 6514 req->num_cmpl_aggr_int = cpu_to_le16(val); 6515 6516 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 6517 req->num_cmpl_dma_aggr = cpu_to_le16(val); 6518 6519 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 6520 coal_cap->num_cmpl_dma_aggr_during_int_max); 6521 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 6522 6523 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 6524 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 6525 req->int_lat_tmr_max = cpu_to_le16(tmr); 6526 6527 /* min timer set to 1/2 of interrupt timer */ 6528 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 6529 val = tmr / 2; 6530 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 6531 req->int_lat_tmr_min = cpu_to_le16(val); 6532 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 6533 } 6534 6535 /* buf timer set to 1/4 of interrupt timer */ 6536 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 6537 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 6538 6539 if (cmpl_params & 6540 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 6541 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 6542 val = clamp_t(u16, tmr, 1, 6543 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 6544 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 6545 req->enables |= 6546 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 6547 } 6548 6549 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 6550 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 6551 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 6552 req->flags = cpu_to_le16(flags); 6553 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 6554 } 6555 6556 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 6557 struct bnxt_coal *hw_coal) 6558 { 6559 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 6560 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 6561 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 6562 u32 nq_params = coal_cap->nq_params; 6563 u16 tmr; 6564 int rc; 6565 6566 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 6567 return 0; 6568 6569 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 6570 if (rc) 6571 return rc; 6572 6573 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 6574 req->flags = 6575 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 6576 6577 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 6578 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 6579 req->int_lat_tmr_min = cpu_to_le16(tmr); 6580 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 6581 return hwrm_req_send(bp, req); 6582 } 6583 6584 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 6585 { 6586 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 6587 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 6588 struct bnxt_coal coal; 6589 int rc; 6590 6591 /* Tick values in micro seconds. 6592 * 1 coal_buf x bufs_per_record = 1 completion record. 6593 */ 6594 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 6595 6596 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 6597 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 6598 6599 if (!bnapi->rx_ring) 6600 return -ENODEV; 6601 6602 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 6603 if (rc) 6604 return rc; 6605 6606 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 6607 6608 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 6609 6610 return hwrm_req_send(bp, req_rx); 6611 } 6612 6613 int bnxt_hwrm_set_coal(struct bnxt *bp) 6614 { 6615 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx, 6616 *req; 6617 int i, rc; 6618 6619 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 6620 if (rc) 6621 return rc; 6622 6623 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 6624 if (rc) { 6625 hwrm_req_drop(bp, req_rx); 6626 return rc; 6627 } 6628 6629 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 6630 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 6631 6632 hwrm_req_hold(bp, req_rx); 6633 hwrm_req_hold(bp, req_tx); 6634 for (i = 0; i < bp->cp_nr_rings; i++) { 6635 struct bnxt_napi *bnapi = bp->bnapi[i]; 6636 struct bnxt_coal *hw_coal; 6637 u16 ring_id; 6638 6639 req = req_rx; 6640 if (!bnapi->rx_ring) { 6641 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring); 6642 req = req_tx; 6643 } else { 6644 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 6645 } 6646 req->ring_id = cpu_to_le16(ring_id); 6647 6648 rc = hwrm_req_send(bp, req); 6649 if (rc) 6650 break; 6651 6652 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 6653 continue; 6654 6655 if (bnapi->rx_ring && bnapi->tx_ring) { 6656 req = req_tx; 6657 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring); 6658 req->ring_id = cpu_to_le16(ring_id); 6659 rc = hwrm_req_send(bp, req); 6660 if (rc) 6661 break; 6662 } 6663 if (bnapi->rx_ring) 6664 hw_coal = &bp->rx_coal; 6665 else 6666 hw_coal = &bp->tx_coal; 6667 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 6668 } 6669 hwrm_req_drop(bp, req_rx); 6670 hwrm_req_drop(bp, req_tx); 6671 return rc; 6672 } 6673 6674 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 6675 { 6676 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 6677 struct hwrm_stat_ctx_free_input *req; 6678 int i; 6679 6680 if (!bp->bnapi) 6681 return; 6682 6683 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6684 return; 6685 6686 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 6687 return; 6688 if (BNXT_FW_MAJ(bp) <= 20) { 6689 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 6690 hwrm_req_drop(bp, req); 6691 return; 6692 } 6693 hwrm_req_hold(bp, req0); 6694 } 6695 hwrm_req_hold(bp, req); 6696 for (i = 0; i < bp->cp_nr_rings; i++) { 6697 struct bnxt_napi *bnapi = bp->bnapi[i]; 6698 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 6699 6700 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 6701 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 6702 if (req0) { 6703 req0->stat_ctx_id = req->stat_ctx_id; 6704 hwrm_req_send(bp, req0); 6705 } 6706 hwrm_req_send(bp, req); 6707 6708 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 6709 } 6710 } 6711 hwrm_req_drop(bp, req); 6712 if (req0) 6713 hwrm_req_drop(bp, req0); 6714 } 6715 6716 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 6717 { 6718 struct hwrm_stat_ctx_alloc_output *resp; 6719 struct hwrm_stat_ctx_alloc_input *req; 6720 int rc, i; 6721 6722 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6723 return 0; 6724 6725 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 6726 if (rc) 6727 return rc; 6728 6729 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 6730 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 6731 6732 resp = hwrm_req_hold(bp, req); 6733 for (i = 0; i < bp->cp_nr_rings; i++) { 6734 struct bnxt_napi *bnapi = bp->bnapi[i]; 6735 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 6736 6737 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 6738 6739 rc = hwrm_req_send(bp, req); 6740 if (rc) 6741 break; 6742 6743 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 6744 6745 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 6746 } 6747 hwrm_req_drop(bp, req); 6748 return rc; 6749 } 6750 6751 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 6752 { 6753 struct hwrm_func_qcfg_output *resp; 6754 struct hwrm_func_qcfg_input *req; 6755 u32 min_db_offset = 0; 6756 u16 flags; 6757 int rc; 6758 6759 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 6760 if (rc) 6761 return rc; 6762 6763 req->fid = cpu_to_le16(0xffff); 6764 resp = hwrm_req_hold(bp, req); 6765 rc = hwrm_req_send(bp, req); 6766 if (rc) 6767 goto func_qcfg_exit; 6768 6769 #ifdef CONFIG_BNXT_SRIOV 6770 if (BNXT_VF(bp)) { 6771 struct bnxt_vf_info *vf = &bp->vf; 6772 6773 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 6774 } else { 6775 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 6776 } 6777 #endif 6778 flags = le16_to_cpu(resp->flags); 6779 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 6780 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 6781 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 6782 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 6783 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 6784 } 6785 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 6786 bp->flags |= BNXT_FLAG_MULTI_HOST; 6787 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 6788 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 6789 6790 switch (resp->port_partition_type) { 6791 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 6792 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 6793 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 6794 bp->port_partition_type = resp->port_partition_type; 6795 break; 6796 } 6797 if (bp->hwrm_spec_code < 0x10707 || 6798 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 6799 bp->br_mode = BRIDGE_MODE_VEB; 6800 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 6801 bp->br_mode = BRIDGE_MODE_VEPA; 6802 else 6803 bp->br_mode = BRIDGE_MODE_UNDEF; 6804 6805 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 6806 if (!bp->max_mtu) 6807 bp->max_mtu = BNXT_MAX_MTU; 6808 6809 if (bp->db_size) 6810 goto func_qcfg_exit; 6811 6812 if (bp->flags & BNXT_FLAG_CHIP_P5) { 6813 if (BNXT_PF(bp)) 6814 min_db_offset = DB_PF_OFFSET_P5; 6815 else 6816 min_db_offset = DB_VF_OFFSET_P5; 6817 } 6818 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 6819 1024); 6820 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 6821 bp->db_size <= min_db_offset) 6822 bp->db_size = pci_resource_len(bp->pdev, 2); 6823 6824 func_qcfg_exit: 6825 hwrm_req_drop(bp, req); 6826 return rc; 6827 } 6828 6829 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_info *ctx, 6830 struct hwrm_func_backing_store_qcaps_output *resp) 6831 { 6832 struct bnxt_mem_init *mem_init; 6833 u16 init_mask; 6834 u8 init_val; 6835 u8 *offset; 6836 int i; 6837 6838 init_val = resp->ctx_kind_initializer; 6839 init_mask = le16_to_cpu(resp->ctx_init_mask); 6840 offset = &resp->qp_init_offset; 6841 mem_init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP]; 6842 for (i = 0; i < BNXT_CTX_MEM_INIT_MAX; i++, mem_init++, offset++) { 6843 mem_init->init_val = init_val; 6844 mem_init->offset = BNXT_MEM_INVALID_OFFSET; 6845 if (!init_mask) 6846 continue; 6847 if (i == BNXT_CTX_MEM_INIT_STAT) 6848 offset = &resp->stat_init_offset; 6849 if (init_mask & (1 << i)) 6850 mem_init->offset = *offset * 4; 6851 else 6852 mem_init->init_val = 0; 6853 } 6854 ctx->mem_init[BNXT_CTX_MEM_INIT_QP].size = ctx->qp_entry_size; 6855 ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ].size = ctx->srq_entry_size; 6856 ctx->mem_init[BNXT_CTX_MEM_INIT_CQ].size = ctx->cq_entry_size; 6857 ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC].size = ctx->vnic_entry_size; 6858 ctx->mem_init[BNXT_CTX_MEM_INIT_STAT].size = ctx->stat_entry_size; 6859 ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV].size = ctx->mrav_entry_size; 6860 } 6861 6862 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 6863 { 6864 struct hwrm_func_backing_store_qcaps_output *resp; 6865 struct hwrm_func_backing_store_qcaps_input *req; 6866 int rc; 6867 6868 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx) 6869 return 0; 6870 6871 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 6872 if (rc) 6873 return rc; 6874 6875 resp = hwrm_req_hold(bp, req); 6876 rc = hwrm_req_send_silent(bp, req); 6877 if (!rc) { 6878 struct bnxt_ctx_pg_info *ctx_pg; 6879 struct bnxt_ctx_mem_info *ctx; 6880 int i, tqm_rings; 6881 6882 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 6883 if (!ctx) { 6884 rc = -ENOMEM; 6885 goto ctx_err; 6886 } 6887 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries); 6888 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 6889 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 6890 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size); 6891 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 6892 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries); 6893 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size); 6894 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 6895 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries); 6896 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size); 6897 ctx->vnic_max_vnic_entries = 6898 le16_to_cpu(resp->vnic_max_vnic_entries); 6899 ctx->vnic_max_ring_table_entries = 6900 le16_to_cpu(resp->vnic_max_ring_table_entries); 6901 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size); 6902 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries); 6903 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size); 6904 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size); 6905 ctx->tqm_min_entries_per_ring = 6906 le32_to_cpu(resp->tqm_min_entries_per_ring); 6907 ctx->tqm_max_entries_per_ring = 6908 le32_to_cpu(resp->tqm_max_entries_per_ring); 6909 ctx->tqm_entries_multiple = resp->tqm_entries_multiple; 6910 if (!ctx->tqm_entries_multiple) 6911 ctx->tqm_entries_multiple = 1; 6912 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries); 6913 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size); 6914 ctx->mrav_num_entries_units = 6915 le16_to_cpu(resp->mrav_num_entries_units); 6916 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size); 6917 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries); 6918 6919 bnxt_init_ctx_initializer(ctx, resp); 6920 6921 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 6922 if (!ctx->tqm_fp_rings_count) 6923 ctx->tqm_fp_rings_count = bp->max_q; 6924 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 6925 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 6926 6927 tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS; 6928 ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL); 6929 if (!ctx_pg) { 6930 kfree(ctx); 6931 rc = -ENOMEM; 6932 goto ctx_err; 6933 } 6934 for (i = 0; i < tqm_rings; i++, ctx_pg++) 6935 ctx->tqm_mem[i] = ctx_pg; 6936 bp->ctx = ctx; 6937 } else { 6938 rc = 0; 6939 } 6940 ctx_err: 6941 hwrm_req_drop(bp, req); 6942 return rc; 6943 } 6944 6945 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 6946 __le64 *pg_dir) 6947 { 6948 if (!rmem->nr_pages) 6949 return; 6950 6951 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 6952 if (rmem->depth >= 1) { 6953 if (rmem->depth == 2) 6954 *pg_attr |= 2; 6955 else 6956 *pg_attr |= 1; 6957 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 6958 } else { 6959 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 6960 } 6961 } 6962 6963 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 6964 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 6965 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 6966 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 6967 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 6968 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 6969 6970 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 6971 { 6972 struct hwrm_func_backing_store_cfg_input *req; 6973 struct bnxt_ctx_mem_info *ctx = bp->ctx; 6974 struct bnxt_ctx_pg_info *ctx_pg; 6975 void **__req = (void **)&req; 6976 u32 req_len = sizeof(*req); 6977 __le32 *num_entries; 6978 __le64 *pg_dir; 6979 u32 flags = 0; 6980 u8 *pg_attr; 6981 u32 ena; 6982 int rc; 6983 int i; 6984 6985 if (!ctx) 6986 return 0; 6987 6988 if (req_len > bp->hwrm_max_ext_req_len) 6989 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 6990 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 6991 if (rc) 6992 return rc; 6993 6994 req->enables = cpu_to_le32(enables); 6995 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 6996 ctx_pg = &ctx->qp_mem; 6997 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 6998 req->qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries); 6999 req->qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries); 7000 req->qp_entry_size = cpu_to_le16(ctx->qp_entry_size); 7001 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7002 &req->qpc_pg_size_qpc_lvl, 7003 &req->qpc_page_dir); 7004 } 7005 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 7006 ctx_pg = &ctx->srq_mem; 7007 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 7008 req->srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries); 7009 req->srq_entry_size = cpu_to_le16(ctx->srq_entry_size); 7010 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7011 &req->srq_pg_size_srq_lvl, 7012 &req->srq_page_dir); 7013 } 7014 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 7015 ctx_pg = &ctx->cq_mem; 7016 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 7017 req->cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries); 7018 req->cq_entry_size = cpu_to_le16(ctx->cq_entry_size); 7019 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7020 &req->cq_pg_size_cq_lvl, 7021 &req->cq_page_dir); 7022 } 7023 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 7024 ctx_pg = &ctx->vnic_mem; 7025 req->vnic_num_vnic_entries = 7026 cpu_to_le16(ctx->vnic_max_vnic_entries); 7027 req->vnic_num_ring_table_entries = 7028 cpu_to_le16(ctx->vnic_max_ring_table_entries); 7029 req->vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size); 7030 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7031 &req->vnic_pg_size_vnic_lvl, 7032 &req->vnic_page_dir); 7033 } 7034 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 7035 ctx_pg = &ctx->stat_mem; 7036 req->stat_num_entries = cpu_to_le32(ctx->stat_max_entries); 7037 req->stat_entry_size = cpu_to_le16(ctx->stat_entry_size); 7038 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7039 &req->stat_pg_size_stat_lvl, 7040 &req->stat_page_dir); 7041 } 7042 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 7043 ctx_pg = &ctx->mrav_mem; 7044 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 7045 if (ctx->mrav_num_entries_units) 7046 flags |= 7047 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 7048 req->mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size); 7049 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7050 &req->mrav_pg_size_mrav_lvl, 7051 &req->mrav_page_dir); 7052 } 7053 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 7054 ctx_pg = &ctx->tim_mem; 7055 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 7056 req->tim_entry_size = cpu_to_le16(ctx->tim_entry_size); 7057 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 7058 &req->tim_pg_size_tim_lvl, 7059 &req->tim_page_dir); 7060 } 7061 for (i = 0, num_entries = &req->tqm_sp_num_entries, 7062 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 7063 pg_dir = &req->tqm_sp_page_dir, 7064 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP; 7065 i < BNXT_MAX_TQM_RINGS; 7066 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 7067 if (!(enables & ena)) 7068 continue; 7069 7070 req->tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size); 7071 ctx_pg = ctx->tqm_mem[i]; 7072 *num_entries = cpu_to_le32(ctx_pg->entries); 7073 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 7074 } 7075 req->flags = cpu_to_le32(flags); 7076 return hwrm_req_send(bp, req); 7077 } 7078 7079 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 7080 struct bnxt_ctx_pg_info *ctx_pg) 7081 { 7082 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 7083 7084 rmem->page_size = BNXT_PAGE_SIZE; 7085 rmem->pg_arr = ctx_pg->ctx_pg_arr; 7086 rmem->dma_arr = ctx_pg->ctx_dma_arr; 7087 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 7088 if (rmem->depth >= 1) 7089 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 7090 return bnxt_alloc_ring(bp, rmem); 7091 } 7092 7093 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 7094 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 7095 u8 depth, struct bnxt_mem_init *mem_init) 7096 { 7097 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 7098 int rc; 7099 7100 if (!mem_size) 7101 return -EINVAL; 7102 7103 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 7104 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 7105 ctx_pg->nr_pages = 0; 7106 return -EINVAL; 7107 } 7108 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 7109 int nr_tbls, i; 7110 7111 rmem->depth = 2; 7112 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 7113 GFP_KERNEL); 7114 if (!ctx_pg->ctx_pg_tbl) 7115 return -ENOMEM; 7116 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 7117 rmem->nr_pages = nr_tbls; 7118 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 7119 if (rc) 7120 return rc; 7121 for (i = 0; i < nr_tbls; i++) { 7122 struct bnxt_ctx_pg_info *pg_tbl; 7123 7124 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 7125 if (!pg_tbl) 7126 return -ENOMEM; 7127 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 7128 rmem = &pg_tbl->ring_mem; 7129 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 7130 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 7131 rmem->depth = 1; 7132 rmem->nr_pages = MAX_CTX_PAGES; 7133 rmem->mem_init = mem_init; 7134 if (i == (nr_tbls - 1)) { 7135 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 7136 7137 if (rem) 7138 rmem->nr_pages = rem; 7139 } 7140 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 7141 if (rc) 7142 break; 7143 } 7144 } else { 7145 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 7146 if (rmem->nr_pages > 1 || depth) 7147 rmem->depth = 1; 7148 rmem->mem_init = mem_init; 7149 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 7150 } 7151 return rc; 7152 } 7153 7154 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 7155 struct bnxt_ctx_pg_info *ctx_pg) 7156 { 7157 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 7158 7159 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 7160 ctx_pg->ctx_pg_tbl) { 7161 int i, nr_tbls = rmem->nr_pages; 7162 7163 for (i = 0; i < nr_tbls; i++) { 7164 struct bnxt_ctx_pg_info *pg_tbl; 7165 struct bnxt_ring_mem_info *rmem2; 7166 7167 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 7168 if (!pg_tbl) 7169 continue; 7170 rmem2 = &pg_tbl->ring_mem; 7171 bnxt_free_ring(bp, rmem2); 7172 ctx_pg->ctx_pg_arr[i] = NULL; 7173 kfree(pg_tbl); 7174 ctx_pg->ctx_pg_tbl[i] = NULL; 7175 } 7176 kfree(ctx_pg->ctx_pg_tbl); 7177 ctx_pg->ctx_pg_tbl = NULL; 7178 } 7179 bnxt_free_ring(bp, rmem); 7180 ctx_pg->nr_pages = 0; 7181 } 7182 7183 void bnxt_free_ctx_mem(struct bnxt *bp) 7184 { 7185 struct bnxt_ctx_mem_info *ctx = bp->ctx; 7186 int i; 7187 7188 if (!ctx) 7189 return; 7190 7191 if (ctx->tqm_mem[0]) { 7192 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 7193 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]); 7194 kfree(ctx->tqm_mem[0]); 7195 ctx->tqm_mem[0] = NULL; 7196 } 7197 7198 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem); 7199 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem); 7200 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem); 7201 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem); 7202 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem); 7203 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem); 7204 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem); 7205 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 7206 } 7207 7208 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 7209 { 7210 struct bnxt_ctx_pg_info *ctx_pg; 7211 struct bnxt_ctx_mem_info *ctx; 7212 struct bnxt_mem_init *init; 7213 u32 mem_size, ena, entries; 7214 u32 entries_sp, min; 7215 u32 num_mr, num_ah; 7216 u32 extra_srqs = 0; 7217 u32 extra_qps = 0; 7218 u8 pg_lvl = 1; 7219 int i, rc; 7220 7221 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 7222 if (rc) { 7223 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 7224 rc); 7225 return rc; 7226 } 7227 ctx = bp->ctx; 7228 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 7229 return 0; 7230 7231 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 7232 pg_lvl = 2; 7233 extra_qps = 65536; 7234 extra_srqs = 8192; 7235 } 7236 7237 ctx_pg = &ctx->qp_mem; 7238 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries + 7239 extra_qps; 7240 if (ctx->qp_entry_size) { 7241 mem_size = ctx->qp_entry_size * ctx_pg->entries; 7242 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_QP]; 7243 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init); 7244 if (rc) 7245 return rc; 7246 } 7247 7248 ctx_pg = &ctx->srq_mem; 7249 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs; 7250 if (ctx->srq_entry_size) { 7251 mem_size = ctx->srq_entry_size * ctx_pg->entries; 7252 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_SRQ]; 7253 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init); 7254 if (rc) 7255 return rc; 7256 } 7257 7258 ctx_pg = &ctx->cq_mem; 7259 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2; 7260 if (ctx->cq_entry_size) { 7261 mem_size = ctx->cq_entry_size * ctx_pg->entries; 7262 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_CQ]; 7263 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, init); 7264 if (rc) 7265 return rc; 7266 } 7267 7268 ctx_pg = &ctx->vnic_mem; 7269 ctx_pg->entries = ctx->vnic_max_vnic_entries + 7270 ctx->vnic_max_ring_table_entries; 7271 if (ctx->vnic_entry_size) { 7272 mem_size = ctx->vnic_entry_size * ctx_pg->entries; 7273 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_VNIC]; 7274 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init); 7275 if (rc) 7276 return rc; 7277 } 7278 7279 ctx_pg = &ctx->stat_mem; 7280 ctx_pg->entries = ctx->stat_max_entries; 7281 if (ctx->stat_entry_size) { 7282 mem_size = ctx->stat_entry_size * ctx_pg->entries; 7283 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_STAT]; 7284 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, init); 7285 if (rc) 7286 return rc; 7287 } 7288 7289 ena = 0; 7290 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 7291 goto skip_rdma; 7292 7293 ctx_pg = &ctx->mrav_mem; 7294 /* 128K extra is needed to accommodate static AH context 7295 * allocation by f/w. 7296 */ 7297 num_mr = 1024 * 256; 7298 num_ah = 1024 * 128; 7299 ctx_pg->entries = num_mr + num_ah; 7300 if (ctx->mrav_entry_size) { 7301 mem_size = ctx->mrav_entry_size * ctx_pg->entries; 7302 init = &ctx->mem_init[BNXT_CTX_MEM_INIT_MRAV]; 7303 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, init); 7304 if (rc) 7305 return rc; 7306 } 7307 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 7308 if (ctx->mrav_num_entries_units) 7309 ctx_pg->entries = 7310 ((num_mr / ctx->mrav_num_entries_units) << 16) | 7311 (num_ah / ctx->mrav_num_entries_units); 7312 7313 ctx_pg = &ctx->tim_mem; 7314 ctx_pg->entries = ctx->qp_mem.entries; 7315 if (ctx->tim_entry_size) { 7316 mem_size = ctx->tim_entry_size * ctx_pg->entries; 7317 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, NULL); 7318 if (rc) 7319 return rc; 7320 } 7321 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 7322 7323 skip_rdma: 7324 min = ctx->tqm_min_entries_per_ring; 7325 entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries + 7326 2 * (extra_qps + ctx->qp_min_qp1_entries) + min; 7327 entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple); 7328 entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries); 7329 entries = roundup(entries, ctx->tqm_entries_multiple); 7330 entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring); 7331 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) { 7332 ctx_pg = ctx->tqm_mem[i]; 7333 ctx_pg->entries = i ? entries : entries_sp; 7334 if (ctx->tqm_entry_size) { 7335 mem_size = ctx->tqm_entry_size * ctx_pg->entries; 7336 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, 7337 NULL); 7338 if (rc) 7339 return rc; 7340 } 7341 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 7342 } 7343 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 7344 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 7345 if (rc) { 7346 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 7347 rc); 7348 return rc; 7349 } 7350 ctx->flags |= BNXT_CTX_FLAG_INITED; 7351 return 0; 7352 } 7353 7354 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 7355 { 7356 struct hwrm_func_resource_qcaps_output *resp; 7357 struct hwrm_func_resource_qcaps_input *req; 7358 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7359 int rc; 7360 7361 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 7362 if (rc) 7363 return rc; 7364 7365 req->fid = cpu_to_le16(0xffff); 7366 resp = hwrm_req_hold(bp, req); 7367 rc = hwrm_req_send_silent(bp, req); 7368 if (rc) 7369 goto hwrm_func_resc_qcaps_exit; 7370 7371 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 7372 if (!all) 7373 goto hwrm_func_resc_qcaps_exit; 7374 7375 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 7376 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 7377 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 7378 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 7379 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 7380 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 7381 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 7382 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 7383 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 7384 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 7385 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 7386 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 7387 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 7388 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 7389 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 7390 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 7391 7392 if (bp->flags & BNXT_FLAG_CHIP_P5) { 7393 u16 max_msix = le16_to_cpu(resp->max_msix); 7394 7395 hw_resc->max_nqs = max_msix; 7396 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 7397 } 7398 7399 if (BNXT_PF(bp)) { 7400 struct bnxt_pf_info *pf = &bp->pf; 7401 7402 pf->vf_resv_strategy = 7403 le16_to_cpu(resp->vf_reservation_strategy); 7404 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 7405 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 7406 } 7407 hwrm_func_resc_qcaps_exit: 7408 hwrm_req_drop(bp, req); 7409 return rc; 7410 } 7411 7412 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 7413 { 7414 struct hwrm_port_mac_ptp_qcfg_output *resp; 7415 struct hwrm_port_mac_ptp_qcfg_input *req; 7416 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 7417 u8 flags; 7418 int rc; 7419 7420 if (bp->hwrm_spec_code < 0x10801) { 7421 rc = -ENODEV; 7422 goto no_ptp; 7423 } 7424 7425 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 7426 if (rc) 7427 goto no_ptp; 7428 7429 req->port_id = cpu_to_le16(bp->pf.port_id); 7430 resp = hwrm_req_hold(bp, req); 7431 rc = hwrm_req_send(bp, req); 7432 if (rc) 7433 goto exit; 7434 7435 flags = resp->flags; 7436 if (!(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 7437 rc = -ENODEV; 7438 goto exit; 7439 } 7440 if (!ptp) { 7441 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 7442 if (!ptp) { 7443 rc = -ENOMEM; 7444 goto exit; 7445 } 7446 ptp->bp = bp; 7447 bp->ptp_cfg = ptp; 7448 } 7449 if (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK) { 7450 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 7451 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 7452 } else if (bp->flags & BNXT_FLAG_CHIP_P5) { 7453 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 7454 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 7455 } else { 7456 rc = -ENODEV; 7457 goto exit; 7458 } 7459 rc = bnxt_ptp_init(bp); 7460 if (rc) 7461 netdev_warn(bp->dev, "PTP initialization failed.\n"); 7462 exit: 7463 hwrm_req_drop(bp, req); 7464 if (!rc) 7465 return 0; 7466 7467 no_ptp: 7468 bnxt_ptp_clear(bp); 7469 kfree(ptp); 7470 bp->ptp_cfg = NULL; 7471 return rc; 7472 } 7473 7474 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 7475 { 7476 struct hwrm_func_qcaps_output *resp; 7477 struct hwrm_func_qcaps_input *req; 7478 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7479 u32 flags, flags_ext; 7480 int rc; 7481 7482 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 7483 if (rc) 7484 return rc; 7485 7486 req->fid = cpu_to_le16(0xffff); 7487 resp = hwrm_req_hold(bp, req); 7488 rc = hwrm_req_send(bp, req); 7489 if (rc) 7490 goto hwrm_func_qcaps_exit; 7491 7492 flags = le32_to_cpu(resp->flags); 7493 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 7494 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 7495 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 7496 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 7497 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 7498 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 7499 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 7500 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 7501 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 7502 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 7503 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 7504 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 7505 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 7506 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 7507 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 7508 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 7509 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 7510 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 7511 7512 flags_ext = le32_to_cpu(resp->flags_ext); 7513 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 7514 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 7515 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 7516 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 7517 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 7518 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 7519 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 7520 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 7521 7522 bp->tx_push_thresh = 0; 7523 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 7524 BNXT_FW_MAJ(bp) > 217) 7525 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 7526 7527 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 7528 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 7529 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 7530 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 7531 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 7532 if (!hw_resc->max_hw_ring_grps) 7533 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 7534 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 7535 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 7536 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 7537 7538 if (BNXT_PF(bp)) { 7539 struct bnxt_pf_info *pf = &bp->pf; 7540 7541 pf->fw_fid = le16_to_cpu(resp->fid); 7542 pf->port_id = le16_to_cpu(resp->port_id); 7543 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 7544 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 7545 pf->max_vfs = le16_to_cpu(resp->max_vfs); 7546 pf->max_encap_records = le32_to_cpu(resp->max_encap_records); 7547 pf->max_decap_records = le32_to_cpu(resp->max_decap_records); 7548 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 7549 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 7550 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 7551 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 7552 bp->flags &= ~BNXT_FLAG_WOL_CAP; 7553 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 7554 bp->flags |= BNXT_FLAG_WOL_CAP; 7555 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 7556 __bnxt_hwrm_ptp_qcfg(bp); 7557 } else { 7558 bnxt_ptp_clear(bp); 7559 kfree(bp->ptp_cfg); 7560 bp->ptp_cfg = NULL; 7561 } 7562 } else { 7563 #ifdef CONFIG_BNXT_SRIOV 7564 struct bnxt_vf_info *vf = &bp->vf; 7565 7566 vf->fw_fid = le16_to_cpu(resp->fid); 7567 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 7568 #endif 7569 } 7570 7571 hwrm_func_qcaps_exit: 7572 hwrm_req_drop(bp, req); 7573 return rc; 7574 } 7575 7576 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 7577 { 7578 struct hwrm_dbg_qcaps_output *resp; 7579 struct hwrm_dbg_qcaps_input *req; 7580 int rc; 7581 7582 bp->fw_dbg_cap = 0; 7583 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 7584 return; 7585 7586 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 7587 if (rc) 7588 return; 7589 7590 req->fid = cpu_to_le16(0xffff); 7591 resp = hwrm_req_hold(bp, req); 7592 rc = hwrm_req_send(bp, req); 7593 if (rc) 7594 goto hwrm_dbg_qcaps_exit; 7595 7596 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 7597 7598 hwrm_dbg_qcaps_exit: 7599 hwrm_req_drop(bp, req); 7600 } 7601 7602 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 7603 7604 static int bnxt_hwrm_func_qcaps(struct bnxt *bp) 7605 { 7606 int rc; 7607 7608 rc = __bnxt_hwrm_func_qcaps(bp); 7609 if (rc) 7610 return rc; 7611 7612 bnxt_hwrm_dbg_qcaps(bp); 7613 7614 rc = bnxt_hwrm_queue_qportcfg(bp); 7615 if (rc) { 7616 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 7617 return rc; 7618 } 7619 if (bp->hwrm_spec_code >= 0x10803) { 7620 rc = bnxt_alloc_ctx_mem(bp); 7621 if (rc) 7622 return rc; 7623 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 7624 if (!rc) 7625 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 7626 } 7627 return 0; 7628 } 7629 7630 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 7631 { 7632 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 7633 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 7634 u32 flags; 7635 int rc; 7636 7637 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 7638 return 0; 7639 7640 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 7641 if (rc) 7642 return rc; 7643 7644 resp = hwrm_req_hold(bp, req); 7645 rc = hwrm_req_send(bp, req); 7646 if (rc) 7647 goto hwrm_cfa_adv_qcaps_exit; 7648 7649 flags = le32_to_cpu(resp->flags); 7650 if (flags & 7651 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 7652 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 7653 7654 hwrm_cfa_adv_qcaps_exit: 7655 hwrm_req_drop(bp, req); 7656 return rc; 7657 } 7658 7659 static int __bnxt_alloc_fw_health(struct bnxt *bp) 7660 { 7661 if (bp->fw_health) 7662 return 0; 7663 7664 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 7665 if (!bp->fw_health) 7666 return -ENOMEM; 7667 7668 mutex_init(&bp->fw_health->lock); 7669 return 0; 7670 } 7671 7672 static int bnxt_alloc_fw_health(struct bnxt *bp) 7673 { 7674 int rc; 7675 7676 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 7677 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 7678 return 0; 7679 7680 rc = __bnxt_alloc_fw_health(bp); 7681 if (rc) { 7682 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 7683 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 7684 return rc; 7685 } 7686 7687 return 0; 7688 } 7689 7690 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 7691 { 7692 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 7693 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 7694 BNXT_FW_HEALTH_WIN_MAP_OFF); 7695 } 7696 7697 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 7698 { 7699 struct bnxt_fw_health *fw_health = bp->fw_health; 7700 u32 reg_type; 7701 7702 if (!fw_health) 7703 return; 7704 7705 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 7706 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 7707 fw_health->status_reliable = false; 7708 7709 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 7710 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 7711 fw_health->resets_reliable = false; 7712 } 7713 7714 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 7715 { 7716 void __iomem *hs; 7717 u32 status_loc; 7718 u32 reg_type; 7719 u32 sig; 7720 7721 if (bp->fw_health) 7722 bp->fw_health->status_reliable = false; 7723 7724 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 7725 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 7726 7727 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 7728 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 7729 if (!bp->chip_num) { 7730 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 7731 bp->chip_num = readl(bp->bar0 + 7732 BNXT_FW_HEALTH_WIN_BASE + 7733 BNXT_GRC_REG_CHIP_NUM); 7734 } 7735 if (!BNXT_CHIP_P5(bp)) 7736 return; 7737 7738 status_loc = BNXT_GRC_REG_STATUS_P5 | 7739 BNXT_FW_HEALTH_REG_TYPE_BAR0; 7740 } else { 7741 status_loc = readl(hs + offsetof(struct hcomm_status, 7742 fw_status_loc)); 7743 } 7744 7745 if (__bnxt_alloc_fw_health(bp)) { 7746 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 7747 return; 7748 } 7749 7750 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 7751 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 7752 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 7753 __bnxt_map_fw_health_reg(bp, status_loc); 7754 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 7755 BNXT_FW_HEALTH_WIN_OFF(status_loc); 7756 } 7757 7758 bp->fw_health->status_reliable = true; 7759 } 7760 7761 static int bnxt_map_fw_health_regs(struct bnxt *bp) 7762 { 7763 struct bnxt_fw_health *fw_health = bp->fw_health; 7764 u32 reg_base = 0xffffffff; 7765 int i; 7766 7767 bp->fw_health->status_reliable = false; 7768 bp->fw_health->resets_reliable = false; 7769 /* Only pre-map the monitoring GRC registers using window 3 */ 7770 for (i = 0; i < 4; i++) { 7771 u32 reg = fw_health->regs[i]; 7772 7773 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 7774 continue; 7775 if (reg_base == 0xffffffff) 7776 reg_base = reg & BNXT_GRC_BASE_MASK; 7777 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 7778 return -ERANGE; 7779 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 7780 } 7781 bp->fw_health->status_reliable = true; 7782 bp->fw_health->resets_reliable = true; 7783 if (reg_base == 0xffffffff) 7784 return 0; 7785 7786 __bnxt_map_fw_health_reg(bp, reg_base); 7787 return 0; 7788 } 7789 7790 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 7791 { 7792 struct bnxt_fw_health *fw_health = bp->fw_health; 7793 struct hwrm_error_recovery_qcfg_output *resp; 7794 struct hwrm_error_recovery_qcfg_input *req; 7795 int rc, i; 7796 7797 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 7798 return 0; 7799 7800 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 7801 if (rc) 7802 return rc; 7803 7804 resp = hwrm_req_hold(bp, req); 7805 rc = hwrm_req_send(bp, req); 7806 if (rc) 7807 goto err_recovery_out; 7808 fw_health->flags = le32_to_cpu(resp->flags); 7809 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 7810 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 7811 rc = -EINVAL; 7812 goto err_recovery_out; 7813 } 7814 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 7815 fw_health->master_func_wait_dsecs = 7816 le32_to_cpu(resp->master_func_wait_period); 7817 fw_health->normal_func_wait_dsecs = 7818 le32_to_cpu(resp->normal_func_wait_period); 7819 fw_health->post_reset_wait_dsecs = 7820 le32_to_cpu(resp->master_func_wait_period_after_reset); 7821 fw_health->post_reset_max_wait_dsecs = 7822 le32_to_cpu(resp->max_bailout_time_after_reset); 7823 fw_health->regs[BNXT_FW_HEALTH_REG] = 7824 le32_to_cpu(resp->fw_health_status_reg); 7825 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 7826 le32_to_cpu(resp->fw_heartbeat_reg); 7827 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 7828 le32_to_cpu(resp->fw_reset_cnt_reg); 7829 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 7830 le32_to_cpu(resp->reset_inprogress_reg); 7831 fw_health->fw_reset_inprog_reg_mask = 7832 le32_to_cpu(resp->reset_inprogress_reg_mask); 7833 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 7834 if (fw_health->fw_reset_seq_cnt >= 16) { 7835 rc = -EINVAL; 7836 goto err_recovery_out; 7837 } 7838 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 7839 fw_health->fw_reset_seq_regs[i] = 7840 le32_to_cpu(resp->reset_reg[i]); 7841 fw_health->fw_reset_seq_vals[i] = 7842 le32_to_cpu(resp->reset_reg_val[i]); 7843 fw_health->fw_reset_seq_delay_msec[i] = 7844 resp->delay_after_reset[i]; 7845 } 7846 err_recovery_out: 7847 hwrm_req_drop(bp, req); 7848 if (!rc) 7849 rc = bnxt_map_fw_health_regs(bp); 7850 if (rc) 7851 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 7852 return rc; 7853 } 7854 7855 static int bnxt_hwrm_func_reset(struct bnxt *bp) 7856 { 7857 struct hwrm_func_reset_input *req; 7858 int rc; 7859 7860 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 7861 if (rc) 7862 return rc; 7863 7864 req->enables = 0; 7865 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 7866 return hwrm_req_send(bp, req); 7867 } 7868 7869 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 7870 { 7871 struct hwrm_nvm_get_dev_info_output nvm_info; 7872 7873 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 7874 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 7875 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 7876 nvm_info.nvm_cfg_ver_upd); 7877 } 7878 7879 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 7880 { 7881 struct hwrm_queue_qportcfg_output *resp; 7882 struct hwrm_queue_qportcfg_input *req; 7883 u8 i, j, *qptr; 7884 bool no_rdma; 7885 int rc = 0; 7886 7887 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 7888 if (rc) 7889 return rc; 7890 7891 resp = hwrm_req_hold(bp, req); 7892 rc = hwrm_req_send(bp, req); 7893 if (rc) 7894 goto qportcfg_exit; 7895 7896 if (!resp->max_configurable_queues) { 7897 rc = -EINVAL; 7898 goto qportcfg_exit; 7899 } 7900 bp->max_tc = resp->max_configurable_queues; 7901 bp->max_lltc = resp->max_configurable_lossless_queues; 7902 if (bp->max_tc > BNXT_MAX_QUEUE) 7903 bp->max_tc = BNXT_MAX_QUEUE; 7904 7905 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 7906 qptr = &resp->queue_id0; 7907 for (i = 0, j = 0; i < bp->max_tc; i++) { 7908 bp->q_info[j].queue_id = *qptr; 7909 bp->q_ids[i] = *qptr++; 7910 bp->q_info[j].queue_profile = *qptr++; 7911 bp->tc_to_qidx[j] = j; 7912 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 7913 (no_rdma && BNXT_PF(bp))) 7914 j++; 7915 } 7916 bp->max_q = bp->max_tc; 7917 bp->max_tc = max_t(u8, j, 1); 7918 7919 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 7920 bp->max_tc = 1; 7921 7922 if (bp->max_lltc > bp->max_tc) 7923 bp->max_lltc = bp->max_tc; 7924 7925 qportcfg_exit: 7926 hwrm_req_drop(bp, req); 7927 return rc; 7928 } 7929 7930 static int bnxt_hwrm_poll(struct bnxt *bp) 7931 { 7932 struct hwrm_ver_get_input *req; 7933 int rc; 7934 7935 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 7936 if (rc) 7937 return rc; 7938 7939 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 7940 req->hwrm_intf_min = HWRM_VERSION_MINOR; 7941 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 7942 7943 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 7944 rc = hwrm_req_send(bp, req); 7945 return rc; 7946 } 7947 7948 static int bnxt_hwrm_ver_get(struct bnxt *bp) 7949 { 7950 struct hwrm_ver_get_output *resp; 7951 struct hwrm_ver_get_input *req; 7952 u16 fw_maj, fw_min, fw_bld, fw_rsv; 7953 u32 dev_caps_cfg, hwrm_ver; 7954 int rc, len; 7955 7956 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 7957 if (rc) 7958 return rc; 7959 7960 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 7961 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 7962 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 7963 req->hwrm_intf_min = HWRM_VERSION_MINOR; 7964 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 7965 7966 resp = hwrm_req_hold(bp, req); 7967 rc = hwrm_req_send(bp, req); 7968 if (rc) 7969 goto hwrm_ver_get_exit; 7970 7971 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 7972 7973 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 7974 resp->hwrm_intf_min_8b << 8 | 7975 resp->hwrm_intf_upd_8b; 7976 if (resp->hwrm_intf_maj_8b < 1) { 7977 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 7978 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 7979 resp->hwrm_intf_upd_8b); 7980 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 7981 } 7982 7983 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 7984 HWRM_VERSION_UPDATE; 7985 7986 if (bp->hwrm_spec_code > hwrm_ver) 7987 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 7988 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 7989 HWRM_VERSION_UPDATE); 7990 else 7991 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 7992 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 7993 resp->hwrm_intf_upd_8b); 7994 7995 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 7996 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 7997 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 7998 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 7999 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 8000 len = FW_VER_STR_LEN; 8001 } else { 8002 fw_maj = resp->hwrm_fw_maj_8b; 8003 fw_min = resp->hwrm_fw_min_8b; 8004 fw_bld = resp->hwrm_fw_bld_8b; 8005 fw_rsv = resp->hwrm_fw_rsvd_8b; 8006 len = BC_HWRM_STR_LEN; 8007 } 8008 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 8009 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 8010 fw_rsv); 8011 8012 if (strlen(resp->active_pkg_name)) { 8013 int fw_ver_len = strlen(bp->fw_ver_str); 8014 8015 snprintf(bp->fw_ver_str + fw_ver_len, 8016 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 8017 resp->active_pkg_name); 8018 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 8019 } 8020 8021 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 8022 if (!bp->hwrm_cmd_timeout) 8023 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 8024 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 8025 if (!bp->hwrm_cmd_max_timeout) 8026 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 8027 else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT) 8028 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n", 8029 bp->hwrm_cmd_max_timeout / 1000); 8030 8031 if (resp->hwrm_intf_maj_8b >= 1) { 8032 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 8033 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 8034 } 8035 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 8036 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 8037 8038 bp->chip_num = le16_to_cpu(resp->chip_num); 8039 bp->chip_rev = resp->chip_rev; 8040 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 8041 !resp->chip_metal) 8042 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 8043 8044 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 8045 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 8046 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 8047 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 8048 8049 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 8050 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 8051 8052 if (dev_caps_cfg & 8053 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 8054 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 8055 8056 if (dev_caps_cfg & 8057 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 8058 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 8059 8060 if (dev_caps_cfg & 8061 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 8062 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 8063 8064 hwrm_ver_get_exit: 8065 hwrm_req_drop(bp, req); 8066 return rc; 8067 } 8068 8069 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 8070 { 8071 struct hwrm_fw_set_time_input *req; 8072 struct tm tm; 8073 time64_t now = ktime_get_real_seconds(); 8074 int rc; 8075 8076 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 8077 bp->hwrm_spec_code < 0x10400) 8078 return -EOPNOTSUPP; 8079 8080 time64_to_tm(now, 0, &tm); 8081 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 8082 if (rc) 8083 return rc; 8084 8085 req->year = cpu_to_le16(1900 + tm.tm_year); 8086 req->month = 1 + tm.tm_mon; 8087 req->day = tm.tm_mday; 8088 req->hour = tm.tm_hour; 8089 req->minute = tm.tm_min; 8090 req->second = tm.tm_sec; 8091 return hwrm_req_send(bp, req); 8092 } 8093 8094 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 8095 { 8096 u64 sw_tmp; 8097 8098 hw &= mask; 8099 sw_tmp = (*sw & ~mask) | hw; 8100 if (hw < (*sw & mask)) 8101 sw_tmp += mask + 1; 8102 WRITE_ONCE(*sw, sw_tmp); 8103 } 8104 8105 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 8106 int count, bool ignore_zero) 8107 { 8108 int i; 8109 8110 for (i = 0; i < count; i++) { 8111 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 8112 8113 if (ignore_zero && !hw) 8114 continue; 8115 8116 if (masks[i] == -1ULL) 8117 sw_stats[i] = hw; 8118 else 8119 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 8120 } 8121 } 8122 8123 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 8124 { 8125 if (!stats->hw_stats) 8126 return; 8127 8128 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 8129 stats->hw_masks, stats->len / 8, false); 8130 } 8131 8132 static void bnxt_accumulate_all_stats(struct bnxt *bp) 8133 { 8134 struct bnxt_stats_mem *ring0_stats; 8135 bool ignore_zero = false; 8136 int i; 8137 8138 /* Chip bug. Counter intermittently becomes 0. */ 8139 if (bp->flags & BNXT_FLAG_CHIP_P5) 8140 ignore_zero = true; 8141 8142 for (i = 0; i < bp->cp_nr_rings; i++) { 8143 struct bnxt_napi *bnapi = bp->bnapi[i]; 8144 struct bnxt_cp_ring_info *cpr; 8145 struct bnxt_stats_mem *stats; 8146 8147 cpr = &bnapi->cp_ring; 8148 stats = &cpr->stats; 8149 if (!i) 8150 ring0_stats = stats; 8151 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 8152 ring0_stats->hw_masks, 8153 ring0_stats->len / 8, ignore_zero); 8154 } 8155 if (bp->flags & BNXT_FLAG_PORT_STATS) { 8156 struct bnxt_stats_mem *stats = &bp->port_stats; 8157 __le64 *hw_stats = stats->hw_stats; 8158 u64 *sw_stats = stats->sw_stats; 8159 u64 *masks = stats->hw_masks; 8160 int cnt; 8161 8162 cnt = sizeof(struct rx_port_stats) / 8; 8163 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 8164 8165 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 8166 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 8167 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 8168 cnt = sizeof(struct tx_port_stats) / 8; 8169 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 8170 } 8171 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 8172 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 8173 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 8174 } 8175 } 8176 8177 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 8178 { 8179 struct hwrm_port_qstats_input *req; 8180 struct bnxt_pf_info *pf = &bp->pf; 8181 int rc; 8182 8183 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 8184 return 0; 8185 8186 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 8187 return -EOPNOTSUPP; 8188 8189 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 8190 if (rc) 8191 return rc; 8192 8193 req->flags = flags; 8194 req->port_id = cpu_to_le16(pf->port_id); 8195 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 8196 BNXT_TX_PORT_STATS_BYTE_OFFSET); 8197 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 8198 return hwrm_req_send(bp, req); 8199 } 8200 8201 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 8202 { 8203 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 8204 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 8205 struct hwrm_port_qstats_ext_output *resp_qs; 8206 struct hwrm_port_qstats_ext_input *req_qs; 8207 struct bnxt_pf_info *pf = &bp->pf; 8208 u32 tx_stat_size; 8209 int rc; 8210 8211 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 8212 return 0; 8213 8214 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 8215 return -EOPNOTSUPP; 8216 8217 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 8218 if (rc) 8219 return rc; 8220 8221 req_qs->flags = flags; 8222 req_qs->port_id = cpu_to_le16(pf->port_id); 8223 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 8224 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 8225 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 8226 sizeof(struct tx_port_stats_ext) : 0; 8227 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 8228 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 8229 resp_qs = hwrm_req_hold(bp, req_qs); 8230 rc = hwrm_req_send(bp, req_qs); 8231 if (!rc) { 8232 bp->fw_rx_stats_ext_size = 8233 le16_to_cpu(resp_qs->rx_stat_size) / 8; 8234 if (BNXT_FW_MAJ(bp) < 220 && 8235 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 8236 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 8237 8238 bp->fw_tx_stats_ext_size = tx_stat_size ? 8239 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 8240 } else { 8241 bp->fw_rx_stats_ext_size = 0; 8242 bp->fw_tx_stats_ext_size = 0; 8243 } 8244 hwrm_req_drop(bp, req_qs); 8245 8246 if (flags) 8247 return rc; 8248 8249 if (bp->fw_tx_stats_ext_size <= 8250 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 8251 bp->pri2cos_valid = 0; 8252 return rc; 8253 } 8254 8255 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 8256 if (rc) 8257 return rc; 8258 8259 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 8260 8261 resp_qc = hwrm_req_hold(bp, req_qc); 8262 rc = hwrm_req_send(bp, req_qc); 8263 if (!rc) { 8264 u8 *pri2cos; 8265 int i, j; 8266 8267 pri2cos = &resp_qc->pri0_cos_queue_id; 8268 for (i = 0; i < 8; i++) { 8269 u8 queue_id = pri2cos[i]; 8270 u8 queue_idx; 8271 8272 /* Per port queue IDs start from 0, 10, 20, etc */ 8273 queue_idx = queue_id % 10; 8274 if (queue_idx > BNXT_MAX_QUEUE) { 8275 bp->pri2cos_valid = false; 8276 hwrm_req_drop(bp, req_qc); 8277 return rc; 8278 } 8279 for (j = 0; j < bp->max_q; j++) { 8280 if (bp->q_ids[j] == queue_id) 8281 bp->pri2cos_idx[i] = queue_idx; 8282 } 8283 } 8284 bp->pri2cos_valid = true; 8285 } 8286 hwrm_req_drop(bp, req_qc); 8287 8288 return rc; 8289 } 8290 8291 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 8292 { 8293 bnxt_hwrm_tunnel_dst_port_free(bp, 8294 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 8295 bnxt_hwrm_tunnel_dst_port_free(bp, 8296 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 8297 } 8298 8299 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 8300 { 8301 int rc, i; 8302 u32 tpa_flags = 0; 8303 8304 if (set_tpa) 8305 tpa_flags = bp->flags & BNXT_FLAG_TPA; 8306 else if (BNXT_NO_FW_ACCESS(bp)) 8307 return 0; 8308 for (i = 0; i < bp->nr_vnics; i++) { 8309 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags); 8310 if (rc) { 8311 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 8312 i, rc); 8313 return rc; 8314 } 8315 } 8316 return 0; 8317 } 8318 8319 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 8320 { 8321 int i; 8322 8323 for (i = 0; i < bp->nr_vnics; i++) 8324 bnxt_hwrm_vnic_set_rss(bp, i, false); 8325 } 8326 8327 static void bnxt_clear_vnic(struct bnxt *bp) 8328 { 8329 if (!bp->vnic_info) 8330 return; 8331 8332 bnxt_hwrm_clear_vnic_filter(bp); 8333 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) { 8334 /* clear all RSS setting before free vnic ctx */ 8335 bnxt_hwrm_clear_vnic_rss(bp); 8336 bnxt_hwrm_vnic_ctx_free(bp); 8337 } 8338 /* before free the vnic, undo the vnic tpa settings */ 8339 if (bp->flags & BNXT_FLAG_TPA) 8340 bnxt_set_tpa(bp, false); 8341 bnxt_hwrm_vnic_free(bp); 8342 if (bp->flags & BNXT_FLAG_CHIP_P5) 8343 bnxt_hwrm_vnic_ctx_free(bp); 8344 } 8345 8346 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 8347 bool irq_re_init) 8348 { 8349 bnxt_clear_vnic(bp); 8350 bnxt_hwrm_ring_free(bp, close_path); 8351 bnxt_hwrm_ring_grp_free(bp); 8352 if (irq_re_init) { 8353 bnxt_hwrm_stat_ctx_free(bp); 8354 bnxt_hwrm_free_tunnel_ports(bp); 8355 } 8356 } 8357 8358 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 8359 { 8360 struct hwrm_func_cfg_input *req; 8361 u8 evb_mode; 8362 int rc; 8363 8364 if (br_mode == BRIDGE_MODE_VEB) 8365 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 8366 else if (br_mode == BRIDGE_MODE_VEPA) 8367 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 8368 else 8369 return -EINVAL; 8370 8371 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG); 8372 if (rc) 8373 return rc; 8374 8375 req->fid = cpu_to_le16(0xffff); 8376 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 8377 req->evb_mode = evb_mode; 8378 return hwrm_req_send(bp, req); 8379 } 8380 8381 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 8382 { 8383 struct hwrm_func_cfg_input *req; 8384 int rc; 8385 8386 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 8387 return 0; 8388 8389 rc = hwrm_req_init(bp, req, HWRM_FUNC_CFG); 8390 if (rc) 8391 return rc; 8392 8393 req->fid = cpu_to_le16(0xffff); 8394 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 8395 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 8396 if (size == 128) 8397 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 8398 8399 return hwrm_req_send(bp, req); 8400 } 8401 8402 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id) 8403 { 8404 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 8405 int rc; 8406 8407 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 8408 goto skip_rss_ctx; 8409 8410 /* allocate context for vnic */ 8411 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0); 8412 if (rc) { 8413 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 8414 vnic_id, rc); 8415 goto vnic_setup_err; 8416 } 8417 bp->rsscos_nr_ctxs++; 8418 8419 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 8420 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1); 8421 if (rc) { 8422 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 8423 vnic_id, rc); 8424 goto vnic_setup_err; 8425 } 8426 bp->rsscos_nr_ctxs++; 8427 } 8428 8429 skip_rss_ctx: 8430 /* configure default vnic, ring grp */ 8431 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id); 8432 if (rc) { 8433 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 8434 vnic_id, rc); 8435 goto vnic_setup_err; 8436 } 8437 8438 /* Enable RSS hashing on vnic */ 8439 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true); 8440 if (rc) { 8441 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 8442 vnic_id, rc); 8443 goto vnic_setup_err; 8444 } 8445 8446 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 8447 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id); 8448 if (rc) { 8449 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 8450 vnic_id, rc); 8451 } 8452 } 8453 8454 vnic_setup_err: 8455 return rc; 8456 } 8457 8458 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id) 8459 { 8460 int rc, i, nr_ctxs; 8461 8462 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 8463 for (i = 0; i < nr_ctxs; i++) { 8464 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i); 8465 if (rc) { 8466 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 8467 vnic_id, i, rc); 8468 break; 8469 } 8470 bp->rsscos_nr_ctxs++; 8471 } 8472 if (i < nr_ctxs) 8473 return -ENOMEM; 8474 8475 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true); 8476 if (rc) { 8477 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 8478 vnic_id, rc); 8479 return rc; 8480 } 8481 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id); 8482 if (rc) { 8483 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 8484 vnic_id, rc); 8485 return rc; 8486 } 8487 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 8488 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id); 8489 if (rc) { 8490 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 8491 vnic_id, rc); 8492 } 8493 } 8494 return rc; 8495 } 8496 8497 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id) 8498 { 8499 if (bp->flags & BNXT_FLAG_CHIP_P5) 8500 return __bnxt_setup_vnic_p5(bp, vnic_id); 8501 else 8502 return __bnxt_setup_vnic(bp, vnic_id); 8503 } 8504 8505 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 8506 { 8507 #ifdef CONFIG_RFS_ACCEL 8508 int i, rc = 0; 8509 8510 if (bp->flags & BNXT_FLAG_CHIP_P5) 8511 return 0; 8512 8513 for (i = 0; i < bp->rx_nr_rings; i++) { 8514 struct bnxt_vnic_info *vnic; 8515 u16 vnic_id = i + 1; 8516 u16 ring_id = i; 8517 8518 if (vnic_id >= bp->nr_vnics) 8519 break; 8520 8521 vnic = &bp->vnic_info[vnic_id]; 8522 vnic->flags |= BNXT_VNIC_RFS_FLAG; 8523 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP) 8524 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 8525 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1); 8526 if (rc) { 8527 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 8528 vnic_id, rc); 8529 break; 8530 } 8531 rc = bnxt_setup_vnic(bp, vnic_id); 8532 if (rc) 8533 break; 8534 } 8535 return rc; 8536 #else 8537 return 0; 8538 #endif 8539 } 8540 8541 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 8542 static bool bnxt_promisc_ok(struct bnxt *bp) 8543 { 8544 #ifdef CONFIG_BNXT_SRIOV 8545 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 8546 return false; 8547 #endif 8548 return true; 8549 } 8550 8551 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 8552 { 8553 unsigned int rc = 0; 8554 8555 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1); 8556 if (rc) { 8557 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 8558 rc); 8559 return rc; 8560 } 8561 8562 rc = bnxt_hwrm_vnic_cfg(bp, 1); 8563 if (rc) { 8564 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 8565 rc); 8566 return rc; 8567 } 8568 return rc; 8569 } 8570 8571 static int bnxt_cfg_rx_mode(struct bnxt *); 8572 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 8573 8574 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 8575 { 8576 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 8577 int rc = 0; 8578 unsigned int rx_nr_rings = bp->rx_nr_rings; 8579 8580 if (irq_re_init) { 8581 rc = bnxt_hwrm_stat_ctx_alloc(bp); 8582 if (rc) { 8583 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 8584 rc); 8585 goto err_out; 8586 } 8587 } 8588 8589 rc = bnxt_hwrm_ring_alloc(bp); 8590 if (rc) { 8591 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 8592 goto err_out; 8593 } 8594 8595 rc = bnxt_hwrm_ring_grp_alloc(bp); 8596 if (rc) { 8597 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 8598 goto err_out; 8599 } 8600 8601 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8602 rx_nr_rings--; 8603 8604 /* default vnic 0 */ 8605 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings); 8606 if (rc) { 8607 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 8608 goto err_out; 8609 } 8610 8611 rc = bnxt_setup_vnic(bp, 0); 8612 if (rc) 8613 goto err_out; 8614 8615 if (bp->flags & BNXT_FLAG_RFS) { 8616 rc = bnxt_alloc_rfs_vnics(bp); 8617 if (rc) 8618 goto err_out; 8619 } 8620 8621 if (bp->flags & BNXT_FLAG_TPA) { 8622 rc = bnxt_set_tpa(bp, true); 8623 if (rc) 8624 goto err_out; 8625 } 8626 8627 if (BNXT_VF(bp)) 8628 bnxt_update_vf_mac(bp); 8629 8630 /* Filter for default vnic 0 */ 8631 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 8632 if (rc) { 8633 if (BNXT_VF(bp) && rc == -ENODEV) 8634 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 8635 else 8636 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 8637 goto err_out; 8638 } 8639 vnic->uc_filter_count = 1; 8640 8641 vnic->rx_mask = 0; 8642 if (bp->dev->flags & IFF_BROADCAST) 8643 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 8644 8645 if (bp->dev->flags & IFF_PROMISC) 8646 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 8647 8648 if (bp->dev->flags & IFF_ALLMULTI) { 8649 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 8650 vnic->mc_list_count = 0; 8651 } else { 8652 u32 mask = 0; 8653 8654 bnxt_mc_list_updated(bp, &mask); 8655 vnic->rx_mask |= mask; 8656 } 8657 8658 rc = bnxt_cfg_rx_mode(bp); 8659 if (rc) 8660 goto err_out; 8661 8662 rc = bnxt_hwrm_set_coal(bp); 8663 if (rc) 8664 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 8665 rc); 8666 8667 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 8668 rc = bnxt_setup_nitroa0_vnic(bp); 8669 if (rc) 8670 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 8671 rc); 8672 } 8673 8674 if (BNXT_VF(bp)) { 8675 bnxt_hwrm_func_qcfg(bp); 8676 netdev_update_features(bp->dev); 8677 } 8678 8679 return 0; 8680 8681 err_out: 8682 bnxt_hwrm_resource_free(bp, 0, true); 8683 8684 return rc; 8685 } 8686 8687 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 8688 { 8689 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 8690 return 0; 8691 } 8692 8693 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 8694 { 8695 bnxt_init_cp_rings(bp); 8696 bnxt_init_rx_rings(bp); 8697 bnxt_init_tx_rings(bp); 8698 bnxt_init_ring_grps(bp, irq_re_init); 8699 bnxt_init_vnics(bp); 8700 8701 return bnxt_init_chip(bp, irq_re_init); 8702 } 8703 8704 static int bnxt_set_real_num_queues(struct bnxt *bp) 8705 { 8706 int rc; 8707 struct net_device *dev = bp->dev; 8708 8709 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 8710 bp->tx_nr_rings_xdp); 8711 if (rc) 8712 return rc; 8713 8714 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 8715 if (rc) 8716 return rc; 8717 8718 #ifdef CONFIG_RFS_ACCEL 8719 if (bp->flags & BNXT_FLAG_RFS) 8720 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 8721 #endif 8722 8723 return rc; 8724 } 8725 8726 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 8727 bool shared) 8728 { 8729 int _rx = *rx, _tx = *tx; 8730 8731 if (shared) { 8732 *rx = min_t(int, _rx, max); 8733 *tx = min_t(int, _tx, max); 8734 } else { 8735 if (max < 2) 8736 return -ENOMEM; 8737 8738 while (_rx + _tx > max) { 8739 if (_rx > _tx && _rx > 1) 8740 _rx--; 8741 else if (_tx > 1) 8742 _tx--; 8743 } 8744 *rx = _rx; 8745 *tx = _tx; 8746 } 8747 return 0; 8748 } 8749 8750 static void bnxt_setup_msix(struct bnxt *bp) 8751 { 8752 const int len = sizeof(bp->irq_tbl[0].name); 8753 struct net_device *dev = bp->dev; 8754 int tcs, i; 8755 8756 tcs = netdev_get_num_tc(dev); 8757 if (tcs) { 8758 int i, off, count; 8759 8760 for (i = 0; i < tcs; i++) { 8761 count = bp->tx_nr_rings_per_tc; 8762 off = i * count; 8763 netdev_set_tc_queue(dev, i, count, off); 8764 } 8765 } 8766 8767 for (i = 0; i < bp->cp_nr_rings; i++) { 8768 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 8769 char *attr; 8770 8771 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 8772 attr = "TxRx"; 8773 else if (i < bp->rx_nr_rings) 8774 attr = "rx"; 8775 else 8776 attr = "tx"; 8777 8778 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 8779 attr, i); 8780 bp->irq_tbl[map_idx].handler = bnxt_msix; 8781 } 8782 } 8783 8784 static void bnxt_setup_inta(struct bnxt *bp) 8785 { 8786 const int len = sizeof(bp->irq_tbl[0].name); 8787 8788 if (netdev_get_num_tc(bp->dev)) 8789 netdev_reset_tc(bp->dev); 8790 8791 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx", 8792 0); 8793 bp->irq_tbl[0].handler = bnxt_inta; 8794 } 8795 8796 static int bnxt_init_int_mode(struct bnxt *bp); 8797 8798 static int bnxt_setup_int_mode(struct bnxt *bp) 8799 { 8800 int rc; 8801 8802 if (!bp->irq_tbl) { 8803 rc = bnxt_init_int_mode(bp); 8804 if (rc || !bp->irq_tbl) 8805 return rc ?: -ENODEV; 8806 } 8807 8808 if (bp->flags & BNXT_FLAG_USING_MSIX) 8809 bnxt_setup_msix(bp); 8810 else 8811 bnxt_setup_inta(bp); 8812 8813 rc = bnxt_set_real_num_queues(bp); 8814 return rc; 8815 } 8816 8817 #ifdef CONFIG_RFS_ACCEL 8818 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 8819 { 8820 return bp->hw_resc.max_rsscos_ctxs; 8821 } 8822 8823 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 8824 { 8825 return bp->hw_resc.max_vnics; 8826 } 8827 #endif 8828 8829 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 8830 { 8831 return bp->hw_resc.max_stat_ctxs; 8832 } 8833 8834 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 8835 { 8836 return bp->hw_resc.max_cp_rings; 8837 } 8838 8839 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 8840 { 8841 unsigned int cp = bp->hw_resc.max_cp_rings; 8842 8843 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 8844 cp -= bnxt_get_ulp_msix_num(bp); 8845 8846 return cp; 8847 } 8848 8849 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 8850 { 8851 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8852 8853 if (bp->flags & BNXT_FLAG_CHIP_P5) 8854 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 8855 8856 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 8857 } 8858 8859 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 8860 { 8861 bp->hw_resc.max_irqs = max_irqs; 8862 } 8863 8864 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 8865 { 8866 unsigned int cp; 8867 8868 cp = bnxt_get_max_func_cp_rings_for_en(bp); 8869 if (bp->flags & BNXT_FLAG_CHIP_P5) 8870 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 8871 else 8872 return cp - bp->cp_nr_rings; 8873 } 8874 8875 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 8876 { 8877 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 8878 } 8879 8880 int bnxt_get_avail_msix(struct bnxt *bp, int num) 8881 { 8882 int max_cp = bnxt_get_max_func_cp_rings(bp); 8883 int max_irq = bnxt_get_max_func_irqs(bp); 8884 int total_req = bp->cp_nr_rings + num; 8885 int max_idx, avail_msix; 8886 8887 max_idx = bp->total_irqs; 8888 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 8889 max_idx = min_t(int, bp->total_irqs, max_cp); 8890 avail_msix = max_idx - bp->cp_nr_rings; 8891 if (!BNXT_NEW_RM(bp) || avail_msix >= num) 8892 return avail_msix; 8893 8894 if (max_irq < total_req) { 8895 num = max_irq - bp->cp_nr_rings; 8896 if (num <= 0) 8897 return 0; 8898 } 8899 return num; 8900 } 8901 8902 static int bnxt_get_num_msix(struct bnxt *bp) 8903 { 8904 if (!BNXT_NEW_RM(bp)) 8905 return bnxt_get_max_func_irqs(bp); 8906 8907 return bnxt_nq_rings_in_use(bp); 8908 } 8909 8910 static int bnxt_init_msix(struct bnxt *bp) 8911 { 8912 int i, total_vecs, max, rc = 0, min = 1, ulp_msix; 8913 struct msix_entry *msix_ent; 8914 8915 total_vecs = bnxt_get_num_msix(bp); 8916 max = bnxt_get_max_func_irqs(bp); 8917 if (total_vecs > max) 8918 total_vecs = max; 8919 8920 if (!total_vecs) 8921 return 0; 8922 8923 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL); 8924 if (!msix_ent) 8925 return -ENOMEM; 8926 8927 for (i = 0; i < total_vecs; i++) { 8928 msix_ent[i].entry = i; 8929 msix_ent[i].vector = 0; 8930 } 8931 8932 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 8933 min = 2; 8934 8935 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs); 8936 ulp_msix = bnxt_get_ulp_msix_num(bp); 8937 if (total_vecs < 0 || total_vecs < ulp_msix) { 8938 rc = -ENODEV; 8939 goto msix_setup_exit; 8940 } 8941 8942 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL); 8943 if (bp->irq_tbl) { 8944 for (i = 0; i < total_vecs; i++) 8945 bp->irq_tbl[i].vector = msix_ent[i].vector; 8946 8947 bp->total_irqs = total_vecs; 8948 /* Trim rings based upon num of vectors allocated */ 8949 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 8950 total_vecs - ulp_msix, min == 1); 8951 if (rc) 8952 goto msix_setup_exit; 8953 8954 bp->cp_nr_rings = (min == 1) ? 8955 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) : 8956 bp->tx_nr_rings + bp->rx_nr_rings; 8957 8958 } else { 8959 rc = -ENOMEM; 8960 goto msix_setup_exit; 8961 } 8962 bp->flags |= BNXT_FLAG_USING_MSIX; 8963 kfree(msix_ent); 8964 return 0; 8965 8966 msix_setup_exit: 8967 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc); 8968 kfree(bp->irq_tbl); 8969 bp->irq_tbl = NULL; 8970 pci_disable_msix(bp->pdev); 8971 kfree(msix_ent); 8972 return rc; 8973 } 8974 8975 static int bnxt_init_inta(struct bnxt *bp) 8976 { 8977 bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL); 8978 if (!bp->irq_tbl) 8979 return -ENOMEM; 8980 8981 bp->total_irqs = 1; 8982 bp->rx_nr_rings = 1; 8983 bp->tx_nr_rings = 1; 8984 bp->cp_nr_rings = 1; 8985 bp->flags |= BNXT_FLAG_SHARED_RINGS; 8986 bp->irq_tbl[0].vector = bp->pdev->irq; 8987 return 0; 8988 } 8989 8990 static int bnxt_init_int_mode(struct bnxt *bp) 8991 { 8992 int rc = -ENODEV; 8993 8994 if (bp->flags & BNXT_FLAG_MSIX_CAP) 8995 rc = bnxt_init_msix(bp); 8996 8997 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) { 8998 /* fallback to INTA */ 8999 rc = bnxt_init_inta(bp); 9000 } 9001 return rc; 9002 } 9003 9004 static void bnxt_clear_int_mode(struct bnxt *bp) 9005 { 9006 if (bp->flags & BNXT_FLAG_USING_MSIX) 9007 pci_disable_msix(bp->pdev); 9008 9009 kfree(bp->irq_tbl); 9010 bp->irq_tbl = NULL; 9011 bp->flags &= ~BNXT_FLAG_USING_MSIX; 9012 } 9013 9014 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 9015 { 9016 int tcs = netdev_get_num_tc(bp->dev); 9017 bool irq_cleared = false; 9018 int rc; 9019 9020 if (!bnxt_need_reserve_rings(bp)) 9021 return 0; 9022 9023 if (irq_re_init && BNXT_NEW_RM(bp) && 9024 bnxt_get_num_msix(bp) != bp->total_irqs) { 9025 bnxt_ulp_irq_stop(bp); 9026 bnxt_clear_int_mode(bp); 9027 irq_cleared = true; 9028 } 9029 rc = __bnxt_reserve_rings(bp); 9030 if (irq_cleared) { 9031 if (!rc) 9032 rc = bnxt_init_int_mode(bp); 9033 bnxt_ulp_irq_restart(bp, rc); 9034 } 9035 if (rc) { 9036 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 9037 return rc; 9038 } 9039 if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) { 9040 netdev_err(bp->dev, "tx ring reservation failure\n"); 9041 netdev_reset_tc(bp->dev); 9042 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 9043 return -ENOMEM; 9044 } 9045 return 0; 9046 } 9047 9048 static void bnxt_free_irq(struct bnxt *bp) 9049 { 9050 struct bnxt_irq *irq; 9051 int i; 9052 9053 #ifdef CONFIG_RFS_ACCEL 9054 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 9055 bp->dev->rx_cpu_rmap = NULL; 9056 #endif 9057 if (!bp->irq_tbl || !bp->bnapi) 9058 return; 9059 9060 for (i = 0; i < bp->cp_nr_rings; i++) { 9061 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 9062 9063 irq = &bp->irq_tbl[map_idx]; 9064 if (irq->requested) { 9065 if (irq->have_cpumask) { 9066 irq_set_affinity_hint(irq->vector, NULL); 9067 free_cpumask_var(irq->cpu_mask); 9068 irq->have_cpumask = 0; 9069 } 9070 free_irq(irq->vector, bp->bnapi[i]); 9071 } 9072 9073 irq->requested = 0; 9074 } 9075 } 9076 9077 static int bnxt_request_irq(struct bnxt *bp) 9078 { 9079 int i, j, rc = 0; 9080 unsigned long flags = 0; 9081 #ifdef CONFIG_RFS_ACCEL 9082 struct cpu_rmap *rmap; 9083 #endif 9084 9085 rc = bnxt_setup_int_mode(bp); 9086 if (rc) { 9087 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 9088 rc); 9089 return rc; 9090 } 9091 #ifdef CONFIG_RFS_ACCEL 9092 rmap = bp->dev->rx_cpu_rmap; 9093 #endif 9094 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) 9095 flags = IRQF_SHARED; 9096 9097 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 9098 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 9099 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 9100 9101 #ifdef CONFIG_RFS_ACCEL 9102 if (rmap && bp->bnapi[i]->rx_ring) { 9103 rc = irq_cpu_rmap_add(rmap, irq->vector); 9104 if (rc) 9105 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 9106 j); 9107 j++; 9108 } 9109 #endif 9110 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 9111 bp->bnapi[i]); 9112 if (rc) 9113 break; 9114 9115 irq->requested = 1; 9116 9117 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 9118 int numa_node = dev_to_node(&bp->pdev->dev); 9119 9120 irq->have_cpumask = 1; 9121 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 9122 irq->cpu_mask); 9123 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask); 9124 if (rc) { 9125 netdev_warn(bp->dev, 9126 "Set affinity failed, IRQ = %d\n", 9127 irq->vector); 9128 break; 9129 } 9130 } 9131 } 9132 return rc; 9133 } 9134 9135 static void bnxt_del_napi(struct bnxt *bp) 9136 { 9137 int i; 9138 9139 if (!bp->bnapi) 9140 return; 9141 9142 for (i = 0; i < bp->cp_nr_rings; i++) { 9143 struct bnxt_napi *bnapi = bp->bnapi[i]; 9144 9145 __netif_napi_del(&bnapi->napi); 9146 } 9147 /* We called __netif_napi_del(), we need 9148 * to respect an RCU grace period before freeing napi structures. 9149 */ 9150 synchronize_net(); 9151 } 9152 9153 static void bnxt_init_napi(struct bnxt *bp) 9154 { 9155 int i; 9156 unsigned int cp_nr_rings = bp->cp_nr_rings; 9157 struct bnxt_napi *bnapi; 9158 9159 if (bp->flags & BNXT_FLAG_USING_MSIX) { 9160 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 9161 9162 if (bp->flags & BNXT_FLAG_CHIP_P5) 9163 poll_fn = bnxt_poll_p5; 9164 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 9165 cp_nr_rings--; 9166 for (i = 0; i < cp_nr_rings; i++) { 9167 bnapi = bp->bnapi[i]; 9168 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64); 9169 } 9170 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 9171 bnapi = bp->bnapi[cp_nr_rings]; 9172 netif_napi_add(bp->dev, &bnapi->napi, 9173 bnxt_poll_nitroa0, 64); 9174 } 9175 } else { 9176 bnapi = bp->bnapi[0]; 9177 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64); 9178 } 9179 } 9180 9181 static void bnxt_disable_napi(struct bnxt *bp) 9182 { 9183 int i; 9184 9185 if (!bp->bnapi || 9186 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 9187 return; 9188 9189 for (i = 0; i < bp->cp_nr_rings; i++) { 9190 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 9191 9192 napi_disable(&bp->bnapi[i]->napi); 9193 if (bp->bnapi[i]->rx_ring) 9194 cancel_work_sync(&cpr->dim.work); 9195 } 9196 } 9197 9198 static void bnxt_enable_napi(struct bnxt *bp) 9199 { 9200 int i; 9201 9202 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 9203 for (i = 0; i < bp->cp_nr_rings; i++) { 9204 struct bnxt_napi *bnapi = bp->bnapi[i]; 9205 struct bnxt_cp_ring_info *cpr; 9206 9207 cpr = &bnapi->cp_ring; 9208 if (bnapi->in_reset) 9209 cpr->sw_stats.rx.rx_resets++; 9210 bnapi->in_reset = false; 9211 9212 if (bnapi->rx_ring) { 9213 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 9214 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 9215 } 9216 napi_enable(&bnapi->napi); 9217 } 9218 } 9219 9220 void bnxt_tx_disable(struct bnxt *bp) 9221 { 9222 int i; 9223 struct bnxt_tx_ring_info *txr; 9224 9225 if (bp->tx_ring) { 9226 for (i = 0; i < bp->tx_nr_rings; i++) { 9227 txr = &bp->tx_ring[i]; 9228 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 9229 } 9230 } 9231 /* Make sure napi polls see @dev_state change */ 9232 synchronize_net(); 9233 /* Drop carrier first to prevent TX timeout */ 9234 netif_carrier_off(bp->dev); 9235 /* Stop all TX queues */ 9236 netif_tx_disable(bp->dev); 9237 } 9238 9239 void bnxt_tx_enable(struct bnxt *bp) 9240 { 9241 int i; 9242 struct bnxt_tx_ring_info *txr; 9243 9244 for (i = 0; i < bp->tx_nr_rings; i++) { 9245 txr = &bp->tx_ring[i]; 9246 WRITE_ONCE(txr->dev_state, 0); 9247 } 9248 /* Make sure napi polls see @dev_state change */ 9249 synchronize_net(); 9250 netif_tx_wake_all_queues(bp->dev); 9251 if (bp->link_info.link_up) 9252 netif_carrier_on(bp->dev); 9253 } 9254 9255 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 9256 { 9257 u8 active_fec = link_info->active_fec_sig_mode & 9258 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 9259 9260 switch (active_fec) { 9261 default: 9262 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 9263 return "None"; 9264 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 9265 return "Clause 74 BaseR"; 9266 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 9267 return "Clause 91 RS(528,514)"; 9268 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 9269 return "Clause 91 RS544_1XN"; 9270 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 9271 return "Clause 91 RS(544,514)"; 9272 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 9273 return "Clause 91 RS272_1XN"; 9274 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 9275 return "Clause 91 RS(272,257)"; 9276 } 9277 } 9278 9279 void bnxt_report_link(struct bnxt *bp) 9280 { 9281 if (bp->link_info.link_up) { 9282 const char *signal = ""; 9283 const char *flow_ctrl; 9284 const char *duplex; 9285 u32 speed; 9286 u16 fec; 9287 9288 netif_carrier_on(bp->dev); 9289 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 9290 if (speed == SPEED_UNKNOWN) { 9291 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 9292 return; 9293 } 9294 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 9295 duplex = "full"; 9296 else 9297 duplex = "half"; 9298 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 9299 flow_ctrl = "ON - receive & transmit"; 9300 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 9301 flow_ctrl = "ON - transmit"; 9302 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 9303 flow_ctrl = "ON - receive"; 9304 else 9305 flow_ctrl = "none"; 9306 if (bp->link_info.phy_qcfg_resp.option_flags & 9307 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 9308 u8 sig_mode = bp->link_info.active_fec_sig_mode & 9309 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 9310 switch (sig_mode) { 9311 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 9312 signal = "(NRZ) "; 9313 break; 9314 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 9315 signal = "(PAM4) "; 9316 break; 9317 default: 9318 break; 9319 } 9320 } 9321 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 9322 speed, signal, duplex, flow_ctrl); 9323 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 9324 netdev_info(bp->dev, "EEE is %s\n", 9325 bp->eee.eee_active ? "active" : 9326 "not active"); 9327 fec = bp->link_info.fec_cfg; 9328 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 9329 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 9330 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 9331 bnxt_report_fec(&bp->link_info)); 9332 } else { 9333 netif_carrier_off(bp->dev); 9334 netdev_err(bp->dev, "NIC Link is Down\n"); 9335 } 9336 } 9337 9338 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 9339 { 9340 if (!resp->supported_speeds_auto_mode && 9341 !resp->supported_speeds_force_mode && 9342 !resp->supported_pam4_speeds_auto_mode && 9343 !resp->supported_pam4_speeds_force_mode) 9344 return true; 9345 return false; 9346 } 9347 9348 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 9349 { 9350 struct bnxt_link_info *link_info = &bp->link_info; 9351 struct hwrm_port_phy_qcaps_output *resp; 9352 struct hwrm_port_phy_qcaps_input *req; 9353 int rc = 0; 9354 9355 if (bp->hwrm_spec_code < 0x10201) 9356 return 0; 9357 9358 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 9359 if (rc) 9360 return rc; 9361 9362 resp = hwrm_req_hold(bp, req); 9363 rc = hwrm_req_send(bp, req); 9364 if (rc) 9365 goto hwrm_phy_qcaps_exit; 9366 9367 bp->phy_flags = resp->flags; 9368 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 9369 struct ethtool_eee *eee = &bp->eee; 9370 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 9371 9372 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0); 9373 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 9374 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 9375 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 9376 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 9377 } 9378 9379 if (bp->hwrm_spec_code >= 0x10a01) { 9380 if (bnxt_phy_qcaps_no_speed(resp)) { 9381 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 9382 netdev_warn(bp->dev, "Ethernet link disabled\n"); 9383 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 9384 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 9385 netdev_info(bp->dev, "Ethernet link enabled\n"); 9386 /* Phy re-enabled, reprobe the speeds */ 9387 link_info->support_auto_speeds = 0; 9388 link_info->support_pam4_auto_speeds = 0; 9389 } 9390 } 9391 if (resp->supported_speeds_auto_mode) 9392 link_info->support_auto_speeds = 9393 le16_to_cpu(resp->supported_speeds_auto_mode); 9394 if (resp->supported_pam4_speeds_auto_mode) 9395 link_info->support_pam4_auto_speeds = 9396 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 9397 9398 bp->port_count = resp->port_cnt; 9399 9400 hwrm_phy_qcaps_exit: 9401 hwrm_req_drop(bp, req); 9402 return rc; 9403 } 9404 9405 static bool bnxt_support_dropped(u16 advertising, u16 supported) 9406 { 9407 u16 diff = advertising ^ supported; 9408 9409 return ((supported | diff) != supported); 9410 } 9411 9412 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 9413 { 9414 struct bnxt_link_info *link_info = &bp->link_info; 9415 struct hwrm_port_phy_qcfg_output *resp; 9416 struct hwrm_port_phy_qcfg_input *req; 9417 u8 link_up = link_info->link_up; 9418 bool support_changed = false; 9419 int rc; 9420 9421 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 9422 if (rc) 9423 return rc; 9424 9425 resp = hwrm_req_hold(bp, req); 9426 rc = hwrm_req_send(bp, req); 9427 if (rc) { 9428 hwrm_req_drop(bp, req); 9429 if (BNXT_VF(bp) && rc == -ENODEV) { 9430 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 9431 rc = 0; 9432 } 9433 return rc; 9434 } 9435 9436 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 9437 link_info->phy_link_status = resp->link; 9438 link_info->duplex = resp->duplex_cfg; 9439 if (bp->hwrm_spec_code >= 0x10800) 9440 link_info->duplex = resp->duplex_state; 9441 link_info->pause = resp->pause; 9442 link_info->auto_mode = resp->auto_mode; 9443 link_info->auto_pause_setting = resp->auto_pause; 9444 link_info->lp_pause = resp->link_partner_adv_pause; 9445 link_info->force_pause_setting = resp->force_pause; 9446 link_info->duplex_setting = resp->duplex_cfg; 9447 if (link_info->phy_link_status == BNXT_LINK_LINK) 9448 link_info->link_speed = le16_to_cpu(resp->link_speed); 9449 else 9450 link_info->link_speed = 0; 9451 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 9452 link_info->force_pam4_link_speed = 9453 le16_to_cpu(resp->force_pam4_link_speed); 9454 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 9455 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 9456 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 9457 link_info->auto_pam4_link_speeds = 9458 le16_to_cpu(resp->auto_pam4_link_speed_mask); 9459 link_info->lp_auto_link_speeds = 9460 le16_to_cpu(resp->link_partner_adv_speeds); 9461 link_info->lp_auto_pam4_link_speeds = 9462 resp->link_partner_pam4_adv_speeds; 9463 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 9464 link_info->phy_ver[0] = resp->phy_maj; 9465 link_info->phy_ver[1] = resp->phy_min; 9466 link_info->phy_ver[2] = resp->phy_bld; 9467 link_info->media_type = resp->media_type; 9468 link_info->phy_type = resp->phy_type; 9469 link_info->transceiver = resp->xcvr_pkg_type; 9470 link_info->phy_addr = resp->eee_config_phy_addr & 9471 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 9472 link_info->module_status = resp->module_status; 9473 9474 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 9475 struct ethtool_eee *eee = &bp->eee; 9476 u16 fw_speeds; 9477 9478 eee->eee_active = 0; 9479 if (resp->eee_config_phy_addr & 9480 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 9481 eee->eee_active = 1; 9482 fw_speeds = le16_to_cpu( 9483 resp->link_partner_adv_eee_link_speed_mask); 9484 eee->lp_advertised = 9485 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0); 9486 } 9487 9488 /* Pull initial EEE config */ 9489 if (!chng_link_state) { 9490 if (resp->eee_config_phy_addr & 9491 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 9492 eee->eee_enabled = 1; 9493 9494 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 9495 eee->advertised = 9496 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0); 9497 9498 if (resp->eee_config_phy_addr & 9499 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 9500 __le32 tmr; 9501 9502 eee->tx_lpi_enabled = 1; 9503 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 9504 eee->tx_lpi_timer = le32_to_cpu(tmr) & 9505 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 9506 } 9507 } 9508 } 9509 9510 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 9511 if (bp->hwrm_spec_code >= 0x10504) { 9512 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 9513 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 9514 } 9515 /* TODO: need to add more logic to report VF link */ 9516 if (chng_link_state) { 9517 if (link_info->phy_link_status == BNXT_LINK_LINK) 9518 link_info->link_up = 1; 9519 else 9520 link_info->link_up = 0; 9521 if (link_up != link_info->link_up) 9522 bnxt_report_link(bp); 9523 } else { 9524 /* alwasy link down if not require to update link state */ 9525 link_info->link_up = 0; 9526 } 9527 hwrm_req_drop(bp, req); 9528 9529 if (!BNXT_PHY_CFG_ABLE(bp)) 9530 return 0; 9531 9532 /* Check if any advertised speeds are no longer supported. The caller 9533 * holds the link_lock mutex, so we can modify link_info settings. 9534 */ 9535 if (bnxt_support_dropped(link_info->advertising, 9536 link_info->support_auto_speeds)) { 9537 link_info->advertising = link_info->support_auto_speeds; 9538 support_changed = true; 9539 } 9540 if (bnxt_support_dropped(link_info->advertising_pam4, 9541 link_info->support_pam4_auto_speeds)) { 9542 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 9543 support_changed = true; 9544 } 9545 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 9546 bnxt_hwrm_set_link_setting(bp, true, false); 9547 return 0; 9548 } 9549 9550 static void bnxt_get_port_module_status(struct bnxt *bp) 9551 { 9552 struct bnxt_link_info *link_info = &bp->link_info; 9553 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 9554 u8 module_status; 9555 9556 if (bnxt_update_link(bp, true)) 9557 return; 9558 9559 module_status = link_info->module_status; 9560 switch (module_status) { 9561 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 9562 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 9563 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 9564 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 9565 bp->pf.port_id); 9566 if (bp->hwrm_spec_code >= 0x10201) { 9567 netdev_warn(bp->dev, "Module part number %s\n", 9568 resp->phy_vendor_partnumber); 9569 } 9570 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 9571 netdev_warn(bp->dev, "TX is disabled\n"); 9572 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 9573 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 9574 } 9575 } 9576 9577 static void 9578 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 9579 { 9580 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 9581 if (bp->hwrm_spec_code >= 0x10201) 9582 req->auto_pause = 9583 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 9584 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 9585 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 9586 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 9587 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 9588 req->enables |= 9589 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 9590 } else { 9591 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 9592 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 9593 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 9594 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 9595 req->enables |= 9596 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 9597 if (bp->hwrm_spec_code >= 0x10201) { 9598 req->auto_pause = req->force_pause; 9599 req->enables |= cpu_to_le32( 9600 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 9601 } 9602 } 9603 } 9604 9605 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 9606 { 9607 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 9608 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 9609 if (bp->link_info.advertising) { 9610 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 9611 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 9612 } 9613 if (bp->link_info.advertising_pam4) { 9614 req->enables |= 9615 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 9616 req->auto_link_pam4_speed_mask = 9617 cpu_to_le16(bp->link_info.advertising_pam4); 9618 } 9619 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 9620 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 9621 } else { 9622 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 9623 if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 9624 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 9625 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 9626 } else { 9627 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 9628 } 9629 } 9630 9631 /* tell chimp that the setting takes effect immediately */ 9632 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 9633 } 9634 9635 int bnxt_hwrm_set_pause(struct bnxt *bp) 9636 { 9637 struct hwrm_port_phy_cfg_input *req; 9638 int rc; 9639 9640 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 9641 if (rc) 9642 return rc; 9643 9644 bnxt_hwrm_set_pause_common(bp, req); 9645 9646 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 9647 bp->link_info.force_link_chng) 9648 bnxt_hwrm_set_link_common(bp, req); 9649 9650 rc = hwrm_req_send(bp, req); 9651 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 9652 /* since changing of pause setting doesn't trigger any link 9653 * change event, the driver needs to update the current pause 9654 * result upon successfully return of the phy_cfg command 9655 */ 9656 bp->link_info.pause = 9657 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 9658 bp->link_info.auto_pause_setting = 0; 9659 if (!bp->link_info.force_link_chng) 9660 bnxt_report_link(bp); 9661 } 9662 bp->link_info.force_link_chng = false; 9663 return rc; 9664 } 9665 9666 static void bnxt_hwrm_set_eee(struct bnxt *bp, 9667 struct hwrm_port_phy_cfg_input *req) 9668 { 9669 struct ethtool_eee *eee = &bp->eee; 9670 9671 if (eee->eee_enabled) { 9672 u16 eee_speeds; 9673 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 9674 9675 if (eee->tx_lpi_enabled) 9676 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 9677 else 9678 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 9679 9680 req->flags |= cpu_to_le32(flags); 9681 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 9682 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 9683 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 9684 } else { 9685 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 9686 } 9687 } 9688 9689 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 9690 { 9691 struct hwrm_port_phy_cfg_input *req; 9692 int rc; 9693 9694 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 9695 if (rc) 9696 return rc; 9697 9698 if (set_pause) 9699 bnxt_hwrm_set_pause_common(bp, req); 9700 9701 bnxt_hwrm_set_link_common(bp, req); 9702 9703 if (set_eee) 9704 bnxt_hwrm_set_eee(bp, req); 9705 return hwrm_req_send(bp, req); 9706 } 9707 9708 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 9709 { 9710 struct hwrm_port_phy_cfg_input *req; 9711 int rc; 9712 9713 if (!BNXT_SINGLE_PF(bp)) 9714 return 0; 9715 9716 if (pci_num_vf(bp->pdev) && 9717 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 9718 return 0; 9719 9720 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 9721 if (rc) 9722 return rc; 9723 9724 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 9725 return hwrm_req_send(bp, req); 9726 } 9727 9728 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 9729 { 9730 #ifdef CONFIG_TEE_BNXT_FW 9731 int rc = tee_bnxt_fw_load(); 9732 9733 if (rc) 9734 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 9735 9736 return rc; 9737 #else 9738 netdev_err(bp->dev, "OP-TEE not supported\n"); 9739 return -ENODEV; 9740 #endif 9741 } 9742 9743 static int bnxt_try_recover_fw(struct bnxt *bp) 9744 { 9745 if (bp->fw_health && bp->fw_health->status_reliable) { 9746 int retry = 0, rc; 9747 u32 sts; 9748 9749 do { 9750 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 9751 rc = bnxt_hwrm_poll(bp); 9752 if (!BNXT_FW_IS_BOOTING(sts) && 9753 !BNXT_FW_IS_RECOVERING(sts)) 9754 break; 9755 retry++; 9756 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 9757 9758 if (!BNXT_FW_IS_HEALTHY(sts)) { 9759 netdev_err(bp->dev, 9760 "Firmware not responding, status: 0x%x\n", 9761 sts); 9762 rc = -ENODEV; 9763 } 9764 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 9765 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 9766 return bnxt_fw_reset_via_optee(bp); 9767 } 9768 return rc; 9769 } 9770 9771 return -ENODEV; 9772 } 9773 9774 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 9775 { 9776 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9777 int rc; 9778 9779 if (!BNXT_NEW_RM(bp)) 9780 return 0; /* no resource reservations required */ 9781 9782 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9783 if (rc) 9784 netdev_err(bp->dev, "resc_qcaps failed\n"); 9785 9786 hw_resc->resv_cp_rings = 0; 9787 hw_resc->resv_stat_ctxs = 0; 9788 hw_resc->resv_irqs = 0; 9789 hw_resc->resv_tx_rings = 0; 9790 hw_resc->resv_rx_rings = 0; 9791 hw_resc->resv_hw_ring_grps = 0; 9792 hw_resc->resv_vnics = 0; 9793 if (!fw_reset) { 9794 bp->tx_nr_rings = 0; 9795 bp->rx_nr_rings = 0; 9796 } 9797 9798 return rc; 9799 } 9800 9801 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 9802 { 9803 struct hwrm_func_drv_if_change_output *resp; 9804 struct hwrm_func_drv_if_change_input *req; 9805 bool fw_reset = !bp->irq_tbl; 9806 bool resc_reinit = false; 9807 int rc, retry = 0; 9808 u32 flags = 0; 9809 9810 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 9811 return 0; 9812 9813 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 9814 if (rc) 9815 return rc; 9816 9817 if (up) 9818 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 9819 resp = hwrm_req_hold(bp, req); 9820 9821 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 9822 while (retry < BNXT_FW_IF_RETRY) { 9823 rc = hwrm_req_send(bp, req); 9824 if (rc != -EAGAIN) 9825 break; 9826 9827 msleep(50); 9828 retry++; 9829 } 9830 9831 if (rc == -EAGAIN) { 9832 hwrm_req_drop(bp, req); 9833 return rc; 9834 } else if (!rc) { 9835 flags = le32_to_cpu(resp->flags); 9836 } else if (up) { 9837 rc = bnxt_try_recover_fw(bp); 9838 fw_reset = true; 9839 } 9840 hwrm_req_drop(bp, req); 9841 if (rc) 9842 return rc; 9843 9844 if (!up) { 9845 bnxt_inv_fw_health_reg(bp); 9846 return 0; 9847 } 9848 9849 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 9850 resc_reinit = true; 9851 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE) 9852 fw_reset = true; 9853 else if (bp->fw_health && !bp->fw_health->status_reliable) 9854 bnxt_try_map_fw_health_reg(bp); 9855 9856 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 9857 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 9858 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 9859 return -ENODEV; 9860 } 9861 if (resc_reinit || fw_reset) { 9862 if (fw_reset) { 9863 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 9864 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 9865 bnxt_ulp_stop(bp); 9866 bnxt_free_ctx_mem(bp); 9867 kfree(bp->ctx); 9868 bp->ctx = NULL; 9869 bnxt_dcb_free(bp); 9870 rc = bnxt_fw_init_one(bp); 9871 if (rc) { 9872 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 9873 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 9874 return rc; 9875 } 9876 bnxt_clear_int_mode(bp); 9877 rc = bnxt_init_int_mode(bp); 9878 if (rc) { 9879 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 9880 netdev_err(bp->dev, "init int mode failed\n"); 9881 return rc; 9882 } 9883 } 9884 rc = bnxt_cancel_reservations(bp, fw_reset); 9885 } 9886 return rc; 9887 } 9888 9889 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 9890 { 9891 struct hwrm_port_led_qcaps_output *resp; 9892 struct hwrm_port_led_qcaps_input *req; 9893 struct bnxt_pf_info *pf = &bp->pf; 9894 int rc; 9895 9896 bp->num_leds = 0; 9897 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 9898 return 0; 9899 9900 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 9901 if (rc) 9902 return rc; 9903 9904 req->port_id = cpu_to_le16(pf->port_id); 9905 resp = hwrm_req_hold(bp, req); 9906 rc = hwrm_req_send(bp, req); 9907 if (rc) { 9908 hwrm_req_drop(bp, req); 9909 return rc; 9910 } 9911 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 9912 int i; 9913 9914 bp->num_leds = resp->num_leds; 9915 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 9916 bp->num_leds); 9917 for (i = 0; i < bp->num_leds; i++) { 9918 struct bnxt_led_info *led = &bp->leds[i]; 9919 __le16 caps = led->led_state_caps; 9920 9921 if (!led->led_group_id || 9922 !BNXT_LED_ALT_BLINK_CAP(caps)) { 9923 bp->num_leds = 0; 9924 break; 9925 } 9926 } 9927 } 9928 hwrm_req_drop(bp, req); 9929 return 0; 9930 } 9931 9932 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 9933 { 9934 struct hwrm_wol_filter_alloc_output *resp; 9935 struct hwrm_wol_filter_alloc_input *req; 9936 int rc; 9937 9938 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 9939 if (rc) 9940 return rc; 9941 9942 req->port_id = cpu_to_le16(bp->pf.port_id); 9943 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 9944 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 9945 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 9946 9947 resp = hwrm_req_hold(bp, req); 9948 rc = hwrm_req_send(bp, req); 9949 if (!rc) 9950 bp->wol_filter_id = resp->wol_filter_id; 9951 hwrm_req_drop(bp, req); 9952 return rc; 9953 } 9954 9955 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 9956 { 9957 struct hwrm_wol_filter_free_input *req; 9958 int rc; 9959 9960 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 9961 if (rc) 9962 return rc; 9963 9964 req->port_id = cpu_to_le16(bp->pf.port_id); 9965 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 9966 req->wol_filter_id = bp->wol_filter_id; 9967 9968 return hwrm_req_send(bp, req); 9969 } 9970 9971 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 9972 { 9973 struct hwrm_wol_filter_qcfg_output *resp; 9974 struct hwrm_wol_filter_qcfg_input *req; 9975 u16 next_handle = 0; 9976 int rc; 9977 9978 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 9979 if (rc) 9980 return rc; 9981 9982 req->port_id = cpu_to_le16(bp->pf.port_id); 9983 req->handle = cpu_to_le16(handle); 9984 resp = hwrm_req_hold(bp, req); 9985 rc = hwrm_req_send(bp, req); 9986 if (!rc) { 9987 next_handle = le16_to_cpu(resp->next_handle); 9988 if (next_handle != 0) { 9989 if (resp->wol_type == 9990 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 9991 bp->wol = 1; 9992 bp->wol_filter_id = resp->wol_filter_id; 9993 } 9994 } 9995 } 9996 hwrm_req_drop(bp, req); 9997 return next_handle; 9998 } 9999 10000 static void bnxt_get_wol_settings(struct bnxt *bp) 10001 { 10002 u16 handle = 0; 10003 10004 bp->wol = 0; 10005 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 10006 return; 10007 10008 do { 10009 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 10010 } while (handle && handle != 0xffff); 10011 } 10012 10013 #ifdef CONFIG_BNXT_HWMON 10014 static ssize_t bnxt_show_temp(struct device *dev, 10015 struct device_attribute *devattr, char *buf) 10016 { 10017 struct hwrm_temp_monitor_query_output *resp; 10018 struct hwrm_temp_monitor_query_input *req; 10019 struct bnxt *bp = dev_get_drvdata(dev); 10020 u32 len = 0; 10021 int rc; 10022 10023 rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY); 10024 if (rc) 10025 return rc; 10026 resp = hwrm_req_hold(bp, req); 10027 rc = hwrm_req_send(bp, req); 10028 if (!rc) 10029 len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */ 10030 hwrm_req_drop(bp, req); 10031 if (rc) 10032 return rc; 10033 return len; 10034 } 10035 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0); 10036 10037 static struct attribute *bnxt_attrs[] = { 10038 &sensor_dev_attr_temp1_input.dev_attr.attr, 10039 NULL 10040 }; 10041 ATTRIBUTE_GROUPS(bnxt); 10042 10043 static void bnxt_hwmon_close(struct bnxt *bp) 10044 { 10045 if (bp->hwmon_dev) { 10046 hwmon_device_unregister(bp->hwmon_dev); 10047 bp->hwmon_dev = NULL; 10048 } 10049 } 10050 10051 static void bnxt_hwmon_open(struct bnxt *bp) 10052 { 10053 struct hwrm_temp_monitor_query_input *req; 10054 struct pci_dev *pdev = bp->pdev; 10055 int rc; 10056 10057 rc = hwrm_req_init(bp, req, HWRM_TEMP_MONITOR_QUERY); 10058 if (!rc) 10059 rc = hwrm_req_send_silent(bp, req); 10060 if (rc == -EACCES || rc == -EOPNOTSUPP) { 10061 bnxt_hwmon_close(bp); 10062 return; 10063 } 10064 10065 if (bp->hwmon_dev) 10066 return; 10067 10068 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, 10069 DRV_MODULE_NAME, bp, 10070 bnxt_groups); 10071 if (IS_ERR(bp->hwmon_dev)) { 10072 bp->hwmon_dev = NULL; 10073 dev_warn(&pdev->dev, "Cannot register hwmon device\n"); 10074 } 10075 } 10076 #else 10077 static void bnxt_hwmon_close(struct bnxt *bp) 10078 { 10079 } 10080 10081 static void bnxt_hwmon_open(struct bnxt *bp) 10082 { 10083 } 10084 #endif 10085 10086 static bool bnxt_eee_config_ok(struct bnxt *bp) 10087 { 10088 struct ethtool_eee *eee = &bp->eee; 10089 struct bnxt_link_info *link_info = &bp->link_info; 10090 10091 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 10092 return true; 10093 10094 if (eee->eee_enabled) { 10095 u32 advertising = 10096 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0); 10097 10098 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 10099 eee->eee_enabled = 0; 10100 return false; 10101 } 10102 if (eee->advertised & ~advertising) { 10103 eee->advertised = advertising & eee->supported; 10104 return false; 10105 } 10106 } 10107 return true; 10108 } 10109 10110 static int bnxt_update_phy_setting(struct bnxt *bp) 10111 { 10112 int rc; 10113 bool update_link = false; 10114 bool update_pause = false; 10115 bool update_eee = false; 10116 struct bnxt_link_info *link_info = &bp->link_info; 10117 10118 rc = bnxt_update_link(bp, true); 10119 if (rc) { 10120 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 10121 rc); 10122 return rc; 10123 } 10124 if (!BNXT_SINGLE_PF(bp)) 10125 return 0; 10126 10127 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 10128 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 10129 link_info->req_flow_ctrl) 10130 update_pause = true; 10131 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 10132 link_info->force_pause_setting != link_info->req_flow_ctrl) 10133 update_pause = true; 10134 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 10135 if (BNXT_AUTO_MODE(link_info->auto_mode)) 10136 update_link = true; 10137 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 10138 link_info->req_link_speed != link_info->force_link_speed) 10139 update_link = true; 10140 else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 10141 link_info->req_link_speed != link_info->force_pam4_link_speed) 10142 update_link = true; 10143 if (link_info->req_duplex != link_info->duplex_setting) 10144 update_link = true; 10145 } else { 10146 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 10147 update_link = true; 10148 if (link_info->advertising != link_info->auto_link_speeds || 10149 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 10150 update_link = true; 10151 } 10152 10153 /* The last close may have shutdown the link, so need to call 10154 * PHY_CFG to bring it back up. 10155 */ 10156 if (!bp->link_info.link_up) 10157 update_link = true; 10158 10159 if (!bnxt_eee_config_ok(bp)) 10160 update_eee = true; 10161 10162 if (update_link) 10163 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 10164 else if (update_pause) 10165 rc = bnxt_hwrm_set_pause(bp); 10166 if (rc) { 10167 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 10168 rc); 10169 return rc; 10170 } 10171 10172 return rc; 10173 } 10174 10175 /* Common routine to pre-map certain register block to different GRC window. 10176 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows 10177 * in PF and 3 windows in VF that can be customized to map in different 10178 * register blocks. 10179 */ 10180 static void bnxt_preset_reg_win(struct bnxt *bp) 10181 { 10182 if (BNXT_PF(bp)) { 10183 /* CAG registers map to GRC window #4 */ 10184 writel(BNXT_CAG_REG_BASE, 10185 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12); 10186 } 10187 } 10188 10189 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 10190 10191 static int bnxt_reinit_after_abort(struct bnxt *bp) 10192 { 10193 int rc; 10194 10195 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 10196 return -EBUSY; 10197 10198 if (bp->dev->reg_state == NETREG_UNREGISTERED) 10199 return -ENODEV; 10200 10201 rc = bnxt_fw_init_one(bp); 10202 if (!rc) { 10203 bnxt_clear_int_mode(bp); 10204 rc = bnxt_init_int_mode(bp); 10205 if (!rc) { 10206 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 10207 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 10208 } 10209 } 10210 return rc; 10211 } 10212 10213 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 10214 { 10215 int rc = 0; 10216 10217 bnxt_preset_reg_win(bp); 10218 netif_carrier_off(bp->dev); 10219 if (irq_re_init) { 10220 /* Reserve rings now if none were reserved at driver probe. */ 10221 rc = bnxt_init_dflt_ring_mode(bp); 10222 if (rc) { 10223 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 10224 return rc; 10225 } 10226 } 10227 rc = bnxt_reserve_rings(bp, irq_re_init); 10228 if (rc) 10229 return rc; 10230 if ((bp->flags & BNXT_FLAG_RFS) && 10231 !(bp->flags & BNXT_FLAG_USING_MSIX)) { 10232 /* disable RFS if falling back to INTA */ 10233 bp->dev->hw_features &= ~NETIF_F_NTUPLE; 10234 bp->flags &= ~BNXT_FLAG_RFS; 10235 } 10236 10237 rc = bnxt_alloc_mem(bp, irq_re_init); 10238 if (rc) { 10239 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 10240 goto open_err_free_mem; 10241 } 10242 10243 if (irq_re_init) { 10244 bnxt_init_napi(bp); 10245 rc = bnxt_request_irq(bp); 10246 if (rc) { 10247 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 10248 goto open_err_irq; 10249 } 10250 } 10251 10252 rc = bnxt_init_nic(bp, irq_re_init); 10253 if (rc) { 10254 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 10255 goto open_err_irq; 10256 } 10257 10258 bnxt_enable_napi(bp); 10259 bnxt_debug_dev_init(bp); 10260 10261 if (link_re_init) { 10262 mutex_lock(&bp->link_lock); 10263 rc = bnxt_update_phy_setting(bp); 10264 mutex_unlock(&bp->link_lock); 10265 if (rc) { 10266 netdev_warn(bp->dev, "failed to update phy settings\n"); 10267 if (BNXT_SINGLE_PF(bp)) { 10268 bp->link_info.phy_retry = true; 10269 bp->link_info.phy_retry_expires = 10270 jiffies + 5 * HZ; 10271 } 10272 } 10273 } 10274 10275 if (irq_re_init) 10276 udp_tunnel_nic_reset_ntf(bp->dev); 10277 10278 set_bit(BNXT_STATE_OPEN, &bp->state); 10279 bnxt_enable_int(bp); 10280 /* Enable TX queues */ 10281 bnxt_tx_enable(bp); 10282 mod_timer(&bp->timer, jiffies + bp->current_interval); 10283 /* Poll link status and check for SFP+ module status */ 10284 mutex_lock(&bp->link_lock); 10285 bnxt_get_port_module_status(bp); 10286 mutex_unlock(&bp->link_lock); 10287 10288 /* VF-reps may need to be re-opened after the PF is re-opened */ 10289 if (BNXT_PF(bp)) 10290 bnxt_vf_reps_open(bp); 10291 return 0; 10292 10293 open_err_irq: 10294 bnxt_del_napi(bp); 10295 10296 open_err_free_mem: 10297 bnxt_free_skbs(bp); 10298 bnxt_free_irq(bp); 10299 bnxt_free_mem(bp, true); 10300 return rc; 10301 } 10302 10303 /* rtnl_lock held */ 10304 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 10305 { 10306 int rc = 0; 10307 10308 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 10309 rc = -EIO; 10310 if (!rc) 10311 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 10312 if (rc) { 10313 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 10314 dev_close(bp->dev); 10315 } 10316 return rc; 10317 } 10318 10319 /* rtnl_lock held, open the NIC half way by allocating all resources, but 10320 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline 10321 * self tests. 10322 */ 10323 int bnxt_half_open_nic(struct bnxt *bp) 10324 { 10325 int rc = 0; 10326 10327 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 10328 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 10329 rc = -ENODEV; 10330 goto half_open_err; 10331 } 10332 10333 rc = bnxt_alloc_mem(bp, false); 10334 if (rc) { 10335 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 10336 goto half_open_err; 10337 } 10338 rc = bnxt_init_nic(bp, false); 10339 if (rc) { 10340 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 10341 goto half_open_err; 10342 } 10343 return 0; 10344 10345 half_open_err: 10346 bnxt_free_skbs(bp); 10347 bnxt_free_mem(bp, false); 10348 dev_close(bp->dev); 10349 return rc; 10350 } 10351 10352 /* rtnl_lock held, this call can only be made after a previous successful 10353 * call to bnxt_half_open_nic(). 10354 */ 10355 void bnxt_half_close_nic(struct bnxt *bp) 10356 { 10357 bnxt_hwrm_resource_free(bp, false, false); 10358 bnxt_free_skbs(bp); 10359 bnxt_free_mem(bp, false); 10360 } 10361 10362 void bnxt_reenable_sriov(struct bnxt *bp) 10363 { 10364 if (BNXT_PF(bp)) { 10365 struct bnxt_pf_info *pf = &bp->pf; 10366 int n = pf->active_vfs; 10367 10368 if (n) 10369 bnxt_cfg_hw_sriov(bp, &n, true); 10370 } 10371 } 10372 10373 static int bnxt_open(struct net_device *dev) 10374 { 10375 struct bnxt *bp = netdev_priv(dev); 10376 int rc; 10377 10378 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 10379 rc = bnxt_reinit_after_abort(bp); 10380 if (rc) { 10381 if (rc == -EBUSY) 10382 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 10383 else 10384 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 10385 return -ENODEV; 10386 } 10387 } 10388 10389 rc = bnxt_hwrm_if_change(bp, true); 10390 if (rc) 10391 return rc; 10392 10393 rc = __bnxt_open_nic(bp, true, true); 10394 if (rc) { 10395 bnxt_hwrm_if_change(bp, false); 10396 } else { 10397 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 10398 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 10399 bnxt_ulp_start(bp, 0); 10400 bnxt_reenable_sriov(bp); 10401 } 10402 } 10403 bnxt_hwmon_open(bp); 10404 } 10405 10406 return rc; 10407 } 10408 10409 static bool bnxt_drv_busy(struct bnxt *bp) 10410 { 10411 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 10412 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 10413 } 10414 10415 static void bnxt_get_ring_stats(struct bnxt *bp, 10416 struct rtnl_link_stats64 *stats); 10417 10418 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 10419 bool link_re_init) 10420 { 10421 /* Close the VF-reps before closing PF */ 10422 if (BNXT_PF(bp)) 10423 bnxt_vf_reps_close(bp); 10424 10425 /* Change device state to avoid TX queue wake up's */ 10426 bnxt_tx_disable(bp); 10427 10428 clear_bit(BNXT_STATE_OPEN, &bp->state); 10429 smp_mb__after_atomic(); 10430 while (bnxt_drv_busy(bp)) 10431 msleep(20); 10432 10433 /* Flush rings and and disable interrupts */ 10434 bnxt_shutdown_nic(bp, irq_re_init); 10435 10436 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 10437 10438 bnxt_debug_dev_exit(bp); 10439 bnxt_disable_napi(bp); 10440 del_timer_sync(&bp->timer); 10441 bnxt_free_skbs(bp); 10442 10443 /* Save ring stats before shutdown */ 10444 if (bp->bnapi && irq_re_init) 10445 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 10446 if (irq_re_init) { 10447 bnxt_free_irq(bp); 10448 bnxt_del_napi(bp); 10449 } 10450 bnxt_free_mem(bp, irq_re_init); 10451 } 10452 10453 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 10454 { 10455 int rc = 0; 10456 10457 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 10458 /* If we get here, it means firmware reset is in progress 10459 * while we are trying to close. We can safely proceed with 10460 * the close because we are holding rtnl_lock(). Some firmware 10461 * messages may fail as we proceed to close. We set the 10462 * ABORT_ERR flag here so that the FW reset thread will later 10463 * abort when it gets the rtnl_lock() and sees the flag. 10464 */ 10465 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 10466 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 10467 } 10468 10469 #ifdef CONFIG_BNXT_SRIOV 10470 if (bp->sriov_cfg) { 10471 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 10472 !bp->sriov_cfg, 10473 BNXT_SRIOV_CFG_WAIT_TMO); 10474 if (rc) 10475 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n"); 10476 } 10477 #endif 10478 __bnxt_close_nic(bp, irq_re_init, link_re_init); 10479 return rc; 10480 } 10481 10482 static int bnxt_close(struct net_device *dev) 10483 { 10484 struct bnxt *bp = netdev_priv(dev); 10485 10486 bnxt_hwmon_close(bp); 10487 bnxt_close_nic(bp, true, true); 10488 bnxt_hwrm_shutdown_link(bp); 10489 bnxt_hwrm_if_change(bp, false); 10490 return 0; 10491 } 10492 10493 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 10494 u16 *val) 10495 { 10496 struct hwrm_port_phy_mdio_read_output *resp; 10497 struct hwrm_port_phy_mdio_read_input *req; 10498 int rc; 10499 10500 if (bp->hwrm_spec_code < 0x10a00) 10501 return -EOPNOTSUPP; 10502 10503 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 10504 if (rc) 10505 return rc; 10506 10507 req->port_id = cpu_to_le16(bp->pf.port_id); 10508 req->phy_addr = phy_addr; 10509 req->reg_addr = cpu_to_le16(reg & 0x1f); 10510 if (mdio_phy_id_is_c45(phy_addr)) { 10511 req->cl45_mdio = 1; 10512 req->phy_addr = mdio_phy_id_prtad(phy_addr); 10513 req->dev_addr = mdio_phy_id_devad(phy_addr); 10514 req->reg_addr = cpu_to_le16(reg); 10515 } 10516 10517 resp = hwrm_req_hold(bp, req); 10518 rc = hwrm_req_send(bp, req); 10519 if (!rc) 10520 *val = le16_to_cpu(resp->reg_data); 10521 hwrm_req_drop(bp, req); 10522 return rc; 10523 } 10524 10525 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 10526 u16 val) 10527 { 10528 struct hwrm_port_phy_mdio_write_input *req; 10529 int rc; 10530 10531 if (bp->hwrm_spec_code < 0x10a00) 10532 return -EOPNOTSUPP; 10533 10534 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 10535 if (rc) 10536 return rc; 10537 10538 req->port_id = cpu_to_le16(bp->pf.port_id); 10539 req->phy_addr = phy_addr; 10540 req->reg_addr = cpu_to_le16(reg & 0x1f); 10541 if (mdio_phy_id_is_c45(phy_addr)) { 10542 req->cl45_mdio = 1; 10543 req->phy_addr = mdio_phy_id_prtad(phy_addr); 10544 req->dev_addr = mdio_phy_id_devad(phy_addr); 10545 req->reg_addr = cpu_to_le16(reg); 10546 } 10547 req->reg_data = cpu_to_le16(val); 10548 10549 return hwrm_req_send(bp, req); 10550 } 10551 10552 /* rtnl_lock held */ 10553 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 10554 { 10555 struct mii_ioctl_data *mdio = if_mii(ifr); 10556 struct bnxt *bp = netdev_priv(dev); 10557 int rc; 10558 10559 switch (cmd) { 10560 case SIOCGMIIPHY: 10561 mdio->phy_id = bp->link_info.phy_addr; 10562 10563 fallthrough; 10564 case SIOCGMIIREG: { 10565 u16 mii_regval = 0; 10566 10567 if (!netif_running(dev)) 10568 return -EAGAIN; 10569 10570 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 10571 &mii_regval); 10572 mdio->val_out = mii_regval; 10573 return rc; 10574 } 10575 10576 case SIOCSMIIREG: 10577 if (!netif_running(dev)) 10578 return -EAGAIN; 10579 10580 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 10581 mdio->val_in); 10582 10583 case SIOCSHWTSTAMP: 10584 return bnxt_hwtstamp_set(dev, ifr); 10585 10586 case SIOCGHWTSTAMP: 10587 return bnxt_hwtstamp_get(dev, ifr); 10588 10589 default: 10590 /* do nothing */ 10591 break; 10592 } 10593 return -EOPNOTSUPP; 10594 } 10595 10596 static void bnxt_get_ring_stats(struct bnxt *bp, 10597 struct rtnl_link_stats64 *stats) 10598 { 10599 int i; 10600 10601 for (i = 0; i < bp->cp_nr_rings; i++) { 10602 struct bnxt_napi *bnapi = bp->bnapi[i]; 10603 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 10604 u64 *sw = cpr->stats.sw_stats; 10605 10606 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 10607 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 10608 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 10609 10610 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 10611 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 10612 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 10613 10614 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 10615 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 10616 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 10617 10618 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 10619 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 10620 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 10621 10622 stats->rx_missed_errors += 10623 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 10624 10625 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 10626 10627 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 10628 10629 stats->rx_dropped += 10630 cpr->sw_stats.rx.rx_netpoll_discards + 10631 cpr->sw_stats.rx.rx_oom_discards; 10632 } 10633 } 10634 10635 static void bnxt_add_prev_stats(struct bnxt *bp, 10636 struct rtnl_link_stats64 *stats) 10637 { 10638 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 10639 10640 stats->rx_packets += prev_stats->rx_packets; 10641 stats->tx_packets += prev_stats->tx_packets; 10642 stats->rx_bytes += prev_stats->rx_bytes; 10643 stats->tx_bytes += prev_stats->tx_bytes; 10644 stats->rx_missed_errors += prev_stats->rx_missed_errors; 10645 stats->multicast += prev_stats->multicast; 10646 stats->rx_dropped += prev_stats->rx_dropped; 10647 stats->tx_dropped += prev_stats->tx_dropped; 10648 } 10649 10650 static void 10651 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 10652 { 10653 struct bnxt *bp = netdev_priv(dev); 10654 10655 set_bit(BNXT_STATE_READ_STATS, &bp->state); 10656 /* Make sure bnxt_close_nic() sees that we are reading stats before 10657 * we check the BNXT_STATE_OPEN flag. 10658 */ 10659 smp_mb__after_atomic(); 10660 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 10661 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 10662 *stats = bp->net_stats_prev; 10663 return; 10664 } 10665 10666 bnxt_get_ring_stats(bp, stats); 10667 bnxt_add_prev_stats(bp, stats); 10668 10669 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10670 u64 *rx = bp->port_stats.sw_stats; 10671 u64 *tx = bp->port_stats.sw_stats + 10672 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10673 10674 stats->rx_crc_errors = 10675 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 10676 stats->rx_frame_errors = 10677 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 10678 stats->rx_length_errors = 10679 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 10680 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 10681 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 10682 stats->rx_errors = 10683 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 10684 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 10685 stats->collisions = 10686 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 10687 stats->tx_fifo_errors = 10688 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 10689 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 10690 } 10691 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 10692 } 10693 10694 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 10695 { 10696 struct net_device *dev = bp->dev; 10697 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 10698 struct netdev_hw_addr *ha; 10699 u8 *haddr; 10700 int mc_count = 0; 10701 bool update = false; 10702 int off = 0; 10703 10704 netdev_for_each_mc_addr(ha, dev) { 10705 if (mc_count >= BNXT_MAX_MC_ADDRS) { 10706 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10707 vnic->mc_list_count = 0; 10708 return false; 10709 } 10710 haddr = ha->addr; 10711 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 10712 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 10713 update = true; 10714 } 10715 off += ETH_ALEN; 10716 mc_count++; 10717 } 10718 if (mc_count) 10719 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 10720 10721 if (mc_count != vnic->mc_list_count) { 10722 vnic->mc_list_count = mc_count; 10723 update = true; 10724 } 10725 return update; 10726 } 10727 10728 static bool bnxt_uc_list_updated(struct bnxt *bp) 10729 { 10730 struct net_device *dev = bp->dev; 10731 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 10732 struct netdev_hw_addr *ha; 10733 int off = 0; 10734 10735 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 10736 return true; 10737 10738 netdev_for_each_uc_addr(ha, dev) { 10739 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 10740 return true; 10741 10742 off += ETH_ALEN; 10743 } 10744 return false; 10745 } 10746 10747 static void bnxt_set_rx_mode(struct net_device *dev) 10748 { 10749 struct bnxt *bp = netdev_priv(dev); 10750 struct bnxt_vnic_info *vnic; 10751 bool mc_update = false; 10752 bool uc_update; 10753 u32 mask; 10754 10755 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 10756 return; 10757 10758 vnic = &bp->vnic_info[0]; 10759 mask = vnic->rx_mask; 10760 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 10761 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 10762 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 10763 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 10764 10765 if (dev->flags & IFF_PROMISC) 10766 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10767 10768 uc_update = bnxt_uc_list_updated(bp); 10769 10770 if (dev->flags & IFF_BROADCAST) 10771 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 10772 if (dev->flags & IFF_ALLMULTI) { 10773 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10774 vnic->mc_list_count = 0; 10775 } else { 10776 mc_update = bnxt_mc_list_updated(bp, &mask); 10777 } 10778 10779 if (mask != vnic->rx_mask || uc_update || mc_update) { 10780 vnic->rx_mask = mask; 10781 10782 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event); 10783 bnxt_queue_sp_work(bp); 10784 } 10785 } 10786 10787 static int bnxt_cfg_rx_mode(struct bnxt *bp) 10788 { 10789 struct net_device *dev = bp->dev; 10790 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 10791 struct hwrm_cfa_l2_filter_free_input *req; 10792 struct netdev_hw_addr *ha; 10793 int i, off = 0, rc; 10794 bool uc_update; 10795 10796 netif_addr_lock_bh(dev); 10797 uc_update = bnxt_uc_list_updated(bp); 10798 netif_addr_unlock_bh(dev); 10799 10800 if (!uc_update) 10801 goto skip_uc; 10802 10803 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 10804 if (rc) 10805 return rc; 10806 hwrm_req_hold(bp, req); 10807 for (i = 1; i < vnic->uc_filter_count; i++) { 10808 req->l2_filter_id = vnic->fw_l2_filter_id[i]; 10809 10810 rc = hwrm_req_send(bp, req); 10811 } 10812 hwrm_req_drop(bp, req); 10813 10814 vnic->uc_filter_count = 1; 10815 10816 netif_addr_lock_bh(dev); 10817 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 10818 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10819 } else { 10820 netdev_for_each_uc_addr(ha, dev) { 10821 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 10822 off += ETH_ALEN; 10823 vnic->uc_filter_count++; 10824 } 10825 } 10826 netif_addr_unlock_bh(dev); 10827 10828 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 10829 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 10830 if (rc) { 10831 if (BNXT_VF(bp) && rc == -ENODEV) { 10832 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 10833 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 10834 else 10835 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 10836 rc = 0; 10837 } else { 10838 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10839 } 10840 vnic->uc_filter_count = i; 10841 return rc; 10842 } 10843 } 10844 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 10845 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 10846 10847 skip_uc: 10848 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 10849 !bnxt_promisc_ok(bp)) 10850 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10851 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 10852 if (rc && vnic->mc_list_count) { 10853 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 10854 rc); 10855 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10856 vnic->mc_list_count = 0; 10857 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 10858 } 10859 if (rc) 10860 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 10861 rc); 10862 10863 return rc; 10864 } 10865 10866 static bool bnxt_can_reserve_rings(struct bnxt *bp) 10867 { 10868 #ifdef CONFIG_BNXT_SRIOV 10869 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 10870 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 10871 10872 /* No minimum rings were provisioned by the PF. Don't 10873 * reserve rings by default when device is down. 10874 */ 10875 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 10876 return true; 10877 10878 if (!netif_running(bp->dev)) 10879 return false; 10880 } 10881 #endif 10882 return true; 10883 } 10884 10885 /* If the chip and firmware supports RFS */ 10886 static bool bnxt_rfs_supported(struct bnxt *bp) 10887 { 10888 if (bp->flags & BNXT_FLAG_CHIP_P5) { 10889 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 10890 return true; 10891 return false; 10892 } 10893 /* 212 firmware is broken for aRFS */ 10894 if (BNXT_FW_MAJ(bp) == 212) 10895 return false; 10896 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 10897 return true; 10898 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP) 10899 return true; 10900 return false; 10901 } 10902 10903 /* If runtime conditions support RFS */ 10904 static bool bnxt_rfs_capable(struct bnxt *bp) 10905 { 10906 #ifdef CONFIG_RFS_ACCEL 10907 int vnics, max_vnics, max_rss_ctxs; 10908 10909 if (bp->flags & BNXT_FLAG_CHIP_P5) 10910 return bnxt_rfs_supported(bp); 10911 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp)) 10912 return false; 10913 10914 vnics = 1 + bp->rx_nr_rings; 10915 max_vnics = bnxt_get_max_func_vnics(bp); 10916 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 10917 10918 /* RSS contexts not a limiting factor */ 10919 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP) 10920 max_rss_ctxs = max_vnics; 10921 if (vnics > max_vnics || vnics > max_rss_ctxs) { 10922 if (bp->rx_nr_rings > 1) 10923 netdev_warn(bp->dev, 10924 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 10925 min(max_rss_ctxs - 1, max_vnics - 1)); 10926 return false; 10927 } 10928 10929 if (!BNXT_NEW_RM(bp)) 10930 return true; 10931 10932 if (vnics == bp->hw_resc.resv_vnics) 10933 return true; 10934 10935 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics); 10936 if (vnics <= bp->hw_resc.resv_vnics) 10937 return true; 10938 10939 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 10940 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1); 10941 return false; 10942 #else 10943 return false; 10944 #endif 10945 } 10946 10947 static netdev_features_t bnxt_fix_features(struct net_device *dev, 10948 netdev_features_t features) 10949 { 10950 struct bnxt *bp = netdev_priv(dev); 10951 netdev_features_t vlan_features; 10952 10953 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp)) 10954 features &= ~NETIF_F_NTUPLE; 10955 10956 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 10957 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 10958 10959 if (!(features & NETIF_F_GRO)) 10960 features &= ~NETIF_F_GRO_HW; 10961 10962 if (features & NETIF_F_GRO_HW) 10963 features &= ~NETIF_F_LRO; 10964 10965 /* Both CTAG and STAG VLAN accelaration on the RX side have to be 10966 * turned on or off together. 10967 */ 10968 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 10969 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 10970 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 10971 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 10972 else if (vlan_features) 10973 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 10974 } 10975 #ifdef CONFIG_BNXT_SRIOV 10976 if (BNXT_VF(bp) && bp->vf.vlan) 10977 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 10978 #endif 10979 return features; 10980 } 10981 10982 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 10983 { 10984 struct bnxt *bp = netdev_priv(dev); 10985 u32 flags = bp->flags; 10986 u32 changes; 10987 int rc = 0; 10988 bool re_init = false; 10989 bool update_tpa = false; 10990 10991 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 10992 if (features & NETIF_F_GRO_HW) 10993 flags |= BNXT_FLAG_GRO; 10994 else if (features & NETIF_F_LRO) 10995 flags |= BNXT_FLAG_LRO; 10996 10997 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 10998 flags &= ~BNXT_FLAG_TPA; 10999 11000 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 11001 flags |= BNXT_FLAG_STRIP_VLAN; 11002 11003 if (features & NETIF_F_NTUPLE) 11004 flags |= BNXT_FLAG_RFS; 11005 11006 changes = flags ^ bp->flags; 11007 if (changes & BNXT_FLAG_TPA) { 11008 update_tpa = true; 11009 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 11010 (flags & BNXT_FLAG_TPA) == 0 || 11011 (bp->flags & BNXT_FLAG_CHIP_P5)) 11012 re_init = true; 11013 } 11014 11015 if (changes & ~BNXT_FLAG_TPA) 11016 re_init = true; 11017 11018 if (flags != bp->flags) { 11019 u32 old_flags = bp->flags; 11020 11021 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 11022 bp->flags = flags; 11023 if (update_tpa) 11024 bnxt_set_ring_params(bp); 11025 return rc; 11026 } 11027 11028 if (re_init) { 11029 bnxt_close_nic(bp, false, false); 11030 bp->flags = flags; 11031 if (update_tpa) 11032 bnxt_set_ring_params(bp); 11033 11034 return bnxt_open_nic(bp, false, false); 11035 } 11036 if (update_tpa) { 11037 bp->flags = flags; 11038 rc = bnxt_set_tpa(bp, 11039 (flags & BNXT_FLAG_TPA) ? 11040 true : false); 11041 if (rc) 11042 bp->flags = old_flags; 11043 } 11044 } 11045 return rc; 11046 } 11047 11048 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 11049 u8 **nextp) 11050 { 11051 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 11052 int hdr_count = 0; 11053 u8 *nexthdr; 11054 int start; 11055 11056 /* Check that there are at most 2 IPv6 extension headers, no 11057 * fragment header, and each is <= 64 bytes. 11058 */ 11059 start = nw_off + sizeof(*ip6h); 11060 nexthdr = &ip6h->nexthdr; 11061 while (ipv6_ext_hdr(*nexthdr)) { 11062 struct ipv6_opt_hdr *hp; 11063 int hdrlen; 11064 11065 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 11066 *nexthdr == NEXTHDR_FRAGMENT) 11067 return false; 11068 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 11069 skb_headlen(skb), NULL); 11070 if (!hp) 11071 return false; 11072 if (*nexthdr == NEXTHDR_AUTH) 11073 hdrlen = ipv6_authlen(hp); 11074 else 11075 hdrlen = ipv6_optlen(hp); 11076 11077 if (hdrlen > 64) 11078 return false; 11079 nexthdr = &hp->nexthdr; 11080 start += hdrlen; 11081 hdr_count++; 11082 } 11083 if (nextp) { 11084 /* Caller will check inner protocol */ 11085 if (skb->encapsulation) { 11086 *nextp = nexthdr; 11087 return true; 11088 } 11089 *nextp = NULL; 11090 } 11091 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 11092 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 11093 } 11094 11095 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 11096 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 11097 { 11098 struct udphdr *uh = udp_hdr(skb); 11099 __be16 udp_port = uh->dest; 11100 11101 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port) 11102 return false; 11103 if (skb->inner_protocol_type == ENCAP_TYPE_ETHER) { 11104 struct ethhdr *eh = inner_eth_hdr(skb); 11105 11106 switch (eh->h_proto) { 11107 case htons(ETH_P_IP): 11108 return true; 11109 case htons(ETH_P_IPV6): 11110 return bnxt_exthdr_check(bp, skb, 11111 skb_inner_network_offset(skb), 11112 NULL); 11113 } 11114 } 11115 return false; 11116 } 11117 11118 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 11119 { 11120 switch (l4_proto) { 11121 case IPPROTO_UDP: 11122 return bnxt_udp_tunl_check(bp, skb); 11123 case IPPROTO_IPIP: 11124 return true; 11125 case IPPROTO_GRE: { 11126 switch (skb->inner_protocol) { 11127 default: 11128 return false; 11129 case htons(ETH_P_IP): 11130 return true; 11131 case htons(ETH_P_IPV6): 11132 fallthrough; 11133 } 11134 } 11135 case IPPROTO_IPV6: 11136 /* Check ext headers of inner ipv6 */ 11137 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 11138 NULL); 11139 } 11140 return false; 11141 } 11142 11143 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 11144 struct net_device *dev, 11145 netdev_features_t features) 11146 { 11147 struct bnxt *bp = netdev_priv(dev); 11148 u8 *l4_proto; 11149 11150 features = vlan_features_check(skb, features); 11151 switch (vlan_get_protocol(skb)) { 11152 case htons(ETH_P_IP): 11153 if (!skb->encapsulation) 11154 return features; 11155 l4_proto = &ip_hdr(skb)->protocol; 11156 if (bnxt_tunl_check(bp, skb, *l4_proto)) 11157 return features; 11158 break; 11159 case htons(ETH_P_IPV6): 11160 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 11161 &l4_proto)) 11162 break; 11163 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 11164 return features; 11165 break; 11166 } 11167 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 11168 } 11169 11170 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 11171 u32 *reg_buf) 11172 { 11173 struct hwrm_dbg_read_direct_output *resp; 11174 struct hwrm_dbg_read_direct_input *req; 11175 __le32 *dbg_reg_buf; 11176 dma_addr_t mapping; 11177 int rc, i; 11178 11179 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 11180 if (rc) 11181 return rc; 11182 11183 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 11184 &mapping); 11185 if (!dbg_reg_buf) { 11186 rc = -ENOMEM; 11187 goto dbg_rd_reg_exit; 11188 } 11189 11190 req->host_dest_addr = cpu_to_le64(mapping); 11191 11192 resp = hwrm_req_hold(bp, req); 11193 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 11194 req->read_len32 = cpu_to_le32(num_words); 11195 11196 rc = hwrm_req_send(bp, req); 11197 if (rc || resp->error_code) { 11198 rc = -EIO; 11199 goto dbg_rd_reg_exit; 11200 } 11201 for (i = 0; i < num_words; i++) 11202 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 11203 11204 dbg_rd_reg_exit: 11205 hwrm_req_drop(bp, req); 11206 return rc; 11207 } 11208 11209 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 11210 u32 ring_id, u32 *prod, u32 *cons) 11211 { 11212 struct hwrm_dbg_ring_info_get_output *resp; 11213 struct hwrm_dbg_ring_info_get_input *req; 11214 int rc; 11215 11216 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 11217 if (rc) 11218 return rc; 11219 11220 req->ring_type = ring_type; 11221 req->fw_ring_id = cpu_to_le32(ring_id); 11222 resp = hwrm_req_hold(bp, req); 11223 rc = hwrm_req_send(bp, req); 11224 if (!rc) { 11225 *prod = le32_to_cpu(resp->producer_index); 11226 *cons = le32_to_cpu(resp->consumer_index); 11227 } 11228 hwrm_req_drop(bp, req); 11229 return rc; 11230 } 11231 11232 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 11233 { 11234 struct bnxt_tx_ring_info *txr = bnapi->tx_ring; 11235 int i = bnapi->index; 11236 11237 if (!txr) 11238 return; 11239 11240 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 11241 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 11242 txr->tx_cons); 11243 } 11244 11245 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 11246 { 11247 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 11248 int i = bnapi->index; 11249 11250 if (!rxr) 11251 return; 11252 11253 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", 11254 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 11255 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 11256 rxr->rx_sw_agg_prod); 11257 } 11258 11259 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 11260 { 11261 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 11262 int i = bnapi->index; 11263 11264 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 11265 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 11266 } 11267 11268 static void bnxt_dbg_dump_states(struct bnxt *bp) 11269 { 11270 int i; 11271 struct bnxt_napi *bnapi; 11272 11273 for (i = 0; i < bp->cp_nr_rings; i++) { 11274 bnapi = bp->bnapi[i]; 11275 if (netif_msg_drv(bp)) { 11276 bnxt_dump_tx_sw_state(bnapi); 11277 bnxt_dump_rx_sw_state(bnapi); 11278 bnxt_dump_cp_sw_state(bnapi); 11279 } 11280 } 11281 } 11282 11283 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 11284 { 11285 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 11286 struct hwrm_ring_reset_input *req; 11287 struct bnxt_napi *bnapi = rxr->bnapi; 11288 struct bnxt_cp_ring_info *cpr; 11289 u16 cp_ring_id; 11290 int rc; 11291 11292 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 11293 if (rc) 11294 return rc; 11295 11296 cpr = &bnapi->cp_ring; 11297 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 11298 req->cmpl_ring = cpu_to_le16(cp_ring_id); 11299 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 11300 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 11301 return hwrm_req_send_silent(bp, req); 11302 } 11303 11304 static void bnxt_reset_task(struct bnxt *bp, bool silent) 11305 { 11306 if (!silent) 11307 bnxt_dbg_dump_states(bp); 11308 if (netif_running(bp->dev)) { 11309 int rc; 11310 11311 if (silent) { 11312 bnxt_close_nic(bp, false, false); 11313 bnxt_open_nic(bp, false, false); 11314 } else { 11315 bnxt_ulp_stop(bp); 11316 bnxt_close_nic(bp, true, false); 11317 rc = bnxt_open_nic(bp, true, false); 11318 bnxt_ulp_start(bp, rc); 11319 } 11320 } 11321 } 11322 11323 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 11324 { 11325 struct bnxt *bp = netdev_priv(dev); 11326 11327 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 11328 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 11329 bnxt_queue_sp_work(bp); 11330 } 11331 11332 static void bnxt_fw_health_check(struct bnxt *bp) 11333 { 11334 struct bnxt_fw_health *fw_health = bp->fw_health; 11335 u32 val; 11336 11337 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11338 return; 11339 11340 /* Make sure it is enabled before checking the tmr_counter. */ 11341 smp_rmb(); 11342 if (fw_health->tmr_counter) { 11343 fw_health->tmr_counter--; 11344 return; 11345 } 11346 11347 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 11348 if (val == fw_health->last_fw_heartbeat) { 11349 fw_health->arrests++; 11350 goto fw_reset; 11351 } 11352 11353 fw_health->last_fw_heartbeat = val; 11354 11355 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 11356 if (val != fw_health->last_fw_reset_cnt) { 11357 fw_health->discoveries++; 11358 goto fw_reset; 11359 } 11360 11361 fw_health->tmr_counter = fw_health->tmr_multiplier; 11362 return; 11363 11364 fw_reset: 11365 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event); 11366 bnxt_queue_sp_work(bp); 11367 } 11368 11369 static void bnxt_timer(struct timer_list *t) 11370 { 11371 struct bnxt *bp = from_timer(bp, t, timer); 11372 struct net_device *dev = bp->dev; 11373 11374 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 11375 return; 11376 11377 if (atomic_read(&bp->intr_sem) != 0) 11378 goto bnxt_restart_timer; 11379 11380 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 11381 bnxt_fw_health_check(bp); 11382 11383 if (bp->link_info.link_up && bp->stats_coal_ticks) { 11384 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event); 11385 bnxt_queue_sp_work(bp); 11386 } 11387 11388 if (bnxt_tc_flower_enabled(bp)) { 11389 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event); 11390 bnxt_queue_sp_work(bp); 11391 } 11392 11393 #ifdef CONFIG_RFS_ACCEL 11394 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) { 11395 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event); 11396 bnxt_queue_sp_work(bp); 11397 } 11398 #endif /*CONFIG_RFS_ACCEL*/ 11399 11400 if (bp->link_info.phy_retry) { 11401 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 11402 bp->link_info.phy_retry = false; 11403 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 11404 } else { 11405 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event); 11406 bnxt_queue_sp_work(bp); 11407 } 11408 } 11409 11410 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) { 11411 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event); 11412 bnxt_queue_sp_work(bp); 11413 } 11414 11415 if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev && 11416 netif_carrier_ok(dev)) { 11417 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event); 11418 bnxt_queue_sp_work(bp); 11419 } 11420 bnxt_restart_timer: 11421 mod_timer(&bp->timer, jiffies + bp->current_interval); 11422 } 11423 11424 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 11425 { 11426 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 11427 * set. If the device is being closed, bnxt_close() may be holding 11428 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we 11429 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl(). 11430 */ 11431 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 11432 rtnl_lock(); 11433 } 11434 11435 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 11436 { 11437 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 11438 rtnl_unlock(); 11439 } 11440 11441 /* Only called from bnxt_sp_task() */ 11442 static void bnxt_reset(struct bnxt *bp, bool silent) 11443 { 11444 bnxt_rtnl_lock_sp(bp); 11445 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 11446 bnxt_reset_task(bp, silent); 11447 bnxt_rtnl_unlock_sp(bp); 11448 } 11449 11450 /* Only called from bnxt_sp_task() */ 11451 static void bnxt_rx_ring_reset(struct bnxt *bp) 11452 { 11453 int i; 11454 11455 bnxt_rtnl_lock_sp(bp); 11456 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 11457 bnxt_rtnl_unlock_sp(bp); 11458 return; 11459 } 11460 /* Disable and flush TPA before resetting the RX ring */ 11461 if (bp->flags & BNXT_FLAG_TPA) 11462 bnxt_set_tpa(bp, false); 11463 for (i = 0; i < bp->rx_nr_rings; i++) { 11464 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 11465 struct bnxt_cp_ring_info *cpr; 11466 int rc; 11467 11468 if (!rxr->bnapi->in_reset) 11469 continue; 11470 11471 rc = bnxt_hwrm_rx_ring_reset(bp, i); 11472 if (rc) { 11473 if (rc == -EINVAL || rc == -EOPNOTSUPP) 11474 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 11475 else 11476 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 11477 rc); 11478 bnxt_reset_task(bp, true); 11479 break; 11480 } 11481 bnxt_free_one_rx_ring_skbs(bp, i); 11482 rxr->rx_prod = 0; 11483 rxr->rx_agg_prod = 0; 11484 rxr->rx_sw_agg_prod = 0; 11485 rxr->rx_next_cons = 0; 11486 rxr->bnapi->in_reset = false; 11487 bnxt_alloc_one_rx_ring(bp, i); 11488 cpr = &rxr->bnapi->cp_ring; 11489 cpr->sw_stats.rx.rx_resets++; 11490 if (bp->flags & BNXT_FLAG_AGG_RINGS) 11491 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 11492 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 11493 } 11494 if (bp->flags & BNXT_FLAG_TPA) 11495 bnxt_set_tpa(bp, true); 11496 bnxt_rtnl_unlock_sp(bp); 11497 } 11498 11499 static void bnxt_fw_reset_close(struct bnxt *bp) 11500 { 11501 bnxt_ulp_stop(bp); 11502 /* When firmware is in fatal state, quiesce device and disable 11503 * bus master to prevent any potential bad DMAs before freeing 11504 * kernel memory. 11505 */ 11506 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 11507 u16 val = 0; 11508 11509 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 11510 if (val == 0xffff) 11511 bp->fw_reset_min_dsecs = 0; 11512 bnxt_tx_disable(bp); 11513 bnxt_disable_napi(bp); 11514 bnxt_disable_int_sync(bp); 11515 bnxt_free_irq(bp); 11516 bnxt_clear_int_mode(bp); 11517 pci_disable_device(bp->pdev); 11518 } 11519 __bnxt_close_nic(bp, true, false); 11520 bnxt_vf_reps_free(bp); 11521 bnxt_clear_int_mode(bp); 11522 bnxt_hwrm_func_drv_unrgtr(bp); 11523 if (pci_is_enabled(bp->pdev)) 11524 pci_disable_device(bp->pdev); 11525 bnxt_free_ctx_mem(bp); 11526 kfree(bp->ctx); 11527 bp->ctx = NULL; 11528 } 11529 11530 static bool is_bnxt_fw_ok(struct bnxt *bp) 11531 { 11532 struct bnxt_fw_health *fw_health = bp->fw_health; 11533 bool no_heartbeat = false, has_reset = false; 11534 u32 val; 11535 11536 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 11537 if (val == fw_health->last_fw_heartbeat) 11538 no_heartbeat = true; 11539 11540 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 11541 if (val != fw_health->last_fw_reset_cnt) 11542 has_reset = true; 11543 11544 if (!no_heartbeat && has_reset) 11545 return true; 11546 11547 return false; 11548 } 11549 11550 /* rtnl_lock is acquired before calling this function */ 11551 static void bnxt_force_fw_reset(struct bnxt *bp) 11552 { 11553 struct bnxt_fw_health *fw_health = bp->fw_health; 11554 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 11555 u32 wait_dsecs; 11556 11557 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 11558 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11559 return; 11560 11561 if (ptp) { 11562 spin_lock_bh(&ptp->ptp_lock); 11563 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 11564 spin_unlock_bh(&ptp->ptp_lock); 11565 } else { 11566 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 11567 } 11568 bnxt_fw_reset_close(bp); 11569 wait_dsecs = fw_health->master_func_wait_dsecs; 11570 if (fw_health->primary) { 11571 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 11572 wait_dsecs = 0; 11573 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 11574 } else { 11575 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 11576 wait_dsecs = fw_health->normal_func_wait_dsecs; 11577 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 11578 } 11579 11580 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 11581 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 11582 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 11583 } 11584 11585 void bnxt_fw_exception(struct bnxt *bp) 11586 { 11587 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 11588 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 11589 bnxt_rtnl_lock_sp(bp); 11590 bnxt_force_fw_reset(bp); 11591 bnxt_rtnl_unlock_sp(bp); 11592 } 11593 11594 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 11595 * < 0 on error. 11596 */ 11597 static int bnxt_get_registered_vfs(struct bnxt *bp) 11598 { 11599 #ifdef CONFIG_BNXT_SRIOV 11600 int rc; 11601 11602 if (!BNXT_PF(bp)) 11603 return 0; 11604 11605 rc = bnxt_hwrm_func_qcfg(bp); 11606 if (rc) { 11607 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 11608 return rc; 11609 } 11610 if (bp->pf.registered_vfs) 11611 return bp->pf.registered_vfs; 11612 if (bp->sriov_cfg) 11613 return 1; 11614 #endif 11615 return 0; 11616 } 11617 11618 void bnxt_fw_reset(struct bnxt *bp) 11619 { 11620 bnxt_rtnl_lock_sp(bp); 11621 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 11622 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 11623 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 11624 int n = 0, tmo; 11625 11626 if (ptp) { 11627 spin_lock_bh(&ptp->ptp_lock); 11628 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 11629 spin_unlock_bh(&ptp->ptp_lock); 11630 } else { 11631 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 11632 } 11633 if (bp->pf.active_vfs && 11634 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 11635 n = bnxt_get_registered_vfs(bp); 11636 if (n < 0) { 11637 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 11638 n); 11639 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 11640 dev_close(bp->dev); 11641 goto fw_reset_exit; 11642 } else if (n > 0) { 11643 u16 vf_tmo_dsecs = n * 10; 11644 11645 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 11646 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 11647 bp->fw_reset_state = 11648 BNXT_FW_RESET_STATE_POLL_VF; 11649 bnxt_queue_fw_reset_work(bp, HZ / 10); 11650 goto fw_reset_exit; 11651 } 11652 bnxt_fw_reset_close(bp); 11653 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 11654 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 11655 tmo = HZ / 10; 11656 } else { 11657 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 11658 tmo = bp->fw_reset_min_dsecs * HZ / 10; 11659 } 11660 bnxt_queue_fw_reset_work(bp, tmo); 11661 } 11662 fw_reset_exit: 11663 bnxt_rtnl_unlock_sp(bp); 11664 } 11665 11666 static void bnxt_chk_missed_irq(struct bnxt *bp) 11667 { 11668 int i; 11669 11670 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 11671 return; 11672 11673 for (i = 0; i < bp->cp_nr_rings; i++) { 11674 struct bnxt_napi *bnapi = bp->bnapi[i]; 11675 struct bnxt_cp_ring_info *cpr; 11676 u32 fw_ring_id; 11677 int j; 11678 11679 if (!bnapi) 11680 continue; 11681 11682 cpr = &bnapi->cp_ring; 11683 for (j = 0; j < 2; j++) { 11684 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j]; 11685 u32 val[2]; 11686 11687 if (!cpr2 || cpr2->has_more_work || 11688 !bnxt_has_work(bp, cpr2)) 11689 continue; 11690 11691 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 11692 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 11693 continue; 11694 } 11695 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 11696 bnxt_dbg_hwrm_ring_info_get(bp, 11697 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 11698 fw_ring_id, &val[0], &val[1]); 11699 cpr->sw_stats.cmn.missed_irqs++; 11700 } 11701 } 11702 } 11703 11704 static void bnxt_cfg_ntp_filters(struct bnxt *); 11705 11706 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 11707 { 11708 struct bnxt_link_info *link_info = &bp->link_info; 11709 11710 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 11711 link_info->autoneg = BNXT_AUTONEG_SPEED; 11712 if (bp->hwrm_spec_code >= 0x10201) { 11713 if (link_info->auto_pause_setting & 11714 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 11715 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 11716 } else { 11717 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 11718 } 11719 link_info->advertising = link_info->auto_link_speeds; 11720 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 11721 } else { 11722 link_info->req_link_speed = link_info->force_link_speed; 11723 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 11724 if (link_info->force_pam4_link_speed) { 11725 link_info->req_link_speed = 11726 link_info->force_pam4_link_speed; 11727 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 11728 } 11729 link_info->req_duplex = link_info->duplex_setting; 11730 } 11731 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 11732 link_info->req_flow_ctrl = 11733 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 11734 else 11735 link_info->req_flow_ctrl = link_info->force_pause_setting; 11736 } 11737 11738 static void bnxt_fw_echo_reply(struct bnxt *bp) 11739 { 11740 struct bnxt_fw_health *fw_health = bp->fw_health; 11741 struct hwrm_func_echo_response_input *req; 11742 int rc; 11743 11744 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 11745 if (rc) 11746 return; 11747 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 11748 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 11749 hwrm_req_send(bp, req); 11750 } 11751 11752 static void bnxt_sp_task(struct work_struct *work) 11753 { 11754 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 11755 11756 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 11757 smp_mb__after_atomic(); 11758 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 11759 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 11760 return; 11761 } 11762 11763 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 11764 bnxt_cfg_rx_mode(bp); 11765 11766 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 11767 bnxt_cfg_ntp_filters(bp); 11768 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 11769 bnxt_hwrm_exec_fwd_req(bp); 11770 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 11771 bnxt_hwrm_port_qstats(bp, 0); 11772 bnxt_hwrm_port_qstats_ext(bp, 0); 11773 bnxt_accumulate_all_stats(bp); 11774 } 11775 11776 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 11777 int rc; 11778 11779 mutex_lock(&bp->link_lock); 11780 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 11781 &bp->sp_event)) 11782 bnxt_hwrm_phy_qcaps(bp); 11783 11784 rc = bnxt_update_link(bp, true); 11785 if (rc) 11786 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 11787 rc); 11788 11789 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 11790 &bp->sp_event)) 11791 bnxt_init_ethtool_link_settings(bp); 11792 mutex_unlock(&bp->link_lock); 11793 } 11794 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 11795 int rc; 11796 11797 mutex_lock(&bp->link_lock); 11798 rc = bnxt_update_phy_setting(bp); 11799 mutex_unlock(&bp->link_lock); 11800 if (rc) { 11801 netdev_warn(bp->dev, "update phy settings retry failed\n"); 11802 } else { 11803 bp->link_info.phy_retry = false; 11804 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 11805 } 11806 } 11807 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 11808 mutex_lock(&bp->link_lock); 11809 bnxt_get_port_module_status(bp); 11810 mutex_unlock(&bp->link_lock); 11811 } 11812 11813 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 11814 bnxt_tc_flow_stats_work(bp); 11815 11816 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 11817 bnxt_chk_missed_irq(bp); 11818 11819 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 11820 bnxt_fw_echo_reply(bp); 11821 11822 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 11823 * must be the last functions to be called before exiting. 11824 */ 11825 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 11826 bnxt_reset(bp, false); 11827 11828 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 11829 bnxt_reset(bp, true); 11830 11831 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 11832 bnxt_rx_ring_reset(bp); 11833 11834 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 11835 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 11836 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 11837 bnxt_devlink_health_fw_report(bp); 11838 else 11839 bnxt_fw_reset(bp); 11840 } 11841 11842 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 11843 if (!is_bnxt_fw_ok(bp)) 11844 bnxt_devlink_health_fw_report(bp); 11845 } 11846 11847 smp_mb__before_atomic(); 11848 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 11849 } 11850 11851 /* Under rtnl_lock */ 11852 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 11853 int tx_xdp) 11854 { 11855 int max_rx, max_tx, tx_sets = 1; 11856 int tx_rings_needed, stats; 11857 int rx_rings = rx; 11858 int cp, vnics, rc; 11859 11860 if (tcs) 11861 tx_sets = tcs; 11862 11863 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh); 11864 if (rc) 11865 return rc; 11866 11867 if (max_rx < rx) 11868 return -ENOMEM; 11869 11870 tx_rings_needed = tx * tx_sets + tx_xdp; 11871 if (max_tx < tx_rings_needed) 11872 return -ENOMEM; 11873 11874 vnics = 1; 11875 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS) 11876 vnics += rx_rings; 11877 11878 if (bp->flags & BNXT_FLAG_AGG_RINGS) 11879 rx_rings <<= 1; 11880 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx; 11881 stats = cp; 11882 if (BNXT_NEW_RM(bp)) { 11883 cp += bnxt_get_ulp_msix_num(bp); 11884 stats += bnxt_get_ulp_stat_ctxs(bp); 11885 } 11886 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp, 11887 stats, vnics); 11888 } 11889 11890 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 11891 { 11892 if (bp->bar2) { 11893 pci_iounmap(pdev, bp->bar2); 11894 bp->bar2 = NULL; 11895 } 11896 11897 if (bp->bar1) { 11898 pci_iounmap(pdev, bp->bar1); 11899 bp->bar1 = NULL; 11900 } 11901 11902 if (bp->bar0) { 11903 pci_iounmap(pdev, bp->bar0); 11904 bp->bar0 = NULL; 11905 } 11906 } 11907 11908 static void bnxt_cleanup_pci(struct bnxt *bp) 11909 { 11910 bnxt_unmap_bars(bp, bp->pdev); 11911 pci_release_regions(bp->pdev); 11912 if (pci_is_enabled(bp->pdev)) 11913 pci_disable_device(bp->pdev); 11914 } 11915 11916 static void bnxt_init_dflt_coal(struct bnxt *bp) 11917 { 11918 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 11919 struct bnxt_coal *coal; 11920 u16 flags = 0; 11921 11922 if (coal_cap->cmpl_params & 11923 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 11924 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 11925 11926 /* Tick values in micro seconds. 11927 * 1 coal_buf x bufs_per_record = 1 completion record. 11928 */ 11929 coal = &bp->rx_coal; 11930 coal->coal_ticks = 10; 11931 coal->coal_bufs = 30; 11932 coal->coal_ticks_irq = 1; 11933 coal->coal_bufs_irq = 2; 11934 coal->idle_thresh = 50; 11935 coal->bufs_per_record = 2; 11936 coal->budget = 64; /* NAPI budget */ 11937 coal->flags = flags; 11938 11939 coal = &bp->tx_coal; 11940 coal->coal_ticks = 28; 11941 coal->coal_bufs = 30; 11942 coal->coal_ticks_irq = 2; 11943 coal->coal_bufs_irq = 2; 11944 coal->bufs_per_record = 1; 11945 coal->flags = flags; 11946 11947 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 11948 } 11949 11950 static int bnxt_fw_init_one_p1(struct bnxt *bp) 11951 { 11952 int rc; 11953 11954 bp->fw_cap = 0; 11955 rc = bnxt_hwrm_ver_get(bp); 11956 bnxt_try_map_fw_health_reg(bp); 11957 if (rc) { 11958 rc = bnxt_try_recover_fw(bp); 11959 if (rc) 11960 return rc; 11961 rc = bnxt_hwrm_ver_get(bp); 11962 if (rc) 11963 return rc; 11964 } 11965 11966 bnxt_nvm_cfg_ver_get(bp); 11967 11968 rc = bnxt_hwrm_func_reset(bp); 11969 if (rc) 11970 return -ENODEV; 11971 11972 bnxt_hwrm_fw_set_time(bp); 11973 return 0; 11974 } 11975 11976 static int bnxt_fw_init_one_p2(struct bnxt *bp) 11977 { 11978 int rc; 11979 11980 /* Get the MAX capabilities for this function */ 11981 rc = bnxt_hwrm_func_qcaps(bp); 11982 if (rc) { 11983 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 11984 rc); 11985 return -ENODEV; 11986 } 11987 11988 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 11989 if (rc) 11990 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 11991 rc); 11992 11993 if (bnxt_alloc_fw_health(bp)) { 11994 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 11995 } else { 11996 rc = bnxt_hwrm_error_recovery_qcfg(bp); 11997 if (rc) 11998 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 11999 rc); 12000 } 12001 12002 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 12003 if (rc) 12004 return -ENODEV; 12005 12006 bnxt_hwrm_func_qcfg(bp); 12007 bnxt_hwrm_vnic_qcaps(bp); 12008 bnxt_hwrm_port_led_qcaps(bp); 12009 bnxt_ethtool_init(bp); 12010 bnxt_dcb_init(bp); 12011 return 0; 12012 } 12013 12014 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 12015 { 12016 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP; 12017 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 12018 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 12019 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 12020 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 12021 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 12022 bp->flags |= BNXT_FLAG_UDP_RSS_CAP; 12023 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 12024 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 12025 } 12026 } 12027 12028 static void bnxt_set_dflt_rfs(struct bnxt *bp) 12029 { 12030 struct net_device *dev = bp->dev; 12031 12032 dev->hw_features &= ~NETIF_F_NTUPLE; 12033 dev->features &= ~NETIF_F_NTUPLE; 12034 bp->flags &= ~BNXT_FLAG_RFS; 12035 if (bnxt_rfs_supported(bp)) { 12036 dev->hw_features |= NETIF_F_NTUPLE; 12037 if (bnxt_rfs_capable(bp)) { 12038 bp->flags |= BNXT_FLAG_RFS; 12039 dev->features |= NETIF_F_NTUPLE; 12040 } 12041 } 12042 } 12043 12044 static void bnxt_fw_init_one_p3(struct bnxt *bp) 12045 { 12046 struct pci_dev *pdev = bp->pdev; 12047 12048 bnxt_set_dflt_rss_hash_type(bp); 12049 bnxt_set_dflt_rfs(bp); 12050 12051 bnxt_get_wol_settings(bp); 12052 if (bp->flags & BNXT_FLAG_WOL_CAP) 12053 device_set_wakeup_enable(&pdev->dev, bp->wol); 12054 else 12055 device_set_wakeup_capable(&pdev->dev, false); 12056 12057 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 12058 bnxt_hwrm_coal_params_qcaps(bp); 12059 } 12060 12061 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 12062 12063 int bnxt_fw_init_one(struct bnxt *bp) 12064 { 12065 int rc; 12066 12067 rc = bnxt_fw_init_one_p1(bp); 12068 if (rc) { 12069 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 12070 return rc; 12071 } 12072 rc = bnxt_fw_init_one_p2(bp); 12073 if (rc) { 12074 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 12075 return rc; 12076 } 12077 rc = bnxt_probe_phy(bp, false); 12078 if (rc) 12079 return rc; 12080 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 12081 if (rc) 12082 return rc; 12083 12084 /* In case fw capabilities have changed, destroy the unneeded 12085 * reporters and create newly capable ones. 12086 */ 12087 bnxt_dl_fw_reporters_destroy(bp, false); 12088 bnxt_dl_fw_reporters_create(bp); 12089 bnxt_fw_init_one_p3(bp); 12090 return 0; 12091 } 12092 12093 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 12094 { 12095 struct bnxt_fw_health *fw_health = bp->fw_health; 12096 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 12097 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 12098 u32 reg_type, reg_off, delay_msecs; 12099 12100 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 12101 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 12102 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 12103 switch (reg_type) { 12104 case BNXT_FW_HEALTH_REG_TYPE_CFG: 12105 pci_write_config_dword(bp->pdev, reg_off, val); 12106 break; 12107 case BNXT_FW_HEALTH_REG_TYPE_GRC: 12108 writel(reg_off & BNXT_GRC_BASE_MASK, 12109 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 12110 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 12111 fallthrough; 12112 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 12113 writel(val, bp->bar0 + reg_off); 12114 break; 12115 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 12116 writel(val, bp->bar1 + reg_off); 12117 break; 12118 } 12119 if (delay_msecs) { 12120 pci_read_config_dword(bp->pdev, 0, &val); 12121 msleep(delay_msecs); 12122 } 12123 } 12124 12125 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 12126 { 12127 struct hwrm_func_qcfg_output *resp; 12128 struct hwrm_func_qcfg_input *req; 12129 bool result = true; /* firmware will enforce if unknown */ 12130 12131 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 12132 return result; 12133 12134 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 12135 return result; 12136 12137 req->fid = cpu_to_le16(0xffff); 12138 resp = hwrm_req_hold(bp, req); 12139 if (!hwrm_req_send(bp, req)) 12140 result = !!(le16_to_cpu(resp->flags) & 12141 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 12142 hwrm_req_drop(bp, req); 12143 return result; 12144 } 12145 12146 static void bnxt_reset_all(struct bnxt *bp) 12147 { 12148 struct bnxt_fw_health *fw_health = bp->fw_health; 12149 int i, rc; 12150 12151 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 12152 bnxt_fw_reset_via_optee(bp); 12153 bp->fw_reset_timestamp = jiffies; 12154 return; 12155 } 12156 12157 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 12158 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 12159 bnxt_fw_reset_writel(bp, i); 12160 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 12161 struct hwrm_fw_reset_input *req; 12162 12163 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 12164 if (!rc) { 12165 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 12166 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 12167 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 12168 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 12169 rc = hwrm_req_send(bp, req); 12170 } 12171 if (rc != -ENODEV) 12172 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 12173 } 12174 bp->fw_reset_timestamp = jiffies; 12175 } 12176 12177 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 12178 { 12179 return time_after(jiffies, bp->fw_reset_timestamp + 12180 (bp->fw_reset_max_dsecs * HZ / 10)); 12181 } 12182 12183 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 12184 { 12185 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 12186 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) { 12187 bnxt_ulp_start(bp, rc); 12188 bnxt_dl_health_fw_status_update(bp, false); 12189 } 12190 bp->fw_reset_state = 0; 12191 dev_close(bp->dev); 12192 } 12193 12194 static void bnxt_fw_reset_task(struct work_struct *work) 12195 { 12196 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 12197 int rc = 0; 12198 12199 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 12200 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 12201 return; 12202 } 12203 12204 switch (bp->fw_reset_state) { 12205 case BNXT_FW_RESET_STATE_POLL_VF: { 12206 int n = bnxt_get_registered_vfs(bp); 12207 int tmo; 12208 12209 if (n < 0) { 12210 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 12211 n, jiffies_to_msecs(jiffies - 12212 bp->fw_reset_timestamp)); 12213 goto fw_reset_abort; 12214 } else if (n > 0) { 12215 if (bnxt_fw_reset_timeout(bp)) { 12216 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 12217 bp->fw_reset_state = 0; 12218 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 12219 n); 12220 return; 12221 } 12222 bnxt_queue_fw_reset_work(bp, HZ / 10); 12223 return; 12224 } 12225 bp->fw_reset_timestamp = jiffies; 12226 rtnl_lock(); 12227 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12228 bnxt_fw_reset_abort(bp, rc); 12229 rtnl_unlock(); 12230 return; 12231 } 12232 bnxt_fw_reset_close(bp); 12233 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 12234 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 12235 tmo = HZ / 10; 12236 } else { 12237 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 12238 tmo = bp->fw_reset_min_dsecs * HZ / 10; 12239 } 12240 rtnl_unlock(); 12241 bnxt_queue_fw_reset_work(bp, tmo); 12242 return; 12243 } 12244 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 12245 u32 val; 12246 12247 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12248 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 12249 !bnxt_fw_reset_timeout(bp)) { 12250 bnxt_queue_fw_reset_work(bp, HZ / 5); 12251 return; 12252 } 12253 12254 if (!bp->fw_health->primary) { 12255 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 12256 12257 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 12258 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 12259 return; 12260 } 12261 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 12262 } 12263 fallthrough; 12264 case BNXT_FW_RESET_STATE_RESET_FW: 12265 bnxt_reset_all(bp); 12266 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 12267 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 12268 return; 12269 case BNXT_FW_RESET_STATE_ENABLE_DEV: 12270 bnxt_inv_fw_health_reg(bp); 12271 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 12272 !bp->fw_reset_min_dsecs) { 12273 u16 val; 12274 12275 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 12276 if (val == 0xffff) { 12277 if (bnxt_fw_reset_timeout(bp)) { 12278 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 12279 rc = -ETIMEDOUT; 12280 goto fw_reset_abort; 12281 } 12282 bnxt_queue_fw_reset_work(bp, HZ / 1000); 12283 return; 12284 } 12285 } 12286 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 12287 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 12288 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 12289 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 12290 bnxt_dl_remote_reload(bp); 12291 if (pci_enable_device(bp->pdev)) { 12292 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 12293 rc = -ENODEV; 12294 goto fw_reset_abort; 12295 } 12296 pci_set_master(bp->pdev); 12297 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 12298 fallthrough; 12299 case BNXT_FW_RESET_STATE_POLL_FW: 12300 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 12301 rc = bnxt_hwrm_poll(bp); 12302 if (rc) { 12303 if (bnxt_fw_reset_timeout(bp)) { 12304 netdev_err(bp->dev, "Firmware reset aborted\n"); 12305 goto fw_reset_abort_status; 12306 } 12307 bnxt_queue_fw_reset_work(bp, HZ / 5); 12308 return; 12309 } 12310 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 12311 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 12312 fallthrough; 12313 case BNXT_FW_RESET_STATE_OPENING: 12314 while (!rtnl_trylock()) { 12315 bnxt_queue_fw_reset_work(bp, HZ / 10); 12316 return; 12317 } 12318 rc = bnxt_open(bp->dev); 12319 if (rc) { 12320 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 12321 bnxt_fw_reset_abort(bp, rc); 12322 rtnl_unlock(); 12323 return; 12324 } 12325 12326 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 12327 bp->fw_health->enabled) { 12328 bp->fw_health->last_fw_reset_cnt = 12329 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 12330 } 12331 bp->fw_reset_state = 0; 12332 /* Make sure fw_reset_state is 0 before clearing the flag */ 12333 smp_mb__before_atomic(); 12334 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 12335 bnxt_ulp_start(bp, 0); 12336 bnxt_reenable_sriov(bp); 12337 bnxt_vf_reps_alloc(bp); 12338 bnxt_vf_reps_open(bp); 12339 bnxt_ptp_reapply_pps(bp); 12340 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 12341 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 12342 bnxt_dl_health_fw_recovery_done(bp); 12343 bnxt_dl_health_fw_status_update(bp, true); 12344 } 12345 rtnl_unlock(); 12346 break; 12347 } 12348 return; 12349 12350 fw_reset_abort_status: 12351 if (bp->fw_health->status_reliable || 12352 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 12353 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12354 12355 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 12356 } 12357 fw_reset_abort: 12358 rtnl_lock(); 12359 bnxt_fw_reset_abort(bp, rc); 12360 rtnl_unlock(); 12361 } 12362 12363 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 12364 { 12365 int rc; 12366 struct bnxt *bp = netdev_priv(dev); 12367 12368 SET_NETDEV_DEV(dev, &pdev->dev); 12369 12370 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 12371 rc = pci_enable_device(pdev); 12372 if (rc) { 12373 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 12374 goto init_err; 12375 } 12376 12377 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 12378 dev_err(&pdev->dev, 12379 "Cannot find PCI device base address, aborting\n"); 12380 rc = -ENODEV; 12381 goto init_err_disable; 12382 } 12383 12384 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 12385 if (rc) { 12386 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 12387 goto init_err_disable; 12388 } 12389 12390 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 12391 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 12392 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 12393 rc = -EIO; 12394 goto init_err_release; 12395 } 12396 12397 pci_set_master(pdev); 12398 12399 bp->dev = dev; 12400 bp->pdev = pdev; 12401 12402 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 12403 * determines the BAR size. 12404 */ 12405 bp->bar0 = pci_ioremap_bar(pdev, 0); 12406 if (!bp->bar0) { 12407 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 12408 rc = -ENOMEM; 12409 goto init_err_release; 12410 } 12411 12412 bp->bar2 = pci_ioremap_bar(pdev, 4); 12413 if (!bp->bar2) { 12414 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 12415 rc = -ENOMEM; 12416 goto init_err_release; 12417 } 12418 12419 pci_enable_pcie_error_reporting(pdev); 12420 12421 INIT_WORK(&bp->sp_task, bnxt_sp_task); 12422 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 12423 12424 spin_lock_init(&bp->ntp_fltr_lock); 12425 #if BITS_PER_LONG == 32 12426 spin_lock_init(&bp->db_lock); 12427 #endif 12428 12429 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 12430 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 12431 12432 timer_setup(&bp->timer, bnxt_timer, 0); 12433 bp->current_interval = BNXT_TIMER_INTERVAL; 12434 12435 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 12436 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 12437 12438 clear_bit(BNXT_STATE_OPEN, &bp->state); 12439 return 0; 12440 12441 init_err_release: 12442 bnxt_unmap_bars(bp, pdev); 12443 pci_release_regions(pdev); 12444 12445 init_err_disable: 12446 pci_disable_device(pdev); 12447 12448 init_err: 12449 return rc; 12450 } 12451 12452 /* rtnl_lock held */ 12453 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 12454 { 12455 struct sockaddr *addr = p; 12456 struct bnxt *bp = netdev_priv(dev); 12457 int rc = 0; 12458 12459 if (!is_valid_ether_addr(addr->sa_data)) 12460 return -EADDRNOTAVAIL; 12461 12462 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 12463 return 0; 12464 12465 rc = bnxt_approve_mac(bp, addr->sa_data, true); 12466 if (rc) 12467 return rc; 12468 12469 eth_hw_addr_set(dev, addr->sa_data); 12470 if (netif_running(dev)) { 12471 bnxt_close_nic(bp, false, false); 12472 rc = bnxt_open_nic(bp, false, false); 12473 } 12474 12475 return rc; 12476 } 12477 12478 /* rtnl_lock held */ 12479 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 12480 { 12481 struct bnxt *bp = netdev_priv(dev); 12482 12483 if (netif_running(dev)) 12484 bnxt_close_nic(bp, true, false); 12485 12486 dev->mtu = new_mtu; 12487 bnxt_set_ring_params(bp); 12488 12489 if (netif_running(dev)) 12490 return bnxt_open_nic(bp, true, false); 12491 12492 return 0; 12493 } 12494 12495 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 12496 { 12497 struct bnxt *bp = netdev_priv(dev); 12498 bool sh = false; 12499 int rc; 12500 12501 if (tc > bp->max_tc) { 12502 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 12503 tc, bp->max_tc); 12504 return -EINVAL; 12505 } 12506 12507 if (netdev_get_num_tc(dev) == tc) 12508 return 0; 12509 12510 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 12511 sh = true; 12512 12513 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 12514 sh, tc, bp->tx_nr_rings_xdp); 12515 if (rc) 12516 return rc; 12517 12518 /* Needs to close the device and do hw resource re-allocations */ 12519 if (netif_running(bp->dev)) 12520 bnxt_close_nic(bp, true, false); 12521 12522 if (tc) { 12523 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 12524 netdev_set_num_tc(dev, tc); 12525 } else { 12526 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 12527 netdev_reset_tc(dev); 12528 } 12529 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 12530 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) : 12531 bp->tx_nr_rings + bp->rx_nr_rings; 12532 12533 if (netif_running(bp->dev)) 12534 return bnxt_open_nic(bp, true, false); 12535 12536 return 0; 12537 } 12538 12539 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 12540 void *cb_priv) 12541 { 12542 struct bnxt *bp = cb_priv; 12543 12544 if (!bnxt_tc_flower_enabled(bp) || 12545 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 12546 return -EOPNOTSUPP; 12547 12548 switch (type) { 12549 case TC_SETUP_CLSFLOWER: 12550 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 12551 default: 12552 return -EOPNOTSUPP; 12553 } 12554 } 12555 12556 LIST_HEAD(bnxt_block_cb_list); 12557 12558 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 12559 void *type_data) 12560 { 12561 struct bnxt *bp = netdev_priv(dev); 12562 12563 switch (type) { 12564 case TC_SETUP_BLOCK: 12565 return flow_block_cb_setup_simple(type_data, 12566 &bnxt_block_cb_list, 12567 bnxt_setup_tc_block_cb, 12568 bp, bp, true); 12569 case TC_SETUP_QDISC_MQPRIO: { 12570 struct tc_mqprio_qopt *mqprio = type_data; 12571 12572 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 12573 12574 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 12575 } 12576 default: 12577 return -EOPNOTSUPP; 12578 } 12579 } 12580 12581 #ifdef CONFIG_RFS_ACCEL 12582 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 12583 struct bnxt_ntuple_filter *f2) 12584 { 12585 struct flow_keys *keys1 = &f1->fkeys; 12586 struct flow_keys *keys2 = &f2->fkeys; 12587 12588 if (keys1->basic.n_proto != keys2->basic.n_proto || 12589 keys1->basic.ip_proto != keys2->basic.ip_proto) 12590 return false; 12591 12592 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 12593 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 12594 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst) 12595 return false; 12596 } else { 12597 if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src, 12598 sizeof(keys1->addrs.v6addrs.src)) || 12599 memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst, 12600 sizeof(keys1->addrs.v6addrs.dst))) 12601 return false; 12602 } 12603 12604 if (keys1->ports.ports == keys2->ports.ports && 12605 keys1->control.flags == keys2->control.flags && 12606 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) && 12607 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr)) 12608 return true; 12609 12610 return false; 12611 } 12612 12613 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 12614 u16 rxq_index, u32 flow_id) 12615 { 12616 struct bnxt *bp = netdev_priv(dev); 12617 struct bnxt_ntuple_filter *fltr, *new_fltr; 12618 struct flow_keys *fkeys; 12619 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 12620 int rc = 0, idx, bit_id, l2_idx = 0; 12621 struct hlist_head *head; 12622 u32 flags; 12623 12624 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) { 12625 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 12626 int off = 0, j; 12627 12628 netif_addr_lock_bh(dev); 12629 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) { 12630 if (ether_addr_equal(eth->h_dest, 12631 vnic->uc_list + off)) { 12632 l2_idx = j + 1; 12633 break; 12634 } 12635 } 12636 netif_addr_unlock_bh(dev); 12637 if (!l2_idx) 12638 return -EINVAL; 12639 } 12640 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 12641 if (!new_fltr) 12642 return -ENOMEM; 12643 12644 fkeys = &new_fltr->fkeys; 12645 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 12646 rc = -EPROTONOSUPPORT; 12647 goto err_free; 12648 } 12649 12650 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 12651 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 12652 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 12653 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 12654 rc = -EPROTONOSUPPORT; 12655 goto err_free; 12656 } 12657 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) && 12658 bp->hwrm_spec_code < 0x10601) { 12659 rc = -EPROTONOSUPPORT; 12660 goto err_free; 12661 } 12662 flags = fkeys->control.flags; 12663 if (((flags & FLOW_DIS_ENCAPSULATION) && 12664 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 12665 rc = -EPROTONOSUPPORT; 12666 goto err_free; 12667 } 12668 12669 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN); 12670 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN); 12671 12672 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 12673 head = &bp->ntp_fltr_hash_tbl[idx]; 12674 rcu_read_lock(); 12675 hlist_for_each_entry_rcu(fltr, head, hash) { 12676 if (bnxt_fltr_match(fltr, new_fltr)) { 12677 rcu_read_unlock(); 12678 rc = 0; 12679 goto err_free; 12680 } 12681 } 12682 rcu_read_unlock(); 12683 12684 spin_lock_bh(&bp->ntp_fltr_lock); 12685 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 12686 BNXT_NTP_FLTR_MAX_FLTR, 0); 12687 if (bit_id < 0) { 12688 spin_unlock_bh(&bp->ntp_fltr_lock); 12689 rc = -ENOMEM; 12690 goto err_free; 12691 } 12692 12693 new_fltr->sw_id = (u16)bit_id; 12694 new_fltr->flow_id = flow_id; 12695 new_fltr->l2_fltr_idx = l2_idx; 12696 new_fltr->rxq = rxq_index; 12697 hlist_add_head_rcu(&new_fltr->hash, head); 12698 bp->ntp_fltr_count++; 12699 spin_unlock_bh(&bp->ntp_fltr_lock); 12700 12701 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event); 12702 bnxt_queue_sp_work(bp); 12703 12704 return new_fltr->sw_id; 12705 12706 err_free: 12707 kfree(new_fltr); 12708 return rc; 12709 } 12710 12711 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 12712 { 12713 int i; 12714 12715 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 12716 struct hlist_head *head; 12717 struct hlist_node *tmp; 12718 struct bnxt_ntuple_filter *fltr; 12719 int rc; 12720 12721 head = &bp->ntp_fltr_hash_tbl[i]; 12722 hlist_for_each_entry_safe(fltr, tmp, head, hash) { 12723 bool del = false; 12724 12725 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) { 12726 if (rps_may_expire_flow(bp->dev, fltr->rxq, 12727 fltr->flow_id, 12728 fltr->sw_id)) { 12729 bnxt_hwrm_cfa_ntuple_filter_free(bp, 12730 fltr); 12731 del = true; 12732 } 12733 } else { 12734 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 12735 fltr); 12736 if (rc) 12737 del = true; 12738 else 12739 set_bit(BNXT_FLTR_VALID, &fltr->state); 12740 } 12741 12742 if (del) { 12743 spin_lock_bh(&bp->ntp_fltr_lock); 12744 hlist_del_rcu(&fltr->hash); 12745 bp->ntp_fltr_count--; 12746 spin_unlock_bh(&bp->ntp_fltr_lock); 12747 synchronize_rcu(); 12748 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 12749 kfree(fltr); 12750 } 12751 } 12752 } 12753 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 12754 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 12755 } 12756 12757 #else 12758 12759 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 12760 { 12761 } 12762 12763 #endif /* CONFIG_RFS_ACCEL */ 12764 12765 static int bnxt_udp_tunnel_sync(struct net_device *netdev, unsigned int table) 12766 { 12767 struct bnxt *bp = netdev_priv(netdev); 12768 struct udp_tunnel_info ti; 12769 unsigned int cmd; 12770 12771 udp_tunnel_nic_get_port(netdev, table, 0, &ti); 12772 if (ti.type == UDP_TUNNEL_TYPE_VXLAN) 12773 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 12774 else 12775 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 12776 12777 if (ti.port) 12778 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti.port, cmd); 12779 12780 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 12781 } 12782 12783 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 12784 .sync_table = bnxt_udp_tunnel_sync, 12785 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 12786 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 12787 .tables = { 12788 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 12789 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 12790 }, 12791 }; 12792 12793 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 12794 struct net_device *dev, u32 filter_mask, 12795 int nlflags) 12796 { 12797 struct bnxt *bp = netdev_priv(dev); 12798 12799 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 12800 nlflags, filter_mask, NULL); 12801 } 12802 12803 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 12804 u16 flags, struct netlink_ext_ack *extack) 12805 { 12806 struct bnxt *bp = netdev_priv(dev); 12807 struct nlattr *attr, *br_spec; 12808 int rem, rc = 0; 12809 12810 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 12811 return -EOPNOTSUPP; 12812 12813 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 12814 if (!br_spec) 12815 return -EINVAL; 12816 12817 nla_for_each_nested(attr, br_spec, rem) { 12818 u16 mode; 12819 12820 if (nla_type(attr) != IFLA_BRIDGE_MODE) 12821 continue; 12822 12823 if (nla_len(attr) < sizeof(mode)) 12824 return -EINVAL; 12825 12826 mode = nla_get_u16(attr); 12827 if (mode == bp->br_mode) 12828 break; 12829 12830 rc = bnxt_hwrm_set_br_mode(bp, mode); 12831 if (!rc) 12832 bp->br_mode = mode; 12833 break; 12834 } 12835 return rc; 12836 } 12837 12838 int bnxt_get_port_parent_id(struct net_device *dev, 12839 struct netdev_phys_item_id *ppid) 12840 { 12841 struct bnxt *bp = netdev_priv(dev); 12842 12843 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 12844 return -EOPNOTSUPP; 12845 12846 /* The PF and it's VF-reps only support the switchdev framework */ 12847 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 12848 return -EOPNOTSUPP; 12849 12850 ppid->id_len = sizeof(bp->dsn); 12851 memcpy(ppid->id, bp->dsn, ppid->id_len); 12852 12853 return 0; 12854 } 12855 12856 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev) 12857 { 12858 struct bnxt *bp = netdev_priv(dev); 12859 12860 return &bp->dl_port; 12861 } 12862 12863 static const struct net_device_ops bnxt_netdev_ops = { 12864 .ndo_open = bnxt_open, 12865 .ndo_start_xmit = bnxt_start_xmit, 12866 .ndo_stop = bnxt_close, 12867 .ndo_get_stats64 = bnxt_get_stats64, 12868 .ndo_set_rx_mode = bnxt_set_rx_mode, 12869 .ndo_eth_ioctl = bnxt_ioctl, 12870 .ndo_validate_addr = eth_validate_addr, 12871 .ndo_set_mac_address = bnxt_change_mac_addr, 12872 .ndo_change_mtu = bnxt_change_mtu, 12873 .ndo_fix_features = bnxt_fix_features, 12874 .ndo_set_features = bnxt_set_features, 12875 .ndo_features_check = bnxt_features_check, 12876 .ndo_tx_timeout = bnxt_tx_timeout, 12877 #ifdef CONFIG_BNXT_SRIOV 12878 .ndo_get_vf_config = bnxt_get_vf_config, 12879 .ndo_set_vf_mac = bnxt_set_vf_mac, 12880 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 12881 .ndo_set_vf_rate = bnxt_set_vf_bw, 12882 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 12883 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 12884 .ndo_set_vf_trust = bnxt_set_vf_trust, 12885 #endif 12886 .ndo_setup_tc = bnxt_setup_tc, 12887 #ifdef CONFIG_RFS_ACCEL 12888 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 12889 #endif 12890 .ndo_bpf = bnxt_xdp, 12891 .ndo_xdp_xmit = bnxt_xdp_xmit, 12892 .ndo_bridge_getlink = bnxt_bridge_getlink, 12893 .ndo_bridge_setlink = bnxt_bridge_setlink, 12894 .ndo_get_devlink_port = bnxt_get_devlink_port, 12895 }; 12896 12897 static void bnxt_remove_one(struct pci_dev *pdev) 12898 { 12899 struct net_device *dev = pci_get_drvdata(pdev); 12900 struct bnxt *bp = netdev_priv(dev); 12901 12902 if (BNXT_PF(bp)) 12903 bnxt_sriov_disable(bp); 12904 12905 if (BNXT_PF(bp)) 12906 devlink_port_type_clear(&bp->dl_port); 12907 12908 bnxt_ptp_clear(bp); 12909 pci_disable_pcie_error_reporting(pdev); 12910 unregister_netdev(dev); 12911 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 12912 /* Flush any pending tasks */ 12913 cancel_work_sync(&bp->sp_task); 12914 cancel_delayed_work_sync(&bp->fw_reset_task); 12915 bp->sp_event = 0; 12916 12917 bnxt_dl_fw_reporters_destroy(bp, true); 12918 bnxt_dl_unregister(bp); 12919 bnxt_shutdown_tc(bp); 12920 12921 bnxt_clear_int_mode(bp); 12922 bnxt_hwrm_func_drv_unrgtr(bp); 12923 bnxt_free_hwrm_resources(bp); 12924 bnxt_ethtool_free(bp); 12925 bnxt_dcb_free(bp); 12926 kfree(bp->edev); 12927 bp->edev = NULL; 12928 kfree(bp->ptp_cfg); 12929 bp->ptp_cfg = NULL; 12930 kfree(bp->fw_health); 12931 bp->fw_health = NULL; 12932 bnxt_cleanup_pci(bp); 12933 bnxt_free_ctx_mem(bp); 12934 kfree(bp->ctx); 12935 bp->ctx = NULL; 12936 kfree(bp->rss_indir_tbl); 12937 bp->rss_indir_tbl = NULL; 12938 bnxt_free_port_stats(bp); 12939 free_netdev(dev); 12940 } 12941 12942 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 12943 { 12944 int rc = 0; 12945 struct bnxt_link_info *link_info = &bp->link_info; 12946 12947 bp->phy_flags = 0; 12948 rc = bnxt_hwrm_phy_qcaps(bp); 12949 if (rc) { 12950 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 12951 rc); 12952 return rc; 12953 } 12954 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 12955 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 12956 else 12957 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 12958 if (!fw_dflt) 12959 return 0; 12960 12961 mutex_lock(&bp->link_lock); 12962 rc = bnxt_update_link(bp, false); 12963 if (rc) { 12964 mutex_unlock(&bp->link_lock); 12965 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 12966 rc); 12967 return rc; 12968 } 12969 12970 /* Older firmware does not have supported_auto_speeds, so assume 12971 * that all supported speeds can be autonegotiated. 12972 */ 12973 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 12974 link_info->support_auto_speeds = link_info->support_speeds; 12975 12976 bnxt_init_ethtool_link_settings(bp); 12977 mutex_unlock(&bp->link_lock); 12978 return 0; 12979 } 12980 12981 static int bnxt_get_max_irq(struct pci_dev *pdev) 12982 { 12983 u16 ctrl; 12984 12985 if (!pdev->msix_cap) 12986 return 1; 12987 12988 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 12989 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 12990 } 12991 12992 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 12993 int *max_cp) 12994 { 12995 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12996 int max_ring_grps = 0, max_irq; 12997 12998 *max_tx = hw_resc->max_tx_rings; 12999 *max_rx = hw_resc->max_rx_rings; 13000 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 13001 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 13002 bnxt_get_ulp_msix_num(bp), 13003 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp)); 13004 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) 13005 *max_cp = min_t(int, *max_cp, max_irq); 13006 max_ring_grps = hw_resc->max_hw_ring_grps; 13007 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 13008 *max_cp -= 1; 13009 *max_rx -= 2; 13010 } 13011 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13012 *max_rx >>= 1; 13013 if (bp->flags & BNXT_FLAG_CHIP_P5) { 13014 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 13015 /* On P5 chips, max_cp output param should be available NQs */ 13016 *max_cp = max_irq; 13017 } 13018 *max_rx = min_t(int, *max_rx, max_ring_grps); 13019 } 13020 13021 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 13022 { 13023 int rx, tx, cp; 13024 13025 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 13026 *max_rx = rx; 13027 *max_tx = tx; 13028 if (!rx || !tx || !cp) 13029 return -ENOMEM; 13030 13031 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 13032 } 13033 13034 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 13035 bool shared) 13036 { 13037 int rc; 13038 13039 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 13040 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 13041 /* Not enough rings, try disabling agg rings. */ 13042 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 13043 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 13044 if (rc) { 13045 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 13046 bp->flags |= BNXT_FLAG_AGG_RINGS; 13047 return rc; 13048 } 13049 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 13050 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13051 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13052 bnxt_set_ring_params(bp); 13053 } 13054 13055 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 13056 int max_cp, max_stat, max_irq; 13057 13058 /* Reserve minimum resources for RoCE */ 13059 max_cp = bnxt_get_max_func_cp_rings(bp); 13060 max_stat = bnxt_get_max_func_stat_ctxs(bp); 13061 max_irq = bnxt_get_max_func_irqs(bp); 13062 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 13063 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 13064 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 13065 return 0; 13066 13067 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 13068 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 13069 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 13070 max_cp = min_t(int, max_cp, max_irq); 13071 max_cp = min_t(int, max_cp, max_stat); 13072 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 13073 if (rc) 13074 rc = 0; 13075 } 13076 return rc; 13077 } 13078 13079 /* In initial default shared ring setting, each shared ring must have a 13080 * RX/TX ring pair. 13081 */ 13082 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 13083 { 13084 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 13085 bp->rx_nr_rings = bp->cp_nr_rings; 13086 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 13087 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 13088 } 13089 13090 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 13091 { 13092 int dflt_rings, max_rx_rings, max_tx_rings, rc; 13093 13094 if (!bnxt_can_reserve_rings(bp)) 13095 return 0; 13096 13097 if (sh) 13098 bp->flags |= BNXT_FLAG_SHARED_RINGS; 13099 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 13100 /* Reduce default rings on multi-port cards so that total default 13101 * rings do not exceed CPU count. 13102 */ 13103 if (bp->port_count > 1) { 13104 int max_rings = 13105 max_t(int, num_online_cpus() / bp->port_count, 1); 13106 13107 dflt_rings = min_t(int, dflt_rings, max_rings); 13108 } 13109 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 13110 if (rc) 13111 return rc; 13112 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 13113 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 13114 if (sh) 13115 bnxt_trim_dflt_sh_rings(bp); 13116 else 13117 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 13118 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 13119 13120 rc = __bnxt_reserve_rings(bp); 13121 if (rc && rc != -ENODEV) 13122 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 13123 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 13124 if (sh) 13125 bnxt_trim_dflt_sh_rings(bp); 13126 13127 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 13128 if (bnxt_need_reserve_rings(bp)) { 13129 rc = __bnxt_reserve_rings(bp); 13130 if (rc && rc != -ENODEV) 13131 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 13132 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 13133 } 13134 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 13135 bp->rx_nr_rings++; 13136 bp->cp_nr_rings++; 13137 } 13138 if (rc) { 13139 bp->tx_nr_rings = 0; 13140 bp->rx_nr_rings = 0; 13141 } 13142 return rc; 13143 } 13144 13145 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 13146 { 13147 int rc; 13148 13149 if (bp->tx_nr_rings) 13150 return 0; 13151 13152 bnxt_ulp_irq_stop(bp); 13153 bnxt_clear_int_mode(bp); 13154 rc = bnxt_set_dflt_rings(bp, true); 13155 if (rc) { 13156 if (BNXT_VF(bp) && rc == -ENODEV) 13157 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 13158 else 13159 netdev_err(bp->dev, "Not enough rings available.\n"); 13160 goto init_dflt_ring_err; 13161 } 13162 rc = bnxt_init_int_mode(bp); 13163 if (rc) 13164 goto init_dflt_ring_err; 13165 13166 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 13167 if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) { 13168 bp->flags |= BNXT_FLAG_RFS; 13169 bp->dev->features |= NETIF_F_NTUPLE; 13170 } 13171 init_dflt_ring_err: 13172 bnxt_ulp_irq_restart(bp, rc); 13173 return rc; 13174 } 13175 13176 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 13177 { 13178 int rc; 13179 13180 ASSERT_RTNL(); 13181 bnxt_hwrm_func_qcaps(bp); 13182 13183 if (netif_running(bp->dev)) 13184 __bnxt_close_nic(bp, true, false); 13185 13186 bnxt_ulp_irq_stop(bp); 13187 bnxt_clear_int_mode(bp); 13188 rc = bnxt_init_int_mode(bp); 13189 bnxt_ulp_irq_restart(bp, rc); 13190 13191 if (netif_running(bp->dev)) { 13192 if (rc) 13193 dev_close(bp->dev); 13194 else 13195 rc = bnxt_open_nic(bp, true, false); 13196 } 13197 13198 return rc; 13199 } 13200 13201 static int bnxt_init_mac_addr(struct bnxt *bp) 13202 { 13203 int rc = 0; 13204 13205 if (BNXT_PF(bp)) { 13206 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 13207 } else { 13208 #ifdef CONFIG_BNXT_SRIOV 13209 struct bnxt_vf_info *vf = &bp->vf; 13210 bool strict_approval = true; 13211 13212 if (is_valid_ether_addr(vf->mac_addr)) { 13213 /* overwrite netdev dev_addr with admin VF MAC */ 13214 eth_hw_addr_set(bp->dev, vf->mac_addr); 13215 /* Older PF driver or firmware may not approve this 13216 * correctly. 13217 */ 13218 strict_approval = false; 13219 } else { 13220 eth_hw_addr_random(bp->dev); 13221 } 13222 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 13223 #endif 13224 } 13225 return rc; 13226 } 13227 13228 static void bnxt_vpd_read_info(struct bnxt *bp) 13229 { 13230 struct pci_dev *pdev = bp->pdev; 13231 unsigned int vpd_size, kw_len; 13232 int pos, size; 13233 u8 *vpd_data; 13234 13235 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 13236 if (IS_ERR(vpd_data)) { 13237 pci_warn(pdev, "Unable to read VPD\n"); 13238 return; 13239 } 13240 13241 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 13242 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 13243 if (pos < 0) 13244 goto read_sn; 13245 13246 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 13247 memcpy(bp->board_partno, &vpd_data[pos], size); 13248 13249 read_sn: 13250 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 13251 PCI_VPD_RO_KEYWORD_SERIALNO, 13252 &kw_len); 13253 if (pos < 0) 13254 goto exit; 13255 13256 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 13257 memcpy(bp->board_serialno, &vpd_data[pos], size); 13258 exit: 13259 kfree(vpd_data); 13260 } 13261 13262 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 13263 { 13264 struct pci_dev *pdev = bp->pdev; 13265 u64 qword; 13266 13267 qword = pci_get_dsn(pdev); 13268 if (!qword) { 13269 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 13270 return -EOPNOTSUPP; 13271 } 13272 13273 put_unaligned_le64(qword, dsn); 13274 13275 bp->flags |= BNXT_FLAG_DSN_VALID; 13276 return 0; 13277 } 13278 13279 static int bnxt_map_db_bar(struct bnxt *bp) 13280 { 13281 if (!bp->db_size) 13282 return -ENODEV; 13283 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 13284 if (!bp->bar1) 13285 return -ENOMEM; 13286 return 0; 13287 } 13288 13289 void bnxt_print_device_info(struct bnxt *bp) 13290 { 13291 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 13292 board_info[bp->board_idx].name, 13293 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 13294 13295 pcie_print_link_status(bp->pdev); 13296 } 13297 13298 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 13299 { 13300 struct net_device *dev; 13301 struct bnxt *bp; 13302 int rc, max_irqs; 13303 13304 if (pci_is_bridge(pdev)) 13305 return -ENODEV; 13306 13307 /* Clear any pending DMA transactions from crash kernel 13308 * while loading driver in capture kernel. 13309 */ 13310 if (is_kdump_kernel()) { 13311 pci_clear_master(pdev); 13312 pcie_flr(pdev); 13313 } 13314 13315 max_irqs = bnxt_get_max_irq(pdev); 13316 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs); 13317 if (!dev) 13318 return -ENOMEM; 13319 13320 bp = netdev_priv(dev); 13321 bp->board_idx = ent->driver_data; 13322 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 13323 bnxt_set_max_func_irqs(bp, max_irqs); 13324 13325 if (bnxt_vf_pciid(bp->board_idx)) 13326 bp->flags |= BNXT_FLAG_VF; 13327 13328 if (pdev->msix_cap) 13329 bp->flags |= BNXT_FLAG_MSIX_CAP; 13330 13331 rc = bnxt_init_board(pdev, dev); 13332 if (rc < 0) 13333 goto init_err_free; 13334 13335 dev->netdev_ops = &bnxt_netdev_ops; 13336 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 13337 dev->ethtool_ops = &bnxt_ethtool_ops; 13338 pci_set_drvdata(pdev, dev); 13339 13340 rc = bnxt_alloc_hwrm_resources(bp); 13341 if (rc) 13342 goto init_err_pci_clean; 13343 13344 mutex_init(&bp->hwrm_cmd_lock); 13345 mutex_init(&bp->link_lock); 13346 13347 rc = bnxt_fw_init_one_p1(bp); 13348 if (rc) 13349 goto init_err_pci_clean; 13350 13351 if (BNXT_PF(bp)) 13352 bnxt_vpd_read_info(bp); 13353 13354 if (BNXT_CHIP_P5(bp)) { 13355 bp->flags |= BNXT_FLAG_CHIP_P5; 13356 if (BNXT_CHIP_SR2(bp)) 13357 bp->flags |= BNXT_FLAG_CHIP_SR2; 13358 } 13359 13360 rc = bnxt_alloc_rss_indir_tbl(bp); 13361 if (rc) 13362 goto init_err_pci_clean; 13363 13364 rc = bnxt_fw_init_one_p2(bp); 13365 if (rc) 13366 goto init_err_pci_clean; 13367 13368 rc = bnxt_map_db_bar(bp); 13369 if (rc) { 13370 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 13371 rc); 13372 goto init_err_pci_clean; 13373 } 13374 13375 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 13376 NETIF_F_TSO | NETIF_F_TSO6 | 13377 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 13378 NETIF_F_GSO_IPXIP4 | 13379 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 13380 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 13381 NETIF_F_RXCSUM | NETIF_F_GRO; 13382 13383 if (BNXT_SUPPORTS_TPA(bp)) 13384 dev->hw_features |= NETIF_F_LRO; 13385 13386 dev->hw_enc_features = 13387 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 13388 NETIF_F_TSO | NETIF_F_TSO6 | 13389 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 13390 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 13391 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 13392 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 13393 13394 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 13395 NETIF_F_GSO_GRE_CSUM; 13396 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 13397 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 13398 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13399 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 13400 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 13401 if (BNXT_SUPPORTS_TPA(bp)) 13402 dev->hw_features |= NETIF_F_GRO_HW; 13403 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 13404 if (dev->features & NETIF_F_GRO_HW) 13405 dev->features &= ~NETIF_F_LRO; 13406 dev->priv_flags |= IFF_UNICAST_FLT; 13407 13408 #ifdef CONFIG_BNXT_SRIOV 13409 init_waitqueue_head(&bp->sriov_cfg_wait); 13410 mutex_init(&bp->sriov_lock); 13411 #endif 13412 if (BNXT_SUPPORTS_TPA(bp)) { 13413 bp->gro_func = bnxt_gro_func_5730x; 13414 if (BNXT_CHIP_P4(bp)) 13415 bp->gro_func = bnxt_gro_func_5731x; 13416 else if (BNXT_CHIP_P5(bp)) 13417 bp->gro_func = bnxt_gro_func_5750x; 13418 } 13419 if (!BNXT_CHIP_P4_PLUS(bp)) 13420 bp->flags |= BNXT_FLAG_DOUBLE_DB; 13421 13422 rc = bnxt_init_mac_addr(bp); 13423 if (rc) { 13424 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 13425 rc = -EADDRNOTAVAIL; 13426 goto init_err_pci_clean; 13427 } 13428 13429 if (BNXT_PF(bp)) { 13430 /* Read the adapter's DSN to use as the eswitch switch_id */ 13431 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 13432 } 13433 13434 /* MTU range: 60 - FW defined max */ 13435 dev->min_mtu = ETH_ZLEN; 13436 dev->max_mtu = bp->max_mtu; 13437 13438 rc = bnxt_probe_phy(bp, true); 13439 if (rc) 13440 goto init_err_pci_clean; 13441 13442 bnxt_set_rx_skb_mode(bp, false); 13443 bnxt_set_tpa_flags(bp); 13444 bnxt_set_ring_params(bp); 13445 rc = bnxt_set_dflt_rings(bp, true); 13446 if (rc) { 13447 if (BNXT_VF(bp) && rc == -ENODEV) { 13448 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 13449 } else { 13450 netdev_err(bp->dev, "Not enough rings available.\n"); 13451 rc = -ENOMEM; 13452 } 13453 goto init_err_pci_clean; 13454 } 13455 13456 bnxt_fw_init_one_p3(bp); 13457 13458 bnxt_init_dflt_coal(bp); 13459 13460 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13461 bp->flags |= BNXT_FLAG_STRIP_VLAN; 13462 13463 rc = bnxt_init_int_mode(bp); 13464 if (rc) 13465 goto init_err_pci_clean; 13466 13467 /* No TC has been set yet and rings may have been trimmed due to 13468 * limited MSIX, so we re-initialize the TX rings per TC. 13469 */ 13470 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 13471 13472 if (BNXT_PF(bp)) { 13473 if (!bnxt_pf_wq) { 13474 bnxt_pf_wq = 13475 create_singlethread_workqueue("bnxt_pf_wq"); 13476 if (!bnxt_pf_wq) { 13477 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 13478 rc = -ENOMEM; 13479 goto init_err_pci_clean; 13480 } 13481 } 13482 rc = bnxt_init_tc(bp); 13483 if (rc) 13484 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 13485 rc); 13486 } 13487 13488 bnxt_inv_fw_health_reg(bp); 13489 rc = bnxt_dl_register(bp); 13490 if (rc) 13491 goto init_err_dl; 13492 13493 rc = register_netdev(dev); 13494 if (rc) 13495 goto init_err_cleanup; 13496 13497 if (BNXT_PF(bp)) 13498 devlink_port_type_eth_set(&bp->dl_port, bp->dev); 13499 bnxt_dl_fw_reporters_create(bp); 13500 13501 bnxt_print_device_info(bp); 13502 13503 pci_save_state(pdev); 13504 return 0; 13505 13506 init_err_cleanup: 13507 bnxt_dl_unregister(bp); 13508 init_err_dl: 13509 bnxt_shutdown_tc(bp); 13510 bnxt_clear_int_mode(bp); 13511 13512 init_err_pci_clean: 13513 bnxt_hwrm_func_drv_unrgtr(bp); 13514 bnxt_free_hwrm_resources(bp); 13515 bnxt_ethtool_free(bp); 13516 bnxt_ptp_clear(bp); 13517 kfree(bp->ptp_cfg); 13518 bp->ptp_cfg = NULL; 13519 kfree(bp->fw_health); 13520 bp->fw_health = NULL; 13521 bnxt_cleanup_pci(bp); 13522 bnxt_free_ctx_mem(bp); 13523 kfree(bp->ctx); 13524 bp->ctx = NULL; 13525 kfree(bp->rss_indir_tbl); 13526 bp->rss_indir_tbl = NULL; 13527 13528 init_err_free: 13529 free_netdev(dev); 13530 return rc; 13531 } 13532 13533 static void bnxt_shutdown(struct pci_dev *pdev) 13534 { 13535 struct net_device *dev = pci_get_drvdata(pdev); 13536 struct bnxt *bp; 13537 13538 if (!dev) 13539 return; 13540 13541 rtnl_lock(); 13542 bp = netdev_priv(dev); 13543 if (!bp) 13544 goto shutdown_exit; 13545 13546 if (netif_running(dev)) 13547 dev_close(dev); 13548 13549 bnxt_ulp_shutdown(bp); 13550 bnxt_clear_int_mode(bp); 13551 pci_disable_device(pdev); 13552 13553 if (system_state == SYSTEM_POWER_OFF) { 13554 pci_wake_from_d3(pdev, bp->wol); 13555 pci_set_power_state(pdev, PCI_D3hot); 13556 } 13557 13558 shutdown_exit: 13559 rtnl_unlock(); 13560 } 13561 13562 #ifdef CONFIG_PM_SLEEP 13563 static int bnxt_suspend(struct device *device) 13564 { 13565 struct net_device *dev = dev_get_drvdata(device); 13566 struct bnxt *bp = netdev_priv(dev); 13567 int rc = 0; 13568 13569 rtnl_lock(); 13570 bnxt_ulp_stop(bp); 13571 if (netif_running(dev)) { 13572 netif_device_detach(dev); 13573 rc = bnxt_close(dev); 13574 } 13575 bnxt_hwrm_func_drv_unrgtr(bp); 13576 pci_disable_device(bp->pdev); 13577 bnxt_free_ctx_mem(bp); 13578 kfree(bp->ctx); 13579 bp->ctx = NULL; 13580 rtnl_unlock(); 13581 return rc; 13582 } 13583 13584 static int bnxt_resume(struct device *device) 13585 { 13586 struct net_device *dev = dev_get_drvdata(device); 13587 struct bnxt *bp = netdev_priv(dev); 13588 int rc = 0; 13589 13590 rtnl_lock(); 13591 rc = pci_enable_device(bp->pdev); 13592 if (rc) { 13593 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 13594 rc); 13595 goto resume_exit; 13596 } 13597 pci_set_master(bp->pdev); 13598 if (bnxt_hwrm_ver_get(bp)) { 13599 rc = -ENODEV; 13600 goto resume_exit; 13601 } 13602 rc = bnxt_hwrm_func_reset(bp); 13603 if (rc) { 13604 rc = -EBUSY; 13605 goto resume_exit; 13606 } 13607 13608 rc = bnxt_hwrm_func_qcaps(bp); 13609 if (rc) 13610 goto resume_exit; 13611 13612 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 13613 rc = -ENODEV; 13614 goto resume_exit; 13615 } 13616 13617 bnxt_get_wol_settings(bp); 13618 if (netif_running(dev)) { 13619 rc = bnxt_open(dev); 13620 if (!rc) 13621 netif_device_attach(dev); 13622 } 13623 13624 resume_exit: 13625 bnxt_ulp_start(bp, rc); 13626 if (!rc) 13627 bnxt_reenable_sriov(bp); 13628 rtnl_unlock(); 13629 return rc; 13630 } 13631 13632 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 13633 #define BNXT_PM_OPS (&bnxt_pm_ops) 13634 13635 #else 13636 13637 #define BNXT_PM_OPS NULL 13638 13639 #endif /* CONFIG_PM_SLEEP */ 13640 13641 /** 13642 * bnxt_io_error_detected - called when PCI error is detected 13643 * @pdev: Pointer to PCI device 13644 * @state: The current pci connection state 13645 * 13646 * This function is called after a PCI bus error affecting 13647 * this device has been detected. 13648 */ 13649 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 13650 pci_channel_state_t state) 13651 { 13652 struct net_device *netdev = pci_get_drvdata(pdev); 13653 struct bnxt *bp = netdev_priv(netdev); 13654 13655 netdev_info(netdev, "PCI I/O error detected\n"); 13656 13657 rtnl_lock(); 13658 netif_device_detach(netdev); 13659 13660 bnxt_ulp_stop(bp); 13661 13662 if (state == pci_channel_io_perm_failure) { 13663 rtnl_unlock(); 13664 return PCI_ERS_RESULT_DISCONNECT; 13665 } 13666 13667 if (state == pci_channel_io_frozen) 13668 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 13669 13670 if (netif_running(netdev)) 13671 bnxt_close(netdev); 13672 13673 if (pci_is_enabled(pdev)) 13674 pci_disable_device(pdev); 13675 bnxt_free_ctx_mem(bp); 13676 kfree(bp->ctx); 13677 bp->ctx = NULL; 13678 rtnl_unlock(); 13679 13680 /* Request a slot slot reset. */ 13681 return PCI_ERS_RESULT_NEED_RESET; 13682 } 13683 13684 /** 13685 * bnxt_io_slot_reset - called after the pci bus has been reset. 13686 * @pdev: Pointer to PCI device 13687 * 13688 * Restart the card from scratch, as if from a cold-boot. 13689 * At this point, the card has exprienced a hard reset, 13690 * followed by fixups by BIOS, and has its config space 13691 * set up identically to what it was at cold boot. 13692 */ 13693 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 13694 { 13695 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 13696 struct net_device *netdev = pci_get_drvdata(pdev); 13697 struct bnxt *bp = netdev_priv(netdev); 13698 int err = 0, off; 13699 13700 netdev_info(bp->dev, "PCI Slot Reset\n"); 13701 13702 rtnl_lock(); 13703 13704 if (pci_enable_device(pdev)) { 13705 dev_err(&pdev->dev, 13706 "Cannot re-enable PCI device after reset.\n"); 13707 } else { 13708 pci_set_master(pdev); 13709 /* Upon fatal error, our device internal logic that latches to 13710 * BAR value is getting reset and will restore only upon 13711 * rewritting the BARs. 13712 * 13713 * As pci_restore_state() does not re-write the BARs if the 13714 * value is same as saved value earlier, driver needs to 13715 * write the BARs to 0 to force restore, in case of fatal error. 13716 */ 13717 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 13718 &bp->state)) { 13719 for (off = PCI_BASE_ADDRESS_0; 13720 off <= PCI_BASE_ADDRESS_5; off += 4) 13721 pci_write_config_dword(bp->pdev, off, 0); 13722 } 13723 pci_restore_state(pdev); 13724 pci_save_state(pdev); 13725 13726 err = bnxt_hwrm_func_reset(bp); 13727 if (!err) 13728 result = PCI_ERS_RESULT_RECOVERED; 13729 } 13730 13731 rtnl_unlock(); 13732 13733 return result; 13734 } 13735 13736 /** 13737 * bnxt_io_resume - called when traffic can start flowing again. 13738 * @pdev: Pointer to PCI device 13739 * 13740 * This callback is called when the error recovery driver tells 13741 * us that its OK to resume normal operation. 13742 */ 13743 static void bnxt_io_resume(struct pci_dev *pdev) 13744 { 13745 struct net_device *netdev = pci_get_drvdata(pdev); 13746 struct bnxt *bp = netdev_priv(netdev); 13747 int err; 13748 13749 netdev_info(bp->dev, "PCI Slot Resume\n"); 13750 rtnl_lock(); 13751 13752 err = bnxt_hwrm_func_qcaps(bp); 13753 if (!err && netif_running(netdev)) 13754 err = bnxt_open(netdev); 13755 13756 bnxt_ulp_start(bp, err); 13757 if (!err) { 13758 bnxt_reenable_sriov(bp); 13759 netif_device_attach(netdev); 13760 } 13761 13762 rtnl_unlock(); 13763 } 13764 13765 static const struct pci_error_handlers bnxt_err_handler = { 13766 .error_detected = bnxt_io_error_detected, 13767 .slot_reset = bnxt_io_slot_reset, 13768 .resume = bnxt_io_resume 13769 }; 13770 13771 static struct pci_driver bnxt_pci_driver = { 13772 .name = DRV_MODULE_NAME, 13773 .id_table = bnxt_pci_tbl, 13774 .probe = bnxt_init_one, 13775 .remove = bnxt_remove_one, 13776 .shutdown = bnxt_shutdown, 13777 .driver.pm = BNXT_PM_OPS, 13778 .err_handler = &bnxt_err_handler, 13779 #if defined(CONFIG_BNXT_SRIOV) 13780 .sriov_configure = bnxt_sriov_configure, 13781 #endif 13782 }; 13783 13784 static int __init bnxt_init(void) 13785 { 13786 bnxt_debug_init(); 13787 return pci_register_driver(&bnxt_pci_driver); 13788 } 13789 13790 static void __exit bnxt_exit(void) 13791 { 13792 pci_unregister_driver(&bnxt_pci_driver); 13793 if (bnxt_pf_wq) 13794 destroy_workqueue(bnxt_pf_wq); 13795 bnxt_debug_exit(); 13796 } 13797 13798 module_init(bnxt_init); 13799 module_exit(bnxt_exit); 13800