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 struct cpu_rmap *rmap = NULL; 11611 int i, j, rc = 0; 11612 unsigned long flags = 0; 11613 11614 rc = bnxt_setup_int_mode(bp); 11615 if (rc) { 11616 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11617 rc); 11618 return rc; 11619 } 11620 #ifdef CONFIG_RFS_ACCEL 11621 rmap = bp->dev->rx_cpu_rmap; 11622 #endif 11623 11624 /* Enable TPH support as part of IRQ request */ 11625 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11626 if (!rc) 11627 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11628 11629 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11630 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11631 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11632 11633 if (IS_ENABLED(CONFIG_RFS_ACCEL) && 11634 rmap && bp->bnapi[i]->rx_ring) { 11635 rc = irq_cpu_rmap_add(rmap, irq->vector); 11636 if (rc) 11637 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11638 j); 11639 j++; 11640 } 11641 11642 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11643 bp->bnapi[i]); 11644 if (rc) 11645 break; 11646 11647 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 11648 irq->requested = 1; 11649 11650 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11651 int numa_node = dev_to_node(&bp->pdev->dev); 11652 u16 tag; 11653 11654 irq->have_cpumask = 1; 11655 irq->msix_nr = map_idx; 11656 irq->ring_nr = i; 11657 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11658 irq->cpu_mask); 11659 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11660 if (rc) { 11661 netdev_warn(bp->dev, 11662 "Update affinity hint failed, IRQ = %d\n", 11663 irq->vector); 11664 break; 11665 } 11666 11667 bnxt_register_irq_notifier(bp, irq); 11668 11669 /* Init ST table entry */ 11670 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11671 cpumask_first(irq->cpu_mask), 11672 &tag)) 11673 continue; 11674 11675 pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag); 11676 } 11677 } 11678 return rc; 11679 } 11680 11681 static void bnxt_del_napi(struct bnxt *bp) 11682 { 11683 int i; 11684 11685 if (!bp->bnapi) 11686 return; 11687 11688 for (i = 0; i < bp->rx_nr_rings; i++) 11689 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11690 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11691 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11692 11693 for (i = 0; i < bp->cp_nr_rings; i++) { 11694 struct bnxt_napi *bnapi = bp->bnapi[i]; 11695 11696 __netif_napi_del_locked(&bnapi->napi); 11697 } 11698 /* We called __netif_napi_del_locked(), we need 11699 * to respect an RCU grace period before freeing napi structures. 11700 */ 11701 synchronize_net(); 11702 } 11703 11704 static void bnxt_init_napi(struct bnxt *bp) 11705 { 11706 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11707 unsigned int cp_nr_rings = bp->cp_nr_rings; 11708 struct bnxt_napi *bnapi; 11709 int i; 11710 11711 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11712 poll_fn = bnxt_poll_p5; 11713 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11714 cp_nr_rings--; 11715 11716 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11717 11718 for (i = 0; i < cp_nr_rings; i++) { 11719 bnapi = bp->bnapi[i]; 11720 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 11721 bnapi->index); 11722 } 11723 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11724 bnapi = bp->bnapi[cp_nr_rings]; 11725 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11726 } 11727 } 11728 11729 static void bnxt_disable_napi(struct bnxt *bp) 11730 { 11731 int i; 11732 11733 if (!bp->bnapi || 11734 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11735 return; 11736 11737 for (i = 0; i < bp->cp_nr_rings; i++) { 11738 struct bnxt_napi *bnapi = bp->bnapi[i]; 11739 struct bnxt_cp_ring_info *cpr; 11740 11741 cpr = &bnapi->cp_ring; 11742 if (bnapi->tx_fault) 11743 cpr->sw_stats->tx.tx_resets++; 11744 if (bnapi->in_reset) 11745 cpr->sw_stats->rx.rx_resets++; 11746 napi_disable_locked(&bnapi->napi); 11747 } 11748 } 11749 11750 static void bnxt_enable_napi(struct bnxt *bp) 11751 { 11752 int i; 11753 11754 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11755 for (i = 0; i < bp->cp_nr_rings; i++) { 11756 struct bnxt_napi *bnapi = bp->bnapi[i]; 11757 struct bnxt_cp_ring_info *cpr; 11758 11759 bnapi->tx_fault = 0; 11760 11761 cpr = &bnapi->cp_ring; 11762 bnapi->in_reset = false; 11763 11764 if (bnapi->rx_ring) { 11765 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11766 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11767 } 11768 napi_enable_locked(&bnapi->napi); 11769 } 11770 } 11771 11772 void bnxt_tx_disable(struct bnxt *bp) 11773 { 11774 int i; 11775 struct bnxt_tx_ring_info *txr; 11776 11777 if (bp->tx_ring) { 11778 for (i = 0; i < bp->tx_nr_rings; i++) { 11779 txr = &bp->tx_ring[i]; 11780 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11781 } 11782 } 11783 /* Make sure napi polls see @dev_state change */ 11784 synchronize_net(); 11785 /* Drop carrier first to prevent TX timeout */ 11786 netif_carrier_off(bp->dev); 11787 /* Stop all TX queues */ 11788 netif_tx_disable(bp->dev); 11789 } 11790 11791 void bnxt_tx_enable(struct bnxt *bp) 11792 { 11793 int i; 11794 struct bnxt_tx_ring_info *txr; 11795 11796 for (i = 0; i < bp->tx_nr_rings; i++) { 11797 txr = &bp->tx_ring[i]; 11798 WRITE_ONCE(txr->dev_state, 0); 11799 } 11800 /* Make sure napi polls see @dev_state change */ 11801 synchronize_net(); 11802 netif_tx_wake_all_queues(bp->dev); 11803 if (BNXT_LINK_IS_UP(bp)) 11804 netif_carrier_on(bp->dev); 11805 } 11806 11807 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11808 { 11809 u8 active_fec = link_info->active_fec_sig_mode & 11810 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11811 11812 switch (active_fec) { 11813 default: 11814 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11815 return "None"; 11816 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11817 return "Clause 74 BaseR"; 11818 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11819 return "Clause 91 RS(528,514)"; 11820 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11821 return "Clause 91 RS544_1XN"; 11822 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11823 return "Clause 91 RS(544,514)"; 11824 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11825 return "Clause 91 RS272_1XN"; 11826 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11827 return "Clause 91 RS(272,257)"; 11828 } 11829 } 11830 11831 void bnxt_report_link(struct bnxt *bp) 11832 { 11833 if (BNXT_LINK_IS_UP(bp)) { 11834 const char *signal = ""; 11835 const char *flow_ctrl; 11836 const char *duplex; 11837 u32 speed; 11838 u16 fec; 11839 11840 netif_carrier_on(bp->dev); 11841 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11842 if (speed == SPEED_UNKNOWN) { 11843 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11844 return; 11845 } 11846 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11847 duplex = "full"; 11848 else 11849 duplex = "half"; 11850 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11851 flow_ctrl = "ON - receive & transmit"; 11852 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11853 flow_ctrl = "ON - transmit"; 11854 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11855 flow_ctrl = "ON - receive"; 11856 else 11857 flow_ctrl = "none"; 11858 if (bp->link_info.phy_qcfg_resp.option_flags & 11859 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11860 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11861 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11862 switch (sig_mode) { 11863 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11864 signal = "(NRZ) "; 11865 break; 11866 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11867 signal = "(PAM4 56Gbps) "; 11868 break; 11869 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11870 signal = "(PAM4 112Gbps) "; 11871 break; 11872 default: 11873 break; 11874 } 11875 } 11876 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11877 speed, signal, duplex, flow_ctrl); 11878 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11879 netdev_info(bp->dev, "EEE is %s\n", 11880 bp->eee.eee_active ? "active" : 11881 "not active"); 11882 fec = bp->link_info.fec_cfg; 11883 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11884 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11885 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11886 bnxt_report_fec(&bp->link_info)); 11887 } else { 11888 netif_carrier_off(bp->dev); 11889 netdev_err(bp->dev, "NIC Link is Down\n"); 11890 } 11891 } 11892 11893 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11894 { 11895 if (!resp->supported_speeds_auto_mode && 11896 !resp->supported_speeds_force_mode && 11897 !resp->supported_pam4_speeds_auto_mode && 11898 !resp->supported_pam4_speeds_force_mode && 11899 !resp->supported_speeds2_auto_mode && 11900 !resp->supported_speeds2_force_mode) 11901 return true; 11902 return false; 11903 } 11904 11905 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11906 { 11907 struct bnxt_link_info *link_info = &bp->link_info; 11908 struct hwrm_port_phy_qcaps_output *resp; 11909 struct hwrm_port_phy_qcaps_input *req; 11910 int rc = 0; 11911 11912 if (bp->hwrm_spec_code < 0x10201) 11913 return 0; 11914 11915 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11916 if (rc) 11917 return rc; 11918 11919 resp = hwrm_req_hold(bp, req); 11920 rc = hwrm_req_send(bp, req); 11921 if (rc) 11922 goto hwrm_phy_qcaps_exit; 11923 11924 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11925 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11926 struct ethtool_keee *eee = &bp->eee; 11927 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11928 11929 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11930 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11931 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11932 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11933 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11934 } 11935 11936 if (bp->hwrm_spec_code >= 0x10a01) { 11937 if (bnxt_phy_qcaps_no_speed(resp)) { 11938 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11939 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11940 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11941 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11942 netdev_info(bp->dev, "Ethernet link enabled\n"); 11943 /* Phy re-enabled, reprobe the speeds */ 11944 link_info->support_auto_speeds = 0; 11945 link_info->support_pam4_auto_speeds = 0; 11946 link_info->support_auto_speeds2 = 0; 11947 } 11948 } 11949 if (resp->supported_speeds_auto_mode) 11950 link_info->support_auto_speeds = 11951 le16_to_cpu(resp->supported_speeds_auto_mode); 11952 if (resp->supported_pam4_speeds_auto_mode) 11953 link_info->support_pam4_auto_speeds = 11954 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11955 if (resp->supported_speeds2_auto_mode) 11956 link_info->support_auto_speeds2 = 11957 le16_to_cpu(resp->supported_speeds2_auto_mode); 11958 11959 bp->port_count = resp->port_cnt; 11960 11961 hwrm_phy_qcaps_exit: 11962 hwrm_req_drop(bp, req); 11963 return rc; 11964 } 11965 11966 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 11967 { 11968 struct hwrm_port_mac_qcaps_output *resp; 11969 struct hwrm_port_mac_qcaps_input *req; 11970 int rc; 11971 11972 if (bp->hwrm_spec_code < 0x10a03) 11973 return; 11974 11975 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 11976 if (rc) 11977 return; 11978 11979 resp = hwrm_req_hold(bp, req); 11980 rc = hwrm_req_send_silent(bp, req); 11981 if (!rc) 11982 bp->mac_flags = resp->flags; 11983 hwrm_req_drop(bp, req); 11984 } 11985 11986 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11987 { 11988 u16 diff = advertising ^ supported; 11989 11990 return ((supported | diff) != supported); 11991 } 11992 11993 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 11994 { 11995 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 11996 11997 /* Check if any advertised speeds are no longer supported. The caller 11998 * holds the link_lock mutex, so we can modify link_info settings. 11999 */ 12000 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12001 if (bnxt_support_dropped(link_info->advertising, 12002 link_info->support_auto_speeds2)) { 12003 link_info->advertising = link_info->support_auto_speeds2; 12004 return true; 12005 } 12006 return false; 12007 } 12008 if (bnxt_support_dropped(link_info->advertising, 12009 link_info->support_auto_speeds)) { 12010 link_info->advertising = link_info->support_auto_speeds; 12011 return true; 12012 } 12013 if (bnxt_support_dropped(link_info->advertising_pam4, 12014 link_info->support_pam4_auto_speeds)) { 12015 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12016 return true; 12017 } 12018 return false; 12019 } 12020 12021 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12022 { 12023 struct bnxt_link_info *link_info = &bp->link_info; 12024 struct hwrm_port_phy_qcfg_output *resp; 12025 struct hwrm_port_phy_qcfg_input *req; 12026 u8 link_state = link_info->link_state; 12027 bool support_changed; 12028 int rc; 12029 12030 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12031 if (rc) 12032 return rc; 12033 12034 resp = hwrm_req_hold(bp, req); 12035 rc = hwrm_req_send(bp, req); 12036 if (rc) { 12037 hwrm_req_drop(bp, req); 12038 if (BNXT_VF(bp) && rc == -ENODEV) { 12039 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12040 rc = 0; 12041 } 12042 return rc; 12043 } 12044 12045 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12046 link_info->phy_link_status = resp->link; 12047 link_info->duplex = resp->duplex_cfg; 12048 if (bp->hwrm_spec_code >= 0x10800) 12049 link_info->duplex = resp->duplex_state; 12050 link_info->pause = resp->pause; 12051 link_info->auto_mode = resp->auto_mode; 12052 link_info->auto_pause_setting = resp->auto_pause; 12053 link_info->lp_pause = resp->link_partner_adv_pause; 12054 link_info->force_pause_setting = resp->force_pause; 12055 link_info->duplex_setting = resp->duplex_cfg; 12056 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12057 link_info->link_speed = le16_to_cpu(resp->link_speed); 12058 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12059 link_info->active_lanes = resp->active_lanes; 12060 } else { 12061 link_info->link_speed = 0; 12062 link_info->active_lanes = 0; 12063 } 12064 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12065 link_info->force_pam4_link_speed = 12066 le16_to_cpu(resp->force_pam4_link_speed); 12067 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12068 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12069 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12070 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12071 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12072 link_info->auto_pam4_link_speeds = 12073 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12074 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12075 link_info->lp_auto_link_speeds = 12076 le16_to_cpu(resp->link_partner_adv_speeds); 12077 link_info->lp_auto_pam4_link_speeds = 12078 resp->link_partner_pam4_adv_speeds; 12079 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12080 link_info->phy_ver[0] = resp->phy_maj; 12081 link_info->phy_ver[1] = resp->phy_min; 12082 link_info->phy_ver[2] = resp->phy_bld; 12083 link_info->media_type = resp->media_type; 12084 link_info->phy_type = resp->phy_type; 12085 link_info->transceiver = resp->xcvr_pkg_type; 12086 link_info->phy_addr = resp->eee_config_phy_addr & 12087 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12088 link_info->module_status = resp->module_status; 12089 12090 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12091 struct ethtool_keee *eee = &bp->eee; 12092 u16 fw_speeds; 12093 12094 eee->eee_active = 0; 12095 if (resp->eee_config_phy_addr & 12096 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12097 eee->eee_active = 1; 12098 fw_speeds = le16_to_cpu( 12099 resp->link_partner_adv_eee_link_speed_mask); 12100 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12101 } 12102 12103 /* Pull initial EEE config */ 12104 if (!chng_link_state) { 12105 if (resp->eee_config_phy_addr & 12106 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12107 eee->eee_enabled = 1; 12108 12109 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12110 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12111 12112 if (resp->eee_config_phy_addr & 12113 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12114 __le32 tmr; 12115 12116 eee->tx_lpi_enabled = 1; 12117 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12118 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12119 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12120 } 12121 } 12122 } 12123 12124 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12125 if (bp->hwrm_spec_code >= 0x10504) { 12126 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12127 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12128 } 12129 /* TODO: need to add more logic to report VF link */ 12130 if (chng_link_state) { 12131 if (link_info->phy_link_status == BNXT_LINK_LINK) 12132 link_info->link_state = BNXT_LINK_STATE_UP; 12133 else 12134 link_info->link_state = BNXT_LINK_STATE_DOWN; 12135 if (link_state != link_info->link_state) 12136 bnxt_report_link(bp); 12137 } else { 12138 /* always link down if not require to update link state */ 12139 link_info->link_state = BNXT_LINK_STATE_DOWN; 12140 } 12141 hwrm_req_drop(bp, req); 12142 12143 if (!BNXT_PHY_CFG_ABLE(bp)) 12144 return 0; 12145 12146 support_changed = bnxt_support_speed_dropped(link_info); 12147 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12148 bnxt_hwrm_set_link_setting(bp, true, false); 12149 return 0; 12150 } 12151 12152 static void bnxt_get_port_module_status(struct bnxt *bp) 12153 { 12154 struct bnxt_link_info *link_info = &bp->link_info; 12155 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12156 u8 module_status; 12157 12158 if (bnxt_update_link(bp, true)) 12159 return; 12160 12161 module_status = link_info->module_status; 12162 switch (module_status) { 12163 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12164 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12165 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12166 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12167 bp->pf.port_id); 12168 if (bp->hwrm_spec_code >= 0x10201) { 12169 netdev_warn(bp->dev, "Module part number %s\n", 12170 resp->phy_vendor_partnumber); 12171 } 12172 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12173 netdev_warn(bp->dev, "TX is disabled\n"); 12174 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12175 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12176 } 12177 } 12178 12179 static void 12180 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12181 { 12182 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12183 if (bp->hwrm_spec_code >= 0x10201) 12184 req->auto_pause = 12185 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12186 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12187 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12188 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12189 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12190 req->enables |= 12191 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12192 } else { 12193 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12194 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12195 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12196 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12197 req->enables |= 12198 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12199 if (bp->hwrm_spec_code >= 0x10201) { 12200 req->auto_pause = req->force_pause; 12201 req->enables |= cpu_to_le32( 12202 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12203 } 12204 } 12205 } 12206 12207 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12208 { 12209 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12210 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12211 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12212 req->enables |= 12213 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12214 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12215 } else if (bp->link_info.advertising) { 12216 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12217 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12218 } 12219 if (bp->link_info.advertising_pam4) { 12220 req->enables |= 12221 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12222 req->auto_link_pam4_speed_mask = 12223 cpu_to_le16(bp->link_info.advertising_pam4); 12224 } 12225 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12226 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12227 } else { 12228 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12229 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12230 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12231 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12232 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12233 (u32)bp->link_info.req_link_speed); 12234 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12235 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12236 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12237 } else { 12238 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12239 } 12240 } 12241 12242 /* tell chimp that the setting takes effect immediately */ 12243 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12244 } 12245 12246 int bnxt_hwrm_set_pause(struct bnxt *bp) 12247 { 12248 struct hwrm_port_phy_cfg_input *req; 12249 int rc; 12250 12251 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12252 if (rc) 12253 return rc; 12254 12255 bnxt_hwrm_set_pause_common(bp, req); 12256 12257 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12258 bp->link_info.force_link_chng) 12259 bnxt_hwrm_set_link_common(bp, req); 12260 12261 rc = hwrm_req_send(bp, req); 12262 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12263 /* since changing of pause setting doesn't trigger any link 12264 * change event, the driver needs to update the current pause 12265 * result upon successfully return of the phy_cfg command 12266 */ 12267 bp->link_info.pause = 12268 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12269 bp->link_info.auto_pause_setting = 0; 12270 if (!bp->link_info.force_link_chng) 12271 bnxt_report_link(bp); 12272 } 12273 bp->link_info.force_link_chng = false; 12274 return rc; 12275 } 12276 12277 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12278 struct hwrm_port_phy_cfg_input *req) 12279 { 12280 struct ethtool_keee *eee = &bp->eee; 12281 12282 if (eee->eee_enabled) { 12283 u16 eee_speeds; 12284 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12285 12286 if (eee->tx_lpi_enabled) 12287 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12288 else 12289 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12290 12291 req->flags |= cpu_to_le32(flags); 12292 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12293 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12294 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12295 } else { 12296 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12297 } 12298 } 12299 12300 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12301 { 12302 struct hwrm_port_phy_cfg_input *req; 12303 int rc; 12304 12305 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12306 if (rc) 12307 return rc; 12308 12309 if (set_pause) 12310 bnxt_hwrm_set_pause_common(bp, req); 12311 12312 bnxt_hwrm_set_link_common(bp, req); 12313 12314 if (set_eee) 12315 bnxt_hwrm_set_eee(bp, req); 12316 return hwrm_req_send(bp, req); 12317 } 12318 12319 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12320 { 12321 struct hwrm_port_phy_cfg_input *req; 12322 int rc; 12323 12324 if (!BNXT_SINGLE_PF(bp)) 12325 return 0; 12326 12327 if (pci_num_vf(bp->pdev) && 12328 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12329 return 0; 12330 12331 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12332 if (rc) 12333 return rc; 12334 12335 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12336 rc = hwrm_req_send(bp, req); 12337 if (!rc) { 12338 mutex_lock(&bp->link_lock); 12339 /* Device is not obliged link down in certain scenarios, even 12340 * when forced. Setting the state unknown is consistent with 12341 * driver startup and will force link state to be reported 12342 * during subsequent open based on PORT_PHY_QCFG. 12343 */ 12344 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12345 mutex_unlock(&bp->link_lock); 12346 } 12347 return rc; 12348 } 12349 12350 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12351 { 12352 #ifdef CONFIG_TEE_BNXT_FW 12353 int rc = tee_bnxt_fw_load(); 12354 12355 if (rc) 12356 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12357 12358 return rc; 12359 #else 12360 netdev_err(bp->dev, "OP-TEE not supported\n"); 12361 return -ENODEV; 12362 #endif 12363 } 12364 12365 static int bnxt_try_recover_fw(struct bnxt *bp) 12366 { 12367 if (bp->fw_health && bp->fw_health->status_reliable) { 12368 int retry = 0, rc; 12369 u32 sts; 12370 12371 do { 12372 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12373 rc = bnxt_hwrm_poll(bp); 12374 if (!BNXT_FW_IS_BOOTING(sts) && 12375 !BNXT_FW_IS_RECOVERING(sts)) 12376 break; 12377 retry++; 12378 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12379 12380 if (!BNXT_FW_IS_HEALTHY(sts)) { 12381 netdev_err(bp->dev, 12382 "Firmware not responding, status: 0x%x\n", 12383 sts); 12384 rc = -ENODEV; 12385 } 12386 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12387 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12388 return bnxt_fw_reset_via_optee(bp); 12389 } 12390 return rc; 12391 } 12392 12393 return -ENODEV; 12394 } 12395 12396 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12397 { 12398 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12399 12400 if (!BNXT_NEW_RM(bp)) 12401 return; /* no resource reservations required */ 12402 12403 hw_resc->resv_cp_rings = 0; 12404 hw_resc->resv_stat_ctxs = 0; 12405 hw_resc->resv_irqs = 0; 12406 hw_resc->resv_tx_rings = 0; 12407 hw_resc->resv_rx_rings = 0; 12408 hw_resc->resv_hw_ring_grps = 0; 12409 hw_resc->resv_vnics = 0; 12410 hw_resc->resv_rsscos_ctxs = 0; 12411 if (!fw_reset) { 12412 bp->tx_nr_rings = 0; 12413 bp->rx_nr_rings = 0; 12414 } 12415 } 12416 12417 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12418 { 12419 int rc; 12420 12421 if (!BNXT_NEW_RM(bp)) 12422 return 0; /* no resource reservations required */ 12423 12424 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12425 if (rc) 12426 netdev_err(bp->dev, "resc_qcaps failed\n"); 12427 12428 bnxt_clear_reservations(bp, fw_reset); 12429 12430 return rc; 12431 } 12432 12433 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12434 { 12435 struct hwrm_func_drv_if_change_output *resp; 12436 struct hwrm_func_drv_if_change_input *req; 12437 bool resc_reinit = false; 12438 bool caps_change = false; 12439 int rc, retry = 0; 12440 bool fw_reset; 12441 u32 flags = 0; 12442 12443 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12444 bp->fw_reset_state = 0; 12445 12446 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12447 return 0; 12448 12449 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12450 if (rc) 12451 return rc; 12452 12453 if (up) 12454 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12455 resp = hwrm_req_hold(bp, req); 12456 12457 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12458 while (retry < BNXT_FW_IF_RETRY) { 12459 rc = hwrm_req_send(bp, req); 12460 if (rc != -EAGAIN) 12461 break; 12462 12463 msleep(50); 12464 retry++; 12465 } 12466 12467 if (rc == -EAGAIN) { 12468 hwrm_req_drop(bp, req); 12469 return rc; 12470 } else if (!rc) { 12471 flags = le32_to_cpu(resp->flags); 12472 } else if (up) { 12473 rc = bnxt_try_recover_fw(bp); 12474 fw_reset = true; 12475 } 12476 hwrm_req_drop(bp, req); 12477 if (rc) 12478 return rc; 12479 12480 if (!up) { 12481 bnxt_inv_fw_health_reg(bp); 12482 return 0; 12483 } 12484 12485 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12486 resc_reinit = true; 12487 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12488 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12489 fw_reset = true; 12490 else 12491 bnxt_remap_fw_health_regs(bp); 12492 12493 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12494 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12495 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12496 return -ENODEV; 12497 } 12498 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12499 caps_change = true; 12500 12501 if (resc_reinit || fw_reset || caps_change) { 12502 if (fw_reset || caps_change) { 12503 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12504 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12505 bnxt_ulp_irq_stop(bp); 12506 bnxt_free_ctx_mem(bp, false); 12507 bnxt_dcb_free(bp); 12508 rc = bnxt_fw_init_one(bp); 12509 if (rc) { 12510 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12511 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12512 return rc; 12513 } 12514 /* IRQ will be initialized later in bnxt_request_irq()*/ 12515 bnxt_clear_int_mode(bp); 12516 } 12517 rc = bnxt_cancel_reservations(bp, fw_reset); 12518 } 12519 return rc; 12520 } 12521 12522 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12523 { 12524 struct hwrm_port_led_qcaps_output *resp; 12525 struct hwrm_port_led_qcaps_input *req; 12526 struct bnxt_pf_info *pf = &bp->pf; 12527 int rc; 12528 12529 bp->num_leds = 0; 12530 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12531 return 0; 12532 12533 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12534 if (rc) 12535 return rc; 12536 12537 req->port_id = cpu_to_le16(pf->port_id); 12538 resp = hwrm_req_hold(bp, req); 12539 rc = hwrm_req_send(bp, req); 12540 if (rc) { 12541 hwrm_req_drop(bp, req); 12542 return rc; 12543 } 12544 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12545 int i; 12546 12547 bp->num_leds = resp->num_leds; 12548 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12549 bp->num_leds); 12550 for (i = 0; i < bp->num_leds; i++) { 12551 struct bnxt_led_info *led = &bp->leds[i]; 12552 __le16 caps = led->led_state_caps; 12553 12554 if (!led->led_group_id || 12555 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12556 bp->num_leds = 0; 12557 break; 12558 } 12559 } 12560 } 12561 hwrm_req_drop(bp, req); 12562 return 0; 12563 } 12564 12565 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12566 { 12567 struct hwrm_wol_filter_alloc_output *resp; 12568 struct hwrm_wol_filter_alloc_input *req; 12569 int rc; 12570 12571 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12572 if (rc) 12573 return rc; 12574 12575 req->port_id = cpu_to_le16(bp->pf.port_id); 12576 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12577 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12578 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12579 12580 resp = hwrm_req_hold(bp, req); 12581 rc = hwrm_req_send(bp, req); 12582 if (!rc) 12583 bp->wol_filter_id = resp->wol_filter_id; 12584 hwrm_req_drop(bp, req); 12585 return rc; 12586 } 12587 12588 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12589 { 12590 struct hwrm_wol_filter_free_input *req; 12591 int rc; 12592 12593 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12594 if (rc) 12595 return rc; 12596 12597 req->port_id = cpu_to_le16(bp->pf.port_id); 12598 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12599 req->wol_filter_id = bp->wol_filter_id; 12600 12601 return hwrm_req_send(bp, req); 12602 } 12603 12604 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12605 { 12606 struct hwrm_wol_filter_qcfg_output *resp; 12607 struct hwrm_wol_filter_qcfg_input *req; 12608 u16 next_handle = 0; 12609 int rc; 12610 12611 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12612 if (rc) 12613 return rc; 12614 12615 req->port_id = cpu_to_le16(bp->pf.port_id); 12616 req->handle = cpu_to_le16(handle); 12617 resp = hwrm_req_hold(bp, req); 12618 rc = hwrm_req_send(bp, req); 12619 if (!rc) { 12620 next_handle = le16_to_cpu(resp->next_handle); 12621 if (next_handle != 0) { 12622 if (resp->wol_type == 12623 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12624 bp->wol = 1; 12625 bp->wol_filter_id = resp->wol_filter_id; 12626 } 12627 } 12628 } 12629 hwrm_req_drop(bp, req); 12630 return next_handle; 12631 } 12632 12633 static void bnxt_get_wol_settings(struct bnxt *bp) 12634 { 12635 u16 handle = 0; 12636 12637 bp->wol = 0; 12638 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12639 return; 12640 12641 do { 12642 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12643 } while (handle && handle != 0xffff); 12644 } 12645 12646 static bool bnxt_eee_config_ok(struct bnxt *bp) 12647 { 12648 struct ethtool_keee *eee = &bp->eee; 12649 struct bnxt_link_info *link_info = &bp->link_info; 12650 12651 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12652 return true; 12653 12654 if (eee->eee_enabled) { 12655 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12656 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12657 12658 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12659 12660 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12661 eee->eee_enabled = 0; 12662 return false; 12663 } 12664 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12665 linkmode_and(eee->advertised, advertising, 12666 eee->supported); 12667 return false; 12668 } 12669 } 12670 return true; 12671 } 12672 12673 static int bnxt_update_phy_setting(struct bnxt *bp) 12674 { 12675 int rc; 12676 bool update_link = false; 12677 bool update_pause = false; 12678 bool update_eee = false; 12679 struct bnxt_link_info *link_info = &bp->link_info; 12680 12681 rc = bnxt_update_link(bp, true); 12682 if (rc) { 12683 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12684 rc); 12685 return rc; 12686 } 12687 if (!BNXT_SINGLE_PF(bp)) 12688 return 0; 12689 12690 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12691 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12692 link_info->req_flow_ctrl) 12693 update_pause = true; 12694 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12695 link_info->force_pause_setting != link_info->req_flow_ctrl) 12696 update_pause = true; 12697 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12698 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12699 update_link = true; 12700 if (bnxt_force_speed_updated(link_info)) 12701 update_link = true; 12702 if (link_info->req_duplex != link_info->duplex_setting) 12703 update_link = true; 12704 } else { 12705 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12706 update_link = true; 12707 if (bnxt_auto_speed_updated(link_info)) 12708 update_link = true; 12709 } 12710 12711 /* The last close may have shutdown the link, so need to call 12712 * PHY_CFG to bring it back up. 12713 */ 12714 if (!BNXT_LINK_IS_UP(bp)) 12715 update_link = true; 12716 12717 if (!bnxt_eee_config_ok(bp)) 12718 update_eee = true; 12719 12720 if (update_link) 12721 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12722 else if (update_pause) 12723 rc = bnxt_hwrm_set_pause(bp); 12724 if (rc) { 12725 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12726 rc); 12727 return rc; 12728 } 12729 12730 return rc; 12731 } 12732 12733 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12734 12735 static int bnxt_reinit_after_abort(struct bnxt *bp) 12736 { 12737 int rc; 12738 12739 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12740 return -EBUSY; 12741 12742 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12743 return -ENODEV; 12744 12745 rc = bnxt_fw_init_one(bp); 12746 if (!rc) { 12747 bnxt_clear_int_mode(bp); 12748 rc = bnxt_init_int_mode(bp); 12749 if (!rc) { 12750 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12751 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12752 } 12753 } 12754 return rc; 12755 } 12756 12757 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12758 { 12759 struct bnxt_ntuple_filter *ntp_fltr; 12760 struct bnxt_l2_filter *l2_fltr; 12761 12762 if (list_empty(&fltr->list)) 12763 return; 12764 12765 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12766 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12767 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12768 atomic_inc(&l2_fltr->refcnt); 12769 ntp_fltr->l2_fltr = l2_fltr; 12770 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12771 bnxt_del_ntp_filter(bp, ntp_fltr); 12772 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12773 fltr->sw_id); 12774 } 12775 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12776 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12777 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12778 bnxt_del_l2_filter(bp, l2_fltr); 12779 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12780 fltr->sw_id); 12781 } 12782 } 12783 } 12784 12785 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12786 { 12787 struct bnxt_filter_base *usr_fltr, *tmp; 12788 12789 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12790 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12791 } 12792 12793 static int bnxt_set_xps_mapping(struct bnxt *bp) 12794 { 12795 int numa_node = dev_to_node(&bp->pdev->dev); 12796 unsigned int q_idx, map_idx, cpu, i; 12797 const struct cpumask *cpu_mask_ptr; 12798 int nr_cpus = num_online_cpus(); 12799 cpumask_t *q_map; 12800 int rc = 0; 12801 12802 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12803 if (!q_map) 12804 return -ENOMEM; 12805 12806 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12807 * Each TC has the same number of TX queues. The nth TX queue for each 12808 * TC will have the same CPU mask. 12809 */ 12810 for (i = 0; i < nr_cpus; i++) { 12811 map_idx = i % bp->tx_nr_rings_per_tc; 12812 cpu = cpumask_local_spread(i, numa_node); 12813 cpu_mask_ptr = get_cpu_mask(cpu); 12814 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12815 } 12816 12817 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12818 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12819 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12820 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12821 if (rc) { 12822 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12823 q_idx); 12824 break; 12825 } 12826 } 12827 12828 kfree(q_map); 12829 12830 return rc; 12831 } 12832 12833 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12834 { 12835 int rc = 0; 12836 12837 netif_carrier_off(bp->dev); 12838 if (irq_re_init) { 12839 /* Reserve rings now if none were reserved at driver probe. */ 12840 rc = bnxt_init_dflt_ring_mode(bp); 12841 if (rc) { 12842 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12843 return rc; 12844 } 12845 } 12846 rc = bnxt_reserve_rings(bp, irq_re_init); 12847 if (rc) 12848 return rc; 12849 12850 rc = bnxt_alloc_mem(bp, irq_re_init); 12851 if (rc) { 12852 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12853 goto open_err_free_mem; 12854 } 12855 12856 if (irq_re_init) { 12857 bnxt_init_napi(bp); 12858 rc = bnxt_request_irq(bp); 12859 if (rc) { 12860 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12861 goto open_err_irq; 12862 } 12863 } 12864 12865 rc = bnxt_init_nic(bp, irq_re_init); 12866 if (rc) { 12867 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12868 goto open_err_irq; 12869 } 12870 12871 bnxt_enable_napi(bp); 12872 bnxt_debug_dev_init(bp); 12873 12874 if (link_re_init) { 12875 mutex_lock(&bp->link_lock); 12876 rc = bnxt_update_phy_setting(bp); 12877 mutex_unlock(&bp->link_lock); 12878 if (rc) { 12879 netdev_warn(bp->dev, "failed to update phy settings\n"); 12880 if (BNXT_SINGLE_PF(bp)) { 12881 bp->link_info.phy_retry = true; 12882 bp->link_info.phy_retry_expires = 12883 jiffies + 5 * HZ; 12884 } 12885 } 12886 } 12887 12888 if (irq_re_init) { 12889 udp_tunnel_nic_reset_ntf(bp->dev); 12890 rc = bnxt_set_xps_mapping(bp); 12891 if (rc) 12892 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12893 } 12894 12895 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12896 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12897 static_branch_enable(&bnxt_xdp_locking_key); 12898 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12899 static_branch_disable(&bnxt_xdp_locking_key); 12900 } 12901 set_bit(BNXT_STATE_OPEN, &bp->state); 12902 bnxt_enable_int(bp); 12903 /* Enable TX queues */ 12904 bnxt_tx_enable(bp); 12905 mod_timer(&bp->timer, jiffies + bp->current_interval); 12906 /* Poll link status and check for SFP+ module status */ 12907 mutex_lock(&bp->link_lock); 12908 bnxt_get_port_module_status(bp); 12909 mutex_unlock(&bp->link_lock); 12910 12911 /* VF-reps may need to be re-opened after the PF is re-opened */ 12912 if (BNXT_PF(bp)) 12913 bnxt_vf_reps_open(bp); 12914 bnxt_ptp_init_rtc(bp, true); 12915 bnxt_ptp_cfg_tstamp_filters(bp); 12916 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12917 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12918 bnxt_cfg_usr_fltrs(bp); 12919 return 0; 12920 12921 open_err_irq: 12922 bnxt_del_napi(bp); 12923 12924 open_err_free_mem: 12925 bnxt_free_skbs(bp); 12926 bnxt_free_irq(bp); 12927 bnxt_free_mem(bp, true); 12928 return rc; 12929 } 12930 12931 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12932 { 12933 int rc = 0; 12934 12935 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12936 rc = -EIO; 12937 if (!rc) 12938 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12939 if (rc) { 12940 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12941 netif_close(bp->dev); 12942 } 12943 return rc; 12944 } 12945 12946 /* netdev instance lock held, open the NIC half way by allocating all 12947 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 12948 * for offline self tests. 12949 */ 12950 int bnxt_half_open_nic(struct bnxt *bp) 12951 { 12952 int rc = 0; 12953 12954 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12955 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12956 rc = -ENODEV; 12957 goto half_open_err; 12958 } 12959 12960 rc = bnxt_alloc_mem(bp, true); 12961 if (rc) { 12962 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12963 goto half_open_err; 12964 } 12965 bnxt_init_napi(bp); 12966 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12967 rc = bnxt_init_nic(bp, true); 12968 if (rc) { 12969 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12970 bnxt_del_napi(bp); 12971 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12972 goto half_open_err; 12973 } 12974 return 0; 12975 12976 half_open_err: 12977 bnxt_free_skbs(bp); 12978 bnxt_free_mem(bp, true); 12979 netif_close(bp->dev); 12980 return rc; 12981 } 12982 12983 /* netdev instance lock held, this call can only be made after a previous 12984 * successful call to bnxt_half_open_nic(). 12985 */ 12986 void bnxt_half_close_nic(struct bnxt *bp) 12987 { 12988 bnxt_hwrm_resource_free(bp, false, true); 12989 bnxt_del_napi(bp); 12990 bnxt_free_skbs(bp); 12991 bnxt_free_mem(bp, true); 12992 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12993 } 12994 12995 void bnxt_reenable_sriov(struct bnxt *bp) 12996 { 12997 if (BNXT_PF(bp)) { 12998 struct bnxt_pf_info *pf = &bp->pf; 12999 int n = pf->active_vfs; 13000 13001 if (n) 13002 bnxt_cfg_hw_sriov(bp, &n, true); 13003 } 13004 } 13005 13006 static int bnxt_open(struct net_device *dev) 13007 { 13008 struct bnxt *bp = netdev_priv(dev); 13009 int rc; 13010 13011 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13012 rc = bnxt_reinit_after_abort(bp); 13013 if (rc) { 13014 if (rc == -EBUSY) 13015 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13016 else 13017 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13018 return -ENODEV; 13019 } 13020 } 13021 13022 rc = bnxt_hwrm_if_change(bp, true); 13023 if (rc) 13024 return rc; 13025 13026 rc = __bnxt_open_nic(bp, true, true); 13027 if (rc) { 13028 bnxt_hwrm_if_change(bp, false); 13029 } else { 13030 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13031 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13032 bnxt_queue_sp_work(bp, 13033 BNXT_RESTART_ULP_SP_EVENT); 13034 } 13035 } 13036 13037 return rc; 13038 } 13039 13040 static bool bnxt_drv_busy(struct bnxt *bp) 13041 { 13042 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13043 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13044 } 13045 13046 static void bnxt_get_ring_stats(struct bnxt *bp, 13047 struct rtnl_link_stats64 *stats); 13048 13049 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13050 bool link_re_init) 13051 { 13052 /* Close the VF-reps before closing PF */ 13053 if (BNXT_PF(bp)) 13054 bnxt_vf_reps_close(bp); 13055 13056 /* Change device state to avoid TX queue wake up's */ 13057 bnxt_tx_disable(bp); 13058 13059 clear_bit(BNXT_STATE_OPEN, &bp->state); 13060 smp_mb__after_atomic(); 13061 while (bnxt_drv_busy(bp)) 13062 msleep(20); 13063 13064 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13065 bnxt_clear_rss_ctxs(bp); 13066 /* Flush rings and disable interrupts */ 13067 bnxt_shutdown_nic(bp, irq_re_init); 13068 13069 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13070 13071 bnxt_debug_dev_exit(bp); 13072 bnxt_disable_napi(bp); 13073 timer_delete_sync(&bp->timer); 13074 bnxt_free_skbs(bp); 13075 13076 /* Save ring stats before shutdown */ 13077 if (bp->bnapi && irq_re_init) { 13078 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13079 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 13080 } 13081 if (irq_re_init) { 13082 bnxt_free_irq(bp); 13083 bnxt_del_napi(bp); 13084 } 13085 bnxt_free_mem(bp, irq_re_init); 13086 } 13087 13088 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13089 { 13090 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13091 /* If we get here, it means firmware reset is in progress 13092 * while we are trying to close. We can safely proceed with 13093 * the close because we are holding netdev instance lock. 13094 * Some firmware messages may fail as we proceed to close. 13095 * We set the ABORT_ERR flag here so that the FW reset thread 13096 * will later abort when it gets the netdev instance lock 13097 * and sees the flag. 13098 */ 13099 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13100 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13101 } 13102 13103 #ifdef CONFIG_BNXT_SRIOV 13104 if (bp->sriov_cfg) { 13105 int rc; 13106 13107 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13108 !bp->sriov_cfg, 13109 BNXT_SRIOV_CFG_WAIT_TMO); 13110 if (!rc) 13111 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13112 else if (rc < 0) 13113 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13114 } 13115 #endif 13116 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13117 } 13118 13119 static int bnxt_close(struct net_device *dev) 13120 { 13121 struct bnxt *bp = netdev_priv(dev); 13122 13123 bnxt_close_nic(bp, true, true); 13124 bnxt_hwrm_shutdown_link(bp); 13125 bnxt_hwrm_if_change(bp, false); 13126 return 0; 13127 } 13128 13129 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13130 u16 *val) 13131 { 13132 struct hwrm_port_phy_mdio_read_output *resp; 13133 struct hwrm_port_phy_mdio_read_input *req; 13134 int rc; 13135 13136 if (bp->hwrm_spec_code < 0x10a00) 13137 return -EOPNOTSUPP; 13138 13139 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13140 if (rc) 13141 return rc; 13142 13143 req->port_id = cpu_to_le16(bp->pf.port_id); 13144 req->phy_addr = phy_addr; 13145 req->reg_addr = cpu_to_le16(reg & 0x1f); 13146 if (mdio_phy_id_is_c45(phy_addr)) { 13147 req->cl45_mdio = 1; 13148 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13149 req->dev_addr = mdio_phy_id_devad(phy_addr); 13150 req->reg_addr = cpu_to_le16(reg); 13151 } 13152 13153 resp = hwrm_req_hold(bp, req); 13154 rc = hwrm_req_send(bp, req); 13155 if (!rc) 13156 *val = le16_to_cpu(resp->reg_data); 13157 hwrm_req_drop(bp, req); 13158 return rc; 13159 } 13160 13161 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13162 u16 val) 13163 { 13164 struct hwrm_port_phy_mdio_write_input *req; 13165 int rc; 13166 13167 if (bp->hwrm_spec_code < 0x10a00) 13168 return -EOPNOTSUPP; 13169 13170 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13171 if (rc) 13172 return rc; 13173 13174 req->port_id = cpu_to_le16(bp->pf.port_id); 13175 req->phy_addr = phy_addr; 13176 req->reg_addr = cpu_to_le16(reg & 0x1f); 13177 if (mdio_phy_id_is_c45(phy_addr)) { 13178 req->cl45_mdio = 1; 13179 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13180 req->dev_addr = mdio_phy_id_devad(phy_addr); 13181 req->reg_addr = cpu_to_le16(reg); 13182 } 13183 req->reg_data = cpu_to_le16(val); 13184 13185 return hwrm_req_send(bp, req); 13186 } 13187 13188 /* netdev instance lock held */ 13189 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13190 { 13191 struct mii_ioctl_data *mdio = if_mii(ifr); 13192 struct bnxt *bp = netdev_priv(dev); 13193 int rc; 13194 13195 switch (cmd) { 13196 case SIOCGMIIPHY: 13197 mdio->phy_id = bp->link_info.phy_addr; 13198 13199 fallthrough; 13200 case SIOCGMIIREG: { 13201 u16 mii_regval = 0; 13202 13203 if (!netif_running(dev)) 13204 return -EAGAIN; 13205 13206 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13207 &mii_regval); 13208 mdio->val_out = mii_regval; 13209 return rc; 13210 } 13211 13212 case SIOCSMIIREG: 13213 if (!netif_running(dev)) 13214 return -EAGAIN; 13215 13216 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13217 mdio->val_in); 13218 13219 case SIOCSHWTSTAMP: 13220 return bnxt_hwtstamp_set(dev, ifr); 13221 13222 case SIOCGHWTSTAMP: 13223 return bnxt_hwtstamp_get(dev, ifr); 13224 13225 default: 13226 /* do nothing */ 13227 break; 13228 } 13229 return -EOPNOTSUPP; 13230 } 13231 13232 static void bnxt_get_ring_stats(struct bnxt *bp, 13233 struct rtnl_link_stats64 *stats) 13234 { 13235 int i; 13236 13237 for (i = 0; i < bp->cp_nr_rings; i++) { 13238 struct bnxt_napi *bnapi = bp->bnapi[i]; 13239 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13240 u64 *sw = cpr->stats.sw_stats; 13241 13242 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13243 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13244 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13245 13246 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13247 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13248 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13249 13250 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13251 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13252 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13253 13254 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13255 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13256 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13257 13258 stats->rx_missed_errors += 13259 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13260 13261 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13262 13263 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13264 13265 stats->rx_dropped += 13266 cpr->sw_stats->rx.rx_netpoll_discards + 13267 cpr->sw_stats->rx.rx_oom_discards; 13268 } 13269 } 13270 13271 static void bnxt_add_prev_stats(struct bnxt *bp, 13272 struct rtnl_link_stats64 *stats) 13273 { 13274 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13275 13276 stats->rx_packets += prev_stats->rx_packets; 13277 stats->tx_packets += prev_stats->tx_packets; 13278 stats->rx_bytes += prev_stats->rx_bytes; 13279 stats->tx_bytes += prev_stats->tx_bytes; 13280 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13281 stats->multicast += prev_stats->multicast; 13282 stats->rx_dropped += prev_stats->rx_dropped; 13283 stats->tx_dropped += prev_stats->tx_dropped; 13284 } 13285 13286 static void 13287 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13288 { 13289 struct bnxt *bp = netdev_priv(dev); 13290 13291 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13292 /* Make sure bnxt_close_nic() sees that we are reading stats before 13293 * we check the BNXT_STATE_OPEN flag. 13294 */ 13295 smp_mb__after_atomic(); 13296 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13297 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13298 *stats = bp->net_stats_prev; 13299 return; 13300 } 13301 13302 bnxt_get_ring_stats(bp, stats); 13303 bnxt_add_prev_stats(bp, stats); 13304 13305 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13306 u64 *rx = bp->port_stats.sw_stats; 13307 u64 *tx = bp->port_stats.sw_stats + 13308 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13309 13310 stats->rx_crc_errors = 13311 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13312 stats->rx_frame_errors = 13313 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13314 stats->rx_length_errors = 13315 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13316 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13317 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13318 stats->rx_errors = 13319 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13320 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13321 stats->collisions = 13322 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13323 stats->tx_fifo_errors = 13324 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13325 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13326 } 13327 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13328 } 13329 13330 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 13331 struct bnxt_total_ring_err_stats *stats, 13332 struct bnxt_cp_ring_info *cpr) 13333 { 13334 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13335 u64 *hw_stats = cpr->stats.sw_stats; 13336 13337 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13338 stats->rx_total_resets += sw_stats->rx.rx_resets; 13339 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13340 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13341 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13342 stats->rx_total_ring_discards += 13343 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13344 stats->tx_total_resets += sw_stats->tx.tx_resets; 13345 stats->tx_total_ring_discards += 13346 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13347 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13348 } 13349 13350 void bnxt_get_ring_err_stats(struct bnxt *bp, 13351 struct bnxt_total_ring_err_stats *stats) 13352 { 13353 int i; 13354 13355 for (i = 0; i < bp->cp_nr_rings; i++) 13356 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13357 } 13358 13359 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 13360 { 13361 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13362 struct net_device *dev = bp->dev; 13363 struct netdev_hw_addr *ha; 13364 u8 *haddr; 13365 int mc_count = 0; 13366 bool update = false; 13367 int off = 0; 13368 13369 netdev_for_each_mc_addr(ha, dev) { 13370 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13371 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13372 vnic->mc_list_count = 0; 13373 return false; 13374 } 13375 haddr = ha->addr; 13376 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13377 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13378 update = true; 13379 } 13380 off += ETH_ALEN; 13381 mc_count++; 13382 } 13383 if (mc_count) 13384 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13385 13386 if (mc_count != vnic->mc_list_count) { 13387 vnic->mc_list_count = mc_count; 13388 update = true; 13389 } 13390 return update; 13391 } 13392 13393 static bool bnxt_uc_list_updated(struct bnxt *bp) 13394 { 13395 struct net_device *dev = bp->dev; 13396 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13397 struct netdev_hw_addr *ha; 13398 int off = 0; 13399 13400 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13401 return true; 13402 13403 netdev_for_each_uc_addr(ha, dev) { 13404 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13405 return true; 13406 13407 off += ETH_ALEN; 13408 } 13409 return false; 13410 } 13411 13412 static void bnxt_set_rx_mode(struct net_device *dev) 13413 { 13414 struct bnxt *bp = netdev_priv(dev); 13415 struct bnxt_vnic_info *vnic; 13416 bool mc_update = false; 13417 bool uc_update; 13418 u32 mask; 13419 13420 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13421 return; 13422 13423 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13424 mask = vnic->rx_mask; 13425 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13426 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13427 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13428 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13429 13430 if (dev->flags & IFF_PROMISC) 13431 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13432 13433 uc_update = bnxt_uc_list_updated(bp); 13434 13435 if (dev->flags & IFF_BROADCAST) 13436 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13437 if (dev->flags & IFF_ALLMULTI) { 13438 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13439 vnic->mc_list_count = 0; 13440 } else if (dev->flags & IFF_MULTICAST) { 13441 mc_update = bnxt_mc_list_updated(bp, &mask); 13442 } 13443 13444 if (mask != vnic->rx_mask || uc_update || mc_update) { 13445 vnic->rx_mask = mask; 13446 13447 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13448 } 13449 } 13450 13451 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13452 { 13453 struct net_device *dev = bp->dev; 13454 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13455 struct netdev_hw_addr *ha; 13456 int i, off = 0, rc; 13457 bool uc_update; 13458 13459 netif_addr_lock_bh(dev); 13460 uc_update = bnxt_uc_list_updated(bp); 13461 netif_addr_unlock_bh(dev); 13462 13463 if (!uc_update) 13464 goto skip_uc; 13465 13466 for (i = 1; i < vnic->uc_filter_count; i++) { 13467 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13468 13469 bnxt_hwrm_l2_filter_free(bp, fltr); 13470 bnxt_del_l2_filter(bp, fltr); 13471 } 13472 13473 vnic->uc_filter_count = 1; 13474 13475 netif_addr_lock_bh(dev); 13476 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13477 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13478 } else { 13479 netdev_for_each_uc_addr(ha, dev) { 13480 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13481 off += ETH_ALEN; 13482 vnic->uc_filter_count++; 13483 } 13484 } 13485 netif_addr_unlock_bh(dev); 13486 13487 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13488 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13489 if (rc) { 13490 if (BNXT_VF(bp) && rc == -ENODEV) { 13491 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13492 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13493 else 13494 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13495 rc = 0; 13496 } else { 13497 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13498 } 13499 vnic->uc_filter_count = i; 13500 return rc; 13501 } 13502 } 13503 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13504 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13505 13506 skip_uc: 13507 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13508 !bnxt_promisc_ok(bp)) 13509 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13510 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13511 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13512 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13513 rc); 13514 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13515 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13516 vnic->mc_list_count = 0; 13517 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13518 } 13519 if (rc) 13520 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13521 rc); 13522 13523 return rc; 13524 } 13525 13526 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13527 { 13528 #ifdef CONFIG_BNXT_SRIOV 13529 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13530 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13531 13532 /* No minimum rings were provisioned by the PF. Don't 13533 * reserve rings by default when device is down. 13534 */ 13535 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13536 return true; 13537 13538 if (!netif_running(bp->dev)) 13539 return false; 13540 } 13541 #endif 13542 return true; 13543 } 13544 13545 /* If the chip and firmware supports RFS */ 13546 static bool bnxt_rfs_supported(struct bnxt *bp) 13547 { 13548 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13549 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13550 return true; 13551 return false; 13552 } 13553 /* 212 firmware is broken for aRFS */ 13554 if (BNXT_FW_MAJ(bp) == 212) 13555 return false; 13556 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13557 return true; 13558 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13559 return true; 13560 return false; 13561 } 13562 13563 /* If runtime conditions support RFS */ 13564 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13565 { 13566 struct bnxt_hw_rings hwr = {0}; 13567 int max_vnics, max_rss_ctxs; 13568 13569 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13570 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13571 return bnxt_rfs_supported(bp); 13572 13573 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13574 return false; 13575 13576 hwr.grp = bp->rx_nr_rings; 13577 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13578 if (new_rss_ctx) 13579 hwr.vnic++; 13580 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13581 max_vnics = bnxt_get_max_func_vnics(bp); 13582 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13583 13584 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13585 if (bp->rx_nr_rings > 1) 13586 netdev_warn(bp->dev, 13587 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13588 min(max_rss_ctxs - 1, max_vnics - 1)); 13589 return false; 13590 } 13591 13592 if (!BNXT_NEW_RM(bp)) 13593 return true; 13594 13595 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13596 * issue that will mess up the default VNIC if we reduce the 13597 * reservations. 13598 */ 13599 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13600 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13601 return true; 13602 13603 bnxt_hwrm_reserve_rings(bp, &hwr); 13604 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13605 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13606 return true; 13607 13608 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13609 hwr.vnic = 1; 13610 hwr.rss_ctx = 0; 13611 bnxt_hwrm_reserve_rings(bp, &hwr); 13612 return false; 13613 } 13614 13615 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13616 netdev_features_t features) 13617 { 13618 struct bnxt *bp = netdev_priv(dev); 13619 netdev_features_t vlan_features; 13620 13621 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13622 features &= ~NETIF_F_NTUPLE; 13623 13624 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13625 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13626 13627 if (!(features & NETIF_F_GRO)) 13628 features &= ~NETIF_F_GRO_HW; 13629 13630 if (features & NETIF_F_GRO_HW) 13631 features &= ~NETIF_F_LRO; 13632 13633 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13634 * turned on or off together. 13635 */ 13636 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13637 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13638 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13639 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13640 else if (vlan_features) 13641 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13642 } 13643 #ifdef CONFIG_BNXT_SRIOV 13644 if (BNXT_VF(bp) && bp->vf.vlan) 13645 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13646 #endif 13647 return features; 13648 } 13649 13650 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13651 bool link_re_init, u32 flags, bool update_tpa) 13652 { 13653 bnxt_close_nic(bp, irq_re_init, link_re_init); 13654 bp->flags = flags; 13655 if (update_tpa) 13656 bnxt_set_ring_params(bp); 13657 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13658 } 13659 13660 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13661 { 13662 bool update_tpa = false, update_ntuple = false; 13663 struct bnxt *bp = netdev_priv(dev); 13664 u32 flags = bp->flags; 13665 u32 changes; 13666 int rc = 0; 13667 bool re_init = false; 13668 13669 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13670 if (features & NETIF_F_GRO_HW) 13671 flags |= BNXT_FLAG_GRO; 13672 else if (features & NETIF_F_LRO) 13673 flags |= BNXT_FLAG_LRO; 13674 13675 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13676 flags &= ~BNXT_FLAG_TPA; 13677 13678 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13679 flags |= BNXT_FLAG_STRIP_VLAN; 13680 13681 if (features & NETIF_F_NTUPLE) 13682 flags |= BNXT_FLAG_RFS; 13683 else 13684 bnxt_clear_usr_fltrs(bp, true); 13685 13686 changes = flags ^ bp->flags; 13687 if (changes & BNXT_FLAG_TPA) { 13688 update_tpa = true; 13689 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13690 (flags & BNXT_FLAG_TPA) == 0 || 13691 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13692 re_init = true; 13693 } 13694 13695 if (changes & ~BNXT_FLAG_TPA) 13696 re_init = true; 13697 13698 if (changes & BNXT_FLAG_RFS) 13699 update_ntuple = true; 13700 13701 if (flags != bp->flags) { 13702 u32 old_flags = bp->flags; 13703 13704 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13705 bp->flags = flags; 13706 if (update_tpa) 13707 bnxt_set_ring_params(bp); 13708 return rc; 13709 } 13710 13711 if (update_ntuple) 13712 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13713 13714 if (re_init) 13715 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13716 13717 if (update_tpa) { 13718 bp->flags = flags; 13719 rc = bnxt_set_tpa(bp, 13720 (flags & BNXT_FLAG_TPA) ? 13721 true : false); 13722 if (rc) 13723 bp->flags = old_flags; 13724 } 13725 } 13726 return rc; 13727 } 13728 13729 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13730 u8 **nextp) 13731 { 13732 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13733 struct hop_jumbo_hdr *jhdr; 13734 int hdr_count = 0; 13735 u8 *nexthdr; 13736 int start; 13737 13738 /* Check that there are at most 2 IPv6 extension headers, no 13739 * fragment header, and each is <= 64 bytes. 13740 */ 13741 start = nw_off + sizeof(*ip6h); 13742 nexthdr = &ip6h->nexthdr; 13743 while (ipv6_ext_hdr(*nexthdr)) { 13744 struct ipv6_opt_hdr *hp; 13745 int hdrlen; 13746 13747 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13748 *nexthdr == NEXTHDR_FRAGMENT) 13749 return false; 13750 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13751 skb_headlen(skb), NULL); 13752 if (!hp) 13753 return false; 13754 if (*nexthdr == NEXTHDR_AUTH) 13755 hdrlen = ipv6_authlen(hp); 13756 else 13757 hdrlen = ipv6_optlen(hp); 13758 13759 if (hdrlen > 64) 13760 return false; 13761 13762 /* The ext header may be a hop-by-hop header inserted for 13763 * big TCP purposes. This will be removed before sending 13764 * from NIC, so do not count it. 13765 */ 13766 if (*nexthdr == NEXTHDR_HOP) { 13767 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13768 goto increment_hdr; 13769 13770 jhdr = (struct hop_jumbo_hdr *)hp; 13771 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13772 jhdr->nexthdr != IPPROTO_TCP) 13773 goto increment_hdr; 13774 13775 goto next_hdr; 13776 } 13777 increment_hdr: 13778 hdr_count++; 13779 next_hdr: 13780 nexthdr = &hp->nexthdr; 13781 start += hdrlen; 13782 } 13783 if (nextp) { 13784 /* Caller will check inner protocol */ 13785 if (skb->encapsulation) { 13786 *nextp = nexthdr; 13787 return true; 13788 } 13789 *nextp = NULL; 13790 } 13791 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13792 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13793 } 13794 13795 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13796 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13797 { 13798 struct udphdr *uh = udp_hdr(skb); 13799 __be16 udp_port = uh->dest; 13800 13801 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13802 udp_port != bp->vxlan_gpe_port) 13803 return false; 13804 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13805 struct ethhdr *eh = inner_eth_hdr(skb); 13806 13807 switch (eh->h_proto) { 13808 case htons(ETH_P_IP): 13809 return true; 13810 case htons(ETH_P_IPV6): 13811 return bnxt_exthdr_check(bp, skb, 13812 skb_inner_network_offset(skb), 13813 NULL); 13814 } 13815 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13816 return true; 13817 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13818 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13819 NULL); 13820 } 13821 return false; 13822 } 13823 13824 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13825 { 13826 switch (l4_proto) { 13827 case IPPROTO_UDP: 13828 return bnxt_udp_tunl_check(bp, skb); 13829 case IPPROTO_IPIP: 13830 return true; 13831 case IPPROTO_GRE: { 13832 switch (skb->inner_protocol) { 13833 default: 13834 return false; 13835 case htons(ETH_P_IP): 13836 return true; 13837 case htons(ETH_P_IPV6): 13838 fallthrough; 13839 } 13840 } 13841 case IPPROTO_IPV6: 13842 /* Check ext headers of inner ipv6 */ 13843 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13844 NULL); 13845 } 13846 return false; 13847 } 13848 13849 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13850 struct net_device *dev, 13851 netdev_features_t features) 13852 { 13853 struct bnxt *bp = netdev_priv(dev); 13854 u8 *l4_proto; 13855 13856 features = vlan_features_check(skb, features); 13857 switch (vlan_get_protocol(skb)) { 13858 case htons(ETH_P_IP): 13859 if (!skb->encapsulation) 13860 return features; 13861 l4_proto = &ip_hdr(skb)->protocol; 13862 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13863 return features; 13864 break; 13865 case htons(ETH_P_IPV6): 13866 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13867 &l4_proto)) 13868 break; 13869 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13870 return features; 13871 break; 13872 } 13873 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13874 } 13875 13876 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13877 u32 *reg_buf) 13878 { 13879 struct hwrm_dbg_read_direct_output *resp; 13880 struct hwrm_dbg_read_direct_input *req; 13881 __le32 *dbg_reg_buf; 13882 dma_addr_t mapping; 13883 int rc, i; 13884 13885 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13886 if (rc) 13887 return rc; 13888 13889 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13890 &mapping); 13891 if (!dbg_reg_buf) { 13892 rc = -ENOMEM; 13893 goto dbg_rd_reg_exit; 13894 } 13895 13896 req->host_dest_addr = cpu_to_le64(mapping); 13897 13898 resp = hwrm_req_hold(bp, req); 13899 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13900 req->read_len32 = cpu_to_le32(num_words); 13901 13902 rc = hwrm_req_send(bp, req); 13903 if (rc || resp->error_code) { 13904 rc = -EIO; 13905 goto dbg_rd_reg_exit; 13906 } 13907 for (i = 0; i < num_words; i++) 13908 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13909 13910 dbg_rd_reg_exit: 13911 hwrm_req_drop(bp, req); 13912 return rc; 13913 } 13914 13915 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13916 u32 ring_id, u32 *prod, u32 *cons) 13917 { 13918 struct hwrm_dbg_ring_info_get_output *resp; 13919 struct hwrm_dbg_ring_info_get_input *req; 13920 int rc; 13921 13922 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13923 if (rc) 13924 return rc; 13925 13926 req->ring_type = ring_type; 13927 req->fw_ring_id = cpu_to_le32(ring_id); 13928 resp = hwrm_req_hold(bp, req); 13929 rc = hwrm_req_send(bp, req); 13930 if (!rc) { 13931 *prod = le32_to_cpu(resp->producer_index); 13932 *cons = le32_to_cpu(resp->consumer_index); 13933 } 13934 hwrm_req_drop(bp, req); 13935 return rc; 13936 } 13937 13938 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13939 { 13940 struct bnxt_tx_ring_info *txr; 13941 int i = bnapi->index, j; 13942 13943 bnxt_for_each_napi_tx(j, bnapi, txr) 13944 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13945 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13946 txr->tx_cons); 13947 } 13948 13949 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13950 { 13951 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13952 int i = bnapi->index; 13953 13954 if (!rxr) 13955 return; 13956 13957 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", 13958 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13959 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13960 rxr->rx_sw_agg_prod); 13961 } 13962 13963 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13964 { 13965 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13966 int i = bnapi->index; 13967 13968 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13969 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13970 } 13971 13972 static void bnxt_dbg_dump_states(struct bnxt *bp) 13973 { 13974 int i; 13975 struct bnxt_napi *bnapi; 13976 13977 for (i = 0; i < bp->cp_nr_rings; i++) { 13978 bnapi = bp->bnapi[i]; 13979 if (netif_msg_drv(bp)) { 13980 bnxt_dump_tx_sw_state(bnapi); 13981 bnxt_dump_rx_sw_state(bnapi); 13982 bnxt_dump_cp_sw_state(bnapi); 13983 } 13984 } 13985 } 13986 13987 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13988 { 13989 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 13990 struct hwrm_ring_reset_input *req; 13991 struct bnxt_napi *bnapi = rxr->bnapi; 13992 struct bnxt_cp_ring_info *cpr; 13993 u16 cp_ring_id; 13994 int rc; 13995 13996 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 13997 if (rc) 13998 return rc; 13999 14000 cpr = &bnapi->cp_ring; 14001 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14002 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14003 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14004 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14005 return hwrm_req_send_silent(bp, req); 14006 } 14007 14008 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14009 { 14010 if (!silent) 14011 bnxt_dbg_dump_states(bp); 14012 if (netif_running(bp->dev)) { 14013 bnxt_close_nic(bp, !silent, false); 14014 bnxt_open_nic(bp, !silent, false); 14015 } 14016 } 14017 14018 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14019 { 14020 struct bnxt *bp = netdev_priv(dev); 14021 14022 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14023 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14024 } 14025 14026 static void bnxt_fw_health_check(struct bnxt *bp) 14027 { 14028 struct bnxt_fw_health *fw_health = bp->fw_health; 14029 struct pci_dev *pdev = bp->pdev; 14030 u32 val; 14031 14032 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14033 return; 14034 14035 /* Make sure it is enabled before checking the tmr_counter. */ 14036 smp_rmb(); 14037 if (fw_health->tmr_counter) { 14038 fw_health->tmr_counter--; 14039 return; 14040 } 14041 14042 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14043 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14044 fw_health->arrests++; 14045 goto fw_reset; 14046 } 14047 14048 fw_health->last_fw_heartbeat = val; 14049 14050 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14051 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14052 fw_health->discoveries++; 14053 goto fw_reset; 14054 } 14055 14056 fw_health->tmr_counter = fw_health->tmr_multiplier; 14057 return; 14058 14059 fw_reset: 14060 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14061 } 14062 14063 static void bnxt_timer(struct timer_list *t) 14064 { 14065 struct bnxt *bp = timer_container_of(bp, t, timer); 14066 struct net_device *dev = bp->dev; 14067 14068 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14069 return; 14070 14071 if (atomic_read(&bp->intr_sem) != 0) 14072 goto bnxt_restart_timer; 14073 14074 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14075 bnxt_fw_health_check(bp); 14076 14077 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14078 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14079 14080 if (bnxt_tc_flower_enabled(bp)) 14081 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14082 14083 #ifdef CONFIG_RFS_ACCEL 14084 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14085 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14086 #endif /*CONFIG_RFS_ACCEL*/ 14087 14088 if (bp->link_info.phy_retry) { 14089 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14090 bp->link_info.phy_retry = false; 14091 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14092 } else { 14093 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14094 } 14095 } 14096 14097 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 14098 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 14099 14100 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14101 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14102 14103 bnxt_restart_timer: 14104 mod_timer(&bp->timer, jiffies + bp->current_interval); 14105 } 14106 14107 static void bnxt_lock_sp(struct bnxt *bp) 14108 { 14109 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14110 * set. If the device is being closed, bnxt_close() may be holding 14111 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14112 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14113 * instance lock. 14114 */ 14115 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14116 netdev_lock(bp->dev); 14117 } 14118 14119 static void bnxt_unlock_sp(struct bnxt *bp) 14120 { 14121 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14122 netdev_unlock(bp->dev); 14123 } 14124 14125 /* Same as bnxt_lock_sp() with additional rtnl_lock */ 14126 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 14127 { 14128 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14129 rtnl_lock(); 14130 netdev_lock(bp->dev); 14131 } 14132 14133 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 14134 { 14135 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14136 netdev_unlock(bp->dev); 14137 rtnl_unlock(); 14138 } 14139 14140 /* Only called from bnxt_sp_task() */ 14141 static void bnxt_reset(struct bnxt *bp, bool silent) 14142 { 14143 bnxt_rtnl_lock_sp(bp); 14144 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14145 bnxt_reset_task(bp, silent); 14146 bnxt_rtnl_unlock_sp(bp); 14147 } 14148 14149 /* Only called from bnxt_sp_task() */ 14150 static void bnxt_rx_ring_reset(struct bnxt *bp) 14151 { 14152 int i; 14153 14154 bnxt_rtnl_lock_sp(bp); 14155 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14156 bnxt_rtnl_unlock_sp(bp); 14157 return; 14158 } 14159 /* Disable and flush TPA before resetting the RX ring */ 14160 if (bp->flags & BNXT_FLAG_TPA) 14161 bnxt_set_tpa(bp, false); 14162 for (i = 0; i < bp->rx_nr_rings; i++) { 14163 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14164 struct bnxt_cp_ring_info *cpr; 14165 int rc; 14166 14167 if (!rxr->bnapi->in_reset) 14168 continue; 14169 14170 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14171 if (rc) { 14172 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14173 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14174 else 14175 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14176 rc); 14177 bnxt_reset_task(bp, true); 14178 break; 14179 } 14180 bnxt_free_one_rx_ring_skbs(bp, rxr); 14181 rxr->rx_prod = 0; 14182 rxr->rx_agg_prod = 0; 14183 rxr->rx_sw_agg_prod = 0; 14184 rxr->rx_next_cons = 0; 14185 rxr->bnapi->in_reset = false; 14186 bnxt_alloc_one_rx_ring(bp, i); 14187 cpr = &rxr->bnapi->cp_ring; 14188 cpr->sw_stats->rx.rx_resets++; 14189 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14190 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14191 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14192 } 14193 if (bp->flags & BNXT_FLAG_TPA) 14194 bnxt_set_tpa(bp, true); 14195 bnxt_rtnl_unlock_sp(bp); 14196 } 14197 14198 static void bnxt_fw_fatal_close(struct bnxt *bp) 14199 { 14200 bnxt_tx_disable(bp); 14201 bnxt_disable_napi(bp); 14202 bnxt_disable_int_sync(bp); 14203 bnxt_free_irq(bp); 14204 bnxt_clear_int_mode(bp); 14205 pci_disable_device(bp->pdev); 14206 } 14207 14208 static void bnxt_fw_reset_close(struct bnxt *bp) 14209 { 14210 /* When firmware is in fatal state, quiesce device and disable 14211 * bus master to prevent any potential bad DMAs before freeing 14212 * kernel memory. 14213 */ 14214 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14215 u16 val = 0; 14216 14217 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14218 if (val == 0xffff) 14219 bp->fw_reset_min_dsecs = 0; 14220 bnxt_fw_fatal_close(bp); 14221 } 14222 __bnxt_close_nic(bp, true, false); 14223 bnxt_vf_reps_free(bp); 14224 bnxt_clear_int_mode(bp); 14225 bnxt_hwrm_func_drv_unrgtr(bp); 14226 if (pci_is_enabled(bp->pdev)) 14227 pci_disable_device(bp->pdev); 14228 bnxt_free_ctx_mem(bp, false); 14229 } 14230 14231 static bool is_bnxt_fw_ok(struct bnxt *bp) 14232 { 14233 struct bnxt_fw_health *fw_health = bp->fw_health; 14234 bool no_heartbeat = false, has_reset = false; 14235 u32 val; 14236 14237 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14238 if (val == fw_health->last_fw_heartbeat) 14239 no_heartbeat = true; 14240 14241 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14242 if (val != fw_health->last_fw_reset_cnt) 14243 has_reset = true; 14244 14245 if (!no_heartbeat && has_reset) 14246 return true; 14247 14248 return false; 14249 } 14250 14251 /* netdev instance lock is acquired before calling this function */ 14252 static void bnxt_force_fw_reset(struct bnxt *bp) 14253 { 14254 struct bnxt_fw_health *fw_health = bp->fw_health; 14255 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14256 u32 wait_dsecs; 14257 14258 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14259 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14260 return; 14261 14262 /* we have to serialize with bnxt_refclk_read()*/ 14263 if (ptp) { 14264 unsigned long flags; 14265 14266 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14267 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14268 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14269 } else { 14270 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14271 } 14272 bnxt_fw_reset_close(bp); 14273 wait_dsecs = fw_health->master_func_wait_dsecs; 14274 if (fw_health->primary) { 14275 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14276 wait_dsecs = 0; 14277 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14278 } else { 14279 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14280 wait_dsecs = fw_health->normal_func_wait_dsecs; 14281 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14282 } 14283 14284 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14285 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14286 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14287 } 14288 14289 void bnxt_fw_exception(struct bnxt *bp) 14290 { 14291 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14292 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14293 bnxt_ulp_stop(bp); 14294 bnxt_lock_sp(bp); 14295 bnxt_force_fw_reset(bp); 14296 bnxt_unlock_sp(bp); 14297 } 14298 14299 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14300 * < 0 on error. 14301 */ 14302 static int bnxt_get_registered_vfs(struct bnxt *bp) 14303 { 14304 #ifdef CONFIG_BNXT_SRIOV 14305 int rc; 14306 14307 if (!BNXT_PF(bp)) 14308 return 0; 14309 14310 rc = bnxt_hwrm_func_qcfg(bp); 14311 if (rc) { 14312 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14313 return rc; 14314 } 14315 if (bp->pf.registered_vfs) 14316 return bp->pf.registered_vfs; 14317 if (bp->sriov_cfg) 14318 return 1; 14319 #endif 14320 return 0; 14321 } 14322 14323 void bnxt_fw_reset(struct bnxt *bp) 14324 { 14325 bnxt_ulp_stop(bp); 14326 bnxt_lock_sp(bp); 14327 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14328 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14329 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14330 int n = 0, tmo; 14331 14332 /* we have to serialize with bnxt_refclk_read()*/ 14333 if (ptp) { 14334 unsigned long flags; 14335 14336 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14337 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14338 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14339 } else { 14340 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14341 } 14342 if (bp->pf.active_vfs && 14343 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14344 n = bnxt_get_registered_vfs(bp); 14345 if (n < 0) { 14346 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14347 n); 14348 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14349 netif_close(bp->dev); 14350 goto fw_reset_exit; 14351 } else if (n > 0) { 14352 u16 vf_tmo_dsecs = n * 10; 14353 14354 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14355 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14356 bp->fw_reset_state = 14357 BNXT_FW_RESET_STATE_POLL_VF; 14358 bnxt_queue_fw_reset_work(bp, HZ / 10); 14359 goto fw_reset_exit; 14360 } 14361 bnxt_fw_reset_close(bp); 14362 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14363 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14364 tmo = HZ / 10; 14365 } else { 14366 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14367 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14368 } 14369 bnxt_queue_fw_reset_work(bp, tmo); 14370 } 14371 fw_reset_exit: 14372 bnxt_unlock_sp(bp); 14373 } 14374 14375 static void bnxt_chk_missed_irq(struct bnxt *bp) 14376 { 14377 int i; 14378 14379 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14380 return; 14381 14382 for (i = 0; i < bp->cp_nr_rings; i++) { 14383 struct bnxt_napi *bnapi = bp->bnapi[i]; 14384 struct bnxt_cp_ring_info *cpr; 14385 u32 fw_ring_id; 14386 int j; 14387 14388 if (!bnapi) 14389 continue; 14390 14391 cpr = &bnapi->cp_ring; 14392 for (j = 0; j < cpr->cp_ring_count; j++) { 14393 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14394 u32 val[2]; 14395 14396 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14397 continue; 14398 14399 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14400 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14401 continue; 14402 } 14403 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14404 bnxt_dbg_hwrm_ring_info_get(bp, 14405 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14406 fw_ring_id, &val[0], &val[1]); 14407 cpr->sw_stats->cmn.missed_irqs++; 14408 } 14409 } 14410 } 14411 14412 static void bnxt_cfg_ntp_filters(struct bnxt *); 14413 14414 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14415 { 14416 struct bnxt_link_info *link_info = &bp->link_info; 14417 14418 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14419 link_info->autoneg = BNXT_AUTONEG_SPEED; 14420 if (bp->hwrm_spec_code >= 0x10201) { 14421 if (link_info->auto_pause_setting & 14422 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14423 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14424 } else { 14425 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14426 } 14427 bnxt_set_auto_speed(link_info); 14428 } else { 14429 bnxt_set_force_speed(link_info); 14430 link_info->req_duplex = link_info->duplex_setting; 14431 } 14432 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14433 link_info->req_flow_ctrl = 14434 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14435 else 14436 link_info->req_flow_ctrl = link_info->force_pause_setting; 14437 } 14438 14439 static void bnxt_fw_echo_reply(struct bnxt *bp) 14440 { 14441 struct bnxt_fw_health *fw_health = bp->fw_health; 14442 struct hwrm_func_echo_response_input *req; 14443 int rc; 14444 14445 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14446 if (rc) 14447 return; 14448 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14449 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14450 hwrm_req_send(bp, req); 14451 } 14452 14453 static void bnxt_ulp_restart(struct bnxt *bp) 14454 { 14455 bnxt_ulp_stop(bp); 14456 bnxt_ulp_start(bp, 0); 14457 } 14458 14459 static void bnxt_sp_task(struct work_struct *work) 14460 { 14461 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14462 14463 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14464 smp_mb__after_atomic(); 14465 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14466 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14467 return; 14468 } 14469 14470 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14471 bnxt_ulp_restart(bp); 14472 bnxt_reenable_sriov(bp); 14473 } 14474 14475 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14476 bnxt_cfg_rx_mode(bp); 14477 14478 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14479 bnxt_cfg_ntp_filters(bp); 14480 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14481 bnxt_hwrm_exec_fwd_req(bp); 14482 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14483 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14484 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14485 bnxt_hwrm_port_qstats(bp, 0); 14486 bnxt_hwrm_port_qstats_ext(bp, 0); 14487 bnxt_accumulate_all_stats(bp); 14488 } 14489 14490 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14491 int rc; 14492 14493 mutex_lock(&bp->link_lock); 14494 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14495 &bp->sp_event)) 14496 bnxt_hwrm_phy_qcaps(bp); 14497 14498 rc = bnxt_update_link(bp, true); 14499 if (rc) 14500 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14501 rc); 14502 14503 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14504 &bp->sp_event)) 14505 bnxt_init_ethtool_link_settings(bp); 14506 mutex_unlock(&bp->link_lock); 14507 } 14508 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14509 int rc; 14510 14511 mutex_lock(&bp->link_lock); 14512 rc = bnxt_update_phy_setting(bp); 14513 mutex_unlock(&bp->link_lock); 14514 if (rc) { 14515 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14516 } else { 14517 bp->link_info.phy_retry = false; 14518 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14519 } 14520 } 14521 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14522 mutex_lock(&bp->link_lock); 14523 bnxt_get_port_module_status(bp); 14524 mutex_unlock(&bp->link_lock); 14525 } 14526 14527 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14528 bnxt_tc_flow_stats_work(bp); 14529 14530 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14531 bnxt_chk_missed_irq(bp); 14532 14533 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14534 bnxt_fw_echo_reply(bp); 14535 14536 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14537 bnxt_hwmon_notify_event(bp); 14538 14539 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14540 * must be the last functions to be called before exiting. 14541 */ 14542 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14543 bnxt_reset(bp, false); 14544 14545 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14546 bnxt_reset(bp, true); 14547 14548 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14549 bnxt_rx_ring_reset(bp); 14550 14551 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14552 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14553 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14554 bnxt_devlink_health_fw_report(bp); 14555 else 14556 bnxt_fw_reset(bp); 14557 } 14558 14559 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14560 if (!is_bnxt_fw_ok(bp)) 14561 bnxt_devlink_health_fw_report(bp); 14562 } 14563 14564 smp_mb__before_atomic(); 14565 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14566 } 14567 14568 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14569 int *max_cp); 14570 14571 /* Under netdev instance lock */ 14572 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14573 int tx_xdp) 14574 { 14575 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14576 struct bnxt_hw_rings hwr = {0}; 14577 int rx_rings = rx; 14578 int rc; 14579 14580 if (tcs) 14581 tx_sets = tcs; 14582 14583 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14584 14585 if (max_rx < rx_rings) 14586 return -ENOMEM; 14587 14588 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14589 rx_rings <<= 1; 14590 14591 hwr.rx = rx_rings; 14592 hwr.tx = tx * tx_sets + tx_xdp; 14593 if (max_tx < hwr.tx) 14594 return -ENOMEM; 14595 14596 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14597 14598 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14599 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14600 if (max_cp < hwr.cp) 14601 return -ENOMEM; 14602 hwr.stat = hwr.cp; 14603 if (BNXT_NEW_RM(bp)) { 14604 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14605 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14606 hwr.grp = rx; 14607 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14608 } 14609 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14610 hwr.cp_p5 = hwr.tx + rx; 14611 rc = bnxt_hwrm_check_rings(bp, &hwr); 14612 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14613 if (!bnxt_ulp_registered(bp->edev)) { 14614 hwr.cp += bnxt_get_ulp_msix_num(bp); 14615 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14616 } 14617 if (hwr.cp > bp->total_irqs) { 14618 int total_msix = bnxt_change_msix(bp, hwr.cp); 14619 14620 if (total_msix < hwr.cp) { 14621 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14622 hwr.cp, total_msix); 14623 rc = -ENOSPC; 14624 } 14625 } 14626 } 14627 return rc; 14628 } 14629 14630 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14631 { 14632 if (bp->bar2) { 14633 pci_iounmap(pdev, bp->bar2); 14634 bp->bar2 = NULL; 14635 } 14636 14637 if (bp->bar1) { 14638 pci_iounmap(pdev, bp->bar1); 14639 bp->bar1 = NULL; 14640 } 14641 14642 if (bp->bar0) { 14643 pci_iounmap(pdev, bp->bar0); 14644 bp->bar0 = NULL; 14645 } 14646 } 14647 14648 static void bnxt_cleanup_pci(struct bnxt *bp) 14649 { 14650 bnxt_unmap_bars(bp, bp->pdev); 14651 pci_release_regions(bp->pdev); 14652 if (pci_is_enabled(bp->pdev)) 14653 pci_disable_device(bp->pdev); 14654 } 14655 14656 static void bnxt_init_dflt_coal(struct bnxt *bp) 14657 { 14658 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14659 struct bnxt_coal *coal; 14660 u16 flags = 0; 14661 14662 if (coal_cap->cmpl_params & 14663 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14664 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14665 14666 /* Tick values in micro seconds. 14667 * 1 coal_buf x bufs_per_record = 1 completion record. 14668 */ 14669 coal = &bp->rx_coal; 14670 coal->coal_ticks = 10; 14671 coal->coal_bufs = 30; 14672 coal->coal_ticks_irq = 1; 14673 coal->coal_bufs_irq = 2; 14674 coal->idle_thresh = 50; 14675 coal->bufs_per_record = 2; 14676 coal->budget = 64; /* NAPI budget */ 14677 coal->flags = flags; 14678 14679 coal = &bp->tx_coal; 14680 coal->coal_ticks = 28; 14681 coal->coal_bufs = 30; 14682 coal->coal_ticks_irq = 2; 14683 coal->coal_bufs_irq = 2; 14684 coal->bufs_per_record = 1; 14685 coal->flags = flags; 14686 14687 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14688 } 14689 14690 /* FW that pre-reserves 1 VNIC per function */ 14691 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14692 { 14693 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14694 14695 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14696 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14697 return true; 14698 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14699 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14700 return true; 14701 return false; 14702 } 14703 14704 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14705 { 14706 int rc; 14707 14708 bp->fw_cap = 0; 14709 rc = bnxt_hwrm_ver_get(bp); 14710 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14711 * so wait before continuing with recovery. 14712 */ 14713 if (rc) 14714 msleep(100); 14715 bnxt_try_map_fw_health_reg(bp); 14716 if (rc) { 14717 rc = bnxt_try_recover_fw(bp); 14718 if (rc) 14719 return rc; 14720 rc = bnxt_hwrm_ver_get(bp); 14721 if (rc) 14722 return rc; 14723 } 14724 14725 bnxt_nvm_cfg_ver_get(bp); 14726 14727 rc = bnxt_hwrm_func_reset(bp); 14728 if (rc) 14729 return -ENODEV; 14730 14731 bnxt_hwrm_fw_set_time(bp); 14732 return 0; 14733 } 14734 14735 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14736 { 14737 int rc; 14738 14739 /* Get the MAX capabilities for this function */ 14740 rc = bnxt_hwrm_func_qcaps(bp); 14741 if (rc) { 14742 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14743 rc); 14744 return -ENODEV; 14745 } 14746 14747 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14748 if (rc) 14749 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14750 rc); 14751 14752 if (bnxt_alloc_fw_health(bp)) { 14753 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14754 } else { 14755 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14756 if (rc) 14757 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14758 rc); 14759 } 14760 14761 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14762 if (rc) 14763 return -ENODEV; 14764 14765 rc = bnxt_alloc_crash_dump_mem(bp); 14766 if (rc) 14767 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14768 rc); 14769 if (!rc) { 14770 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14771 if (rc) { 14772 bnxt_free_crash_dump_mem(bp); 14773 netdev_warn(bp->dev, 14774 "hwrm crash dump mem failure rc: %d\n", rc); 14775 } 14776 } 14777 14778 if (bnxt_fw_pre_resv_vnics(bp)) 14779 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14780 14781 bnxt_hwrm_func_qcfg(bp); 14782 bnxt_hwrm_vnic_qcaps(bp); 14783 bnxt_hwrm_port_led_qcaps(bp); 14784 bnxt_ethtool_init(bp); 14785 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14786 __bnxt_hwrm_ptp_qcfg(bp); 14787 bnxt_dcb_init(bp); 14788 bnxt_hwmon_init(bp); 14789 return 0; 14790 } 14791 14792 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14793 { 14794 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14795 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14796 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14797 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14798 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14799 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14800 bp->rss_hash_delta = bp->rss_hash_cfg; 14801 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14802 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14803 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14804 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14805 } 14806 } 14807 14808 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14809 { 14810 struct net_device *dev = bp->dev; 14811 14812 dev->hw_features &= ~NETIF_F_NTUPLE; 14813 dev->features &= ~NETIF_F_NTUPLE; 14814 bp->flags &= ~BNXT_FLAG_RFS; 14815 if (bnxt_rfs_supported(bp)) { 14816 dev->hw_features |= NETIF_F_NTUPLE; 14817 if (bnxt_rfs_capable(bp, false)) { 14818 bp->flags |= BNXT_FLAG_RFS; 14819 dev->features |= NETIF_F_NTUPLE; 14820 } 14821 } 14822 } 14823 14824 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14825 { 14826 struct pci_dev *pdev = bp->pdev; 14827 14828 bnxt_set_dflt_rss_hash_type(bp); 14829 bnxt_set_dflt_rfs(bp); 14830 14831 bnxt_get_wol_settings(bp); 14832 if (bp->flags & BNXT_FLAG_WOL_CAP) 14833 device_set_wakeup_enable(&pdev->dev, bp->wol); 14834 else 14835 device_set_wakeup_capable(&pdev->dev, false); 14836 14837 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14838 bnxt_hwrm_coal_params_qcaps(bp); 14839 } 14840 14841 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14842 14843 int bnxt_fw_init_one(struct bnxt *bp) 14844 { 14845 int rc; 14846 14847 rc = bnxt_fw_init_one_p1(bp); 14848 if (rc) { 14849 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14850 return rc; 14851 } 14852 rc = bnxt_fw_init_one_p2(bp); 14853 if (rc) { 14854 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14855 return rc; 14856 } 14857 rc = bnxt_probe_phy(bp, false); 14858 if (rc) 14859 return rc; 14860 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14861 if (rc) 14862 return rc; 14863 14864 bnxt_fw_init_one_p3(bp); 14865 return 0; 14866 } 14867 14868 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14869 { 14870 struct bnxt_fw_health *fw_health = bp->fw_health; 14871 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14872 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14873 u32 reg_type, reg_off, delay_msecs; 14874 14875 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14876 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14877 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14878 switch (reg_type) { 14879 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14880 pci_write_config_dword(bp->pdev, reg_off, val); 14881 break; 14882 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14883 writel(reg_off & BNXT_GRC_BASE_MASK, 14884 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14885 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14886 fallthrough; 14887 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14888 writel(val, bp->bar0 + reg_off); 14889 break; 14890 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14891 writel(val, bp->bar1 + reg_off); 14892 break; 14893 } 14894 if (delay_msecs) { 14895 pci_read_config_dword(bp->pdev, 0, &val); 14896 msleep(delay_msecs); 14897 } 14898 } 14899 14900 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14901 { 14902 struct hwrm_func_qcfg_output *resp; 14903 struct hwrm_func_qcfg_input *req; 14904 bool result = true; /* firmware will enforce if unknown */ 14905 14906 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14907 return result; 14908 14909 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14910 return result; 14911 14912 req->fid = cpu_to_le16(0xffff); 14913 resp = hwrm_req_hold(bp, req); 14914 if (!hwrm_req_send(bp, req)) 14915 result = !!(le16_to_cpu(resp->flags) & 14916 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14917 hwrm_req_drop(bp, req); 14918 return result; 14919 } 14920 14921 static void bnxt_reset_all(struct bnxt *bp) 14922 { 14923 struct bnxt_fw_health *fw_health = bp->fw_health; 14924 int i, rc; 14925 14926 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14927 bnxt_fw_reset_via_optee(bp); 14928 bp->fw_reset_timestamp = jiffies; 14929 return; 14930 } 14931 14932 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14933 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14934 bnxt_fw_reset_writel(bp, i); 14935 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14936 struct hwrm_fw_reset_input *req; 14937 14938 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14939 if (!rc) { 14940 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14941 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14942 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14943 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14944 rc = hwrm_req_send(bp, req); 14945 } 14946 if (rc != -ENODEV) 14947 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14948 } 14949 bp->fw_reset_timestamp = jiffies; 14950 } 14951 14952 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14953 { 14954 return time_after(jiffies, bp->fw_reset_timestamp + 14955 (bp->fw_reset_max_dsecs * HZ / 10)); 14956 } 14957 14958 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14959 { 14960 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14961 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14962 bnxt_dl_health_fw_status_update(bp, false); 14963 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 14964 netif_close(bp->dev); 14965 } 14966 14967 static void bnxt_fw_reset_task(struct work_struct *work) 14968 { 14969 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14970 int rc = 0; 14971 14972 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14973 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14974 return; 14975 } 14976 14977 switch (bp->fw_reset_state) { 14978 case BNXT_FW_RESET_STATE_POLL_VF: { 14979 int n = bnxt_get_registered_vfs(bp); 14980 int tmo; 14981 14982 if (n < 0) { 14983 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14984 n, jiffies_to_msecs(jiffies - 14985 bp->fw_reset_timestamp)); 14986 goto fw_reset_abort; 14987 } else if (n > 0) { 14988 if (bnxt_fw_reset_timeout(bp)) { 14989 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14990 bp->fw_reset_state = 0; 14991 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14992 n); 14993 goto ulp_start; 14994 } 14995 bnxt_queue_fw_reset_work(bp, HZ / 10); 14996 return; 14997 } 14998 bp->fw_reset_timestamp = jiffies; 14999 netdev_lock(bp->dev); 15000 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 15001 bnxt_fw_reset_abort(bp, rc); 15002 netdev_unlock(bp->dev); 15003 goto ulp_start; 15004 } 15005 bnxt_fw_reset_close(bp); 15006 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15007 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15008 tmo = HZ / 10; 15009 } else { 15010 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15011 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15012 } 15013 netdev_unlock(bp->dev); 15014 bnxt_queue_fw_reset_work(bp, tmo); 15015 return; 15016 } 15017 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15018 u32 val; 15019 15020 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15021 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15022 !bnxt_fw_reset_timeout(bp)) { 15023 bnxt_queue_fw_reset_work(bp, HZ / 5); 15024 return; 15025 } 15026 15027 if (!bp->fw_health->primary) { 15028 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15029 15030 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15031 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15032 return; 15033 } 15034 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15035 } 15036 fallthrough; 15037 case BNXT_FW_RESET_STATE_RESET_FW: 15038 bnxt_reset_all(bp); 15039 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15040 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15041 return; 15042 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15043 bnxt_inv_fw_health_reg(bp); 15044 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15045 !bp->fw_reset_min_dsecs) { 15046 u16 val; 15047 15048 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15049 if (val == 0xffff) { 15050 if (bnxt_fw_reset_timeout(bp)) { 15051 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15052 rc = -ETIMEDOUT; 15053 goto fw_reset_abort; 15054 } 15055 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15056 return; 15057 } 15058 } 15059 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15060 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15061 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15062 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15063 bnxt_dl_remote_reload(bp); 15064 if (pci_enable_device(bp->pdev)) { 15065 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15066 rc = -ENODEV; 15067 goto fw_reset_abort; 15068 } 15069 pci_set_master(bp->pdev); 15070 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15071 fallthrough; 15072 case BNXT_FW_RESET_STATE_POLL_FW: 15073 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15074 rc = bnxt_hwrm_poll(bp); 15075 if (rc) { 15076 if (bnxt_fw_reset_timeout(bp)) { 15077 netdev_err(bp->dev, "Firmware reset aborted\n"); 15078 goto fw_reset_abort_status; 15079 } 15080 bnxt_queue_fw_reset_work(bp, HZ / 5); 15081 return; 15082 } 15083 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15084 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15085 fallthrough; 15086 case BNXT_FW_RESET_STATE_OPENING: 15087 while (!rtnl_trylock()) { 15088 bnxt_queue_fw_reset_work(bp, HZ / 10); 15089 return; 15090 } 15091 netdev_lock(bp->dev); 15092 rc = bnxt_open(bp->dev); 15093 if (rc) { 15094 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15095 bnxt_fw_reset_abort(bp, rc); 15096 netdev_unlock(bp->dev); 15097 rtnl_unlock(); 15098 goto ulp_start; 15099 } 15100 15101 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15102 bp->fw_health->enabled) { 15103 bp->fw_health->last_fw_reset_cnt = 15104 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15105 } 15106 bp->fw_reset_state = 0; 15107 /* Make sure fw_reset_state is 0 before clearing the flag */ 15108 smp_mb__before_atomic(); 15109 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15110 bnxt_ptp_reapply_pps(bp); 15111 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15112 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15113 bnxt_dl_health_fw_recovery_done(bp); 15114 bnxt_dl_health_fw_status_update(bp, true); 15115 } 15116 netdev_unlock(bp->dev); 15117 rtnl_unlock(); 15118 bnxt_ulp_start(bp, 0); 15119 bnxt_reenable_sriov(bp); 15120 netdev_lock(bp->dev); 15121 bnxt_vf_reps_alloc(bp); 15122 bnxt_vf_reps_open(bp); 15123 netdev_unlock(bp->dev); 15124 break; 15125 } 15126 return; 15127 15128 fw_reset_abort_status: 15129 if (bp->fw_health->status_reliable || 15130 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15131 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15132 15133 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15134 } 15135 fw_reset_abort: 15136 netdev_lock(bp->dev); 15137 bnxt_fw_reset_abort(bp, rc); 15138 netdev_unlock(bp->dev); 15139 ulp_start: 15140 bnxt_ulp_start(bp, rc); 15141 } 15142 15143 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15144 { 15145 int rc; 15146 struct bnxt *bp = netdev_priv(dev); 15147 15148 SET_NETDEV_DEV(dev, &pdev->dev); 15149 15150 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15151 rc = pci_enable_device(pdev); 15152 if (rc) { 15153 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15154 goto init_err; 15155 } 15156 15157 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15158 dev_err(&pdev->dev, 15159 "Cannot find PCI device base address, aborting\n"); 15160 rc = -ENODEV; 15161 goto init_err_disable; 15162 } 15163 15164 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15165 if (rc) { 15166 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15167 goto init_err_disable; 15168 } 15169 15170 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15171 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15172 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15173 rc = -EIO; 15174 goto init_err_release; 15175 } 15176 15177 pci_set_master(pdev); 15178 15179 bp->dev = dev; 15180 bp->pdev = pdev; 15181 15182 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15183 * determines the BAR size. 15184 */ 15185 bp->bar0 = pci_ioremap_bar(pdev, 0); 15186 if (!bp->bar0) { 15187 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15188 rc = -ENOMEM; 15189 goto init_err_release; 15190 } 15191 15192 bp->bar2 = pci_ioremap_bar(pdev, 4); 15193 if (!bp->bar2) { 15194 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15195 rc = -ENOMEM; 15196 goto init_err_release; 15197 } 15198 15199 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15200 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15201 15202 spin_lock_init(&bp->ntp_fltr_lock); 15203 #if BITS_PER_LONG == 32 15204 spin_lock_init(&bp->db_lock); 15205 #endif 15206 15207 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15208 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15209 15210 timer_setup(&bp->timer, bnxt_timer, 0); 15211 bp->current_interval = BNXT_TIMER_INTERVAL; 15212 15213 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15214 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15215 15216 clear_bit(BNXT_STATE_OPEN, &bp->state); 15217 return 0; 15218 15219 init_err_release: 15220 bnxt_unmap_bars(bp, pdev); 15221 pci_release_regions(pdev); 15222 15223 init_err_disable: 15224 pci_disable_device(pdev); 15225 15226 init_err: 15227 return rc; 15228 } 15229 15230 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15231 { 15232 struct sockaddr *addr = p; 15233 struct bnxt *bp = netdev_priv(dev); 15234 int rc = 0; 15235 15236 netdev_assert_locked(dev); 15237 15238 if (!is_valid_ether_addr(addr->sa_data)) 15239 return -EADDRNOTAVAIL; 15240 15241 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15242 return 0; 15243 15244 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15245 if (rc) 15246 return rc; 15247 15248 eth_hw_addr_set(dev, addr->sa_data); 15249 bnxt_clear_usr_fltrs(bp, true); 15250 if (netif_running(dev)) { 15251 bnxt_close_nic(bp, false, false); 15252 rc = bnxt_open_nic(bp, false, false); 15253 } 15254 15255 return rc; 15256 } 15257 15258 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15259 { 15260 struct bnxt *bp = netdev_priv(dev); 15261 15262 netdev_assert_locked(dev); 15263 15264 if (netif_running(dev)) 15265 bnxt_close_nic(bp, true, false); 15266 15267 WRITE_ONCE(dev->mtu, new_mtu); 15268 15269 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15270 * program is attached. We need to set the AGG rings settings and 15271 * rx_skb_func accordingly. 15272 */ 15273 if (READ_ONCE(bp->xdp_prog)) 15274 bnxt_set_rx_skb_mode(bp, true); 15275 15276 bnxt_set_ring_params(bp); 15277 15278 if (netif_running(dev)) 15279 return bnxt_open_nic(bp, true, false); 15280 15281 return 0; 15282 } 15283 15284 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15285 { 15286 struct bnxt *bp = netdev_priv(dev); 15287 bool sh = false; 15288 int rc, tx_cp; 15289 15290 if (tc > bp->max_tc) { 15291 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15292 tc, bp->max_tc); 15293 return -EINVAL; 15294 } 15295 15296 if (bp->num_tc == tc) 15297 return 0; 15298 15299 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15300 sh = true; 15301 15302 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15303 sh, tc, bp->tx_nr_rings_xdp); 15304 if (rc) 15305 return rc; 15306 15307 /* Needs to close the device and do hw resource re-allocations */ 15308 if (netif_running(bp->dev)) 15309 bnxt_close_nic(bp, true, false); 15310 15311 if (tc) { 15312 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15313 netdev_set_num_tc(dev, tc); 15314 bp->num_tc = tc; 15315 } else { 15316 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15317 netdev_reset_tc(dev); 15318 bp->num_tc = 0; 15319 } 15320 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15321 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15322 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15323 tx_cp + bp->rx_nr_rings; 15324 15325 if (netif_running(bp->dev)) 15326 return bnxt_open_nic(bp, true, false); 15327 15328 return 0; 15329 } 15330 15331 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15332 void *cb_priv) 15333 { 15334 struct bnxt *bp = cb_priv; 15335 15336 if (!bnxt_tc_flower_enabled(bp) || 15337 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15338 return -EOPNOTSUPP; 15339 15340 switch (type) { 15341 case TC_SETUP_CLSFLOWER: 15342 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15343 default: 15344 return -EOPNOTSUPP; 15345 } 15346 } 15347 15348 LIST_HEAD(bnxt_block_cb_list); 15349 15350 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15351 void *type_data) 15352 { 15353 struct bnxt *bp = netdev_priv(dev); 15354 15355 switch (type) { 15356 case TC_SETUP_BLOCK: 15357 return flow_block_cb_setup_simple(type_data, 15358 &bnxt_block_cb_list, 15359 bnxt_setup_tc_block_cb, 15360 bp, bp, true); 15361 case TC_SETUP_QDISC_MQPRIO: { 15362 struct tc_mqprio_qopt *mqprio = type_data; 15363 15364 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15365 15366 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15367 } 15368 default: 15369 return -EOPNOTSUPP; 15370 } 15371 } 15372 15373 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15374 const struct sk_buff *skb) 15375 { 15376 struct bnxt_vnic_info *vnic; 15377 15378 if (skb) 15379 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15380 15381 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15382 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15383 } 15384 15385 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15386 u32 idx) 15387 { 15388 struct hlist_head *head; 15389 int bit_id; 15390 15391 spin_lock_bh(&bp->ntp_fltr_lock); 15392 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15393 if (bit_id < 0) { 15394 spin_unlock_bh(&bp->ntp_fltr_lock); 15395 return -ENOMEM; 15396 } 15397 15398 fltr->base.sw_id = (u16)bit_id; 15399 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15400 fltr->base.flags |= BNXT_ACT_RING_DST; 15401 head = &bp->ntp_fltr_hash_tbl[idx]; 15402 hlist_add_head_rcu(&fltr->base.hash, head); 15403 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15404 bnxt_insert_usr_fltr(bp, &fltr->base); 15405 bp->ntp_fltr_count++; 15406 spin_unlock_bh(&bp->ntp_fltr_lock); 15407 return 0; 15408 } 15409 15410 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15411 struct bnxt_ntuple_filter *f2) 15412 { 15413 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15414 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15415 struct flow_keys *keys1 = &f1->fkeys; 15416 struct flow_keys *keys2 = &f2->fkeys; 15417 15418 if (keys1->basic.n_proto != keys2->basic.n_proto || 15419 keys1->basic.ip_proto != keys2->basic.ip_proto) 15420 return false; 15421 15422 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15423 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15424 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15425 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15426 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15427 return false; 15428 } else { 15429 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15430 &keys2->addrs.v6addrs.src) || 15431 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15432 &masks2->addrs.v6addrs.src) || 15433 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15434 &keys2->addrs.v6addrs.dst) || 15435 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15436 &masks2->addrs.v6addrs.dst)) 15437 return false; 15438 } 15439 15440 return keys1->ports.src == keys2->ports.src && 15441 masks1->ports.src == masks2->ports.src && 15442 keys1->ports.dst == keys2->ports.dst && 15443 masks1->ports.dst == masks2->ports.dst && 15444 keys1->control.flags == keys2->control.flags && 15445 f1->l2_fltr == f2->l2_fltr; 15446 } 15447 15448 struct bnxt_ntuple_filter * 15449 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15450 struct bnxt_ntuple_filter *fltr, u32 idx) 15451 { 15452 struct bnxt_ntuple_filter *f; 15453 struct hlist_head *head; 15454 15455 head = &bp->ntp_fltr_hash_tbl[idx]; 15456 hlist_for_each_entry_rcu(f, head, base.hash) { 15457 if (bnxt_fltr_match(f, fltr)) 15458 return f; 15459 } 15460 return NULL; 15461 } 15462 15463 #ifdef CONFIG_RFS_ACCEL 15464 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15465 u16 rxq_index, u32 flow_id) 15466 { 15467 struct bnxt *bp = netdev_priv(dev); 15468 struct bnxt_ntuple_filter *fltr, *new_fltr; 15469 struct flow_keys *fkeys; 15470 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15471 struct bnxt_l2_filter *l2_fltr; 15472 int rc = 0, idx; 15473 u32 flags; 15474 15475 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15476 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15477 atomic_inc(&l2_fltr->refcnt); 15478 } else { 15479 struct bnxt_l2_key key; 15480 15481 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15482 key.vlan = 0; 15483 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15484 if (!l2_fltr) 15485 return -EINVAL; 15486 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15487 bnxt_del_l2_filter(bp, l2_fltr); 15488 return -EINVAL; 15489 } 15490 } 15491 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15492 if (!new_fltr) { 15493 bnxt_del_l2_filter(bp, l2_fltr); 15494 return -ENOMEM; 15495 } 15496 15497 fkeys = &new_fltr->fkeys; 15498 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15499 rc = -EPROTONOSUPPORT; 15500 goto err_free; 15501 } 15502 15503 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15504 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15505 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15506 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15507 rc = -EPROTONOSUPPORT; 15508 goto err_free; 15509 } 15510 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15511 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15512 if (bp->hwrm_spec_code < 0x10601) { 15513 rc = -EPROTONOSUPPORT; 15514 goto err_free; 15515 } 15516 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15517 } 15518 flags = fkeys->control.flags; 15519 if (((flags & FLOW_DIS_ENCAPSULATION) && 15520 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15521 rc = -EPROTONOSUPPORT; 15522 goto err_free; 15523 } 15524 new_fltr->l2_fltr = l2_fltr; 15525 15526 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15527 rcu_read_lock(); 15528 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15529 if (fltr) { 15530 rc = fltr->base.sw_id; 15531 rcu_read_unlock(); 15532 goto err_free; 15533 } 15534 rcu_read_unlock(); 15535 15536 new_fltr->flow_id = flow_id; 15537 new_fltr->base.rxq = rxq_index; 15538 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15539 if (!rc) { 15540 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15541 return new_fltr->base.sw_id; 15542 } 15543 15544 err_free: 15545 bnxt_del_l2_filter(bp, l2_fltr); 15546 kfree(new_fltr); 15547 return rc; 15548 } 15549 #endif 15550 15551 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15552 { 15553 spin_lock_bh(&bp->ntp_fltr_lock); 15554 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15555 spin_unlock_bh(&bp->ntp_fltr_lock); 15556 return; 15557 } 15558 hlist_del_rcu(&fltr->base.hash); 15559 bnxt_del_one_usr_fltr(bp, &fltr->base); 15560 bp->ntp_fltr_count--; 15561 spin_unlock_bh(&bp->ntp_fltr_lock); 15562 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15563 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15564 kfree_rcu(fltr, base.rcu); 15565 } 15566 15567 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15568 { 15569 #ifdef CONFIG_RFS_ACCEL 15570 int i; 15571 15572 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15573 struct hlist_head *head; 15574 struct hlist_node *tmp; 15575 struct bnxt_ntuple_filter *fltr; 15576 int rc; 15577 15578 head = &bp->ntp_fltr_hash_tbl[i]; 15579 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15580 bool del = false; 15581 15582 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15583 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15584 continue; 15585 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15586 fltr->flow_id, 15587 fltr->base.sw_id)) { 15588 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15589 fltr); 15590 del = true; 15591 } 15592 } else { 15593 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15594 fltr); 15595 if (rc) 15596 del = true; 15597 else 15598 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15599 } 15600 15601 if (del) 15602 bnxt_del_ntp_filter(bp, fltr); 15603 } 15604 } 15605 #endif 15606 } 15607 15608 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15609 unsigned int entry, struct udp_tunnel_info *ti) 15610 { 15611 struct bnxt *bp = netdev_priv(netdev); 15612 unsigned int cmd; 15613 15614 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15615 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15616 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15617 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15618 else 15619 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15620 15621 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15622 } 15623 15624 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15625 unsigned int entry, struct udp_tunnel_info *ti) 15626 { 15627 struct bnxt *bp = netdev_priv(netdev); 15628 unsigned int cmd; 15629 15630 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15631 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15632 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15633 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15634 else 15635 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15636 15637 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15638 } 15639 15640 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15641 .set_port = bnxt_udp_tunnel_set_port, 15642 .unset_port = bnxt_udp_tunnel_unset_port, 15643 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15644 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15645 .tables = { 15646 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15647 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15648 }, 15649 }, bnxt_udp_tunnels_p7 = { 15650 .set_port = bnxt_udp_tunnel_set_port, 15651 .unset_port = bnxt_udp_tunnel_unset_port, 15652 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15653 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15654 .tables = { 15655 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15656 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15657 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15658 }, 15659 }; 15660 15661 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15662 struct net_device *dev, u32 filter_mask, 15663 int nlflags) 15664 { 15665 struct bnxt *bp = netdev_priv(dev); 15666 15667 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15668 nlflags, filter_mask, NULL); 15669 } 15670 15671 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15672 u16 flags, struct netlink_ext_ack *extack) 15673 { 15674 struct bnxt *bp = netdev_priv(dev); 15675 struct nlattr *attr, *br_spec; 15676 int rem, rc = 0; 15677 15678 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15679 return -EOPNOTSUPP; 15680 15681 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15682 if (!br_spec) 15683 return -EINVAL; 15684 15685 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15686 u16 mode; 15687 15688 mode = nla_get_u16(attr); 15689 if (mode == bp->br_mode) 15690 break; 15691 15692 rc = bnxt_hwrm_set_br_mode(bp, mode); 15693 if (!rc) 15694 bp->br_mode = mode; 15695 break; 15696 } 15697 return rc; 15698 } 15699 15700 int bnxt_get_port_parent_id(struct net_device *dev, 15701 struct netdev_phys_item_id *ppid) 15702 { 15703 struct bnxt *bp = netdev_priv(dev); 15704 15705 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15706 return -EOPNOTSUPP; 15707 15708 /* The PF and it's VF-reps only support the switchdev framework */ 15709 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15710 return -EOPNOTSUPP; 15711 15712 ppid->id_len = sizeof(bp->dsn); 15713 memcpy(ppid->id, bp->dsn, ppid->id_len); 15714 15715 return 0; 15716 } 15717 15718 static const struct net_device_ops bnxt_netdev_ops = { 15719 .ndo_open = bnxt_open, 15720 .ndo_start_xmit = bnxt_start_xmit, 15721 .ndo_stop = bnxt_close, 15722 .ndo_get_stats64 = bnxt_get_stats64, 15723 .ndo_set_rx_mode = bnxt_set_rx_mode, 15724 .ndo_eth_ioctl = bnxt_ioctl, 15725 .ndo_validate_addr = eth_validate_addr, 15726 .ndo_set_mac_address = bnxt_change_mac_addr, 15727 .ndo_change_mtu = bnxt_change_mtu, 15728 .ndo_fix_features = bnxt_fix_features, 15729 .ndo_set_features = bnxt_set_features, 15730 .ndo_features_check = bnxt_features_check, 15731 .ndo_tx_timeout = bnxt_tx_timeout, 15732 #ifdef CONFIG_BNXT_SRIOV 15733 .ndo_get_vf_config = bnxt_get_vf_config, 15734 .ndo_set_vf_mac = bnxt_set_vf_mac, 15735 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15736 .ndo_set_vf_rate = bnxt_set_vf_bw, 15737 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15738 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15739 .ndo_set_vf_trust = bnxt_set_vf_trust, 15740 #endif 15741 .ndo_setup_tc = bnxt_setup_tc, 15742 #ifdef CONFIG_RFS_ACCEL 15743 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15744 #endif 15745 .ndo_bpf = bnxt_xdp, 15746 .ndo_xdp_xmit = bnxt_xdp_xmit, 15747 .ndo_bridge_getlink = bnxt_bridge_getlink, 15748 .ndo_bridge_setlink = bnxt_bridge_setlink, 15749 }; 15750 15751 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15752 struct netdev_queue_stats_rx *stats) 15753 { 15754 struct bnxt *bp = netdev_priv(dev); 15755 struct bnxt_cp_ring_info *cpr; 15756 u64 *sw; 15757 15758 if (!bp->bnapi) 15759 return; 15760 15761 cpr = &bp->bnapi[i]->cp_ring; 15762 sw = cpr->stats.sw_stats; 15763 15764 stats->packets = 0; 15765 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15766 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15767 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15768 15769 stats->bytes = 0; 15770 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15771 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15772 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15773 15774 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15775 } 15776 15777 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15778 struct netdev_queue_stats_tx *stats) 15779 { 15780 struct bnxt *bp = netdev_priv(dev); 15781 struct bnxt_napi *bnapi; 15782 u64 *sw; 15783 15784 if (!bp->tx_ring) 15785 return; 15786 15787 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15788 sw = bnapi->cp_ring.stats.sw_stats; 15789 15790 stats->packets = 0; 15791 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15792 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15793 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15794 15795 stats->bytes = 0; 15796 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15797 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15798 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15799 } 15800 15801 static void bnxt_get_base_stats(struct net_device *dev, 15802 struct netdev_queue_stats_rx *rx, 15803 struct netdev_queue_stats_tx *tx) 15804 { 15805 struct bnxt *bp = netdev_priv(dev); 15806 15807 rx->packets = bp->net_stats_prev.rx_packets; 15808 rx->bytes = bp->net_stats_prev.rx_bytes; 15809 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15810 15811 tx->packets = bp->net_stats_prev.tx_packets; 15812 tx->bytes = bp->net_stats_prev.tx_bytes; 15813 } 15814 15815 static const struct netdev_stat_ops bnxt_stat_ops = { 15816 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15817 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15818 .get_base_stats = bnxt_get_base_stats, 15819 }; 15820 15821 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15822 { 15823 struct bnxt_rx_ring_info *rxr, *clone; 15824 struct bnxt *bp = netdev_priv(dev); 15825 struct bnxt_ring_struct *ring; 15826 int rc; 15827 15828 if (!bp->rx_ring) 15829 return -ENETDOWN; 15830 15831 rxr = &bp->rx_ring[idx]; 15832 clone = qmem; 15833 memcpy(clone, rxr, sizeof(*rxr)); 15834 bnxt_init_rx_ring_struct(bp, clone); 15835 bnxt_reset_rx_ring_struct(bp, clone); 15836 15837 clone->rx_prod = 0; 15838 clone->rx_agg_prod = 0; 15839 clone->rx_sw_agg_prod = 0; 15840 clone->rx_next_cons = 0; 15841 clone->need_head_pool = false; 15842 15843 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15844 if (rc) 15845 return rc; 15846 15847 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15848 if (rc < 0) 15849 goto err_page_pool_destroy; 15850 15851 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15852 MEM_TYPE_PAGE_POOL, 15853 clone->page_pool); 15854 if (rc) 15855 goto err_rxq_info_unreg; 15856 15857 ring = &clone->rx_ring_struct; 15858 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15859 if (rc) 15860 goto err_free_rx_ring; 15861 15862 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15863 ring = &clone->rx_agg_ring_struct; 15864 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15865 if (rc) 15866 goto err_free_rx_agg_ring; 15867 15868 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15869 if (rc) 15870 goto err_free_rx_agg_ring; 15871 } 15872 15873 if (bp->flags & BNXT_FLAG_TPA) { 15874 rc = bnxt_alloc_one_tpa_info(bp, clone); 15875 if (rc) 15876 goto err_free_tpa_info; 15877 } 15878 15879 bnxt_init_one_rx_ring_rxbd(bp, clone); 15880 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15881 15882 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15883 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15884 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 15885 if (bp->flags & BNXT_FLAG_TPA) 15886 bnxt_alloc_one_tpa_info_data(bp, clone); 15887 15888 return 0; 15889 15890 err_free_tpa_info: 15891 bnxt_free_one_tpa_info(bp, clone); 15892 err_free_rx_agg_ring: 15893 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15894 err_free_rx_ring: 15895 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15896 err_rxq_info_unreg: 15897 xdp_rxq_info_unreg(&clone->xdp_rxq); 15898 err_page_pool_destroy: 15899 page_pool_destroy(clone->page_pool); 15900 if (bnxt_separate_head_pool(clone)) 15901 page_pool_destroy(clone->head_pool); 15902 clone->page_pool = NULL; 15903 clone->head_pool = NULL; 15904 return rc; 15905 } 15906 15907 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15908 { 15909 struct bnxt_rx_ring_info *rxr = qmem; 15910 struct bnxt *bp = netdev_priv(dev); 15911 struct bnxt_ring_struct *ring; 15912 15913 bnxt_free_one_rx_ring_skbs(bp, rxr); 15914 bnxt_free_one_tpa_info(bp, rxr); 15915 15916 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15917 15918 page_pool_destroy(rxr->page_pool); 15919 if (bnxt_separate_head_pool(rxr)) 15920 page_pool_destroy(rxr->head_pool); 15921 rxr->page_pool = NULL; 15922 rxr->head_pool = NULL; 15923 15924 ring = &rxr->rx_ring_struct; 15925 bnxt_free_ring(bp, &ring->ring_mem); 15926 15927 ring = &rxr->rx_agg_ring_struct; 15928 bnxt_free_ring(bp, &ring->ring_mem); 15929 15930 kfree(rxr->rx_agg_bmap); 15931 rxr->rx_agg_bmap = NULL; 15932 } 15933 15934 static void bnxt_copy_rx_ring(struct bnxt *bp, 15935 struct bnxt_rx_ring_info *dst, 15936 struct bnxt_rx_ring_info *src) 15937 { 15938 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15939 struct bnxt_ring_struct *dst_ring, *src_ring; 15940 int i; 15941 15942 dst_ring = &dst->rx_ring_struct; 15943 dst_rmem = &dst_ring->ring_mem; 15944 src_ring = &src->rx_ring_struct; 15945 src_rmem = &src_ring->ring_mem; 15946 15947 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15948 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15949 WARN_ON(dst_rmem->flags != src_rmem->flags); 15950 WARN_ON(dst_rmem->depth != src_rmem->depth); 15951 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15952 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15953 15954 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15955 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15956 *dst_rmem->vmem = *src_rmem->vmem; 15957 for (i = 0; i < dst_rmem->nr_pages; i++) { 15958 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15959 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15960 } 15961 15962 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15963 return; 15964 15965 dst_ring = &dst->rx_agg_ring_struct; 15966 dst_rmem = &dst_ring->ring_mem; 15967 src_ring = &src->rx_agg_ring_struct; 15968 src_rmem = &src_ring->ring_mem; 15969 15970 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15971 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15972 WARN_ON(dst_rmem->flags != src_rmem->flags); 15973 WARN_ON(dst_rmem->depth != src_rmem->depth); 15974 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15975 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15976 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15977 15978 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15979 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15980 *dst_rmem->vmem = *src_rmem->vmem; 15981 for (i = 0; i < dst_rmem->nr_pages; i++) { 15982 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15983 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15984 } 15985 15986 dst->rx_agg_bmap = src->rx_agg_bmap; 15987 } 15988 15989 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15990 { 15991 struct bnxt *bp = netdev_priv(dev); 15992 struct bnxt_rx_ring_info *rxr, *clone; 15993 struct bnxt_cp_ring_info *cpr; 15994 struct bnxt_vnic_info *vnic; 15995 struct bnxt_napi *bnapi; 15996 int i, rc; 15997 u16 mru; 15998 15999 rxr = &bp->rx_ring[idx]; 16000 clone = qmem; 16001 16002 rxr->rx_prod = clone->rx_prod; 16003 rxr->rx_agg_prod = clone->rx_agg_prod; 16004 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 16005 rxr->rx_next_cons = clone->rx_next_cons; 16006 rxr->rx_tpa = clone->rx_tpa; 16007 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 16008 rxr->page_pool = clone->page_pool; 16009 rxr->head_pool = clone->head_pool; 16010 rxr->xdp_rxq = clone->xdp_rxq; 16011 rxr->need_head_pool = clone->need_head_pool; 16012 16013 bnxt_copy_rx_ring(bp, rxr, clone); 16014 16015 bnapi = rxr->bnapi; 16016 cpr = &bnapi->cp_ring; 16017 16018 /* All rings have been reserved and previously allocated. 16019 * Reallocating with the same parameters should never fail. 16020 */ 16021 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16022 if (rc) 16023 goto err_reset; 16024 16025 if (bp->tph_mode) { 16026 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16027 if (rc) 16028 goto err_reset; 16029 } 16030 16031 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16032 if (rc) 16033 goto err_reset; 16034 16035 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16036 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16037 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16038 16039 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16040 rc = bnxt_tx_queue_start(bp, idx); 16041 if (rc) 16042 goto err_reset; 16043 } 16044 16045 napi_enable_locked(&bnapi->napi); 16046 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16047 16048 mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 16049 for (i = 0; i < bp->nr_vnics; i++) { 16050 vnic = &bp->vnic_info[i]; 16051 16052 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16053 if (rc) 16054 return rc; 16055 } 16056 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16057 16058 err_reset: 16059 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16060 rc); 16061 napi_enable_locked(&bnapi->napi); 16062 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16063 netif_close(dev); 16064 return rc; 16065 } 16066 16067 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16068 { 16069 struct bnxt *bp = netdev_priv(dev); 16070 struct bnxt_rx_ring_info *rxr; 16071 struct bnxt_cp_ring_info *cpr; 16072 struct bnxt_vnic_info *vnic; 16073 struct bnxt_napi *bnapi; 16074 int i; 16075 16076 for (i = 0; i < bp->nr_vnics; i++) { 16077 vnic = &bp->vnic_info[i]; 16078 16079 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16080 } 16081 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16082 /* Make sure NAPI sees that the VNIC is disabled */ 16083 synchronize_net(); 16084 rxr = &bp->rx_ring[idx]; 16085 bnapi = rxr->bnapi; 16086 cpr = &bnapi->cp_ring; 16087 cancel_work_sync(&cpr->dim.work); 16088 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16089 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16090 page_pool_disable_direct_recycling(rxr->page_pool); 16091 if (bnxt_separate_head_pool(rxr)) 16092 page_pool_disable_direct_recycling(rxr->head_pool); 16093 16094 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16095 bnxt_tx_queue_stop(bp, idx); 16096 16097 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16098 * completion is handled in NAPI to guarantee no more DMA on that ring 16099 * after seeing the completion. 16100 */ 16101 napi_disable_locked(&bnapi->napi); 16102 16103 if (bp->tph_mode) { 16104 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16105 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16106 } 16107 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16108 16109 memcpy(qmem, rxr, sizeof(*rxr)); 16110 bnxt_init_rx_ring_struct(bp, qmem); 16111 16112 return 0; 16113 } 16114 16115 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16116 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16117 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16118 .ndo_queue_mem_free = bnxt_queue_mem_free, 16119 .ndo_queue_start = bnxt_queue_start, 16120 .ndo_queue_stop = bnxt_queue_stop, 16121 }; 16122 16123 static void bnxt_remove_one(struct pci_dev *pdev) 16124 { 16125 struct net_device *dev = pci_get_drvdata(pdev); 16126 struct bnxt *bp = netdev_priv(dev); 16127 16128 if (BNXT_PF(bp)) 16129 bnxt_sriov_disable(bp); 16130 16131 bnxt_rdma_aux_device_del(bp); 16132 16133 unregister_netdev(dev); 16134 bnxt_ptp_clear(bp); 16135 16136 bnxt_rdma_aux_device_uninit(bp); 16137 16138 bnxt_free_l2_filters(bp, true); 16139 bnxt_free_ntp_fltrs(bp, true); 16140 WARN_ON(bp->num_rss_ctx); 16141 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16142 /* Flush any pending tasks */ 16143 cancel_work_sync(&bp->sp_task); 16144 cancel_delayed_work_sync(&bp->fw_reset_task); 16145 bp->sp_event = 0; 16146 16147 bnxt_dl_fw_reporters_destroy(bp); 16148 bnxt_dl_unregister(bp); 16149 bnxt_shutdown_tc(bp); 16150 16151 bnxt_clear_int_mode(bp); 16152 bnxt_hwrm_func_drv_unrgtr(bp); 16153 bnxt_free_hwrm_resources(bp); 16154 bnxt_hwmon_uninit(bp); 16155 bnxt_ethtool_free(bp); 16156 bnxt_dcb_free(bp); 16157 kfree(bp->ptp_cfg); 16158 bp->ptp_cfg = NULL; 16159 kfree(bp->fw_health); 16160 bp->fw_health = NULL; 16161 bnxt_cleanup_pci(bp); 16162 bnxt_free_ctx_mem(bp, true); 16163 bnxt_free_crash_dump_mem(bp); 16164 kfree(bp->rss_indir_tbl); 16165 bp->rss_indir_tbl = NULL; 16166 bnxt_free_port_stats(bp); 16167 free_netdev(dev); 16168 } 16169 16170 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16171 { 16172 int rc = 0; 16173 struct bnxt_link_info *link_info = &bp->link_info; 16174 16175 bp->phy_flags = 0; 16176 rc = bnxt_hwrm_phy_qcaps(bp); 16177 if (rc) { 16178 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16179 rc); 16180 return rc; 16181 } 16182 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16183 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16184 else 16185 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16186 16187 bp->mac_flags = 0; 16188 bnxt_hwrm_mac_qcaps(bp); 16189 16190 if (!fw_dflt) 16191 return 0; 16192 16193 mutex_lock(&bp->link_lock); 16194 rc = bnxt_update_link(bp, false); 16195 if (rc) { 16196 mutex_unlock(&bp->link_lock); 16197 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16198 rc); 16199 return rc; 16200 } 16201 16202 /* Older firmware does not have supported_auto_speeds, so assume 16203 * that all supported speeds can be autonegotiated. 16204 */ 16205 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16206 link_info->support_auto_speeds = link_info->support_speeds; 16207 16208 bnxt_init_ethtool_link_settings(bp); 16209 mutex_unlock(&bp->link_lock); 16210 return 0; 16211 } 16212 16213 static int bnxt_get_max_irq(struct pci_dev *pdev) 16214 { 16215 u16 ctrl; 16216 16217 if (!pdev->msix_cap) 16218 return 1; 16219 16220 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16221 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16222 } 16223 16224 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16225 int *max_cp) 16226 { 16227 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16228 int max_ring_grps = 0, max_irq; 16229 16230 *max_tx = hw_resc->max_tx_rings; 16231 *max_rx = hw_resc->max_rx_rings; 16232 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16233 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16234 bnxt_get_ulp_msix_num_in_use(bp), 16235 hw_resc->max_stat_ctxs - 16236 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16237 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16238 *max_cp = min_t(int, *max_cp, max_irq); 16239 max_ring_grps = hw_resc->max_hw_ring_grps; 16240 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16241 *max_cp -= 1; 16242 *max_rx -= 2; 16243 } 16244 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16245 *max_rx >>= 1; 16246 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16247 int rc; 16248 16249 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16250 if (rc) { 16251 *max_rx = 0; 16252 *max_tx = 0; 16253 } 16254 /* On P5 chips, max_cp output param should be available NQs */ 16255 *max_cp = max_irq; 16256 } 16257 *max_rx = min_t(int, *max_rx, max_ring_grps); 16258 } 16259 16260 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16261 { 16262 int rx, tx, cp; 16263 16264 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16265 *max_rx = rx; 16266 *max_tx = tx; 16267 if (!rx || !tx || !cp) 16268 return -ENOMEM; 16269 16270 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16271 } 16272 16273 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16274 bool shared) 16275 { 16276 int rc; 16277 16278 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16279 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16280 /* Not enough rings, try disabling agg rings. */ 16281 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16282 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16283 if (rc) { 16284 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16285 bp->flags |= BNXT_FLAG_AGG_RINGS; 16286 return rc; 16287 } 16288 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16289 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16290 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16291 bnxt_set_ring_params(bp); 16292 } 16293 16294 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16295 int max_cp, max_stat, max_irq; 16296 16297 /* Reserve minimum resources for RoCE */ 16298 max_cp = bnxt_get_max_func_cp_rings(bp); 16299 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16300 max_irq = bnxt_get_max_func_irqs(bp); 16301 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16302 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16303 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16304 return 0; 16305 16306 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16307 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16308 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16309 max_cp = min_t(int, max_cp, max_irq); 16310 max_cp = min_t(int, max_cp, max_stat); 16311 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16312 if (rc) 16313 rc = 0; 16314 } 16315 return rc; 16316 } 16317 16318 /* In initial default shared ring setting, each shared ring must have a 16319 * RX/TX ring pair. 16320 */ 16321 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16322 { 16323 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16324 bp->rx_nr_rings = bp->cp_nr_rings; 16325 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16326 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16327 } 16328 16329 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16330 { 16331 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16332 int avail_msix; 16333 16334 if (!bnxt_can_reserve_rings(bp)) 16335 return 0; 16336 16337 if (sh) 16338 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16339 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16340 /* Reduce default rings on multi-port cards so that total default 16341 * rings do not exceed CPU count. 16342 */ 16343 if (bp->port_count > 1) { 16344 int max_rings = 16345 max_t(int, num_online_cpus() / bp->port_count, 1); 16346 16347 dflt_rings = min_t(int, dflt_rings, max_rings); 16348 } 16349 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16350 if (rc) 16351 return rc; 16352 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16353 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16354 if (sh) 16355 bnxt_trim_dflt_sh_rings(bp); 16356 else 16357 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16358 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16359 16360 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16361 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16362 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16363 16364 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16365 bnxt_set_dflt_ulp_stat_ctxs(bp); 16366 } 16367 16368 rc = __bnxt_reserve_rings(bp); 16369 if (rc && rc != -ENODEV) 16370 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16371 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16372 if (sh) 16373 bnxt_trim_dflt_sh_rings(bp); 16374 16375 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16376 if (bnxt_need_reserve_rings(bp)) { 16377 rc = __bnxt_reserve_rings(bp); 16378 if (rc && rc != -ENODEV) 16379 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16380 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16381 } 16382 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16383 bp->rx_nr_rings++; 16384 bp->cp_nr_rings++; 16385 } 16386 if (rc) { 16387 bp->tx_nr_rings = 0; 16388 bp->rx_nr_rings = 0; 16389 } 16390 return rc; 16391 } 16392 16393 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16394 { 16395 int rc; 16396 16397 if (bp->tx_nr_rings) 16398 return 0; 16399 16400 bnxt_ulp_irq_stop(bp); 16401 bnxt_clear_int_mode(bp); 16402 rc = bnxt_set_dflt_rings(bp, true); 16403 if (rc) { 16404 if (BNXT_VF(bp) && rc == -ENODEV) 16405 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16406 else 16407 netdev_err(bp->dev, "Not enough rings available.\n"); 16408 goto init_dflt_ring_err; 16409 } 16410 rc = bnxt_init_int_mode(bp); 16411 if (rc) 16412 goto init_dflt_ring_err; 16413 16414 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16415 16416 bnxt_set_dflt_rfs(bp); 16417 16418 init_dflt_ring_err: 16419 bnxt_ulp_irq_restart(bp, rc); 16420 return rc; 16421 } 16422 16423 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16424 { 16425 int rc; 16426 16427 netdev_ops_assert_locked(bp->dev); 16428 bnxt_hwrm_func_qcaps(bp); 16429 16430 if (netif_running(bp->dev)) 16431 __bnxt_close_nic(bp, true, false); 16432 16433 bnxt_ulp_irq_stop(bp); 16434 bnxt_clear_int_mode(bp); 16435 rc = bnxt_init_int_mode(bp); 16436 bnxt_ulp_irq_restart(bp, rc); 16437 16438 if (netif_running(bp->dev)) { 16439 if (rc) 16440 netif_close(bp->dev); 16441 else 16442 rc = bnxt_open_nic(bp, true, false); 16443 } 16444 16445 return rc; 16446 } 16447 16448 static int bnxt_init_mac_addr(struct bnxt *bp) 16449 { 16450 int rc = 0; 16451 16452 if (BNXT_PF(bp)) { 16453 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 16454 } else { 16455 #ifdef CONFIG_BNXT_SRIOV 16456 struct bnxt_vf_info *vf = &bp->vf; 16457 bool strict_approval = true; 16458 16459 if (is_valid_ether_addr(vf->mac_addr)) { 16460 /* overwrite netdev dev_addr with admin VF MAC */ 16461 eth_hw_addr_set(bp->dev, vf->mac_addr); 16462 /* Older PF driver or firmware may not approve this 16463 * correctly. 16464 */ 16465 strict_approval = false; 16466 } else { 16467 eth_hw_addr_random(bp->dev); 16468 } 16469 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16470 #endif 16471 } 16472 return rc; 16473 } 16474 16475 static void bnxt_vpd_read_info(struct bnxt *bp) 16476 { 16477 struct pci_dev *pdev = bp->pdev; 16478 unsigned int vpd_size, kw_len; 16479 int pos, size; 16480 u8 *vpd_data; 16481 16482 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16483 if (IS_ERR(vpd_data)) { 16484 pci_warn(pdev, "Unable to read VPD\n"); 16485 return; 16486 } 16487 16488 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16489 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16490 if (pos < 0) 16491 goto read_sn; 16492 16493 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16494 memcpy(bp->board_partno, &vpd_data[pos], size); 16495 16496 read_sn: 16497 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16498 PCI_VPD_RO_KEYWORD_SERIALNO, 16499 &kw_len); 16500 if (pos < 0) 16501 goto exit; 16502 16503 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16504 memcpy(bp->board_serialno, &vpd_data[pos], size); 16505 exit: 16506 kfree(vpd_data); 16507 } 16508 16509 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16510 { 16511 struct pci_dev *pdev = bp->pdev; 16512 u64 qword; 16513 16514 qword = pci_get_dsn(pdev); 16515 if (!qword) { 16516 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16517 return -EOPNOTSUPP; 16518 } 16519 16520 put_unaligned_le64(qword, dsn); 16521 16522 bp->flags |= BNXT_FLAG_DSN_VALID; 16523 return 0; 16524 } 16525 16526 static int bnxt_map_db_bar(struct bnxt *bp) 16527 { 16528 if (!bp->db_size) 16529 return -ENODEV; 16530 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16531 if (!bp->bar1) 16532 return -ENOMEM; 16533 return 0; 16534 } 16535 16536 void bnxt_print_device_info(struct bnxt *bp) 16537 { 16538 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16539 board_info[bp->board_idx].name, 16540 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16541 16542 pcie_print_link_status(bp->pdev); 16543 } 16544 16545 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16546 { 16547 struct bnxt_hw_resc *hw_resc; 16548 struct net_device *dev; 16549 struct bnxt *bp; 16550 int rc, max_irqs; 16551 16552 if (pci_is_bridge(pdev)) 16553 return -ENODEV; 16554 16555 if (!pdev->msix_cap) { 16556 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16557 return -ENODEV; 16558 } 16559 16560 /* Clear any pending DMA transactions from crash kernel 16561 * while loading driver in capture kernel. 16562 */ 16563 if (is_kdump_kernel()) { 16564 pci_clear_master(pdev); 16565 pcie_flr(pdev); 16566 } 16567 16568 max_irqs = bnxt_get_max_irq(pdev); 16569 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16570 max_irqs); 16571 if (!dev) 16572 return -ENOMEM; 16573 16574 bp = netdev_priv(dev); 16575 bp->board_idx = ent->driver_data; 16576 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16577 bnxt_set_max_func_irqs(bp, max_irqs); 16578 16579 if (bnxt_vf_pciid(bp->board_idx)) 16580 bp->flags |= BNXT_FLAG_VF; 16581 16582 /* No devlink port registration in case of a VF */ 16583 if (BNXT_PF(bp)) 16584 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16585 16586 rc = bnxt_init_board(pdev, dev); 16587 if (rc < 0) 16588 goto init_err_free; 16589 16590 dev->netdev_ops = &bnxt_netdev_ops; 16591 dev->stat_ops = &bnxt_stat_ops; 16592 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16593 dev->ethtool_ops = &bnxt_ethtool_ops; 16594 pci_set_drvdata(pdev, dev); 16595 16596 rc = bnxt_alloc_hwrm_resources(bp); 16597 if (rc) 16598 goto init_err_pci_clean; 16599 16600 mutex_init(&bp->hwrm_cmd_lock); 16601 mutex_init(&bp->link_lock); 16602 16603 rc = bnxt_fw_init_one_p1(bp); 16604 if (rc) 16605 goto init_err_pci_clean; 16606 16607 if (BNXT_PF(bp)) 16608 bnxt_vpd_read_info(bp); 16609 16610 if (BNXT_CHIP_P5_PLUS(bp)) { 16611 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16612 if (BNXT_CHIP_P7(bp)) 16613 bp->flags |= BNXT_FLAG_CHIP_P7; 16614 } 16615 16616 rc = bnxt_alloc_rss_indir_tbl(bp); 16617 if (rc) 16618 goto init_err_pci_clean; 16619 16620 rc = bnxt_fw_init_one_p2(bp); 16621 if (rc) 16622 goto init_err_pci_clean; 16623 16624 rc = bnxt_map_db_bar(bp); 16625 if (rc) { 16626 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16627 rc); 16628 goto init_err_pci_clean; 16629 } 16630 16631 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16632 NETIF_F_TSO | NETIF_F_TSO6 | 16633 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16634 NETIF_F_GSO_IPXIP4 | 16635 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16636 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16637 NETIF_F_RXCSUM | NETIF_F_GRO; 16638 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16639 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16640 16641 if (BNXT_SUPPORTS_TPA(bp)) 16642 dev->hw_features |= NETIF_F_LRO; 16643 16644 dev->hw_enc_features = 16645 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16646 NETIF_F_TSO | NETIF_F_TSO6 | 16647 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16648 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16649 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16650 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16651 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16652 if (bp->flags & BNXT_FLAG_CHIP_P7) 16653 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16654 else 16655 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16656 16657 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16658 NETIF_F_GSO_GRE_CSUM; 16659 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16660 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16661 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16662 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16663 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16664 if (BNXT_SUPPORTS_TPA(bp)) 16665 dev->hw_features |= NETIF_F_GRO_HW; 16666 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16667 if (dev->features & NETIF_F_GRO_HW) 16668 dev->features &= ~NETIF_F_LRO; 16669 dev->priv_flags |= IFF_UNICAST_FLT; 16670 16671 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16672 if (bp->tso_max_segs) 16673 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16674 16675 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16676 NETDEV_XDP_ACT_RX_SG; 16677 16678 #ifdef CONFIG_BNXT_SRIOV 16679 init_waitqueue_head(&bp->sriov_cfg_wait); 16680 #endif 16681 if (BNXT_SUPPORTS_TPA(bp)) { 16682 bp->gro_func = bnxt_gro_func_5730x; 16683 if (BNXT_CHIP_P4(bp)) 16684 bp->gro_func = bnxt_gro_func_5731x; 16685 else if (BNXT_CHIP_P5_PLUS(bp)) 16686 bp->gro_func = bnxt_gro_func_5750x; 16687 } 16688 if (!BNXT_CHIP_P4_PLUS(bp)) 16689 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16690 16691 rc = bnxt_init_mac_addr(bp); 16692 if (rc) { 16693 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16694 rc = -EADDRNOTAVAIL; 16695 goto init_err_pci_clean; 16696 } 16697 16698 if (BNXT_PF(bp)) { 16699 /* Read the adapter's DSN to use as the eswitch switch_id */ 16700 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16701 } 16702 16703 /* MTU range: 60 - FW defined max */ 16704 dev->min_mtu = ETH_ZLEN; 16705 dev->max_mtu = bp->max_mtu; 16706 16707 rc = bnxt_probe_phy(bp, true); 16708 if (rc) 16709 goto init_err_pci_clean; 16710 16711 hw_resc = &bp->hw_resc; 16712 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16713 BNXT_L2_FLTR_MAX_FLTR; 16714 /* Older firmware may not report these filters properly */ 16715 if (bp->max_fltr < BNXT_MAX_FLTR) 16716 bp->max_fltr = BNXT_MAX_FLTR; 16717 bnxt_init_l2_fltr_tbl(bp); 16718 __bnxt_set_rx_skb_mode(bp, false); 16719 bnxt_set_tpa_flags(bp); 16720 bnxt_init_ring_params(bp); 16721 bnxt_set_ring_params(bp); 16722 bnxt_rdma_aux_device_init(bp); 16723 rc = bnxt_set_dflt_rings(bp, true); 16724 if (rc) { 16725 if (BNXT_VF(bp) && rc == -ENODEV) { 16726 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16727 } else { 16728 netdev_err(bp->dev, "Not enough rings available.\n"); 16729 rc = -ENOMEM; 16730 } 16731 goto init_err_pci_clean; 16732 } 16733 16734 bnxt_fw_init_one_p3(bp); 16735 16736 bnxt_init_dflt_coal(bp); 16737 16738 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16739 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16740 16741 rc = bnxt_init_int_mode(bp); 16742 if (rc) 16743 goto init_err_pci_clean; 16744 16745 /* No TC has been set yet and rings may have been trimmed due to 16746 * limited MSIX, so we re-initialize the TX rings per TC. 16747 */ 16748 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16749 16750 if (BNXT_PF(bp)) { 16751 if (!bnxt_pf_wq) { 16752 bnxt_pf_wq = 16753 create_singlethread_workqueue("bnxt_pf_wq"); 16754 if (!bnxt_pf_wq) { 16755 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16756 rc = -ENOMEM; 16757 goto init_err_pci_clean; 16758 } 16759 } 16760 rc = bnxt_init_tc(bp); 16761 if (rc) 16762 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16763 rc); 16764 } 16765 16766 bnxt_inv_fw_health_reg(bp); 16767 rc = bnxt_dl_register(bp); 16768 if (rc) 16769 goto init_err_dl; 16770 16771 INIT_LIST_HEAD(&bp->usr_fltr_list); 16772 16773 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16774 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16775 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16776 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16777 dev->request_ops_lock = true; 16778 16779 rc = register_netdev(dev); 16780 if (rc) 16781 goto init_err_cleanup; 16782 16783 bnxt_dl_fw_reporters_create(bp); 16784 16785 bnxt_rdma_aux_device_add(bp); 16786 16787 bnxt_print_device_info(bp); 16788 16789 pci_save_state(pdev); 16790 16791 return 0; 16792 init_err_cleanup: 16793 bnxt_rdma_aux_device_uninit(bp); 16794 bnxt_dl_unregister(bp); 16795 init_err_dl: 16796 bnxt_shutdown_tc(bp); 16797 bnxt_clear_int_mode(bp); 16798 16799 init_err_pci_clean: 16800 bnxt_hwrm_func_drv_unrgtr(bp); 16801 bnxt_free_hwrm_resources(bp); 16802 bnxt_hwmon_uninit(bp); 16803 bnxt_ethtool_free(bp); 16804 bnxt_ptp_clear(bp); 16805 kfree(bp->ptp_cfg); 16806 bp->ptp_cfg = NULL; 16807 kfree(bp->fw_health); 16808 bp->fw_health = NULL; 16809 bnxt_cleanup_pci(bp); 16810 bnxt_free_ctx_mem(bp, true); 16811 bnxt_free_crash_dump_mem(bp); 16812 kfree(bp->rss_indir_tbl); 16813 bp->rss_indir_tbl = NULL; 16814 16815 init_err_free: 16816 free_netdev(dev); 16817 return rc; 16818 } 16819 16820 static void bnxt_shutdown(struct pci_dev *pdev) 16821 { 16822 struct net_device *dev = pci_get_drvdata(pdev); 16823 struct bnxt *bp; 16824 16825 if (!dev) 16826 return; 16827 16828 rtnl_lock(); 16829 netdev_lock(dev); 16830 bp = netdev_priv(dev); 16831 if (!bp) 16832 goto shutdown_exit; 16833 16834 if (netif_running(dev)) 16835 netif_close(dev); 16836 16837 bnxt_ptp_clear(bp); 16838 bnxt_clear_int_mode(bp); 16839 pci_disable_device(pdev); 16840 16841 if (system_state == SYSTEM_POWER_OFF) { 16842 pci_wake_from_d3(pdev, bp->wol); 16843 pci_set_power_state(pdev, PCI_D3hot); 16844 } 16845 16846 shutdown_exit: 16847 netdev_unlock(dev); 16848 rtnl_unlock(); 16849 } 16850 16851 #ifdef CONFIG_PM_SLEEP 16852 static int bnxt_suspend(struct device *device) 16853 { 16854 struct net_device *dev = dev_get_drvdata(device); 16855 struct bnxt *bp = netdev_priv(dev); 16856 int rc = 0; 16857 16858 bnxt_ulp_stop(bp); 16859 16860 netdev_lock(dev); 16861 if (netif_running(dev)) { 16862 netif_device_detach(dev); 16863 rc = bnxt_close(dev); 16864 } 16865 bnxt_hwrm_func_drv_unrgtr(bp); 16866 bnxt_ptp_clear(bp); 16867 pci_disable_device(bp->pdev); 16868 bnxt_free_ctx_mem(bp, false); 16869 netdev_unlock(dev); 16870 return rc; 16871 } 16872 16873 static int bnxt_resume(struct device *device) 16874 { 16875 struct net_device *dev = dev_get_drvdata(device); 16876 struct bnxt *bp = netdev_priv(dev); 16877 int rc = 0; 16878 16879 rtnl_lock(); 16880 netdev_lock(dev); 16881 rc = pci_enable_device(bp->pdev); 16882 if (rc) { 16883 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16884 rc); 16885 goto resume_exit; 16886 } 16887 pci_set_master(bp->pdev); 16888 if (bnxt_hwrm_ver_get(bp)) { 16889 rc = -ENODEV; 16890 goto resume_exit; 16891 } 16892 rc = bnxt_hwrm_func_reset(bp); 16893 if (rc) { 16894 rc = -EBUSY; 16895 goto resume_exit; 16896 } 16897 16898 rc = bnxt_hwrm_func_qcaps(bp); 16899 if (rc) 16900 goto resume_exit; 16901 16902 bnxt_clear_reservations(bp, true); 16903 16904 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16905 rc = -ENODEV; 16906 goto resume_exit; 16907 } 16908 if (bp->fw_crash_mem) 16909 bnxt_hwrm_crash_dump_mem_cfg(bp); 16910 16911 if (bnxt_ptp_init(bp)) { 16912 kfree(bp->ptp_cfg); 16913 bp->ptp_cfg = NULL; 16914 } 16915 bnxt_get_wol_settings(bp); 16916 if (netif_running(dev)) { 16917 rc = bnxt_open(dev); 16918 if (!rc) 16919 netif_device_attach(dev); 16920 } 16921 16922 resume_exit: 16923 netdev_unlock(bp->dev); 16924 rtnl_unlock(); 16925 bnxt_ulp_start(bp, rc); 16926 if (!rc) 16927 bnxt_reenable_sriov(bp); 16928 return rc; 16929 } 16930 16931 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16932 #define BNXT_PM_OPS (&bnxt_pm_ops) 16933 16934 #else 16935 16936 #define BNXT_PM_OPS NULL 16937 16938 #endif /* CONFIG_PM_SLEEP */ 16939 16940 /** 16941 * bnxt_io_error_detected - called when PCI error is detected 16942 * @pdev: Pointer to PCI device 16943 * @state: The current pci connection state 16944 * 16945 * This function is called after a PCI bus error affecting 16946 * this device has been detected. 16947 */ 16948 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16949 pci_channel_state_t state) 16950 { 16951 struct net_device *netdev = pci_get_drvdata(pdev); 16952 struct bnxt *bp = netdev_priv(netdev); 16953 bool abort = false; 16954 16955 netdev_info(netdev, "PCI I/O error detected\n"); 16956 16957 bnxt_ulp_stop(bp); 16958 16959 netdev_lock(netdev); 16960 netif_device_detach(netdev); 16961 16962 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16963 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16964 abort = true; 16965 } 16966 16967 if (abort || state == pci_channel_io_perm_failure) { 16968 netdev_unlock(netdev); 16969 return PCI_ERS_RESULT_DISCONNECT; 16970 } 16971 16972 /* Link is not reliable anymore if state is pci_channel_io_frozen 16973 * so we disable bus master to prevent any potential bad DMAs before 16974 * freeing kernel memory. 16975 */ 16976 if (state == pci_channel_io_frozen) { 16977 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16978 bnxt_fw_fatal_close(bp); 16979 } 16980 16981 if (netif_running(netdev)) 16982 __bnxt_close_nic(bp, true, true); 16983 16984 if (pci_is_enabled(pdev)) 16985 pci_disable_device(pdev); 16986 bnxt_free_ctx_mem(bp, false); 16987 netdev_unlock(netdev); 16988 16989 /* Request a slot slot reset. */ 16990 return PCI_ERS_RESULT_NEED_RESET; 16991 } 16992 16993 /** 16994 * bnxt_io_slot_reset - called after the pci bus has been reset. 16995 * @pdev: Pointer to PCI device 16996 * 16997 * Restart the card from scratch, as if from a cold-boot. 16998 * At this point, the card has experienced a hard reset, 16999 * followed by fixups by BIOS, and has its config space 17000 * set up identically to what it was at cold boot. 17001 */ 17002 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 17003 { 17004 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 17005 struct net_device *netdev = pci_get_drvdata(pdev); 17006 struct bnxt *bp = netdev_priv(netdev); 17007 int retry = 0; 17008 int err = 0; 17009 int off; 17010 17011 netdev_info(bp->dev, "PCI Slot Reset\n"); 17012 17013 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 17014 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 17015 msleep(900); 17016 17017 netdev_lock(netdev); 17018 17019 if (pci_enable_device(pdev)) { 17020 dev_err(&pdev->dev, 17021 "Cannot re-enable PCI device after reset.\n"); 17022 } else { 17023 pci_set_master(pdev); 17024 /* Upon fatal error, our device internal logic that latches to 17025 * BAR value is getting reset and will restore only upon 17026 * rewriting the BARs. 17027 * 17028 * As pci_restore_state() does not re-write the BARs if the 17029 * value is same as saved value earlier, driver needs to 17030 * write the BARs to 0 to force restore, in case of fatal error. 17031 */ 17032 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17033 &bp->state)) { 17034 for (off = PCI_BASE_ADDRESS_0; 17035 off <= PCI_BASE_ADDRESS_5; off += 4) 17036 pci_write_config_dword(bp->pdev, off, 0); 17037 } 17038 pci_restore_state(pdev); 17039 pci_save_state(pdev); 17040 17041 bnxt_inv_fw_health_reg(bp); 17042 bnxt_try_map_fw_health_reg(bp); 17043 17044 /* In some PCIe AER scenarios, firmware may take up to 17045 * 10 seconds to become ready in the worst case. 17046 */ 17047 do { 17048 err = bnxt_try_recover_fw(bp); 17049 if (!err) 17050 break; 17051 retry++; 17052 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17053 17054 if (err) { 17055 dev_err(&pdev->dev, "Firmware not ready\n"); 17056 goto reset_exit; 17057 } 17058 17059 err = bnxt_hwrm_func_reset(bp); 17060 if (!err) 17061 result = PCI_ERS_RESULT_RECOVERED; 17062 17063 /* IRQ will be initialized later in bnxt_io_resume */ 17064 bnxt_ulp_irq_stop(bp); 17065 bnxt_clear_int_mode(bp); 17066 } 17067 17068 reset_exit: 17069 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17070 bnxt_clear_reservations(bp, true); 17071 netdev_unlock(netdev); 17072 17073 return result; 17074 } 17075 17076 /** 17077 * bnxt_io_resume - called when traffic can start flowing again. 17078 * @pdev: Pointer to PCI device 17079 * 17080 * This callback is called when the error recovery driver tells 17081 * us that its OK to resume normal operation. 17082 */ 17083 static void bnxt_io_resume(struct pci_dev *pdev) 17084 { 17085 struct net_device *netdev = pci_get_drvdata(pdev); 17086 struct bnxt *bp = netdev_priv(netdev); 17087 int err; 17088 17089 netdev_info(bp->dev, "PCI Slot Resume\n"); 17090 rtnl_lock(); 17091 netdev_lock(netdev); 17092 17093 err = bnxt_hwrm_func_qcaps(bp); 17094 if (!err) { 17095 if (netif_running(netdev)) { 17096 err = bnxt_open(netdev); 17097 } else { 17098 err = bnxt_reserve_rings(bp, true); 17099 if (!err) 17100 err = bnxt_init_int_mode(bp); 17101 } 17102 } 17103 17104 if (!err) 17105 netif_device_attach(netdev); 17106 17107 netdev_unlock(netdev); 17108 rtnl_unlock(); 17109 bnxt_ulp_start(bp, err); 17110 if (!err) 17111 bnxt_reenable_sriov(bp); 17112 } 17113 17114 static const struct pci_error_handlers bnxt_err_handler = { 17115 .error_detected = bnxt_io_error_detected, 17116 .slot_reset = bnxt_io_slot_reset, 17117 .resume = bnxt_io_resume 17118 }; 17119 17120 static struct pci_driver bnxt_pci_driver = { 17121 .name = DRV_MODULE_NAME, 17122 .id_table = bnxt_pci_tbl, 17123 .probe = bnxt_init_one, 17124 .remove = bnxt_remove_one, 17125 .shutdown = bnxt_shutdown, 17126 .driver.pm = BNXT_PM_OPS, 17127 .err_handler = &bnxt_err_handler, 17128 #if defined(CONFIG_BNXT_SRIOV) 17129 .sriov_configure = bnxt_sriov_configure, 17130 #endif 17131 }; 17132 17133 static int __init bnxt_init(void) 17134 { 17135 int err; 17136 17137 bnxt_debug_init(); 17138 err = pci_register_driver(&bnxt_pci_driver); 17139 if (err) { 17140 bnxt_debug_exit(); 17141 return err; 17142 } 17143 17144 return 0; 17145 } 17146 17147 static void __exit bnxt_exit(void) 17148 { 17149 pci_unregister_driver(&bnxt_pci_driver); 17150 if (bnxt_pf_wq) 17151 destroy_workqueue(bnxt_pf_wq); 17152 bnxt_debug_exit(); 17153 } 17154 17155 module_init(bnxt_init); 17156 module_exit(bnxt_exit); 17157