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/bitmap.h> 52 #include <linux/cpu_rmap.h> 53 #include <linux/cpumask.h> 54 #include <net/pkt_cls.h> 55 #include <net/page_pool/helpers.h> 56 #include <linux/align.h> 57 #include <net/netdev_queues.h> 58 59 #include "bnxt_hsi.h" 60 #include "bnxt.h" 61 #include "bnxt_hwrm.h" 62 #include "bnxt_ulp.h" 63 #include "bnxt_sriov.h" 64 #include "bnxt_ethtool.h" 65 #include "bnxt_dcb.h" 66 #include "bnxt_xdp.h" 67 #include "bnxt_ptp.h" 68 #include "bnxt_vfr.h" 69 #include "bnxt_tc.h" 70 #include "bnxt_devlink.h" 71 #include "bnxt_debugfs.h" 72 #include "bnxt_coredump.h" 73 #include "bnxt_hwmon.h" 74 75 #define BNXT_TX_TIMEOUT (5 * HZ) 76 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 77 NETIF_MSG_TX_ERR) 78 79 MODULE_LICENSE("GPL"); 80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 81 82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 84 #define BNXT_RX_COPY_THRESH 256 85 86 #define BNXT_TX_PUSH_THRESH 164 87 88 /* indexed by enum board_idx */ 89 static const struct { 90 char *name; 91 } board_info[] = { 92 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 93 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 94 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 95 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 96 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 97 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 98 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 99 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 100 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 101 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 102 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 103 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 104 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 105 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 106 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 108 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 109 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 110 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 111 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 112 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 113 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 114 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 115 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 116 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 117 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 118 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 119 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 120 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 121 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 122 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 123 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 124 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 125 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 126 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 127 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 128 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 129 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 130 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 131 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 132 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 133 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 134 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 135 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 136 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 137 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 138 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 139 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 140 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 141 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 142 }; 143 144 static const struct pci_device_id bnxt_pci_tbl[] = { 145 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 146 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 147 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 148 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 149 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 150 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 151 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 152 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 153 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 154 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 155 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 156 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 157 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 158 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 159 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 160 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 163 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 164 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 165 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 166 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 167 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 168 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 169 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 170 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 171 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 172 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 173 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 175 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 179 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 180 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 181 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 182 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 183 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 184 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 185 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 186 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 187 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 188 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 189 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 190 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 191 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 193 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 194 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 195 #ifdef CONFIG_BNXT_SRIOV 196 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 197 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 198 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 199 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 200 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 201 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 202 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 203 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 206 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 207 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 208 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 209 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 210 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 211 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 212 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 215 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 216 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 217 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 218 #endif 219 { 0 } 220 }; 221 222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 223 224 static const u16 bnxt_vf_req_snif[] = { 225 HWRM_FUNC_CFG, 226 HWRM_FUNC_VF_CFG, 227 HWRM_PORT_PHY_QCFG, 228 HWRM_CFA_L2_FILTER_ALLOC, 229 }; 230 231 static const u16 bnxt_async_events_arr[] = { 232 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 233 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 234 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 235 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 236 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 237 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 238 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 239 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 240 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 241 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 242 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 243 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 244 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 245 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 246 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 247 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 248 }; 249 250 static struct workqueue_struct *bnxt_pf_wq; 251 252 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 253 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 254 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 255 256 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 257 .ports = { 258 .src = 0, 259 .dst = 0, 260 }, 261 .addrs = { 262 .v6addrs = { 263 .src = BNXT_IPV6_MASK_NONE, 264 .dst = BNXT_IPV6_MASK_NONE, 265 }, 266 }, 267 }; 268 269 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 270 .ports = { 271 .src = cpu_to_be16(0xffff), 272 .dst = cpu_to_be16(0xffff), 273 }, 274 .addrs = { 275 .v6addrs = { 276 .src = BNXT_IPV6_MASK_ALL, 277 .dst = BNXT_IPV6_MASK_ALL, 278 }, 279 }, 280 }; 281 282 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 283 .ports = { 284 .src = cpu_to_be16(0xffff), 285 .dst = cpu_to_be16(0xffff), 286 }, 287 .addrs = { 288 .v4addrs = { 289 .src = cpu_to_be32(0xffffffff), 290 .dst = cpu_to_be32(0xffffffff), 291 }, 292 }, 293 }; 294 295 static bool bnxt_vf_pciid(enum board_idx idx) 296 { 297 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 298 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 299 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 300 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF); 301 } 302 303 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 304 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 305 306 #define BNXT_DB_CQ(db, idx) \ 307 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 308 309 #define BNXT_DB_NQ_P5(db, idx) \ 310 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 311 (db)->doorbell) 312 313 #define BNXT_DB_NQ_P7(db, idx) \ 314 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 315 DB_RING_IDX(db, idx), (db)->doorbell) 316 317 #define BNXT_DB_CQ_ARM(db, idx) \ 318 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 319 320 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 321 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 322 DB_RING_IDX(db, idx), (db)->doorbell) 323 324 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 325 { 326 if (bp->flags & BNXT_FLAG_CHIP_P7) 327 BNXT_DB_NQ_P7(db, idx); 328 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 329 BNXT_DB_NQ_P5(db, idx); 330 else 331 BNXT_DB_CQ(db, idx); 332 } 333 334 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 335 { 336 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 337 BNXT_DB_NQ_ARM_P5(db, idx); 338 else 339 BNXT_DB_CQ_ARM(db, idx); 340 } 341 342 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 343 { 344 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 345 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 346 DB_RING_IDX(db, idx), db->doorbell); 347 else 348 BNXT_DB_CQ(db, idx); 349 } 350 351 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 352 { 353 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 354 return; 355 356 if (BNXT_PF(bp)) 357 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 358 else 359 schedule_delayed_work(&bp->fw_reset_task, delay); 360 } 361 362 static void __bnxt_queue_sp_work(struct bnxt *bp) 363 { 364 if (BNXT_PF(bp)) 365 queue_work(bnxt_pf_wq, &bp->sp_task); 366 else 367 schedule_work(&bp->sp_task); 368 } 369 370 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 371 { 372 set_bit(event, &bp->sp_event); 373 __bnxt_queue_sp_work(bp); 374 } 375 376 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 377 { 378 if (!rxr->bnapi->in_reset) { 379 rxr->bnapi->in_reset = true; 380 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 381 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 382 else 383 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 384 __bnxt_queue_sp_work(bp); 385 } 386 rxr->rx_next_cons = 0xffff; 387 } 388 389 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 390 u16 curr) 391 { 392 struct bnxt_napi *bnapi = txr->bnapi; 393 394 if (bnapi->tx_fault) 395 return; 396 397 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 398 txr->txq_index, txr->tx_hw_cons, 399 txr->tx_cons, txr->tx_prod, curr); 400 WARN_ON_ONCE(1); 401 bnapi->tx_fault = 1; 402 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 403 } 404 405 const u16 bnxt_lhint_arr[] = { 406 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 407 TX_BD_FLAGS_LHINT_512_TO_1023, 408 TX_BD_FLAGS_LHINT_1024_TO_2047, 409 TX_BD_FLAGS_LHINT_1024_TO_2047, 410 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 411 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 412 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 413 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 414 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 415 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 416 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 417 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 418 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 419 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 420 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 421 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 422 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 423 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 424 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 425 }; 426 427 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 428 { 429 struct metadata_dst *md_dst = skb_metadata_dst(skb); 430 431 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 432 return 0; 433 434 return md_dst->u.port_info.port_id; 435 } 436 437 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 438 u16 prod) 439 { 440 /* Sync BD data before updating doorbell */ 441 wmb(); 442 bnxt_db_write(bp, &txr->tx_db, prod); 443 txr->kick_pending = 0; 444 } 445 446 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 447 { 448 struct bnxt *bp = netdev_priv(dev); 449 struct tx_bd *txbd, *txbd0; 450 struct tx_bd_ext *txbd1; 451 struct netdev_queue *txq; 452 int i; 453 dma_addr_t mapping; 454 unsigned int length, pad = 0; 455 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 456 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 457 struct pci_dev *pdev = bp->pdev; 458 u16 prod, last_frag, txts_prod; 459 struct bnxt_tx_ring_info *txr; 460 struct bnxt_sw_tx_bd *tx_buf; 461 __le32 lflags = 0; 462 463 i = skb_get_queue_mapping(skb); 464 if (unlikely(i >= bp->tx_nr_rings)) { 465 dev_kfree_skb_any(skb); 466 dev_core_stats_tx_dropped_inc(dev); 467 return NETDEV_TX_OK; 468 } 469 470 txq = netdev_get_tx_queue(dev, i); 471 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 472 prod = txr->tx_prod; 473 474 free_size = bnxt_tx_avail(bp, txr); 475 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 476 /* We must have raced with NAPI cleanup */ 477 if (net_ratelimit() && txr->kick_pending) 478 netif_warn(bp, tx_err, dev, 479 "bnxt: ring busy w/ flush pending!\n"); 480 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 481 bp->tx_wake_thresh)) 482 return NETDEV_TX_BUSY; 483 } 484 485 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 486 goto tx_free; 487 488 length = skb->len; 489 len = skb_headlen(skb); 490 last_frag = skb_shinfo(skb)->nr_frags; 491 492 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 493 494 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 495 tx_buf->skb = skb; 496 tx_buf->nr_frags = last_frag; 497 498 vlan_tag_flags = 0; 499 cfa_action = bnxt_xmit_get_cfa_action(skb); 500 if (skb_vlan_tag_present(skb)) { 501 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 502 skb_vlan_tag_get(skb); 503 /* Currently supports 8021Q, 8021AD vlan offloads 504 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 505 */ 506 if (skb->vlan_proto == htons(ETH_P_8021Q)) 507 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 508 } 509 510 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 511 ptp->tx_tstamp_en) { 512 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 513 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 514 tx_buf->is_ts_pkt = 1; 515 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 516 } else if (!skb_is_gso(skb)) { 517 u16 seq_id, hdr_off; 518 519 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 520 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 521 if (vlan_tag_flags) 522 hdr_off += VLAN_HLEN; 523 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 524 tx_buf->is_ts_pkt = 1; 525 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 526 527 ptp->txts_req[txts_prod].tx_seqid = seq_id; 528 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 529 tx_buf->txts_prod = txts_prod; 530 } 531 } 532 } 533 if (unlikely(skb->no_fcs)) 534 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 535 536 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 537 !lflags) { 538 struct tx_push_buffer *tx_push_buf = txr->tx_push; 539 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 540 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 541 void __iomem *db = txr->tx_db.doorbell; 542 void *pdata = tx_push_buf->data; 543 u64 *end; 544 int j, push_len; 545 546 /* Set COAL_NOW to be ready quickly for the next push */ 547 tx_push->tx_bd_len_flags_type = 548 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 549 TX_BD_TYPE_LONG_TX_BD | 550 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 551 TX_BD_FLAGS_COAL_NOW | 552 TX_BD_FLAGS_PACKET_END | 553 (2 << TX_BD_FLAGS_BD_CNT_SHIFT)); 554 555 if (skb->ip_summed == CHECKSUM_PARTIAL) 556 tx_push1->tx_bd_hsize_lflags = 557 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 558 else 559 tx_push1->tx_bd_hsize_lflags = 0; 560 561 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 562 tx_push1->tx_bd_cfa_action = 563 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 564 565 end = pdata + length; 566 end = PTR_ALIGN(end, 8) - 1; 567 *end = 0; 568 569 skb_copy_from_linear_data(skb, pdata, len); 570 pdata += len; 571 for (j = 0; j < last_frag; j++) { 572 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 573 void *fptr; 574 575 fptr = skb_frag_address_safe(frag); 576 if (!fptr) 577 goto normal_tx; 578 579 memcpy(pdata, fptr, skb_frag_size(frag)); 580 pdata += skb_frag_size(frag); 581 } 582 583 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 584 txbd->tx_bd_haddr = txr->data_mapping; 585 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 586 prod = NEXT_TX(prod); 587 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 588 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 589 memcpy(txbd, tx_push1, sizeof(*txbd)); 590 prod = NEXT_TX(prod); 591 tx_push->doorbell = 592 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 593 DB_RING_IDX(&txr->tx_db, prod)); 594 WRITE_ONCE(txr->tx_prod, prod); 595 596 tx_buf->is_push = 1; 597 netdev_tx_sent_queue(txq, skb->len); 598 wmb(); /* Sync is_push and byte queue before pushing data */ 599 600 push_len = (length + sizeof(*tx_push) + 7) / 8; 601 if (push_len > 16) { 602 __iowrite64_copy(db, tx_push_buf, 16); 603 __iowrite32_copy(db + 4, tx_push_buf + 1, 604 (push_len - 16) << 1); 605 } else { 606 __iowrite64_copy(db, tx_push_buf, push_len); 607 } 608 609 goto tx_done; 610 } 611 612 normal_tx: 613 if (length < BNXT_MIN_PKT_SIZE) { 614 pad = BNXT_MIN_PKT_SIZE - length; 615 if (skb_pad(skb, pad)) 616 /* SKB already freed. */ 617 goto tx_kick_pending; 618 length = BNXT_MIN_PKT_SIZE; 619 } 620 621 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 622 623 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 624 goto tx_free; 625 626 dma_unmap_addr_set(tx_buf, mapping, mapping); 627 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 628 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT); 629 630 txbd->tx_bd_haddr = cpu_to_le64(mapping); 631 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 632 633 prod = NEXT_TX(prod); 634 txbd1 = (struct tx_bd_ext *) 635 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 636 637 txbd1->tx_bd_hsize_lflags = lflags; 638 if (skb_is_gso(skb)) { 639 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 640 u32 hdr_len; 641 642 if (skb->encapsulation) { 643 if (udp_gso) 644 hdr_len = skb_inner_transport_offset(skb) + 645 sizeof(struct udphdr); 646 else 647 hdr_len = skb_inner_tcp_all_headers(skb); 648 } else if (udp_gso) { 649 hdr_len = skb_transport_offset(skb) + 650 sizeof(struct udphdr); 651 } else { 652 hdr_len = skb_tcp_all_headers(skb); 653 } 654 655 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 656 TX_BD_FLAGS_T_IPID | 657 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 658 length = skb_shinfo(skb)->gso_size; 659 txbd1->tx_bd_mss = cpu_to_le32(length); 660 length += hdr_len; 661 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 662 txbd1->tx_bd_hsize_lflags |= 663 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 664 txbd1->tx_bd_mss = 0; 665 } 666 667 length >>= 9; 668 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 669 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 670 skb->len); 671 i = 0; 672 goto tx_dma_error; 673 } 674 flags |= bnxt_lhint_arr[length]; 675 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 676 677 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 678 txbd1->tx_bd_cfa_action = 679 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 680 txbd0 = txbd; 681 for (i = 0; i < last_frag; i++) { 682 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 683 684 prod = NEXT_TX(prod); 685 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 686 687 len = skb_frag_size(frag); 688 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 689 DMA_TO_DEVICE); 690 691 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 692 goto tx_dma_error; 693 694 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 695 dma_unmap_addr_set(tx_buf, mapping, mapping); 696 697 txbd->tx_bd_haddr = cpu_to_le64(mapping); 698 699 flags = len << TX_BD_LEN_SHIFT; 700 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 701 } 702 703 flags &= ~TX_BD_LEN; 704 txbd->tx_bd_len_flags_type = 705 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 706 TX_BD_FLAGS_PACKET_END); 707 708 netdev_tx_sent_queue(txq, skb->len); 709 710 skb_tx_timestamp(skb); 711 712 prod = NEXT_TX(prod); 713 WRITE_ONCE(txr->tx_prod, prod); 714 715 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 716 bnxt_txr_db_kick(bp, txr, prod); 717 } else { 718 if (free_size >= bp->tx_wake_thresh) 719 txbd0->tx_bd_len_flags_type |= 720 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 721 txr->kick_pending = 1; 722 } 723 724 tx_done: 725 726 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 727 if (netdev_xmit_more() && !tx_buf->is_push) { 728 txbd0->tx_bd_len_flags_type &= 729 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 730 bnxt_txr_db_kick(bp, txr, prod); 731 } 732 733 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 734 bp->tx_wake_thresh); 735 } 736 return NETDEV_TX_OK; 737 738 tx_dma_error: 739 last_frag = i; 740 741 /* start back at beginning and unmap skb */ 742 prod = txr->tx_prod; 743 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 744 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 745 skb_headlen(skb), DMA_TO_DEVICE); 746 prod = NEXT_TX(prod); 747 748 /* unmap remaining mapped pages */ 749 for (i = 0; i < last_frag; i++) { 750 prod = NEXT_TX(prod); 751 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 752 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 753 skb_frag_size(&skb_shinfo(skb)->frags[i]), 754 DMA_TO_DEVICE); 755 } 756 757 tx_free: 758 dev_kfree_skb_any(skb); 759 tx_kick_pending: 760 if (BNXT_TX_PTP_IS_SET(lflags)) { 761 txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0; 762 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 763 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 764 /* set SKB to err so PTP worker will clean up */ 765 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 766 } 767 if (txr->kick_pending) 768 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 769 txr->tx_buf_ring[txr->tx_prod].skb = NULL; 770 dev_core_stats_tx_dropped_inc(dev); 771 return NETDEV_TX_OK; 772 } 773 774 /* Returns true if some remaining TX packets not processed. */ 775 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 776 int budget) 777 { 778 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 779 struct pci_dev *pdev = bp->pdev; 780 u16 hw_cons = txr->tx_hw_cons; 781 unsigned int tx_bytes = 0; 782 u16 cons = txr->tx_cons; 783 int tx_pkts = 0; 784 bool rc = false; 785 786 while (RING_TX(bp, cons) != hw_cons) { 787 struct bnxt_sw_tx_bd *tx_buf; 788 struct sk_buff *skb; 789 bool is_ts_pkt; 790 int j, last; 791 792 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 793 skb = tx_buf->skb; 794 795 if (unlikely(!skb)) { 796 bnxt_sched_reset_txr(bp, txr, cons); 797 return rc; 798 } 799 800 is_ts_pkt = tx_buf->is_ts_pkt; 801 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 802 rc = true; 803 break; 804 } 805 806 cons = NEXT_TX(cons); 807 tx_pkts++; 808 tx_bytes += skb->len; 809 tx_buf->skb = NULL; 810 tx_buf->is_ts_pkt = 0; 811 812 if (tx_buf->is_push) { 813 tx_buf->is_push = 0; 814 goto next_tx_int; 815 } 816 817 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 818 skb_headlen(skb), DMA_TO_DEVICE); 819 last = tx_buf->nr_frags; 820 821 for (j = 0; j < last; j++) { 822 cons = NEXT_TX(cons); 823 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 824 dma_unmap_page( 825 &pdev->dev, 826 dma_unmap_addr(tx_buf, mapping), 827 skb_frag_size(&skb_shinfo(skb)->frags[j]), 828 DMA_TO_DEVICE); 829 } 830 if (unlikely(is_ts_pkt)) { 831 if (BNXT_CHIP_P5(bp)) { 832 /* PTP worker takes ownership of the skb */ 833 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 834 skb = NULL; 835 } 836 } 837 838 next_tx_int: 839 cons = NEXT_TX(cons); 840 841 dev_consume_skb_any(skb); 842 } 843 844 WRITE_ONCE(txr->tx_cons, cons); 845 846 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 847 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 848 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 849 850 return rc; 851 } 852 853 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 854 { 855 struct bnxt_tx_ring_info *txr; 856 bool more = false; 857 int i; 858 859 bnxt_for_each_napi_tx(i, bnapi, txr) { 860 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 861 more |= __bnxt_tx_int(bp, txr, budget); 862 } 863 if (!more) 864 bnapi->events &= ~BNXT_TX_CMP_EVENT; 865 } 866 867 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 868 struct bnxt_rx_ring_info *rxr, 869 unsigned int *offset, 870 gfp_t gfp) 871 { 872 struct page *page; 873 874 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 875 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 876 BNXT_RX_PAGE_SIZE); 877 } else { 878 page = page_pool_dev_alloc_pages(rxr->page_pool); 879 *offset = 0; 880 } 881 if (!page) 882 return NULL; 883 884 *mapping = page_pool_get_dma_addr(page) + *offset; 885 return page; 886 } 887 888 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 889 gfp_t gfp) 890 { 891 u8 *data; 892 struct pci_dev *pdev = bp->pdev; 893 894 if (gfp == GFP_ATOMIC) 895 data = napi_alloc_frag(bp->rx_buf_size); 896 else 897 data = netdev_alloc_frag(bp->rx_buf_size); 898 if (!data) 899 return NULL; 900 901 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset, 902 bp->rx_buf_use_size, bp->rx_dir, 903 DMA_ATTR_WEAK_ORDERING); 904 905 if (dma_mapping_error(&pdev->dev, *mapping)) { 906 skb_free_frag(data); 907 data = NULL; 908 } 909 return data; 910 } 911 912 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 913 u16 prod, gfp_t gfp) 914 { 915 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 916 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 917 dma_addr_t mapping; 918 919 if (BNXT_RX_PAGE_MODE(bp)) { 920 unsigned int offset; 921 struct page *page = 922 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 923 924 if (!page) 925 return -ENOMEM; 926 927 mapping += bp->rx_dma_offset; 928 rx_buf->data = page; 929 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 930 } else { 931 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp); 932 933 if (!data) 934 return -ENOMEM; 935 936 rx_buf->data = data; 937 rx_buf->data_ptr = data + bp->rx_offset; 938 } 939 rx_buf->mapping = mapping; 940 941 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 942 return 0; 943 } 944 945 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 946 { 947 u16 prod = rxr->rx_prod; 948 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 949 struct bnxt *bp = rxr->bnapi->bp; 950 struct rx_bd *cons_bd, *prod_bd; 951 952 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 953 cons_rx_buf = &rxr->rx_buf_ring[cons]; 954 955 prod_rx_buf->data = data; 956 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 957 958 prod_rx_buf->mapping = cons_rx_buf->mapping; 959 960 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 961 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 962 963 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 964 } 965 966 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 967 { 968 u16 next, max = rxr->rx_agg_bmap_size; 969 970 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 971 if (next >= max) 972 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 973 return next; 974 } 975 976 static inline int bnxt_alloc_rx_page(struct bnxt *bp, 977 struct bnxt_rx_ring_info *rxr, 978 u16 prod, gfp_t gfp) 979 { 980 struct rx_bd *rxbd = 981 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 982 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 983 struct page *page; 984 dma_addr_t mapping; 985 u16 sw_prod = rxr->rx_sw_agg_prod; 986 unsigned int offset = 0; 987 988 page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 989 990 if (!page) 991 return -ENOMEM; 992 993 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 994 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 995 996 __set_bit(sw_prod, rxr->rx_agg_bmap); 997 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 998 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 999 1000 rx_agg_buf->page = page; 1001 rx_agg_buf->offset = offset; 1002 rx_agg_buf->mapping = mapping; 1003 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1004 rxbd->rx_bd_opaque = sw_prod; 1005 return 0; 1006 } 1007 1008 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1009 struct bnxt_cp_ring_info *cpr, 1010 u16 cp_cons, u16 curr) 1011 { 1012 struct rx_agg_cmp *agg; 1013 1014 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1015 agg = (struct rx_agg_cmp *) 1016 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1017 return agg; 1018 } 1019 1020 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1021 struct bnxt_rx_ring_info *rxr, 1022 u16 agg_id, u16 curr) 1023 { 1024 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1025 1026 return &tpa_info->agg_arr[curr]; 1027 } 1028 1029 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1030 u16 start, u32 agg_bufs, bool tpa) 1031 { 1032 struct bnxt_napi *bnapi = cpr->bnapi; 1033 struct bnxt *bp = bnapi->bp; 1034 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1035 u16 prod = rxr->rx_agg_prod; 1036 u16 sw_prod = rxr->rx_sw_agg_prod; 1037 bool p5_tpa = false; 1038 u32 i; 1039 1040 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1041 p5_tpa = true; 1042 1043 for (i = 0; i < agg_bufs; i++) { 1044 u16 cons; 1045 struct rx_agg_cmp *agg; 1046 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1047 struct rx_bd *prod_bd; 1048 struct page *page; 1049 1050 if (p5_tpa) 1051 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1052 else 1053 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1054 cons = agg->rx_agg_cmp_opaque; 1055 __clear_bit(cons, rxr->rx_agg_bmap); 1056 1057 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1058 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1059 1060 __set_bit(sw_prod, rxr->rx_agg_bmap); 1061 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1062 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1063 1064 /* It is possible for sw_prod to be equal to cons, so 1065 * set cons_rx_buf->page to NULL first. 1066 */ 1067 page = cons_rx_buf->page; 1068 cons_rx_buf->page = NULL; 1069 prod_rx_buf->page = page; 1070 prod_rx_buf->offset = cons_rx_buf->offset; 1071 1072 prod_rx_buf->mapping = cons_rx_buf->mapping; 1073 1074 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1075 1076 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1077 prod_bd->rx_bd_opaque = sw_prod; 1078 1079 prod = NEXT_RX_AGG(prod); 1080 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1081 } 1082 rxr->rx_agg_prod = prod; 1083 rxr->rx_sw_agg_prod = sw_prod; 1084 } 1085 1086 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1087 struct bnxt_rx_ring_info *rxr, 1088 u16 cons, void *data, u8 *data_ptr, 1089 dma_addr_t dma_addr, 1090 unsigned int offset_and_len) 1091 { 1092 unsigned int len = offset_and_len & 0xffff; 1093 struct page *page = data; 1094 u16 prod = rxr->rx_prod; 1095 struct sk_buff *skb; 1096 int err; 1097 1098 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1099 if (unlikely(err)) { 1100 bnxt_reuse_rx_data(rxr, cons, data); 1101 return NULL; 1102 } 1103 dma_addr -= bp->rx_dma_offset; 1104 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1105 bp->rx_dir); 1106 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1107 if (!skb) { 1108 page_pool_recycle_direct(rxr->page_pool, page); 1109 return NULL; 1110 } 1111 skb_mark_for_recycle(skb); 1112 skb_reserve(skb, bp->rx_offset); 1113 __skb_put(skb, len); 1114 1115 return skb; 1116 } 1117 1118 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1119 struct bnxt_rx_ring_info *rxr, 1120 u16 cons, void *data, u8 *data_ptr, 1121 dma_addr_t dma_addr, 1122 unsigned int offset_and_len) 1123 { 1124 unsigned int payload = offset_and_len >> 16; 1125 unsigned int len = offset_and_len & 0xffff; 1126 skb_frag_t *frag; 1127 struct page *page = data; 1128 u16 prod = rxr->rx_prod; 1129 struct sk_buff *skb; 1130 int off, err; 1131 1132 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1133 if (unlikely(err)) { 1134 bnxt_reuse_rx_data(rxr, cons, data); 1135 return NULL; 1136 } 1137 dma_addr -= bp->rx_dma_offset; 1138 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1139 bp->rx_dir); 1140 1141 if (unlikely(!payload)) 1142 payload = eth_get_headlen(bp->dev, data_ptr, len); 1143 1144 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1145 if (!skb) { 1146 page_pool_recycle_direct(rxr->page_pool, page); 1147 return NULL; 1148 } 1149 1150 skb_mark_for_recycle(skb); 1151 off = (void *)data_ptr - page_address(page); 1152 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1153 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1154 payload + NET_IP_ALIGN); 1155 1156 frag = &skb_shinfo(skb)->frags[0]; 1157 skb_frag_size_sub(frag, payload); 1158 skb_frag_off_add(frag, payload); 1159 skb->data_len -= payload; 1160 skb->tail += payload; 1161 1162 return skb; 1163 } 1164 1165 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1166 struct bnxt_rx_ring_info *rxr, u16 cons, 1167 void *data, u8 *data_ptr, 1168 dma_addr_t dma_addr, 1169 unsigned int offset_and_len) 1170 { 1171 u16 prod = rxr->rx_prod; 1172 struct sk_buff *skb; 1173 int err; 1174 1175 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1176 if (unlikely(err)) { 1177 bnxt_reuse_rx_data(rxr, cons, data); 1178 return NULL; 1179 } 1180 1181 skb = napi_build_skb(data, bp->rx_buf_size); 1182 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1183 bp->rx_dir, DMA_ATTR_WEAK_ORDERING); 1184 if (!skb) { 1185 skb_free_frag(data); 1186 return NULL; 1187 } 1188 1189 skb_reserve(skb, bp->rx_offset); 1190 skb_put(skb, offset_and_len & 0xffff); 1191 return skb; 1192 } 1193 1194 static u32 __bnxt_rx_agg_pages(struct bnxt *bp, 1195 struct bnxt_cp_ring_info *cpr, 1196 struct skb_shared_info *shinfo, 1197 u16 idx, u32 agg_bufs, bool tpa, 1198 struct xdp_buff *xdp) 1199 { 1200 struct bnxt_napi *bnapi = cpr->bnapi; 1201 struct pci_dev *pdev = bp->pdev; 1202 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1203 u16 prod = rxr->rx_agg_prod; 1204 u32 i, total_frag_len = 0; 1205 bool p5_tpa = false; 1206 1207 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1208 p5_tpa = true; 1209 1210 for (i = 0; i < agg_bufs; i++) { 1211 skb_frag_t *frag = &shinfo->frags[i]; 1212 u16 cons, frag_len; 1213 struct rx_agg_cmp *agg; 1214 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1215 struct page *page; 1216 dma_addr_t mapping; 1217 1218 if (p5_tpa) 1219 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1220 else 1221 agg = bnxt_get_agg(bp, cpr, idx, i); 1222 cons = agg->rx_agg_cmp_opaque; 1223 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1224 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1225 1226 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1227 skb_frag_fill_page_desc(frag, cons_rx_buf->page, 1228 cons_rx_buf->offset, frag_len); 1229 shinfo->nr_frags = i + 1; 1230 __clear_bit(cons, rxr->rx_agg_bmap); 1231 1232 /* It is possible for bnxt_alloc_rx_page() to allocate 1233 * a sw_prod index that equals the cons index, so we 1234 * need to clear the cons entry now. 1235 */ 1236 mapping = cons_rx_buf->mapping; 1237 page = cons_rx_buf->page; 1238 cons_rx_buf->page = NULL; 1239 1240 if (xdp && page_is_pfmemalloc(page)) 1241 xdp_buff_set_frag_pfmemalloc(xdp); 1242 1243 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) { 1244 --shinfo->nr_frags; 1245 cons_rx_buf->page = page; 1246 1247 /* Update prod since possibly some pages have been 1248 * allocated already. 1249 */ 1250 rxr->rx_agg_prod = prod; 1251 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1252 return 0; 1253 } 1254 1255 dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE, 1256 bp->rx_dir); 1257 1258 total_frag_len += frag_len; 1259 prod = NEXT_RX_AGG(prod); 1260 } 1261 rxr->rx_agg_prod = prod; 1262 return total_frag_len; 1263 } 1264 1265 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp, 1266 struct bnxt_cp_ring_info *cpr, 1267 struct sk_buff *skb, u16 idx, 1268 u32 agg_bufs, bool tpa) 1269 { 1270 struct skb_shared_info *shinfo = skb_shinfo(skb); 1271 u32 total_frag_len = 0; 1272 1273 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx, 1274 agg_bufs, tpa, NULL); 1275 if (!total_frag_len) { 1276 skb_mark_for_recycle(skb); 1277 dev_kfree_skb(skb); 1278 return NULL; 1279 } 1280 1281 skb->data_len += total_frag_len; 1282 skb->len += total_frag_len; 1283 skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs; 1284 return skb; 1285 } 1286 1287 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp, 1288 struct bnxt_cp_ring_info *cpr, 1289 struct xdp_buff *xdp, u16 idx, 1290 u32 agg_bufs, bool tpa) 1291 { 1292 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1293 u32 total_frag_len = 0; 1294 1295 if (!xdp_buff_has_frags(xdp)) 1296 shinfo->nr_frags = 0; 1297 1298 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, 1299 idx, agg_bufs, tpa, xdp); 1300 if (total_frag_len) { 1301 xdp_buff_set_frags_flag(xdp); 1302 shinfo->nr_frags = agg_bufs; 1303 shinfo->xdp_frags_size = total_frag_len; 1304 } 1305 return total_frag_len; 1306 } 1307 1308 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1309 u8 agg_bufs, u32 *raw_cons) 1310 { 1311 u16 last; 1312 struct rx_agg_cmp *agg; 1313 1314 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1315 last = RING_CMP(*raw_cons); 1316 agg = (struct rx_agg_cmp *) 1317 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1318 return RX_AGG_CMP_VALID(agg, *raw_cons); 1319 } 1320 1321 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1322 unsigned int len, 1323 dma_addr_t mapping) 1324 { 1325 struct bnxt *bp = bnapi->bp; 1326 struct pci_dev *pdev = bp->pdev; 1327 struct sk_buff *skb; 1328 1329 skb = napi_alloc_skb(&bnapi->napi, len); 1330 if (!skb) 1331 return NULL; 1332 1333 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh, 1334 bp->rx_dir); 1335 1336 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1337 len + NET_IP_ALIGN); 1338 1339 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh, 1340 bp->rx_dir); 1341 1342 skb_put(skb, len); 1343 1344 return skb; 1345 } 1346 1347 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1348 unsigned int len, 1349 dma_addr_t mapping) 1350 { 1351 return bnxt_copy_data(bnapi, data, len, mapping); 1352 } 1353 1354 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1355 struct xdp_buff *xdp, 1356 unsigned int len, 1357 dma_addr_t mapping) 1358 { 1359 unsigned int metasize = 0; 1360 u8 *data = xdp->data; 1361 struct sk_buff *skb; 1362 1363 len = xdp->data_end - xdp->data_meta; 1364 metasize = xdp->data - xdp->data_meta; 1365 data = xdp->data_meta; 1366 1367 skb = bnxt_copy_data(bnapi, data, len, mapping); 1368 if (!skb) 1369 return skb; 1370 1371 if (metasize) { 1372 skb_metadata_set(skb, metasize); 1373 __skb_pull(skb, metasize); 1374 } 1375 1376 return skb; 1377 } 1378 1379 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1380 u32 *raw_cons, void *cmp) 1381 { 1382 struct rx_cmp *rxcmp = cmp; 1383 u32 tmp_raw_cons = *raw_cons; 1384 u8 cmp_type, agg_bufs = 0; 1385 1386 cmp_type = RX_CMP_TYPE(rxcmp); 1387 1388 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1389 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1390 RX_CMP_AGG_BUFS) >> 1391 RX_CMP_AGG_BUFS_SHIFT; 1392 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1393 struct rx_tpa_end_cmp *tpa_end = cmp; 1394 1395 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1396 return 0; 1397 1398 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1399 } 1400 1401 if (agg_bufs) { 1402 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1403 return -EBUSY; 1404 } 1405 *raw_cons = tmp_raw_cons; 1406 return 0; 1407 } 1408 1409 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1410 { 1411 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1412 u16 idx = agg_id & MAX_TPA_P5_MASK; 1413 1414 if (test_bit(idx, map->agg_idx_bmap)) 1415 idx = find_first_zero_bit(map->agg_idx_bmap, 1416 BNXT_AGG_IDX_BMAP_SIZE); 1417 __set_bit(idx, map->agg_idx_bmap); 1418 map->agg_id_tbl[agg_id] = idx; 1419 return idx; 1420 } 1421 1422 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1423 { 1424 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1425 1426 __clear_bit(idx, map->agg_idx_bmap); 1427 } 1428 1429 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1430 { 1431 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1432 1433 return map->agg_id_tbl[agg_id]; 1434 } 1435 1436 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1437 struct rx_tpa_start_cmp *tpa_start, 1438 struct rx_tpa_start_cmp_ext *tpa_start1) 1439 { 1440 tpa_info->cfa_code_valid = 1; 1441 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1442 tpa_info->vlan_valid = 0; 1443 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1444 tpa_info->vlan_valid = 1; 1445 tpa_info->metadata = 1446 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1447 } 1448 } 1449 1450 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1451 struct rx_tpa_start_cmp *tpa_start, 1452 struct rx_tpa_start_cmp_ext *tpa_start1) 1453 { 1454 tpa_info->vlan_valid = 0; 1455 if (TPA_START_VLAN_VALID(tpa_start)) { 1456 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1457 u32 vlan_proto = ETH_P_8021Q; 1458 1459 tpa_info->vlan_valid = 1; 1460 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1461 vlan_proto = ETH_P_8021AD; 1462 tpa_info->metadata = vlan_proto << 16 | 1463 TPA_START_METADATA0_TCI(tpa_start1); 1464 } 1465 } 1466 1467 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1468 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1469 struct rx_tpa_start_cmp_ext *tpa_start1) 1470 { 1471 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1472 struct bnxt_tpa_info *tpa_info; 1473 u16 cons, prod, agg_id; 1474 struct rx_bd *prod_bd; 1475 dma_addr_t mapping; 1476 1477 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1478 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1479 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1480 } else { 1481 agg_id = TPA_START_AGG_ID(tpa_start); 1482 } 1483 cons = tpa_start->rx_tpa_start_cmp_opaque; 1484 prod = rxr->rx_prod; 1485 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1486 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1487 tpa_info = &rxr->rx_tpa[agg_id]; 1488 1489 if (unlikely(cons != rxr->rx_next_cons || 1490 TPA_START_ERROR(tpa_start))) { 1491 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1492 cons, rxr->rx_next_cons, 1493 TPA_START_ERROR_CODE(tpa_start1)); 1494 bnxt_sched_reset_rxr(bp, rxr); 1495 return; 1496 } 1497 prod_rx_buf->data = tpa_info->data; 1498 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1499 1500 mapping = tpa_info->mapping; 1501 prod_rx_buf->mapping = mapping; 1502 1503 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1504 1505 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1506 1507 tpa_info->data = cons_rx_buf->data; 1508 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1509 cons_rx_buf->data = NULL; 1510 tpa_info->mapping = cons_rx_buf->mapping; 1511 1512 tpa_info->len = 1513 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1514 RX_TPA_START_CMP_LEN_SHIFT; 1515 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1516 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1517 tpa_info->gso_type = SKB_GSO_TCPV4; 1518 if (TPA_START_IS_IPV6(tpa_start1)) 1519 tpa_info->gso_type = SKB_GSO_TCPV6; 1520 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1521 else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP && 1522 TPA_START_HASH_TYPE(tpa_start) == 3) 1523 tpa_info->gso_type = SKB_GSO_TCPV6; 1524 tpa_info->rss_hash = 1525 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1526 } else { 1527 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1528 tpa_info->gso_type = 0; 1529 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1530 } 1531 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1532 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1533 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1534 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1535 else 1536 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1537 tpa_info->agg_count = 0; 1538 1539 rxr->rx_prod = NEXT_RX(prod); 1540 cons = RING_RX(bp, NEXT_RX(cons)); 1541 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1542 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1543 1544 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1545 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1546 cons_rx_buf->data = NULL; 1547 } 1548 1549 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1550 { 1551 if (agg_bufs) 1552 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1553 } 1554 1555 #ifdef CONFIG_INET 1556 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1557 { 1558 struct udphdr *uh = NULL; 1559 1560 if (ip_proto == htons(ETH_P_IP)) { 1561 struct iphdr *iph = (struct iphdr *)skb->data; 1562 1563 if (iph->protocol == IPPROTO_UDP) 1564 uh = (struct udphdr *)(iph + 1); 1565 } else { 1566 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1567 1568 if (iph->nexthdr == IPPROTO_UDP) 1569 uh = (struct udphdr *)(iph + 1); 1570 } 1571 if (uh) { 1572 if (uh->check) 1573 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1574 else 1575 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1576 } 1577 } 1578 #endif 1579 1580 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1581 int payload_off, int tcp_ts, 1582 struct sk_buff *skb) 1583 { 1584 #ifdef CONFIG_INET 1585 struct tcphdr *th; 1586 int len, nw_off; 1587 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1588 u32 hdr_info = tpa_info->hdr_info; 1589 bool loopback = false; 1590 1591 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1592 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1593 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1594 1595 /* If the packet is an internal loopback packet, the offsets will 1596 * have an extra 4 bytes. 1597 */ 1598 if (inner_mac_off == 4) { 1599 loopback = true; 1600 } else if (inner_mac_off > 4) { 1601 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1602 ETH_HLEN - 2)); 1603 1604 /* We only support inner iPv4/ipv6. If we don't see the 1605 * correct protocol ID, it must be a loopback packet where 1606 * the offsets are off by 4. 1607 */ 1608 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1609 loopback = true; 1610 } 1611 if (loopback) { 1612 /* internal loopback packet, subtract all offsets by 4 */ 1613 inner_ip_off -= 4; 1614 inner_mac_off -= 4; 1615 outer_ip_off -= 4; 1616 } 1617 1618 nw_off = inner_ip_off - ETH_HLEN; 1619 skb_set_network_header(skb, nw_off); 1620 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1621 struct ipv6hdr *iph = ipv6_hdr(skb); 1622 1623 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1624 len = skb->len - skb_transport_offset(skb); 1625 th = tcp_hdr(skb); 1626 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1627 } else { 1628 struct iphdr *iph = ip_hdr(skb); 1629 1630 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1631 len = skb->len - skb_transport_offset(skb); 1632 th = tcp_hdr(skb); 1633 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1634 } 1635 1636 if (inner_mac_off) { /* tunnel */ 1637 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1638 ETH_HLEN - 2)); 1639 1640 bnxt_gro_tunnel(skb, proto); 1641 } 1642 #endif 1643 return skb; 1644 } 1645 1646 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1647 int payload_off, int tcp_ts, 1648 struct sk_buff *skb) 1649 { 1650 #ifdef CONFIG_INET 1651 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1652 u32 hdr_info = tpa_info->hdr_info; 1653 int iphdr_len, nw_off; 1654 1655 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1656 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1657 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1658 1659 nw_off = inner_ip_off - ETH_HLEN; 1660 skb_set_network_header(skb, nw_off); 1661 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1662 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1663 skb_set_transport_header(skb, nw_off + iphdr_len); 1664 1665 if (inner_mac_off) { /* tunnel */ 1666 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1667 ETH_HLEN - 2)); 1668 1669 bnxt_gro_tunnel(skb, proto); 1670 } 1671 #endif 1672 return skb; 1673 } 1674 1675 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1676 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1677 1678 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1679 int payload_off, int tcp_ts, 1680 struct sk_buff *skb) 1681 { 1682 #ifdef CONFIG_INET 1683 struct tcphdr *th; 1684 int len, nw_off, tcp_opt_len = 0; 1685 1686 if (tcp_ts) 1687 tcp_opt_len = 12; 1688 1689 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1690 struct iphdr *iph; 1691 1692 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1693 ETH_HLEN; 1694 skb_set_network_header(skb, nw_off); 1695 iph = ip_hdr(skb); 1696 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1697 len = skb->len - skb_transport_offset(skb); 1698 th = tcp_hdr(skb); 1699 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1700 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1701 struct ipv6hdr *iph; 1702 1703 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1704 ETH_HLEN; 1705 skb_set_network_header(skb, nw_off); 1706 iph = ipv6_hdr(skb); 1707 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1708 len = skb->len - skb_transport_offset(skb); 1709 th = tcp_hdr(skb); 1710 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1711 } else { 1712 dev_kfree_skb_any(skb); 1713 return NULL; 1714 } 1715 1716 if (nw_off) /* tunnel */ 1717 bnxt_gro_tunnel(skb, skb->protocol); 1718 #endif 1719 return skb; 1720 } 1721 1722 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1723 struct bnxt_tpa_info *tpa_info, 1724 struct rx_tpa_end_cmp *tpa_end, 1725 struct rx_tpa_end_cmp_ext *tpa_end1, 1726 struct sk_buff *skb) 1727 { 1728 #ifdef CONFIG_INET 1729 int payload_off; 1730 u16 segs; 1731 1732 segs = TPA_END_TPA_SEGS(tpa_end); 1733 if (segs == 1) 1734 return skb; 1735 1736 NAPI_GRO_CB(skb)->count = segs; 1737 skb_shinfo(skb)->gso_size = 1738 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1739 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1740 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1741 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1742 else 1743 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1744 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1745 if (likely(skb)) 1746 tcp_gro_complete(skb); 1747 #endif 1748 return skb; 1749 } 1750 1751 /* Given the cfa_code of a received packet determine which 1752 * netdev (vf-rep or PF) the packet is destined to. 1753 */ 1754 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1755 { 1756 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1757 1758 /* if vf-rep dev is NULL, the must belongs to the PF */ 1759 return dev ? dev : bp->dev; 1760 } 1761 1762 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1763 struct bnxt_cp_ring_info *cpr, 1764 u32 *raw_cons, 1765 struct rx_tpa_end_cmp *tpa_end, 1766 struct rx_tpa_end_cmp_ext *tpa_end1, 1767 u8 *event) 1768 { 1769 struct bnxt_napi *bnapi = cpr->bnapi; 1770 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1771 struct net_device *dev = bp->dev; 1772 u8 *data_ptr, agg_bufs; 1773 unsigned int len; 1774 struct bnxt_tpa_info *tpa_info; 1775 dma_addr_t mapping; 1776 struct sk_buff *skb; 1777 u16 idx = 0, agg_id; 1778 void *data; 1779 bool gro; 1780 1781 if (unlikely(bnapi->in_reset)) { 1782 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1783 1784 if (rc < 0) 1785 return ERR_PTR(-EBUSY); 1786 return NULL; 1787 } 1788 1789 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1790 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1791 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1792 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1793 tpa_info = &rxr->rx_tpa[agg_id]; 1794 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1795 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1796 agg_bufs, tpa_info->agg_count); 1797 agg_bufs = tpa_info->agg_count; 1798 } 1799 tpa_info->agg_count = 0; 1800 *event |= BNXT_AGG_EVENT; 1801 bnxt_free_agg_idx(rxr, agg_id); 1802 idx = agg_id; 1803 gro = !!(bp->flags & BNXT_FLAG_GRO); 1804 } else { 1805 agg_id = TPA_END_AGG_ID(tpa_end); 1806 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1807 tpa_info = &rxr->rx_tpa[agg_id]; 1808 idx = RING_CMP(*raw_cons); 1809 if (agg_bufs) { 1810 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1811 return ERR_PTR(-EBUSY); 1812 1813 *event |= BNXT_AGG_EVENT; 1814 idx = NEXT_CMP(idx); 1815 } 1816 gro = !!TPA_END_GRO(tpa_end); 1817 } 1818 data = tpa_info->data; 1819 data_ptr = tpa_info->data_ptr; 1820 prefetch(data_ptr); 1821 len = tpa_info->len; 1822 mapping = tpa_info->mapping; 1823 1824 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1825 bnxt_abort_tpa(cpr, idx, agg_bufs); 1826 if (agg_bufs > MAX_SKB_FRAGS) 1827 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1828 agg_bufs, (int)MAX_SKB_FRAGS); 1829 return NULL; 1830 } 1831 1832 if (len <= bp->rx_copy_thresh) { 1833 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1834 if (!skb) { 1835 bnxt_abort_tpa(cpr, idx, agg_bufs); 1836 cpr->sw_stats->rx.rx_oom_discards += 1; 1837 return NULL; 1838 } 1839 } else { 1840 u8 *new_data; 1841 dma_addr_t new_mapping; 1842 1843 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC); 1844 if (!new_data) { 1845 bnxt_abort_tpa(cpr, idx, agg_bufs); 1846 cpr->sw_stats->rx.rx_oom_discards += 1; 1847 return NULL; 1848 } 1849 1850 tpa_info->data = new_data; 1851 tpa_info->data_ptr = new_data + bp->rx_offset; 1852 tpa_info->mapping = new_mapping; 1853 1854 skb = napi_build_skb(data, bp->rx_buf_size); 1855 dma_unmap_single_attrs(&bp->pdev->dev, mapping, 1856 bp->rx_buf_use_size, bp->rx_dir, 1857 DMA_ATTR_WEAK_ORDERING); 1858 1859 if (!skb) { 1860 skb_free_frag(data); 1861 bnxt_abort_tpa(cpr, idx, agg_bufs); 1862 cpr->sw_stats->rx.rx_oom_discards += 1; 1863 return NULL; 1864 } 1865 skb_reserve(skb, bp->rx_offset); 1866 skb_put(skb, len); 1867 } 1868 1869 if (agg_bufs) { 1870 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true); 1871 if (!skb) { 1872 /* Page reuse already handled by bnxt_rx_pages(). */ 1873 cpr->sw_stats->rx.rx_oom_discards += 1; 1874 return NULL; 1875 } 1876 } 1877 1878 if (tpa_info->cfa_code_valid) 1879 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1880 skb->protocol = eth_type_trans(skb, dev); 1881 1882 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1883 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1884 1885 if (tpa_info->vlan_valid && 1886 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1887 __be16 vlan_proto = htons(tpa_info->metadata >> 1888 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1889 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1890 1891 if (eth_type_vlan(vlan_proto)) { 1892 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1893 } else { 1894 dev_kfree_skb(skb); 1895 return NULL; 1896 } 1897 } 1898 1899 skb_checksum_none_assert(skb); 1900 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1901 skb->ip_summed = CHECKSUM_UNNECESSARY; 1902 skb->csum_level = 1903 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1904 } 1905 1906 if (gro) 1907 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1908 1909 return skb; 1910 } 1911 1912 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1913 struct rx_agg_cmp *rx_agg) 1914 { 1915 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1916 struct bnxt_tpa_info *tpa_info; 1917 1918 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1919 tpa_info = &rxr->rx_tpa[agg_id]; 1920 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1921 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1922 } 1923 1924 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1925 struct sk_buff *skb) 1926 { 1927 skb_mark_for_recycle(skb); 1928 1929 if (skb->dev != bp->dev) { 1930 /* this packet belongs to a vf-rep */ 1931 bnxt_vf_rep_rx(bp, skb); 1932 return; 1933 } 1934 skb_record_rx_queue(skb, bnapi->index); 1935 napi_gro_receive(&bnapi->napi, skb); 1936 } 1937 1938 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 1939 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 1940 { 1941 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 1942 1943 if (BNXT_PTP_RX_TS_VALID(flags)) 1944 goto ts_valid; 1945 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 1946 return false; 1947 1948 ts_valid: 1949 *cmpl_ts = ts; 1950 return true; 1951 } 1952 1953 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 1954 struct rx_cmp *rxcmp, 1955 struct rx_cmp_ext *rxcmp1) 1956 { 1957 __be16 vlan_proto; 1958 u16 vtag; 1959 1960 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1961 __le32 flags2 = rxcmp1->rx_cmp_flags2; 1962 u32 meta_data; 1963 1964 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 1965 return skb; 1966 1967 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 1968 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1969 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 1970 if (eth_type_vlan(vlan_proto)) 1971 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1972 else 1973 goto vlan_err; 1974 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 1975 if (RX_CMP_VLAN_VALID(rxcmp)) { 1976 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 1977 1978 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 1979 vlan_proto = htons(ETH_P_8021Q); 1980 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 1981 vlan_proto = htons(ETH_P_8021AD); 1982 else 1983 goto vlan_err; 1984 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 1985 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1986 } 1987 } 1988 return skb; 1989 vlan_err: 1990 dev_kfree_skb(skb); 1991 return NULL; 1992 } 1993 1994 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 1995 struct rx_cmp *rxcmp) 1996 { 1997 u8 ext_op; 1998 1999 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2000 switch (ext_op) { 2001 case EXT_OP_INNER_4: 2002 case EXT_OP_OUTER_4: 2003 case EXT_OP_INNFL_3: 2004 case EXT_OP_OUTFL_3: 2005 return PKT_HASH_TYPE_L4; 2006 default: 2007 return PKT_HASH_TYPE_L3; 2008 } 2009 } 2010 2011 /* returns the following: 2012 * 1 - 1 packet successfully received 2013 * 0 - successful TPA_START, packet not completed yet 2014 * -EBUSY - completion ring does not have all the agg buffers yet 2015 * -ENOMEM - packet aborted due to out of memory 2016 * -EIO - packet aborted due to hw error indicated in BD 2017 */ 2018 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2019 u32 *raw_cons, u8 *event) 2020 { 2021 struct bnxt_napi *bnapi = cpr->bnapi; 2022 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2023 struct net_device *dev = bp->dev; 2024 struct rx_cmp *rxcmp; 2025 struct rx_cmp_ext *rxcmp1; 2026 u32 tmp_raw_cons = *raw_cons; 2027 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2028 struct bnxt_sw_rx_bd *rx_buf; 2029 unsigned int len; 2030 u8 *data_ptr, agg_bufs, cmp_type; 2031 bool xdp_active = false; 2032 dma_addr_t dma_addr; 2033 struct sk_buff *skb; 2034 struct xdp_buff xdp; 2035 u32 flags, misc; 2036 u32 cmpl_ts; 2037 void *data; 2038 int rc = 0; 2039 2040 rxcmp = (struct rx_cmp *) 2041 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2042 2043 cmp_type = RX_CMP_TYPE(rxcmp); 2044 2045 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2046 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2047 goto next_rx_no_prod_no_len; 2048 } 2049 2050 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2051 cp_cons = RING_CMP(tmp_raw_cons); 2052 rxcmp1 = (struct rx_cmp_ext *) 2053 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2054 2055 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2056 return -EBUSY; 2057 2058 /* The valid test of the entry must be done first before 2059 * reading any further. 2060 */ 2061 dma_rmb(); 2062 prod = rxr->rx_prod; 2063 2064 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2065 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2066 bnxt_tpa_start(bp, rxr, cmp_type, 2067 (struct rx_tpa_start_cmp *)rxcmp, 2068 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2069 2070 *event |= BNXT_RX_EVENT; 2071 goto next_rx_no_prod_no_len; 2072 2073 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2074 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2075 (struct rx_tpa_end_cmp *)rxcmp, 2076 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2077 2078 if (IS_ERR(skb)) 2079 return -EBUSY; 2080 2081 rc = -ENOMEM; 2082 if (likely(skb)) { 2083 bnxt_deliver_skb(bp, bnapi, skb); 2084 rc = 1; 2085 } 2086 *event |= BNXT_RX_EVENT; 2087 goto next_rx_no_prod_no_len; 2088 } 2089 2090 cons = rxcmp->rx_cmp_opaque; 2091 if (unlikely(cons != rxr->rx_next_cons)) { 2092 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2093 2094 /* 0xffff is forced error, don't print it */ 2095 if (rxr->rx_next_cons != 0xffff) 2096 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2097 cons, rxr->rx_next_cons); 2098 bnxt_sched_reset_rxr(bp, rxr); 2099 if (rc1) 2100 return rc1; 2101 goto next_rx_no_prod_no_len; 2102 } 2103 rx_buf = &rxr->rx_buf_ring[cons]; 2104 data = rx_buf->data; 2105 data_ptr = rx_buf->data_ptr; 2106 prefetch(data_ptr); 2107 2108 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2109 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2110 2111 if (agg_bufs) { 2112 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2113 return -EBUSY; 2114 2115 cp_cons = NEXT_CMP(cp_cons); 2116 *event |= BNXT_AGG_EVENT; 2117 } 2118 *event |= BNXT_RX_EVENT; 2119 2120 rx_buf->data = NULL; 2121 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2122 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2123 2124 bnxt_reuse_rx_data(rxr, cons, data); 2125 if (agg_bufs) 2126 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2127 false); 2128 2129 rc = -EIO; 2130 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2131 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2132 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2133 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2134 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2135 rx_err); 2136 bnxt_sched_reset_rxr(bp, rxr); 2137 } 2138 } 2139 goto next_rx_no_len; 2140 } 2141 2142 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2143 len = flags >> RX_CMP_LEN_SHIFT; 2144 dma_addr = rx_buf->mapping; 2145 2146 if (bnxt_xdp_attached(bp, rxr)) { 2147 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2148 if (agg_bufs) { 2149 u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp, 2150 cp_cons, agg_bufs, 2151 false); 2152 if (!frag_len) 2153 goto oom_next_rx; 2154 } 2155 xdp_active = true; 2156 } 2157 2158 if (xdp_active) { 2159 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2160 rc = 1; 2161 goto next_rx; 2162 } 2163 } 2164 2165 if (len <= bp->rx_copy_thresh) { 2166 if (!xdp_active) 2167 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2168 else 2169 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2170 bnxt_reuse_rx_data(rxr, cons, data); 2171 if (!skb) { 2172 if (agg_bufs) { 2173 if (!xdp_active) 2174 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2175 agg_bufs, false); 2176 else 2177 bnxt_xdp_buff_frags_free(rxr, &xdp); 2178 } 2179 goto oom_next_rx; 2180 } 2181 } else { 2182 u32 payload; 2183 2184 if (rx_buf->data_ptr == data_ptr) 2185 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2186 else 2187 payload = 0; 2188 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2189 payload | len); 2190 if (!skb) 2191 goto oom_next_rx; 2192 } 2193 2194 if (agg_bufs) { 2195 if (!xdp_active) { 2196 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false); 2197 if (!skb) 2198 goto oom_next_rx; 2199 } else { 2200 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1); 2201 if (!skb) { 2202 /* we should be able to free the old skb here */ 2203 bnxt_xdp_buff_frags_free(rxr, &xdp); 2204 goto oom_next_rx; 2205 } 2206 } 2207 } 2208 2209 if (RX_CMP_HASH_VALID(rxcmp)) { 2210 enum pkt_hash_types type; 2211 2212 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2213 type = bnxt_rss_ext_op(bp, rxcmp); 2214 } else { 2215 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp); 2216 2217 /* RSS profiles 1 and 3 with extract code 0 for inner 2218 * 4-tuple 2219 */ 2220 if (hash_type != 1 && hash_type != 3) 2221 type = PKT_HASH_TYPE_L3; 2222 else 2223 type = PKT_HASH_TYPE_L4; 2224 } 2225 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2226 } 2227 2228 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2229 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2230 skb->protocol = eth_type_trans(skb, dev); 2231 2232 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2233 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2234 if (!skb) 2235 goto next_rx; 2236 } 2237 2238 skb_checksum_none_assert(skb); 2239 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2240 if (dev->features & NETIF_F_RXCSUM) { 2241 skb->ip_summed = CHECKSUM_UNNECESSARY; 2242 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2243 } 2244 } else { 2245 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2246 if (dev->features & NETIF_F_RXCSUM) 2247 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2248 } 2249 } 2250 2251 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2252 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2253 u64 ns, ts; 2254 2255 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2256 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2257 2258 spin_lock_bh(&ptp->ptp_lock); 2259 ns = timecounter_cyc2time(&ptp->tc, ts); 2260 spin_unlock_bh(&ptp->ptp_lock); 2261 memset(skb_hwtstamps(skb), 0, 2262 sizeof(*skb_hwtstamps(skb))); 2263 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2264 } 2265 } 2266 } 2267 bnxt_deliver_skb(bp, bnapi, skb); 2268 rc = 1; 2269 2270 next_rx: 2271 cpr->rx_packets += 1; 2272 cpr->rx_bytes += len; 2273 2274 next_rx_no_len: 2275 rxr->rx_prod = NEXT_RX(prod); 2276 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2277 2278 next_rx_no_prod_no_len: 2279 *raw_cons = tmp_raw_cons; 2280 2281 return rc; 2282 2283 oom_next_rx: 2284 cpr->sw_stats->rx.rx_oom_discards += 1; 2285 rc = -ENOMEM; 2286 goto next_rx; 2287 } 2288 2289 /* In netpoll mode, if we are using a combined completion ring, we need to 2290 * discard the rx packets and recycle the buffers. 2291 */ 2292 static int bnxt_force_rx_discard(struct bnxt *bp, 2293 struct bnxt_cp_ring_info *cpr, 2294 u32 *raw_cons, u8 *event) 2295 { 2296 u32 tmp_raw_cons = *raw_cons; 2297 struct rx_cmp_ext *rxcmp1; 2298 struct rx_cmp *rxcmp; 2299 u16 cp_cons; 2300 u8 cmp_type; 2301 int rc; 2302 2303 cp_cons = RING_CMP(tmp_raw_cons); 2304 rxcmp = (struct rx_cmp *) 2305 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2306 2307 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2308 cp_cons = RING_CMP(tmp_raw_cons); 2309 rxcmp1 = (struct rx_cmp_ext *) 2310 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2311 2312 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2313 return -EBUSY; 2314 2315 /* The valid test of the entry must be done first before 2316 * reading any further. 2317 */ 2318 dma_rmb(); 2319 cmp_type = RX_CMP_TYPE(rxcmp); 2320 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2321 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2322 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2323 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2324 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2325 struct rx_tpa_end_cmp_ext *tpa_end1; 2326 2327 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2328 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2329 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2330 } 2331 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2332 if (rc && rc != -EBUSY) 2333 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2334 return rc; 2335 } 2336 2337 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2338 { 2339 struct bnxt_fw_health *fw_health = bp->fw_health; 2340 u32 reg = fw_health->regs[reg_idx]; 2341 u32 reg_type, reg_off, val = 0; 2342 2343 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2344 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2345 switch (reg_type) { 2346 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2347 pci_read_config_dword(bp->pdev, reg_off, &val); 2348 break; 2349 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2350 reg_off = fw_health->mapped_regs[reg_idx]; 2351 fallthrough; 2352 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2353 val = readl(bp->bar0 + reg_off); 2354 break; 2355 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2356 val = readl(bp->bar1 + reg_off); 2357 break; 2358 } 2359 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2360 val &= fw_health->fw_reset_inprog_reg_mask; 2361 return val; 2362 } 2363 2364 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2365 { 2366 int i; 2367 2368 for (i = 0; i < bp->rx_nr_rings; i++) { 2369 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2370 struct bnxt_ring_grp_info *grp_info; 2371 2372 grp_info = &bp->grp_info[grp_idx]; 2373 if (grp_info->agg_fw_ring_id == ring_id) 2374 return grp_idx; 2375 } 2376 return INVALID_HW_RING_ID; 2377 } 2378 2379 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2380 { 2381 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2382 2383 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2384 return link_info->force_link_speed2; 2385 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2386 return link_info->force_pam4_link_speed; 2387 return link_info->force_link_speed; 2388 } 2389 2390 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2391 { 2392 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2393 2394 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2395 link_info->req_link_speed = link_info->force_link_speed2; 2396 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2397 switch (link_info->req_link_speed) { 2398 case BNXT_LINK_SPEED_50GB_PAM4: 2399 case BNXT_LINK_SPEED_100GB_PAM4: 2400 case BNXT_LINK_SPEED_200GB_PAM4: 2401 case BNXT_LINK_SPEED_400GB_PAM4: 2402 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2403 break; 2404 case BNXT_LINK_SPEED_100GB_PAM4_112: 2405 case BNXT_LINK_SPEED_200GB_PAM4_112: 2406 case BNXT_LINK_SPEED_400GB_PAM4_112: 2407 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2408 break; 2409 default: 2410 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2411 } 2412 return; 2413 } 2414 link_info->req_link_speed = link_info->force_link_speed; 2415 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2416 if (link_info->force_pam4_link_speed) { 2417 link_info->req_link_speed = link_info->force_pam4_link_speed; 2418 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2419 } 2420 } 2421 2422 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2423 { 2424 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2425 2426 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2427 link_info->advertising = link_info->auto_link_speeds2; 2428 return; 2429 } 2430 link_info->advertising = link_info->auto_link_speeds; 2431 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2432 } 2433 2434 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2435 { 2436 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2437 2438 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2439 if (link_info->req_link_speed != link_info->force_link_speed2) 2440 return true; 2441 return false; 2442 } 2443 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2444 link_info->req_link_speed != link_info->force_link_speed) 2445 return true; 2446 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2447 link_info->req_link_speed != link_info->force_pam4_link_speed) 2448 return true; 2449 return false; 2450 } 2451 2452 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2453 { 2454 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2455 2456 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2457 if (link_info->advertising != link_info->auto_link_speeds2) 2458 return true; 2459 return false; 2460 } 2461 if (link_info->advertising != link_info->auto_link_speeds || 2462 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2463 return true; 2464 return false; 2465 } 2466 2467 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2468 ((data2) & \ 2469 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2470 2471 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2472 (((data2) & \ 2473 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2474 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2475 2476 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2477 ((data1) & \ 2478 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2479 2480 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2481 (((data1) & \ 2482 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2483 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2484 2485 /* Return true if the workqueue has to be scheduled */ 2486 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2487 { 2488 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2489 2490 switch (err_type) { 2491 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2492 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2493 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2494 break; 2495 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2496 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2497 break; 2498 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2499 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2500 break; 2501 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2502 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2503 char *threshold_type; 2504 bool notify = false; 2505 char *dir_str; 2506 2507 switch (type) { 2508 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2509 threshold_type = "warning"; 2510 break; 2511 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2512 threshold_type = "critical"; 2513 break; 2514 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2515 threshold_type = "fatal"; 2516 break; 2517 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2518 threshold_type = "shutdown"; 2519 break; 2520 default: 2521 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2522 return false; 2523 } 2524 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2525 dir_str = "above"; 2526 notify = true; 2527 } else { 2528 dir_str = "below"; 2529 } 2530 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2531 dir_str, threshold_type); 2532 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2533 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2534 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2535 if (notify) { 2536 bp->thermal_threshold_type = type; 2537 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2538 return true; 2539 } 2540 return false; 2541 } 2542 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2543 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2544 break; 2545 default: 2546 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2547 err_type); 2548 break; 2549 } 2550 return false; 2551 } 2552 2553 #define BNXT_GET_EVENT_PORT(data) \ 2554 ((data) & \ 2555 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2556 2557 #define BNXT_EVENT_RING_TYPE(data2) \ 2558 ((data2) & \ 2559 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2560 2561 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2562 (BNXT_EVENT_RING_TYPE(data2) == \ 2563 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2564 2565 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2566 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2567 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2568 2569 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2570 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2571 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2572 2573 #define BNXT_PHC_BITS 48 2574 2575 static int bnxt_async_event_process(struct bnxt *bp, 2576 struct hwrm_async_event_cmpl *cmpl) 2577 { 2578 u16 event_id = le16_to_cpu(cmpl->event_id); 2579 u32 data1 = le32_to_cpu(cmpl->event_data1); 2580 u32 data2 = le32_to_cpu(cmpl->event_data2); 2581 2582 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2583 event_id, data1, data2); 2584 2585 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2586 switch (event_id) { 2587 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2588 struct bnxt_link_info *link_info = &bp->link_info; 2589 2590 if (BNXT_VF(bp)) 2591 goto async_event_process_exit; 2592 2593 /* print unsupported speed warning in forced speed mode only */ 2594 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2595 (data1 & 0x20000)) { 2596 u16 fw_speed = bnxt_get_force_speed(link_info); 2597 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2598 2599 if (speed != SPEED_UNKNOWN) 2600 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2601 speed); 2602 } 2603 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2604 } 2605 fallthrough; 2606 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2607 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2608 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2609 fallthrough; 2610 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2611 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2612 break; 2613 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2614 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2615 break; 2616 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2617 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2618 2619 if (BNXT_VF(bp)) 2620 break; 2621 2622 if (bp->pf.port_id != port_id) 2623 break; 2624 2625 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2626 break; 2627 } 2628 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2629 if (BNXT_PF(bp)) 2630 goto async_event_process_exit; 2631 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2632 break; 2633 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2634 char *type_str = "Solicited"; 2635 2636 if (!bp->fw_health) 2637 goto async_event_process_exit; 2638 2639 bp->fw_reset_timestamp = jiffies; 2640 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2641 if (!bp->fw_reset_min_dsecs) 2642 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2643 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2644 if (!bp->fw_reset_max_dsecs) 2645 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2646 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2647 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2648 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2649 type_str = "Fatal"; 2650 bp->fw_health->fatalities++; 2651 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2652 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2653 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2654 type_str = "Non-fatal"; 2655 bp->fw_health->survivals++; 2656 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2657 } 2658 netif_warn(bp, hw, bp->dev, 2659 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2660 type_str, data1, data2, 2661 bp->fw_reset_min_dsecs * 100, 2662 bp->fw_reset_max_dsecs * 100); 2663 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2664 break; 2665 } 2666 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2667 struct bnxt_fw_health *fw_health = bp->fw_health; 2668 char *status_desc = "healthy"; 2669 u32 status; 2670 2671 if (!fw_health) 2672 goto async_event_process_exit; 2673 2674 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2675 fw_health->enabled = false; 2676 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2677 break; 2678 } 2679 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2680 fw_health->tmr_multiplier = 2681 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2682 bp->current_interval * 10); 2683 fw_health->tmr_counter = fw_health->tmr_multiplier; 2684 if (!fw_health->enabled) 2685 fw_health->last_fw_heartbeat = 2686 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2687 fw_health->last_fw_reset_cnt = 2688 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2689 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2690 if (status != BNXT_FW_STATUS_HEALTHY) 2691 status_desc = "unhealthy"; 2692 netif_info(bp, drv, bp->dev, 2693 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2694 fw_health->primary ? "primary" : "backup", status, 2695 status_desc, fw_health->last_fw_reset_cnt); 2696 if (!fw_health->enabled) { 2697 /* Make sure tmr_counter is set and visible to 2698 * bnxt_health_check() before setting enabled to true. 2699 */ 2700 smp_wmb(); 2701 fw_health->enabled = true; 2702 } 2703 goto async_event_process_exit; 2704 } 2705 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2706 netif_notice(bp, hw, bp->dev, 2707 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2708 data1, data2); 2709 goto async_event_process_exit; 2710 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2711 struct bnxt_rx_ring_info *rxr; 2712 u16 grp_idx; 2713 2714 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2715 goto async_event_process_exit; 2716 2717 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2718 BNXT_EVENT_RING_TYPE(data2), data1); 2719 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2720 goto async_event_process_exit; 2721 2722 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2723 if (grp_idx == INVALID_HW_RING_ID) { 2724 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2725 data1); 2726 goto async_event_process_exit; 2727 } 2728 rxr = bp->bnapi[grp_idx]->rx_ring; 2729 bnxt_sched_reset_rxr(bp, rxr); 2730 goto async_event_process_exit; 2731 } 2732 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2733 struct bnxt_fw_health *fw_health = bp->fw_health; 2734 2735 netif_notice(bp, hw, bp->dev, 2736 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2737 data1, data2); 2738 if (fw_health) { 2739 fw_health->echo_req_data1 = data1; 2740 fw_health->echo_req_data2 = data2; 2741 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2742 break; 2743 } 2744 goto async_event_process_exit; 2745 } 2746 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2747 bnxt_ptp_pps_event(bp, data1, data2); 2748 goto async_event_process_exit; 2749 } 2750 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2751 if (bnxt_event_error_report(bp, data1, data2)) 2752 break; 2753 goto async_event_process_exit; 2754 } 2755 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2756 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2757 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2758 if (BNXT_PTP_USE_RTC(bp)) { 2759 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2760 u64 ns; 2761 2762 if (!ptp) 2763 goto async_event_process_exit; 2764 2765 spin_lock_bh(&ptp->ptp_lock); 2766 bnxt_ptp_update_current_time(bp); 2767 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2768 BNXT_PHC_BITS) | ptp->current_time); 2769 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2770 spin_unlock_bh(&ptp->ptp_lock); 2771 } 2772 break; 2773 } 2774 goto async_event_process_exit; 2775 } 2776 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2777 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2778 2779 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2780 goto async_event_process_exit; 2781 } 2782 default: 2783 goto async_event_process_exit; 2784 } 2785 __bnxt_queue_sp_work(bp); 2786 async_event_process_exit: 2787 return 0; 2788 } 2789 2790 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2791 { 2792 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2793 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2794 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2795 (struct hwrm_fwd_req_cmpl *)txcmp; 2796 2797 switch (cmpl_type) { 2798 case CMPL_BASE_TYPE_HWRM_DONE: 2799 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2800 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2801 break; 2802 2803 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2804 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2805 2806 if ((vf_id < bp->pf.first_vf_id) || 2807 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2808 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2809 vf_id); 2810 return -EINVAL; 2811 } 2812 2813 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2814 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2815 break; 2816 2817 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2818 bnxt_async_event_process(bp, 2819 (struct hwrm_async_event_cmpl *)txcmp); 2820 break; 2821 2822 default: 2823 break; 2824 } 2825 2826 return 0; 2827 } 2828 2829 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2830 { 2831 struct bnxt_napi *bnapi = dev_instance; 2832 struct bnxt *bp = bnapi->bp; 2833 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2834 u32 cons = RING_CMP(cpr->cp_raw_cons); 2835 2836 cpr->event_ctr++; 2837 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2838 napi_schedule(&bnapi->napi); 2839 return IRQ_HANDLED; 2840 } 2841 2842 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2843 { 2844 u32 raw_cons = cpr->cp_raw_cons; 2845 u16 cons = RING_CMP(raw_cons); 2846 struct tx_cmp *txcmp; 2847 2848 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2849 2850 return TX_CMP_VALID(txcmp, raw_cons); 2851 } 2852 2853 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2854 int budget) 2855 { 2856 struct bnxt_napi *bnapi = cpr->bnapi; 2857 u32 raw_cons = cpr->cp_raw_cons; 2858 u32 cons; 2859 int rx_pkts = 0; 2860 u8 event = 0; 2861 struct tx_cmp *txcmp; 2862 2863 cpr->has_more_work = 0; 2864 cpr->had_work_done = 1; 2865 while (1) { 2866 u8 cmp_type; 2867 int rc; 2868 2869 cons = RING_CMP(raw_cons); 2870 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2871 2872 if (!TX_CMP_VALID(txcmp, raw_cons)) 2873 break; 2874 2875 /* The valid test of the entry must be done first before 2876 * reading any further. 2877 */ 2878 dma_rmb(); 2879 cmp_type = TX_CMP_TYPE(txcmp); 2880 if (cmp_type == CMP_TYPE_TX_L2_CMP || 2881 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 2882 u32 opaque = txcmp->tx_cmp_opaque; 2883 struct bnxt_tx_ring_info *txr; 2884 u16 tx_freed; 2885 2886 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 2887 event |= BNXT_TX_CMP_EVENT; 2888 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 2889 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 2890 else 2891 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 2892 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 2893 bp->tx_ring_mask; 2894 /* return full budget so NAPI will complete. */ 2895 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 2896 rx_pkts = budget; 2897 raw_cons = NEXT_RAW_CMP(raw_cons); 2898 if (budget) 2899 cpr->has_more_work = 1; 2900 break; 2901 } 2902 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 2903 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 2904 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 2905 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2906 if (likely(budget)) 2907 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 2908 else 2909 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 2910 &event); 2911 if (likely(rc >= 0)) 2912 rx_pkts += rc; 2913 /* Increment rx_pkts when rc is -ENOMEM to count towards 2914 * the NAPI budget. Otherwise, we may potentially loop 2915 * here forever if we consistently cannot allocate 2916 * buffers. 2917 */ 2918 else if (rc == -ENOMEM && budget) 2919 rx_pkts++; 2920 else if (rc == -EBUSY) /* partial completion */ 2921 break; 2922 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 2923 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 2924 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 2925 bnxt_hwrm_handler(bp, txcmp); 2926 } 2927 raw_cons = NEXT_RAW_CMP(raw_cons); 2928 2929 if (rx_pkts && rx_pkts == budget) { 2930 cpr->has_more_work = 1; 2931 break; 2932 } 2933 } 2934 2935 if (event & BNXT_REDIRECT_EVENT) { 2936 xdp_do_flush(); 2937 event &= ~BNXT_REDIRECT_EVENT; 2938 } 2939 2940 if (event & BNXT_TX_EVENT) { 2941 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 2942 u16 prod = txr->tx_prod; 2943 2944 /* Sync BD data before updating doorbell */ 2945 wmb(); 2946 2947 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 2948 event &= ~BNXT_TX_EVENT; 2949 } 2950 2951 cpr->cp_raw_cons = raw_cons; 2952 bnapi->events |= event; 2953 return rx_pkts; 2954 } 2955 2956 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 2957 int budget) 2958 { 2959 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 2960 bnapi->tx_int(bp, bnapi, budget); 2961 2962 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 2963 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2964 2965 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 2966 bnapi->events &= ~BNXT_RX_EVENT; 2967 } 2968 if (bnapi->events & BNXT_AGG_EVENT) { 2969 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2970 2971 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 2972 bnapi->events &= ~BNXT_AGG_EVENT; 2973 } 2974 } 2975 2976 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2977 int budget) 2978 { 2979 struct bnxt_napi *bnapi = cpr->bnapi; 2980 int rx_pkts; 2981 2982 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 2983 2984 /* ACK completion ring before freeing tx ring and producing new 2985 * buffers in rx/agg rings to prevent overflowing the completion 2986 * ring. 2987 */ 2988 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 2989 2990 __bnxt_poll_work_done(bp, bnapi, budget); 2991 return rx_pkts; 2992 } 2993 2994 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 2995 { 2996 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 2997 struct bnxt *bp = bnapi->bp; 2998 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2999 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3000 struct tx_cmp *txcmp; 3001 struct rx_cmp_ext *rxcmp1; 3002 u32 cp_cons, tmp_raw_cons; 3003 u32 raw_cons = cpr->cp_raw_cons; 3004 bool flush_xdp = false; 3005 u32 rx_pkts = 0; 3006 u8 event = 0; 3007 3008 while (1) { 3009 int rc; 3010 3011 cp_cons = RING_CMP(raw_cons); 3012 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3013 3014 if (!TX_CMP_VALID(txcmp, raw_cons)) 3015 break; 3016 3017 /* The valid test of the entry must be done first before 3018 * reading any further. 3019 */ 3020 dma_rmb(); 3021 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3022 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3023 cp_cons = RING_CMP(tmp_raw_cons); 3024 rxcmp1 = (struct rx_cmp_ext *) 3025 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3026 3027 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3028 break; 3029 3030 /* force an error to recycle the buffer */ 3031 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3032 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3033 3034 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3035 if (likely(rc == -EIO) && budget) 3036 rx_pkts++; 3037 else if (rc == -EBUSY) /* partial completion */ 3038 break; 3039 if (event & BNXT_REDIRECT_EVENT) 3040 flush_xdp = true; 3041 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3042 CMPL_BASE_TYPE_HWRM_DONE)) { 3043 bnxt_hwrm_handler(bp, txcmp); 3044 } else { 3045 netdev_err(bp->dev, 3046 "Invalid completion received on special ring\n"); 3047 } 3048 raw_cons = NEXT_RAW_CMP(raw_cons); 3049 3050 if (rx_pkts == budget) 3051 break; 3052 } 3053 3054 cpr->cp_raw_cons = raw_cons; 3055 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3056 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3057 3058 if (event & BNXT_AGG_EVENT) 3059 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3060 if (flush_xdp) 3061 xdp_do_flush(); 3062 3063 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3064 napi_complete_done(napi, rx_pkts); 3065 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3066 } 3067 return rx_pkts; 3068 } 3069 3070 static int bnxt_poll(struct napi_struct *napi, int budget) 3071 { 3072 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3073 struct bnxt *bp = bnapi->bp; 3074 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3075 int work_done = 0; 3076 3077 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3078 napi_complete(napi); 3079 return 0; 3080 } 3081 while (1) { 3082 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3083 3084 if (work_done >= budget) { 3085 if (!budget) 3086 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3087 break; 3088 } 3089 3090 if (!bnxt_has_work(bp, cpr)) { 3091 if (napi_complete_done(napi, work_done)) 3092 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3093 break; 3094 } 3095 } 3096 if (bp->flags & BNXT_FLAG_DIM) { 3097 struct dim_sample dim_sample = {}; 3098 3099 dim_update_sample(cpr->event_ctr, 3100 cpr->rx_packets, 3101 cpr->rx_bytes, 3102 &dim_sample); 3103 net_dim(&cpr->dim, dim_sample); 3104 } 3105 return work_done; 3106 } 3107 3108 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3109 { 3110 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3111 int i, work_done = 0; 3112 3113 for (i = 0; i < cpr->cp_ring_count; i++) { 3114 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3115 3116 if (cpr2->had_nqe_notify) { 3117 work_done += __bnxt_poll_work(bp, cpr2, 3118 budget - work_done); 3119 cpr->has_more_work |= cpr2->has_more_work; 3120 } 3121 } 3122 return work_done; 3123 } 3124 3125 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3126 u64 dbr_type, int budget) 3127 { 3128 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3129 int i; 3130 3131 for (i = 0; i < cpr->cp_ring_count; i++) { 3132 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3133 struct bnxt_db_info *db; 3134 3135 if (cpr2->had_work_done) { 3136 u32 tgl = 0; 3137 3138 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3139 cpr2->had_nqe_notify = 0; 3140 tgl = cpr2->toggle; 3141 } 3142 db = &cpr2->cp_db; 3143 bnxt_writeq(bp, 3144 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3145 DB_RING_IDX(db, cpr2->cp_raw_cons), 3146 db->doorbell); 3147 cpr2->had_work_done = 0; 3148 } 3149 } 3150 __bnxt_poll_work_done(bp, bnapi, budget); 3151 } 3152 3153 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3154 { 3155 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3156 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3157 struct bnxt_cp_ring_info *cpr_rx; 3158 u32 raw_cons = cpr->cp_raw_cons; 3159 struct bnxt *bp = bnapi->bp; 3160 struct nqe_cn *nqcmp; 3161 int work_done = 0; 3162 u32 cons; 3163 3164 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3165 napi_complete(napi); 3166 return 0; 3167 } 3168 if (cpr->has_more_work) { 3169 cpr->has_more_work = 0; 3170 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3171 } 3172 while (1) { 3173 u16 type; 3174 3175 cons = RING_CMP(raw_cons); 3176 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3177 3178 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3179 if (cpr->has_more_work) 3180 break; 3181 3182 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3183 budget); 3184 cpr->cp_raw_cons = raw_cons; 3185 if (napi_complete_done(napi, work_done)) 3186 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3187 cpr->cp_raw_cons); 3188 goto poll_done; 3189 } 3190 3191 /* The valid test of the entry must be done first before 3192 * reading any further. 3193 */ 3194 dma_rmb(); 3195 3196 type = le16_to_cpu(nqcmp->type); 3197 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3198 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3199 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3200 struct bnxt_cp_ring_info *cpr2; 3201 3202 /* No more budget for RX work */ 3203 if (budget && work_done >= budget && 3204 cq_type == BNXT_NQ_HDL_TYPE_RX) 3205 break; 3206 3207 idx = BNXT_NQ_HDL_IDX(idx); 3208 cpr2 = &cpr->cp_ring_arr[idx]; 3209 cpr2->had_nqe_notify = 1; 3210 cpr2->toggle = NQE_CN_TOGGLE(type); 3211 work_done += __bnxt_poll_work(bp, cpr2, 3212 budget - work_done); 3213 cpr->has_more_work |= cpr2->has_more_work; 3214 } else { 3215 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3216 } 3217 raw_cons = NEXT_RAW_CMP(raw_cons); 3218 } 3219 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3220 if (raw_cons != cpr->cp_raw_cons) { 3221 cpr->cp_raw_cons = raw_cons; 3222 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3223 } 3224 poll_done: 3225 cpr_rx = &cpr->cp_ring_arr[0]; 3226 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3227 (bp->flags & BNXT_FLAG_DIM)) { 3228 struct dim_sample dim_sample = {}; 3229 3230 dim_update_sample(cpr->event_ctr, 3231 cpr_rx->rx_packets, 3232 cpr_rx->rx_bytes, 3233 &dim_sample); 3234 net_dim(&cpr->dim, dim_sample); 3235 } 3236 return work_done; 3237 } 3238 3239 static void bnxt_free_tx_skbs(struct bnxt *bp) 3240 { 3241 int i, max_idx; 3242 struct pci_dev *pdev = bp->pdev; 3243 3244 if (!bp->tx_ring) 3245 return; 3246 3247 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3248 for (i = 0; i < bp->tx_nr_rings; i++) { 3249 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3250 int j; 3251 3252 if (!txr->tx_buf_ring) 3253 continue; 3254 3255 for (j = 0; j < max_idx;) { 3256 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j]; 3257 struct sk_buff *skb; 3258 int k, last; 3259 3260 if (i < bp->tx_nr_rings_xdp && 3261 tx_buf->action == XDP_REDIRECT) { 3262 dma_unmap_single(&pdev->dev, 3263 dma_unmap_addr(tx_buf, mapping), 3264 dma_unmap_len(tx_buf, len), 3265 DMA_TO_DEVICE); 3266 xdp_return_frame(tx_buf->xdpf); 3267 tx_buf->action = 0; 3268 tx_buf->xdpf = NULL; 3269 j++; 3270 continue; 3271 } 3272 3273 skb = tx_buf->skb; 3274 if (!skb) { 3275 j++; 3276 continue; 3277 } 3278 3279 tx_buf->skb = NULL; 3280 3281 if (tx_buf->is_push) { 3282 dev_kfree_skb(skb); 3283 j += 2; 3284 continue; 3285 } 3286 3287 dma_unmap_single(&pdev->dev, 3288 dma_unmap_addr(tx_buf, mapping), 3289 skb_headlen(skb), 3290 DMA_TO_DEVICE); 3291 3292 last = tx_buf->nr_frags; 3293 j += 2; 3294 for (k = 0; k < last; k++, j++) { 3295 int ring_idx = j & bp->tx_ring_mask; 3296 skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 3297 3298 tx_buf = &txr->tx_buf_ring[ring_idx]; 3299 dma_unmap_page( 3300 &pdev->dev, 3301 dma_unmap_addr(tx_buf, mapping), 3302 skb_frag_size(frag), DMA_TO_DEVICE); 3303 } 3304 dev_kfree_skb(skb); 3305 } 3306 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i)); 3307 } 3308 } 3309 3310 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3311 { 3312 struct pci_dev *pdev = bp->pdev; 3313 int i, max_idx; 3314 3315 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3316 3317 for (i = 0; i < max_idx; i++) { 3318 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3319 dma_addr_t mapping = rx_buf->mapping; 3320 void *data = rx_buf->data; 3321 3322 if (!data) 3323 continue; 3324 3325 rx_buf->data = NULL; 3326 if (BNXT_RX_PAGE_MODE(bp)) { 3327 page_pool_recycle_direct(rxr->page_pool, data); 3328 } else { 3329 dma_unmap_single_attrs(&pdev->dev, mapping, 3330 bp->rx_buf_use_size, bp->rx_dir, 3331 DMA_ATTR_WEAK_ORDERING); 3332 skb_free_frag(data); 3333 } 3334 } 3335 } 3336 3337 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3338 { 3339 int i, max_idx; 3340 3341 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3342 3343 for (i = 0; i < max_idx; i++) { 3344 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3345 struct page *page = rx_agg_buf->page; 3346 3347 if (!page) 3348 continue; 3349 3350 rx_agg_buf->page = NULL; 3351 __clear_bit(i, rxr->rx_agg_bmap); 3352 3353 page_pool_recycle_direct(rxr->page_pool, page); 3354 } 3355 } 3356 3357 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr) 3358 { 3359 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 3360 struct pci_dev *pdev = bp->pdev; 3361 struct bnxt_tpa_idx_map *map; 3362 int i; 3363 3364 if (!rxr->rx_tpa) 3365 goto skip_rx_tpa_free; 3366 3367 for (i = 0; i < bp->max_tpa; i++) { 3368 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3369 u8 *data = tpa_info->data; 3370 3371 if (!data) 3372 continue; 3373 3374 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping, 3375 bp->rx_buf_use_size, bp->rx_dir, 3376 DMA_ATTR_WEAK_ORDERING); 3377 3378 tpa_info->data = NULL; 3379 3380 skb_free_frag(data); 3381 } 3382 3383 skip_rx_tpa_free: 3384 if (!rxr->rx_buf_ring) 3385 goto skip_rx_buf_free; 3386 3387 bnxt_free_one_rx_ring(bp, rxr); 3388 3389 skip_rx_buf_free: 3390 if (!rxr->rx_agg_ring) 3391 goto skip_rx_agg_free; 3392 3393 bnxt_free_one_rx_agg_ring(bp, rxr); 3394 3395 skip_rx_agg_free: 3396 map = rxr->rx_tpa_idx_map; 3397 if (map) 3398 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3399 } 3400 3401 static void bnxt_free_rx_skbs(struct bnxt *bp) 3402 { 3403 int i; 3404 3405 if (!bp->rx_ring) 3406 return; 3407 3408 for (i = 0; i < bp->rx_nr_rings; i++) 3409 bnxt_free_one_rx_ring_skbs(bp, i); 3410 } 3411 3412 static void bnxt_free_skbs(struct bnxt *bp) 3413 { 3414 bnxt_free_tx_skbs(bp); 3415 bnxt_free_rx_skbs(bp); 3416 } 3417 3418 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3419 { 3420 u8 init_val = ctxm->init_value; 3421 u16 offset = ctxm->init_offset; 3422 u8 *p2 = p; 3423 int i; 3424 3425 if (!init_val) 3426 return; 3427 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3428 memset(p, init_val, len); 3429 return; 3430 } 3431 for (i = 0; i < len; i += ctxm->entry_size) 3432 *(p2 + i + offset) = init_val; 3433 } 3434 3435 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3436 { 3437 struct pci_dev *pdev = bp->pdev; 3438 int i; 3439 3440 if (!rmem->pg_arr) 3441 goto skip_pages; 3442 3443 for (i = 0; i < rmem->nr_pages; i++) { 3444 if (!rmem->pg_arr[i]) 3445 continue; 3446 3447 dma_free_coherent(&pdev->dev, rmem->page_size, 3448 rmem->pg_arr[i], rmem->dma_arr[i]); 3449 3450 rmem->pg_arr[i] = NULL; 3451 } 3452 skip_pages: 3453 if (rmem->pg_tbl) { 3454 size_t pg_tbl_size = rmem->nr_pages * 8; 3455 3456 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3457 pg_tbl_size = rmem->page_size; 3458 dma_free_coherent(&pdev->dev, pg_tbl_size, 3459 rmem->pg_tbl, rmem->pg_tbl_map); 3460 rmem->pg_tbl = NULL; 3461 } 3462 if (rmem->vmem_size && *rmem->vmem) { 3463 vfree(*rmem->vmem); 3464 *rmem->vmem = NULL; 3465 } 3466 } 3467 3468 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3469 { 3470 struct pci_dev *pdev = bp->pdev; 3471 u64 valid_bit = 0; 3472 int i; 3473 3474 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3475 valid_bit = PTU_PTE_VALID; 3476 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3477 size_t pg_tbl_size = rmem->nr_pages * 8; 3478 3479 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3480 pg_tbl_size = rmem->page_size; 3481 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3482 &rmem->pg_tbl_map, 3483 GFP_KERNEL); 3484 if (!rmem->pg_tbl) 3485 return -ENOMEM; 3486 } 3487 3488 for (i = 0; i < rmem->nr_pages; i++) { 3489 u64 extra_bits = valid_bit; 3490 3491 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3492 rmem->page_size, 3493 &rmem->dma_arr[i], 3494 GFP_KERNEL); 3495 if (!rmem->pg_arr[i]) 3496 return -ENOMEM; 3497 3498 if (rmem->ctx_mem) 3499 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3500 rmem->page_size); 3501 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3502 if (i == rmem->nr_pages - 2 && 3503 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3504 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3505 else if (i == rmem->nr_pages - 1 && 3506 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3507 extra_bits |= PTU_PTE_LAST; 3508 rmem->pg_tbl[i] = 3509 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3510 } 3511 } 3512 3513 if (rmem->vmem_size) { 3514 *rmem->vmem = vzalloc(rmem->vmem_size); 3515 if (!(*rmem->vmem)) 3516 return -ENOMEM; 3517 } 3518 return 0; 3519 } 3520 3521 static void bnxt_free_tpa_info(struct bnxt *bp) 3522 { 3523 int i, j; 3524 3525 for (i = 0; i < bp->rx_nr_rings; i++) { 3526 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3527 3528 kfree(rxr->rx_tpa_idx_map); 3529 rxr->rx_tpa_idx_map = NULL; 3530 if (rxr->rx_tpa) { 3531 for (j = 0; j < bp->max_tpa; j++) { 3532 kfree(rxr->rx_tpa[j].agg_arr); 3533 rxr->rx_tpa[j].agg_arr = NULL; 3534 } 3535 } 3536 kfree(rxr->rx_tpa); 3537 rxr->rx_tpa = NULL; 3538 } 3539 } 3540 3541 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3542 { 3543 int i, j; 3544 3545 bp->max_tpa = MAX_TPA; 3546 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3547 if (!bp->max_tpa_v2) 3548 return 0; 3549 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3550 } 3551 3552 for (i = 0; i < bp->rx_nr_rings; i++) { 3553 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3554 struct rx_agg_cmp *agg; 3555 3556 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3557 GFP_KERNEL); 3558 if (!rxr->rx_tpa) 3559 return -ENOMEM; 3560 3561 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3562 continue; 3563 for (j = 0; j < bp->max_tpa; j++) { 3564 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3565 if (!agg) 3566 return -ENOMEM; 3567 rxr->rx_tpa[j].agg_arr = agg; 3568 } 3569 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3570 GFP_KERNEL); 3571 if (!rxr->rx_tpa_idx_map) 3572 return -ENOMEM; 3573 } 3574 return 0; 3575 } 3576 3577 static void bnxt_free_rx_rings(struct bnxt *bp) 3578 { 3579 int i; 3580 3581 if (!bp->rx_ring) 3582 return; 3583 3584 bnxt_free_tpa_info(bp); 3585 for (i = 0; i < bp->rx_nr_rings; i++) { 3586 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3587 struct bnxt_ring_struct *ring; 3588 3589 if (rxr->xdp_prog) 3590 bpf_prog_put(rxr->xdp_prog); 3591 3592 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3593 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3594 3595 page_pool_destroy(rxr->page_pool); 3596 rxr->page_pool = NULL; 3597 3598 kfree(rxr->rx_agg_bmap); 3599 rxr->rx_agg_bmap = NULL; 3600 3601 ring = &rxr->rx_ring_struct; 3602 bnxt_free_ring(bp, &ring->ring_mem); 3603 3604 ring = &rxr->rx_agg_ring_struct; 3605 bnxt_free_ring(bp, &ring->ring_mem); 3606 } 3607 } 3608 3609 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3610 struct bnxt_rx_ring_info *rxr, 3611 int numa_node) 3612 { 3613 struct page_pool_params pp = { 0 }; 3614 3615 pp.pool_size = bp->rx_agg_ring_size; 3616 if (BNXT_RX_PAGE_MODE(bp)) 3617 pp.pool_size += bp->rx_ring_size; 3618 pp.nid = numa_node; 3619 pp.napi = &rxr->bnapi->napi; 3620 pp.netdev = bp->dev; 3621 pp.dev = &bp->pdev->dev; 3622 pp.dma_dir = bp->rx_dir; 3623 pp.max_len = PAGE_SIZE; 3624 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3625 3626 rxr->page_pool = page_pool_create(&pp); 3627 if (IS_ERR(rxr->page_pool)) { 3628 int err = PTR_ERR(rxr->page_pool); 3629 3630 rxr->page_pool = NULL; 3631 return err; 3632 } 3633 return 0; 3634 } 3635 3636 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3637 { 3638 int numa_node = dev_to_node(&bp->pdev->dev); 3639 int i, rc = 0, agg_rings = 0, cpu; 3640 3641 if (!bp->rx_ring) 3642 return -ENOMEM; 3643 3644 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3645 agg_rings = 1; 3646 3647 for (i = 0; i < bp->rx_nr_rings; i++) { 3648 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3649 struct bnxt_ring_struct *ring; 3650 int cpu_node; 3651 3652 ring = &rxr->rx_ring_struct; 3653 3654 cpu = cpumask_local_spread(i, numa_node); 3655 cpu_node = cpu_to_node(cpu); 3656 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3657 i, cpu_node); 3658 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3659 if (rc) 3660 return rc; 3661 3662 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3663 if (rc < 0) 3664 return rc; 3665 3666 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3667 MEM_TYPE_PAGE_POOL, 3668 rxr->page_pool); 3669 if (rc) { 3670 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3671 return rc; 3672 } 3673 3674 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3675 if (rc) 3676 return rc; 3677 3678 ring->grp_idx = i; 3679 if (agg_rings) { 3680 u16 mem_size; 3681 3682 ring = &rxr->rx_agg_ring_struct; 3683 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3684 if (rc) 3685 return rc; 3686 3687 ring->grp_idx = i; 3688 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3689 mem_size = rxr->rx_agg_bmap_size / 8; 3690 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3691 if (!rxr->rx_agg_bmap) 3692 return -ENOMEM; 3693 } 3694 } 3695 if (bp->flags & BNXT_FLAG_TPA) 3696 rc = bnxt_alloc_tpa_info(bp); 3697 return rc; 3698 } 3699 3700 static void bnxt_free_tx_rings(struct bnxt *bp) 3701 { 3702 int i; 3703 struct pci_dev *pdev = bp->pdev; 3704 3705 if (!bp->tx_ring) 3706 return; 3707 3708 for (i = 0; i < bp->tx_nr_rings; i++) { 3709 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3710 struct bnxt_ring_struct *ring; 3711 3712 if (txr->tx_push) { 3713 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3714 txr->tx_push, txr->tx_push_mapping); 3715 txr->tx_push = NULL; 3716 } 3717 3718 ring = &txr->tx_ring_struct; 3719 3720 bnxt_free_ring(bp, &ring->ring_mem); 3721 } 3722 } 3723 3724 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3725 ((tc) * (bp)->tx_nr_rings_per_tc) 3726 3727 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3728 ((tx) % (bp)->tx_nr_rings_per_tc) 3729 3730 #define BNXT_RING_TO_TC(bp, tx) \ 3731 ((tx) / (bp)->tx_nr_rings_per_tc) 3732 3733 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3734 { 3735 int i, j, rc; 3736 struct pci_dev *pdev = bp->pdev; 3737 3738 bp->tx_push_size = 0; 3739 if (bp->tx_push_thresh) { 3740 int push_size; 3741 3742 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3743 bp->tx_push_thresh); 3744 3745 if (push_size > 256) { 3746 push_size = 0; 3747 bp->tx_push_thresh = 0; 3748 } 3749 3750 bp->tx_push_size = push_size; 3751 } 3752 3753 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3754 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3755 struct bnxt_ring_struct *ring; 3756 u8 qidx; 3757 3758 ring = &txr->tx_ring_struct; 3759 3760 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3761 if (rc) 3762 return rc; 3763 3764 ring->grp_idx = txr->bnapi->index; 3765 if (bp->tx_push_size) { 3766 dma_addr_t mapping; 3767 3768 /* One pre-allocated DMA buffer to backup 3769 * TX push operation 3770 */ 3771 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3772 bp->tx_push_size, 3773 &txr->tx_push_mapping, 3774 GFP_KERNEL); 3775 3776 if (!txr->tx_push) 3777 return -ENOMEM; 3778 3779 mapping = txr->tx_push_mapping + 3780 sizeof(struct tx_push_bd); 3781 txr->data_mapping = cpu_to_le64(mapping); 3782 } 3783 qidx = bp->tc_to_qidx[j]; 3784 ring->queue_id = bp->q_info[qidx].queue_id; 3785 spin_lock_init(&txr->xdp_tx_lock); 3786 if (i < bp->tx_nr_rings_xdp) 3787 continue; 3788 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 3789 j++; 3790 } 3791 return 0; 3792 } 3793 3794 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 3795 { 3796 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 3797 3798 kfree(cpr->cp_desc_ring); 3799 cpr->cp_desc_ring = NULL; 3800 ring->ring_mem.pg_arr = NULL; 3801 kfree(cpr->cp_desc_mapping); 3802 cpr->cp_desc_mapping = NULL; 3803 ring->ring_mem.dma_arr = NULL; 3804 } 3805 3806 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 3807 { 3808 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 3809 if (!cpr->cp_desc_ring) 3810 return -ENOMEM; 3811 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 3812 GFP_KERNEL); 3813 if (!cpr->cp_desc_mapping) 3814 return -ENOMEM; 3815 return 0; 3816 } 3817 3818 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 3819 { 3820 int i; 3821 3822 if (!bp->bnapi) 3823 return; 3824 for (i = 0; i < bp->cp_nr_rings; i++) { 3825 struct bnxt_napi *bnapi = bp->bnapi[i]; 3826 3827 if (!bnapi) 3828 continue; 3829 bnxt_free_cp_arrays(&bnapi->cp_ring); 3830 } 3831 } 3832 3833 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 3834 { 3835 int i, n = bp->cp_nr_pages; 3836 3837 for (i = 0; i < bp->cp_nr_rings; i++) { 3838 struct bnxt_napi *bnapi = bp->bnapi[i]; 3839 int rc; 3840 3841 if (!bnapi) 3842 continue; 3843 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 3844 if (rc) 3845 return rc; 3846 } 3847 return 0; 3848 } 3849 3850 static void bnxt_free_cp_rings(struct bnxt *bp) 3851 { 3852 int i; 3853 3854 if (!bp->bnapi) 3855 return; 3856 3857 for (i = 0; i < bp->cp_nr_rings; i++) { 3858 struct bnxt_napi *bnapi = bp->bnapi[i]; 3859 struct bnxt_cp_ring_info *cpr; 3860 struct bnxt_ring_struct *ring; 3861 int j; 3862 3863 if (!bnapi) 3864 continue; 3865 3866 cpr = &bnapi->cp_ring; 3867 ring = &cpr->cp_ring_struct; 3868 3869 bnxt_free_ring(bp, &ring->ring_mem); 3870 3871 if (!cpr->cp_ring_arr) 3872 continue; 3873 3874 for (j = 0; j < cpr->cp_ring_count; j++) { 3875 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 3876 3877 ring = &cpr2->cp_ring_struct; 3878 bnxt_free_ring(bp, &ring->ring_mem); 3879 bnxt_free_cp_arrays(cpr2); 3880 } 3881 kfree(cpr->cp_ring_arr); 3882 cpr->cp_ring_arr = NULL; 3883 cpr->cp_ring_count = 0; 3884 } 3885 } 3886 3887 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 3888 struct bnxt_cp_ring_info *cpr) 3889 { 3890 struct bnxt_ring_mem_info *rmem; 3891 struct bnxt_ring_struct *ring; 3892 int rc; 3893 3894 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 3895 if (rc) { 3896 bnxt_free_cp_arrays(cpr); 3897 return -ENOMEM; 3898 } 3899 ring = &cpr->cp_ring_struct; 3900 rmem = &ring->ring_mem; 3901 rmem->nr_pages = bp->cp_nr_pages; 3902 rmem->page_size = HW_CMPD_RING_SIZE; 3903 rmem->pg_arr = (void **)cpr->cp_desc_ring; 3904 rmem->dma_arr = cpr->cp_desc_mapping; 3905 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 3906 rc = bnxt_alloc_ring(bp, rmem); 3907 if (rc) { 3908 bnxt_free_ring(bp, rmem); 3909 bnxt_free_cp_arrays(cpr); 3910 } 3911 return rc; 3912 } 3913 3914 static int bnxt_alloc_cp_rings(struct bnxt *bp) 3915 { 3916 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 3917 int i, j, rc, ulp_msix; 3918 int tcs = bp->num_tc; 3919 3920 if (!tcs) 3921 tcs = 1; 3922 ulp_msix = bnxt_get_ulp_msix_num(bp); 3923 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 3924 struct bnxt_napi *bnapi = bp->bnapi[i]; 3925 struct bnxt_cp_ring_info *cpr, *cpr2; 3926 struct bnxt_ring_struct *ring; 3927 int cp_count = 0, k; 3928 int rx = 0, tx = 0; 3929 3930 if (!bnapi) 3931 continue; 3932 3933 cpr = &bnapi->cp_ring; 3934 cpr->bnapi = bnapi; 3935 ring = &cpr->cp_ring_struct; 3936 3937 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3938 if (rc) 3939 return rc; 3940 3941 ring->map_idx = ulp_msix + i; 3942 3943 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3944 continue; 3945 3946 if (i < bp->rx_nr_rings) { 3947 cp_count++; 3948 rx = 1; 3949 } 3950 if (i < bp->tx_nr_rings_xdp) { 3951 cp_count++; 3952 tx = 1; 3953 } else if ((sh && i < bp->tx_nr_rings) || 3954 (!sh && i >= bp->rx_nr_rings)) { 3955 cp_count += tcs; 3956 tx = 1; 3957 } 3958 3959 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 3960 GFP_KERNEL); 3961 if (!cpr->cp_ring_arr) 3962 return -ENOMEM; 3963 cpr->cp_ring_count = cp_count; 3964 3965 for (k = 0; k < cp_count; k++) { 3966 cpr2 = &cpr->cp_ring_arr[k]; 3967 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 3968 if (rc) 3969 return rc; 3970 cpr2->bnapi = bnapi; 3971 cpr2->sw_stats = cpr->sw_stats; 3972 cpr2->cp_idx = k; 3973 if (!k && rx) { 3974 bp->rx_ring[i].rx_cpr = cpr2; 3975 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 3976 } else { 3977 int n, tc = k - rx; 3978 3979 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 3980 bp->tx_ring[n].tx_cpr = cpr2; 3981 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 3982 } 3983 } 3984 if (tx) 3985 j++; 3986 } 3987 return 0; 3988 } 3989 3990 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 3991 struct bnxt_rx_ring_info *rxr) 3992 { 3993 struct bnxt_ring_mem_info *rmem; 3994 struct bnxt_ring_struct *ring; 3995 3996 ring = &rxr->rx_ring_struct; 3997 rmem = &ring->ring_mem; 3998 rmem->nr_pages = bp->rx_nr_pages; 3999 rmem->page_size = HW_RXBD_RING_SIZE; 4000 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4001 rmem->dma_arr = rxr->rx_desc_mapping; 4002 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4003 rmem->vmem = (void **)&rxr->rx_buf_ring; 4004 4005 ring = &rxr->rx_agg_ring_struct; 4006 rmem = &ring->ring_mem; 4007 rmem->nr_pages = bp->rx_agg_nr_pages; 4008 rmem->page_size = HW_RXBD_RING_SIZE; 4009 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4010 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4011 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4012 rmem->vmem = (void **)&rxr->rx_agg_ring; 4013 } 4014 4015 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4016 struct bnxt_rx_ring_info *rxr) 4017 { 4018 struct bnxt_ring_mem_info *rmem; 4019 struct bnxt_ring_struct *ring; 4020 int i; 4021 4022 rxr->page_pool->p.napi = NULL; 4023 rxr->page_pool = NULL; 4024 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4025 4026 ring = &rxr->rx_ring_struct; 4027 rmem = &ring->ring_mem; 4028 rmem->pg_tbl = NULL; 4029 rmem->pg_tbl_map = 0; 4030 for (i = 0; i < rmem->nr_pages; i++) { 4031 rmem->pg_arr[i] = NULL; 4032 rmem->dma_arr[i] = 0; 4033 } 4034 *rmem->vmem = NULL; 4035 4036 ring = &rxr->rx_agg_ring_struct; 4037 rmem = &ring->ring_mem; 4038 rmem->pg_tbl = NULL; 4039 rmem->pg_tbl_map = 0; 4040 for (i = 0; i < rmem->nr_pages; i++) { 4041 rmem->pg_arr[i] = NULL; 4042 rmem->dma_arr[i] = 0; 4043 } 4044 *rmem->vmem = NULL; 4045 } 4046 4047 static void bnxt_init_ring_struct(struct bnxt *bp) 4048 { 4049 int i, j; 4050 4051 for (i = 0; i < bp->cp_nr_rings; i++) { 4052 struct bnxt_napi *bnapi = bp->bnapi[i]; 4053 struct bnxt_ring_mem_info *rmem; 4054 struct bnxt_cp_ring_info *cpr; 4055 struct bnxt_rx_ring_info *rxr; 4056 struct bnxt_tx_ring_info *txr; 4057 struct bnxt_ring_struct *ring; 4058 4059 if (!bnapi) 4060 continue; 4061 4062 cpr = &bnapi->cp_ring; 4063 ring = &cpr->cp_ring_struct; 4064 rmem = &ring->ring_mem; 4065 rmem->nr_pages = bp->cp_nr_pages; 4066 rmem->page_size = HW_CMPD_RING_SIZE; 4067 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4068 rmem->dma_arr = cpr->cp_desc_mapping; 4069 rmem->vmem_size = 0; 4070 4071 rxr = bnapi->rx_ring; 4072 if (!rxr) 4073 goto skip_rx; 4074 4075 ring = &rxr->rx_ring_struct; 4076 rmem = &ring->ring_mem; 4077 rmem->nr_pages = bp->rx_nr_pages; 4078 rmem->page_size = HW_RXBD_RING_SIZE; 4079 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4080 rmem->dma_arr = rxr->rx_desc_mapping; 4081 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4082 rmem->vmem = (void **)&rxr->rx_buf_ring; 4083 4084 ring = &rxr->rx_agg_ring_struct; 4085 rmem = &ring->ring_mem; 4086 rmem->nr_pages = bp->rx_agg_nr_pages; 4087 rmem->page_size = HW_RXBD_RING_SIZE; 4088 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4089 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4090 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4091 rmem->vmem = (void **)&rxr->rx_agg_ring; 4092 4093 skip_rx: 4094 bnxt_for_each_napi_tx(j, bnapi, txr) { 4095 ring = &txr->tx_ring_struct; 4096 rmem = &ring->ring_mem; 4097 rmem->nr_pages = bp->tx_nr_pages; 4098 rmem->page_size = HW_TXBD_RING_SIZE; 4099 rmem->pg_arr = (void **)txr->tx_desc_ring; 4100 rmem->dma_arr = txr->tx_desc_mapping; 4101 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4102 rmem->vmem = (void **)&txr->tx_buf_ring; 4103 } 4104 } 4105 } 4106 4107 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4108 { 4109 int i; 4110 u32 prod; 4111 struct rx_bd **rx_buf_ring; 4112 4113 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4114 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4115 int j; 4116 struct rx_bd *rxbd; 4117 4118 rxbd = rx_buf_ring[i]; 4119 if (!rxbd) 4120 continue; 4121 4122 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4123 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4124 rxbd->rx_bd_opaque = prod; 4125 } 4126 } 4127 } 4128 4129 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4130 struct bnxt_rx_ring_info *rxr, 4131 int ring_nr) 4132 { 4133 u32 prod; 4134 int i; 4135 4136 prod = rxr->rx_prod; 4137 for (i = 0; i < bp->rx_ring_size; i++) { 4138 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4139 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4140 ring_nr, i, bp->rx_ring_size); 4141 break; 4142 } 4143 prod = NEXT_RX(prod); 4144 } 4145 rxr->rx_prod = prod; 4146 } 4147 4148 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp, 4149 struct bnxt_rx_ring_info *rxr, 4150 int ring_nr) 4151 { 4152 u32 prod; 4153 int i; 4154 4155 prod = rxr->rx_agg_prod; 4156 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4157 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) { 4158 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4159 ring_nr, i, bp->rx_ring_size); 4160 break; 4161 } 4162 prod = NEXT_RX_AGG(prod); 4163 } 4164 rxr->rx_agg_prod = prod; 4165 } 4166 4167 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4168 { 4169 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4170 int i; 4171 4172 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4173 4174 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4175 return 0; 4176 4177 bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr); 4178 4179 if (rxr->rx_tpa) { 4180 dma_addr_t mapping; 4181 u8 *data; 4182 4183 for (i = 0; i < bp->max_tpa; i++) { 4184 data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL); 4185 if (!data) 4186 return -ENOMEM; 4187 4188 rxr->rx_tpa[i].data = data; 4189 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4190 rxr->rx_tpa[i].mapping = mapping; 4191 } 4192 } 4193 return 0; 4194 } 4195 4196 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4197 struct bnxt_rx_ring_info *rxr) 4198 { 4199 struct bnxt_ring_struct *ring; 4200 u32 type; 4201 4202 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4203 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4204 4205 if (NET_IP_ALIGN == 2) 4206 type |= RX_BD_FLAGS_SOP; 4207 4208 ring = &rxr->rx_ring_struct; 4209 bnxt_init_rxbd_pages(ring, type); 4210 ring->fw_ring_id = INVALID_HW_RING_ID; 4211 } 4212 4213 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4214 struct bnxt_rx_ring_info *rxr) 4215 { 4216 struct bnxt_ring_struct *ring; 4217 u32 type; 4218 4219 ring = &rxr->rx_agg_ring_struct; 4220 ring->fw_ring_id = INVALID_HW_RING_ID; 4221 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4222 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4223 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4224 4225 bnxt_init_rxbd_pages(ring, type); 4226 } 4227 } 4228 4229 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4230 { 4231 struct bnxt_rx_ring_info *rxr; 4232 4233 rxr = &bp->rx_ring[ring_nr]; 4234 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4235 4236 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4237 &rxr->bnapi->napi); 4238 4239 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4240 bpf_prog_add(bp->xdp_prog, 1); 4241 rxr->xdp_prog = bp->xdp_prog; 4242 } 4243 4244 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4245 4246 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4247 } 4248 4249 static void bnxt_init_cp_rings(struct bnxt *bp) 4250 { 4251 int i, j; 4252 4253 for (i = 0; i < bp->cp_nr_rings; i++) { 4254 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4255 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4256 4257 ring->fw_ring_id = INVALID_HW_RING_ID; 4258 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4259 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4260 if (!cpr->cp_ring_arr) 4261 continue; 4262 for (j = 0; j < cpr->cp_ring_count; j++) { 4263 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4264 4265 ring = &cpr2->cp_ring_struct; 4266 ring->fw_ring_id = INVALID_HW_RING_ID; 4267 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4268 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4269 } 4270 } 4271 } 4272 4273 static int bnxt_init_rx_rings(struct bnxt *bp) 4274 { 4275 int i, rc = 0; 4276 4277 if (BNXT_RX_PAGE_MODE(bp)) { 4278 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4279 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4280 } else { 4281 bp->rx_offset = BNXT_RX_OFFSET; 4282 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4283 } 4284 4285 for (i = 0; i < bp->rx_nr_rings; i++) { 4286 rc = bnxt_init_one_rx_ring(bp, i); 4287 if (rc) 4288 break; 4289 } 4290 4291 return rc; 4292 } 4293 4294 static int bnxt_init_tx_rings(struct bnxt *bp) 4295 { 4296 u16 i; 4297 4298 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4299 BNXT_MIN_TX_DESC_CNT); 4300 4301 for (i = 0; i < bp->tx_nr_rings; i++) { 4302 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4303 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4304 4305 ring->fw_ring_id = INVALID_HW_RING_ID; 4306 4307 if (i >= bp->tx_nr_rings_xdp) 4308 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4309 NETDEV_QUEUE_TYPE_TX, 4310 &txr->bnapi->napi); 4311 } 4312 4313 return 0; 4314 } 4315 4316 static void bnxt_free_ring_grps(struct bnxt *bp) 4317 { 4318 kfree(bp->grp_info); 4319 bp->grp_info = NULL; 4320 } 4321 4322 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4323 { 4324 int i; 4325 4326 if (irq_re_init) { 4327 bp->grp_info = kcalloc(bp->cp_nr_rings, 4328 sizeof(struct bnxt_ring_grp_info), 4329 GFP_KERNEL); 4330 if (!bp->grp_info) 4331 return -ENOMEM; 4332 } 4333 for (i = 0; i < bp->cp_nr_rings; i++) { 4334 if (irq_re_init) 4335 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4336 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4337 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4338 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4339 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4340 } 4341 return 0; 4342 } 4343 4344 static void bnxt_free_vnics(struct bnxt *bp) 4345 { 4346 kfree(bp->vnic_info); 4347 bp->vnic_info = NULL; 4348 bp->nr_vnics = 0; 4349 } 4350 4351 static int bnxt_alloc_vnics(struct bnxt *bp) 4352 { 4353 int num_vnics = 1; 4354 4355 #ifdef CONFIG_RFS_ACCEL 4356 if (bp->flags & BNXT_FLAG_RFS) { 4357 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4358 num_vnics++; 4359 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4360 num_vnics += bp->rx_nr_rings; 4361 } 4362 #endif 4363 4364 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4365 num_vnics++; 4366 4367 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4368 GFP_KERNEL); 4369 if (!bp->vnic_info) 4370 return -ENOMEM; 4371 4372 bp->nr_vnics = num_vnics; 4373 return 0; 4374 } 4375 4376 static void bnxt_init_vnics(struct bnxt *bp) 4377 { 4378 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4379 int i; 4380 4381 for (i = 0; i < bp->nr_vnics; i++) { 4382 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4383 int j; 4384 4385 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4386 vnic->vnic_id = i; 4387 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4388 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4389 4390 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4391 4392 if (bp->vnic_info[i].rss_hash_key) { 4393 if (i == BNXT_VNIC_DEFAULT) { 4394 u8 *key = (void *)vnic->rss_hash_key; 4395 int k; 4396 4397 if (!bp->rss_hash_key_valid && 4398 !bp->rss_hash_key_updated) { 4399 get_random_bytes(bp->rss_hash_key, 4400 HW_HASH_KEY_SIZE); 4401 bp->rss_hash_key_updated = true; 4402 } 4403 4404 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4405 HW_HASH_KEY_SIZE); 4406 4407 if (!bp->rss_hash_key_updated) 4408 continue; 4409 4410 bp->rss_hash_key_updated = false; 4411 bp->rss_hash_key_valid = true; 4412 4413 bp->toeplitz_prefix = 0; 4414 for (k = 0; k < 8; k++) { 4415 bp->toeplitz_prefix <<= 8; 4416 bp->toeplitz_prefix |= key[k]; 4417 } 4418 } else { 4419 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4420 HW_HASH_KEY_SIZE); 4421 } 4422 } 4423 } 4424 } 4425 4426 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4427 { 4428 int pages; 4429 4430 pages = ring_size / desc_per_pg; 4431 4432 if (!pages) 4433 return 1; 4434 4435 pages++; 4436 4437 while (pages & (pages - 1)) 4438 pages++; 4439 4440 return pages; 4441 } 4442 4443 void bnxt_set_tpa_flags(struct bnxt *bp) 4444 { 4445 bp->flags &= ~BNXT_FLAG_TPA; 4446 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4447 return; 4448 if (bp->dev->features & NETIF_F_LRO) 4449 bp->flags |= BNXT_FLAG_LRO; 4450 else if (bp->dev->features & NETIF_F_GRO_HW) 4451 bp->flags |= BNXT_FLAG_GRO; 4452 } 4453 4454 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4455 * be set on entry. 4456 */ 4457 void bnxt_set_ring_params(struct bnxt *bp) 4458 { 4459 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4460 u32 agg_factor = 0, agg_ring_size = 0; 4461 4462 /* 8 for CRC and VLAN */ 4463 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4464 4465 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4466 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4467 4468 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH; 4469 ring_size = bp->rx_ring_size; 4470 bp->rx_agg_ring_size = 0; 4471 bp->rx_agg_nr_pages = 0; 4472 4473 if (bp->flags & BNXT_FLAG_TPA) 4474 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4475 4476 bp->flags &= ~BNXT_FLAG_JUMBO; 4477 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4478 u32 jumbo_factor; 4479 4480 bp->flags |= BNXT_FLAG_JUMBO; 4481 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4482 if (jumbo_factor > agg_factor) 4483 agg_factor = jumbo_factor; 4484 } 4485 if (agg_factor) { 4486 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4487 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4488 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4489 bp->rx_ring_size, ring_size); 4490 bp->rx_ring_size = ring_size; 4491 } 4492 agg_ring_size = ring_size * agg_factor; 4493 4494 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4495 RX_DESC_CNT); 4496 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4497 u32 tmp = agg_ring_size; 4498 4499 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4500 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4501 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4502 tmp, agg_ring_size); 4503 } 4504 bp->rx_agg_ring_size = agg_ring_size; 4505 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4506 4507 if (BNXT_RX_PAGE_MODE(bp)) { 4508 rx_space = PAGE_SIZE; 4509 rx_size = PAGE_SIZE - 4510 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4511 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4512 } else { 4513 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN); 4514 rx_space = rx_size + NET_SKB_PAD + 4515 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4516 } 4517 } 4518 4519 bp->rx_buf_use_size = rx_size; 4520 bp->rx_buf_size = rx_space; 4521 4522 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4523 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4524 4525 ring_size = bp->tx_ring_size; 4526 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4527 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4528 4529 max_rx_cmpl = bp->rx_ring_size; 4530 /* MAX TPA needs to be added because TPA_START completions are 4531 * immediately recycled, so the TPA completions are not bound by 4532 * the RX ring size. 4533 */ 4534 if (bp->flags & BNXT_FLAG_TPA) 4535 max_rx_cmpl += bp->max_tpa; 4536 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4537 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4538 bp->cp_ring_size = ring_size; 4539 4540 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4541 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4542 bp->cp_nr_pages = MAX_CP_PAGES; 4543 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4544 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4545 ring_size, bp->cp_ring_size); 4546 } 4547 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4548 bp->cp_ring_mask = bp->cp_bit - 1; 4549 } 4550 4551 /* Changing allocation mode of RX rings. 4552 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4553 */ 4554 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4555 { 4556 struct net_device *dev = bp->dev; 4557 4558 if (page_mode) { 4559 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 4560 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4561 4562 if (bp->xdp_prog->aux->xdp_has_frags) 4563 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4564 else 4565 dev->max_mtu = 4566 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4567 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4568 bp->flags |= BNXT_FLAG_JUMBO; 4569 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4570 } else { 4571 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4572 bp->rx_skb_func = bnxt_rx_page_skb; 4573 } 4574 bp->rx_dir = DMA_BIDIRECTIONAL; 4575 /* Disable LRO or GRO_HW */ 4576 netdev_update_features(dev); 4577 } else { 4578 dev->max_mtu = bp->max_mtu; 4579 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4580 bp->rx_dir = DMA_FROM_DEVICE; 4581 bp->rx_skb_func = bnxt_rx_skb; 4582 } 4583 return 0; 4584 } 4585 4586 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4587 { 4588 int i; 4589 struct bnxt_vnic_info *vnic; 4590 struct pci_dev *pdev = bp->pdev; 4591 4592 if (!bp->vnic_info) 4593 return; 4594 4595 for (i = 0; i < bp->nr_vnics; i++) { 4596 vnic = &bp->vnic_info[i]; 4597 4598 kfree(vnic->fw_grp_ids); 4599 vnic->fw_grp_ids = NULL; 4600 4601 kfree(vnic->uc_list); 4602 vnic->uc_list = NULL; 4603 4604 if (vnic->mc_list) { 4605 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4606 vnic->mc_list, vnic->mc_list_mapping); 4607 vnic->mc_list = NULL; 4608 } 4609 4610 if (vnic->rss_table) { 4611 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4612 vnic->rss_table, 4613 vnic->rss_table_dma_addr); 4614 vnic->rss_table = NULL; 4615 } 4616 4617 vnic->rss_hash_key = NULL; 4618 vnic->flags = 0; 4619 } 4620 } 4621 4622 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4623 { 4624 int i, rc = 0, size; 4625 struct bnxt_vnic_info *vnic; 4626 struct pci_dev *pdev = bp->pdev; 4627 int max_rings; 4628 4629 for (i = 0; i < bp->nr_vnics; i++) { 4630 vnic = &bp->vnic_info[i]; 4631 4632 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4633 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4634 4635 if (mem_size > 0) { 4636 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4637 if (!vnic->uc_list) { 4638 rc = -ENOMEM; 4639 goto out; 4640 } 4641 } 4642 } 4643 4644 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4645 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4646 vnic->mc_list = 4647 dma_alloc_coherent(&pdev->dev, 4648 vnic->mc_list_size, 4649 &vnic->mc_list_mapping, 4650 GFP_KERNEL); 4651 if (!vnic->mc_list) { 4652 rc = -ENOMEM; 4653 goto out; 4654 } 4655 } 4656 4657 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4658 goto vnic_skip_grps; 4659 4660 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4661 max_rings = bp->rx_nr_rings; 4662 else 4663 max_rings = 1; 4664 4665 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4666 if (!vnic->fw_grp_ids) { 4667 rc = -ENOMEM; 4668 goto out; 4669 } 4670 vnic_skip_grps: 4671 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4672 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4673 continue; 4674 4675 /* Allocate rss table and hash key */ 4676 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4677 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4678 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4679 4680 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4681 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4682 vnic->rss_table_size, 4683 &vnic->rss_table_dma_addr, 4684 GFP_KERNEL); 4685 if (!vnic->rss_table) { 4686 rc = -ENOMEM; 4687 goto out; 4688 } 4689 4690 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4691 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4692 } 4693 return 0; 4694 4695 out: 4696 return rc; 4697 } 4698 4699 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4700 { 4701 struct bnxt_hwrm_wait_token *token; 4702 4703 dma_pool_destroy(bp->hwrm_dma_pool); 4704 bp->hwrm_dma_pool = NULL; 4705 4706 rcu_read_lock(); 4707 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4708 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4709 rcu_read_unlock(); 4710 } 4711 4712 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 4713 { 4714 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 4715 BNXT_HWRM_DMA_SIZE, 4716 BNXT_HWRM_DMA_ALIGN, 0); 4717 if (!bp->hwrm_dma_pool) 4718 return -ENOMEM; 4719 4720 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4721 4722 return 0; 4723 } 4724 4725 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4726 { 4727 kfree(stats->hw_masks); 4728 stats->hw_masks = NULL; 4729 kfree(stats->sw_stats); 4730 stats->sw_stats = NULL; 4731 if (stats->hw_stats) { 4732 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 4733 stats->hw_stats_map); 4734 stats->hw_stats = NULL; 4735 } 4736 } 4737 4738 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 4739 bool alloc_masks) 4740 { 4741 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 4742 &stats->hw_stats_map, GFP_KERNEL); 4743 if (!stats->hw_stats) 4744 return -ENOMEM; 4745 4746 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 4747 if (!stats->sw_stats) 4748 goto stats_mem_err; 4749 4750 if (alloc_masks) { 4751 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 4752 if (!stats->hw_masks) 4753 goto stats_mem_err; 4754 } 4755 return 0; 4756 4757 stats_mem_err: 4758 bnxt_free_stats_mem(bp, stats); 4759 return -ENOMEM; 4760 } 4761 4762 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 4763 { 4764 int i; 4765 4766 for (i = 0; i < count; i++) 4767 mask_arr[i] = mask; 4768 } 4769 4770 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 4771 { 4772 int i; 4773 4774 for (i = 0; i < count; i++) 4775 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 4776 } 4777 4778 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 4779 struct bnxt_stats_mem *stats) 4780 { 4781 struct hwrm_func_qstats_ext_output *resp; 4782 struct hwrm_func_qstats_ext_input *req; 4783 __le64 *hw_masks; 4784 int rc; 4785 4786 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 4787 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4788 return -EOPNOTSUPP; 4789 4790 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 4791 if (rc) 4792 return rc; 4793 4794 req->fid = cpu_to_le16(0xffff); 4795 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4796 4797 resp = hwrm_req_hold(bp, req); 4798 rc = hwrm_req_send(bp, req); 4799 if (!rc) { 4800 hw_masks = &resp->rx_ucast_pkts; 4801 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 4802 } 4803 hwrm_req_drop(bp, req); 4804 return rc; 4805 } 4806 4807 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 4808 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 4809 4810 static void bnxt_init_stats(struct bnxt *bp) 4811 { 4812 struct bnxt_napi *bnapi = bp->bnapi[0]; 4813 struct bnxt_cp_ring_info *cpr; 4814 struct bnxt_stats_mem *stats; 4815 __le64 *rx_stats, *tx_stats; 4816 int rc, rx_count, tx_count; 4817 u64 *rx_masks, *tx_masks; 4818 u64 mask; 4819 u8 flags; 4820 4821 cpr = &bnapi->cp_ring; 4822 stats = &cpr->stats; 4823 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 4824 if (rc) { 4825 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4826 mask = (1ULL << 48) - 1; 4827 else 4828 mask = -1ULL; 4829 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 4830 } 4831 if (bp->flags & BNXT_FLAG_PORT_STATS) { 4832 stats = &bp->port_stats; 4833 rx_stats = stats->hw_stats; 4834 rx_masks = stats->hw_masks; 4835 rx_count = sizeof(struct rx_port_stats) / 8; 4836 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4837 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4838 tx_count = sizeof(struct tx_port_stats) / 8; 4839 4840 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 4841 rc = bnxt_hwrm_port_qstats(bp, flags); 4842 if (rc) { 4843 mask = (1ULL << 40) - 1; 4844 4845 bnxt_fill_masks(rx_masks, mask, rx_count); 4846 bnxt_fill_masks(tx_masks, mask, tx_count); 4847 } else { 4848 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4849 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 4850 bnxt_hwrm_port_qstats(bp, 0); 4851 } 4852 } 4853 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 4854 stats = &bp->rx_port_stats_ext; 4855 rx_stats = stats->hw_stats; 4856 rx_masks = stats->hw_masks; 4857 rx_count = sizeof(struct rx_port_stats_ext) / 8; 4858 stats = &bp->tx_port_stats_ext; 4859 tx_stats = stats->hw_stats; 4860 tx_masks = stats->hw_masks; 4861 tx_count = sizeof(struct tx_port_stats_ext) / 8; 4862 4863 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4864 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 4865 if (rc) { 4866 mask = (1ULL << 40) - 1; 4867 4868 bnxt_fill_masks(rx_masks, mask, rx_count); 4869 if (tx_stats) 4870 bnxt_fill_masks(tx_masks, mask, tx_count); 4871 } else { 4872 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4873 if (tx_stats) 4874 bnxt_copy_hw_masks(tx_masks, tx_stats, 4875 tx_count); 4876 bnxt_hwrm_port_qstats_ext(bp, 0); 4877 } 4878 } 4879 } 4880 4881 static void bnxt_free_port_stats(struct bnxt *bp) 4882 { 4883 bp->flags &= ~BNXT_FLAG_PORT_STATS; 4884 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 4885 4886 bnxt_free_stats_mem(bp, &bp->port_stats); 4887 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 4888 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 4889 } 4890 4891 static void bnxt_free_ring_stats(struct bnxt *bp) 4892 { 4893 int i; 4894 4895 if (!bp->bnapi) 4896 return; 4897 4898 for (i = 0; i < bp->cp_nr_rings; i++) { 4899 struct bnxt_napi *bnapi = bp->bnapi[i]; 4900 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4901 4902 bnxt_free_stats_mem(bp, &cpr->stats); 4903 4904 kfree(cpr->sw_stats); 4905 cpr->sw_stats = NULL; 4906 } 4907 } 4908 4909 static int bnxt_alloc_stats(struct bnxt *bp) 4910 { 4911 u32 size, i; 4912 int rc; 4913 4914 size = bp->hw_ring_stats_size; 4915 4916 for (i = 0; i < bp->cp_nr_rings; i++) { 4917 struct bnxt_napi *bnapi = bp->bnapi[i]; 4918 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4919 4920 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 4921 if (!cpr->sw_stats) 4922 return -ENOMEM; 4923 4924 cpr->stats.len = size; 4925 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 4926 if (rc) 4927 return rc; 4928 4929 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 4930 } 4931 4932 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 4933 return 0; 4934 4935 if (bp->port_stats.hw_stats) 4936 goto alloc_ext_stats; 4937 4938 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 4939 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 4940 if (rc) 4941 return rc; 4942 4943 bp->flags |= BNXT_FLAG_PORT_STATS; 4944 4945 alloc_ext_stats: 4946 /* Display extended statistics only if FW supports it */ 4947 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 4948 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 4949 return 0; 4950 4951 if (bp->rx_port_stats_ext.hw_stats) 4952 goto alloc_tx_ext_stats; 4953 4954 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 4955 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 4956 /* Extended stats are optional */ 4957 if (rc) 4958 return 0; 4959 4960 alloc_tx_ext_stats: 4961 if (bp->tx_port_stats_ext.hw_stats) 4962 return 0; 4963 4964 if (bp->hwrm_spec_code >= 0x10902 || 4965 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 4966 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 4967 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 4968 /* Extended stats are optional */ 4969 if (rc) 4970 return 0; 4971 } 4972 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 4973 return 0; 4974 } 4975 4976 static void bnxt_clear_ring_indices(struct bnxt *bp) 4977 { 4978 int i, j; 4979 4980 if (!bp->bnapi) 4981 return; 4982 4983 for (i = 0; i < bp->cp_nr_rings; i++) { 4984 struct bnxt_napi *bnapi = bp->bnapi[i]; 4985 struct bnxt_cp_ring_info *cpr; 4986 struct bnxt_rx_ring_info *rxr; 4987 struct bnxt_tx_ring_info *txr; 4988 4989 if (!bnapi) 4990 continue; 4991 4992 cpr = &bnapi->cp_ring; 4993 cpr->cp_raw_cons = 0; 4994 4995 bnxt_for_each_napi_tx(j, bnapi, txr) { 4996 txr->tx_prod = 0; 4997 txr->tx_cons = 0; 4998 txr->tx_hw_cons = 0; 4999 } 5000 5001 rxr = bnapi->rx_ring; 5002 if (rxr) { 5003 rxr->rx_prod = 0; 5004 rxr->rx_agg_prod = 0; 5005 rxr->rx_sw_agg_prod = 0; 5006 rxr->rx_next_cons = 0; 5007 } 5008 bnapi->events = 0; 5009 } 5010 } 5011 5012 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5013 { 5014 u8 type = fltr->type, flags = fltr->flags; 5015 5016 INIT_LIST_HEAD(&fltr->list); 5017 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5018 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5019 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5020 } 5021 5022 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5023 { 5024 if (!list_empty(&fltr->list)) 5025 list_del_init(&fltr->list); 5026 } 5027 5028 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5029 { 5030 struct bnxt_filter_base *usr_fltr, *tmp; 5031 5032 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5033 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5034 continue; 5035 bnxt_del_one_usr_fltr(bp, usr_fltr); 5036 } 5037 } 5038 5039 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5040 { 5041 hlist_del(&fltr->hash); 5042 bnxt_del_one_usr_fltr(bp, fltr); 5043 if (fltr->flags) { 5044 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5045 bp->ntp_fltr_count--; 5046 } 5047 kfree(fltr); 5048 } 5049 5050 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5051 { 5052 int i; 5053 5054 /* Under rtnl_lock and all our NAPIs have been disabled. It's 5055 * safe to delete the hash table. 5056 */ 5057 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5058 struct hlist_head *head; 5059 struct hlist_node *tmp; 5060 struct bnxt_ntuple_filter *fltr; 5061 5062 head = &bp->ntp_fltr_hash_tbl[i]; 5063 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5064 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5065 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5066 !list_empty(&fltr->base.list))) 5067 continue; 5068 bnxt_del_fltr(bp, &fltr->base); 5069 } 5070 } 5071 if (!all) 5072 return; 5073 5074 bitmap_free(bp->ntp_fltr_bmap); 5075 bp->ntp_fltr_bmap = NULL; 5076 bp->ntp_fltr_count = 0; 5077 } 5078 5079 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5080 { 5081 int i, rc = 0; 5082 5083 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5084 return 0; 5085 5086 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5087 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5088 5089 bp->ntp_fltr_count = 0; 5090 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5091 5092 if (!bp->ntp_fltr_bmap) 5093 rc = -ENOMEM; 5094 5095 return rc; 5096 } 5097 5098 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5099 { 5100 int i; 5101 5102 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5103 struct hlist_head *head; 5104 struct hlist_node *tmp; 5105 struct bnxt_l2_filter *fltr; 5106 5107 head = &bp->l2_fltr_hash_tbl[i]; 5108 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5109 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5110 !list_empty(&fltr->base.list))) 5111 continue; 5112 bnxt_del_fltr(bp, &fltr->base); 5113 } 5114 } 5115 } 5116 5117 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5118 { 5119 int i; 5120 5121 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5122 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5123 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5124 } 5125 5126 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5127 { 5128 bnxt_free_vnic_attributes(bp); 5129 bnxt_free_tx_rings(bp); 5130 bnxt_free_rx_rings(bp); 5131 bnxt_free_cp_rings(bp); 5132 bnxt_free_all_cp_arrays(bp); 5133 bnxt_free_ntp_fltrs(bp, false); 5134 bnxt_free_l2_filters(bp, false); 5135 if (irq_re_init) { 5136 bnxt_free_ring_stats(bp); 5137 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5138 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5139 bnxt_free_port_stats(bp); 5140 bnxt_free_ring_grps(bp); 5141 bnxt_free_vnics(bp); 5142 kfree(bp->tx_ring_map); 5143 bp->tx_ring_map = NULL; 5144 kfree(bp->tx_ring); 5145 bp->tx_ring = NULL; 5146 kfree(bp->rx_ring); 5147 bp->rx_ring = NULL; 5148 kfree(bp->bnapi); 5149 bp->bnapi = NULL; 5150 } else { 5151 bnxt_clear_ring_indices(bp); 5152 } 5153 } 5154 5155 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5156 { 5157 int i, j, rc, size, arr_size; 5158 void *bnapi; 5159 5160 if (irq_re_init) { 5161 /* Allocate bnapi mem pointer array and mem block for 5162 * all queues 5163 */ 5164 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5165 bp->cp_nr_rings); 5166 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5167 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5168 if (!bnapi) 5169 return -ENOMEM; 5170 5171 bp->bnapi = bnapi; 5172 bnapi += arr_size; 5173 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5174 bp->bnapi[i] = bnapi; 5175 bp->bnapi[i]->index = i; 5176 bp->bnapi[i]->bp = bp; 5177 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5178 struct bnxt_cp_ring_info *cpr = 5179 &bp->bnapi[i]->cp_ring; 5180 5181 cpr->cp_ring_struct.ring_mem.flags = 5182 BNXT_RMEM_RING_PTE_FLAG; 5183 } 5184 } 5185 5186 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5187 sizeof(struct bnxt_rx_ring_info), 5188 GFP_KERNEL); 5189 if (!bp->rx_ring) 5190 return -ENOMEM; 5191 5192 for (i = 0; i < bp->rx_nr_rings; i++) { 5193 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5194 5195 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5196 rxr->rx_ring_struct.ring_mem.flags = 5197 BNXT_RMEM_RING_PTE_FLAG; 5198 rxr->rx_agg_ring_struct.ring_mem.flags = 5199 BNXT_RMEM_RING_PTE_FLAG; 5200 } else { 5201 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5202 } 5203 rxr->bnapi = bp->bnapi[i]; 5204 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5205 } 5206 5207 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5208 sizeof(struct bnxt_tx_ring_info), 5209 GFP_KERNEL); 5210 if (!bp->tx_ring) 5211 return -ENOMEM; 5212 5213 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5214 GFP_KERNEL); 5215 5216 if (!bp->tx_ring_map) 5217 return -ENOMEM; 5218 5219 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5220 j = 0; 5221 else 5222 j = bp->rx_nr_rings; 5223 5224 for (i = 0; i < bp->tx_nr_rings; i++) { 5225 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5226 struct bnxt_napi *bnapi2; 5227 5228 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5229 txr->tx_ring_struct.ring_mem.flags = 5230 BNXT_RMEM_RING_PTE_FLAG; 5231 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5232 if (i >= bp->tx_nr_rings_xdp) { 5233 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5234 5235 bnapi2 = bp->bnapi[k]; 5236 txr->txq_index = i - bp->tx_nr_rings_xdp; 5237 txr->tx_napi_idx = 5238 BNXT_RING_TO_TC(bp, txr->txq_index); 5239 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5240 bnapi2->tx_int = bnxt_tx_int; 5241 } else { 5242 bnapi2 = bp->bnapi[j]; 5243 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5244 bnapi2->tx_ring[0] = txr; 5245 bnapi2->tx_int = bnxt_tx_int_xdp; 5246 j++; 5247 } 5248 txr->bnapi = bnapi2; 5249 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5250 txr->tx_cpr = &bnapi2->cp_ring; 5251 } 5252 5253 rc = bnxt_alloc_stats(bp); 5254 if (rc) 5255 goto alloc_mem_err; 5256 bnxt_init_stats(bp); 5257 5258 rc = bnxt_alloc_ntp_fltrs(bp); 5259 if (rc) 5260 goto alloc_mem_err; 5261 5262 rc = bnxt_alloc_vnics(bp); 5263 if (rc) 5264 goto alloc_mem_err; 5265 } 5266 5267 rc = bnxt_alloc_all_cp_arrays(bp); 5268 if (rc) 5269 goto alloc_mem_err; 5270 5271 bnxt_init_ring_struct(bp); 5272 5273 rc = bnxt_alloc_rx_rings(bp); 5274 if (rc) 5275 goto alloc_mem_err; 5276 5277 rc = bnxt_alloc_tx_rings(bp); 5278 if (rc) 5279 goto alloc_mem_err; 5280 5281 rc = bnxt_alloc_cp_rings(bp); 5282 if (rc) 5283 goto alloc_mem_err; 5284 5285 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5286 BNXT_VNIC_MCAST_FLAG | 5287 BNXT_VNIC_UCAST_FLAG; 5288 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5289 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5290 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5291 5292 rc = bnxt_alloc_vnic_attributes(bp); 5293 if (rc) 5294 goto alloc_mem_err; 5295 return 0; 5296 5297 alloc_mem_err: 5298 bnxt_free_mem(bp, true); 5299 return rc; 5300 } 5301 5302 static void bnxt_disable_int(struct bnxt *bp) 5303 { 5304 int i; 5305 5306 if (!bp->bnapi) 5307 return; 5308 5309 for (i = 0; i < bp->cp_nr_rings; i++) { 5310 struct bnxt_napi *bnapi = bp->bnapi[i]; 5311 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5312 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5313 5314 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5315 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5316 } 5317 } 5318 5319 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5320 { 5321 struct bnxt_napi *bnapi = bp->bnapi[n]; 5322 struct bnxt_cp_ring_info *cpr; 5323 5324 cpr = &bnapi->cp_ring; 5325 return cpr->cp_ring_struct.map_idx; 5326 } 5327 5328 static void bnxt_disable_int_sync(struct bnxt *bp) 5329 { 5330 int i; 5331 5332 if (!bp->irq_tbl) 5333 return; 5334 5335 atomic_inc(&bp->intr_sem); 5336 5337 bnxt_disable_int(bp); 5338 for (i = 0; i < bp->cp_nr_rings; i++) { 5339 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5340 5341 synchronize_irq(bp->irq_tbl[map_idx].vector); 5342 } 5343 } 5344 5345 static void bnxt_enable_int(struct bnxt *bp) 5346 { 5347 int i; 5348 5349 atomic_set(&bp->intr_sem, 0); 5350 for (i = 0; i < bp->cp_nr_rings; i++) { 5351 struct bnxt_napi *bnapi = bp->bnapi[i]; 5352 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5353 5354 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5355 } 5356 } 5357 5358 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5359 bool async_only) 5360 { 5361 DECLARE_BITMAP(async_events_bmap, 256); 5362 u32 *events = (u32 *)async_events_bmap; 5363 struct hwrm_func_drv_rgtr_output *resp; 5364 struct hwrm_func_drv_rgtr_input *req; 5365 u32 flags; 5366 int rc, i; 5367 5368 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5369 if (rc) 5370 return rc; 5371 5372 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5373 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5374 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5375 5376 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5377 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5378 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5379 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5380 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5381 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5382 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5383 req->flags = cpu_to_le32(flags); 5384 req->ver_maj_8b = DRV_VER_MAJ; 5385 req->ver_min_8b = DRV_VER_MIN; 5386 req->ver_upd_8b = DRV_VER_UPD; 5387 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5388 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5389 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5390 5391 if (BNXT_PF(bp)) { 5392 u32 data[8]; 5393 int i; 5394 5395 memset(data, 0, sizeof(data)); 5396 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5397 u16 cmd = bnxt_vf_req_snif[i]; 5398 unsigned int bit, idx; 5399 5400 idx = cmd / 32; 5401 bit = cmd % 32; 5402 data[idx] |= 1 << bit; 5403 } 5404 5405 for (i = 0; i < 8; i++) 5406 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5407 5408 req->enables |= 5409 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5410 } 5411 5412 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5413 req->flags |= cpu_to_le32( 5414 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5415 5416 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5417 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5418 u16 event_id = bnxt_async_events_arr[i]; 5419 5420 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5421 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5422 continue; 5423 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5424 !bp->ptp_cfg) 5425 continue; 5426 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5427 } 5428 if (bmap && bmap_size) { 5429 for (i = 0; i < bmap_size; i++) { 5430 if (test_bit(i, bmap)) 5431 __set_bit(i, async_events_bmap); 5432 } 5433 } 5434 for (i = 0; i < 8; i++) 5435 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5436 5437 if (async_only) 5438 req->enables = 5439 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5440 5441 resp = hwrm_req_hold(bp, req); 5442 rc = hwrm_req_send(bp, req); 5443 if (!rc) { 5444 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5445 if (resp->flags & 5446 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5447 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5448 } 5449 hwrm_req_drop(bp, req); 5450 return rc; 5451 } 5452 5453 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5454 { 5455 struct hwrm_func_drv_unrgtr_input *req; 5456 int rc; 5457 5458 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5459 return 0; 5460 5461 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5462 if (rc) 5463 return rc; 5464 return hwrm_req_send(bp, req); 5465 } 5466 5467 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5468 5469 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5470 { 5471 struct hwrm_tunnel_dst_port_free_input *req; 5472 int rc; 5473 5474 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5475 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5476 return 0; 5477 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5478 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5479 return 0; 5480 5481 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5482 if (rc) 5483 return rc; 5484 5485 req->tunnel_type = tunnel_type; 5486 5487 switch (tunnel_type) { 5488 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5489 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5490 bp->vxlan_port = 0; 5491 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5492 break; 5493 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5494 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5495 bp->nge_port = 0; 5496 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5497 break; 5498 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5499 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5500 bp->vxlan_gpe_port = 0; 5501 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5502 break; 5503 default: 5504 break; 5505 } 5506 5507 rc = hwrm_req_send(bp, req); 5508 if (rc) 5509 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5510 rc); 5511 if (bp->flags & BNXT_FLAG_TPA) 5512 bnxt_set_tpa(bp, true); 5513 return rc; 5514 } 5515 5516 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5517 u8 tunnel_type) 5518 { 5519 struct hwrm_tunnel_dst_port_alloc_output *resp; 5520 struct hwrm_tunnel_dst_port_alloc_input *req; 5521 int rc; 5522 5523 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5524 if (rc) 5525 return rc; 5526 5527 req->tunnel_type = tunnel_type; 5528 req->tunnel_dst_port_val = port; 5529 5530 resp = hwrm_req_hold(bp, req); 5531 rc = hwrm_req_send(bp, req); 5532 if (rc) { 5533 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5534 rc); 5535 goto err_out; 5536 } 5537 5538 switch (tunnel_type) { 5539 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5540 bp->vxlan_port = port; 5541 bp->vxlan_fw_dst_port_id = 5542 le16_to_cpu(resp->tunnel_dst_port_id); 5543 break; 5544 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5545 bp->nge_port = port; 5546 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5547 break; 5548 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5549 bp->vxlan_gpe_port = port; 5550 bp->vxlan_gpe_fw_dst_port_id = 5551 le16_to_cpu(resp->tunnel_dst_port_id); 5552 break; 5553 default: 5554 break; 5555 } 5556 if (bp->flags & BNXT_FLAG_TPA) 5557 bnxt_set_tpa(bp, true); 5558 5559 err_out: 5560 hwrm_req_drop(bp, req); 5561 return rc; 5562 } 5563 5564 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5565 { 5566 struct hwrm_cfa_l2_set_rx_mask_input *req; 5567 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5568 int rc; 5569 5570 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5571 if (rc) 5572 return rc; 5573 5574 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5575 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5576 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5577 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5578 } 5579 req->mask = cpu_to_le32(vnic->rx_mask); 5580 return hwrm_req_send_silent(bp, req); 5581 } 5582 5583 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5584 { 5585 if (!atomic_dec_and_test(&fltr->refcnt)) 5586 return; 5587 spin_lock_bh(&bp->ntp_fltr_lock); 5588 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5589 spin_unlock_bh(&bp->ntp_fltr_lock); 5590 return; 5591 } 5592 hlist_del_rcu(&fltr->base.hash); 5593 bnxt_del_one_usr_fltr(bp, &fltr->base); 5594 if (fltr->base.flags) { 5595 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5596 bp->ntp_fltr_count--; 5597 } 5598 spin_unlock_bh(&bp->ntp_fltr_lock); 5599 kfree_rcu(fltr, base.rcu); 5600 } 5601 5602 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5603 struct bnxt_l2_key *key, 5604 u32 idx) 5605 { 5606 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5607 struct bnxt_l2_filter *fltr; 5608 5609 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5610 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5611 5612 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5613 l2_key->vlan == key->vlan) 5614 return fltr; 5615 } 5616 return NULL; 5617 } 5618 5619 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5620 struct bnxt_l2_key *key, 5621 u32 idx) 5622 { 5623 struct bnxt_l2_filter *fltr = NULL; 5624 5625 rcu_read_lock(); 5626 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5627 if (fltr) 5628 atomic_inc(&fltr->refcnt); 5629 rcu_read_unlock(); 5630 return fltr; 5631 } 5632 5633 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5634 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5635 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5636 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5637 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5638 5639 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5640 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5641 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5642 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5643 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5644 5645 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5646 { 5647 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5648 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5649 return sizeof(fkeys->addrs.v4addrs) + 5650 sizeof(fkeys->ports); 5651 5652 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5653 return sizeof(fkeys->addrs.v4addrs); 5654 } 5655 5656 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5657 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5658 return sizeof(fkeys->addrs.v6addrs) + 5659 sizeof(fkeys->ports); 5660 5661 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5662 return sizeof(fkeys->addrs.v6addrs); 5663 } 5664 5665 return 0; 5666 } 5667 5668 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5669 const unsigned char *key) 5670 { 5671 u64 prefix = bp->toeplitz_prefix, hash = 0; 5672 struct bnxt_ipv4_tuple tuple4; 5673 struct bnxt_ipv6_tuple tuple6; 5674 int i, j, len = 0; 5675 u8 *four_tuple; 5676 5677 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5678 if (!len) 5679 return 0; 5680 5681 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5682 tuple4.v4addrs = fkeys->addrs.v4addrs; 5683 tuple4.ports = fkeys->ports; 5684 four_tuple = (unsigned char *)&tuple4; 5685 } else { 5686 tuple6.v6addrs = fkeys->addrs.v6addrs; 5687 tuple6.ports = fkeys->ports; 5688 four_tuple = (unsigned char *)&tuple6; 5689 } 5690 5691 for (i = 0, j = 8; i < len; i++, j++) { 5692 u8 byte = four_tuple[i]; 5693 int bit; 5694 5695 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5696 if (byte & 0x80) 5697 hash ^= prefix; 5698 } 5699 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5700 } 5701 5702 /* The valid part of the hash is in the upper 32 bits. */ 5703 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5704 } 5705 5706 #ifdef CONFIG_RFS_ACCEL 5707 static struct bnxt_l2_filter * 5708 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 5709 { 5710 struct bnxt_l2_filter *fltr; 5711 u32 idx; 5712 5713 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5714 BNXT_L2_FLTR_HASH_MASK; 5715 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5716 return fltr; 5717 } 5718 #endif 5719 5720 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 5721 struct bnxt_l2_key *key, u32 idx) 5722 { 5723 struct hlist_head *head; 5724 5725 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 5726 fltr->l2_key.vlan = key->vlan; 5727 fltr->base.type = BNXT_FLTR_TYPE_L2; 5728 if (fltr->base.flags) { 5729 int bit_id; 5730 5731 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 5732 bp->max_fltr, 0); 5733 if (bit_id < 0) 5734 return -ENOMEM; 5735 fltr->base.sw_id = (u16)bit_id; 5736 bp->ntp_fltr_count++; 5737 } 5738 head = &bp->l2_fltr_hash_tbl[idx]; 5739 hlist_add_head_rcu(&fltr->base.hash, head); 5740 bnxt_insert_usr_fltr(bp, &fltr->base); 5741 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 5742 atomic_set(&fltr->refcnt, 1); 5743 return 0; 5744 } 5745 5746 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 5747 struct bnxt_l2_key *key, 5748 gfp_t gfp) 5749 { 5750 struct bnxt_l2_filter *fltr; 5751 u32 idx; 5752 int rc; 5753 5754 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5755 BNXT_L2_FLTR_HASH_MASK; 5756 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5757 if (fltr) 5758 return fltr; 5759 5760 fltr = kzalloc(sizeof(*fltr), gfp); 5761 if (!fltr) 5762 return ERR_PTR(-ENOMEM); 5763 spin_lock_bh(&bp->ntp_fltr_lock); 5764 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5765 spin_unlock_bh(&bp->ntp_fltr_lock); 5766 if (rc) { 5767 bnxt_del_l2_filter(bp, fltr); 5768 fltr = ERR_PTR(rc); 5769 } 5770 return fltr; 5771 } 5772 5773 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 5774 struct bnxt_l2_key *key, 5775 u16 flags) 5776 { 5777 struct bnxt_l2_filter *fltr; 5778 u32 idx; 5779 int rc; 5780 5781 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5782 BNXT_L2_FLTR_HASH_MASK; 5783 spin_lock_bh(&bp->ntp_fltr_lock); 5784 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5785 if (fltr) { 5786 fltr = ERR_PTR(-EEXIST); 5787 goto l2_filter_exit; 5788 } 5789 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 5790 if (!fltr) { 5791 fltr = ERR_PTR(-ENOMEM); 5792 goto l2_filter_exit; 5793 } 5794 fltr->base.flags = flags; 5795 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5796 if (rc) { 5797 spin_unlock_bh(&bp->ntp_fltr_lock); 5798 bnxt_del_l2_filter(bp, fltr); 5799 return ERR_PTR(rc); 5800 } 5801 5802 l2_filter_exit: 5803 spin_unlock_bh(&bp->ntp_fltr_lock); 5804 return fltr; 5805 } 5806 5807 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 5808 { 5809 #ifdef CONFIG_BNXT_SRIOV 5810 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 5811 5812 return vf->fw_fid; 5813 #else 5814 return INVALID_HW_RING_ID; 5815 #endif 5816 } 5817 5818 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5819 { 5820 struct hwrm_cfa_l2_filter_free_input *req; 5821 u16 target_id = 0xffff; 5822 int rc; 5823 5824 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 5825 struct bnxt_pf_info *pf = &bp->pf; 5826 5827 if (fltr->base.vf_idx >= pf->active_vfs) 5828 return -EINVAL; 5829 5830 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 5831 if (target_id == INVALID_HW_RING_ID) 5832 return -EINVAL; 5833 } 5834 5835 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 5836 if (rc) 5837 return rc; 5838 5839 req->target_id = cpu_to_le16(target_id); 5840 req->l2_filter_id = fltr->base.filter_id; 5841 return hwrm_req_send(bp, req); 5842 } 5843 5844 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5845 { 5846 struct hwrm_cfa_l2_filter_alloc_output *resp; 5847 struct hwrm_cfa_l2_filter_alloc_input *req; 5848 u16 target_id = 0xffff; 5849 int rc; 5850 5851 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 5852 struct bnxt_pf_info *pf = &bp->pf; 5853 5854 if (fltr->base.vf_idx >= pf->active_vfs) 5855 return -EINVAL; 5856 5857 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 5858 } 5859 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 5860 if (rc) 5861 return rc; 5862 5863 req->target_id = cpu_to_le16(target_id); 5864 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 5865 5866 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 5867 req->flags |= 5868 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 5869 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 5870 req->enables = 5871 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 5872 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 5873 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 5874 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 5875 eth_broadcast_addr(req->l2_addr_mask); 5876 5877 if (fltr->l2_key.vlan) { 5878 req->enables |= 5879 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 5880 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 5881 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 5882 req->num_vlans = 1; 5883 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 5884 req->l2_ivlan_mask = cpu_to_le16(0xfff); 5885 } 5886 5887 resp = hwrm_req_hold(bp, req); 5888 rc = hwrm_req_send(bp, req); 5889 if (!rc) { 5890 fltr->base.filter_id = resp->l2_filter_id; 5891 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 5892 } 5893 hwrm_req_drop(bp, req); 5894 return rc; 5895 } 5896 5897 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 5898 struct bnxt_ntuple_filter *fltr) 5899 { 5900 struct hwrm_cfa_ntuple_filter_free_input *req; 5901 int rc; 5902 5903 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 5904 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 5905 if (rc) 5906 return rc; 5907 5908 req->ntuple_filter_id = fltr->base.filter_id; 5909 return hwrm_req_send(bp, req); 5910 } 5911 5912 #define BNXT_NTP_FLTR_FLAGS \ 5913 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 5914 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 5915 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 5916 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 5917 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 5918 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 5919 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 5920 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 5921 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 5922 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 5923 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 5924 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 5925 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 5926 5927 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 5928 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 5929 5930 void bnxt_fill_ipv6_mask(__be32 mask[4]) 5931 { 5932 int i; 5933 5934 for (i = 0; i < 4; i++) 5935 mask[i] = cpu_to_be32(~0); 5936 } 5937 5938 static void 5939 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 5940 struct hwrm_cfa_ntuple_filter_alloc_input *req, 5941 struct bnxt_ntuple_filter *fltr) 5942 { 5943 u16 rxq = fltr->base.rxq; 5944 5945 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 5946 struct ethtool_rxfh_context *ctx; 5947 struct bnxt_rss_ctx *rss_ctx; 5948 struct bnxt_vnic_info *vnic; 5949 5950 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 5951 fltr->base.fw_vnic_id); 5952 if (ctx) { 5953 rss_ctx = ethtool_rxfh_context_priv(ctx); 5954 vnic = &rss_ctx->vnic; 5955 5956 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 5957 } 5958 return; 5959 } 5960 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 5961 struct bnxt_vnic_info *vnic; 5962 u32 enables; 5963 5964 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 5965 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 5966 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 5967 req->enables |= cpu_to_le32(enables); 5968 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 5969 } else { 5970 u32 flags; 5971 5972 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 5973 req->flags |= cpu_to_le32(flags); 5974 req->dst_id = cpu_to_le16(rxq); 5975 } 5976 } 5977 5978 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 5979 struct bnxt_ntuple_filter *fltr) 5980 { 5981 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 5982 struct hwrm_cfa_ntuple_filter_alloc_input *req; 5983 struct bnxt_flow_masks *masks = &fltr->fmasks; 5984 struct flow_keys *keys = &fltr->fkeys; 5985 struct bnxt_l2_filter *l2_fltr; 5986 struct bnxt_vnic_info *vnic; 5987 int rc; 5988 5989 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 5990 if (rc) 5991 return rc; 5992 5993 l2_fltr = fltr->l2_fltr; 5994 req->l2_filter_id = l2_fltr->base.filter_id; 5995 5996 if (fltr->base.flags & BNXT_ACT_DROP) { 5997 req->flags = 5998 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 5999 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6000 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6001 } else { 6002 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6003 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6004 } 6005 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6006 6007 req->ethertype = htons(ETH_P_IP); 6008 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6009 req->ip_protocol = keys->basic.ip_proto; 6010 6011 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6012 req->ethertype = htons(ETH_P_IPV6); 6013 req->ip_addr_type = 6014 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6015 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6016 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6017 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6018 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6019 } else { 6020 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6021 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6022 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6023 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6024 } 6025 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6026 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6027 req->tunnel_type = 6028 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6029 } 6030 6031 req->src_port = keys->ports.src; 6032 req->src_port_mask = masks->ports.src; 6033 req->dst_port = keys->ports.dst; 6034 req->dst_port_mask = masks->ports.dst; 6035 6036 resp = hwrm_req_hold(bp, req); 6037 rc = hwrm_req_send(bp, req); 6038 if (!rc) 6039 fltr->base.filter_id = resp->ntuple_filter_id; 6040 hwrm_req_drop(bp, req); 6041 return rc; 6042 } 6043 6044 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6045 const u8 *mac_addr) 6046 { 6047 struct bnxt_l2_filter *fltr; 6048 struct bnxt_l2_key key; 6049 int rc; 6050 6051 ether_addr_copy(key.dst_mac_addr, mac_addr); 6052 key.vlan = 0; 6053 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6054 if (IS_ERR(fltr)) 6055 return PTR_ERR(fltr); 6056 6057 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6058 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6059 if (rc) 6060 bnxt_del_l2_filter(bp, fltr); 6061 else 6062 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6063 return rc; 6064 } 6065 6066 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6067 { 6068 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6069 6070 /* Any associated ntuple filters will also be cleared by firmware. */ 6071 for (i = 0; i < num_of_vnics; i++) { 6072 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6073 6074 for (j = 0; j < vnic->uc_filter_count; j++) { 6075 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6076 6077 bnxt_hwrm_l2_filter_free(bp, fltr); 6078 bnxt_del_l2_filter(bp, fltr); 6079 } 6080 vnic->uc_filter_count = 0; 6081 } 6082 } 6083 6084 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6085 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6086 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6087 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6088 6089 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6090 struct hwrm_vnic_tpa_cfg_input *req) 6091 { 6092 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6093 6094 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6095 return; 6096 6097 if (bp->vxlan_port) 6098 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6099 if (bp->vxlan_gpe_port) 6100 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6101 if (bp->nge_port) 6102 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6103 6104 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6105 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6106 } 6107 6108 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6109 u32 tpa_flags) 6110 { 6111 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6112 struct hwrm_vnic_tpa_cfg_input *req; 6113 int rc; 6114 6115 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6116 return 0; 6117 6118 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6119 if (rc) 6120 return rc; 6121 6122 if (tpa_flags) { 6123 u16 mss = bp->dev->mtu - 40; 6124 u32 nsegs, n, segs = 0, flags; 6125 6126 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6127 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6128 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6129 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6130 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6131 if (tpa_flags & BNXT_FLAG_GRO) 6132 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6133 6134 req->flags = cpu_to_le32(flags); 6135 6136 req->enables = 6137 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6138 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6139 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6140 6141 /* Number of segs are log2 units, and first packet is not 6142 * included as part of this units. 6143 */ 6144 if (mss <= BNXT_RX_PAGE_SIZE) { 6145 n = BNXT_RX_PAGE_SIZE / mss; 6146 nsegs = (MAX_SKB_FRAGS - 1) * n; 6147 } else { 6148 n = mss / BNXT_RX_PAGE_SIZE; 6149 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6150 n++; 6151 nsegs = (MAX_SKB_FRAGS - n) / n; 6152 } 6153 6154 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6155 segs = MAX_TPA_SEGS_P5; 6156 max_aggs = bp->max_tpa; 6157 } else { 6158 segs = ilog2(nsegs); 6159 } 6160 req->max_agg_segs = cpu_to_le16(segs); 6161 req->max_aggs = cpu_to_le16(max_aggs); 6162 6163 req->min_agg_len = cpu_to_le32(512); 6164 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6165 } 6166 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6167 6168 return hwrm_req_send(bp, req); 6169 } 6170 6171 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6172 { 6173 struct bnxt_ring_grp_info *grp_info; 6174 6175 grp_info = &bp->grp_info[ring->grp_idx]; 6176 return grp_info->cp_fw_ring_id; 6177 } 6178 6179 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6180 { 6181 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6182 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6183 else 6184 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6185 } 6186 6187 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6188 { 6189 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6190 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6191 else 6192 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6193 } 6194 6195 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6196 { 6197 int entries; 6198 6199 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6200 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6201 else 6202 entries = HW_HASH_INDEX_SIZE; 6203 6204 bp->rss_indir_tbl_entries = entries; 6205 bp->rss_indir_tbl = 6206 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6207 if (!bp->rss_indir_tbl) 6208 return -ENOMEM; 6209 6210 return 0; 6211 } 6212 6213 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6214 struct ethtool_rxfh_context *rss_ctx) 6215 { 6216 u16 max_rings, max_entries, pad, i; 6217 u32 *rss_indir_tbl; 6218 6219 if (!bp->rx_nr_rings) 6220 return; 6221 6222 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6223 max_rings = bp->rx_nr_rings - 1; 6224 else 6225 max_rings = bp->rx_nr_rings; 6226 6227 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6228 if (rss_ctx) 6229 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6230 else 6231 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6232 6233 for (i = 0; i < max_entries; i++) 6234 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6235 6236 pad = bp->rss_indir_tbl_entries - max_entries; 6237 if (pad) 6238 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6239 } 6240 6241 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6242 { 6243 u32 i, tbl_size, max_ring = 0; 6244 6245 if (!bp->rss_indir_tbl) 6246 return 0; 6247 6248 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6249 for (i = 0; i < tbl_size; i++) 6250 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6251 return max_ring; 6252 } 6253 6254 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6255 { 6256 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6257 if (!rx_rings) 6258 return 0; 6259 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6260 BNXT_RSS_TABLE_ENTRIES_P5); 6261 } 6262 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6263 return 2; 6264 return 1; 6265 } 6266 6267 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6268 { 6269 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6270 u16 i, j; 6271 6272 /* Fill the RSS indirection table with ring group ids */ 6273 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6274 if (!no_rss) 6275 j = bp->rss_indir_tbl[i]; 6276 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6277 } 6278 } 6279 6280 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6281 struct bnxt_vnic_info *vnic) 6282 { 6283 __le16 *ring_tbl = vnic->rss_table; 6284 struct bnxt_rx_ring_info *rxr; 6285 u16 tbl_size, i; 6286 6287 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6288 6289 for (i = 0; i < tbl_size; i++) { 6290 u16 ring_id, j; 6291 6292 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6293 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6294 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6295 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6296 else 6297 j = bp->rss_indir_tbl[i]; 6298 rxr = &bp->rx_ring[j]; 6299 6300 ring_id = rxr->rx_ring_struct.fw_ring_id; 6301 *ring_tbl++ = cpu_to_le16(ring_id); 6302 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6303 *ring_tbl++ = cpu_to_le16(ring_id); 6304 } 6305 } 6306 6307 static void 6308 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6309 struct bnxt_vnic_info *vnic) 6310 { 6311 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6312 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6313 if (bp->flags & BNXT_FLAG_CHIP_P7) 6314 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6315 } else { 6316 bnxt_fill_hw_rss_tbl(bp, vnic); 6317 } 6318 6319 if (bp->rss_hash_delta) { 6320 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6321 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6322 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6323 else 6324 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6325 } else { 6326 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6327 } 6328 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6329 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6330 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6331 } 6332 6333 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6334 bool set_rss) 6335 { 6336 struct hwrm_vnic_rss_cfg_input *req; 6337 int rc; 6338 6339 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6340 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6341 return 0; 6342 6343 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6344 if (rc) 6345 return rc; 6346 6347 if (set_rss) 6348 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6349 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6350 return hwrm_req_send(bp, req); 6351 } 6352 6353 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6354 struct bnxt_vnic_info *vnic, bool set_rss) 6355 { 6356 struct hwrm_vnic_rss_cfg_input *req; 6357 dma_addr_t ring_tbl_map; 6358 u32 i, nr_ctxs; 6359 int rc; 6360 6361 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6362 if (rc) 6363 return rc; 6364 6365 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6366 if (!set_rss) 6367 return hwrm_req_send(bp, req); 6368 6369 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6370 ring_tbl_map = vnic->rss_table_dma_addr; 6371 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6372 6373 hwrm_req_hold(bp, req); 6374 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6375 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6376 req->ring_table_pair_index = i; 6377 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6378 rc = hwrm_req_send(bp, req); 6379 if (rc) 6380 goto exit; 6381 } 6382 6383 exit: 6384 hwrm_req_drop(bp, req); 6385 return rc; 6386 } 6387 6388 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6389 { 6390 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6391 struct hwrm_vnic_rss_qcfg_output *resp; 6392 struct hwrm_vnic_rss_qcfg_input *req; 6393 6394 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6395 return; 6396 6397 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6398 /* all contexts configured to same hash_type, zero always exists */ 6399 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6400 resp = hwrm_req_hold(bp, req); 6401 if (!hwrm_req_send(bp, req)) { 6402 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6403 bp->rss_hash_delta = 0; 6404 } 6405 hwrm_req_drop(bp, req); 6406 } 6407 6408 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6409 { 6410 struct hwrm_vnic_plcmodes_cfg_input *req; 6411 int rc; 6412 6413 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6414 if (rc) 6415 return rc; 6416 6417 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6418 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6419 6420 if (BNXT_RX_PAGE_MODE(bp)) { 6421 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6422 } else { 6423 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6424 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6425 req->enables |= 6426 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6427 req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh); 6428 req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh); 6429 } 6430 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6431 return hwrm_req_send(bp, req); 6432 } 6433 6434 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6435 struct bnxt_vnic_info *vnic, 6436 u16 ctx_idx) 6437 { 6438 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6439 6440 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6441 return; 6442 6443 req->rss_cos_lb_ctx_id = 6444 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6445 6446 hwrm_req_send(bp, req); 6447 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6448 } 6449 6450 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6451 { 6452 int i, j; 6453 6454 for (i = 0; i < bp->nr_vnics; i++) { 6455 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6456 6457 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6458 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6459 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6460 } 6461 } 6462 bp->rsscos_nr_ctxs = 0; 6463 } 6464 6465 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6466 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6467 { 6468 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6469 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6470 int rc; 6471 6472 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6473 if (rc) 6474 return rc; 6475 6476 resp = hwrm_req_hold(bp, req); 6477 rc = hwrm_req_send(bp, req); 6478 if (!rc) 6479 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6480 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6481 hwrm_req_drop(bp, req); 6482 6483 return rc; 6484 } 6485 6486 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6487 { 6488 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6489 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6490 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6491 } 6492 6493 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6494 { 6495 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6496 struct hwrm_vnic_cfg_input *req; 6497 unsigned int ring = 0, grp_idx; 6498 u16 def_vlan = 0; 6499 int rc; 6500 6501 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6502 if (rc) 6503 return rc; 6504 6505 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6506 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6507 6508 req->default_rx_ring_id = 6509 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6510 req->default_cmpl_ring_id = 6511 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6512 req->enables = 6513 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6514 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6515 goto vnic_mru; 6516 } 6517 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6518 /* Only RSS support for now TBD: COS & LB */ 6519 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6520 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6521 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6522 VNIC_CFG_REQ_ENABLES_MRU); 6523 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6524 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6525 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6526 VNIC_CFG_REQ_ENABLES_MRU); 6527 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6528 } else { 6529 req->rss_rule = cpu_to_le16(0xffff); 6530 } 6531 6532 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6533 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6534 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6535 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6536 } else { 6537 req->cos_rule = cpu_to_le16(0xffff); 6538 } 6539 6540 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6541 ring = 0; 6542 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6543 ring = vnic->vnic_id - 1; 6544 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6545 ring = bp->rx_nr_rings - 1; 6546 6547 grp_idx = bp->rx_ring[ring].bnapi->index; 6548 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6549 req->lb_rule = cpu_to_le16(0xffff); 6550 vnic_mru: 6551 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 6552 req->mru = cpu_to_le16(vnic->mru); 6553 6554 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6555 #ifdef CONFIG_BNXT_SRIOV 6556 if (BNXT_VF(bp)) 6557 def_vlan = bp->vf.vlan; 6558 #endif 6559 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6560 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6561 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6562 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6563 6564 return hwrm_req_send(bp, req); 6565 } 6566 6567 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6568 struct bnxt_vnic_info *vnic) 6569 { 6570 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6571 struct hwrm_vnic_free_input *req; 6572 6573 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6574 return; 6575 6576 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6577 6578 hwrm_req_send(bp, req); 6579 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6580 } 6581 } 6582 6583 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6584 { 6585 u16 i; 6586 6587 for (i = 0; i < bp->nr_vnics; i++) 6588 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6589 } 6590 6591 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6592 unsigned int start_rx_ring_idx, 6593 unsigned int nr_rings) 6594 { 6595 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6596 struct hwrm_vnic_alloc_output *resp; 6597 struct hwrm_vnic_alloc_input *req; 6598 int rc; 6599 6600 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6601 if (rc) 6602 return rc; 6603 6604 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6605 goto vnic_no_ring_grps; 6606 6607 /* map ring groups to this vnic */ 6608 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6609 grp_idx = bp->rx_ring[i].bnapi->index; 6610 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6611 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6612 j, nr_rings); 6613 break; 6614 } 6615 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6616 } 6617 6618 vnic_no_ring_grps: 6619 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6620 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6621 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6622 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6623 6624 resp = hwrm_req_hold(bp, req); 6625 rc = hwrm_req_send(bp, req); 6626 if (!rc) 6627 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6628 hwrm_req_drop(bp, req); 6629 return rc; 6630 } 6631 6632 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6633 { 6634 struct hwrm_vnic_qcaps_output *resp; 6635 struct hwrm_vnic_qcaps_input *req; 6636 int rc; 6637 6638 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6639 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6640 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6641 if (bp->hwrm_spec_code < 0x10600) 6642 return 0; 6643 6644 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6645 if (rc) 6646 return rc; 6647 6648 resp = hwrm_req_hold(bp, req); 6649 rc = hwrm_req_send(bp, req); 6650 if (!rc) { 6651 u32 flags = le32_to_cpu(resp->flags); 6652 6653 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6654 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6655 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6656 if (flags & 6657 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6658 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6659 6660 /* Older P5 fw before EXT_HW_STATS support did not set 6661 * VLAN_STRIP_CAP properly. 6662 */ 6663 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6664 (BNXT_CHIP_P5(bp) && 6665 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6666 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6667 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6668 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6669 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6670 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6671 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6672 if (bp->max_tpa_v2) { 6673 if (BNXT_CHIP_P5(bp)) 6674 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6675 else 6676 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6677 } 6678 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6679 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6680 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6681 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6682 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6683 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6684 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6685 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6686 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6687 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6688 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6689 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6690 } 6691 hwrm_req_drop(bp, req); 6692 return rc; 6693 } 6694 6695 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6696 { 6697 struct hwrm_ring_grp_alloc_output *resp; 6698 struct hwrm_ring_grp_alloc_input *req; 6699 int rc; 6700 u16 i; 6701 6702 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6703 return 0; 6704 6705 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6706 if (rc) 6707 return rc; 6708 6709 resp = hwrm_req_hold(bp, req); 6710 for (i = 0; i < bp->rx_nr_rings; i++) { 6711 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 6712 6713 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 6714 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 6715 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 6716 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 6717 6718 rc = hwrm_req_send(bp, req); 6719 6720 if (rc) 6721 break; 6722 6723 bp->grp_info[grp_idx].fw_grp_id = 6724 le32_to_cpu(resp->ring_group_id); 6725 } 6726 hwrm_req_drop(bp, req); 6727 return rc; 6728 } 6729 6730 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 6731 { 6732 struct hwrm_ring_grp_free_input *req; 6733 u16 i; 6734 6735 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 6736 return; 6737 6738 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 6739 return; 6740 6741 hwrm_req_hold(bp, req); 6742 for (i = 0; i < bp->cp_nr_rings; i++) { 6743 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 6744 continue; 6745 req->ring_group_id = 6746 cpu_to_le32(bp->grp_info[i].fw_grp_id); 6747 6748 hwrm_req_send(bp, req); 6749 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 6750 } 6751 hwrm_req_drop(bp, req); 6752 } 6753 6754 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 6755 struct bnxt_ring_struct *ring, 6756 u32 ring_type, u32 map_index) 6757 { 6758 struct hwrm_ring_alloc_output *resp; 6759 struct hwrm_ring_alloc_input *req; 6760 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 6761 struct bnxt_ring_grp_info *grp_info; 6762 int rc, err = 0; 6763 u16 ring_id; 6764 6765 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 6766 if (rc) 6767 goto exit; 6768 6769 req->enables = 0; 6770 if (rmem->nr_pages > 1) { 6771 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 6772 /* Page size is in log2 units */ 6773 req->page_size = BNXT_PAGE_SHIFT; 6774 req->page_tbl_depth = 1; 6775 } else { 6776 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 6777 } 6778 req->fbo = 0; 6779 /* Association of ring index with doorbell index and MSIX number */ 6780 req->logical_id = cpu_to_le16(map_index); 6781 6782 switch (ring_type) { 6783 case HWRM_RING_ALLOC_TX: { 6784 struct bnxt_tx_ring_info *txr; 6785 u16 flags = 0; 6786 6787 txr = container_of(ring, struct bnxt_tx_ring_info, 6788 tx_ring_struct); 6789 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 6790 /* Association of transmit ring with completion ring */ 6791 grp_info = &bp->grp_info[ring->grp_idx]; 6792 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 6793 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 6794 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6795 req->queue_id = cpu_to_le16(ring->queue_id); 6796 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 6797 req->cmpl_coal_cnt = 6798 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 6799 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 6800 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 6801 req->flags = cpu_to_le16(flags); 6802 break; 6803 } 6804 case HWRM_RING_ALLOC_RX: 6805 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6806 req->length = cpu_to_le32(bp->rx_ring_mask + 1); 6807 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6808 u16 flags = 0; 6809 6810 /* Association of rx ring with stats context */ 6811 grp_info = &bp->grp_info[ring->grp_idx]; 6812 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 6813 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6814 req->enables |= cpu_to_le32( 6815 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6816 if (NET_IP_ALIGN == 2) 6817 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD; 6818 req->flags = cpu_to_le16(flags); 6819 } 6820 break; 6821 case HWRM_RING_ALLOC_AGG: 6822 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6823 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 6824 /* Association of agg ring with rx ring */ 6825 grp_info = &bp->grp_info[ring->grp_idx]; 6826 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 6827 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 6828 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6829 req->enables |= cpu_to_le32( 6830 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID | 6831 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6832 } else { 6833 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6834 } 6835 req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1); 6836 break; 6837 case HWRM_RING_ALLOC_CMPL: 6838 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 6839 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 6840 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6841 /* Association of cp ring with nq */ 6842 grp_info = &bp->grp_info[map_index]; 6843 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 6844 req->cq_handle = cpu_to_le64(ring->handle); 6845 req->enables |= cpu_to_le32( 6846 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 6847 } else { 6848 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 6849 } 6850 break; 6851 case HWRM_RING_ALLOC_NQ: 6852 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 6853 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 6854 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 6855 break; 6856 default: 6857 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 6858 ring_type); 6859 return -1; 6860 } 6861 6862 resp = hwrm_req_hold(bp, req); 6863 rc = hwrm_req_send(bp, req); 6864 err = le16_to_cpu(resp->error_code); 6865 ring_id = le16_to_cpu(resp->ring_id); 6866 hwrm_req_drop(bp, req); 6867 6868 exit: 6869 if (rc || err) { 6870 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 6871 ring_type, rc, err); 6872 return -EIO; 6873 } 6874 ring->fw_ring_id = ring_id; 6875 return rc; 6876 } 6877 6878 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 6879 { 6880 int rc; 6881 6882 if (BNXT_PF(bp)) { 6883 struct hwrm_func_cfg_input *req; 6884 6885 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 6886 if (rc) 6887 return rc; 6888 6889 req->fid = cpu_to_le16(0xffff); 6890 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 6891 req->async_event_cr = cpu_to_le16(idx); 6892 return hwrm_req_send(bp, req); 6893 } else { 6894 struct hwrm_func_vf_cfg_input *req; 6895 6896 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 6897 if (rc) 6898 return rc; 6899 6900 req->enables = 6901 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 6902 req->async_event_cr = cpu_to_le16(idx); 6903 return hwrm_req_send(bp, req); 6904 } 6905 } 6906 6907 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 6908 u32 ring_type) 6909 { 6910 switch (ring_type) { 6911 case HWRM_RING_ALLOC_TX: 6912 db->db_ring_mask = bp->tx_ring_mask; 6913 break; 6914 case HWRM_RING_ALLOC_RX: 6915 db->db_ring_mask = bp->rx_ring_mask; 6916 break; 6917 case HWRM_RING_ALLOC_AGG: 6918 db->db_ring_mask = bp->rx_agg_ring_mask; 6919 break; 6920 case HWRM_RING_ALLOC_CMPL: 6921 case HWRM_RING_ALLOC_NQ: 6922 db->db_ring_mask = bp->cp_ring_mask; 6923 break; 6924 } 6925 if (bp->flags & BNXT_FLAG_CHIP_P7) { 6926 db->db_epoch_mask = db->db_ring_mask + 1; 6927 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 6928 } 6929 } 6930 6931 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 6932 u32 map_idx, u32 xid) 6933 { 6934 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6935 switch (ring_type) { 6936 case HWRM_RING_ALLOC_TX: 6937 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 6938 break; 6939 case HWRM_RING_ALLOC_RX: 6940 case HWRM_RING_ALLOC_AGG: 6941 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 6942 break; 6943 case HWRM_RING_ALLOC_CMPL: 6944 db->db_key64 = DBR_PATH_L2; 6945 break; 6946 case HWRM_RING_ALLOC_NQ: 6947 db->db_key64 = DBR_PATH_L2; 6948 break; 6949 } 6950 db->db_key64 |= (u64)xid << DBR_XID_SFT; 6951 6952 if (bp->flags & BNXT_FLAG_CHIP_P7) 6953 db->db_key64 |= DBR_VALID; 6954 6955 db->doorbell = bp->bar1 + bp->db_offset; 6956 } else { 6957 db->doorbell = bp->bar1 + map_idx * 0x80; 6958 switch (ring_type) { 6959 case HWRM_RING_ALLOC_TX: 6960 db->db_key32 = DB_KEY_TX; 6961 break; 6962 case HWRM_RING_ALLOC_RX: 6963 case HWRM_RING_ALLOC_AGG: 6964 db->db_key32 = DB_KEY_RX; 6965 break; 6966 case HWRM_RING_ALLOC_CMPL: 6967 db->db_key32 = DB_KEY_CP; 6968 break; 6969 } 6970 } 6971 bnxt_set_db_mask(bp, db, ring_type); 6972 } 6973 6974 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 6975 struct bnxt_rx_ring_info *rxr) 6976 { 6977 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 6978 struct bnxt_napi *bnapi = rxr->bnapi; 6979 u32 type = HWRM_RING_ALLOC_RX; 6980 u32 map_idx = bnapi->index; 6981 int rc; 6982 6983 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 6984 if (rc) 6985 return rc; 6986 6987 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 6988 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 6989 6990 return 0; 6991 } 6992 6993 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 6994 struct bnxt_rx_ring_info *rxr) 6995 { 6996 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 6997 u32 type = HWRM_RING_ALLOC_AGG; 6998 u32 grp_idx = ring->grp_idx; 6999 u32 map_idx; 7000 int rc; 7001 7002 map_idx = grp_idx + bp->rx_nr_rings; 7003 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7004 if (rc) 7005 return rc; 7006 7007 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7008 ring->fw_ring_id); 7009 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7010 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7011 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7012 7013 return 0; 7014 } 7015 7016 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7017 { 7018 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7019 int i, rc = 0; 7020 u32 type; 7021 7022 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7023 type = HWRM_RING_ALLOC_NQ; 7024 else 7025 type = HWRM_RING_ALLOC_CMPL; 7026 for (i = 0; i < bp->cp_nr_rings; i++) { 7027 struct bnxt_napi *bnapi = bp->bnapi[i]; 7028 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7029 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7030 u32 map_idx = ring->map_idx; 7031 unsigned int vector; 7032 7033 vector = bp->irq_tbl[map_idx].vector; 7034 disable_irq_nosync(vector); 7035 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7036 if (rc) { 7037 enable_irq(vector); 7038 goto err_out; 7039 } 7040 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7041 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7042 enable_irq(vector); 7043 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7044 7045 if (!i) { 7046 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7047 if (rc) 7048 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7049 } 7050 } 7051 7052 type = HWRM_RING_ALLOC_TX; 7053 for (i = 0; i < bp->tx_nr_rings; i++) { 7054 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7055 struct bnxt_ring_struct *ring; 7056 u32 map_idx; 7057 7058 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7059 struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr; 7060 struct bnxt_napi *bnapi = txr->bnapi; 7061 u32 type2 = HWRM_RING_ALLOC_CMPL; 7062 7063 ring = &cpr2->cp_ring_struct; 7064 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7065 map_idx = bnapi->index; 7066 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7067 if (rc) 7068 goto err_out; 7069 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7070 ring->fw_ring_id); 7071 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7072 } 7073 ring = &txr->tx_ring_struct; 7074 map_idx = i; 7075 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7076 if (rc) 7077 goto err_out; 7078 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id); 7079 } 7080 7081 for (i = 0; i < bp->rx_nr_rings; i++) { 7082 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7083 7084 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7085 if (rc) 7086 goto err_out; 7087 /* If we have agg rings, post agg buffers first. */ 7088 if (!agg_rings) 7089 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7090 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7091 struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr; 7092 struct bnxt_napi *bnapi = rxr->bnapi; 7093 u32 type2 = HWRM_RING_ALLOC_CMPL; 7094 struct bnxt_ring_struct *ring; 7095 u32 map_idx = bnapi->index; 7096 7097 ring = &cpr2->cp_ring_struct; 7098 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7099 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7100 if (rc) 7101 goto err_out; 7102 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7103 ring->fw_ring_id); 7104 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7105 } 7106 } 7107 7108 if (agg_rings) { 7109 for (i = 0; i < bp->rx_nr_rings; i++) { 7110 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7111 if (rc) 7112 goto err_out; 7113 } 7114 } 7115 err_out: 7116 return rc; 7117 } 7118 7119 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7120 struct bnxt_ring_struct *ring, 7121 u32 ring_type, int cmpl_ring_id) 7122 { 7123 struct hwrm_ring_free_output *resp; 7124 struct hwrm_ring_free_input *req; 7125 u16 error_code = 0; 7126 int rc; 7127 7128 if (BNXT_NO_FW_ACCESS(bp)) 7129 return 0; 7130 7131 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7132 if (rc) 7133 goto exit; 7134 7135 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7136 req->ring_type = ring_type; 7137 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7138 7139 resp = hwrm_req_hold(bp, req); 7140 rc = hwrm_req_send(bp, req); 7141 error_code = le16_to_cpu(resp->error_code); 7142 hwrm_req_drop(bp, req); 7143 exit: 7144 if (rc || error_code) { 7145 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7146 ring_type, rc, error_code); 7147 return -EIO; 7148 } 7149 return 0; 7150 } 7151 7152 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7153 struct bnxt_rx_ring_info *rxr, 7154 bool close_path) 7155 { 7156 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7157 u32 grp_idx = rxr->bnapi->index; 7158 u32 cmpl_ring_id; 7159 7160 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7161 return; 7162 7163 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7164 hwrm_ring_free_send_msg(bp, ring, 7165 RING_FREE_REQ_RING_TYPE_RX, 7166 close_path ? cmpl_ring_id : 7167 INVALID_HW_RING_ID); 7168 ring->fw_ring_id = INVALID_HW_RING_ID; 7169 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7170 } 7171 7172 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7173 struct bnxt_rx_ring_info *rxr, 7174 bool close_path) 7175 { 7176 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7177 u32 grp_idx = rxr->bnapi->index; 7178 u32 type, cmpl_ring_id; 7179 7180 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7181 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7182 else 7183 type = RING_FREE_REQ_RING_TYPE_RX; 7184 7185 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7186 return; 7187 7188 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7189 hwrm_ring_free_send_msg(bp, ring, type, 7190 close_path ? cmpl_ring_id : 7191 INVALID_HW_RING_ID); 7192 ring->fw_ring_id = INVALID_HW_RING_ID; 7193 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7194 } 7195 7196 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7197 { 7198 u32 type; 7199 int i; 7200 7201 if (!bp->bnapi) 7202 return; 7203 7204 for (i = 0; i < bp->tx_nr_rings; i++) { 7205 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7206 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7207 7208 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7209 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr); 7210 7211 hwrm_ring_free_send_msg(bp, ring, 7212 RING_FREE_REQ_RING_TYPE_TX, 7213 close_path ? cmpl_ring_id : 7214 INVALID_HW_RING_ID); 7215 ring->fw_ring_id = INVALID_HW_RING_ID; 7216 } 7217 } 7218 7219 for (i = 0; i < bp->rx_nr_rings; i++) { 7220 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7221 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7222 } 7223 7224 /* The completion rings are about to be freed. After that the 7225 * IRQ doorbell will not work anymore. So we need to disable 7226 * IRQ here. 7227 */ 7228 bnxt_disable_int_sync(bp); 7229 7230 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7231 type = RING_FREE_REQ_RING_TYPE_NQ; 7232 else 7233 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7234 for (i = 0; i < bp->cp_nr_rings; i++) { 7235 struct bnxt_napi *bnapi = bp->bnapi[i]; 7236 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7237 struct bnxt_ring_struct *ring; 7238 int j; 7239 7240 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) { 7241 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 7242 7243 ring = &cpr2->cp_ring_struct; 7244 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7245 continue; 7246 hwrm_ring_free_send_msg(bp, ring, 7247 RING_FREE_REQ_RING_TYPE_L2_CMPL, 7248 INVALID_HW_RING_ID); 7249 ring->fw_ring_id = INVALID_HW_RING_ID; 7250 } 7251 ring = &cpr->cp_ring_struct; 7252 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7253 hwrm_ring_free_send_msg(bp, ring, type, 7254 INVALID_HW_RING_ID); 7255 ring->fw_ring_id = INVALID_HW_RING_ID; 7256 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7257 } 7258 } 7259 } 7260 7261 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7262 bool shared); 7263 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7264 bool shared); 7265 7266 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7267 { 7268 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7269 struct hwrm_func_qcfg_output *resp; 7270 struct hwrm_func_qcfg_input *req; 7271 int rc; 7272 7273 if (bp->hwrm_spec_code < 0x10601) 7274 return 0; 7275 7276 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7277 if (rc) 7278 return rc; 7279 7280 req->fid = cpu_to_le16(0xffff); 7281 resp = hwrm_req_hold(bp, req); 7282 rc = hwrm_req_send(bp, req); 7283 if (rc) { 7284 hwrm_req_drop(bp, req); 7285 return rc; 7286 } 7287 7288 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7289 if (BNXT_NEW_RM(bp)) { 7290 u16 cp, stats; 7291 7292 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7293 hw_resc->resv_hw_ring_grps = 7294 le32_to_cpu(resp->alloc_hw_ring_grps); 7295 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7296 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7297 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7298 stats = le16_to_cpu(resp->alloc_stat_ctx); 7299 hw_resc->resv_irqs = cp; 7300 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7301 int rx = hw_resc->resv_rx_rings; 7302 int tx = hw_resc->resv_tx_rings; 7303 7304 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7305 rx >>= 1; 7306 if (cp < (rx + tx)) { 7307 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7308 if (rc) 7309 goto get_rings_exit; 7310 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7311 rx <<= 1; 7312 hw_resc->resv_rx_rings = rx; 7313 hw_resc->resv_tx_rings = tx; 7314 } 7315 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7316 hw_resc->resv_hw_ring_grps = rx; 7317 } 7318 hw_resc->resv_cp_rings = cp; 7319 hw_resc->resv_stat_ctxs = stats; 7320 } 7321 get_rings_exit: 7322 hwrm_req_drop(bp, req); 7323 return rc; 7324 } 7325 7326 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7327 { 7328 struct hwrm_func_qcfg_output *resp; 7329 struct hwrm_func_qcfg_input *req; 7330 int rc; 7331 7332 if (bp->hwrm_spec_code < 0x10601) 7333 return 0; 7334 7335 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7336 if (rc) 7337 return rc; 7338 7339 req->fid = cpu_to_le16(fid); 7340 resp = hwrm_req_hold(bp, req); 7341 rc = hwrm_req_send(bp, req); 7342 if (!rc) 7343 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7344 7345 hwrm_req_drop(bp, req); 7346 return rc; 7347 } 7348 7349 static bool bnxt_rfs_supported(struct bnxt *bp); 7350 7351 static struct hwrm_func_cfg_input * 7352 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7353 { 7354 struct hwrm_func_cfg_input *req; 7355 u32 enables = 0; 7356 7357 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7358 return NULL; 7359 7360 req->fid = cpu_to_le16(0xffff); 7361 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7362 req->num_tx_rings = cpu_to_le16(hwr->tx); 7363 if (BNXT_NEW_RM(bp)) { 7364 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7365 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7366 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7367 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7368 enables |= hwr->cp_p5 ? 7369 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7370 } else { 7371 enables |= hwr->cp ? 7372 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7373 enables |= hwr->grp ? 7374 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7375 } 7376 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7377 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7378 0; 7379 req->num_rx_rings = cpu_to_le16(hwr->rx); 7380 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7381 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7382 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7383 req->num_msix = cpu_to_le16(hwr->cp); 7384 } else { 7385 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7386 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7387 } 7388 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7389 req->num_vnics = cpu_to_le16(hwr->vnic); 7390 } 7391 req->enables = cpu_to_le32(enables); 7392 return req; 7393 } 7394 7395 static struct hwrm_func_vf_cfg_input * 7396 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7397 { 7398 struct hwrm_func_vf_cfg_input *req; 7399 u32 enables = 0; 7400 7401 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7402 return NULL; 7403 7404 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7405 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7406 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7407 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7408 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7409 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7410 enables |= hwr->cp_p5 ? 7411 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7412 } else { 7413 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7414 enables |= hwr->grp ? 7415 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7416 } 7417 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7418 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7419 7420 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7421 req->num_tx_rings = cpu_to_le16(hwr->tx); 7422 req->num_rx_rings = cpu_to_le16(hwr->rx); 7423 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7424 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7425 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7426 } else { 7427 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7428 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7429 } 7430 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7431 req->num_vnics = cpu_to_le16(hwr->vnic); 7432 7433 req->enables = cpu_to_le32(enables); 7434 return req; 7435 } 7436 7437 static int 7438 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7439 { 7440 struct hwrm_func_cfg_input *req; 7441 int rc; 7442 7443 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7444 if (!req) 7445 return -ENOMEM; 7446 7447 if (!req->enables) { 7448 hwrm_req_drop(bp, req); 7449 return 0; 7450 } 7451 7452 rc = hwrm_req_send(bp, req); 7453 if (rc) 7454 return rc; 7455 7456 if (bp->hwrm_spec_code < 0x10601) 7457 bp->hw_resc.resv_tx_rings = hwr->tx; 7458 7459 return bnxt_hwrm_get_rings(bp); 7460 } 7461 7462 static int 7463 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7464 { 7465 struct hwrm_func_vf_cfg_input *req; 7466 int rc; 7467 7468 if (!BNXT_NEW_RM(bp)) { 7469 bp->hw_resc.resv_tx_rings = hwr->tx; 7470 return 0; 7471 } 7472 7473 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7474 if (!req) 7475 return -ENOMEM; 7476 7477 rc = hwrm_req_send(bp, req); 7478 if (rc) 7479 return rc; 7480 7481 return bnxt_hwrm_get_rings(bp); 7482 } 7483 7484 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7485 { 7486 if (BNXT_PF(bp)) 7487 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7488 else 7489 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7490 } 7491 7492 int bnxt_nq_rings_in_use(struct bnxt *bp) 7493 { 7494 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7495 } 7496 7497 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7498 { 7499 int cp; 7500 7501 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7502 return bnxt_nq_rings_in_use(bp); 7503 7504 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7505 return cp; 7506 } 7507 7508 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7509 { 7510 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7511 } 7512 7513 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7514 { 7515 if (!hwr->grp) 7516 return 0; 7517 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7518 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7519 7520 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7521 rss_ctx *= hwr->vnic; 7522 return rss_ctx; 7523 } 7524 if (BNXT_VF(bp)) 7525 return BNXT_VF_MAX_RSS_CTX; 7526 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7527 return hwr->grp + 1; 7528 return 1; 7529 } 7530 7531 /* Check if a default RSS map needs to be setup. This function is only 7532 * used on older firmware that does not require reserving RX rings. 7533 */ 7534 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7535 { 7536 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7537 7538 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7539 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7540 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7541 if (!netif_is_rxfh_configured(bp->dev)) 7542 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7543 } 7544 } 7545 7546 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7547 { 7548 if (bp->flags & BNXT_FLAG_RFS) { 7549 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7550 return 2 + bp->num_rss_ctx; 7551 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7552 return rx_rings + 1; 7553 } 7554 return 1; 7555 } 7556 7557 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7558 { 7559 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7560 int cp = bnxt_cp_rings_in_use(bp); 7561 int nq = bnxt_nq_rings_in_use(bp); 7562 int rx = bp->rx_nr_rings, stat; 7563 int vnic, grp = rx; 7564 7565 /* Old firmware does not need RX ring reservations but we still 7566 * need to setup a default RSS map when needed. With new firmware 7567 * we go through RX ring reservations first and then set up the 7568 * RSS map for the successfully reserved RX rings when needed. 7569 */ 7570 if (!BNXT_NEW_RM(bp)) 7571 bnxt_check_rss_tbl_no_rmgr(bp); 7572 7573 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7574 bp->hwrm_spec_code >= 0x10601) 7575 return true; 7576 7577 if (!BNXT_NEW_RM(bp)) 7578 return false; 7579 7580 vnic = bnxt_get_total_vnics(bp, rx); 7581 7582 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7583 rx <<= 1; 7584 stat = bnxt_get_func_stat_ctxs(bp); 7585 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7586 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7587 (hw_resc->resv_hw_ring_grps != grp && 7588 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7589 return true; 7590 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7591 hw_resc->resv_irqs != nq) 7592 return true; 7593 return false; 7594 } 7595 7596 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7597 { 7598 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7599 7600 hwr->tx = hw_resc->resv_tx_rings; 7601 if (BNXT_NEW_RM(bp)) { 7602 hwr->rx = hw_resc->resv_rx_rings; 7603 hwr->cp = hw_resc->resv_irqs; 7604 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7605 hwr->cp_p5 = hw_resc->resv_cp_rings; 7606 hwr->grp = hw_resc->resv_hw_ring_grps; 7607 hwr->vnic = hw_resc->resv_vnics; 7608 hwr->stat = hw_resc->resv_stat_ctxs; 7609 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7610 } 7611 } 7612 7613 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7614 { 7615 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7616 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7617 } 7618 7619 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7620 7621 static int __bnxt_reserve_rings(struct bnxt *bp) 7622 { 7623 struct bnxt_hw_rings hwr = {0}; 7624 int rx_rings, old_rx_rings, rc; 7625 int cp = bp->cp_nr_rings; 7626 int ulp_msix = 0; 7627 bool sh = false; 7628 int tx_cp; 7629 7630 if (!bnxt_need_reserve_rings(bp)) 7631 return 0; 7632 7633 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7634 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7635 if (!ulp_msix) 7636 bnxt_set_ulp_stat_ctxs(bp, 0); 7637 7638 if (ulp_msix > bp->ulp_num_msix_want) 7639 ulp_msix = bp->ulp_num_msix_want; 7640 hwr.cp = cp + ulp_msix; 7641 } else { 7642 hwr.cp = bnxt_nq_rings_in_use(bp); 7643 } 7644 7645 hwr.tx = bp->tx_nr_rings; 7646 hwr.rx = bp->rx_nr_rings; 7647 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7648 sh = true; 7649 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7650 hwr.cp_p5 = hwr.rx + hwr.tx; 7651 7652 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7653 7654 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7655 hwr.rx <<= 1; 7656 hwr.grp = bp->rx_nr_rings; 7657 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 7658 hwr.stat = bnxt_get_func_stat_ctxs(bp); 7659 old_rx_rings = bp->hw_resc.resv_rx_rings; 7660 7661 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 7662 if (rc) 7663 return rc; 7664 7665 bnxt_copy_reserved_rings(bp, &hwr); 7666 7667 rx_rings = hwr.rx; 7668 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 7669 if (hwr.rx >= 2) { 7670 rx_rings = hwr.rx >> 1; 7671 } else { 7672 if (netif_running(bp->dev)) 7673 return -ENOMEM; 7674 7675 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 7676 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 7677 bp->dev->hw_features &= ~NETIF_F_LRO; 7678 bp->dev->features &= ~NETIF_F_LRO; 7679 bnxt_set_ring_params(bp); 7680 } 7681 } 7682 rx_rings = min_t(int, rx_rings, hwr.grp); 7683 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 7684 if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 7685 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 7686 hwr.cp = min_t(int, hwr.cp, hwr.stat); 7687 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 7688 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7689 hwr.rx = rx_rings << 1; 7690 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 7691 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 7692 bp->tx_nr_rings = hwr.tx; 7693 7694 /* If we cannot reserve all the RX rings, reset the RSS map only 7695 * if absolutely necessary 7696 */ 7697 if (rx_rings != bp->rx_nr_rings) { 7698 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 7699 rx_rings, bp->rx_nr_rings); 7700 if (netif_is_rxfh_configured(bp->dev) && 7701 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 7702 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 7703 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 7704 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 7705 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 7706 } 7707 } 7708 bp->rx_nr_rings = rx_rings; 7709 bp->cp_nr_rings = hwr.cp; 7710 7711 if (!bnxt_rings_ok(bp, &hwr)) 7712 return -ENOMEM; 7713 7714 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 7715 !netif_is_rxfh_configured(bp->dev)) 7716 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7717 7718 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 7719 int resv_msix, resv_ctx, ulp_ctxs; 7720 struct bnxt_hw_resc *hw_resc; 7721 7722 hw_resc = &bp->hw_resc; 7723 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 7724 ulp_msix = min_t(int, resv_msix, ulp_msix); 7725 bnxt_set_ulp_msix_num(bp, ulp_msix); 7726 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 7727 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 7728 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 7729 } 7730 7731 return rc; 7732 } 7733 7734 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7735 { 7736 struct hwrm_func_vf_cfg_input *req; 7737 u32 flags; 7738 7739 if (!BNXT_NEW_RM(bp)) 7740 return 0; 7741 7742 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7743 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 7744 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7745 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7746 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7747 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 7748 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 7749 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7750 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7751 7752 req->flags = cpu_to_le32(flags); 7753 return hwrm_req_send_silent(bp, req); 7754 } 7755 7756 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7757 { 7758 struct hwrm_func_cfg_input *req; 7759 u32 flags; 7760 7761 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7762 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 7763 if (BNXT_NEW_RM(bp)) { 7764 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7765 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7766 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7767 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 7768 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7769 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 7770 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 7771 else 7772 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7773 } 7774 7775 req->flags = cpu_to_le32(flags); 7776 return hwrm_req_send_silent(bp, req); 7777 } 7778 7779 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7780 { 7781 if (bp->hwrm_spec_code < 0x10801) 7782 return 0; 7783 7784 if (BNXT_PF(bp)) 7785 return bnxt_hwrm_check_pf_rings(bp, hwr); 7786 7787 return bnxt_hwrm_check_vf_rings(bp, hwr); 7788 } 7789 7790 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 7791 { 7792 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7793 struct hwrm_ring_aggint_qcaps_output *resp; 7794 struct hwrm_ring_aggint_qcaps_input *req; 7795 int rc; 7796 7797 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 7798 coal_cap->num_cmpl_dma_aggr_max = 63; 7799 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 7800 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 7801 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 7802 coal_cap->int_lat_tmr_min_max = 65535; 7803 coal_cap->int_lat_tmr_max_max = 65535; 7804 coal_cap->num_cmpl_aggr_int_max = 65535; 7805 coal_cap->timer_units = 80; 7806 7807 if (bp->hwrm_spec_code < 0x10902) 7808 return; 7809 7810 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 7811 return; 7812 7813 resp = hwrm_req_hold(bp, req); 7814 rc = hwrm_req_send_silent(bp, req); 7815 if (!rc) { 7816 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 7817 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 7818 coal_cap->num_cmpl_dma_aggr_max = 7819 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 7820 coal_cap->num_cmpl_dma_aggr_during_int_max = 7821 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 7822 coal_cap->cmpl_aggr_dma_tmr_max = 7823 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 7824 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 7825 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 7826 coal_cap->int_lat_tmr_min_max = 7827 le16_to_cpu(resp->int_lat_tmr_min_max); 7828 coal_cap->int_lat_tmr_max_max = 7829 le16_to_cpu(resp->int_lat_tmr_max_max); 7830 coal_cap->num_cmpl_aggr_int_max = 7831 le16_to_cpu(resp->num_cmpl_aggr_int_max); 7832 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 7833 } 7834 hwrm_req_drop(bp, req); 7835 } 7836 7837 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 7838 { 7839 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7840 7841 return usec * 1000 / coal_cap->timer_units; 7842 } 7843 7844 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 7845 struct bnxt_coal *hw_coal, 7846 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 7847 { 7848 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7849 u16 val, tmr, max, flags = hw_coal->flags; 7850 u32 cmpl_params = coal_cap->cmpl_params; 7851 7852 max = hw_coal->bufs_per_record * 128; 7853 if (hw_coal->budget) 7854 max = hw_coal->bufs_per_record * hw_coal->budget; 7855 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 7856 7857 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 7858 req->num_cmpl_aggr_int = cpu_to_le16(val); 7859 7860 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 7861 req->num_cmpl_dma_aggr = cpu_to_le16(val); 7862 7863 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 7864 coal_cap->num_cmpl_dma_aggr_during_int_max); 7865 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 7866 7867 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 7868 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 7869 req->int_lat_tmr_max = cpu_to_le16(tmr); 7870 7871 /* min timer set to 1/2 of interrupt timer */ 7872 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 7873 val = tmr / 2; 7874 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 7875 req->int_lat_tmr_min = cpu_to_le16(val); 7876 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 7877 } 7878 7879 /* buf timer set to 1/4 of interrupt timer */ 7880 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 7881 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 7882 7883 if (cmpl_params & 7884 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 7885 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 7886 val = clamp_t(u16, tmr, 1, 7887 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 7888 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 7889 req->enables |= 7890 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 7891 } 7892 7893 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 7894 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 7895 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 7896 req->flags = cpu_to_le16(flags); 7897 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 7898 } 7899 7900 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 7901 struct bnxt_coal *hw_coal) 7902 { 7903 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 7904 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7905 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7906 u32 nq_params = coal_cap->nq_params; 7907 u16 tmr; 7908 int rc; 7909 7910 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 7911 return 0; 7912 7913 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7914 if (rc) 7915 return rc; 7916 7917 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 7918 req->flags = 7919 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 7920 7921 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 7922 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 7923 req->int_lat_tmr_min = cpu_to_le16(tmr); 7924 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 7925 return hwrm_req_send(bp, req); 7926 } 7927 7928 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 7929 { 7930 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 7931 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7932 struct bnxt_coal coal; 7933 int rc; 7934 7935 /* Tick values in micro seconds. 7936 * 1 coal_buf x bufs_per_record = 1 completion record. 7937 */ 7938 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 7939 7940 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 7941 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 7942 7943 if (!bnapi->rx_ring) 7944 return -ENODEV; 7945 7946 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7947 if (rc) 7948 return rc; 7949 7950 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 7951 7952 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 7953 7954 return hwrm_req_send(bp, req_rx); 7955 } 7956 7957 static int 7958 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 7959 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 7960 { 7961 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 7962 7963 req->ring_id = cpu_to_le16(ring_id); 7964 return hwrm_req_send(bp, req); 7965 } 7966 7967 static int 7968 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 7969 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 7970 { 7971 struct bnxt_tx_ring_info *txr; 7972 int i, rc; 7973 7974 bnxt_for_each_napi_tx(i, bnapi, txr) { 7975 u16 ring_id; 7976 7977 ring_id = bnxt_cp_ring_for_tx(bp, txr); 7978 req->ring_id = cpu_to_le16(ring_id); 7979 rc = hwrm_req_send(bp, req); 7980 if (rc) 7981 return rc; 7982 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7983 return 0; 7984 } 7985 return 0; 7986 } 7987 7988 int bnxt_hwrm_set_coal(struct bnxt *bp) 7989 { 7990 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 7991 int i, rc; 7992 7993 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7994 if (rc) 7995 return rc; 7996 7997 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7998 if (rc) { 7999 hwrm_req_drop(bp, req_rx); 8000 return rc; 8001 } 8002 8003 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8004 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8005 8006 hwrm_req_hold(bp, req_rx); 8007 hwrm_req_hold(bp, req_tx); 8008 for (i = 0; i < bp->cp_nr_rings; i++) { 8009 struct bnxt_napi *bnapi = bp->bnapi[i]; 8010 struct bnxt_coal *hw_coal; 8011 8012 if (!bnapi->rx_ring) 8013 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8014 else 8015 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8016 if (rc) 8017 break; 8018 8019 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8020 continue; 8021 8022 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8023 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8024 if (rc) 8025 break; 8026 } 8027 if (bnapi->rx_ring) 8028 hw_coal = &bp->rx_coal; 8029 else 8030 hw_coal = &bp->tx_coal; 8031 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8032 } 8033 hwrm_req_drop(bp, req_rx); 8034 hwrm_req_drop(bp, req_tx); 8035 return rc; 8036 } 8037 8038 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8039 { 8040 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8041 struct hwrm_stat_ctx_free_input *req; 8042 int i; 8043 8044 if (!bp->bnapi) 8045 return; 8046 8047 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8048 return; 8049 8050 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8051 return; 8052 if (BNXT_FW_MAJ(bp) <= 20) { 8053 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8054 hwrm_req_drop(bp, req); 8055 return; 8056 } 8057 hwrm_req_hold(bp, req0); 8058 } 8059 hwrm_req_hold(bp, req); 8060 for (i = 0; i < bp->cp_nr_rings; i++) { 8061 struct bnxt_napi *bnapi = bp->bnapi[i]; 8062 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8063 8064 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8065 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8066 if (req0) { 8067 req0->stat_ctx_id = req->stat_ctx_id; 8068 hwrm_req_send(bp, req0); 8069 } 8070 hwrm_req_send(bp, req); 8071 8072 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8073 } 8074 } 8075 hwrm_req_drop(bp, req); 8076 if (req0) 8077 hwrm_req_drop(bp, req0); 8078 } 8079 8080 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8081 { 8082 struct hwrm_stat_ctx_alloc_output *resp; 8083 struct hwrm_stat_ctx_alloc_input *req; 8084 int rc, i; 8085 8086 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8087 return 0; 8088 8089 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8090 if (rc) 8091 return rc; 8092 8093 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8094 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8095 8096 resp = hwrm_req_hold(bp, req); 8097 for (i = 0; i < bp->cp_nr_rings; i++) { 8098 struct bnxt_napi *bnapi = bp->bnapi[i]; 8099 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8100 8101 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8102 8103 rc = hwrm_req_send(bp, req); 8104 if (rc) 8105 break; 8106 8107 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8108 8109 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8110 } 8111 hwrm_req_drop(bp, req); 8112 return rc; 8113 } 8114 8115 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8116 { 8117 struct hwrm_func_qcfg_output *resp; 8118 struct hwrm_func_qcfg_input *req; 8119 u16 flags; 8120 int rc; 8121 8122 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8123 if (rc) 8124 return rc; 8125 8126 req->fid = cpu_to_le16(0xffff); 8127 resp = hwrm_req_hold(bp, req); 8128 rc = hwrm_req_send(bp, req); 8129 if (rc) 8130 goto func_qcfg_exit; 8131 8132 #ifdef CONFIG_BNXT_SRIOV 8133 if (BNXT_VF(bp)) { 8134 struct bnxt_vf_info *vf = &bp->vf; 8135 8136 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8137 } else { 8138 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8139 } 8140 #endif 8141 flags = le16_to_cpu(resp->flags); 8142 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8143 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8144 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8145 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8146 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8147 } 8148 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8149 bp->flags |= BNXT_FLAG_MULTI_HOST; 8150 8151 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8152 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8153 8154 switch (resp->port_partition_type) { 8155 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8156 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8157 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8158 bp->port_partition_type = resp->port_partition_type; 8159 break; 8160 } 8161 if (bp->hwrm_spec_code < 0x10707 || 8162 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8163 bp->br_mode = BRIDGE_MODE_VEB; 8164 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8165 bp->br_mode = BRIDGE_MODE_VEPA; 8166 else 8167 bp->br_mode = BRIDGE_MODE_UNDEF; 8168 8169 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8170 if (!bp->max_mtu) 8171 bp->max_mtu = BNXT_MAX_MTU; 8172 8173 if (bp->db_size) 8174 goto func_qcfg_exit; 8175 8176 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8177 if (BNXT_CHIP_P5(bp)) { 8178 if (BNXT_PF(bp)) 8179 bp->db_offset = DB_PF_OFFSET_P5; 8180 else 8181 bp->db_offset = DB_VF_OFFSET_P5; 8182 } 8183 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8184 1024); 8185 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8186 bp->db_size <= bp->db_offset) 8187 bp->db_size = pci_resource_len(bp->pdev, 2); 8188 8189 func_qcfg_exit: 8190 hwrm_req_drop(bp, req); 8191 return rc; 8192 } 8193 8194 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8195 u8 init_val, u8 init_offset, 8196 bool init_mask_set) 8197 { 8198 ctxm->init_value = init_val; 8199 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8200 if (init_mask_set) 8201 ctxm->init_offset = init_offset * 4; 8202 else 8203 ctxm->init_value = 0; 8204 } 8205 8206 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8207 { 8208 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8209 u16 type; 8210 8211 for (type = 0; type < ctx_max; type++) { 8212 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8213 int n = 1; 8214 8215 if (!ctxm->max_entries) 8216 continue; 8217 8218 if (ctxm->instance_bmap) 8219 n = hweight32(ctxm->instance_bmap); 8220 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8221 if (!ctxm->pg_info) 8222 return -ENOMEM; 8223 } 8224 return 0; 8225 } 8226 8227 #define BNXT_CTX_INIT_VALID(flags) \ 8228 (!!((flags) & \ 8229 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8230 8231 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8232 { 8233 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8234 struct hwrm_func_backing_store_qcaps_v2_input *req; 8235 struct bnxt_ctx_mem_info *ctx; 8236 u16 type; 8237 int rc; 8238 8239 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8240 if (rc) 8241 return rc; 8242 8243 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8244 if (!ctx) 8245 return -ENOMEM; 8246 bp->ctx = ctx; 8247 8248 resp = hwrm_req_hold(bp, req); 8249 8250 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8251 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8252 u8 init_val, init_off, i; 8253 __le32 *p; 8254 u32 flags; 8255 8256 req->type = cpu_to_le16(type); 8257 rc = hwrm_req_send(bp, req); 8258 if (rc) 8259 goto ctx_done; 8260 flags = le32_to_cpu(resp->flags); 8261 type = le16_to_cpu(resp->next_valid_type); 8262 if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID)) 8263 continue; 8264 8265 ctxm->type = le16_to_cpu(resp->type); 8266 ctxm->entry_size = le16_to_cpu(resp->entry_size); 8267 ctxm->flags = flags; 8268 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8269 ctxm->entry_multiple = resp->entry_multiple; 8270 ctxm->max_entries = le32_to_cpu(resp->max_num_entries); 8271 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8272 init_val = resp->ctx_init_value; 8273 init_off = resp->ctx_init_offset; 8274 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8275 BNXT_CTX_INIT_VALID(flags)); 8276 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8277 BNXT_MAX_SPLIT_ENTRY); 8278 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8279 i++, p++) 8280 ctxm->split[i] = le32_to_cpu(*p); 8281 } 8282 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8283 8284 ctx_done: 8285 hwrm_req_drop(bp, req); 8286 return rc; 8287 } 8288 8289 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8290 { 8291 struct hwrm_func_backing_store_qcaps_output *resp; 8292 struct hwrm_func_backing_store_qcaps_input *req; 8293 int rc; 8294 8295 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx) 8296 return 0; 8297 8298 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8299 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8300 8301 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8302 if (rc) 8303 return rc; 8304 8305 resp = hwrm_req_hold(bp, req); 8306 rc = hwrm_req_send_silent(bp, req); 8307 if (!rc) { 8308 struct bnxt_ctx_mem_type *ctxm; 8309 struct bnxt_ctx_mem_info *ctx; 8310 u8 init_val, init_idx = 0; 8311 u16 init_mask; 8312 8313 ctx = bp->ctx; 8314 if (!ctx) { 8315 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8316 if (!ctx) { 8317 rc = -ENOMEM; 8318 goto ctx_err; 8319 } 8320 bp->ctx = ctx; 8321 } 8322 init_val = resp->ctx_kind_initializer; 8323 init_mask = le16_to_cpu(resp->ctx_init_mask); 8324 8325 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8326 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8327 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8328 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8329 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8330 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8331 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8332 (init_mask & (1 << init_idx++)) != 0); 8333 8334 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8335 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8336 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8337 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8338 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8339 (init_mask & (1 << init_idx++)) != 0); 8340 8341 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8342 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8343 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8344 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8345 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8346 (init_mask & (1 << init_idx++)) != 0); 8347 8348 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8349 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8350 ctxm->max_entries = ctxm->vnic_entries + 8351 le16_to_cpu(resp->vnic_max_ring_table_entries); 8352 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8353 bnxt_init_ctx_initializer(ctxm, init_val, 8354 resp->vnic_init_offset, 8355 (init_mask & (1 << init_idx++)) != 0); 8356 8357 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8358 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8359 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8360 bnxt_init_ctx_initializer(ctxm, init_val, 8361 resp->stat_init_offset, 8362 (init_mask & (1 << init_idx++)) != 0); 8363 8364 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8365 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8366 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8367 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8368 ctxm->entry_multiple = resp->tqm_entries_multiple; 8369 if (!ctxm->entry_multiple) 8370 ctxm->entry_multiple = 1; 8371 8372 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8373 8374 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8375 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8376 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8377 ctxm->mrav_num_entries_units = 8378 le16_to_cpu(resp->mrav_num_entries_units); 8379 bnxt_init_ctx_initializer(ctxm, init_val, 8380 resp->mrav_init_offset, 8381 (init_mask & (1 << init_idx++)) != 0); 8382 8383 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8384 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8385 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8386 8387 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8388 if (!ctx->tqm_fp_rings_count) 8389 ctx->tqm_fp_rings_count = bp->max_q; 8390 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8391 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8392 8393 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8394 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8395 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8396 8397 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8398 } else { 8399 rc = 0; 8400 } 8401 ctx_err: 8402 hwrm_req_drop(bp, req); 8403 return rc; 8404 } 8405 8406 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8407 __le64 *pg_dir) 8408 { 8409 if (!rmem->nr_pages) 8410 return; 8411 8412 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8413 if (rmem->depth >= 1) { 8414 if (rmem->depth == 2) 8415 *pg_attr |= 2; 8416 else 8417 *pg_attr |= 1; 8418 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8419 } else { 8420 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8421 } 8422 } 8423 8424 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8425 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8426 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8427 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8428 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8429 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8430 8431 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8432 { 8433 struct hwrm_func_backing_store_cfg_input *req; 8434 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8435 struct bnxt_ctx_pg_info *ctx_pg; 8436 struct bnxt_ctx_mem_type *ctxm; 8437 void **__req = (void **)&req; 8438 u32 req_len = sizeof(*req); 8439 __le32 *num_entries; 8440 __le64 *pg_dir; 8441 u32 flags = 0; 8442 u8 *pg_attr; 8443 u32 ena; 8444 int rc; 8445 int i; 8446 8447 if (!ctx) 8448 return 0; 8449 8450 if (req_len > bp->hwrm_max_ext_req_len) 8451 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8452 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8453 if (rc) 8454 return rc; 8455 8456 req->enables = cpu_to_le32(enables); 8457 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8458 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8459 ctx_pg = ctxm->pg_info; 8460 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8461 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8462 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8463 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8464 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8465 &req->qpc_pg_size_qpc_lvl, 8466 &req->qpc_page_dir); 8467 8468 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8469 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8470 } 8471 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8472 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8473 ctx_pg = ctxm->pg_info; 8474 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8475 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8476 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8477 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8478 &req->srq_pg_size_srq_lvl, 8479 &req->srq_page_dir); 8480 } 8481 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8482 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8483 ctx_pg = ctxm->pg_info; 8484 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8485 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8486 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8487 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8488 &req->cq_pg_size_cq_lvl, 8489 &req->cq_page_dir); 8490 } 8491 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8492 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8493 ctx_pg = ctxm->pg_info; 8494 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8495 req->vnic_num_ring_table_entries = 8496 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8497 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8498 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8499 &req->vnic_pg_size_vnic_lvl, 8500 &req->vnic_page_dir); 8501 } 8502 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8503 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8504 ctx_pg = ctxm->pg_info; 8505 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8506 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8507 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8508 &req->stat_pg_size_stat_lvl, 8509 &req->stat_page_dir); 8510 } 8511 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8512 u32 units; 8513 8514 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8515 ctx_pg = ctxm->pg_info; 8516 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8517 units = ctxm->mrav_num_entries_units; 8518 if (units) { 8519 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8520 u32 entries; 8521 8522 num_mr = ctx_pg->entries - num_ah; 8523 entries = ((num_mr / units) << 16) | (num_ah / units); 8524 req->mrav_num_entries = cpu_to_le32(entries); 8525 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8526 } 8527 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8528 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8529 &req->mrav_pg_size_mrav_lvl, 8530 &req->mrav_page_dir); 8531 } 8532 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8533 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8534 ctx_pg = ctxm->pg_info; 8535 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8536 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8537 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8538 &req->tim_pg_size_tim_lvl, 8539 &req->tim_page_dir); 8540 } 8541 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8542 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8543 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8544 pg_dir = &req->tqm_sp_page_dir, 8545 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8546 ctx_pg = ctxm->pg_info; 8547 i < BNXT_MAX_TQM_RINGS; 8548 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8549 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8550 if (!(enables & ena)) 8551 continue; 8552 8553 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8554 *num_entries = cpu_to_le32(ctx_pg->entries); 8555 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8556 } 8557 req->flags = cpu_to_le32(flags); 8558 return hwrm_req_send(bp, req); 8559 } 8560 8561 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8562 struct bnxt_ctx_pg_info *ctx_pg) 8563 { 8564 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8565 8566 rmem->page_size = BNXT_PAGE_SIZE; 8567 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8568 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8569 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8570 if (rmem->depth >= 1) 8571 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8572 return bnxt_alloc_ring(bp, rmem); 8573 } 8574 8575 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8576 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8577 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8578 { 8579 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8580 int rc; 8581 8582 if (!mem_size) 8583 return -EINVAL; 8584 8585 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8586 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8587 ctx_pg->nr_pages = 0; 8588 return -EINVAL; 8589 } 8590 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8591 int nr_tbls, i; 8592 8593 rmem->depth = 2; 8594 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8595 GFP_KERNEL); 8596 if (!ctx_pg->ctx_pg_tbl) 8597 return -ENOMEM; 8598 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8599 rmem->nr_pages = nr_tbls; 8600 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8601 if (rc) 8602 return rc; 8603 for (i = 0; i < nr_tbls; i++) { 8604 struct bnxt_ctx_pg_info *pg_tbl; 8605 8606 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8607 if (!pg_tbl) 8608 return -ENOMEM; 8609 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8610 rmem = &pg_tbl->ring_mem; 8611 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8612 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8613 rmem->depth = 1; 8614 rmem->nr_pages = MAX_CTX_PAGES; 8615 rmem->ctx_mem = ctxm; 8616 if (i == (nr_tbls - 1)) { 8617 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8618 8619 if (rem) 8620 rmem->nr_pages = rem; 8621 } 8622 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8623 if (rc) 8624 break; 8625 } 8626 } else { 8627 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8628 if (rmem->nr_pages > 1 || depth) 8629 rmem->depth = 1; 8630 rmem->ctx_mem = ctxm; 8631 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8632 } 8633 return rc; 8634 } 8635 8636 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 8637 struct bnxt_ctx_pg_info *ctx_pg) 8638 { 8639 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8640 8641 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 8642 ctx_pg->ctx_pg_tbl) { 8643 int i, nr_tbls = rmem->nr_pages; 8644 8645 for (i = 0; i < nr_tbls; i++) { 8646 struct bnxt_ctx_pg_info *pg_tbl; 8647 struct bnxt_ring_mem_info *rmem2; 8648 8649 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 8650 if (!pg_tbl) 8651 continue; 8652 rmem2 = &pg_tbl->ring_mem; 8653 bnxt_free_ring(bp, rmem2); 8654 ctx_pg->ctx_pg_arr[i] = NULL; 8655 kfree(pg_tbl); 8656 ctx_pg->ctx_pg_tbl[i] = NULL; 8657 } 8658 kfree(ctx_pg->ctx_pg_tbl); 8659 ctx_pg->ctx_pg_tbl = NULL; 8660 } 8661 bnxt_free_ring(bp, rmem); 8662 ctx_pg->nr_pages = 0; 8663 } 8664 8665 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 8666 struct bnxt_ctx_mem_type *ctxm, u32 entries, 8667 u8 pg_lvl) 8668 { 8669 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 8670 int i, rc = 0, n = 1; 8671 u32 mem_size; 8672 8673 if (!ctxm->entry_size || !ctx_pg) 8674 return -EINVAL; 8675 if (ctxm->instance_bmap) 8676 n = hweight32(ctxm->instance_bmap); 8677 if (ctxm->entry_multiple) 8678 entries = roundup(entries, ctxm->entry_multiple); 8679 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 8680 mem_size = entries * ctxm->entry_size; 8681 for (i = 0; i < n && !rc; i++) { 8682 ctx_pg[i].entries = entries; 8683 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 8684 ctxm->init_value ? ctxm : NULL); 8685 } 8686 return rc; 8687 } 8688 8689 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 8690 struct bnxt_ctx_mem_type *ctxm, 8691 bool last) 8692 { 8693 struct hwrm_func_backing_store_cfg_v2_input *req; 8694 u32 instance_bmap = ctxm->instance_bmap; 8695 int i, j, rc = 0, n = 1; 8696 __le32 *p; 8697 8698 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 8699 return 0; 8700 8701 if (instance_bmap) 8702 n = hweight32(ctxm->instance_bmap); 8703 else 8704 instance_bmap = 1; 8705 8706 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 8707 if (rc) 8708 return rc; 8709 hwrm_req_hold(bp, req); 8710 req->type = cpu_to_le16(ctxm->type); 8711 req->entry_size = cpu_to_le16(ctxm->entry_size); 8712 req->subtype_valid_cnt = ctxm->split_entry_cnt; 8713 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 8714 p[i] = cpu_to_le32(ctxm->split[i]); 8715 for (i = 0, j = 0; j < n && !rc; i++) { 8716 struct bnxt_ctx_pg_info *ctx_pg; 8717 8718 if (!(instance_bmap & (1 << i))) 8719 continue; 8720 req->instance = cpu_to_le16(i); 8721 ctx_pg = &ctxm->pg_info[j++]; 8722 if (!ctx_pg->entries) 8723 continue; 8724 req->num_entries = cpu_to_le32(ctx_pg->entries); 8725 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8726 &req->page_size_pbl_level, 8727 &req->page_dir); 8728 if (last && j == n) 8729 req->flags = 8730 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 8731 rc = hwrm_req_send(bp, req); 8732 } 8733 hwrm_req_drop(bp, req); 8734 return rc; 8735 } 8736 8737 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena) 8738 { 8739 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8740 struct bnxt_ctx_mem_type *ctxm; 8741 u16 last_type; 8742 int rc = 0; 8743 u16 type; 8744 8745 if (!ena) 8746 return 0; 8747 else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) 8748 last_type = BNXT_CTX_MAX - 1; 8749 else 8750 last_type = BNXT_CTX_L2_MAX - 1; 8751 ctx->ctx_arr[last_type].last = 1; 8752 8753 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 8754 ctxm = &ctx->ctx_arr[type]; 8755 8756 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 8757 if (rc) 8758 return rc; 8759 } 8760 return 0; 8761 } 8762 8763 void bnxt_free_ctx_mem(struct bnxt *bp) 8764 { 8765 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8766 u16 type; 8767 8768 if (!ctx) 8769 return; 8770 8771 for (type = 0; type < BNXT_CTX_V2_MAX; type++) { 8772 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8773 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 8774 int i, n = 1; 8775 8776 if (!ctx_pg) 8777 continue; 8778 if (ctxm->instance_bmap) 8779 n = hweight32(ctxm->instance_bmap); 8780 for (i = 0; i < n; i++) 8781 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 8782 8783 kfree(ctx_pg); 8784 ctxm->pg_info = NULL; 8785 } 8786 8787 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 8788 kfree(ctx); 8789 bp->ctx = NULL; 8790 } 8791 8792 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 8793 { 8794 struct bnxt_ctx_mem_type *ctxm; 8795 struct bnxt_ctx_mem_info *ctx; 8796 u32 l2_qps, qp1_qps, max_qps; 8797 u32 ena, entries_sp, entries; 8798 u32 srqs, max_srqs, min; 8799 u32 num_mr, num_ah; 8800 u32 extra_srqs = 0; 8801 u32 extra_qps = 0; 8802 u32 fast_qpmd_qps; 8803 u8 pg_lvl = 1; 8804 int i, rc; 8805 8806 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 8807 if (rc) { 8808 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 8809 rc); 8810 return rc; 8811 } 8812 ctx = bp->ctx; 8813 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 8814 return 0; 8815 8816 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8817 l2_qps = ctxm->qp_l2_entries; 8818 qp1_qps = ctxm->qp_qp1_entries; 8819 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 8820 max_qps = ctxm->max_entries; 8821 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8822 srqs = ctxm->srq_l2_entries; 8823 max_srqs = ctxm->max_entries; 8824 ena = 0; 8825 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 8826 pg_lvl = 2; 8827 extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps); 8828 /* allocate extra qps if fw supports RoCE fast qp destroy feature */ 8829 extra_qps += fast_qpmd_qps; 8830 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 8831 if (fast_qpmd_qps) 8832 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 8833 } 8834 8835 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8836 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 8837 pg_lvl); 8838 if (rc) 8839 return rc; 8840 8841 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8842 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 8843 if (rc) 8844 return rc; 8845 8846 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8847 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 8848 extra_qps * 2, pg_lvl); 8849 if (rc) 8850 return rc; 8851 8852 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8853 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 8854 if (rc) 8855 return rc; 8856 8857 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8858 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 8859 if (rc) 8860 return rc; 8861 8862 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 8863 goto skip_rdma; 8864 8865 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8866 /* 128K extra is needed to accommodate static AH context 8867 * allocation by f/w. 8868 */ 8869 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 8870 num_ah = min_t(u32, num_mr, 1024 * 128); 8871 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 8872 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 8873 ctxm->mrav_av_entries = num_ah; 8874 8875 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 8876 if (rc) 8877 return rc; 8878 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 8879 8880 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8881 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 8882 if (rc) 8883 return rc; 8884 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 8885 8886 skip_rdma: 8887 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8888 min = ctxm->min_entries; 8889 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 8890 2 * (extra_qps + qp1_qps) + min; 8891 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 8892 if (rc) 8893 return rc; 8894 8895 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8896 entries = l2_qps + 2 * (extra_qps + qp1_qps); 8897 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 8898 if (rc) 8899 return rc; 8900 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 8901 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 8902 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 8903 8904 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8905 rc = bnxt_backing_store_cfg_v2(bp, ena); 8906 else 8907 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 8908 if (rc) { 8909 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 8910 rc); 8911 return rc; 8912 } 8913 ctx->flags |= BNXT_CTX_FLAG_INITED; 8914 return 0; 8915 } 8916 8917 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 8918 { 8919 struct hwrm_dbg_crashdump_medium_cfg_input *req; 8920 u16 page_attr; 8921 int rc; 8922 8923 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 8924 return 0; 8925 8926 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 8927 if (rc) 8928 return rc; 8929 8930 if (BNXT_PAGE_SIZE == 0x2000) 8931 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 8932 else if (BNXT_PAGE_SIZE == 0x10000) 8933 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 8934 else 8935 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 8936 req->pg_size_lvl = cpu_to_le16(page_attr | 8937 bp->fw_crash_mem->ring_mem.depth); 8938 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 8939 req->size = cpu_to_le32(bp->fw_crash_len); 8940 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 8941 return hwrm_req_send(bp, req); 8942 } 8943 8944 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 8945 { 8946 if (bp->fw_crash_mem) { 8947 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 8948 kfree(bp->fw_crash_mem); 8949 bp->fw_crash_mem = NULL; 8950 } 8951 } 8952 8953 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 8954 { 8955 u32 mem_size = 0; 8956 int rc; 8957 8958 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 8959 return 0; 8960 8961 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 8962 if (rc) 8963 return rc; 8964 8965 mem_size = round_up(mem_size, 4); 8966 8967 /* keep and use the existing pages */ 8968 if (bp->fw_crash_mem && 8969 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 8970 goto alloc_done; 8971 8972 if (bp->fw_crash_mem) 8973 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 8974 else 8975 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 8976 GFP_KERNEL); 8977 if (!bp->fw_crash_mem) 8978 return -ENOMEM; 8979 8980 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 8981 if (rc) { 8982 bnxt_free_crash_dump_mem(bp); 8983 return rc; 8984 } 8985 8986 alloc_done: 8987 bp->fw_crash_len = mem_size; 8988 return 0; 8989 } 8990 8991 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 8992 { 8993 struct hwrm_func_resource_qcaps_output *resp; 8994 struct hwrm_func_resource_qcaps_input *req; 8995 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8996 int rc; 8997 8998 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 8999 if (rc) 9000 return rc; 9001 9002 req->fid = cpu_to_le16(0xffff); 9003 resp = hwrm_req_hold(bp, req); 9004 rc = hwrm_req_send_silent(bp, req); 9005 if (rc) 9006 goto hwrm_func_resc_qcaps_exit; 9007 9008 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9009 if (!all) 9010 goto hwrm_func_resc_qcaps_exit; 9011 9012 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9013 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9014 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9015 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9016 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9017 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9018 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9019 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9020 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9021 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9022 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9023 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9024 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9025 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9026 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9027 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9028 9029 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9030 u16 max_msix = le16_to_cpu(resp->max_msix); 9031 9032 hw_resc->max_nqs = max_msix; 9033 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9034 } 9035 9036 if (BNXT_PF(bp)) { 9037 struct bnxt_pf_info *pf = &bp->pf; 9038 9039 pf->vf_resv_strategy = 9040 le16_to_cpu(resp->vf_reservation_strategy); 9041 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9042 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9043 } 9044 hwrm_func_resc_qcaps_exit: 9045 hwrm_req_drop(bp, req); 9046 return rc; 9047 } 9048 9049 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9050 { 9051 struct hwrm_port_mac_ptp_qcfg_output *resp; 9052 struct hwrm_port_mac_ptp_qcfg_input *req; 9053 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9054 bool phc_cfg; 9055 u8 flags; 9056 int rc; 9057 9058 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9059 rc = -ENODEV; 9060 goto no_ptp; 9061 } 9062 9063 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9064 if (rc) 9065 goto no_ptp; 9066 9067 req->port_id = cpu_to_le16(bp->pf.port_id); 9068 resp = hwrm_req_hold(bp, req); 9069 rc = hwrm_req_send(bp, req); 9070 if (rc) 9071 goto exit; 9072 9073 flags = resp->flags; 9074 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9075 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9076 rc = -ENODEV; 9077 goto exit; 9078 } 9079 if (!ptp) { 9080 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9081 if (!ptp) { 9082 rc = -ENOMEM; 9083 goto exit; 9084 } 9085 ptp->bp = bp; 9086 bp->ptp_cfg = ptp; 9087 } 9088 9089 if (flags & 9090 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9091 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9092 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9093 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9094 } else if (BNXT_CHIP_P5(bp)) { 9095 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9096 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9097 } else { 9098 rc = -ENODEV; 9099 goto exit; 9100 } 9101 phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9102 rc = bnxt_ptp_init(bp, phc_cfg); 9103 if (rc) 9104 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9105 exit: 9106 hwrm_req_drop(bp, req); 9107 if (!rc) 9108 return 0; 9109 9110 no_ptp: 9111 bnxt_ptp_clear(bp); 9112 kfree(ptp); 9113 bp->ptp_cfg = NULL; 9114 return rc; 9115 } 9116 9117 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9118 { 9119 struct hwrm_func_qcaps_output *resp; 9120 struct hwrm_func_qcaps_input *req; 9121 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9122 u32 flags, flags_ext, flags_ext2; 9123 int rc; 9124 9125 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9126 if (rc) 9127 return rc; 9128 9129 req->fid = cpu_to_le16(0xffff); 9130 resp = hwrm_req_hold(bp, req); 9131 rc = hwrm_req_send(bp, req); 9132 if (rc) 9133 goto hwrm_func_qcaps_exit; 9134 9135 flags = le32_to_cpu(resp->flags); 9136 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9137 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9138 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9139 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9140 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9141 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9142 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9143 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9144 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9145 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9146 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9147 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9148 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9149 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9150 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9151 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9152 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9153 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9154 9155 flags_ext = le32_to_cpu(resp->flags_ext); 9156 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9157 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9158 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9159 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9160 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9161 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9162 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9163 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9164 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9165 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9166 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9167 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9168 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9169 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9170 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9171 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9172 9173 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9174 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9175 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9176 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9177 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9178 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9179 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9180 9181 bp->tx_push_thresh = 0; 9182 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9183 BNXT_FW_MAJ(bp) > 217) 9184 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9185 9186 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9187 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9188 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9189 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9190 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9191 if (!hw_resc->max_hw_ring_grps) 9192 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9193 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9194 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9195 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9196 9197 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9198 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9199 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9200 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9201 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9202 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9203 9204 if (BNXT_PF(bp)) { 9205 struct bnxt_pf_info *pf = &bp->pf; 9206 9207 pf->fw_fid = le16_to_cpu(resp->fid); 9208 pf->port_id = le16_to_cpu(resp->port_id); 9209 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9210 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9211 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9212 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9213 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9214 bp->flags |= BNXT_FLAG_WOL_CAP; 9215 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9216 bp->fw_cap |= BNXT_FW_CAP_PTP; 9217 } else { 9218 bnxt_ptp_clear(bp); 9219 kfree(bp->ptp_cfg); 9220 bp->ptp_cfg = NULL; 9221 } 9222 } else { 9223 #ifdef CONFIG_BNXT_SRIOV 9224 struct bnxt_vf_info *vf = &bp->vf; 9225 9226 vf->fw_fid = le16_to_cpu(resp->fid); 9227 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9228 #endif 9229 } 9230 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9231 9232 hwrm_func_qcaps_exit: 9233 hwrm_req_drop(bp, req); 9234 return rc; 9235 } 9236 9237 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9238 { 9239 struct hwrm_dbg_qcaps_output *resp; 9240 struct hwrm_dbg_qcaps_input *req; 9241 int rc; 9242 9243 bp->fw_dbg_cap = 0; 9244 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9245 return; 9246 9247 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9248 if (rc) 9249 return; 9250 9251 req->fid = cpu_to_le16(0xffff); 9252 resp = hwrm_req_hold(bp, req); 9253 rc = hwrm_req_send(bp, req); 9254 if (rc) 9255 goto hwrm_dbg_qcaps_exit; 9256 9257 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9258 9259 hwrm_dbg_qcaps_exit: 9260 hwrm_req_drop(bp, req); 9261 } 9262 9263 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9264 9265 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9266 { 9267 int rc; 9268 9269 rc = __bnxt_hwrm_func_qcaps(bp); 9270 if (rc) 9271 return rc; 9272 9273 bnxt_hwrm_dbg_qcaps(bp); 9274 9275 rc = bnxt_hwrm_queue_qportcfg(bp); 9276 if (rc) { 9277 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9278 return rc; 9279 } 9280 if (bp->hwrm_spec_code >= 0x10803) { 9281 rc = bnxt_alloc_ctx_mem(bp); 9282 if (rc) 9283 return rc; 9284 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9285 if (!rc) 9286 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9287 } 9288 return 0; 9289 } 9290 9291 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9292 { 9293 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9294 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9295 u32 flags; 9296 int rc; 9297 9298 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9299 return 0; 9300 9301 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9302 if (rc) 9303 return rc; 9304 9305 resp = hwrm_req_hold(bp, req); 9306 rc = hwrm_req_send(bp, req); 9307 if (rc) 9308 goto hwrm_cfa_adv_qcaps_exit; 9309 9310 flags = le32_to_cpu(resp->flags); 9311 if (flags & 9312 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9313 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9314 9315 if (flags & 9316 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9317 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9318 9319 if (flags & 9320 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9321 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9322 9323 hwrm_cfa_adv_qcaps_exit: 9324 hwrm_req_drop(bp, req); 9325 return rc; 9326 } 9327 9328 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9329 { 9330 if (bp->fw_health) 9331 return 0; 9332 9333 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9334 if (!bp->fw_health) 9335 return -ENOMEM; 9336 9337 mutex_init(&bp->fw_health->lock); 9338 return 0; 9339 } 9340 9341 static int bnxt_alloc_fw_health(struct bnxt *bp) 9342 { 9343 int rc; 9344 9345 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9346 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9347 return 0; 9348 9349 rc = __bnxt_alloc_fw_health(bp); 9350 if (rc) { 9351 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9352 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9353 return rc; 9354 } 9355 9356 return 0; 9357 } 9358 9359 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9360 { 9361 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9362 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9363 BNXT_FW_HEALTH_WIN_MAP_OFF); 9364 } 9365 9366 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9367 { 9368 struct bnxt_fw_health *fw_health = bp->fw_health; 9369 u32 reg_type; 9370 9371 if (!fw_health) 9372 return; 9373 9374 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9375 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9376 fw_health->status_reliable = false; 9377 9378 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9379 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9380 fw_health->resets_reliable = false; 9381 } 9382 9383 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9384 { 9385 void __iomem *hs; 9386 u32 status_loc; 9387 u32 reg_type; 9388 u32 sig; 9389 9390 if (bp->fw_health) 9391 bp->fw_health->status_reliable = false; 9392 9393 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9394 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9395 9396 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9397 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9398 if (!bp->chip_num) { 9399 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9400 bp->chip_num = readl(bp->bar0 + 9401 BNXT_FW_HEALTH_WIN_BASE + 9402 BNXT_GRC_REG_CHIP_NUM); 9403 } 9404 if (!BNXT_CHIP_P5_PLUS(bp)) 9405 return; 9406 9407 status_loc = BNXT_GRC_REG_STATUS_P5 | 9408 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9409 } else { 9410 status_loc = readl(hs + offsetof(struct hcomm_status, 9411 fw_status_loc)); 9412 } 9413 9414 if (__bnxt_alloc_fw_health(bp)) { 9415 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9416 return; 9417 } 9418 9419 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9420 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9421 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9422 __bnxt_map_fw_health_reg(bp, status_loc); 9423 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9424 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9425 } 9426 9427 bp->fw_health->status_reliable = true; 9428 } 9429 9430 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9431 { 9432 struct bnxt_fw_health *fw_health = bp->fw_health; 9433 u32 reg_base = 0xffffffff; 9434 int i; 9435 9436 bp->fw_health->status_reliable = false; 9437 bp->fw_health->resets_reliable = false; 9438 /* Only pre-map the monitoring GRC registers using window 3 */ 9439 for (i = 0; i < 4; i++) { 9440 u32 reg = fw_health->regs[i]; 9441 9442 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9443 continue; 9444 if (reg_base == 0xffffffff) 9445 reg_base = reg & BNXT_GRC_BASE_MASK; 9446 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9447 return -ERANGE; 9448 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9449 } 9450 bp->fw_health->status_reliable = true; 9451 bp->fw_health->resets_reliable = true; 9452 if (reg_base == 0xffffffff) 9453 return 0; 9454 9455 __bnxt_map_fw_health_reg(bp, reg_base); 9456 return 0; 9457 } 9458 9459 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9460 { 9461 if (!bp->fw_health) 9462 return; 9463 9464 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9465 bp->fw_health->status_reliable = true; 9466 bp->fw_health->resets_reliable = true; 9467 } else { 9468 bnxt_try_map_fw_health_reg(bp); 9469 } 9470 } 9471 9472 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 9473 { 9474 struct bnxt_fw_health *fw_health = bp->fw_health; 9475 struct hwrm_error_recovery_qcfg_output *resp; 9476 struct hwrm_error_recovery_qcfg_input *req; 9477 int rc, i; 9478 9479 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9480 return 0; 9481 9482 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 9483 if (rc) 9484 return rc; 9485 9486 resp = hwrm_req_hold(bp, req); 9487 rc = hwrm_req_send(bp, req); 9488 if (rc) 9489 goto err_recovery_out; 9490 fw_health->flags = le32_to_cpu(resp->flags); 9491 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 9492 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 9493 rc = -EINVAL; 9494 goto err_recovery_out; 9495 } 9496 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 9497 fw_health->master_func_wait_dsecs = 9498 le32_to_cpu(resp->master_func_wait_period); 9499 fw_health->normal_func_wait_dsecs = 9500 le32_to_cpu(resp->normal_func_wait_period); 9501 fw_health->post_reset_wait_dsecs = 9502 le32_to_cpu(resp->master_func_wait_period_after_reset); 9503 fw_health->post_reset_max_wait_dsecs = 9504 le32_to_cpu(resp->max_bailout_time_after_reset); 9505 fw_health->regs[BNXT_FW_HEALTH_REG] = 9506 le32_to_cpu(resp->fw_health_status_reg); 9507 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 9508 le32_to_cpu(resp->fw_heartbeat_reg); 9509 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 9510 le32_to_cpu(resp->fw_reset_cnt_reg); 9511 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 9512 le32_to_cpu(resp->reset_inprogress_reg); 9513 fw_health->fw_reset_inprog_reg_mask = 9514 le32_to_cpu(resp->reset_inprogress_reg_mask); 9515 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 9516 if (fw_health->fw_reset_seq_cnt >= 16) { 9517 rc = -EINVAL; 9518 goto err_recovery_out; 9519 } 9520 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 9521 fw_health->fw_reset_seq_regs[i] = 9522 le32_to_cpu(resp->reset_reg[i]); 9523 fw_health->fw_reset_seq_vals[i] = 9524 le32_to_cpu(resp->reset_reg_val[i]); 9525 fw_health->fw_reset_seq_delay_msec[i] = 9526 resp->delay_after_reset[i]; 9527 } 9528 err_recovery_out: 9529 hwrm_req_drop(bp, req); 9530 if (!rc) 9531 rc = bnxt_map_fw_health_regs(bp); 9532 if (rc) 9533 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9534 return rc; 9535 } 9536 9537 static int bnxt_hwrm_func_reset(struct bnxt *bp) 9538 { 9539 struct hwrm_func_reset_input *req; 9540 int rc; 9541 9542 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 9543 if (rc) 9544 return rc; 9545 9546 req->enables = 0; 9547 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 9548 return hwrm_req_send(bp, req); 9549 } 9550 9551 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 9552 { 9553 struct hwrm_nvm_get_dev_info_output nvm_info; 9554 9555 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 9556 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 9557 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 9558 nvm_info.nvm_cfg_ver_upd); 9559 } 9560 9561 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 9562 { 9563 struct hwrm_queue_qportcfg_output *resp; 9564 struct hwrm_queue_qportcfg_input *req; 9565 u8 i, j, *qptr; 9566 bool no_rdma; 9567 int rc = 0; 9568 9569 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 9570 if (rc) 9571 return rc; 9572 9573 resp = hwrm_req_hold(bp, req); 9574 rc = hwrm_req_send(bp, req); 9575 if (rc) 9576 goto qportcfg_exit; 9577 9578 if (!resp->max_configurable_queues) { 9579 rc = -EINVAL; 9580 goto qportcfg_exit; 9581 } 9582 bp->max_tc = resp->max_configurable_queues; 9583 bp->max_lltc = resp->max_configurable_lossless_queues; 9584 if (bp->max_tc > BNXT_MAX_QUEUE) 9585 bp->max_tc = BNXT_MAX_QUEUE; 9586 9587 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 9588 qptr = &resp->queue_id0; 9589 for (i = 0, j = 0; i < bp->max_tc; i++) { 9590 bp->q_info[j].queue_id = *qptr; 9591 bp->q_ids[i] = *qptr++; 9592 bp->q_info[j].queue_profile = *qptr++; 9593 bp->tc_to_qidx[j] = j; 9594 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 9595 (no_rdma && BNXT_PF(bp))) 9596 j++; 9597 } 9598 bp->max_q = bp->max_tc; 9599 bp->max_tc = max_t(u8, j, 1); 9600 9601 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 9602 bp->max_tc = 1; 9603 9604 if (bp->max_lltc > bp->max_tc) 9605 bp->max_lltc = bp->max_tc; 9606 9607 qportcfg_exit: 9608 hwrm_req_drop(bp, req); 9609 return rc; 9610 } 9611 9612 static int bnxt_hwrm_poll(struct bnxt *bp) 9613 { 9614 struct hwrm_ver_get_input *req; 9615 int rc; 9616 9617 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9618 if (rc) 9619 return rc; 9620 9621 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9622 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9623 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9624 9625 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 9626 rc = hwrm_req_send(bp, req); 9627 return rc; 9628 } 9629 9630 static int bnxt_hwrm_ver_get(struct bnxt *bp) 9631 { 9632 struct hwrm_ver_get_output *resp; 9633 struct hwrm_ver_get_input *req; 9634 u16 fw_maj, fw_min, fw_bld, fw_rsv; 9635 u32 dev_caps_cfg, hwrm_ver; 9636 int rc, len; 9637 9638 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9639 if (rc) 9640 return rc; 9641 9642 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 9643 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 9644 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9645 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9646 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9647 9648 resp = hwrm_req_hold(bp, req); 9649 rc = hwrm_req_send(bp, req); 9650 if (rc) 9651 goto hwrm_ver_get_exit; 9652 9653 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 9654 9655 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 9656 resp->hwrm_intf_min_8b << 8 | 9657 resp->hwrm_intf_upd_8b; 9658 if (resp->hwrm_intf_maj_8b < 1) { 9659 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 9660 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 9661 resp->hwrm_intf_upd_8b); 9662 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 9663 } 9664 9665 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 9666 HWRM_VERSION_UPDATE; 9667 9668 if (bp->hwrm_spec_code > hwrm_ver) 9669 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 9670 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 9671 HWRM_VERSION_UPDATE); 9672 else 9673 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 9674 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 9675 resp->hwrm_intf_upd_8b); 9676 9677 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 9678 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 9679 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 9680 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 9681 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 9682 len = FW_VER_STR_LEN; 9683 } else { 9684 fw_maj = resp->hwrm_fw_maj_8b; 9685 fw_min = resp->hwrm_fw_min_8b; 9686 fw_bld = resp->hwrm_fw_bld_8b; 9687 fw_rsv = resp->hwrm_fw_rsvd_8b; 9688 len = BC_HWRM_STR_LEN; 9689 } 9690 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 9691 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 9692 fw_rsv); 9693 9694 if (strlen(resp->active_pkg_name)) { 9695 int fw_ver_len = strlen(bp->fw_ver_str); 9696 9697 snprintf(bp->fw_ver_str + fw_ver_len, 9698 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 9699 resp->active_pkg_name); 9700 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 9701 } 9702 9703 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 9704 if (!bp->hwrm_cmd_timeout) 9705 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 9706 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 9707 if (!bp->hwrm_cmd_max_timeout) 9708 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 9709 else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT) 9710 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n", 9711 bp->hwrm_cmd_max_timeout / 1000); 9712 9713 if (resp->hwrm_intf_maj_8b >= 1) { 9714 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 9715 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 9716 } 9717 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 9718 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 9719 9720 bp->chip_num = le16_to_cpu(resp->chip_num); 9721 bp->chip_rev = resp->chip_rev; 9722 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 9723 !resp->chip_metal) 9724 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 9725 9726 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 9727 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 9728 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 9729 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 9730 9731 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 9732 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 9733 9734 if (dev_caps_cfg & 9735 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 9736 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 9737 9738 if (dev_caps_cfg & 9739 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 9740 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 9741 9742 if (dev_caps_cfg & 9743 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 9744 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 9745 9746 hwrm_ver_get_exit: 9747 hwrm_req_drop(bp, req); 9748 return rc; 9749 } 9750 9751 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 9752 { 9753 struct hwrm_fw_set_time_input *req; 9754 struct tm tm; 9755 time64_t now = ktime_get_real_seconds(); 9756 int rc; 9757 9758 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 9759 bp->hwrm_spec_code < 0x10400) 9760 return -EOPNOTSUPP; 9761 9762 time64_to_tm(now, 0, &tm); 9763 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 9764 if (rc) 9765 return rc; 9766 9767 req->year = cpu_to_le16(1900 + tm.tm_year); 9768 req->month = 1 + tm.tm_mon; 9769 req->day = tm.tm_mday; 9770 req->hour = tm.tm_hour; 9771 req->minute = tm.tm_min; 9772 req->second = tm.tm_sec; 9773 return hwrm_req_send(bp, req); 9774 } 9775 9776 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 9777 { 9778 u64 sw_tmp; 9779 9780 hw &= mask; 9781 sw_tmp = (*sw & ~mask) | hw; 9782 if (hw < (*sw & mask)) 9783 sw_tmp += mask + 1; 9784 WRITE_ONCE(*sw, sw_tmp); 9785 } 9786 9787 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 9788 int count, bool ignore_zero) 9789 { 9790 int i; 9791 9792 for (i = 0; i < count; i++) { 9793 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 9794 9795 if (ignore_zero && !hw) 9796 continue; 9797 9798 if (masks[i] == -1ULL) 9799 sw_stats[i] = hw; 9800 else 9801 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 9802 } 9803 } 9804 9805 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 9806 { 9807 if (!stats->hw_stats) 9808 return; 9809 9810 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 9811 stats->hw_masks, stats->len / 8, false); 9812 } 9813 9814 static void bnxt_accumulate_all_stats(struct bnxt *bp) 9815 { 9816 struct bnxt_stats_mem *ring0_stats; 9817 bool ignore_zero = false; 9818 int i; 9819 9820 /* Chip bug. Counter intermittently becomes 0. */ 9821 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 9822 ignore_zero = true; 9823 9824 for (i = 0; i < bp->cp_nr_rings; i++) { 9825 struct bnxt_napi *bnapi = bp->bnapi[i]; 9826 struct bnxt_cp_ring_info *cpr; 9827 struct bnxt_stats_mem *stats; 9828 9829 cpr = &bnapi->cp_ring; 9830 stats = &cpr->stats; 9831 if (!i) 9832 ring0_stats = stats; 9833 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 9834 ring0_stats->hw_masks, 9835 ring0_stats->len / 8, ignore_zero); 9836 } 9837 if (bp->flags & BNXT_FLAG_PORT_STATS) { 9838 struct bnxt_stats_mem *stats = &bp->port_stats; 9839 __le64 *hw_stats = stats->hw_stats; 9840 u64 *sw_stats = stats->sw_stats; 9841 u64 *masks = stats->hw_masks; 9842 int cnt; 9843 9844 cnt = sizeof(struct rx_port_stats) / 8; 9845 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 9846 9847 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9848 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9849 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9850 cnt = sizeof(struct tx_port_stats) / 8; 9851 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 9852 } 9853 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 9854 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 9855 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 9856 } 9857 } 9858 9859 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 9860 { 9861 struct hwrm_port_qstats_input *req; 9862 struct bnxt_pf_info *pf = &bp->pf; 9863 int rc; 9864 9865 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 9866 return 0; 9867 9868 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 9869 return -EOPNOTSUPP; 9870 9871 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 9872 if (rc) 9873 return rc; 9874 9875 req->flags = flags; 9876 req->port_id = cpu_to_le16(pf->port_id); 9877 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 9878 BNXT_TX_PORT_STATS_BYTE_OFFSET); 9879 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 9880 return hwrm_req_send(bp, req); 9881 } 9882 9883 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 9884 { 9885 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 9886 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 9887 struct hwrm_port_qstats_ext_output *resp_qs; 9888 struct hwrm_port_qstats_ext_input *req_qs; 9889 struct bnxt_pf_info *pf = &bp->pf; 9890 u32 tx_stat_size; 9891 int rc; 9892 9893 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 9894 return 0; 9895 9896 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 9897 return -EOPNOTSUPP; 9898 9899 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 9900 if (rc) 9901 return rc; 9902 9903 req_qs->flags = flags; 9904 req_qs->port_id = cpu_to_le16(pf->port_id); 9905 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 9906 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 9907 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 9908 sizeof(struct tx_port_stats_ext) : 0; 9909 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 9910 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 9911 resp_qs = hwrm_req_hold(bp, req_qs); 9912 rc = hwrm_req_send(bp, req_qs); 9913 if (!rc) { 9914 bp->fw_rx_stats_ext_size = 9915 le16_to_cpu(resp_qs->rx_stat_size) / 8; 9916 if (BNXT_FW_MAJ(bp) < 220 && 9917 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 9918 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 9919 9920 bp->fw_tx_stats_ext_size = tx_stat_size ? 9921 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 9922 } else { 9923 bp->fw_rx_stats_ext_size = 0; 9924 bp->fw_tx_stats_ext_size = 0; 9925 } 9926 hwrm_req_drop(bp, req_qs); 9927 9928 if (flags) 9929 return rc; 9930 9931 if (bp->fw_tx_stats_ext_size <= 9932 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 9933 bp->pri2cos_valid = 0; 9934 return rc; 9935 } 9936 9937 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 9938 if (rc) 9939 return rc; 9940 9941 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 9942 9943 resp_qc = hwrm_req_hold(bp, req_qc); 9944 rc = hwrm_req_send(bp, req_qc); 9945 if (!rc) { 9946 u8 *pri2cos; 9947 int i, j; 9948 9949 pri2cos = &resp_qc->pri0_cos_queue_id; 9950 for (i = 0; i < 8; i++) { 9951 u8 queue_id = pri2cos[i]; 9952 u8 queue_idx; 9953 9954 /* Per port queue IDs start from 0, 10, 20, etc */ 9955 queue_idx = queue_id % 10; 9956 if (queue_idx > BNXT_MAX_QUEUE) { 9957 bp->pri2cos_valid = false; 9958 hwrm_req_drop(bp, req_qc); 9959 return rc; 9960 } 9961 for (j = 0; j < bp->max_q; j++) { 9962 if (bp->q_ids[j] == queue_id) 9963 bp->pri2cos_idx[i] = queue_idx; 9964 } 9965 } 9966 bp->pri2cos_valid = true; 9967 } 9968 hwrm_req_drop(bp, req_qc); 9969 9970 return rc; 9971 } 9972 9973 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 9974 { 9975 bnxt_hwrm_tunnel_dst_port_free(bp, 9976 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 9977 bnxt_hwrm_tunnel_dst_port_free(bp, 9978 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 9979 } 9980 9981 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 9982 { 9983 int rc, i; 9984 u32 tpa_flags = 0; 9985 9986 if (set_tpa) 9987 tpa_flags = bp->flags & BNXT_FLAG_TPA; 9988 else if (BNXT_NO_FW_ACCESS(bp)) 9989 return 0; 9990 for (i = 0; i < bp->nr_vnics; i++) { 9991 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 9992 if (rc) { 9993 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 9994 i, rc); 9995 return rc; 9996 } 9997 } 9998 return 0; 9999 } 10000 10001 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10002 { 10003 int i; 10004 10005 for (i = 0; i < bp->nr_vnics; i++) 10006 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10007 } 10008 10009 static void bnxt_clear_vnic(struct bnxt *bp) 10010 { 10011 if (!bp->vnic_info) 10012 return; 10013 10014 bnxt_hwrm_clear_vnic_filter(bp); 10015 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10016 /* clear all RSS setting before free vnic ctx */ 10017 bnxt_hwrm_clear_vnic_rss(bp); 10018 bnxt_hwrm_vnic_ctx_free(bp); 10019 } 10020 /* before free the vnic, undo the vnic tpa settings */ 10021 if (bp->flags & BNXT_FLAG_TPA) 10022 bnxt_set_tpa(bp, false); 10023 bnxt_hwrm_vnic_free(bp); 10024 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10025 bnxt_hwrm_vnic_ctx_free(bp); 10026 } 10027 10028 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10029 bool irq_re_init) 10030 { 10031 bnxt_clear_vnic(bp); 10032 bnxt_hwrm_ring_free(bp, close_path); 10033 bnxt_hwrm_ring_grp_free(bp); 10034 if (irq_re_init) { 10035 bnxt_hwrm_stat_ctx_free(bp); 10036 bnxt_hwrm_free_tunnel_ports(bp); 10037 } 10038 } 10039 10040 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10041 { 10042 struct hwrm_func_cfg_input *req; 10043 u8 evb_mode; 10044 int rc; 10045 10046 if (br_mode == BRIDGE_MODE_VEB) 10047 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10048 else if (br_mode == BRIDGE_MODE_VEPA) 10049 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10050 else 10051 return -EINVAL; 10052 10053 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10054 if (rc) 10055 return rc; 10056 10057 req->fid = cpu_to_le16(0xffff); 10058 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10059 req->evb_mode = evb_mode; 10060 return hwrm_req_send(bp, req); 10061 } 10062 10063 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10064 { 10065 struct hwrm_func_cfg_input *req; 10066 int rc; 10067 10068 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10069 return 0; 10070 10071 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10072 if (rc) 10073 return rc; 10074 10075 req->fid = cpu_to_le16(0xffff); 10076 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10077 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10078 if (size == 128) 10079 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10080 10081 return hwrm_req_send(bp, req); 10082 } 10083 10084 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10085 { 10086 int rc; 10087 10088 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10089 goto skip_rss_ctx; 10090 10091 /* allocate context for vnic */ 10092 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10093 if (rc) { 10094 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10095 vnic->vnic_id, rc); 10096 goto vnic_setup_err; 10097 } 10098 bp->rsscos_nr_ctxs++; 10099 10100 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10101 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10102 if (rc) { 10103 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10104 vnic->vnic_id, rc); 10105 goto vnic_setup_err; 10106 } 10107 bp->rsscos_nr_ctxs++; 10108 } 10109 10110 skip_rss_ctx: 10111 /* configure default vnic, ring grp */ 10112 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10113 if (rc) { 10114 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10115 vnic->vnic_id, rc); 10116 goto vnic_setup_err; 10117 } 10118 10119 /* Enable RSS hashing on vnic */ 10120 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10121 if (rc) { 10122 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10123 vnic->vnic_id, rc); 10124 goto vnic_setup_err; 10125 } 10126 10127 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10128 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10129 if (rc) { 10130 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10131 vnic->vnic_id, rc); 10132 } 10133 } 10134 10135 vnic_setup_err: 10136 return rc; 10137 } 10138 10139 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10140 u8 valid) 10141 { 10142 struct hwrm_vnic_update_input *req; 10143 int rc; 10144 10145 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10146 if (rc) 10147 return rc; 10148 10149 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10150 10151 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10152 req->mru = cpu_to_le16(vnic->mru); 10153 10154 req->enables = cpu_to_le32(valid); 10155 10156 return hwrm_req_send(bp, req); 10157 } 10158 10159 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10160 { 10161 int rc; 10162 10163 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10164 if (rc) { 10165 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10166 vnic->vnic_id, rc); 10167 return rc; 10168 } 10169 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10170 if (rc) 10171 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10172 vnic->vnic_id, rc); 10173 return rc; 10174 } 10175 10176 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10177 { 10178 int rc, i, nr_ctxs; 10179 10180 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10181 for (i = 0; i < nr_ctxs; i++) { 10182 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10183 if (rc) { 10184 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10185 vnic->vnic_id, i, rc); 10186 break; 10187 } 10188 bp->rsscos_nr_ctxs++; 10189 } 10190 if (i < nr_ctxs) 10191 return -ENOMEM; 10192 10193 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10194 if (rc) 10195 return rc; 10196 10197 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10198 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10199 if (rc) { 10200 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10201 vnic->vnic_id, rc); 10202 } 10203 } 10204 return rc; 10205 } 10206 10207 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10208 { 10209 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10210 return __bnxt_setup_vnic_p5(bp, vnic); 10211 else 10212 return __bnxt_setup_vnic(bp, vnic); 10213 } 10214 10215 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10216 struct bnxt_vnic_info *vnic, 10217 u16 start_rx_ring_idx, int rx_rings) 10218 { 10219 int rc; 10220 10221 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10222 if (rc) { 10223 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10224 vnic->vnic_id, rc); 10225 return rc; 10226 } 10227 return bnxt_setup_vnic(bp, vnic); 10228 } 10229 10230 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10231 { 10232 struct bnxt_vnic_info *vnic; 10233 int i, rc = 0; 10234 10235 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10236 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10237 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10238 } 10239 10240 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10241 return 0; 10242 10243 for (i = 0; i < bp->rx_nr_rings; i++) { 10244 u16 vnic_id = i + 1; 10245 u16 ring_id = i; 10246 10247 if (vnic_id >= bp->nr_vnics) 10248 break; 10249 10250 vnic = &bp->vnic_info[vnic_id]; 10251 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10252 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10253 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10254 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10255 break; 10256 } 10257 return rc; 10258 } 10259 10260 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10261 bool all) 10262 { 10263 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10264 struct bnxt_filter_base *usr_fltr, *tmp; 10265 struct bnxt_ntuple_filter *ntp_fltr; 10266 int i; 10267 10268 if (netif_running(bp->dev)) { 10269 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10270 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10271 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10272 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10273 } 10274 } 10275 if (!all) 10276 return; 10277 10278 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10279 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10280 usr_fltr->fw_vnic_id == rss_ctx->index) { 10281 ntp_fltr = container_of(usr_fltr, 10282 struct bnxt_ntuple_filter, 10283 base); 10284 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10285 bnxt_del_ntp_filter(bp, ntp_fltr); 10286 bnxt_del_one_usr_fltr(bp, usr_fltr); 10287 } 10288 } 10289 10290 if (vnic->rss_table) 10291 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10292 vnic->rss_table, 10293 vnic->rss_table_dma_addr); 10294 bp->num_rss_ctx--; 10295 } 10296 10297 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10298 { 10299 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10300 struct ethtool_rxfh_context *ctx; 10301 unsigned long context; 10302 10303 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10304 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10305 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10306 10307 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10308 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10309 __bnxt_setup_vnic_p5(bp, vnic)) { 10310 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10311 rss_ctx->index); 10312 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10313 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10314 } 10315 } 10316 } 10317 10318 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10319 { 10320 struct ethtool_rxfh_context *ctx; 10321 unsigned long context; 10322 10323 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10324 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10325 10326 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10327 } 10328 } 10329 10330 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10331 static bool bnxt_promisc_ok(struct bnxt *bp) 10332 { 10333 #ifdef CONFIG_BNXT_SRIOV 10334 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10335 return false; 10336 #endif 10337 return true; 10338 } 10339 10340 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10341 { 10342 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10343 unsigned int rc = 0; 10344 10345 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10346 if (rc) { 10347 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10348 rc); 10349 return rc; 10350 } 10351 10352 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10353 if (rc) { 10354 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10355 rc); 10356 return rc; 10357 } 10358 return rc; 10359 } 10360 10361 static int bnxt_cfg_rx_mode(struct bnxt *); 10362 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10363 10364 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10365 { 10366 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10367 int rc = 0; 10368 unsigned int rx_nr_rings = bp->rx_nr_rings; 10369 10370 if (irq_re_init) { 10371 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10372 if (rc) { 10373 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10374 rc); 10375 goto err_out; 10376 } 10377 } 10378 10379 rc = bnxt_hwrm_ring_alloc(bp); 10380 if (rc) { 10381 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10382 goto err_out; 10383 } 10384 10385 rc = bnxt_hwrm_ring_grp_alloc(bp); 10386 if (rc) { 10387 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10388 goto err_out; 10389 } 10390 10391 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10392 rx_nr_rings--; 10393 10394 /* default vnic 0 */ 10395 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10396 if (rc) { 10397 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10398 goto err_out; 10399 } 10400 10401 if (BNXT_VF(bp)) 10402 bnxt_hwrm_func_qcfg(bp); 10403 10404 rc = bnxt_setup_vnic(bp, vnic); 10405 if (rc) 10406 goto err_out; 10407 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 10408 bnxt_hwrm_update_rss_hash_cfg(bp); 10409 10410 if (bp->flags & BNXT_FLAG_RFS) { 10411 rc = bnxt_alloc_rfs_vnics(bp); 10412 if (rc) 10413 goto err_out; 10414 } 10415 10416 if (bp->flags & BNXT_FLAG_TPA) { 10417 rc = bnxt_set_tpa(bp, true); 10418 if (rc) 10419 goto err_out; 10420 } 10421 10422 if (BNXT_VF(bp)) 10423 bnxt_update_vf_mac(bp); 10424 10425 /* Filter for default vnic 0 */ 10426 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 10427 if (rc) { 10428 if (BNXT_VF(bp) && rc == -ENODEV) 10429 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 10430 else 10431 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10432 goto err_out; 10433 } 10434 vnic->uc_filter_count = 1; 10435 10436 vnic->rx_mask = 0; 10437 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 10438 goto skip_rx_mask; 10439 10440 if (bp->dev->flags & IFF_BROADCAST) 10441 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 10442 10443 if (bp->dev->flags & IFF_PROMISC) 10444 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10445 10446 if (bp->dev->flags & IFF_ALLMULTI) { 10447 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10448 vnic->mc_list_count = 0; 10449 } else if (bp->dev->flags & IFF_MULTICAST) { 10450 u32 mask = 0; 10451 10452 bnxt_mc_list_updated(bp, &mask); 10453 vnic->rx_mask |= mask; 10454 } 10455 10456 rc = bnxt_cfg_rx_mode(bp); 10457 if (rc) 10458 goto err_out; 10459 10460 skip_rx_mask: 10461 rc = bnxt_hwrm_set_coal(bp); 10462 if (rc) 10463 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 10464 rc); 10465 10466 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10467 rc = bnxt_setup_nitroa0_vnic(bp); 10468 if (rc) 10469 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 10470 rc); 10471 } 10472 10473 if (BNXT_VF(bp)) { 10474 bnxt_hwrm_func_qcfg(bp); 10475 netdev_update_features(bp->dev); 10476 } 10477 10478 return 0; 10479 10480 err_out: 10481 bnxt_hwrm_resource_free(bp, 0, true); 10482 10483 return rc; 10484 } 10485 10486 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 10487 { 10488 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 10489 return 0; 10490 } 10491 10492 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 10493 { 10494 bnxt_init_cp_rings(bp); 10495 bnxt_init_rx_rings(bp); 10496 bnxt_init_tx_rings(bp); 10497 bnxt_init_ring_grps(bp, irq_re_init); 10498 bnxt_init_vnics(bp); 10499 10500 return bnxt_init_chip(bp, irq_re_init); 10501 } 10502 10503 static int bnxt_set_real_num_queues(struct bnxt *bp) 10504 { 10505 int rc; 10506 struct net_device *dev = bp->dev; 10507 10508 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 10509 bp->tx_nr_rings_xdp); 10510 if (rc) 10511 return rc; 10512 10513 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 10514 if (rc) 10515 return rc; 10516 10517 #ifdef CONFIG_RFS_ACCEL 10518 if (bp->flags & BNXT_FLAG_RFS) 10519 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 10520 #endif 10521 10522 return rc; 10523 } 10524 10525 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10526 bool shared) 10527 { 10528 int _rx = *rx, _tx = *tx; 10529 10530 if (shared) { 10531 *rx = min_t(int, _rx, max); 10532 *tx = min_t(int, _tx, max); 10533 } else { 10534 if (max < 2) 10535 return -ENOMEM; 10536 10537 while (_rx + _tx > max) { 10538 if (_rx > _tx && _rx > 1) 10539 _rx--; 10540 else if (_tx > 1) 10541 _tx--; 10542 } 10543 *rx = _rx; 10544 *tx = _tx; 10545 } 10546 return 0; 10547 } 10548 10549 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 10550 { 10551 return (tx - tx_xdp) / tx_sets + tx_xdp; 10552 } 10553 10554 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 10555 { 10556 int tcs = bp->num_tc; 10557 10558 if (!tcs) 10559 tcs = 1; 10560 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 10561 } 10562 10563 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 10564 { 10565 int tcs = bp->num_tc; 10566 10567 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 10568 bp->tx_nr_rings_xdp; 10569 } 10570 10571 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10572 bool sh) 10573 { 10574 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 10575 10576 if (tx_cp != *tx) { 10577 int tx_saved = tx_cp, rc; 10578 10579 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 10580 if (rc) 10581 return rc; 10582 if (tx_cp != tx_saved) 10583 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 10584 return 0; 10585 } 10586 return __bnxt_trim_rings(bp, rx, tx, max, sh); 10587 } 10588 10589 static void bnxt_setup_msix(struct bnxt *bp) 10590 { 10591 const int len = sizeof(bp->irq_tbl[0].name); 10592 struct net_device *dev = bp->dev; 10593 int tcs, i; 10594 10595 tcs = bp->num_tc; 10596 if (tcs) { 10597 int i, off, count; 10598 10599 for (i = 0; i < tcs; i++) { 10600 count = bp->tx_nr_rings_per_tc; 10601 off = BNXT_TC_TO_RING_BASE(bp, i); 10602 netdev_set_tc_queue(dev, i, count, off); 10603 } 10604 } 10605 10606 for (i = 0; i < bp->cp_nr_rings; i++) { 10607 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10608 char *attr; 10609 10610 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 10611 attr = "TxRx"; 10612 else if (i < bp->rx_nr_rings) 10613 attr = "rx"; 10614 else 10615 attr = "tx"; 10616 10617 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 10618 attr, i); 10619 bp->irq_tbl[map_idx].handler = bnxt_msix; 10620 } 10621 } 10622 10623 static int bnxt_init_int_mode(struct bnxt *bp); 10624 10625 static int bnxt_change_msix(struct bnxt *bp, int total) 10626 { 10627 struct msi_map map; 10628 int i; 10629 10630 /* add MSIX to the end if needed */ 10631 for (i = bp->total_irqs; i < total; i++) { 10632 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 10633 if (map.index < 0) 10634 return bp->total_irqs; 10635 bp->irq_tbl[i].vector = map.virq; 10636 bp->total_irqs++; 10637 } 10638 10639 /* trim MSIX from the end if needed */ 10640 for (i = bp->total_irqs; i > total; i--) { 10641 map.index = i - 1; 10642 map.virq = bp->irq_tbl[i - 1].vector; 10643 pci_msix_free_irq(bp->pdev, map); 10644 bp->total_irqs--; 10645 } 10646 return bp->total_irqs; 10647 } 10648 10649 static int bnxt_setup_int_mode(struct bnxt *bp) 10650 { 10651 int rc; 10652 10653 if (!bp->irq_tbl) { 10654 rc = bnxt_init_int_mode(bp); 10655 if (rc || !bp->irq_tbl) 10656 return rc ?: -ENODEV; 10657 } 10658 10659 bnxt_setup_msix(bp); 10660 10661 rc = bnxt_set_real_num_queues(bp); 10662 return rc; 10663 } 10664 10665 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 10666 { 10667 return bp->hw_resc.max_rsscos_ctxs; 10668 } 10669 10670 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 10671 { 10672 return bp->hw_resc.max_vnics; 10673 } 10674 10675 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 10676 { 10677 return bp->hw_resc.max_stat_ctxs; 10678 } 10679 10680 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 10681 { 10682 return bp->hw_resc.max_cp_rings; 10683 } 10684 10685 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 10686 { 10687 unsigned int cp = bp->hw_resc.max_cp_rings; 10688 10689 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 10690 cp -= bnxt_get_ulp_msix_num(bp); 10691 10692 return cp; 10693 } 10694 10695 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 10696 { 10697 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 10698 10699 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10700 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 10701 10702 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 10703 } 10704 10705 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 10706 { 10707 bp->hw_resc.max_irqs = max_irqs; 10708 } 10709 10710 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 10711 { 10712 unsigned int cp; 10713 10714 cp = bnxt_get_max_func_cp_rings_for_en(bp); 10715 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10716 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 10717 else 10718 return cp - bp->cp_nr_rings; 10719 } 10720 10721 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 10722 { 10723 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 10724 } 10725 10726 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 10727 { 10728 int max_irq = bnxt_get_max_func_irqs(bp); 10729 int total_req = bp->cp_nr_rings + num; 10730 10731 if (max_irq < total_req) { 10732 num = max_irq - bp->cp_nr_rings; 10733 if (num <= 0) 10734 return 0; 10735 } 10736 return num; 10737 } 10738 10739 static int bnxt_get_num_msix(struct bnxt *bp) 10740 { 10741 if (!BNXT_NEW_RM(bp)) 10742 return bnxt_get_max_func_irqs(bp); 10743 10744 return bnxt_nq_rings_in_use(bp); 10745 } 10746 10747 static int bnxt_init_int_mode(struct bnxt *bp) 10748 { 10749 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 10750 10751 total_vecs = bnxt_get_num_msix(bp); 10752 max = bnxt_get_max_func_irqs(bp); 10753 if (total_vecs > max) 10754 total_vecs = max; 10755 10756 if (!total_vecs) 10757 return 0; 10758 10759 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 10760 min = 2; 10761 10762 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 10763 PCI_IRQ_MSIX); 10764 ulp_msix = bnxt_get_ulp_msix_num(bp); 10765 if (total_vecs < 0 || total_vecs < ulp_msix) { 10766 rc = -ENODEV; 10767 goto msix_setup_exit; 10768 } 10769 10770 tbl_size = total_vecs; 10771 if (pci_msix_can_alloc_dyn(bp->pdev)) 10772 tbl_size = max; 10773 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 10774 if (bp->irq_tbl) { 10775 for (i = 0; i < total_vecs; i++) 10776 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 10777 10778 bp->total_irqs = total_vecs; 10779 /* Trim rings based upon num of vectors allocated */ 10780 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 10781 total_vecs - ulp_msix, min == 1); 10782 if (rc) 10783 goto msix_setup_exit; 10784 10785 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 10786 bp->cp_nr_rings = (min == 1) ? 10787 max_t(int, tx_cp, bp->rx_nr_rings) : 10788 tx_cp + bp->rx_nr_rings; 10789 10790 } else { 10791 rc = -ENOMEM; 10792 goto msix_setup_exit; 10793 } 10794 return 0; 10795 10796 msix_setup_exit: 10797 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 10798 kfree(bp->irq_tbl); 10799 bp->irq_tbl = NULL; 10800 pci_free_irq_vectors(bp->pdev); 10801 return rc; 10802 } 10803 10804 static void bnxt_clear_int_mode(struct bnxt *bp) 10805 { 10806 pci_free_irq_vectors(bp->pdev); 10807 10808 kfree(bp->irq_tbl); 10809 bp->irq_tbl = NULL; 10810 } 10811 10812 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 10813 { 10814 bool irq_cleared = false; 10815 bool irq_change = false; 10816 int tcs = bp->num_tc; 10817 int irqs_required; 10818 int rc; 10819 10820 if (!bnxt_need_reserve_rings(bp)) 10821 return 0; 10822 10823 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 10824 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 10825 10826 if (ulp_msix > bp->ulp_num_msix_want) 10827 ulp_msix = bp->ulp_num_msix_want; 10828 irqs_required = ulp_msix + bp->cp_nr_rings; 10829 } else { 10830 irqs_required = bnxt_get_num_msix(bp); 10831 } 10832 10833 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 10834 irq_change = true; 10835 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 10836 bnxt_ulp_irq_stop(bp); 10837 bnxt_clear_int_mode(bp); 10838 irq_cleared = true; 10839 } 10840 } 10841 rc = __bnxt_reserve_rings(bp); 10842 if (irq_cleared) { 10843 if (!rc) 10844 rc = bnxt_init_int_mode(bp); 10845 bnxt_ulp_irq_restart(bp, rc); 10846 } else if (irq_change && !rc) { 10847 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 10848 rc = -ENOSPC; 10849 } 10850 if (rc) { 10851 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 10852 return rc; 10853 } 10854 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 10855 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 10856 netdev_err(bp->dev, "tx ring reservation failure\n"); 10857 netdev_reset_tc(bp->dev); 10858 bp->num_tc = 0; 10859 if (bp->tx_nr_rings_xdp) 10860 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 10861 else 10862 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 10863 return -ENOMEM; 10864 } 10865 return 0; 10866 } 10867 10868 static void bnxt_free_irq(struct bnxt *bp) 10869 { 10870 struct bnxt_irq *irq; 10871 int i; 10872 10873 #ifdef CONFIG_RFS_ACCEL 10874 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 10875 bp->dev->rx_cpu_rmap = NULL; 10876 #endif 10877 if (!bp->irq_tbl || !bp->bnapi) 10878 return; 10879 10880 for (i = 0; i < bp->cp_nr_rings; i++) { 10881 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10882 10883 irq = &bp->irq_tbl[map_idx]; 10884 if (irq->requested) { 10885 if (irq->have_cpumask) { 10886 irq_set_affinity_hint(irq->vector, NULL); 10887 free_cpumask_var(irq->cpu_mask); 10888 irq->have_cpumask = 0; 10889 } 10890 free_irq(irq->vector, bp->bnapi[i]); 10891 } 10892 10893 irq->requested = 0; 10894 } 10895 } 10896 10897 static int bnxt_request_irq(struct bnxt *bp) 10898 { 10899 int i, j, rc = 0; 10900 unsigned long flags = 0; 10901 #ifdef CONFIG_RFS_ACCEL 10902 struct cpu_rmap *rmap; 10903 #endif 10904 10905 rc = bnxt_setup_int_mode(bp); 10906 if (rc) { 10907 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 10908 rc); 10909 return rc; 10910 } 10911 #ifdef CONFIG_RFS_ACCEL 10912 rmap = bp->dev->rx_cpu_rmap; 10913 #endif 10914 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 10915 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10916 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 10917 10918 #ifdef CONFIG_RFS_ACCEL 10919 if (rmap && bp->bnapi[i]->rx_ring) { 10920 rc = irq_cpu_rmap_add(rmap, irq->vector); 10921 if (rc) 10922 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 10923 j); 10924 j++; 10925 } 10926 #endif 10927 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 10928 bp->bnapi[i]); 10929 if (rc) 10930 break; 10931 10932 netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector); 10933 irq->requested = 1; 10934 10935 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 10936 int numa_node = dev_to_node(&bp->pdev->dev); 10937 10938 irq->have_cpumask = 1; 10939 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 10940 irq->cpu_mask); 10941 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask); 10942 if (rc) { 10943 netdev_warn(bp->dev, 10944 "Set affinity failed, IRQ = %d\n", 10945 irq->vector); 10946 break; 10947 } 10948 } 10949 } 10950 return rc; 10951 } 10952 10953 static void bnxt_del_napi(struct bnxt *bp) 10954 { 10955 int i; 10956 10957 if (!bp->bnapi) 10958 return; 10959 10960 for (i = 0; i < bp->rx_nr_rings; i++) 10961 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 10962 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 10963 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 10964 10965 for (i = 0; i < bp->cp_nr_rings; i++) { 10966 struct bnxt_napi *bnapi = bp->bnapi[i]; 10967 10968 __netif_napi_del(&bnapi->napi); 10969 } 10970 /* We called __netif_napi_del(), we need 10971 * to respect an RCU grace period before freeing napi structures. 10972 */ 10973 synchronize_net(); 10974 } 10975 10976 static void bnxt_init_napi(struct bnxt *bp) 10977 { 10978 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 10979 unsigned int cp_nr_rings = bp->cp_nr_rings; 10980 struct bnxt_napi *bnapi; 10981 int i; 10982 10983 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10984 poll_fn = bnxt_poll_p5; 10985 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10986 cp_nr_rings--; 10987 for (i = 0; i < cp_nr_rings; i++) { 10988 bnapi = bp->bnapi[i]; 10989 netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn, 10990 bnapi->index); 10991 } 10992 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10993 bnapi = bp->bnapi[cp_nr_rings]; 10994 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 10995 } 10996 } 10997 10998 static void bnxt_disable_napi(struct bnxt *bp) 10999 { 11000 int i; 11001 11002 if (!bp->bnapi || 11003 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11004 return; 11005 11006 for (i = 0; i < bp->cp_nr_rings; i++) { 11007 struct bnxt_napi *bnapi = bp->bnapi[i]; 11008 struct bnxt_cp_ring_info *cpr; 11009 11010 cpr = &bnapi->cp_ring; 11011 if (bnapi->tx_fault) 11012 cpr->sw_stats->tx.tx_resets++; 11013 if (bnapi->in_reset) 11014 cpr->sw_stats->rx.rx_resets++; 11015 napi_disable(&bnapi->napi); 11016 if (bnapi->rx_ring) 11017 cancel_work_sync(&cpr->dim.work); 11018 } 11019 } 11020 11021 static void bnxt_enable_napi(struct bnxt *bp) 11022 { 11023 int i; 11024 11025 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11026 for (i = 0; i < bp->cp_nr_rings; i++) { 11027 struct bnxt_napi *bnapi = bp->bnapi[i]; 11028 struct bnxt_cp_ring_info *cpr; 11029 11030 bnapi->tx_fault = 0; 11031 11032 cpr = &bnapi->cp_ring; 11033 bnapi->in_reset = false; 11034 11035 if (bnapi->rx_ring) { 11036 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11037 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11038 } 11039 napi_enable(&bnapi->napi); 11040 } 11041 } 11042 11043 void bnxt_tx_disable(struct bnxt *bp) 11044 { 11045 int i; 11046 struct bnxt_tx_ring_info *txr; 11047 11048 if (bp->tx_ring) { 11049 for (i = 0; i < bp->tx_nr_rings; i++) { 11050 txr = &bp->tx_ring[i]; 11051 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11052 } 11053 } 11054 /* Make sure napi polls see @dev_state change */ 11055 synchronize_net(); 11056 /* Drop carrier first to prevent TX timeout */ 11057 netif_carrier_off(bp->dev); 11058 /* Stop all TX queues */ 11059 netif_tx_disable(bp->dev); 11060 } 11061 11062 void bnxt_tx_enable(struct bnxt *bp) 11063 { 11064 int i; 11065 struct bnxt_tx_ring_info *txr; 11066 11067 for (i = 0; i < bp->tx_nr_rings; i++) { 11068 txr = &bp->tx_ring[i]; 11069 WRITE_ONCE(txr->dev_state, 0); 11070 } 11071 /* Make sure napi polls see @dev_state change */ 11072 synchronize_net(); 11073 netif_tx_wake_all_queues(bp->dev); 11074 if (BNXT_LINK_IS_UP(bp)) 11075 netif_carrier_on(bp->dev); 11076 } 11077 11078 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11079 { 11080 u8 active_fec = link_info->active_fec_sig_mode & 11081 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11082 11083 switch (active_fec) { 11084 default: 11085 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11086 return "None"; 11087 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11088 return "Clause 74 BaseR"; 11089 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11090 return "Clause 91 RS(528,514)"; 11091 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11092 return "Clause 91 RS544_1XN"; 11093 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11094 return "Clause 91 RS(544,514)"; 11095 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11096 return "Clause 91 RS272_1XN"; 11097 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11098 return "Clause 91 RS(272,257)"; 11099 } 11100 } 11101 11102 void bnxt_report_link(struct bnxt *bp) 11103 { 11104 if (BNXT_LINK_IS_UP(bp)) { 11105 const char *signal = ""; 11106 const char *flow_ctrl; 11107 const char *duplex; 11108 u32 speed; 11109 u16 fec; 11110 11111 netif_carrier_on(bp->dev); 11112 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11113 if (speed == SPEED_UNKNOWN) { 11114 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11115 return; 11116 } 11117 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11118 duplex = "full"; 11119 else 11120 duplex = "half"; 11121 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11122 flow_ctrl = "ON - receive & transmit"; 11123 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11124 flow_ctrl = "ON - transmit"; 11125 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11126 flow_ctrl = "ON - receive"; 11127 else 11128 flow_ctrl = "none"; 11129 if (bp->link_info.phy_qcfg_resp.option_flags & 11130 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11131 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11132 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11133 switch (sig_mode) { 11134 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11135 signal = "(NRZ) "; 11136 break; 11137 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11138 signal = "(PAM4 56Gbps) "; 11139 break; 11140 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11141 signal = "(PAM4 112Gbps) "; 11142 break; 11143 default: 11144 break; 11145 } 11146 } 11147 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11148 speed, signal, duplex, flow_ctrl); 11149 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11150 netdev_info(bp->dev, "EEE is %s\n", 11151 bp->eee.eee_active ? "active" : 11152 "not active"); 11153 fec = bp->link_info.fec_cfg; 11154 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11155 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11156 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11157 bnxt_report_fec(&bp->link_info)); 11158 } else { 11159 netif_carrier_off(bp->dev); 11160 netdev_err(bp->dev, "NIC Link is Down\n"); 11161 } 11162 } 11163 11164 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11165 { 11166 if (!resp->supported_speeds_auto_mode && 11167 !resp->supported_speeds_force_mode && 11168 !resp->supported_pam4_speeds_auto_mode && 11169 !resp->supported_pam4_speeds_force_mode && 11170 !resp->supported_speeds2_auto_mode && 11171 !resp->supported_speeds2_force_mode) 11172 return true; 11173 return false; 11174 } 11175 11176 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11177 { 11178 struct bnxt_link_info *link_info = &bp->link_info; 11179 struct hwrm_port_phy_qcaps_output *resp; 11180 struct hwrm_port_phy_qcaps_input *req; 11181 int rc = 0; 11182 11183 if (bp->hwrm_spec_code < 0x10201) 11184 return 0; 11185 11186 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11187 if (rc) 11188 return rc; 11189 11190 resp = hwrm_req_hold(bp, req); 11191 rc = hwrm_req_send(bp, req); 11192 if (rc) 11193 goto hwrm_phy_qcaps_exit; 11194 11195 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11196 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11197 struct ethtool_keee *eee = &bp->eee; 11198 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11199 11200 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11201 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11202 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11203 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11204 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11205 } 11206 11207 if (bp->hwrm_spec_code >= 0x10a01) { 11208 if (bnxt_phy_qcaps_no_speed(resp)) { 11209 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11210 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11211 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11212 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11213 netdev_info(bp->dev, "Ethernet link enabled\n"); 11214 /* Phy re-enabled, reprobe the speeds */ 11215 link_info->support_auto_speeds = 0; 11216 link_info->support_pam4_auto_speeds = 0; 11217 link_info->support_auto_speeds2 = 0; 11218 } 11219 } 11220 if (resp->supported_speeds_auto_mode) 11221 link_info->support_auto_speeds = 11222 le16_to_cpu(resp->supported_speeds_auto_mode); 11223 if (resp->supported_pam4_speeds_auto_mode) 11224 link_info->support_pam4_auto_speeds = 11225 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11226 if (resp->supported_speeds2_auto_mode) 11227 link_info->support_auto_speeds2 = 11228 le16_to_cpu(resp->supported_speeds2_auto_mode); 11229 11230 bp->port_count = resp->port_cnt; 11231 11232 hwrm_phy_qcaps_exit: 11233 hwrm_req_drop(bp, req); 11234 return rc; 11235 } 11236 11237 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11238 { 11239 u16 diff = advertising ^ supported; 11240 11241 return ((supported | diff) != supported); 11242 } 11243 11244 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 11245 { 11246 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 11247 11248 /* Check if any advertised speeds are no longer supported. The caller 11249 * holds the link_lock mutex, so we can modify link_info settings. 11250 */ 11251 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11252 if (bnxt_support_dropped(link_info->advertising, 11253 link_info->support_auto_speeds2)) { 11254 link_info->advertising = link_info->support_auto_speeds2; 11255 return true; 11256 } 11257 return false; 11258 } 11259 if (bnxt_support_dropped(link_info->advertising, 11260 link_info->support_auto_speeds)) { 11261 link_info->advertising = link_info->support_auto_speeds; 11262 return true; 11263 } 11264 if (bnxt_support_dropped(link_info->advertising_pam4, 11265 link_info->support_pam4_auto_speeds)) { 11266 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 11267 return true; 11268 } 11269 return false; 11270 } 11271 11272 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 11273 { 11274 struct bnxt_link_info *link_info = &bp->link_info; 11275 struct hwrm_port_phy_qcfg_output *resp; 11276 struct hwrm_port_phy_qcfg_input *req; 11277 u8 link_state = link_info->link_state; 11278 bool support_changed; 11279 int rc; 11280 11281 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 11282 if (rc) 11283 return rc; 11284 11285 resp = hwrm_req_hold(bp, req); 11286 rc = hwrm_req_send(bp, req); 11287 if (rc) { 11288 hwrm_req_drop(bp, req); 11289 if (BNXT_VF(bp) && rc == -ENODEV) { 11290 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 11291 rc = 0; 11292 } 11293 return rc; 11294 } 11295 11296 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 11297 link_info->phy_link_status = resp->link; 11298 link_info->duplex = resp->duplex_cfg; 11299 if (bp->hwrm_spec_code >= 0x10800) 11300 link_info->duplex = resp->duplex_state; 11301 link_info->pause = resp->pause; 11302 link_info->auto_mode = resp->auto_mode; 11303 link_info->auto_pause_setting = resp->auto_pause; 11304 link_info->lp_pause = resp->link_partner_adv_pause; 11305 link_info->force_pause_setting = resp->force_pause; 11306 link_info->duplex_setting = resp->duplex_cfg; 11307 if (link_info->phy_link_status == BNXT_LINK_LINK) { 11308 link_info->link_speed = le16_to_cpu(resp->link_speed); 11309 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 11310 link_info->active_lanes = resp->active_lanes; 11311 } else { 11312 link_info->link_speed = 0; 11313 link_info->active_lanes = 0; 11314 } 11315 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 11316 link_info->force_pam4_link_speed = 11317 le16_to_cpu(resp->force_pam4_link_speed); 11318 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 11319 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 11320 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 11321 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 11322 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 11323 link_info->auto_pam4_link_speeds = 11324 le16_to_cpu(resp->auto_pam4_link_speed_mask); 11325 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 11326 link_info->lp_auto_link_speeds = 11327 le16_to_cpu(resp->link_partner_adv_speeds); 11328 link_info->lp_auto_pam4_link_speeds = 11329 resp->link_partner_pam4_adv_speeds; 11330 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 11331 link_info->phy_ver[0] = resp->phy_maj; 11332 link_info->phy_ver[1] = resp->phy_min; 11333 link_info->phy_ver[2] = resp->phy_bld; 11334 link_info->media_type = resp->media_type; 11335 link_info->phy_type = resp->phy_type; 11336 link_info->transceiver = resp->xcvr_pkg_type; 11337 link_info->phy_addr = resp->eee_config_phy_addr & 11338 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 11339 link_info->module_status = resp->module_status; 11340 11341 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 11342 struct ethtool_keee *eee = &bp->eee; 11343 u16 fw_speeds; 11344 11345 eee->eee_active = 0; 11346 if (resp->eee_config_phy_addr & 11347 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 11348 eee->eee_active = 1; 11349 fw_speeds = le16_to_cpu( 11350 resp->link_partner_adv_eee_link_speed_mask); 11351 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 11352 } 11353 11354 /* Pull initial EEE config */ 11355 if (!chng_link_state) { 11356 if (resp->eee_config_phy_addr & 11357 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 11358 eee->eee_enabled = 1; 11359 11360 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 11361 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 11362 11363 if (resp->eee_config_phy_addr & 11364 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 11365 __le32 tmr; 11366 11367 eee->tx_lpi_enabled = 1; 11368 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 11369 eee->tx_lpi_timer = le32_to_cpu(tmr) & 11370 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 11371 } 11372 } 11373 } 11374 11375 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 11376 if (bp->hwrm_spec_code >= 0x10504) { 11377 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 11378 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 11379 } 11380 /* TODO: need to add more logic to report VF link */ 11381 if (chng_link_state) { 11382 if (link_info->phy_link_status == BNXT_LINK_LINK) 11383 link_info->link_state = BNXT_LINK_STATE_UP; 11384 else 11385 link_info->link_state = BNXT_LINK_STATE_DOWN; 11386 if (link_state != link_info->link_state) 11387 bnxt_report_link(bp); 11388 } else { 11389 /* always link down if not require to update link state */ 11390 link_info->link_state = BNXT_LINK_STATE_DOWN; 11391 } 11392 hwrm_req_drop(bp, req); 11393 11394 if (!BNXT_PHY_CFG_ABLE(bp)) 11395 return 0; 11396 11397 support_changed = bnxt_support_speed_dropped(link_info); 11398 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 11399 bnxt_hwrm_set_link_setting(bp, true, false); 11400 return 0; 11401 } 11402 11403 static void bnxt_get_port_module_status(struct bnxt *bp) 11404 { 11405 struct bnxt_link_info *link_info = &bp->link_info; 11406 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 11407 u8 module_status; 11408 11409 if (bnxt_update_link(bp, true)) 11410 return; 11411 11412 module_status = link_info->module_status; 11413 switch (module_status) { 11414 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 11415 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 11416 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 11417 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 11418 bp->pf.port_id); 11419 if (bp->hwrm_spec_code >= 0x10201) { 11420 netdev_warn(bp->dev, "Module part number %s\n", 11421 resp->phy_vendor_partnumber); 11422 } 11423 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 11424 netdev_warn(bp->dev, "TX is disabled\n"); 11425 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 11426 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 11427 } 11428 } 11429 11430 static void 11431 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11432 { 11433 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 11434 if (bp->hwrm_spec_code >= 0x10201) 11435 req->auto_pause = 11436 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 11437 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11438 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 11439 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11440 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 11441 req->enables |= 11442 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11443 } else { 11444 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11445 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 11446 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11447 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 11448 req->enables |= 11449 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 11450 if (bp->hwrm_spec_code >= 0x10201) { 11451 req->auto_pause = req->force_pause; 11452 req->enables |= cpu_to_le32( 11453 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11454 } 11455 } 11456 } 11457 11458 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11459 { 11460 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 11461 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 11462 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11463 req->enables |= 11464 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 11465 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 11466 } else if (bp->link_info.advertising) { 11467 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 11468 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 11469 } 11470 if (bp->link_info.advertising_pam4) { 11471 req->enables |= 11472 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 11473 req->auto_link_pam4_speed_mask = 11474 cpu_to_le16(bp->link_info.advertising_pam4); 11475 } 11476 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 11477 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 11478 } else { 11479 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 11480 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11481 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 11482 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 11483 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 11484 (u32)bp->link_info.req_link_speed); 11485 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 11486 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11487 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 11488 } else { 11489 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11490 } 11491 } 11492 11493 /* tell chimp that the setting takes effect immediately */ 11494 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 11495 } 11496 11497 int bnxt_hwrm_set_pause(struct bnxt *bp) 11498 { 11499 struct hwrm_port_phy_cfg_input *req; 11500 int rc; 11501 11502 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11503 if (rc) 11504 return rc; 11505 11506 bnxt_hwrm_set_pause_common(bp, req); 11507 11508 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 11509 bp->link_info.force_link_chng) 11510 bnxt_hwrm_set_link_common(bp, req); 11511 11512 rc = hwrm_req_send(bp, req); 11513 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 11514 /* since changing of pause setting doesn't trigger any link 11515 * change event, the driver needs to update the current pause 11516 * result upon successfully return of the phy_cfg command 11517 */ 11518 bp->link_info.pause = 11519 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 11520 bp->link_info.auto_pause_setting = 0; 11521 if (!bp->link_info.force_link_chng) 11522 bnxt_report_link(bp); 11523 } 11524 bp->link_info.force_link_chng = false; 11525 return rc; 11526 } 11527 11528 static void bnxt_hwrm_set_eee(struct bnxt *bp, 11529 struct hwrm_port_phy_cfg_input *req) 11530 { 11531 struct ethtool_keee *eee = &bp->eee; 11532 11533 if (eee->eee_enabled) { 11534 u16 eee_speeds; 11535 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 11536 11537 if (eee->tx_lpi_enabled) 11538 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 11539 else 11540 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 11541 11542 req->flags |= cpu_to_le32(flags); 11543 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 11544 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 11545 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 11546 } else { 11547 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 11548 } 11549 } 11550 11551 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 11552 { 11553 struct hwrm_port_phy_cfg_input *req; 11554 int rc; 11555 11556 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11557 if (rc) 11558 return rc; 11559 11560 if (set_pause) 11561 bnxt_hwrm_set_pause_common(bp, req); 11562 11563 bnxt_hwrm_set_link_common(bp, req); 11564 11565 if (set_eee) 11566 bnxt_hwrm_set_eee(bp, req); 11567 return hwrm_req_send(bp, req); 11568 } 11569 11570 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 11571 { 11572 struct hwrm_port_phy_cfg_input *req; 11573 int rc; 11574 11575 if (!BNXT_SINGLE_PF(bp)) 11576 return 0; 11577 11578 if (pci_num_vf(bp->pdev) && 11579 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 11580 return 0; 11581 11582 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11583 if (rc) 11584 return rc; 11585 11586 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 11587 rc = hwrm_req_send(bp, req); 11588 if (!rc) { 11589 mutex_lock(&bp->link_lock); 11590 /* Device is not obliged link down in certain scenarios, even 11591 * when forced. Setting the state unknown is consistent with 11592 * driver startup and will force link state to be reported 11593 * during subsequent open based on PORT_PHY_QCFG. 11594 */ 11595 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 11596 mutex_unlock(&bp->link_lock); 11597 } 11598 return rc; 11599 } 11600 11601 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 11602 { 11603 #ifdef CONFIG_TEE_BNXT_FW 11604 int rc = tee_bnxt_fw_load(); 11605 11606 if (rc) 11607 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 11608 11609 return rc; 11610 #else 11611 netdev_err(bp->dev, "OP-TEE not supported\n"); 11612 return -ENODEV; 11613 #endif 11614 } 11615 11616 static int bnxt_try_recover_fw(struct bnxt *bp) 11617 { 11618 if (bp->fw_health && bp->fw_health->status_reliable) { 11619 int retry = 0, rc; 11620 u32 sts; 11621 11622 do { 11623 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 11624 rc = bnxt_hwrm_poll(bp); 11625 if (!BNXT_FW_IS_BOOTING(sts) && 11626 !BNXT_FW_IS_RECOVERING(sts)) 11627 break; 11628 retry++; 11629 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 11630 11631 if (!BNXT_FW_IS_HEALTHY(sts)) { 11632 netdev_err(bp->dev, 11633 "Firmware not responding, status: 0x%x\n", 11634 sts); 11635 rc = -ENODEV; 11636 } 11637 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 11638 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 11639 return bnxt_fw_reset_via_optee(bp); 11640 } 11641 return rc; 11642 } 11643 11644 return -ENODEV; 11645 } 11646 11647 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 11648 { 11649 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11650 11651 if (!BNXT_NEW_RM(bp)) 11652 return; /* no resource reservations required */ 11653 11654 hw_resc->resv_cp_rings = 0; 11655 hw_resc->resv_stat_ctxs = 0; 11656 hw_resc->resv_irqs = 0; 11657 hw_resc->resv_tx_rings = 0; 11658 hw_resc->resv_rx_rings = 0; 11659 hw_resc->resv_hw_ring_grps = 0; 11660 hw_resc->resv_vnics = 0; 11661 hw_resc->resv_rsscos_ctxs = 0; 11662 if (!fw_reset) { 11663 bp->tx_nr_rings = 0; 11664 bp->rx_nr_rings = 0; 11665 } 11666 } 11667 11668 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 11669 { 11670 int rc; 11671 11672 if (!BNXT_NEW_RM(bp)) 11673 return 0; /* no resource reservations required */ 11674 11675 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 11676 if (rc) 11677 netdev_err(bp->dev, "resc_qcaps failed\n"); 11678 11679 bnxt_clear_reservations(bp, fw_reset); 11680 11681 return rc; 11682 } 11683 11684 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 11685 { 11686 struct hwrm_func_drv_if_change_output *resp; 11687 struct hwrm_func_drv_if_change_input *req; 11688 bool fw_reset = !bp->irq_tbl; 11689 bool resc_reinit = false; 11690 int rc, retry = 0; 11691 u32 flags = 0; 11692 11693 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 11694 return 0; 11695 11696 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 11697 if (rc) 11698 return rc; 11699 11700 if (up) 11701 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 11702 resp = hwrm_req_hold(bp, req); 11703 11704 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 11705 while (retry < BNXT_FW_IF_RETRY) { 11706 rc = hwrm_req_send(bp, req); 11707 if (rc != -EAGAIN) 11708 break; 11709 11710 msleep(50); 11711 retry++; 11712 } 11713 11714 if (rc == -EAGAIN) { 11715 hwrm_req_drop(bp, req); 11716 return rc; 11717 } else if (!rc) { 11718 flags = le32_to_cpu(resp->flags); 11719 } else if (up) { 11720 rc = bnxt_try_recover_fw(bp); 11721 fw_reset = true; 11722 } 11723 hwrm_req_drop(bp, req); 11724 if (rc) 11725 return rc; 11726 11727 if (!up) { 11728 bnxt_inv_fw_health_reg(bp); 11729 return 0; 11730 } 11731 11732 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 11733 resc_reinit = true; 11734 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 11735 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 11736 fw_reset = true; 11737 else 11738 bnxt_remap_fw_health_regs(bp); 11739 11740 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 11741 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 11742 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 11743 return -ENODEV; 11744 } 11745 if (resc_reinit || fw_reset) { 11746 if (fw_reset) { 11747 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11748 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11749 bnxt_ulp_irq_stop(bp); 11750 bnxt_free_ctx_mem(bp); 11751 bnxt_dcb_free(bp); 11752 rc = bnxt_fw_init_one(bp); 11753 if (rc) { 11754 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11755 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 11756 return rc; 11757 } 11758 bnxt_clear_int_mode(bp); 11759 rc = bnxt_init_int_mode(bp); 11760 if (rc) { 11761 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11762 netdev_err(bp->dev, "init int mode failed\n"); 11763 return rc; 11764 } 11765 } 11766 rc = bnxt_cancel_reservations(bp, fw_reset); 11767 } 11768 return rc; 11769 } 11770 11771 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 11772 { 11773 struct hwrm_port_led_qcaps_output *resp; 11774 struct hwrm_port_led_qcaps_input *req; 11775 struct bnxt_pf_info *pf = &bp->pf; 11776 int rc; 11777 11778 bp->num_leds = 0; 11779 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 11780 return 0; 11781 11782 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 11783 if (rc) 11784 return rc; 11785 11786 req->port_id = cpu_to_le16(pf->port_id); 11787 resp = hwrm_req_hold(bp, req); 11788 rc = hwrm_req_send(bp, req); 11789 if (rc) { 11790 hwrm_req_drop(bp, req); 11791 return rc; 11792 } 11793 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 11794 int i; 11795 11796 bp->num_leds = resp->num_leds; 11797 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 11798 bp->num_leds); 11799 for (i = 0; i < bp->num_leds; i++) { 11800 struct bnxt_led_info *led = &bp->leds[i]; 11801 __le16 caps = led->led_state_caps; 11802 11803 if (!led->led_group_id || 11804 !BNXT_LED_ALT_BLINK_CAP(caps)) { 11805 bp->num_leds = 0; 11806 break; 11807 } 11808 } 11809 } 11810 hwrm_req_drop(bp, req); 11811 return 0; 11812 } 11813 11814 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 11815 { 11816 struct hwrm_wol_filter_alloc_output *resp; 11817 struct hwrm_wol_filter_alloc_input *req; 11818 int rc; 11819 11820 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 11821 if (rc) 11822 return rc; 11823 11824 req->port_id = cpu_to_le16(bp->pf.port_id); 11825 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 11826 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 11827 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 11828 11829 resp = hwrm_req_hold(bp, req); 11830 rc = hwrm_req_send(bp, req); 11831 if (!rc) 11832 bp->wol_filter_id = resp->wol_filter_id; 11833 hwrm_req_drop(bp, req); 11834 return rc; 11835 } 11836 11837 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 11838 { 11839 struct hwrm_wol_filter_free_input *req; 11840 int rc; 11841 11842 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 11843 if (rc) 11844 return rc; 11845 11846 req->port_id = cpu_to_le16(bp->pf.port_id); 11847 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 11848 req->wol_filter_id = bp->wol_filter_id; 11849 11850 return hwrm_req_send(bp, req); 11851 } 11852 11853 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 11854 { 11855 struct hwrm_wol_filter_qcfg_output *resp; 11856 struct hwrm_wol_filter_qcfg_input *req; 11857 u16 next_handle = 0; 11858 int rc; 11859 11860 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 11861 if (rc) 11862 return rc; 11863 11864 req->port_id = cpu_to_le16(bp->pf.port_id); 11865 req->handle = cpu_to_le16(handle); 11866 resp = hwrm_req_hold(bp, req); 11867 rc = hwrm_req_send(bp, req); 11868 if (!rc) { 11869 next_handle = le16_to_cpu(resp->next_handle); 11870 if (next_handle != 0) { 11871 if (resp->wol_type == 11872 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 11873 bp->wol = 1; 11874 bp->wol_filter_id = resp->wol_filter_id; 11875 } 11876 } 11877 } 11878 hwrm_req_drop(bp, req); 11879 return next_handle; 11880 } 11881 11882 static void bnxt_get_wol_settings(struct bnxt *bp) 11883 { 11884 u16 handle = 0; 11885 11886 bp->wol = 0; 11887 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 11888 return; 11889 11890 do { 11891 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 11892 } while (handle && handle != 0xffff); 11893 } 11894 11895 static bool bnxt_eee_config_ok(struct bnxt *bp) 11896 { 11897 struct ethtool_keee *eee = &bp->eee; 11898 struct bnxt_link_info *link_info = &bp->link_info; 11899 11900 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 11901 return true; 11902 11903 if (eee->eee_enabled) { 11904 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 11905 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 11906 11907 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 11908 11909 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 11910 eee->eee_enabled = 0; 11911 return false; 11912 } 11913 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 11914 linkmode_and(eee->advertised, advertising, 11915 eee->supported); 11916 return false; 11917 } 11918 } 11919 return true; 11920 } 11921 11922 static int bnxt_update_phy_setting(struct bnxt *bp) 11923 { 11924 int rc; 11925 bool update_link = false; 11926 bool update_pause = false; 11927 bool update_eee = false; 11928 struct bnxt_link_info *link_info = &bp->link_info; 11929 11930 rc = bnxt_update_link(bp, true); 11931 if (rc) { 11932 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 11933 rc); 11934 return rc; 11935 } 11936 if (!BNXT_SINGLE_PF(bp)) 11937 return 0; 11938 11939 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 11940 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 11941 link_info->req_flow_ctrl) 11942 update_pause = true; 11943 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 11944 link_info->force_pause_setting != link_info->req_flow_ctrl) 11945 update_pause = true; 11946 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 11947 if (BNXT_AUTO_MODE(link_info->auto_mode)) 11948 update_link = true; 11949 if (bnxt_force_speed_updated(link_info)) 11950 update_link = true; 11951 if (link_info->req_duplex != link_info->duplex_setting) 11952 update_link = true; 11953 } else { 11954 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 11955 update_link = true; 11956 if (bnxt_auto_speed_updated(link_info)) 11957 update_link = true; 11958 } 11959 11960 /* The last close may have shutdown the link, so need to call 11961 * PHY_CFG to bring it back up. 11962 */ 11963 if (!BNXT_LINK_IS_UP(bp)) 11964 update_link = true; 11965 11966 if (!bnxt_eee_config_ok(bp)) 11967 update_eee = true; 11968 11969 if (update_link) 11970 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 11971 else if (update_pause) 11972 rc = bnxt_hwrm_set_pause(bp); 11973 if (rc) { 11974 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 11975 rc); 11976 return rc; 11977 } 11978 11979 return rc; 11980 } 11981 11982 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 11983 11984 static int bnxt_reinit_after_abort(struct bnxt *bp) 11985 { 11986 int rc; 11987 11988 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11989 return -EBUSY; 11990 11991 if (bp->dev->reg_state == NETREG_UNREGISTERED) 11992 return -ENODEV; 11993 11994 rc = bnxt_fw_init_one(bp); 11995 if (!rc) { 11996 bnxt_clear_int_mode(bp); 11997 rc = bnxt_init_int_mode(bp); 11998 if (!rc) { 11999 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12000 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12001 } 12002 } 12003 return rc; 12004 } 12005 12006 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12007 { 12008 struct bnxt_ntuple_filter *ntp_fltr; 12009 struct bnxt_l2_filter *l2_fltr; 12010 12011 if (list_empty(&fltr->list)) 12012 return; 12013 12014 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12015 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12016 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12017 atomic_inc(&l2_fltr->refcnt); 12018 ntp_fltr->l2_fltr = l2_fltr; 12019 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12020 bnxt_del_ntp_filter(bp, ntp_fltr); 12021 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12022 fltr->sw_id); 12023 } 12024 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12025 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12026 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12027 bnxt_del_l2_filter(bp, l2_fltr); 12028 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12029 fltr->sw_id); 12030 } 12031 } 12032 } 12033 12034 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12035 { 12036 struct bnxt_filter_base *usr_fltr, *tmp; 12037 12038 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12039 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12040 } 12041 12042 static int bnxt_set_xps_mapping(struct bnxt *bp) 12043 { 12044 int numa_node = dev_to_node(&bp->pdev->dev); 12045 unsigned int q_idx, map_idx, cpu, i; 12046 const struct cpumask *cpu_mask_ptr; 12047 int nr_cpus = num_online_cpus(); 12048 cpumask_t *q_map; 12049 int rc = 0; 12050 12051 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12052 if (!q_map) 12053 return -ENOMEM; 12054 12055 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12056 * Each TC has the same number of TX queues. The nth TX queue for each 12057 * TC will have the same CPU mask. 12058 */ 12059 for (i = 0; i < nr_cpus; i++) { 12060 map_idx = i % bp->tx_nr_rings_per_tc; 12061 cpu = cpumask_local_spread(i, numa_node); 12062 cpu_mask_ptr = get_cpu_mask(cpu); 12063 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12064 } 12065 12066 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12067 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12068 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12069 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12070 if (rc) { 12071 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12072 q_idx); 12073 break; 12074 } 12075 } 12076 12077 kfree(q_map); 12078 12079 return rc; 12080 } 12081 12082 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12083 { 12084 int rc = 0; 12085 12086 netif_carrier_off(bp->dev); 12087 if (irq_re_init) { 12088 /* Reserve rings now if none were reserved at driver probe. */ 12089 rc = bnxt_init_dflt_ring_mode(bp); 12090 if (rc) { 12091 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12092 return rc; 12093 } 12094 } 12095 rc = bnxt_reserve_rings(bp, irq_re_init); 12096 if (rc) 12097 return rc; 12098 12099 rc = bnxt_alloc_mem(bp, irq_re_init); 12100 if (rc) { 12101 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12102 goto open_err_free_mem; 12103 } 12104 12105 if (irq_re_init) { 12106 bnxt_init_napi(bp); 12107 rc = bnxt_request_irq(bp); 12108 if (rc) { 12109 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12110 goto open_err_irq; 12111 } 12112 } 12113 12114 rc = bnxt_init_nic(bp, irq_re_init); 12115 if (rc) { 12116 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12117 goto open_err_irq; 12118 } 12119 12120 bnxt_enable_napi(bp); 12121 bnxt_debug_dev_init(bp); 12122 12123 if (link_re_init) { 12124 mutex_lock(&bp->link_lock); 12125 rc = bnxt_update_phy_setting(bp); 12126 mutex_unlock(&bp->link_lock); 12127 if (rc) { 12128 netdev_warn(bp->dev, "failed to update phy settings\n"); 12129 if (BNXT_SINGLE_PF(bp)) { 12130 bp->link_info.phy_retry = true; 12131 bp->link_info.phy_retry_expires = 12132 jiffies + 5 * HZ; 12133 } 12134 } 12135 } 12136 12137 if (irq_re_init) { 12138 udp_tunnel_nic_reset_ntf(bp->dev); 12139 rc = bnxt_set_xps_mapping(bp); 12140 if (rc) 12141 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12142 } 12143 12144 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12145 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12146 static_branch_enable(&bnxt_xdp_locking_key); 12147 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12148 static_branch_disable(&bnxt_xdp_locking_key); 12149 } 12150 set_bit(BNXT_STATE_OPEN, &bp->state); 12151 bnxt_enable_int(bp); 12152 /* Enable TX queues */ 12153 bnxt_tx_enable(bp); 12154 mod_timer(&bp->timer, jiffies + bp->current_interval); 12155 /* Poll link status and check for SFP+ module status */ 12156 mutex_lock(&bp->link_lock); 12157 bnxt_get_port_module_status(bp); 12158 mutex_unlock(&bp->link_lock); 12159 12160 /* VF-reps may need to be re-opened after the PF is re-opened */ 12161 if (BNXT_PF(bp)) 12162 bnxt_vf_reps_open(bp); 12163 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 12164 WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS); 12165 bnxt_ptp_init_rtc(bp, true); 12166 bnxt_ptp_cfg_tstamp_filters(bp); 12167 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12168 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12169 bnxt_cfg_usr_fltrs(bp); 12170 return 0; 12171 12172 open_err_irq: 12173 bnxt_del_napi(bp); 12174 12175 open_err_free_mem: 12176 bnxt_free_skbs(bp); 12177 bnxt_free_irq(bp); 12178 bnxt_free_mem(bp, true); 12179 return rc; 12180 } 12181 12182 /* rtnl_lock held */ 12183 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12184 { 12185 int rc = 0; 12186 12187 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12188 rc = -EIO; 12189 if (!rc) 12190 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12191 if (rc) { 12192 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12193 dev_close(bp->dev); 12194 } 12195 return rc; 12196 } 12197 12198 /* rtnl_lock held, open the NIC half way by allocating all resources, but 12199 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline 12200 * self tests. 12201 */ 12202 int bnxt_half_open_nic(struct bnxt *bp) 12203 { 12204 int rc = 0; 12205 12206 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12207 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12208 rc = -ENODEV; 12209 goto half_open_err; 12210 } 12211 12212 rc = bnxt_alloc_mem(bp, true); 12213 if (rc) { 12214 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12215 goto half_open_err; 12216 } 12217 bnxt_init_napi(bp); 12218 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12219 rc = bnxt_init_nic(bp, true); 12220 if (rc) { 12221 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12222 bnxt_del_napi(bp); 12223 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12224 goto half_open_err; 12225 } 12226 return 0; 12227 12228 half_open_err: 12229 bnxt_free_skbs(bp); 12230 bnxt_free_mem(bp, true); 12231 dev_close(bp->dev); 12232 return rc; 12233 } 12234 12235 /* rtnl_lock held, this call can only be made after a previous successful 12236 * call to bnxt_half_open_nic(). 12237 */ 12238 void bnxt_half_close_nic(struct bnxt *bp) 12239 { 12240 bnxt_hwrm_resource_free(bp, false, true); 12241 bnxt_del_napi(bp); 12242 bnxt_free_skbs(bp); 12243 bnxt_free_mem(bp, true); 12244 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12245 } 12246 12247 void bnxt_reenable_sriov(struct bnxt *bp) 12248 { 12249 if (BNXT_PF(bp)) { 12250 struct bnxt_pf_info *pf = &bp->pf; 12251 int n = pf->active_vfs; 12252 12253 if (n) 12254 bnxt_cfg_hw_sriov(bp, &n, true); 12255 } 12256 } 12257 12258 static int bnxt_open(struct net_device *dev) 12259 { 12260 struct bnxt *bp = netdev_priv(dev); 12261 int rc; 12262 12263 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12264 rc = bnxt_reinit_after_abort(bp); 12265 if (rc) { 12266 if (rc == -EBUSY) 12267 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 12268 else 12269 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 12270 return -ENODEV; 12271 } 12272 } 12273 12274 rc = bnxt_hwrm_if_change(bp, true); 12275 if (rc) 12276 return rc; 12277 12278 rc = __bnxt_open_nic(bp, true, true); 12279 if (rc) { 12280 bnxt_hwrm_if_change(bp, false); 12281 } else { 12282 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 12283 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12284 bnxt_queue_sp_work(bp, 12285 BNXT_RESTART_ULP_SP_EVENT); 12286 } 12287 } 12288 12289 return rc; 12290 } 12291 12292 static bool bnxt_drv_busy(struct bnxt *bp) 12293 { 12294 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 12295 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 12296 } 12297 12298 static void bnxt_get_ring_stats(struct bnxt *bp, 12299 struct rtnl_link_stats64 *stats); 12300 12301 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 12302 bool link_re_init) 12303 { 12304 /* Close the VF-reps before closing PF */ 12305 if (BNXT_PF(bp)) 12306 bnxt_vf_reps_close(bp); 12307 12308 /* Change device state to avoid TX queue wake up's */ 12309 bnxt_tx_disable(bp); 12310 12311 clear_bit(BNXT_STATE_OPEN, &bp->state); 12312 smp_mb__after_atomic(); 12313 while (bnxt_drv_busy(bp)) 12314 msleep(20); 12315 12316 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12317 bnxt_clear_rss_ctxs(bp); 12318 /* Flush rings and disable interrupts */ 12319 bnxt_shutdown_nic(bp, irq_re_init); 12320 12321 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 12322 12323 bnxt_debug_dev_exit(bp); 12324 bnxt_disable_napi(bp); 12325 del_timer_sync(&bp->timer); 12326 bnxt_free_skbs(bp); 12327 12328 /* Save ring stats before shutdown */ 12329 if (bp->bnapi && irq_re_init) { 12330 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 12331 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 12332 } 12333 if (irq_re_init) { 12334 bnxt_free_irq(bp); 12335 bnxt_del_napi(bp); 12336 } 12337 bnxt_free_mem(bp, irq_re_init); 12338 } 12339 12340 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12341 { 12342 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 12343 /* If we get here, it means firmware reset is in progress 12344 * while we are trying to close. We can safely proceed with 12345 * the close because we are holding rtnl_lock(). Some firmware 12346 * messages may fail as we proceed to close. We set the 12347 * ABORT_ERR flag here so that the FW reset thread will later 12348 * abort when it gets the rtnl_lock() and sees the flag. 12349 */ 12350 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 12351 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12352 } 12353 12354 #ifdef CONFIG_BNXT_SRIOV 12355 if (bp->sriov_cfg) { 12356 int rc; 12357 12358 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 12359 !bp->sriov_cfg, 12360 BNXT_SRIOV_CFG_WAIT_TMO); 12361 if (!rc) 12362 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 12363 else if (rc < 0) 12364 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 12365 } 12366 #endif 12367 __bnxt_close_nic(bp, irq_re_init, link_re_init); 12368 } 12369 12370 static int bnxt_close(struct net_device *dev) 12371 { 12372 struct bnxt *bp = netdev_priv(dev); 12373 12374 bnxt_close_nic(bp, true, true); 12375 bnxt_hwrm_shutdown_link(bp); 12376 bnxt_hwrm_if_change(bp, false); 12377 return 0; 12378 } 12379 12380 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 12381 u16 *val) 12382 { 12383 struct hwrm_port_phy_mdio_read_output *resp; 12384 struct hwrm_port_phy_mdio_read_input *req; 12385 int rc; 12386 12387 if (bp->hwrm_spec_code < 0x10a00) 12388 return -EOPNOTSUPP; 12389 12390 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 12391 if (rc) 12392 return rc; 12393 12394 req->port_id = cpu_to_le16(bp->pf.port_id); 12395 req->phy_addr = phy_addr; 12396 req->reg_addr = cpu_to_le16(reg & 0x1f); 12397 if (mdio_phy_id_is_c45(phy_addr)) { 12398 req->cl45_mdio = 1; 12399 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12400 req->dev_addr = mdio_phy_id_devad(phy_addr); 12401 req->reg_addr = cpu_to_le16(reg); 12402 } 12403 12404 resp = hwrm_req_hold(bp, req); 12405 rc = hwrm_req_send(bp, req); 12406 if (!rc) 12407 *val = le16_to_cpu(resp->reg_data); 12408 hwrm_req_drop(bp, req); 12409 return rc; 12410 } 12411 12412 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 12413 u16 val) 12414 { 12415 struct hwrm_port_phy_mdio_write_input *req; 12416 int rc; 12417 12418 if (bp->hwrm_spec_code < 0x10a00) 12419 return -EOPNOTSUPP; 12420 12421 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 12422 if (rc) 12423 return rc; 12424 12425 req->port_id = cpu_to_le16(bp->pf.port_id); 12426 req->phy_addr = phy_addr; 12427 req->reg_addr = cpu_to_le16(reg & 0x1f); 12428 if (mdio_phy_id_is_c45(phy_addr)) { 12429 req->cl45_mdio = 1; 12430 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12431 req->dev_addr = mdio_phy_id_devad(phy_addr); 12432 req->reg_addr = cpu_to_le16(reg); 12433 } 12434 req->reg_data = cpu_to_le16(val); 12435 12436 return hwrm_req_send(bp, req); 12437 } 12438 12439 /* rtnl_lock held */ 12440 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 12441 { 12442 struct mii_ioctl_data *mdio = if_mii(ifr); 12443 struct bnxt *bp = netdev_priv(dev); 12444 int rc; 12445 12446 switch (cmd) { 12447 case SIOCGMIIPHY: 12448 mdio->phy_id = bp->link_info.phy_addr; 12449 12450 fallthrough; 12451 case SIOCGMIIREG: { 12452 u16 mii_regval = 0; 12453 12454 if (!netif_running(dev)) 12455 return -EAGAIN; 12456 12457 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 12458 &mii_regval); 12459 mdio->val_out = mii_regval; 12460 return rc; 12461 } 12462 12463 case SIOCSMIIREG: 12464 if (!netif_running(dev)) 12465 return -EAGAIN; 12466 12467 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 12468 mdio->val_in); 12469 12470 case SIOCSHWTSTAMP: 12471 return bnxt_hwtstamp_set(dev, ifr); 12472 12473 case SIOCGHWTSTAMP: 12474 return bnxt_hwtstamp_get(dev, ifr); 12475 12476 default: 12477 /* do nothing */ 12478 break; 12479 } 12480 return -EOPNOTSUPP; 12481 } 12482 12483 static void bnxt_get_ring_stats(struct bnxt *bp, 12484 struct rtnl_link_stats64 *stats) 12485 { 12486 int i; 12487 12488 for (i = 0; i < bp->cp_nr_rings; i++) { 12489 struct bnxt_napi *bnapi = bp->bnapi[i]; 12490 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 12491 u64 *sw = cpr->stats.sw_stats; 12492 12493 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 12494 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12495 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 12496 12497 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 12498 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 12499 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 12500 12501 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 12502 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 12503 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 12504 12505 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 12506 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 12507 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 12508 12509 stats->rx_missed_errors += 12510 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 12511 12512 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12513 12514 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 12515 12516 stats->rx_dropped += 12517 cpr->sw_stats->rx.rx_netpoll_discards + 12518 cpr->sw_stats->rx.rx_oom_discards; 12519 } 12520 } 12521 12522 static void bnxt_add_prev_stats(struct bnxt *bp, 12523 struct rtnl_link_stats64 *stats) 12524 { 12525 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 12526 12527 stats->rx_packets += prev_stats->rx_packets; 12528 stats->tx_packets += prev_stats->tx_packets; 12529 stats->rx_bytes += prev_stats->rx_bytes; 12530 stats->tx_bytes += prev_stats->tx_bytes; 12531 stats->rx_missed_errors += prev_stats->rx_missed_errors; 12532 stats->multicast += prev_stats->multicast; 12533 stats->rx_dropped += prev_stats->rx_dropped; 12534 stats->tx_dropped += prev_stats->tx_dropped; 12535 } 12536 12537 static void 12538 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 12539 { 12540 struct bnxt *bp = netdev_priv(dev); 12541 12542 set_bit(BNXT_STATE_READ_STATS, &bp->state); 12543 /* Make sure bnxt_close_nic() sees that we are reading stats before 12544 * we check the BNXT_STATE_OPEN flag. 12545 */ 12546 smp_mb__after_atomic(); 12547 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 12548 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12549 *stats = bp->net_stats_prev; 12550 return; 12551 } 12552 12553 bnxt_get_ring_stats(bp, stats); 12554 bnxt_add_prev_stats(bp, stats); 12555 12556 if (bp->flags & BNXT_FLAG_PORT_STATS) { 12557 u64 *rx = bp->port_stats.sw_stats; 12558 u64 *tx = bp->port_stats.sw_stats + 12559 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 12560 12561 stats->rx_crc_errors = 12562 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 12563 stats->rx_frame_errors = 12564 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 12565 stats->rx_length_errors = 12566 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 12567 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 12568 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 12569 stats->rx_errors = 12570 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 12571 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 12572 stats->collisions = 12573 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 12574 stats->tx_fifo_errors = 12575 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 12576 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 12577 } 12578 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12579 } 12580 12581 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 12582 struct bnxt_total_ring_err_stats *stats, 12583 struct bnxt_cp_ring_info *cpr) 12584 { 12585 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 12586 u64 *hw_stats = cpr->stats.sw_stats; 12587 12588 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 12589 stats->rx_total_resets += sw_stats->rx.rx_resets; 12590 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 12591 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 12592 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 12593 stats->rx_total_ring_discards += 12594 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 12595 stats->tx_total_resets += sw_stats->tx.tx_resets; 12596 stats->tx_total_ring_discards += 12597 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 12598 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 12599 } 12600 12601 void bnxt_get_ring_err_stats(struct bnxt *bp, 12602 struct bnxt_total_ring_err_stats *stats) 12603 { 12604 int i; 12605 12606 for (i = 0; i < bp->cp_nr_rings; i++) 12607 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 12608 } 12609 12610 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 12611 { 12612 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12613 struct net_device *dev = bp->dev; 12614 struct netdev_hw_addr *ha; 12615 u8 *haddr; 12616 int mc_count = 0; 12617 bool update = false; 12618 int off = 0; 12619 12620 netdev_for_each_mc_addr(ha, dev) { 12621 if (mc_count >= BNXT_MAX_MC_ADDRS) { 12622 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12623 vnic->mc_list_count = 0; 12624 return false; 12625 } 12626 haddr = ha->addr; 12627 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 12628 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 12629 update = true; 12630 } 12631 off += ETH_ALEN; 12632 mc_count++; 12633 } 12634 if (mc_count) 12635 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 12636 12637 if (mc_count != vnic->mc_list_count) { 12638 vnic->mc_list_count = mc_count; 12639 update = true; 12640 } 12641 return update; 12642 } 12643 12644 static bool bnxt_uc_list_updated(struct bnxt *bp) 12645 { 12646 struct net_device *dev = bp->dev; 12647 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12648 struct netdev_hw_addr *ha; 12649 int off = 0; 12650 12651 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 12652 return true; 12653 12654 netdev_for_each_uc_addr(ha, dev) { 12655 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 12656 return true; 12657 12658 off += ETH_ALEN; 12659 } 12660 return false; 12661 } 12662 12663 static void bnxt_set_rx_mode(struct net_device *dev) 12664 { 12665 struct bnxt *bp = netdev_priv(dev); 12666 struct bnxt_vnic_info *vnic; 12667 bool mc_update = false; 12668 bool uc_update; 12669 u32 mask; 12670 12671 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 12672 return; 12673 12674 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12675 mask = vnic->rx_mask; 12676 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 12677 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 12678 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 12679 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 12680 12681 if (dev->flags & IFF_PROMISC) 12682 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12683 12684 uc_update = bnxt_uc_list_updated(bp); 12685 12686 if (dev->flags & IFF_BROADCAST) 12687 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 12688 if (dev->flags & IFF_ALLMULTI) { 12689 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12690 vnic->mc_list_count = 0; 12691 } else if (dev->flags & IFF_MULTICAST) { 12692 mc_update = bnxt_mc_list_updated(bp, &mask); 12693 } 12694 12695 if (mask != vnic->rx_mask || uc_update || mc_update) { 12696 vnic->rx_mask = mask; 12697 12698 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 12699 } 12700 } 12701 12702 static int bnxt_cfg_rx_mode(struct bnxt *bp) 12703 { 12704 struct net_device *dev = bp->dev; 12705 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12706 struct netdev_hw_addr *ha; 12707 int i, off = 0, rc; 12708 bool uc_update; 12709 12710 netif_addr_lock_bh(dev); 12711 uc_update = bnxt_uc_list_updated(bp); 12712 netif_addr_unlock_bh(dev); 12713 12714 if (!uc_update) 12715 goto skip_uc; 12716 12717 for (i = 1; i < vnic->uc_filter_count; i++) { 12718 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 12719 12720 bnxt_hwrm_l2_filter_free(bp, fltr); 12721 bnxt_del_l2_filter(bp, fltr); 12722 } 12723 12724 vnic->uc_filter_count = 1; 12725 12726 netif_addr_lock_bh(dev); 12727 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 12728 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12729 } else { 12730 netdev_for_each_uc_addr(ha, dev) { 12731 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 12732 off += ETH_ALEN; 12733 vnic->uc_filter_count++; 12734 } 12735 } 12736 netif_addr_unlock_bh(dev); 12737 12738 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 12739 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 12740 if (rc) { 12741 if (BNXT_VF(bp) && rc == -ENODEV) { 12742 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 12743 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 12744 else 12745 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 12746 rc = 0; 12747 } else { 12748 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 12749 } 12750 vnic->uc_filter_count = i; 12751 return rc; 12752 } 12753 } 12754 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 12755 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 12756 12757 skip_uc: 12758 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 12759 !bnxt_promisc_ok(bp)) 12760 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12761 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 12762 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 12763 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 12764 rc); 12765 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 12766 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12767 vnic->mc_list_count = 0; 12768 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 12769 } 12770 if (rc) 12771 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 12772 rc); 12773 12774 return rc; 12775 } 12776 12777 static bool bnxt_can_reserve_rings(struct bnxt *bp) 12778 { 12779 #ifdef CONFIG_BNXT_SRIOV 12780 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 12781 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12782 12783 /* No minimum rings were provisioned by the PF. Don't 12784 * reserve rings by default when device is down. 12785 */ 12786 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 12787 return true; 12788 12789 if (!netif_running(bp->dev)) 12790 return false; 12791 } 12792 #endif 12793 return true; 12794 } 12795 12796 /* If the chip and firmware supports RFS */ 12797 static bool bnxt_rfs_supported(struct bnxt *bp) 12798 { 12799 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 12800 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 12801 return true; 12802 return false; 12803 } 12804 /* 212 firmware is broken for aRFS */ 12805 if (BNXT_FW_MAJ(bp) == 212) 12806 return false; 12807 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 12808 return true; 12809 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 12810 return true; 12811 return false; 12812 } 12813 12814 /* If runtime conditions support RFS */ 12815 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 12816 { 12817 struct bnxt_hw_rings hwr = {0}; 12818 int max_vnics, max_rss_ctxs; 12819 12820 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 12821 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 12822 return bnxt_rfs_supported(bp); 12823 12824 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 12825 return false; 12826 12827 hwr.grp = bp->rx_nr_rings; 12828 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 12829 if (new_rss_ctx) 12830 hwr.vnic++; 12831 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 12832 max_vnics = bnxt_get_max_func_vnics(bp); 12833 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 12834 12835 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 12836 if (bp->rx_nr_rings > 1) 12837 netdev_warn(bp->dev, 12838 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 12839 min(max_rss_ctxs - 1, max_vnics - 1)); 12840 return false; 12841 } 12842 12843 if (!BNXT_NEW_RM(bp)) 12844 return true; 12845 12846 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 12847 * issue that will mess up the default VNIC if we reduce the 12848 * reservations. 12849 */ 12850 if (hwr.vnic <= bp->hw_resc.resv_vnics && 12851 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 12852 return true; 12853 12854 bnxt_hwrm_reserve_rings(bp, &hwr); 12855 if (hwr.vnic <= bp->hw_resc.resv_vnics && 12856 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 12857 return true; 12858 12859 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 12860 hwr.vnic = 1; 12861 hwr.rss_ctx = 0; 12862 bnxt_hwrm_reserve_rings(bp, &hwr); 12863 return false; 12864 } 12865 12866 static netdev_features_t bnxt_fix_features(struct net_device *dev, 12867 netdev_features_t features) 12868 { 12869 struct bnxt *bp = netdev_priv(dev); 12870 netdev_features_t vlan_features; 12871 12872 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 12873 features &= ~NETIF_F_NTUPLE; 12874 12875 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 12876 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 12877 12878 if (!(features & NETIF_F_GRO)) 12879 features &= ~NETIF_F_GRO_HW; 12880 12881 if (features & NETIF_F_GRO_HW) 12882 features &= ~NETIF_F_LRO; 12883 12884 /* Both CTAG and STAG VLAN accelaration on the RX side have to be 12885 * turned on or off together. 12886 */ 12887 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 12888 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 12889 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 12890 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 12891 else if (vlan_features) 12892 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 12893 } 12894 #ifdef CONFIG_BNXT_SRIOV 12895 if (BNXT_VF(bp) && bp->vf.vlan) 12896 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 12897 #endif 12898 return features; 12899 } 12900 12901 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 12902 bool link_re_init, u32 flags, bool update_tpa) 12903 { 12904 bnxt_close_nic(bp, irq_re_init, link_re_init); 12905 bp->flags = flags; 12906 if (update_tpa) 12907 bnxt_set_ring_params(bp); 12908 return bnxt_open_nic(bp, irq_re_init, link_re_init); 12909 } 12910 12911 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 12912 { 12913 bool update_tpa = false, update_ntuple = false; 12914 struct bnxt *bp = netdev_priv(dev); 12915 u32 flags = bp->flags; 12916 u32 changes; 12917 int rc = 0; 12918 bool re_init = false; 12919 12920 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 12921 if (features & NETIF_F_GRO_HW) 12922 flags |= BNXT_FLAG_GRO; 12923 else if (features & NETIF_F_LRO) 12924 flags |= BNXT_FLAG_LRO; 12925 12926 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 12927 flags &= ~BNXT_FLAG_TPA; 12928 12929 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 12930 flags |= BNXT_FLAG_STRIP_VLAN; 12931 12932 if (features & NETIF_F_NTUPLE) 12933 flags |= BNXT_FLAG_RFS; 12934 else 12935 bnxt_clear_usr_fltrs(bp, true); 12936 12937 changes = flags ^ bp->flags; 12938 if (changes & BNXT_FLAG_TPA) { 12939 update_tpa = true; 12940 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 12941 (flags & BNXT_FLAG_TPA) == 0 || 12942 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 12943 re_init = true; 12944 } 12945 12946 if (changes & ~BNXT_FLAG_TPA) 12947 re_init = true; 12948 12949 if (changes & BNXT_FLAG_RFS) 12950 update_ntuple = true; 12951 12952 if (flags != bp->flags) { 12953 u32 old_flags = bp->flags; 12954 12955 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 12956 bp->flags = flags; 12957 if (update_tpa) 12958 bnxt_set_ring_params(bp); 12959 return rc; 12960 } 12961 12962 if (update_ntuple) 12963 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 12964 12965 if (re_init) 12966 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 12967 12968 if (update_tpa) { 12969 bp->flags = flags; 12970 rc = bnxt_set_tpa(bp, 12971 (flags & BNXT_FLAG_TPA) ? 12972 true : false); 12973 if (rc) 12974 bp->flags = old_flags; 12975 } 12976 } 12977 return rc; 12978 } 12979 12980 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 12981 u8 **nextp) 12982 { 12983 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 12984 struct hop_jumbo_hdr *jhdr; 12985 int hdr_count = 0; 12986 u8 *nexthdr; 12987 int start; 12988 12989 /* Check that there are at most 2 IPv6 extension headers, no 12990 * fragment header, and each is <= 64 bytes. 12991 */ 12992 start = nw_off + sizeof(*ip6h); 12993 nexthdr = &ip6h->nexthdr; 12994 while (ipv6_ext_hdr(*nexthdr)) { 12995 struct ipv6_opt_hdr *hp; 12996 int hdrlen; 12997 12998 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 12999 *nexthdr == NEXTHDR_FRAGMENT) 13000 return false; 13001 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13002 skb_headlen(skb), NULL); 13003 if (!hp) 13004 return false; 13005 if (*nexthdr == NEXTHDR_AUTH) 13006 hdrlen = ipv6_authlen(hp); 13007 else 13008 hdrlen = ipv6_optlen(hp); 13009 13010 if (hdrlen > 64) 13011 return false; 13012 13013 /* The ext header may be a hop-by-hop header inserted for 13014 * big TCP purposes. This will be removed before sending 13015 * from NIC, so do not count it. 13016 */ 13017 if (*nexthdr == NEXTHDR_HOP) { 13018 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13019 goto increment_hdr; 13020 13021 jhdr = (struct hop_jumbo_hdr *)hp; 13022 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13023 jhdr->nexthdr != IPPROTO_TCP) 13024 goto increment_hdr; 13025 13026 goto next_hdr; 13027 } 13028 increment_hdr: 13029 hdr_count++; 13030 next_hdr: 13031 nexthdr = &hp->nexthdr; 13032 start += hdrlen; 13033 } 13034 if (nextp) { 13035 /* Caller will check inner protocol */ 13036 if (skb->encapsulation) { 13037 *nextp = nexthdr; 13038 return true; 13039 } 13040 *nextp = NULL; 13041 } 13042 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13043 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13044 } 13045 13046 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13047 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13048 { 13049 struct udphdr *uh = udp_hdr(skb); 13050 __be16 udp_port = uh->dest; 13051 13052 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13053 udp_port != bp->vxlan_gpe_port) 13054 return false; 13055 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13056 struct ethhdr *eh = inner_eth_hdr(skb); 13057 13058 switch (eh->h_proto) { 13059 case htons(ETH_P_IP): 13060 return true; 13061 case htons(ETH_P_IPV6): 13062 return bnxt_exthdr_check(bp, skb, 13063 skb_inner_network_offset(skb), 13064 NULL); 13065 } 13066 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13067 return true; 13068 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13069 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13070 NULL); 13071 } 13072 return false; 13073 } 13074 13075 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13076 { 13077 switch (l4_proto) { 13078 case IPPROTO_UDP: 13079 return bnxt_udp_tunl_check(bp, skb); 13080 case IPPROTO_IPIP: 13081 return true; 13082 case IPPROTO_GRE: { 13083 switch (skb->inner_protocol) { 13084 default: 13085 return false; 13086 case htons(ETH_P_IP): 13087 return true; 13088 case htons(ETH_P_IPV6): 13089 fallthrough; 13090 } 13091 } 13092 case IPPROTO_IPV6: 13093 /* Check ext headers of inner ipv6 */ 13094 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13095 NULL); 13096 } 13097 return false; 13098 } 13099 13100 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13101 struct net_device *dev, 13102 netdev_features_t features) 13103 { 13104 struct bnxt *bp = netdev_priv(dev); 13105 u8 *l4_proto; 13106 13107 features = vlan_features_check(skb, features); 13108 switch (vlan_get_protocol(skb)) { 13109 case htons(ETH_P_IP): 13110 if (!skb->encapsulation) 13111 return features; 13112 l4_proto = &ip_hdr(skb)->protocol; 13113 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13114 return features; 13115 break; 13116 case htons(ETH_P_IPV6): 13117 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13118 &l4_proto)) 13119 break; 13120 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13121 return features; 13122 break; 13123 } 13124 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13125 } 13126 13127 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13128 u32 *reg_buf) 13129 { 13130 struct hwrm_dbg_read_direct_output *resp; 13131 struct hwrm_dbg_read_direct_input *req; 13132 __le32 *dbg_reg_buf; 13133 dma_addr_t mapping; 13134 int rc, i; 13135 13136 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13137 if (rc) 13138 return rc; 13139 13140 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13141 &mapping); 13142 if (!dbg_reg_buf) { 13143 rc = -ENOMEM; 13144 goto dbg_rd_reg_exit; 13145 } 13146 13147 req->host_dest_addr = cpu_to_le64(mapping); 13148 13149 resp = hwrm_req_hold(bp, req); 13150 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13151 req->read_len32 = cpu_to_le32(num_words); 13152 13153 rc = hwrm_req_send(bp, req); 13154 if (rc || resp->error_code) { 13155 rc = -EIO; 13156 goto dbg_rd_reg_exit; 13157 } 13158 for (i = 0; i < num_words; i++) 13159 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13160 13161 dbg_rd_reg_exit: 13162 hwrm_req_drop(bp, req); 13163 return rc; 13164 } 13165 13166 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13167 u32 ring_id, u32 *prod, u32 *cons) 13168 { 13169 struct hwrm_dbg_ring_info_get_output *resp; 13170 struct hwrm_dbg_ring_info_get_input *req; 13171 int rc; 13172 13173 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13174 if (rc) 13175 return rc; 13176 13177 req->ring_type = ring_type; 13178 req->fw_ring_id = cpu_to_le32(ring_id); 13179 resp = hwrm_req_hold(bp, req); 13180 rc = hwrm_req_send(bp, req); 13181 if (!rc) { 13182 *prod = le32_to_cpu(resp->producer_index); 13183 *cons = le32_to_cpu(resp->consumer_index); 13184 } 13185 hwrm_req_drop(bp, req); 13186 return rc; 13187 } 13188 13189 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13190 { 13191 struct bnxt_tx_ring_info *txr; 13192 int i = bnapi->index, j; 13193 13194 bnxt_for_each_napi_tx(j, bnapi, txr) 13195 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13196 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13197 txr->tx_cons); 13198 } 13199 13200 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13201 { 13202 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13203 int i = bnapi->index; 13204 13205 if (!rxr) 13206 return; 13207 13208 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", 13209 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13210 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13211 rxr->rx_sw_agg_prod); 13212 } 13213 13214 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13215 { 13216 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13217 int i = bnapi->index; 13218 13219 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13220 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13221 } 13222 13223 static void bnxt_dbg_dump_states(struct bnxt *bp) 13224 { 13225 int i; 13226 struct bnxt_napi *bnapi; 13227 13228 for (i = 0; i < bp->cp_nr_rings; i++) { 13229 bnapi = bp->bnapi[i]; 13230 if (netif_msg_drv(bp)) { 13231 bnxt_dump_tx_sw_state(bnapi); 13232 bnxt_dump_rx_sw_state(bnapi); 13233 bnxt_dump_cp_sw_state(bnapi); 13234 } 13235 } 13236 } 13237 13238 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13239 { 13240 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 13241 struct hwrm_ring_reset_input *req; 13242 struct bnxt_napi *bnapi = rxr->bnapi; 13243 struct bnxt_cp_ring_info *cpr; 13244 u16 cp_ring_id; 13245 int rc; 13246 13247 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 13248 if (rc) 13249 return rc; 13250 13251 cpr = &bnapi->cp_ring; 13252 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 13253 req->cmpl_ring = cpu_to_le16(cp_ring_id); 13254 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 13255 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 13256 return hwrm_req_send_silent(bp, req); 13257 } 13258 13259 static void bnxt_reset_task(struct bnxt *bp, bool silent) 13260 { 13261 if (!silent) 13262 bnxt_dbg_dump_states(bp); 13263 if (netif_running(bp->dev)) { 13264 bnxt_close_nic(bp, !silent, false); 13265 bnxt_open_nic(bp, !silent, false); 13266 } 13267 } 13268 13269 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 13270 { 13271 struct bnxt *bp = netdev_priv(dev); 13272 13273 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 13274 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 13275 } 13276 13277 static void bnxt_fw_health_check(struct bnxt *bp) 13278 { 13279 struct bnxt_fw_health *fw_health = bp->fw_health; 13280 struct pci_dev *pdev = bp->pdev; 13281 u32 val; 13282 13283 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13284 return; 13285 13286 /* Make sure it is enabled before checking the tmr_counter. */ 13287 smp_rmb(); 13288 if (fw_health->tmr_counter) { 13289 fw_health->tmr_counter--; 13290 return; 13291 } 13292 13293 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13294 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 13295 fw_health->arrests++; 13296 goto fw_reset; 13297 } 13298 13299 fw_health->last_fw_heartbeat = val; 13300 13301 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13302 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 13303 fw_health->discoveries++; 13304 goto fw_reset; 13305 } 13306 13307 fw_health->tmr_counter = fw_health->tmr_multiplier; 13308 return; 13309 13310 fw_reset: 13311 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 13312 } 13313 13314 static void bnxt_timer(struct timer_list *t) 13315 { 13316 struct bnxt *bp = from_timer(bp, t, timer); 13317 struct net_device *dev = bp->dev; 13318 13319 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 13320 return; 13321 13322 if (atomic_read(&bp->intr_sem) != 0) 13323 goto bnxt_restart_timer; 13324 13325 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 13326 bnxt_fw_health_check(bp); 13327 13328 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 13329 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 13330 13331 if (bnxt_tc_flower_enabled(bp)) 13332 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 13333 13334 #ifdef CONFIG_RFS_ACCEL 13335 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 13336 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 13337 #endif /*CONFIG_RFS_ACCEL*/ 13338 13339 if (bp->link_info.phy_retry) { 13340 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 13341 bp->link_info.phy_retry = false; 13342 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 13343 } else { 13344 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 13345 } 13346 } 13347 13348 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13349 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13350 13351 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 13352 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 13353 13354 bnxt_restart_timer: 13355 mod_timer(&bp->timer, jiffies + bp->current_interval); 13356 } 13357 13358 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 13359 { 13360 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 13361 * set. If the device is being closed, bnxt_close() may be holding 13362 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we 13363 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl(). 13364 */ 13365 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13366 rtnl_lock(); 13367 } 13368 13369 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 13370 { 13371 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13372 rtnl_unlock(); 13373 } 13374 13375 /* Only called from bnxt_sp_task() */ 13376 static void bnxt_reset(struct bnxt *bp, bool silent) 13377 { 13378 bnxt_rtnl_lock_sp(bp); 13379 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 13380 bnxt_reset_task(bp, silent); 13381 bnxt_rtnl_unlock_sp(bp); 13382 } 13383 13384 /* Only called from bnxt_sp_task() */ 13385 static void bnxt_rx_ring_reset(struct bnxt *bp) 13386 { 13387 int i; 13388 13389 bnxt_rtnl_lock_sp(bp); 13390 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13391 bnxt_rtnl_unlock_sp(bp); 13392 return; 13393 } 13394 /* Disable and flush TPA before resetting the RX ring */ 13395 if (bp->flags & BNXT_FLAG_TPA) 13396 bnxt_set_tpa(bp, false); 13397 for (i = 0; i < bp->rx_nr_rings; i++) { 13398 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 13399 struct bnxt_cp_ring_info *cpr; 13400 int rc; 13401 13402 if (!rxr->bnapi->in_reset) 13403 continue; 13404 13405 rc = bnxt_hwrm_rx_ring_reset(bp, i); 13406 if (rc) { 13407 if (rc == -EINVAL || rc == -EOPNOTSUPP) 13408 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 13409 else 13410 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 13411 rc); 13412 bnxt_reset_task(bp, true); 13413 break; 13414 } 13415 bnxt_free_one_rx_ring_skbs(bp, i); 13416 rxr->rx_prod = 0; 13417 rxr->rx_agg_prod = 0; 13418 rxr->rx_sw_agg_prod = 0; 13419 rxr->rx_next_cons = 0; 13420 rxr->bnapi->in_reset = false; 13421 bnxt_alloc_one_rx_ring(bp, i); 13422 cpr = &rxr->bnapi->cp_ring; 13423 cpr->sw_stats->rx.rx_resets++; 13424 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13425 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 13426 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 13427 } 13428 if (bp->flags & BNXT_FLAG_TPA) 13429 bnxt_set_tpa(bp, true); 13430 bnxt_rtnl_unlock_sp(bp); 13431 } 13432 13433 static void bnxt_fw_fatal_close(struct bnxt *bp) 13434 { 13435 bnxt_tx_disable(bp); 13436 bnxt_disable_napi(bp); 13437 bnxt_disable_int_sync(bp); 13438 bnxt_free_irq(bp); 13439 bnxt_clear_int_mode(bp); 13440 pci_disable_device(bp->pdev); 13441 } 13442 13443 static void bnxt_fw_reset_close(struct bnxt *bp) 13444 { 13445 /* When firmware is in fatal state, quiesce device and disable 13446 * bus master to prevent any potential bad DMAs before freeing 13447 * kernel memory. 13448 */ 13449 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 13450 u16 val = 0; 13451 13452 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 13453 if (val == 0xffff) 13454 bp->fw_reset_min_dsecs = 0; 13455 bnxt_fw_fatal_close(bp); 13456 } 13457 __bnxt_close_nic(bp, true, false); 13458 bnxt_vf_reps_free(bp); 13459 bnxt_clear_int_mode(bp); 13460 bnxt_hwrm_func_drv_unrgtr(bp); 13461 if (pci_is_enabled(bp->pdev)) 13462 pci_disable_device(bp->pdev); 13463 bnxt_free_ctx_mem(bp); 13464 } 13465 13466 static bool is_bnxt_fw_ok(struct bnxt *bp) 13467 { 13468 struct bnxt_fw_health *fw_health = bp->fw_health; 13469 bool no_heartbeat = false, has_reset = false; 13470 u32 val; 13471 13472 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13473 if (val == fw_health->last_fw_heartbeat) 13474 no_heartbeat = true; 13475 13476 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13477 if (val != fw_health->last_fw_reset_cnt) 13478 has_reset = true; 13479 13480 if (!no_heartbeat && has_reset) 13481 return true; 13482 13483 return false; 13484 } 13485 13486 /* rtnl_lock is acquired before calling this function */ 13487 static void bnxt_force_fw_reset(struct bnxt *bp) 13488 { 13489 struct bnxt_fw_health *fw_health = bp->fw_health; 13490 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13491 u32 wait_dsecs; 13492 13493 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 13494 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13495 return; 13496 13497 if (ptp) { 13498 spin_lock_bh(&ptp->ptp_lock); 13499 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13500 spin_unlock_bh(&ptp->ptp_lock); 13501 } else { 13502 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13503 } 13504 bnxt_fw_reset_close(bp); 13505 wait_dsecs = fw_health->master_func_wait_dsecs; 13506 if (fw_health->primary) { 13507 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 13508 wait_dsecs = 0; 13509 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 13510 } else { 13511 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 13512 wait_dsecs = fw_health->normal_func_wait_dsecs; 13513 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13514 } 13515 13516 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 13517 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 13518 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 13519 } 13520 13521 void bnxt_fw_exception(struct bnxt *bp) 13522 { 13523 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 13524 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 13525 bnxt_ulp_stop(bp); 13526 bnxt_rtnl_lock_sp(bp); 13527 bnxt_force_fw_reset(bp); 13528 bnxt_rtnl_unlock_sp(bp); 13529 } 13530 13531 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 13532 * < 0 on error. 13533 */ 13534 static int bnxt_get_registered_vfs(struct bnxt *bp) 13535 { 13536 #ifdef CONFIG_BNXT_SRIOV 13537 int rc; 13538 13539 if (!BNXT_PF(bp)) 13540 return 0; 13541 13542 rc = bnxt_hwrm_func_qcfg(bp); 13543 if (rc) { 13544 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 13545 return rc; 13546 } 13547 if (bp->pf.registered_vfs) 13548 return bp->pf.registered_vfs; 13549 if (bp->sriov_cfg) 13550 return 1; 13551 #endif 13552 return 0; 13553 } 13554 13555 void bnxt_fw_reset(struct bnxt *bp) 13556 { 13557 bnxt_ulp_stop(bp); 13558 bnxt_rtnl_lock_sp(bp); 13559 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 13560 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13561 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13562 int n = 0, tmo; 13563 13564 if (ptp) { 13565 spin_lock_bh(&ptp->ptp_lock); 13566 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13567 spin_unlock_bh(&ptp->ptp_lock); 13568 } else { 13569 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13570 } 13571 if (bp->pf.active_vfs && 13572 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 13573 n = bnxt_get_registered_vfs(bp); 13574 if (n < 0) { 13575 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 13576 n); 13577 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13578 dev_close(bp->dev); 13579 goto fw_reset_exit; 13580 } else if (n > 0) { 13581 u16 vf_tmo_dsecs = n * 10; 13582 13583 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 13584 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 13585 bp->fw_reset_state = 13586 BNXT_FW_RESET_STATE_POLL_VF; 13587 bnxt_queue_fw_reset_work(bp, HZ / 10); 13588 goto fw_reset_exit; 13589 } 13590 bnxt_fw_reset_close(bp); 13591 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 13592 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 13593 tmo = HZ / 10; 13594 } else { 13595 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13596 tmo = bp->fw_reset_min_dsecs * HZ / 10; 13597 } 13598 bnxt_queue_fw_reset_work(bp, tmo); 13599 } 13600 fw_reset_exit: 13601 bnxt_rtnl_unlock_sp(bp); 13602 } 13603 13604 static void bnxt_chk_missed_irq(struct bnxt *bp) 13605 { 13606 int i; 13607 13608 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13609 return; 13610 13611 for (i = 0; i < bp->cp_nr_rings; i++) { 13612 struct bnxt_napi *bnapi = bp->bnapi[i]; 13613 struct bnxt_cp_ring_info *cpr; 13614 u32 fw_ring_id; 13615 int j; 13616 13617 if (!bnapi) 13618 continue; 13619 13620 cpr = &bnapi->cp_ring; 13621 for (j = 0; j < cpr->cp_ring_count; j++) { 13622 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 13623 u32 val[2]; 13624 13625 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 13626 continue; 13627 13628 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 13629 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 13630 continue; 13631 } 13632 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 13633 bnxt_dbg_hwrm_ring_info_get(bp, 13634 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 13635 fw_ring_id, &val[0], &val[1]); 13636 cpr->sw_stats->cmn.missed_irqs++; 13637 } 13638 } 13639 } 13640 13641 static void bnxt_cfg_ntp_filters(struct bnxt *); 13642 13643 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 13644 { 13645 struct bnxt_link_info *link_info = &bp->link_info; 13646 13647 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 13648 link_info->autoneg = BNXT_AUTONEG_SPEED; 13649 if (bp->hwrm_spec_code >= 0x10201) { 13650 if (link_info->auto_pause_setting & 13651 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 13652 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 13653 } else { 13654 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 13655 } 13656 bnxt_set_auto_speed(link_info); 13657 } else { 13658 bnxt_set_force_speed(link_info); 13659 link_info->req_duplex = link_info->duplex_setting; 13660 } 13661 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 13662 link_info->req_flow_ctrl = 13663 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 13664 else 13665 link_info->req_flow_ctrl = link_info->force_pause_setting; 13666 } 13667 13668 static void bnxt_fw_echo_reply(struct bnxt *bp) 13669 { 13670 struct bnxt_fw_health *fw_health = bp->fw_health; 13671 struct hwrm_func_echo_response_input *req; 13672 int rc; 13673 13674 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 13675 if (rc) 13676 return; 13677 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 13678 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 13679 hwrm_req_send(bp, req); 13680 } 13681 13682 static void bnxt_ulp_restart(struct bnxt *bp) 13683 { 13684 bnxt_ulp_stop(bp); 13685 bnxt_ulp_start(bp, 0); 13686 } 13687 13688 static void bnxt_sp_task(struct work_struct *work) 13689 { 13690 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 13691 13692 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13693 smp_mb__after_atomic(); 13694 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13695 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13696 return; 13697 } 13698 13699 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 13700 bnxt_ulp_restart(bp); 13701 bnxt_reenable_sriov(bp); 13702 } 13703 13704 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 13705 bnxt_cfg_rx_mode(bp); 13706 13707 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 13708 bnxt_cfg_ntp_filters(bp); 13709 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 13710 bnxt_hwrm_exec_fwd_req(bp); 13711 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 13712 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 13713 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 13714 bnxt_hwrm_port_qstats(bp, 0); 13715 bnxt_hwrm_port_qstats_ext(bp, 0); 13716 bnxt_accumulate_all_stats(bp); 13717 } 13718 13719 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 13720 int rc; 13721 13722 mutex_lock(&bp->link_lock); 13723 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 13724 &bp->sp_event)) 13725 bnxt_hwrm_phy_qcaps(bp); 13726 13727 rc = bnxt_update_link(bp, true); 13728 if (rc) 13729 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 13730 rc); 13731 13732 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 13733 &bp->sp_event)) 13734 bnxt_init_ethtool_link_settings(bp); 13735 mutex_unlock(&bp->link_lock); 13736 } 13737 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 13738 int rc; 13739 13740 mutex_lock(&bp->link_lock); 13741 rc = bnxt_update_phy_setting(bp); 13742 mutex_unlock(&bp->link_lock); 13743 if (rc) { 13744 netdev_warn(bp->dev, "update phy settings retry failed\n"); 13745 } else { 13746 bp->link_info.phy_retry = false; 13747 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 13748 } 13749 } 13750 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 13751 mutex_lock(&bp->link_lock); 13752 bnxt_get_port_module_status(bp); 13753 mutex_unlock(&bp->link_lock); 13754 } 13755 13756 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 13757 bnxt_tc_flow_stats_work(bp); 13758 13759 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 13760 bnxt_chk_missed_irq(bp); 13761 13762 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 13763 bnxt_fw_echo_reply(bp); 13764 13765 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 13766 bnxt_hwmon_notify_event(bp); 13767 13768 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 13769 * must be the last functions to be called before exiting. 13770 */ 13771 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 13772 bnxt_reset(bp, false); 13773 13774 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 13775 bnxt_reset(bp, true); 13776 13777 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 13778 bnxt_rx_ring_reset(bp); 13779 13780 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 13781 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 13782 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 13783 bnxt_devlink_health_fw_report(bp); 13784 else 13785 bnxt_fw_reset(bp); 13786 } 13787 13788 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 13789 if (!is_bnxt_fw_ok(bp)) 13790 bnxt_devlink_health_fw_report(bp); 13791 } 13792 13793 smp_mb__before_atomic(); 13794 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13795 } 13796 13797 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 13798 int *max_cp); 13799 13800 /* Under rtnl_lock */ 13801 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 13802 int tx_xdp) 13803 { 13804 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 13805 struct bnxt_hw_rings hwr = {0}; 13806 int rx_rings = rx; 13807 int rc; 13808 13809 if (tcs) 13810 tx_sets = tcs; 13811 13812 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 13813 13814 if (max_rx < rx_rings) 13815 return -ENOMEM; 13816 13817 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13818 rx_rings <<= 1; 13819 13820 hwr.rx = rx_rings; 13821 hwr.tx = tx * tx_sets + tx_xdp; 13822 if (max_tx < hwr.tx) 13823 return -ENOMEM; 13824 13825 hwr.vnic = bnxt_get_total_vnics(bp, rx); 13826 13827 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 13828 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 13829 if (max_cp < hwr.cp) 13830 return -ENOMEM; 13831 hwr.stat = hwr.cp; 13832 if (BNXT_NEW_RM(bp)) { 13833 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 13834 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 13835 hwr.grp = rx; 13836 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13837 } 13838 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 13839 hwr.cp_p5 = hwr.tx + rx; 13840 rc = bnxt_hwrm_check_rings(bp, &hwr); 13841 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 13842 if (!bnxt_ulp_registered(bp->edev)) { 13843 hwr.cp += bnxt_get_ulp_msix_num(bp); 13844 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 13845 } 13846 if (hwr.cp > bp->total_irqs) { 13847 int total_msix = bnxt_change_msix(bp, hwr.cp); 13848 13849 if (total_msix < hwr.cp) { 13850 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 13851 hwr.cp, total_msix); 13852 rc = -ENOSPC; 13853 } 13854 } 13855 } 13856 return rc; 13857 } 13858 13859 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 13860 { 13861 if (bp->bar2) { 13862 pci_iounmap(pdev, bp->bar2); 13863 bp->bar2 = NULL; 13864 } 13865 13866 if (bp->bar1) { 13867 pci_iounmap(pdev, bp->bar1); 13868 bp->bar1 = NULL; 13869 } 13870 13871 if (bp->bar0) { 13872 pci_iounmap(pdev, bp->bar0); 13873 bp->bar0 = NULL; 13874 } 13875 } 13876 13877 static void bnxt_cleanup_pci(struct bnxt *bp) 13878 { 13879 bnxt_unmap_bars(bp, bp->pdev); 13880 pci_release_regions(bp->pdev); 13881 if (pci_is_enabled(bp->pdev)) 13882 pci_disable_device(bp->pdev); 13883 } 13884 13885 static void bnxt_init_dflt_coal(struct bnxt *bp) 13886 { 13887 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 13888 struct bnxt_coal *coal; 13889 u16 flags = 0; 13890 13891 if (coal_cap->cmpl_params & 13892 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 13893 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 13894 13895 /* Tick values in micro seconds. 13896 * 1 coal_buf x bufs_per_record = 1 completion record. 13897 */ 13898 coal = &bp->rx_coal; 13899 coal->coal_ticks = 10; 13900 coal->coal_bufs = 30; 13901 coal->coal_ticks_irq = 1; 13902 coal->coal_bufs_irq = 2; 13903 coal->idle_thresh = 50; 13904 coal->bufs_per_record = 2; 13905 coal->budget = 64; /* NAPI budget */ 13906 coal->flags = flags; 13907 13908 coal = &bp->tx_coal; 13909 coal->coal_ticks = 28; 13910 coal->coal_bufs = 30; 13911 coal->coal_ticks_irq = 2; 13912 coal->coal_bufs_irq = 2; 13913 coal->bufs_per_record = 1; 13914 coal->flags = flags; 13915 13916 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 13917 } 13918 13919 /* FW that pre-reserves 1 VNIC per function */ 13920 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 13921 { 13922 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 13923 13924 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13925 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 13926 return true; 13927 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13928 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 13929 return true; 13930 return false; 13931 } 13932 13933 static int bnxt_fw_init_one_p1(struct bnxt *bp) 13934 { 13935 int rc; 13936 13937 bp->fw_cap = 0; 13938 rc = bnxt_hwrm_ver_get(bp); 13939 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 13940 * so wait before continuing with recovery. 13941 */ 13942 if (rc) 13943 msleep(100); 13944 bnxt_try_map_fw_health_reg(bp); 13945 if (rc) { 13946 rc = bnxt_try_recover_fw(bp); 13947 if (rc) 13948 return rc; 13949 rc = bnxt_hwrm_ver_get(bp); 13950 if (rc) 13951 return rc; 13952 } 13953 13954 bnxt_nvm_cfg_ver_get(bp); 13955 13956 rc = bnxt_hwrm_func_reset(bp); 13957 if (rc) 13958 return -ENODEV; 13959 13960 bnxt_hwrm_fw_set_time(bp); 13961 return 0; 13962 } 13963 13964 static int bnxt_fw_init_one_p2(struct bnxt *bp) 13965 { 13966 int rc; 13967 13968 /* Get the MAX capabilities for this function */ 13969 rc = bnxt_hwrm_func_qcaps(bp); 13970 if (rc) { 13971 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 13972 rc); 13973 return -ENODEV; 13974 } 13975 13976 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 13977 if (rc) 13978 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 13979 rc); 13980 13981 if (bnxt_alloc_fw_health(bp)) { 13982 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 13983 } else { 13984 rc = bnxt_hwrm_error_recovery_qcfg(bp); 13985 if (rc) 13986 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 13987 rc); 13988 } 13989 13990 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 13991 if (rc) 13992 return -ENODEV; 13993 13994 rc = bnxt_alloc_crash_dump_mem(bp); 13995 if (rc) 13996 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 13997 rc); 13998 if (!rc) { 13999 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14000 if (rc) { 14001 bnxt_free_crash_dump_mem(bp); 14002 netdev_warn(bp->dev, 14003 "hwrm crash dump mem failure rc: %d\n", rc); 14004 } 14005 } 14006 14007 if (bnxt_fw_pre_resv_vnics(bp)) 14008 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14009 14010 bnxt_hwrm_func_qcfg(bp); 14011 bnxt_hwrm_vnic_qcaps(bp); 14012 bnxt_hwrm_port_led_qcaps(bp); 14013 bnxt_ethtool_init(bp); 14014 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14015 __bnxt_hwrm_ptp_qcfg(bp); 14016 bnxt_dcb_init(bp); 14017 bnxt_hwmon_init(bp); 14018 return 0; 14019 } 14020 14021 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14022 { 14023 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14024 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14025 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14026 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14027 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14028 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14029 bp->rss_hash_delta = bp->rss_hash_cfg; 14030 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14031 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14032 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14033 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14034 } 14035 } 14036 14037 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14038 { 14039 struct net_device *dev = bp->dev; 14040 14041 dev->hw_features &= ~NETIF_F_NTUPLE; 14042 dev->features &= ~NETIF_F_NTUPLE; 14043 bp->flags &= ~BNXT_FLAG_RFS; 14044 if (bnxt_rfs_supported(bp)) { 14045 dev->hw_features |= NETIF_F_NTUPLE; 14046 if (bnxt_rfs_capable(bp, false)) { 14047 bp->flags |= BNXT_FLAG_RFS; 14048 dev->features |= NETIF_F_NTUPLE; 14049 } 14050 } 14051 } 14052 14053 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14054 { 14055 struct pci_dev *pdev = bp->pdev; 14056 14057 bnxt_set_dflt_rss_hash_type(bp); 14058 bnxt_set_dflt_rfs(bp); 14059 14060 bnxt_get_wol_settings(bp); 14061 if (bp->flags & BNXT_FLAG_WOL_CAP) 14062 device_set_wakeup_enable(&pdev->dev, bp->wol); 14063 else 14064 device_set_wakeup_capable(&pdev->dev, false); 14065 14066 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14067 bnxt_hwrm_coal_params_qcaps(bp); 14068 } 14069 14070 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14071 14072 int bnxt_fw_init_one(struct bnxt *bp) 14073 { 14074 int rc; 14075 14076 rc = bnxt_fw_init_one_p1(bp); 14077 if (rc) { 14078 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14079 return rc; 14080 } 14081 rc = bnxt_fw_init_one_p2(bp); 14082 if (rc) { 14083 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14084 return rc; 14085 } 14086 rc = bnxt_probe_phy(bp, false); 14087 if (rc) 14088 return rc; 14089 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14090 if (rc) 14091 return rc; 14092 14093 bnxt_fw_init_one_p3(bp); 14094 return 0; 14095 } 14096 14097 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14098 { 14099 struct bnxt_fw_health *fw_health = bp->fw_health; 14100 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14101 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14102 u32 reg_type, reg_off, delay_msecs; 14103 14104 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14105 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14106 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14107 switch (reg_type) { 14108 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14109 pci_write_config_dword(bp->pdev, reg_off, val); 14110 break; 14111 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14112 writel(reg_off & BNXT_GRC_BASE_MASK, 14113 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14114 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14115 fallthrough; 14116 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14117 writel(val, bp->bar0 + reg_off); 14118 break; 14119 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14120 writel(val, bp->bar1 + reg_off); 14121 break; 14122 } 14123 if (delay_msecs) { 14124 pci_read_config_dword(bp->pdev, 0, &val); 14125 msleep(delay_msecs); 14126 } 14127 } 14128 14129 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14130 { 14131 struct hwrm_func_qcfg_output *resp; 14132 struct hwrm_func_qcfg_input *req; 14133 bool result = true; /* firmware will enforce if unknown */ 14134 14135 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14136 return result; 14137 14138 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14139 return result; 14140 14141 req->fid = cpu_to_le16(0xffff); 14142 resp = hwrm_req_hold(bp, req); 14143 if (!hwrm_req_send(bp, req)) 14144 result = !!(le16_to_cpu(resp->flags) & 14145 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14146 hwrm_req_drop(bp, req); 14147 return result; 14148 } 14149 14150 static void bnxt_reset_all(struct bnxt *bp) 14151 { 14152 struct bnxt_fw_health *fw_health = bp->fw_health; 14153 int i, rc; 14154 14155 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14156 bnxt_fw_reset_via_optee(bp); 14157 bp->fw_reset_timestamp = jiffies; 14158 return; 14159 } 14160 14161 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14162 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14163 bnxt_fw_reset_writel(bp, i); 14164 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14165 struct hwrm_fw_reset_input *req; 14166 14167 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14168 if (!rc) { 14169 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14170 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14171 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14172 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14173 rc = hwrm_req_send(bp, req); 14174 } 14175 if (rc != -ENODEV) 14176 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14177 } 14178 bp->fw_reset_timestamp = jiffies; 14179 } 14180 14181 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14182 { 14183 return time_after(jiffies, bp->fw_reset_timestamp + 14184 (bp->fw_reset_max_dsecs * HZ / 10)); 14185 } 14186 14187 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14188 { 14189 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14190 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14191 bnxt_dl_health_fw_status_update(bp, false); 14192 bp->fw_reset_state = 0; 14193 dev_close(bp->dev); 14194 } 14195 14196 static void bnxt_fw_reset_task(struct work_struct *work) 14197 { 14198 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14199 int rc = 0; 14200 14201 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14202 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14203 return; 14204 } 14205 14206 switch (bp->fw_reset_state) { 14207 case BNXT_FW_RESET_STATE_POLL_VF: { 14208 int n = bnxt_get_registered_vfs(bp); 14209 int tmo; 14210 14211 if (n < 0) { 14212 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14213 n, jiffies_to_msecs(jiffies - 14214 bp->fw_reset_timestamp)); 14215 goto fw_reset_abort; 14216 } else if (n > 0) { 14217 if (bnxt_fw_reset_timeout(bp)) { 14218 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14219 bp->fw_reset_state = 0; 14220 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14221 n); 14222 goto ulp_start; 14223 } 14224 bnxt_queue_fw_reset_work(bp, HZ / 10); 14225 return; 14226 } 14227 bp->fw_reset_timestamp = jiffies; 14228 rtnl_lock(); 14229 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 14230 bnxt_fw_reset_abort(bp, rc); 14231 rtnl_unlock(); 14232 goto ulp_start; 14233 } 14234 bnxt_fw_reset_close(bp); 14235 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14236 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14237 tmo = HZ / 10; 14238 } else { 14239 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14240 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14241 } 14242 rtnl_unlock(); 14243 bnxt_queue_fw_reset_work(bp, tmo); 14244 return; 14245 } 14246 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 14247 u32 val; 14248 14249 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14250 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 14251 !bnxt_fw_reset_timeout(bp)) { 14252 bnxt_queue_fw_reset_work(bp, HZ / 5); 14253 return; 14254 } 14255 14256 if (!bp->fw_health->primary) { 14257 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 14258 14259 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14260 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14261 return; 14262 } 14263 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14264 } 14265 fallthrough; 14266 case BNXT_FW_RESET_STATE_RESET_FW: 14267 bnxt_reset_all(bp); 14268 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14269 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 14270 return; 14271 case BNXT_FW_RESET_STATE_ENABLE_DEV: 14272 bnxt_inv_fw_health_reg(bp); 14273 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 14274 !bp->fw_reset_min_dsecs) { 14275 u16 val; 14276 14277 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14278 if (val == 0xffff) { 14279 if (bnxt_fw_reset_timeout(bp)) { 14280 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 14281 rc = -ETIMEDOUT; 14282 goto fw_reset_abort; 14283 } 14284 bnxt_queue_fw_reset_work(bp, HZ / 1000); 14285 return; 14286 } 14287 } 14288 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14289 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 14290 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 14291 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 14292 bnxt_dl_remote_reload(bp); 14293 if (pci_enable_device(bp->pdev)) { 14294 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 14295 rc = -ENODEV; 14296 goto fw_reset_abort; 14297 } 14298 pci_set_master(bp->pdev); 14299 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 14300 fallthrough; 14301 case BNXT_FW_RESET_STATE_POLL_FW: 14302 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 14303 rc = bnxt_hwrm_poll(bp); 14304 if (rc) { 14305 if (bnxt_fw_reset_timeout(bp)) { 14306 netdev_err(bp->dev, "Firmware reset aborted\n"); 14307 goto fw_reset_abort_status; 14308 } 14309 bnxt_queue_fw_reset_work(bp, HZ / 5); 14310 return; 14311 } 14312 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 14313 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 14314 fallthrough; 14315 case BNXT_FW_RESET_STATE_OPENING: 14316 while (!rtnl_trylock()) { 14317 bnxt_queue_fw_reset_work(bp, HZ / 10); 14318 return; 14319 } 14320 rc = bnxt_open(bp->dev); 14321 if (rc) { 14322 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 14323 bnxt_fw_reset_abort(bp, rc); 14324 rtnl_unlock(); 14325 goto ulp_start; 14326 } 14327 14328 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 14329 bp->fw_health->enabled) { 14330 bp->fw_health->last_fw_reset_cnt = 14331 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14332 } 14333 bp->fw_reset_state = 0; 14334 /* Make sure fw_reset_state is 0 before clearing the flag */ 14335 smp_mb__before_atomic(); 14336 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14337 bnxt_ptp_reapply_pps(bp); 14338 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 14339 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 14340 bnxt_dl_health_fw_recovery_done(bp); 14341 bnxt_dl_health_fw_status_update(bp, true); 14342 } 14343 rtnl_unlock(); 14344 bnxt_ulp_start(bp, 0); 14345 bnxt_reenable_sriov(bp); 14346 rtnl_lock(); 14347 bnxt_vf_reps_alloc(bp); 14348 bnxt_vf_reps_open(bp); 14349 rtnl_unlock(); 14350 break; 14351 } 14352 return; 14353 14354 fw_reset_abort_status: 14355 if (bp->fw_health->status_reliable || 14356 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 14357 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14358 14359 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 14360 } 14361 fw_reset_abort: 14362 rtnl_lock(); 14363 bnxt_fw_reset_abort(bp, rc); 14364 rtnl_unlock(); 14365 ulp_start: 14366 bnxt_ulp_start(bp, rc); 14367 } 14368 14369 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 14370 { 14371 int rc; 14372 struct bnxt *bp = netdev_priv(dev); 14373 14374 SET_NETDEV_DEV(dev, &pdev->dev); 14375 14376 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 14377 rc = pci_enable_device(pdev); 14378 if (rc) { 14379 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 14380 goto init_err; 14381 } 14382 14383 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 14384 dev_err(&pdev->dev, 14385 "Cannot find PCI device base address, aborting\n"); 14386 rc = -ENODEV; 14387 goto init_err_disable; 14388 } 14389 14390 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 14391 if (rc) { 14392 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 14393 goto init_err_disable; 14394 } 14395 14396 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 14397 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 14398 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 14399 rc = -EIO; 14400 goto init_err_release; 14401 } 14402 14403 pci_set_master(pdev); 14404 14405 bp->dev = dev; 14406 bp->pdev = pdev; 14407 14408 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 14409 * determines the BAR size. 14410 */ 14411 bp->bar0 = pci_ioremap_bar(pdev, 0); 14412 if (!bp->bar0) { 14413 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 14414 rc = -ENOMEM; 14415 goto init_err_release; 14416 } 14417 14418 bp->bar2 = pci_ioremap_bar(pdev, 4); 14419 if (!bp->bar2) { 14420 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 14421 rc = -ENOMEM; 14422 goto init_err_release; 14423 } 14424 14425 INIT_WORK(&bp->sp_task, bnxt_sp_task); 14426 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 14427 14428 spin_lock_init(&bp->ntp_fltr_lock); 14429 #if BITS_PER_LONG == 32 14430 spin_lock_init(&bp->db_lock); 14431 #endif 14432 14433 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 14434 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 14435 14436 timer_setup(&bp->timer, bnxt_timer, 0); 14437 bp->current_interval = BNXT_TIMER_INTERVAL; 14438 14439 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 14440 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 14441 14442 clear_bit(BNXT_STATE_OPEN, &bp->state); 14443 return 0; 14444 14445 init_err_release: 14446 bnxt_unmap_bars(bp, pdev); 14447 pci_release_regions(pdev); 14448 14449 init_err_disable: 14450 pci_disable_device(pdev); 14451 14452 init_err: 14453 return rc; 14454 } 14455 14456 /* rtnl_lock held */ 14457 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 14458 { 14459 struct sockaddr *addr = p; 14460 struct bnxt *bp = netdev_priv(dev); 14461 int rc = 0; 14462 14463 if (!is_valid_ether_addr(addr->sa_data)) 14464 return -EADDRNOTAVAIL; 14465 14466 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 14467 return 0; 14468 14469 rc = bnxt_approve_mac(bp, addr->sa_data, true); 14470 if (rc) 14471 return rc; 14472 14473 eth_hw_addr_set(dev, addr->sa_data); 14474 bnxt_clear_usr_fltrs(bp, true); 14475 if (netif_running(dev)) { 14476 bnxt_close_nic(bp, false, false); 14477 rc = bnxt_open_nic(bp, false, false); 14478 } 14479 14480 return rc; 14481 } 14482 14483 /* rtnl_lock held */ 14484 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 14485 { 14486 struct bnxt *bp = netdev_priv(dev); 14487 14488 if (netif_running(dev)) 14489 bnxt_close_nic(bp, true, false); 14490 14491 WRITE_ONCE(dev->mtu, new_mtu); 14492 bnxt_set_ring_params(bp); 14493 14494 if (netif_running(dev)) 14495 return bnxt_open_nic(bp, true, false); 14496 14497 return 0; 14498 } 14499 14500 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 14501 { 14502 struct bnxt *bp = netdev_priv(dev); 14503 bool sh = false; 14504 int rc, tx_cp; 14505 14506 if (tc > bp->max_tc) { 14507 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 14508 tc, bp->max_tc); 14509 return -EINVAL; 14510 } 14511 14512 if (bp->num_tc == tc) 14513 return 0; 14514 14515 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 14516 sh = true; 14517 14518 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 14519 sh, tc, bp->tx_nr_rings_xdp); 14520 if (rc) 14521 return rc; 14522 14523 /* Needs to close the device and do hw resource re-allocations */ 14524 if (netif_running(bp->dev)) 14525 bnxt_close_nic(bp, true, false); 14526 14527 if (tc) { 14528 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 14529 netdev_set_num_tc(dev, tc); 14530 bp->num_tc = tc; 14531 } else { 14532 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 14533 netdev_reset_tc(dev); 14534 bp->num_tc = 0; 14535 } 14536 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 14537 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 14538 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 14539 tx_cp + bp->rx_nr_rings; 14540 14541 if (netif_running(bp->dev)) 14542 return bnxt_open_nic(bp, true, false); 14543 14544 return 0; 14545 } 14546 14547 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 14548 void *cb_priv) 14549 { 14550 struct bnxt *bp = cb_priv; 14551 14552 if (!bnxt_tc_flower_enabled(bp) || 14553 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 14554 return -EOPNOTSUPP; 14555 14556 switch (type) { 14557 case TC_SETUP_CLSFLOWER: 14558 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 14559 default: 14560 return -EOPNOTSUPP; 14561 } 14562 } 14563 14564 LIST_HEAD(bnxt_block_cb_list); 14565 14566 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 14567 void *type_data) 14568 { 14569 struct bnxt *bp = netdev_priv(dev); 14570 14571 switch (type) { 14572 case TC_SETUP_BLOCK: 14573 return flow_block_cb_setup_simple(type_data, 14574 &bnxt_block_cb_list, 14575 bnxt_setup_tc_block_cb, 14576 bp, bp, true); 14577 case TC_SETUP_QDISC_MQPRIO: { 14578 struct tc_mqprio_qopt *mqprio = type_data; 14579 14580 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 14581 14582 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 14583 } 14584 default: 14585 return -EOPNOTSUPP; 14586 } 14587 } 14588 14589 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 14590 const struct sk_buff *skb) 14591 { 14592 struct bnxt_vnic_info *vnic; 14593 14594 if (skb) 14595 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 14596 14597 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 14598 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 14599 } 14600 14601 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 14602 u32 idx) 14603 { 14604 struct hlist_head *head; 14605 int bit_id; 14606 14607 spin_lock_bh(&bp->ntp_fltr_lock); 14608 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 14609 if (bit_id < 0) { 14610 spin_unlock_bh(&bp->ntp_fltr_lock); 14611 return -ENOMEM; 14612 } 14613 14614 fltr->base.sw_id = (u16)bit_id; 14615 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 14616 fltr->base.flags |= BNXT_ACT_RING_DST; 14617 head = &bp->ntp_fltr_hash_tbl[idx]; 14618 hlist_add_head_rcu(&fltr->base.hash, head); 14619 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 14620 bnxt_insert_usr_fltr(bp, &fltr->base); 14621 bp->ntp_fltr_count++; 14622 spin_unlock_bh(&bp->ntp_fltr_lock); 14623 return 0; 14624 } 14625 14626 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 14627 struct bnxt_ntuple_filter *f2) 14628 { 14629 struct bnxt_flow_masks *masks1 = &f1->fmasks; 14630 struct bnxt_flow_masks *masks2 = &f2->fmasks; 14631 struct flow_keys *keys1 = &f1->fkeys; 14632 struct flow_keys *keys2 = &f2->fkeys; 14633 14634 if (keys1->basic.n_proto != keys2->basic.n_proto || 14635 keys1->basic.ip_proto != keys2->basic.ip_proto) 14636 return false; 14637 14638 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 14639 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 14640 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 14641 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 14642 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 14643 return false; 14644 } else { 14645 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 14646 &keys2->addrs.v6addrs.src) || 14647 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 14648 &masks2->addrs.v6addrs.src) || 14649 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 14650 &keys2->addrs.v6addrs.dst) || 14651 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 14652 &masks2->addrs.v6addrs.dst)) 14653 return false; 14654 } 14655 14656 return keys1->ports.src == keys2->ports.src && 14657 masks1->ports.src == masks2->ports.src && 14658 keys1->ports.dst == keys2->ports.dst && 14659 masks1->ports.dst == masks2->ports.dst && 14660 keys1->control.flags == keys2->control.flags && 14661 f1->l2_fltr == f2->l2_fltr; 14662 } 14663 14664 struct bnxt_ntuple_filter * 14665 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 14666 struct bnxt_ntuple_filter *fltr, u32 idx) 14667 { 14668 struct bnxt_ntuple_filter *f; 14669 struct hlist_head *head; 14670 14671 head = &bp->ntp_fltr_hash_tbl[idx]; 14672 hlist_for_each_entry_rcu(f, head, base.hash) { 14673 if (bnxt_fltr_match(f, fltr)) 14674 return f; 14675 } 14676 return NULL; 14677 } 14678 14679 #ifdef CONFIG_RFS_ACCEL 14680 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 14681 u16 rxq_index, u32 flow_id) 14682 { 14683 struct bnxt *bp = netdev_priv(dev); 14684 struct bnxt_ntuple_filter *fltr, *new_fltr; 14685 struct flow_keys *fkeys; 14686 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 14687 struct bnxt_l2_filter *l2_fltr; 14688 int rc = 0, idx; 14689 u32 flags; 14690 14691 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 14692 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 14693 atomic_inc(&l2_fltr->refcnt); 14694 } else { 14695 struct bnxt_l2_key key; 14696 14697 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 14698 key.vlan = 0; 14699 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 14700 if (!l2_fltr) 14701 return -EINVAL; 14702 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 14703 bnxt_del_l2_filter(bp, l2_fltr); 14704 return -EINVAL; 14705 } 14706 } 14707 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 14708 if (!new_fltr) { 14709 bnxt_del_l2_filter(bp, l2_fltr); 14710 return -ENOMEM; 14711 } 14712 14713 fkeys = &new_fltr->fkeys; 14714 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 14715 rc = -EPROTONOSUPPORT; 14716 goto err_free; 14717 } 14718 14719 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 14720 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 14721 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 14722 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 14723 rc = -EPROTONOSUPPORT; 14724 goto err_free; 14725 } 14726 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 14727 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 14728 if (bp->hwrm_spec_code < 0x10601) { 14729 rc = -EPROTONOSUPPORT; 14730 goto err_free; 14731 } 14732 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 14733 } 14734 flags = fkeys->control.flags; 14735 if (((flags & FLOW_DIS_ENCAPSULATION) && 14736 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 14737 rc = -EPROTONOSUPPORT; 14738 goto err_free; 14739 } 14740 new_fltr->l2_fltr = l2_fltr; 14741 14742 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 14743 rcu_read_lock(); 14744 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 14745 if (fltr) { 14746 rc = fltr->base.sw_id; 14747 rcu_read_unlock(); 14748 goto err_free; 14749 } 14750 rcu_read_unlock(); 14751 14752 new_fltr->flow_id = flow_id; 14753 new_fltr->base.rxq = rxq_index; 14754 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 14755 if (!rc) { 14756 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14757 return new_fltr->base.sw_id; 14758 } 14759 14760 err_free: 14761 bnxt_del_l2_filter(bp, l2_fltr); 14762 kfree(new_fltr); 14763 return rc; 14764 } 14765 #endif 14766 14767 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 14768 { 14769 spin_lock_bh(&bp->ntp_fltr_lock); 14770 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 14771 spin_unlock_bh(&bp->ntp_fltr_lock); 14772 return; 14773 } 14774 hlist_del_rcu(&fltr->base.hash); 14775 bnxt_del_one_usr_fltr(bp, &fltr->base); 14776 bp->ntp_fltr_count--; 14777 spin_unlock_bh(&bp->ntp_fltr_lock); 14778 bnxt_del_l2_filter(bp, fltr->l2_fltr); 14779 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 14780 kfree_rcu(fltr, base.rcu); 14781 } 14782 14783 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 14784 { 14785 #ifdef CONFIG_RFS_ACCEL 14786 int i; 14787 14788 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 14789 struct hlist_head *head; 14790 struct hlist_node *tmp; 14791 struct bnxt_ntuple_filter *fltr; 14792 int rc; 14793 14794 head = &bp->ntp_fltr_hash_tbl[i]; 14795 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 14796 bool del = false; 14797 14798 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 14799 if (fltr->base.flags & BNXT_ACT_NO_AGING) 14800 continue; 14801 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 14802 fltr->flow_id, 14803 fltr->base.sw_id)) { 14804 bnxt_hwrm_cfa_ntuple_filter_free(bp, 14805 fltr); 14806 del = true; 14807 } 14808 } else { 14809 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 14810 fltr); 14811 if (rc) 14812 del = true; 14813 else 14814 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 14815 } 14816 14817 if (del) 14818 bnxt_del_ntp_filter(bp, fltr); 14819 } 14820 } 14821 #endif 14822 } 14823 14824 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 14825 unsigned int entry, struct udp_tunnel_info *ti) 14826 { 14827 struct bnxt *bp = netdev_priv(netdev); 14828 unsigned int cmd; 14829 14830 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 14831 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 14832 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 14833 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 14834 else 14835 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 14836 14837 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 14838 } 14839 14840 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 14841 unsigned int entry, struct udp_tunnel_info *ti) 14842 { 14843 struct bnxt *bp = netdev_priv(netdev); 14844 unsigned int cmd; 14845 14846 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 14847 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 14848 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 14849 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 14850 else 14851 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 14852 14853 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 14854 } 14855 14856 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 14857 .set_port = bnxt_udp_tunnel_set_port, 14858 .unset_port = bnxt_udp_tunnel_unset_port, 14859 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 14860 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 14861 .tables = { 14862 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 14863 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 14864 }, 14865 }, bnxt_udp_tunnels_p7 = { 14866 .set_port = bnxt_udp_tunnel_set_port, 14867 .unset_port = bnxt_udp_tunnel_unset_port, 14868 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 14869 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 14870 .tables = { 14871 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 14872 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 14873 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 14874 }, 14875 }; 14876 14877 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 14878 struct net_device *dev, u32 filter_mask, 14879 int nlflags) 14880 { 14881 struct bnxt *bp = netdev_priv(dev); 14882 14883 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 14884 nlflags, filter_mask, NULL); 14885 } 14886 14887 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 14888 u16 flags, struct netlink_ext_ack *extack) 14889 { 14890 struct bnxt *bp = netdev_priv(dev); 14891 struct nlattr *attr, *br_spec; 14892 int rem, rc = 0; 14893 14894 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 14895 return -EOPNOTSUPP; 14896 14897 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 14898 if (!br_spec) 14899 return -EINVAL; 14900 14901 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 14902 u16 mode; 14903 14904 mode = nla_get_u16(attr); 14905 if (mode == bp->br_mode) 14906 break; 14907 14908 rc = bnxt_hwrm_set_br_mode(bp, mode); 14909 if (!rc) 14910 bp->br_mode = mode; 14911 break; 14912 } 14913 return rc; 14914 } 14915 14916 int bnxt_get_port_parent_id(struct net_device *dev, 14917 struct netdev_phys_item_id *ppid) 14918 { 14919 struct bnxt *bp = netdev_priv(dev); 14920 14921 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 14922 return -EOPNOTSUPP; 14923 14924 /* The PF and it's VF-reps only support the switchdev framework */ 14925 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 14926 return -EOPNOTSUPP; 14927 14928 ppid->id_len = sizeof(bp->dsn); 14929 memcpy(ppid->id, bp->dsn, ppid->id_len); 14930 14931 return 0; 14932 } 14933 14934 static const struct net_device_ops bnxt_netdev_ops = { 14935 .ndo_open = bnxt_open, 14936 .ndo_start_xmit = bnxt_start_xmit, 14937 .ndo_stop = bnxt_close, 14938 .ndo_get_stats64 = bnxt_get_stats64, 14939 .ndo_set_rx_mode = bnxt_set_rx_mode, 14940 .ndo_eth_ioctl = bnxt_ioctl, 14941 .ndo_validate_addr = eth_validate_addr, 14942 .ndo_set_mac_address = bnxt_change_mac_addr, 14943 .ndo_change_mtu = bnxt_change_mtu, 14944 .ndo_fix_features = bnxt_fix_features, 14945 .ndo_set_features = bnxt_set_features, 14946 .ndo_features_check = bnxt_features_check, 14947 .ndo_tx_timeout = bnxt_tx_timeout, 14948 #ifdef CONFIG_BNXT_SRIOV 14949 .ndo_get_vf_config = bnxt_get_vf_config, 14950 .ndo_set_vf_mac = bnxt_set_vf_mac, 14951 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 14952 .ndo_set_vf_rate = bnxt_set_vf_bw, 14953 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 14954 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 14955 .ndo_set_vf_trust = bnxt_set_vf_trust, 14956 #endif 14957 .ndo_setup_tc = bnxt_setup_tc, 14958 #ifdef CONFIG_RFS_ACCEL 14959 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 14960 #endif 14961 .ndo_bpf = bnxt_xdp, 14962 .ndo_xdp_xmit = bnxt_xdp_xmit, 14963 .ndo_bridge_getlink = bnxt_bridge_getlink, 14964 .ndo_bridge_setlink = bnxt_bridge_setlink, 14965 }; 14966 14967 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 14968 struct netdev_queue_stats_rx *stats) 14969 { 14970 struct bnxt *bp = netdev_priv(dev); 14971 struct bnxt_cp_ring_info *cpr; 14972 u64 *sw; 14973 14974 cpr = &bp->bnapi[i]->cp_ring; 14975 sw = cpr->stats.sw_stats; 14976 14977 stats->packets = 0; 14978 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 14979 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 14980 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 14981 14982 stats->bytes = 0; 14983 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 14984 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 14985 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 14986 14987 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 14988 } 14989 14990 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 14991 struct netdev_queue_stats_tx *stats) 14992 { 14993 struct bnxt *bp = netdev_priv(dev); 14994 struct bnxt_napi *bnapi; 14995 u64 *sw; 14996 14997 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 14998 sw = bnapi->cp_ring.stats.sw_stats; 14999 15000 stats->packets = 0; 15001 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15002 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15003 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15004 15005 stats->bytes = 0; 15006 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15007 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15008 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15009 } 15010 15011 static void bnxt_get_base_stats(struct net_device *dev, 15012 struct netdev_queue_stats_rx *rx, 15013 struct netdev_queue_stats_tx *tx) 15014 { 15015 struct bnxt *bp = netdev_priv(dev); 15016 15017 rx->packets = bp->net_stats_prev.rx_packets; 15018 rx->bytes = bp->net_stats_prev.rx_bytes; 15019 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15020 15021 tx->packets = bp->net_stats_prev.tx_packets; 15022 tx->bytes = bp->net_stats_prev.tx_bytes; 15023 } 15024 15025 static const struct netdev_stat_ops bnxt_stat_ops = { 15026 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15027 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15028 .get_base_stats = bnxt_get_base_stats, 15029 }; 15030 15031 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 15032 { 15033 u16 mem_size; 15034 15035 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 15036 mem_size = rxr->rx_agg_bmap_size / 8; 15037 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 15038 if (!rxr->rx_agg_bmap) 15039 return -ENOMEM; 15040 15041 return 0; 15042 } 15043 15044 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15045 { 15046 struct bnxt_rx_ring_info *rxr, *clone; 15047 struct bnxt *bp = netdev_priv(dev); 15048 struct bnxt_ring_struct *ring; 15049 int rc; 15050 15051 rxr = &bp->rx_ring[idx]; 15052 clone = qmem; 15053 memcpy(clone, rxr, sizeof(*rxr)); 15054 bnxt_init_rx_ring_struct(bp, clone); 15055 bnxt_reset_rx_ring_struct(bp, clone); 15056 15057 clone->rx_prod = 0; 15058 clone->rx_agg_prod = 0; 15059 clone->rx_sw_agg_prod = 0; 15060 clone->rx_next_cons = 0; 15061 15062 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15063 if (rc) 15064 return rc; 15065 15066 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15067 if (rc < 0) 15068 goto err_page_pool_destroy; 15069 15070 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15071 MEM_TYPE_PAGE_POOL, 15072 clone->page_pool); 15073 if (rc) 15074 goto err_rxq_info_unreg; 15075 15076 ring = &clone->rx_ring_struct; 15077 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15078 if (rc) 15079 goto err_free_rx_ring; 15080 15081 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15082 ring = &clone->rx_agg_ring_struct; 15083 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15084 if (rc) 15085 goto err_free_rx_agg_ring; 15086 15087 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15088 if (rc) 15089 goto err_free_rx_agg_ring; 15090 } 15091 15092 bnxt_init_one_rx_ring_rxbd(bp, clone); 15093 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15094 15095 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15096 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15097 bnxt_alloc_one_rx_ring_page(bp, clone, idx); 15098 15099 return 0; 15100 15101 err_free_rx_agg_ring: 15102 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15103 err_free_rx_ring: 15104 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15105 err_rxq_info_unreg: 15106 xdp_rxq_info_unreg(&clone->xdp_rxq); 15107 err_page_pool_destroy: 15108 clone->page_pool->p.napi = NULL; 15109 page_pool_destroy(clone->page_pool); 15110 clone->page_pool = NULL; 15111 return rc; 15112 } 15113 15114 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15115 { 15116 struct bnxt_rx_ring_info *rxr = qmem; 15117 struct bnxt *bp = netdev_priv(dev); 15118 struct bnxt_ring_struct *ring; 15119 15120 bnxt_free_one_rx_ring(bp, rxr); 15121 bnxt_free_one_rx_agg_ring(bp, rxr); 15122 15123 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15124 15125 page_pool_destroy(rxr->page_pool); 15126 rxr->page_pool = NULL; 15127 15128 ring = &rxr->rx_ring_struct; 15129 bnxt_free_ring(bp, &ring->ring_mem); 15130 15131 ring = &rxr->rx_agg_ring_struct; 15132 bnxt_free_ring(bp, &ring->ring_mem); 15133 15134 kfree(rxr->rx_agg_bmap); 15135 rxr->rx_agg_bmap = NULL; 15136 } 15137 15138 static void bnxt_copy_rx_ring(struct bnxt *bp, 15139 struct bnxt_rx_ring_info *dst, 15140 struct bnxt_rx_ring_info *src) 15141 { 15142 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15143 struct bnxt_ring_struct *dst_ring, *src_ring; 15144 int i; 15145 15146 dst_ring = &dst->rx_ring_struct; 15147 dst_rmem = &dst_ring->ring_mem; 15148 src_ring = &src->rx_ring_struct; 15149 src_rmem = &src_ring->ring_mem; 15150 15151 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15152 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15153 WARN_ON(dst_rmem->flags != src_rmem->flags); 15154 WARN_ON(dst_rmem->depth != src_rmem->depth); 15155 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15156 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15157 15158 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15159 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15160 *dst_rmem->vmem = *src_rmem->vmem; 15161 for (i = 0; i < dst_rmem->nr_pages; i++) { 15162 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15163 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15164 } 15165 15166 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15167 return; 15168 15169 dst_ring = &dst->rx_agg_ring_struct; 15170 dst_rmem = &dst_ring->ring_mem; 15171 src_ring = &src->rx_agg_ring_struct; 15172 src_rmem = &src_ring->ring_mem; 15173 15174 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15175 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15176 WARN_ON(dst_rmem->flags != src_rmem->flags); 15177 WARN_ON(dst_rmem->depth != src_rmem->depth); 15178 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15179 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15180 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15181 15182 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15183 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15184 *dst_rmem->vmem = *src_rmem->vmem; 15185 for (i = 0; i < dst_rmem->nr_pages; i++) { 15186 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15187 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15188 } 15189 15190 dst->rx_agg_bmap = src->rx_agg_bmap; 15191 } 15192 15193 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15194 { 15195 struct bnxt *bp = netdev_priv(dev); 15196 struct bnxt_rx_ring_info *rxr, *clone; 15197 struct bnxt_cp_ring_info *cpr; 15198 struct bnxt_vnic_info *vnic; 15199 int i, rc; 15200 15201 rxr = &bp->rx_ring[idx]; 15202 clone = qmem; 15203 15204 rxr->rx_prod = clone->rx_prod; 15205 rxr->rx_agg_prod = clone->rx_agg_prod; 15206 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 15207 rxr->rx_next_cons = clone->rx_next_cons; 15208 rxr->page_pool = clone->page_pool; 15209 rxr->xdp_rxq = clone->xdp_rxq; 15210 15211 bnxt_copy_rx_ring(bp, rxr, clone); 15212 15213 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 15214 if (rc) 15215 return rc; 15216 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 15217 if (rc) 15218 goto err_free_hwrm_rx_ring; 15219 15220 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 15221 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15222 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 15223 15224 cpr = &rxr->bnapi->cp_ring; 15225 cpr->sw_stats->rx.rx_resets++; 15226 15227 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15228 vnic = &bp->vnic_info[i]; 15229 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 15230 bnxt_hwrm_vnic_update(bp, vnic, 15231 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15232 } 15233 15234 return 0; 15235 15236 err_free_hwrm_rx_ring: 15237 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15238 return rc; 15239 } 15240 15241 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 15242 { 15243 struct bnxt *bp = netdev_priv(dev); 15244 struct bnxt_rx_ring_info *rxr; 15245 struct bnxt_vnic_info *vnic; 15246 int i; 15247 15248 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15249 vnic = &bp->vnic_info[i]; 15250 vnic->mru = 0; 15251 bnxt_hwrm_vnic_update(bp, vnic, 15252 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15253 } 15254 15255 rxr = &bp->rx_ring[idx]; 15256 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15257 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 15258 rxr->rx_next_cons = 0; 15259 page_pool_disable_direct_recycling(rxr->page_pool); 15260 15261 memcpy(qmem, rxr, sizeof(*rxr)); 15262 bnxt_init_rx_ring_struct(bp, qmem); 15263 15264 return 0; 15265 } 15266 15267 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 15268 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 15269 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 15270 .ndo_queue_mem_free = bnxt_queue_mem_free, 15271 .ndo_queue_start = bnxt_queue_start, 15272 .ndo_queue_stop = bnxt_queue_stop, 15273 }; 15274 15275 static void bnxt_remove_one(struct pci_dev *pdev) 15276 { 15277 struct net_device *dev = pci_get_drvdata(pdev); 15278 struct bnxt *bp = netdev_priv(dev); 15279 15280 if (BNXT_PF(bp)) 15281 bnxt_sriov_disable(bp); 15282 15283 bnxt_rdma_aux_device_del(bp); 15284 15285 bnxt_ptp_clear(bp); 15286 unregister_netdev(dev); 15287 15288 bnxt_rdma_aux_device_uninit(bp); 15289 15290 bnxt_free_l2_filters(bp, true); 15291 bnxt_free_ntp_fltrs(bp, true); 15292 WARN_ON(bp->num_rss_ctx); 15293 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15294 /* Flush any pending tasks */ 15295 cancel_work_sync(&bp->sp_task); 15296 cancel_delayed_work_sync(&bp->fw_reset_task); 15297 bp->sp_event = 0; 15298 15299 bnxt_dl_fw_reporters_destroy(bp); 15300 bnxt_dl_unregister(bp); 15301 bnxt_shutdown_tc(bp); 15302 15303 bnxt_clear_int_mode(bp); 15304 bnxt_hwrm_func_drv_unrgtr(bp); 15305 bnxt_free_hwrm_resources(bp); 15306 bnxt_hwmon_uninit(bp); 15307 bnxt_ethtool_free(bp); 15308 bnxt_dcb_free(bp); 15309 kfree(bp->ptp_cfg); 15310 bp->ptp_cfg = NULL; 15311 kfree(bp->fw_health); 15312 bp->fw_health = NULL; 15313 bnxt_cleanup_pci(bp); 15314 bnxt_free_ctx_mem(bp); 15315 bnxt_free_crash_dump_mem(bp); 15316 kfree(bp->rss_indir_tbl); 15317 bp->rss_indir_tbl = NULL; 15318 bnxt_free_port_stats(bp); 15319 free_netdev(dev); 15320 } 15321 15322 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 15323 { 15324 int rc = 0; 15325 struct bnxt_link_info *link_info = &bp->link_info; 15326 15327 bp->phy_flags = 0; 15328 rc = bnxt_hwrm_phy_qcaps(bp); 15329 if (rc) { 15330 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 15331 rc); 15332 return rc; 15333 } 15334 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 15335 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 15336 else 15337 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 15338 if (!fw_dflt) 15339 return 0; 15340 15341 mutex_lock(&bp->link_lock); 15342 rc = bnxt_update_link(bp, false); 15343 if (rc) { 15344 mutex_unlock(&bp->link_lock); 15345 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 15346 rc); 15347 return rc; 15348 } 15349 15350 /* Older firmware does not have supported_auto_speeds, so assume 15351 * that all supported speeds can be autonegotiated. 15352 */ 15353 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 15354 link_info->support_auto_speeds = link_info->support_speeds; 15355 15356 bnxt_init_ethtool_link_settings(bp); 15357 mutex_unlock(&bp->link_lock); 15358 return 0; 15359 } 15360 15361 static int bnxt_get_max_irq(struct pci_dev *pdev) 15362 { 15363 u16 ctrl; 15364 15365 if (!pdev->msix_cap) 15366 return 1; 15367 15368 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 15369 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 15370 } 15371 15372 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15373 int *max_cp) 15374 { 15375 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 15376 int max_ring_grps = 0, max_irq; 15377 15378 *max_tx = hw_resc->max_tx_rings; 15379 *max_rx = hw_resc->max_rx_rings; 15380 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 15381 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 15382 bnxt_get_ulp_msix_num_in_use(bp), 15383 hw_resc->max_stat_ctxs - 15384 bnxt_get_ulp_stat_ctxs_in_use(bp)); 15385 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 15386 *max_cp = min_t(int, *max_cp, max_irq); 15387 max_ring_grps = hw_resc->max_hw_ring_grps; 15388 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 15389 *max_cp -= 1; 15390 *max_rx -= 2; 15391 } 15392 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15393 *max_rx >>= 1; 15394 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 15395 int rc; 15396 15397 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 15398 if (rc) { 15399 *max_rx = 0; 15400 *max_tx = 0; 15401 } 15402 /* On P5 chips, max_cp output param should be available NQs */ 15403 *max_cp = max_irq; 15404 } 15405 *max_rx = min_t(int, *max_rx, max_ring_grps); 15406 } 15407 15408 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 15409 { 15410 int rx, tx, cp; 15411 15412 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 15413 *max_rx = rx; 15414 *max_tx = tx; 15415 if (!rx || !tx || !cp) 15416 return -ENOMEM; 15417 15418 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 15419 } 15420 15421 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15422 bool shared) 15423 { 15424 int rc; 15425 15426 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15427 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 15428 /* Not enough rings, try disabling agg rings. */ 15429 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 15430 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15431 if (rc) { 15432 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 15433 bp->flags |= BNXT_FLAG_AGG_RINGS; 15434 return rc; 15435 } 15436 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 15437 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15438 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15439 bnxt_set_ring_params(bp); 15440 } 15441 15442 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 15443 int max_cp, max_stat, max_irq; 15444 15445 /* Reserve minimum resources for RoCE */ 15446 max_cp = bnxt_get_max_func_cp_rings(bp); 15447 max_stat = bnxt_get_max_func_stat_ctxs(bp); 15448 max_irq = bnxt_get_max_func_irqs(bp); 15449 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 15450 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 15451 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 15452 return 0; 15453 15454 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 15455 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 15456 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 15457 max_cp = min_t(int, max_cp, max_irq); 15458 max_cp = min_t(int, max_cp, max_stat); 15459 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 15460 if (rc) 15461 rc = 0; 15462 } 15463 return rc; 15464 } 15465 15466 /* In initial default shared ring setting, each shared ring must have a 15467 * RX/TX ring pair. 15468 */ 15469 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 15470 { 15471 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 15472 bp->rx_nr_rings = bp->cp_nr_rings; 15473 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 15474 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15475 } 15476 15477 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 15478 { 15479 int dflt_rings, max_rx_rings, max_tx_rings, rc; 15480 int avail_msix; 15481 15482 if (!bnxt_can_reserve_rings(bp)) 15483 return 0; 15484 15485 if (sh) 15486 bp->flags |= BNXT_FLAG_SHARED_RINGS; 15487 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 15488 /* Reduce default rings on multi-port cards so that total default 15489 * rings do not exceed CPU count. 15490 */ 15491 if (bp->port_count > 1) { 15492 int max_rings = 15493 max_t(int, num_online_cpus() / bp->port_count, 1); 15494 15495 dflt_rings = min_t(int, dflt_rings, max_rings); 15496 } 15497 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 15498 if (rc) 15499 return rc; 15500 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 15501 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 15502 if (sh) 15503 bnxt_trim_dflt_sh_rings(bp); 15504 else 15505 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 15506 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15507 15508 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 15509 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 15510 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 15511 15512 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 15513 bnxt_set_dflt_ulp_stat_ctxs(bp); 15514 } 15515 15516 rc = __bnxt_reserve_rings(bp); 15517 if (rc && rc != -ENODEV) 15518 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 15519 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15520 if (sh) 15521 bnxt_trim_dflt_sh_rings(bp); 15522 15523 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 15524 if (bnxt_need_reserve_rings(bp)) { 15525 rc = __bnxt_reserve_rings(bp); 15526 if (rc && rc != -ENODEV) 15527 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 15528 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15529 } 15530 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 15531 bp->rx_nr_rings++; 15532 bp->cp_nr_rings++; 15533 } 15534 if (rc) { 15535 bp->tx_nr_rings = 0; 15536 bp->rx_nr_rings = 0; 15537 } 15538 return rc; 15539 } 15540 15541 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 15542 { 15543 int rc; 15544 15545 if (bp->tx_nr_rings) 15546 return 0; 15547 15548 bnxt_ulp_irq_stop(bp); 15549 bnxt_clear_int_mode(bp); 15550 rc = bnxt_set_dflt_rings(bp, true); 15551 if (rc) { 15552 if (BNXT_VF(bp) && rc == -ENODEV) 15553 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 15554 else 15555 netdev_err(bp->dev, "Not enough rings available.\n"); 15556 goto init_dflt_ring_err; 15557 } 15558 rc = bnxt_init_int_mode(bp); 15559 if (rc) 15560 goto init_dflt_ring_err; 15561 15562 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15563 15564 bnxt_set_dflt_rfs(bp); 15565 15566 init_dflt_ring_err: 15567 bnxt_ulp_irq_restart(bp, rc); 15568 return rc; 15569 } 15570 15571 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 15572 { 15573 int rc; 15574 15575 ASSERT_RTNL(); 15576 bnxt_hwrm_func_qcaps(bp); 15577 15578 if (netif_running(bp->dev)) 15579 __bnxt_close_nic(bp, true, false); 15580 15581 bnxt_ulp_irq_stop(bp); 15582 bnxt_clear_int_mode(bp); 15583 rc = bnxt_init_int_mode(bp); 15584 bnxt_ulp_irq_restart(bp, rc); 15585 15586 if (netif_running(bp->dev)) { 15587 if (rc) 15588 dev_close(bp->dev); 15589 else 15590 rc = bnxt_open_nic(bp, true, false); 15591 } 15592 15593 return rc; 15594 } 15595 15596 static int bnxt_init_mac_addr(struct bnxt *bp) 15597 { 15598 int rc = 0; 15599 15600 if (BNXT_PF(bp)) { 15601 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 15602 } else { 15603 #ifdef CONFIG_BNXT_SRIOV 15604 struct bnxt_vf_info *vf = &bp->vf; 15605 bool strict_approval = true; 15606 15607 if (is_valid_ether_addr(vf->mac_addr)) { 15608 /* overwrite netdev dev_addr with admin VF MAC */ 15609 eth_hw_addr_set(bp->dev, vf->mac_addr); 15610 /* Older PF driver or firmware may not approve this 15611 * correctly. 15612 */ 15613 strict_approval = false; 15614 } else { 15615 eth_hw_addr_random(bp->dev); 15616 } 15617 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 15618 #endif 15619 } 15620 return rc; 15621 } 15622 15623 static void bnxt_vpd_read_info(struct bnxt *bp) 15624 { 15625 struct pci_dev *pdev = bp->pdev; 15626 unsigned int vpd_size, kw_len; 15627 int pos, size; 15628 u8 *vpd_data; 15629 15630 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 15631 if (IS_ERR(vpd_data)) { 15632 pci_warn(pdev, "Unable to read VPD\n"); 15633 return; 15634 } 15635 15636 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 15637 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 15638 if (pos < 0) 15639 goto read_sn; 15640 15641 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 15642 memcpy(bp->board_partno, &vpd_data[pos], size); 15643 15644 read_sn: 15645 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 15646 PCI_VPD_RO_KEYWORD_SERIALNO, 15647 &kw_len); 15648 if (pos < 0) 15649 goto exit; 15650 15651 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 15652 memcpy(bp->board_serialno, &vpd_data[pos], size); 15653 exit: 15654 kfree(vpd_data); 15655 } 15656 15657 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 15658 { 15659 struct pci_dev *pdev = bp->pdev; 15660 u64 qword; 15661 15662 qword = pci_get_dsn(pdev); 15663 if (!qword) { 15664 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 15665 return -EOPNOTSUPP; 15666 } 15667 15668 put_unaligned_le64(qword, dsn); 15669 15670 bp->flags |= BNXT_FLAG_DSN_VALID; 15671 return 0; 15672 } 15673 15674 static int bnxt_map_db_bar(struct bnxt *bp) 15675 { 15676 if (!bp->db_size) 15677 return -ENODEV; 15678 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 15679 if (!bp->bar1) 15680 return -ENOMEM; 15681 return 0; 15682 } 15683 15684 void bnxt_print_device_info(struct bnxt *bp) 15685 { 15686 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 15687 board_info[bp->board_idx].name, 15688 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 15689 15690 pcie_print_link_status(bp->pdev); 15691 } 15692 15693 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 15694 { 15695 struct bnxt_hw_resc *hw_resc; 15696 struct net_device *dev; 15697 struct bnxt *bp; 15698 int rc, max_irqs; 15699 15700 if (pci_is_bridge(pdev)) 15701 return -ENODEV; 15702 15703 if (!pdev->msix_cap) { 15704 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 15705 return -ENODEV; 15706 } 15707 15708 /* Clear any pending DMA transactions from crash kernel 15709 * while loading driver in capture kernel. 15710 */ 15711 if (is_kdump_kernel()) { 15712 pci_clear_master(pdev); 15713 pcie_flr(pdev); 15714 } 15715 15716 max_irqs = bnxt_get_max_irq(pdev); 15717 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 15718 max_irqs); 15719 if (!dev) 15720 return -ENOMEM; 15721 15722 bp = netdev_priv(dev); 15723 bp->board_idx = ent->driver_data; 15724 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 15725 bnxt_set_max_func_irqs(bp, max_irqs); 15726 15727 if (bnxt_vf_pciid(bp->board_idx)) 15728 bp->flags |= BNXT_FLAG_VF; 15729 15730 /* No devlink port registration in case of a VF */ 15731 if (BNXT_PF(bp)) 15732 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 15733 15734 rc = bnxt_init_board(pdev, dev); 15735 if (rc < 0) 15736 goto init_err_free; 15737 15738 dev->netdev_ops = &bnxt_netdev_ops; 15739 dev->stat_ops = &bnxt_stat_ops; 15740 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 15741 dev->ethtool_ops = &bnxt_ethtool_ops; 15742 pci_set_drvdata(pdev, dev); 15743 15744 rc = bnxt_alloc_hwrm_resources(bp); 15745 if (rc) 15746 goto init_err_pci_clean; 15747 15748 mutex_init(&bp->hwrm_cmd_lock); 15749 mutex_init(&bp->link_lock); 15750 15751 rc = bnxt_fw_init_one_p1(bp); 15752 if (rc) 15753 goto init_err_pci_clean; 15754 15755 if (BNXT_PF(bp)) 15756 bnxt_vpd_read_info(bp); 15757 15758 if (BNXT_CHIP_P5_PLUS(bp)) { 15759 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 15760 if (BNXT_CHIP_P7(bp)) 15761 bp->flags |= BNXT_FLAG_CHIP_P7; 15762 } 15763 15764 rc = bnxt_alloc_rss_indir_tbl(bp); 15765 if (rc) 15766 goto init_err_pci_clean; 15767 15768 rc = bnxt_fw_init_one_p2(bp); 15769 if (rc) 15770 goto init_err_pci_clean; 15771 15772 rc = bnxt_map_db_bar(bp); 15773 if (rc) { 15774 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 15775 rc); 15776 goto init_err_pci_clean; 15777 } 15778 15779 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 15780 NETIF_F_TSO | NETIF_F_TSO6 | 15781 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 15782 NETIF_F_GSO_IPXIP4 | 15783 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 15784 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 15785 NETIF_F_RXCSUM | NETIF_F_GRO; 15786 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 15787 dev->hw_features |= NETIF_F_GSO_UDP_L4; 15788 15789 if (BNXT_SUPPORTS_TPA(bp)) 15790 dev->hw_features |= NETIF_F_LRO; 15791 15792 dev->hw_enc_features = 15793 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 15794 NETIF_F_TSO | NETIF_F_TSO6 | 15795 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 15796 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 15797 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 15798 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 15799 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 15800 if (bp->flags & BNXT_FLAG_CHIP_P7) 15801 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 15802 else 15803 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 15804 15805 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 15806 NETIF_F_GSO_GRE_CSUM; 15807 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 15808 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 15809 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 15810 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 15811 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 15812 if (BNXT_SUPPORTS_TPA(bp)) 15813 dev->hw_features |= NETIF_F_GRO_HW; 15814 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 15815 if (dev->features & NETIF_F_GRO_HW) 15816 dev->features &= ~NETIF_F_LRO; 15817 dev->priv_flags |= IFF_UNICAST_FLT; 15818 15819 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 15820 if (bp->tso_max_segs) 15821 netif_set_tso_max_segs(dev, bp->tso_max_segs); 15822 15823 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 15824 NETDEV_XDP_ACT_RX_SG; 15825 15826 #ifdef CONFIG_BNXT_SRIOV 15827 init_waitqueue_head(&bp->sriov_cfg_wait); 15828 #endif 15829 if (BNXT_SUPPORTS_TPA(bp)) { 15830 bp->gro_func = bnxt_gro_func_5730x; 15831 if (BNXT_CHIP_P4(bp)) 15832 bp->gro_func = bnxt_gro_func_5731x; 15833 else if (BNXT_CHIP_P5_PLUS(bp)) 15834 bp->gro_func = bnxt_gro_func_5750x; 15835 } 15836 if (!BNXT_CHIP_P4_PLUS(bp)) 15837 bp->flags |= BNXT_FLAG_DOUBLE_DB; 15838 15839 rc = bnxt_init_mac_addr(bp); 15840 if (rc) { 15841 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 15842 rc = -EADDRNOTAVAIL; 15843 goto init_err_pci_clean; 15844 } 15845 15846 if (BNXT_PF(bp)) { 15847 /* Read the adapter's DSN to use as the eswitch switch_id */ 15848 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 15849 } 15850 15851 /* MTU range: 60 - FW defined max */ 15852 dev->min_mtu = ETH_ZLEN; 15853 dev->max_mtu = bp->max_mtu; 15854 15855 rc = bnxt_probe_phy(bp, true); 15856 if (rc) 15857 goto init_err_pci_clean; 15858 15859 hw_resc = &bp->hw_resc; 15860 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 15861 BNXT_L2_FLTR_MAX_FLTR; 15862 /* Older firmware may not report these filters properly */ 15863 if (bp->max_fltr < BNXT_MAX_FLTR) 15864 bp->max_fltr = BNXT_MAX_FLTR; 15865 bnxt_init_l2_fltr_tbl(bp); 15866 bnxt_set_rx_skb_mode(bp, false); 15867 bnxt_set_tpa_flags(bp); 15868 bnxt_set_ring_params(bp); 15869 bnxt_rdma_aux_device_init(bp); 15870 rc = bnxt_set_dflt_rings(bp, true); 15871 if (rc) { 15872 if (BNXT_VF(bp) && rc == -ENODEV) { 15873 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 15874 } else { 15875 netdev_err(bp->dev, "Not enough rings available.\n"); 15876 rc = -ENOMEM; 15877 } 15878 goto init_err_pci_clean; 15879 } 15880 15881 bnxt_fw_init_one_p3(bp); 15882 15883 bnxt_init_dflt_coal(bp); 15884 15885 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 15886 bp->flags |= BNXT_FLAG_STRIP_VLAN; 15887 15888 rc = bnxt_init_int_mode(bp); 15889 if (rc) 15890 goto init_err_pci_clean; 15891 15892 /* No TC has been set yet and rings may have been trimmed due to 15893 * limited MSIX, so we re-initialize the TX rings per TC. 15894 */ 15895 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15896 15897 if (BNXT_PF(bp)) { 15898 if (!bnxt_pf_wq) { 15899 bnxt_pf_wq = 15900 create_singlethread_workqueue("bnxt_pf_wq"); 15901 if (!bnxt_pf_wq) { 15902 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 15903 rc = -ENOMEM; 15904 goto init_err_pci_clean; 15905 } 15906 } 15907 rc = bnxt_init_tc(bp); 15908 if (rc) 15909 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 15910 rc); 15911 } 15912 15913 bnxt_inv_fw_health_reg(bp); 15914 rc = bnxt_dl_register(bp); 15915 if (rc) 15916 goto init_err_dl; 15917 15918 INIT_LIST_HEAD(&bp->usr_fltr_list); 15919 15920 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 15921 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 15922 if (BNXT_SUPPORTS_QUEUE_API(bp)) 15923 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 15924 15925 rc = register_netdev(dev); 15926 if (rc) 15927 goto init_err_cleanup; 15928 15929 bnxt_dl_fw_reporters_create(bp); 15930 15931 bnxt_rdma_aux_device_add(bp); 15932 15933 bnxt_print_device_info(bp); 15934 15935 pci_save_state(pdev); 15936 15937 return 0; 15938 init_err_cleanup: 15939 bnxt_rdma_aux_device_uninit(bp); 15940 bnxt_dl_unregister(bp); 15941 init_err_dl: 15942 bnxt_shutdown_tc(bp); 15943 bnxt_clear_int_mode(bp); 15944 15945 init_err_pci_clean: 15946 bnxt_hwrm_func_drv_unrgtr(bp); 15947 bnxt_free_hwrm_resources(bp); 15948 bnxt_hwmon_uninit(bp); 15949 bnxt_ethtool_free(bp); 15950 bnxt_ptp_clear(bp); 15951 kfree(bp->ptp_cfg); 15952 bp->ptp_cfg = NULL; 15953 kfree(bp->fw_health); 15954 bp->fw_health = NULL; 15955 bnxt_cleanup_pci(bp); 15956 bnxt_free_ctx_mem(bp); 15957 bnxt_free_crash_dump_mem(bp); 15958 kfree(bp->rss_indir_tbl); 15959 bp->rss_indir_tbl = NULL; 15960 15961 init_err_free: 15962 free_netdev(dev); 15963 return rc; 15964 } 15965 15966 static void bnxt_shutdown(struct pci_dev *pdev) 15967 { 15968 struct net_device *dev = pci_get_drvdata(pdev); 15969 struct bnxt *bp; 15970 15971 if (!dev) 15972 return; 15973 15974 rtnl_lock(); 15975 bp = netdev_priv(dev); 15976 if (!bp) 15977 goto shutdown_exit; 15978 15979 if (netif_running(dev)) 15980 dev_close(dev); 15981 15982 bnxt_clear_int_mode(bp); 15983 pci_disable_device(pdev); 15984 15985 if (system_state == SYSTEM_POWER_OFF) { 15986 pci_wake_from_d3(pdev, bp->wol); 15987 pci_set_power_state(pdev, PCI_D3hot); 15988 } 15989 15990 shutdown_exit: 15991 rtnl_unlock(); 15992 } 15993 15994 #ifdef CONFIG_PM_SLEEP 15995 static int bnxt_suspend(struct device *device) 15996 { 15997 struct net_device *dev = dev_get_drvdata(device); 15998 struct bnxt *bp = netdev_priv(dev); 15999 int rc = 0; 16000 16001 bnxt_ulp_stop(bp); 16002 16003 rtnl_lock(); 16004 if (netif_running(dev)) { 16005 netif_device_detach(dev); 16006 rc = bnxt_close(dev); 16007 } 16008 bnxt_hwrm_func_drv_unrgtr(bp); 16009 pci_disable_device(bp->pdev); 16010 bnxt_free_ctx_mem(bp); 16011 rtnl_unlock(); 16012 return rc; 16013 } 16014 16015 static int bnxt_resume(struct device *device) 16016 { 16017 struct net_device *dev = dev_get_drvdata(device); 16018 struct bnxt *bp = netdev_priv(dev); 16019 int rc = 0; 16020 16021 rtnl_lock(); 16022 rc = pci_enable_device(bp->pdev); 16023 if (rc) { 16024 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16025 rc); 16026 goto resume_exit; 16027 } 16028 pci_set_master(bp->pdev); 16029 if (bnxt_hwrm_ver_get(bp)) { 16030 rc = -ENODEV; 16031 goto resume_exit; 16032 } 16033 rc = bnxt_hwrm_func_reset(bp); 16034 if (rc) { 16035 rc = -EBUSY; 16036 goto resume_exit; 16037 } 16038 16039 rc = bnxt_hwrm_func_qcaps(bp); 16040 if (rc) 16041 goto resume_exit; 16042 16043 bnxt_clear_reservations(bp, true); 16044 16045 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16046 rc = -ENODEV; 16047 goto resume_exit; 16048 } 16049 if (bp->fw_crash_mem) 16050 bnxt_hwrm_crash_dump_mem_cfg(bp); 16051 16052 bnxt_get_wol_settings(bp); 16053 if (netif_running(dev)) { 16054 rc = bnxt_open(dev); 16055 if (!rc) 16056 netif_device_attach(dev); 16057 } 16058 16059 resume_exit: 16060 rtnl_unlock(); 16061 bnxt_ulp_start(bp, rc); 16062 if (!rc) 16063 bnxt_reenable_sriov(bp); 16064 return rc; 16065 } 16066 16067 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16068 #define BNXT_PM_OPS (&bnxt_pm_ops) 16069 16070 #else 16071 16072 #define BNXT_PM_OPS NULL 16073 16074 #endif /* CONFIG_PM_SLEEP */ 16075 16076 /** 16077 * bnxt_io_error_detected - called when PCI error is detected 16078 * @pdev: Pointer to PCI device 16079 * @state: The current pci connection state 16080 * 16081 * This function is called after a PCI bus error affecting 16082 * this device has been detected. 16083 */ 16084 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16085 pci_channel_state_t state) 16086 { 16087 struct net_device *netdev = pci_get_drvdata(pdev); 16088 struct bnxt *bp = netdev_priv(netdev); 16089 bool abort = false; 16090 16091 netdev_info(netdev, "PCI I/O error detected\n"); 16092 16093 bnxt_ulp_stop(bp); 16094 16095 rtnl_lock(); 16096 netif_device_detach(netdev); 16097 16098 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16099 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16100 abort = true; 16101 } 16102 16103 if (abort || state == pci_channel_io_perm_failure) { 16104 rtnl_unlock(); 16105 return PCI_ERS_RESULT_DISCONNECT; 16106 } 16107 16108 /* Link is not reliable anymore if state is pci_channel_io_frozen 16109 * so we disable bus master to prevent any potential bad DMAs before 16110 * freeing kernel memory. 16111 */ 16112 if (state == pci_channel_io_frozen) { 16113 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16114 bnxt_fw_fatal_close(bp); 16115 } 16116 16117 if (netif_running(netdev)) 16118 __bnxt_close_nic(bp, true, true); 16119 16120 if (pci_is_enabled(pdev)) 16121 pci_disable_device(pdev); 16122 bnxt_free_ctx_mem(bp); 16123 rtnl_unlock(); 16124 16125 /* Request a slot slot reset. */ 16126 return PCI_ERS_RESULT_NEED_RESET; 16127 } 16128 16129 /** 16130 * bnxt_io_slot_reset - called after the pci bus has been reset. 16131 * @pdev: Pointer to PCI device 16132 * 16133 * Restart the card from scratch, as if from a cold-boot. 16134 * At this point, the card has exprienced a hard reset, 16135 * followed by fixups by BIOS, and has its config space 16136 * set up identically to what it was at cold boot. 16137 */ 16138 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 16139 { 16140 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 16141 struct net_device *netdev = pci_get_drvdata(pdev); 16142 struct bnxt *bp = netdev_priv(netdev); 16143 int retry = 0; 16144 int err = 0; 16145 int off; 16146 16147 netdev_info(bp->dev, "PCI Slot Reset\n"); 16148 16149 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 16150 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 16151 msleep(900); 16152 16153 rtnl_lock(); 16154 16155 if (pci_enable_device(pdev)) { 16156 dev_err(&pdev->dev, 16157 "Cannot re-enable PCI device after reset.\n"); 16158 } else { 16159 pci_set_master(pdev); 16160 /* Upon fatal error, our device internal logic that latches to 16161 * BAR value is getting reset and will restore only upon 16162 * rewritting the BARs. 16163 * 16164 * As pci_restore_state() does not re-write the BARs if the 16165 * value is same as saved value earlier, driver needs to 16166 * write the BARs to 0 to force restore, in case of fatal error. 16167 */ 16168 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 16169 &bp->state)) { 16170 for (off = PCI_BASE_ADDRESS_0; 16171 off <= PCI_BASE_ADDRESS_5; off += 4) 16172 pci_write_config_dword(bp->pdev, off, 0); 16173 } 16174 pci_restore_state(pdev); 16175 pci_save_state(pdev); 16176 16177 bnxt_inv_fw_health_reg(bp); 16178 bnxt_try_map_fw_health_reg(bp); 16179 16180 /* In some PCIe AER scenarios, firmware may take up to 16181 * 10 seconds to become ready in the worst case. 16182 */ 16183 do { 16184 err = bnxt_try_recover_fw(bp); 16185 if (!err) 16186 break; 16187 retry++; 16188 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 16189 16190 if (err) { 16191 dev_err(&pdev->dev, "Firmware not ready\n"); 16192 goto reset_exit; 16193 } 16194 16195 err = bnxt_hwrm_func_reset(bp); 16196 if (!err) 16197 result = PCI_ERS_RESULT_RECOVERED; 16198 16199 bnxt_ulp_irq_stop(bp); 16200 bnxt_clear_int_mode(bp); 16201 err = bnxt_init_int_mode(bp); 16202 bnxt_ulp_irq_restart(bp, err); 16203 } 16204 16205 reset_exit: 16206 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16207 bnxt_clear_reservations(bp, true); 16208 rtnl_unlock(); 16209 16210 return result; 16211 } 16212 16213 /** 16214 * bnxt_io_resume - called when traffic can start flowing again. 16215 * @pdev: Pointer to PCI device 16216 * 16217 * This callback is called when the error recovery driver tells 16218 * us that its OK to resume normal operation. 16219 */ 16220 static void bnxt_io_resume(struct pci_dev *pdev) 16221 { 16222 struct net_device *netdev = pci_get_drvdata(pdev); 16223 struct bnxt *bp = netdev_priv(netdev); 16224 int err; 16225 16226 netdev_info(bp->dev, "PCI Slot Resume\n"); 16227 rtnl_lock(); 16228 16229 err = bnxt_hwrm_func_qcaps(bp); 16230 if (!err && netif_running(netdev)) 16231 err = bnxt_open(netdev); 16232 16233 if (!err) 16234 netif_device_attach(netdev); 16235 16236 rtnl_unlock(); 16237 bnxt_ulp_start(bp, err); 16238 if (!err) 16239 bnxt_reenable_sriov(bp); 16240 } 16241 16242 static const struct pci_error_handlers bnxt_err_handler = { 16243 .error_detected = bnxt_io_error_detected, 16244 .slot_reset = bnxt_io_slot_reset, 16245 .resume = bnxt_io_resume 16246 }; 16247 16248 static struct pci_driver bnxt_pci_driver = { 16249 .name = DRV_MODULE_NAME, 16250 .id_table = bnxt_pci_tbl, 16251 .probe = bnxt_init_one, 16252 .remove = bnxt_remove_one, 16253 .shutdown = bnxt_shutdown, 16254 .driver.pm = BNXT_PM_OPS, 16255 .err_handler = &bnxt_err_handler, 16256 #if defined(CONFIG_BNXT_SRIOV) 16257 .sriov_configure = bnxt_sriov_configure, 16258 #endif 16259 }; 16260 16261 static int __init bnxt_init(void) 16262 { 16263 int err; 16264 16265 bnxt_debug_init(); 16266 err = pci_register_driver(&bnxt_pci_driver); 16267 if (err) { 16268 bnxt_debug_exit(); 16269 return err; 16270 } 16271 16272 return 0; 16273 } 16274 16275 static void __exit bnxt_exit(void) 16276 { 16277 pci_unregister_driver(&bnxt_pci_driver); 16278 if (bnxt_pf_wq) 16279 destroy_workqueue(bnxt_pf_wq); 16280 bnxt_debug_exit(); 16281 } 16282 16283 module_init(bnxt_init); 16284 module_exit(bnxt_exit); 16285