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_lock.h> 58 #include <net/netdev_queues.h> 59 #include <net/netdev_rx_queue.h> 60 #include <linux/pci-tph.h> 61 62 #include "bnxt_hsi.h" 63 #include "bnxt.h" 64 #include "bnxt_hwrm.h" 65 #include "bnxt_ulp.h" 66 #include "bnxt_sriov.h" 67 #include "bnxt_ethtool.h" 68 #include "bnxt_dcb.h" 69 #include "bnxt_xdp.h" 70 #include "bnxt_ptp.h" 71 #include "bnxt_vfr.h" 72 #include "bnxt_tc.h" 73 #include "bnxt_devlink.h" 74 #include "bnxt_debugfs.h" 75 #include "bnxt_coredump.h" 76 #include "bnxt_hwmon.h" 77 78 #define BNXT_TX_TIMEOUT (5 * HZ) 79 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 80 NETIF_MSG_TX_ERR) 81 82 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 83 MODULE_LICENSE("GPL"); 84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 85 86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 88 89 #define BNXT_TX_PUSH_THRESH 164 90 91 /* indexed by enum board_idx */ 92 static const struct { 93 char *name; 94 } board_info[] = { 95 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 96 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 97 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 98 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 99 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 100 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 101 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 102 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 103 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 104 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 105 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 106 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 108 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 109 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 110 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 111 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 112 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 113 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 114 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 115 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 116 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 117 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 118 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 119 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 120 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 121 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 122 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 123 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 124 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 125 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 126 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 127 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 128 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 129 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 130 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 131 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 132 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 133 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 134 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 135 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 136 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 137 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 138 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 139 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 140 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 141 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 142 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 143 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 144 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 145 }; 146 147 static const struct pci_device_id bnxt_pci_tbl[] = { 148 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 149 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 150 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 151 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 152 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 153 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 154 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 155 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 156 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 157 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 158 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 159 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 160 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 163 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 164 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 165 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 166 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 167 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 168 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 169 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 170 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 171 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 172 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 173 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 175 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 179 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 180 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 181 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 182 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 183 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 184 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 185 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 186 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 187 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 188 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 189 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 190 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 191 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 193 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 194 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 195 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 196 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 197 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 198 #ifdef CONFIG_BNXT_SRIOV 199 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 200 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 201 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 202 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 203 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 205 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 206 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 207 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 208 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 209 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 210 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 211 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 212 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 215 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 216 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 217 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 218 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 219 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 220 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 221 #endif 222 { 0 } 223 }; 224 225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 226 227 static const u16 bnxt_vf_req_snif[] = { 228 HWRM_FUNC_CFG, 229 HWRM_FUNC_VF_CFG, 230 HWRM_PORT_PHY_QCFG, 231 HWRM_CFA_L2_FILTER_ALLOC, 232 }; 233 234 static const u16 bnxt_async_events_arr[] = { 235 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 236 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 237 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 238 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 239 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 240 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 241 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 242 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 243 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 244 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 245 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 246 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 247 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 248 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 249 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 250 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 251 ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER, 252 }; 253 254 const u16 bnxt_bstore_to_trace[] = { 255 [BNXT_CTX_SRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE, 256 [BNXT_CTX_SRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE, 257 [BNXT_CTX_CRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE, 258 [BNXT_CTX_CRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE, 259 [BNXT_CTX_RIGP0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE, 260 [BNXT_CTX_L2HWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE, 261 [BNXT_CTX_REHWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE, 262 [BNXT_CTX_CA0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE, 263 [BNXT_CTX_CA1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE, 264 [BNXT_CTX_CA2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE, 265 [BNXT_CTX_RIGP1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE, 266 }; 267 268 static struct workqueue_struct *bnxt_pf_wq; 269 270 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 271 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 272 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 273 274 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 275 .ports = { 276 .src = 0, 277 .dst = 0, 278 }, 279 .addrs = { 280 .v6addrs = { 281 .src = BNXT_IPV6_MASK_NONE, 282 .dst = BNXT_IPV6_MASK_NONE, 283 }, 284 }, 285 }; 286 287 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 288 .ports = { 289 .src = cpu_to_be16(0xffff), 290 .dst = cpu_to_be16(0xffff), 291 }, 292 .addrs = { 293 .v6addrs = { 294 .src = BNXT_IPV6_MASK_ALL, 295 .dst = BNXT_IPV6_MASK_ALL, 296 }, 297 }, 298 }; 299 300 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 301 .ports = { 302 .src = cpu_to_be16(0xffff), 303 .dst = cpu_to_be16(0xffff), 304 }, 305 .addrs = { 306 .v4addrs = { 307 .src = cpu_to_be32(0xffffffff), 308 .dst = cpu_to_be32(0xffffffff), 309 }, 310 }, 311 }; 312 313 static bool bnxt_vf_pciid(enum board_idx idx) 314 { 315 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 316 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 317 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 318 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF); 319 } 320 321 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 322 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 323 324 #define BNXT_DB_CQ(db, idx) \ 325 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 326 327 #define BNXT_DB_NQ_P5(db, idx) \ 328 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 329 (db)->doorbell) 330 331 #define BNXT_DB_NQ_P7(db, idx) \ 332 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 333 DB_RING_IDX(db, idx), (db)->doorbell) 334 335 #define BNXT_DB_CQ_ARM(db, idx) \ 336 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 337 338 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 339 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 340 DB_RING_IDX(db, idx), (db)->doorbell) 341 342 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 343 { 344 if (bp->flags & BNXT_FLAG_CHIP_P7) 345 BNXT_DB_NQ_P7(db, idx); 346 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 347 BNXT_DB_NQ_P5(db, idx); 348 else 349 BNXT_DB_CQ(db, idx); 350 } 351 352 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 353 { 354 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 355 BNXT_DB_NQ_ARM_P5(db, idx); 356 else 357 BNXT_DB_CQ_ARM(db, idx); 358 } 359 360 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 361 { 362 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 363 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 364 DB_RING_IDX(db, idx), db->doorbell); 365 else 366 BNXT_DB_CQ(db, idx); 367 } 368 369 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 370 { 371 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 372 return; 373 374 if (BNXT_PF(bp)) 375 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 376 else 377 schedule_delayed_work(&bp->fw_reset_task, delay); 378 } 379 380 static void __bnxt_queue_sp_work(struct bnxt *bp) 381 { 382 if (BNXT_PF(bp)) 383 queue_work(bnxt_pf_wq, &bp->sp_task); 384 else 385 schedule_work(&bp->sp_task); 386 } 387 388 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 389 { 390 set_bit(event, &bp->sp_event); 391 __bnxt_queue_sp_work(bp); 392 } 393 394 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 395 { 396 if (!rxr->bnapi->in_reset) { 397 rxr->bnapi->in_reset = true; 398 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 399 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 400 else 401 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 402 __bnxt_queue_sp_work(bp); 403 } 404 rxr->rx_next_cons = 0xffff; 405 } 406 407 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 408 u16 curr) 409 { 410 struct bnxt_napi *bnapi = txr->bnapi; 411 412 if (bnapi->tx_fault) 413 return; 414 415 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 416 txr->txq_index, txr->tx_hw_cons, 417 txr->tx_cons, txr->tx_prod, curr); 418 WARN_ON_ONCE(1); 419 bnapi->tx_fault = 1; 420 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 421 } 422 423 const u16 bnxt_lhint_arr[] = { 424 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 425 TX_BD_FLAGS_LHINT_512_TO_1023, 426 TX_BD_FLAGS_LHINT_1024_TO_2047, 427 TX_BD_FLAGS_LHINT_1024_TO_2047, 428 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 429 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 430 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 431 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 432 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 433 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 434 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 435 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 436 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 437 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 438 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 439 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 440 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 441 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 442 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 443 }; 444 445 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 446 { 447 struct metadata_dst *md_dst = skb_metadata_dst(skb); 448 449 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 450 return 0; 451 452 return md_dst->u.port_info.port_id; 453 } 454 455 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 456 u16 prod) 457 { 458 /* Sync BD data before updating doorbell */ 459 wmb(); 460 bnxt_db_write(bp, &txr->tx_db, prod); 461 txr->kick_pending = 0; 462 } 463 464 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 465 { 466 struct bnxt *bp = netdev_priv(dev); 467 struct tx_bd *txbd, *txbd0; 468 struct tx_bd_ext *txbd1; 469 struct netdev_queue *txq; 470 int i; 471 dma_addr_t mapping; 472 unsigned int length, pad = 0; 473 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 474 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 475 struct pci_dev *pdev = bp->pdev; 476 u16 prod, last_frag, txts_prod; 477 struct bnxt_tx_ring_info *txr; 478 struct bnxt_sw_tx_bd *tx_buf; 479 __le32 lflags = 0; 480 481 i = skb_get_queue_mapping(skb); 482 if (unlikely(i >= bp->tx_nr_rings)) { 483 dev_kfree_skb_any(skb); 484 dev_core_stats_tx_dropped_inc(dev); 485 return NETDEV_TX_OK; 486 } 487 488 txq = netdev_get_tx_queue(dev, i); 489 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 490 prod = txr->tx_prod; 491 492 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS) 493 if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) { 494 netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d. SKB will be linearized.\n", 495 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS); 496 if (skb_linearize(skb)) { 497 dev_kfree_skb_any(skb); 498 dev_core_stats_tx_dropped_inc(dev); 499 return NETDEV_TX_OK; 500 } 501 } 502 #endif 503 free_size = bnxt_tx_avail(bp, txr); 504 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 505 /* We must have raced with NAPI cleanup */ 506 if (net_ratelimit() && txr->kick_pending) 507 netif_warn(bp, tx_err, dev, 508 "bnxt: ring busy w/ flush pending!\n"); 509 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 510 bp->tx_wake_thresh)) 511 return NETDEV_TX_BUSY; 512 } 513 514 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 515 goto tx_free; 516 517 length = skb->len; 518 len = skb_headlen(skb); 519 last_frag = skb_shinfo(skb)->nr_frags; 520 521 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 522 523 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 524 tx_buf->skb = skb; 525 tx_buf->nr_frags = last_frag; 526 527 vlan_tag_flags = 0; 528 cfa_action = bnxt_xmit_get_cfa_action(skb); 529 if (skb_vlan_tag_present(skb)) { 530 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 531 skb_vlan_tag_get(skb); 532 /* Currently supports 8021Q, 8021AD vlan offloads 533 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 534 */ 535 if (skb->vlan_proto == htons(ETH_P_8021Q)) 536 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 537 } 538 539 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 540 ptp->tx_tstamp_en) { 541 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 542 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 543 tx_buf->is_ts_pkt = 1; 544 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 545 } else if (!skb_is_gso(skb)) { 546 u16 seq_id, hdr_off; 547 548 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 549 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 550 if (vlan_tag_flags) 551 hdr_off += VLAN_HLEN; 552 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 553 tx_buf->is_ts_pkt = 1; 554 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 555 556 ptp->txts_req[txts_prod].tx_seqid = seq_id; 557 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 558 tx_buf->txts_prod = txts_prod; 559 } 560 } 561 } 562 if (unlikely(skb->no_fcs)) 563 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 564 565 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 566 !lflags) { 567 struct tx_push_buffer *tx_push_buf = txr->tx_push; 568 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 569 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 570 void __iomem *db = txr->tx_db.doorbell; 571 void *pdata = tx_push_buf->data; 572 u64 *end; 573 int j, push_len; 574 575 /* Set COAL_NOW to be ready quickly for the next push */ 576 tx_push->tx_bd_len_flags_type = 577 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 578 TX_BD_TYPE_LONG_TX_BD | 579 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 580 TX_BD_FLAGS_COAL_NOW | 581 TX_BD_FLAGS_PACKET_END | 582 TX_BD_CNT(2)); 583 584 if (skb->ip_summed == CHECKSUM_PARTIAL) 585 tx_push1->tx_bd_hsize_lflags = 586 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 587 else 588 tx_push1->tx_bd_hsize_lflags = 0; 589 590 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 591 tx_push1->tx_bd_cfa_action = 592 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 593 594 end = pdata + length; 595 end = PTR_ALIGN(end, 8) - 1; 596 *end = 0; 597 598 skb_copy_from_linear_data(skb, pdata, len); 599 pdata += len; 600 for (j = 0; j < last_frag; j++) { 601 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 602 void *fptr; 603 604 fptr = skb_frag_address_safe(frag); 605 if (!fptr) 606 goto normal_tx; 607 608 memcpy(pdata, fptr, skb_frag_size(frag)); 609 pdata += skb_frag_size(frag); 610 } 611 612 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 613 txbd->tx_bd_haddr = txr->data_mapping; 614 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 615 prod = NEXT_TX(prod); 616 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 617 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 618 memcpy(txbd, tx_push1, sizeof(*txbd)); 619 prod = NEXT_TX(prod); 620 tx_push->doorbell = 621 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 622 DB_RING_IDX(&txr->tx_db, prod)); 623 WRITE_ONCE(txr->tx_prod, prod); 624 625 tx_buf->is_push = 1; 626 netdev_tx_sent_queue(txq, skb->len); 627 wmb(); /* Sync is_push and byte queue before pushing data */ 628 629 push_len = (length + sizeof(*tx_push) + 7) / 8; 630 if (push_len > 16) { 631 __iowrite64_copy(db, tx_push_buf, 16); 632 __iowrite32_copy(db + 4, tx_push_buf + 1, 633 (push_len - 16) << 1); 634 } else { 635 __iowrite64_copy(db, tx_push_buf, push_len); 636 } 637 638 goto tx_done; 639 } 640 641 normal_tx: 642 if (length < BNXT_MIN_PKT_SIZE) { 643 pad = BNXT_MIN_PKT_SIZE - length; 644 if (skb_pad(skb, pad)) 645 /* SKB already freed. */ 646 goto tx_kick_pending; 647 length = BNXT_MIN_PKT_SIZE; 648 } 649 650 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 651 652 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 653 goto tx_free; 654 655 dma_unmap_addr_set(tx_buf, mapping, mapping); 656 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 657 TX_BD_CNT(last_frag + 2); 658 659 txbd->tx_bd_haddr = cpu_to_le64(mapping); 660 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 661 662 prod = NEXT_TX(prod); 663 txbd1 = (struct tx_bd_ext *) 664 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 665 666 txbd1->tx_bd_hsize_lflags = lflags; 667 if (skb_is_gso(skb)) { 668 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 669 u32 hdr_len; 670 671 if (skb->encapsulation) { 672 if (udp_gso) 673 hdr_len = skb_inner_transport_offset(skb) + 674 sizeof(struct udphdr); 675 else 676 hdr_len = skb_inner_tcp_all_headers(skb); 677 } else if (udp_gso) { 678 hdr_len = skb_transport_offset(skb) + 679 sizeof(struct udphdr); 680 } else { 681 hdr_len = skb_tcp_all_headers(skb); 682 } 683 684 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 685 TX_BD_FLAGS_T_IPID | 686 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 687 length = skb_shinfo(skb)->gso_size; 688 txbd1->tx_bd_mss = cpu_to_le32(length); 689 length += hdr_len; 690 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 691 txbd1->tx_bd_hsize_lflags |= 692 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 693 txbd1->tx_bd_mss = 0; 694 } 695 696 length >>= 9; 697 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 698 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 699 skb->len); 700 i = 0; 701 goto tx_dma_error; 702 } 703 flags |= bnxt_lhint_arr[length]; 704 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 705 706 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 707 txbd1->tx_bd_cfa_action = 708 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 709 txbd0 = txbd; 710 for (i = 0; i < last_frag; i++) { 711 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 712 713 prod = NEXT_TX(prod); 714 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 715 716 len = skb_frag_size(frag); 717 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 718 DMA_TO_DEVICE); 719 720 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 721 goto tx_dma_error; 722 723 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 724 dma_unmap_addr_set(tx_buf, mapping, mapping); 725 726 txbd->tx_bd_haddr = cpu_to_le64(mapping); 727 728 flags = len << TX_BD_LEN_SHIFT; 729 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 730 } 731 732 flags &= ~TX_BD_LEN; 733 txbd->tx_bd_len_flags_type = 734 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 735 TX_BD_FLAGS_PACKET_END); 736 737 netdev_tx_sent_queue(txq, skb->len); 738 739 skb_tx_timestamp(skb); 740 741 prod = NEXT_TX(prod); 742 WRITE_ONCE(txr->tx_prod, prod); 743 744 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 745 bnxt_txr_db_kick(bp, txr, prod); 746 } else { 747 if (free_size >= bp->tx_wake_thresh) 748 txbd0->tx_bd_len_flags_type |= 749 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 750 txr->kick_pending = 1; 751 } 752 753 tx_done: 754 755 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 756 if (netdev_xmit_more() && !tx_buf->is_push) { 757 txbd0->tx_bd_len_flags_type &= 758 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 759 bnxt_txr_db_kick(bp, txr, prod); 760 } 761 762 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 763 bp->tx_wake_thresh); 764 } 765 return NETDEV_TX_OK; 766 767 tx_dma_error: 768 last_frag = i; 769 770 /* start back at beginning and unmap skb */ 771 prod = txr->tx_prod; 772 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 773 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 774 skb_headlen(skb), DMA_TO_DEVICE); 775 prod = NEXT_TX(prod); 776 777 /* unmap remaining mapped pages */ 778 for (i = 0; i < last_frag; i++) { 779 prod = NEXT_TX(prod); 780 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 781 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 782 skb_frag_size(&skb_shinfo(skb)->frags[i]), 783 DMA_TO_DEVICE); 784 } 785 786 tx_free: 787 dev_kfree_skb_any(skb); 788 tx_kick_pending: 789 if (BNXT_TX_PTP_IS_SET(lflags)) { 790 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0; 791 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 792 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 793 /* set SKB to err so PTP worker will clean up */ 794 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 795 } 796 if (txr->kick_pending) 797 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 798 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL; 799 dev_core_stats_tx_dropped_inc(dev); 800 return NETDEV_TX_OK; 801 } 802 803 /* Returns true if some remaining TX packets not processed. */ 804 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 805 int budget) 806 { 807 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 808 struct pci_dev *pdev = bp->pdev; 809 u16 hw_cons = txr->tx_hw_cons; 810 unsigned int tx_bytes = 0; 811 u16 cons = txr->tx_cons; 812 int tx_pkts = 0; 813 bool rc = false; 814 815 while (RING_TX(bp, cons) != hw_cons) { 816 struct bnxt_sw_tx_bd *tx_buf; 817 struct sk_buff *skb; 818 bool is_ts_pkt; 819 int j, last; 820 821 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 822 skb = tx_buf->skb; 823 824 if (unlikely(!skb)) { 825 bnxt_sched_reset_txr(bp, txr, cons); 826 return rc; 827 } 828 829 is_ts_pkt = tx_buf->is_ts_pkt; 830 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 831 rc = true; 832 break; 833 } 834 835 cons = NEXT_TX(cons); 836 tx_pkts++; 837 tx_bytes += skb->len; 838 tx_buf->skb = NULL; 839 tx_buf->is_ts_pkt = 0; 840 841 if (tx_buf->is_push) { 842 tx_buf->is_push = 0; 843 goto next_tx_int; 844 } 845 846 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 847 skb_headlen(skb), DMA_TO_DEVICE); 848 last = tx_buf->nr_frags; 849 850 for (j = 0; j < last; j++) { 851 cons = NEXT_TX(cons); 852 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 853 dma_unmap_page( 854 &pdev->dev, 855 dma_unmap_addr(tx_buf, mapping), 856 skb_frag_size(&skb_shinfo(skb)->frags[j]), 857 DMA_TO_DEVICE); 858 } 859 if (unlikely(is_ts_pkt)) { 860 if (BNXT_CHIP_P5(bp)) { 861 /* PTP worker takes ownership of the skb */ 862 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 863 skb = NULL; 864 } 865 } 866 867 next_tx_int: 868 cons = NEXT_TX(cons); 869 870 dev_consume_skb_any(skb); 871 } 872 873 WRITE_ONCE(txr->tx_cons, cons); 874 875 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 876 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 877 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 878 879 return rc; 880 } 881 882 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 883 { 884 struct bnxt_tx_ring_info *txr; 885 bool more = false; 886 int i; 887 888 bnxt_for_each_napi_tx(i, bnapi, txr) { 889 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 890 more |= __bnxt_tx_int(bp, txr, budget); 891 } 892 if (!more) 893 bnapi->events &= ~BNXT_TX_CMP_EVENT; 894 } 895 896 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr) 897 { 898 return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE; 899 } 900 901 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 902 struct bnxt_rx_ring_info *rxr, 903 unsigned int *offset, 904 gfp_t gfp) 905 { 906 struct page *page; 907 908 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 909 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 910 BNXT_RX_PAGE_SIZE); 911 } else { 912 page = page_pool_dev_alloc_pages(rxr->page_pool); 913 *offset = 0; 914 } 915 if (!page) 916 return NULL; 917 918 *mapping = page_pool_get_dma_addr(page) + *offset; 919 return page; 920 } 921 922 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping, 923 struct bnxt_rx_ring_info *rxr, 924 gfp_t gfp) 925 { 926 netmem_ref netmem; 927 928 netmem = page_pool_alloc_netmems(rxr->page_pool, gfp); 929 if (!netmem) 930 return 0; 931 932 *mapping = page_pool_get_dma_addr_netmem(netmem); 933 return netmem; 934 } 935 936 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 937 struct bnxt_rx_ring_info *rxr, 938 gfp_t gfp) 939 { 940 unsigned int offset; 941 struct page *page; 942 943 page = page_pool_alloc_frag(rxr->head_pool, &offset, 944 bp->rx_buf_size, gfp); 945 if (!page) 946 return NULL; 947 948 *mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset; 949 return page_address(page) + offset; 950 } 951 952 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 953 u16 prod, gfp_t gfp) 954 { 955 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 956 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 957 dma_addr_t mapping; 958 959 if (BNXT_RX_PAGE_MODE(bp)) { 960 unsigned int offset; 961 struct page *page = 962 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 963 964 if (!page) 965 return -ENOMEM; 966 967 mapping += bp->rx_dma_offset; 968 rx_buf->data = page; 969 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 970 } else { 971 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp); 972 973 if (!data) 974 return -ENOMEM; 975 976 rx_buf->data = data; 977 rx_buf->data_ptr = data + bp->rx_offset; 978 } 979 rx_buf->mapping = mapping; 980 981 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 982 return 0; 983 } 984 985 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 986 { 987 u16 prod = rxr->rx_prod; 988 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 989 struct bnxt *bp = rxr->bnapi->bp; 990 struct rx_bd *cons_bd, *prod_bd; 991 992 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 993 cons_rx_buf = &rxr->rx_buf_ring[cons]; 994 995 prod_rx_buf->data = data; 996 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 997 998 prod_rx_buf->mapping = cons_rx_buf->mapping; 999 1000 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1001 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 1002 1003 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 1004 } 1005 1006 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1007 { 1008 u16 next, max = rxr->rx_agg_bmap_size; 1009 1010 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 1011 if (next >= max) 1012 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 1013 return next; 1014 } 1015 1016 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1017 u16 prod, gfp_t gfp) 1018 { 1019 struct rx_bd *rxbd = 1020 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1021 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 1022 u16 sw_prod = rxr->rx_sw_agg_prod; 1023 unsigned int offset = 0; 1024 dma_addr_t mapping; 1025 netmem_ref netmem; 1026 1027 netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, gfp); 1028 if (!netmem) 1029 return -ENOMEM; 1030 1031 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1032 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1033 1034 __set_bit(sw_prod, rxr->rx_agg_bmap); 1035 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1036 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1037 1038 rx_agg_buf->netmem = netmem; 1039 rx_agg_buf->offset = offset; 1040 rx_agg_buf->mapping = mapping; 1041 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1042 rxbd->rx_bd_opaque = sw_prod; 1043 return 0; 1044 } 1045 1046 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1047 struct bnxt_cp_ring_info *cpr, 1048 u16 cp_cons, u16 curr) 1049 { 1050 struct rx_agg_cmp *agg; 1051 1052 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1053 agg = (struct rx_agg_cmp *) 1054 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1055 return agg; 1056 } 1057 1058 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1059 struct bnxt_rx_ring_info *rxr, 1060 u16 agg_id, u16 curr) 1061 { 1062 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1063 1064 return &tpa_info->agg_arr[curr]; 1065 } 1066 1067 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1068 u16 start, u32 agg_bufs, bool tpa) 1069 { 1070 struct bnxt_napi *bnapi = cpr->bnapi; 1071 struct bnxt *bp = bnapi->bp; 1072 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1073 u16 prod = rxr->rx_agg_prod; 1074 u16 sw_prod = rxr->rx_sw_agg_prod; 1075 bool p5_tpa = false; 1076 u32 i; 1077 1078 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1079 p5_tpa = true; 1080 1081 for (i = 0; i < agg_bufs; i++) { 1082 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1083 struct rx_agg_cmp *agg; 1084 struct rx_bd *prod_bd; 1085 netmem_ref netmem; 1086 u16 cons; 1087 1088 if (p5_tpa) 1089 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1090 else 1091 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1092 cons = agg->rx_agg_cmp_opaque; 1093 __clear_bit(cons, rxr->rx_agg_bmap); 1094 1095 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1096 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1097 1098 __set_bit(sw_prod, rxr->rx_agg_bmap); 1099 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1100 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1101 1102 /* It is possible for sw_prod to be equal to cons, so 1103 * set cons_rx_buf->netmem to 0 first. 1104 */ 1105 netmem = cons_rx_buf->netmem; 1106 cons_rx_buf->netmem = 0; 1107 prod_rx_buf->netmem = netmem; 1108 prod_rx_buf->offset = cons_rx_buf->offset; 1109 1110 prod_rx_buf->mapping = cons_rx_buf->mapping; 1111 1112 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1113 1114 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1115 prod_bd->rx_bd_opaque = sw_prod; 1116 1117 prod = NEXT_RX_AGG(prod); 1118 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1119 } 1120 rxr->rx_agg_prod = prod; 1121 rxr->rx_sw_agg_prod = sw_prod; 1122 } 1123 1124 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1125 struct bnxt_rx_ring_info *rxr, 1126 u16 cons, void *data, u8 *data_ptr, 1127 dma_addr_t dma_addr, 1128 unsigned int offset_and_len) 1129 { 1130 unsigned int len = offset_and_len & 0xffff; 1131 struct page *page = data; 1132 u16 prod = rxr->rx_prod; 1133 struct sk_buff *skb; 1134 int err; 1135 1136 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1137 if (unlikely(err)) { 1138 bnxt_reuse_rx_data(rxr, cons, data); 1139 return NULL; 1140 } 1141 dma_addr -= bp->rx_dma_offset; 1142 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1143 bp->rx_dir); 1144 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1145 if (!skb) { 1146 page_pool_recycle_direct(rxr->page_pool, page); 1147 return NULL; 1148 } 1149 skb_mark_for_recycle(skb); 1150 skb_reserve(skb, bp->rx_offset); 1151 __skb_put(skb, len); 1152 1153 return skb; 1154 } 1155 1156 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1157 struct bnxt_rx_ring_info *rxr, 1158 u16 cons, void *data, u8 *data_ptr, 1159 dma_addr_t dma_addr, 1160 unsigned int offset_and_len) 1161 { 1162 unsigned int payload = offset_and_len >> 16; 1163 unsigned int len = offset_and_len & 0xffff; 1164 skb_frag_t *frag; 1165 struct page *page = data; 1166 u16 prod = rxr->rx_prod; 1167 struct sk_buff *skb; 1168 int off, err; 1169 1170 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1171 if (unlikely(err)) { 1172 bnxt_reuse_rx_data(rxr, cons, data); 1173 return NULL; 1174 } 1175 dma_addr -= bp->rx_dma_offset; 1176 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1177 bp->rx_dir); 1178 1179 if (unlikely(!payload)) 1180 payload = eth_get_headlen(bp->dev, data_ptr, len); 1181 1182 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1183 if (!skb) { 1184 page_pool_recycle_direct(rxr->page_pool, page); 1185 return NULL; 1186 } 1187 1188 skb_mark_for_recycle(skb); 1189 off = (void *)data_ptr - page_address(page); 1190 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1191 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1192 payload + NET_IP_ALIGN); 1193 1194 frag = &skb_shinfo(skb)->frags[0]; 1195 skb_frag_size_sub(frag, payload); 1196 skb_frag_off_add(frag, payload); 1197 skb->data_len -= payload; 1198 skb->tail += payload; 1199 1200 return skb; 1201 } 1202 1203 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1204 struct bnxt_rx_ring_info *rxr, u16 cons, 1205 void *data, u8 *data_ptr, 1206 dma_addr_t dma_addr, 1207 unsigned int offset_and_len) 1208 { 1209 u16 prod = rxr->rx_prod; 1210 struct sk_buff *skb; 1211 int err; 1212 1213 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1214 if (unlikely(err)) { 1215 bnxt_reuse_rx_data(rxr, cons, data); 1216 return NULL; 1217 } 1218 1219 skb = napi_build_skb(data, bp->rx_buf_size); 1220 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1221 bp->rx_dir); 1222 if (!skb) { 1223 page_pool_free_va(rxr->head_pool, data, true); 1224 return NULL; 1225 } 1226 1227 skb_mark_for_recycle(skb); 1228 skb_reserve(skb, bp->rx_offset); 1229 skb_put(skb, offset_and_len & 0xffff); 1230 return skb; 1231 } 1232 1233 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp, 1234 struct bnxt_cp_ring_info *cpr, 1235 u16 idx, u32 agg_bufs, bool tpa, 1236 struct sk_buff *skb, 1237 struct xdp_buff *xdp) 1238 { 1239 struct bnxt_napi *bnapi = cpr->bnapi; 1240 struct skb_shared_info *shinfo; 1241 struct bnxt_rx_ring_info *rxr; 1242 u32 i, total_frag_len = 0; 1243 bool p5_tpa = false; 1244 u16 prod; 1245 1246 rxr = bnapi->rx_ring; 1247 prod = rxr->rx_agg_prod; 1248 1249 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1250 p5_tpa = true; 1251 1252 if (skb) 1253 shinfo = skb_shinfo(skb); 1254 else 1255 shinfo = xdp_get_shared_info_from_buff(xdp); 1256 1257 for (i = 0; i < agg_bufs; i++) { 1258 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1259 struct rx_agg_cmp *agg; 1260 u16 cons, frag_len; 1261 netmem_ref netmem; 1262 1263 if (p5_tpa) 1264 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1265 else 1266 agg = bnxt_get_agg(bp, cpr, idx, i); 1267 cons = agg->rx_agg_cmp_opaque; 1268 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1269 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1270 1271 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1272 if (skb) { 1273 skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem, 1274 cons_rx_buf->offset, 1275 frag_len, BNXT_RX_PAGE_SIZE); 1276 } else { 1277 skb_frag_t *frag = &shinfo->frags[i]; 1278 1279 skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem, 1280 cons_rx_buf->offset, 1281 frag_len); 1282 shinfo->nr_frags = i + 1; 1283 } 1284 __clear_bit(cons, rxr->rx_agg_bmap); 1285 1286 /* It is possible for bnxt_alloc_rx_netmem() to allocate 1287 * a sw_prod index that equals the cons index, so we 1288 * need to clear the cons entry now. 1289 */ 1290 netmem = cons_rx_buf->netmem; 1291 cons_rx_buf->netmem = 0; 1292 1293 if (xdp && netmem_is_pfmemalloc(netmem)) 1294 xdp_buff_set_frag_pfmemalloc(xdp); 1295 1296 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) { 1297 if (skb) { 1298 skb->len -= frag_len; 1299 skb->data_len -= frag_len; 1300 skb->truesize -= BNXT_RX_PAGE_SIZE; 1301 } 1302 1303 --shinfo->nr_frags; 1304 cons_rx_buf->netmem = netmem; 1305 1306 /* Update prod since possibly some netmems have been 1307 * allocated already. 1308 */ 1309 rxr->rx_agg_prod = prod; 1310 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1311 return 0; 1312 } 1313 1314 page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0, 1315 BNXT_RX_PAGE_SIZE); 1316 1317 total_frag_len += frag_len; 1318 prod = NEXT_RX_AGG(prod); 1319 } 1320 rxr->rx_agg_prod = prod; 1321 return total_frag_len; 1322 } 1323 1324 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp, 1325 struct bnxt_cp_ring_info *cpr, 1326 struct sk_buff *skb, u16 idx, 1327 u32 agg_bufs, bool tpa) 1328 { 1329 u32 total_frag_len = 0; 1330 1331 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1332 skb, NULL); 1333 if (!total_frag_len) { 1334 skb_mark_for_recycle(skb); 1335 dev_kfree_skb(skb); 1336 return NULL; 1337 } 1338 1339 return skb; 1340 } 1341 1342 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp, 1343 struct bnxt_cp_ring_info *cpr, 1344 struct xdp_buff *xdp, u16 idx, 1345 u32 agg_bufs, bool tpa) 1346 { 1347 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1348 u32 total_frag_len = 0; 1349 1350 if (!xdp_buff_has_frags(xdp)) 1351 shinfo->nr_frags = 0; 1352 1353 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1354 NULL, xdp); 1355 if (total_frag_len) { 1356 xdp_buff_set_frags_flag(xdp); 1357 shinfo->nr_frags = agg_bufs; 1358 shinfo->xdp_frags_size = total_frag_len; 1359 } 1360 return total_frag_len; 1361 } 1362 1363 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1364 u8 agg_bufs, u32 *raw_cons) 1365 { 1366 u16 last; 1367 struct rx_agg_cmp *agg; 1368 1369 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1370 last = RING_CMP(*raw_cons); 1371 agg = (struct rx_agg_cmp *) 1372 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1373 return RX_AGG_CMP_VALID(agg, *raw_cons); 1374 } 1375 1376 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1377 unsigned int len, 1378 dma_addr_t mapping) 1379 { 1380 struct bnxt *bp = bnapi->bp; 1381 struct pci_dev *pdev = bp->pdev; 1382 struct sk_buff *skb; 1383 1384 skb = napi_alloc_skb(&bnapi->napi, len); 1385 if (!skb) 1386 return NULL; 1387 1388 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak, 1389 bp->rx_dir); 1390 1391 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1392 len + NET_IP_ALIGN); 1393 1394 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak, 1395 bp->rx_dir); 1396 1397 skb_put(skb, len); 1398 1399 return skb; 1400 } 1401 1402 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1403 unsigned int len, 1404 dma_addr_t mapping) 1405 { 1406 return bnxt_copy_data(bnapi, data, len, mapping); 1407 } 1408 1409 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1410 struct xdp_buff *xdp, 1411 unsigned int len, 1412 dma_addr_t mapping) 1413 { 1414 unsigned int metasize = 0; 1415 u8 *data = xdp->data; 1416 struct sk_buff *skb; 1417 1418 len = xdp->data_end - xdp->data_meta; 1419 metasize = xdp->data - xdp->data_meta; 1420 data = xdp->data_meta; 1421 1422 skb = bnxt_copy_data(bnapi, data, len, mapping); 1423 if (!skb) 1424 return skb; 1425 1426 if (metasize) { 1427 skb_metadata_set(skb, metasize); 1428 __skb_pull(skb, metasize); 1429 } 1430 1431 return skb; 1432 } 1433 1434 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1435 u32 *raw_cons, void *cmp) 1436 { 1437 struct rx_cmp *rxcmp = cmp; 1438 u32 tmp_raw_cons = *raw_cons; 1439 u8 cmp_type, agg_bufs = 0; 1440 1441 cmp_type = RX_CMP_TYPE(rxcmp); 1442 1443 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1444 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1445 RX_CMP_AGG_BUFS) >> 1446 RX_CMP_AGG_BUFS_SHIFT; 1447 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1448 struct rx_tpa_end_cmp *tpa_end = cmp; 1449 1450 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1451 return 0; 1452 1453 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1454 } 1455 1456 if (agg_bufs) { 1457 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1458 return -EBUSY; 1459 } 1460 *raw_cons = tmp_raw_cons; 1461 return 0; 1462 } 1463 1464 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1465 { 1466 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1467 u16 idx = agg_id & MAX_TPA_P5_MASK; 1468 1469 if (test_bit(idx, map->agg_idx_bmap)) 1470 idx = find_first_zero_bit(map->agg_idx_bmap, 1471 BNXT_AGG_IDX_BMAP_SIZE); 1472 __set_bit(idx, map->agg_idx_bmap); 1473 map->agg_id_tbl[agg_id] = idx; 1474 return idx; 1475 } 1476 1477 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1478 { 1479 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1480 1481 __clear_bit(idx, map->agg_idx_bmap); 1482 } 1483 1484 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1485 { 1486 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1487 1488 return map->agg_id_tbl[agg_id]; 1489 } 1490 1491 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1492 struct rx_tpa_start_cmp *tpa_start, 1493 struct rx_tpa_start_cmp_ext *tpa_start1) 1494 { 1495 tpa_info->cfa_code_valid = 1; 1496 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1497 tpa_info->vlan_valid = 0; 1498 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1499 tpa_info->vlan_valid = 1; 1500 tpa_info->metadata = 1501 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1502 } 1503 } 1504 1505 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1506 struct rx_tpa_start_cmp *tpa_start, 1507 struct rx_tpa_start_cmp_ext *tpa_start1) 1508 { 1509 tpa_info->vlan_valid = 0; 1510 if (TPA_START_VLAN_VALID(tpa_start)) { 1511 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1512 u32 vlan_proto = ETH_P_8021Q; 1513 1514 tpa_info->vlan_valid = 1; 1515 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1516 vlan_proto = ETH_P_8021AD; 1517 tpa_info->metadata = vlan_proto << 16 | 1518 TPA_START_METADATA0_TCI(tpa_start1); 1519 } 1520 } 1521 1522 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1523 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1524 struct rx_tpa_start_cmp_ext *tpa_start1) 1525 { 1526 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1527 struct bnxt_tpa_info *tpa_info; 1528 u16 cons, prod, agg_id; 1529 struct rx_bd *prod_bd; 1530 dma_addr_t mapping; 1531 1532 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1533 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1534 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1535 } else { 1536 agg_id = TPA_START_AGG_ID(tpa_start); 1537 } 1538 cons = tpa_start->rx_tpa_start_cmp_opaque; 1539 prod = rxr->rx_prod; 1540 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1541 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1542 tpa_info = &rxr->rx_tpa[agg_id]; 1543 1544 if (unlikely(cons != rxr->rx_next_cons || 1545 TPA_START_ERROR(tpa_start))) { 1546 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1547 cons, rxr->rx_next_cons, 1548 TPA_START_ERROR_CODE(tpa_start1)); 1549 bnxt_sched_reset_rxr(bp, rxr); 1550 return; 1551 } 1552 prod_rx_buf->data = tpa_info->data; 1553 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1554 1555 mapping = tpa_info->mapping; 1556 prod_rx_buf->mapping = mapping; 1557 1558 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1559 1560 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1561 1562 tpa_info->data = cons_rx_buf->data; 1563 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1564 cons_rx_buf->data = NULL; 1565 tpa_info->mapping = cons_rx_buf->mapping; 1566 1567 tpa_info->len = 1568 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1569 RX_TPA_START_CMP_LEN_SHIFT; 1570 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1571 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1572 tpa_info->gso_type = SKB_GSO_TCPV4; 1573 if (TPA_START_IS_IPV6(tpa_start1)) 1574 tpa_info->gso_type = SKB_GSO_TCPV6; 1575 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1576 else if (!BNXT_CHIP_P4_PLUS(bp) && 1577 TPA_START_HASH_TYPE(tpa_start) == 3) 1578 tpa_info->gso_type = SKB_GSO_TCPV6; 1579 tpa_info->rss_hash = 1580 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1581 } else { 1582 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1583 tpa_info->gso_type = 0; 1584 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1585 } 1586 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1587 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1588 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1589 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1590 else 1591 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1592 tpa_info->agg_count = 0; 1593 1594 rxr->rx_prod = NEXT_RX(prod); 1595 cons = RING_RX(bp, NEXT_RX(cons)); 1596 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1597 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1598 1599 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1600 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1601 cons_rx_buf->data = NULL; 1602 } 1603 1604 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1605 { 1606 if (agg_bufs) 1607 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1608 } 1609 1610 #ifdef CONFIG_INET 1611 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1612 { 1613 struct udphdr *uh = NULL; 1614 1615 if (ip_proto == htons(ETH_P_IP)) { 1616 struct iphdr *iph = (struct iphdr *)skb->data; 1617 1618 if (iph->protocol == IPPROTO_UDP) 1619 uh = (struct udphdr *)(iph + 1); 1620 } else { 1621 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1622 1623 if (iph->nexthdr == IPPROTO_UDP) 1624 uh = (struct udphdr *)(iph + 1); 1625 } 1626 if (uh) { 1627 if (uh->check) 1628 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1629 else 1630 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1631 } 1632 } 1633 #endif 1634 1635 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1636 int payload_off, int tcp_ts, 1637 struct sk_buff *skb) 1638 { 1639 #ifdef CONFIG_INET 1640 struct tcphdr *th; 1641 int len, nw_off; 1642 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1643 u32 hdr_info = tpa_info->hdr_info; 1644 bool loopback = false; 1645 1646 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1647 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1648 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1649 1650 /* If the packet is an internal loopback packet, the offsets will 1651 * have an extra 4 bytes. 1652 */ 1653 if (inner_mac_off == 4) { 1654 loopback = true; 1655 } else if (inner_mac_off > 4) { 1656 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1657 ETH_HLEN - 2)); 1658 1659 /* We only support inner iPv4/ipv6. If we don't see the 1660 * correct protocol ID, it must be a loopback packet where 1661 * the offsets are off by 4. 1662 */ 1663 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1664 loopback = true; 1665 } 1666 if (loopback) { 1667 /* internal loopback packet, subtract all offsets by 4 */ 1668 inner_ip_off -= 4; 1669 inner_mac_off -= 4; 1670 outer_ip_off -= 4; 1671 } 1672 1673 nw_off = inner_ip_off - ETH_HLEN; 1674 skb_set_network_header(skb, nw_off); 1675 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1676 struct ipv6hdr *iph = ipv6_hdr(skb); 1677 1678 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1679 len = skb->len - skb_transport_offset(skb); 1680 th = tcp_hdr(skb); 1681 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1682 } else { 1683 struct iphdr *iph = ip_hdr(skb); 1684 1685 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1686 len = skb->len - skb_transport_offset(skb); 1687 th = tcp_hdr(skb); 1688 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1689 } 1690 1691 if (inner_mac_off) { /* tunnel */ 1692 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1693 ETH_HLEN - 2)); 1694 1695 bnxt_gro_tunnel(skb, proto); 1696 } 1697 #endif 1698 return skb; 1699 } 1700 1701 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1702 int payload_off, int tcp_ts, 1703 struct sk_buff *skb) 1704 { 1705 #ifdef CONFIG_INET 1706 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1707 u32 hdr_info = tpa_info->hdr_info; 1708 int iphdr_len, nw_off; 1709 1710 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1711 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1712 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1713 1714 nw_off = inner_ip_off - ETH_HLEN; 1715 skb_set_network_header(skb, nw_off); 1716 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1717 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1718 skb_set_transport_header(skb, nw_off + iphdr_len); 1719 1720 if (inner_mac_off) { /* tunnel */ 1721 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1722 ETH_HLEN - 2)); 1723 1724 bnxt_gro_tunnel(skb, proto); 1725 } 1726 #endif 1727 return skb; 1728 } 1729 1730 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1731 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1732 1733 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1734 int payload_off, int tcp_ts, 1735 struct sk_buff *skb) 1736 { 1737 #ifdef CONFIG_INET 1738 struct tcphdr *th; 1739 int len, nw_off, tcp_opt_len = 0; 1740 1741 if (tcp_ts) 1742 tcp_opt_len = 12; 1743 1744 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1745 struct iphdr *iph; 1746 1747 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1748 ETH_HLEN; 1749 skb_set_network_header(skb, nw_off); 1750 iph = ip_hdr(skb); 1751 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1752 len = skb->len - skb_transport_offset(skb); 1753 th = tcp_hdr(skb); 1754 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1755 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1756 struct ipv6hdr *iph; 1757 1758 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1759 ETH_HLEN; 1760 skb_set_network_header(skb, nw_off); 1761 iph = ipv6_hdr(skb); 1762 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1763 len = skb->len - skb_transport_offset(skb); 1764 th = tcp_hdr(skb); 1765 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1766 } else { 1767 dev_kfree_skb_any(skb); 1768 return NULL; 1769 } 1770 1771 if (nw_off) /* tunnel */ 1772 bnxt_gro_tunnel(skb, skb->protocol); 1773 #endif 1774 return skb; 1775 } 1776 1777 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1778 struct bnxt_tpa_info *tpa_info, 1779 struct rx_tpa_end_cmp *tpa_end, 1780 struct rx_tpa_end_cmp_ext *tpa_end1, 1781 struct sk_buff *skb) 1782 { 1783 #ifdef CONFIG_INET 1784 int payload_off; 1785 u16 segs; 1786 1787 segs = TPA_END_TPA_SEGS(tpa_end); 1788 if (segs == 1) 1789 return skb; 1790 1791 NAPI_GRO_CB(skb)->count = segs; 1792 skb_shinfo(skb)->gso_size = 1793 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1794 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1795 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1796 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1797 else 1798 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1799 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1800 if (likely(skb)) 1801 tcp_gro_complete(skb); 1802 #endif 1803 return skb; 1804 } 1805 1806 /* Given the cfa_code of a received packet determine which 1807 * netdev (vf-rep or PF) the packet is destined to. 1808 */ 1809 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1810 { 1811 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1812 1813 /* if vf-rep dev is NULL, the must belongs to the PF */ 1814 return dev ? dev : bp->dev; 1815 } 1816 1817 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1818 struct bnxt_cp_ring_info *cpr, 1819 u32 *raw_cons, 1820 struct rx_tpa_end_cmp *tpa_end, 1821 struct rx_tpa_end_cmp_ext *tpa_end1, 1822 u8 *event) 1823 { 1824 struct bnxt_napi *bnapi = cpr->bnapi; 1825 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1826 struct net_device *dev = bp->dev; 1827 u8 *data_ptr, agg_bufs; 1828 unsigned int len; 1829 struct bnxt_tpa_info *tpa_info; 1830 dma_addr_t mapping; 1831 struct sk_buff *skb; 1832 u16 idx = 0, agg_id; 1833 void *data; 1834 bool gro; 1835 1836 if (unlikely(bnapi->in_reset)) { 1837 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1838 1839 if (rc < 0) 1840 return ERR_PTR(-EBUSY); 1841 return NULL; 1842 } 1843 1844 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1845 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1846 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1847 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1848 tpa_info = &rxr->rx_tpa[agg_id]; 1849 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1850 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1851 agg_bufs, tpa_info->agg_count); 1852 agg_bufs = tpa_info->agg_count; 1853 } 1854 tpa_info->agg_count = 0; 1855 *event |= BNXT_AGG_EVENT; 1856 bnxt_free_agg_idx(rxr, agg_id); 1857 idx = agg_id; 1858 gro = !!(bp->flags & BNXT_FLAG_GRO); 1859 } else { 1860 agg_id = TPA_END_AGG_ID(tpa_end); 1861 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1862 tpa_info = &rxr->rx_tpa[agg_id]; 1863 idx = RING_CMP(*raw_cons); 1864 if (agg_bufs) { 1865 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1866 return ERR_PTR(-EBUSY); 1867 1868 *event |= BNXT_AGG_EVENT; 1869 idx = NEXT_CMP(idx); 1870 } 1871 gro = !!TPA_END_GRO(tpa_end); 1872 } 1873 data = tpa_info->data; 1874 data_ptr = tpa_info->data_ptr; 1875 prefetch(data_ptr); 1876 len = tpa_info->len; 1877 mapping = tpa_info->mapping; 1878 1879 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1880 bnxt_abort_tpa(cpr, idx, agg_bufs); 1881 if (agg_bufs > MAX_SKB_FRAGS) 1882 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1883 agg_bufs, (int)MAX_SKB_FRAGS); 1884 return NULL; 1885 } 1886 1887 if (len <= bp->rx_copybreak) { 1888 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1889 if (!skb) { 1890 bnxt_abort_tpa(cpr, idx, agg_bufs); 1891 cpr->sw_stats->rx.rx_oom_discards += 1; 1892 return NULL; 1893 } 1894 } else { 1895 u8 *new_data; 1896 dma_addr_t new_mapping; 1897 1898 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1899 GFP_ATOMIC); 1900 if (!new_data) { 1901 bnxt_abort_tpa(cpr, idx, agg_bufs); 1902 cpr->sw_stats->rx.rx_oom_discards += 1; 1903 return NULL; 1904 } 1905 1906 tpa_info->data = new_data; 1907 tpa_info->data_ptr = new_data + bp->rx_offset; 1908 tpa_info->mapping = new_mapping; 1909 1910 skb = napi_build_skb(data, bp->rx_buf_size); 1911 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 1912 bp->rx_buf_use_size, bp->rx_dir); 1913 1914 if (!skb) { 1915 page_pool_free_va(rxr->head_pool, data, true); 1916 bnxt_abort_tpa(cpr, idx, agg_bufs); 1917 cpr->sw_stats->rx.rx_oom_discards += 1; 1918 return NULL; 1919 } 1920 skb_mark_for_recycle(skb); 1921 skb_reserve(skb, bp->rx_offset); 1922 skb_put(skb, len); 1923 } 1924 1925 if (agg_bufs) { 1926 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs, 1927 true); 1928 if (!skb) { 1929 /* Page reuse already handled by bnxt_rx_pages(). */ 1930 cpr->sw_stats->rx.rx_oom_discards += 1; 1931 return NULL; 1932 } 1933 } 1934 1935 if (tpa_info->cfa_code_valid) 1936 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1937 skb->protocol = eth_type_trans(skb, dev); 1938 1939 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1940 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1941 1942 if (tpa_info->vlan_valid && 1943 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1944 __be16 vlan_proto = htons(tpa_info->metadata >> 1945 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1946 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1947 1948 if (eth_type_vlan(vlan_proto)) { 1949 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1950 } else { 1951 dev_kfree_skb(skb); 1952 return NULL; 1953 } 1954 } 1955 1956 skb_checksum_none_assert(skb); 1957 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1958 skb->ip_summed = CHECKSUM_UNNECESSARY; 1959 skb->csum_level = 1960 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1961 } 1962 1963 if (gro) 1964 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1965 1966 return skb; 1967 } 1968 1969 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1970 struct rx_agg_cmp *rx_agg) 1971 { 1972 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1973 struct bnxt_tpa_info *tpa_info; 1974 1975 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1976 tpa_info = &rxr->rx_tpa[agg_id]; 1977 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1978 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1979 } 1980 1981 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1982 struct sk_buff *skb) 1983 { 1984 skb_mark_for_recycle(skb); 1985 1986 if (skb->dev != bp->dev) { 1987 /* this packet belongs to a vf-rep */ 1988 bnxt_vf_rep_rx(bp, skb); 1989 return; 1990 } 1991 skb_record_rx_queue(skb, bnapi->index); 1992 napi_gro_receive(&bnapi->napi, skb); 1993 } 1994 1995 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 1996 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 1997 { 1998 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 1999 2000 if (BNXT_PTP_RX_TS_VALID(flags)) 2001 goto ts_valid; 2002 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 2003 return false; 2004 2005 ts_valid: 2006 *cmpl_ts = ts; 2007 return true; 2008 } 2009 2010 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 2011 struct rx_cmp *rxcmp, 2012 struct rx_cmp_ext *rxcmp1) 2013 { 2014 __be16 vlan_proto; 2015 u16 vtag; 2016 2017 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 2018 __le32 flags2 = rxcmp1->rx_cmp_flags2; 2019 u32 meta_data; 2020 2021 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 2022 return skb; 2023 2024 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 2025 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2026 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 2027 if (eth_type_vlan(vlan_proto)) 2028 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2029 else 2030 goto vlan_err; 2031 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2032 if (RX_CMP_VLAN_VALID(rxcmp)) { 2033 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 2034 2035 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 2036 vlan_proto = htons(ETH_P_8021Q); 2037 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 2038 vlan_proto = htons(ETH_P_8021AD); 2039 else 2040 goto vlan_err; 2041 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 2042 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2043 } 2044 } 2045 return skb; 2046 vlan_err: 2047 skb_mark_for_recycle(skb); 2048 dev_kfree_skb(skb); 2049 return NULL; 2050 } 2051 2052 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 2053 struct rx_cmp *rxcmp) 2054 { 2055 u8 ext_op; 2056 2057 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2058 switch (ext_op) { 2059 case EXT_OP_INNER_4: 2060 case EXT_OP_OUTER_4: 2061 case EXT_OP_INNFL_3: 2062 case EXT_OP_OUTFL_3: 2063 return PKT_HASH_TYPE_L4; 2064 default: 2065 return PKT_HASH_TYPE_L3; 2066 } 2067 } 2068 2069 /* returns the following: 2070 * 1 - 1 packet successfully received 2071 * 0 - successful TPA_START, packet not completed yet 2072 * -EBUSY - completion ring does not have all the agg buffers yet 2073 * -ENOMEM - packet aborted due to out of memory 2074 * -EIO - packet aborted due to hw error indicated in BD 2075 */ 2076 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2077 u32 *raw_cons, u8 *event) 2078 { 2079 struct bnxt_napi *bnapi = cpr->bnapi; 2080 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2081 struct net_device *dev = bp->dev; 2082 struct rx_cmp *rxcmp; 2083 struct rx_cmp_ext *rxcmp1; 2084 u32 tmp_raw_cons = *raw_cons; 2085 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2086 struct skb_shared_info *sinfo; 2087 struct bnxt_sw_rx_bd *rx_buf; 2088 unsigned int len; 2089 u8 *data_ptr, agg_bufs, cmp_type; 2090 bool xdp_active = false; 2091 dma_addr_t dma_addr; 2092 struct sk_buff *skb; 2093 struct xdp_buff xdp; 2094 u32 flags, misc; 2095 u32 cmpl_ts; 2096 void *data; 2097 int rc = 0; 2098 2099 rxcmp = (struct rx_cmp *) 2100 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2101 2102 cmp_type = RX_CMP_TYPE(rxcmp); 2103 2104 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2105 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2106 goto next_rx_no_prod_no_len; 2107 } 2108 2109 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2110 cp_cons = RING_CMP(tmp_raw_cons); 2111 rxcmp1 = (struct rx_cmp_ext *) 2112 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2113 2114 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2115 return -EBUSY; 2116 2117 /* The valid test of the entry must be done first before 2118 * reading any further. 2119 */ 2120 dma_rmb(); 2121 prod = rxr->rx_prod; 2122 2123 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2124 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2125 bnxt_tpa_start(bp, rxr, cmp_type, 2126 (struct rx_tpa_start_cmp *)rxcmp, 2127 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2128 2129 *event |= BNXT_RX_EVENT; 2130 goto next_rx_no_prod_no_len; 2131 2132 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2133 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2134 (struct rx_tpa_end_cmp *)rxcmp, 2135 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2136 2137 if (IS_ERR(skb)) 2138 return -EBUSY; 2139 2140 rc = -ENOMEM; 2141 if (likely(skb)) { 2142 bnxt_deliver_skb(bp, bnapi, skb); 2143 rc = 1; 2144 } 2145 *event |= BNXT_RX_EVENT; 2146 goto next_rx_no_prod_no_len; 2147 } 2148 2149 cons = rxcmp->rx_cmp_opaque; 2150 if (unlikely(cons != rxr->rx_next_cons)) { 2151 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2152 2153 /* 0xffff is forced error, don't print it */ 2154 if (rxr->rx_next_cons != 0xffff) 2155 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2156 cons, rxr->rx_next_cons); 2157 bnxt_sched_reset_rxr(bp, rxr); 2158 if (rc1) 2159 return rc1; 2160 goto next_rx_no_prod_no_len; 2161 } 2162 rx_buf = &rxr->rx_buf_ring[cons]; 2163 data = rx_buf->data; 2164 data_ptr = rx_buf->data_ptr; 2165 prefetch(data_ptr); 2166 2167 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2168 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2169 2170 if (agg_bufs) { 2171 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2172 return -EBUSY; 2173 2174 cp_cons = NEXT_CMP(cp_cons); 2175 *event |= BNXT_AGG_EVENT; 2176 } 2177 *event |= BNXT_RX_EVENT; 2178 2179 rx_buf->data = NULL; 2180 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2181 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2182 2183 bnxt_reuse_rx_data(rxr, cons, data); 2184 if (agg_bufs) 2185 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2186 false); 2187 2188 rc = -EIO; 2189 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2190 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2191 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2192 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2193 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2194 rx_err); 2195 bnxt_sched_reset_rxr(bp, rxr); 2196 } 2197 } 2198 goto next_rx_no_len; 2199 } 2200 2201 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2202 len = flags >> RX_CMP_LEN_SHIFT; 2203 dma_addr = rx_buf->mapping; 2204 2205 if (bnxt_xdp_attached(bp, rxr)) { 2206 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2207 if (agg_bufs) { 2208 u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp, 2209 cp_cons, 2210 agg_bufs, 2211 false); 2212 if (!frag_len) 2213 goto oom_next_rx; 2214 2215 } 2216 xdp_active = true; 2217 } 2218 2219 if (xdp_active) { 2220 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2221 rc = 1; 2222 goto next_rx; 2223 } 2224 if (xdp_buff_has_frags(&xdp)) { 2225 sinfo = xdp_get_shared_info_from_buff(&xdp); 2226 agg_bufs = sinfo->nr_frags; 2227 } else { 2228 agg_bufs = 0; 2229 } 2230 } 2231 2232 if (len <= bp->rx_copybreak) { 2233 if (!xdp_active) 2234 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2235 else 2236 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2237 bnxt_reuse_rx_data(rxr, cons, data); 2238 if (!skb) { 2239 if (agg_bufs) { 2240 if (!xdp_active) 2241 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2242 agg_bufs, false); 2243 else 2244 bnxt_xdp_buff_frags_free(rxr, &xdp); 2245 } 2246 goto oom_next_rx; 2247 } 2248 } else { 2249 u32 payload; 2250 2251 if (rx_buf->data_ptr == data_ptr) 2252 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2253 else 2254 payload = 0; 2255 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2256 payload | len); 2257 if (!skb) 2258 goto oom_next_rx; 2259 } 2260 2261 if (agg_bufs) { 2262 if (!xdp_active) { 2263 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons, 2264 agg_bufs, false); 2265 if (!skb) 2266 goto oom_next_rx; 2267 } else { 2268 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, 2269 rxr->page_pool, &xdp); 2270 if (!skb) { 2271 /* we should be able to free the old skb here */ 2272 bnxt_xdp_buff_frags_free(rxr, &xdp); 2273 goto oom_next_rx; 2274 } 2275 } 2276 } 2277 2278 if (RX_CMP_HASH_VALID(rxcmp)) { 2279 enum pkt_hash_types type; 2280 2281 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2282 type = bnxt_rss_ext_op(bp, rxcmp); 2283 } else { 2284 u32 itypes = RX_CMP_ITYPES(rxcmp); 2285 2286 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2287 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2288 type = PKT_HASH_TYPE_L4; 2289 else 2290 type = PKT_HASH_TYPE_L3; 2291 } 2292 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2293 } 2294 2295 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2296 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2297 skb->protocol = eth_type_trans(skb, dev); 2298 2299 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2300 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2301 if (!skb) 2302 goto next_rx; 2303 } 2304 2305 skb_checksum_none_assert(skb); 2306 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2307 if (dev->features & NETIF_F_RXCSUM) { 2308 skb->ip_summed = CHECKSUM_UNNECESSARY; 2309 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2310 } 2311 } else { 2312 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2313 if (dev->features & NETIF_F_RXCSUM) 2314 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2315 } 2316 } 2317 2318 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2319 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2320 u64 ns, ts; 2321 2322 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2323 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2324 2325 ns = bnxt_timecounter_cyc2time(ptp, ts); 2326 memset(skb_hwtstamps(skb), 0, 2327 sizeof(*skb_hwtstamps(skb))); 2328 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2329 } 2330 } 2331 } 2332 bnxt_deliver_skb(bp, bnapi, skb); 2333 rc = 1; 2334 2335 next_rx: 2336 cpr->rx_packets += 1; 2337 cpr->rx_bytes += len; 2338 2339 next_rx_no_len: 2340 rxr->rx_prod = NEXT_RX(prod); 2341 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2342 2343 next_rx_no_prod_no_len: 2344 *raw_cons = tmp_raw_cons; 2345 2346 return rc; 2347 2348 oom_next_rx: 2349 cpr->sw_stats->rx.rx_oom_discards += 1; 2350 rc = -ENOMEM; 2351 goto next_rx; 2352 } 2353 2354 /* In netpoll mode, if we are using a combined completion ring, we need to 2355 * discard the rx packets and recycle the buffers. 2356 */ 2357 static int bnxt_force_rx_discard(struct bnxt *bp, 2358 struct bnxt_cp_ring_info *cpr, 2359 u32 *raw_cons, u8 *event) 2360 { 2361 u32 tmp_raw_cons = *raw_cons; 2362 struct rx_cmp_ext *rxcmp1; 2363 struct rx_cmp *rxcmp; 2364 u16 cp_cons; 2365 u8 cmp_type; 2366 int rc; 2367 2368 cp_cons = RING_CMP(tmp_raw_cons); 2369 rxcmp = (struct rx_cmp *) 2370 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2371 2372 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2373 cp_cons = RING_CMP(tmp_raw_cons); 2374 rxcmp1 = (struct rx_cmp_ext *) 2375 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2376 2377 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2378 return -EBUSY; 2379 2380 /* The valid test of the entry must be done first before 2381 * reading any further. 2382 */ 2383 dma_rmb(); 2384 cmp_type = RX_CMP_TYPE(rxcmp); 2385 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2386 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2387 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2388 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2389 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2390 struct rx_tpa_end_cmp_ext *tpa_end1; 2391 2392 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2393 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2394 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2395 } 2396 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2397 if (rc && rc != -EBUSY) 2398 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2399 return rc; 2400 } 2401 2402 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2403 { 2404 struct bnxt_fw_health *fw_health = bp->fw_health; 2405 u32 reg = fw_health->regs[reg_idx]; 2406 u32 reg_type, reg_off, val = 0; 2407 2408 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2409 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2410 switch (reg_type) { 2411 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2412 pci_read_config_dword(bp->pdev, reg_off, &val); 2413 break; 2414 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2415 reg_off = fw_health->mapped_regs[reg_idx]; 2416 fallthrough; 2417 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2418 val = readl(bp->bar0 + reg_off); 2419 break; 2420 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2421 val = readl(bp->bar1 + reg_off); 2422 break; 2423 } 2424 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2425 val &= fw_health->fw_reset_inprog_reg_mask; 2426 return val; 2427 } 2428 2429 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2430 { 2431 int i; 2432 2433 for (i = 0; i < bp->rx_nr_rings; i++) { 2434 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2435 struct bnxt_ring_grp_info *grp_info; 2436 2437 grp_info = &bp->grp_info[grp_idx]; 2438 if (grp_info->agg_fw_ring_id == ring_id) 2439 return grp_idx; 2440 } 2441 return INVALID_HW_RING_ID; 2442 } 2443 2444 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2445 { 2446 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2447 2448 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2449 return link_info->force_link_speed2; 2450 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2451 return link_info->force_pam4_link_speed; 2452 return link_info->force_link_speed; 2453 } 2454 2455 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2456 { 2457 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2458 2459 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2460 link_info->req_link_speed = link_info->force_link_speed2; 2461 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2462 switch (link_info->req_link_speed) { 2463 case BNXT_LINK_SPEED_50GB_PAM4: 2464 case BNXT_LINK_SPEED_100GB_PAM4: 2465 case BNXT_LINK_SPEED_200GB_PAM4: 2466 case BNXT_LINK_SPEED_400GB_PAM4: 2467 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2468 break; 2469 case BNXT_LINK_SPEED_100GB_PAM4_112: 2470 case BNXT_LINK_SPEED_200GB_PAM4_112: 2471 case BNXT_LINK_SPEED_400GB_PAM4_112: 2472 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2473 break; 2474 default: 2475 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2476 } 2477 return; 2478 } 2479 link_info->req_link_speed = link_info->force_link_speed; 2480 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2481 if (link_info->force_pam4_link_speed) { 2482 link_info->req_link_speed = link_info->force_pam4_link_speed; 2483 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2484 } 2485 } 2486 2487 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2488 { 2489 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2490 2491 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2492 link_info->advertising = link_info->auto_link_speeds2; 2493 return; 2494 } 2495 link_info->advertising = link_info->auto_link_speeds; 2496 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2497 } 2498 2499 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2500 { 2501 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2502 2503 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2504 if (link_info->req_link_speed != link_info->force_link_speed2) 2505 return true; 2506 return false; 2507 } 2508 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2509 link_info->req_link_speed != link_info->force_link_speed) 2510 return true; 2511 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2512 link_info->req_link_speed != link_info->force_pam4_link_speed) 2513 return true; 2514 return false; 2515 } 2516 2517 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2518 { 2519 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2520 2521 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2522 if (link_info->advertising != link_info->auto_link_speeds2) 2523 return true; 2524 return false; 2525 } 2526 if (link_info->advertising != link_info->auto_link_speeds || 2527 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2528 return true; 2529 return false; 2530 } 2531 2532 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2533 { 2534 u32 flags = bp->ctx->ctx_arr[type].flags; 2535 2536 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2537 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2538 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2539 } 2540 2541 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2542 { 2543 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2544 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2545 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2546 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2547 struct bnxt_bs_trace_info *bs_trace; 2548 int last_pg; 2549 2550 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2551 return; 2552 2553 mem_size = ctxm->max_entries * ctxm->entry_size; 2554 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2555 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2556 2557 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2558 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2559 2560 rmem = &ctx_pg[0].ring_mem; 2561 bs_trace = &bp->bs_trace[trace_type]; 2562 bs_trace->ctx_type = ctxm->type; 2563 bs_trace->trace_type = trace_type; 2564 if (pages > MAX_CTX_PAGES) { 2565 int last_pg_dir = rmem->nr_pages - 1; 2566 2567 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2568 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2569 } else { 2570 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2571 } 2572 bs_trace->magic_byte += magic_byte_offset; 2573 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2574 } 2575 2576 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2577 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2578 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2579 2580 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2581 (((data2) & \ 2582 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2583 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2584 2585 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2586 ((data2) & \ 2587 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2588 2589 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2590 (((data2) & \ 2591 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2592 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2593 2594 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2595 ((data1) & \ 2596 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2597 2598 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2599 (((data1) & \ 2600 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2601 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2602 2603 /* Return true if the workqueue has to be scheduled */ 2604 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2605 { 2606 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2607 2608 switch (err_type) { 2609 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2610 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2611 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2612 break; 2613 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2614 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2615 break; 2616 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2617 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2618 break; 2619 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2620 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2621 char *threshold_type; 2622 bool notify = false; 2623 char *dir_str; 2624 2625 switch (type) { 2626 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2627 threshold_type = "warning"; 2628 break; 2629 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2630 threshold_type = "critical"; 2631 break; 2632 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2633 threshold_type = "fatal"; 2634 break; 2635 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2636 threshold_type = "shutdown"; 2637 break; 2638 default: 2639 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2640 return false; 2641 } 2642 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2643 dir_str = "above"; 2644 notify = true; 2645 } else { 2646 dir_str = "below"; 2647 } 2648 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2649 dir_str, threshold_type); 2650 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2651 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2652 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2653 if (notify) { 2654 bp->thermal_threshold_type = type; 2655 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2656 return true; 2657 } 2658 return false; 2659 } 2660 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2661 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2662 break; 2663 default: 2664 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2665 err_type); 2666 break; 2667 } 2668 return false; 2669 } 2670 2671 #define BNXT_GET_EVENT_PORT(data) \ 2672 ((data) & \ 2673 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2674 2675 #define BNXT_EVENT_RING_TYPE(data2) \ 2676 ((data2) & \ 2677 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2678 2679 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2680 (BNXT_EVENT_RING_TYPE(data2) == \ 2681 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2682 2683 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2684 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2685 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2686 2687 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2688 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2689 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2690 2691 #define BNXT_PHC_BITS 48 2692 2693 static int bnxt_async_event_process(struct bnxt *bp, 2694 struct hwrm_async_event_cmpl *cmpl) 2695 { 2696 u16 event_id = le16_to_cpu(cmpl->event_id); 2697 u32 data1 = le32_to_cpu(cmpl->event_data1); 2698 u32 data2 = le32_to_cpu(cmpl->event_data2); 2699 2700 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2701 event_id, data1, data2); 2702 2703 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2704 switch (event_id) { 2705 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2706 struct bnxt_link_info *link_info = &bp->link_info; 2707 2708 if (BNXT_VF(bp)) 2709 goto async_event_process_exit; 2710 2711 /* print unsupported speed warning in forced speed mode only */ 2712 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2713 (data1 & 0x20000)) { 2714 u16 fw_speed = bnxt_get_force_speed(link_info); 2715 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2716 2717 if (speed != SPEED_UNKNOWN) 2718 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2719 speed); 2720 } 2721 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2722 } 2723 fallthrough; 2724 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2725 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2726 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2727 fallthrough; 2728 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2729 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2730 break; 2731 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2732 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2733 break; 2734 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2735 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2736 2737 if (BNXT_VF(bp)) 2738 break; 2739 2740 if (bp->pf.port_id != port_id) 2741 break; 2742 2743 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2744 break; 2745 } 2746 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2747 if (BNXT_PF(bp)) 2748 goto async_event_process_exit; 2749 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2750 break; 2751 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2752 char *type_str = "Solicited"; 2753 2754 if (!bp->fw_health) 2755 goto async_event_process_exit; 2756 2757 bp->fw_reset_timestamp = jiffies; 2758 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2759 if (!bp->fw_reset_min_dsecs) 2760 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2761 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2762 if (!bp->fw_reset_max_dsecs) 2763 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2764 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2765 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2766 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2767 type_str = "Fatal"; 2768 bp->fw_health->fatalities++; 2769 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2770 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2771 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2772 type_str = "Non-fatal"; 2773 bp->fw_health->survivals++; 2774 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2775 } 2776 netif_warn(bp, hw, bp->dev, 2777 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2778 type_str, data1, data2, 2779 bp->fw_reset_min_dsecs * 100, 2780 bp->fw_reset_max_dsecs * 100); 2781 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2782 break; 2783 } 2784 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2785 struct bnxt_fw_health *fw_health = bp->fw_health; 2786 char *status_desc = "healthy"; 2787 u32 status; 2788 2789 if (!fw_health) 2790 goto async_event_process_exit; 2791 2792 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2793 fw_health->enabled = false; 2794 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2795 break; 2796 } 2797 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2798 fw_health->tmr_multiplier = 2799 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2800 bp->current_interval * 10); 2801 fw_health->tmr_counter = fw_health->tmr_multiplier; 2802 if (!fw_health->enabled) 2803 fw_health->last_fw_heartbeat = 2804 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2805 fw_health->last_fw_reset_cnt = 2806 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2807 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2808 if (status != BNXT_FW_STATUS_HEALTHY) 2809 status_desc = "unhealthy"; 2810 netif_info(bp, drv, bp->dev, 2811 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2812 fw_health->primary ? "primary" : "backup", status, 2813 status_desc, fw_health->last_fw_reset_cnt); 2814 if (!fw_health->enabled) { 2815 /* Make sure tmr_counter is set and visible to 2816 * bnxt_health_check() before setting enabled to true. 2817 */ 2818 smp_wmb(); 2819 fw_health->enabled = true; 2820 } 2821 goto async_event_process_exit; 2822 } 2823 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2824 netif_notice(bp, hw, bp->dev, 2825 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2826 data1, data2); 2827 goto async_event_process_exit; 2828 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2829 struct bnxt_rx_ring_info *rxr; 2830 u16 grp_idx; 2831 2832 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2833 goto async_event_process_exit; 2834 2835 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2836 BNXT_EVENT_RING_TYPE(data2), data1); 2837 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2838 goto async_event_process_exit; 2839 2840 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2841 if (grp_idx == INVALID_HW_RING_ID) { 2842 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2843 data1); 2844 goto async_event_process_exit; 2845 } 2846 rxr = bp->bnapi[grp_idx]->rx_ring; 2847 bnxt_sched_reset_rxr(bp, rxr); 2848 goto async_event_process_exit; 2849 } 2850 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2851 struct bnxt_fw_health *fw_health = bp->fw_health; 2852 2853 netif_notice(bp, hw, bp->dev, 2854 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2855 data1, data2); 2856 if (fw_health) { 2857 fw_health->echo_req_data1 = data1; 2858 fw_health->echo_req_data2 = data2; 2859 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2860 break; 2861 } 2862 goto async_event_process_exit; 2863 } 2864 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2865 bnxt_ptp_pps_event(bp, data1, data2); 2866 goto async_event_process_exit; 2867 } 2868 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2869 if (bnxt_event_error_report(bp, data1, data2)) 2870 break; 2871 goto async_event_process_exit; 2872 } 2873 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2874 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2875 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2876 if (BNXT_PTP_USE_RTC(bp)) { 2877 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2878 unsigned long flags; 2879 u64 ns; 2880 2881 if (!ptp) 2882 goto async_event_process_exit; 2883 2884 bnxt_ptp_update_current_time(bp); 2885 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2886 BNXT_PHC_BITS) | ptp->current_time); 2887 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2888 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2889 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2890 } 2891 break; 2892 } 2893 goto async_event_process_exit; 2894 } 2895 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2896 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2897 2898 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2899 goto async_event_process_exit; 2900 } 2901 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2902 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2903 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2904 2905 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2906 goto async_event_process_exit; 2907 } 2908 default: 2909 goto async_event_process_exit; 2910 } 2911 __bnxt_queue_sp_work(bp); 2912 async_event_process_exit: 2913 bnxt_ulp_async_events(bp, cmpl); 2914 return 0; 2915 } 2916 2917 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2918 { 2919 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2920 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2921 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2922 (struct hwrm_fwd_req_cmpl *)txcmp; 2923 2924 switch (cmpl_type) { 2925 case CMPL_BASE_TYPE_HWRM_DONE: 2926 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2927 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2928 break; 2929 2930 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2931 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2932 2933 if ((vf_id < bp->pf.first_vf_id) || 2934 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2935 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2936 vf_id); 2937 return -EINVAL; 2938 } 2939 2940 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2941 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2942 break; 2943 2944 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2945 bnxt_async_event_process(bp, 2946 (struct hwrm_async_event_cmpl *)txcmp); 2947 break; 2948 2949 default: 2950 break; 2951 } 2952 2953 return 0; 2954 } 2955 2956 static bool bnxt_vnic_is_active(struct bnxt *bp) 2957 { 2958 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 2959 2960 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 2961 } 2962 2963 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2964 { 2965 struct bnxt_napi *bnapi = dev_instance; 2966 struct bnxt *bp = bnapi->bp; 2967 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2968 u32 cons = RING_CMP(cpr->cp_raw_cons); 2969 2970 cpr->event_ctr++; 2971 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2972 napi_schedule(&bnapi->napi); 2973 return IRQ_HANDLED; 2974 } 2975 2976 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2977 { 2978 u32 raw_cons = cpr->cp_raw_cons; 2979 u16 cons = RING_CMP(raw_cons); 2980 struct tx_cmp *txcmp; 2981 2982 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2983 2984 return TX_CMP_VALID(txcmp, raw_cons); 2985 } 2986 2987 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2988 int budget) 2989 { 2990 struct bnxt_napi *bnapi = cpr->bnapi; 2991 u32 raw_cons = cpr->cp_raw_cons; 2992 bool flush_xdp = false; 2993 u32 cons; 2994 int rx_pkts = 0; 2995 u8 event = 0; 2996 struct tx_cmp *txcmp; 2997 2998 cpr->has_more_work = 0; 2999 cpr->had_work_done = 1; 3000 while (1) { 3001 u8 cmp_type; 3002 int rc; 3003 3004 cons = RING_CMP(raw_cons); 3005 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3006 3007 if (!TX_CMP_VALID(txcmp, raw_cons)) 3008 break; 3009 3010 /* The valid test of the entry must be done first before 3011 * reading any further. 3012 */ 3013 dma_rmb(); 3014 cmp_type = TX_CMP_TYPE(txcmp); 3015 if (cmp_type == CMP_TYPE_TX_L2_CMP || 3016 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 3017 u32 opaque = txcmp->tx_cmp_opaque; 3018 struct bnxt_tx_ring_info *txr; 3019 u16 tx_freed; 3020 3021 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 3022 event |= BNXT_TX_CMP_EVENT; 3023 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 3024 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 3025 else 3026 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 3027 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 3028 bp->tx_ring_mask; 3029 /* return full budget so NAPI will complete. */ 3030 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 3031 rx_pkts = budget; 3032 raw_cons = NEXT_RAW_CMP(raw_cons); 3033 if (budget) 3034 cpr->has_more_work = 1; 3035 break; 3036 } 3037 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 3038 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 3039 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 3040 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 3041 if (likely(budget)) 3042 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3043 else 3044 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 3045 &event); 3046 if (event & BNXT_REDIRECT_EVENT) 3047 flush_xdp = true; 3048 if (likely(rc >= 0)) 3049 rx_pkts += rc; 3050 /* Increment rx_pkts when rc is -ENOMEM to count towards 3051 * the NAPI budget. Otherwise, we may potentially loop 3052 * here forever if we consistently cannot allocate 3053 * buffers. 3054 */ 3055 else if (rc == -ENOMEM && budget) 3056 rx_pkts++; 3057 else if (rc == -EBUSY) /* partial completion */ 3058 break; 3059 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3060 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3061 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3062 bnxt_hwrm_handler(bp, txcmp); 3063 } 3064 raw_cons = NEXT_RAW_CMP(raw_cons); 3065 3066 if (rx_pkts && rx_pkts == budget) { 3067 cpr->has_more_work = 1; 3068 break; 3069 } 3070 } 3071 3072 if (flush_xdp) { 3073 xdp_do_flush(); 3074 event &= ~BNXT_REDIRECT_EVENT; 3075 } 3076 3077 if (event & BNXT_TX_EVENT) { 3078 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3079 u16 prod = txr->tx_prod; 3080 3081 /* Sync BD data before updating doorbell */ 3082 wmb(); 3083 3084 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3085 event &= ~BNXT_TX_EVENT; 3086 } 3087 3088 cpr->cp_raw_cons = raw_cons; 3089 bnapi->events |= event; 3090 return rx_pkts; 3091 } 3092 3093 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3094 int budget) 3095 { 3096 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3097 bnapi->tx_int(bp, bnapi, budget); 3098 3099 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3100 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3101 3102 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3103 bnapi->events &= ~BNXT_RX_EVENT; 3104 } 3105 if (bnapi->events & BNXT_AGG_EVENT) { 3106 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3107 3108 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3109 bnapi->events &= ~BNXT_AGG_EVENT; 3110 } 3111 } 3112 3113 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3114 int budget) 3115 { 3116 struct bnxt_napi *bnapi = cpr->bnapi; 3117 int rx_pkts; 3118 3119 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3120 3121 /* ACK completion ring before freeing tx ring and producing new 3122 * buffers in rx/agg rings to prevent overflowing the completion 3123 * ring. 3124 */ 3125 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3126 3127 __bnxt_poll_work_done(bp, bnapi, budget); 3128 return rx_pkts; 3129 } 3130 3131 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3132 { 3133 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3134 struct bnxt *bp = bnapi->bp; 3135 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3136 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3137 struct tx_cmp *txcmp; 3138 struct rx_cmp_ext *rxcmp1; 3139 u32 cp_cons, tmp_raw_cons; 3140 u32 raw_cons = cpr->cp_raw_cons; 3141 bool flush_xdp = false; 3142 u32 rx_pkts = 0; 3143 u8 event = 0; 3144 3145 while (1) { 3146 int rc; 3147 3148 cp_cons = RING_CMP(raw_cons); 3149 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3150 3151 if (!TX_CMP_VALID(txcmp, raw_cons)) 3152 break; 3153 3154 /* The valid test of the entry must be done first before 3155 * reading any further. 3156 */ 3157 dma_rmb(); 3158 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3159 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3160 cp_cons = RING_CMP(tmp_raw_cons); 3161 rxcmp1 = (struct rx_cmp_ext *) 3162 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3163 3164 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3165 break; 3166 3167 /* force an error to recycle the buffer */ 3168 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3169 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3170 3171 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3172 if (likely(rc == -EIO) && budget) 3173 rx_pkts++; 3174 else if (rc == -EBUSY) /* partial completion */ 3175 break; 3176 if (event & BNXT_REDIRECT_EVENT) 3177 flush_xdp = true; 3178 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3179 CMPL_BASE_TYPE_HWRM_DONE)) { 3180 bnxt_hwrm_handler(bp, txcmp); 3181 } else { 3182 netdev_err(bp->dev, 3183 "Invalid completion received on special ring\n"); 3184 } 3185 raw_cons = NEXT_RAW_CMP(raw_cons); 3186 3187 if (rx_pkts == budget) 3188 break; 3189 } 3190 3191 cpr->cp_raw_cons = raw_cons; 3192 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3193 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3194 3195 if (event & BNXT_AGG_EVENT) 3196 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3197 if (flush_xdp) 3198 xdp_do_flush(); 3199 3200 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3201 napi_complete_done(napi, rx_pkts); 3202 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3203 } 3204 return rx_pkts; 3205 } 3206 3207 static int bnxt_poll(struct napi_struct *napi, int budget) 3208 { 3209 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3210 struct bnxt *bp = bnapi->bp; 3211 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3212 int work_done = 0; 3213 3214 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3215 napi_complete(napi); 3216 return 0; 3217 } 3218 while (1) { 3219 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3220 3221 if (work_done >= budget) { 3222 if (!budget) 3223 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3224 break; 3225 } 3226 3227 if (!bnxt_has_work(bp, cpr)) { 3228 if (napi_complete_done(napi, work_done)) 3229 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3230 break; 3231 } 3232 } 3233 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3234 struct dim_sample dim_sample = {}; 3235 3236 dim_update_sample(cpr->event_ctr, 3237 cpr->rx_packets, 3238 cpr->rx_bytes, 3239 &dim_sample); 3240 net_dim(&cpr->dim, &dim_sample); 3241 } 3242 return work_done; 3243 } 3244 3245 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3246 { 3247 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3248 int i, work_done = 0; 3249 3250 for (i = 0; i < cpr->cp_ring_count; i++) { 3251 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3252 3253 if (cpr2->had_nqe_notify) { 3254 work_done += __bnxt_poll_work(bp, cpr2, 3255 budget - work_done); 3256 cpr->has_more_work |= cpr2->has_more_work; 3257 } 3258 } 3259 return work_done; 3260 } 3261 3262 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3263 u64 dbr_type, int budget) 3264 { 3265 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3266 int i; 3267 3268 for (i = 0; i < cpr->cp_ring_count; i++) { 3269 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3270 struct bnxt_db_info *db; 3271 3272 if (cpr2->had_work_done) { 3273 u32 tgl = 0; 3274 3275 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3276 cpr2->had_nqe_notify = 0; 3277 tgl = cpr2->toggle; 3278 } 3279 db = &cpr2->cp_db; 3280 bnxt_writeq(bp, 3281 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3282 DB_RING_IDX(db, cpr2->cp_raw_cons), 3283 db->doorbell); 3284 cpr2->had_work_done = 0; 3285 } 3286 } 3287 __bnxt_poll_work_done(bp, bnapi, budget); 3288 } 3289 3290 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3291 { 3292 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3293 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3294 struct bnxt_cp_ring_info *cpr_rx; 3295 u32 raw_cons = cpr->cp_raw_cons; 3296 struct bnxt *bp = bnapi->bp; 3297 struct nqe_cn *nqcmp; 3298 int work_done = 0; 3299 u32 cons; 3300 3301 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3302 napi_complete(napi); 3303 return 0; 3304 } 3305 if (cpr->has_more_work) { 3306 cpr->has_more_work = 0; 3307 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3308 } 3309 while (1) { 3310 u16 type; 3311 3312 cons = RING_CMP(raw_cons); 3313 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3314 3315 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3316 if (cpr->has_more_work) 3317 break; 3318 3319 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3320 budget); 3321 cpr->cp_raw_cons = raw_cons; 3322 if (napi_complete_done(napi, work_done)) 3323 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3324 cpr->cp_raw_cons); 3325 goto poll_done; 3326 } 3327 3328 /* The valid test of the entry must be done first before 3329 * reading any further. 3330 */ 3331 dma_rmb(); 3332 3333 type = le16_to_cpu(nqcmp->type); 3334 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3335 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3336 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3337 struct bnxt_cp_ring_info *cpr2; 3338 3339 /* No more budget for RX work */ 3340 if (budget && work_done >= budget && 3341 cq_type == BNXT_NQ_HDL_TYPE_RX) 3342 break; 3343 3344 idx = BNXT_NQ_HDL_IDX(idx); 3345 cpr2 = &cpr->cp_ring_arr[idx]; 3346 cpr2->had_nqe_notify = 1; 3347 cpr2->toggle = NQE_CN_TOGGLE(type); 3348 work_done += __bnxt_poll_work(bp, cpr2, 3349 budget - work_done); 3350 cpr->has_more_work |= cpr2->has_more_work; 3351 } else { 3352 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3353 } 3354 raw_cons = NEXT_RAW_CMP(raw_cons); 3355 } 3356 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3357 if (raw_cons != cpr->cp_raw_cons) { 3358 cpr->cp_raw_cons = raw_cons; 3359 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3360 } 3361 poll_done: 3362 cpr_rx = &cpr->cp_ring_arr[0]; 3363 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3364 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3365 struct dim_sample dim_sample = {}; 3366 3367 dim_update_sample(cpr->event_ctr, 3368 cpr_rx->rx_packets, 3369 cpr_rx->rx_bytes, 3370 &dim_sample); 3371 net_dim(&cpr->dim, &dim_sample); 3372 } 3373 return work_done; 3374 } 3375 3376 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp, 3377 struct bnxt_tx_ring_info *txr, int idx) 3378 { 3379 int i, max_idx; 3380 struct pci_dev *pdev = bp->pdev; 3381 3382 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3383 3384 for (i = 0; i < max_idx;) { 3385 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i]; 3386 struct sk_buff *skb; 3387 int j, last; 3388 3389 if (idx < bp->tx_nr_rings_xdp && 3390 tx_buf->action == XDP_REDIRECT) { 3391 dma_unmap_single(&pdev->dev, 3392 dma_unmap_addr(tx_buf, mapping), 3393 dma_unmap_len(tx_buf, len), 3394 DMA_TO_DEVICE); 3395 xdp_return_frame(tx_buf->xdpf); 3396 tx_buf->action = 0; 3397 tx_buf->xdpf = NULL; 3398 i++; 3399 continue; 3400 } 3401 3402 skb = tx_buf->skb; 3403 if (!skb) { 3404 i++; 3405 continue; 3406 } 3407 3408 tx_buf->skb = NULL; 3409 3410 if (tx_buf->is_push) { 3411 dev_kfree_skb(skb); 3412 i += 2; 3413 continue; 3414 } 3415 3416 dma_unmap_single(&pdev->dev, 3417 dma_unmap_addr(tx_buf, mapping), 3418 skb_headlen(skb), 3419 DMA_TO_DEVICE); 3420 3421 last = tx_buf->nr_frags; 3422 i += 2; 3423 for (j = 0; j < last; j++, i++) { 3424 int ring_idx = i & bp->tx_ring_mask; 3425 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 3426 3427 tx_buf = &txr->tx_buf_ring[ring_idx]; 3428 dma_unmap_page(&pdev->dev, 3429 dma_unmap_addr(tx_buf, mapping), 3430 skb_frag_size(frag), DMA_TO_DEVICE); 3431 } 3432 dev_kfree_skb(skb); 3433 } 3434 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx)); 3435 } 3436 3437 static void bnxt_free_tx_skbs(struct bnxt *bp) 3438 { 3439 int i; 3440 3441 if (!bp->tx_ring) 3442 return; 3443 3444 for (i = 0; i < bp->tx_nr_rings; i++) { 3445 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3446 3447 if (!txr->tx_buf_ring) 3448 continue; 3449 3450 bnxt_free_one_tx_ring_skbs(bp, txr, i); 3451 } 3452 3453 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 3454 bnxt_ptp_free_txts_skbs(bp->ptp_cfg); 3455 } 3456 3457 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3458 { 3459 int i, max_idx; 3460 3461 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3462 3463 for (i = 0; i < max_idx; i++) { 3464 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3465 void *data = rx_buf->data; 3466 3467 if (!data) 3468 continue; 3469 3470 rx_buf->data = NULL; 3471 if (BNXT_RX_PAGE_MODE(bp)) 3472 page_pool_recycle_direct(rxr->page_pool, data); 3473 else 3474 page_pool_free_va(rxr->head_pool, data, true); 3475 } 3476 } 3477 3478 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3479 { 3480 int i, max_idx; 3481 3482 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3483 3484 for (i = 0; i < max_idx; i++) { 3485 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3486 netmem_ref netmem = rx_agg_buf->netmem; 3487 3488 if (!netmem) 3489 continue; 3490 3491 rx_agg_buf->netmem = 0; 3492 __clear_bit(i, rxr->rx_agg_bmap); 3493 3494 page_pool_recycle_direct_netmem(rxr->page_pool, netmem); 3495 } 3496 } 3497 3498 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3499 struct bnxt_rx_ring_info *rxr) 3500 { 3501 int i; 3502 3503 for (i = 0; i < bp->max_tpa; i++) { 3504 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3505 u8 *data = tpa_info->data; 3506 3507 if (!data) 3508 continue; 3509 3510 tpa_info->data = NULL; 3511 page_pool_free_va(rxr->head_pool, data, false); 3512 } 3513 } 3514 3515 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3516 struct bnxt_rx_ring_info *rxr) 3517 { 3518 struct bnxt_tpa_idx_map *map; 3519 3520 if (!rxr->rx_tpa) 3521 goto skip_rx_tpa_free; 3522 3523 bnxt_free_one_tpa_info_data(bp, rxr); 3524 3525 skip_rx_tpa_free: 3526 if (!rxr->rx_buf_ring) 3527 goto skip_rx_buf_free; 3528 3529 bnxt_free_one_rx_ring(bp, rxr); 3530 3531 skip_rx_buf_free: 3532 if (!rxr->rx_agg_ring) 3533 goto skip_rx_agg_free; 3534 3535 bnxt_free_one_rx_agg_ring(bp, rxr); 3536 3537 skip_rx_agg_free: 3538 map = rxr->rx_tpa_idx_map; 3539 if (map) 3540 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3541 } 3542 3543 static void bnxt_free_rx_skbs(struct bnxt *bp) 3544 { 3545 int i; 3546 3547 if (!bp->rx_ring) 3548 return; 3549 3550 for (i = 0; i < bp->rx_nr_rings; i++) 3551 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3552 } 3553 3554 static void bnxt_free_skbs(struct bnxt *bp) 3555 { 3556 bnxt_free_tx_skbs(bp); 3557 bnxt_free_rx_skbs(bp); 3558 } 3559 3560 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3561 { 3562 u8 init_val = ctxm->init_value; 3563 u16 offset = ctxm->init_offset; 3564 u8 *p2 = p; 3565 int i; 3566 3567 if (!init_val) 3568 return; 3569 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3570 memset(p, init_val, len); 3571 return; 3572 } 3573 for (i = 0; i < len; i += ctxm->entry_size) 3574 *(p2 + i + offset) = init_val; 3575 } 3576 3577 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3578 void *buf, size_t offset, size_t head, 3579 size_t tail) 3580 { 3581 int i, head_page, start_idx, source_offset; 3582 size_t len, rem_len, total_len, max_bytes; 3583 3584 head_page = head / rmem->page_size; 3585 source_offset = head % rmem->page_size; 3586 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3587 if (!total_len) 3588 total_len = MAX_CTX_BYTES; 3589 start_idx = head_page % MAX_CTX_PAGES; 3590 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3591 source_offset; 3592 total_len = min(total_len, max_bytes); 3593 rem_len = total_len; 3594 3595 for (i = start_idx; rem_len; i++, source_offset = 0) { 3596 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3597 if (buf) 3598 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3599 len); 3600 offset += len; 3601 rem_len -= len; 3602 } 3603 return total_len; 3604 } 3605 3606 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3607 { 3608 struct pci_dev *pdev = bp->pdev; 3609 int i; 3610 3611 if (!rmem->pg_arr) 3612 goto skip_pages; 3613 3614 for (i = 0; i < rmem->nr_pages; i++) { 3615 if (!rmem->pg_arr[i]) 3616 continue; 3617 3618 dma_free_coherent(&pdev->dev, rmem->page_size, 3619 rmem->pg_arr[i], rmem->dma_arr[i]); 3620 3621 rmem->pg_arr[i] = NULL; 3622 } 3623 skip_pages: 3624 if (rmem->pg_tbl) { 3625 size_t pg_tbl_size = rmem->nr_pages * 8; 3626 3627 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3628 pg_tbl_size = rmem->page_size; 3629 dma_free_coherent(&pdev->dev, pg_tbl_size, 3630 rmem->pg_tbl, rmem->pg_tbl_map); 3631 rmem->pg_tbl = NULL; 3632 } 3633 if (rmem->vmem_size && *rmem->vmem) { 3634 vfree(*rmem->vmem); 3635 *rmem->vmem = NULL; 3636 } 3637 } 3638 3639 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3640 { 3641 struct pci_dev *pdev = bp->pdev; 3642 u64 valid_bit = 0; 3643 int i; 3644 3645 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3646 valid_bit = PTU_PTE_VALID; 3647 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3648 size_t pg_tbl_size = rmem->nr_pages * 8; 3649 3650 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3651 pg_tbl_size = rmem->page_size; 3652 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3653 &rmem->pg_tbl_map, 3654 GFP_KERNEL); 3655 if (!rmem->pg_tbl) 3656 return -ENOMEM; 3657 } 3658 3659 for (i = 0; i < rmem->nr_pages; i++) { 3660 u64 extra_bits = valid_bit; 3661 3662 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3663 rmem->page_size, 3664 &rmem->dma_arr[i], 3665 GFP_KERNEL); 3666 if (!rmem->pg_arr[i]) 3667 return -ENOMEM; 3668 3669 if (rmem->ctx_mem) 3670 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3671 rmem->page_size); 3672 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3673 if (i == rmem->nr_pages - 2 && 3674 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3675 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3676 else if (i == rmem->nr_pages - 1 && 3677 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3678 extra_bits |= PTU_PTE_LAST; 3679 rmem->pg_tbl[i] = 3680 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3681 } 3682 } 3683 3684 if (rmem->vmem_size) { 3685 *rmem->vmem = vzalloc(rmem->vmem_size); 3686 if (!(*rmem->vmem)) 3687 return -ENOMEM; 3688 } 3689 return 0; 3690 } 3691 3692 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3693 struct bnxt_rx_ring_info *rxr) 3694 { 3695 int i; 3696 3697 kfree(rxr->rx_tpa_idx_map); 3698 rxr->rx_tpa_idx_map = NULL; 3699 if (rxr->rx_tpa) { 3700 for (i = 0; i < bp->max_tpa; i++) { 3701 kfree(rxr->rx_tpa[i].agg_arr); 3702 rxr->rx_tpa[i].agg_arr = NULL; 3703 } 3704 } 3705 kfree(rxr->rx_tpa); 3706 rxr->rx_tpa = NULL; 3707 } 3708 3709 static void bnxt_free_tpa_info(struct bnxt *bp) 3710 { 3711 int i; 3712 3713 for (i = 0; i < bp->rx_nr_rings; i++) { 3714 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3715 3716 bnxt_free_one_tpa_info(bp, rxr); 3717 } 3718 } 3719 3720 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3721 struct bnxt_rx_ring_info *rxr) 3722 { 3723 struct rx_agg_cmp *agg; 3724 int i; 3725 3726 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3727 GFP_KERNEL); 3728 if (!rxr->rx_tpa) 3729 return -ENOMEM; 3730 3731 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3732 return 0; 3733 for (i = 0; i < bp->max_tpa; i++) { 3734 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3735 if (!agg) 3736 return -ENOMEM; 3737 rxr->rx_tpa[i].agg_arr = agg; 3738 } 3739 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3740 GFP_KERNEL); 3741 if (!rxr->rx_tpa_idx_map) 3742 return -ENOMEM; 3743 3744 return 0; 3745 } 3746 3747 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3748 { 3749 int i, rc; 3750 3751 bp->max_tpa = MAX_TPA; 3752 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3753 if (!bp->max_tpa_v2) 3754 return 0; 3755 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3756 } 3757 3758 for (i = 0; i < bp->rx_nr_rings; i++) { 3759 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3760 3761 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3762 if (rc) 3763 return rc; 3764 } 3765 return 0; 3766 } 3767 3768 static void bnxt_free_rx_rings(struct bnxt *bp) 3769 { 3770 int i; 3771 3772 if (!bp->rx_ring) 3773 return; 3774 3775 bnxt_free_tpa_info(bp); 3776 for (i = 0; i < bp->rx_nr_rings; i++) { 3777 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3778 struct bnxt_ring_struct *ring; 3779 3780 if (rxr->xdp_prog) 3781 bpf_prog_put(rxr->xdp_prog); 3782 3783 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3784 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3785 3786 page_pool_destroy(rxr->page_pool); 3787 if (bnxt_separate_head_pool(rxr)) 3788 page_pool_destroy(rxr->head_pool); 3789 rxr->page_pool = rxr->head_pool = NULL; 3790 3791 kfree(rxr->rx_agg_bmap); 3792 rxr->rx_agg_bmap = NULL; 3793 3794 ring = &rxr->rx_ring_struct; 3795 bnxt_free_ring(bp, &ring->ring_mem); 3796 3797 ring = &rxr->rx_agg_ring_struct; 3798 bnxt_free_ring(bp, &ring->ring_mem); 3799 } 3800 } 3801 3802 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3803 struct bnxt_rx_ring_info *rxr, 3804 int numa_node) 3805 { 3806 struct page_pool_params pp = { 0 }; 3807 struct page_pool *pool; 3808 3809 pp.pool_size = bp->rx_agg_ring_size; 3810 if (BNXT_RX_PAGE_MODE(bp)) 3811 pp.pool_size += bp->rx_ring_size; 3812 pp.nid = numa_node; 3813 pp.napi = &rxr->bnapi->napi; 3814 pp.netdev = bp->dev; 3815 pp.dev = &bp->pdev->dev; 3816 pp.dma_dir = bp->rx_dir; 3817 pp.max_len = PAGE_SIZE; 3818 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | 3819 PP_FLAG_ALLOW_UNREADABLE_NETMEM; 3820 pp.queue_idx = rxr->bnapi->index; 3821 3822 pool = page_pool_create(&pp); 3823 if (IS_ERR(pool)) 3824 return PTR_ERR(pool); 3825 rxr->page_pool = pool; 3826 3827 rxr->need_head_pool = page_pool_is_unreadable(pool); 3828 if (bnxt_separate_head_pool(rxr)) { 3829 pp.pool_size = max(bp->rx_ring_size, 1024); 3830 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3831 pool = page_pool_create(&pp); 3832 if (IS_ERR(pool)) 3833 goto err_destroy_pp; 3834 } 3835 rxr->head_pool = pool; 3836 3837 return 0; 3838 3839 err_destroy_pp: 3840 page_pool_destroy(rxr->page_pool); 3841 rxr->page_pool = NULL; 3842 return PTR_ERR(pool); 3843 } 3844 3845 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3846 { 3847 u16 mem_size; 3848 3849 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3850 mem_size = rxr->rx_agg_bmap_size / 8; 3851 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3852 if (!rxr->rx_agg_bmap) 3853 return -ENOMEM; 3854 3855 return 0; 3856 } 3857 3858 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3859 { 3860 int numa_node = dev_to_node(&bp->pdev->dev); 3861 int i, rc = 0, agg_rings = 0, cpu; 3862 3863 if (!bp->rx_ring) 3864 return -ENOMEM; 3865 3866 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3867 agg_rings = 1; 3868 3869 for (i = 0; i < bp->rx_nr_rings; i++) { 3870 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3871 struct bnxt_ring_struct *ring; 3872 int cpu_node; 3873 3874 ring = &rxr->rx_ring_struct; 3875 3876 cpu = cpumask_local_spread(i, numa_node); 3877 cpu_node = cpu_to_node(cpu); 3878 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3879 i, cpu_node); 3880 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3881 if (rc) 3882 return rc; 3883 3884 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3885 if (rc < 0) 3886 return rc; 3887 3888 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3889 MEM_TYPE_PAGE_POOL, 3890 rxr->page_pool); 3891 if (rc) { 3892 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3893 return rc; 3894 } 3895 3896 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3897 if (rc) 3898 return rc; 3899 3900 ring->grp_idx = i; 3901 if (agg_rings) { 3902 ring = &rxr->rx_agg_ring_struct; 3903 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3904 if (rc) 3905 return rc; 3906 3907 ring->grp_idx = i; 3908 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 3909 if (rc) 3910 return rc; 3911 } 3912 } 3913 if (bp->flags & BNXT_FLAG_TPA) 3914 rc = bnxt_alloc_tpa_info(bp); 3915 return rc; 3916 } 3917 3918 static void bnxt_free_tx_rings(struct bnxt *bp) 3919 { 3920 int i; 3921 struct pci_dev *pdev = bp->pdev; 3922 3923 if (!bp->tx_ring) 3924 return; 3925 3926 for (i = 0; i < bp->tx_nr_rings; i++) { 3927 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3928 struct bnxt_ring_struct *ring; 3929 3930 if (txr->tx_push) { 3931 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3932 txr->tx_push, txr->tx_push_mapping); 3933 txr->tx_push = NULL; 3934 } 3935 3936 ring = &txr->tx_ring_struct; 3937 3938 bnxt_free_ring(bp, &ring->ring_mem); 3939 } 3940 } 3941 3942 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3943 ((tc) * (bp)->tx_nr_rings_per_tc) 3944 3945 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3946 ((tx) % (bp)->tx_nr_rings_per_tc) 3947 3948 #define BNXT_RING_TO_TC(bp, tx) \ 3949 ((tx) / (bp)->tx_nr_rings_per_tc) 3950 3951 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3952 { 3953 int i, j, rc; 3954 struct pci_dev *pdev = bp->pdev; 3955 3956 bp->tx_push_size = 0; 3957 if (bp->tx_push_thresh) { 3958 int push_size; 3959 3960 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3961 bp->tx_push_thresh); 3962 3963 if (push_size > 256) { 3964 push_size = 0; 3965 bp->tx_push_thresh = 0; 3966 } 3967 3968 bp->tx_push_size = push_size; 3969 } 3970 3971 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3972 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3973 struct bnxt_ring_struct *ring; 3974 u8 qidx; 3975 3976 ring = &txr->tx_ring_struct; 3977 3978 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3979 if (rc) 3980 return rc; 3981 3982 ring->grp_idx = txr->bnapi->index; 3983 if (bp->tx_push_size) { 3984 dma_addr_t mapping; 3985 3986 /* One pre-allocated DMA buffer to backup 3987 * TX push operation 3988 */ 3989 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3990 bp->tx_push_size, 3991 &txr->tx_push_mapping, 3992 GFP_KERNEL); 3993 3994 if (!txr->tx_push) 3995 return -ENOMEM; 3996 3997 mapping = txr->tx_push_mapping + 3998 sizeof(struct tx_push_bd); 3999 txr->data_mapping = cpu_to_le64(mapping); 4000 } 4001 qidx = bp->tc_to_qidx[j]; 4002 ring->queue_id = bp->q_info[qidx].queue_id; 4003 spin_lock_init(&txr->xdp_tx_lock); 4004 if (i < bp->tx_nr_rings_xdp) 4005 continue; 4006 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 4007 j++; 4008 } 4009 return 0; 4010 } 4011 4012 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 4013 { 4014 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4015 4016 kfree(cpr->cp_desc_ring); 4017 cpr->cp_desc_ring = NULL; 4018 ring->ring_mem.pg_arr = NULL; 4019 kfree(cpr->cp_desc_mapping); 4020 cpr->cp_desc_mapping = NULL; 4021 ring->ring_mem.dma_arr = NULL; 4022 } 4023 4024 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 4025 { 4026 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 4027 if (!cpr->cp_desc_ring) 4028 return -ENOMEM; 4029 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 4030 GFP_KERNEL); 4031 if (!cpr->cp_desc_mapping) 4032 return -ENOMEM; 4033 return 0; 4034 } 4035 4036 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 4037 { 4038 int i; 4039 4040 if (!bp->bnapi) 4041 return; 4042 for (i = 0; i < bp->cp_nr_rings; i++) { 4043 struct bnxt_napi *bnapi = bp->bnapi[i]; 4044 4045 if (!bnapi) 4046 continue; 4047 bnxt_free_cp_arrays(&bnapi->cp_ring); 4048 } 4049 } 4050 4051 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 4052 { 4053 int i, n = bp->cp_nr_pages; 4054 4055 for (i = 0; i < bp->cp_nr_rings; i++) { 4056 struct bnxt_napi *bnapi = bp->bnapi[i]; 4057 int rc; 4058 4059 if (!bnapi) 4060 continue; 4061 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 4062 if (rc) 4063 return rc; 4064 } 4065 return 0; 4066 } 4067 4068 static void bnxt_free_cp_rings(struct bnxt *bp) 4069 { 4070 int i; 4071 4072 if (!bp->bnapi) 4073 return; 4074 4075 for (i = 0; i < bp->cp_nr_rings; i++) { 4076 struct bnxt_napi *bnapi = bp->bnapi[i]; 4077 struct bnxt_cp_ring_info *cpr; 4078 struct bnxt_ring_struct *ring; 4079 int j; 4080 4081 if (!bnapi) 4082 continue; 4083 4084 cpr = &bnapi->cp_ring; 4085 ring = &cpr->cp_ring_struct; 4086 4087 bnxt_free_ring(bp, &ring->ring_mem); 4088 4089 if (!cpr->cp_ring_arr) 4090 continue; 4091 4092 for (j = 0; j < cpr->cp_ring_count; j++) { 4093 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4094 4095 ring = &cpr2->cp_ring_struct; 4096 bnxt_free_ring(bp, &ring->ring_mem); 4097 bnxt_free_cp_arrays(cpr2); 4098 } 4099 kfree(cpr->cp_ring_arr); 4100 cpr->cp_ring_arr = NULL; 4101 cpr->cp_ring_count = 0; 4102 } 4103 } 4104 4105 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4106 struct bnxt_cp_ring_info *cpr) 4107 { 4108 struct bnxt_ring_mem_info *rmem; 4109 struct bnxt_ring_struct *ring; 4110 int rc; 4111 4112 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4113 if (rc) { 4114 bnxt_free_cp_arrays(cpr); 4115 return -ENOMEM; 4116 } 4117 ring = &cpr->cp_ring_struct; 4118 rmem = &ring->ring_mem; 4119 rmem->nr_pages = bp->cp_nr_pages; 4120 rmem->page_size = HW_CMPD_RING_SIZE; 4121 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4122 rmem->dma_arr = cpr->cp_desc_mapping; 4123 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4124 rc = bnxt_alloc_ring(bp, rmem); 4125 if (rc) { 4126 bnxt_free_ring(bp, rmem); 4127 bnxt_free_cp_arrays(cpr); 4128 } 4129 return rc; 4130 } 4131 4132 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4133 { 4134 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4135 int i, j, rc, ulp_msix; 4136 int tcs = bp->num_tc; 4137 4138 if (!tcs) 4139 tcs = 1; 4140 ulp_msix = bnxt_get_ulp_msix_num(bp); 4141 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4142 struct bnxt_napi *bnapi = bp->bnapi[i]; 4143 struct bnxt_cp_ring_info *cpr, *cpr2; 4144 struct bnxt_ring_struct *ring; 4145 int cp_count = 0, k; 4146 int rx = 0, tx = 0; 4147 4148 if (!bnapi) 4149 continue; 4150 4151 cpr = &bnapi->cp_ring; 4152 cpr->bnapi = bnapi; 4153 ring = &cpr->cp_ring_struct; 4154 4155 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4156 if (rc) 4157 return rc; 4158 4159 ring->map_idx = ulp_msix + i; 4160 4161 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4162 continue; 4163 4164 if (i < bp->rx_nr_rings) { 4165 cp_count++; 4166 rx = 1; 4167 } 4168 if (i < bp->tx_nr_rings_xdp) { 4169 cp_count++; 4170 tx = 1; 4171 } else if ((sh && i < bp->tx_nr_rings) || 4172 (!sh && i >= bp->rx_nr_rings)) { 4173 cp_count += tcs; 4174 tx = 1; 4175 } 4176 4177 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 4178 GFP_KERNEL); 4179 if (!cpr->cp_ring_arr) 4180 return -ENOMEM; 4181 cpr->cp_ring_count = cp_count; 4182 4183 for (k = 0; k < cp_count; k++) { 4184 cpr2 = &cpr->cp_ring_arr[k]; 4185 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4186 if (rc) 4187 return rc; 4188 cpr2->bnapi = bnapi; 4189 cpr2->sw_stats = cpr->sw_stats; 4190 cpr2->cp_idx = k; 4191 if (!k && rx) { 4192 bp->rx_ring[i].rx_cpr = cpr2; 4193 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4194 } else { 4195 int n, tc = k - rx; 4196 4197 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4198 bp->tx_ring[n].tx_cpr = cpr2; 4199 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4200 } 4201 } 4202 if (tx) 4203 j++; 4204 } 4205 return 0; 4206 } 4207 4208 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4209 struct bnxt_rx_ring_info *rxr) 4210 { 4211 struct bnxt_ring_mem_info *rmem; 4212 struct bnxt_ring_struct *ring; 4213 4214 ring = &rxr->rx_ring_struct; 4215 rmem = &ring->ring_mem; 4216 rmem->nr_pages = bp->rx_nr_pages; 4217 rmem->page_size = HW_RXBD_RING_SIZE; 4218 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4219 rmem->dma_arr = rxr->rx_desc_mapping; 4220 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4221 rmem->vmem = (void **)&rxr->rx_buf_ring; 4222 4223 ring = &rxr->rx_agg_ring_struct; 4224 rmem = &ring->ring_mem; 4225 rmem->nr_pages = bp->rx_agg_nr_pages; 4226 rmem->page_size = HW_RXBD_RING_SIZE; 4227 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4228 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4229 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4230 rmem->vmem = (void **)&rxr->rx_agg_ring; 4231 } 4232 4233 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4234 struct bnxt_rx_ring_info *rxr) 4235 { 4236 struct bnxt_ring_mem_info *rmem; 4237 struct bnxt_ring_struct *ring; 4238 int i; 4239 4240 rxr->page_pool->p.napi = NULL; 4241 rxr->page_pool = NULL; 4242 rxr->head_pool->p.napi = NULL; 4243 rxr->head_pool = NULL; 4244 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4245 4246 ring = &rxr->rx_ring_struct; 4247 rmem = &ring->ring_mem; 4248 rmem->pg_tbl = NULL; 4249 rmem->pg_tbl_map = 0; 4250 for (i = 0; i < rmem->nr_pages; i++) { 4251 rmem->pg_arr[i] = NULL; 4252 rmem->dma_arr[i] = 0; 4253 } 4254 *rmem->vmem = NULL; 4255 4256 ring = &rxr->rx_agg_ring_struct; 4257 rmem = &ring->ring_mem; 4258 rmem->pg_tbl = NULL; 4259 rmem->pg_tbl_map = 0; 4260 for (i = 0; i < rmem->nr_pages; i++) { 4261 rmem->pg_arr[i] = NULL; 4262 rmem->dma_arr[i] = 0; 4263 } 4264 *rmem->vmem = NULL; 4265 } 4266 4267 static void bnxt_init_ring_struct(struct bnxt *bp) 4268 { 4269 int i, j; 4270 4271 for (i = 0; i < bp->cp_nr_rings; i++) { 4272 struct bnxt_napi *bnapi = bp->bnapi[i]; 4273 struct bnxt_ring_mem_info *rmem; 4274 struct bnxt_cp_ring_info *cpr; 4275 struct bnxt_rx_ring_info *rxr; 4276 struct bnxt_tx_ring_info *txr; 4277 struct bnxt_ring_struct *ring; 4278 4279 if (!bnapi) 4280 continue; 4281 4282 cpr = &bnapi->cp_ring; 4283 ring = &cpr->cp_ring_struct; 4284 rmem = &ring->ring_mem; 4285 rmem->nr_pages = bp->cp_nr_pages; 4286 rmem->page_size = HW_CMPD_RING_SIZE; 4287 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4288 rmem->dma_arr = cpr->cp_desc_mapping; 4289 rmem->vmem_size = 0; 4290 4291 rxr = bnapi->rx_ring; 4292 if (!rxr) 4293 goto skip_rx; 4294 4295 ring = &rxr->rx_ring_struct; 4296 rmem = &ring->ring_mem; 4297 rmem->nr_pages = bp->rx_nr_pages; 4298 rmem->page_size = HW_RXBD_RING_SIZE; 4299 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4300 rmem->dma_arr = rxr->rx_desc_mapping; 4301 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4302 rmem->vmem = (void **)&rxr->rx_buf_ring; 4303 4304 ring = &rxr->rx_agg_ring_struct; 4305 rmem = &ring->ring_mem; 4306 rmem->nr_pages = bp->rx_agg_nr_pages; 4307 rmem->page_size = HW_RXBD_RING_SIZE; 4308 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4309 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4310 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4311 rmem->vmem = (void **)&rxr->rx_agg_ring; 4312 4313 skip_rx: 4314 bnxt_for_each_napi_tx(j, bnapi, txr) { 4315 ring = &txr->tx_ring_struct; 4316 rmem = &ring->ring_mem; 4317 rmem->nr_pages = bp->tx_nr_pages; 4318 rmem->page_size = HW_TXBD_RING_SIZE; 4319 rmem->pg_arr = (void **)txr->tx_desc_ring; 4320 rmem->dma_arr = txr->tx_desc_mapping; 4321 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4322 rmem->vmem = (void **)&txr->tx_buf_ring; 4323 } 4324 } 4325 } 4326 4327 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4328 { 4329 int i; 4330 u32 prod; 4331 struct rx_bd **rx_buf_ring; 4332 4333 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4334 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4335 int j; 4336 struct rx_bd *rxbd; 4337 4338 rxbd = rx_buf_ring[i]; 4339 if (!rxbd) 4340 continue; 4341 4342 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4343 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4344 rxbd->rx_bd_opaque = prod; 4345 } 4346 } 4347 } 4348 4349 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4350 struct bnxt_rx_ring_info *rxr, 4351 int ring_nr) 4352 { 4353 u32 prod; 4354 int i; 4355 4356 prod = rxr->rx_prod; 4357 for (i = 0; i < bp->rx_ring_size; i++) { 4358 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4359 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4360 ring_nr, i, bp->rx_ring_size); 4361 break; 4362 } 4363 prod = NEXT_RX(prod); 4364 } 4365 rxr->rx_prod = prod; 4366 } 4367 4368 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp, 4369 struct bnxt_rx_ring_info *rxr, 4370 int ring_nr) 4371 { 4372 u32 prod; 4373 int i; 4374 4375 prod = rxr->rx_agg_prod; 4376 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4377 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) { 4378 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4379 ring_nr, i, bp->rx_ring_size); 4380 break; 4381 } 4382 prod = NEXT_RX_AGG(prod); 4383 } 4384 rxr->rx_agg_prod = prod; 4385 } 4386 4387 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4388 struct bnxt_rx_ring_info *rxr) 4389 { 4390 dma_addr_t mapping; 4391 u8 *data; 4392 int i; 4393 4394 for (i = 0; i < bp->max_tpa; i++) { 4395 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4396 GFP_KERNEL); 4397 if (!data) 4398 return -ENOMEM; 4399 4400 rxr->rx_tpa[i].data = data; 4401 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4402 rxr->rx_tpa[i].mapping = mapping; 4403 } 4404 4405 return 0; 4406 } 4407 4408 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4409 { 4410 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4411 int rc; 4412 4413 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4414 4415 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4416 return 0; 4417 4418 bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr); 4419 4420 if (rxr->rx_tpa) { 4421 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4422 if (rc) 4423 return rc; 4424 } 4425 return 0; 4426 } 4427 4428 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4429 struct bnxt_rx_ring_info *rxr) 4430 { 4431 struct bnxt_ring_struct *ring; 4432 u32 type; 4433 4434 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4435 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4436 4437 if (NET_IP_ALIGN == 2) 4438 type |= RX_BD_FLAGS_SOP; 4439 4440 ring = &rxr->rx_ring_struct; 4441 bnxt_init_rxbd_pages(ring, type); 4442 ring->fw_ring_id = INVALID_HW_RING_ID; 4443 } 4444 4445 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4446 struct bnxt_rx_ring_info *rxr) 4447 { 4448 struct bnxt_ring_struct *ring; 4449 u32 type; 4450 4451 ring = &rxr->rx_agg_ring_struct; 4452 ring->fw_ring_id = INVALID_HW_RING_ID; 4453 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4454 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4455 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4456 4457 bnxt_init_rxbd_pages(ring, type); 4458 } 4459 } 4460 4461 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4462 { 4463 struct bnxt_rx_ring_info *rxr; 4464 4465 rxr = &bp->rx_ring[ring_nr]; 4466 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4467 4468 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4469 &rxr->bnapi->napi); 4470 4471 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4472 bpf_prog_add(bp->xdp_prog, 1); 4473 rxr->xdp_prog = bp->xdp_prog; 4474 } 4475 4476 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4477 4478 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4479 } 4480 4481 static void bnxt_init_cp_rings(struct bnxt *bp) 4482 { 4483 int i, j; 4484 4485 for (i = 0; i < bp->cp_nr_rings; i++) { 4486 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4487 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4488 4489 ring->fw_ring_id = INVALID_HW_RING_ID; 4490 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4491 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4492 if (!cpr->cp_ring_arr) 4493 continue; 4494 for (j = 0; j < cpr->cp_ring_count; j++) { 4495 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4496 4497 ring = &cpr2->cp_ring_struct; 4498 ring->fw_ring_id = INVALID_HW_RING_ID; 4499 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4500 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4501 } 4502 } 4503 } 4504 4505 static int bnxt_init_rx_rings(struct bnxt *bp) 4506 { 4507 int i, rc = 0; 4508 4509 if (BNXT_RX_PAGE_MODE(bp)) { 4510 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4511 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4512 } else { 4513 bp->rx_offset = BNXT_RX_OFFSET; 4514 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4515 } 4516 4517 for (i = 0; i < bp->rx_nr_rings; i++) { 4518 rc = bnxt_init_one_rx_ring(bp, i); 4519 if (rc) 4520 break; 4521 } 4522 4523 return rc; 4524 } 4525 4526 static int bnxt_init_tx_rings(struct bnxt *bp) 4527 { 4528 u16 i; 4529 4530 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4531 BNXT_MIN_TX_DESC_CNT); 4532 4533 for (i = 0; i < bp->tx_nr_rings; i++) { 4534 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4535 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4536 4537 ring->fw_ring_id = INVALID_HW_RING_ID; 4538 4539 if (i >= bp->tx_nr_rings_xdp) 4540 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4541 NETDEV_QUEUE_TYPE_TX, 4542 &txr->bnapi->napi); 4543 } 4544 4545 return 0; 4546 } 4547 4548 static void bnxt_free_ring_grps(struct bnxt *bp) 4549 { 4550 kfree(bp->grp_info); 4551 bp->grp_info = NULL; 4552 } 4553 4554 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4555 { 4556 int i; 4557 4558 if (irq_re_init) { 4559 bp->grp_info = kcalloc(bp->cp_nr_rings, 4560 sizeof(struct bnxt_ring_grp_info), 4561 GFP_KERNEL); 4562 if (!bp->grp_info) 4563 return -ENOMEM; 4564 } 4565 for (i = 0; i < bp->cp_nr_rings; i++) { 4566 if (irq_re_init) 4567 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4568 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4569 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4570 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4571 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4572 } 4573 return 0; 4574 } 4575 4576 static void bnxt_free_vnics(struct bnxt *bp) 4577 { 4578 kfree(bp->vnic_info); 4579 bp->vnic_info = NULL; 4580 bp->nr_vnics = 0; 4581 } 4582 4583 static int bnxt_alloc_vnics(struct bnxt *bp) 4584 { 4585 int num_vnics = 1; 4586 4587 #ifdef CONFIG_RFS_ACCEL 4588 if (bp->flags & BNXT_FLAG_RFS) { 4589 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4590 num_vnics++; 4591 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4592 num_vnics += bp->rx_nr_rings; 4593 } 4594 #endif 4595 4596 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4597 num_vnics++; 4598 4599 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4600 GFP_KERNEL); 4601 if (!bp->vnic_info) 4602 return -ENOMEM; 4603 4604 bp->nr_vnics = num_vnics; 4605 return 0; 4606 } 4607 4608 static void bnxt_init_vnics(struct bnxt *bp) 4609 { 4610 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4611 int i; 4612 4613 for (i = 0; i < bp->nr_vnics; i++) { 4614 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4615 int j; 4616 4617 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4618 vnic->vnic_id = i; 4619 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4620 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4621 4622 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4623 4624 if (bp->vnic_info[i].rss_hash_key) { 4625 if (i == BNXT_VNIC_DEFAULT) { 4626 u8 *key = (void *)vnic->rss_hash_key; 4627 int k; 4628 4629 if (!bp->rss_hash_key_valid && 4630 !bp->rss_hash_key_updated) { 4631 get_random_bytes(bp->rss_hash_key, 4632 HW_HASH_KEY_SIZE); 4633 bp->rss_hash_key_updated = true; 4634 } 4635 4636 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4637 HW_HASH_KEY_SIZE); 4638 4639 if (!bp->rss_hash_key_updated) 4640 continue; 4641 4642 bp->rss_hash_key_updated = false; 4643 bp->rss_hash_key_valid = true; 4644 4645 bp->toeplitz_prefix = 0; 4646 for (k = 0; k < 8; k++) { 4647 bp->toeplitz_prefix <<= 8; 4648 bp->toeplitz_prefix |= key[k]; 4649 } 4650 } else { 4651 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4652 HW_HASH_KEY_SIZE); 4653 } 4654 } 4655 } 4656 } 4657 4658 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4659 { 4660 int pages; 4661 4662 pages = ring_size / desc_per_pg; 4663 4664 if (!pages) 4665 return 1; 4666 4667 pages++; 4668 4669 while (pages & (pages - 1)) 4670 pages++; 4671 4672 return pages; 4673 } 4674 4675 void bnxt_set_tpa_flags(struct bnxt *bp) 4676 { 4677 bp->flags &= ~BNXT_FLAG_TPA; 4678 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4679 return; 4680 if (bp->dev->features & NETIF_F_LRO) 4681 bp->flags |= BNXT_FLAG_LRO; 4682 else if (bp->dev->features & NETIF_F_GRO_HW) 4683 bp->flags |= BNXT_FLAG_GRO; 4684 } 4685 4686 static void bnxt_init_ring_params(struct bnxt *bp) 4687 { 4688 unsigned int rx_size; 4689 4690 bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK; 4691 /* Try to fit 4 chunks into a 4k page */ 4692 rx_size = SZ_1K - 4693 NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4694 bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size); 4695 } 4696 4697 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4698 * be set on entry. 4699 */ 4700 void bnxt_set_ring_params(struct bnxt *bp) 4701 { 4702 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4703 u32 agg_factor = 0, agg_ring_size = 0; 4704 4705 /* 8 for CRC and VLAN */ 4706 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4707 4708 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4709 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4710 4711 ring_size = bp->rx_ring_size; 4712 bp->rx_agg_ring_size = 0; 4713 bp->rx_agg_nr_pages = 0; 4714 4715 if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS) 4716 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4717 4718 bp->flags &= ~BNXT_FLAG_JUMBO; 4719 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4720 u32 jumbo_factor; 4721 4722 bp->flags |= BNXT_FLAG_JUMBO; 4723 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4724 if (jumbo_factor > agg_factor) 4725 agg_factor = jumbo_factor; 4726 } 4727 if (agg_factor) { 4728 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4729 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4730 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4731 bp->rx_ring_size, ring_size); 4732 bp->rx_ring_size = ring_size; 4733 } 4734 agg_ring_size = ring_size * agg_factor; 4735 4736 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4737 RX_DESC_CNT); 4738 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4739 u32 tmp = agg_ring_size; 4740 4741 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4742 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4743 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4744 tmp, agg_ring_size); 4745 } 4746 bp->rx_agg_ring_size = agg_ring_size; 4747 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4748 4749 if (BNXT_RX_PAGE_MODE(bp)) { 4750 rx_space = PAGE_SIZE; 4751 rx_size = PAGE_SIZE - 4752 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4753 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4754 } else { 4755 rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK, 4756 bp->rx_copybreak, 4757 bp->dev->cfg_pending->hds_thresh); 4758 rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN); 4759 rx_space = rx_size + NET_SKB_PAD + 4760 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4761 } 4762 } 4763 4764 bp->rx_buf_use_size = rx_size; 4765 bp->rx_buf_size = rx_space; 4766 4767 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4768 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4769 4770 ring_size = bp->tx_ring_size; 4771 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4772 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4773 4774 max_rx_cmpl = bp->rx_ring_size; 4775 /* MAX TPA needs to be added because TPA_START completions are 4776 * immediately recycled, so the TPA completions are not bound by 4777 * the RX ring size. 4778 */ 4779 if (bp->flags & BNXT_FLAG_TPA) 4780 max_rx_cmpl += bp->max_tpa; 4781 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4782 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4783 bp->cp_ring_size = ring_size; 4784 4785 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4786 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4787 bp->cp_nr_pages = MAX_CP_PAGES; 4788 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4789 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4790 ring_size, bp->cp_ring_size); 4791 } 4792 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4793 bp->cp_ring_mask = bp->cp_bit - 1; 4794 } 4795 4796 /* Changing allocation mode of RX rings. 4797 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4798 */ 4799 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4800 { 4801 struct net_device *dev = bp->dev; 4802 4803 if (page_mode) { 4804 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 4805 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4806 4807 if (bp->xdp_prog->aux->xdp_has_frags) 4808 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4809 else 4810 dev->max_mtu = 4811 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4812 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4813 bp->flags |= BNXT_FLAG_JUMBO; 4814 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4815 } else { 4816 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4817 bp->rx_skb_func = bnxt_rx_page_skb; 4818 } 4819 bp->rx_dir = DMA_BIDIRECTIONAL; 4820 } else { 4821 dev->max_mtu = bp->max_mtu; 4822 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4823 bp->rx_dir = DMA_FROM_DEVICE; 4824 bp->rx_skb_func = bnxt_rx_skb; 4825 } 4826 } 4827 4828 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4829 { 4830 __bnxt_set_rx_skb_mode(bp, page_mode); 4831 4832 if (!page_mode) { 4833 int rx, tx; 4834 4835 bnxt_get_max_rings(bp, &rx, &tx, true); 4836 if (rx > 1) { 4837 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 4838 bp->dev->hw_features |= NETIF_F_LRO; 4839 } 4840 } 4841 4842 /* Update LRO and GRO_HW availability */ 4843 netdev_update_features(bp->dev); 4844 } 4845 4846 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4847 { 4848 int i; 4849 struct bnxt_vnic_info *vnic; 4850 struct pci_dev *pdev = bp->pdev; 4851 4852 if (!bp->vnic_info) 4853 return; 4854 4855 for (i = 0; i < bp->nr_vnics; i++) { 4856 vnic = &bp->vnic_info[i]; 4857 4858 kfree(vnic->fw_grp_ids); 4859 vnic->fw_grp_ids = NULL; 4860 4861 kfree(vnic->uc_list); 4862 vnic->uc_list = NULL; 4863 4864 if (vnic->mc_list) { 4865 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4866 vnic->mc_list, vnic->mc_list_mapping); 4867 vnic->mc_list = NULL; 4868 } 4869 4870 if (vnic->rss_table) { 4871 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4872 vnic->rss_table, 4873 vnic->rss_table_dma_addr); 4874 vnic->rss_table = NULL; 4875 } 4876 4877 vnic->rss_hash_key = NULL; 4878 vnic->flags = 0; 4879 } 4880 } 4881 4882 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4883 { 4884 int i, rc = 0, size; 4885 struct bnxt_vnic_info *vnic; 4886 struct pci_dev *pdev = bp->pdev; 4887 int max_rings; 4888 4889 for (i = 0; i < bp->nr_vnics; i++) { 4890 vnic = &bp->vnic_info[i]; 4891 4892 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4893 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4894 4895 if (mem_size > 0) { 4896 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4897 if (!vnic->uc_list) { 4898 rc = -ENOMEM; 4899 goto out; 4900 } 4901 } 4902 } 4903 4904 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4905 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4906 vnic->mc_list = 4907 dma_alloc_coherent(&pdev->dev, 4908 vnic->mc_list_size, 4909 &vnic->mc_list_mapping, 4910 GFP_KERNEL); 4911 if (!vnic->mc_list) { 4912 rc = -ENOMEM; 4913 goto out; 4914 } 4915 } 4916 4917 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4918 goto vnic_skip_grps; 4919 4920 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4921 max_rings = bp->rx_nr_rings; 4922 else 4923 max_rings = 1; 4924 4925 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4926 if (!vnic->fw_grp_ids) { 4927 rc = -ENOMEM; 4928 goto out; 4929 } 4930 vnic_skip_grps: 4931 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4932 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4933 continue; 4934 4935 /* Allocate rss table and hash key */ 4936 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4937 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4938 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4939 4940 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4941 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4942 vnic->rss_table_size, 4943 &vnic->rss_table_dma_addr, 4944 GFP_KERNEL); 4945 if (!vnic->rss_table) { 4946 rc = -ENOMEM; 4947 goto out; 4948 } 4949 4950 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4951 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4952 } 4953 return 0; 4954 4955 out: 4956 return rc; 4957 } 4958 4959 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4960 { 4961 struct bnxt_hwrm_wait_token *token; 4962 4963 dma_pool_destroy(bp->hwrm_dma_pool); 4964 bp->hwrm_dma_pool = NULL; 4965 4966 rcu_read_lock(); 4967 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4968 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4969 rcu_read_unlock(); 4970 } 4971 4972 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 4973 { 4974 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 4975 BNXT_HWRM_DMA_SIZE, 4976 BNXT_HWRM_DMA_ALIGN, 0); 4977 if (!bp->hwrm_dma_pool) 4978 return -ENOMEM; 4979 4980 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4981 4982 return 0; 4983 } 4984 4985 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4986 { 4987 kfree(stats->hw_masks); 4988 stats->hw_masks = NULL; 4989 kfree(stats->sw_stats); 4990 stats->sw_stats = NULL; 4991 if (stats->hw_stats) { 4992 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 4993 stats->hw_stats_map); 4994 stats->hw_stats = NULL; 4995 } 4996 } 4997 4998 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 4999 bool alloc_masks) 5000 { 5001 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 5002 &stats->hw_stats_map, GFP_KERNEL); 5003 if (!stats->hw_stats) 5004 return -ENOMEM; 5005 5006 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 5007 if (!stats->sw_stats) 5008 goto stats_mem_err; 5009 5010 if (alloc_masks) { 5011 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 5012 if (!stats->hw_masks) 5013 goto stats_mem_err; 5014 } 5015 return 0; 5016 5017 stats_mem_err: 5018 bnxt_free_stats_mem(bp, stats); 5019 return -ENOMEM; 5020 } 5021 5022 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 5023 { 5024 int i; 5025 5026 for (i = 0; i < count; i++) 5027 mask_arr[i] = mask; 5028 } 5029 5030 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 5031 { 5032 int i; 5033 5034 for (i = 0; i < count; i++) 5035 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 5036 } 5037 5038 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 5039 struct bnxt_stats_mem *stats) 5040 { 5041 struct hwrm_func_qstats_ext_output *resp; 5042 struct hwrm_func_qstats_ext_input *req; 5043 __le64 *hw_masks; 5044 int rc; 5045 5046 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 5047 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5048 return -EOPNOTSUPP; 5049 5050 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 5051 if (rc) 5052 return rc; 5053 5054 req->fid = cpu_to_le16(0xffff); 5055 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5056 5057 resp = hwrm_req_hold(bp, req); 5058 rc = hwrm_req_send(bp, req); 5059 if (!rc) { 5060 hw_masks = &resp->rx_ucast_pkts; 5061 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 5062 } 5063 hwrm_req_drop(bp, req); 5064 return rc; 5065 } 5066 5067 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 5068 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 5069 5070 static void bnxt_init_stats(struct bnxt *bp) 5071 { 5072 struct bnxt_napi *bnapi = bp->bnapi[0]; 5073 struct bnxt_cp_ring_info *cpr; 5074 struct bnxt_stats_mem *stats; 5075 __le64 *rx_stats, *tx_stats; 5076 int rc, rx_count, tx_count; 5077 u64 *rx_masks, *tx_masks; 5078 u64 mask; 5079 u8 flags; 5080 5081 cpr = &bnapi->cp_ring; 5082 stats = &cpr->stats; 5083 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 5084 if (rc) { 5085 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5086 mask = (1ULL << 48) - 1; 5087 else 5088 mask = -1ULL; 5089 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 5090 } 5091 if (bp->flags & BNXT_FLAG_PORT_STATS) { 5092 stats = &bp->port_stats; 5093 rx_stats = stats->hw_stats; 5094 rx_masks = stats->hw_masks; 5095 rx_count = sizeof(struct rx_port_stats) / 8; 5096 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5097 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5098 tx_count = sizeof(struct tx_port_stats) / 8; 5099 5100 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 5101 rc = bnxt_hwrm_port_qstats(bp, flags); 5102 if (rc) { 5103 mask = (1ULL << 40) - 1; 5104 5105 bnxt_fill_masks(rx_masks, mask, rx_count); 5106 bnxt_fill_masks(tx_masks, mask, tx_count); 5107 } else { 5108 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5109 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5110 bnxt_hwrm_port_qstats(bp, 0); 5111 } 5112 } 5113 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5114 stats = &bp->rx_port_stats_ext; 5115 rx_stats = stats->hw_stats; 5116 rx_masks = stats->hw_masks; 5117 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5118 stats = &bp->tx_port_stats_ext; 5119 tx_stats = stats->hw_stats; 5120 tx_masks = stats->hw_masks; 5121 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5122 5123 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5124 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5125 if (rc) { 5126 mask = (1ULL << 40) - 1; 5127 5128 bnxt_fill_masks(rx_masks, mask, rx_count); 5129 if (tx_stats) 5130 bnxt_fill_masks(tx_masks, mask, tx_count); 5131 } else { 5132 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5133 if (tx_stats) 5134 bnxt_copy_hw_masks(tx_masks, tx_stats, 5135 tx_count); 5136 bnxt_hwrm_port_qstats_ext(bp, 0); 5137 } 5138 } 5139 } 5140 5141 static void bnxt_free_port_stats(struct bnxt *bp) 5142 { 5143 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5144 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5145 5146 bnxt_free_stats_mem(bp, &bp->port_stats); 5147 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5148 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5149 } 5150 5151 static void bnxt_free_ring_stats(struct bnxt *bp) 5152 { 5153 int i; 5154 5155 if (!bp->bnapi) 5156 return; 5157 5158 for (i = 0; i < bp->cp_nr_rings; i++) { 5159 struct bnxt_napi *bnapi = bp->bnapi[i]; 5160 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5161 5162 bnxt_free_stats_mem(bp, &cpr->stats); 5163 5164 kfree(cpr->sw_stats); 5165 cpr->sw_stats = NULL; 5166 } 5167 } 5168 5169 static int bnxt_alloc_stats(struct bnxt *bp) 5170 { 5171 u32 size, i; 5172 int rc; 5173 5174 size = bp->hw_ring_stats_size; 5175 5176 for (i = 0; i < bp->cp_nr_rings; i++) { 5177 struct bnxt_napi *bnapi = bp->bnapi[i]; 5178 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5179 5180 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 5181 if (!cpr->sw_stats) 5182 return -ENOMEM; 5183 5184 cpr->stats.len = size; 5185 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5186 if (rc) 5187 return rc; 5188 5189 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5190 } 5191 5192 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5193 return 0; 5194 5195 if (bp->port_stats.hw_stats) 5196 goto alloc_ext_stats; 5197 5198 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5199 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5200 if (rc) 5201 return rc; 5202 5203 bp->flags |= BNXT_FLAG_PORT_STATS; 5204 5205 alloc_ext_stats: 5206 /* Display extended statistics only if FW supports it */ 5207 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5208 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5209 return 0; 5210 5211 if (bp->rx_port_stats_ext.hw_stats) 5212 goto alloc_tx_ext_stats; 5213 5214 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5215 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5216 /* Extended stats are optional */ 5217 if (rc) 5218 return 0; 5219 5220 alloc_tx_ext_stats: 5221 if (bp->tx_port_stats_ext.hw_stats) 5222 return 0; 5223 5224 if (bp->hwrm_spec_code >= 0x10902 || 5225 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5226 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5227 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5228 /* Extended stats are optional */ 5229 if (rc) 5230 return 0; 5231 } 5232 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5233 return 0; 5234 } 5235 5236 static void bnxt_clear_ring_indices(struct bnxt *bp) 5237 { 5238 int i, j; 5239 5240 if (!bp->bnapi) 5241 return; 5242 5243 for (i = 0; i < bp->cp_nr_rings; i++) { 5244 struct bnxt_napi *bnapi = bp->bnapi[i]; 5245 struct bnxt_cp_ring_info *cpr; 5246 struct bnxt_rx_ring_info *rxr; 5247 struct bnxt_tx_ring_info *txr; 5248 5249 if (!bnapi) 5250 continue; 5251 5252 cpr = &bnapi->cp_ring; 5253 cpr->cp_raw_cons = 0; 5254 5255 bnxt_for_each_napi_tx(j, bnapi, txr) { 5256 txr->tx_prod = 0; 5257 txr->tx_cons = 0; 5258 txr->tx_hw_cons = 0; 5259 } 5260 5261 rxr = bnapi->rx_ring; 5262 if (rxr) { 5263 rxr->rx_prod = 0; 5264 rxr->rx_agg_prod = 0; 5265 rxr->rx_sw_agg_prod = 0; 5266 rxr->rx_next_cons = 0; 5267 } 5268 bnapi->events = 0; 5269 } 5270 } 5271 5272 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5273 { 5274 u8 type = fltr->type, flags = fltr->flags; 5275 5276 INIT_LIST_HEAD(&fltr->list); 5277 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5278 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5279 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5280 } 5281 5282 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5283 { 5284 if (!list_empty(&fltr->list)) 5285 list_del_init(&fltr->list); 5286 } 5287 5288 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5289 { 5290 struct bnxt_filter_base *usr_fltr, *tmp; 5291 5292 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5293 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5294 continue; 5295 bnxt_del_one_usr_fltr(bp, usr_fltr); 5296 } 5297 } 5298 5299 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5300 { 5301 hlist_del(&fltr->hash); 5302 bnxt_del_one_usr_fltr(bp, fltr); 5303 if (fltr->flags) { 5304 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5305 bp->ntp_fltr_count--; 5306 } 5307 kfree(fltr); 5308 } 5309 5310 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5311 { 5312 int i; 5313 5314 netdev_assert_locked(bp->dev); 5315 5316 /* Under netdev instance lock and all our NAPIs have been disabled. 5317 * It's safe to delete the hash table. 5318 */ 5319 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5320 struct hlist_head *head; 5321 struct hlist_node *tmp; 5322 struct bnxt_ntuple_filter *fltr; 5323 5324 head = &bp->ntp_fltr_hash_tbl[i]; 5325 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5326 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5327 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5328 !list_empty(&fltr->base.list))) 5329 continue; 5330 bnxt_del_fltr(bp, &fltr->base); 5331 } 5332 } 5333 if (!all) 5334 return; 5335 5336 bitmap_free(bp->ntp_fltr_bmap); 5337 bp->ntp_fltr_bmap = NULL; 5338 bp->ntp_fltr_count = 0; 5339 } 5340 5341 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5342 { 5343 int i, rc = 0; 5344 5345 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5346 return 0; 5347 5348 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5349 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5350 5351 bp->ntp_fltr_count = 0; 5352 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5353 5354 if (!bp->ntp_fltr_bmap) 5355 rc = -ENOMEM; 5356 5357 return rc; 5358 } 5359 5360 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5361 { 5362 int i; 5363 5364 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5365 struct hlist_head *head; 5366 struct hlist_node *tmp; 5367 struct bnxt_l2_filter *fltr; 5368 5369 head = &bp->l2_fltr_hash_tbl[i]; 5370 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5371 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5372 !list_empty(&fltr->base.list))) 5373 continue; 5374 bnxt_del_fltr(bp, &fltr->base); 5375 } 5376 } 5377 } 5378 5379 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5380 { 5381 int i; 5382 5383 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5384 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5385 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5386 } 5387 5388 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5389 { 5390 bnxt_free_vnic_attributes(bp); 5391 bnxt_free_tx_rings(bp); 5392 bnxt_free_rx_rings(bp); 5393 bnxt_free_cp_rings(bp); 5394 bnxt_free_all_cp_arrays(bp); 5395 bnxt_free_ntp_fltrs(bp, false); 5396 bnxt_free_l2_filters(bp, false); 5397 if (irq_re_init) { 5398 bnxt_free_ring_stats(bp); 5399 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5400 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5401 bnxt_free_port_stats(bp); 5402 bnxt_free_ring_grps(bp); 5403 bnxt_free_vnics(bp); 5404 kfree(bp->tx_ring_map); 5405 bp->tx_ring_map = NULL; 5406 kfree(bp->tx_ring); 5407 bp->tx_ring = NULL; 5408 kfree(bp->rx_ring); 5409 bp->rx_ring = NULL; 5410 kfree(bp->bnapi); 5411 bp->bnapi = NULL; 5412 } else { 5413 bnxt_clear_ring_indices(bp); 5414 } 5415 } 5416 5417 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5418 { 5419 int i, j, rc, size, arr_size; 5420 void *bnapi; 5421 5422 if (irq_re_init) { 5423 /* Allocate bnapi mem pointer array and mem block for 5424 * all queues 5425 */ 5426 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5427 bp->cp_nr_rings); 5428 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5429 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5430 if (!bnapi) 5431 return -ENOMEM; 5432 5433 bp->bnapi = bnapi; 5434 bnapi += arr_size; 5435 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5436 bp->bnapi[i] = bnapi; 5437 bp->bnapi[i]->index = i; 5438 bp->bnapi[i]->bp = bp; 5439 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5440 struct bnxt_cp_ring_info *cpr = 5441 &bp->bnapi[i]->cp_ring; 5442 5443 cpr->cp_ring_struct.ring_mem.flags = 5444 BNXT_RMEM_RING_PTE_FLAG; 5445 } 5446 } 5447 5448 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5449 sizeof(struct bnxt_rx_ring_info), 5450 GFP_KERNEL); 5451 if (!bp->rx_ring) 5452 return -ENOMEM; 5453 5454 for (i = 0; i < bp->rx_nr_rings; i++) { 5455 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5456 5457 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5458 rxr->rx_ring_struct.ring_mem.flags = 5459 BNXT_RMEM_RING_PTE_FLAG; 5460 rxr->rx_agg_ring_struct.ring_mem.flags = 5461 BNXT_RMEM_RING_PTE_FLAG; 5462 } else { 5463 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5464 } 5465 rxr->bnapi = bp->bnapi[i]; 5466 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5467 } 5468 5469 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5470 sizeof(struct bnxt_tx_ring_info), 5471 GFP_KERNEL); 5472 if (!bp->tx_ring) 5473 return -ENOMEM; 5474 5475 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5476 GFP_KERNEL); 5477 5478 if (!bp->tx_ring_map) 5479 return -ENOMEM; 5480 5481 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5482 j = 0; 5483 else 5484 j = bp->rx_nr_rings; 5485 5486 for (i = 0; i < bp->tx_nr_rings; i++) { 5487 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5488 struct bnxt_napi *bnapi2; 5489 5490 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5491 txr->tx_ring_struct.ring_mem.flags = 5492 BNXT_RMEM_RING_PTE_FLAG; 5493 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5494 if (i >= bp->tx_nr_rings_xdp) { 5495 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5496 5497 bnapi2 = bp->bnapi[k]; 5498 txr->txq_index = i - bp->tx_nr_rings_xdp; 5499 txr->tx_napi_idx = 5500 BNXT_RING_TO_TC(bp, txr->txq_index); 5501 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5502 bnapi2->tx_int = bnxt_tx_int; 5503 } else { 5504 bnapi2 = bp->bnapi[j]; 5505 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5506 bnapi2->tx_ring[0] = txr; 5507 bnapi2->tx_int = bnxt_tx_int_xdp; 5508 j++; 5509 } 5510 txr->bnapi = bnapi2; 5511 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5512 txr->tx_cpr = &bnapi2->cp_ring; 5513 } 5514 5515 rc = bnxt_alloc_stats(bp); 5516 if (rc) 5517 goto alloc_mem_err; 5518 bnxt_init_stats(bp); 5519 5520 rc = bnxt_alloc_ntp_fltrs(bp); 5521 if (rc) 5522 goto alloc_mem_err; 5523 5524 rc = bnxt_alloc_vnics(bp); 5525 if (rc) 5526 goto alloc_mem_err; 5527 } 5528 5529 rc = bnxt_alloc_all_cp_arrays(bp); 5530 if (rc) 5531 goto alloc_mem_err; 5532 5533 bnxt_init_ring_struct(bp); 5534 5535 rc = bnxt_alloc_rx_rings(bp); 5536 if (rc) 5537 goto alloc_mem_err; 5538 5539 rc = bnxt_alloc_tx_rings(bp); 5540 if (rc) 5541 goto alloc_mem_err; 5542 5543 rc = bnxt_alloc_cp_rings(bp); 5544 if (rc) 5545 goto alloc_mem_err; 5546 5547 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5548 BNXT_VNIC_MCAST_FLAG | 5549 BNXT_VNIC_UCAST_FLAG; 5550 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5551 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5552 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5553 5554 rc = bnxt_alloc_vnic_attributes(bp); 5555 if (rc) 5556 goto alloc_mem_err; 5557 return 0; 5558 5559 alloc_mem_err: 5560 bnxt_free_mem(bp, true); 5561 return rc; 5562 } 5563 5564 static void bnxt_disable_int(struct bnxt *bp) 5565 { 5566 int i; 5567 5568 if (!bp->bnapi) 5569 return; 5570 5571 for (i = 0; i < bp->cp_nr_rings; i++) { 5572 struct bnxt_napi *bnapi = bp->bnapi[i]; 5573 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5574 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5575 5576 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5577 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5578 } 5579 } 5580 5581 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5582 { 5583 struct bnxt_napi *bnapi = bp->bnapi[n]; 5584 struct bnxt_cp_ring_info *cpr; 5585 5586 cpr = &bnapi->cp_ring; 5587 return cpr->cp_ring_struct.map_idx; 5588 } 5589 5590 static void bnxt_disable_int_sync(struct bnxt *bp) 5591 { 5592 int i; 5593 5594 if (!bp->irq_tbl) 5595 return; 5596 5597 atomic_inc(&bp->intr_sem); 5598 5599 bnxt_disable_int(bp); 5600 for (i = 0; i < bp->cp_nr_rings; i++) { 5601 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5602 5603 synchronize_irq(bp->irq_tbl[map_idx].vector); 5604 } 5605 } 5606 5607 static void bnxt_enable_int(struct bnxt *bp) 5608 { 5609 int i; 5610 5611 atomic_set(&bp->intr_sem, 0); 5612 for (i = 0; i < bp->cp_nr_rings; i++) { 5613 struct bnxt_napi *bnapi = bp->bnapi[i]; 5614 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5615 5616 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5617 } 5618 } 5619 5620 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5621 bool async_only) 5622 { 5623 DECLARE_BITMAP(async_events_bmap, 256); 5624 u32 *events = (u32 *)async_events_bmap; 5625 struct hwrm_func_drv_rgtr_output *resp; 5626 struct hwrm_func_drv_rgtr_input *req; 5627 u32 flags; 5628 int rc, i; 5629 5630 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5631 if (rc) 5632 return rc; 5633 5634 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5635 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5636 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5637 5638 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5639 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5640 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5641 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5642 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5643 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5644 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5645 if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2) 5646 flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT; 5647 req->flags = cpu_to_le32(flags); 5648 req->ver_maj_8b = DRV_VER_MAJ; 5649 req->ver_min_8b = DRV_VER_MIN; 5650 req->ver_upd_8b = DRV_VER_UPD; 5651 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5652 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5653 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5654 5655 if (BNXT_PF(bp)) { 5656 u32 data[8]; 5657 int i; 5658 5659 memset(data, 0, sizeof(data)); 5660 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5661 u16 cmd = bnxt_vf_req_snif[i]; 5662 unsigned int bit, idx; 5663 5664 idx = cmd / 32; 5665 bit = cmd % 32; 5666 data[idx] |= 1 << bit; 5667 } 5668 5669 for (i = 0; i < 8; i++) 5670 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5671 5672 req->enables |= 5673 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5674 } 5675 5676 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5677 req->flags |= cpu_to_le32( 5678 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5679 5680 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5681 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5682 u16 event_id = bnxt_async_events_arr[i]; 5683 5684 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5685 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5686 continue; 5687 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5688 !bp->ptp_cfg) 5689 continue; 5690 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5691 } 5692 if (bmap && bmap_size) { 5693 for (i = 0; i < bmap_size; i++) { 5694 if (test_bit(i, bmap)) 5695 __set_bit(i, async_events_bmap); 5696 } 5697 } 5698 for (i = 0; i < 8; i++) 5699 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5700 5701 if (async_only) 5702 req->enables = 5703 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5704 5705 resp = hwrm_req_hold(bp, req); 5706 rc = hwrm_req_send(bp, req); 5707 if (!rc) { 5708 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5709 if (resp->flags & 5710 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5711 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5712 } 5713 hwrm_req_drop(bp, req); 5714 return rc; 5715 } 5716 5717 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5718 { 5719 struct hwrm_func_drv_unrgtr_input *req; 5720 int rc; 5721 5722 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5723 return 0; 5724 5725 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5726 if (rc) 5727 return rc; 5728 return hwrm_req_send(bp, req); 5729 } 5730 5731 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5732 5733 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5734 { 5735 struct hwrm_tunnel_dst_port_free_input *req; 5736 int rc; 5737 5738 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5739 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5740 return 0; 5741 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5742 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5743 return 0; 5744 5745 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5746 if (rc) 5747 return rc; 5748 5749 req->tunnel_type = tunnel_type; 5750 5751 switch (tunnel_type) { 5752 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5753 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5754 bp->vxlan_port = 0; 5755 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5756 break; 5757 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5758 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5759 bp->nge_port = 0; 5760 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5761 break; 5762 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5763 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5764 bp->vxlan_gpe_port = 0; 5765 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5766 break; 5767 default: 5768 break; 5769 } 5770 5771 rc = hwrm_req_send(bp, req); 5772 if (rc) 5773 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5774 rc); 5775 if (bp->flags & BNXT_FLAG_TPA) 5776 bnxt_set_tpa(bp, true); 5777 return rc; 5778 } 5779 5780 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5781 u8 tunnel_type) 5782 { 5783 struct hwrm_tunnel_dst_port_alloc_output *resp; 5784 struct hwrm_tunnel_dst_port_alloc_input *req; 5785 int rc; 5786 5787 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5788 if (rc) 5789 return rc; 5790 5791 req->tunnel_type = tunnel_type; 5792 req->tunnel_dst_port_val = port; 5793 5794 resp = hwrm_req_hold(bp, req); 5795 rc = hwrm_req_send(bp, req); 5796 if (rc) { 5797 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5798 rc); 5799 goto err_out; 5800 } 5801 5802 switch (tunnel_type) { 5803 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5804 bp->vxlan_port = port; 5805 bp->vxlan_fw_dst_port_id = 5806 le16_to_cpu(resp->tunnel_dst_port_id); 5807 break; 5808 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5809 bp->nge_port = port; 5810 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5811 break; 5812 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5813 bp->vxlan_gpe_port = port; 5814 bp->vxlan_gpe_fw_dst_port_id = 5815 le16_to_cpu(resp->tunnel_dst_port_id); 5816 break; 5817 default: 5818 break; 5819 } 5820 if (bp->flags & BNXT_FLAG_TPA) 5821 bnxt_set_tpa(bp, true); 5822 5823 err_out: 5824 hwrm_req_drop(bp, req); 5825 return rc; 5826 } 5827 5828 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5829 { 5830 struct hwrm_cfa_l2_set_rx_mask_input *req; 5831 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5832 int rc; 5833 5834 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5835 if (rc) 5836 return rc; 5837 5838 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5839 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5840 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5841 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5842 } 5843 req->mask = cpu_to_le32(vnic->rx_mask); 5844 return hwrm_req_send_silent(bp, req); 5845 } 5846 5847 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5848 { 5849 if (!atomic_dec_and_test(&fltr->refcnt)) 5850 return; 5851 spin_lock_bh(&bp->ntp_fltr_lock); 5852 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5853 spin_unlock_bh(&bp->ntp_fltr_lock); 5854 return; 5855 } 5856 hlist_del_rcu(&fltr->base.hash); 5857 bnxt_del_one_usr_fltr(bp, &fltr->base); 5858 if (fltr->base.flags) { 5859 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5860 bp->ntp_fltr_count--; 5861 } 5862 spin_unlock_bh(&bp->ntp_fltr_lock); 5863 kfree_rcu(fltr, base.rcu); 5864 } 5865 5866 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5867 struct bnxt_l2_key *key, 5868 u32 idx) 5869 { 5870 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5871 struct bnxt_l2_filter *fltr; 5872 5873 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5874 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5875 5876 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5877 l2_key->vlan == key->vlan) 5878 return fltr; 5879 } 5880 return NULL; 5881 } 5882 5883 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5884 struct bnxt_l2_key *key, 5885 u32 idx) 5886 { 5887 struct bnxt_l2_filter *fltr = NULL; 5888 5889 rcu_read_lock(); 5890 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5891 if (fltr) 5892 atomic_inc(&fltr->refcnt); 5893 rcu_read_unlock(); 5894 return fltr; 5895 } 5896 5897 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5898 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5899 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5900 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5901 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5902 5903 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5904 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5905 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5906 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5907 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5908 5909 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5910 { 5911 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5912 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5913 return sizeof(fkeys->addrs.v4addrs) + 5914 sizeof(fkeys->ports); 5915 5916 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5917 return sizeof(fkeys->addrs.v4addrs); 5918 } 5919 5920 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5921 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5922 return sizeof(fkeys->addrs.v6addrs) + 5923 sizeof(fkeys->ports); 5924 5925 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5926 return sizeof(fkeys->addrs.v6addrs); 5927 } 5928 5929 return 0; 5930 } 5931 5932 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5933 const unsigned char *key) 5934 { 5935 u64 prefix = bp->toeplitz_prefix, hash = 0; 5936 struct bnxt_ipv4_tuple tuple4; 5937 struct bnxt_ipv6_tuple tuple6; 5938 int i, j, len = 0; 5939 u8 *four_tuple; 5940 5941 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5942 if (!len) 5943 return 0; 5944 5945 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5946 tuple4.v4addrs = fkeys->addrs.v4addrs; 5947 tuple4.ports = fkeys->ports; 5948 four_tuple = (unsigned char *)&tuple4; 5949 } else { 5950 tuple6.v6addrs = fkeys->addrs.v6addrs; 5951 tuple6.ports = fkeys->ports; 5952 four_tuple = (unsigned char *)&tuple6; 5953 } 5954 5955 for (i = 0, j = 8; i < len; i++, j++) { 5956 u8 byte = four_tuple[i]; 5957 int bit; 5958 5959 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5960 if (byte & 0x80) 5961 hash ^= prefix; 5962 } 5963 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5964 } 5965 5966 /* The valid part of the hash is in the upper 32 bits. */ 5967 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5968 } 5969 5970 #ifdef CONFIG_RFS_ACCEL 5971 static struct bnxt_l2_filter * 5972 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 5973 { 5974 struct bnxt_l2_filter *fltr; 5975 u32 idx; 5976 5977 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5978 BNXT_L2_FLTR_HASH_MASK; 5979 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5980 return fltr; 5981 } 5982 #endif 5983 5984 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 5985 struct bnxt_l2_key *key, u32 idx) 5986 { 5987 struct hlist_head *head; 5988 5989 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 5990 fltr->l2_key.vlan = key->vlan; 5991 fltr->base.type = BNXT_FLTR_TYPE_L2; 5992 if (fltr->base.flags) { 5993 int bit_id; 5994 5995 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 5996 bp->max_fltr, 0); 5997 if (bit_id < 0) 5998 return -ENOMEM; 5999 fltr->base.sw_id = (u16)bit_id; 6000 bp->ntp_fltr_count++; 6001 } 6002 head = &bp->l2_fltr_hash_tbl[idx]; 6003 hlist_add_head_rcu(&fltr->base.hash, head); 6004 bnxt_insert_usr_fltr(bp, &fltr->base); 6005 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 6006 atomic_set(&fltr->refcnt, 1); 6007 return 0; 6008 } 6009 6010 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 6011 struct bnxt_l2_key *key, 6012 gfp_t gfp) 6013 { 6014 struct bnxt_l2_filter *fltr; 6015 u32 idx; 6016 int rc; 6017 6018 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6019 BNXT_L2_FLTR_HASH_MASK; 6020 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6021 if (fltr) 6022 return fltr; 6023 6024 fltr = kzalloc(sizeof(*fltr), gfp); 6025 if (!fltr) 6026 return ERR_PTR(-ENOMEM); 6027 spin_lock_bh(&bp->ntp_fltr_lock); 6028 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6029 spin_unlock_bh(&bp->ntp_fltr_lock); 6030 if (rc) { 6031 bnxt_del_l2_filter(bp, fltr); 6032 fltr = ERR_PTR(rc); 6033 } 6034 return fltr; 6035 } 6036 6037 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 6038 struct bnxt_l2_key *key, 6039 u16 flags) 6040 { 6041 struct bnxt_l2_filter *fltr; 6042 u32 idx; 6043 int rc; 6044 6045 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6046 BNXT_L2_FLTR_HASH_MASK; 6047 spin_lock_bh(&bp->ntp_fltr_lock); 6048 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6049 if (fltr) { 6050 fltr = ERR_PTR(-EEXIST); 6051 goto l2_filter_exit; 6052 } 6053 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 6054 if (!fltr) { 6055 fltr = ERR_PTR(-ENOMEM); 6056 goto l2_filter_exit; 6057 } 6058 fltr->base.flags = flags; 6059 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6060 if (rc) { 6061 spin_unlock_bh(&bp->ntp_fltr_lock); 6062 bnxt_del_l2_filter(bp, fltr); 6063 return ERR_PTR(rc); 6064 } 6065 6066 l2_filter_exit: 6067 spin_unlock_bh(&bp->ntp_fltr_lock); 6068 return fltr; 6069 } 6070 6071 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 6072 { 6073 #ifdef CONFIG_BNXT_SRIOV 6074 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 6075 6076 return vf->fw_fid; 6077 #else 6078 return INVALID_HW_RING_ID; 6079 #endif 6080 } 6081 6082 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6083 { 6084 struct hwrm_cfa_l2_filter_free_input *req; 6085 u16 target_id = 0xffff; 6086 int rc; 6087 6088 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6089 struct bnxt_pf_info *pf = &bp->pf; 6090 6091 if (fltr->base.vf_idx >= pf->active_vfs) 6092 return -EINVAL; 6093 6094 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6095 if (target_id == INVALID_HW_RING_ID) 6096 return -EINVAL; 6097 } 6098 6099 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 6100 if (rc) 6101 return rc; 6102 6103 req->target_id = cpu_to_le16(target_id); 6104 req->l2_filter_id = fltr->base.filter_id; 6105 return hwrm_req_send(bp, req); 6106 } 6107 6108 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6109 { 6110 struct hwrm_cfa_l2_filter_alloc_output *resp; 6111 struct hwrm_cfa_l2_filter_alloc_input *req; 6112 u16 target_id = 0xffff; 6113 int rc; 6114 6115 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6116 struct bnxt_pf_info *pf = &bp->pf; 6117 6118 if (fltr->base.vf_idx >= pf->active_vfs) 6119 return -EINVAL; 6120 6121 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6122 } 6123 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6124 if (rc) 6125 return rc; 6126 6127 req->target_id = cpu_to_le16(target_id); 6128 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6129 6130 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6131 req->flags |= 6132 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6133 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6134 req->enables = 6135 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6136 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6137 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6138 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6139 eth_broadcast_addr(req->l2_addr_mask); 6140 6141 if (fltr->l2_key.vlan) { 6142 req->enables |= 6143 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6144 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6145 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6146 req->num_vlans = 1; 6147 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6148 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6149 } 6150 6151 resp = hwrm_req_hold(bp, req); 6152 rc = hwrm_req_send(bp, req); 6153 if (!rc) { 6154 fltr->base.filter_id = resp->l2_filter_id; 6155 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6156 } 6157 hwrm_req_drop(bp, req); 6158 return rc; 6159 } 6160 6161 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6162 struct bnxt_ntuple_filter *fltr) 6163 { 6164 struct hwrm_cfa_ntuple_filter_free_input *req; 6165 int rc; 6166 6167 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6168 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6169 if (rc) 6170 return rc; 6171 6172 req->ntuple_filter_id = fltr->base.filter_id; 6173 return hwrm_req_send(bp, req); 6174 } 6175 6176 #define BNXT_NTP_FLTR_FLAGS \ 6177 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6178 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6179 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6180 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6181 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6182 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6183 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6184 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6185 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6186 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6187 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6188 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6189 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6190 6191 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6192 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6193 6194 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6195 { 6196 int i; 6197 6198 for (i = 0; i < 4; i++) 6199 mask[i] = cpu_to_be32(~0); 6200 } 6201 6202 static void 6203 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6204 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6205 struct bnxt_ntuple_filter *fltr) 6206 { 6207 u16 rxq = fltr->base.rxq; 6208 6209 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6210 struct ethtool_rxfh_context *ctx; 6211 struct bnxt_rss_ctx *rss_ctx; 6212 struct bnxt_vnic_info *vnic; 6213 6214 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6215 fltr->base.fw_vnic_id); 6216 if (ctx) { 6217 rss_ctx = ethtool_rxfh_context_priv(ctx); 6218 vnic = &rss_ctx->vnic; 6219 6220 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6221 } 6222 return; 6223 } 6224 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6225 struct bnxt_vnic_info *vnic; 6226 u32 enables; 6227 6228 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6229 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6230 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6231 req->enables |= cpu_to_le32(enables); 6232 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6233 } else { 6234 u32 flags; 6235 6236 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6237 req->flags |= cpu_to_le32(flags); 6238 req->dst_id = cpu_to_le16(rxq); 6239 } 6240 } 6241 6242 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6243 struct bnxt_ntuple_filter *fltr) 6244 { 6245 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6246 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6247 struct bnxt_flow_masks *masks = &fltr->fmasks; 6248 struct flow_keys *keys = &fltr->fkeys; 6249 struct bnxt_l2_filter *l2_fltr; 6250 struct bnxt_vnic_info *vnic; 6251 int rc; 6252 6253 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6254 if (rc) 6255 return rc; 6256 6257 l2_fltr = fltr->l2_fltr; 6258 req->l2_filter_id = l2_fltr->base.filter_id; 6259 6260 if (fltr->base.flags & BNXT_ACT_DROP) { 6261 req->flags = 6262 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6263 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6264 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6265 } else { 6266 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6267 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6268 } 6269 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6270 6271 req->ethertype = htons(ETH_P_IP); 6272 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6273 req->ip_protocol = keys->basic.ip_proto; 6274 6275 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6276 req->ethertype = htons(ETH_P_IPV6); 6277 req->ip_addr_type = 6278 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6279 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6280 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6281 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6282 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6283 } else { 6284 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6285 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6286 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6287 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6288 } 6289 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6290 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6291 req->tunnel_type = 6292 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6293 } 6294 6295 req->src_port = keys->ports.src; 6296 req->src_port_mask = masks->ports.src; 6297 req->dst_port = keys->ports.dst; 6298 req->dst_port_mask = masks->ports.dst; 6299 6300 resp = hwrm_req_hold(bp, req); 6301 rc = hwrm_req_send(bp, req); 6302 if (!rc) 6303 fltr->base.filter_id = resp->ntuple_filter_id; 6304 hwrm_req_drop(bp, req); 6305 return rc; 6306 } 6307 6308 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6309 const u8 *mac_addr) 6310 { 6311 struct bnxt_l2_filter *fltr; 6312 struct bnxt_l2_key key; 6313 int rc; 6314 6315 ether_addr_copy(key.dst_mac_addr, mac_addr); 6316 key.vlan = 0; 6317 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6318 if (IS_ERR(fltr)) 6319 return PTR_ERR(fltr); 6320 6321 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6322 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6323 if (rc) 6324 bnxt_del_l2_filter(bp, fltr); 6325 else 6326 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6327 return rc; 6328 } 6329 6330 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6331 { 6332 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6333 6334 /* Any associated ntuple filters will also be cleared by firmware. */ 6335 for (i = 0; i < num_of_vnics; i++) { 6336 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6337 6338 for (j = 0; j < vnic->uc_filter_count; j++) { 6339 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6340 6341 bnxt_hwrm_l2_filter_free(bp, fltr); 6342 bnxt_del_l2_filter(bp, fltr); 6343 } 6344 vnic->uc_filter_count = 0; 6345 } 6346 } 6347 6348 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6349 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6350 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6351 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6352 6353 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6354 struct hwrm_vnic_tpa_cfg_input *req) 6355 { 6356 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6357 6358 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6359 return; 6360 6361 if (bp->vxlan_port) 6362 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6363 if (bp->vxlan_gpe_port) 6364 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6365 if (bp->nge_port) 6366 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6367 6368 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6369 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6370 } 6371 6372 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6373 u32 tpa_flags) 6374 { 6375 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6376 struct hwrm_vnic_tpa_cfg_input *req; 6377 int rc; 6378 6379 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6380 return 0; 6381 6382 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6383 if (rc) 6384 return rc; 6385 6386 if (tpa_flags) { 6387 u16 mss = bp->dev->mtu - 40; 6388 u32 nsegs, n, segs = 0, flags; 6389 6390 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6391 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6392 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6393 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6394 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6395 if (tpa_flags & BNXT_FLAG_GRO) 6396 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6397 6398 req->flags = cpu_to_le32(flags); 6399 6400 req->enables = 6401 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6402 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6403 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6404 6405 /* Number of segs are log2 units, and first packet is not 6406 * included as part of this units. 6407 */ 6408 if (mss <= BNXT_RX_PAGE_SIZE) { 6409 n = BNXT_RX_PAGE_SIZE / mss; 6410 nsegs = (MAX_SKB_FRAGS - 1) * n; 6411 } else { 6412 n = mss / BNXT_RX_PAGE_SIZE; 6413 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6414 n++; 6415 nsegs = (MAX_SKB_FRAGS - n) / n; 6416 } 6417 6418 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6419 segs = MAX_TPA_SEGS_P5; 6420 max_aggs = bp->max_tpa; 6421 } else { 6422 segs = ilog2(nsegs); 6423 } 6424 req->max_agg_segs = cpu_to_le16(segs); 6425 req->max_aggs = cpu_to_le16(max_aggs); 6426 6427 req->min_agg_len = cpu_to_le32(512); 6428 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6429 } 6430 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6431 6432 return hwrm_req_send(bp, req); 6433 } 6434 6435 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6436 { 6437 struct bnxt_ring_grp_info *grp_info; 6438 6439 grp_info = &bp->grp_info[ring->grp_idx]; 6440 return grp_info->cp_fw_ring_id; 6441 } 6442 6443 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6444 { 6445 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6446 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6447 else 6448 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6449 } 6450 6451 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6452 { 6453 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6454 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6455 else 6456 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6457 } 6458 6459 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6460 { 6461 int entries; 6462 6463 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6464 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6465 else 6466 entries = HW_HASH_INDEX_SIZE; 6467 6468 bp->rss_indir_tbl_entries = entries; 6469 bp->rss_indir_tbl = 6470 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6471 if (!bp->rss_indir_tbl) 6472 return -ENOMEM; 6473 6474 return 0; 6475 } 6476 6477 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6478 struct ethtool_rxfh_context *rss_ctx) 6479 { 6480 u16 max_rings, max_entries, pad, i; 6481 u32 *rss_indir_tbl; 6482 6483 if (!bp->rx_nr_rings) 6484 return; 6485 6486 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6487 max_rings = bp->rx_nr_rings - 1; 6488 else 6489 max_rings = bp->rx_nr_rings; 6490 6491 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6492 if (rss_ctx) 6493 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6494 else 6495 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6496 6497 for (i = 0; i < max_entries; i++) 6498 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6499 6500 pad = bp->rss_indir_tbl_entries - max_entries; 6501 if (pad) 6502 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6503 } 6504 6505 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6506 { 6507 u32 i, tbl_size, max_ring = 0; 6508 6509 if (!bp->rss_indir_tbl) 6510 return 0; 6511 6512 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6513 for (i = 0; i < tbl_size; i++) 6514 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6515 return max_ring; 6516 } 6517 6518 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6519 { 6520 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6521 if (!rx_rings) 6522 return 0; 6523 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6524 BNXT_RSS_TABLE_ENTRIES_P5); 6525 } 6526 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6527 return 2; 6528 return 1; 6529 } 6530 6531 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6532 { 6533 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6534 u16 i, j; 6535 6536 /* Fill the RSS indirection table with ring group ids */ 6537 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6538 if (!no_rss) 6539 j = bp->rss_indir_tbl[i]; 6540 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6541 } 6542 } 6543 6544 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6545 struct bnxt_vnic_info *vnic) 6546 { 6547 __le16 *ring_tbl = vnic->rss_table; 6548 struct bnxt_rx_ring_info *rxr; 6549 u16 tbl_size, i; 6550 6551 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6552 6553 for (i = 0; i < tbl_size; i++) { 6554 u16 ring_id, j; 6555 6556 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6557 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6558 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6559 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6560 else 6561 j = bp->rss_indir_tbl[i]; 6562 rxr = &bp->rx_ring[j]; 6563 6564 ring_id = rxr->rx_ring_struct.fw_ring_id; 6565 *ring_tbl++ = cpu_to_le16(ring_id); 6566 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6567 *ring_tbl++ = cpu_to_le16(ring_id); 6568 } 6569 } 6570 6571 static void 6572 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6573 struct bnxt_vnic_info *vnic) 6574 { 6575 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6576 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6577 if (bp->flags & BNXT_FLAG_CHIP_P7) 6578 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6579 } else { 6580 bnxt_fill_hw_rss_tbl(bp, vnic); 6581 } 6582 6583 if (bp->rss_hash_delta) { 6584 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6585 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6586 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6587 else 6588 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6589 } else { 6590 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6591 } 6592 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6593 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6594 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6595 } 6596 6597 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6598 bool set_rss) 6599 { 6600 struct hwrm_vnic_rss_cfg_input *req; 6601 int rc; 6602 6603 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6604 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6605 return 0; 6606 6607 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6608 if (rc) 6609 return rc; 6610 6611 if (set_rss) 6612 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6613 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6614 return hwrm_req_send(bp, req); 6615 } 6616 6617 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6618 struct bnxt_vnic_info *vnic, bool set_rss) 6619 { 6620 struct hwrm_vnic_rss_cfg_input *req; 6621 dma_addr_t ring_tbl_map; 6622 u32 i, nr_ctxs; 6623 int rc; 6624 6625 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6626 if (rc) 6627 return rc; 6628 6629 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6630 if (!set_rss) 6631 return hwrm_req_send(bp, req); 6632 6633 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6634 ring_tbl_map = vnic->rss_table_dma_addr; 6635 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6636 6637 hwrm_req_hold(bp, req); 6638 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6639 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6640 req->ring_table_pair_index = i; 6641 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6642 rc = hwrm_req_send(bp, req); 6643 if (rc) 6644 goto exit; 6645 } 6646 6647 exit: 6648 hwrm_req_drop(bp, req); 6649 return rc; 6650 } 6651 6652 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6653 { 6654 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6655 struct hwrm_vnic_rss_qcfg_output *resp; 6656 struct hwrm_vnic_rss_qcfg_input *req; 6657 6658 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6659 return; 6660 6661 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6662 /* all contexts configured to same hash_type, zero always exists */ 6663 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6664 resp = hwrm_req_hold(bp, req); 6665 if (!hwrm_req_send(bp, req)) { 6666 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6667 bp->rss_hash_delta = 0; 6668 } 6669 hwrm_req_drop(bp, req); 6670 } 6671 6672 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6673 { 6674 u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh; 6675 struct hwrm_vnic_plcmodes_cfg_input *req; 6676 int rc; 6677 6678 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6679 if (rc) 6680 return rc; 6681 6682 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6683 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6684 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6685 6686 if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 6687 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6688 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6689 req->enables |= 6690 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6691 req->hds_threshold = cpu_to_le16(hds_thresh); 6692 } 6693 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6694 return hwrm_req_send(bp, req); 6695 } 6696 6697 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6698 struct bnxt_vnic_info *vnic, 6699 u16 ctx_idx) 6700 { 6701 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6702 6703 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6704 return; 6705 6706 req->rss_cos_lb_ctx_id = 6707 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6708 6709 hwrm_req_send(bp, req); 6710 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6711 } 6712 6713 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6714 { 6715 int i, j; 6716 6717 for (i = 0; i < bp->nr_vnics; i++) { 6718 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6719 6720 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6721 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6722 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6723 } 6724 } 6725 bp->rsscos_nr_ctxs = 0; 6726 } 6727 6728 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6729 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6730 { 6731 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6732 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6733 int rc; 6734 6735 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6736 if (rc) 6737 return rc; 6738 6739 resp = hwrm_req_hold(bp, req); 6740 rc = hwrm_req_send(bp, req); 6741 if (!rc) 6742 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6743 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6744 hwrm_req_drop(bp, req); 6745 6746 return rc; 6747 } 6748 6749 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6750 { 6751 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6752 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6753 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6754 } 6755 6756 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6757 { 6758 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6759 struct hwrm_vnic_cfg_input *req; 6760 unsigned int ring = 0, grp_idx; 6761 u16 def_vlan = 0; 6762 int rc; 6763 6764 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6765 if (rc) 6766 return rc; 6767 6768 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6769 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6770 6771 req->default_rx_ring_id = 6772 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6773 req->default_cmpl_ring_id = 6774 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6775 req->enables = 6776 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6777 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6778 goto vnic_mru; 6779 } 6780 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6781 /* Only RSS support for now TBD: COS & LB */ 6782 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6783 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6784 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6785 VNIC_CFG_REQ_ENABLES_MRU); 6786 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6787 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6788 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6789 VNIC_CFG_REQ_ENABLES_MRU); 6790 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6791 } else { 6792 req->rss_rule = cpu_to_le16(0xffff); 6793 } 6794 6795 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6796 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6797 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6798 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6799 } else { 6800 req->cos_rule = cpu_to_le16(0xffff); 6801 } 6802 6803 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6804 ring = 0; 6805 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6806 ring = vnic->vnic_id - 1; 6807 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6808 ring = bp->rx_nr_rings - 1; 6809 6810 grp_idx = bp->rx_ring[ring].bnapi->index; 6811 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6812 req->lb_rule = cpu_to_le16(0xffff); 6813 vnic_mru: 6814 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 6815 req->mru = cpu_to_le16(vnic->mru); 6816 6817 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6818 #ifdef CONFIG_BNXT_SRIOV 6819 if (BNXT_VF(bp)) 6820 def_vlan = bp->vf.vlan; 6821 #endif 6822 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6823 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6824 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6825 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6826 6827 return hwrm_req_send(bp, req); 6828 } 6829 6830 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6831 struct bnxt_vnic_info *vnic) 6832 { 6833 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6834 struct hwrm_vnic_free_input *req; 6835 6836 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6837 return; 6838 6839 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6840 6841 hwrm_req_send(bp, req); 6842 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6843 } 6844 } 6845 6846 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6847 { 6848 u16 i; 6849 6850 for (i = 0; i < bp->nr_vnics; i++) 6851 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6852 } 6853 6854 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6855 unsigned int start_rx_ring_idx, 6856 unsigned int nr_rings) 6857 { 6858 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6859 struct hwrm_vnic_alloc_output *resp; 6860 struct hwrm_vnic_alloc_input *req; 6861 int rc; 6862 6863 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6864 if (rc) 6865 return rc; 6866 6867 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6868 goto vnic_no_ring_grps; 6869 6870 /* map ring groups to this vnic */ 6871 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6872 grp_idx = bp->rx_ring[i].bnapi->index; 6873 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6874 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6875 j, nr_rings); 6876 break; 6877 } 6878 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6879 } 6880 6881 vnic_no_ring_grps: 6882 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6883 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6884 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6885 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6886 6887 resp = hwrm_req_hold(bp, req); 6888 rc = hwrm_req_send(bp, req); 6889 if (!rc) 6890 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6891 hwrm_req_drop(bp, req); 6892 return rc; 6893 } 6894 6895 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6896 { 6897 struct hwrm_vnic_qcaps_output *resp; 6898 struct hwrm_vnic_qcaps_input *req; 6899 int rc; 6900 6901 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6902 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6903 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6904 if (bp->hwrm_spec_code < 0x10600) 6905 return 0; 6906 6907 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6908 if (rc) 6909 return rc; 6910 6911 resp = hwrm_req_hold(bp, req); 6912 rc = hwrm_req_send(bp, req); 6913 if (!rc) { 6914 u32 flags = le32_to_cpu(resp->flags); 6915 6916 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6917 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6918 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6919 if (flags & 6920 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6921 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6922 6923 /* Older P5 fw before EXT_HW_STATS support did not set 6924 * VLAN_STRIP_CAP properly. 6925 */ 6926 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6927 (BNXT_CHIP_P5(bp) && 6928 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6929 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6930 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6931 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6932 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6933 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6934 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6935 if (bp->max_tpa_v2) { 6936 if (BNXT_CHIP_P5(bp)) 6937 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6938 else 6939 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6940 } 6941 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6942 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6943 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6944 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6945 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6946 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6947 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6948 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6949 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6950 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6951 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6952 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6953 } 6954 hwrm_req_drop(bp, req); 6955 return rc; 6956 } 6957 6958 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6959 { 6960 struct hwrm_ring_grp_alloc_output *resp; 6961 struct hwrm_ring_grp_alloc_input *req; 6962 int rc; 6963 u16 i; 6964 6965 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6966 return 0; 6967 6968 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6969 if (rc) 6970 return rc; 6971 6972 resp = hwrm_req_hold(bp, req); 6973 for (i = 0; i < bp->rx_nr_rings; i++) { 6974 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 6975 6976 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 6977 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 6978 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 6979 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 6980 6981 rc = hwrm_req_send(bp, req); 6982 6983 if (rc) 6984 break; 6985 6986 bp->grp_info[grp_idx].fw_grp_id = 6987 le32_to_cpu(resp->ring_group_id); 6988 } 6989 hwrm_req_drop(bp, req); 6990 return rc; 6991 } 6992 6993 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 6994 { 6995 struct hwrm_ring_grp_free_input *req; 6996 u16 i; 6997 6998 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 6999 return; 7000 7001 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 7002 return; 7003 7004 hwrm_req_hold(bp, req); 7005 for (i = 0; i < bp->cp_nr_rings; i++) { 7006 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 7007 continue; 7008 req->ring_group_id = 7009 cpu_to_le32(bp->grp_info[i].fw_grp_id); 7010 7011 hwrm_req_send(bp, req); 7012 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 7013 } 7014 hwrm_req_drop(bp, req); 7015 } 7016 7017 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type, 7018 struct hwrm_ring_alloc_input *req, 7019 struct bnxt_ring_struct *ring) 7020 { 7021 struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx]; 7022 u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID | 7023 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID; 7024 7025 if (ring_type == HWRM_RING_ALLOC_AGG) { 7026 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 7027 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 7028 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 7029 enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID; 7030 } else { 7031 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 7032 if (NET_IP_ALIGN == 2) 7033 req->flags = 7034 cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD); 7035 } 7036 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7037 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7038 req->enables |= cpu_to_le32(enables); 7039 } 7040 7041 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 7042 struct bnxt_ring_struct *ring, 7043 u32 ring_type, u32 map_index) 7044 { 7045 struct hwrm_ring_alloc_output *resp; 7046 struct hwrm_ring_alloc_input *req; 7047 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 7048 struct bnxt_ring_grp_info *grp_info; 7049 int rc, err = 0; 7050 u16 ring_id; 7051 7052 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 7053 if (rc) 7054 goto exit; 7055 7056 req->enables = 0; 7057 if (rmem->nr_pages > 1) { 7058 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 7059 /* Page size is in log2 units */ 7060 req->page_size = BNXT_PAGE_SHIFT; 7061 req->page_tbl_depth = 1; 7062 } else { 7063 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 7064 } 7065 req->fbo = 0; 7066 /* Association of ring index with doorbell index and MSIX number */ 7067 req->logical_id = cpu_to_le16(map_index); 7068 7069 switch (ring_type) { 7070 case HWRM_RING_ALLOC_TX: { 7071 struct bnxt_tx_ring_info *txr; 7072 u16 flags = 0; 7073 7074 txr = container_of(ring, struct bnxt_tx_ring_info, 7075 tx_ring_struct); 7076 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 7077 /* Association of transmit ring with completion ring */ 7078 grp_info = &bp->grp_info[ring->grp_idx]; 7079 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 7080 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 7081 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7082 req->queue_id = cpu_to_le16(ring->queue_id); 7083 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 7084 req->cmpl_coal_cnt = 7085 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 7086 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 7087 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 7088 req->flags = cpu_to_le16(flags); 7089 break; 7090 } 7091 case HWRM_RING_ALLOC_RX: 7092 case HWRM_RING_ALLOC_AGG: 7093 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 7094 req->length = (ring_type == HWRM_RING_ALLOC_RX) ? 7095 cpu_to_le32(bp->rx_ring_mask + 1) : 7096 cpu_to_le32(bp->rx_agg_ring_mask + 1); 7097 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7098 bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring); 7099 break; 7100 case HWRM_RING_ALLOC_CMPL: 7101 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 7102 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7103 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7104 /* Association of cp ring with nq */ 7105 grp_info = &bp->grp_info[map_index]; 7106 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7107 req->cq_handle = cpu_to_le64(ring->handle); 7108 req->enables |= cpu_to_le32( 7109 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7110 } else { 7111 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7112 } 7113 break; 7114 case HWRM_RING_ALLOC_NQ: 7115 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7116 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7117 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7118 break; 7119 default: 7120 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7121 ring_type); 7122 return -1; 7123 } 7124 7125 resp = hwrm_req_hold(bp, req); 7126 rc = hwrm_req_send(bp, req); 7127 err = le16_to_cpu(resp->error_code); 7128 ring_id = le16_to_cpu(resp->ring_id); 7129 hwrm_req_drop(bp, req); 7130 7131 exit: 7132 if (rc || err) { 7133 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7134 ring_type, rc, err); 7135 return -EIO; 7136 } 7137 ring->fw_ring_id = ring_id; 7138 return rc; 7139 } 7140 7141 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7142 { 7143 int rc; 7144 7145 if (BNXT_PF(bp)) { 7146 struct hwrm_func_cfg_input *req; 7147 7148 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7149 if (rc) 7150 return rc; 7151 7152 req->fid = cpu_to_le16(0xffff); 7153 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7154 req->async_event_cr = cpu_to_le16(idx); 7155 return hwrm_req_send(bp, req); 7156 } else { 7157 struct hwrm_func_vf_cfg_input *req; 7158 7159 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7160 if (rc) 7161 return rc; 7162 7163 req->enables = 7164 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7165 req->async_event_cr = cpu_to_le16(idx); 7166 return hwrm_req_send(bp, req); 7167 } 7168 } 7169 7170 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7171 u32 ring_type) 7172 { 7173 switch (ring_type) { 7174 case HWRM_RING_ALLOC_TX: 7175 db->db_ring_mask = bp->tx_ring_mask; 7176 break; 7177 case HWRM_RING_ALLOC_RX: 7178 db->db_ring_mask = bp->rx_ring_mask; 7179 break; 7180 case HWRM_RING_ALLOC_AGG: 7181 db->db_ring_mask = bp->rx_agg_ring_mask; 7182 break; 7183 case HWRM_RING_ALLOC_CMPL: 7184 case HWRM_RING_ALLOC_NQ: 7185 db->db_ring_mask = bp->cp_ring_mask; 7186 break; 7187 } 7188 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7189 db->db_epoch_mask = db->db_ring_mask + 1; 7190 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7191 } 7192 } 7193 7194 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7195 u32 map_idx, u32 xid) 7196 { 7197 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7198 switch (ring_type) { 7199 case HWRM_RING_ALLOC_TX: 7200 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7201 break; 7202 case HWRM_RING_ALLOC_RX: 7203 case HWRM_RING_ALLOC_AGG: 7204 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7205 break; 7206 case HWRM_RING_ALLOC_CMPL: 7207 db->db_key64 = DBR_PATH_L2; 7208 break; 7209 case HWRM_RING_ALLOC_NQ: 7210 db->db_key64 = DBR_PATH_L2; 7211 break; 7212 } 7213 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7214 7215 if (bp->flags & BNXT_FLAG_CHIP_P7) 7216 db->db_key64 |= DBR_VALID; 7217 7218 db->doorbell = bp->bar1 + bp->db_offset; 7219 } else { 7220 db->doorbell = bp->bar1 + map_idx * 0x80; 7221 switch (ring_type) { 7222 case HWRM_RING_ALLOC_TX: 7223 db->db_key32 = DB_KEY_TX; 7224 break; 7225 case HWRM_RING_ALLOC_RX: 7226 case HWRM_RING_ALLOC_AGG: 7227 db->db_key32 = DB_KEY_RX; 7228 break; 7229 case HWRM_RING_ALLOC_CMPL: 7230 db->db_key32 = DB_KEY_CP; 7231 break; 7232 } 7233 } 7234 bnxt_set_db_mask(bp, db, ring_type); 7235 } 7236 7237 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7238 struct bnxt_rx_ring_info *rxr) 7239 { 7240 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7241 struct bnxt_napi *bnapi = rxr->bnapi; 7242 u32 type = HWRM_RING_ALLOC_RX; 7243 u32 map_idx = bnapi->index; 7244 int rc; 7245 7246 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7247 if (rc) 7248 return rc; 7249 7250 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7251 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7252 7253 return 0; 7254 } 7255 7256 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7257 struct bnxt_rx_ring_info *rxr) 7258 { 7259 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7260 u32 type = HWRM_RING_ALLOC_AGG; 7261 u32 grp_idx = ring->grp_idx; 7262 u32 map_idx; 7263 int rc; 7264 7265 map_idx = grp_idx + bp->rx_nr_rings; 7266 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7267 if (rc) 7268 return rc; 7269 7270 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7271 ring->fw_ring_id); 7272 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7273 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7274 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7275 7276 return 0; 7277 } 7278 7279 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp, 7280 struct bnxt_cp_ring_info *cpr) 7281 { 7282 const u32 type = HWRM_RING_ALLOC_CMPL; 7283 struct bnxt_napi *bnapi = cpr->bnapi; 7284 struct bnxt_ring_struct *ring; 7285 u32 map_idx = bnapi->index; 7286 int rc; 7287 7288 ring = &cpr->cp_ring_struct; 7289 ring->handle = BNXT_SET_NQ_HDL(cpr); 7290 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7291 if (rc) 7292 return rc; 7293 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7294 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7295 return 0; 7296 } 7297 7298 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp, 7299 struct bnxt_tx_ring_info *txr, u32 tx_idx) 7300 { 7301 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7302 const u32 type = HWRM_RING_ALLOC_TX; 7303 int rc; 7304 7305 rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx); 7306 if (rc) 7307 return rc; 7308 bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id); 7309 return 0; 7310 } 7311 7312 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7313 { 7314 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7315 int i, rc = 0; 7316 u32 type; 7317 7318 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7319 type = HWRM_RING_ALLOC_NQ; 7320 else 7321 type = HWRM_RING_ALLOC_CMPL; 7322 for (i = 0; i < bp->cp_nr_rings; i++) { 7323 struct bnxt_napi *bnapi = bp->bnapi[i]; 7324 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7325 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7326 u32 map_idx = ring->map_idx; 7327 unsigned int vector; 7328 7329 vector = bp->irq_tbl[map_idx].vector; 7330 disable_irq_nosync(vector); 7331 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7332 if (rc) { 7333 enable_irq(vector); 7334 goto err_out; 7335 } 7336 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7337 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7338 enable_irq(vector); 7339 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7340 7341 if (!i) { 7342 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7343 if (rc) 7344 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7345 } 7346 } 7347 7348 for (i = 0; i < bp->tx_nr_rings; i++) { 7349 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7350 7351 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7352 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 7353 if (rc) 7354 goto err_out; 7355 } 7356 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i); 7357 if (rc) 7358 goto err_out; 7359 } 7360 7361 for (i = 0; i < bp->rx_nr_rings; i++) { 7362 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7363 7364 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7365 if (rc) 7366 goto err_out; 7367 /* If we have agg rings, post agg buffers first. */ 7368 if (!agg_rings) 7369 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7370 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7371 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 7372 if (rc) 7373 goto err_out; 7374 } 7375 } 7376 7377 if (agg_rings) { 7378 for (i = 0; i < bp->rx_nr_rings; i++) { 7379 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7380 if (rc) 7381 goto err_out; 7382 } 7383 } 7384 err_out: 7385 return rc; 7386 } 7387 7388 static void bnxt_cancel_dim(struct bnxt *bp) 7389 { 7390 int i; 7391 7392 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7393 * if NAPI is enabled. 7394 */ 7395 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7396 return; 7397 7398 /* Make sure NAPI sees that the VNIC is disabled */ 7399 synchronize_net(); 7400 for (i = 0; i < bp->rx_nr_rings; i++) { 7401 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7402 struct bnxt_napi *bnapi = rxr->bnapi; 7403 7404 cancel_work_sync(&bnapi->cp_ring.dim.work); 7405 } 7406 } 7407 7408 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7409 struct bnxt_ring_struct *ring, 7410 u32 ring_type, int cmpl_ring_id) 7411 { 7412 struct hwrm_ring_free_output *resp; 7413 struct hwrm_ring_free_input *req; 7414 u16 error_code = 0; 7415 int rc; 7416 7417 if (BNXT_NO_FW_ACCESS(bp)) 7418 return 0; 7419 7420 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7421 if (rc) 7422 goto exit; 7423 7424 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7425 req->ring_type = ring_type; 7426 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7427 7428 resp = hwrm_req_hold(bp, req); 7429 rc = hwrm_req_send(bp, req); 7430 error_code = le16_to_cpu(resp->error_code); 7431 hwrm_req_drop(bp, req); 7432 exit: 7433 if (rc || error_code) { 7434 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7435 ring_type, rc, error_code); 7436 return -EIO; 7437 } 7438 return 0; 7439 } 7440 7441 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp, 7442 struct bnxt_tx_ring_info *txr, 7443 bool close_path) 7444 { 7445 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7446 u32 cmpl_ring_id; 7447 7448 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7449 return; 7450 7451 cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) : 7452 INVALID_HW_RING_ID; 7453 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX, 7454 cmpl_ring_id); 7455 ring->fw_ring_id = INVALID_HW_RING_ID; 7456 } 7457 7458 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7459 struct bnxt_rx_ring_info *rxr, 7460 bool close_path) 7461 { 7462 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7463 u32 grp_idx = rxr->bnapi->index; 7464 u32 cmpl_ring_id; 7465 7466 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7467 return; 7468 7469 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7470 hwrm_ring_free_send_msg(bp, ring, 7471 RING_FREE_REQ_RING_TYPE_RX, 7472 close_path ? cmpl_ring_id : 7473 INVALID_HW_RING_ID); 7474 ring->fw_ring_id = INVALID_HW_RING_ID; 7475 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7476 } 7477 7478 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7479 struct bnxt_rx_ring_info *rxr, 7480 bool close_path) 7481 { 7482 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7483 u32 grp_idx = rxr->bnapi->index; 7484 u32 type, cmpl_ring_id; 7485 7486 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7487 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7488 else 7489 type = RING_FREE_REQ_RING_TYPE_RX; 7490 7491 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7492 return; 7493 7494 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7495 hwrm_ring_free_send_msg(bp, ring, type, 7496 close_path ? cmpl_ring_id : 7497 INVALID_HW_RING_ID); 7498 ring->fw_ring_id = INVALID_HW_RING_ID; 7499 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7500 } 7501 7502 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp, 7503 struct bnxt_cp_ring_info *cpr) 7504 { 7505 struct bnxt_ring_struct *ring; 7506 7507 ring = &cpr->cp_ring_struct; 7508 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7509 return; 7510 7511 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL, 7512 INVALID_HW_RING_ID); 7513 ring->fw_ring_id = INVALID_HW_RING_ID; 7514 } 7515 7516 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 7517 { 7518 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7519 int i, size = ring->ring_mem.page_size; 7520 7521 cpr->cp_raw_cons = 0; 7522 cpr->toggle = 0; 7523 7524 for (i = 0; i < bp->cp_nr_pages; i++) 7525 if (cpr->cp_desc_ring[i]) 7526 memset(cpr->cp_desc_ring[i], 0, size); 7527 } 7528 7529 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7530 { 7531 u32 type; 7532 int i; 7533 7534 if (!bp->bnapi) 7535 return; 7536 7537 for (i = 0; i < bp->tx_nr_rings; i++) 7538 bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path); 7539 7540 bnxt_cancel_dim(bp); 7541 for (i = 0; i < bp->rx_nr_rings; i++) { 7542 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7543 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7544 } 7545 7546 /* The completion rings are about to be freed. After that the 7547 * IRQ doorbell will not work anymore. So we need to disable 7548 * IRQ here. 7549 */ 7550 bnxt_disable_int_sync(bp); 7551 7552 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7553 type = RING_FREE_REQ_RING_TYPE_NQ; 7554 else 7555 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7556 for (i = 0; i < bp->cp_nr_rings; i++) { 7557 struct bnxt_napi *bnapi = bp->bnapi[i]; 7558 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7559 struct bnxt_ring_struct *ring; 7560 int j; 7561 7562 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) 7563 bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]); 7564 7565 ring = &cpr->cp_ring_struct; 7566 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7567 hwrm_ring_free_send_msg(bp, ring, type, 7568 INVALID_HW_RING_ID); 7569 ring->fw_ring_id = INVALID_HW_RING_ID; 7570 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7571 } 7572 } 7573 } 7574 7575 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7576 bool shared); 7577 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7578 bool shared); 7579 7580 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7581 { 7582 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7583 struct hwrm_func_qcfg_output *resp; 7584 struct hwrm_func_qcfg_input *req; 7585 int rc; 7586 7587 if (bp->hwrm_spec_code < 0x10601) 7588 return 0; 7589 7590 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7591 if (rc) 7592 return rc; 7593 7594 req->fid = cpu_to_le16(0xffff); 7595 resp = hwrm_req_hold(bp, req); 7596 rc = hwrm_req_send(bp, req); 7597 if (rc) { 7598 hwrm_req_drop(bp, req); 7599 return rc; 7600 } 7601 7602 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7603 if (BNXT_NEW_RM(bp)) { 7604 u16 cp, stats; 7605 7606 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7607 hw_resc->resv_hw_ring_grps = 7608 le32_to_cpu(resp->alloc_hw_ring_grps); 7609 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7610 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7611 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7612 stats = le16_to_cpu(resp->alloc_stat_ctx); 7613 hw_resc->resv_irqs = cp; 7614 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7615 int rx = hw_resc->resv_rx_rings; 7616 int tx = hw_resc->resv_tx_rings; 7617 7618 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7619 rx >>= 1; 7620 if (cp < (rx + tx)) { 7621 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7622 if (rc) 7623 goto get_rings_exit; 7624 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7625 rx <<= 1; 7626 hw_resc->resv_rx_rings = rx; 7627 hw_resc->resv_tx_rings = tx; 7628 } 7629 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7630 hw_resc->resv_hw_ring_grps = rx; 7631 } 7632 hw_resc->resv_cp_rings = cp; 7633 hw_resc->resv_stat_ctxs = stats; 7634 } 7635 get_rings_exit: 7636 hwrm_req_drop(bp, req); 7637 return rc; 7638 } 7639 7640 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7641 { 7642 struct hwrm_func_qcfg_output *resp; 7643 struct hwrm_func_qcfg_input *req; 7644 int rc; 7645 7646 if (bp->hwrm_spec_code < 0x10601) 7647 return 0; 7648 7649 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7650 if (rc) 7651 return rc; 7652 7653 req->fid = cpu_to_le16(fid); 7654 resp = hwrm_req_hold(bp, req); 7655 rc = hwrm_req_send(bp, req); 7656 if (!rc) 7657 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7658 7659 hwrm_req_drop(bp, req); 7660 return rc; 7661 } 7662 7663 static bool bnxt_rfs_supported(struct bnxt *bp); 7664 7665 static struct hwrm_func_cfg_input * 7666 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7667 { 7668 struct hwrm_func_cfg_input *req; 7669 u32 enables = 0; 7670 7671 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7672 return NULL; 7673 7674 req->fid = cpu_to_le16(0xffff); 7675 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7676 req->num_tx_rings = cpu_to_le16(hwr->tx); 7677 if (BNXT_NEW_RM(bp)) { 7678 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7679 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7680 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7681 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7682 enables |= hwr->cp_p5 ? 7683 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7684 } else { 7685 enables |= hwr->cp ? 7686 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7687 enables |= hwr->grp ? 7688 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7689 } 7690 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7691 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7692 0; 7693 req->num_rx_rings = cpu_to_le16(hwr->rx); 7694 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7695 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7696 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7697 req->num_msix = cpu_to_le16(hwr->cp); 7698 } else { 7699 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7700 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7701 } 7702 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7703 req->num_vnics = cpu_to_le16(hwr->vnic); 7704 } 7705 req->enables = cpu_to_le32(enables); 7706 return req; 7707 } 7708 7709 static struct hwrm_func_vf_cfg_input * 7710 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7711 { 7712 struct hwrm_func_vf_cfg_input *req; 7713 u32 enables = 0; 7714 7715 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7716 return NULL; 7717 7718 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7719 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7720 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7721 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7722 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7723 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7724 enables |= hwr->cp_p5 ? 7725 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7726 } else { 7727 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7728 enables |= hwr->grp ? 7729 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7730 } 7731 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7732 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7733 7734 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7735 req->num_tx_rings = cpu_to_le16(hwr->tx); 7736 req->num_rx_rings = cpu_to_le16(hwr->rx); 7737 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7738 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7739 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7740 } else { 7741 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7742 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7743 } 7744 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7745 req->num_vnics = cpu_to_le16(hwr->vnic); 7746 7747 req->enables = cpu_to_le32(enables); 7748 return req; 7749 } 7750 7751 static int 7752 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7753 { 7754 struct hwrm_func_cfg_input *req; 7755 int rc; 7756 7757 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7758 if (!req) 7759 return -ENOMEM; 7760 7761 if (!req->enables) { 7762 hwrm_req_drop(bp, req); 7763 return 0; 7764 } 7765 7766 rc = hwrm_req_send(bp, req); 7767 if (rc) 7768 return rc; 7769 7770 if (bp->hwrm_spec_code < 0x10601) 7771 bp->hw_resc.resv_tx_rings = hwr->tx; 7772 7773 return bnxt_hwrm_get_rings(bp); 7774 } 7775 7776 static int 7777 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7778 { 7779 struct hwrm_func_vf_cfg_input *req; 7780 int rc; 7781 7782 if (!BNXT_NEW_RM(bp)) { 7783 bp->hw_resc.resv_tx_rings = hwr->tx; 7784 return 0; 7785 } 7786 7787 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7788 if (!req) 7789 return -ENOMEM; 7790 7791 rc = hwrm_req_send(bp, req); 7792 if (rc) 7793 return rc; 7794 7795 return bnxt_hwrm_get_rings(bp); 7796 } 7797 7798 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7799 { 7800 if (BNXT_PF(bp)) 7801 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7802 else 7803 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7804 } 7805 7806 int bnxt_nq_rings_in_use(struct bnxt *bp) 7807 { 7808 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7809 } 7810 7811 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7812 { 7813 int cp; 7814 7815 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7816 return bnxt_nq_rings_in_use(bp); 7817 7818 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7819 return cp; 7820 } 7821 7822 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7823 { 7824 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7825 } 7826 7827 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7828 { 7829 if (!hwr->grp) 7830 return 0; 7831 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7832 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7833 7834 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7835 rss_ctx *= hwr->vnic; 7836 return rss_ctx; 7837 } 7838 if (BNXT_VF(bp)) 7839 return BNXT_VF_MAX_RSS_CTX; 7840 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7841 return hwr->grp + 1; 7842 return 1; 7843 } 7844 7845 /* Check if a default RSS map needs to be setup. This function is only 7846 * used on older firmware that does not require reserving RX rings. 7847 */ 7848 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7849 { 7850 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7851 7852 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7853 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7854 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7855 if (!netif_is_rxfh_configured(bp->dev)) 7856 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7857 } 7858 } 7859 7860 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7861 { 7862 if (bp->flags & BNXT_FLAG_RFS) { 7863 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7864 return 2 + bp->num_rss_ctx; 7865 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7866 return rx_rings + 1; 7867 } 7868 return 1; 7869 } 7870 7871 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7872 { 7873 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7874 int cp = bnxt_cp_rings_in_use(bp); 7875 int nq = bnxt_nq_rings_in_use(bp); 7876 int rx = bp->rx_nr_rings, stat; 7877 int vnic, grp = rx; 7878 7879 /* Old firmware does not need RX ring reservations but we still 7880 * need to setup a default RSS map when needed. With new firmware 7881 * we go through RX ring reservations first and then set up the 7882 * RSS map for the successfully reserved RX rings when needed. 7883 */ 7884 if (!BNXT_NEW_RM(bp)) 7885 bnxt_check_rss_tbl_no_rmgr(bp); 7886 7887 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7888 bp->hwrm_spec_code >= 0x10601) 7889 return true; 7890 7891 if (!BNXT_NEW_RM(bp)) 7892 return false; 7893 7894 vnic = bnxt_get_total_vnics(bp, rx); 7895 7896 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7897 rx <<= 1; 7898 stat = bnxt_get_func_stat_ctxs(bp); 7899 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7900 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7901 (hw_resc->resv_hw_ring_grps != grp && 7902 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7903 return true; 7904 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7905 hw_resc->resv_irqs != nq) 7906 return true; 7907 return false; 7908 } 7909 7910 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7911 { 7912 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7913 7914 hwr->tx = hw_resc->resv_tx_rings; 7915 if (BNXT_NEW_RM(bp)) { 7916 hwr->rx = hw_resc->resv_rx_rings; 7917 hwr->cp = hw_resc->resv_irqs; 7918 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7919 hwr->cp_p5 = hw_resc->resv_cp_rings; 7920 hwr->grp = hw_resc->resv_hw_ring_grps; 7921 hwr->vnic = hw_resc->resv_vnics; 7922 hwr->stat = hw_resc->resv_stat_ctxs; 7923 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7924 } 7925 } 7926 7927 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7928 { 7929 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7930 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7931 } 7932 7933 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7934 7935 static int __bnxt_reserve_rings(struct bnxt *bp) 7936 { 7937 struct bnxt_hw_rings hwr = {0}; 7938 int rx_rings, old_rx_rings, rc; 7939 int cp = bp->cp_nr_rings; 7940 int ulp_msix = 0; 7941 bool sh = false; 7942 int tx_cp; 7943 7944 if (!bnxt_need_reserve_rings(bp)) 7945 return 0; 7946 7947 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7948 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7949 if (!ulp_msix) 7950 bnxt_set_ulp_stat_ctxs(bp, 0); 7951 7952 if (ulp_msix > bp->ulp_num_msix_want) 7953 ulp_msix = bp->ulp_num_msix_want; 7954 hwr.cp = cp + ulp_msix; 7955 } else { 7956 hwr.cp = bnxt_nq_rings_in_use(bp); 7957 } 7958 7959 hwr.tx = bp->tx_nr_rings; 7960 hwr.rx = bp->rx_nr_rings; 7961 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7962 sh = true; 7963 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7964 hwr.cp_p5 = hwr.rx + hwr.tx; 7965 7966 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7967 7968 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7969 hwr.rx <<= 1; 7970 hwr.grp = bp->rx_nr_rings; 7971 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 7972 hwr.stat = bnxt_get_func_stat_ctxs(bp); 7973 old_rx_rings = bp->hw_resc.resv_rx_rings; 7974 7975 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 7976 if (rc) 7977 return rc; 7978 7979 bnxt_copy_reserved_rings(bp, &hwr); 7980 7981 rx_rings = hwr.rx; 7982 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 7983 if (hwr.rx >= 2) { 7984 rx_rings = hwr.rx >> 1; 7985 } else { 7986 if (netif_running(bp->dev)) 7987 return -ENOMEM; 7988 7989 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 7990 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 7991 bp->dev->hw_features &= ~NETIF_F_LRO; 7992 bp->dev->features &= ~NETIF_F_LRO; 7993 bnxt_set_ring_params(bp); 7994 } 7995 } 7996 rx_rings = min_t(int, rx_rings, hwr.grp); 7997 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 7998 if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 7999 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 8000 hwr.cp = min_t(int, hwr.cp, hwr.stat); 8001 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 8002 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8003 hwr.rx = rx_rings << 1; 8004 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 8005 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 8006 bp->tx_nr_rings = hwr.tx; 8007 8008 /* If we cannot reserve all the RX rings, reset the RSS map only 8009 * if absolutely necessary 8010 */ 8011 if (rx_rings != bp->rx_nr_rings) { 8012 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 8013 rx_rings, bp->rx_nr_rings); 8014 if (netif_is_rxfh_configured(bp->dev) && 8015 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 8016 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 8017 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 8018 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 8019 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 8020 } 8021 } 8022 bp->rx_nr_rings = rx_rings; 8023 bp->cp_nr_rings = hwr.cp; 8024 8025 if (!bnxt_rings_ok(bp, &hwr)) 8026 return -ENOMEM; 8027 8028 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 8029 !netif_is_rxfh_configured(bp->dev)) 8030 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8031 8032 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 8033 int resv_msix, resv_ctx, ulp_ctxs; 8034 struct bnxt_hw_resc *hw_resc; 8035 8036 hw_resc = &bp->hw_resc; 8037 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 8038 ulp_msix = min_t(int, resv_msix, ulp_msix); 8039 bnxt_set_ulp_msix_num(bp, ulp_msix); 8040 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 8041 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 8042 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 8043 } 8044 8045 return rc; 8046 } 8047 8048 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8049 { 8050 struct hwrm_func_vf_cfg_input *req; 8051 u32 flags; 8052 8053 if (!BNXT_NEW_RM(bp)) 8054 return 0; 8055 8056 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8057 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 8058 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8059 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8060 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8061 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 8062 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 8063 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8064 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8065 8066 req->flags = cpu_to_le32(flags); 8067 return hwrm_req_send_silent(bp, req); 8068 } 8069 8070 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8071 { 8072 struct hwrm_func_cfg_input *req; 8073 u32 flags; 8074 8075 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 8076 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 8077 if (BNXT_NEW_RM(bp)) { 8078 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8079 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8080 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8081 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 8082 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8083 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 8084 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 8085 else 8086 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8087 } 8088 8089 req->flags = cpu_to_le32(flags); 8090 return hwrm_req_send_silent(bp, req); 8091 } 8092 8093 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8094 { 8095 if (bp->hwrm_spec_code < 0x10801) 8096 return 0; 8097 8098 if (BNXT_PF(bp)) 8099 return bnxt_hwrm_check_pf_rings(bp, hwr); 8100 8101 return bnxt_hwrm_check_vf_rings(bp, hwr); 8102 } 8103 8104 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 8105 { 8106 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8107 struct hwrm_ring_aggint_qcaps_output *resp; 8108 struct hwrm_ring_aggint_qcaps_input *req; 8109 int rc; 8110 8111 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 8112 coal_cap->num_cmpl_dma_aggr_max = 63; 8113 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 8114 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 8115 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 8116 coal_cap->int_lat_tmr_min_max = 65535; 8117 coal_cap->int_lat_tmr_max_max = 65535; 8118 coal_cap->num_cmpl_aggr_int_max = 65535; 8119 coal_cap->timer_units = 80; 8120 8121 if (bp->hwrm_spec_code < 0x10902) 8122 return; 8123 8124 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 8125 return; 8126 8127 resp = hwrm_req_hold(bp, req); 8128 rc = hwrm_req_send_silent(bp, req); 8129 if (!rc) { 8130 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 8131 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 8132 coal_cap->num_cmpl_dma_aggr_max = 8133 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 8134 coal_cap->num_cmpl_dma_aggr_during_int_max = 8135 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8136 coal_cap->cmpl_aggr_dma_tmr_max = 8137 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8138 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8139 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8140 coal_cap->int_lat_tmr_min_max = 8141 le16_to_cpu(resp->int_lat_tmr_min_max); 8142 coal_cap->int_lat_tmr_max_max = 8143 le16_to_cpu(resp->int_lat_tmr_max_max); 8144 coal_cap->num_cmpl_aggr_int_max = 8145 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8146 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8147 } 8148 hwrm_req_drop(bp, req); 8149 } 8150 8151 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8152 { 8153 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8154 8155 return usec * 1000 / coal_cap->timer_units; 8156 } 8157 8158 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8159 struct bnxt_coal *hw_coal, 8160 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8161 { 8162 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8163 u16 val, tmr, max, flags = hw_coal->flags; 8164 u32 cmpl_params = coal_cap->cmpl_params; 8165 8166 max = hw_coal->bufs_per_record * 128; 8167 if (hw_coal->budget) 8168 max = hw_coal->bufs_per_record * hw_coal->budget; 8169 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8170 8171 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8172 req->num_cmpl_aggr_int = cpu_to_le16(val); 8173 8174 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8175 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8176 8177 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8178 coal_cap->num_cmpl_dma_aggr_during_int_max); 8179 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8180 8181 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8182 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8183 req->int_lat_tmr_max = cpu_to_le16(tmr); 8184 8185 /* min timer set to 1/2 of interrupt timer */ 8186 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8187 val = tmr / 2; 8188 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8189 req->int_lat_tmr_min = cpu_to_le16(val); 8190 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8191 } 8192 8193 /* buf timer set to 1/4 of interrupt timer */ 8194 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8195 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8196 8197 if (cmpl_params & 8198 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8199 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8200 val = clamp_t(u16, tmr, 1, 8201 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8202 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8203 req->enables |= 8204 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8205 } 8206 8207 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8208 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8209 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8210 req->flags = cpu_to_le16(flags); 8211 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8212 } 8213 8214 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8215 struct bnxt_coal *hw_coal) 8216 { 8217 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8218 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8219 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8220 u32 nq_params = coal_cap->nq_params; 8221 u16 tmr; 8222 int rc; 8223 8224 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8225 return 0; 8226 8227 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8228 if (rc) 8229 return rc; 8230 8231 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8232 req->flags = 8233 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8234 8235 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8236 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8237 req->int_lat_tmr_min = cpu_to_le16(tmr); 8238 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8239 return hwrm_req_send(bp, req); 8240 } 8241 8242 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8243 { 8244 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8245 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8246 struct bnxt_coal coal; 8247 int rc; 8248 8249 /* Tick values in micro seconds. 8250 * 1 coal_buf x bufs_per_record = 1 completion record. 8251 */ 8252 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8253 8254 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8255 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8256 8257 if (!bnapi->rx_ring) 8258 return -ENODEV; 8259 8260 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8261 if (rc) 8262 return rc; 8263 8264 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8265 8266 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8267 8268 return hwrm_req_send(bp, req_rx); 8269 } 8270 8271 static int 8272 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8273 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8274 { 8275 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8276 8277 req->ring_id = cpu_to_le16(ring_id); 8278 return hwrm_req_send(bp, req); 8279 } 8280 8281 static int 8282 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8283 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8284 { 8285 struct bnxt_tx_ring_info *txr; 8286 int i, rc; 8287 8288 bnxt_for_each_napi_tx(i, bnapi, txr) { 8289 u16 ring_id; 8290 8291 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8292 req->ring_id = cpu_to_le16(ring_id); 8293 rc = hwrm_req_send(bp, req); 8294 if (rc) 8295 return rc; 8296 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8297 return 0; 8298 } 8299 return 0; 8300 } 8301 8302 int bnxt_hwrm_set_coal(struct bnxt *bp) 8303 { 8304 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8305 int i, rc; 8306 8307 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8308 if (rc) 8309 return rc; 8310 8311 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8312 if (rc) { 8313 hwrm_req_drop(bp, req_rx); 8314 return rc; 8315 } 8316 8317 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8318 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8319 8320 hwrm_req_hold(bp, req_rx); 8321 hwrm_req_hold(bp, req_tx); 8322 for (i = 0; i < bp->cp_nr_rings; i++) { 8323 struct bnxt_napi *bnapi = bp->bnapi[i]; 8324 struct bnxt_coal *hw_coal; 8325 8326 if (!bnapi->rx_ring) 8327 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8328 else 8329 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8330 if (rc) 8331 break; 8332 8333 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8334 continue; 8335 8336 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8337 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8338 if (rc) 8339 break; 8340 } 8341 if (bnapi->rx_ring) 8342 hw_coal = &bp->rx_coal; 8343 else 8344 hw_coal = &bp->tx_coal; 8345 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8346 } 8347 hwrm_req_drop(bp, req_rx); 8348 hwrm_req_drop(bp, req_tx); 8349 return rc; 8350 } 8351 8352 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8353 { 8354 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8355 struct hwrm_stat_ctx_free_input *req; 8356 int i; 8357 8358 if (!bp->bnapi) 8359 return; 8360 8361 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8362 return; 8363 8364 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8365 return; 8366 if (BNXT_FW_MAJ(bp) <= 20) { 8367 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8368 hwrm_req_drop(bp, req); 8369 return; 8370 } 8371 hwrm_req_hold(bp, req0); 8372 } 8373 hwrm_req_hold(bp, req); 8374 for (i = 0; i < bp->cp_nr_rings; i++) { 8375 struct bnxt_napi *bnapi = bp->bnapi[i]; 8376 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8377 8378 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8379 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8380 if (req0) { 8381 req0->stat_ctx_id = req->stat_ctx_id; 8382 hwrm_req_send(bp, req0); 8383 } 8384 hwrm_req_send(bp, req); 8385 8386 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8387 } 8388 } 8389 hwrm_req_drop(bp, req); 8390 if (req0) 8391 hwrm_req_drop(bp, req0); 8392 } 8393 8394 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8395 { 8396 struct hwrm_stat_ctx_alloc_output *resp; 8397 struct hwrm_stat_ctx_alloc_input *req; 8398 int rc, i; 8399 8400 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8401 return 0; 8402 8403 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8404 if (rc) 8405 return rc; 8406 8407 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8408 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8409 8410 resp = hwrm_req_hold(bp, req); 8411 for (i = 0; i < bp->cp_nr_rings; i++) { 8412 struct bnxt_napi *bnapi = bp->bnapi[i]; 8413 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8414 8415 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8416 8417 rc = hwrm_req_send(bp, req); 8418 if (rc) 8419 break; 8420 8421 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8422 8423 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8424 } 8425 hwrm_req_drop(bp, req); 8426 return rc; 8427 } 8428 8429 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8430 { 8431 struct hwrm_func_qcfg_output *resp; 8432 struct hwrm_func_qcfg_input *req; 8433 u16 flags; 8434 int rc; 8435 8436 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8437 if (rc) 8438 return rc; 8439 8440 req->fid = cpu_to_le16(0xffff); 8441 resp = hwrm_req_hold(bp, req); 8442 rc = hwrm_req_send(bp, req); 8443 if (rc) 8444 goto func_qcfg_exit; 8445 8446 flags = le16_to_cpu(resp->flags); 8447 #ifdef CONFIG_BNXT_SRIOV 8448 if (BNXT_VF(bp)) { 8449 struct bnxt_vf_info *vf = &bp->vf; 8450 8451 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8452 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8453 vf->flags |= BNXT_VF_TRUST; 8454 else 8455 vf->flags &= ~BNXT_VF_TRUST; 8456 } else { 8457 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8458 } 8459 #endif 8460 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8461 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8462 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8463 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8464 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8465 } 8466 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8467 bp->flags |= BNXT_FLAG_MULTI_HOST; 8468 8469 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8470 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8471 8472 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8473 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8474 8475 switch (resp->port_partition_type) { 8476 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8477 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2: 8478 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8479 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8480 bp->port_partition_type = resp->port_partition_type; 8481 break; 8482 } 8483 if (bp->hwrm_spec_code < 0x10707 || 8484 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8485 bp->br_mode = BRIDGE_MODE_VEB; 8486 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8487 bp->br_mode = BRIDGE_MODE_VEPA; 8488 else 8489 bp->br_mode = BRIDGE_MODE_UNDEF; 8490 8491 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8492 if (!bp->max_mtu) 8493 bp->max_mtu = BNXT_MAX_MTU; 8494 8495 if (bp->db_size) 8496 goto func_qcfg_exit; 8497 8498 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8499 if (BNXT_CHIP_P5(bp)) { 8500 if (BNXT_PF(bp)) 8501 bp->db_offset = DB_PF_OFFSET_P5; 8502 else 8503 bp->db_offset = DB_VF_OFFSET_P5; 8504 } 8505 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8506 1024); 8507 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8508 bp->db_size <= bp->db_offset) 8509 bp->db_size = pci_resource_len(bp->pdev, 2); 8510 8511 func_qcfg_exit: 8512 hwrm_req_drop(bp, req); 8513 return rc; 8514 } 8515 8516 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8517 u8 init_val, u8 init_offset, 8518 bool init_mask_set) 8519 { 8520 ctxm->init_value = init_val; 8521 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8522 if (init_mask_set) 8523 ctxm->init_offset = init_offset * 4; 8524 else 8525 ctxm->init_value = 0; 8526 } 8527 8528 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8529 { 8530 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8531 u16 type; 8532 8533 for (type = 0; type < ctx_max; type++) { 8534 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8535 int n = 1; 8536 8537 if (!ctxm->max_entries || ctxm->pg_info) 8538 continue; 8539 8540 if (ctxm->instance_bmap) 8541 n = hweight32(ctxm->instance_bmap); 8542 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8543 if (!ctxm->pg_info) 8544 return -ENOMEM; 8545 } 8546 return 0; 8547 } 8548 8549 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8550 struct bnxt_ctx_mem_type *ctxm, bool force); 8551 8552 #define BNXT_CTX_INIT_VALID(flags) \ 8553 (!!((flags) & \ 8554 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8555 8556 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8557 { 8558 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8559 struct hwrm_func_backing_store_qcaps_v2_input *req; 8560 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8561 u16 type; 8562 int rc; 8563 8564 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8565 if (rc) 8566 return rc; 8567 8568 if (!ctx) { 8569 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8570 if (!ctx) 8571 return -ENOMEM; 8572 bp->ctx = ctx; 8573 } 8574 8575 resp = hwrm_req_hold(bp, req); 8576 8577 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8578 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8579 u8 init_val, init_off, i; 8580 u32 max_entries; 8581 u16 entry_size; 8582 __le32 *p; 8583 u32 flags; 8584 8585 req->type = cpu_to_le16(type); 8586 rc = hwrm_req_send(bp, req); 8587 if (rc) 8588 goto ctx_done; 8589 flags = le32_to_cpu(resp->flags); 8590 type = le16_to_cpu(resp->next_valid_type); 8591 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8592 bnxt_free_one_ctx_mem(bp, ctxm, true); 8593 continue; 8594 } 8595 entry_size = le16_to_cpu(resp->entry_size); 8596 max_entries = le32_to_cpu(resp->max_num_entries); 8597 if (ctxm->mem_valid) { 8598 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8599 ctxm->entry_size != entry_size || 8600 ctxm->max_entries != max_entries) 8601 bnxt_free_one_ctx_mem(bp, ctxm, true); 8602 else 8603 continue; 8604 } 8605 ctxm->type = le16_to_cpu(resp->type); 8606 ctxm->entry_size = entry_size; 8607 ctxm->flags = flags; 8608 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8609 ctxm->entry_multiple = resp->entry_multiple; 8610 ctxm->max_entries = max_entries; 8611 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8612 init_val = resp->ctx_init_value; 8613 init_off = resp->ctx_init_offset; 8614 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8615 BNXT_CTX_INIT_VALID(flags)); 8616 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8617 BNXT_MAX_SPLIT_ENTRY); 8618 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8619 i++, p++) 8620 ctxm->split[i] = le32_to_cpu(*p); 8621 } 8622 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8623 8624 ctx_done: 8625 hwrm_req_drop(bp, req); 8626 return rc; 8627 } 8628 8629 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8630 { 8631 struct hwrm_func_backing_store_qcaps_output *resp; 8632 struct hwrm_func_backing_store_qcaps_input *req; 8633 int rc; 8634 8635 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8636 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8637 return 0; 8638 8639 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8640 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8641 8642 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8643 if (rc) 8644 return rc; 8645 8646 resp = hwrm_req_hold(bp, req); 8647 rc = hwrm_req_send_silent(bp, req); 8648 if (!rc) { 8649 struct bnxt_ctx_mem_type *ctxm; 8650 struct bnxt_ctx_mem_info *ctx; 8651 u8 init_val, init_idx = 0; 8652 u16 init_mask; 8653 8654 ctx = bp->ctx; 8655 if (!ctx) { 8656 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8657 if (!ctx) { 8658 rc = -ENOMEM; 8659 goto ctx_err; 8660 } 8661 bp->ctx = ctx; 8662 } 8663 init_val = resp->ctx_kind_initializer; 8664 init_mask = le16_to_cpu(resp->ctx_init_mask); 8665 8666 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8667 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8668 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8669 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8670 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8671 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8672 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8673 (init_mask & (1 << init_idx++)) != 0); 8674 8675 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8676 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8677 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8678 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8679 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8680 (init_mask & (1 << init_idx++)) != 0); 8681 8682 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8683 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8684 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8685 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8686 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8687 (init_mask & (1 << init_idx++)) != 0); 8688 8689 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8690 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8691 ctxm->max_entries = ctxm->vnic_entries + 8692 le16_to_cpu(resp->vnic_max_ring_table_entries); 8693 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8694 bnxt_init_ctx_initializer(ctxm, init_val, 8695 resp->vnic_init_offset, 8696 (init_mask & (1 << init_idx++)) != 0); 8697 8698 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8699 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8700 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8701 bnxt_init_ctx_initializer(ctxm, init_val, 8702 resp->stat_init_offset, 8703 (init_mask & (1 << init_idx++)) != 0); 8704 8705 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8706 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8707 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8708 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8709 ctxm->entry_multiple = resp->tqm_entries_multiple; 8710 if (!ctxm->entry_multiple) 8711 ctxm->entry_multiple = 1; 8712 8713 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8714 8715 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8716 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8717 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8718 ctxm->mrav_num_entries_units = 8719 le16_to_cpu(resp->mrav_num_entries_units); 8720 bnxt_init_ctx_initializer(ctxm, init_val, 8721 resp->mrav_init_offset, 8722 (init_mask & (1 << init_idx++)) != 0); 8723 8724 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8725 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8726 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8727 8728 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8729 if (!ctx->tqm_fp_rings_count) 8730 ctx->tqm_fp_rings_count = bp->max_q; 8731 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8732 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8733 8734 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8735 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8736 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8737 8738 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8739 } else { 8740 rc = 0; 8741 } 8742 ctx_err: 8743 hwrm_req_drop(bp, req); 8744 return rc; 8745 } 8746 8747 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8748 __le64 *pg_dir) 8749 { 8750 if (!rmem->nr_pages) 8751 return; 8752 8753 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8754 if (rmem->depth >= 1) { 8755 if (rmem->depth == 2) 8756 *pg_attr |= 2; 8757 else 8758 *pg_attr |= 1; 8759 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8760 } else { 8761 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8762 } 8763 } 8764 8765 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8766 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8767 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8768 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8769 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8770 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8771 8772 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8773 { 8774 struct hwrm_func_backing_store_cfg_input *req; 8775 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8776 struct bnxt_ctx_pg_info *ctx_pg; 8777 struct bnxt_ctx_mem_type *ctxm; 8778 void **__req = (void **)&req; 8779 u32 req_len = sizeof(*req); 8780 __le32 *num_entries; 8781 __le64 *pg_dir; 8782 u32 flags = 0; 8783 u8 *pg_attr; 8784 u32 ena; 8785 int rc; 8786 int i; 8787 8788 if (!ctx) 8789 return 0; 8790 8791 if (req_len > bp->hwrm_max_ext_req_len) 8792 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8793 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8794 if (rc) 8795 return rc; 8796 8797 req->enables = cpu_to_le32(enables); 8798 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8799 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8800 ctx_pg = ctxm->pg_info; 8801 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8802 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8803 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8804 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8805 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8806 &req->qpc_pg_size_qpc_lvl, 8807 &req->qpc_page_dir); 8808 8809 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8810 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8811 } 8812 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8813 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8814 ctx_pg = ctxm->pg_info; 8815 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8816 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8817 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8818 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8819 &req->srq_pg_size_srq_lvl, 8820 &req->srq_page_dir); 8821 } 8822 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8823 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8824 ctx_pg = ctxm->pg_info; 8825 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8826 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8827 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8828 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8829 &req->cq_pg_size_cq_lvl, 8830 &req->cq_page_dir); 8831 } 8832 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8833 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8834 ctx_pg = ctxm->pg_info; 8835 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8836 req->vnic_num_ring_table_entries = 8837 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8838 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8839 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8840 &req->vnic_pg_size_vnic_lvl, 8841 &req->vnic_page_dir); 8842 } 8843 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8844 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8845 ctx_pg = ctxm->pg_info; 8846 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8847 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8848 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8849 &req->stat_pg_size_stat_lvl, 8850 &req->stat_page_dir); 8851 } 8852 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8853 u32 units; 8854 8855 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8856 ctx_pg = ctxm->pg_info; 8857 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8858 units = ctxm->mrav_num_entries_units; 8859 if (units) { 8860 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8861 u32 entries; 8862 8863 num_mr = ctx_pg->entries - num_ah; 8864 entries = ((num_mr / units) << 16) | (num_ah / units); 8865 req->mrav_num_entries = cpu_to_le32(entries); 8866 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8867 } 8868 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8869 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8870 &req->mrav_pg_size_mrav_lvl, 8871 &req->mrav_page_dir); 8872 } 8873 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8874 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8875 ctx_pg = ctxm->pg_info; 8876 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8877 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8878 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8879 &req->tim_pg_size_tim_lvl, 8880 &req->tim_page_dir); 8881 } 8882 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8883 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8884 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8885 pg_dir = &req->tqm_sp_page_dir, 8886 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8887 ctx_pg = ctxm->pg_info; 8888 i < BNXT_MAX_TQM_RINGS; 8889 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8890 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8891 if (!(enables & ena)) 8892 continue; 8893 8894 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8895 *num_entries = cpu_to_le32(ctx_pg->entries); 8896 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8897 } 8898 req->flags = cpu_to_le32(flags); 8899 return hwrm_req_send(bp, req); 8900 } 8901 8902 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8903 struct bnxt_ctx_pg_info *ctx_pg) 8904 { 8905 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8906 8907 rmem->page_size = BNXT_PAGE_SIZE; 8908 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8909 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8910 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8911 if (rmem->depth >= 1) 8912 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8913 return bnxt_alloc_ring(bp, rmem); 8914 } 8915 8916 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8917 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8918 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8919 { 8920 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8921 int rc; 8922 8923 if (!mem_size) 8924 return -EINVAL; 8925 8926 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8927 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8928 ctx_pg->nr_pages = 0; 8929 return -EINVAL; 8930 } 8931 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8932 int nr_tbls, i; 8933 8934 rmem->depth = 2; 8935 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8936 GFP_KERNEL); 8937 if (!ctx_pg->ctx_pg_tbl) 8938 return -ENOMEM; 8939 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8940 rmem->nr_pages = nr_tbls; 8941 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8942 if (rc) 8943 return rc; 8944 for (i = 0; i < nr_tbls; i++) { 8945 struct bnxt_ctx_pg_info *pg_tbl; 8946 8947 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8948 if (!pg_tbl) 8949 return -ENOMEM; 8950 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8951 rmem = &pg_tbl->ring_mem; 8952 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8953 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8954 rmem->depth = 1; 8955 rmem->nr_pages = MAX_CTX_PAGES; 8956 rmem->ctx_mem = ctxm; 8957 if (i == (nr_tbls - 1)) { 8958 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8959 8960 if (rem) 8961 rmem->nr_pages = rem; 8962 } 8963 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8964 if (rc) 8965 break; 8966 } 8967 } else { 8968 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8969 if (rmem->nr_pages > 1 || depth) 8970 rmem->depth = 1; 8971 rmem->ctx_mem = ctxm; 8972 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8973 } 8974 return rc; 8975 } 8976 8977 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 8978 struct bnxt_ctx_pg_info *ctx_pg, 8979 void *buf, size_t offset, size_t head, 8980 size_t tail) 8981 { 8982 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8983 size_t nr_pages = ctx_pg->nr_pages; 8984 int page_size = rmem->page_size; 8985 size_t len = 0, total_len = 0; 8986 u16 depth = rmem->depth; 8987 8988 tail %= nr_pages * page_size; 8989 do { 8990 if (depth > 1) { 8991 int i = head / (page_size * MAX_CTX_PAGES); 8992 struct bnxt_ctx_pg_info *pg_tbl; 8993 8994 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 8995 rmem = &pg_tbl->ring_mem; 8996 } 8997 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 8998 head += len; 8999 offset += len; 9000 total_len += len; 9001 if (head >= nr_pages * page_size) 9002 head = 0; 9003 } while (head != tail); 9004 return total_len; 9005 } 9006 9007 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 9008 struct bnxt_ctx_pg_info *ctx_pg) 9009 { 9010 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9011 9012 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 9013 ctx_pg->ctx_pg_tbl) { 9014 int i, nr_tbls = rmem->nr_pages; 9015 9016 for (i = 0; i < nr_tbls; i++) { 9017 struct bnxt_ctx_pg_info *pg_tbl; 9018 struct bnxt_ring_mem_info *rmem2; 9019 9020 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9021 if (!pg_tbl) 9022 continue; 9023 rmem2 = &pg_tbl->ring_mem; 9024 bnxt_free_ring(bp, rmem2); 9025 ctx_pg->ctx_pg_arr[i] = NULL; 9026 kfree(pg_tbl); 9027 ctx_pg->ctx_pg_tbl[i] = NULL; 9028 } 9029 kfree(ctx_pg->ctx_pg_tbl); 9030 ctx_pg->ctx_pg_tbl = NULL; 9031 } 9032 bnxt_free_ring(bp, rmem); 9033 ctx_pg->nr_pages = 0; 9034 } 9035 9036 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 9037 struct bnxt_ctx_mem_type *ctxm, u32 entries, 9038 u8 pg_lvl) 9039 { 9040 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9041 int i, rc = 0, n = 1; 9042 u32 mem_size; 9043 9044 if (!ctxm->entry_size || !ctx_pg) 9045 return -EINVAL; 9046 if (ctxm->instance_bmap) 9047 n = hweight32(ctxm->instance_bmap); 9048 if (ctxm->entry_multiple) 9049 entries = roundup(entries, ctxm->entry_multiple); 9050 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 9051 mem_size = entries * ctxm->entry_size; 9052 for (i = 0; i < n && !rc; i++) { 9053 ctx_pg[i].entries = entries; 9054 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 9055 ctxm->init_value ? ctxm : NULL); 9056 } 9057 if (!rc) 9058 ctxm->mem_valid = 1; 9059 return rc; 9060 } 9061 9062 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 9063 struct bnxt_ctx_mem_type *ctxm, 9064 bool last) 9065 { 9066 struct hwrm_func_backing_store_cfg_v2_input *req; 9067 u32 instance_bmap = ctxm->instance_bmap; 9068 int i, j, rc = 0, n = 1; 9069 __le32 *p; 9070 9071 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 9072 return 0; 9073 9074 if (instance_bmap) 9075 n = hweight32(ctxm->instance_bmap); 9076 else 9077 instance_bmap = 1; 9078 9079 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 9080 if (rc) 9081 return rc; 9082 hwrm_req_hold(bp, req); 9083 req->type = cpu_to_le16(ctxm->type); 9084 req->entry_size = cpu_to_le16(ctxm->entry_size); 9085 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 9086 bnxt_bs_trace_avail(bp, ctxm->type)) { 9087 struct bnxt_bs_trace_info *bs_trace; 9088 u32 enables; 9089 9090 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 9091 req->enables = cpu_to_le32(enables); 9092 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 9093 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 9094 } 9095 req->subtype_valid_cnt = ctxm->split_entry_cnt; 9096 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 9097 p[i] = cpu_to_le32(ctxm->split[i]); 9098 for (i = 0, j = 0; j < n && !rc; i++) { 9099 struct bnxt_ctx_pg_info *ctx_pg; 9100 9101 if (!(instance_bmap & (1 << i))) 9102 continue; 9103 req->instance = cpu_to_le16(i); 9104 ctx_pg = &ctxm->pg_info[j++]; 9105 if (!ctx_pg->entries) 9106 continue; 9107 req->num_entries = cpu_to_le32(ctx_pg->entries); 9108 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9109 &req->page_size_pbl_level, 9110 &req->page_dir); 9111 if (last && j == n) 9112 req->flags = 9113 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 9114 rc = hwrm_req_send(bp, req); 9115 } 9116 hwrm_req_drop(bp, req); 9117 return rc; 9118 } 9119 9120 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena) 9121 { 9122 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9123 struct bnxt_ctx_mem_type *ctxm; 9124 u16 last_type = BNXT_CTX_INV; 9125 int rc = 0; 9126 u16 type; 9127 9128 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) { 9129 ctxm = &ctx->ctx_arr[type]; 9130 if (!bnxt_bs_trace_avail(bp, type)) 9131 continue; 9132 if (!ctxm->mem_valid) { 9133 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 9134 ctxm->max_entries, 1); 9135 if (rc) { 9136 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9137 type); 9138 continue; 9139 } 9140 bnxt_bs_trace_init(bp, ctxm); 9141 } 9142 last_type = type; 9143 } 9144 9145 if (last_type == BNXT_CTX_INV) { 9146 if (!ena) 9147 return 0; 9148 else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) 9149 last_type = BNXT_CTX_MAX - 1; 9150 else 9151 last_type = BNXT_CTX_L2_MAX - 1; 9152 } 9153 ctx->ctx_arr[last_type].last = 1; 9154 9155 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9156 ctxm = &ctx->ctx_arr[type]; 9157 9158 if (!ctxm->mem_valid) 9159 continue; 9160 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9161 if (rc) 9162 return rc; 9163 } 9164 return 0; 9165 } 9166 9167 /** 9168 * __bnxt_copy_ctx_mem - copy host context memory 9169 * @bp: The driver context 9170 * @ctxm: The pointer to the context memory type 9171 * @buf: The destination buffer or NULL to just obtain the length 9172 * @offset: The buffer offset to copy the data to 9173 * @head: The head offset of context memory to copy from 9174 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9175 * 9176 * This function is called for debugging purposes to dump the host context 9177 * used by the chip. 9178 * 9179 * Return: Length of memory copied 9180 */ 9181 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9182 struct bnxt_ctx_mem_type *ctxm, void *buf, 9183 size_t offset, size_t head, size_t tail) 9184 { 9185 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9186 size_t len = 0, total_len = 0; 9187 int i, n = 1; 9188 9189 if (!ctx_pg) 9190 return 0; 9191 9192 if (ctxm->instance_bmap) 9193 n = hweight32(ctxm->instance_bmap); 9194 for (i = 0; i < n; i++) { 9195 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9196 tail); 9197 offset += len; 9198 total_len += len; 9199 } 9200 return total_len; 9201 } 9202 9203 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9204 void *buf, size_t offset) 9205 { 9206 size_t tail = ctxm->max_entries * ctxm->entry_size; 9207 9208 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9209 } 9210 9211 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9212 struct bnxt_ctx_mem_type *ctxm, bool force) 9213 { 9214 struct bnxt_ctx_pg_info *ctx_pg; 9215 int i, n = 1; 9216 9217 ctxm->last = 0; 9218 9219 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9220 return; 9221 9222 ctx_pg = ctxm->pg_info; 9223 if (ctx_pg) { 9224 if (ctxm->instance_bmap) 9225 n = hweight32(ctxm->instance_bmap); 9226 for (i = 0; i < n; i++) 9227 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9228 9229 kfree(ctx_pg); 9230 ctxm->pg_info = NULL; 9231 ctxm->mem_valid = 0; 9232 } 9233 memset(ctxm, 0, sizeof(*ctxm)); 9234 } 9235 9236 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9237 { 9238 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9239 u16 type; 9240 9241 if (!ctx) 9242 return; 9243 9244 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9245 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9246 9247 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9248 if (force) { 9249 kfree(ctx); 9250 bp->ctx = NULL; 9251 } 9252 } 9253 9254 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9255 { 9256 struct bnxt_ctx_mem_type *ctxm; 9257 struct bnxt_ctx_mem_info *ctx; 9258 u32 l2_qps, qp1_qps, max_qps; 9259 u32 ena, entries_sp, entries; 9260 u32 srqs, max_srqs, min; 9261 u32 num_mr, num_ah; 9262 u32 extra_srqs = 0; 9263 u32 extra_qps = 0; 9264 u32 fast_qpmd_qps; 9265 u8 pg_lvl = 1; 9266 int i, rc; 9267 9268 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9269 if (rc) { 9270 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9271 rc); 9272 return rc; 9273 } 9274 ctx = bp->ctx; 9275 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9276 return 0; 9277 9278 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9279 l2_qps = ctxm->qp_l2_entries; 9280 qp1_qps = ctxm->qp_qp1_entries; 9281 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9282 max_qps = ctxm->max_entries; 9283 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9284 srqs = ctxm->srq_l2_entries; 9285 max_srqs = ctxm->max_entries; 9286 ena = 0; 9287 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9288 pg_lvl = 2; 9289 if (BNXT_SW_RES_LMT(bp)) { 9290 extra_qps = max_qps - l2_qps - qp1_qps; 9291 extra_srqs = max_srqs - srqs; 9292 } else { 9293 extra_qps = min_t(u32, 65536, 9294 max_qps - l2_qps - qp1_qps); 9295 /* allocate extra qps if fw supports RoCE fast qp 9296 * destroy feature 9297 */ 9298 extra_qps += fast_qpmd_qps; 9299 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9300 } 9301 if (fast_qpmd_qps) 9302 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9303 } 9304 9305 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9306 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9307 pg_lvl); 9308 if (rc) 9309 return rc; 9310 9311 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9312 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9313 if (rc) 9314 return rc; 9315 9316 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9317 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9318 extra_qps * 2, pg_lvl); 9319 if (rc) 9320 return rc; 9321 9322 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9323 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9324 if (rc) 9325 return rc; 9326 9327 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9328 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9329 if (rc) 9330 return rc; 9331 9332 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9333 goto skip_rdma; 9334 9335 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9336 if (BNXT_SW_RES_LMT(bp) && 9337 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9338 num_ah = ctxm->mrav_av_entries; 9339 num_mr = ctxm->max_entries - num_ah; 9340 } else { 9341 /* 128K extra is needed to accommodate static AH context 9342 * allocation by f/w. 9343 */ 9344 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9345 num_ah = min_t(u32, num_mr, 1024 * 128); 9346 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9347 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9348 ctxm->mrav_av_entries = num_ah; 9349 } 9350 9351 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9352 if (rc) 9353 return rc; 9354 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9355 9356 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9357 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9358 if (rc) 9359 return rc; 9360 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9361 9362 skip_rdma: 9363 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9364 min = ctxm->min_entries; 9365 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9366 2 * (extra_qps + qp1_qps) + min; 9367 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9368 if (rc) 9369 return rc; 9370 9371 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9372 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9373 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9374 if (rc) 9375 return rc; 9376 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9377 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9378 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9379 9380 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9381 rc = bnxt_backing_store_cfg_v2(bp, ena); 9382 else 9383 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9384 if (rc) { 9385 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9386 rc); 9387 return rc; 9388 } 9389 ctx->flags |= BNXT_CTX_FLAG_INITED; 9390 return 0; 9391 } 9392 9393 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9394 { 9395 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9396 u16 page_attr; 9397 int rc; 9398 9399 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9400 return 0; 9401 9402 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9403 if (rc) 9404 return rc; 9405 9406 if (BNXT_PAGE_SIZE == 0x2000) 9407 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9408 else if (BNXT_PAGE_SIZE == 0x10000) 9409 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9410 else 9411 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9412 req->pg_size_lvl = cpu_to_le16(page_attr | 9413 bp->fw_crash_mem->ring_mem.depth); 9414 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9415 req->size = cpu_to_le32(bp->fw_crash_len); 9416 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9417 return hwrm_req_send(bp, req); 9418 } 9419 9420 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9421 { 9422 if (bp->fw_crash_mem) { 9423 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9424 kfree(bp->fw_crash_mem); 9425 bp->fw_crash_mem = NULL; 9426 } 9427 } 9428 9429 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9430 { 9431 u32 mem_size = 0; 9432 int rc; 9433 9434 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9435 return 0; 9436 9437 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9438 if (rc) 9439 return rc; 9440 9441 mem_size = round_up(mem_size, 4); 9442 9443 /* keep and use the existing pages */ 9444 if (bp->fw_crash_mem && 9445 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9446 goto alloc_done; 9447 9448 if (bp->fw_crash_mem) 9449 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9450 else 9451 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 9452 GFP_KERNEL); 9453 if (!bp->fw_crash_mem) 9454 return -ENOMEM; 9455 9456 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9457 if (rc) { 9458 bnxt_free_crash_dump_mem(bp); 9459 return rc; 9460 } 9461 9462 alloc_done: 9463 bp->fw_crash_len = mem_size; 9464 return 0; 9465 } 9466 9467 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9468 { 9469 struct hwrm_func_resource_qcaps_output *resp; 9470 struct hwrm_func_resource_qcaps_input *req; 9471 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9472 int rc; 9473 9474 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9475 if (rc) 9476 return rc; 9477 9478 req->fid = cpu_to_le16(0xffff); 9479 resp = hwrm_req_hold(bp, req); 9480 rc = hwrm_req_send_silent(bp, req); 9481 if (rc) 9482 goto hwrm_func_resc_qcaps_exit; 9483 9484 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9485 if (!all) 9486 goto hwrm_func_resc_qcaps_exit; 9487 9488 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9489 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9490 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9491 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9492 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9493 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9494 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9495 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9496 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9497 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9498 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9499 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9500 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9501 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9502 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9503 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9504 9505 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9506 u16 max_msix = le16_to_cpu(resp->max_msix); 9507 9508 hw_resc->max_nqs = max_msix; 9509 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9510 } 9511 9512 if (BNXT_PF(bp)) { 9513 struct bnxt_pf_info *pf = &bp->pf; 9514 9515 pf->vf_resv_strategy = 9516 le16_to_cpu(resp->vf_reservation_strategy); 9517 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9518 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9519 } 9520 hwrm_func_resc_qcaps_exit: 9521 hwrm_req_drop(bp, req); 9522 return rc; 9523 } 9524 9525 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9526 { 9527 struct hwrm_port_mac_ptp_qcfg_output *resp; 9528 struct hwrm_port_mac_ptp_qcfg_input *req; 9529 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9530 u8 flags; 9531 int rc; 9532 9533 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9534 rc = -ENODEV; 9535 goto no_ptp; 9536 } 9537 9538 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9539 if (rc) 9540 goto no_ptp; 9541 9542 req->port_id = cpu_to_le16(bp->pf.port_id); 9543 resp = hwrm_req_hold(bp, req); 9544 rc = hwrm_req_send(bp, req); 9545 if (rc) 9546 goto exit; 9547 9548 flags = resp->flags; 9549 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9550 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9551 rc = -ENODEV; 9552 goto exit; 9553 } 9554 if (!ptp) { 9555 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9556 if (!ptp) { 9557 rc = -ENOMEM; 9558 goto exit; 9559 } 9560 ptp->bp = bp; 9561 bp->ptp_cfg = ptp; 9562 } 9563 9564 if (flags & 9565 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9566 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9567 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9568 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9569 } else if (BNXT_CHIP_P5(bp)) { 9570 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9571 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9572 } else { 9573 rc = -ENODEV; 9574 goto exit; 9575 } 9576 ptp->rtc_configured = 9577 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9578 rc = bnxt_ptp_init(bp); 9579 if (rc) 9580 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9581 exit: 9582 hwrm_req_drop(bp, req); 9583 if (!rc) 9584 return 0; 9585 9586 no_ptp: 9587 bnxt_ptp_clear(bp); 9588 kfree(ptp); 9589 bp->ptp_cfg = NULL; 9590 return rc; 9591 } 9592 9593 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9594 { 9595 struct hwrm_func_qcaps_output *resp; 9596 struct hwrm_func_qcaps_input *req; 9597 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9598 u32 flags, flags_ext, flags_ext2; 9599 int rc; 9600 9601 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9602 if (rc) 9603 return rc; 9604 9605 req->fid = cpu_to_le16(0xffff); 9606 resp = hwrm_req_hold(bp, req); 9607 rc = hwrm_req_send(bp, req); 9608 if (rc) 9609 goto hwrm_func_qcaps_exit; 9610 9611 flags = le32_to_cpu(resp->flags); 9612 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9613 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9614 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9615 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9616 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9617 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9618 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9619 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9620 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9621 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9622 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9623 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9624 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9625 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9626 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9627 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9628 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9629 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9630 9631 flags_ext = le32_to_cpu(resp->flags_ext); 9632 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9633 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9634 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9635 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9636 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9637 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9638 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9639 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9640 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9641 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9642 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED) 9643 bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2; 9644 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9645 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9646 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9647 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9648 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9649 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9650 9651 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9652 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9653 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9654 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9655 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9656 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9657 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9658 if (flags_ext2 & 9659 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9660 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9661 if (BNXT_PF(bp) && 9662 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9663 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9664 9665 bp->tx_push_thresh = 0; 9666 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9667 BNXT_FW_MAJ(bp) > 217) 9668 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9669 9670 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9671 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9672 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9673 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9674 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9675 if (!hw_resc->max_hw_ring_grps) 9676 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9677 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9678 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9679 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9680 9681 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9682 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9683 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9684 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9685 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9686 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9687 9688 if (BNXT_PF(bp)) { 9689 struct bnxt_pf_info *pf = &bp->pf; 9690 9691 pf->fw_fid = le16_to_cpu(resp->fid); 9692 pf->port_id = le16_to_cpu(resp->port_id); 9693 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9694 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9695 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9696 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9697 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9698 bp->flags |= BNXT_FLAG_WOL_CAP; 9699 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9700 bp->fw_cap |= BNXT_FW_CAP_PTP; 9701 } else { 9702 bnxt_ptp_clear(bp); 9703 kfree(bp->ptp_cfg); 9704 bp->ptp_cfg = NULL; 9705 } 9706 } else { 9707 #ifdef CONFIG_BNXT_SRIOV 9708 struct bnxt_vf_info *vf = &bp->vf; 9709 9710 vf->fw_fid = le16_to_cpu(resp->fid); 9711 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9712 #endif 9713 } 9714 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9715 9716 hwrm_func_qcaps_exit: 9717 hwrm_req_drop(bp, req); 9718 return rc; 9719 } 9720 9721 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9722 { 9723 struct hwrm_dbg_qcaps_output *resp; 9724 struct hwrm_dbg_qcaps_input *req; 9725 int rc; 9726 9727 bp->fw_dbg_cap = 0; 9728 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9729 return; 9730 9731 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9732 if (rc) 9733 return; 9734 9735 req->fid = cpu_to_le16(0xffff); 9736 resp = hwrm_req_hold(bp, req); 9737 rc = hwrm_req_send(bp, req); 9738 if (rc) 9739 goto hwrm_dbg_qcaps_exit; 9740 9741 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9742 9743 hwrm_dbg_qcaps_exit: 9744 hwrm_req_drop(bp, req); 9745 } 9746 9747 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9748 9749 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9750 { 9751 int rc; 9752 9753 rc = __bnxt_hwrm_func_qcaps(bp); 9754 if (rc) 9755 return rc; 9756 9757 bnxt_hwrm_dbg_qcaps(bp); 9758 9759 rc = bnxt_hwrm_queue_qportcfg(bp); 9760 if (rc) { 9761 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9762 return rc; 9763 } 9764 if (bp->hwrm_spec_code >= 0x10803) { 9765 rc = bnxt_alloc_ctx_mem(bp); 9766 if (rc) 9767 return rc; 9768 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9769 if (!rc) 9770 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9771 } 9772 return 0; 9773 } 9774 9775 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9776 { 9777 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9778 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9779 u32 flags; 9780 int rc; 9781 9782 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9783 return 0; 9784 9785 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9786 if (rc) 9787 return rc; 9788 9789 resp = hwrm_req_hold(bp, req); 9790 rc = hwrm_req_send(bp, req); 9791 if (rc) 9792 goto hwrm_cfa_adv_qcaps_exit; 9793 9794 flags = le32_to_cpu(resp->flags); 9795 if (flags & 9796 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9797 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9798 9799 if (flags & 9800 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9801 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9802 9803 if (flags & 9804 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9805 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9806 9807 hwrm_cfa_adv_qcaps_exit: 9808 hwrm_req_drop(bp, req); 9809 return rc; 9810 } 9811 9812 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9813 { 9814 if (bp->fw_health) 9815 return 0; 9816 9817 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9818 if (!bp->fw_health) 9819 return -ENOMEM; 9820 9821 mutex_init(&bp->fw_health->lock); 9822 return 0; 9823 } 9824 9825 static int bnxt_alloc_fw_health(struct bnxt *bp) 9826 { 9827 int rc; 9828 9829 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9830 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9831 return 0; 9832 9833 rc = __bnxt_alloc_fw_health(bp); 9834 if (rc) { 9835 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9836 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9837 return rc; 9838 } 9839 9840 return 0; 9841 } 9842 9843 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9844 { 9845 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9846 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9847 BNXT_FW_HEALTH_WIN_MAP_OFF); 9848 } 9849 9850 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9851 { 9852 struct bnxt_fw_health *fw_health = bp->fw_health; 9853 u32 reg_type; 9854 9855 if (!fw_health) 9856 return; 9857 9858 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9859 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9860 fw_health->status_reliable = false; 9861 9862 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9863 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9864 fw_health->resets_reliable = false; 9865 } 9866 9867 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9868 { 9869 void __iomem *hs; 9870 u32 status_loc; 9871 u32 reg_type; 9872 u32 sig; 9873 9874 if (bp->fw_health) 9875 bp->fw_health->status_reliable = false; 9876 9877 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9878 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9879 9880 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9881 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9882 if (!bp->chip_num) { 9883 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9884 bp->chip_num = readl(bp->bar0 + 9885 BNXT_FW_HEALTH_WIN_BASE + 9886 BNXT_GRC_REG_CHIP_NUM); 9887 } 9888 if (!BNXT_CHIP_P5_PLUS(bp)) 9889 return; 9890 9891 status_loc = BNXT_GRC_REG_STATUS_P5 | 9892 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9893 } else { 9894 status_loc = readl(hs + offsetof(struct hcomm_status, 9895 fw_status_loc)); 9896 } 9897 9898 if (__bnxt_alloc_fw_health(bp)) { 9899 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9900 return; 9901 } 9902 9903 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9904 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9905 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9906 __bnxt_map_fw_health_reg(bp, status_loc); 9907 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9908 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9909 } 9910 9911 bp->fw_health->status_reliable = true; 9912 } 9913 9914 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9915 { 9916 struct bnxt_fw_health *fw_health = bp->fw_health; 9917 u32 reg_base = 0xffffffff; 9918 int i; 9919 9920 bp->fw_health->status_reliable = false; 9921 bp->fw_health->resets_reliable = false; 9922 /* Only pre-map the monitoring GRC registers using window 3 */ 9923 for (i = 0; i < 4; i++) { 9924 u32 reg = fw_health->regs[i]; 9925 9926 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9927 continue; 9928 if (reg_base == 0xffffffff) 9929 reg_base = reg & BNXT_GRC_BASE_MASK; 9930 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9931 return -ERANGE; 9932 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9933 } 9934 bp->fw_health->status_reliable = true; 9935 bp->fw_health->resets_reliable = true; 9936 if (reg_base == 0xffffffff) 9937 return 0; 9938 9939 __bnxt_map_fw_health_reg(bp, reg_base); 9940 return 0; 9941 } 9942 9943 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9944 { 9945 if (!bp->fw_health) 9946 return; 9947 9948 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9949 bp->fw_health->status_reliable = true; 9950 bp->fw_health->resets_reliable = true; 9951 } else { 9952 bnxt_try_map_fw_health_reg(bp); 9953 } 9954 } 9955 9956 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 9957 { 9958 struct bnxt_fw_health *fw_health = bp->fw_health; 9959 struct hwrm_error_recovery_qcfg_output *resp; 9960 struct hwrm_error_recovery_qcfg_input *req; 9961 int rc, i; 9962 9963 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9964 return 0; 9965 9966 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 9967 if (rc) 9968 return rc; 9969 9970 resp = hwrm_req_hold(bp, req); 9971 rc = hwrm_req_send(bp, req); 9972 if (rc) 9973 goto err_recovery_out; 9974 fw_health->flags = le32_to_cpu(resp->flags); 9975 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 9976 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 9977 rc = -EINVAL; 9978 goto err_recovery_out; 9979 } 9980 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 9981 fw_health->master_func_wait_dsecs = 9982 le32_to_cpu(resp->master_func_wait_period); 9983 fw_health->normal_func_wait_dsecs = 9984 le32_to_cpu(resp->normal_func_wait_period); 9985 fw_health->post_reset_wait_dsecs = 9986 le32_to_cpu(resp->master_func_wait_period_after_reset); 9987 fw_health->post_reset_max_wait_dsecs = 9988 le32_to_cpu(resp->max_bailout_time_after_reset); 9989 fw_health->regs[BNXT_FW_HEALTH_REG] = 9990 le32_to_cpu(resp->fw_health_status_reg); 9991 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 9992 le32_to_cpu(resp->fw_heartbeat_reg); 9993 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 9994 le32_to_cpu(resp->fw_reset_cnt_reg); 9995 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 9996 le32_to_cpu(resp->reset_inprogress_reg); 9997 fw_health->fw_reset_inprog_reg_mask = 9998 le32_to_cpu(resp->reset_inprogress_reg_mask); 9999 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 10000 if (fw_health->fw_reset_seq_cnt >= 16) { 10001 rc = -EINVAL; 10002 goto err_recovery_out; 10003 } 10004 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 10005 fw_health->fw_reset_seq_regs[i] = 10006 le32_to_cpu(resp->reset_reg[i]); 10007 fw_health->fw_reset_seq_vals[i] = 10008 le32_to_cpu(resp->reset_reg_val[i]); 10009 fw_health->fw_reset_seq_delay_msec[i] = 10010 resp->delay_after_reset[i]; 10011 } 10012 err_recovery_out: 10013 hwrm_req_drop(bp, req); 10014 if (!rc) 10015 rc = bnxt_map_fw_health_regs(bp); 10016 if (rc) 10017 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10018 return rc; 10019 } 10020 10021 static int bnxt_hwrm_func_reset(struct bnxt *bp) 10022 { 10023 struct hwrm_func_reset_input *req; 10024 int rc; 10025 10026 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 10027 if (rc) 10028 return rc; 10029 10030 req->enables = 0; 10031 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 10032 return hwrm_req_send(bp, req); 10033 } 10034 10035 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 10036 { 10037 struct hwrm_nvm_get_dev_info_output nvm_info; 10038 10039 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 10040 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 10041 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 10042 nvm_info.nvm_cfg_ver_upd); 10043 } 10044 10045 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 10046 { 10047 struct hwrm_queue_qportcfg_output *resp; 10048 struct hwrm_queue_qportcfg_input *req; 10049 u8 i, j, *qptr; 10050 bool no_rdma; 10051 int rc = 0; 10052 10053 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 10054 if (rc) 10055 return rc; 10056 10057 resp = hwrm_req_hold(bp, req); 10058 rc = hwrm_req_send(bp, req); 10059 if (rc) 10060 goto qportcfg_exit; 10061 10062 if (!resp->max_configurable_queues) { 10063 rc = -EINVAL; 10064 goto qportcfg_exit; 10065 } 10066 bp->max_tc = resp->max_configurable_queues; 10067 bp->max_lltc = resp->max_configurable_lossless_queues; 10068 if (bp->max_tc > BNXT_MAX_QUEUE) 10069 bp->max_tc = BNXT_MAX_QUEUE; 10070 10071 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 10072 qptr = &resp->queue_id0; 10073 for (i = 0, j = 0; i < bp->max_tc; i++) { 10074 bp->q_info[j].queue_id = *qptr; 10075 bp->q_ids[i] = *qptr++; 10076 bp->q_info[j].queue_profile = *qptr++; 10077 bp->tc_to_qidx[j] = j; 10078 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 10079 (no_rdma && BNXT_PF(bp))) 10080 j++; 10081 } 10082 bp->max_q = bp->max_tc; 10083 bp->max_tc = max_t(u8, j, 1); 10084 10085 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 10086 bp->max_tc = 1; 10087 10088 if (bp->max_lltc > bp->max_tc) 10089 bp->max_lltc = bp->max_tc; 10090 10091 qportcfg_exit: 10092 hwrm_req_drop(bp, req); 10093 return rc; 10094 } 10095 10096 static int bnxt_hwrm_poll(struct bnxt *bp) 10097 { 10098 struct hwrm_ver_get_input *req; 10099 int rc; 10100 10101 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10102 if (rc) 10103 return rc; 10104 10105 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10106 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10107 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10108 10109 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 10110 rc = hwrm_req_send(bp, req); 10111 return rc; 10112 } 10113 10114 static int bnxt_hwrm_ver_get(struct bnxt *bp) 10115 { 10116 struct hwrm_ver_get_output *resp; 10117 struct hwrm_ver_get_input *req; 10118 u16 fw_maj, fw_min, fw_bld, fw_rsv; 10119 u32 dev_caps_cfg, hwrm_ver; 10120 int rc, len, max_tmo_secs; 10121 10122 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10123 if (rc) 10124 return rc; 10125 10126 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 10127 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 10128 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10129 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10130 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10131 10132 resp = hwrm_req_hold(bp, req); 10133 rc = hwrm_req_send(bp, req); 10134 if (rc) 10135 goto hwrm_ver_get_exit; 10136 10137 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 10138 10139 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10140 resp->hwrm_intf_min_8b << 8 | 10141 resp->hwrm_intf_upd_8b; 10142 if (resp->hwrm_intf_maj_8b < 1) { 10143 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10144 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10145 resp->hwrm_intf_upd_8b); 10146 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10147 } 10148 10149 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10150 HWRM_VERSION_UPDATE; 10151 10152 if (bp->hwrm_spec_code > hwrm_ver) 10153 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10154 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10155 HWRM_VERSION_UPDATE); 10156 else 10157 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10158 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10159 resp->hwrm_intf_upd_8b); 10160 10161 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10162 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10163 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10164 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10165 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10166 len = FW_VER_STR_LEN; 10167 } else { 10168 fw_maj = resp->hwrm_fw_maj_8b; 10169 fw_min = resp->hwrm_fw_min_8b; 10170 fw_bld = resp->hwrm_fw_bld_8b; 10171 fw_rsv = resp->hwrm_fw_rsvd_8b; 10172 len = BC_HWRM_STR_LEN; 10173 } 10174 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10175 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10176 fw_rsv); 10177 10178 if (strlen(resp->active_pkg_name)) { 10179 int fw_ver_len = strlen(bp->fw_ver_str); 10180 10181 snprintf(bp->fw_ver_str + fw_ver_len, 10182 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10183 resp->active_pkg_name); 10184 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10185 } 10186 10187 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10188 if (!bp->hwrm_cmd_timeout) 10189 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10190 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10191 if (!bp->hwrm_cmd_max_timeout) 10192 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10193 max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000; 10194 #ifdef CONFIG_DETECT_HUNG_TASK 10195 if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT || 10196 max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) { 10197 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n", 10198 max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT); 10199 } 10200 #endif 10201 10202 if (resp->hwrm_intf_maj_8b >= 1) { 10203 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10204 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10205 } 10206 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10207 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10208 10209 bp->chip_num = le16_to_cpu(resp->chip_num); 10210 bp->chip_rev = resp->chip_rev; 10211 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10212 !resp->chip_metal) 10213 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10214 10215 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10216 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10217 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10218 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10219 10220 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10221 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10222 10223 if (dev_caps_cfg & 10224 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10225 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10226 10227 if (dev_caps_cfg & 10228 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10229 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10230 10231 if (dev_caps_cfg & 10232 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10233 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10234 10235 hwrm_ver_get_exit: 10236 hwrm_req_drop(bp, req); 10237 return rc; 10238 } 10239 10240 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10241 { 10242 struct hwrm_fw_set_time_input *req; 10243 struct tm tm; 10244 time64_t now = ktime_get_real_seconds(); 10245 int rc; 10246 10247 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10248 bp->hwrm_spec_code < 0x10400) 10249 return -EOPNOTSUPP; 10250 10251 time64_to_tm(now, 0, &tm); 10252 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10253 if (rc) 10254 return rc; 10255 10256 req->year = cpu_to_le16(1900 + tm.tm_year); 10257 req->month = 1 + tm.tm_mon; 10258 req->day = tm.tm_mday; 10259 req->hour = tm.tm_hour; 10260 req->minute = tm.tm_min; 10261 req->second = tm.tm_sec; 10262 return hwrm_req_send(bp, req); 10263 } 10264 10265 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10266 { 10267 u64 sw_tmp; 10268 10269 hw &= mask; 10270 sw_tmp = (*sw & ~mask) | hw; 10271 if (hw < (*sw & mask)) 10272 sw_tmp += mask + 1; 10273 WRITE_ONCE(*sw, sw_tmp); 10274 } 10275 10276 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10277 int count, bool ignore_zero) 10278 { 10279 int i; 10280 10281 for (i = 0; i < count; i++) { 10282 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10283 10284 if (ignore_zero && !hw) 10285 continue; 10286 10287 if (masks[i] == -1ULL) 10288 sw_stats[i] = hw; 10289 else 10290 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10291 } 10292 } 10293 10294 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10295 { 10296 if (!stats->hw_stats) 10297 return; 10298 10299 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10300 stats->hw_masks, stats->len / 8, false); 10301 } 10302 10303 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10304 { 10305 struct bnxt_stats_mem *ring0_stats; 10306 bool ignore_zero = false; 10307 int i; 10308 10309 /* Chip bug. Counter intermittently becomes 0. */ 10310 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10311 ignore_zero = true; 10312 10313 for (i = 0; i < bp->cp_nr_rings; i++) { 10314 struct bnxt_napi *bnapi = bp->bnapi[i]; 10315 struct bnxt_cp_ring_info *cpr; 10316 struct bnxt_stats_mem *stats; 10317 10318 cpr = &bnapi->cp_ring; 10319 stats = &cpr->stats; 10320 if (!i) 10321 ring0_stats = stats; 10322 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10323 ring0_stats->hw_masks, 10324 ring0_stats->len / 8, ignore_zero); 10325 } 10326 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10327 struct bnxt_stats_mem *stats = &bp->port_stats; 10328 __le64 *hw_stats = stats->hw_stats; 10329 u64 *sw_stats = stats->sw_stats; 10330 u64 *masks = stats->hw_masks; 10331 int cnt; 10332 10333 cnt = sizeof(struct rx_port_stats) / 8; 10334 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10335 10336 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10337 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10338 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10339 cnt = sizeof(struct tx_port_stats) / 8; 10340 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10341 } 10342 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10343 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10344 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10345 } 10346 } 10347 10348 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10349 { 10350 struct hwrm_port_qstats_input *req; 10351 struct bnxt_pf_info *pf = &bp->pf; 10352 int rc; 10353 10354 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10355 return 0; 10356 10357 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10358 return -EOPNOTSUPP; 10359 10360 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10361 if (rc) 10362 return rc; 10363 10364 req->flags = flags; 10365 req->port_id = cpu_to_le16(pf->port_id); 10366 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10367 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10368 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10369 return hwrm_req_send(bp, req); 10370 } 10371 10372 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10373 { 10374 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10375 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10376 struct hwrm_port_qstats_ext_output *resp_qs; 10377 struct hwrm_port_qstats_ext_input *req_qs; 10378 struct bnxt_pf_info *pf = &bp->pf; 10379 u32 tx_stat_size; 10380 int rc; 10381 10382 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10383 return 0; 10384 10385 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10386 return -EOPNOTSUPP; 10387 10388 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10389 if (rc) 10390 return rc; 10391 10392 req_qs->flags = flags; 10393 req_qs->port_id = cpu_to_le16(pf->port_id); 10394 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10395 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10396 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10397 sizeof(struct tx_port_stats_ext) : 0; 10398 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10399 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10400 resp_qs = hwrm_req_hold(bp, req_qs); 10401 rc = hwrm_req_send(bp, req_qs); 10402 if (!rc) { 10403 bp->fw_rx_stats_ext_size = 10404 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10405 if (BNXT_FW_MAJ(bp) < 220 && 10406 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10407 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10408 10409 bp->fw_tx_stats_ext_size = tx_stat_size ? 10410 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10411 } else { 10412 bp->fw_rx_stats_ext_size = 0; 10413 bp->fw_tx_stats_ext_size = 0; 10414 } 10415 hwrm_req_drop(bp, req_qs); 10416 10417 if (flags) 10418 return rc; 10419 10420 if (bp->fw_tx_stats_ext_size <= 10421 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10422 bp->pri2cos_valid = 0; 10423 return rc; 10424 } 10425 10426 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10427 if (rc) 10428 return rc; 10429 10430 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10431 10432 resp_qc = hwrm_req_hold(bp, req_qc); 10433 rc = hwrm_req_send(bp, req_qc); 10434 if (!rc) { 10435 u8 *pri2cos; 10436 int i, j; 10437 10438 pri2cos = &resp_qc->pri0_cos_queue_id; 10439 for (i = 0; i < 8; i++) { 10440 u8 queue_id = pri2cos[i]; 10441 u8 queue_idx; 10442 10443 /* Per port queue IDs start from 0, 10, 20, etc */ 10444 queue_idx = queue_id % 10; 10445 if (queue_idx > BNXT_MAX_QUEUE) { 10446 bp->pri2cos_valid = false; 10447 hwrm_req_drop(bp, req_qc); 10448 return rc; 10449 } 10450 for (j = 0; j < bp->max_q; j++) { 10451 if (bp->q_ids[j] == queue_id) 10452 bp->pri2cos_idx[i] = queue_idx; 10453 } 10454 } 10455 bp->pri2cos_valid = true; 10456 } 10457 hwrm_req_drop(bp, req_qc); 10458 10459 return rc; 10460 } 10461 10462 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10463 { 10464 bnxt_hwrm_tunnel_dst_port_free(bp, 10465 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10466 bnxt_hwrm_tunnel_dst_port_free(bp, 10467 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10468 } 10469 10470 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10471 { 10472 int rc, i; 10473 u32 tpa_flags = 0; 10474 10475 if (set_tpa) 10476 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10477 else if (BNXT_NO_FW_ACCESS(bp)) 10478 return 0; 10479 for (i = 0; i < bp->nr_vnics; i++) { 10480 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10481 if (rc) { 10482 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10483 i, rc); 10484 return rc; 10485 } 10486 } 10487 return 0; 10488 } 10489 10490 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10491 { 10492 int i; 10493 10494 for (i = 0; i < bp->nr_vnics; i++) 10495 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10496 } 10497 10498 static void bnxt_clear_vnic(struct bnxt *bp) 10499 { 10500 if (!bp->vnic_info) 10501 return; 10502 10503 bnxt_hwrm_clear_vnic_filter(bp); 10504 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10505 /* clear all RSS setting before free vnic ctx */ 10506 bnxt_hwrm_clear_vnic_rss(bp); 10507 bnxt_hwrm_vnic_ctx_free(bp); 10508 } 10509 /* before free the vnic, undo the vnic tpa settings */ 10510 if (bp->flags & BNXT_FLAG_TPA) 10511 bnxt_set_tpa(bp, false); 10512 bnxt_hwrm_vnic_free(bp); 10513 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10514 bnxt_hwrm_vnic_ctx_free(bp); 10515 } 10516 10517 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10518 bool irq_re_init) 10519 { 10520 bnxt_clear_vnic(bp); 10521 bnxt_hwrm_ring_free(bp, close_path); 10522 bnxt_hwrm_ring_grp_free(bp); 10523 if (irq_re_init) { 10524 bnxt_hwrm_stat_ctx_free(bp); 10525 bnxt_hwrm_free_tunnel_ports(bp); 10526 } 10527 } 10528 10529 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10530 { 10531 struct hwrm_func_cfg_input *req; 10532 u8 evb_mode; 10533 int rc; 10534 10535 if (br_mode == BRIDGE_MODE_VEB) 10536 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10537 else if (br_mode == BRIDGE_MODE_VEPA) 10538 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10539 else 10540 return -EINVAL; 10541 10542 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10543 if (rc) 10544 return rc; 10545 10546 req->fid = cpu_to_le16(0xffff); 10547 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10548 req->evb_mode = evb_mode; 10549 return hwrm_req_send(bp, req); 10550 } 10551 10552 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10553 { 10554 struct hwrm_func_cfg_input *req; 10555 int rc; 10556 10557 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10558 return 0; 10559 10560 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10561 if (rc) 10562 return rc; 10563 10564 req->fid = cpu_to_le16(0xffff); 10565 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10566 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10567 if (size == 128) 10568 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10569 10570 return hwrm_req_send(bp, req); 10571 } 10572 10573 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10574 { 10575 int rc; 10576 10577 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10578 goto skip_rss_ctx; 10579 10580 /* allocate context for vnic */ 10581 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10582 if (rc) { 10583 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10584 vnic->vnic_id, rc); 10585 goto vnic_setup_err; 10586 } 10587 bp->rsscos_nr_ctxs++; 10588 10589 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10590 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10591 if (rc) { 10592 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10593 vnic->vnic_id, rc); 10594 goto vnic_setup_err; 10595 } 10596 bp->rsscos_nr_ctxs++; 10597 } 10598 10599 skip_rss_ctx: 10600 /* configure default vnic, ring grp */ 10601 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10602 if (rc) { 10603 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10604 vnic->vnic_id, rc); 10605 goto vnic_setup_err; 10606 } 10607 10608 /* Enable RSS hashing on vnic */ 10609 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10610 if (rc) { 10611 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10612 vnic->vnic_id, rc); 10613 goto vnic_setup_err; 10614 } 10615 10616 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10617 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10618 if (rc) { 10619 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10620 vnic->vnic_id, rc); 10621 } 10622 } 10623 10624 vnic_setup_err: 10625 return rc; 10626 } 10627 10628 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10629 u8 valid) 10630 { 10631 struct hwrm_vnic_update_input *req; 10632 int rc; 10633 10634 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10635 if (rc) 10636 return rc; 10637 10638 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10639 10640 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10641 req->mru = cpu_to_le16(vnic->mru); 10642 10643 req->enables = cpu_to_le32(valid); 10644 10645 return hwrm_req_send(bp, req); 10646 } 10647 10648 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10649 { 10650 int rc; 10651 10652 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10653 if (rc) { 10654 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10655 vnic->vnic_id, rc); 10656 return rc; 10657 } 10658 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10659 if (rc) 10660 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10661 vnic->vnic_id, rc); 10662 return rc; 10663 } 10664 10665 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10666 { 10667 int rc, i, nr_ctxs; 10668 10669 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10670 for (i = 0; i < nr_ctxs; i++) { 10671 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10672 if (rc) { 10673 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10674 vnic->vnic_id, i, rc); 10675 break; 10676 } 10677 bp->rsscos_nr_ctxs++; 10678 } 10679 if (i < nr_ctxs) 10680 return -ENOMEM; 10681 10682 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10683 if (rc) 10684 return rc; 10685 10686 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10687 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10688 if (rc) { 10689 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10690 vnic->vnic_id, rc); 10691 } 10692 } 10693 return rc; 10694 } 10695 10696 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10697 { 10698 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10699 return __bnxt_setup_vnic_p5(bp, vnic); 10700 else 10701 return __bnxt_setup_vnic(bp, vnic); 10702 } 10703 10704 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10705 struct bnxt_vnic_info *vnic, 10706 u16 start_rx_ring_idx, int rx_rings) 10707 { 10708 int rc; 10709 10710 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10711 if (rc) { 10712 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10713 vnic->vnic_id, rc); 10714 return rc; 10715 } 10716 return bnxt_setup_vnic(bp, vnic); 10717 } 10718 10719 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10720 { 10721 struct bnxt_vnic_info *vnic; 10722 int i, rc = 0; 10723 10724 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10725 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10726 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10727 } 10728 10729 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10730 return 0; 10731 10732 for (i = 0; i < bp->rx_nr_rings; i++) { 10733 u16 vnic_id = i + 1; 10734 u16 ring_id = i; 10735 10736 if (vnic_id >= bp->nr_vnics) 10737 break; 10738 10739 vnic = &bp->vnic_info[vnic_id]; 10740 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10741 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10742 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10743 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10744 break; 10745 } 10746 return rc; 10747 } 10748 10749 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10750 bool all) 10751 { 10752 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10753 struct bnxt_filter_base *usr_fltr, *tmp; 10754 struct bnxt_ntuple_filter *ntp_fltr; 10755 int i; 10756 10757 if (netif_running(bp->dev)) { 10758 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10759 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10760 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10761 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10762 } 10763 } 10764 if (!all) 10765 return; 10766 10767 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10768 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10769 usr_fltr->fw_vnic_id == rss_ctx->index) { 10770 ntp_fltr = container_of(usr_fltr, 10771 struct bnxt_ntuple_filter, 10772 base); 10773 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10774 bnxt_del_ntp_filter(bp, ntp_fltr); 10775 bnxt_del_one_usr_fltr(bp, usr_fltr); 10776 } 10777 } 10778 10779 if (vnic->rss_table) 10780 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10781 vnic->rss_table, 10782 vnic->rss_table_dma_addr); 10783 bp->num_rss_ctx--; 10784 } 10785 10786 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10787 int rxr_id) 10788 { 10789 u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 10790 int i, vnic_rx; 10791 10792 /* Ntuple VNIC always has all the rx rings. Any change of ring id 10793 * must be updated because a future filter may use it. 10794 */ 10795 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 10796 return true; 10797 10798 for (i = 0; i < tbl_size; i++) { 10799 if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 10800 vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 10801 else 10802 vnic_rx = bp->rss_indir_tbl[i]; 10803 10804 if (rxr_id == vnic_rx) 10805 return true; 10806 } 10807 10808 return false; 10809 } 10810 10811 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10812 u16 mru, int rxr_id) 10813 { 10814 int rc; 10815 10816 if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id)) 10817 return 0; 10818 10819 if (mru) { 10820 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10821 if (rc) { 10822 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10823 vnic->vnic_id, rc); 10824 return rc; 10825 } 10826 } 10827 vnic->mru = mru; 10828 bnxt_hwrm_vnic_update(bp, vnic, 10829 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 10830 10831 return 0; 10832 } 10833 10834 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id) 10835 { 10836 struct ethtool_rxfh_context *ctx; 10837 unsigned long context; 10838 int rc; 10839 10840 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10841 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10842 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10843 10844 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id); 10845 if (rc) 10846 return rc; 10847 } 10848 10849 return 0; 10850 } 10851 10852 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10853 { 10854 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10855 struct ethtool_rxfh_context *ctx; 10856 unsigned long context; 10857 10858 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10859 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10860 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10861 10862 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10863 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10864 __bnxt_setup_vnic_p5(bp, vnic)) { 10865 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10866 rss_ctx->index); 10867 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10868 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10869 } 10870 } 10871 } 10872 10873 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10874 { 10875 struct ethtool_rxfh_context *ctx; 10876 unsigned long context; 10877 10878 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10879 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10880 10881 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10882 } 10883 } 10884 10885 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10886 static bool bnxt_promisc_ok(struct bnxt *bp) 10887 { 10888 #ifdef CONFIG_BNXT_SRIOV 10889 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10890 return false; 10891 #endif 10892 return true; 10893 } 10894 10895 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10896 { 10897 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10898 unsigned int rc = 0; 10899 10900 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10901 if (rc) { 10902 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10903 rc); 10904 return rc; 10905 } 10906 10907 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10908 if (rc) { 10909 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10910 rc); 10911 return rc; 10912 } 10913 return rc; 10914 } 10915 10916 static int bnxt_cfg_rx_mode(struct bnxt *); 10917 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10918 10919 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10920 { 10921 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10922 int rc = 0; 10923 unsigned int rx_nr_rings = bp->rx_nr_rings; 10924 10925 if (irq_re_init) { 10926 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10927 if (rc) { 10928 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10929 rc); 10930 goto err_out; 10931 } 10932 } 10933 10934 rc = bnxt_hwrm_ring_alloc(bp); 10935 if (rc) { 10936 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10937 goto err_out; 10938 } 10939 10940 rc = bnxt_hwrm_ring_grp_alloc(bp); 10941 if (rc) { 10942 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10943 goto err_out; 10944 } 10945 10946 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10947 rx_nr_rings--; 10948 10949 /* default vnic 0 */ 10950 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10951 if (rc) { 10952 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10953 goto err_out; 10954 } 10955 10956 if (BNXT_VF(bp)) 10957 bnxt_hwrm_func_qcfg(bp); 10958 10959 rc = bnxt_setup_vnic(bp, vnic); 10960 if (rc) 10961 goto err_out; 10962 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 10963 bnxt_hwrm_update_rss_hash_cfg(bp); 10964 10965 if (bp->flags & BNXT_FLAG_RFS) { 10966 rc = bnxt_alloc_rfs_vnics(bp); 10967 if (rc) 10968 goto err_out; 10969 } 10970 10971 if (bp->flags & BNXT_FLAG_TPA) { 10972 rc = bnxt_set_tpa(bp, true); 10973 if (rc) 10974 goto err_out; 10975 } 10976 10977 if (BNXT_VF(bp)) 10978 bnxt_update_vf_mac(bp); 10979 10980 /* Filter for default vnic 0 */ 10981 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 10982 if (rc) { 10983 if (BNXT_VF(bp) && rc == -ENODEV) 10984 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 10985 else 10986 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10987 goto err_out; 10988 } 10989 vnic->uc_filter_count = 1; 10990 10991 vnic->rx_mask = 0; 10992 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 10993 goto skip_rx_mask; 10994 10995 if (bp->dev->flags & IFF_BROADCAST) 10996 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 10997 10998 if (bp->dev->flags & IFF_PROMISC) 10999 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 11000 11001 if (bp->dev->flags & IFF_ALLMULTI) { 11002 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 11003 vnic->mc_list_count = 0; 11004 } else if (bp->dev->flags & IFF_MULTICAST) { 11005 u32 mask = 0; 11006 11007 bnxt_mc_list_updated(bp, &mask); 11008 vnic->rx_mask |= mask; 11009 } 11010 11011 rc = bnxt_cfg_rx_mode(bp); 11012 if (rc) 11013 goto err_out; 11014 11015 skip_rx_mask: 11016 rc = bnxt_hwrm_set_coal(bp); 11017 if (rc) 11018 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 11019 rc); 11020 11021 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11022 rc = bnxt_setup_nitroa0_vnic(bp); 11023 if (rc) 11024 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 11025 rc); 11026 } 11027 11028 if (BNXT_VF(bp)) { 11029 bnxt_hwrm_func_qcfg(bp); 11030 netdev_update_features(bp->dev); 11031 } 11032 11033 return 0; 11034 11035 err_out: 11036 bnxt_hwrm_resource_free(bp, 0, true); 11037 11038 return rc; 11039 } 11040 11041 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 11042 { 11043 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 11044 return 0; 11045 } 11046 11047 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 11048 { 11049 bnxt_init_cp_rings(bp); 11050 bnxt_init_rx_rings(bp); 11051 bnxt_init_tx_rings(bp); 11052 bnxt_init_ring_grps(bp, irq_re_init); 11053 bnxt_init_vnics(bp); 11054 11055 return bnxt_init_chip(bp, irq_re_init); 11056 } 11057 11058 static int bnxt_set_real_num_queues(struct bnxt *bp) 11059 { 11060 int rc; 11061 struct net_device *dev = bp->dev; 11062 11063 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 11064 bp->tx_nr_rings_xdp); 11065 if (rc) 11066 return rc; 11067 11068 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 11069 if (rc) 11070 return rc; 11071 11072 #ifdef CONFIG_RFS_ACCEL 11073 if (bp->flags & BNXT_FLAG_RFS) 11074 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 11075 #endif 11076 11077 return rc; 11078 } 11079 11080 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11081 bool shared) 11082 { 11083 int _rx = *rx, _tx = *tx; 11084 11085 if (shared) { 11086 *rx = min_t(int, _rx, max); 11087 *tx = min_t(int, _tx, max); 11088 } else { 11089 if (max < 2) 11090 return -ENOMEM; 11091 11092 while (_rx + _tx > max) { 11093 if (_rx > _tx && _rx > 1) 11094 _rx--; 11095 else if (_tx > 1) 11096 _tx--; 11097 } 11098 *rx = _rx; 11099 *tx = _tx; 11100 } 11101 return 0; 11102 } 11103 11104 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 11105 { 11106 return (tx - tx_xdp) / tx_sets + tx_xdp; 11107 } 11108 11109 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 11110 { 11111 int tcs = bp->num_tc; 11112 11113 if (!tcs) 11114 tcs = 1; 11115 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 11116 } 11117 11118 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 11119 { 11120 int tcs = bp->num_tc; 11121 11122 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 11123 bp->tx_nr_rings_xdp; 11124 } 11125 11126 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11127 bool sh) 11128 { 11129 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 11130 11131 if (tx_cp != *tx) { 11132 int tx_saved = tx_cp, rc; 11133 11134 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 11135 if (rc) 11136 return rc; 11137 if (tx_cp != tx_saved) 11138 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 11139 return 0; 11140 } 11141 return __bnxt_trim_rings(bp, rx, tx, max, sh); 11142 } 11143 11144 static void bnxt_setup_msix(struct bnxt *bp) 11145 { 11146 const int len = sizeof(bp->irq_tbl[0].name); 11147 struct net_device *dev = bp->dev; 11148 int tcs, i; 11149 11150 tcs = bp->num_tc; 11151 if (tcs) { 11152 int i, off, count; 11153 11154 for (i = 0; i < tcs; i++) { 11155 count = bp->tx_nr_rings_per_tc; 11156 off = BNXT_TC_TO_RING_BASE(bp, i); 11157 netdev_set_tc_queue(dev, i, count, off); 11158 } 11159 } 11160 11161 for (i = 0; i < bp->cp_nr_rings; i++) { 11162 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11163 char *attr; 11164 11165 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 11166 attr = "TxRx"; 11167 else if (i < bp->rx_nr_rings) 11168 attr = "rx"; 11169 else 11170 attr = "tx"; 11171 11172 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 11173 attr, i); 11174 bp->irq_tbl[map_idx].handler = bnxt_msix; 11175 } 11176 } 11177 11178 static int bnxt_init_int_mode(struct bnxt *bp); 11179 11180 static int bnxt_change_msix(struct bnxt *bp, int total) 11181 { 11182 struct msi_map map; 11183 int i; 11184 11185 /* add MSIX to the end if needed */ 11186 for (i = bp->total_irqs; i < total; i++) { 11187 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 11188 if (map.index < 0) 11189 return bp->total_irqs; 11190 bp->irq_tbl[i].vector = map.virq; 11191 bp->total_irqs++; 11192 } 11193 11194 /* trim MSIX from the end if needed */ 11195 for (i = bp->total_irqs; i > total; i--) { 11196 map.index = i - 1; 11197 map.virq = bp->irq_tbl[i - 1].vector; 11198 pci_msix_free_irq(bp->pdev, map); 11199 bp->total_irqs--; 11200 } 11201 return bp->total_irqs; 11202 } 11203 11204 static int bnxt_setup_int_mode(struct bnxt *bp) 11205 { 11206 int rc; 11207 11208 if (!bp->irq_tbl) { 11209 rc = bnxt_init_int_mode(bp); 11210 if (rc || !bp->irq_tbl) 11211 return rc ?: -ENODEV; 11212 } 11213 11214 bnxt_setup_msix(bp); 11215 11216 rc = bnxt_set_real_num_queues(bp); 11217 return rc; 11218 } 11219 11220 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11221 { 11222 return bp->hw_resc.max_rsscos_ctxs; 11223 } 11224 11225 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11226 { 11227 return bp->hw_resc.max_vnics; 11228 } 11229 11230 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11231 { 11232 return bp->hw_resc.max_stat_ctxs; 11233 } 11234 11235 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11236 { 11237 return bp->hw_resc.max_cp_rings; 11238 } 11239 11240 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11241 { 11242 unsigned int cp = bp->hw_resc.max_cp_rings; 11243 11244 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11245 cp -= bnxt_get_ulp_msix_num(bp); 11246 11247 return cp; 11248 } 11249 11250 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11251 { 11252 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11253 11254 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11255 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11256 11257 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11258 } 11259 11260 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11261 { 11262 bp->hw_resc.max_irqs = max_irqs; 11263 } 11264 11265 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11266 { 11267 unsigned int cp; 11268 11269 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11270 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11271 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11272 else 11273 return cp - bp->cp_nr_rings; 11274 } 11275 11276 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11277 { 11278 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11279 } 11280 11281 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11282 { 11283 int max_irq = bnxt_get_max_func_irqs(bp); 11284 int total_req = bp->cp_nr_rings + num; 11285 11286 if (max_irq < total_req) { 11287 num = max_irq - bp->cp_nr_rings; 11288 if (num <= 0) 11289 return 0; 11290 } 11291 return num; 11292 } 11293 11294 static int bnxt_get_num_msix(struct bnxt *bp) 11295 { 11296 if (!BNXT_NEW_RM(bp)) 11297 return bnxt_get_max_func_irqs(bp); 11298 11299 return bnxt_nq_rings_in_use(bp); 11300 } 11301 11302 static int bnxt_init_int_mode(struct bnxt *bp) 11303 { 11304 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 11305 11306 total_vecs = bnxt_get_num_msix(bp); 11307 max = bnxt_get_max_func_irqs(bp); 11308 if (total_vecs > max) 11309 total_vecs = max; 11310 11311 if (!total_vecs) 11312 return 0; 11313 11314 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11315 min = 2; 11316 11317 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11318 PCI_IRQ_MSIX); 11319 ulp_msix = bnxt_get_ulp_msix_num(bp); 11320 if (total_vecs < 0 || total_vecs < ulp_msix) { 11321 rc = -ENODEV; 11322 goto msix_setup_exit; 11323 } 11324 11325 tbl_size = total_vecs; 11326 if (pci_msix_can_alloc_dyn(bp->pdev)) 11327 tbl_size = max; 11328 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 11329 if (bp->irq_tbl) { 11330 for (i = 0; i < total_vecs; i++) 11331 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11332 11333 bp->total_irqs = total_vecs; 11334 /* Trim rings based upon num of vectors allocated */ 11335 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11336 total_vecs - ulp_msix, min == 1); 11337 if (rc) 11338 goto msix_setup_exit; 11339 11340 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11341 bp->cp_nr_rings = (min == 1) ? 11342 max_t(int, tx_cp, bp->rx_nr_rings) : 11343 tx_cp + bp->rx_nr_rings; 11344 11345 } else { 11346 rc = -ENOMEM; 11347 goto msix_setup_exit; 11348 } 11349 return 0; 11350 11351 msix_setup_exit: 11352 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11353 kfree(bp->irq_tbl); 11354 bp->irq_tbl = NULL; 11355 pci_free_irq_vectors(bp->pdev); 11356 return rc; 11357 } 11358 11359 static void bnxt_clear_int_mode(struct bnxt *bp) 11360 { 11361 pci_free_irq_vectors(bp->pdev); 11362 11363 kfree(bp->irq_tbl); 11364 bp->irq_tbl = NULL; 11365 } 11366 11367 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11368 { 11369 bool irq_cleared = false; 11370 bool irq_change = false; 11371 int tcs = bp->num_tc; 11372 int irqs_required; 11373 int rc; 11374 11375 if (!bnxt_need_reserve_rings(bp)) 11376 return 0; 11377 11378 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 11379 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11380 11381 if (ulp_msix > bp->ulp_num_msix_want) 11382 ulp_msix = bp->ulp_num_msix_want; 11383 irqs_required = ulp_msix + bp->cp_nr_rings; 11384 } else { 11385 irqs_required = bnxt_get_num_msix(bp); 11386 } 11387 11388 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11389 irq_change = true; 11390 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11391 bnxt_ulp_irq_stop(bp); 11392 bnxt_clear_int_mode(bp); 11393 irq_cleared = true; 11394 } 11395 } 11396 rc = __bnxt_reserve_rings(bp); 11397 if (irq_cleared) { 11398 if (!rc) 11399 rc = bnxt_init_int_mode(bp); 11400 bnxt_ulp_irq_restart(bp, rc); 11401 } else if (irq_change && !rc) { 11402 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11403 rc = -ENOSPC; 11404 } 11405 if (rc) { 11406 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11407 return rc; 11408 } 11409 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11410 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11411 netdev_err(bp->dev, "tx ring reservation failure\n"); 11412 netdev_reset_tc(bp->dev); 11413 bp->num_tc = 0; 11414 if (bp->tx_nr_rings_xdp) 11415 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11416 else 11417 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11418 return -ENOMEM; 11419 } 11420 return 0; 11421 } 11422 11423 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx) 11424 { 11425 struct bnxt_tx_ring_info *txr; 11426 struct netdev_queue *txq; 11427 struct bnxt_napi *bnapi; 11428 int i; 11429 11430 bnapi = bp->bnapi[idx]; 11431 bnxt_for_each_napi_tx(i, bnapi, txr) { 11432 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11433 synchronize_net(); 11434 11435 if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) { 11436 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11437 if (txq) { 11438 __netif_tx_lock_bh(txq); 11439 netif_tx_stop_queue(txq); 11440 __netif_tx_unlock_bh(txq); 11441 } 11442 } 11443 11444 if (!bp->tph_mode) 11445 continue; 11446 11447 bnxt_hwrm_tx_ring_free(bp, txr, true); 11448 bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr); 11449 bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index); 11450 bnxt_clear_one_cp_ring(bp, txr->tx_cpr); 11451 } 11452 } 11453 11454 static int bnxt_tx_queue_start(struct bnxt *bp, int idx) 11455 { 11456 struct bnxt_tx_ring_info *txr; 11457 struct netdev_queue *txq; 11458 struct bnxt_napi *bnapi; 11459 int rc, i; 11460 11461 bnapi = bp->bnapi[idx]; 11462 /* All rings have been reserved and previously allocated. 11463 * Reallocating with the same parameters should never fail. 11464 */ 11465 bnxt_for_each_napi_tx(i, bnapi, txr) { 11466 if (!bp->tph_mode) 11467 goto start_tx; 11468 11469 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 11470 if (rc) 11471 return rc; 11472 11473 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false); 11474 if (rc) 11475 return rc; 11476 11477 txr->tx_prod = 0; 11478 txr->tx_cons = 0; 11479 txr->tx_hw_cons = 0; 11480 start_tx: 11481 WRITE_ONCE(txr->dev_state, 0); 11482 synchronize_net(); 11483 11484 if (bnapi->flags & BNXT_NAPI_FLAG_XDP) 11485 continue; 11486 11487 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11488 if (txq) 11489 netif_tx_start_queue(txq); 11490 } 11491 11492 return 0; 11493 } 11494 11495 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify, 11496 const cpumask_t *mask) 11497 { 11498 struct bnxt_irq *irq; 11499 u16 tag; 11500 int err; 11501 11502 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11503 11504 if (!irq->bp->tph_mode) 11505 return; 11506 11507 cpumask_copy(irq->cpu_mask, mask); 11508 11509 if (irq->ring_nr >= irq->bp->rx_nr_rings) 11510 return; 11511 11512 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11513 cpumask_first(irq->cpu_mask), &tag)) 11514 return; 11515 11516 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag)) 11517 return; 11518 11519 netdev_lock(irq->bp->dev); 11520 if (netif_running(irq->bp->dev)) { 11521 err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr); 11522 if (err) 11523 netdev_err(irq->bp->dev, 11524 "RX queue restart failed: err=%d\n", err); 11525 } 11526 netdev_unlock(irq->bp->dev); 11527 } 11528 11529 static void bnxt_irq_affinity_release(struct kref *ref) 11530 { 11531 struct irq_affinity_notify *notify = 11532 container_of(ref, struct irq_affinity_notify, kref); 11533 struct bnxt_irq *irq; 11534 11535 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11536 11537 if (!irq->bp->tph_mode) 11538 return; 11539 11540 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) { 11541 netdev_err(irq->bp->dev, 11542 "Setting ST=0 for MSIX entry %d failed\n", 11543 irq->msix_nr); 11544 return; 11545 } 11546 } 11547 11548 static void bnxt_release_irq_notifier(struct bnxt_irq *irq) 11549 { 11550 irq_set_affinity_notifier(irq->vector, NULL); 11551 } 11552 11553 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq) 11554 { 11555 struct irq_affinity_notify *notify; 11556 11557 irq->bp = bp; 11558 11559 /* Nothing to do if TPH is not enabled */ 11560 if (!bp->tph_mode) 11561 return; 11562 11563 /* Register IRQ affinity notifier */ 11564 notify = &irq->affinity_notify; 11565 notify->irq = irq->vector; 11566 notify->notify = bnxt_irq_affinity_notify; 11567 notify->release = bnxt_irq_affinity_release; 11568 11569 irq_set_affinity_notifier(irq->vector, notify); 11570 } 11571 11572 static void bnxt_free_irq(struct bnxt *bp) 11573 { 11574 struct bnxt_irq *irq; 11575 int i; 11576 11577 #ifdef CONFIG_RFS_ACCEL 11578 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11579 bp->dev->rx_cpu_rmap = NULL; 11580 #endif 11581 if (!bp->irq_tbl || !bp->bnapi) 11582 return; 11583 11584 for (i = 0; i < bp->cp_nr_rings; i++) { 11585 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11586 11587 irq = &bp->irq_tbl[map_idx]; 11588 if (irq->requested) { 11589 if (irq->have_cpumask) { 11590 irq_update_affinity_hint(irq->vector, NULL); 11591 free_cpumask_var(irq->cpu_mask); 11592 irq->have_cpumask = 0; 11593 } 11594 11595 bnxt_release_irq_notifier(irq); 11596 11597 free_irq(irq->vector, bp->bnapi[i]); 11598 } 11599 11600 irq->requested = 0; 11601 } 11602 11603 /* Disable TPH support */ 11604 pcie_disable_tph(bp->pdev); 11605 bp->tph_mode = 0; 11606 } 11607 11608 static int bnxt_request_irq(struct bnxt *bp) 11609 { 11610 int i, j, rc = 0; 11611 unsigned long flags = 0; 11612 #ifdef CONFIG_RFS_ACCEL 11613 struct cpu_rmap *rmap; 11614 #endif 11615 11616 rc = bnxt_setup_int_mode(bp); 11617 if (rc) { 11618 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11619 rc); 11620 return rc; 11621 } 11622 #ifdef CONFIG_RFS_ACCEL 11623 rmap = bp->dev->rx_cpu_rmap; 11624 #endif 11625 11626 /* Enable TPH support as part of IRQ request */ 11627 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11628 if (!rc) 11629 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11630 11631 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11632 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11633 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11634 11635 #ifdef CONFIG_RFS_ACCEL 11636 if (rmap && bp->bnapi[i]->rx_ring) { 11637 rc = irq_cpu_rmap_add(rmap, irq->vector); 11638 if (rc) 11639 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11640 j); 11641 j++; 11642 } 11643 #endif 11644 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11645 bp->bnapi[i]); 11646 if (rc) 11647 break; 11648 11649 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 11650 irq->requested = 1; 11651 11652 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11653 int numa_node = dev_to_node(&bp->pdev->dev); 11654 u16 tag; 11655 11656 irq->have_cpumask = 1; 11657 irq->msix_nr = map_idx; 11658 irq->ring_nr = i; 11659 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11660 irq->cpu_mask); 11661 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11662 if (rc) { 11663 netdev_warn(bp->dev, 11664 "Update affinity hint failed, IRQ = %d\n", 11665 irq->vector); 11666 break; 11667 } 11668 11669 bnxt_register_irq_notifier(bp, irq); 11670 11671 /* Init ST table entry */ 11672 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11673 cpumask_first(irq->cpu_mask), 11674 &tag)) 11675 continue; 11676 11677 pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag); 11678 } 11679 } 11680 return rc; 11681 } 11682 11683 static void bnxt_del_napi(struct bnxt *bp) 11684 { 11685 int i; 11686 11687 if (!bp->bnapi) 11688 return; 11689 11690 for (i = 0; i < bp->rx_nr_rings; i++) 11691 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11692 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11693 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11694 11695 for (i = 0; i < bp->cp_nr_rings; i++) { 11696 struct bnxt_napi *bnapi = bp->bnapi[i]; 11697 11698 __netif_napi_del_locked(&bnapi->napi); 11699 } 11700 /* We called __netif_napi_del_locked(), we need 11701 * to respect an RCU grace period before freeing napi structures. 11702 */ 11703 synchronize_net(); 11704 } 11705 11706 static void bnxt_init_napi(struct bnxt *bp) 11707 { 11708 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11709 unsigned int cp_nr_rings = bp->cp_nr_rings; 11710 struct bnxt_napi *bnapi; 11711 int i; 11712 11713 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11714 poll_fn = bnxt_poll_p5; 11715 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11716 cp_nr_rings--; 11717 11718 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11719 11720 for (i = 0; i < cp_nr_rings; i++) { 11721 bnapi = bp->bnapi[i]; 11722 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 11723 bnapi->index); 11724 } 11725 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11726 bnapi = bp->bnapi[cp_nr_rings]; 11727 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11728 } 11729 } 11730 11731 static void bnxt_disable_napi(struct bnxt *bp) 11732 { 11733 int i; 11734 11735 if (!bp->bnapi || 11736 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11737 return; 11738 11739 for (i = 0; i < bp->cp_nr_rings; i++) { 11740 struct bnxt_napi *bnapi = bp->bnapi[i]; 11741 struct bnxt_cp_ring_info *cpr; 11742 11743 cpr = &bnapi->cp_ring; 11744 if (bnapi->tx_fault) 11745 cpr->sw_stats->tx.tx_resets++; 11746 if (bnapi->in_reset) 11747 cpr->sw_stats->rx.rx_resets++; 11748 napi_disable_locked(&bnapi->napi); 11749 } 11750 } 11751 11752 static void bnxt_enable_napi(struct bnxt *bp) 11753 { 11754 int i; 11755 11756 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11757 for (i = 0; i < bp->cp_nr_rings; i++) { 11758 struct bnxt_napi *bnapi = bp->bnapi[i]; 11759 struct bnxt_cp_ring_info *cpr; 11760 11761 bnapi->tx_fault = 0; 11762 11763 cpr = &bnapi->cp_ring; 11764 bnapi->in_reset = false; 11765 11766 if (bnapi->rx_ring) { 11767 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11768 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11769 } 11770 napi_enable_locked(&bnapi->napi); 11771 } 11772 } 11773 11774 void bnxt_tx_disable(struct bnxt *bp) 11775 { 11776 int i; 11777 struct bnxt_tx_ring_info *txr; 11778 11779 if (bp->tx_ring) { 11780 for (i = 0; i < bp->tx_nr_rings; i++) { 11781 txr = &bp->tx_ring[i]; 11782 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11783 } 11784 } 11785 /* Make sure napi polls see @dev_state change */ 11786 synchronize_net(); 11787 /* Drop carrier first to prevent TX timeout */ 11788 netif_carrier_off(bp->dev); 11789 /* Stop all TX queues */ 11790 netif_tx_disable(bp->dev); 11791 } 11792 11793 void bnxt_tx_enable(struct bnxt *bp) 11794 { 11795 int i; 11796 struct bnxt_tx_ring_info *txr; 11797 11798 for (i = 0; i < bp->tx_nr_rings; i++) { 11799 txr = &bp->tx_ring[i]; 11800 WRITE_ONCE(txr->dev_state, 0); 11801 } 11802 /* Make sure napi polls see @dev_state change */ 11803 synchronize_net(); 11804 netif_tx_wake_all_queues(bp->dev); 11805 if (BNXT_LINK_IS_UP(bp)) 11806 netif_carrier_on(bp->dev); 11807 } 11808 11809 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11810 { 11811 u8 active_fec = link_info->active_fec_sig_mode & 11812 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11813 11814 switch (active_fec) { 11815 default: 11816 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11817 return "None"; 11818 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11819 return "Clause 74 BaseR"; 11820 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11821 return "Clause 91 RS(528,514)"; 11822 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11823 return "Clause 91 RS544_1XN"; 11824 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11825 return "Clause 91 RS(544,514)"; 11826 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11827 return "Clause 91 RS272_1XN"; 11828 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11829 return "Clause 91 RS(272,257)"; 11830 } 11831 } 11832 11833 void bnxt_report_link(struct bnxt *bp) 11834 { 11835 if (BNXT_LINK_IS_UP(bp)) { 11836 const char *signal = ""; 11837 const char *flow_ctrl; 11838 const char *duplex; 11839 u32 speed; 11840 u16 fec; 11841 11842 netif_carrier_on(bp->dev); 11843 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11844 if (speed == SPEED_UNKNOWN) { 11845 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11846 return; 11847 } 11848 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11849 duplex = "full"; 11850 else 11851 duplex = "half"; 11852 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11853 flow_ctrl = "ON - receive & transmit"; 11854 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11855 flow_ctrl = "ON - transmit"; 11856 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11857 flow_ctrl = "ON - receive"; 11858 else 11859 flow_ctrl = "none"; 11860 if (bp->link_info.phy_qcfg_resp.option_flags & 11861 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11862 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11863 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11864 switch (sig_mode) { 11865 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11866 signal = "(NRZ) "; 11867 break; 11868 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11869 signal = "(PAM4 56Gbps) "; 11870 break; 11871 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11872 signal = "(PAM4 112Gbps) "; 11873 break; 11874 default: 11875 break; 11876 } 11877 } 11878 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11879 speed, signal, duplex, flow_ctrl); 11880 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11881 netdev_info(bp->dev, "EEE is %s\n", 11882 bp->eee.eee_active ? "active" : 11883 "not active"); 11884 fec = bp->link_info.fec_cfg; 11885 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11886 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11887 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11888 bnxt_report_fec(&bp->link_info)); 11889 } else { 11890 netif_carrier_off(bp->dev); 11891 netdev_err(bp->dev, "NIC Link is Down\n"); 11892 } 11893 } 11894 11895 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11896 { 11897 if (!resp->supported_speeds_auto_mode && 11898 !resp->supported_speeds_force_mode && 11899 !resp->supported_pam4_speeds_auto_mode && 11900 !resp->supported_pam4_speeds_force_mode && 11901 !resp->supported_speeds2_auto_mode && 11902 !resp->supported_speeds2_force_mode) 11903 return true; 11904 return false; 11905 } 11906 11907 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11908 { 11909 struct bnxt_link_info *link_info = &bp->link_info; 11910 struct hwrm_port_phy_qcaps_output *resp; 11911 struct hwrm_port_phy_qcaps_input *req; 11912 int rc = 0; 11913 11914 if (bp->hwrm_spec_code < 0x10201) 11915 return 0; 11916 11917 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11918 if (rc) 11919 return rc; 11920 11921 resp = hwrm_req_hold(bp, req); 11922 rc = hwrm_req_send(bp, req); 11923 if (rc) 11924 goto hwrm_phy_qcaps_exit; 11925 11926 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11927 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11928 struct ethtool_keee *eee = &bp->eee; 11929 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11930 11931 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11932 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11933 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11934 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11935 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11936 } 11937 11938 if (bp->hwrm_spec_code >= 0x10a01) { 11939 if (bnxt_phy_qcaps_no_speed(resp)) { 11940 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11941 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11942 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11943 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11944 netdev_info(bp->dev, "Ethernet link enabled\n"); 11945 /* Phy re-enabled, reprobe the speeds */ 11946 link_info->support_auto_speeds = 0; 11947 link_info->support_pam4_auto_speeds = 0; 11948 link_info->support_auto_speeds2 = 0; 11949 } 11950 } 11951 if (resp->supported_speeds_auto_mode) 11952 link_info->support_auto_speeds = 11953 le16_to_cpu(resp->supported_speeds_auto_mode); 11954 if (resp->supported_pam4_speeds_auto_mode) 11955 link_info->support_pam4_auto_speeds = 11956 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11957 if (resp->supported_speeds2_auto_mode) 11958 link_info->support_auto_speeds2 = 11959 le16_to_cpu(resp->supported_speeds2_auto_mode); 11960 11961 bp->port_count = resp->port_cnt; 11962 11963 hwrm_phy_qcaps_exit: 11964 hwrm_req_drop(bp, req); 11965 return rc; 11966 } 11967 11968 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 11969 { 11970 struct hwrm_port_mac_qcaps_output *resp; 11971 struct hwrm_port_mac_qcaps_input *req; 11972 int rc; 11973 11974 if (bp->hwrm_spec_code < 0x10a03) 11975 return; 11976 11977 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 11978 if (rc) 11979 return; 11980 11981 resp = hwrm_req_hold(bp, req); 11982 rc = hwrm_req_send_silent(bp, req); 11983 if (!rc) 11984 bp->mac_flags = resp->flags; 11985 hwrm_req_drop(bp, req); 11986 } 11987 11988 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11989 { 11990 u16 diff = advertising ^ supported; 11991 11992 return ((supported | diff) != supported); 11993 } 11994 11995 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 11996 { 11997 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 11998 11999 /* Check if any advertised speeds are no longer supported. The caller 12000 * holds the link_lock mutex, so we can modify link_info settings. 12001 */ 12002 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12003 if (bnxt_support_dropped(link_info->advertising, 12004 link_info->support_auto_speeds2)) { 12005 link_info->advertising = link_info->support_auto_speeds2; 12006 return true; 12007 } 12008 return false; 12009 } 12010 if (bnxt_support_dropped(link_info->advertising, 12011 link_info->support_auto_speeds)) { 12012 link_info->advertising = link_info->support_auto_speeds; 12013 return true; 12014 } 12015 if (bnxt_support_dropped(link_info->advertising_pam4, 12016 link_info->support_pam4_auto_speeds)) { 12017 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12018 return true; 12019 } 12020 return false; 12021 } 12022 12023 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12024 { 12025 struct bnxt_link_info *link_info = &bp->link_info; 12026 struct hwrm_port_phy_qcfg_output *resp; 12027 struct hwrm_port_phy_qcfg_input *req; 12028 u8 link_state = link_info->link_state; 12029 bool support_changed; 12030 int rc; 12031 12032 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12033 if (rc) 12034 return rc; 12035 12036 resp = hwrm_req_hold(bp, req); 12037 rc = hwrm_req_send(bp, req); 12038 if (rc) { 12039 hwrm_req_drop(bp, req); 12040 if (BNXT_VF(bp) && rc == -ENODEV) { 12041 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12042 rc = 0; 12043 } 12044 return rc; 12045 } 12046 12047 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12048 link_info->phy_link_status = resp->link; 12049 link_info->duplex = resp->duplex_cfg; 12050 if (bp->hwrm_spec_code >= 0x10800) 12051 link_info->duplex = resp->duplex_state; 12052 link_info->pause = resp->pause; 12053 link_info->auto_mode = resp->auto_mode; 12054 link_info->auto_pause_setting = resp->auto_pause; 12055 link_info->lp_pause = resp->link_partner_adv_pause; 12056 link_info->force_pause_setting = resp->force_pause; 12057 link_info->duplex_setting = resp->duplex_cfg; 12058 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12059 link_info->link_speed = le16_to_cpu(resp->link_speed); 12060 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12061 link_info->active_lanes = resp->active_lanes; 12062 } else { 12063 link_info->link_speed = 0; 12064 link_info->active_lanes = 0; 12065 } 12066 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12067 link_info->force_pam4_link_speed = 12068 le16_to_cpu(resp->force_pam4_link_speed); 12069 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12070 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12071 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12072 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12073 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12074 link_info->auto_pam4_link_speeds = 12075 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12076 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12077 link_info->lp_auto_link_speeds = 12078 le16_to_cpu(resp->link_partner_adv_speeds); 12079 link_info->lp_auto_pam4_link_speeds = 12080 resp->link_partner_pam4_adv_speeds; 12081 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12082 link_info->phy_ver[0] = resp->phy_maj; 12083 link_info->phy_ver[1] = resp->phy_min; 12084 link_info->phy_ver[2] = resp->phy_bld; 12085 link_info->media_type = resp->media_type; 12086 link_info->phy_type = resp->phy_type; 12087 link_info->transceiver = resp->xcvr_pkg_type; 12088 link_info->phy_addr = resp->eee_config_phy_addr & 12089 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12090 link_info->module_status = resp->module_status; 12091 12092 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12093 struct ethtool_keee *eee = &bp->eee; 12094 u16 fw_speeds; 12095 12096 eee->eee_active = 0; 12097 if (resp->eee_config_phy_addr & 12098 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12099 eee->eee_active = 1; 12100 fw_speeds = le16_to_cpu( 12101 resp->link_partner_adv_eee_link_speed_mask); 12102 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12103 } 12104 12105 /* Pull initial EEE config */ 12106 if (!chng_link_state) { 12107 if (resp->eee_config_phy_addr & 12108 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12109 eee->eee_enabled = 1; 12110 12111 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12112 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12113 12114 if (resp->eee_config_phy_addr & 12115 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12116 __le32 tmr; 12117 12118 eee->tx_lpi_enabled = 1; 12119 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12120 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12121 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12122 } 12123 } 12124 } 12125 12126 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12127 if (bp->hwrm_spec_code >= 0x10504) { 12128 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12129 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12130 } 12131 /* TODO: need to add more logic to report VF link */ 12132 if (chng_link_state) { 12133 if (link_info->phy_link_status == BNXT_LINK_LINK) 12134 link_info->link_state = BNXT_LINK_STATE_UP; 12135 else 12136 link_info->link_state = BNXT_LINK_STATE_DOWN; 12137 if (link_state != link_info->link_state) 12138 bnxt_report_link(bp); 12139 } else { 12140 /* always link down if not require to update link state */ 12141 link_info->link_state = BNXT_LINK_STATE_DOWN; 12142 } 12143 hwrm_req_drop(bp, req); 12144 12145 if (!BNXT_PHY_CFG_ABLE(bp)) 12146 return 0; 12147 12148 support_changed = bnxt_support_speed_dropped(link_info); 12149 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12150 bnxt_hwrm_set_link_setting(bp, true, false); 12151 return 0; 12152 } 12153 12154 static void bnxt_get_port_module_status(struct bnxt *bp) 12155 { 12156 struct bnxt_link_info *link_info = &bp->link_info; 12157 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12158 u8 module_status; 12159 12160 if (bnxt_update_link(bp, true)) 12161 return; 12162 12163 module_status = link_info->module_status; 12164 switch (module_status) { 12165 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12166 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12167 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12168 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12169 bp->pf.port_id); 12170 if (bp->hwrm_spec_code >= 0x10201) { 12171 netdev_warn(bp->dev, "Module part number %s\n", 12172 resp->phy_vendor_partnumber); 12173 } 12174 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12175 netdev_warn(bp->dev, "TX is disabled\n"); 12176 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12177 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12178 } 12179 } 12180 12181 static void 12182 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12183 { 12184 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12185 if (bp->hwrm_spec_code >= 0x10201) 12186 req->auto_pause = 12187 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12188 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12189 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12190 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12191 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12192 req->enables |= 12193 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12194 } else { 12195 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12196 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12197 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12198 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12199 req->enables |= 12200 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12201 if (bp->hwrm_spec_code >= 0x10201) { 12202 req->auto_pause = req->force_pause; 12203 req->enables |= cpu_to_le32( 12204 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12205 } 12206 } 12207 } 12208 12209 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12210 { 12211 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12212 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12213 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12214 req->enables |= 12215 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12216 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12217 } else if (bp->link_info.advertising) { 12218 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12219 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12220 } 12221 if (bp->link_info.advertising_pam4) { 12222 req->enables |= 12223 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12224 req->auto_link_pam4_speed_mask = 12225 cpu_to_le16(bp->link_info.advertising_pam4); 12226 } 12227 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12228 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12229 } else { 12230 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12231 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12232 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12233 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12234 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12235 (u32)bp->link_info.req_link_speed); 12236 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12237 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12238 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12239 } else { 12240 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12241 } 12242 } 12243 12244 /* tell chimp that the setting takes effect immediately */ 12245 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12246 } 12247 12248 int bnxt_hwrm_set_pause(struct bnxt *bp) 12249 { 12250 struct hwrm_port_phy_cfg_input *req; 12251 int rc; 12252 12253 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12254 if (rc) 12255 return rc; 12256 12257 bnxt_hwrm_set_pause_common(bp, req); 12258 12259 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12260 bp->link_info.force_link_chng) 12261 bnxt_hwrm_set_link_common(bp, req); 12262 12263 rc = hwrm_req_send(bp, req); 12264 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12265 /* since changing of pause setting doesn't trigger any link 12266 * change event, the driver needs to update the current pause 12267 * result upon successfully return of the phy_cfg command 12268 */ 12269 bp->link_info.pause = 12270 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12271 bp->link_info.auto_pause_setting = 0; 12272 if (!bp->link_info.force_link_chng) 12273 bnxt_report_link(bp); 12274 } 12275 bp->link_info.force_link_chng = false; 12276 return rc; 12277 } 12278 12279 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12280 struct hwrm_port_phy_cfg_input *req) 12281 { 12282 struct ethtool_keee *eee = &bp->eee; 12283 12284 if (eee->eee_enabled) { 12285 u16 eee_speeds; 12286 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12287 12288 if (eee->tx_lpi_enabled) 12289 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12290 else 12291 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12292 12293 req->flags |= cpu_to_le32(flags); 12294 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12295 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12296 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12297 } else { 12298 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12299 } 12300 } 12301 12302 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12303 { 12304 struct hwrm_port_phy_cfg_input *req; 12305 int rc; 12306 12307 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12308 if (rc) 12309 return rc; 12310 12311 if (set_pause) 12312 bnxt_hwrm_set_pause_common(bp, req); 12313 12314 bnxt_hwrm_set_link_common(bp, req); 12315 12316 if (set_eee) 12317 bnxt_hwrm_set_eee(bp, req); 12318 return hwrm_req_send(bp, req); 12319 } 12320 12321 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12322 { 12323 struct hwrm_port_phy_cfg_input *req; 12324 int rc; 12325 12326 if (!BNXT_SINGLE_PF(bp)) 12327 return 0; 12328 12329 if (pci_num_vf(bp->pdev) && 12330 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12331 return 0; 12332 12333 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12334 if (rc) 12335 return rc; 12336 12337 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12338 rc = hwrm_req_send(bp, req); 12339 if (!rc) { 12340 mutex_lock(&bp->link_lock); 12341 /* Device is not obliged link down in certain scenarios, even 12342 * when forced. Setting the state unknown is consistent with 12343 * driver startup and will force link state to be reported 12344 * during subsequent open based on PORT_PHY_QCFG. 12345 */ 12346 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12347 mutex_unlock(&bp->link_lock); 12348 } 12349 return rc; 12350 } 12351 12352 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12353 { 12354 #ifdef CONFIG_TEE_BNXT_FW 12355 int rc = tee_bnxt_fw_load(); 12356 12357 if (rc) 12358 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12359 12360 return rc; 12361 #else 12362 netdev_err(bp->dev, "OP-TEE not supported\n"); 12363 return -ENODEV; 12364 #endif 12365 } 12366 12367 static int bnxt_try_recover_fw(struct bnxt *bp) 12368 { 12369 if (bp->fw_health && bp->fw_health->status_reliable) { 12370 int retry = 0, rc; 12371 u32 sts; 12372 12373 do { 12374 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12375 rc = bnxt_hwrm_poll(bp); 12376 if (!BNXT_FW_IS_BOOTING(sts) && 12377 !BNXT_FW_IS_RECOVERING(sts)) 12378 break; 12379 retry++; 12380 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12381 12382 if (!BNXT_FW_IS_HEALTHY(sts)) { 12383 netdev_err(bp->dev, 12384 "Firmware not responding, status: 0x%x\n", 12385 sts); 12386 rc = -ENODEV; 12387 } 12388 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12389 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12390 return bnxt_fw_reset_via_optee(bp); 12391 } 12392 return rc; 12393 } 12394 12395 return -ENODEV; 12396 } 12397 12398 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12399 { 12400 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12401 12402 if (!BNXT_NEW_RM(bp)) 12403 return; /* no resource reservations required */ 12404 12405 hw_resc->resv_cp_rings = 0; 12406 hw_resc->resv_stat_ctxs = 0; 12407 hw_resc->resv_irqs = 0; 12408 hw_resc->resv_tx_rings = 0; 12409 hw_resc->resv_rx_rings = 0; 12410 hw_resc->resv_hw_ring_grps = 0; 12411 hw_resc->resv_vnics = 0; 12412 hw_resc->resv_rsscos_ctxs = 0; 12413 if (!fw_reset) { 12414 bp->tx_nr_rings = 0; 12415 bp->rx_nr_rings = 0; 12416 } 12417 } 12418 12419 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12420 { 12421 int rc; 12422 12423 if (!BNXT_NEW_RM(bp)) 12424 return 0; /* no resource reservations required */ 12425 12426 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12427 if (rc) 12428 netdev_err(bp->dev, "resc_qcaps failed\n"); 12429 12430 bnxt_clear_reservations(bp, fw_reset); 12431 12432 return rc; 12433 } 12434 12435 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12436 { 12437 struct hwrm_func_drv_if_change_output *resp; 12438 struct hwrm_func_drv_if_change_input *req; 12439 bool resc_reinit = false; 12440 bool caps_change = false; 12441 int rc, retry = 0; 12442 bool fw_reset; 12443 u32 flags = 0; 12444 12445 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12446 bp->fw_reset_state = 0; 12447 12448 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12449 return 0; 12450 12451 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12452 if (rc) 12453 return rc; 12454 12455 if (up) 12456 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12457 resp = hwrm_req_hold(bp, req); 12458 12459 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12460 while (retry < BNXT_FW_IF_RETRY) { 12461 rc = hwrm_req_send(bp, req); 12462 if (rc != -EAGAIN) 12463 break; 12464 12465 msleep(50); 12466 retry++; 12467 } 12468 12469 if (rc == -EAGAIN) { 12470 hwrm_req_drop(bp, req); 12471 return rc; 12472 } else if (!rc) { 12473 flags = le32_to_cpu(resp->flags); 12474 } else if (up) { 12475 rc = bnxt_try_recover_fw(bp); 12476 fw_reset = true; 12477 } 12478 hwrm_req_drop(bp, req); 12479 if (rc) 12480 return rc; 12481 12482 if (!up) { 12483 bnxt_inv_fw_health_reg(bp); 12484 return 0; 12485 } 12486 12487 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12488 resc_reinit = true; 12489 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12490 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12491 fw_reset = true; 12492 else 12493 bnxt_remap_fw_health_regs(bp); 12494 12495 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12496 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12497 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12498 return -ENODEV; 12499 } 12500 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12501 caps_change = true; 12502 12503 if (resc_reinit || fw_reset || caps_change) { 12504 if (fw_reset || caps_change) { 12505 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12506 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12507 bnxt_ulp_irq_stop(bp); 12508 bnxt_free_ctx_mem(bp, false); 12509 bnxt_dcb_free(bp); 12510 rc = bnxt_fw_init_one(bp); 12511 if (rc) { 12512 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12513 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12514 return rc; 12515 } 12516 /* IRQ will be initialized later in bnxt_request_irq()*/ 12517 bnxt_clear_int_mode(bp); 12518 } 12519 rc = bnxt_cancel_reservations(bp, fw_reset); 12520 } 12521 return rc; 12522 } 12523 12524 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12525 { 12526 struct hwrm_port_led_qcaps_output *resp; 12527 struct hwrm_port_led_qcaps_input *req; 12528 struct bnxt_pf_info *pf = &bp->pf; 12529 int rc; 12530 12531 bp->num_leds = 0; 12532 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12533 return 0; 12534 12535 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12536 if (rc) 12537 return rc; 12538 12539 req->port_id = cpu_to_le16(pf->port_id); 12540 resp = hwrm_req_hold(bp, req); 12541 rc = hwrm_req_send(bp, req); 12542 if (rc) { 12543 hwrm_req_drop(bp, req); 12544 return rc; 12545 } 12546 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12547 int i; 12548 12549 bp->num_leds = resp->num_leds; 12550 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12551 bp->num_leds); 12552 for (i = 0; i < bp->num_leds; i++) { 12553 struct bnxt_led_info *led = &bp->leds[i]; 12554 __le16 caps = led->led_state_caps; 12555 12556 if (!led->led_group_id || 12557 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12558 bp->num_leds = 0; 12559 break; 12560 } 12561 } 12562 } 12563 hwrm_req_drop(bp, req); 12564 return 0; 12565 } 12566 12567 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12568 { 12569 struct hwrm_wol_filter_alloc_output *resp; 12570 struct hwrm_wol_filter_alloc_input *req; 12571 int rc; 12572 12573 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12574 if (rc) 12575 return rc; 12576 12577 req->port_id = cpu_to_le16(bp->pf.port_id); 12578 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12579 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12580 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12581 12582 resp = hwrm_req_hold(bp, req); 12583 rc = hwrm_req_send(bp, req); 12584 if (!rc) 12585 bp->wol_filter_id = resp->wol_filter_id; 12586 hwrm_req_drop(bp, req); 12587 return rc; 12588 } 12589 12590 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12591 { 12592 struct hwrm_wol_filter_free_input *req; 12593 int rc; 12594 12595 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12596 if (rc) 12597 return rc; 12598 12599 req->port_id = cpu_to_le16(bp->pf.port_id); 12600 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12601 req->wol_filter_id = bp->wol_filter_id; 12602 12603 return hwrm_req_send(bp, req); 12604 } 12605 12606 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12607 { 12608 struct hwrm_wol_filter_qcfg_output *resp; 12609 struct hwrm_wol_filter_qcfg_input *req; 12610 u16 next_handle = 0; 12611 int rc; 12612 12613 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12614 if (rc) 12615 return rc; 12616 12617 req->port_id = cpu_to_le16(bp->pf.port_id); 12618 req->handle = cpu_to_le16(handle); 12619 resp = hwrm_req_hold(bp, req); 12620 rc = hwrm_req_send(bp, req); 12621 if (!rc) { 12622 next_handle = le16_to_cpu(resp->next_handle); 12623 if (next_handle != 0) { 12624 if (resp->wol_type == 12625 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12626 bp->wol = 1; 12627 bp->wol_filter_id = resp->wol_filter_id; 12628 } 12629 } 12630 } 12631 hwrm_req_drop(bp, req); 12632 return next_handle; 12633 } 12634 12635 static void bnxt_get_wol_settings(struct bnxt *bp) 12636 { 12637 u16 handle = 0; 12638 12639 bp->wol = 0; 12640 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12641 return; 12642 12643 do { 12644 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12645 } while (handle && handle != 0xffff); 12646 } 12647 12648 static bool bnxt_eee_config_ok(struct bnxt *bp) 12649 { 12650 struct ethtool_keee *eee = &bp->eee; 12651 struct bnxt_link_info *link_info = &bp->link_info; 12652 12653 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12654 return true; 12655 12656 if (eee->eee_enabled) { 12657 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12658 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12659 12660 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12661 12662 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12663 eee->eee_enabled = 0; 12664 return false; 12665 } 12666 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12667 linkmode_and(eee->advertised, advertising, 12668 eee->supported); 12669 return false; 12670 } 12671 } 12672 return true; 12673 } 12674 12675 static int bnxt_update_phy_setting(struct bnxt *bp) 12676 { 12677 int rc; 12678 bool update_link = false; 12679 bool update_pause = false; 12680 bool update_eee = false; 12681 struct bnxt_link_info *link_info = &bp->link_info; 12682 12683 rc = bnxt_update_link(bp, true); 12684 if (rc) { 12685 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12686 rc); 12687 return rc; 12688 } 12689 if (!BNXT_SINGLE_PF(bp)) 12690 return 0; 12691 12692 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12693 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12694 link_info->req_flow_ctrl) 12695 update_pause = true; 12696 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12697 link_info->force_pause_setting != link_info->req_flow_ctrl) 12698 update_pause = true; 12699 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12700 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12701 update_link = true; 12702 if (bnxt_force_speed_updated(link_info)) 12703 update_link = true; 12704 if (link_info->req_duplex != link_info->duplex_setting) 12705 update_link = true; 12706 } else { 12707 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12708 update_link = true; 12709 if (bnxt_auto_speed_updated(link_info)) 12710 update_link = true; 12711 } 12712 12713 /* The last close may have shutdown the link, so need to call 12714 * PHY_CFG to bring it back up. 12715 */ 12716 if (!BNXT_LINK_IS_UP(bp)) 12717 update_link = true; 12718 12719 if (!bnxt_eee_config_ok(bp)) 12720 update_eee = true; 12721 12722 if (update_link) 12723 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12724 else if (update_pause) 12725 rc = bnxt_hwrm_set_pause(bp); 12726 if (rc) { 12727 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12728 rc); 12729 return rc; 12730 } 12731 12732 return rc; 12733 } 12734 12735 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12736 12737 static int bnxt_reinit_after_abort(struct bnxt *bp) 12738 { 12739 int rc; 12740 12741 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12742 return -EBUSY; 12743 12744 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12745 return -ENODEV; 12746 12747 rc = bnxt_fw_init_one(bp); 12748 if (!rc) { 12749 bnxt_clear_int_mode(bp); 12750 rc = bnxt_init_int_mode(bp); 12751 if (!rc) { 12752 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12753 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12754 } 12755 } 12756 return rc; 12757 } 12758 12759 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12760 { 12761 struct bnxt_ntuple_filter *ntp_fltr; 12762 struct bnxt_l2_filter *l2_fltr; 12763 12764 if (list_empty(&fltr->list)) 12765 return; 12766 12767 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12768 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12769 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12770 atomic_inc(&l2_fltr->refcnt); 12771 ntp_fltr->l2_fltr = l2_fltr; 12772 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12773 bnxt_del_ntp_filter(bp, ntp_fltr); 12774 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12775 fltr->sw_id); 12776 } 12777 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12778 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12779 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12780 bnxt_del_l2_filter(bp, l2_fltr); 12781 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12782 fltr->sw_id); 12783 } 12784 } 12785 } 12786 12787 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12788 { 12789 struct bnxt_filter_base *usr_fltr, *tmp; 12790 12791 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12792 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12793 } 12794 12795 static int bnxt_set_xps_mapping(struct bnxt *bp) 12796 { 12797 int numa_node = dev_to_node(&bp->pdev->dev); 12798 unsigned int q_idx, map_idx, cpu, i; 12799 const struct cpumask *cpu_mask_ptr; 12800 int nr_cpus = num_online_cpus(); 12801 cpumask_t *q_map; 12802 int rc = 0; 12803 12804 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12805 if (!q_map) 12806 return -ENOMEM; 12807 12808 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12809 * Each TC has the same number of TX queues. The nth TX queue for each 12810 * TC will have the same CPU mask. 12811 */ 12812 for (i = 0; i < nr_cpus; i++) { 12813 map_idx = i % bp->tx_nr_rings_per_tc; 12814 cpu = cpumask_local_spread(i, numa_node); 12815 cpu_mask_ptr = get_cpu_mask(cpu); 12816 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12817 } 12818 12819 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12820 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12821 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12822 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12823 if (rc) { 12824 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12825 q_idx); 12826 break; 12827 } 12828 } 12829 12830 kfree(q_map); 12831 12832 return rc; 12833 } 12834 12835 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12836 { 12837 int rc = 0; 12838 12839 netif_carrier_off(bp->dev); 12840 if (irq_re_init) { 12841 /* Reserve rings now if none were reserved at driver probe. */ 12842 rc = bnxt_init_dflt_ring_mode(bp); 12843 if (rc) { 12844 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12845 return rc; 12846 } 12847 } 12848 rc = bnxt_reserve_rings(bp, irq_re_init); 12849 if (rc) 12850 return rc; 12851 12852 rc = bnxt_alloc_mem(bp, irq_re_init); 12853 if (rc) { 12854 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12855 goto open_err_free_mem; 12856 } 12857 12858 if (irq_re_init) { 12859 bnxt_init_napi(bp); 12860 rc = bnxt_request_irq(bp); 12861 if (rc) { 12862 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12863 goto open_err_irq; 12864 } 12865 } 12866 12867 rc = bnxt_init_nic(bp, irq_re_init); 12868 if (rc) { 12869 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12870 goto open_err_irq; 12871 } 12872 12873 bnxt_enable_napi(bp); 12874 bnxt_debug_dev_init(bp); 12875 12876 if (link_re_init) { 12877 mutex_lock(&bp->link_lock); 12878 rc = bnxt_update_phy_setting(bp); 12879 mutex_unlock(&bp->link_lock); 12880 if (rc) { 12881 netdev_warn(bp->dev, "failed to update phy settings\n"); 12882 if (BNXT_SINGLE_PF(bp)) { 12883 bp->link_info.phy_retry = true; 12884 bp->link_info.phy_retry_expires = 12885 jiffies + 5 * HZ; 12886 } 12887 } 12888 } 12889 12890 if (irq_re_init) { 12891 udp_tunnel_nic_reset_ntf(bp->dev); 12892 rc = bnxt_set_xps_mapping(bp); 12893 if (rc) 12894 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12895 } 12896 12897 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12898 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12899 static_branch_enable(&bnxt_xdp_locking_key); 12900 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12901 static_branch_disable(&bnxt_xdp_locking_key); 12902 } 12903 set_bit(BNXT_STATE_OPEN, &bp->state); 12904 bnxt_enable_int(bp); 12905 /* Enable TX queues */ 12906 bnxt_tx_enable(bp); 12907 mod_timer(&bp->timer, jiffies + bp->current_interval); 12908 /* Poll link status and check for SFP+ module status */ 12909 mutex_lock(&bp->link_lock); 12910 bnxt_get_port_module_status(bp); 12911 mutex_unlock(&bp->link_lock); 12912 12913 /* VF-reps may need to be re-opened after the PF is re-opened */ 12914 if (BNXT_PF(bp)) 12915 bnxt_vf_reps_open(bp); 12916 bnxt_ptp_init_rtc(bp, true); 12917 bnxt_ptp_cfg_tstamp_filters(bp); 12918 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12919 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12920 bnxt_cfg_usr_fltrs(bp); 12921 return 0; 12922 12923 open_err_irq: 12924 bnxt_del_napi(bp); 12925 12926 open_err_free_mem: 12927 bnxt_free_skbs(bp); 12928 bnxt_free_irq(bp); 12929 bnxt_free_mem(bp, true); 12930 return rc; 12931 } 12932 12933 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12934 { 12935 int rc = 0; 12936 12937 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12938 rc = -EIO; 12939 if (!rc) 12940 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12941 if (rc) { 12942 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12943 netif_close(bp->dev); 12944 } 12945 return rc; 12946 } 12947 12948 /* netdev instance lock held, open the NIC half way by allocating all 12949 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 12950 * for offline self tests. 12951 */ 12952 int bnxt_half_open_nic(struct bnxt *bp) 12953 { 12954 int rc = 0; 12955 12956 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12957 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12958 rc = -ENODEV; 12959 goto half_open_err; 12960 } 12961 12962 rc = bnxt_alloc_mem(bp, true); 12963 if (rc) { 12964 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12965 goto half_open_err; 12966 } 12967 bnxt_init_napi(bp); 12968 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12969 rc = bnxt_init_nic(bp, true); 12970 if (rc) { 12971 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12972 bnxt_del_napi(bp); 12973 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12974 goto half_open_err; 12975 } 12976 return 0; 12977 12978 half_open_err: 12979 bnxt_free_skbs(bp); 12980 bnxt_free_mem(bp, true); 12981 netif_close(bp->dev); 12982 return rc; 12983 } 12984 12985 /* netdev instance lock held, this call can only be made after a previous 12986 * successful call to bnxt_half_open_nic(). 12987 */ 12988 void bnxt_half_close_nic(struct bnxt *bp) 12989 { 12990 bnxt_hwrm_resource_free(bp, false, true); 12991 bnxt_del_napi(bp); 12992 bnxt_free_skbs(bp); 12993 bnxt_free_mem(bp, true); 12994 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12995 } 12996 12997 void bnxt_reenable_sriov(struct bnxt *bp) 12998 { 12999 if (BNXT_PF(bp)) { 13000 struct bnxt_pf_info *pf = &bp->pf; 13001 int n = pf->active_vfs; 13002 13003 if (n) 13004 bnxt_cfg_hw_sriov(bp, &n, true); 13005 } 13006 } 13007 13008 static int bnxt_open(struct net_device *dev) 13009 { 13010 struct bnxt *bp = netdev_priv(dev); 13011 int rc; 13012 13013 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13014 rc = bnxt_reinit_after_abort(bp); 13015 if (rc) { 13016 if (rc == -EBUSY) 13017 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13018 else 13019 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13020 return -ENODEV; 13021 } 13022 } 13023 13024 rc = bnxt_hwrm_if_change(bp, true); 13025 if (rc) 13026 return rc; 13027 13028 rc = __bnxt_open_nic(bp, true, true); 13029 if (rc) { 13030 bnxt_hwrm_if_change(bp, false); 13031 } else { 13032 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13033 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13034 bnxt_queue_sp_work(bp, 13035 BNXT_RESTART_ULP_SP_EVENT); 13036 } 13037 } 13038 13039 return rc; 13040 } 13041 13042 static bool bnxt_drv_busy(struct bnxt *bp) 13043 { 13044 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13045 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13046 } 13047 13048 static void bnxt_get_ring_stats(struct bnxt *bp, 13049 struct rtnl_link_stats64 *stats); 13050 13051 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13052 bool link_re_init) 13053 { 13054 /* Close the VF-reps before closing PF */ 13055 if (BNXT_PF(bp)) 13056 bnxt_vf_reps_close(bp); 13057 13058 /* Change device state to avoid TX queue wake up's */ 13059 bnxt_tx_disable(bp); 13060 13061 clear_bit(BNXT_STATE_OPEN, &bp->state); 13062 smp_mb__after_atomic(); 13063 while (bnxt_drv_busy(bp)) 13064 msleep(20); 13065 13066 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13067 bnxt_clear_rss_ctxs(bp); 13068 /* Flush rings and disable interrupts */ 13069 bnxt_shutdown_nic(bp, irq_re_init); 13070 13071 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13072 13073 bnxt_debug_dev_exit(bp); 13074 bnxt_disable_napi(bp); 13075 timer_delete_sync(&bp->timer); 13076 bnxt_free_skbs(bp); 13077 13078 /* Save ring stats before shutdown */ 13079 if (bp->bnapi && irq_re_init) { 13080 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13081 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 13082 } 13083 if (irq_re_init) { 13084 bnxt_free_irq(bp); 13085 bnxt_del_napi(bp); 13086 } 13087 bnxt_free_mem(bp, irq_re_init); 13088 } 13089 13090 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13091 { 13092 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13093 /* If we get here, it means firmware reset is in progress 13094 * while we are trying to close. We can safely proceed with 13095 * the close because we are holding netdev instance lock. 13096 * Some firmware messages may fail as we proceed to close. 13097 * We set the ABORT_ERR flag here so that the FW reset thread 13098 * will later abort when it gets the netdev instance lock 13099 * and sees the flag. 13100 */ 13101 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13102 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13103 } 13104 13105 #ifdef CONFIG_BNXT_SRIOV 13106 if (bp->sriov_cfg) { 13107 int rc; 13108 13109 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13110 !bp->sriov_cfg, 13111 BNXT_SRIOV_CFG_WAIT_TMO); 13112 if (!rc) 13113 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13114 else if (rc < 0) 13115 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13116 } 13117 #endif 13118 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13119 } 13120 13121 static int bnxt_close(struct net_device *dev) 13122 { 13123 struct bnxt *bp = netdev_priv(dev); 13124 13125 bnxt_close_nic(bp, true, true); 13126 bnxt_hwrm_shutdown_link(bp); 13127 bnxt_hwrm_if_change(bp, false); 13128 return 0; 13129 } 13130 13131 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13132 u16 *val) 13133 { 13134 struct hwrm_port_phy_mdio_read_output *resp; 13135 struct hwrm_port_phy_mdio_read_input *req; 13136 int rc; 13137 13138 if (bp->hwrm_spec_code < 0x10a00) 13139 return -EOPNOTSUPP; 13140 13141 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13142 if (rc) 13143 return rc; 13144 13145 req->port_id = cpu_to_le16(bp->pf.port_id); 13146 req->phy_addr = phy_addr; 13147 req->reg_addr = cpu_to_le16(reg & 0x1f); 13148 if (mdio_phy_id_is_c45(phy_addr)) { 13149 req->cl45_mdio = 1; 13150 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13151 req->dev_addr = mdio_phy_id_devad(phy_addr); 13152 req->reg_addr = cpu_to_le16(reg); 13153 } 13154 13155 resp = hwrm_req_hold(bp, req); 13156 rc = hwrm_req_send(bp, req); 13157 if (!rc) 13158 *val = le16_to_cpu(resp->reg_data); 13159 hwrm_req_drop(bp, req); 13160 return rc; 13161 } 13162 13163 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13164 u16 val) 13165 { 13166 struct hwrm_port_phy_mdio_write_input *req; 13167 int rc; 13168 13169 if (bp->hwrm_spec_code < 0x10a00) 13170 return -EOPNOTSUPP; 13171 13172 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13173 if (rc) 13174 return rc; 13175 13176 req->port_id = cpu_to_le16(bp->pf.port_id); 13177 req->phy_addr = phy_addr; 13178 req->reg_addr = cpu_to_le16(reg & 0x1f); 13179 if (mdio_phy_id_is_c45(phy_addr)) { 13180 req->cl45_mdio = 1; 13181 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13182 req->dev_addr = mdio_phy_id_devad(phy_addr); 13183 req->reg_addr = cpu_to_le16(reg); 13184 } 13185 req->reg_data = cpu_to_le16(val); 13186 13187 return hwrm_req_send(bp, req); 13188 } 13189 13190 /* netdev instance lock held */ 13191 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13192 { 13193 struct mii_ioctl_data *mdio = if_mii(ifr); 13194 struct bnxt *bp = netdev_priv(dev); 13195 int rc; 13196 13197 switch (cmd) { 13198 case SIOCGMIIPHY: 13199 mdio->phy_id = bp->link_info.phy_addr; 13200 13201 fallthrough; 13202 case SIOCGMIIREG: { 13203 u16 mii_regval = 0; 13204 13205 if (!netif_running(dev)) 13206 return -EAGAIN; 13207 13208 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13209 &mii_regval); 13210 mdio->val_out = mii_regval; 13211 return rc; 13212 } 13213 13214 case SIOCSMIIREG: 13215 if (!netif_running(dev)) 13216 return -EAGAIN; 13217 13218 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13219 mdio->val_in); 13220 13221 case SIOCSHWTSTAMP: 13222 return bnxt_hwtstamp_set(dev, ifr); 13223 13224 case SIOCGHWTSTAMP: 13225 return bnxt_hwtstamp_get(dev, ifr); 13226 13227 default: 13228 /* do nothing */ 13229 break; 13230 } 13231 return -EOPNOTSUPP; 13232 } 13233 13234 static void bnxt_get_ring_stats(struct bnxt *bp, 13235 struct rtnl_link_stats64 *stats) 13236 { 13237 int i; 13238 13239 for (i = 0; i < bp->cp_nr_rings; i++) { 13240 struct bnxt_napi *bnapi = bp->bnapi[i]; 13241 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13242 u64 *sw = cpr->stats.sw_stats; 13243 13244 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13245 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13246 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13247 13248 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13249 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13250 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13251 13252 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13253 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13254 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13255 13256 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13257 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13258 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13259 13260 stats->rx_missed_errors += 13261 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13262 13263 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13264 13265 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13266 13267 stats->rx_dropped += 13268 cpr->sw_stats->rx.rx_netpoll_discards + 13269 cpr->sw_stats->rx.rx_oom_discards; 13270 } 13271 } 13272 13273 static void bnxt_add_prev_stats(struct bnxt *bp, 13274 struct rtnl_link_stats64 *stats) 13275 { 13276 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13277 13278 stats->rx_packets += prev_stats->rx_packets; 13279 stats->tx_packets += prev_stats->tx_packets; 13280 stats->rx_bytes += prev_stats->rx_bytes; 13281 stats->tx_bytes += prev_stats->tx_bytes; 13282 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13283 stats->multicast += prev_stats->multicast; 13284 stats->rx_dropped += prev_stats->rx_dropped; 13285 stats->tx_dropped += prev_stats->tx_dropped; 13286 } 13287 13288 static void 13289 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13290 { 13291 struct bnxt *bp = netdev_priv(dev); 13292 13293 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13294 /* Make sure bnxt_close_nic() sees that we are reading stats before 13295 * we check the BNXT_STATE_OPEN flag. 13296 */ 13297 smp_mb__after_atomic(); 13298 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13299 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13300 *stats = bp->net_stats_prev; 13301 return; 13302 } 13303 13304 bnxt_get_ring_stats(bp, stats); 13305 bnxt_add_prev_stats(bp, stats); 13306 13307 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13308 u64 *rx = bp->port_stats.sw_stats; 13309 u64 *tx = bp->port_stats.sw_stats + 13310 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13311 13312 stats->rx_crc_errors = 13313 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13314 stats->rx_frame_errors = 13315 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13316 stats->rx_length_errors = 13317 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13318 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13319 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13320 stats->rx_errors = 13321 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13322 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13323 stats->collisions = 13324 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13325 stats->tx_fifo_errors = 13326 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13327 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13328 } 13329 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13330 } 13331 13332 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 13333 struct bnxt_total_ring_err_stats *stats, 13334 struct bnxt_cp_ring_info *cpr) 13335 { 13336 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13337 u64 *hw_stats = cpr->stats.sw_stats; 13338 13339 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13340 stats->rx_total_resets += sw_stats->rx.rx_resets; 13341 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13342 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13343 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13344 stats->rx_total_ring_discards += 13345 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13346 stats->tx_total_resets += sw_stats->tx.tx_resets; 13347 stats->tx_total_ring_discards += 13348 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13349 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13350 } 13351 13352 void bnxt_get_ring_err_stats(struct bnxt *bp, 13353 struct bnxt_total_ring_err_stats *stats) 13354 { 13355 int i; 13356 13357 for (i = 0; i < bp->cp_nr_rings; i++) 13358 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13359 } 13360 13361 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 13362 { 13363 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13364 struct net_device *dev = bp->dev; 13365 struct netdev_hw_addr *ha; 13366 u8 *haddr; 13367 int mc_count = 0; 13368 bool update = false; 13369 int off = 0; 13370 13371 netdev_for_each_mc_addr(ha, dev) { 13372 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13373 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13374 vnic->mc_list_count = 0; 13375 return false; 13376 } 13377 haddr = ha->addr; 13378 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13379 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13380 update = true; 13381 } 13382 off += ETH_ALEN; 13383 mc_count++; 13384 } 13385 if (mc_count) 13386 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13387 13388 if (mc_count != vnic->mc_list_count) { 13389 vnic->mc_list_count = mc_count; 13390 update = true; 13391 } 13392 return update; 13393 } 13394 13395 static bool bnxt_uc_list_updated(struct bnxt *bp) 13396 { 13397 struct net_device *dev = bp->dev; 13398 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13399 struct netdev_hw_addr *ha; 13400 int off = 0; 13401 13402 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13403 return true; 13404 13405 netdev_for_each_uc_addr(ha, dev) { 13406 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13407 return true; 13408 13409 off += ETH_ALEN; 13410 } 13411 return false; 13412 } 13413 13414 static void bnxt_set_rx_mode(struct net_device *dev) 13415 { 13416 struct bnxt *bp = netdev_priv(dev); 13417 struct bnxt_vnic_info *vnic; 13418 bool mc_update = false; 13419 bool uc_update; 13420 u32 mask; 13421 13422 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13423 return; 13424 13425 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13426 mask = vnic->rx_mask; 13427 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13428 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13429 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13430 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13431 13432 if (dev->flags & IFF_PROMISC) 13433 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13434 13435 uc_update = bnxt_uc_list_updated(bp); 13436 13437 if (dev->flags & IFF_BROADCAST) 13438 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13439 if (dev->flags & IFF_ALLMULTI) { 13440 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13441 vnic->mc_list_count = 0; 13442 } else if (dev->flags & IFF_MULTICAST) { 13443 mc_update = bnxt_mc_list_updated(bp, &mask); 13444 } 13445 13446 if (mask != vnic->rx_mask || uc_update || mc_update) { 13447 vnic->rx_mask = mask; 13448 13449 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13450 } 13451 } 13452 13453 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13454 { 13455 struct net_device *dev = bp->dev; 13456 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13457 struct netdev_hw_addr *ha; 13458 int i, off = 0, rc; 13459 bool uc_update; 13460 13461 netif_addr_lock_bh(dev); 13462 uc_update = bnxt_uc_list_updated(bp); 13463 netif_addr_unlock_bh(dev); 13464 13465 if (!uc_update) 13466 goto skip_uc; 13467 13468 for (i = 1; i < vnic->uc_filter_count; i++) { 13469 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13470 13471 bnxt_hwrm_l2_filter_free(bp, fltr); 13472 bnxt_del_l2_filter(bp, fltr); 13473 } 13474 13475 vnic->uc_filter_count = 1; 13476 13477 netif_addr_lock_bh(dev); 13478 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13479 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13480 } else { 13481 netdev_for_each_uc_addr(ha, dev) { 13482 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13483 off += ETH_ALEN; 13484 vnic->uc_filter_count++; 13485 } 13486 } 13487 netif_addr_unlock_bh(dev); 13488 13489 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13490 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13491 if (rc) { 13492 if (BNXT_VF(bp) && rc == -ENODEV) { 13493 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13494 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13495 else 13496 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13497 rc = 0; 13498 } else { 13499 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13500 } 13501 vnic->uc_filter_count = i; 13502 return rc; 13503 } 13504 } 13505 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13506 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13507 13508 skip_uc: 13509 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13510 !bnxt_promisc_ok(bp)) 13511 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13512 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13513 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13514 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13515 rc); 13516 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13517 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13518 vnic->mc_list_count = 0; 13519 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13520 } 13521 if (rc) 13522 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13523 rc); 13524 13525 return rc; 13526 } 13527 13528 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13529 { 13530 #ifdef CONFIG_BNXT_SRIOV 13531 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13532 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13533 13534 /* No minimum rings were provisioned by the PF. Don't 13535 * reserve rings by default when device is down. 13536 */ 13537 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13538 return true; 13539 13540 if (!netif_running(bp->dev)) 13541 return false; 13542 } 13543 #endif 13544 return true; 13545 } 13546 13547 /* If the chip and firmware supports RFS */ 13548 static bool bnxt_rfs_supported(struct bnxt *bp) 13549 { 13550 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13551 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13552 return true; 13553 return false; 13554 } 13555 /* 212 firmware is broken for aRFS */ 13556 if (BNXT_FW_MAJ(bp) == 212) 13557 return false; 13558 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13559 return true; 13560 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13561 return true; 13562 return false; 13563 } 13564 13565 /* If runtime conditions support RFS */ 13566 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13567 { 13568 struct bnxt_hw_rings hwr = {0}; 13569 int max_vnics, max_rss_ctxs; 13570 13571 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13572 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13573 return bnxt_rfs_supported(bp); 13574 13575 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13576 return false; 13577 13578 hwr.grp = bp->rx_nr_rings; 13579 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13580 if (new_rss_ctx) 13581 hwr.vnic++; 13582 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13583 max_vnics = bnxt_get_max_func_vnics(bp); 13584 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13585 13586 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13587 if (bp->rx_nr_rings > 1) 13588 netdev_warn(bp->dev, 13589 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13590 min(max_rss_ctxs - 1, max_vnics - 1)); 13591 return false; 13592 } 13593 13594 if (!BNXT_NEW_RM(bp)) 13595 return true; 13596 13597 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13598 * issue that will mess up the default VNIC if we reduce the 13599 * reservations. 13600 */ 13601 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13602 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13603 return true; 13604 13605 bnxt_hwrm_reserve_rings(bp, &hwr); 13606 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13607 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13608 return true; 13609 13610 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13611 hwr.vnic = 1; 13612 hwr.rss_ctx = 0; 13613 bnxt_hwrm_reserve_rings(bp, &hwr); 13614 return false; 13615 } 13616 13617 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13618 netdev_features_t features) 13619 { 13620 struct bnxt *bp = netdev_priv(dev); 13621 netdev_features_t vlan_features; 13622 13623 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13624 features &= ~NETIF_F_NTUPLE; 13625 13626 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13627 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13628 13629 if (!(features & NETIF_F_GRO)) 13630 features &= ~NETIF_F_GRO_HW; 13631 13632 if (features & NETIF_F_GRO_HW) 13633 features &= ~NETIF_F_LRO; 13634 13635 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13636 * turned on or off together. 13637 */ 13638 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13639 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13640 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13641 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13642 else if (vlan_features) 13643 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13644 } 13645 #ifdef CONFIG_BNXT_SRIOV 13646 if (BNXT_VF(bp) && bp->vf.vlan) 13647 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13648 #endif 13649 return features; 13650 } 13651 13652 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13653 bool link_re_init, u32 flags, bool update_tpa) 13654 { 13655 bnxt_close_nic(bp, irq_re_init, link_re_init); 13656 bp->flags = flags; 13657 if (update_tpa) 13658 bnxt_set_ring_params(bp); 13659 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13660 } 13661 13662 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13663 { 13664 bool update_tpa = false, update_ntuple = false; 13665 struct bnxt *bp = netdev_priv(dev); 13666 u32 flags = bp->flags; 13667 u32 changes; 13668 int rc = 0; 13669 bool re_init = false; 13670 13671 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13672 if (features & NETIF_F_GRO_HW) 13673 flags |= BNXT_FLAG_GRO; 13674 else if (features & NETIF_F_LRO) 13675 flags |= BNXT_FLAG_LRO; 13676 13677 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13678 flags &= ~BNXT_FLAG_TPA; 13679 13680 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13681 flags |= BNXT_FLAG_STRIP_VLAN; 13682 13683 if (features & NETIF_F_NTUPLE) 13684 flags |= BNXT_FLAG_RFS; 13685 else 13686 bnxt_clear_usr_fltrs(bp, true); 13687 13688 changes = flags ^ bp->flags; 13689 if (changes & BNXT_FLAG_TPA) { 13690 update_tpa = true; 13691 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13692 (flags & BNXT_FLAG_TPA) == 0 || 13693 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13694 re_init = true; 13695 } 13696 13697 if (changes & ~BNXT_FLAG_TPA) 13698 re_init = true; 13699 13700 if (changes & BNXT_FLAG_RFS) 13701 update_ntuple = true; 13702 13703 if (flags != bp->flags) { 13704 u32 old_flags = bp->flags; 13705 13706 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13707 bp->flags = flags; 13708 if (update_tpa) 13709 bnxt_set_ring_params(bp); 13710 return rc; 13711 } 13712 13713 if (update_ntuple) 13714 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13715 13716 if (re_init) 13717 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13718 13719 if (update_tpa) { 13720 bp->flags = flags; 13721 rc = bnxt_set_tpa(bp, 13722 (flags & BNXT_FLAG_TPA) ? 13723 true : false); 13724 if (rc) 13725 bp->flags = old_flags; 13726 } 13727 } 13728 return rc; 13729 } 13730 13731 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13732 u8 **nextp) 13733 { 13734 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13735 struct hop_jumbo_hdr *jhdr; 13736 int hdr_count = 0; 13737 u8 *nexthdr; 13738 int start; 13739 13740 /* Check that there are at most 2 IPv6 extension headers, no 13741 * fragment header, and each is <= 64 bytes. 13742 */ 13743 start = nw_off + sizeof(*ip6h); 13744 nexthdr = &ip6h->nexthdr; 13745 while (ipv6_ext_hdr(*nexthdr)) { 13746 struct ipv6_opt_hdr *hp; 13747 int hdrlen; 13748 13749 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13750 *nexthdr == NEXTHDR_FRAGMENT) 13751 return false; 13752 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13753 skb_headlen(skb), NULL); 13754 if (!hp) 13755 return false; 13756 if (*nexthdr == NEXTHDR_AUTH) 13757 hdrlen = ipv6_authlen(hp); 13758 else 13759 hdrlen = ipv6_optlen(hp); 13760 13761 if (hdrlen > 64) 13762 return false; 13763 13764 /* The ext header may be a hop-by-hop header inserted for 13765 * big TCP purposes. This will be removed before sending 13766 * from NIC, so do not count it. 13767 */ 13768 if (*nexthdr == NEXTHDR_HOP) { 13769 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13770 goto increment_hdr; 13771 13772 jhdr = (struct hop_jumbo_hdr *)hp; 13773 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13774 jhdr->nexthdr != IPPROTO_TCP) 13775 goto increment_hdr; 13776 13777 goto next_hdr; 13778 } 13779 increment_hdr: 13780 hdr_count++; 13781 next_hdr: 13782 nexthdr = &hp->nexthdr; 13783 start += hdrlen; 13784 } 13785 if (nextp) { 13786 /* Caller will check inner protocol */ 13787 if (skb->encapsulation) { 13788 *nextp = nexthdr; 13789 return true; 13790 } 13791 *nextp = NULL; 13792 } 13793 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13794 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13795 } 13796 13797 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13798 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13799 { 13800 struct udphdr *uh = udp_hdr(skb); 13801 __be16 udp_port = uh->dest; 13802 13803 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13804 udp_port != bp->vxlan_gpe_port) 13805 return false; 13806 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13807 struct ethhdr *eh = inner_eth_hdr(skb); 13808 13809 switch (eh->h_proto) { 13810 case htons(ETH_P_IP): 13811 return true; 13812 case htons(ETH_P_IPV6): 13813 return bnxt_exthdr_check(bp, skb, 13814 skb_inner_network_offset(skb), 13815 NULL); 13816 } 13817 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13818 return true; 13819 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13820 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13821 NULL); 13822 } 13823 return false; 13824 } 13825 13826 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13827 { 13828 switch (l4_proto) { 13829 case IPPROTO_UDP: 13830 return bnxt_udp_tunl_check(bp, skb); 13831 case IPPROTO_IPIP: 13832 return true; 13833 case IPPROTO_GRE: { 13834 switch (skb->inner_protocol) { 13835 default: 13836 return false; 13837 case htons(ETH_P_IP): 13838 return true; 13839 case htons(ETH_P_IPV6): 13840 fallthrough; 13841 } 13842 } 13843 case IPPROTO_IPV6: 13844 /* Check ext headers of inner ipv6 */ 13845 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13846 NULL); 13847 } 13848 return false; 13849 } 13850 13851 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13852 struct net_device *dev, 13853 netdev_features_t features) 13854 { 13855 struct bnxt *bp = netdev_priv(dev); 13856 u8 *l4_proto; 13857 13858 features = vlan_features_check(skb, features); 13859 switch (vlan_get_protocol(skb)) { 13860 case htons(ETH_P_IP): 13861 if (!skb->encapsulation) 13862 return features; 13863 l4_proto = &ip_hdr(skb)->protocol; 13864 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13865 return features; 13866 break; 13867 case htons(ETH_P_IPV6): 13868 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13869 &l4_proto)) 13870 break; 13871 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13872 return features; 13873 break; 13874 } 13875 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13876 } 13877 13878 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13879 u32 *reg_buf) 13880 { 13881 struct hwrm_dbg_read_direct_output *resp; 13882 struct hwrm_dbg_read_direct_input *req; 13883 __le32 *dbg_reg_buf; 13884 dma_addr_t mapping; 13885 int rc, i; 13886 13887 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13888 if (rc) 13889 return rc; 13890 13891 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13892 &mapping); 13893 if (!dbg_reg_buf) { 13894 rc = -ENOMEM; 13895 goto dbg_rd_reg_exit; 13896 } 13897 13898 req->host_dest_addr = cpu_to_le64(mapping); 13899 13900 resp = hwrm_req_hold(bp, req); 13901 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13902 req->read_len32 = cpu_to_le32(num_words); 13903 13904 rc = hwrm_req_send(bp, req); 13905 if (rc || resp->error_code) { 13906 rc = -EIO; 13907 goto dbg_rd_reg_exit; 13908 } 13909 for (i = 0; i < num_words; i++) 13910 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13911 13912 dbg_rd_reg_exit: 13913 hwrm_req_drop(bp, req); 13914 return rc; 13915 } 13916 13917 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13918 u32 ring_id, u32 *prod, u32 *cons) 13919 { 13920 struct hwrm_dbg_ring_info_get_output *resp; 13921 struct hwrm_dbg_ring_info_get_input *req; 13922 int rc; 13923 13924 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13925 if (rc) 13926 return rc; 13927 13928 req->ring_type = ring_type; 13929 req->fw_ring_id = cpu_to_le32(ring_id); 13930 resp = hwrm_req_hold(bp, req); 13931 rc = hwrm_req_send(bp, req); 13932 if (!rc) { 13933 *prod = le32_to_cpu(resp->producer_index); 13934 *cons = le32_to_cpu(resp->consumer_index); 13935 } 13936 hwrm_req_drop(bp, req); 13937 return rc; 13938 } 13939 13940 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13941 { 13942 struct bnxt_tx_ring_info *txr; 13943 int i = bnapi->index, j; 13944 13945 bnxt_for_each_napi_tx(j, bnapi, txr) 13946 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13947 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13948 txr->tx_cons); 13949 } 13950 13951 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13952 { 13953 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13954 int i = bnapi->index; 13955 13956 if (!rxr) 13957 return; 13958 13959 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", 13960 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13961 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13962 rxr->rx_sw_agg_prod); 13963 } 13964 13965 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13966 { 13967 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13968 int i = bnapi->index; 13969 13970 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13971 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13972 } 13973 13974 static void bnxt_dbg_dump_states(struct bnxt *bp) 13975 { 13976 int i; 13977 struct bnxt_napi *bnapi; 13978 13979 for (i = 0; i < bp->cp_nr_rings; i++) { 13980 bnapi = bp->bnapi[i]; 13981 if (netif_msg_drv(bp)) { 13982 bnxt_dump_tx_sw_state(bnapi); 13983 bnxt_dump_rx_sw_state(bnapi); 13984 bnxt_dump_cp_sw_state(bnapi); 13985 } 13986 } 13987 } 13988 13989 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13990 { 13991 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 13992 struct hwrm_ring_reset_input *req; 13993 struct bnxt_napi *bnapi = rxr->bnapi; 13994 struct bnxt_cp_ring_info *cpr; 13995 u16 cp_ring_id; 13996 int rc; 13997 13998 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 13999 if (rc) 14000 return rc; 14001 14002 cpr = &bnapi->cp_ring; 14003 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14004 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14005 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14006 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14007 return hwrm_req_send_silent(bp, req); 14008 } 14009 14010 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14011 { 14012 if (!silent) 14013 bnxt_dbg_dump_states(bp); 14014 if (netif_running(bp->dev)) { 14015 bnxt_close_nic(bp, !silent, false); 14016 bnxt_open_nic(bp, !silent, false); 14017 } 14018 } 14019 14020 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14021 { 14022 struct bnxt *bp = netdev_priv(dev); 14023 14024 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14025 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14026 } 14027 14028 static void bnxt_fw_health_check(struct bnxt *bp) 14029 { 14030 struct bnxt_fw_health *fw_health = bp->fw_health; 14031 struct pci_dev *pdev = bp->pdev; 14032 u32 val; 14033 14034 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14035 return; 14036 14037 /* Make sure it is enabled before checking the tmr_counter. */ 14038 smp_rmb(); 14039 if (fw_health->tmr_counter) { 14040 fw_health->tmr_counter--; 14041 return; 14042 } 14043 14044 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14045 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14046 fw_health->arrests++; 14047 goto fw_reset; 14048 } 14049 14050 fw_health->last_fw_heartbeat = val; 14051 14052 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14053 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14054 fw_health->discoveries++; 14055 goto fw_reset; 14056 } 14057 14058 fw_health->tmr_counter = fw_health->tmr_multiplier; 14059 return; 14060 14061 fw_reset: 14062 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14063 } 14064 14065 static void bnxt_timer(struct timer_list *t) 14066 { 14067 struct bnxt *bp = timer_container_of(bp, t, timer); 14068 struct net_device *dev = bp->dev; 14069 14070 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14071 return; 14072 14073 if (atomic_read(&bp->intr_sem) != 0) 14074 goto bnxt_restart_timer; 14075 14076 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14077 bnxt_fw_health_check(bp); 14078 14079 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14080 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14081 14082 if (bnxt_tc_flower_enabled(bp)) 14083 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14084 14085 #ifdef CONFIG_RFS_ACCEL 14086 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14087 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14088 #endif /*CONFIG_RFS_ACCEL*/ 14089 14090 if (bp->link_info.phy_retry) { 14091 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14092 bp->link_info.phy_retry = false; 14093 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14094 } else { 14095 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14096 } 14097 } 14098 14099 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 14100 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 14101 14102 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14103 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14104 14105 bnxt_restart_timer: 14106 mod_timer(&bp->timer, jiffies + bp->current_interval); 14107 } 14108 14109 static void bnxt_lock_sp(struct bnxt *bp) 14110 { 14111 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14112 * set. If the device is being closed, bnxt_close() may be holding 14113 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14114 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14115 * instance lock. 14116 */ 14117 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14118 netdev_lock(bp->dev); 14119 } 14120 14121 static void bnxt_unlock_sp(struct bnxt *bp) 14122 { 14123 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14124 netdev_unlock(bp->dev); 14125 } 14126 14127 /* Same as bnxt_lock_sp() with additional rtnl_lock */ 14128 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 14129 { 14130 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14131 rtnl_lock(); 14132 netdev_lock(bp->dev); 14133 } 14134 14135 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 14136 { 14137 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14138 netdev_unlock(bp->dev); 14139 rtnl_unlock(); 14140 } 14141 14142 /* Only called from bnxt_sp_task() */ 14143 static void bnxt_reset(struct bnxt *bp, bool silent) 14144 { 14145 bnxt_rtnl_lock_sp(bp); 14146 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14147 bnxt_reset_task(bp, silent); 14148 bnxt_rtnl_unlock_sp(bp); 14149 } 14150 14151 /* Only called from bnxt_sp_task() */ 14152 static void bnxt_rx_ring_reset(struct bnxt *bp) 14153 { 14154 int i; 14155 14156 bnxt_rtnl_lock_sp(bp); 14157 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14158 bnxt_rtnl_unlock_sp(bp); 14159 return; 14160 } 14161 /* Disable and flush TPA before resetting the RX ring */ 14162 if (bp->flags & BNXT_FLAG_TPA) 14163 bnxt_set_tpa(bp, false); 14164 for (i = 0; i < bp->rx_nr_rings; i++) { 14165 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14166 struct bnxt_cp_ring_info *cpr; 14167 int rc; 14168 14169 if (!rxr->bnapi->in_reset) 14170 continue; 14171 14172 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14173 if (rc) { 14174 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14175 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14176 else 14177 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14178 rc); 14179 bnxt_reset_task(bp, true); 14180 break; 14181 } 14182 bnxt_free_one_rx_ring_skbs(bp, rxr); 14183 rxr->rx_prod = 0; 14184 rxr->rx_agg_prod = 0; 14185 rxr->rx_sw_agg_prod = 0; 14186 rxr->rx_next_cons = 0; 14187 rxr->bnapi->in_reset = false; 14188 bnxt_alloc_one_rx_ring(bp, i); 14189 cpr = &rxr->bnapi->cp_ring; 14190 cpr->sw_stats->rx.rx_resets++; 14191 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14192 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14193 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14194 } 14195 if (bp->flags & BNXT_FLAG_TPA) 14196 bnxt_set_tpa(bp, true); 14197 bnxt_rtnl_unlock_sp(bp); 14198 } 14199 14200 static void bnxt_fw_fatal_close(struct bnxt *bp) 14201 { 14202 bnxt_tx_disable(bp); 14203 bnxt_disable_napi(bp); 14204 bnxt_disable_int_sync(bp); 14205 bnxt_free_irq(bp); 14206 bnxt_clear_int_mode(bp); 14207 pci_disable_device(bp->pdev); 14208 } 14209 14210 static void bnxt_fw_reset_close(struct bnxt *bp) 14211 { 14212 /* When firmware is in fatal state, quiesce device and disable 14213 * bus master to prevent any potential bad DMAs before freeing 14214 * kernel memory. 14215 */ 14216 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14217 u16 val = 0; 14218 14219 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14220 if (val == 0xffff) 14221 bp->fw_reset_min_dsecs = 0; 14222 bnxt_fw_fatal_close(bp); 14223 } 14224 __bnxt_close_nic(bp, true, false); 14225 bnxt_vf_reps_free(bp); 14226 bnxt_clear_int_mode(bp); 14227 bnxt_hwrm_func_drv_unrgtr(bp); 14228 if (pci_is_enabled(bp->pdev)) 14229 pci_disable_device(bp->pdev); 14230 bnxt_free_ctx_mem(bp, false); 14231 } 14232 14233 static bool is_bnxt_fw_ok(struct bnxt *bp) 14234 { 14235 struct bnxt_fw_health *fw_health = bp->fw_health; 14236 bool no_heartbeat = false, has_reset = false; 14237 u32 val; 14238 14239 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14240 if (val == fw_health->last_fw_heartbeat) 14241 no_heartbeat = true; 14242 14243 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14244 if (val != fw_health->last_fw_reset_cnt) 14245 has_reset = true; 14246 14247 if (!no_heartbeat && has_reset) 14248 return true; 14249 14250 return false; 14251 } 14252 14253 /* netdev instance lock is acquired before calling this function */ 14254 static void bnxt_force_fw_reset(struct bnxt *bp) 14255 { 14256 struct bnxt_fw_health *fw_health = bp->fw_health; 14257 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14258 u32 wait_dsecs; 14259 14260 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14261 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14262 return; 14263 14264 /* we have to serialize with bnxt_refclk_read()*/ 14265 if (ptp) { 14266 unsigned long flags; 14267 14268 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14269 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14270 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14271 } else { 14272 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14273 } 14274 bnxt_fw_reset_close(bp); 14275 wait_dsecs = fw_health->master_func_wait_dsecs; 14276 if (fw_health->primary) { 14277 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14278 wait_dsecs = 0; 14279 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14280 } else { 14281 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14282 wait_dsecs = fw_health->normal_func_wait_dsecs; 14283 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14284 } 14285 14286 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14287 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14288 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14289 } 14290 14291 void bnxt_fw_exception(struct bnxt *bp) 14292 { 14293 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14294 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14295 bnxt_ulp_stop(bp); 14296 bnxt_lock_sp(bp); 14297 bnxt_force_fw_reset(bp); 14298 bnxt_unlock_sp(bp); 14299 } 14300 14301 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14302 * < 0 on error. 14303 */ 14304 static int bnxt_get_registered_vfs(struct bnxt *bp) 14305 { 14306 #ifdef CONFIG_BNXT_SRIOV 14307 int rc; 14308 14309 if (!BNXT_PF(bp)) 14310 return 0; 14311 14312 rc = bnxt_hwrm_func_qcfg(bp); 14313 if (rc) { 14314 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14315 return rc; 14316 } 14317 if (bp->pf.registered_vfs) 14318 return bp->pf.registered_vfs; 14319 if (bp->sriov_cfg) 14320 return 1; 14321 #endif 14322 return 0; 14323 } 14324 14325 void bnxt_fw_reset(struct bnxt *bp) 14326 { 14327 bnxt_ulp_stop(bp); 14328 bnxt_lock_sp(bp); 14329 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14330 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14331 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14332 int n = 0, tmo; 14333 14334 /* we have to serialize with bnxt_refclk_read()*/ 14335 if (ptp) { 14336 unsigned long flags; 14337 14338 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14339 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14340 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14341 } else { 14342 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14343 } 14344 if (bp->pf.active_vfs && 14345 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14346 n = bnxt_get_registered_vfs(bp); 14347 if (n < 0) { 14348 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14349 n); 14350 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14351 netif_close(bp->dev); 14352 goto fw_reset_exit; 14353 } else if (n > 0) { 14354 u16 vf_tmo_dsecs = n * 10; 14355 14356 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14357 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14358 bp->fw_reset_state = 14359 BNXT_FW_RESET_STATE_POLL_VF; 14360 bnxt_queue_fw_reset_work(bp, HZ / 10); 14361 goto fw_reset_exit; 14362 } 14363 bnxt_fw_reset_close(bp); 14364 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14365 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14366 tmo = HZ / 10; 14367 } else { 14368 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14369 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14370 } 14371 bnxt_queue_fw_reset_work(bp, tmo); 14372 } 14373 fw_reset_exit: 14374 bnxt_unlock_sp(bp); 14375 } 14376 14377 static void bnxt_chk_missed_irq(struct bnxt *bp) 14378 { 14379 int i; 14380 14381 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14382 return; 14383 14384 for (i = 0; i < bp->cp_nr_rings; i++) { 14385 struct bnxt_napi *bnapi = bp->bnapi[i]; 14386 struct bnxt_cp_ring_info *cpr; 14387 u32 fw_ring_id; 14388 int j; 14389 14390 if (!bnapi) 14391 continue; 14392 14393 cpr = &bnapi->cp_ring; 14394 for (j = 0; j < cpr->cp_ring_count; j++) { 14395 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14396 u32 val[2]; 14397 14398 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14399 continue; 14400 14401 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14402 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14403 continue; 14404 } 14405 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14406 bnxt_dbg_hwrm_ring_info_get(bp, 14407 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14408 fw_ring_id, &val[0], &val[1]); 14409 cpr->sw_stats->cmn.missed_irqs++; 14410 } 14411 } 14412 } 14413 14414 static void bnxt_cfg_ntp_filters(struct bnxt *); 14415 14416 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14417 { 14418 struct bnxt_link_info *link_info = &bp->link_info; 14419 14420 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14421 link_info->autoneg = BNXT_AUTONEG_SPEED; 14422 if (bp->hwrm_spec_code >= 0x10201) { 14423 if (link_info->auto_pause_setting & 14424 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14425 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14426 } else { 14427 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14428 } 14429 bnxt_set_auto_speed(link_info); 14430 } else { 14431 bnxt_set_force_speed(link_info); 14432 link_info->req_duplex = link_info->duplex_setting; 14433 } 14434 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14435 link_info->req_flow_ctrl = 14436 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14437 else 14438 link_info->req_flow_ctrl = link_info->force_pause_setting; 14439 } 14440 14441 static void bnxt_fw_echo_reply(struct bnxt *bp) 14442 { 14443 struct bnxt_fw_health *fw_health = bp->fw_health; 14444 struct hwrm_func_echo_response_input *req; 14445 int rc; 14446 14447 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14448 if (rc) 14449 return; 14450 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14451 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14452 hwrm_req_send(bp, req); 14453 } 14454 14455 static void bnxt_ulp_restart(struct bnxt *bp) 14456 { 14457 bnxt_ulp_stop(bp); 14458 bnxt_ulp_start(bp, 0); 14459 } 14460 14461 static void bnxt_sp_task(struct work_struct *work) 14462 { 14463 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14464 14465 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14466 smp_mb__after_atomic(); 14467 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14468 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14469 return; 14470 } 14471 14472 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14473 bnxt_ulp_restart(bp); 14474 bnxt_reenable_sriov(bp); 14475 } 14476 14477 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14478 bnxt_cfg_rx_mode(bp); 14479 14480 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14481 bnxt_cfg_ntp_filters(bp); 14482 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14483 bnxt_hwrm_exec_fwd_req(bp); 14484 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14485 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14486 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14487 bnxt_hwrm_port_qstats(bp, 0); 14488 bnxt_hwrm_port_qstats_ext(bp, 0); 14489 bnxt_accumulate_all_stats(bp); 14490 } 14491 14492 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14493 int rc; 14494 14495 mutex_lock(&bp->link_lock); 14496 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14497 &bp->sp_event)) 14498 bnxt_hwrm_phy_qcaps(bp); 14499 14500 rc = bnxt_update_link(bp, true); 14501 if (rc) 14502 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14503 rc); 14504 14505 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14506 &bp->sp_event)) 14507 bnxt_init_ethtool_link_settings(bp); 14508 mutex_unlock(&bp->link_lock); 14509 } 14510 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14511 int rc; 14512 14513 mutex_lock(&bp->link_lock); 14514 rc = bnxt_update_phy_setting(bp); 14515 mutex_unlock(&bp->link_lock); 14516 if (rc) { 14517 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14518 } else { 14519 bp->link_info.phy_retry = false; 14520 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14521 } 14522 } 14523 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14524 mutex_lock(&bp->link_lock); 14525 bnxt_get_port_module_status(bp); 14526 mutex_unlock(&bp->link_lock); 14527 } 14528 14529 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14530 bnxt_tc_flow_stats_work(bp); 14531 14532 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14533 bnxt_chk_missed_irq(bp); 14534 14535 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14536 bnxt_fw_echo_reply(bp); 14537 14538 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14539 bnxt_hwmon_notify_event(bp); 14540 14541 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14542 * must be the last functions to be called before exiting. 14543 */ 14544 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14545 bnxt_reset(bp, false); 14546 14547 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14548 bnxt_reset(bp, true); 14549 14550 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14551 bnxt_rx_ring_reset(bp); 14552 14553 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14554 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14555 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14556 bnxt_devlink_health_fw_report(bp); 14557 else 14558 bnxt_fw_reset(bp); 14559 } 14560 14561 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14562 if (!is_bnxt_fw_ok(bp)) 14563 bnxt_devlink_health_fw_report(bp); 14564 } 14565 14566 smp_mb__before_atomic(); 14567 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14568 } 14569 14570 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14571 int *max_cp); 14572 14573 /* Under netdev instance lock */ 14574 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14575 int tx_xdp) 14576 { 14577 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14578 struct bnxt_hw_rings hwr = {0}; 14579 int rx_rings = rx; 14580 int rc; 14581 14582 if (tcs) 14583 tx_sets = tcs; 14584 14585 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14586 14587 if (max_rx < rx_rings) 14588 return -ENOMEM; 14589 14590 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14591 rx_rings <<= 1; 14592 14593 hwr.rx = rx_rings; 14594 hwr.tx = tx * tx_sets + tx_xdp; 14595 if (max_tx < hwr.tx) 14596 return -ENOMEM; 14597 14598 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14599 14600 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14601 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14602 if (max_cp < hwr.cp) 14603 return -ENOMEM; 14604 hwr.stat = hwr.cp; 14605 if (BNXT_NEW_RM(bp)) { 14606 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14607 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14608 hwr.grp = rx; 14609 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14610 } 14611 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14612 hwr.cp_p5 = hwr.tx + rx; 14613 rc = bnxt_hwrm_check_rings(bp, &hwr); 14614 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14615 if (!bnxt_ulp_registered(bp->edev)) { 14616 hwr.cp += bnxt_get_ulp_msix_num(bp); 14617 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14618 } 14619 if (hwr.cp > bp->total_irqs) { 14620 int total_msix = bnxt_change_msix(bp, hwr.cp); 14621 14622 if (total_msix < hwr.cp) { 14623 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14624 hwr.cp, total_msix); 14625 rc = -ENOSPC; 14626 } 14627 } 14628 } 14629 return rc; 14630 } 14631 14632 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14633 { 14634 if (bp->bar2) { 14635 pci_iounmap(pdev, bp->bar2); 14636 bp->bar2 = NULL; 14637 } 14638 14639 if (bp->bar1) { 14640 pci_iounmap(pdev, bp->bar1); 14641 bp->bar1 = NULL; 14642 } 14643 14644 if (bp->bar0) { 14645 pci_iounmap(pdev, bp->bar0); 14646 bp->bar0 = NULL; 14647 } 14648 } 14649 14650 static void bnxt_cleanup_pci(struct bnxt *bp) 14651 { 14652 bnxt_unmap_bars(bp, bp->pdev); 14653 pci_release_regions(bp->pdev); 14654 if (pci_is_enabled(bp->pdev)) 14655 pci_disable_device(bp->pdev); 14656 } 14657 14658 static void bnxt_init_dflt_coal(struct bnxt *bp) 14659 { 14660 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14661 struct bnxt_coal *coal; 14662 u16 flags = 0; 14663 14664 if (coal_cap->cmpl_params & 14665 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14666 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14667 14668 /* Tick values in micro seconds. 14669 * 1 coal_buf x bufs_per_record = 1 completion record. 14670 */ 14671 coal = &bp->rx_coal; 14672 coal->coal_ticks = 10; 14673 coal->coal_bufs = 30; 14674 coal->coal_ticks_irq = 1; 14675 coal->coal_bufs_irq = 2; 14676 coal->idle_thresh = 50; 14677 coal->bufs_per_record = 2; 14678 coal->budget = 64; /* NAPI budget */ 14679 coal->flags = flags; 14680 14681 coal = &bp->tx_coal; 14682 coal->coal_ticks = 28; 14683 coal->coal_bufs = 30; 14684 coal->coal_ticks_irq = 2; 14685 coal->coal_bufs_irq = 2; 14686 coal->bufs_per_record = 1; 14687 coal->flags = flags; 14688 14689 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14690 } 14691 14692 /* FW that pre-reserves 1 VNIC per function */ 14693 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14694 { 14695 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14696 14697 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14698 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14699 return true; 14700 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14701 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14702 return true; 14703 return false; 14704 } 14705 14706 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14707 { 14708 int rc; 14709 14710 bp->fw_cap = 0; 14711 rc = bnxt_hwrm_ver_get(bp); 14712 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14713 * so wait before continuing with recovery. 14714 */ 14715 if (rc) 14716 msleep(100); 14717 bnxt_try_map_fw_health_reg(bp); 14718 if (rc) { 14719 rc = bnxt_try_recover_fw(bp); 14720 if (rc) 14721 return rc; 14722 rc = bnxt_hwrm_ver_get(bp); 14723 if (rc) 14724 return rc; 14725 } 14726 14727 bnxt_nvm_cfg_ver_get(bp); 14728 14729 rc = bnxt_hwrm_func_reset(bp); 14730 if (rc) 14731 return -ENODEV; 14732 14733 bnxt_hwrm_fw_set_time(bp); 14734 return 0; 14735 } 14736 14737 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14738 { 14739 int rc; 14740 14741 /* Get the MAX capabilities for this function */ 14742 rc = bnxt_hwrm_func_qcaps(bp); 14743 if (rc) { 14744 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14745 rc); 14746 return -ENODEV; 14747 } 14748 14749 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14750 if (rc) 14751 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14752 rc); 14753 14754 if (bnxt_alloc_fw_health(bp)) { 14755 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14756 } else { 14757 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14758 if (rc) 14759 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14760 rc); 14761 } 14762 14763 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14764 if (rc) 14765 return -ENODEV; 14766 14767 rc = bnxt_alloc_crash_dump_mem(bp); 14768 if (rc) 14769 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14770 rc); 14771 if (!rc) { 14772 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14773 if (rc) { 14774 bnxt_free_crash_dump_mem(bp); 14775 netdev_warn(bp->dev, 14776 "hwrm crash dump mem failure rc: %d\n", rc); 14777 } 14778 } 14779 14780 if (bnxt_fw_pre_resv_vnics(bp)) 14781 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14782 14783 bnxt_hwrm_func_qcfg(bp); 14784 bnxt_hwrm_vnic_qcaps(bp); 14785 bnxt_hwrm_port_led_qcaps(bp); 14786 bnxt_ethtool_init(bp); 14787 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14788 __bnxt_hwrm_ptp_qcfg(bp); 14789 bnxt_dcb_init(bp); 14790 bnxt_hwmon_init(bp); 14791 return 0; 14792 } 14793 14794 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14795 { 14796 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14797 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14798 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14799 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14800 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14801 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14802 bp->rss_hash_delta = bp->rss_hash_cfg; 14803 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14804 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14805 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14806 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14807 } 14808 } 14809 14810 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14811 { 14812 struct net_device *dev = bp->dev; 14813 14814 dev->hw_features &= ~NETIF_F_NTUPLE; 14815 dev->features &= ~NETIF_F_NTUPLE; 14816 bp->flags &= ~BNXT_FLAG_RFS; 14817 if (bnxt_rfs_supported(bp)) { 14818 dev->hw_features |= NETIF_F_NTUPLE; 14819 if (bnxt_rfs_capable(bp, false)) { 14820 bp->flags |= BNXT_FLAG_RFS; 14821 dev->features |= NETIF_F_NTUPLE; 14822 } 14823 } 14824 } 14825 14826 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14827 { 14828 struct pci_dev *pdev = bp->pdev; 14829 14830 bnxt_set_dflt_rss_hash_type(bp); 14831 bnxt_set_dflt_rfs(bp); 14832 14833 bnxt_get_wol_settings(bp); 14834 if (bp->flags & BNXT_FLAG_WOL_CAP) 14835 device_set_wakeup_enable(&pdev->dev, bp->wol); 14836 else 14837 device_set_wakeup_capable(&pdev->dev, false); 14838 14839 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14840 bnxt_hwrm_coal_params_qcaps(bp); 14841 } 14842 14843 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14844 14845 int bnxt_fw_init_one(struct bnxt *bp) 14846 { 14847 int rc; 14848 14849 rc = bnxt_fw_init_one_p1(bp); 14850 if (rc) { 14851 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14852 return rc; 14853 } 14854 rc = bnxt_fw_init_one_p2(bp); 14855 if (rc) { 14856 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14857 return rc; 14858 } 14859 rc = bnxt_probe_phy(bp, false); 14860 if (rc) 14861 return rc; 14862 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14863 if (rc) 14864 return rc; 14865 14866 bnxt_fw_init_one_p3(bp); 14867 return 0; 14868 } 14869 14870 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14871 { 14872 struct bnxt_fw_health *fw_health = bp->fw_health; 14873 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14874 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14875 u32 reg_type, reg_off, delay_msecs; 14876 14877 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14878 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14879 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14880 switch (reg_type) { 14881 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14882 pci_write_config_dword(bp->pdev, reg_off, val); 14883 break; 14884 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14885 writel(reg_off & BNXT_GRC_BASE_MASK, 14886 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14887 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14888 fallthrough; 14889 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14890 writel(val, bp->bar0 + reg_off); 14891 break; 14892 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14893 writel(val, bp->bar1 + reg_off); 14894 break; 14895 } 14896 if (delay_msecs) { 14897 pci_read_config_dword(bp->pdev, 0, &val); 14898 msleep(delay_msecs); 14899 } 14900 } 14901 14902 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14903 { 14904 struct hwrm_func_qcfg_output *resp; 14905 struct hwrm_func_qcfg_input *req; 14906 bool result = true; /* firmware will enforce if unknown */ 14907 14908 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14909 return result; 14910 14911 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14912 return result; 14913 14914 req->fid = cpu_to_le16(0xffff); 14915 resp = hwrm_req_hold(bp, req); 14916 if (!hwrm_req_send(bp, req)) 14917 result = !!(le16_to_cpu(resp->flags) & 14918 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14919 hwrm_req_drop(bp, req); 14920 return result; 14921 } 14922 14923 static void bnxt_reset_all(struct bnxt *bp) 14924 { 14925 struct bnxt_fw_health *fw_health = bp->fw_health; 14926 int i, rc; 14927 14928 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14929 bnxt_fw_reset_via_optee(bp); 14930 bp->fw_reset_timestamp = jiffies; 14931 return; 14932 } 14933 14934 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14935 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14936 bnxt_fw_reset_writel(bp, i); 14937 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14938 struct hwrm_fw_reset_input *req; 14939 14940 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14941 if (!rc) { 14942 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14943 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14944 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14945 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14946 rc = hwrm_req_send(bp, req); 14947 } 14948 if (rc != -ENODEV) 14949 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14950 } 14951 bp->fw_reset_timestamp = jiffies; 14952 } 14953 14954 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14955 { 14956 return time_after(jiffies, bp->fw_reset_timestamp + 14957 (bp->fw_reset_max_dsecs * HZ / 10)); 14958 } 14959 14960 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14961 { 14962 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14963 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14964 bnxt_dl_health_fw_status_update(bp, false); 14965 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 14966 netif_close(bp->dev); 14967 } 14968 14969 static void bnxt_fw_reset_task(struct work_struct *work) 14970 { 14971 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14972 int rc = 0; 14973 14974 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14975 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14976 return; 14977 } 14978 14979 switch (bp->fw_reset_state) { 14980 case BNXT_FW_RESET_STATE_POLL_VF: { 14981 int n = bnxt_get_registered_vfs(bp); 14982 int tmo; 14983 14984 if (n < 0) { 14985 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14986 n, jiffies_to_msecs(jiffies - 14987 bp->fw_reset_timestamp)); 14988 goto fw_reset_abort; 14989 } else if (n > 0) { 14990 if (bnxt_fw_reset_timeout(bp)) { 14991 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14992 bp->fw_reset_state = 0; 14993 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14994 n); 14995 goto ulp_start; 14996 } 14997 bnxt_queue_fw_reset_work(bp, HZ / 10); 14998 return; 14999 } 15000 bp->fw_reset_timestamp = jiffies; 15001 netdev_lock(bp->dev); 15002 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 15003 bnxt_fw_reset_abort(bp, rc); 15004 netdev_unlock(bp->dev); 15005 goto ulp_start; 15006 } 15007 bnxt_fw_reset_close(bp); 15008 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15009 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15010 tmo = HZ / 10; 15011 } else { 15012 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15013 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15014 } 15015 netdev_unlock(bp->dev); 15016 bnxt_queue_fw_reset_work(bp, tmo); 15017 return; 15018 } 15019 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15020 u32 val; 15021 15022 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15023 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15024 !bnxt_fw_reset_timeout(bp)) { 15025 bnxt_queue_fw_reset_work(bp, HZ / 5); 15026 return; 15027 } 15028 15029 if (!bp->fw_health->primary) { 15030 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15031 15032 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15033 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15034 return; 15035 } 15036 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15037 } 15038 fallthrough; 15039 case BNXT_FW_RESET_STATE_RESET_FW: 15040 bnxt_reset_all(bp); 15041 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15042 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15043 return; 15044 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15045 bnxt_inv_fw_health_reg(bp); 15046 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15047 !bp->fw_reset_min_dsecs) { 15048 u16 val; 15049 15050 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15051 if (val == 0xffff) { 15052 if (bnxt_fw_reset_timeout(bp)) { 15053 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15054 rc = -ETIMEDOUT; 15055 goto fw_reset_abort; 15056 } 15057 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15058 return; 15059 } 15060 } 15061 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15062 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15063 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15064 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15065 bnxt_dl_remote_reload(bp); 15066 if (pci_enable_device(bp->pdev)) { 15067 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15068 rc = -ENODEV; 15069 goto fw_reset_abort; 15070 } 15071 pci_set_master(bp->pdev); 15072 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15073 fallthrough; 15074 case BNXT_FW_RESET_STATE_POLL_FW: 15075 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15076 rc = bnxt_hwrm_poll(bp); 15077 if (rc) { 15078 if (bnxt_fw_reset_timeout(bp)) { 15079 netdev_err(bp->dev, "Firmware reset aborted\n"); 15080 goto fw_reset_abort_status; 15081 } 15082 bnxt_queue_fw_reset_work(bp, HZ / 5); 15083 return; 15084 } 15085 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15086 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15087 fallthrough; 15088 case BNXT_FW_RESET_STATE_OPENING: 15089 while (!rtnl_trylock()) { 15090 bnxt_queue_fw_reset_work(bp, HZ / 10); 15091 return; 15092 } 15093 netdev_lock(bp->dev); 15094 rc = bnxt_open(bp->dev); 15095 if (rc) { 15096 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15097 bnxt_fw_reset_abort(bp, rc); 15098 netdev_unlock(bp->dev); 15099 rtnl_unlock(); 15100 goto ulp_start; 15101 } 15102 15103 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15104 bp->fw_health->enabled) { 15105 bp->fw_health->last_fw_reset_cnt = 15106 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15107 } 15108 bp->fw_reset_state = 0; 15109 /* Make sure fw_reset_state is 0 before clearing the flag */ 15110 smp_mb__before_atomic(); 15111 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15112 bnxt_ptp_reapply_pps(bp); 15113 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15114 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15115 bnxt_dl_health_fw_recovery_done(bp); 15116 bnxt_dl_health_fw_status_update(bp, true); 15117 } 15118 netdev_unlock(bp->dev); 15119 rtnl_unlock(); 15120 bnxt_ulp_start(bp, 0); 15121 bnxt_reenable_sriov(bp); 15122 netdev_lock(bp->dev); 15123 bnxt_vf_reps_alloc(bp); 15124 bnxt_vf_reps_open(bp); 15125 netdev_unlock(bp->dev); 15126 break; 15127 } 15128 return; 15129 15130 fw_reset_abort_status: 15131 if (bp->fw_health->status_reliable || 15132 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15133 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15134 15135 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15136 } 15137 fw_reset_abort: 15138 netdev_lock(bp->dev); 15139 bnxt_fw_reset_abort(bp, rc); 15140 netdev_unlock(bp->dev); 15141 ulp_start: 15142 bnxt_ulp_start(bp, rc); 15143 } 15144 15145 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15146 { 15147 int rc; 15148 struct bnxt *bp = netdev_priv(dev); 15149 15150 SET_NETDEV_DEV(dev, &pdev->dev); 15151 15152 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15153 rc = pci_enable_device(pdev); 15154 if (rc) { 15155 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15156 goto init_err; 15157 } 15158 15159 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15160 dev_err(&pdev->dev, 15161 "Cannot find PCI device base address, aborting\n"); 15162 rc = -ENODEV; 15163 goto init_err_disable; 15164 } 15165 15166 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15167 if (rc) { 15168 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15169 goto init_err_disable; 15170 } 15171 15172 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15173 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15174 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15175 rc = -EIO; 15176 goto init_err_release; 15177 } 15178 15179 pci_set_master(pdev); 15180 15181 bp->dev = dev; 15182 bp->pdev = pdev; 15183 15184 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15185 * determines the BAR size. 15186 */ 15187 bp->bar0 = pci_ioremap_bar(pdev, 0); 15188 if (!bp->bar0) { 15189 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15190 rc = -ENOMEM; 15191 goto init_err_release; 15192 } 15193 15194 bp->bar2 = pci_ioremap_bar(pdev, 4); 15195 if (!bp->bar2) { 15196 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15197 rc = -ENOMEM; 15198 goto init_err_release; 15199 } 15200 15201 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15202 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15203 15204 spin_lock_init(&bp->ntp_fltr_lock); 15205 #if BITS_PER_LONG == 32 15206 spin_lock_init(&bp->db_lock); 15207 #endif 15208 15209 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15210 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15211 15212 timer_setup(&bp->timer, bnxt_timer, 0); 15213 bp->current_interval = BNXT_TIMER_INTERVAL; 15214 15215 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15216 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15217 15218 clear_bit(BNXT_STATE_OPEN, &bp->state); 15219 return 0; 15220 15221 init_err_release: 15222 bnxt_unmap_bars(bp, pdev); 15223 pci_release_regions(pdev); 15224 15225 init_err_disable: 15226 pci_disable_device(pdev); 15227 15228 init_err: 15229 return rc; 15230 } 15231 15232 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15233 { 15234 struct sockaddr *addr = p; 15235 struct bnxt *bp = netdev_priv(dev); 15236 int rc = 0; 15237 15238 netdev_assert_locked(dev); 15239 15240 if (!is_valid_ether_addr(addr->sa_data)) 15241 return -EADDRNOTAVAIL; 15242 15243 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15244 return 0; 15245 15246 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15247 if (rc) 15248 return rc; 15249 15250 eth_hw_addr_set(dev, addr->sa_data); 15251 bnxt_clear_usr_fltrs(bp, true); 15252 if (netif_running(dev)) { 15253 bnxt_close_nic(bp, false, false); 15254 rc = bnxt_open_nic(bp, false, false); 15255 } 15256 15257 return rc; 15258 } 15259 15260 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15261 { 15262 struct bnxt *bp = netdev_priv(dev); 15263 15264 netdev_assert_locked(dev); 15265 15266 if (netif_running(dev)) 15267 bnxt_close_nic(bp, true, false); 15268 15269 WRITE_ONCE(dev->mtu, new_mtu); 15270 15271 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15272 * program is attached. We need to set the AGG rings settings and 15273 * rx_skb_func accordingly. 15274 */ 15275 if (READ_ONCE(bp->xdp_prog)) 15276 bnxt_set_rx_skb_mode(bp, true); 15277 15278 bnxt_set_ring_params(bp); 15279 15280 if (netif_running(dev)) 15281 return bnxt_open_nic(bp, true, false); 15282 15283 return 0; 15284 } 15285 15286 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15287 { 15288 struct bnxt *bp = netdev_priv(dev); 15289 bool sh = false; 15290 int rc, tx_cp; 15291 15292 if (tc > bp->max_tc) { 15293 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15294 tc, bp->max_tc); 15295 return -EINVAL; 15296 } 15297 15298 if (bp->num_tc == tc) 15299 return 0; 15300 15301 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15302 sh = true; 15303 15304 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15305 sh, tc, bp->tx_nr_rings_xdp); 15306 if (rc) 15307 return rc; 15308 15309 /* Needs to close the device and do hw resource re-allocations */ 15310 if (netif_running(bp->dev)) 15311 bnxt_close_nic(bp, true, false); 15312 15313 if (tc) { 15314 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15315 netdev_set_num_tc(dev, tc); 15316 bp->num_tc = tc; 15317 } else { 15318 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15319 netdev_reset_tc(dev); 15320 bp->num_tc = 0; 15321 } 15322 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15323 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15324 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15325 tx_cp + bp->rx_nr_rings; 15326 15327 if (netif_running(bp->dev)) 15328 return bnxt_open_nic(bp, true, false); 15329 15330 return 0; 15331 } 15332 15333 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15334 void *cb_priv) 15335 { 15336 struct bnxt *bp = cb_priv; 15337 15338 if (!bnxt_tc_flower_enabled(bp) || 15339 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15340 return -EOPNOTSUPP; 15341 15342 switch (type) { 15343 case TC_SETUP_CLSFLOWER: 15344 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15345 default: 15346 return -EOPNOTSUPP; 15347 } 15348 } 15349 15350 LIST_HEAD(bnxt_block_cb_list); 15351 15352 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15353 void *type_data) 15354 { 15355 struct bnxt *bp = netdev_priv(dev); 15356 15357 switch (type) { 15358 case TC_SETUP_BLOCK: 15359 return flow_block_cb_setup_simple(type_data, 15360 &bnxt_block_cb_list, 15361 bnxt_setup_tc_block_cb, 15362 bp, bp, true); 15363 case TC_SETUP_QDISC_MQPRIO: { 15364 struct tc_mqprio_qopt *mqprio = type_data; 15365 15366 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15367 15368 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15369 } 15370 default: 15371 return -EOPNOTSUPP; 15372 } 15373 } 15374 15375 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15376 const struct sk_buff *skb) 15377 { 15378 struct bnxt_vnic_info *vnic; 15379 15380 if (skb) 15381 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15382 15383 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15384 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15385 } 15386 15387 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15388 u32 idx) 15389 { 15390 struct hlist_head *head; 15391 int bit_id; 15392 15393 spin_lock_bh(&bp->ntp_fltr_lock); 15394 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15395 if (bit_id < 0) { 15396 spin_unlock_bh(&bp->ntp_fltr_lock); 15397 return -ENOMEM; 15398 } 15399 15400 fltr->base.sw_id = (u16)bit_id; 15401 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15402 fltr->base.flags |= BNXT_ACT_RING_DST; 15403 head = &bp->ntp_fltr_hash_tbl[idx]; 15404 hlist_add_head_rcu(&fltr->base.hash, head); 15405 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15406 bnxt_insert_usr_fltr(bp, &fltr->base); 15407 bp->ntp_fltr_count++; 15408 spin_unlock_bh(&bp->ntp_fltr_lock); 15409 return 0; 15410 } 15411 15412 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15413 struct bnxt_ntuple_filter *f2) 15414 { 15415 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15416 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15417 struct flow_keys *keys1 = &f1->fkeys; 15418 struct flow_keys *keys2 = &f2->fkeys; 15419 15420 if (keys1->basic.n_proto != keys2->basic.n_proto || 15421 keys1->basic.ip_proto != keys2->basic.ip_proto) 15422 return false; 15423 15424 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15425 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15426 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15427 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15428 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15429 return false; 15430 } else { 15431 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15432 &keys2->addrs.v6addrs.src) || 15433 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15434 &masks2->addrs.v6addrs.src) || 15435 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15436 &keys2->addrs.v6addrs.dst) || 15437 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15438 &masks2->addrs.v6addrs.dst)) 15439 return false; 15440 } 15441 15442 return keys1->ports.src == keys2->ports.src && 15443 masks1->ports.src == masks2->ports.src && 15444 keys1->ports.dst == keys2->ports.dst && 15445 masks1->ports.dst == masks2->ports.dst && 15446 keys1->control.flags == keys2->control.flags && 15447 f1->l2_fltr == f2->l2_fltr; 15448 } 15449 15450 struct bnxt_ntuple_filter * 15451 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15452 struct bnxt_ntuple_filter *fltr, u32 idx) 15453 { 15454 struct bnxt_ntuple_filter *f; 15455 struct hlist_head *head; 15456 15457 head = &bp->ntp_fltr_hash_tbl[idx]; 15458 hlist_for_each_entry_rcu(f, head, base.hash) { 15459 if (bnxt_fltr_match(f, fltr)) 15460 return f; 15461 } 15462 return NULL; 15463 } 15464 15465 #ifdef CONFIG_RFS_ACCEL 15466 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15467 u16 rxq_index, u32 flow_id) 15468 { 15469 struct bnxt *bp = netdev_priv(dev); 15470 struct bnxt_ntuple_filter *fltr, *new_fltr; 15471 struct flow_keys *fkeys; 15472 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15473 struct bnxt_l2_filter *l2_fltr; 15474 int rc = 0, idx; 15475 u32 flags; 15476 15477 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15478 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15479 atomic_inc(&l2_fltr->refcnt); 15480 } else { 15481 struct bnxt_l2_key key; 15482 15483 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15484 key.vlan = 0; 15485 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15486 if (!l2_fltr) 15487 return -EINVAL; 15488 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15489 bnxt_del_l2_filter(bp, l2_fltr); 15490 return -EINVAL; 15491 } 15492 } 15493 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15494 if (!new_fltr) { 15495 bnxt_del_l2_filter(bp, l2_fltr); 15496 return -ENOMEM; 15497 } 15498 15499 fkeys = &new_fltr->fkeys; 15500 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15501 rc = -EPROTONOSUPPORT; 15502 goto err_free; 15503 } 15504 15505 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15506 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15507 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15508 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15509 rc = -EPROTONOSUPPORT; 15510 goto err_free; 15511 } 15512 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15513 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15514 if (bp->hwrm_spec_code < 0x10601) { 15515 rc = -EPROTONOSUPPORT; 15516 goto err_free; 15517 } 15518 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15519 } 15520 flags = fkeys->control.flags; 15521 if (((flags & FLOW_DIS_ENCAPSULATION) && 15522 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15523 rc = -EPROTONOSUPPORT; 15524 goto err_free; 15525 } 15526 new_fltr->l2_fltr = l2_fltr; 15527 15528 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15529 rcu_read_lock(); 15530 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15531 if (fltr) { 15532 rc = fltr->base.sw_id; 15533 rcu_read_unlock(); 15534 goto err_free; 15535 } 15536 rcu_read_unlock(); 15537 15538 new_fltr->flow_id = flow_id; 15539 new_fltr->base.rxq = rxq_index; 15540 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15541 if (!rc) { 15542 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15543 return new_fltr->base.sw_id; 15544 } 15545 15546 err_free: 15547 bnxt_del_l2_filter(bp, l2_fltr); 15548 kfree(new_fltr); 15549 return rc; 15550 } 15551 #endif 15552 15553 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15554 { 15555 spin_lock_bh(&bp->ntp_fltr_lock); 15556 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15557 spin_unlock_bh(&bp->ntp_fltr_lock); 15558 return; 15559 } 15560 hlist_del_rcu(&fltr->base.hash); 15561 bnxt_del_one_usr_fltr(bp, &fltr->base); 15562 bp->ntp_fltr_count--; 15563 spin_unlock_bh(&bp->ntp_fltr_lock); 15564 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15565 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15566 kfree_rcu(fltr, base.rcu); 15567 } 15568 15569 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15570 { 15571 #ifdef CONFIG_RFS_ACCEL 15572 int i; 15573 15574 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15575 struct hlist_head *head; 15576 struct hlist_node *tmp; 15577 struct bnxt_ntuple_filter *fltr; 15578 int rc; 15579 15580 head = &bp->ntp_fltr_hash_tbl[i]; 15581 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15582 bool del = false; 15583 15584 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15585 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15586 continue; 15587 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15588 fltr->flow_id, 15589 fltr->base.sw_id)) { 15590 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15591 fltr); 15592 del = true; 15593 } 15594 } else { 15595 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15596 fltr); 15597 if (rc) 15598 del = true; 15599 else 15600 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15601 } 15602 15603 if (del) 15604 bnxt_del_ntp_filter(bp, fltr); 15605 } 15606 } 15607 #endif 15608 } 15609 15610 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15611 unsigned int entry, struct udp_tunnel_info *ti) 15612 { 15613 struct bnxt *bp = netdev_priv(netdev); 15614 unsigned int cmd; 15615 15616 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15617 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15618 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15619 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15620 else 15621 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15622 15623 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15624 } 15625 15626 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15627 unsigned int entry, struct udp_tunnel_info *ti) 15628 { 15629 struct bnxt *bp = netdev_priv(netdev); 15630 unsigned int cmd; 15631 15632 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15633 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15634 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15635 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15636 else 15637 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15638 15639 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15640 } 15641 15642 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15643 .set_port = bnxt_udp_tunnel_set_port, 15644 .unset_port = bnxt_udp_tunnel_unset_port, 15645 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15646 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15647 .tables = { 15648 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15649 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15650 }, 15651 }, bnxt_udp_tunnels_p7 = { 15652 .set_port = bnxt_udp_tunnel_set_port, 15653 .unset_port = bnxt_udp_tunnel_unset_port, 15654 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15655 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15656 .tables = { 15657 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15658 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15659 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15660 }, 15661 }; 15662 15663 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15664 struct net_device *dev, u32 filter_mask, 15665 int nlflags) 15666 { 15667 struct bnxt *bp = netdev_priv(dev); 15668 15669 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15670 nlflags, filter_mask, NULL); 15671 } 15672 15673 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15674 u16 flags, struct netlink_ext_ack *extack) 15675 { 15676 struct bnxt *bp = netdev_priv(dev); 15677 struct nlattr *attr, *br_spec; 15678 int rem, rc = 0; 15679 15680 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15681 return -EOPNOTSUPP; 15682 15683 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15684 if (!br_spec) 15685 return -EINVAL; 15686 15687 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15688 u16 mode; 15689 15690 mode = nla_get_u16(attr); 15691 if (mode == bp->br_mode) 15692 break; 15693 15694 rc = bnxt_hwrm_set_br_mode(bp, mode); 15695 if (!rc) 15696 bp->br_mode = mode; 15697 break; 15698 } 15699 return rc; 15700 } 15701 15702 int bnxt_get_port_parent_id(struct net_device *dev, 15703 struct netdev_phys_item_id *ppid) 15704 { 15705 struct bnxt *bp = netdev_priv(dev); 15706 15707 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15708 return -EOPNOTSUPP; 15709 15710 /* The PF and it's VF-reps only support the switchdev framework */ 15711 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15712 return -EOPNOTSUPP; 15713 15714 ppid->id_len = sizeof(bp->dsn); 15715 memcpy(ppid->id, bp->dsn, ppid->id_len); 15716 15717 return 0; 15718 } 15719 15720 static const struct net_device_ops bnxt_netdev_ops = { 15721 .ndo_open = bnxt_open, 15722 .ndo_start_xmit = bnxt_start_xmit, 15723 .ndo_stop = bnxt_close, 15724 .ndo_get_stats64 = bnxt_get_stats64, 15725 .ndo_set_rx_mode = bnxt_set_rx_mode, 15726 .ndo_eth_ioctl = bnxt_ioctl, 15727 .ndo_validate_addr = eth_validate_addr, 15728 .ndo_set_mac_address = bnxt_change_mac_addr, 15729 .ndo_change_mtu = bnxt_change_mtu, 15730 .ndo_fix_features = bnxt_fix_features, 15731 .ndo_set_features = bnxt_set_features, 15732 .ndo_features_check = bnxt_features_check, 15733 .ndo_tx_timeout = bnxt_tx_timeout, 15734 #ifdef CONFIG_BNXT_SRIOV 15735 .ndo_get_vf_config = bnxt_get_vf_config, 15736 .ndo_set_vf_mac = bnxt_set_vf_mac, 15737 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15738 .ndo_set_vf_rate = bnxt_set_vf_bw, 15739 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15740 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15741 .ndo_set_vf_trust = bnxt_set_vf_trust, 15742 #endif 15743 .ndo_setup_tc = bnxt_setup_tc, 15744 #ifdef CONFIG_RFS_ACCEL 15745 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15746 #endif 15747 .ndo_bpf = bnxt_xdp, 15748 .ndo_xdp_xmit = bnxt_xdp_xmit, 15749 .ndo_bridge_getlink = bnxt_bridge_getlink, 15750 .ndo_bridge_setlink = bnxt_bridge_setlink, 15751 }; 15752 15753 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15754 struct netdev_queue_stats_rx *stats) 15755 { 15756 struct bnxt *bp = netdev_priv(dev); 15757 struct bnxt_cp_ring_info *cpr; 15758 u64 *sw; 15759 15760 if (!bp->bnapi) 15761 return; 15762 15763 cpr = &bp->bnapi[i]->cp_ring; 15764 sw = cpr->stats.sw_stats; 15765 15766 stats->packets = 0; 15767 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15768 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15769 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15770 15771 stats->bytes = 0; 15772 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15773 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15774 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15775 15776 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15777 } 15778 15779 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15780 struct netdev_queue_stats_tx *stats) 15781 { 15782 struct bnxt *bp = netdev_priv(dev); 15783 struct bnxt_napi *bnapi; 15784 u64 *sw; 15785 15786 if (!bp->tx_ring) 15787 return; 15788 15789 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15790 sw = bnapi->cp_ring.stats.sw_stats; 15791 15792 stats->packets = 0; 15793 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15794 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15795 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15796 15797 stats->bytes = 0; 15798 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15799 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15800 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15801 } 15802 15803 static void bnxt_get_base_stats(struct net_device *dev, 15804 struct netdev_queue_stats_rx *rx, 15805 struct netdev_queue_stats_tx *tx) 15806 { 15807 struct bnxt *bp = netdev_priv(dev); 15808 15809 rx->packets = bp->net_stats_prev.rx_packets; 15810 rx->bytes = bp->net_stats_prev.rx_bytes; 15811 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15812 15813 tx->packets = bp->net_stats_prev.tx_packets; 15814 tx->bytes = bp->net_stats_prev.tx_bytes; 15815 } 15816 15817 static const struct netdev_stat_ops bnxt_stat_ops = { 15818 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15819 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15820 .get_base_stats = bnxt_get_base_stats, 15821 }; 15822 15823 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15824 { 15825 struct bnxt_rx_ring_info *rxr, *clone; 15826 struct bnxt *bp = netdev_priv(dev); 15827 struct bnxt_ring_struct *ring; 15828 int rc; 15829 15830 if (!bp->rx_ring) 15831 return -ENETDOWN; 15832 15833 rxr = &bp->rx_ring[idx]; 15834 clone = qmem; 15835 memcpy(clone, rxr, sizeof(*rxr)); 15836 bnxt_init_rx_ring_struct(bp, clone); 15837 bnxt_reset_rx_ring_struct(bp, clone); 15838 15839 clone->rx_prod = 0; 15840 clone->rx_agg_prod = 0; 15841 clone->rx_sw_agg_prod = 0; 15842 clone->rx_next_cons = 0; 15843 clone->need_head_pool = false; 15844 15845 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15846 if (rc) 15847 return rc; 15848 15849 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15850 if (rc < 0) 15851 goto err_page_pool_destroy; 15852 15853 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15854 MEM_TYPE_PAGE_POOL, 15855 clone->page_pool); 15856 if (rc) 15857 goto err_rxq_info_unreg; 15858 15859 ring = &clone->rx_ring_struct; 15860 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15861 if (rc) 15862 goto err_free_rx_ring; 15863 15864 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15865 ring = &clone->rx_agg_ring_struct; 15866 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15867 if (rc) 15868 goto err_free_rx_agg_ring; 15869 15870 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15871 if (rc) 15872 goto err_free_rx_agg_ring; 15873 } 15874 15875 if (bp->flags & BNXT_FLAG_TPA) { 15876 rc = bnxt_alloc_one_tpa_info(bp, clone); 15877 if (rc) 15878 goto err_free_tpa_info; 15879 } 15880 15881 bnxt_init_one_rx_ring_rxbd(bp, clone); 15882 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15883 15884 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15885 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15886 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 15887 if (bp->flags & BNXT_FLAG_TPA) 15888 bnxt_alloc_one_tpa_info_data(bp, clone); 15889 15890 return 0; 15891 15892 err_free_tpa_info: 15893 bnxt_free_one_tpa_info(bp, clone); 15894 err_free_rx_agg_ring: 15895 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15896 err_free_rx_ring: 15897 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15898 err_rxq_info_unreg: 15899 xdp_rxq_info_unreg(&clone->xdp_rxq); 15900 err_page_pool_destroy: 15901 page_pool_destroy(clone->page_pool); 15902 if (bnxt_separate_head_pool(clone)) 15903 page_pool_destroy(clone->head_pool); 15904 clone->page_pool = NULL; 15905 clone->head_pool = NULL; 15906 return rc; 15907 } 15908 15909 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15910 { 15911 struct bnxt_rx_ring_info *rxr = qmem; 15912 struct bnxt *bp = netdev_priv(dev); 15913 struct bnxt_ring_struct *ring; 15914 15915 bnxt_free_one_rx_ring_skbs(bp, rxr); 15916 bnxt_free_one_tpa_info(bp, rxr); 15917 15918 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15919 15920 page_pool_destroy(rxr->page_pool); 15921 if (bnxt_separate_head_pool(rxr)) 15922 page_pool_destroy(rxr->head_pool); 15923 rxr->page_pool = NULL; 15924 rxr->head_pool = NULL; 15925 15926 ring = &rxr->rx_ring_struct; 15927 bnxt_free_ring(bp, &ring->ring_mem); 15928 15929 ring = &rxr->rx_agg_ring_struct; 15930 bnxt_free_ring(bp, &ring->ring_mem); 15931 15932 kfree(rxr->rx_agg_bmap); 15933 rxr->rx_agg_bmap = NULL; 15934 } 15935 15936 static void bnxt_copy_rx_ring(struct bnxt *bp, 15937 struct bnxt_rx_ring_info *dst, 15938 struct bnxt_rx_ring_info *src) 15939 { 15940 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15941 struct bnxt_ring_struct *dst_ring, *src_ring; 15942 int i; 15943 15944 dst_ring = &dst->rx_ring_struct; 15945 dst_rmem = &dst_ring->ring_mem; 15946 src_ring = &src->rx_ring_struct; 15947 src_rmem = &src_ring->ring_mem; 15948 15949 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15950 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15951 WARN_ON(dst_rmem->flags != src_rmem->flags); 15952 WARN_ON(dst_rmem->depth != src_rmem->depth); 15953 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15954 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15955 15956 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15957 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15958 *dst_rmem->vmem = *src_rmem->vmem; 15959 for (i = 0; i < dst_rmem->nr_pages; i++) { 15960 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15961 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15962 } 15963 15964 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15965 return; 15966 15967 dst_ring = &dst->rx_agg_ring_struct; 15968 dst_rmem = &dst_ring->ring_mem; 15969 src_ring = &src->rx_agg_ring_struct; 15970 src_rmem = &src_ring->ring_mem; 15971 15972 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15973 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15974 WARN_ON(dst_rmem->flags != src_rmem->flags); 15975 WARN_ON(dst_rmem->depth != src_rmem->depth); 15976 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15977 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15978 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15979 15980 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15981 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15982 *dst_rmem->vmem = *src_rmem->vmem; 15983 for (i = 0; i < dst_rmem->nr_pages; i++) { 15984 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15985 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15986 } 15987 15988 dst->rx_agg_bmap = src->rx_agg_bmap; 15989 } 15990 15991 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15992 { 15993 struct bnxt *bp = netdev_priv(dev); 15994 struct bnxt_rx_ring_info *rxr, *clone; 15995 struct bnxt_cp_ring_info *cpr; 15996 struct bnxt_vnic_info *vnic; 15997 struct bnxt_napi *bnapi; 15998 int i, rc; 15999 u16 mru; 16000 16001 rxr = &bp->rx_ring[idx]; 16002 clone = qmem; 16003 16004 rxr->rx_prod = clone->rx_prod; 16005 rxr->rx_agg_prod = clone->rx_agg_prod; 16006 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 16007 rxr->rx_next_cons = clone->rx_next_cons; 16008 rxr->rx_tpa = clone->rx_tpa; 16009 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 16010 rxr->page_pool = clone->page_pool; 16011 rxr->head_pool = clone->head_pool; 16012 rxr->xdp_rxq = clone->xdp_rxq; 16013 rxr->need_head_pool = clone->need_head_pool; 16014 16015 bnxt_copy_rx_ring(bp, rxr, clone); 16016 16017 bnapi = rxr->bnapi; 16018 cpr = &bnapi->cp_ring; 16019 16020 /* All rings have been reserved and previously allocated. 16021 * Reallocating with the same parameters should never fail. 16022 */ 16023 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16024 if (rc) 16025 goto err_reset; 16026 16027 if (bp->tph_mode) { 16028 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16029 if (rc) 16030 goto err_reset; 16031 } 16032 16033 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16034 if (rc) 16035 goto err_reset; 16036 16037 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16038 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16039 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16040 16041 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16042 rc = bnxt_tx_queue_start(bp, idx); 16043 if (rc) 16044 goto err_reset; 16045 } 16046 16047 napi_enable_locked(&bnapi->napi); 16048 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16049 16050 mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 16051 for (i = 0; i < bp->nr_vnics; i++) { 16052 vnic = &bp->vnic_info[i]; 16053 16054 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16055 if (rc) 16056 return rc; 16057 } 16058 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16059 16060 err_reset: 16061 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16062 rc); 16063 napi_enable_locked(&bnapi->napi); 16064 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16065 netif_close(dev); 16066 return rc; 16067 } 16068 16069 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16070 { 16071 struct bnxt *bp = netdev_priv(dev); 16072 struct bnxt_rx_ring_info *rxr; 16073 struct bnxt_cp_ring_info *cpr; 16074 struct bnxt_vnic_info *vnic; 16075 struct bnxt_napi *bnapi; 16076 int i; 16077 16078 for (i = 0; i < bp->nr_vnics; i++) { 16079 vnic = &bp->vnic_info[i]; 16080 16081 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16082 } 16083 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16084 /* Make sure NAPI sees that the VNIC is disabled */ 16085 synchronize_net(); 16086 rxr = &bp->rx_ring[idx]; 16087 bnapi = rxr->bnapi; 16088 cpr = &bnapi->cp_ring; 16089 cancel_work_sync(&cpr->dim.work); 16090 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16091 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16092 page_pool_disable_direct_recycling(rxr->page_pool); 16093 if (bnxt_separate_head_pool(rxr)) 16094 page_pool_disable_direct_recycling(rxr->head_pool); 16095 16096 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16097 bnxt_tx_queue_stop(bp, idx); 16098 16099 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16100 * completion is handled in NAPI to guarantee no more DMA on that ring 16101 * after seeing the completion. 16102 */ 16103 napi_disable_locked(&bnapi->napi); 16104 16105 if (bp->tph_mode) { 16106 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16107 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16108 } 16109 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16110 16111 memcpy(qmem, rxr, sizeof(*rxr)); 16112 bnxt_init_rx_ring_struct(bp, qmem); 16113 16114 return 0; 16115 } 16116 16117 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16118 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16119 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16120 .ndo_queue_mem_free = bnxt_queue_mem_free, 16121 .ndo_queue_start = bnxt_queue_start, 16122 .ndo_queue_stop = bnxt_queue_stop, 16123 }; 16124 16125 static void bnxt_remove_one(struct pci_dev *pdev) 16126 { 16127 struct net_device *dev = pci_get_drvdata(pdev); 16128 struct bnxt *bp = netdev_priv(dev); 16129 16130 if (BNXT_PF(bp)) 16131 bnxt_sriov_disable(bp); 16132 16133 bnxt_rdma_aux_device_del(bp); 16134 16135 unregister_netdev(dev); 16136 bnxt_ptp_clear(bp); 16137 16138 bnxt_rdma_aux_device_uninit(bp); 16139 16140 bnxt_free_l2_filters(bp, true); 16141 bnxt_free_ntp_fltrs(bp, true); 16142 WARN_ON(bp->num_rss_ctx); 16143 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16144 /* Flush any pending tasks */ 16145 cancel_work_sync(&bp->sp_task); 16146 cancel_delayed_work_sync(&bp->fw_reset_task); 16147 bp->sp_event = 0; 16148 16149 bnxt_dl_fw_reporters_destroy(bp); 16150 bnxt_dl_unregister(bp); 16151 bnxt_shutdown_tc(bp); 16152 16153 bnxt_clear_int_mode(bp); 16154 bnxt_hwrm_func_drv_unrgtr(bp); 16155 bnxt_free_hwrm_resources(bp); 16156 bnxt_hwmon_uninit(bp); 16157 bnxt_ethtool_free(bp); 16158 bnxt_dcb_free(bp); 16159 kfree(bp->ptp_cfg); 16160 bp->ptp_cfg = NULL; 16161 kfree(bp->fw_health); 16162 bp->fw_health = NULL; 16163 bnxt_cleanup_pci(bp); 16164 bnxt_free_ctx_mem(bp, true); 16165 bnxt_free_crash_dump_mem(bp); 16166 kfree(bp->rss_indir_tbl); 16167 bp->rss_indir_tbl = NULL; 16168 bnxt_free_port_stats(bp); 16169 free_netdev(dev); 16170 } 16171 16172 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16173 { 16174 int rc = 0; 16175 struct bnxt_link_info *link_info = &bp->link_info; 16176 16177 bp->phy_flags = 0; 16178 rc = bnxt_hwrm_phy_qcaps(bp); 16179 if (rc) { 16180 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16181 rc); 16182 return rc; 16183 } 16184 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16185 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16186 else 16187 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16188 16189 bp->mac_flags = 0; 16190 bnxt_hwrm_mac_qcaps(bp); 16191 16192 if (!fw_dflt) 16193 return 0; 16194 16195 mutex_lock(&bp->link_lock); 16196 rc = bnxt_update_link(bp, false); 16197 if (rc) { 16198 mutex_unlock(&bp->link_lock); 16199 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16200 rc); 16201 return rc; 16202 } 16203 16204 /* Older firmware does not have supported_auto_speeds, so assume 16205 * that all supported speeds can be autonegotiated. 16206 */ 16207 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16208 link_info->support_auto_speeds = link_info->support_speeds; 16209 16210 bnxt_init_ethtool_link_settings(bp); 16211 mutex_unlock(&bp->link_lock); 16212 return 0; 16213 } 16214 16215 static int bnxt_get_max_irq(struct pci_dev *pdev) 16216 { 16217 u16 ctrl; 16218 16219 if (!pdev->msix_cap) 16220 return 1; 16221 16222 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16223 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16224 } 16225 16226 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16227 int *max_cp) 16228 { 16229 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16230 int max_ring_grps = 0, max_irq; 16231 16232 *max_tx = hw_resc->max_tx_rings; 16233 *max_rx = hw_resc->max_rx_rings; 16234 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16235 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16236 bnxt_get_ulp_msix_num_in_use(bp), 16237 hw_resc->max_stat_ctxs - 16238 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16239 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16240 *max_cp = min_t(int, *max_cp, max_irq); 16241 max_ring_grps = hw_resc->max_hw_ring_grps; 16242 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16243 *max_cp -= 1; 16244 *max_rx -= 2; 16245 } 16246 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16247 *max_rx >>= 1; 16248 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16249 int rc; 16250 16251 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16252 if (rc) { 16253 *max_rx = 0; 16254 *max_tx = 0; 16255 } 16256 /* On P5 chips, max_cp output param should be available NQs */ 16257 *max_cp = max_irq; 16258 } 16259 *max_rx = min_t(int, *max_rx, max_ring_grps); 16260 } 16261 16262 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16263 { 16264 int rx, tx, cp; 16265 16266 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16267 *max_rx = rx; 16268 *max_tx = tx; 16269 if (!rx || !tx || !cp) 16270 return -ENOMEM; 16271 16272 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16273 } 16274 16275 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16276 bool shared) 16277 { 16278 int rc; 16279 16280 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16281 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16282 /* Not enough rings, try disabling agg rings. */ 16283 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16284 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16285 if (rc) { 16286 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16287 bp->flags |= BNXT_FLAG_AGG_RINGS; 16288 return rc; 16289 } 16290 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16291 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16292 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16293 bnxt_set_ring_params(bp); 16294 } 16295 16296 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16297 int max_cp, max_stat, max_irq; 16298 16299 /* Reserve minimum resources for RoCE */ 16300 max_cp = bnxt_get_max_func_cp_rings(bp); 16301 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16302 max_irq = bnxt_get_max_func_irqs(bp); 16303 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16304 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16305 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16306 return 0; 16307 16308 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16309 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16310 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16311 max_cp = min_t(int, max_cp, max_irq); 16312 max_cp = min_t(int, max_cp, max_stat); 16313 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16314 if (rc) 16315 rc = 0; 16316 } 16317 return rc; 16318 } 16319 16320 /* In initial default shared ring setting, each shared ring must have a 16321 * RX/TX ring pair. 16322 */ 16323 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16324 { 16325 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16326 bp->rx_nr_rings = bp->cp_nr_rings; 16327 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16328 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16329 } 16330 16331 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16332 { 16333 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16334 int avail_msix; 16335 16336 if (!bnxt_can_reserve_rings(bp)) 16337 return 0; 16338 16339 if (sh) 16340 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16341 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16342 /* Reduce default rings on multi-port cards so that total default 16343 * rings do not exceed CPU count. 16344 */ 16345 if (bp->port_count > 1) { 16346 int max_rings = 16347 max_t(int, num_online_cpus() / bp->port_count, 1); 16348 16349 dflt_rings = min_t(int, dflt_rings, max_rings); 16350 } 16351 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16352 if (rc) 16353 return rc; 16354 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16355 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16356 if (sh) 16357 bnxt_trim_dflt_sh_rings(bp); 16358 else 16359 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16360 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16361 16362 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16363 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16364 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16365 16366 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16367 bnxt_set_dflt_ulp_stat_ctxs(bp); 16368 } 16369 16370 rc = __bnxt_reserve_rings(bp); 16371 if (rc && rc != -ENODEV) 16372 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16373 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16374 if (sh) 16375 bnxt_trim_dflt_sh_rings(bp); 16376 16377 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16378 if (bnxt_need_reserve_rings(bp)) { 16379 rc = __bnxt_reserve_rings(bp); 16380 if (rc && rc != -ENODEV) 16381 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16382 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16383 } 16384 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16385 bp->rx_nr_rings++; 16386 bp->cp_nr_rings++; 16387 } 16388 if (rc) { 16389 bp->tx_nr_rings = 0; 16390 bp->rx_nr_rings = 0; 16391 } 16392 return rc; 16393 } 16394 16395 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16396 { 16397 int rc; 16398 16399 if (bp->tx_nr_rings) 16400 return 0; 16401 16402 bnxt_ulp_irq_stop(bp); 16403 bnxt_clear_int_mode(bp); 16404 rc = bnxt_set_dflt_rings(bp, true); 16405 if (rc) { 16406 if (BNXT_VF(bp) && rc == -ENODEV) 16407 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16408 else 16409 netdev_err(bp->dev, "Not enough rings available.\n"); 16410 goto init_dflt_ring_err; 16411 } 16412 rc = bnxt_init_int_mode(bp); 16413 if (rc) 16414 goto init_dflt_ring_err; 16415 16416 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16417 16418 bnxt_set_dflt_rfs(bp); 16419 16420 init_dflt_ring_err: 16421 bnxt_ulp_irq_restart(bp, rc); 16422 return rc; 16423 } 16424 16425 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16426 { 16427 int rc; 16428 16429 netdev_ops_assert_locked(bp->dev); 16430 bnxt_hwrm_func_qcaps(bp); 16431 16432 if (netif_running(bp->dev)) 16433 __bnxt_close_nic(bp, true, false); 16434 16435 bnxt_ulp_irq_stop(bp); 16436 bnxt_clear_int_mode(bp); 16437 rc = bnxt_init_int_mode(bp); 16438 bnxt_ulp_irq_restart(bp, rc); 16439 16440 if (netif_running(bp->dev)) { 16441 if (rc) 16442 netif_close(bp->dev); 16443 else 16444 rc = bnxt_open_nic(bp, true, false); 16445 } 16446 16447 return rc; 16448 } 16449 16450 static int bnxt_init_mac_addr(struct bnxt *bp) 16451 { 16452 int rc = 0; 16453 16454 if (BNXT_PF(bp)) { 16455 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 16456 } else { 16457 #ifdef CONFIG_BNXT_SRIOV 16458 struct bnxt_vf_info *vf = &bp->vf; 16459 bool strict_approval = true; 16460 16461 if (is_valid_ether_addr(vf->mac_addr)) { 16462 /* overwrite netdev dev_addr with admin VF MAC */ 16463 eth_hw_addr_set(bp->dev, vf->mac_addr); 16464 /* Older PF driver or firmware may not approve this 16465 * correctly. 16466 */ 16467 strict_approval = false; 16468 } else { 16469 eth_hw_addr_random(bp->dev); 16470 } 16471 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16472 #endif 16473 } 16474 return rc; 16475 } 16476 16477 static void bnxt_vpd_read_info(struct bnxt *bp) 16478 { 16479 struct pci_dev *pdev = bp->pdev; 16480 unsigned int vpd_size, kw_len; 16481 int pos, size; 16482 u8 *vpd_data; 16483 16484 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16485 if (IS_ERR(vpd_data)) { 16486 pci_warn(pdev, "Unable to read VPD\n"); 16487 return; 16488 } 16489 16490 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16491 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16492 if (pos < 0) 16493 goto read_sn; 16494 16495 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16496 memcpy(bp->board_partno, &vpd_data[pos], size); 16497 16498 read_sn: 16499 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16500 PCI_VPD_RO_KEYWORD_SERIALNO, 16501 &kw_len); 16502 if (pos < 0) 16503 goto exit; 16504 16505 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16506 memcpy(bp->board_serialno, &vpd_data[pos], size); 16507 exit: 16508 kfree(vpd_data); 16509 } 16510 16511 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16512 { 16513 struct pci_dev *pdev = bp->pdev; 16514 u64 qword; 16515 16516 qword = pci_get_dsn(pdev); 16517 if (!qword) { 16518 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16519 return -EOPNOTSUPP; 16520 } 16521 16522 put_unaligned_le64(qword, dsn); 16523 16524 bp->flags |= BNXT_FLAG_DSN_VALID; 16525 return 0; 16526 } 16527 16528 static int bnxt_map_db_bar(struct bnxt *bp) 16529 { 16530 if (!bp->db_size) 16531 return -ENODEV; 16532 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16533 if (!bp->bar1) 16534 return -ENOMEM; 16535 return 0; 16536 } 16537 16538 void bnxt_print_device_info(struct bnxt *bp) 16539 { 16540 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16541 board_info[bp->board_idx].name, 16542 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16543 16544 pcie_print_link_status(bp->pdev); 16545 } 16546 16547 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16548 { 16549 struct bnxt_hw_resc *hw_resc; 16550 struct net_device *dev; 16551 struct bnxt *bp; 16552 int rc, max_irqs; 16553 16554 if (pci_is_bridge(pdev)) 16555 return -ENODEV; 16556 16557 if (!pdev->msix_cap) { 16558 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16559 return -ENODEV; 16560 } 16561 16562 /* Clear any pending DMA transactions from crash kernel 16563 * while loading driver in capture kernel. 16564 */ 16565 if (is_kdump_kernel()) { 16566 pci_clear_master(pdev); 16567 pcie_flr(pdev); 16568 } 16569 16570 max_irqs = bnxt_get_max_irq(pdev); 16571 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16572 max_irqs); 16573 if (!dev) 16574 return -ENOMEM; 16575 16576 bp = netdev_priv(dev); 16577 bp->board_idx = ent->driver_data; 16578 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16579 bnxt_set_max_func_irqs(bp, max_irqs); 16580 16581 if (bnxt_vf_pciid(bp->board_idx)) 16582 bp->flags |= BNXT_FLAG_VF; 16583 16584 /* No devlink port registration in case of a VF */ 16585 if (BNXT_PF(bp)) 16586 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16587 16588 rc = bnxt_init_board(pdev, dev); 16589 if (rc < 0) 16590 goto init_err_free; 16591 16592 dev->netdev_ops = &bnxt_netdev_ops; 16593 dev->stat_ops = &bnxt_stat_ops; 16594 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16595 dev->ethtool_ops = &bnxt_ethtool_ops; 16596 pci_set_drvdata(pdev, dev); 16597 16598 rc = bnxt_alloc_hwrm_resources(bp); 16599 if (rc) 16600 goto init_err_pci_clean; 16601 16602 mutex_init(&bp->hwrm_cmd_lock); 16603 mutex_init(&bp->link_lock); 16604 16605 rc = bnxt_fw_init_one_p1(bp); 16606 if (rc) 16607 goto init_err_pci_clean; 16608 16609 if (BNXT_PF(bp)) 16610 bnxt_vpd_read_info(bp); 16611 16612 if (BNXT_CHIP_P5_PLUS(bp)) { 16613 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16614 if (BNXT_CHIP_P7(bp)) 16615 bp->flags |= BNXT_FLAG_CHIP_P7; 16616 } 16617 16618 rc = bnxt_alloc_rss_indir_tbl(bp); 16619 if (rc) 16620 goto init_err_pci_clean; 16621 16622 rc = bnxt_fw_init_one_p2(bp); 16623 if (rc) 16624 goto init_err_pci_clean; 16625 16626 rc = bnxt_map_db_bar(bp); 16627 if (rc) { 16628 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16629 rc); 16630 goto init_err_pci_clean; 16631 } 16632 16633 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16634 NETIF_F_TSO | NETIF_F_TSO6 | 16635 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16636 NETIF_F_GSO_IPXIP4 | 16637 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16638 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16639 NETIF_F_RXCSUM | NETIF_F_GRO; 16640 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16641 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16642 16643 if (BNXT_SUPPORTS_TPA(bp)) 16644 dev->hw_features |= NETIF_F_LRO; 16645 16646 dev->hw_enc_features = 16647 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16648 NETIF_F_TSO | NETIF_F_TSO6 | 16649 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16650 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16651 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16652 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16653 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16654 if (bp->flags & BNXT_FLAG_CHIP_P7) 16655 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16656 else 16657 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16658 16659 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16660 NETIF_F_GSO_GRE_CSUM; 16661 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16662 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16663 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16664 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16665 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16666 if (BNXT_SUPPORTS_TPA(bp)) 16667 dev->hw_features |= NETIF_F_GRO_HW; 16668 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16669 if (dev->features & NETIF_F_GRO_HW) 16670 dev->features &= ~NETIF_F_LRO; 16671 dev->priv_flags |= IFF_UNICAST_FLT; 16672 16673 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16674 if (bp->tso_max_segs) 16675 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16676 16677 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16678 NETDEV_XDP_ACT_RX_SG; 16679 16680 #ifdef CONFIG_BNXT_SRIOV 16681 init_waitqueue_head(&bp->sriov_cfg_wait); 16682 #endif 16683 if (BNXT_SUPPORTS_TPA(bp)) { 16684 bp->gro_func = bnxt_gro_func_5730x; 16685 if (BNXT_CHIP_P4(bp)) 16686 bp->gro_func = bnxt_gro_func_5731x; 16687 else if (BNXT_CHIP_P5_PLUS(bp)) 16688 bp->gro_func = bnxt_gro_func_5750x; 16689 } 16690 if (!BNXT_CHIP_P4_PLUS(bp)) 16691 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16692 16693 rc = bnxt_init_mac_addr(bp); 16694 if (rc) { 16695 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16696 rc = -EADDRNOTAVAIL; 16697 goto init_err_pci_clean; 16698 } 16699 16700 if (BNXT_PF(bp)) { 16701 /* Read the adapter's DSN to use as the eswitch switch_id */ 16702 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16703 } 16704 16705 /* MTU range: 60 - FW defined max */ 16706 dev->min_mtu = ETH_ZLEN; 16707 dev->max_mtu = bp->max_mtu; 16708 16709 rc = bnxt_probe_phy(bp, true); 16710 if (rc) 16711 goto init_err_pci_clean; 16712 16713 hw_resc = &bp->hw_resc; 16714 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16715 BNXT_L2_FLTR_MAX_FLTR; 16716 /* Older firmware may not report these filters properly */ 16717 if (bp->max_fltr < BNXT_MAX_FLTR) 16718 bp->max_fltr = BNXT_MAX_FLTR; 16719 bnxt_init_l2_fltr_tbl(bp); 16720 __bnxt_set_rx_skb_mode(bp, false); 16721 bnxt_set_tpa_flags(bp); 16722 bnxt_init_ring_params(bp); 16723 bnxt_set_ring_params(bp); 16724 bnxt_rdma_aux_device_init(bp); 16725 rc = bnxt_set_dflt_rings(bp, true); 16726 if (rc) { 16727 if (BNXT_VF(bp) && rc == -ENODEV) { 16728 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16729 } else { 16730 netdev_err(bp->dev, "Not enough rings available.\n"); 16731 rc = -ENOMEM; 16732 } 16733 goto init_err_pci_clean; 16734 } 16735 16736 bnxt_fw_init_one_p3(bp); 16737 16738 bnxt_init_dflt_coal(bp); 16739 16740 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16741 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16742 16743 rc = bnxt_init_int_mode(bp); 16744 if (rc) 16745 goto init_err_pci_clean; 16746 16747 /* No TC has been set yet and rings may have been trimmed due to 16748 * limited MSIX, so we re-initialize the TX rings per TC. 16749 */ 16750 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16751 16752 if (BNXT_PF(bp)) { 16753 if (!bnxt_pf_wq) { 16754 bnxt_pf_wq = 16755 create_singlethread_workqueue("bnxt_pf_wq"); 16756 if (!bnxt_pf_wq) { 16757 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16758 rc = -ENOMEM; 16759 goto init_err_pci_clean; 16760 } 16761 } 16762 rc = bnxt_init_tc(bp); 16763 if (rc) 16764 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16765 rc); 16766 } 16767 16768 bnxt_inv_fw_health_reg(bp); 16769 rc = bnxt_dl_register(bp); 16770 if (rc) 16771 goto init_err_dl; 16772 16773 INIT_LIST_HEAD(&bp->usr_fltr_list); 16774 16775 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16776 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16777 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16778 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16779 dev->request_ops_lock = true; 16780 16781 rc = register_netdev(dev); 16782 if (rc) 16783 goto init_err_cleanup; 16784 16785 bnxt_dl_fw_reporters_create(bp); 16786 16787 bnxt_rdma_aux_device_add(bp); 16788 16789 bnxt_print_device_info(bp); 16790 16791 pci_save_state(pdev); 16792 16793 return 0; 16794 init_err_cleanup: 16795 bnxt_rdma_aux_device_uninit(bp); 16796 bnxt_dl_unregister(bp); 16797 init_err_dl: 16798 bnxt_shutdown_tc(bp); 16799 bnxt_clear_int_mode(bp); 16800 16801 init_err_pci_clean: 16802 bnxt_hwrm_func_drv_unrgtr(bp); 16803 bnxt_free_hwrm_resources(bp); 16804 bnxt_hwmon_uninit(bp); 16805 bnxt_ethtool_free(bp); 16806 bnxt_ptp_clear(bp); 16807 kfree(bp->ptp_cfg); 16808 bp->ptp_cfg = NULL; 16809 kfree(bp->fw_health); 16810 bp->fw_health = NULL; 16811 bnxt_cleanup_pci(bp); 16812 bnxt_free_ctx_mem(bp, true); 16813 bnxt_free_crash_dump_mem(bp); 16814 kfree(bp->rss_indir_tbl); 16815 bp->rss_indir_tbl = NULL; 16816 16817 init_err_free: 16818 free_netdev(dev); 16819 return rc; 16820 } 16821 16822 static void bnxt_shutdown(struct pci_dev *pdev) 16823 { 16824 struct net_device *dev = pci_get_drvdata(pdev); 16825 struct bnxt *bp; 16826 16827 if (!dev) 16828 return; 16829 16830 rtnl_lock(); 16831 netdev_lock(dev); 16832 bp = netdev_priv(dev); 16833 if (!bp) 16834 goto shutdown_exit; 16835 16836 if (netif_running(dev)) 16837 netif_close(dev); 16838 16839 bnxt_ptp_clear(bp); 16840 bnxt_clear_int_mode(bp); 16841 pci_disable_device(pdev); 16842 16843 if (system_state == SYSTEM_POWER_OFF) { 16844 pci_wake_from_d3(pdev, bp->wol); 16845 pci_set_power_state(pdev, PCI_D3hot); 16846 } 16847 16848 shutdown_exit: 16849 netdev_unlock(dev); 16850 rtnl_unlock(); 16851 } 16852 16853 #ifdef CONFIG_PM_SLEEP 16854 static int bnxt_suspend(struct device *device) 16855 { 16856 struct net_device *dev = dev_get_drvdata(device); 16857 struct bnxt *bp = netdev_priv(dev); 16858 int rc = 0; 16859 16860 bnxt_ulp_stop(bp); 16861 16862 netdev_lock(dev); 16863 if (netif_running(dev)) { 16864 netif_device_detach(dev); 16865 rc = bnxt_close(dev); 16866 } 16867 bnxt_hwrm_func_drv_unrgtr(bp); 16868 bnxt_ptp_clear(bp); 16869 pci_disable_device(bp->pdev); 16870 bnxt_free_ctx_mem(bp, false); 16871 netdev_unlock(dev); 16872 return rc; 16873 } 16874 16875 static int bnxt_resume(struct device *device) 16876 { 16877 struct net_device *dev = dev_get_drvdata(device); 16878 struct bnxt *bp = netdev_priv(dev); 16879 int rc = 0; 16880 16881 rtnl_lock(); 16882 netdev_lock(dev); 16883 rc = pci_enable_device(bp->pdev); 16884 if (rc) { 16885 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16886 rc); 16887 goto resume_exit; 16888 } 16889 pci_set_master(bp->pdev); 16890 if (bnxt_hwrm_ver_get(bp)) { 16891 rc = -ENODEV; 16892 goto resume_exit; 16893 } 16894 rc = bnxt_hwrm_func_reset(bp); 16895 if (rc) { 16896 rc = -EBUSY; 16897 goto resume_exit; 16898 } 16899 16900 rc = bnxt_hwrm_func_qcaps(bp); 16901 if (rc) 16902 goto resume_exit; 16903 16904 bnxt_clear_reservations(bp, true); 16905 16906 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16907 rc = -ENODEV; 16908 goto resume_exit; 16909 } 16910 if (bp->fw_crash_mem) 16911 bnxt_hwrm_crash_dump_mem_cfg(bp); 16912 16913 if (bnxt_ptp_init(bp)) { 16914 kfree(bp->ptp_cfg); 16915 bp->ptp_cfg = NULL; 16916 } 16917 bnxt_get_wol_settings(bp); 16918 if (netif_running(dev)) { 16919 rc = bnxt_open(dev); 16920 if (!rc) 16921 netif_device_attach(dev); 16922 } 16923 16924 resume_exit: 16925 netdev_unlock(bp->dev); 16926 rtnl_unlock(); 16927 bnxt_ulp_start(bp, rc); 16928 if (!rc) 16929 bnxt_reenable_sriov(bp); 16930 return rc; 16931 } 16932 16933 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16934 #define BNXT_PM_OPS (&bnxt_pm_ops) 16935 16936 #else 16937 16938 #define BNXT_PM_OPS NULL 16939 16940 #endif /* CONFIG_PM_SLEEP */ 16941 16942 /** 16943 * bnxt_io_error_detected - called when PCI error is detected 16944 * @pdev: Pointer to PCI device 16945 * @state: The current pci connection state 16946 * 16947 * This function is called after a PCI bus error affecting 16948 * this device has been detected. 16949 */ 16950 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16951 pci_channel_state_t state) 16952 { 16953 struct net_device *netdev = pci_get_drvdata(pdev); 16954 struct bnxt *bp = netdev_priv(netdev); 16955 bool abort = false; 16956 16957 netdev_info(netdev, "PCI I/O error detected\n"); 16958 16959 bnxt_ulp_stop(bp); 16960 16961 netdev_lock(netdev); 16962 netif_device_detach(netdev); 16963 16964 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16965 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16966 abort = true; 16967 } 16968 16969 if (abort || state == pci_channel_io_perm_failure) { 16970 netdev_unlock(netdev); 16971 return PCI_ERS_RESULT_DISCONNECT; 16972 } 16973 16974 /* Link is not reliable anymore if state is pci_channel_io_frozen 16975 * so we disable bus master to prevent any potential bad DMAs before 16976 * freeing kernel memory. 16977 */ 16978 if (state == pci_channel_io_frozen) { 16979 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16980 bnxt_fw_fatal_close(bp); 16981 } 16982 16983 if (netif_running(netdev)) 16984 __bnxt_close_nic(bp, true, true); 16985 16986 if (pci_is_enabled(pdev)) 16987 pci_disable_device(pdev); 16988 bnxt_free_ctx_mem(bp, false); 16989 netdev_unlock(netdev); 16990 16991 /* Request a slot slot reset. */ 16992 return PCI_ERS_RESULT_NEED_RESET; 16993 } 16994 16995 /** 16996 * bnxt_io_slot_reset - called after the pci bus has been reset. 16997 * @pdev: Pointer to PCI device 16998 * 16999 * Restart the card from scratch, as if from a cold-boot. 17000 * At this point, the card has experienced a hard reset, 17001 * followed by fixups by BIOS, and has its config space 17002 * set up identically to what it was at cold boot. 17003 */ 17004 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 17005 { 17006 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 17007 struct net_device *netdev = pci_get_drvdata(pdev); 17008 struct bnxt *bp = netdev_priv(netdev); 17009 int retry = 0; 17010 int err = 0; 17011 int off; 17012 17013 netdev_info(bp->dev, "PCI Slot Reset\n"); 17014 17015 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 17016 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 17017 msleep(900); 17018 17019 netdev_lock(netdev); 17020 17021 if (pci_enable_device(pdev)) { 17022 dev_err(&pdev->dev, 17023 "Cannot re-enable PCI device after reset.\n"); 17024 } else { 17025 pci_set_master(pdev); 17026 /* Upon fatal error, our device internal logic that latches to 17027 * BAR value is getting reset and will restore only upon 17028 * rewriting the BARs. 17029 * 17030 * As pci_restore_state() does not re-write the BARs if the 17031 * value is same as saved value earlier, driver needs to 17032 * write the BARs to 0 to force restore, in case of fatal error. 17033 */ 17034 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17035 &bp->state)) { 17036 for (off = PCI_BASE_ADDRESS_0; 17037 off <= PCI_BASE_ADDRESS_5; off += 4) 17038 pci_write_config_dword(bp->pdev, off, 0); 17039 } 17040 pci_restore_state(pdev); 17041 pci_save_state(pdev); 17042 17043 bnxt_inv_fw_health_reg(bp); 17044 bnxt_try_map_fw_health_reg(bp); 17045 17046 /* In some PCIe AER scenarios, firmware may take up to 17047 * 10 seconds to become ready in the worst case. 17048 */ 17049 do { 17050 err = bnxt_try_recover_fw(bp); 17051 if (!err) 17052 break; 17053 retry++; 17054 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17055 17056 if (err) { 17057 dev_err(&pdev->dev, "Firmware not ready\n"); 17058 goto reset_exit; 17059 } 17060 17061 err = bnxt_hwrm_func_reset(bp); 17062 if (!err) 17063 result = PCI_ERS_RESULT_RECOVERED; 17064 17065 /* IRQ will be initialized later in bnxt_io_resume */ 17066 bnxt_ulp_irq_stop(bp); 17067 bnxt_clear_int_mode(bp); 17068 } 17069 17070 reset_exit: 17071 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17072 bnxt_clear_reservations(bp, true); 17073 netdev_unlock(netdev); 17074 17075 return result; 17076 } 17077 17078 /** 17079 * bnxt_io_resume - called when traffic can start flowing again. 17080 * @pdev: Pointer to PCI device 17081 * 17082 * This callback is called when the error recovery driver tells 17083 * us that its OK to resume normal operation. 17084 */ 17085 static void bnxt_io_resume(struct pci_dev *pdev) 17086 { 17087 struct net_device *netdev = pci_get_drvdata(pdev); 17088 struct bnxt *bp = netdev_priv(netdev); 17089 int err; 17090 17091 netdev_info(bp->dev, "PCI Slot Resume\n"); 17092 rtnl_lock(); 17093 netdev_lock(netdev); 17094 17095 err = bnxt_hwrm_func_qcaps(bp); 17096 if (!err) { 17097 if (netif_running(netdev)) { 17098 err = bnxt_open(netdev); 17099 } else { 17100 err = bnxt_reserve_rings(bp, true); 17101 if (!err) 17102 err = bnxt_init_int_mode(bp); 17103 } 17104 } 17105 17106 if (!err) 17107 netif_device_attach(netdev); 17108 17109 netdev_unlock(netdev); 17110 rtnl_unlock(); 17111 bnxt_ulp_start(bp, err); 17112 if (!err) 17113 bnxt_reenable_sriov(bp); 17114 } 17115 17116 static const struct pci_error_handlers bnxt_err_handler = { 17117 .error_detected = bnxt_io_error_detected, 17118 .slot_reset = bnxt_io_slot_reset, 17119 .resume = bnxt_io_resume 17120 }; 17121 17122 static struct pci_driver bnxt_pci_driver = { 17123 .name = DRV_MODULE_NAME, 17124 .id_table = bnxt_pci_tbl, 17125 .probe = bnxt_init_one, 17126 .remove = bnxt_remove_one, 17127 .shutdown = bnxt_shutdown, 17128 .driver.pm = BNXT_PM_OPS, 17129 .err_handler = &bnxt_err_handler, 17130 #if defined(CONFIG_BNXT_SRIOV) 17131 .sriov_configure = bnxt_sriov_configure, 17132 #endif 17133 }; 17134 17135 static int __init bnxt_init(void) 17136 { 17137 int err; 17138 17139 bnxt_debug_init(); 17140 err = pci_register_driver(&bnxt_pci_driver); 17141 if (err) { 17142 bnxt_debug_exit(); 17143 return err; 17144 } 17145 17146 return 0; 17147 } 17148 17149 static void __exit bnxt_exit(void) 17150 { 17151 pci_unregister_driver(&bnxt_pci_driver); 17152 if (bnxt_pf_wq) 17153 destroy_workqueue(bnxt_pf_wq); 17154 bnxt_debug_exit(); 17155 } 17156 17157 module_init(bnxt_init); 17158 module_exit(bnxt_exit); 17159