1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2019 Broadcom Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation. 9 */ 10 11 #include <linux/module.h> 12 13 #include <linux/stringify.h> 14 #include <linux/kernel.h> 15 #include <linux/timer.h> 16 #include <linux/errno.h> 17 #include <linux/ioport.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/interrupt.h> 21 #include <linux/pci.h> 22 #include <linux/netdevice.h> 23 #include <linux/etherdevice.h> 24 #include <linux/skbuff.h> 25 #include <linux/dma-mapping.h> 26 #include <linux/bitops.h> 27 #include <linux/io.h> 28 #include <linux/irq.h> 29 #include <linux/delay.h> 30 #include <asm/byteorder.h> 31 #include <asm/page.h> 32 #include <linux/time.h> 33 #include <linux/mii.h> 34 #include <linux/mdio.h> 35 #include <linux/if.h> 36 #include <linux/if_vlan.h> 37 #include <linux/if_bridge.h> 38 #include <linux/rtc.h> 39 #include <linux/bpf.h> 40 #include <net/gro.h> 41 #include <net/ip.h> 42 #include <net/tcp.h> 43 #include <net/udp.h> 44 #include <net/checksum.h> 45 #include <net/ip6_checksum.h> 46 #include <net/udp_tunnel.h> 47 #include <linux/workqueue.h> 48 #include <linux/prefetch.h> 49 #include <linux/cache.h> 50 #include <linux/log2.h> 51 #include <linux/bitmap.h> 52 #include <linux/cpu_rmap.h> 53 #include <linux/cpumask.h> 54 #include <net/pkt_cls.h> 55 #include <net/page_pool/helpers.h> 56 #include <linux/align.h> 57 #include <net/netdev_queues.h> 58 59 #include "bnxt_hsi.h" 60 #include "bnxt.h" 61 #include "bnxt_hwrm.h" 62 #include "bnxt_ulp.h" 63 #include "bnxt_sriov.h" 64 #include "bnxt_ethtool.h" 65 #include "bnxt_dcb.h" 66 #include "bnxt_xdp.h" 67 #include "bnxt_ptp.h" 68 #include "bnxt_vfr.h" 69 #include "bnxt_tc.h" 70 #include "bnxt_devlink.h" 71 #include "bnxt_debugfs.h" 72 #include "bnxt_coredump.h" 73 #include "bnxt_hwmon.h" 74 75 #define BNXT_TX_TIMEOUT (5 * HZ) 76 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 77 NETIF_MSG_TX_ERR) 78 79 MODULE_LICENSE("GPL"); 80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 81 82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 84 #define BNXT_RX_COPY_THRESH 256 85 86 #define BNXT_TX_PUSH_THRESH 164 87 88 /* indexed by enum board_idx */ 89 static const struct { 90 char *name; 91 } board_info[] = { 92 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 93 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 94 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 95 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 96 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 97 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 98 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 99 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 100 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 101 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 102 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 103 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 104 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 105 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 106 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 108 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 109 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 110 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 111 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 112 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 113 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 114 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 115 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 116 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 117 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 118 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 119 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 120 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 121 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 122 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 123 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 124 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 125 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 126 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 127 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 128 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 129 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 130 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 131 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 132 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 133 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 134 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 135 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 136 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 137 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 138 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 139 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 140 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 141 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 142 }; 143 144 static const struct pci_device_id bnxt_pci_tbl[] = { 145 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 146 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 147 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 148 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 149 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 150 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 151 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 152 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 153 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 154 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 155 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 156 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 157 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 158 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 159 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 160 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 163 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 164 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 165 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 166 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 167 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 168 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 169 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 170 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 171 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 172 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 173 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 175 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 179 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 180 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 181 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 182 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 183 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 184 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 185 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 186 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 187 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 188 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 189 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 190 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 191 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 193 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 194 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 195 #ifdef CONFIG_BNXT_SRIOV 196 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 197 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 198 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 199 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 200 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 201 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 202 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 203 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 206 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 207 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 208 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 209 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 210 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 211 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 212 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 215 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 216 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 217 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 218 #endif 219 { 0 } 220 }; 221 222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 223 224 static const u16 bnxt_vf_req_snif[] = { 225 HWRM_FUNC_CFG, 226 HWRM_FUNC_VF_CFG, 227 HWRM_PORT_PHY_QCFG, 228 HWRM_CFA_L2_FILTER_ALLOC, 229 }; 230 231 static const u16 bnxt_async_events_arr[] = { 232 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 233 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 234 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 235 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 236 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 237 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 238 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 239 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 240 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 241 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 242 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 243 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 244 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 245 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 246 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 247 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 248 ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER, 249 }; 250 251 const u16 bnxt_bstore_to_trace[] = { 252 [BNXT_CTX_SRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE, 253 [BNXT_CTX_SRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE, 254 [BNXT_CTX_CRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE, 255 [BNXT_CTX_CRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE, 256 [BNXT_CTX_RIGP0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE, 257 [BNXT_CTX_L2HWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE, 258 [BNXT_CTX_REHWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE, 259 [BNXT_CTX_CA0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE, 260 [BNXT_CTX_CA1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE, 261 [BNXT_CTX_CA2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE, 262 [BNXT_CTX_RIGP1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE, 263 }; 264 265 static struct workqueue_struct *bnxt_pf_wq; 266 267 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 268 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 269 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 270 271 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 272 .ports = { 273 .src = 0, 274 .dst = 0, 275 }, 276 .addrs = { 277 .v6addrs = { 278 .src = BNXT_IPV6_MASK_NONE, 279 .dst = BNXT_IPV6_MASK_NONE, 280 }, 281 }, 282 }; 283 284 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 285 .ports = { 286 .src = cpu_to_be16(0xffff), 287 .dst = cpu_to_be16(0xffff), 288 }, 289 .addrs = { 290 .v6addrs = { 291 .src = BNXT_IPV6_MASK_ALL, 292 .dst = BNXT_IPV6_MASK_ALL, 293 }, 294 }, 295 }; 296 297 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 298 .ports = { 299 .src = cpu_to_be16(0xffff), 300 .dst = cpu_to_be16(0xffff), 301 }, 302 .addrs = { 303 .v4addrs = { 304 .src = cpu_to_be32(0xffffffff), 305 .dst = cpu_to_be32(0xffffffff), 306 }, 307 }, 308 }; 309 310 static bool bnxt_vf_pciid(enum board_idx idx) 311 { 312 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 313 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 314 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 315 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF); 316 } 317 318 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 319 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 320 321 #define BNXT_DB_CQ(db, idx) \ 322 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 323 324 #define BNXT_DB_NQ_P5(db, idx) \ 325 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 326 (db)->doorbell) 327 328 #define BNXT_DB_NQ_P7(db, idx) \ 329 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 330 DB_RING_IDX(db, idx), (db)->doorbell) 331 332 #define BNXT_DB_CQ_ARM(db, idx) \ 333 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 334 335 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 336 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 337 DB_RING_IDX(db, idx), (db)->doorbell) 338 339 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 340 { 341 if (bp->flags & BNXT_FLAG_CHIP_P7) 342 BNXT_DB_NQ_P7(db, idx); 343 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 344 BNXT_DB_NQ_P5(db, idx); 345 else 346 BNXT_DB_CQ(db, idx); 347 } 348 349 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 350 { 351 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 352 BNXT_DB_NQ_ARM_P5(db, idx); 353 else 354 BNXT_DB_CQ_ARM(db, idx); 355 } 356 357 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 358 { 359 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 360 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 361 DB_RING_IDX(db, idx), db->doorbell); 362 else 363 BNXT_DB_CQ(db, idx); 364 } 365 366 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 367 { 368 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 369 return; 370 371 if (BNXT_PF(bp)) 372 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 373 else 374 schedule_delayed_work(&bp->fw_reset_task, delay); 375 } 376 377 static void __bnxt_queue_sp_work(struct bnxt *bp) 378 { 379 if (BNXT_PF(bp)) 380 queue_work(bnxt_pf_wq, &bp->sp_task); 381 else 382 schedule_work(&bp->sp_task); 383 } 384 385 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 386 { 387 set_bit(event, &bp->sp_event); 388 __bnxt_queue_sp_work(bp); 389 } 390 391 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 392 { 393 if (!rxr->bnapi->in_reset) { 394 rxr->bnapi->in_reset = true; 395 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 396 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 397 else 398 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 399 __bnxt_queue_sp_work(bp); 400 } 401 rxr->rx_next_cons = 0xffff; 402 } 403 404 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 405 u16 curr) 406 { 407 struct bnxt_napi *bnapi = txr->bnapi; 408 409 if (bnapi->tx_fault) 410 return; 411 412 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 413 txr->txq_index, txr->tx_hw_cons, 414 txr->tx_cons, txr->tx_prod, curr); 415 WARN_ON_ONCE(1); 416 bnapi->tx_fault = 1; 417 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 418 } 419 420 const u16 bnxt_lhint_arr[] = { 421 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 422 TX_BD_FLAGS_LHINT_512_TO_1023, 423 TX_BD_FLAGS_LHINT_1024_TO_2047, 424 TX_BD_FLAGS_LHINT_1024_TO_2047, 425 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 426 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 427 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 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 }; 441 442 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 443 { 444 struct metadata_dst *md_dst = skb_metadata_dst(skb); 445 446 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 447 return 0; 448 449 return md_dst->u.port_info.port_id; 450 } 451 452 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 453 u16 prod) 454 { 455 /* Sync BD data before updating doorbell */ 456 wmb(); 457 bnxt_db_write(bp, &txr->tx_db, prod); 458 txr->kick_pending = 0; 459 } 460 461 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 462 { 463 struct bnxt *bp = netdev_priv(dev); 464 struct tx_bd *txbd, *txbd0; 465 struct tx_bd_ext *txbd1; 466 struct netdev_queue *txq; 467 int i; 468 dma_addr_t mapping; 469 unsigned int length, pad = 0; 470 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 471 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 472 struct pci_dev *pdev = bp->pdev; 473 u16 prod, last_frag, txts_prod; 474 struct bnxt_tx_ring_info *txr; 475 struct bnxt_sw_tx_bd *tx_buf; 476 __le32 lflags = 0; 477 478 i = skb_get_queue_mapping(skb); 479 if (unlikely(i >= bp->tx_nr_rings)) { 480 dev_kfree_skb_any(skb); 481 dev_core_stats_tx_dropped_inc(dev); 482 return NETDEV_TX_OK; 483 } 484 485 txq = netdev_get_tx_queue(dev, i); 486 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 487 prod = txr->tx_prod; 488 489 free_size = bnxt_tx_avail(bp, txr); 490 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 491 /* We must have raced with NAPI cleanup */ 492 if (net_ratelimit() && txr->kick_pending) 493 netif_warn(bp, tx_err, dev, 494 "bnxt: ring busy w/ flush pending!\n"); 495 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 496 bp->tx_wake_thresh)) 497 return NETDEV_TX_BUSY; 498 } 499 500 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 501 goto tx_free; 502 503 length = skb->len; 504 len = skb_headlen(skb); 505 last_frag = skb_shinfo(skb)->nr_frags; 506 507 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 508 509 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 510 tx_buf->skb = skb; 511 tx_buf->nr_frags = last_frag; 512 513 vlan_tag_flags = 0; 514 cfa_action = bnxt_xmit_get_cfa_action(skb); 515 if (skb_vlan_tag_present(skb)) { 516 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 517 skb_vlan_tag_get(skb); 518 /* Currently supports 8021Q, 8021AD vlan offloads 519 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 520 */ 521 if (skb->vlan_proto == htons(ETH_P_8021Q)) 522 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 523 } 524 525 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 526 ptp->tx_tstamp_en) { 527 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 528 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 529 tx_buf->is_ts_pkt = 1; 530 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 531 } else if (!skb_is_gso(skb)) { 532 u16 seq_id, hdr_off; 533 534 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 535 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 536 if (vlan_tag_flags) 537 hdr_off += VLAN_HLEN; 538 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 539 tx_buf->is_ts_pkt = 1; 540 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 541 542 ptp->txts_req[txts_prod].tx_seqid = seq_id; 543 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 544 tx_buf->txts_prod = txts_prod; 545 } 546 } 547 } 548 if (unlikely(skb->no_fcs)) 549 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 550 551 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 552 !lflags) { 553 struct tx_push_buffer *tx_push_buf = txr->tx_push; 554 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 555 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 556 void __iomem *db = txr->tx_db.doorbell; 557 void *pdata = tx_push_buf->data; 558 u64 *end; 559 int j, push_len; 560 561 /* Set COAL_NOW to be ready quickly for the next push */ 562 tx_push->tx_bd_len_flags_type = 563 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 564 TX_BD_TYPE_LONG_TX_BD | 565 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 566 TX_BD_FLAGS_COAL_NOW | 567 TX_BD_FLAGS_PACKET_END | 568 (2 << TX_BD_FLAGS_BD_CNT_SHIFT)); 569 570 if (skb->ip_summed == CHECKSUM_PARTIAL) 571 tx_push1->tx_bd_hsize_lflags = 572 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 573 else 574 tx_push1->tx_bd_hsize_lflags = 0; 575 576 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 577 tx_push1->tx_bd_cfa_action = 578 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 579 580 end = pdata + length; 581 end = PTR_ALIGN(end, 8) - 1; 582 *end = 0; 583 584 skb_copy_from_linear_data(skb, pdata, len); 585 pdata += len; 586 for (j = 0; j < last_frag; j++) { 587 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 588 void *fptr; 589 590 fptr = skb_frag_address_safe(frag); 591 if (!fptr) 592 goto normal_tx; 593 594 memcpy(pdata, fptr, skb_frag_size(frag)); 595 pdata += skb_frag_size(frag); 596 } 597 598 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 599 txbd->tx_bd_haddr = txr->data_mapping; 600 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 601 prod = NEXT_TX(prod); 602 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 603 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 604 memcpy(txbd, tx_push1, sizeof(*txbd)); 605 prod = NEXT_TX(prod); 606 tx_push->doorbell = 607 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 608 DB_RING_IDX(&txr->tx_db, prod)); 609 WRITE_ONCE(txr->tx_prod, prod); 610 611 tx_buf->is_push = 1; 612 netdev_tx_sent_queue(txq, skb->len); 613 wmb(); /* Sync is_push and byte queue before pushing data */ 614 615 push_len = (length + sizeof(*tx_push) + 7) / 8; 616 if (push_len > 16) { 617 __iowrite64_copy(db, tx_push_buf, 16); 618 __iowrite32_copy(db + 4, tx_push_buf + 1, 619 (push_len - 16) << 1); 620 } else { 621 __iowrite64_copy(db, tx_push_buf, push_len); 622 } 623 624 goto tx_done; 625 } 626 627 normal_tx: 628 if (length < BNXT_MIN_PKT_SIZE) { 629 pad = BNXT_MIN_PKT_SIZE - length; 630 if (skb_pad(skb, pad)) 631 /* SKB already freed. */ 632 goto tx_kick_pending; 633 length = BNXT_MIN_PKT_SIZE; 634 } 635 636 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 637 638 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 639 goto tx_free; 640 641 dma_unmap_addr_set(tx_buf, mapping, mapping); 642 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 643 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT); 644 645 txbd->tx_bd_haddr = cpu_to_le64(mapping); 646 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 647 648 prod = NEXT_TX(prod); 649 txbd1 = (struct tx_bd_ext *) 650 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 651 652 txbd1->tx_bd_hsize_lflags = lflags; 653 if (skb_is_gso(skb)) { 654 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 655 u32 hdr_len; 656 657 if (skb->encapsulation) { 658 if (udp_gso) 659 hdr_len = skb_inner_transport_offset(skb) + 660 sizeof(struct udphdr); 661 else 662 hdr_len = skb_inner_tcp_all_headers(skb); 663 } else if (udp_gso) { 664 hdr_len = skb_transport_offset(skb) + 665 sizeof(struct udphdr); 666 } else { 667 hdr_len = skb_tcp_all_headers(skb); 668 } 669 670 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 671 TX_BD_FLAGS_T_IPID | 672 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 673 length = skb_shinfo(skb)->gso_size; 674 txbd1->tx_bd_mss = cpu_to_le32(length); 675 length += hdr_len; 676 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 677 txbd1->tx_bd_hsize_lflags |= 678 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 679 txbd1->tx_bd_mss = 0; 680 } 681 682 length >>= 9; 683 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 684 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 685 skb->len); 686 i = 0; 687 goto tx_dma_error; 688 } 689 flags |= bnxt_lhint_arr[length]; 690 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 691 692 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 693 txbd1->tx_bd_cfa_action = 694 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 695 txbd0 = txbd; 696 for (i = 0; i < last_frag; i++) { 697 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 698 699 prod = NEXT_TX(prod); 700 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 701 702 len = skb_frag_size(frag); 703 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 704 DMA_TO_DEVICE); 705 706 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 707 goto tx_dma_error; 708 709 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 710 dma_unmap_addr_set(tx_buf, mapping, mapping); 711 712 txbd->tx_bd_haddr = cpu_to_le64(mapping); 713 714 flags = len << TX_BD_LEN_SHIFT; 715 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 716 } 717 718 flags &= ~TX_BD_LEN; 719 txbd->tx_bd_len_flags_type = 720 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 721 TX_BD_FLAGS_PACKET_END); 722 723 netdev_tx_sent_queue(txq, skb->len); 724 725 skb_tx_timestamp(skb); 726 727 prod = NEXT_TX(prod); 728 WRITE_ONCE(txr->tx_prod, prod); 729 730 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 731 bnxt_txr_db_kick(bp, txr, prod); 732 } else { 733 if (free_size >= bp->tx_wake_thresh) 734 txbd0->tx_bd_len_flags_type |= 735 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 736 txr->kick_pending = 1; 737 } 738 739 tx_done: 740 741 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 742 if (netdev_xmit_more() && !tx_buf->is_push) { 743 txbd0->tx_bd_len_flags_type &= 744 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 745 bnxt_txr_db_kick(bp, txr, prod); 746 } 747 748 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 749 bp->tx_wake_thresh); 750 } 751 return NETDEV_TX_OK; 752 753 tx_dma_error: 754 last_frag = i; 755 756 /* start back at beginning and unmap skb */ 757 prod = txr->tx_prod; 758 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 759 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 760 skb_headlen(skb), DMA_TO_DEVICE); 761 prod = NEXT_TX(prod); 762 763 /* unmap remaining mapped pages */ 764 for (i = 0; i < last_frag; i++) { 765 prod = NEXT_TX(prod); 766 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 767 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 768 skb_frag_size(&skb_shinfo(skb)->frags[i]), 769 DMA_TO_DEVICE); 770 } 771 772 tx_free: 773 dev_kfree_skb_any(skb); 774 tx_kick_pending: 775 if (BNXT_TX_PTP_IS_SET(lflags)) { 776 txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0; 777 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 778 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 779 /* set SKB to err so PTP worker will clean up */ 780 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 781 } 782 if (txr->kick_pending) 783 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 784 txr->tx_buf_ring[txr->tx_prod].skb = NULL; 785 dev_core_stats_tx_dropped_inc(dev); 786 return NETDEV_TX_OK; 787 } 788 789 /* Returns true if some remaining TX packets not processed. */ 790 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 791 int budget) 792 { 793 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 794 struct pci_dev *pdev = bp->pdev; 795 u16 hw_cons = txr->tx_hw_cons; 796 unsigned int tx_bytes = 0; 797 u16 cons = txr->tx_cons; 798 int tx_pkts = 0; 799 bool rc = false; 800 801 while (RING_TX(bp, cons) != hw_cons) { 802 struct bnxt_sw_tx_bd *tx_buf; 803 struct sk_buff *skb; 804 bool is_ts_pkt; 805 int j, last; 806 807 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 808 skb = tx_buf->skb; 809 810 if (unlikely(!skb)) { 811 bnxt_sched_reset_txr(bp, txr, cons); 812 return rc; 813 } 814 815 is_ts_pkt = tx_buf->is_ts_pkt; 816 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 817 rc = true; 818 break; 819 } 820 821 cons = NEXT_TX(cons); 822 tx_pkts++; 823 tx_bytes += skb->len; 824 tx_buf->skb = NULL; 825 tx_buf->is_ts_pkt = 0; 826 827 if (tx_buf->is_push) { 828 tx_buf->is_push = 0; 829 goto next_tx_int; 830 } 831 832 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 833 skb_headlen(skb), DMA_TO_DEVICE); 834 last = tx_buf->nr_frags; 835 836 for (j = 0; j < last; j++) { 837 cons = NEXT_TX(cons); 838 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 839 dma_unmap_page( 840 &pdev->dev, 841 dma_unmap_addr(tx_buf, mapping), 842 skb_frag_size(&skb_shinfo(skb)->frags[j]), 843 DMA_TO_DEVICE); 844 } 845 if (unlikely(is_ts_pkt)) { 846 if (BNXT_CHIP_P5(bp)) { 847 /* PTP worker takes ownership of the skb */ 848 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 849 skb = NULL; 850 } 851 } 852 853 next_tx_int: 854 cons = NEXT_TX(cons); 855 856 dev_consume_skb_any(skb); 857 } 858 859 WRITE_ONCE(txr->tx_cons, cons); 860 861 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 862 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 863 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 864 865 return rc; 866 } 867 868 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 869 { 870 struct bnxt_tx_ring_info *txr; 871 bool more = false; 872 int i; 873 874 bnxt_for_each_napi_tx(i, bnapi, txr) { 875 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 876 more |= __bnxt_tx_int(bp, txr, budget); 877 } 878 if (!more) 879 bnapi->events &= ~BNXT_TX_CMP_EVENT; 880 } 881 882 static bool bnxt_separate_head_pool(void) 883 { 884 return PAGE_SIZE > BNXT_RX_PAGE_SIZE; 885 } 886 887 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 888 struct bnxt_rx_ring_info *rxr, 889 unsigned int *offset, 890 gfp_t gfp) 891 { 892 struct page *page; 893 894 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 895 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 896 BNXT_RX_PAGE_SIZE); 897 } else { 898 page = page_pool_dev_alloc_pages(rxr->page_pool); 899 *offset = 0; 900 } 901 if (!page) 902 return NULL; 903 904 *mapping = page_pool_get_dma_addr(page) + *offset; 905 return page; 906 } 907 908 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 909 struct bnxt_rx_ring_info *rxr, 910 gfp_t gfp) 911 { 912 unsigned int offset; 913 struct page *page; 914 915 page = page_pool_alloc_frag(rxr->head_pool, &offset, 916 bp->rx_buf_size, gfp); 917 if (!page) 918 return NULL; 919 920 *mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset; 921 return page_address(page) + offset; 922 } 923 924 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 925 u16 prod, gfp_t gfp) 926 { 927 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 928 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 929 dma_addr_t mapping; 930 931 if (BNXT_RX_PAGE_MODE(bp)) { 932 unsigned int offset; 933 struct page *page = 934 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 935 936 if (!page) 937 return -ENOMEM; 938 939 mapping += bp->rx_dma_offset; 940 rx_buf->data = page; 941 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 942 } else { 943 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp); 944 945 if (!data) 946 return -ENOMEM; 947 948 rx_buf->data = data; 949 rx_buf->data_ptr = data + bp->rx_offset; 950 } 951 rx_buf->mapping = mapping; 952 953 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 954 return 0; 955 } 956 957 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 958 { 959 u16 prod = rxr->rx_prod; 960 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 961 struct bnxt *bp = rxr->bnapi->bp; 962 struct rx_bd *cons_bd, *prod_bd; 963 964 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 965 cons_rx_buf = &rxr->rx_buf_ring[cons]; 966 967 prod_rx_buf->data = data; 968 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 969 970 prod_rx_buf->mapping = cons_rx_buf->mapping; 971 972 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 973 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 974 975 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 976 } 977 978 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 979 { 980 u16 next, max = rxr->rx_agg_bmap_size; 981 982 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 983 if (next >= max) 984 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 985 return next; 986 } 987 988 static inline int bnxt_alloc_rx_page(struct bnxt *bp, 989 struct bnxt_rx_ring_info *rxr, 990 u16 prod, gfp_t gfp) 991 { 992 struct rx_bd *rxbd = 993 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 994 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 995 struct page *page; 996 dma_addr_t mapping; 997 u16 sw_prod = rxr->rx_sw_agg_prod; 998 unsigned int offset = 0; 999 1000 page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 1001 1002 if (!page) 1003 return -ENOMEM; 1004 1005 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1006 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1007 1008 __set_bit(sw_prod, rxr->rx_agg_bmap); 1009 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1010 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1011 1012 rx_agg_buf->page = page; 1013 rx_agg_buf->offset = offset; 1014 rx_agg_buf->mapping = mapping; 1015 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1016 rxbd->rx_bd_opaque = sw_prod; 1017 return 0; 1018 } 1019 1020 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1021 struct bnxt_cp_ring_info *cpr, 1022 u16 cp_cons, u16 curr) 1023 { 1024 struct rx_agg_cmp *agg; 1025 1026 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1027 agg = (struct rx_agg_cmp *) 1028 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1029 return agg; 1030 } 1031 1032 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1033 struct bnxt_rx_ring_info *rxr, 1034 u16 agg_id, u16 curr) 1035 { 1036 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1037 1038 return &tpa_info->agg_arr[curr]; 1039 } 1040 1041 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1042 u16 start, u32 agg_bufs, bool tpa) 1043 { 1044 struct bnxt_napi *bnapi = cpr->bnapi; 1045 struct bnxt *bp = bnapi->bp; 1046 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1047 u16 prod = rxr->rx_agg_prod; 1048 u16 sw_prod = rxr->rx_sw_agg_prod; 1049 bool p5_tpa = false; 1050 u32 i; 1051 1052 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1053 p5_tpa = true; 1054 1055 for (i = 0; i < agg_bufs; i++) { 1056 u16 cons; 1057 struct rx_agg_cmp *agg; 1058 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1059 struct rx_bd *prod_bd; 1060 struct page *page; 1061 1062 if (p5_tpa) 1063 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1064 else 1065 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1066 cons = agg->rx_agg_cmp_opaque; 1067 __clear_bit(cons, rxr->rx_agg_bmap); 1068 1069 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1070 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1071 1072 __set_bit(sw_prod, rxr->rx_agg_bmap); 1073 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1074 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1075 1076 /* It is possible for sw_prod to be equal to cons, so 1077 * set cons_rx_buf->page to NULL first. 1078 */ 1079 page = cons_rx_buf->page; 1080 cons_rx_buf->page = NULL; 1081 prod_rx_buf->page = page; 1082 prod_rx_buf->offset = cons_rx_buf->offset; 1083 1084 prod_rx_buf->mapping = cons_rx_buf->mapping; 1085 1086 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1087 1088 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1089 prod_bd->rx_bd_opaque = sw_prod; 1090 1091 prod = NEXT_RX_AGG(prod); 1092 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1093 } 1094 rxr->rx_agg_prod = prod; 1095 rxr->rx_sw_agg_prod = sw_prod; 1096 } 1097 1098 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1099 struct bnxt_rx_ring_info *rxr, 1100 u16 cons, void *data, u8 *data_ptr, 1101 dma_addr_t dma_addr, 1102 unsigned int offset_and_len) 1103 { 1104 unsigned int len = offset_and_len & 0xffff; 1105 struct page *page = data; 1106 u16 prod = rxr->rx_prod; 1107 struct sk_buff *skb; 1108 int err; 1109 1110 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1111 if (unlikely(err)) { 1112 bnxt_reuse_rx_data(rxr, cons, data); 1113 return NULL; 1114 } 1115 dma_addr -= bp->rx_dma_offset; 1116 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1117 bp->rx_dir); 1118 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1119 if (!skb) { 1120 page_pool_recycle_direct(rxr->page_pool, page); 1121 return NULL; 1122 } 1123 skb_mark_for_recycle(skb); 1124 skb_reserve(skb, bp->rx_offset); 1125 __skb_put(skb, len); 1126 1127 return skb; 1128 } 1129 1130 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1131 struct bnxt_rx_ring_info *rxr, 1132 u16 cons, void *data, u8 *data_ptr, 1133 dma_addr_t dma_addr, 1134 unsigned int offset_and_len) 1135 { 1136 unsigned int payload = offset_and_len >> 16; 1137 unsigned int len = offset_and_len & 0xffff; 1138 skb_frag_t *frag; 1139 struct page *page = data; 1140 u16 prod = rxr->rx_prod; 1141 struct sk_buff *skb; 1142 int off, err; 1143 1144 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1145 if (unlikely(err)) { 1146 bnxt_reuse_rx_data(rxr, cons, data); 1147 return NULL; 1148 } 1149 dma_addr -= bp->rx_dma_offset; 1150 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1151 bp->rx_dir); 1152 1153 if (unlikely(!payload)) 1154 payload = eth_get_headlen(bp->dev, data_ptr, len); 1155 1156 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1157 if (!skb) { 1158 page_pool_recycle_direct(rxr->page_pool, page); 1159 return NULL; 1160 } 1161 1162 skb_mark_for_recycle(skb); 1163 off = (void *)data_ptr - page_address(page); 1164 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1165 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1166 payload + NET_IP_ALIGN); 1167 1168 frag = &skb_shinfo(skb)->frags[0]; 1169 skb_frag_size_sub(frag, payload); 1170 skb_frag_off_add(frag, payload); 1171 skb->data_len -= payload; 1172 skb->tail += payload; 1173 1174 return skb; 1175 } 1176 1177 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1178 struct bnxt_rx_ring_info *rxr, u16 cons, 1179 void *data, u8 *data_ptr, 1180 dma_addr_t dma_addr, 1181 unsigned int offset_and_len) 1182 { 1183 u16 prod = rxr->rx_prod; 1184 struct sk_buff *skb; 1185 int err; 1186 1187 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1188 if (unlikely(err)) { 1189 bnxt_reuse_rx_data(rxr, cons, data); 1190 return NULL; 1191 } 1192 1193 skb = napi_build_skb(data, bp->rx_buf_size); 1194 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1195 bp->rx_dir); 1196 if (!skb) { 1197 page_pool_free_va(rxr->head_pool, data, true); 1198 return NULL; 1199 } 1200 1201 skb_mark_for_recycle(skb); 1202 skb_reserve(skb, bp->rx_offset); 1203 skb_put(skb, offset_and_len & 0xffff); 1204 return skb; 1205 } 1206 1207 static u32 __bnxt_rx_agg_pages(struct bnxt *bp, 1208 struct bnxt_cp_ring_info *cpr, 1209 struct skb_shared_info *shinfo, 1210 u16 idx, u32 agg_bufs, bool tpa, 1211 struct xdp_buff *xdp) 1212 { 1213 struct bnxt_napi *bnapi = cpr->bnapi; 1214 struct pci_dev *pdev = bp->pdev; 1215 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1216 u16 prod = rxr->rx_agg_prod; 1217 u32 i, total_frag_len = 0; 1218 bool p5_tpa = false; 1219 1220 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1221 p5_tpa = true; 1222 1223 for (i = 0; i < agg_bufs; i++) { 1224 skb_frag_t *frag = &shinfo->frags[i]; 1225 u16 cons, frag_len; 1226 struct rx_agg_cmp *agg; 1227 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1228 struct page *page; 1229 dma_addr_t mapping; 1230 1231 if (p5_tpa) 1232 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1233 else 1234 agg = bnxt_get_agg(bp, cpr, idx, i); 1235 cons = agg->rx_agg_cmp_opaque; 1236 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1237 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1238 1239 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1240 skb_frag_fill_page_desc(frag, cons_rx_buf->page, 1241 cons_rx_buf->offset, frag_len); 1242 shinfo->nr_frags = i + 1; 1243 __clear_bit(cons, rxr->rx_agg_bmap); 1244 1245 /* It is possible for bnxt_alloc_rx_page() to allocate 1246 * a sw_prod index that equals the cons index, so we 1247 * need to clear the cons entry now. 1248 */ 1249 mapping = cons_rx_buf->mapping; 1250 page = cons_rx_buf->page; 1251 cons_rx_buf->page = NULL; 1252 1253 if (xdp && page_is_pfmemalloc(page)) 1254 xdp_buff_set_frag_pfmemalloc(xdp); 1255 1256 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) { 1257 --shinfo->nr_frags; 1258 cons_rx_buf->page = page; 1259 1260 /* Update prod since possibly some pages have been 1261 * allocated already. 1262 */ 1263 rxr->rx_agg_prod = prod; 1264 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1265 return 0; 1266 } 1267 1268 dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE, 1269 bp->rx_dir); 1270 1271 total_frag_len += frag_len; 1272 prod = NEXT_RX_AGG(prod); 1273 } 1274 rxr->rx_agg_prod = prod; 1275 return total_frag_len; 1276 } 1277 1278 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp, 1279 struct bnxt_cp_ring_info *cpr, 1280 struct sk_buff *skb, u16 idx, 1281 u32 agg_bufs, bool tpa) 1282 { 1283 struct skb_shared_info *shinfo = skb_shinfo(skb); 1284 u32 total_frag_len = 0; 1285 1286 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx, 1287 agg_bufs, tpa, NULL); 1288 if (!total_frag_len) { 1289 skb_mark_for_recycle(skb); 1290 dev_kfree_skb(skb); 1291 return NULL; 1292 } 1293 1294 skb->data_len += total_frag_len; 1295 skb->len += total_frag_len; 1296 skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs; 1297 return skb; 1298 } 1299 1300 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp, 1301 struct bnxt_cp_ring_info *cpr, 1302 struct xdp_buff *xdp, u16 idx, 1303 u32 agg_bufs, bool tpa) 1304 { 1305 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1306 u32 total_frag_len = 0; 1307 1308 if (!xdp_buff_has_frags(xdp)) 1309 shinfo->nr_frags = 0; 1310 1311 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, 1312 idx, agg_bufs, tpa, xdp); 1313 if (total_frag_len) { 1314 xdp_buff_set_frags_flag(xdp); 1315 shinfo->nr_frags = agg_bufs; 1316 shinfo->xdp_frags_size = total_frag_len; 1317 } 1318 return total_frag_len; 1319 } 1320 1321 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1322 u8 agg_bufs, u32 *raw_cons) 1323 { 1324 u16 last; 1325 struct rx_agg_cmp *agg; 1326 1327 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1328 last = RING_CMP(*raw_cons); 1329 agg = (struct rx_agg_cmp *) 1330 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1331 return RX_AGG_CMP_VALID(agg, *raw_cons); 1332 } 1333 1334 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1335 unsigned int len, 1336 dma_addr_t mapping) 1337 { 1338 struct bnxt *bp = bnapi->bp; 1339 struct pci_dev *pdev = bp->pdev; 1340 struct sk_buff *skb; 1341 1342 skb = napi_alloc_skb(&bnapi->napi, len); 1343 if (!skb) 1344 return NULL; 1345 1346 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh, 1347 bp->rx_dir); 1348 1349 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1350 len + NET_IP_ALIGN); 1351 1352 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh, 1353 bp->rx_dir); 1354 1355 skb_put(skb, len); 1356 1357 return skb; 1358 } 1359 1360 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1361 unsigned int len, 1362 dma_addr_t mapping) 1363 { 1364 return bnxt_copy_data(bnapi, data, len, mapping); 1365 } 1366 1367 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1368 struct xdp_buff *xdp, 1369 unsigned int len, 1370 dma_addr_t mapping) 1371 { 1372 unsigned int metasize = 0; 1373 u8 *data = xdp->data; 1374 struct sk_buff *skb; 1375 1376 len = xdp->data_end - xdp->data_meta; 1377 metasize = xdp->data - xdp->data_meta; 1378 data = xdp->data_meta; 1379 1380 skb = bnxt_copy_data(bnapi, data, len, mapping); 1381 if (!skb) 1382 return skb; 1383 1384 if (metasize) { 1385 skb_metadata_set(skb, metasize); 1386 __skb_pull(skb, metasize); 1387 } 1388 1389 return skb; 1390 } 1391 1392 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1393 u32 *raw_cons, void *cmp) 1394 { 1395 struct rx_cmp *rxcmp = cmp; 1396 u32 tmp_raw_cons = *raw_cons; 1397 u8 cmp_type, agg_bufs = 0; 1398 1399 cmp_type = RX_CMP_TYPE(rxcmp); 1400 1401 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1402 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1403 RX_CMP_AGG_BUFS) >> 1404 RX_CMP_AGG_BUFS_SHIFT; 1405 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1406 struct rx_tpa_end_cmp *tpa_end = cmp; 1407 1408 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1409 return 0; 1410 1411 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1412 } 1413 1414 if (agg_bufs) { 1415 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1416 return -EBUSY; 1417 } 1418 *raw_cons = tmp_raw_cons; 1419 return 0; 1420 } 1421 1422 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1423 { 1424 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1425 u16 idx = agg_id & MAX_TPA_P5_MASK; 1426 1427 if (test_bit(idx, map->agg_idx_bmap)) 1428 idx = find_first_zero_bit(map->agg_idx_bmap, 1429 BNXT_AGG_IDX_BMAP_SIZE); 1430 __set_bit(idx, map->agg_idx_bmap); 1431 map->agg_id_tbl[agg_id] = idx; 1432 return idx; 1433 } 1434 1435 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1436 { 1437 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1438 1439 __clear_bit(idx, map->agg_idx_bmap); 1440 } 1441 1442 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1443 { 1444 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1445 1446 return map->agg_id_tbl[agg_id]; 1447 } 1448 1449 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1450 struct rx_tpa_start_cmp *tpa_start, 1451 struct rx_tpa_start_cmp_ext *tpa_start1) 1452 { 1453 tpa_info->cfa_code_valid = 1; 1454 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1455 tpa_info->vlan_valid = 0; 1456 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1457 tpa_info->vlan_valid = 1; 1458 tpa_info->metadata = 1459 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1460 } 1461 } 1462 1463 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1464 struct rx_tpa_start_cmp *tpa_start, 1465 struct rx_tpa_start_cmp_ext *tpa_start1) 1466 { 1467 tpa_info->vlan_valid = 0; 1468 if (TPA_START_VLAN_VALID(tpa_start)) { 1469 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1470 u32 vlan_proto = ETH_P_8021Q; 1471 1472 tpa_info->vlan_valid = 1; 1473 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1474 vlan_proto = ETH_P_8021AD; 1475 tpa_info->metadata = vlan_proto << 16 | 1476 TPA_START_METADATA0_TCI(tpa_start1); 1477 } 1478 } 1479 1480 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1481 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1482 struct rx_tpa_start_cmp_ext *tpa_start1) 1483 { 1484 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1485 struct bnxt_tpa_info *tpa_info; 1486 u16 cons, prod, agg_id; 1487 struct rx_bd *prod_bd; 1488 dma_addr_t mapping; 1489 1490 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1491 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1492 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1493 } else { 1494 agg_id = TPA_START_AGG_ID(tpa_start); 1495 } 1496 cons = tpa_start->rx_tpa_start_cmp_opaque; 1497 prod = rxr->rx_prod; 1498 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1499 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1500 tpa_info = &rxr->rx_tpa[agg_id]; 1501 1502 if (unlikely(cons != rxr->rx_next_cons || 1503 TPA_START_ERROR(tpa_start))) { 1504 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1505 cons, rxr->rx_next_cons, 1506 TPA_START_ERROR_CODE(tpa_start1)); 1507 bnxt_sched_reset_rxr(bp, rxr); 1508 return; 1509 } 1510 prod_rx_buf->data = tpa_info->data; 1511 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1512 1513 mapping = tpa_info->mapping; 1514 prod_rx_buf->mapping = mapping; 1515 1516 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1517 1518 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1519 1520 tpa_info->data = cons_rx_buf->data; 1521 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1522 cons_rx_buf->data = NULL; 1523 tpa_info->mapping = cons_rx_buf->mapping; 1524 1525 tpa_info->len = 1526 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1527 RX_TPA_START_CMP_LEN_SHIFT; 1528 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1529 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1530 tpa_info->gso_type = SKB_GSO_TCPV4; 1531 if (TPA_START_IS_IPV6(tpa_start1)) 1532 tpa_info->gso_type = SKB_GSO_TCPV6; 1533 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1534 else if (!BNXT_CHIP_P4_PLUS(bp) && 1535 TPA_START_HASH_TYPE(tpa_start) == 3) 1536 tpa_info->gso_type = SKB_GSO_TCPV6; 1537 tpa_info->rss_hash = 1538 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1539 } else { 1540 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1541 tpa_info->gso_type = 0; 1542 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1543 } 1544 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1545 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1546 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1547 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1548 else 1549 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1550 tpa_info->agg_count = 0; 1551 1552 rxr->rx_prod = NEXT_RX(prod); 1553 cons = RING_RX(bp, NEXT_RX(cons)); 1554 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1555 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1556 1557 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1558 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1559 cons_rx_buf->data = NULL; 1560 } 1561 1562 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1563 { 1564 if (agg_bufs) 1565 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1566 } 1567 1568 #ifdef CONFIG_INET 1569 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1570 { 1571 struct udphdr *uh = NULL; 1572 1573 if (ip_proto == htons(ETH_P_IP)) { 1574 struct iphdr *iph = (struct iphdr *)skb->data; 1575 1576 if (iph->protocol == IPPROTO_UDP) 1577 uh = (struct udphdr *)(iph + 1); 1578 } else { 1579 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1580 1581 if (iph->nexthdr == IPPROTO_UDP) 1582 uh = (struct udphdr *)(iph + 1); 1583 } 1584 if (uh) { 1585 if (uh->check) 1586 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1587 else 1588 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1589 } 1590 } 1591 #endif 1592 1593 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1594 int payload_off, int tcp_ts, 1595 struct sk_buff *skb) 1596 { 1597 #ifdef CONFIG_INET 1598 struct tcphdr *th; 1599 int len, nw_off; 1600 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1601 u32 hdr_info = tpa_info->hdr_info; 1602 bool loopback = false; 1603 1604 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1605 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1606 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1607 1608 /* If the packet is an internal loopback packet, the offsets will 1609 * have an extra 4 bytes. 1610 */ 1611 if (inner_mac_off == 4) { 1612 loopback = true; 1613 } else if (inner_mac_off > 4) { 1614 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1615 ETH_HLEN - 2)); 1616 1617 /* We only support inner iPv4/ipv6. If we don't see the 1618 * correct protocol ID, it must be a loopback packet where 1619 * the offsets are off by 4. 1620 */ 1621 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1622 loopback = true; 1623 } 1624 if (loopback) { 1625 /* internal loopback packet, subtract all offsets by 4 */ 1626 inner_ip_off -= 4; 1627 inner_mac_off -= 4; 1628 outer_ip_off -= 4; 1629 } 1630 1631 nw_off = inner_ip_off - ETH_HLEN; 1632 skb_set_network_header(skb, nw_off); 1633 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1634 struct ipv6hdr *iph = ipv6_hdr(skb); 1635 1636 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1637 len = skb->len - skb_transport_offset(skb); 1638 th = tcp_hdr(skb); 1639 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1640 } else { 1641 struct iphdr *iph = ip_hdr(skb); 1642 1643 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1644 len = skb->len - skb_transport_offset(skb); 1645 th = tcp_hdr(skb); 1646 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1647 } 1648 1649 if (inner_mac_off) { /* tunnel */ 1650 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1651 ETH_HLEN - 2)); 1652 1653 bnxt_gro_tunnel(skb, proto); 1654 } 1655 #endif 1656 return skb; 1657 } 1658 1659 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1660 int payload_off, int tcp_ts, 1661 struct sk_buff *skb) 1662 { 1663 #ifdef CONFIG_INET 1664 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1665 u32 hdr_info = tpa_info->hdr_info; 1666 int iphdr_len, nw_off; 1667 1668 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1669 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1670 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1671 1672 nw_off = inner_ip_off - ETH_HLEN; 1673 skb_set_network_header(skb, nw_off); 1674 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1675 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1676 skb_set_transport_header(skb, nw_off + iphdr_len); 1677 1678 if (inner_mac_off) { /* tunnel */ 1679 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1680 ETH_HLEN - 2)); 1681 1682 bnxt_gro_tunnel(skb, proto); 1683 } 1684 #endif 1685 return skb; 1686 } 1687 1688 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1689 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1690 1691 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1692 int payload_off, int tcp_ts, 1693 struct sk_buff *skb) 1694 { 1695 #ifdef CONFIG_INET 1696 struct tcphdr *th; 1697 int len, nw_off, tcp_opt_len = 0; 1698 1699 if (tcp_ts) 1700 tcp_opt_len = 12; 1701 1702 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1703 struct iphdr *iph; 1704 1705 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1706 ETH_HLEN; 1707 skb_set_network_header(skb, nw_off); 1708 iph = ip_hdr(skb); 1709 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1710 len = skb->len - skb_transport_offset(skb); 1711 th = tcp_hdr(skb); 1712 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1713 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1714 struct ipv6hdr *iph; 1715 1716 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1717 ETH_HLEN; 1718 skb_set_network_header(skb, nw_off); 1719 iph = ipv6_hdr(skb); 1720 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1721 len = skb->len - skb_transport_offset(skb); 1722 th = tcp_hdr(skb); 1723 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1724 } else { 1725 dev_kfree_skb_any(skb); 1726 return NULL; 1727 } 1728 1729 if (nw_off) /* tunnel */ 1730 bnxt_gro_tunnel(skb, skb->protocol); 1731 #endif 1732 return skb; 1733 } 1734 1735 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1736 struct bnxt_tpa_info *tpa_info, 1737 struct rx_tpa_end_cmp *tpa_end, 1738 struct rx_tpa_end_cmp_ext *tpa_end1, 1739 struct sk_buff *skb) 1740 { 1741 #ifdef CONFIG_INET 1742 int payload_off; 1743 u16 segs; 1744 1745 segs = TPA_END_TPA_SEGS(tpa_end); 1746 if (segs == 1) 1747 return skb; 1748 1749 NAPI_GRO_CB(skb)->count = segs; 1750 skb_shinfo(skb)->gso_size = 1751 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1752 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1753 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1754 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1755 else 1756 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1757 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1758 if (likely(skb)) 1759 tcp_gro_complete(skb); 1760 #endif 1761 return skb; 1762 } 1763 1764 /* Given the cfa_code of a received packet determine which 1765 * netdev (vf-rep or PF) the packet is destined to. 1766 */ 1767 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1768 { 1769 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1770 1771 /* if vf-rep dev is NULL, the must belongs to the PF */ 1772 return dev ? dev : bp->dev; 1773 } 1774 1775 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1776 struct bnxt_cp_ring_info *cpr, 1777 u32 *raw_cons, 1778 struct rx_tpa_end_cmp *tpa_end, 1779 struct rx_tpa_end_cmp_ext *tpa_end1, 1780 u8 *event) 1781 { 1782 struct bnxt_napi *bnapi = cpr->bnapi; 1783 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1784 struct net_device *dev = bp->dev; 1785 u8 *data_ptr, agg_bufs; 1786 unsigned int len; 1787 struct bnxt_tpa_info *tpa_info; 1788 dma_addr_t mapping; 1789 struct sk_buff *skb; 1790 u16 idx = 0, agg_id; 1791 void *data; 1792 bool gro; 1793 1794 if (unlikely(bnapi->in_reset)) { 1795 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1796 1797 if (rc < 0) 1798 return ERR_PTR(-EBUSY); 1799 return NULL; 1800 } 1801 1802 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1803 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1804 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1805 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1806 tpa_info = &rxr->rx_tpa[agg_id]; 1807 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1808 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1809 agg_bufs, tpa_info->agg_count); 1810 agg_bufs = tpa_info->agg_count; 1811 } 1812 tpa_info->agg_count = 0; 1813 *event |= BNXT_AGG_EVENT; 1814 bnxt_free_agg_idx(rxr, agg_id); 1815 idx = agg_id; 1816 gro = !!(bp->flags & BNXT_FLAG_GRO); 1817 } else { 1818 agg_id = TPA_END_AGG_ID(tpa_end); 1819 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1820 tpa_info = &rxr->rx_tpa[agg_id]; 1821 idx = RING_CMP(*raw_cons); 1822 if (agg_bufs) { 1823 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1824 return ERR_PTR(-EBUSY); 1825 1826 *event |= BNXT_AGG_EVENT; 1827 idx = NEXT_CMP(idx); 1828 } 1829 gro = !!TPA_END_GRO(tpa_end); 1830 } 1831 data = tpa_info->data; 1832 data_ptr = tpa_info->data_ptr; 1833 prefetch(data_ptr); 1834 len = tpa_info->len; 1835 mapping = tpa_info->mapping; 1836 1837 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1838 bnxt_abort_tpa(cpr, idx, agg_bufs); 1839 if (agg_bufs > MAX_SKB_FRAGS) 1840 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1841 agg_bufs, (int)MAX_SKB_FRAGS); 1842 return NULL; 1843 } 1844 1845 if (len <= bp->rx_copy_thresh) { 1846 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1847 if (!skb) { 1848 bnxt_abort_tpa(cpr, idx, agg_bufs); 1849 cpr->sw_stats->rx.rx_oom_discards += 1; 1850 return NULL; 1851 } 1852 } else { 1853 u8 *new_data; 1854 dma_addr_t new_mapping; 1855 1856 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1857 GFP_ATOMIC); 1858 if (!new_data) { 1859 bnxt_abort_tpa(cpr, idx, agg_bufs); 1860 cpr->sw_stats->rx.rx_oom_discards += 1; 1861 return NULL; 1862 } 1863 1864 tpa_info->data = new_data; 1865 tpa_info->data_ptr = new_data + bp->rx_offset; 1866 tpa_info->mapping = new_mapping; 1867 1868 skb = napi_build_skb(data, bp->rx_buf_size); 1869 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 1870 bp->rx_buf_use_size, bp->rx_dir); 1871 1872 if (!skb) { 1873 page_pool_free_va(rxr->head_pool, data, true); 1874 bnxt_abort_tpa(cpr, idx, agg_bufs); 1875 cpr->sw_stats->rx.rx_oom_discards += 1; 1876 return NULL; 1877 } 1878 skb_mark_for_recycle(skb); 1879 skb_reserve(skb, bp->rx_offset); 1880 skb_put(skb, len); 1881 } 1882 1883 if (agg_bufs) { 1884 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true); 1885 if (!skb) { 1886 /* Page reuse already handled by bnxt_rx_pages(). */ 1887 cpr->sw_stats->rx.rx_oom_discards += 1; 1888 return NULL; 1889 } 1890 } 1891 1892 if (tpa_info->cfa_code_valid) 1893 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1894 skb->protocol = eth_type_trans(skb, dev); 1895 1896 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1897 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1898 1899 if (tpa_info->vlan_valid && 1900 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1901 __be16 vlan_proto = htons(tpa_info->metadata >> 1902 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1903 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1904 1905 if (eth_type_vlan(vlan_proto)) { 1906 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1907 } else { 1908 dev_kfree_skb(skb); 1909 return NULL; 1910 } 1911 } 1912 1913 skb_checksum_none_assert(skb); 1914 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1915 skb->ip_summed = CHECKSUM_UNNECESSARY; 1916 skb->csum_level = 1917 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1918 } 1919 1920 if (gro) 1921 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1922 1923 return skb; 1924 } 1925 1926 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1927 struct rx_agg_cmp *rx_agg) 1928 { 1929 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1930 struct bnxt_tpa_info *tpa_info; 1931 1932 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1933 tpa_info = &rxr->rx_tpa[agg_id]; 1934 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1935 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1936 } 1937 1938 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1939 struct sk_buff *skb) 1940 { 1941 skb_mark_for_recycle(skb); 1942 1943 if (skb->dev != bp->dev) { 1944 /* this packet belongs to a vf-rep */ 1945 bnxt_vf_rep_rx(bp, skb); 1946 return; 1947 } 1948 skb_record_rx_queue(skb, bnapi->index); 1949 napi_gro_receive(&bnapi->napi, skb); 1950 } 1951 1952 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 1953 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 1954 { 1955 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 1956 1957 if (BNXT_PTP_RX_TS_VALID(flags)) 1958 goto ts_valid; 1959 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 1960 return false; 1961 1962 ts_valid: 1963 *cmpl_ts = ts; 1964 return true; 1965 } 1966 1967 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 1968 struct rx_cmp *rxcmp, 1969 struct rx_cmp_ext *rxcmp1) 1970 { 1971 __be16 vlan_proto; 1972 u16 vtag; 1973 1974 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1975 __le32 flags2 = rxcmp1->rx_cmp_flags2; 1976 u32 meta_data; 1977 1978 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 1979 return skb; 1980 1981 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 1982 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1983 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 1984 if (eth_type_vlan(vlan_proto)) 1985 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1986 else 1987 goto vlan_err; 1988 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 1989 if (RX_CMP_VLAN_VALID(rxcmp)) { 1990 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 1991 1992 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 1993 vlan_proto = htons(ETH_P_8021Q); 1994 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 1995 vlan_proto = htons(ETH_P_8021AD); 1996 else 1997 goto vlan_err; 1998 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 1999 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2000 } 2001 } 2002 return skb; 2003 vlan_err: 2004 dev_kfree_skb(skb); 2005 return NULL; 2006 } 2007 2008 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 2009 struct rx_cmp *rxcmp) 2010 { 2011 u8 ext_op; 2012 2013 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2014 switch (ext_op) { 2015 case EXT_OP_INNER_4: 2016 case EXT_OP_OUTER_4: 2017 case EXT_OP_INNFL_3: 2018 case EXT_OP_OUTFL_3: 2019 return PKT_HASH_TYPE_L4; 2020 default: 2021 return PKT_HASH_TYPE_L3; 2022 } 2023 } 2024 2025 /* returns the following: 2026 * 1 - 1 packet successfully received 2027 * 0 - successful TPA_START, packet not completed yet 2028 * -EBUSY - completion ring does not have all the agg buffers yet 2029 * -ENOMEM - packet aborted due to out of memory 2030 * -EIO - packet aborted due to hw error indicated in BD 2031 */ 2032 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2033 u32 *raw_cons, u8 *event) 2034 { 2035 struct bnxt_napi *bnapi = cpr->bnapi; 2036 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2037 struct net_device *dev = bp->dev; 2038 struct rx_cmp *rxcmp; 2039 struct rx_cmp_ext *rxcmp1; 2040 u32 tmp_raw_cons = *raw_cons; 2041 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2042 struct bnxt_sw_rx_bd *rx_buf; 2043 unsigned int len; 2044 u8 *data_ptr, agg_bufs, cmp_type; 2045 bool xdp_active = false; 2046 dma_addr_t dma_addr; 2047 struct sk_buff *skb; 2048 struct xdp_buff xdp; 2049 u32 flags, misc; 2050 u32 cmpl_ts; 2051 void *data; 2052 int rc = 0; 2053 2054 rxcmp = (struct rx_cmp *) 2055 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2056 2057 cmp_type = RX_CMP_TYPE(rxcmp); 2058 2059 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2060 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2061 goto next_rx_no_prod_no_len; 2062 } 2063 2064 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2065 cp_cons = RING_CMP(tmp_raw_cons); 2066 rxcmp1 = (struct rx_cmp_ext *) 2067 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2068 2069 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2070 return -EBUSY; 2071 2072 /* The valid test of the entry must be done first before 2073 * reading any further. 2074 */ 2075 dma_rmb(); 2076 prod = rxr->rx_prod; 2077 2078 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2079 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2080 bnxt_tpa_start(bp, rxr, cmp_type, 2081 (struct rx_tpa_start_cmp *)rxcmp, 2082 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2083 2084 *event |= BNXT_RX_EVENT; 2085 goto next_rx_no_prod_no_len; 2086 2087 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2088 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2089 (struct rx_tpa_end_cmp *)rxcmp, 2090 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2091 2092 if (IS_ERR(skb)) 2093 return -EBUSY; 2094 2095 rc = -ENOMEM; 2096 if (likely(skb)) { 2097 bnxt_deliver_skb(bp, bnapi, skb); 2098 rc = 1; 2099 } 2100 *event |= BNXT_RX_EVENT; 2101 goto next_rx_no_prod_no_len; 2102 } 2103 2104 cons = rxcmp->rx_cmp_opaque; 2105 if (unlikely(cons != rxr->rx_next_cons)) { 2106 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2107 2108 /* 0xffff is forced error, don't print it */ 2109 if (rxr->rx_next_cons != 0xffff) 2110 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2111 cons, rxr->rx_next_cons); 2112 bnxt_sched_reset_rxr(bp, rxr); 2113 if (rc1) 2114 return rc1; 2115 goto next_rx_no_prod_no_len; 2116 } 2117 rx_buf = &rxr->rx_buf_ring[cons]; 2118 data = rx_buf->data; 2119 data_ptr = rx_buf->data_ptr; 2120 prefetch(data_ptr); 2121 2122 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2123 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2124 2125 if (agg_bufs) { 2126 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2127 return -EBUSY; 2128 2129 cp_cons = NEXT_CMP(cp_cons); 2130 *event |= BNXT_AGG_EVENT; 2131 } 2132 *event |= BNXT_RX_EVENT; 2133 2134 rx_buf->data = NULL; 2135 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2136 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2137 2138 bnxt_reuse_rx_data(rxr, cons, data); 2139 if (agg_bufs) 2140 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2141 false); 2142 2143 rc = -EIO; 2144 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2145 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2146 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2147 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2148 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2149 rx_err); 2150 bnxt_sched_reset_rxr(bp, rxr); 2151 } 2152 } 2153 goto next_rx_no_len; 2154 } 2155 2156 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2157 len = flags >> RX_CMP_LEN_SHIFT; 2158 dma_addr = rx_buf->mapping; 2159 2160 if (bnxt_xdp_attached(bp, rxr)) { 2161 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2162 if (agg_bufs) { 2163 u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp, 2164 cp_cons, agg_bufs, 2165 false); 2166 if (!frag_len) 2167 goto oom_next_rx; 2168 } 2169 xdp_active = true; 2170 } 2171 2172 if (xdp_active) { 2173 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2174 rc = 1; 2175 goto next_rx; 2176 } 2177 } 2178 2179 if (len <= bp->rx_copy_thresh) { 2180 if (!xdp_active) 2181 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2182 else 2183 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2184 bnxt_reuse_rx_data(rxr, cons, data); 2185 if (!skb) { 2186 if (agg_bufs) { 2187 if (!xdp_active) 2188 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2189 agg_bufs, false); 2190 else 2191 bnxt_xdp_buff_frags_free(rxr, &xdp); 2192 } 2193 goto oom_next_rx; 2194 } 2195 } else { 2196 u32 payload; 2197 2198 if (rx_buf->data_ptr == data_ptr) 2199 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2200 else 2201 payload = 0; 2202 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2203 payload | len); 2204 if (!skb) 2205 goto oom_next_rx; 2206 } 2207 2208 if (agg_bufs) { 2209 if (!xdp_active) { 2210 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false); 2211 if (!skb) 2212 goto oom_next_rx; 2213 } else { 2214 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1); 2215 if (!skb) { 2216 /* we should be able to free the old skb here */ 2217 bnxt_xdp_buff_frags_free(rxr, &xdp); 2218 goto oom_next_rx; 2219 } 2220 } 2221 } 2222 2223 if (RX_CMP_HASH_VALID(rxcmp)) { 2224 enum pkt_hash_types type; 2225 2226 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2227 type = bnxt_rss_ext_op(bp, rxcmp); 2228 } else { 2229 u32 itypes = RX_CMP_ITYPES(rxcmp); 2230 2231 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2232 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2233 type = PKT_HASH_TYPE_L4; 2234 else 2235 type = PKT_HASH_TYPE_L3; 2236 } 2237 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2238 } 2239 2240 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2241 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2242 skb->protocol = eth_type_trans(skb, dev); 2243 2244 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2245 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2246 if (!skb) 2247 goto next_rx; 2248 } 2249 2250 skb_checksum_none_assert(skb); 2251 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2252 if (dev->features & NETIF_F_RXCSUM) { 2253 skb->ip_summed = CHECKSUM_UNNECESSARY; 2254 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2255 } 2256 } else { 2257 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2258 if (dev->features & NETIF_F_RXCSUM) 2259 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2260 } 2261 } 2262 2263 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2264 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2265 u64 ns, ts; 2266 2267 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2268 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2269 2270 ns = bnxt_timecounter_cyc2time(ptp, ts); 2271 memset(skb_hwtstamps(skb), 0, 2272 sizeof(*skb_hwtstamps(skb))); 2273 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2274 } 2275 } 2276 } 2277 bnxt_deliver_skb(bp, bnapi, skb); 2278 rc = 1; 2279 2280 next_rx: 2281 cpr->rx_packets += 1; 2282 cpr->rx_bytes += len; 2283 2284 next_rx_no_len: 2285 rxr->rx_prod = NEXT_RX(prod); 2286 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2287 2288 next_rx_no_prod_no_len: 2289 *raw_cons = tmp_raw_cons; 2290 2291 return rc; 2292 2293 oom_next_rx: 2294 cpr->sw_stats->rx.rx_oom_discards += 1; 2295 rc = -ENOMEM; 2296 goto next_rx; 2297 } 2298 2299 /* In netpoll mode, if we are using a combined completion ring, we need to 2300 * discard the rx packets and recycle the buffers. 2301 */ 2302 static int bnxt_force_rx_discard(struct bnxt *bp, 2303 struct bnxt_cp_ring_info *cpr, 2304 u32 *raw_cons, u8 *event) 2305 { 2306 u32 tmp_raw_cons = *raw_cons; 2307 struct rx_cmp_ext *rxcmp1; 2308 struct rx_cmp *rxcmp; 2309 u16 cp_cons; 2310 u8 cmp_type; 2311 int rc; 2312 2313 cp_cons = RING_CMP(tmp_raw_cons); 2314 rxcmp = (struct rx_cmp *) 2315 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2316 2317 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2318 cp_cons = RING_CMP(tmp_raw_cons); 2319 rxcmp1 = (struct rx_cmp_ext *) 2320 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2321 2322 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2323 return -EBUSY; 2324 2325 /* The valid test of the entry must be done first before 2326 * reading any further. 2327 */ 2328 dma_rmb(); 2329 cmp_type = RX_CMP_TYPE(rxcmp); 2330 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2331 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2332 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2333 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2334 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2335 struct rx_tpa_end_cmp_ext *tpa_end1; 2336 2337 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2338 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2339 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2340 } 2341 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2342 if (rc && rc != -EBUSY) 2343 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2344 return rc; 2345 } 2346 2347 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2348 { 2349 struct bnxt_fw_health *fw_health = bp->fw_health; 2350 u32 reg = fw_health->regs[reg_idx]; 2351 u32 reg_type, reg_off, val = 0; 2352 2353 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2354 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2355 switch (reg_type) { 2356 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2357 pci_read_config_dword(bp->pdev, reg_off, &val); 2358 break; 2359 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2360 reg_off = fw_health->mapped_regs[reg_idx]; 2361 fallthrough; 2362 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2363 val = readl(bp->bar0 + reg_off); 2364 break; 2365 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2366 val = readl(bp->bar1 + reg_off); 2367 break; 2368 } 2369 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2370 val &= fw_health->fw_reset_inprog_reg_mask; 2371 return val; 2372 } 2373 2374 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2375 { 2376 int i; 2377 2378 for (i = 0; i < bp->rx_nr_rings; i++) { 2379 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2380 struct bnxt_ring_grp_info *grp_info; 2381 2382 grp_info = &bp->grp_info[grp_idx]; 2383 if (grp_info->agg_fw_ring_id == ring_id) 2384 return grp_idx; 2385 } 2386 return INVALID_HW_RING_ID; 2387 } 2388 2389 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2390 { 2391 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2392 2393 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2394 return link_info->force_link_speed2; 2395 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2396 return link_info->force_pam4_link_speed; 2397 return link_info->force_link_speed; 2398 } 2399 2400 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2401 { 2402 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2403 2404 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2405 link_info->req_link_speed = link_info->force_link_speed2; 2406 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2407 switch (link_info->req_link_speed) { 2408 case BNXT_LINK_SPEED_50GB_PAM4: 2409 case BNXT_LINK_SPEED_100GB_PAM4: 2410 case BNXT_LINK_SPEED_200GB_PAM4: 2411 case BNXT_LINK_SPEED_400GB_PAM4: 2412 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2413 break; 2414 case BNXT_LINK_SPEED_100GB_PAM4_112: 2415 case BNXT_LINK_SPEED_200GB_PAM4_112: 2416 case BNXT_LINK_SPEED_400GB_PAM4_112: 2417 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2418 break; 2419 default: 2420 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2421 } 2422 return; 2423 } 2424 link_info->req_link_speed = link_info->force_link_speed; 2425 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2426 if (link_info->force_pam4_link_speed) { 2427 link_info->req_link_speed = link_info->force_pam4_link_speed; 2428 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2429 } 2430 } 2431 2432 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2433 { 2434 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2435 2436 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2437 link_info->advertising = link_info->auto_link_speeds2; 2438 return; 2439 } 2440 link_info->advertising = link_info->auto_link_speeds; 2441 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2442 } 2443 2444 static bool bnxt_force_speed_updated(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 if (link_info->req_link_speed != link_info->force_link_speed2) 2450 return true; 2451 return false; 2452 } 2453 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2454 link_info->req_link_speed != link_info->force_link_speed) 2455 return true; 2456 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2457 link_info->req_link_speed != link_info->force_pam4_link_speed) 2458 return true; 2459 return false; 2460 } 2461 2462 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2463 { 2464 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2465 2466 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2467 if (link_info->advertising != link_info->auto_link_speeds2) 2468 return true; 2469 return false; 2470 } 2471 if (link_info->advertising != link_info->auto_link_speeds || 2472 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2473 return true; 2474 return false; 2475 } 2476 2477 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2478 { 2479 u32 flags = bp->ctx->ctx_arr[type].flags; 2480 2481 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2482 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2483 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2484 } 2485 2486 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2487 { 2488 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2489 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2490 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2491 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2492 struct bnxt_bs_trace_info *bs_trace; 2493 int last_pg; 2494 2495 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2496 return; 2497 2498 mem_size = ctxm->max_entries * ctxm->entry_size; 2499 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2500 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2501 2502 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2503 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2504 2505 rmem = &ctx_pg[0].ring_mem; 2506 bs_trace = &bp->bs_trace[trace_type]; 2507 bs_trace->ctx_type = ctxm->type; 2508 bs_trace->trace_type = trace_type; 2509 if (pages > MAX_CTX_PAGES) { 2510 int last_pg_dir = rmem->nr_pages - 1; 2511 2512 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2513 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2514 } else { 2515 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2516 } 2517 bs_trace->magic_byte += magic_byte_offset; 2518 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2519 } 2520 2521 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2522 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2523 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2524 2525 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2526 (((data2) & \ 2527 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2528 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2529 2530 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2531 ((data2) & \ 2532 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2533 2534 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2535 (((data2) & \ 2536 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2537 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2538 2539 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2540 ((data1) & \ 2541 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2542 2543 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2544 (((data1) & \ 2545 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2546 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2547 2548 /* Return true if the workqueue has to be scheduled */ 2549 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2550 { 2551 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2552 2553 switch (err_type) { 2554 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2555 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2556 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2557 break; 2558 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2559 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2560 break; 2561 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2562 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2563 break; 2564 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2565 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2566 char *threshold_type; 2567 bool notify = false; 2568 char *dir_str; 2569 2570 switch (type) { 2571 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2572 threshold_type = "warning"; 2573 break; 2574 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2575 threshold_type = "critical"; 2576 break; 2577 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2578 threshold_type = "fatal"; 2579 break; 2580 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2581 threshold_type = "shutdown"; 2582 break; 2583 default: 2584 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2585 return false; 2586 } 2587 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2588 dir_str = "above"; 2589 notify = true; 2590 } else { 2591 dir_str = "below"; 2592 } 2593 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2594 dir_str, threshold_type); 2595 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2596 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2597 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2598 if (notify) { 2599 bp->thermal_threshold_type = type; 2600 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2601 return true; 2602 } 2603 return false; 2604 } 2605 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2606 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2607 break; 2608 default: 2609 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2610 err_type); 2611 break; 2612 } 2613 return false; 2614 } 2615 2616 #define BNXT_GET_EVENT_PORT(data) \ 2617 ((data) & \ 2618 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2619 2620 #define BNXT_EVENT_RING_TYPE(data2) \ 2621 ((data2) & \ 2622 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2623 2624 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2625 (BNXT_EVENT_RING_TYPE(data2) == \ 2626 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2627 2628 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2629 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2630 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2631 2632 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2633 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2634 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2635 2636 #define BNXT_PHC_BITS 48 2637 2638 static int bnxt_async_event_process(struct bnxt *bp, 2639 struct hwrm_async_event_cmpl *cmpl) 2640 { 2641 u16 event_id = le16_to_cpu(cmpl->event_id); 2642 u32 data1 = le32_to_cpu(cmpl->event_data1); 2643 u32 data2 = le32_to_cpu(cmpl->event_data2); 2644 2645 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2646 event_id, data1, data2); 2647 2648 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2649 switch (event_id) { 2650 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2651 struct bnxt_link_info *link_info = &bp->link_info; 2652 2653 if (BNXT_VF(bp)) 2654 goto async_event_process_exit; 2655 2656 /* print unsupported speed warning in forced speed mode only */ 2657 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2658 (data1 & 0x20000)) { 2659 u16 fw_speed = bnxt_get_force_speed(link_info); 2660 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2661 2662 if (speed != SPEED_UNKNOWN) 2663 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2664 speed); 2665 } 2666 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2667 } 2668 fallthrough; 2669 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2670 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2671 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2672 fallthrough; 2673 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2674 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2675 break; 2676 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2677 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2678 break; 2679 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2680 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2681 2682 if (BNXT_VF(bp)) 2683 break; 2684 2685 if (bp->pf.port_id != port_id) 2686 break; 2687 2688 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2689 break; 2690 } 2691 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2692 if (BNXT_PF(bp)) 2693 goto async_event_process_exit; 2694 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2695 break; 2696 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2697 char *type_str = "Solicited"; 2698 2699 if (!bp->fw_health) 2700 goto async_event_process_exit; 2701 2702 bp->fw_reset_timestamp = jiffies; 2703 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2704 if (!bp->fw_reset_min_dsecs) 2705 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2706 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2707 if (!bp->fw_reset_max_dsecs) 2708 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2709 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2710 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2711 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2712 type_str = "Fatal"; 2713 bp->fw_health->fatalities++; 2714 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2715 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2716 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2717 type_str = "Non-fatal"; 2718 bp->fw_health->survivals++; 2719 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2720 } 2721 netif_warn(bp, hw, bp->dev, 2722 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2723 type_str, data1, data2, 2724 bp->fw_reset_min_dsecs * 100, 2725 bp->fw_reset_max_dsecs * 100); 2726 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2727 break; 2728 } 2729 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2730 struct bnxt_fw_health *fw_health = bp->fw_health; 2731 char *status_desc = "healthy"; 2732 u32 status; 2733 2734 if (!fw_health) 2735 goto async_event_process_exit; 2736 2737 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2738 fw_health->enabled = false; 2739 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2740 break; 2741 } 2742 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2743 fw_health->tmr_multiplier = 2744 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2745 bp->current_interval * 10); 2746 fw_health->tmr_counter = fw_health->tmr_multiplier; 2747 if (!fw_health->enabled) 2748 fw_health->last_fw_heartbeat = 2749 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2750 fw_health->last_fw_reset_cnt = 2751 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2752 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2753 if (status != BNXT_FW_STATUS_HEALTHY) 2754 status_desc = "unhealthy"; 2755 netif_info(bp, drv, bp->dev, 2756 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2757 fw_health->primary ? "primary" : "backup", status, 2758 status_desc, fw_health->last_fw_reset_cnt); 2759 if (!fw_health->enabled) { 2760 /* Make sure tmr_counter is set and visible to 2761 * bnxt_health_check() before setting enabled to true. 2762 */ 2763 smp_wmb(); 2764 fw_health->enabled = true; 2765 } 2766 goto async_event_process_exit; 2767 } 2768 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2769 netif_notice(bp, hw, bp->dev, 2770 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2771 data1, data2); 2772 goto async_event_process_exit; 2773 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2774 struct bnxt_rx_ring_info *rxr; 2775 u16 grp_idx; 2776 2777 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2778 goto async_event_process_exit; 2779 2780 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2781 BNXT_EVENT_RING_TYPE(data2), data1); 2782 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2783 goto async_event_process_exit; 2784 2785 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2786 if (grp_idx == INVALID_HW_RING_ID) { 2787 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2788 data1); 2789 goto async_event_process_exit; 2790 } 2791 rxr = bp->bnapi[grp_idx]->rx_ring; 2792 bnxt_sched_reset_rxr(bp, rxr); 2793 goto async_event_process_exit; 2794 } 2795 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2796 struct bnxt_fw_health *fw_health = bp->fw_health; 2797 2798 netif_notice(bp, hw, bp->dev, 2799 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2800 data1, data2); 2801 if (fw_health) { 2802 fw_health->echo_req_data1 = data1; 2803 fw_health->echo_req_data2 = data2; 2804 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2805 break; 2806 } 2807 goto async_event_process_exit; 2808 } 2809 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2810 bnxt_ptp_pps_event(bp, data1, data2); 2811 goto async_event_process_exit; 2812 } 2813 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2814 if (bnxt_event_error_report(bp, data1, data2)) 2815 break; 2816 goto async_event_process_exit; 2817 } 2818 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2819 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2820 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2821 if (BNXT_PTP_USE_RTC(bp)) { 2822 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2823 unsigned long flags; 2824 u64 ns; 2825 2826 if (!ptp) 2827 goto async_event_process_exit; 2828 2829 bnxt_ptp_update_current_time(bp); 2830 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2831 BNXT_PHC_BITS) | ptp->current_time); 2832 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2833 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2834 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2835 } 2836 break; 2837 } 2838 goto async_event_process_exit; 2839 } 2840 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2841 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2842 2843 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2844 goto async_event_process_exit; 2845 } 2846 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2847 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2848 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2849 2850 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2851 goto async_event_process_exit; 2852 } 2853 default: 2854 goto async_event_process_exit; 2855 } 2856 __bnxt_queue_sp_work(bp); 2857 async_event_process_exit: 2858 return 0; 2859 } 2860 2861 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2862 { 2863 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2864 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2865 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2866 (struct hwrm_fwd_req_cmpl *)txcmp; 2867 2868 switch (cmpl_type) { 2869 case CMPL_BASE_TYPE_HWRM_DONE: 2870 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2871 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2872 break; 2873 2874 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2875 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2876 2877 if ((vf_id < bp->pf.first_vf_id) || 2878 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2879 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2880 vf_id); 2881 return -EINVAL; 2882 } 2883 2884 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2885 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2886 break; 2887 2888 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2889 bnxt_async_event_process(bp, 2890 (struct hwrm_async_event_cmpl *)txcmp); 2891 break; 2892 2893 default: 2894 break; 2895 } 2896 2897 return 0; 2898 } 2899 2900 static bool bnxt_vnic_is_active(struct bnxt *bp) 2901 { 2902 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 2903 2904 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 2905 } 2906 2907 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2908 { 2909 struct bnxt_napi *bnapi = dev_instance; 2910 struct bnxt *bp = bnapi->bp; 2911 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2912 u32 cons = RING_CMP(cpr->cp_raw_cons); 2913 2914 cpr->event_ctr++; 2915 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2916 napi_schedule(&bnapi->napi); 2917 return IRQ_HANDLED; 2918 } 2919 2920 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2921 { 2922 u32 raw_cons = cpr->cp_raw_cons; 2923 u16 cons = RING_CMP(raw_cons); 2924 struct tx_cmp *txcmp; 2925 2926 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2927 2928 return TX_CMP_VALID(txcmp, raw_cons); 2929 } 2930 2931 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2932 int budget) 2933 { 2934 struct bnxt_napi *bnapi = cpr->bnapi; 2935 u32 raw_cons = cpr->cp_raw_cons; 2936 u32 cons; 2937 int rx_pkts = 0; 2938 u8 event = 0; 2939 struct tx_cmp *txcmp; 2940 2941 cpr->has_more_work = 0; 2942 cpr->had_work_done = 1; 2943 while (1) { 2944 u8 cmp_type; 2945 int rc; 2946 2947 cons = RING_CMP(raw_cons); 2948 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2949 2950 if (!TX_CMP_VALID(txcmp, raw_cons)) 2951 break; 2952 2953 /* The valid test of the entry must be done first before 2954 * reading any further. 2955 */ 2956 dma_rmb(); 2957 cmp_type = TX_CMP_TYPE(txcmp); 2958 if (cmp_type == CMP_TYPE_TX_L2_CMP || 2959 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 2960 u32 opaque = txcmp->tx_cmp_opaque; 2961 struct bnxt_tx_ring_info *txr; 2962 u16 tx_freed; 2963 2964 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 2965 event |= BNXT_TX_CMP_EVENT; 2966 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 2967 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 2968 else 2969 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 2970 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 2971 bp->tx_ring_mask; 2972 /* return full budget so NAPI will complete. */ 2973 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 2974 rx_pkts = budget; 2975 raw_cons = NEXT_RAW_CMP(raw_cons); 2976 if (budget) 2977 cpr->has_more_work = 1; 2978 break; 2979 } 2980 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 2981 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 2982 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 2983 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2984 if (likely(budget)) 2985 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 2986 else 2987 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 2988 &event); 2989 if (likely(rc >= 0)) 2990 rx_pkts += rc; 2991 /* Increment rx_pkts when rc is -ENOMEM to count towards 2992 * the NAPI budget. Otherwise, we may potentially loop 2993 * here forever if we consistently cannot allocate 2994 * buffers. 2995 */ 2996 else if (rc == -ENOMEM && budget) 2997 rx_pkts++; 2998 else if (rc == -EBUSY) /* partial completion */ 2999 break; 3000 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3001 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3002 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3003 bnxt_hwrm_handler(bp, txcmp); 3004 } 3005 raw_cons = NEXT_RAW_CMP(raw_cons); 3006 3007 if (rx_pkts && rx_pkts == budget) { 3008 cpr->has_more_work = 1; 3009 break; 3010 } 3011 } 3012 3013 if (event & BNXT_REDIRECT_EVENT) { 3014 xdp_do_flush(); 3015 event &= ~BNXT_REDIRECT_EVENT; 3016 } 3017 3018 if (event & BNXT_TX_EVENT) { 3019 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3020 u16 prod = txr->tx_prod; 3021 3022 /* Sync BD data before updating doorbell */ 3023 wmb(); 3024 3025 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3026 event &= ~BNXT_TX_EVENT; 3027 } 3028 3029 cpr->cp_raw_cons = raw_cons; 3030 bnapi->events |= event; 3031 return rx_pkts; 3032 } 3033 3034 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3035 int budget) 3036 { 3037 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3038 bnapi->tx_int(bp, bnapi, budget); 3039 3040 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3041 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3042 3043 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3044 bnapi->events &= ~BNXT_RX_EVENT; 3045 } 3046 if (bnapi->events & BNXT_AGG_EVENT) { 3047 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3048 3049 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3050 bnapi->events &= ~BNXT_AGG_EVENT; 3051 } 3052 } 3053 3054 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3055 int budget) 3056 { 3057 struct bnxt_napi *bnapi = cpr->bnapi; 3058 int rx_pkts; 3059 3060 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3061 3062 /* ACK completion ring before freeing tx ring and producing new 3063 * buffers in rx/agg rings to prevent overflowing the completion 3064 * ring. 3065 */ 3066 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3067 3068 __bnxt_poll_work_done(bp, bnapi, budget); 3069 return rx_pkts; 3070 } 3071 3072 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3073 { 3074 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3075 struct bnxt *bp = bnapi->bp; 3076 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3077 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3078 struct tx_cmp *txcmp; 3079 struct rx_cmp_ext *rxcmp1; 3080 u32 cp_cons, tmp_raw_cons; 3081 u32 raw_cons = cpr->cp_raw_cons; 3082 bool flush_xdp = false; 3083 u32 rx_pkts = 0; 3084 u8 event = 0; 3085 3086 while (1) { 3087 int rc; 3088 3089 cp_cons = RING_CMP(raw_cons); 3090 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3091 3092 if (!TX_CMP_VALID(txcmp, raw_cons)) 3093 break; 3094 3095 /* The valid test of the entry must be done first before 3096 * reading any further. 3097 */ 3098 dma_rmb(); 3099 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3100 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3101 cp_cons = RING_CMP(tmp_raw_cons); 3102 rxcmp1 = (struct rx_cmp_ext *) 3103 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3104 3105 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3106 break; 3107 3108 /* force an error to recycle the buffer */ 3109 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3110 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3111 3112 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3113 if (likely(rc == -EIO) && budget) 3114 rx_pkts++; 3115 else if (rc == -EBUSY) /* partial completion */ 3116 break; 3117 if (event & BNXT_REDIRECT_EVENT) 3118 flush_xdp = true; 3119 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3120 CMPL_BASE_TYPE_HWRM_DONE)) { 3121 bnxt_hwrm_handler(bp, txcmp); 3122 } else { 3123 netdev_err(bp->dev, 3124 "Invalid completion received on special ring\n"); 3125 } 3126 raw_cons = NEXT_RAW_CMP(raw_cons); 3127 3128 if (rx_pkts == budget) 3129 break; 3130 } 3131 3132 cpr->cp_raw_cons = raw_cons; 3133 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3134 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3135 3136 if (event & BNXT_AGG_EVENT) 3137 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3138 if (flush_xdp) 3139 xdp_do_flush(); 3140 3141 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3142 napi_complete_done(napi, rx_pkts); 3143 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3144 } 3145 return rx_pkts; 3146 } 3147 3148 static int bnxt_poll(struct napi_struct *napi, int budget) 3149 { 3150 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3151 struct bnxt *bp = bnapi->bp; 3152 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3153 int work_done = 0; 3154 3155 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3156 napi_complete(napi); 3157 return 0; 3158 } 3159 while (1) { 3160 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3161 3162 if (work_done >= budget) { 3163 if (!budget) 3164 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3165 break; 3166 } 3167 3168 if (!bnxt_has_work(bp, cpr)) { 3169 if (napi_complete_done(napi, work_done)) 3170 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3171 break; 3172 } 3173 } 3174 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3175 struct dim_sample dim_sample = {}; 3176 3177 dim_update_sample(cpr->event_ctr, 3178 cpr->rx_packets, 3179 cpr->rx_bytes, 3180 &dim_sample); 3181 net_dim(&cpr->dim, &dim_sample); 3182 } 3183 return work_done; 3184 } 3185 3186 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3187 { 3188 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3189 int i, work_done = 0; 3190 3191 for (i = 0; i < cpr->cp_ring_count; i++) { 3192 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3193 3194 if (cpr2->had_nqe_notify) { 3195 work_done += __bnxt_poll_work(bp, cpr2, 3196 budget - work_done); 3197 cpr->has_more_work |= cpr2->has_more_work; 3198 } 3199 } 3200 return work_done; 3201 } 3202 3203 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3204 u64 dbr_type, int budget) 3205 { 3206 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3207 int i; 3208 3209 for (i = 0; i < cpr->cp_ring_count; i++) { 3210 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3211 struct bnxt_db_info *db; 3212 3213 if (cpr2->had_work_done) { 3214 u32 tgl = 0; 3215 3216 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3217 cpr2->had_nqe_notify = 0; 3218 tgl = cpr2->toggle; 3219 } 3220 db = &cpr2->cp_db; 3221 bnxt_writeq(bp, 3222 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3223 DB_RING_IDX(db, cpr2->cp_raw_cons), 3224 db->doorbell); 3225 cpr2->had_work_done = 0; 3226 } 3227 } 3228 __bnxt_poll_work_done(bp, bnapi, budget); 3229 } 3230 3231 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3232 { 3233 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3234 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3235 struct bnxt_cp_ring_info *cpr_rx; 3236 u32 raw_cons = cpr->cp_raw_cons; 3237 struct bnxt *bp = bnapi->bp; 3238 struct nqe_cn *nqcmp; 3239 int work_done = 0; 3240 u32 cons; 3241 3242 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3243 napi_complete(napi); 3244 return 0; 3245 } 3246 if (cpr->has_more_work) { 3247 cpr->has_more_work = 0; 3248 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3249 } 3250 while (1) { 3251 u16 type; 3252 3253 cons = RING_CMP(raw_cons); 3254 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3255 3256 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3257 if (cpr->has_more_work) 3258 break; 3259 3260 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3261 budget); 3262 cpr->cp_raw_cons = raw_cons; 3263 if (napi_complete_done(napi, work_done)) 3264 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3265 cpr->cp_raw_cons); 3266 goto poll_done; 3267 } 3268 3269 /* The valid test of the entry must be done first before 3270 * reading any further. 3271 */ 3272 dma_rmb(); 3273 3274 type = le16_to_cpu(nqcmp->type); 3275 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3276 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3277 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3278 struct bnxt_cp_ring_info *cpr2; 3279 3280 /* No more budget for RX work */ 3281 if (budget && work_done >= budget && 3282 cq_type == BNXT_NQ_HDL_TYPE_RX) 3283 break; 3284 3285 idx = BNXT_NQ_HDL_IDX(idx); 3286 cpr2 = &cpr->cp_ring_arr[idx]; 3287 cpr2->had_nqe_notify = 1; 3288 cpr2->toggle = NQE_CN_TOGGLE(type); 3289 work_done += __bnxt_poll_work(bp, cpr2, 3290 budget - work_done); 3291 cpr->has_more_work |= cpr2->has_more_work; 3292 } else { 3293 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3294 } 3295 raw_cons = NEXT_RAW_CMP(raw_cons); 3296 } 3297 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3298 if (raw_cons != cpr->cp_raw_cons) { 3299 cpr->cp_raw_cons = raw_cons; 3300 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3301 } 3302 poll_done: 3303 cpr_rx = &cpr->cp_ring_arr[0]; 3304 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3305 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3306 struct dim_sample dim_sample = {}; 3307 3308 dim_update_sample(cpr->event_ctr, 3309 cpr_rx->rx_packets, 3310 cpr_rx->rx_bytes, 3311 &dim_sample); 3312 net_dim(&cpr->dim, &dim_sample); 3313 } 3314 return work_done; 3315 } 3316 3317 static void bnxt_free_tx_skbs(struct bnxt *bp) 3318 { 3319 int i, max_idx; 3320 struct pci_dev *pdev = bp->pdev; 3321 3322 if (!bp->tx_ring) 3323 return; 3324 3325 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3326 for (i = 0; i < bp->tx_nr_rings; i++) { 3327 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3328 int j; 3329 3330 if (!txr->tx_buf_ring) 3331 continue; 3332 3333 for (j = 0; j < max_idx;) { 3334 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j]; 3335 struct sk_buff *skb; 3336 int k, last; 3337 3338 if (i < bp->tx_nr_rings_xdp && 3339 tx_buf->action == XDP_REDIRECT) { 3340 dma_unmap_single(&pdev->dev, 3341 dma_unmap_addr(tx_buf, mapping), 3342 dma_unmap_len(tx_buf, len), 3343 DMA_TO_DEVICE); 3344 xdp_return_frame(tx_buf->xdpf); 3345 tx_buf->action = 0; 3346 tx_buf->xdpf = NULL; 3347 j++; 3348 continue; 3349 } 3350 3351 skb = tx_buf->skb; 3352 if (!skb) { 3353 j++; 3354 continue; 3355 } 3356 3357 tx_buf->skb = NULL; 3358 3359 if (tx_buf->is_push) { 3360 dev_kfree_skb(skb); 3361 j += 2; 3362 continue; 3363 } 3364 3365 dma_unmap_single(&pdev->dev, 3366 dma_unmap_addr(tx_buf, mapping), 3367 skb_headlen(skb), 3368 DMA_TO_DEVICE); 3369 3370 last = tx_buf->nr_frags; 3371 j += 2; 3372 for (k = 0; k < last; k++, j++) { 3373 int ring_idx = j & bp->tx_ring_mask; 3374 skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 3375 3376 tx_buf = &txr->tx_buf_ring[ring_idx]; 3377 dma_unmap_page( 3378 &pdev->dev, 3379 dma_unmap_addr(tx_buf, mapping), 3380 skb_frag_size(frag), DMA_TO_DEVICE); 3381 } 3382 dev_kfree_skb(skb); 3383 } 3384 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i)); 3385 } 3386 } 3387 3388 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3389 { 3390 int i, max_idx; 3391 3392 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3393 3394 for (i = 0; i < max_idx; i++) { 3395 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3396 void *data = rx_buf->data; 3397 3398 if (!data) 3399 continue; 3400 3401 rx_buf->data = NULL; 3402 if (BNXT_RX_PAGE_MODE(bp)) 3403 page_pool_recycle_direct(rxr->page_pool, data); 3404 else 3405 page_pool_free_va(rxr->head_pool, data, true); 3406 } 3407 } 3408 3409 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3410 { 3411 int i, max_idx; 3412 3413 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3414 3415 for (i = 0; i < max_idx; i++) { 3416 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3417 struct page *page = rx_agg_buf->page; 3418 3419 if (!page) 3420 continue; 3421 3422 rx_agg_buf->page = NULL; 3423 __clear_bit(i, rxr->rx_agg_bmap); 3424 3425 page_pool_recycle_direct(rxr->page_pool, page); 3426 } 3427 } 3428 3429 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3430 struct bnxt_rx_ring_info *rxr) 3431 { 3432 int i; 3433 3434 for (i = 0; i < bp->max_tpa; i++) { 3435 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3436 u8 *data = tpa_info->data; 3437 3438 if (!data) 3439 continue; 3440 3441 tpa_info->data = NULL; 3442 page_pool_free_va(rxr->head_pool, data, false); 3443 } 3444 } 3445 3446 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3447 struct bnxt_rx_ring_info *rxr) 3448 { 3449 struct bnxt_tpa_idx_map *map; 3450 3451 if (!rxr->rx_tpa) 3452 goto skip_rx_tpa_free; 3453 3454 bnxt_free_one_tpa_info_data(bp, rxr); 3455 3456 skip_rx_tpa_free: 3457 if (!rxr->rx_buf_ring) 3458 goto skip_rx_buf_free; 3459 3460 bnxt_free_one_rx_ring(bp, rxr); 3461 3462 skip_rx_buf_free: 3463 if (!rxr->rx_agg_ring) 3464 goto skip_rx_agg_free; 3465 3466 bnxt_free_one_rx_agg_ring(bp, rxr); 3467 3468 skip_rx_agg_free: 3469 map = rxr->rx_tpa_idx_map; 3470 if (map) 3471 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3472 } 3473 3474 static void bnxt_free_rx_skbs(struct bnxt *bp) 3475 { 3476 int i; 3477 3478 if (!bp->rx_ring) 3479 return; 3480 3481 for (i = 0; i < bp->rx_nr_rings; i++) 3482 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3483 } 3484 3485 static void bnxt_free_skbs(struct bnxt *bp) 3486 { 3487 bnxt_free_tx_skbs(bp); 3488 bnxt_free_rx_skbs(bp); 3489 } 3490 3491 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3492 { 3493 u8 init_val = ctxm->init_value; 3494 u16 offset = ctxm->init_offset; 3495 u8 *p2 = p; 3496 int i; 3497 3498 if (!init_val) 3499 return; 3500 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3501 memset(p, init_val, len); 3502 return; 3503 } 3504 for (i = 0; i < len; i += ctxm->entry_size) 3505 *(p2 + i + offset) = init_val; 3506 } 3507 3508 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3509 void *buf, size_t offset, size_t head, 3510 size_t tail) 3511 { 3512 int i, head_page, start_idx, source_offset; 3513 size_t len, rem_len, total_len, max_bytes; 3514 3515 head_page = head / rmem->page_size; 3516 source_offset = head % rmem->page_size; 3517 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3518 if (!total_len) 3519 total_len = MAX_CTX_BYTES; 3520 start_idx = head_page % MAX_CTX_PAGES; 3521 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3522 source_offset; 3523 total_len = min(total_len, max_bytes); 3524 rem_len = total_len; 3525 3526 for (i = start_idx; rem_len; i++, source_offset = 0) { 3527 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3528 if (buf) 3529 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3530 len); 3531 offset += len; 3532 rem_len -= len; 3533 } 3534 return total_len; 3535 } 3536 3537 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3538 { 3539 struct pci_dev *pdev = bp->pdev; 3540 int i; 3541 3542 if (!rmem->pg_arr) 3543 goto skip_pages; 3544 3545 for (i = 0; i < rmem->nr_pages; i++) { 3546 if (!rmem->pg_arr[i]) 3547 continue; 3548 3549 dma_free_coherent(&pdev->dev, rmem->page_size, 3550 rmem->pg_arr[i], rmem->dma_arr[i]); 3551 3552 rmem->pg_arr[i] = NULL; 3553 } 3554 skip_pages: 3555 if (rmem->pg_tbl) { 3556 size_t pg_tbl_size = rmem->nr_pages * 8; 3557 3558 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3559 pg_tbl_size = rmem->page_size; 3560 dma_free_coherent(&pdev->dev, pg_tbl_size, 3561 rmem->pg_tbl, rmem->pg_tbl_map); 3562 rmem->pg_tbl = NULL; 3563 } 3564 if (rmem->vmem_size && *rmem->vmem) { 3565 vfree(*rmem->vmem); 3566 *rmem->vmem = NULL; 3567 } 3568 } 3569 3570 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3571 { 3572 struct pci_dev *pdev = bp->pdev; 3573 u64 valid_bit = 0; 3574 int i; 3575 3576 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3577 valid_bit = PTU_PTE_VALID; 3578 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3579 size_t pg_tbl_size = rmem->nr_pages * 8; 3580 3581 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3582 pg_tbl_size = rmem->page_size; 3583 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3584 &rmem->pg_tbl_map, 3585 GFP_KERNEL); 3586 if (!rmem->pg_tbl) 3587 return -ENOMEM; 3588 } 3589 3590 for (i = 0; i < rmem->nr_pages; i++) { 3591 u64 extra_bits = valid_bit; 3592 3593 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3594 rmem->page_size, 3595 &rmem->dma_arr[i], 3596 GFP_KERNEL); 3597 if (!rmem->pg_arr[i]) 3598 return -ENOMEM; 3599 3600 if (rmem->ctx_mem) 3601 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3602 rmem->page_size); 3603 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3604 if (i == rmem->nr_pages - 2 && 3605 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3606 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3607 else if (i == rmem->nr_pages - 1 && 3608 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3609 extra_bits |= PTU_PTE_LAST; 3610 rmem->pg_tbl[i] = 3611 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3612 } 3613 } 3614 3615 if (rmem->vmem_size) { 3616 *rmem->vmem = vzalloc(rmem->vmem_size); 3617 if (!(*rmem->vmem)) 3618 return -ENOMEM; 3619 } 3620 return 0; 3621 } 3622 3623 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3624 struct bnxt_rx_ring_info *rxr) 3625 { 3626 int i; 3627 3628 kfree(rxr->rx_tpa_idx_map); 3629 rxr->rx_tpa_idx_map = NULL; 3630 if (rxr->rx_tpa) { 3631 for (i = 0; i < bp->max_tpa; i++) { 3632 kfree(rxr->rx_tpa[i].agg_arr); 3633 rxr->rx_tpa[i].agg_arr = NULL; 3634 } 3635 } 3636 kfree(rxr->rx_tpa); 3637 rxr->rx_tpa = NULL; 3638 } 3639 3640 static void bnxt_free_tpa_info(struct bnxt *bp) 3641 { 3642 int i; 3643 3644 for (i = 0; i < bp->rx_nr_rings; i++) { 3645 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3646 3647 bnxt_free_one_tpa_info(bp, rxr); 3648 } 3649 } 3650 3651 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3652 struct bnxt_rx_ring_info *rxr) 3653 { 3654 struct rx_agg_cmp *agg; 3655 int i; 3656 3657 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3658 GFP_KERNEL); 3659 if (!rxr->rx_tpa) 3660 return -ENOMEM; 3661 3662 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3663 return 0; 3664 for (i = 0; i < bp->max_tpa; i++) { 3665 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3666 if (!agg) 3667 return -ENOMEM; 3668 rxr->rx_tpa[i].agg_arr = agg; 3669 } 3670 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3671 GFP_KERNEL); 3672 if (!rxr->rx_tpa_idx_map) 3673 return -ENOMEM; 3674 3675 return 0; 3676 } 3677 3678 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3679 { 3680 int i, rc; 3681 3682 bp->max_tpa = MAX_TPA; 3683 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3684 if (!bp->max_tpa_v2) 3685 return 0; 3686 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3687 } 3688 3689 for (i = 0; i < bp->rx_nr_rings; i++) { 3690 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3691 3692 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3693 if (rc) 3694 return rc; 3695 } 3696 return 0; 3697 } 3698 3699 static void bnxt_free_rx_rings(struct bnxt *bp) 3700 { 3701 int i; 3702 3703 if (!bp->rx_ring) 3704 return; 3705 3706 bnxt_free_tpa_info(bp); 3707 for (i = 0; i < bp->rx_nr_rings; i++) { 3708 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3709 struct bnxt_ring_struct *ring; 3710 3711 if (rxr->xdp_prog) 3712 bpf_prog_put(rxr->xdp_prog); 3713 3714 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3715 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3716 3717 page_pool_destroy(rxr->page_pool); 3718 if (bnxt_separate_head_pool()) 3719 page_pool_destroy(rxr->head_pool); 3720 rxr->page_pool = rxr->head_pool = NULL; 3721 3722 kfree(rxr->rx_agg_bmap); 3723 rxr->rx_agg_bmap = NULL; 3724 3725 ring = &rxr->rx_ring_struct; 3726 bnxt_free_ring(bp, &ring->ring_mem); 3727 3728 ring = &rxr->rx_agg_ring_struct; 3729 bnxt_free_ring(bp, &ring->ring_mem); 3730 } 3731 } 3732 3733 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3734 struct bnxt_rx_ring_info *rxr, 3735 int numa_node) 3736 { 3737 struct page_pool_params pp = { 0 }; 3738 struct page_pool *pool; 3739 3740 pp.pool_size = bp->rx_agg_ring_size; 3741 if (BNXT_RX_PAGE_MODE(bp)) 3742 pp.pool_size += bp->rx_ring_size; 3743 pp.nid = numa_node; 3744 pp.napi = &rxr->bnapi->napi; 3745 pp.netdev = bp->dev; 3746 pp.dev = &bp->pdev->dev; 3747 pp.dma_dir = bp->rx_dir; 3748 pp.max_len = PAGE_SIZE; 3749 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3750 3751 pool = page_pool_create(&pp); 3752 if (IS_ERR(pool)) 3753 return PTR_ERR(pool); 3754 rxr->page_pool = pool; 3755 3756 if (bnxt_separate_head_pool()) { 3757 pp.pool_size = max(bp->rx_ring_size, 1024); 3758 pool = page_pool_create(&pp); 3759 if (IS_ERR(pool)) 3760 goto err_destroy_pp; 3761 } 3762 rxr->head_pool = pool; 3763 3764 return 0; 3765 3766 err_destroy_pp: 3767 page_pool_destroy(rxr->page_pool); 3768 rxr->page_pool = NULL; 3769 return PTR_ERR(pool); 3770 } 3771 3772 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3773 { 3774 u16 mem_size; 3775 3776 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3777 mem_size = rxr->rx_agg_bmap_size / 8; 3778 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3779 if (!rxr->rx_agg_bmap) 3780 return -ENOMEM; 3781 3782 return 0; 3783 } 3784 3785 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3786 { 3787 int numa_node = dev_to_node(&bp->pdev->dev); 3788 int i, rc = 0, agg_rings = 0, cpu; 3789 3790 if (!bp->rx_ring) 3791 return -ENOMEM; 3792 3793 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3794 agg_rings = 1; 3795 3796 for (i = 0; i < bp->rx_nr_rings; i++) { 3797 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3798 struct bnxt_ring_struct *ring; 3799 int cpu_node; 3800 3801 ring = &rxr->rx_ring_struct; 3802 3803 cpu = cpumask_local_spread(i, numa_node); 3804 cpu_node = cpu_to_node(cpu); 3805 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3806 i, cpu_node); 3807 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3808 if (rc) 3809 return rc; 3810 3811 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3812 if (rc < 0) 3813 return rc; 3814 3815 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3816 MEM_TYPE_PAGE_POOL, 3817 rxr->page_pool); 3818 if (rc) { 3819 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3820 return rc; 3821 } 3822 3823 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3824 if (rc) 3825 return rc; 3826 3827 ring->grp_idx = i; 3828 if (agg_rings) { 3829 ring = &rxr->rx_agg_ring_struct; 3830 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3831 if (rc) 3832 return rc; 3833 3834 ring->grp_idx = i; 3835 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 3836 if (rc) 3837 return rc; 3838 } 3839 } 3840 if (bp->flags & BNXT_FLAG_TPA) 3841 rc = bnxt_alloc_tpa_info(bp); 3842 return rc; 3843 } 3844 3845 static void bnxt_free_tx_rings(struct bnxt *bp) 3846 { 3847 int i; 3848 struct pci_dev *pdev = bp->pdev; 3849 3850 if (!bp->tx_ring) 3851 return; 3852 3853 for (i = 0; i < bp->tx_nr_rings; i++) { 3854 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3855 struct bnxt_ring_struct *ring; 3856 3857 if (txr->tx_push) { 3858 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3859 txr->tx_push, txr->tx_push_mapping); 3860 txr->tx_push = NULL; 3861 } 3862 3863 ring = &txr->tx_ring_struct; 3864 3865 bnxt_free_ring(bp, &ring->ring_mem); 3866 } 3867 } 3868 3869 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3870 ((tc) * (bp)->tx_nr_rings_per_tc) 3871 3872 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3873 ((tx) % (bp)->tx_nr_rings_per_tc) 3874 3875 #define BNXT_RING_TO_TC(bp, tx) \ 3876 ((tx) / (bp)->tx_nr_rings_per_tc) 3877 3878 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3879 { 3880 int i, j, rc; 3881 struct pci_dev *pdev = bp->pdev; 3882 3883 bp->tx_push_size = 0; 3884 if (bp->tx_push_thresh) { 3885 int push_size; 3886 3887 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3888 bp->tx_push_thresh); 3889 3890 if (push_size > 256) { 3891 push_size = 0; 3892 bp->tx_push_thresh = 0; 3893 } 3894 3895 bp->tx_push_size = push_size; 3896 } 3897 3898 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3899 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3900 struct bnxt_ring_struct *ring; 3901 u8 qidx; 3902 3903 ring = &txr->tx_ring_struct; 3904 3905 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3906 if (rc) 3907 return rc; 3908 3909 ring->grp_idx = txr->bnapi->index; 3910 if (bp->tx_push_size) { 3911 dma_addr_t mapping; 3912 3913 /* One pre-allocated DMA buffer to backup 3914 * TX push operation 3915 */ 3916 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3917 bp->tx_push_size, 3918 &txr->tx_push_mapping, 3919 GFP_KERNEL); 3920 3921 if (!txr->tx_push) 3922 return -ENOMEM; 3923 3924 mapping = txr->tx_push_mapping + 3925 sizeof(struct tx_push_bd); 3926 txr->data_mapping = cpu_to_le64(mapping); 3927 } 3928 qidx = bp->tc_to_qidx[j]; 3929 ring->queue_id = bp->q_info[qidx].queue_id; 3930 spin_lock_init(&txr->xdp_tx_lock); 3931 if (i < bp->tx_nr_rings_xdp) 3932 continue; 3933 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 3934 j++; 3935 } 3936 return 0; 3937 } 3938 3939 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 3940 { 3941 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 3942 3943 kfree(cpr->cp_desc_ring); 3944 cpr->cp_desc_ring = NULL; 3945 ring->ring_mem.pg_arr = NULL; 3946 kfree(cpr->cp_desc_mapping); 3947 cpr->cp_desc_mapping = NULL; 3948 ring->ring_mem.dma_arr = NULL; 3949 } 3950 3951 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 3952 { 3953 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 3954 if (!cpr->cp_desc_ring) 3955 return -ENOMEM; 3956 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 3957 GFP_KERNEL); 3958 if (!cpr->cp_desc_mapping) 3959 return -ENOMEM; 3960 return 0; 3961 } 3962 3963 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 3964 { 3965 int i; 3966 3967 if (!bp->bnapi) 3968 return; 3969 for (i = 0; i < bp->cp_nr_rings; i++) { 3970 struct bnxt_napi *bnapi = bp->bnapi[i]; 3971 3972 if (!bnapi) 3973 continue; 3974 bnxt_free_cp_arrays(&bnapi->cp_ring); 3975 } 3976 } 3977 3978 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 3979 { 3980 int i, n = bp->cp_nr_pages; 3981 3982 for (i = 0; i < bp->cp_nr_rings; i++) { 3983 struct bnxt_napi *bnapi = bp->bnapi[i]; 3984 int rc; 3985 3986 if (!bnapi) 3987 continue; 3988 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 3989 if (rc) 3990 return rc; 3991 } 3992 return 0; 3993 } 3994 3995 static void bnxt_free_cp_rings(struct bnxt *bp) 3996 { 3997 int i; 3998 3999 if (!bp->bnapi) 4000 return; 4001 4002 for (i = 0; i < bp->cp_nr_rings; i++) { 4003 struct bnxt_napi *bnapi = bp->bnapi[i]; 4004 struct bnxt_cp_ring_info *cpr; 4005 struct bnxt_ring_struct *ring; 4006 int j; 4007 4008 if (!bnapi) 4009 continue; 4010 4011 cpr = &bnapi->cp_ring; 4012 ring = &cpr->cp_ring_struct; 4013 4014 bnxt_free_ring(bp, &ring->ring_mem); 4015 4016 if (!cpr->cp_ring_arr) 4017 continue; 4018 4019 for (j = 0; j < cpr->cp_ring_count; j++) { 4020 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4021 4022 ring = &cpr2->cp_ring_struct; 4023 bnxt_free_ring(bp, &ring->ring_mem); 4024 bnxt_free_cp_arrays(cpr2); 4025 } 4026 kfree(cpr->cp_ring_arr); 4027 cpr->cp_ring_arr = NULL; 4028 cpr->cp_ring_count = 0; 4029 } 4030 } 4031 4032 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4033 struct bnxt_cp_ring_info *cpr) 4034 { 4035 struct bnxt_ring_mem_info *rmem; 4036 struct bnxt_ring_struct *ring; 4037 int rc; 4038 4039 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4040 if (rc) { 4041 bnxt_free_cp_arrays(cpr); 4042 return -ENOMEM; 4043 } 4044 ring = &cpr->cp_ring_struct; 4045 rmem = &ring->ring_mem; 4046 rmem->nr_pages = bp->cp_nr_pages; 4047 rmem->page_size = HW_CMPD_RING_SIZE; 4048 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4049 rmem->dma_arr = cpr->cp_desc_mapping; 4050 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4051 rc = bnxt_alloc_ring(bp, rmem); 4052 if (rc) { 4053 bnxt_free_ring(bp, rmem); 4054 bnxt_free_cp_arrays(cpr); 4055 } 4056 return rc; 4057 } 4058 4059 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4060 { 4061 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4062 int i, j, rc, ulp_msix; 4063 int tcs = bp->num_tc; 4064 4065 if (!tcs) 4066 tcs = 1; 4067 ulp_msix = bnxt_get_ulp_msix_num(bp); 4068 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4069 struct bnxt_napi *bnapi = bp->bnapi[i]; 4070 struct bnxt_cp_ring_info *cpr, *cpr2; 4071 struct bnxt_ring_struct *ring; 4072 int cp_count = 0, k; 4073 int rx = 0, tx = 0; 4074 4075 if (!bnapi) 4076 continue; 4077 4078 cpr = &bnapi->cp_ring; 4079 cpr->bnapi = bnapi; 4080 ring = &cpr->cp_ring_struct; 4081 4082 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4083 if (rc) 4084 return rc; 4085 4086 ring->map_idx = ulp_msix + i; 4087 4088 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4089 continue; 4090 4091 if (i < bp->rx_nr_rings) { 4092 cp_count++; 4093 rx = 1; 4094 } 4095 if (i < bp->tx_nr_rings_xdp) { 4096 cp_count++; 4097 tx = 1; 4098 } else if ((sh && i < bp->tx_nr_rings) || 4099 (!sh && i >= bp->rx_nr_rings)) { 4100 cp_count += tcs; 4101 tx = 1; 4102 } 4103 4104 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 4105 GFP_KERNEL); 4106 if (!cpr->cp_ring_arr) 4107 return -ENOMEM; 4108 cpr->cp_ring_count = cp_count; 4109 4110 for (k = 0; k < cp_count; k++) { 4111 cpr2 = &cpr->cp_ring_arr[k]; 4112 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4113 if (rc) 4114 return rc; 4115 cpr2->bnapi = bnapi; 4116 cpr2->sw_stats = cpr->sw_stats; 4117 cpr2->cp_idx = k; 4118 if (!k && rx) { 4119 bp->rx_ring[i].rx_cpr = cpr2; 4120 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4121 } else { 4122 int n, tc = k - rx; 4123 4124 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4125 bp->tx_ring[n].tx_cpr = cpr2; 4126 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4127 } 4128 } 4129 if (tx) 4130 j++; 4131 } 4132 return 0; 4133 } 4134 4135 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4136 struct bnxt_rx_ring_info *rxr) 4137 { 4138 struct bnxt_ring_mem_info *rmem; 4139 struct bnxt_ring_struct *ring; 4140 4141 ring = &rxr->rx_ring_struct; 4142 rmem = &ring->ring_mem; 4143 rmem->nr_pages = bp->rx_nr_pages; 4144 rmem->page_size = HW_RXBD_RING_SIZE; 4145 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4146 rmem->dma_arr = rxr->rx_desc_mapping; 4147 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4148 rmem->vmem = (void **)&rxr->rx_buf_ring; 4149 4150 ring = &rxr->rx_agg_ring_struct; 4151 rmem = &ring->ring_mem; 4152 rmem->nr_pages = bp->rx_agg_nr_pages; 4153 rmem->page_size = HW_RXBD_RING_SIZE; 4154 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4155 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4156 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4157 rmem->vmem = (void **)&rxr->rx_agg_ring; 4158 } 4159 4160 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4161 struct bnxt_rx_ring_info *rxr) 4162 { 4163 struct bnxt_ring_mem_info *rmem; 4164 struct bnxt_ring_struct *ring; 4165 int i; 4166 4167 rxr->page_pool->p.napi = NULL; 4168 rxr->page_pool = NULL; 4169 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4170 4171 ring = &rxr->rx_ring_struct; 4172 rmem = &ring->ring_mem; 4173 rmem->pg_tbl = NULL; 4174 rmem->pg_tbl_map = 0; 4175 for (i = 0; i < rmem->nr_pages; i++) { 4176 rmem->pg_arr[i] = NULL; 4177 rmem->dma_arr[i] = 0; 4178 } 4179 *rmem->vmem = NULL; 4180 4181 ring = &rxr->rx_agg_ring_struct; 4182 rmem = &ring->ring_mem; 4183 rmem->pg_tbl = NULL; 4184 rmem->pg_tbl_map = 0; 4185 for (i = 0; i < rmem->nr_pages; i++) { 4186 rmem->pg_arr[i] = NULL; 4187 rmem->dma_arr[i] = 0; 4188 } 4189 *rmem->vmem = NULL; 4190 } 4191 4192 static void bnxt_init_ring_struct(struct bnxt *bp) 4193 { 4194 int i, j; 4195 4196 for (i = 0; i < bp->cp_nr_rings; i++) { 4197 struct bnxt_napi *bnapi = bp->bnapi[i]; 4198 struct bnxt_ring_mem_info *rmem; 4199 struct bnxt_cp_ring_info *cpr; 4200 struct bnxt_rx_ring_info *rxr; 4201 struct bnxt_tx_ring_info *txr; 4202 struct bnxt_ring_struct *ring; 4203 4204 if (!bnapi) 4205 continue; 4206 4207 cpr = &bnapi->cp_ring; 4208 ring = &cpr->cp_ring_struct; 4209 rmem = &ring->ring_mem; 4210 rmem->nr_pages = bp->cp_nr_pages; 4211 rmem->page_size = HW_CMPD_RING_SIZE; 4212 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4213 rmem->dma_arr = cpr->cp_desc_mapping; 4214 rmem->vmem_size = 0; 4215 4216 rxr = bnapi->rx_ring; 4217 if (!rxr) 4218 goto skip_rx; 4219 4220 ring = &rxr->rx_ring_struct; 4221 rmem = &ring->ring_mem; 4222 rmem->nr_pages = bp->rx_nr_pages; 4223 rmem->page_size = HW_RXBD_RING_SIZE; 4224 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4225 rmem->dma_arr = rxr->rx_desc_mapping; 4226 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4227 rmem->vmem = (void **)&rxr->rx_buf_ring; 4228 4229 ring = &rxr->rx_agg_ring_struct; 4230 rmem = &ring->ring_mem; 4231 rmem->nr_pages = bp->rx_agg_nr_pages; 4232 rmem->page_size = HW_RXBD_RING_SIZE; 4233 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4234 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4235 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4236 rmem->vmem = (void **)&rxr->rx_agg_ring; 4237 4238 skip_rx: 4239 bnxt_for_each_napi_tx(j, bnapi, txr) { 4240 ring = &txr->tx_ring_struct; 4241 rmem = &ring->ring_mem; 4242 rmem->nr_pages = bp->tx_nr_pages; 4243 rmem->page_size = HW_TXBD_RING_SIZE; 4244 rmem->pg_arr = (void **)txr->tx_desc_ring; 4245 rmem->dma_arr = txr->tx_desc_mapping; 4246 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4247 rmem->vmem = (void **)&txr->tx_buf_ring; 4248 } 4249 } 4250 } 4251 4252 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4253 { 4254 int i; 4255 u32 prod; 4256 struct rx_bd **rx_buf_ring; 4257 4258 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4259 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4260 int j; 4261 struct rx_bd *rxbd; 4262 4263 rxbd = rx_buf_ring[i]; 4264 if (!rxbd) 4265 continue; 4266 4267 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4268 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4269 rxbd->rx_bd_opaque = prod; 4270 } 4271 } 4272 } 4273 4274 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4275 struct bnxt_rx_ring_info *rxr, 4276 int ring_nr) 4277 { 4278 u32 prod; 4279 int i; 4280 4281 prod = rxr->rx_prod; 4282 for (i = 0; i < bp->rx_ring_size; i++) { 4283 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4284 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4285 ring_nr, i, bp->rx_ring_size); 4286 break; 4287 } 4288 prod = NEXT_RX(prod); 4289 } 4290 rxr->rx_prod = prod; 4291 } 4292 4293 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp, 4294 struct bnxt_rx_ring_info *rxr, 4295 int ring_nr) 4296 { 4297 u32 prod; 4298 int i; 4299 4300 prod = rxr->rx_agg_prod; 4301 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4302 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) { 4303 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4304 ring_nr, i, bp->rx_ring_size); 4305 break; 4306 } 4307 prod = NEXT_RX_AGG(prod); 4308 } 4309 rxr->rx_agg_prod = prod; 4310 } 4311 4312 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4313 struct bnxt_rx_ring_info *rxr) 4314 { 4315 dma_addr_t mapping; 4316 u8 *data; 4317 int i; 4318 4319 for (i = 0; i < bp->max_tpa; i++) { 4320 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4321 GFP_KERNEL); 4322 if (!data) 4323 return -ENOMEM; 4324 4325 rxr->rx_tpa[i].data = data; 4326 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4327 rxr->rx_tpa[i].mapping = mapping; 4328 } 4329 4330 return 0; 4331 } 4332 4333 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4334 { 4335 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4336 int rc; 4337 4338 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4339 4340 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4341 return 0; 4342 4343 bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr); 4344 4345 if (rxr->rx_tpa) { 4346 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4347 if (rc) 4348 return rc; 4349 } 4350 return 0; 4351 } 4352 4353 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4354 struct bnxt_rx_ring_info *rxr) 4355 { 4356 struct bnxt_ring_struct *ring; 4357 u32 type; 4358 4359 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4360 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4361 4362 if (NET_IP_ALIGN == 2) 4363 type |= RX_BD_FLAGS_SOP; 4364 4365 ring = &rxr->rx_ring_struct; 4366 bnxt_init_rxbd_pages(ring, type); 4367 ring->fw_ring_id = INVALID_HW_RING_ID; 4368 } 4369 4370 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4371 struct bnxt_rx_ring_info *rxr) 4372 { 4373 struct bnxt_ring_struct *ring; 4374 u32 type; 4375 4376 ring = &rxr->rx_agg_ring_struct; 4377 ring->fw_ring_id = INVALID_HW_RING_ID; 4378 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4379 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4380 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4381 4382 bnxt_init_rxbd_pages(ring, type); 4383 } 4384 } 4385 4386 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4387 { 4388 struct bnxt_rx_ring_info *rxr; 4389 4390 rxr = &bp->rx_ring[ring_nr]; 4391 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4392 4393 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4394 &rxr->bnapi->napi); 4395 4396 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4397 bpf_prog_add(bp->xdp_prog, 1); 4398 rxr->xdp_prog = bp->xdp_prog; 4399 } 4400 4401 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4402 4403 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4404 } 4405 4406 static void bnxt_init_cp_rings(struct bnxt *bp) 4407 { 4408 int i, j; 4409 4410 for (i = 0; i < bp->cp_nr_rings; i++) { 4411 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4412 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4413 4414 ring->fw_ring_id = INVALID_HW_RING_ID; 4415 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4416 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4417 if (!cpr->cp_ring_arr) 4418 continue; 4419 for (j = 0; j < cpr->cp_ring_count; j++) { 4420 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4421 4422 ring = &cpr2->cp_ring_struct; 4423 ring->fw_ring_id = INVALID_HW_RING_ID; 4424 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4425 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4426 } 4427 } 4428 } 4429 4430 static int bnxt_init_rx_rings(struct bnxt *bp) 4431 { 4432 int i, rc = 0; 4433 4434 if (BNXT_RX_PAGE_MODE(bp)) { 4435 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4436 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4437 } else { 4438 bp->rx_offset = BNXT_RX_OFFSET; 4439 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4440 } 4441 4442 for (i = 0; i < bp->rx_nr_rings; i++) { 4443 rc = bnxt_init_one_rx_ring(bp, i); 4444 if (rc) 4445 break; 4446 } 4447 4448 return rc; 4449 } 4450 4451 static int bnxt_init_tx_rings(struct bnxt *bp) 4452 { 4453 u16 i; 4454 4455 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4456 BNXT_MIN_TX_DESC_CNT); 4457 4458 for (i = 0; i < bp->tx_nr_rings; i++) { 4459 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4460 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4461 4462 ring->fw_ring_id = INVALID_HW_RING_ID; 4463 4464 if (i >= bp->tx_nr_rings_xdp) 4465 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4466 NETDEV_QUEUE_TYPE_TX, 4467 &txr->bnapi->napi); 4468 } 4469 4470 return 0; 4471 } 4472 4473 static void bnxt_free_ring_grps(struct bnxt *bp) 4474 { 4475 kfree(bp->grp_info); 4476 bp->grp_info = NULL; 4477 } 4478 4479 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4480 { 4481 int i; 4482 4483 if (irq_re_init) { 4484 bp->grp_info = kcalloc(bp->cp_nr_rings, 4485 sizeof(struct bnxt_ring_grp_info), 4486 GFP_KERNEL); 4487 if (!bp->grp_info) 4488 return -ENOMEM; 4489 } 4490 for (i = 0; i < bp->cp_nr_rings; i++) { 4491 if (irq_re_init) 4492 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4493 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4494 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4495 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4496 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4497 } 4498 return 0; 4499 } 4500 4501 static void bnxt_free_vnics(struct bnxt *bp) 4502 { 4503 kfree(bp->vnic_info); 4504 bp->vnic_info = NULL; 4505 bp->nr_vnics = 0; 4506 } 4507 4508 static int bnxt_alloc_vnics(struct bnxt *bp) 4509 { 4510 int num_vnics = 1; 4511 4512 #ifdef CONFIG_RFS_ACCEL 4513 if (bp->flags & BNXT_FLAG_RFS) { 4514 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4515 num_vnics++; 4516 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4517 num_vnics += bp->rx_nr_rings; 4518 } 4519 #endif 4520 4521 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4522 num_vnics++; 4523 4524 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4525 GFP_KERNEL); 4526 if (!bp->vnic_info) 4527 return -ENOMEM; 4528 4529 bp->nr_vnics = num_vnics; 4530 return 0; 4531 } 4532 4533 static void bnxt_init_vnics(struct bnxt *bp) 4534 { 4535 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4536 int i; 4537 4538 for (i = 0; i < bp->nr_vnics; i++) { 4539 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4540 int j; 4541 4542 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4543 vnic->vnic_id = i; 4544 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4545 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4546 4547 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4548 4549 if (bp->vnic_info[i].rss_hash_key) { 4550 if (i == BNXT_VNIC_DEFAULT) { 4551 u8 *key = (void *)vnic->rss_hash_key; 4552 int k; 4553 4554 if (!bp->rss_hash_key_valid && 4555 !bp->rss_hash_key_updated) { 4556 get_random_bytes(bp->rss_hash_key, 4557 HW_HASH_KEY_SIZE); 4558 bp->rss_hash_key_updated = true; 4559 } 4560 4561 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4562 HW_HASH_KEY_SIZE); 4563 4564 if (!bp->rss_hash_key_updated) 4565 continue; 4566 4567 bp->rss_hash_key_updated = false; 4568 bp->rss_hash_key_valid = true; 4569 4570 bp->toeplitz_prefix = 0; 4571 for (k = 0; k < 8; k++) { 4572 bp->toeplitz_prefix <<= 8; 4573 bp->toeplitz_prefix |= key[k]; 4574 } 4575 } else { 4576 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4577 HW_HASH_KEY_SIZE); 4578 } 4579 } 4580 } 4581 } 4582 4583 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4584 { 4585 int pages; 4586 4587 pages = ring_size / desc_per_pg; 4588 4589 if (!pages) 4590 return 1; 4591 4592 pages++; 4593 4594 while (pages & (pages - 1)) 4595 pages++; 4596 4597 return pages; 4598 } 4599 4600 void bnxt_set_tpa_flags(struct bnxt *bp) 4601 { 4602 bp->flags &= ~BNXT_FLAG_TPA; 4603 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4604 return; 4605 if (bp->dev->features & NETIF_F_LRO) 4606 bp->flags |= BNXT_FLAG_LRO; 4607 else if (bp->dev->features & NETIF_F_GRO_HW) 4608 bp->flags |= BNXT_FLAG_GRO; 4609 } 4610 4611 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4612 * be set on entry. 4613 */ 4614 void bnxt_set_ring_params(struct bnxt *bp) 4615 { 4616 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4617 u32 agg_factor = 0, agg_ring_size = 0; 4618 4619 /* 8 for CRC and VLAN */ 4620 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4621 4622 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4623 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4624 4625 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH; 4626 ring_size = bp->rx_ring_size; 4627 bp->rx_agg_ring_size = 0; 4628 bp->rx_agg_nr_pages = 0; 4629 4630 if (bp->flags & BNXT_FLAG_TPA) 4631 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4632 4633 bp->flags &= ~BNXT_FLAG_JUMBO; 4634 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4635 u32 jumbo_factor; 4636 4637 bp->flags |= BNXT_FLAG_JUMBO; 4638 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4639 if (jumbo_factor > agg_factor) 4640 agg_factor = jumbo_factor; 4641 } 4642 if (agg_factor) { 4643 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4644 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4645 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4646 bp->rx_ring_size, ring_size); 4647 bp->rx_ring_size = ring_size; 4648 } 4649 agg_ring_size = ring_size * agg_factor; 4650 4651 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4652 RX_DESC_CNT); 4653 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4654 u32 tmp = agg_ring_size; 4655 4656 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4657 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4658 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4659 tmp, agg_ring_size); 4660 } 4661 bp->rx_agg_ring_size = agg_ring_size; 4662 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4663 4664 if (BNXT_RX_PAGE_MODE(bp)) { 4665 rx_space = PAGE_SIZE; 4666 rx_size = PAGE_SIZE - 4667 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4668 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4669 } else { 4670 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN); 4671 rx_space = rx_size + NET_SKB_PAD + 4672 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4673 } 4674 } 4675 4676 bp->rx_buf_use_size = rx_size; 4677 bp->rx_buf_size = rx_space; 4678 4679 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4680 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4681 4682 ring_size = bp->tx_ring_size; 4683 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4684 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4685 4686 max_rx_cmpl = bp->rx_ring_size; 4687 /* MAX TPA needs to be added because TPA_START completions are 4688 * immediately recycled, so the TPA completions are not bound by 4689 * the RX ring size. 4690 */ 4691 if (bp->flags & BNXT_FLAG_TPA) 4692 max_rx_cmpl += bp->max_tpa; 4693 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4694 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4695 bp->cp_ring_size = ring_size; 4696 4697 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4698 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4699 bp->cp_nr_pages = MAX_CP_PAGES; 4700 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4701 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4702 ring_size, bp->cp_ring_size); 4703 } 4704 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4705 bp->cp_ring_mask = bp->cp_bit - 1; 4706 } 4707 4708 /* Changing allocation mode of RX rings. 4709 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4710 */ 4711 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4712 { 4713 struct net_device *dev = bp->dev; 4714 4715 if (page_mode) { 4716 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 4717 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4718 4719 if (bp->xdp_prog->aux->xdp_has_frags) 4720 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4721 else 4722 dev->max_mtu = 4723 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4724 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4725 bp->flags |= BNXT_FLAG_JUMBO; 4726 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4727 } else { 4728 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4729 bp->rx_skb_func = bnxt_rx_page_skb; 4730 } 4731 bp->rx_dir = DMA_BIDIRECTIONAL; 4732 /* Disable LRO or GRO_HW */ 4733 netdev_update_features(dev); 4734 } else { 4735 dev->max_mtu = bp->max_mtu; 4736 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4737 bp->rx_dir = DMA_FROM_DEVICE; 4738 bp->rx_skb_func = bnxt_rx_skb; 4739 } 4740 return 0; 4741 } 4742 4743 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4744 { 4745 int i; 4746 struct bnxt_vnic_info *vnic; 4747 struct pci_dev *pdev = bp->pdev; 4748 4749 if (!bp->vnic_info) 4750 return; 4751 4752 for (i = 0; i < bp->nr_vnics; i++) { 4753 vnic = &bp->vnic_info[i]; 4754 4755 kfree(vnic->fw_grp_ids); 4756 vnic->fw_grp_ids = NULL; 4757 4758 kfree(vnic->uc_list); 4759 vnic->uc_list = NULL; 4760 4761 if (vnic->mc_list) { 4762 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4763 vnic->mc_list, vnic->mc_list_mapping); 4764 vnic->mc_list = NULL; 4765 } 4766 4767 if (vnic->rss_table) { 4768 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4769 vnic->rss_table, 4770 vnic->rss_table_dma_addr); 4771 vnic->rss_table = NULL; 4772 } 4773 4774 vnic->rss_hash_key = NULL; 4775 vnic->flags = 0; 4776 } 4777 } 4778 4779 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4780 { 4781 int i, rc = 0, size; 4782 struct bnxt_vnic_info *vnic; 4783 struct pci_dev *pdev = bp->pdev; 4784 int max_rings; 4785 4786 for (i = 0; i < bp->nr_vnics; i++) { 4787 vnic = &bp->vnic_info[i]; 4788 4789 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4790 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4791 4792 if (mem_size > 0) { 4793 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4794 if (!vnic->uc_list) { 4795 rc = -ENOMEM; 4796 goto out; 4797 } 4798 } 4799 } 4800 4801 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4802 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4803 vnic->mc_list = 4804 dma_alloc_coherent(&pdev->dev, 4805 vnic->mc_list_size, 4806 &vnic->mc_list_mapping, 4807 GFP_KERNEL); 4808 if (!vnic->mc_list) { 4809 rc = -ENOMEM; 4810 goto out; 4811 } 4812 } 4813 4814 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4815 goto vnic_skip_grps; 4816 4817 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4818 max_rings = bp->rx_nr_rings; 4819 else 4820 max_rings = 1; 4821 4822 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4823 if (!vnic->fw_grp_ids) { 4824 rc = -ENOMEM; 4825 goto out; 4826 } 4827 vnic_skip_grps: 4828 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4829 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4830 continue; 4831 4832 /* Allocate rss table and hash key */ 4833 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4834 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4835 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4836 4837 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4838 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4839 vnic->rss_table_size, 4840 &vnic->rss_table_dma_addr, 4841 GFP_KERNEL); 4842 if (!vnic->rss_table) { 4843 rc = -ENOMEM; 4844 goto out; 4845 } 4846 4847 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4848 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4849 } 4850 return 0; 4851 4852 out: 4853 return rc; 4854 } 4855 4856 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4857 { 4858 struct bnxt_hwrm_wait_token *token; 4859 4860 dma_pool_destroy(bp->hwrm_dma_pool); 4861 bp->hwrm_dma_pool = NULL; 4862 4863 rcu_read_lock(); 4864 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4865 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4866 rcu_read_unlock(); 4867 } 4868 4869 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 4870 { 4871 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 4872 BNXT_HWRM_DMA_SIZE, 4873 BNXT_HWRM_DMA_ALIGN, 0); 4874 if (!bp->hwrm_dma_pool) 4875 return -ENOMEM; 4876 4877 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4878 4879 return 0; 4880 } 4881 4882 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4883 { 4884 kfree(stats->hw_masks); 4885 stats->hw_masks = NULL; 4886 kfree(stats->sw_stats); 4887 stats->sw_stats = NULL; 4888 if (stats->hw_stats) { 4889 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 4890 stats->hw_stats_map); 4891 stats->hw_stats = NULL; 4892 } 4893 } 4894 4895 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 4896 bool alloc_masks) 4897 { 4898 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 4899 &stats->hw_stats_map, GFP_KERNEL); 4900 if (!stats->hw_stats) 4901 return -ENOMEM; 4902 4903 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 4904 if (!stats->sw_stats) 4905 goto stats_mem_err; 4906 4907 if (alloc_masks) { 4908 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 4909 if (!stats->hw_masks) 4910 goto stats_mem_err; 4911 } 4912 return 0; 4913 4914 stats_mem_err: 4915 bnxt_free_stats_mem(bp, stats); 4916 return -ENOMEM; 4917 } 4918 4919 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 4920 { 4921 int i; 4922 4923 for (i = 0; i < count; i++) 4924 mask_arr[i] = mask; 4925 } 4926 4927 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 4928 { 4929 int i; 4930 4931 for (i = 0; i < count; i++) 4932 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 4933 } 4934 4935 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 4936 struct bnxt_stats_mem *stats) 4937 { 4938 struct hwrm_func_qstats_ext_output *resp; 4939 struct hwrm_func_qstats_ext_input *req; 4940 __le64 *hw_masks; 4941 int rc; 4942 4943 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 4944 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4945 return -EOPNOTSUPP; 4946 4947 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 4948 if (rc) 4949 return rc; 4950 4951 req->fid = cpu_to_le16(0xffff); 4952 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4953 4954 resp = hwrm_req_hold(bp, req); 4955 rc = hwrm_req_send(bp, req); 4956 if (!rc) { 4957 hw_masks = &resp->rx_ucast_pkts; 4958 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 4959 } 4960 hwrm_req_drop(bp, req); 4961 return rc; 4962 } 4963 4964 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 4965 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 4966 4967 static void bnxt_init_stats(struct bnxt *bp) 4968 { 4969 struct bnxt_napi *bnapi = bp->bnapi[0]; 4970 struct bnxt_cp_ring_info *cpr; 4971 struct bnxt_stats_mem *stats; 4972 __le64 *rx_stats, *tx_stats; 4973 int rc, rx_count, tx_count; 4974 u64 *rx_masks, *tx_masks; 4975 u64 mask; 4976 u8 flags; 4977 4978 cpr = &bnapi->cp_ring; 4979 stats = &cpr->stats; 4980 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 4981 if (rc) { 4982 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4983 mask = (1ULL << 48) - 1; 4984 else 4985 mask = -1ULL; 4986 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 4987 } 4988 if (bp->flags & BNXT_FLAG_PORT_STATS) { 4989 stats = &bp->port_stats; 4990 rx_stats = stats->hw_stats; 4991 rx_masks = stats->hw_masks; 4992 rx_count = sizeof(struct rx_port_stats) / 8; 4993 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4994 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4995 tx_count = sizeof(struct tx_port_stats) / 8; 4996 4997 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 4998 rc = bnxt_hwrm_port_qstats(bp, flags); 4999 if (rc) { 5000 mask = (1ULL << 40) - 1; 5001 5002 bnxt_fill_masks(rx_masks, mask, rx_count); 5003 bnxt_fill_masks(tx_masks, mask, tx_count); 5004 } else { 5005 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5006 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5007 bnxt_hwrm_port_qstats(bp, 0); 5008 } 5009 } 5010 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5011 stats = &bp->rx_port_stats_ext; 5012 rx_stats = stats->hw_stats; 5013 rx_masks = stats->hw_masks; 5014 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5015 stats = &bp->tx_port_stats_ext; 5016 tx_stats = stats->hw_stats; 5017 tx_masks = stats->hw_masks; 5018 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5019 5020 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5021 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5022 if (rc) { 5023 mask = (1ULL << 40) - 1; 5024 5025 bnxt_fill_masks(rx_masks, mask, rx_count); 5026 if (tx_stats) 5027 bnxt_fill_masks(tx_masks, mask, tx_count); 5028 } else { 5029 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5030 if (tx_stats) 5031 bnxt_copy_hw_masks(tx_masks, tx_stats, 5032 tx_count); 5033 bnxt_hwrm_port_qstats_ext(bp, 0); 5034 } 5035 } 5036 } 5037 5038 static void bnxt_free_port_stats(struct bnxt *bp) 5039 { 5040 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5041 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5042 5043 bnxt_free_stats_mem(bp, &bp->port_stats); 5044 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5045 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5046 } 5047 5048 static void bnxt_free_ring_stats(struct bnxt *bp) 5049 { 5050 int i; 5051 5052 if (!bp->bnapi) 5053 return; 5054 5055 for (i = 0; i < bp->cp_nr_rings; i++) { 5056 struct bnxt_napi *bnapi = bp->bnapi[i]; 5057 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5058 5059 bnxt_free_stats_mem(bp, &cpr->stats); 5060 5061 kfree(cpr->sw_stats); 5062 cpr->sw_stats = NULL; 5063 } 5064 } 5065 5066 static int bnxt_alloc_stats(struct bnxt *bp) 5067 { 5068 u32 size, i; 5069 int rc; 5070 5071 size = bp->hw_ring_stats_size; 5072 5073 for (i = 0; i < bp->cp_nr_rings; i++) { 5074 struct bnxt_napi *bnapi = bp->bnapi[i]; 5075 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5076 5077 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 5078 if (!cpr->sw_stats) 5079 return -ENOMEM; 5080 5081 cpr->stats.len = size; 5082 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5083 if (rc) 5084 return rc; 5085 5086 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5087 } 5088 5089 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5090 return 0; 5091 5092 if (bp->port_stats.hw_stats) 5093 goto alloc_ext_stats; 5094 5095 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5096 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5097 if (rc) 5098 return rc; 5099 5100 bp->flags |= BNXT_FLAG_PORT_STATS; 5101 5102 alloc_ext_stats: 5103 /* Display extended statistics only if FW supports it */ 5104 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5105 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5106 return 0; 5107 5108 if (bp->rx_port_stats_ext.hw_stats) 5109 goto alloc_tx_ext_stats; 5110 5111 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5112 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5113 /* Extended stats are optional */ 5114 if (rc) 5115 return 0; 5116 5117 alloc_tx_ext_stats: 5118 if (bp->tx_port_stats_ext.hw_stats) 5119 return 0; 5120 5121 if (bp->hwrm_spec_code >= 0x10902 || 5122 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5123 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5124 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5125 /* Extended stats are optional */ 5126 if (rc) 5127 return 0; 5128 } 5129 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5130 return 0; 5131 } 5132 5133 static void bnxt_clear_ring_indices(struct bnxt *bp) 5134 { 5135 int i, j; 5136 5137 if (!bp->bnapi) 5138 return; 5139 5140 for (i = 0; i < bp->cp_nr_rings; i++) { 5141 struct bnxt_napi *bnapi = bp->bnapi[i]; 5142 struct bnxt_cp_ring_info *cpr; 5143 struct bnxt_rx_ring_info *rxr; 5144 struct bnxt_tx_ring_info *txr; 5145 5146 if (!bnapi) 5147 continue; 5148 5149 cpr = &bnapi->cp_ring; 5150 cpr->cp_raw_cons = 0; 5151 5152 bnxt_for_each_napi_tx(j, bnapi, txr) { 5153 txr->tx_prod = 0; 5154 txr->tx_cons = 0; 5155 txr->tx_hw_cons = 0; 5156 } 5157 5158 rxr = bnapi->rx_ring; 5159 if (rxr) { 5160 rxr->rx_prod = 0; 5161 rxr->rx_agg_prod = 0; 5162 rxr->rx_sw_agg_prod = 0; 5163 rxr->rx_next_cons = 0; 5164 } 5165 bnapi->events = 0; 5166 } 5167 } 5168 5169 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5170 { 5171 u8 type = fltr->type, flags = fltr->flags; 5172 5173 INIT_LIST_HEAD(&fltr->list); 5174 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5175 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5176 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5177 } 5178 5179 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5180 { 5181 if (!list_empty(&fltr->list)) 5182 list_del_init(&fltr->list); 5183 } 5184 5185 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5186 { 5187 struct bnxt_filter_base *usr_fltr, *tmp; 5188 5189 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5190 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5191 continue; 5192 bnxt_del_one_usr_fltr(bp, usr_fltr); 5193 } 5194 } 5195 5196 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5197 { 5198 hlist_del(&fltr->hash); 5199 bnxt_del_one_usr_fltr(bp, fltr); 5200 if (fltr->flags) { 5201 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5202 bp->ntp_fltr_count--; 5203 } 5204 kfree(fltr); 5205 } 5206 5207 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5208 { 5209 int i; 5210 5211 /* Under rtnl_lock and all our NAPIs have been disabled. It's 5212 * safe to delete the hash table. 5213 */ 5214 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5215 struct hlist_head *head; 5216 struct hlist_node *tmp; 5217 struct bnxt_ntuple_filter *fltr; 5218 5219 head = &bp->ntp_fltr_hash_tbl[i]; 5220 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5221 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5222 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5223 !list_empty(&fltr->base.list))) 5224 continue; 5225 bnxt_del_fltr(bp, &fltr->base); 5226 } 5227 } 5228 if (!all) 5229 return; 5230 5231 bitmap_free(bp->ntp_fltr_bmap); 5232 bp->ntp_fltr_bmap = NULL; 5233 bp->ntp_fltr_count = 0; 5234 } 5235 5236 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5237 { 5238 int i, rc = 0; 5239 5240 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5241 return 0; 5242 5243 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5244 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5245 5246 bp->ntp_fltr_count = 0; 5247 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5248 5249 if (!bp->ntp_fltr_bmap) 5250 rc = -ENOMEM; 5251 5252 return rc; 5253 } 5254 5255 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5256 { 5257 int i; 5258 5259 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5260 struct hlist_head *head; 5261 struct hlist_node *tmp; 5262 struct bnxt_l2_filter *fltr; 5263 5264 head = &bp->l2_fltr_hash_tbl[i]; 5265 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5266 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5267 !list_empty(&fltr->base.list))) 5268 continue; 5269 bnxt_del_fltr(bp, &fltr->base); 5270 } 5271 } 5272 } 5273 5274 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5275 { 5276 int i; 5277 5278 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5279 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5280 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5281 } 5282 5283 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5284 { 5285 bnxt_free_vnic_attributes(bp); 5286 bnxt_free_tx_rings(bp); 5287 bnxt_free_rx_rings(bp); 5288 bnxt_free_cp_rings(bp); 5289 bnxt_free_all_cp_arrays(bp); 5290 bnxt_free_ntp_fltrs(bp, false); 5291 bnxt_free_l2_filters(bp, false); 5292 if (irq_re_init) { 5293 bnxt_free_ring_stats(bp); 5294 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5295 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5296 bnxt_free_port_stats(bp); 5297 bnxt_free_ring_grps(bp); 5298 bnxt_free_vnics(bp); 5299 kfree(bp->tx_ring_map); 5300 bp->tx_ring_map = NULL; 5301 kfree(bp->tx_ring); 5302 bp->tx_ring = NULL; 5303 kfree(bp->rx_ring); 5304 bp->rx_ring = NULL; 5305 kfree(bp->bnapi); 5306 bp->bnapi = NULL; 5307 } else { 5308 bnxt_clear_ring_indices(bp); 5309 } 5310 } 5311 5312 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5313 { 5314 int i, j, rc, size, arr_size; 5315 void *bnapi; 5316 5317 if (irq_re_init) { 5318 /* Allocate bnapi mem pointer array and mem block for 5319 * all queues 5320 */ 5321 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5322 bp->cp_nr_rings); 5323 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5324 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5325 if (!bnapi) 5326 return -ENOMEM; 5327 5328 bp->bnapi = bnapi; 5329 bnapi += arr_size; 5330 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5331 bp->bnapi[i] = bnapi; 5332 bp->bnapi[i]->index = i; 5333 bp->bnapi[i]->bp = bp; 5334 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5335 struct bnxt_cp_ring_info *cpr = 5336 &bp->bnapi[i]->cp_ring; 5337 5338 cpr->cp_ring_struct.ring_mem.flags = 5339 BNXT_RMEM_RING_PTE_FLAG; 5340 } 5341 } 5342 5343 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5344 sizeof(struct bnxt_rx_ring_info), 5345 GFP_KERNEL); 5346 if (!bp->rx_ring) 5347 return -ENOMEM; 5348 5349 for (i = 0; i < bp->rx_nr_rings; i++) { 5350 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5351 5352 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5353 rxr->rx_ring_struct.ring_mem.flags = 5354 BNXT_RMEM_RING_PTE_FLAG; 5355 rxr->rx_agg_ring_struct.ring_mem.flags = 5356 BNXT_RMEM_RING_PTE_FLAG; 5357 } else { 5358 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5359 } 5360 rxr->bnapi = bp->bnapi[i]; 5361 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5362 } 5363 5364 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5365 sizeof(struct bnxt_tx_ring_info), 5366 GFP_KERNEL); 5367 if (!bp->tx_ring) 5368 return -ENOMEM; 5369 5370 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5371 GFP_KERNEL); 5372 5373 if (!bp->tx_ring_map) 5374 return -ENOMEM; 5375 5376 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5377 j = 0; 5378 else 5379 j = bp->rx_nr_rings; 5380 5381 for (i = 0; i < bp->tx_nr_rings; i++) { 5382 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5383 struct bnxt_napi *bnapi2; 5384 5385 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5386 txr->tx_ring_struct.ring_mem.flags = 5387 BNXT_RMEM_RING_PTE_FLAG; 5388 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5389 if (i >= bp->tx_nr_rings_xdp) { 5390 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5391 5392 bnapi2 = bp->bnapi[k]; 5393 txr->txq_index = i - bp->tx_nr_rings_xdp; 5394 txr->tx_napi_idx = 5395 BNXT_RING_TO_TC(bp, txr->txq_index); 5396 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5397 bnapi2->tx_int = bnxt_tx_int; 5398 } else { 5399 bnapi2 = bp->bnapi[j]; 5400 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5401 bnapi2->tx_ring[0] = txr; 5402 bnapi2->tx_int = bnxt_tx_int_xdp; 5403 j++; 5404 } 5405 txr->bnapi = bnapi2; 5406 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5407 txr->tx_cpr = &bnapi2->cp_ring; 5408 } 5409 5410 rc = bnxt_alloc_stats(bp); 5411 if (rc) 5412 goto alloc_mem_err; 5413 bnxt_init_stats(bp); 5414 5415 rc = bnxt_alloc_ntp_fltrs(bp); 5416 if (rc) 5417 goto alloc_mem_err; 5418 5419 rc = bnxt_alloc_vnics(bp); 5420 if (rc) 5421 goto alloc_mem_err; 5422 } 5423 5424 rc = bnxt_alloc_all_cp_arrays(bp); 5425 if (rc) 5426 goto alloc_mem_err; 5427 5428 bnxt_init_ring_struct(bp); 5429 5430 rc = bnxt_alloc_rx_rings(bp); 5431 if (rc) 5432 goto alloc_mem_err; 5433 5434 rc = bnxt_alloc_tx_rings(bp); 5435 if (rc) 5436 goto alloc_mem_err; 5437 5438 rc = bnxt_alloc_cp_rings(bp); 5439 if (rc) 5440 goto alloc_mem_err; 5441 5442 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5443 BNXT_VNIC_MCAST_FLAG | 5444 BNXT_VNIC_UCAST_FLAG; 5445 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5446 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5447 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5448 5449 rc = bnxt_alloc_vnic_attributes(bp); 5450 if (rc) 5451 goto alloc_mem_err; 5452 return 0; 5453 5454 alloc_mem_err: 5455 bnxt_free_mem(bp, true); 5456 return rc; 5457 } 5458 5459 static void bnxt_disable_int(struct bnxt *bp) 5460 { 5461 int i; 5462 5463 if (!bp->bnapi) 5464 return; 5465 5466 for (i = 0; i < bp->cp_nr_rings; i++) { 5467 struct bnxt_napi *bnapi = bp->bnapi[i]; 5468 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5469 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5470 5471 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5472 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5473 } 5474 } 5475 5476 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5477 { 5478 struct bnxt_napi *bnapi = bp->bnapi[n]; 5479 struct bnxt_cp_ring_info *cpr; 5480 5481 cpr = &bnapi->cp_ring; 5482 return cpr->cp_ring_struct.map_idx; 5483 } 5484 5485 static void bnxt_disable_int_sync(struct bnxt *bp) 5486 { 5487 int i; 5488 5489 if (!bp->irq_tbl) 5490 return; 5491 5492 atomic_inc(&bp->intr_sem); 5493 5494 bnxt_disable_int(bp); 5495 for (i = 0; i < bp->cp_nr_rings; i++) { 5496 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5497 5498 synchronize_irq(bp->irq_tbl[map_idx].vector); 5499 } 5500 } 5501 5502 static void bnxt_enable_int(struct bnxt *bp) 5503 { 5504 int i; 5505 5506 atomic_set(&bp->intr_sem, 0); 5507 for (i = 0; i < bp->cp_nr_rings; i++) { 5508 struct bnxt_napi *bnapi = bp->bnapi[i]; 5509 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5510 5511 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5512 } 5513 } 5514 5515 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5516 bool async_only) 5517 { 5518 DECLARE_BITMAP(async_events_bmap, 256); 5519 u32 *events = (u32 *)async_events_bmap; 5520 struct hwrm_func_drv_rgtr_output *resp; 5521 struct hwrm_func_drv_rgtr_input *req; 5522 u32 flags; 5523 int rc, i; 5524 5525 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5526 if (rc) 5527 return rc; 5528 5529 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5530 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5531 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5532 5533 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5534 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5535 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5536 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5537 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5538 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5539 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5540 req->flags = cpu_to_le32(flags); 5541 req->ver_maj_8b = DRV_VER_MAJ; 5542 req->ver_min_8b = DRV_VER_MIN; 5543 req->ver_upd_8b = DRV_VER_UPD; 5544 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5545 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5546 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5547 5548 if (BNXT_PF(bp)) { 5549 u32 data[8]; 5550 int i; 5551 5552 memset(data, 0, sizeof(data)); 5553 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5554 u16 cmd = bnxt_vf_req_snif[i]; 5555 unsigned int bit, idx; 5556 5557 idx = cmd / 32; 5558 bit = cmd % 32; 5559 data[idx] |= 1 << bit; 5560 } 5561 5562 for (i = 0; i < 8; i++) 5563 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5564 5565 req->enables |= 5566 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5567 } 5568 5569 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5570 req->flags |= cpu_to_le32( 5571 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5572 5573 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5574 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5575 u16 event_id = bnxt_async_events_arr[i]; 5576 5577 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5578 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5579 continue; 5580 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5581 !bp->ptp_cfg) 5582 continue; 5583 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5584 } 5585 if (bmap && bmap_size) { 5586 for (i = 0; i < bmap_size; i++) { 5587 if (test_bit(i, bmap)) 5588 __set_bit(i, async_events_bmap); 5589 } 5590 } 5591 for (i = 0; i < 8; i++) 5592 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5593 5594 if (async_only) 5595 req->enables = 5596 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5597 5598 resp = hwrm_req_hold(bp, req); 5599 rc = hwrm_req_send(bp, req); 5600 if (!rc) { 5601 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5602 if (resp->flags & 5603 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5604 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5605 } 5606 hwrm_req_drop(bp, req); 5607 return rc; 5608 } 5609 5610 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5611 { 5612 struct hwrm_func_drv_unrgtr_input *req; 5613 int rc; 5614 5615 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5616 return 0; 5617 5618 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5619 if (rc) 5620 return rc; 5621 return hwrm_req_send(bp, req); 5622 } 5623 5624 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5625 5626 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5627 { 5628 struct hwrm_tunnel_dst_port_free_input *req; 5629 int rc; 5630 5631 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5632 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5633 return 0; 5634 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5635 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5636 return 0; 5637 5638 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5639 if (rc) 5640 return rc; 5641 5642 req->tunnel_type = tunnel_type; 5643 5644 switch (tunnel_type) { 5645 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5646 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5647 bp->vxlan_port = 0; 5648 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5649 break; 5650 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5651 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5652 bp->nge_port = 0; 5653 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5654 break; 5655 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5656 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5657 bp->vxlan_gpe_port = 0; 5658 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5659 break; 5660 default: 5661 break; 5662 } 5663 5664 rc = hwrm_req_send(bp, req); 5665 if (rc) 5666 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5667 rc); 5668 if (bp->flags & BNXT_FLAG_TPA) 5669 bnxt_set_tpa(bp, true); 5670 return rc; 5671 } 5672 5673 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5674 u8 tunnel_type) 5675 { 5676 struct hwrm_tunnel_dst_port_alloc_output *resp; 5677 struct hwrm_tunnel_dst_port_alloc_input *req; 5678 int rc; 5679 5680 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5681 if (rc) 5682 return rc; 5683 5684 req->tunnel_type = tunnel_type; 5685 req->tunnel_dst_port_val = port; 5686 5687 resp = hwrm_req_hold(bp, req); 5688 rc = hwrm_req_send(bp, req); 5689 if (rc) { 5690 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5691 rc); 5692 goto err_out; 5693 } 5694 5695 switch (tunnel_type) { 5696 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5697 bp->vxlan_port = port; 5698 bp->vxlan_fw_dst_port_id = 5699 le16_to_cpu(resp->tunnel_dst_port_id); 5700 break; 5701 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5702 bp->nge_port = port; 5703 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5704 break; 5705 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5706 bp->vxlan_gpe_port = port; 5707 bp->vxlan_gpe_fw_dst_port_id = 5708 le16_to_cpu(resp->tunnel_dst_port_id); 5709 break; 5710 default: 5711 break; 5712 } 5713 if (bp->flags & BNXT_FLAG_TPA) 5714 bnxt_set_tpa(bp, true); 5715 5716 err_out: 5717 hwrm_req_drop(bp, req); 5718 return rc; 5719 } 5720 5721 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5722 { 5723 struct hwrm_cfa_l2_set_rx_mask_input *req; 5724 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5725 int rc; 5726 5727 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5728 if (rc) 5729 return rc; 5730 5731 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5732 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5733 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5734 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5735 } 5736 req->mask = cpu_to_le32(vnic->rx_mask); 5737 return hwrm_req_send_silent(bp, req); 5738 } 5739 5740 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5741 { 5742 if (!atomic_dec_and_test(&fltr->refcnt)) 5743 return; 5744 spin_lock_bh(&bp->ntp_fltr_lock); 5745 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5746 spin_unlock_bh(&bp->ntp_fltr_lock); 5747 return; 5748 } 5749 hlist_del_rcu(&fltr->base.hash); 5750 bnxt_del_one_usr_fltr(bp, &fltr->base); 5751 if (fltr->base.flags) { 5752 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5753 bp->ntp_fltr_count--; 5754 } 5755 spin_unlock_bh(&bp->ntp_fltr_lock); 5756 kfree_rcu(fltr, base.rcu); 5757 } 5758 5759 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5760 struct bnxt_l2_key *key, 5761 u32 idx) 5762 { 5763 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5764 struct bnxt_l2_filter *fltr; 5765 5766 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5767 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5768 5769 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5770 l2_key->vlan == key->vlan) 5771 return fltr; 5772 } 5773 return NULL; 5774 } 5775 5776 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5777 struct bnxt_l2_key *key, 5778 u32 idx) 5779 { 5780 struct bnxt_l2_filter *fltr = NULL; 5781 5782 rcu_read_lock(); 5783 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5784 if (fltr) 5785 atomic_inc(&fltr->refcnt); 5786 rcu_read_unlock(); 5787 return fltr; 5788 } 5789 5790 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5791 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5792 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5793 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5794 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5795 5796 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5797 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5798 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5799 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5800 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5801 5802 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5803 { 5804 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5805 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5806 return sizeof(fkeys->addrs.v4addrs) + 5807 sizeof(fkeys->ports); 5808 5809 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5810 return sizeof(fkeys->addrs.v4addrs); 5811 } 5812 5813 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5814 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5815 return sizeof(fkeys->addrs.v6addrs) + 5816 sizeof(fkeys->ports); 5817 5818 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5819 return sizeof(fkeys->addrs.v6addrs); 5820 } 5821 5822 return 0; 5823 } 5824 5825 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5826 const unsigned char *key) 5827 { 5828 u64 prefix = bp->toeplitz_prefix, hash = 0; 5829 struct bnxt_ipv4_tuple tuple4; 5830 struct bnxt_ipv6_tuple tuple6; 5831 int i, j, len = 0; 5832 u8 *four_tuple; 5833 5834 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5835 if (!len) 5836 return 0; 5837 5838 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5839 tuple4.v4addrs = fkeys->addrs.v4addrs; 5840 tuple4.ports = fkeys->ports; 5841 four_tuple = (unsigned char *)&tuple4; 5842 } else { 5843 tuple6.v6addrs = fkeys->addrs.v6addrs; 5844 tuple6.ports = fkeys->ports; 5845 four_tuple = (unsigned char *)&tuple6; 5846 } 5847 5848 for (i = 0, j = 8; i < len; i++, j++) { 5849 u8 byte = four_tuple[i]; 5850 int bit; 5851 5852 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5853 if (byte & 0x80) 5854 hash ^= prefix; 5855 } 5856 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5857 } 5858 5859 /* The valid part of the hash is in the upper 32 bits. */ 5860 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5861 } 5862 5863 #ifdef CONFIG_RFS_ACCEL 5864 static struct bnxt_l2_filter * 5865 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 5866 { 5867 struct bnxt_l2_filter *fltr; 5868 u32 idx; 5869 5870 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5871 BNXT_L2_FLTR_HASH_MASK; 5872 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5873 return fltr; 5874 } 5875 #endif 5876 5877 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 5878 struct bnxt_l2_key *key, u32 idx) 5879 { 5880 struct hlist_head *head; 5881 5882 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 5883 fltr->l2_key.vlan = key->vlan; 5884 fltr->base.type = BNXT_FLTR_TYPE_L2; 5885 if (fltr->base.flags) { 5886 int bit_id; 5887 5888 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 5889 bp->max_fltr, 0); 5890 if (bit_id < 0) 5891 return -ENOMEM; 5892 fltr->base.sw_id = (u16)bit_id; 5893 bp->ntp_fltr_count++; 5894 } 5895 head = &bp->l2_fltr_hash_tbl[idx]; 5896 hlist_add_head_rcu(&fltr->base.hash, head); 5897 bnxt_insert_usr_fltr(bp, &fltr->base); 5898 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 5899 atomic_set(&fltr->refcnt, 1); 5900 return 0; 5901 } 5902 5903 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 5904 struct bnxt_l2_key *key, 5905 gfp_t gfp) 5906 { 5907 struct bnxt_l2_filter *fltr; 5908 u32 idx; 5909 int rc; 5910 5911 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5912 BNXT_L2_FLTR_HASH_MASK; 5913 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5914 if (fltr) 5915 return fltr; 5916 5917 fltr = kzalloc(sizeof(*fltr), gfp); 5918 if (!fltr) 5919 return ERR_PTR(-ENOMEM); 5920 spin_lock_bh(&bp->ntp_fltr_lock); 5921 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5922 spin_unlock_bh(&bp->ntp_fltr_lock); 5923 if (rc) { 5924 bnxt_del_l2_filter(bp, fltr); 5925 fltr = ERR_PTR(rc); 5926 } 5927 return fltr; 5928 } 5929 5930 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 5931 struct bnxt_l2_key *key, 5932 u16 flags) 5933 { 5934 struct bnxt_l2_filter *fltr; 5935 u32 idx; 5936 int rc; 5937 5938 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5939 BNXT_L2_FLTR_HASH_MASK; 5940 spin_lock_bh(&bp->ntp_fltr_lock); 5941 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5942 if (fltr) { 5943 fltr = ERR_PTR(-EEXIST); 5944 goto l2_filter_exit; 5945 } 5946 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 5947 if (!fltr) { 5948 fltr = ERR_PTR(-ENOMEM); 5949 goto l2_filter_exit; 5950 } 5951 fltr->base.flags = flags; 5952 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5953 if (rc) { 5954 spin_unlock_bh(&bp->ntp_fltr_lock); 5955 bnxt_del_l2_filter(bp, fltr); 5956 return ERR_PTR(rc); 5957 } 5958 5959 l2_filter_exit: 5960 spin_unlock_bh(&bp->ntp_fltr_lock); 5961 return fltr; 5962 } 5963 5964 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 5965 { 5966 #ifdef CONFIG_BNXT_SRIOV 5967 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 5968 5969 return vf->fw_fid; 5970 #else 5971 return INVALID_HW_RING_ID; 5972 #endif 5973 } 5974 5975 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5976 { 5977 struct hwrm_cfa_l2_filter_free_input *req; 5978 u16 target_id = 0xffff; 5979 int rc; 5980 5981 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 5982 struct bnxt_pf_info *pf = &bp->pf; 5983 5984 if (fltr->base.vf_idx >= pf->active_vfs) 5985 return -EINVAL; 5986 5987 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 5988 if (target_id == INVALID_HW_RING_ID) 5989 return -EINVAL; 5990 } 5991 5992 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 5993 if (rc) 5994 return rc; 5995 5996 req->target_id = cpu_to_le16(target_id); 5997 req->l2_filter_id = fltr->base.filter_id; 5998 return hwrm_req_send(bp, req); 5999 } 6000 6001 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6002 { 6003 struct hwrm_cfa_l2_filter_alloc_output *resp; 6004 struct hwrm_cfa_l2_filter_alloc_input *req; 6005 u16 target_id = 0xffff; 6006 int rc; 6007 6008 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6009 struct bnxt_pf_info *pf = &bp->pf; 6010 6011 if (fltr->base.vf_idx >= pf->active_vfs) 6012 return -EINVAL; 6013 6014 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6015 } 6016 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6017 if (rc) 6018 return rc; 6019 6020 req->target_id = cpu_to_le16(target_id); 6021 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6022 6023 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6024 req->flags |= 6025 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6026 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6027 req->enables = 6028 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6029 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6030 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6031 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6032 eth_broadcast_addr(req->l2_addr_mask); 6033 6034 if (fltr->l2_key.vlan) { 6035 req->enables |= 6036 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6037 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6038 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6039 req->num_vlans = 1; 6040 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6041 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6042 } 6043 6044 resp = hwrm_req_hold(bp, req); 6045 rc = hwrm_req_send(bp, req); 6046 if (!rc) { 6047 fltr->base.filter_id = resp->l2_filter_id; 6048 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6049 } 6050 hwrm_req_drop(bp, req); 6051 return rc; 6052 } 6053 6054 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6055 struct bnxt_ntuple_filter *fltr) 6056 { 6057 struct hwrm_cfa_ntuple_filter_free_input *req; 6058 int rc; 6059 6060 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6061 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6062 if (rc) 6063 return rc; 6064 6065 req->ntuple_filter_id = fltr->base.filter_id; 6066 return hwrm_req_send(bp, req); 6067 } 6068 6069 #define BNXT_NTP_FLTR_FLAGS \ 6070 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6071 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6072 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6073 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6074 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6075 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6076 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6077 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6078 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6079 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6080 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6081 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6082 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6083 6084 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6085 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6086 6087 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6088 { 6089 int i; 6090 6091 for (i = 0; i < 4; i++) 6092 mask[i] = cpu_to_be32(~0); 6093 } 6094 6095 static void 6096 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6097 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6098 struct bnxt_ntuple_filter *fltr) 6099 { 6100 u16 rxq = fltr->base.rxq; 6101 6102 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6103 struct ethtool_rxfh_context *ctx; 6104 struct bnxt_rss_ctx *rss_ctx; 6105 struct bnxt_vnic_info *vnic; 6106 6107 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6108 fltr->base.fw_vnic_id); 6109 if (ctx) { 6110 rss_ctx = ethtool_rxfh_context_priv(ctx); 6111 vnic = &rss_ctx->vnic; 6112 6113 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6114 } 6115 return; 6116 } 6117 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6118 struct bnxt_vnic_info *vnic; 6119 u32 enables; 6120 6121 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6122 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6123 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6124 req->enables |= cpu_to_le32(enables); 6125 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6126 } else { 6127 u32 flags; 6128 6129 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6130 req->flags |= cpu_to_le32(flags); 6131 req->dst_id = cpu_to_le16(rxq); 6132 } 6133 } 6134 6135 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6136 struct bnxt_ntuple_filter *fltr) 6137 { 6138 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6139 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6140 struct bnxt_flow_masks *masks = &fltr->fmasks; 6141 struct flow_keys *keys = &fltr->fkeys; 6142 struct bnxt_l2_filter *l2_fltr; 6143 struct bnxt_vnic_info *vnic; 6144 int rc; 6145 6146 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6147 if (rc) 6148 return rc; 6149 6150 l2_fltr = fltr->l2_fltr; 6151 req->l2_filter_id = l2_fltr->base.filter_id; 6152 6153 if (fltr->base.flags & BNXT_ACT_DROP) { 6154 req->flags = 6155 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6156 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6157 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6158 } else { 6159 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6160 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6161 } 6162 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6163 6164 req->ethertype = htons(ETH_P_IP); 6165 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6166 req->ip_protocol = keys->basic.ip_proto; 6167 6168 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6169 req->ethertype = htons(ETH_P_IPV6); 6170 req->ip_addr_type = 6171 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6172 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6173 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6174 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6175 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6176 } else { 6177 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6178 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6179 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6180 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6181 } 6182 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6183 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6184 req->tunnel_type = 6185 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6186 } 6187 6188 req->src_port = keys->ports.src; 6189 req->src_port_mask = masks->ports.src; 6190 req->dst_port = keys->ports.dst; 6191 req->dst_port_mask = masks->ports.dst; 6192 6193 resp = hwrm_req_hold(bp, req); 6194 rc = hwrm_req_send(bp, req); 6195 if (!rc) 6196 fltr->base.filter_id = resp->ntuple_filter_id; 6197 hwrm_req_drop(bp, req); 6198 return rc; 6199 } 6200 6201 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6202 const u8 *mac_addr) 6203 { 6204 struct bnxt_l2_filter *fltr; 6205 struct bnxt_l2_key key; 6206 int rc; 6207 6208 ether_addr_copy(key.dst_mac_addr, mac_addr); 6209 key.vlan = 0; 6210 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6211 if (IS_ERR(fltr)) 6212 return PTR_ERR(fltr); 6213 6214 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6215 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6216 if (rc) 6217 bnxt_del_l2_filter(bp, fltr); 6218 else 6219 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6220 return rc; 6221 } 6222 6223 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6224 { 6225 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6226 6227 /* Any associated ntuple filters will also be cleared by firmware. */ 6228 for (i = 0; i < num_of_vnics; i++) { 6229 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6230 6231 for (j = 0; j < vnic->uc_filter_count; j++) { 6232 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6233 6234 bnxt_hwrm_l2_filter_free(bp, fltr); 6235 bnxt_del_l2_filter(bp, fltr); 6236 } 6237 vnic->uc_filter_count = 0; 6238 } 6239 } 6240 6241 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6242 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6243 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6244 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6245 6246 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6247 struct hwrm_vnic_tpa_cfg_input *req) 6248 { 6249 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6250 6251 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6252 return; 6253 6254 if (bp->vxlan_port) 6255 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6256 if (bp->vxlan_gpe_port) 6257 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6258 if (bp->nge_port) 6259 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6260 6261 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6262 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6263 } 6264 6265 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6266 u32 tpa_flags) 6267 { 6268 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6269 struct hwrm_vnic_tpa_cfg_input *req; 6270 int rc; 6271 6272 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6273 return 0; 6274 6275 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6276 if (rc) 6277 return rc; 6278 6279 if (tpa_flags) { 6280 u16 mss = bp->dev->mtu - 40; 6281 u32 nsegs, n, segs = 0, flags; 6282 6283 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6284 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6285 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6286 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6287 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6288 if (tpa_flags & BNXT_FLAG_GRO) 6289 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6290 6291 req->flags = cpu_to_le32(flags); 6292 6293 req->enables = 6294 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6295 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6296 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6297 6298 /* Number of segs are log2 units, and first packet is not 6299 * included as part of this units. 6300 */ 6301 if (mss <= BNXT_RX_PAGE_SIZE) { 6302 n = BNXT_RX_PAGE_SIZE / mss; 6303 nsegs = (MAX_SKB_FRAGS - 1) * n; 6304 } else { 6305 n = mss / BNXT_RX_PAGE_SIZE; 6306 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6307 n++; 6308 nsegs = (MAX_SKB_FRAGS - n) / n; 6309 } 6310 6311 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6312 segs = MAX_TPA_SEGS_P5; 6313 max_aggs = bp->max_tpa; 6314 } else { 6315 segs = ilog2(nsegs); 6316 } 6317 req->max_agg_segs = cpu_to_le16(segs); 6318 req->max_aggs = cpu_to_le16(max_aggs); 6319 6320 req->min_agg_len = cpu_to_le32(512); 6321 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6322 } 6323 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6324 6325 return hwrm_req_send(bp, req); 6326 } 6327 6328 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6329 { 6330 struct bnxt_ring_grp_info *grp_info; 6331 6332 grp_info = &bp->grp_info[ring->grp_idx]; 6333 return grp_info->cp_fw_ring_id; 6334 } 6335 6336 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6337 { 6338 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6339 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6340 else 6341 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6342 } 6343 6344 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6345 { 6346 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6347 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6348 else 6349 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6350 } 6351 6352 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6353 { 6354 int entries; 6355 6356 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6357 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6358 else 6359 entries = HW_HASH_INDEX_SIZE; 6360 6361 bp->rss_indir_tbl_entries = entries; 6362 bp->rss_indir_tbl = 6363 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6364 if (!bp->rss_indir_tbl) 6365 return -ENOMEM; 6366 6367 return 0; 6368 } 6369 6370 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6371 struct ethtool_rxfh_context *rss_ctx) 6372 { 6373 u16 max_rings, max_entries, pad, i; 6374 u32 *rss_indir_tbl; 6375 6376 if (!bp->rx_nr_rings) 6377 return; 6378 6379 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6380 max_rings = bp->rx_nr_rings - 1; 6381 else 6382 max_rings = bp->rx_nr_rings; 6383 6384 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6385 if (rss_ctx) 6386 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6387 else 6388 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6389 6390 for (i = 0; i < max_entries; i++) 6391 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6392 6393 pad = bp->rss_indir_tbl_entries - max_entries; 6394 if (pad) 6395 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6396 } 6397 6398 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6399 { 6400 u32 i, tbl_size, max_ring = 0; 6401 6402 if (!bp->rss_indir_tbl) 6403 return 0; 6404 6405 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6406 for (i = 0; i < tbl_size; i++) 6407 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6408 return max_ring; 6409 } 6410 6411 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6412 { 6413 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6414 if (!rx_rings) 6415 return 0; 6416 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6417 BNXT_RSS_TABLE_ENTRIES_P5); 6418 } 6419 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6420 return 2; 6421 return 1; 6422 } 6423 6424 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6425 { 6426 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6427 u16 i, j; 6428 6429 /* Fill the RSS indirection table with ring group ids */ 6430 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6431 if (!no_rss) 6432 j = bp->rss_indir_tbl[i]; 6433 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6434 } 6435 } 6436 6437 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6438 struct bnxt_vnic_info *vnic) 6439 { 6440 __le16 *ring_tbl = vnic->rss_table; 6441 struct bnxt_rx_ring_info *rxr; 6442 u16 tbl_size, i; 6443 6444 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6445 6446 for (i = 0; i < tbl_size; i++) { 6447 u16 ring_id, j; 6448 6449 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6450 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6451 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6452 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6453 else 6454 j = bp->rss_indir_tbl[i]; 6455 rxr = &bp->rx_ring[j]; 6456 6457 ring_id = rxr->rx_ring_struct.fw_ring_id; 6458 *ring_tbl++ = cpu_to_le16(ring_id); 6459 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6460 *ring_tbl++ = cpu_to_le16(ring_id); 6461 } 6462 } 6463 6464 static void 6465 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6466 struct bnxt_vnic_info *vnic) 6467 { 6468 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6469 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6470 if (bp->flags & BNXT_FLAG_CHIP_P7) 6471 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6472 } else { 6473 bnxt_fill_hw_rss_tbl(bp, vnic); 6474 } 6475 6476 if (bp->rss_hash_delta) { 6477 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6478 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6479 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6480 else 6481 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6482 } else { 6483 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6484 } 6485 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6486 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6487 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6488 } 6489 6490 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6491 bool set_rss) 6492 { 6493 struct hwrm_vnic_rss_cfg_input *req; 6494 int rc; 6495 6496 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6497 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6498 return 0; 6499 6500 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6501 if (rc) 6502 return rc; 6503 6504 if (set_rss) 6505 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6506 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6507 return hwrm_req_send(bp, req); 6508 } 6509 6510 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6511 struct bnxt_vnic_info *vnic, bool set_rss) 6512 { 6513 struct hwrm_vnic_rss_cfg_input *req; 6514 dma_addr_t ring_tbl_map; 6515 u32 i, nr_ctxs; 6516 int rc; 6517 6518 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6519 if (rc) 6520 return rc; 6521 6522 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6523 if (!set_rss) 6524 return hwrm_req_send(bp, req); 6525 6526 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6527 ring_tbl_map = vnic->rss_table_dma_addr; 6528 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6529 6530 hwrm_req_hold(bp, req); 6531 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6532 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6533 req->ring_table_pair_index = i; 6534 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6535 rc = hwrm_req_send(bp, req); 6536 if (rc) 6537 goto exit; 6538 } 6539 6540 exit: 6541 hwrm_req_drop(bp, req); 6542 return rc; 6543 } 6544 6545 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6546 { 6547 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6548 struct hwrm_vnic_rss_qcfg_output *resp; 6549 struct hwrm_vnic_rss_qcfg_input *req; 6550 6551 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6552 return; 6553 6554 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6555 /* all contexts configured to same hash_type, zero always exists */ 6556 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6557 resp = hwrm_req_hold(bp, req); 6558 if (!hwrm_req_send(bp, req)) { 6559 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6560 bp->rss_hash_delta = 0; 6561 } 6562 hwrm_req_drop(bp, req); 6563 } 6564 6565 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6566 { 6567 struct hwrm_vnic_plcmodes_cfg_input *req; 6568 int rc; 6569 6570 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6571 if (rc) 6572 return rc; 6573 6574 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6575 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6576 6577 if (BNXT_RX_PAGE_MODE(bp)) { 6578 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6579 } else { 6580 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6581 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6582 req->enables |= 6583 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6584 req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh); 6585 req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh); 6586 } 6587 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6588 return hwrm_req_send(bp, req); 6589 } 6590 6591 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6592 struct bnxt_vnic_info *vnic, 6593 u16 ctx_idx) 6594 { 6595 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6596 6597 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6598 return; 6599 6600 req->rss_cos_lb_ctx_id = 6601 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6602 6603 hwrm_req_send(bp, req); 6604 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6605 } 6606 6607 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6608 { 6609 int i, j; 6610 6611 for (i = 0; i < bp->nr_vnics; i++) { 6612 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6613 6614 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6615 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6616 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6617 } 6618 } 6619 bp->rsscos_nr_ctxs = 0; 6620 } 6621 6622 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6623 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6624 { 6625 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6626 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6627 int rc; 6628 6629 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6630 if (rc) 6631 return rc; 6632 6633 resp = hwrm_req_hold(bp, req); 6634 rc = hwrm_req_send(bp, req); 6635 if (!rc) 6636 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6637 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6638 hwrm_req_drop(bp, req); 6639 6640 return rc; 6641 } 6642 6643 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6644 { 6645 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6646 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6647 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6648 } 6649 6650 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6651 { 6652 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6653 struct hwrm_vnic_cfg_input *req; 6654 unsigned int ring = 0, grp_idx; 6655 u16 def_vlan = 0; 6656 int rc; 6657 6658 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6659 if (rc) 6660 return rc; 6661 6662 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6663 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6664 6665 req->default_rx_ring_id = 6666 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6667 req->default_cmpl_ring_id = 6668 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6669 req->enables = 6670 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6671 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6672 goto vnic_mru; 6673 } 6674 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6675 /* Only RSS support for now TBD: COS & LB */ 6676 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6677 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6678 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6679 VNIC_CFG_REQ_ENABLES_MRU); 6680 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6681 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6682 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6683 VNIC_CFG_REQ_ENABLES_MRU); 6684 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6685 } else { 6686 req->rss_rule = cpu_to_le16(0xffff); 6687 } 6688 6689 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6690 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6691 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6692 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6693 } else { 6694 req->cos_rule = cpu_to_le16(0xffff); 6695 } 6696 6697 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6698 ring = 0; 6699 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6700 ring = vnic->vnic_id - 1; 6701 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6702 ring = bp->rx_nr_rings - 1; 6703 6704 grp_idx = bp->rx_ring[ring].bnapi->index; 6705 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6706 req->lb_rule = cpu_to_le16(0xffff); 6707 vnic_mru: 6708 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 6709 req->mru = cpu_to_le16(vnic->mru); 6710 6711 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6712 #ifdef CONFIG_BNXT_SRIOV 6713 if (BNXT_VF(bp)) 6714 def_vlan = bp->vf.vlan; 6715 #endif 6716 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6717 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6718 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6719 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6720 6721 return hwrm_req_send(bp, req); 6722 } 6723 6724 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6725 struct bnxt_vnic_info *vnic) 6726 { 6727 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6728 struct hwrm_vnic_free_input *req; 6729 6730 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6731 return; 6732 6733 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6734 6735 hwrm_req_send(bp, req); 6736 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6737 } 6738 } 6739 6740 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6741 { 6742 u16 i; 6743 6744 for (i = 0; i < bp->nr_vnics; i++) 6745 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6746 } 6747 6748 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6749 unsigned int start_rx_ring_idx, 6750 unsigned int nr_rings) 6751 { 6752 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6753 struct hwrm_vnic_alloc_output *resp; 6754 struct hwrm_vnic_alloc_input *req; 6755 int rc; 6756 6757 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6758 if (rc) 6759 return rc; 6760 6761 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6762 goto vnic_no_ring_grps; 6763 6764 /* map ring groups to this vnic */ 6765 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6766 grp_idx = bp->rx_ring[i].bnapi->index; 6767 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6768 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6769 j, nr_rings); 6770 break; 6771 } 6772 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6773 } 6774 6775 vnic_no_ring_grps: 6776 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6777 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6778 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6779 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6780 6781 resp = hwrm_req_hold(bp, req); 6782 rc = hwrm_req_send(bp, req); 6783 if (!rc) 6784 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6785 hwrm_req_drop(bp, req); 6786 return rc; 6787 } 6788 6789 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6790 { 6791 struct hwrm_vnic_qcaps_output *resp; 6792 struct hwrm_vnic_qcaps_input *req; 6793 int rc; 6794 6795 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6796 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6797 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6798 if (bp->hwrm_spec_code < 0x10600) 6799 return 0; 6800 6801 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6802 if (rc) 6803 return rc; 6804 6805 resp = hwrm_req_hold(bp, req); 6806 rc = hwrm_req_send(bp, req); 6807 if (!rc) { 6808 u32 flags = le32_to_cpu(resp->flags); 6809 6810 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6811 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6812 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6813 if (flags & 6814 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6815 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6816 6817 /* Older P5 fw before EXT_HW_STATS support did not set 6818 * VLAN_STRIP_CAP properly. 6819 */ 6820 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6821 (BNXT_CHIP_P5(bp) && 6822 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6823 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6824 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6825 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6826 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6827 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6828 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6829 if (bp->max_tpa_v2) { 6830 if (BNXT_CHIP_P5(bp)) 6831 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6832 else 6833 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6834 } 6835 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6836 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6837 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6838 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6839 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6840 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6841 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6842 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6843 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6844 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6845 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6846 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6847 } 6848 hwrm_req_drop(bp, req); 6849 return rc; 6850 } 6851 6852 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6853 { 6854 struct hwrm_ring_grp_alloc_output *resp; 6855 struct hwrm_ring_grp_alloc_input *req; 6856 int rc; 6857 u16 i; 6858 6859 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6860 return 0; 6861 6862 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6863 if (rc) 6864 return rc; 6865 6866 resp = hwrm_req_hold(bp, req); 6867 for (i = 0; i < bp->rx_nr_rings; i++) { 6868 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 6869 6870 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 6871 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 6872 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 6873 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 6874 6875 rc = hwrm_req_send(bp, req); 6876 6877 if (rc) 6878 break; 6879 6880 bp->grp_info[grp_idx].fw_grp_id = 6881 le32_to_cpu(resp->ring_group_id); 6882 } 6883 hwrm_req_drop(bp, req); 6884 return rc; 6885 } 6886 6887 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 6888 { 6889 struct hwrm_ring_grp_free_input *req; 6890 u16 i; 6891 6892 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 6893 return; 6894 6895 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 6896 return; 6897 6898 hwrm_req_hold(bp, req); 6899 for (i = 0; i < bp->cp_nr_rings; i++) { 6900 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 6901 continue; 6902 req->ring_group_id = 6903 cpu_to_le32(bp->grp_info[i].fw_grp_id); 6904 6905 hwrm_req_send(bp, req); 6906 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 6907 } 6908 hwrm_req_drop(bp, req); 6909 } 6910 6911 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 6912 struct bnxt_ring_struct *ring, 6913 u32 ring_type, u32 map_index) 6914 { 6915 struct hwrm_ring_alloc_output *resp; 6916 struct hwrm_ring_alloc_input *req; 6917 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 6918 struct bnxt_ring_grp_info *grp_info; 6919 int rc, err = 0; 6920 u16 ring_id; 6921 6922 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 6923 if (rc) 6924 goto exit; 6925 6926 req->enables = 0; 6927 if (rmem->nr_pages > 1) { 6928 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 6929 /* Page size is in log2 units */ 6930 req->page_size = BNXT_PAGE_SHIFT; 6931 req->page_tbl_depth = 1; 6932 } else { 6933 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 6934 } 6935 req->fbo = 0; 6936 /* Association of ring index with doorbell index and MSIX number */ 6937 req->logical_id = cpu_to_le16(map_index); 6938 6939 switch (ring_type) { 6940 case HWRM_RING_ALLOC_TX: { 6941 struct bnxt_tx_ring_info *txr; 6942 u16 flags = 0; 6943 6944 txr = container_of(ring, struct bnxt_tx_ring_info, 6945 tx_ring_struct); 6946 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 6947 /* Association of transmit ring with completion ring */ 6948 grp_info = &bp->grp_info[ring->grp_idx]; 6949 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 6950 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 6951 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6952 req->queue_id = cpu_to_le16(ring->queue_id); 6953 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 6954 req->cmpl_coal_cnt = 6955 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 6956 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 6957 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 6958 req->flags = cpu_to_le16(flags); 6959 break; 6960 } 6961 case HWRM_RING_ALLOC_RX: 6962 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6963 req->length = cpu_to_le32(bp->rx_ring_mask + 1); 6964 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6965 u16 flags = 0; 6966 6967 /* Association of rx ring with stats context */ 6968 grp_info = &bp->grp_info[ring->grp_idx]; 6969 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 6970 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6971 req->enables |= cpu_to_le32( 6972 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6973 if (NET_IP_ALIGN == 2) 6974 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD; 6975 req->flags = cpu_to_le16(flags); 6976 } 6977 break; 6978 case HWRM_RING_ALLOC_AGG: 6979 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6980 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 6981 /* Association of agg ring with rx ring */ 6982 grp_info = &bp->grp_info[ring->grp_idx]; 6983 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 6984 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 6985 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6986 req->enables |= cpu_to_le32( 6987 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID | 6988 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6989 } else { 6990 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6991 } 6992 req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1); 6993 break; 6994 case HWRM_RING_ALLOC_CMPL: 6995 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 6996 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 6997 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6998 /* Association of cp ring with nq */ 6999 grp_info = &bp->grp_info[map_index]; 7000 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7001 req->cq_handle = cpu_to_le64(ring->handle); 7002 req->enables |= cpu_to_le32( 7003 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7004 } else { 7005 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7006 } 7007 break; 7008 case HWRM_RING_ALLOC_NQ: 7009 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7010 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7011 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7012 break; 7013 default: 7014 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7015 ring_type); 7016 return -1; 7017 } 7018 7019 resp = hwrm_req_hold(bp, req); 7020 rc = hwrm_req_send(bp, req); 7021 err = le16_to_cpu(resp->error_code); 7022 ring_id = le16_to_cpu(resp->ring_id); 7023 hwrm_req_drop(bp, req); 7024 7025 exit: 7026 if (rc || err) { 7027 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7028 ring_type, rc, err); 7029 return -EIO; 7030 } 7031 ring->fw_ring_id = ring_id; 7032 return rc; 7033 } 7034 7035 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7036 { 7037 int rc; 7038 7039 if (BNXT_PF(bp)) { 7040 struct hwrm_func_cfg_input *req; 7041 7042 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7043 if (rc) 7044 return rc; 7045 7046 req->fid = cpu_to_le16(0xffff); 7047 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7048 req->async_event_cr = cpu_to_le16(idx); 7049 return hwrm_req_send(bp, req); 7050 } else { 7051 struct hwrm_func_vf_cfg_input *req; 7052 7053 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7054 if (rc) 7055 return rc; 7056 7057 req->enables = 7058 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7059 req->async_event_cr = cpu_to_le16(idx); 7060 return hwrm_req_send(bp, req); 7061 } 7062 } 7063 7064 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7065 u32 ring_type) 7066 { 7067 switch (ring_type) { 7068 case HWRM_RING_ALLOC_TX: 7069 db->db_ring_mask = bp->tx_ring_mask; 7070 break; 7071 case HWRM_RING_ALLOC_RX: 7072 db->db_ring_mask = bp->rx_ring_mask; 7073 break; 7074 case HWRM_RING_ALLOC_AGG: 7075 db->db_ring_mask = bp->rx_agg_ring_mask; 7076 break; 7077 case HWRM_RING_ALLOC_CMPL: 7078 case HWRM_RING_ALLOC_NQ: 7079 db->db_ring_mask = bp->cp_ring_mask; 7080 break; 7081 } 7082 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7083 db->db_epoch_mask = db->db_ring_mask + 1; 7084 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7085 } 7086 } 7087 7088 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7089 u32 map_idx, u32 xid) 7090 { 7091 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7092 switch (ring_type) { 7093 case HWRM_RING_ALLOC_TX: 7094 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7095 break; 7096 case HWRM_RING_ALLOC_RX: 7097 case HWRM_RING_ALLOC_AGG: 7098 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7099 break; 7100 case HWRM_RING_ALLOC_CMPL: 7101 db->db_key64 = DBR_PATH_L2; 7102 break; 7103 case HWRM_RING_ALLOC_NQ: 7104 db->db_key64 = DBR_PATH_L2; 7105 break; 7106 } 7107 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7108 7109 if (bp->flags & BNXT_FLAG_CHIP_P7) 7110 db->db_key64 |= DBR_VALID; 7111 7112 db->doorbell = bp->bar1 + bp->db_offset; 7113 } else { 7114 db->doorbell = bp->bar1 + map_idx * 0x80; 7115 switch (ring_type) { 7116 case HWRM_RING_ALLOC_TX: 7117 db->db_key32 = DB_KEY_TX; 7118 break; 7119 case HWRM_RING_ALLOC_RX: 7120 case HWRM_RING_ALLOC_AGG: 7121 db->db_key32 = DB_KEY_RX; 7122 break; 7123 case HWRM_RING_ALLOC_CMPL: 7124 db->db_key32 = DB_KEY_CP; 7125 break; 7126 } 7127 } 7128 bnxt_set_db_mask(bp, db, ring_type); 7129 } 7130 7131 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7132 struct bnxt_rx_ring_info *rxr) 7133 { 7134 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7135 struct bnxt_napi *bnapi = rxr->bnapi; 7136 u32 type = HWRM_RING_ALLOC_RX; 7137 u32 map_idx = bnapi->index; 7138 int rc; 7139 7140 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7141 if (rc) 7142 return rc; 7143 7144 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7145 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7146 7147 return 0; 7148 } 7149 7150 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7151 struct bnxt_rx_ring_info *rxr) 7152 { 7153 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7154 u32 type = HWRM_RING_ALLOC_AGG; 7155 u32 grp_idx = ring->grp_idx; 7156 u32 map_idx; 7157 int rc; 7158 7159 map_idx = grp_idx + bp->rx_nr_rings; 7160 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7161 if (rc) 7162 return rc; 7163 7164 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7165 ring->fw_ring_id); 7166 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7167 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7168 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7169 7170 return 0; 7171 } 7172 7173 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7174 { 7175 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7176 int i, rc = 0; 7177 u32 type; 7178 7179 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7180 type = HWRM_RING_ALLOC_NQ; 7181 else 7182 type = HWRM_RING_ALLOC_CMPL; 7183 for (i = 0; i < bp->cp_nr_rings; i++) { 7184 struct bnxt_napi *bnapi = bp->bnapi[i]; 7185 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7186 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7187 u32 map_idx = ring->map_idx; 7188 unsigned int vector; 7189 7190 vector = bp->irq_tbl[map_idx].vector; 7191 disable_irq_nosync(vector); 7192 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7193 if (rc) { 7194 enable_irq(vector); 7195 goto err_out; 7196 } 7197 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7198 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7199 enable_irq(vector); 7200 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7201 7202 if (!i) { 7203 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7204 if (rc) 7205 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7206 } 7207 } 7208 7209 type = HWRM_RING_ALLOC_TX; 7210 for (i = 0; i < bp->tx_nr_rings; i++) { 7211 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7212 struct bnxt_ring_struct *ring; 7213 u32 map_idx; 7214 7215 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7216 struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr; 7217 struct bnxt_napi *bnapi = txr->bnapi; 7218 u32 type2 = HWRM_RING_ALLOC_CMPL; 7219 7220 ring = &cpr2->cp_ring_struct; 7221 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7222 map_idx = bnapi->index; 7223 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7224 if (rc) 7225 goto err_out; 7226 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7227 ring->fw_ring_id); 7228 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7229 } 7230 ring = &txr->tx_ring_struct; 7231 map_idx = i; 7232 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7233 if (rc) 7234 goto err_out; 7235 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id); 7236 } 7237 7238 for (i = 0; i < bp->rx_nr_rings; i++) { 7239 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7240 7241 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7242 if (rc) 7243 goto err_out; 7244 /* If we have agg rings, post agg buffers first. */ 7245 if (!agg_rings) 7246 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7247 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7248 struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr; 7249 struct bnxt_napi *bnapi = rxr->bnapi; 7250 u32 type2 = HWRM_RING_ALLOC_CMPL; 7251 struct bnxt_ring_struct *ring; 7252 u32 map_idx = bnapi->index; 7253 7254 ring = &cpr2->cp_ring_struct; 7255 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7256 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7257 if (rc) 7258 goto err_out; 7259 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7260 ring->fw_ring_id); 7261 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7262 } 7263 } 7264 7265 if (agg_rings) { 7266 for (i = 0; i < bp->rx_nr_rings; i++) { 7267 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7268 if (rc) 7269 goto err_out; 7270 } 7271 } 7272 err_out: 7273 return rc; 7274 } 7275 7276 static void bnxt_cancel_dim(struct bnxt *bp) 7277 { 7278 int i; 7279 7280 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7281 * if NAPI is enabled. 7282 */ 7283 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7284 return; 7285 7286 /* Make sure NAPI sees that the VNIC is disabled */ 7287 synchronize_net(); 7288 for (i = 0; i < bp->rx_nr_rings; i++) { 7289 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7290 struct bnxt_napi *bnapi = rxr->bnapi; 7291 7292 cancel_work_sync(&bnapi->cp_ring.dim.work); 7293 } 7294 } 7295 7296 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7297 struct bnxt_ring_struct *ring, 7298 u32 ring_type, int cmpl_ring_id) 7299 { 7300 struct hwrm_ring_free_output *resp; 7301 struct hwrm_ring_free_input *req; 7302 u16 error_code = 0; 7303 int rc; 7304 7305 if (BNXT_NO_FW_ACCESS(bp)) 7306 return 0; 7307 7308 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7309 if (rc) 7310 goto exit; 7311 7312 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7313 req->ring_type = ring_type; 7314 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7315 7316 resp = hwrm_req_hold(bp, req); 7317 rc = hwrm_req_send(bp, req); 7318 error_code = le16_to_cpu(resp->error_code); 7319 hwrm_req_drop(bp, req); 7320 exit: 7321 if (rc || error_code) { 7322 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7323 ring_type, rc, error_code); 7324 return -EIO; 7325 } 7326 return 0; 7327 } 7328 7329 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7330 struct bnxt_rx_ring_info *rxr, 7331 bool close_path) 7332 { 7333 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7334 u32 grp_idx = rxr->bnapi->index; 7335 u32 cmpl_ring_id; 7336 7337 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7338 return; 7339 7340 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7341 hwrm_ring_free_send_msg(bp, ring, 7342 RING_FREE_REQ_RING_TYPE_RX, 7343 close_path ? cmpl_ring_id : 7344 INVALID_HW_RING_ID); 7345 ring->fw_ring_id = INVALID_HW_RING_ID; 7346 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7347 } 7348 7349 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7350 struct bnxt_rx_ring_info *rxr, 7351 bool close_path) 7352 { 7353 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7354 u32 grp_idx = rxr->bnapi->index; 7355 u32 type, cmpl_ring_id; 7356 7357 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7358 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7359 else 7360 type = RING_FREE_REQ_RING_TYPE_RX; 7361 7362 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7363 return; 7364 7365 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7366 hwrm_ring_free_send_msg(bp, ring, type, 7367 close_path ? cmpl_ring_id : 7368 INVALID_HW_RING_ID); 7369 ring->fw_ring_id = INVALID_HW_RING_ID; 7370 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7371 } 7372 7373 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7374 { 7375 u32 type; 7376 int i; 7377 7378 if (!bp->bnapi) 7379 return; 7380 7381 for (i = 0; i < bp->tx_nr_rings; i++) { 7382 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7383 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7384 7385 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7386 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr); 7387 7388 hwrm_ring_free_send_msg(bp, ring, 7389 RING_FREE_REQ_RING_TYPE_TX, 7390 close_path ? cmpl_ring_id : 7391 INVALID_HW_RING_ID); 7392 ring->fw_ring_id = INVALID_HW_RING_ID; 7393 } 7394 } 7395 7396 bnxt_cancel_dim(bp); 7397 for (i = 0; i < bp->rx_nr_rings; i++) { 7398 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7399 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7400 } 7401 7402 /* The completion rings are about to be freed. After that the 7403 * IRQ doorbell will not work anymore. So we need to disable 7404 * IRQ here. 7405 */ 7406 bnxt_disable_int_sync(bp); 7407 7408 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7409 type = RING_FREE_REQ_RING_TYPE_NQ; 7410 else 7411 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7412 for (i = 0; i < bp->cp_nr_rings; i++) { 7413 struct bnxt_napi *bnapi = bp->bnapi[i]; 7414 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7415 struct bnxt_ring_struct *ring; 7416 int j; 7417 7418 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) { 7419 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 7420 7421 ring = &cpr2->cp_ring_struct; 7422 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7423 continue; 7424 hwrm_ring_free_send_msg(bp, ring, 7425 RING_FREE_REQ_RING_TYPE_L2_CMPL, 7426 INVALID_HW_RING_ID); 7427 ring->fw_ring_id = INVALID_HW_RING_ID; 7428 } 7429 ring = &cpr->cp_ring_struct; 7430 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7431 hwrm_ring_free_send_msg(bp, ring, type, 7432 INVALID_HW_RING_ID); 7433 ring->fw_ring_id = INVALID_HW_RING_ID; 7434 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7435 } 7436 } 7437 } 7438 7439 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7440 bool shared); 7441 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7442 bool shared); 7443 7444 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7445 { 7446 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7447 struct hwrm_func_qcfg_output *resp; 7448 struct hwrm_func_qcfg_input *req; 7449 int rc; 7450 7451 if (bp->hwrm_spec_code < 0x10601) 7452 return 0; 7453 7454 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7455 if (rc) 7456 return rc; 7457 7458 req->fid = cpu_to_le16(0xffff); 7459 resp = hwrm_req_hold(bp, req); 7460 rc = hwrm_req_send(bp, req); 7461 if (rc) { 7462 hwrm_req_drop(bp, req); 7463 return rc; 7464 } 7465 7466 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7467 if (BNXT_NEW_RM(bp)) { 7468 u16 cp, stats; 7469 7470 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7471 hw_resc->resv_hw_ring_grps = 7472 le32_to_cpu(resp->alloc_hw_ring_grps); 7473 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7474 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7475 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7476 stats = le16_to_cpu(resp->alloc_stat_ctx); 7477 hw_resc->resv_irqs = cp; 7478 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7479 int rx = hw_resc->resv_rx_rings; 7480 int tx = hw_resc->resv_tx_rings; 7481 7482 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7483 rx >>= 1; 7484 if (cp < (rx + tx)) { 7485 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7486 if (rc) 7487 goto get_rings_exit; 7488 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7489 rx <<= 1; 7490 hw_resc->resv_rx_rings = rx; 7491 hw_resc->resv_tx_rings = tx; 7492 } 7493 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7494 hw_resc->resv_hw_ring_grps = rx; 7495 } 7496 hw_resc->resv_cp_rings = cp; 7497 hw_resc->resv_stat_ctxs = stats; 7498 } 7499 get_rings_exit: 7500 hwrm_req_drop(bp, req); 7501 return rc; 7502 } 7503 7504 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7505 { 7506 struct hwrm_func_qcfg_output *resp; 7507 struct hwrm_func_qcfg_input *req; 7508 int rc; 7509 7510 if (bp->hwrm_spec_code < 0x10601) 7511 return 0; 7512 7513 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7514 if (rc) 7515 return rc; 7516 7517 req->fid = cpu_to_le16(fid); 7518 resp = hwrm_req_hold(bp, req); 7519 rc = hwrm_req_send(bp, req); 7520 if (!rc) 7521 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7522 7523 hwrm_req_drop(bp, req); 7524 return rc; 7525 } 7526 7527 static bool bnxt_rfs_supported(struct bnxt *bp); 7528 7529 static struct hwrm_func_cfg_input * 7530 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7531 { 7532 struct hwrm_func_cfg_input *req; 7533 u32 enables = 0; 7534 7535 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7536 return NULL; 7537 7538 req->fid = cpu_to_le16(0xffff); 7539 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7540 req->num_tx_rings = cpu_to_le16(hwr->tx); 7541 if (BNXT_NEW_RM(bp)) { 7542 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7543 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7544 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7545 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7546 enables |= hwr->cp_p5 ? 7547 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7548 } else { 7549 enables |= hwr->cp ? 7550 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7551 enables |= hwr->grp ? 7552 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7553 } 7554 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7555 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7556 0; 7557 req->num_rx_rings = cpu_to_le16(hwr->rx); 7558 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7559 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7560 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7561 req->num_msix = cpu_to_le16(hwr->cp); 7562 } else { 7563 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7564 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7565 } 7566 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7567 req->num_vnics = cpu_to_le16(hwr->vnic); 7568 } 7569 req->enables = cpu_to_le32(enables); 7570 return req; 7571 } 7572 7573 static struct hwrm_func_vf_cfg_input * 7574 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7575 { 7576 struct hwrm_func_vf_cfg_input *req; 7577 u32 enables = 0; 7578 7579 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7580 return NULL; 7581 7582 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7583 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7584 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7585 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7586 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7587 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7588 enables |= hwr->cp_p5 ? 7589 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7590 } else { 7591 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7592 enables |= hwr->grp ? 7593 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7594 } 7595 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7596 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7597 7598 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7599 req->num_tx_rings = cpu_to_le16(hwr->tx); 7600 req->num_rx_rings = cpu_to_le16(hwr->rx); 7601 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7602 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7603 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7604 } else { 7605 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7606 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7607 } 7608 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7609 req->num_vnics = cpu_to_le16(hwr->vnic); 7610 7611 req->enables = cpu_to_le32(enables); 7612 return req; 7613 } 7614 7615 static int 7616 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7617 { 7618 struct hwrm_func_cfg_input *req; 7619 int rc; 7620 7621 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7622 if (!req) 7623 return -ENOMEM; 7624 7625 if (!req->enables) { 7626 hwrm_req_drop(bp, req); 7627 return 0; 7628 } 7629 7630 rc = hwrm_req_send(bp, req); 7631 if (rc) 7632 return rc; 7633 7634 if (bp->hwrm_spec_code < 0x10601) 7635 bp->hw_resc.resv_tx_rings = hwr->tx; 7636 7637 return bnxt_hwrm_get_rings(bp); 7638 } 7639 7640 static int 7641 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7642 { 7643 struct hwrm_func_vf_cfg_input *req; 7644 int rc; 7645 7646 if (!BNXT_NEW_RM(bp)) { 7647 bp->hw_resc.resv_tx_rings = hwr->tx; 7648 return 0; 7649 } 7650 7651 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7652 if (!req) 7653 return -ENOMEM; 7654 7655 rc = hwrm_req_send(bp, req); 7656 if (rc) 7657 return rc; 7658 7659 return bnxt_hwrm_get_rings(bp); 7660 } 7661 7662 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7663 { 7664 if (BNXT_PF(bp)) 7665 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7666 else 7667 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7668 } 7669 7670 int bnxt_nq_rings_in_use(struct bnxt *bp) 7671 { 7672 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7673 } 7674 7675 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7676 { 7677 int cp; 7678 7679 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7680 return bnxt_nq_rings_in_use(bp); 7681 7682 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7683 return cp; 7684 } 7685 7686 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7687 { 7688 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7689 } 7690 7691 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7692 { 7693 if (!hwr->grp) 7694 return 0; 7695 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7696 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7697 7698 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7699 rss_ctx *= hwr->vnic; 7700 return rss_ctx; 7701 } 7702 if (BNXT_VF(bp)) 7703 return BNXT_VF_MAX_RSS_CTX; 7704 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7705 return hwr->grp + 1; 7706 return 1; 7707 } 7708 7709 /* Check if a default RSS map needs to be setup. This function is only 7710 * used on older firmware that does not require reserving RX rings. 7711 */ 7712 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7713 { 7714 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7715 7716 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7717 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7718 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7719 if (!netif_is_rxfh_configured(bp->dev)) 7720 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7721 } 7722 } 7723 7724 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7725 { 7726 if (bp->flags & BNXT_FLAG_RFS) { 7727 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7728 return 2 + bp->num_rss_ctx; 7729 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7730 return rx_rings + 1; 7731 } 7732 return 1; 7733 } 7734 7735 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7736 { 7737 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7738 int cp = bnxt_cp_rings_in_use(bp); 7739 int nq = bnxt_nq_rings_in_use(bp); 7740 int rx = bp->rx_nr_rings, stat; 7741 int vnic, grp = rx; 7742 7743 /* Old firmware does not need RX ring reservations but we still 7744 * need to setup a default RSS map when needed. With new firmware 7745 * we go through RX ring reservations first and then set up the 7746 * RSS map for the successfully reserved RX rings when needed. 7747 */ 7748 if (!BNXT_NEW_RM(bp)) 7749 bnxt_check_rss_tbl_no_rmgr(bp); 7750 7751 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7752 bp->hwrm_spec_code >= 0x10601) 7753 return true; 7754 7755 if (!BNXT_NEW_RM(bp)) 7756 return false; 7757 7758 vnic = bnxt_get_total_vnics(bp, rx); 7759 7760 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7761 rx <<= 1; 7762 stat = bnxt_get_func_stat_ctxs(bp); 7763 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7764 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7765 (hw_resc->resv_hw_ring_grps != grp && 7766 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7767 return true; 7768 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7769 hw_resc->resv_irqs != nq) 7770 return true; 7771 return false; 7772 } 7773 7774 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7775 { 7776 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7777 7778 hwr->tx = hw_resc->resv_tx_rings; 7779 if (BNXT_NEW_RM(bp)) { 7780 hwr->rx = hw_resc->resv_rx_rings; 7781 hwr->cp = hw_resc->resv_irqs; 7782 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7783 hwr->cp_p5 = hw_resc->resv_cp_rings; 7784 hwr->grp = hw_resc->resv_hw_ring_grps; 7785 hwr->vnic = hw_resc->resv_vnics; 7786 hwr->stat = hw_resc->resv_stat_ctxs; 7787 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7788 } 7789 } 7790 7791 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7792 { 7793 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7794 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7795 } 7796 7797 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7798 7799 static int __bnxt_reserve_rings(struct bnxt *bp) 7800 { 7801 struct bnxt_hw_rings hwr = {0}; 7802 int rx_rings, old_rx_rings, rc; 7803 int cp = bp->cp_nr_rings; 7804 int ulp_msix = 0; 7805 bool sh = false; 7806 int tx_cp; 7807 7808 if (!bnxt_need_reserve_rings(bp)) 7809 return 0; 7810 7811 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7812 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7813 if (!ulp_msix) 7814 bnxt_set_ulp_stat_ctxs(bp, 0); 7815 7816 if (ulp_msix > bp->ulp_num_msix_want) 7817 ulp_msix = bp->ulp_num_msix_want; 7818 hwr.cp = cp + ulp_msix; 7819 } else { 7820 hwr.cp = bnxt_nq_rings_in_use(bp); 7821 } 7822 7823 hwr.tx = bp->tx_nr_rings; 7824 hwr.rx = bp->rx_nr_rings; 7825 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7826 sh = true; 7827 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7828 hwr.cp_p5 = hwr.rx + hwr.tx; 7829 7830 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7831 7832 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7833 hwr.rx <<= 1; 7834 hwr.grp = bp->rx_nr_rings; 7835 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 7836 hwr.stat = bnxt_get_func_stat_ctxs(bp); 7837 old_rx_rings = bp->hw_resc.resv_rx_rings; 7838 7839 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 7840 if (rc) 7841 return rc; 7842 7843 bnxt_copy_reserved_rings(bp, &hwr); 7844 7845 rx_rings = hwr.rx; 7846 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 7847 if (hwr.rx >= 2) { 7848 rx_rings = hwr.rx >> 1; 7849 } else { 7850 if (netif_running(bp->dev)) 7851 return -ENOMEM; 7852 7853 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 7854 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 7855 bp->dev->hw_features &= ~NETIF_F_LRO; 7856 bp->dev->features &= ~NETIF_F_LRO; 7857 bnxt_set_ring_params(bp); 7858 } 7859 } 7860 rx_rings = min_t(int, rx_rings, hwr.grp); 7861 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 7862 if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 7863 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 7864 hwr.cp = min_t(int, hwr.cp, hwr.stat); 7865 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 7866 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7867 hwr.rx = rx_rings << 1; 7868 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 7869 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 7870 bp->tx_nr_rings = hwr.tx; 7871 7872 /* If we cannot reserve all the RX rings, reset the RSS map only 7873 * if absolutely necessary 7874 */ 7875 if (rx_rings != bp->rx_nr_rings) { 7876 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 7877 rx_rings, bp->rx_nr_rings); 7878 if (netif_is_rxfh_configured(bp->dev) && 7879 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 7880 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 7881 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 7882 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 7883 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 7884 } 7885 } 7886 bp->rx_nr_rings = rx_rings; 7887 bp->cp_nr_rings = hwr.cp; 7888 7889 if (!bnxt_rings_ok(bp, &hwr)) 7890 return -ENOMEM; 7891 7892 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 7893 !netif_is_rxfh_configured(bp->dev)) 7894 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7895 7896 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 7897 int resv_msix, resv_ctx, ulp_ctxs; 7898 struct bnxt_hw_resc *hw_resc; 7899 7900 hw_resc = &bp->hw_resc; 7901 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 7902 ulp_msix = min_t(int, resv_msix, ulp_msix); 7903 bnxt_set_ulp_msix_num(bp, ulp_msix); 7904 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 7905 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 7906 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 7907 } 7908 7909 return rc; 7910 } 7911 7912 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7913 { 7914 struct hwrm_func_vf_cfg_input *req; 7915 u32 flags; 7916 7917 if (!BNXT_NEW_RM(bp)) 7918 return 0; 7919 7920 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7921 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 7922 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7923 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7924 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7925 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 7926 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 7927 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7928 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7929 7930 req->flags = cpu_to_le32(flags); 7931 return hwrm_req_send_silent(bp, req); 7932 } 7933 7934 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7935 { 7936 struct hwrm_func_cfg_input *req; 7937 u32 flags; 7938 7939 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7940 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 7941 if (BNXT_NEW_RM(bp)) { 7942 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7943 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7944 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7945 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 7946 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7947 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 7948 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 7949 else 7950 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7951 } 7952 7953 req->flags = cpu_to_le32(flags); 7954 return hwrm_req_send_silent(bp, req); 7955 } 7956 7957 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7958 { 7959 if (bp->hwrm_spec_code < 0x10801) 7960 return 0; 7961 7962 if (BNXT_PF(bp)) 7963 return bnxt_hwrm_check_pf_rings(bp, hwr); 7964 7965 return bnxt_hwrm_check_vf_rings(bp, hwr); 7966 } 7967 7968 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 7969 { 7970 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7971 struct hwrm_ring_aggint_qcaps_output *resp; 7972 struct hwrm_ring_aggint_qcaps_input *req; 7973 int rc; 7974 7975 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 7976 coal_cap->num_cmpl_dma_aggr_max = 63; 7977 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 7978 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 7979 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 7980 coal_cap->int_lat_tmr_min_max = 65535; 7981 coal_cap->int_lat_tmr_max_max = 65535; 7982 coal_cap->num_cmpl_aggr_int_max = 65535; 7983 coal_cap->timer_units = 80; 7984 7985 if (bp->hwrm_spec_code < 0x10902) 7986 return; 7987 7988 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 7989 return; 7990 7991 resp = hwrm_req_hold(bp, req); 7992 rc = hwrm_req_send_silent(bp, req); 7993 if (!rc) { 7994 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 7995 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 7996 coal_cap->num_cmpl_dma_aggr_max = 7997 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 7998 coal_cap->num_cmpl_dma_aggr_during_int_max = 7999 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8000 coal_cap->cmpl_aggr_dma_tmr_max = 8001 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8002 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8003 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8004 coal_cap->int_lat_tmr_min_max = 8005 le16_to_cpu(resp->int_lat_tmr_min_max); 8006 coal_cap->int_lat_tmr_max_max = 8007 le16_to_cpu(resp->int_lat_tmr_max_max); 8008 coal_cap->num_cmpl_aggr_int_max = 8009 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8010 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8011 } 8012 hwrm_req_drop(bp, req); 8013 } 8014 8015 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8016 { 8017 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8018 8019 return usec * 1000 / coal_cap->timer_units; 8020 } 8021 8022 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8023 struct bnxt_coal *hw_coal, 8024 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8025 { 8026 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8027 u16 val, tmr, max, flags = hw_coal->flags; 8028 u32 cmpl_params = coal_cap->cmpl_params; 8029 8030 max = hw_coal->bufs_per_record * 128; 8031 if (hw_coal->budget) 8032 max = hw_coal->bufs_per_record * hw_coal->budget; 8033 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8034 8035 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8036 req->num_cmpl_aggr_int = cpu_to_le16(val); 8037 8038 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8039 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8040 8041 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8042 coal_cap->num_cmpl_dma_aggr_during_int_max); 8043 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8044 8045 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8046 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8047 req->int_lat_tmr_max = cpu_to_le16(tmr); 8048 8049 /* min timer set to 1/2 of interrupt timer */ 8050 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8051 val = tmr / 2; 8052 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8053 req->int_lat_tmr_min = cpu_to_le16(val); 8054 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8055 } 8056 8057 /* buf timer set to 1/4 of interrupt timer */ 8058 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8059 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8060 8061 if (cmpl_params & 8062 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8063 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8064 val = clamp_t(u16, tmr, 1, 8065 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8066 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8067 req->enables |= 8068 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8069 } 8070 8071 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8072 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8073 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8074 req->flags = cpu_to_le16(flags); 8075 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8076 } 8077 8078 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8079 struct bnxt_coal *hw_coal) 8080 { 8081 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8082 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8083 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8084 u32 nq_params = coal_cap->nq_params; 8085 u16 tmr; 8086 int rc; 8087 8088 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8089 return 0; 8090 8091 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8092 if (rc) 8093 return rc; 8094 8095 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8096 req->flags = 8097 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8098 8099 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8100 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8101 req->int_lat_tmr_min = cpu_to_le16(tmr); 8102 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8103 return hwrm_req_send(bp, req); 8104 } 8105 8106 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8107 { 8108 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8109 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8110 struct bnxt_coal coal; 8111 int rc; 8112 8113 /* Tick values in micro seconds. 8114 * 1 coal_buf x bufs_per_record = 1 completion record. 8115 */ 8116 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8117 8118 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8119 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8120 8121 if (!bnapi->rx_ring) 8122 return -ENODEV; 8123 8124 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8125 if (rc) 8126 return rc; 8127 8128 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8129 8130 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8131 8132 return hwrm_req_send(bp, req_rx); 8133 } 8134 8135 static int 8136 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8137 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8138 { 8139 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8140 8141 req->ring_id = cpu_to_le16(ring_id); 8142 return hwrm_req_send(bp, req); 8143 } 8144 8145 static int 8146 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8147 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8148 { 8149 struct bnxt_tx_ring_info *txr; 8150 int i, rc; 8151 8152 bnxt_for_each_napi_tx(i, bnapi, txr) { 8153 u16 ring_id; 8154 8155 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8156 req->ring_id = cpu_to_le16(ring_id); 8157 rc = hwrm_req_send(bp, req); 8158 if (rc) 8159 return rc; 8160 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8161 return 0; 8162 } 8163 return 0; 8164 } 8165 8166 int bnxt_hwrm_set_coal(struct bnxt *bp) 8167 { 8168 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8169 int i, rc; 8170 8171 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8172 if (rc) 8173 return rc; 8174 8175 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8176 if (rc) { 8177 hwrm_req_drop(bp, req_rx); 8178 return rc; 8179 } 8180 8181 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8182 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8183 8184 hwrm_req_hold(bp, req_rx); 8185 hwrm_req_hold(bp, req_tx); 8186 for (i = 0; i < bp->cp_nr_rings; i++) { 8187 struct bnxt_napi *bnapi = bp->bnapi[i]; 8188 struct bnxt_coal *hw_coal; 8189 8190 if (!bnapi->rx_ring) 8191 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8192 else 8193 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8194 if (rc) 8195 break; 8196 8197 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8198 continue; 8199 8200 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8201 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8202 if (rc) 8203 break; 8204 } 8205 if (bnapi->rx_ring) 8206 hw_coal = &bp->rx_coal; 8207 else 8208 hw_coal = &bp->tx_coal; 8209 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8210 } 8211 hwrm_req_drop(bp, req_rx); 8212 hwrm_req_drop(bp, req_tx); 8213 return rc; 8214 } 8215 8216 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8217 { 8218 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8219 struct hwrm_stat_ctx_free_input *req; 8220 int i; 8221 8222 if (!bp->bnapi) 8223 return; 8224 8225 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8226 return; 8227 8228 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8229 return; 8230 if (BNXT_FW_MAJ(bp) <= 20) { 8231 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8232 hwrm_req_drop(bp, req); 8233 return; 8234 } 8235 hwrm_req_hold(bp, req0); 8236 } 8237 hwrm_req_hold(bp, req); 8238 for (i = 0; i < bp->cp_nr_rings; i++) { 8239 struct bnxt_napi *bnapi = bp->bnapi[i]; 8240 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8241 8242 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8243 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8244 if (req0) { 8245 req0->stat_ctx_id = req->stat_ctx_id; 8246 hwrm_req_send(bp, req0); 8247 } 8248 hwrm_req_send(bp, req); 8249 8250 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8251 } 8252 } 8253 hwrm_req_drop(bp, req); 8254 if (req0) 8255 hwrm_req_drop(bp, req0); 8256 } 8257 8258 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8259 { 8260 struct hwrm_stat_ctx_alloc_output *resp; 8261 struct hwrm_stat_ctx_alloc_input *req; 8262 int rc, i; 8263 8264 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8265 return 0; 8266 8267 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8268 if (rc) 8269 return rc; 8270 8271 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8272 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8273 8274 resp = hwrm_req_hold(bp, req); 8275 for (i = 0; i < bp->cp_nr_rings; i++) { 8276 struct bnxt_napi *bnapi = bp->bnapi[i]; 8277 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8278 8279 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8280 8281 rc = hwrm_req_send(bp, req); 8282 if (rc) 8283 break; 8284 8285 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8286 8287 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8288 } 8289 hwrm_req_drop(bp, req); 8290 return rc; 8291 } 8292 8293 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8294 { 8295 struct hwrm_func_qcfg_output *resp; 8296 struct hwrm_func_qcfg_input *req; 8297 u16 flags; 8298 int rc; 8299 8300 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8301 if (rc) 8302 return rc; 8303 8304 req->fid = cpu_to_le16(0xffff); 8305 resp = hwrm_req_hold(bp, req); 8306 rc = hwrm_req_send(bp, req); 8307 if (rc) 8308 goto func_qcfg_exit; 8309 8310 flags = le16_to_cpu(resp->flags); 8311 #ifdef CONFIG_BNXT_SRIOV 8312 if (BNXT_VF(bp)) { 8313 struct bnxt_vf_info *vf = &bp->vf; 8314 8315 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8316 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8317 vf->flags |= BNXT_VF_TRUST; 8318 else 8319 vf->flags &= ~BNXT_VF_TRUST; 8320 } else { 8321 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8322 } 8323 #endif 8324 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8325 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8326 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8327 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8328 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8329 } 8330 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8331 bp->flags |= BNXT_FLAG_MULTI_HOST; 8332 8333 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8334 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8335 8336 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8337 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8338 8339 switch (resp->port_partition_type) { 8340 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8341 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8342 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8343 bp->port_partition_type = resp->port_partition_type; 8344 break; 8345 } 8346 if (bp->hwrm_spec_code < 0x10707 || 8347 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8348 bp->br_mode = BRIDGE_MODE_VEB; 8349 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8350 bp->br_mode = BRIDGE_MODE_VEPA; 8351 else 8352 bp->br_mode = BRIDGE_MODE_UNDEF; 8353 8354 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8355 if (!bp->max_mtu) 8356 bp->max_mtu = BNXT_MAX_MTU; 8357 8358 if (bp->db_size) 8359 goto func_qcfg_exit; 8360 8361 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8362 if (BNXT_CHIP_P5(bp)) { 8363 if (BNXT_PF(bp)) 8364 bp->db_offset = DB_PF_OFFSET_P5; 8365 else 8366 bp->db_offset = DB_VF_OFFSET_P5; 8367 } 8368 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8369 1024); 8370 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8371 bp->db_size <= bp->db_offset) 8372 bp->db_size = pci_resource_len(bp->pdev, 2); 8373 8374 func_qcfg_exit: 8375 hwrm_req_drop(bp, req); 8376 return rc; 8377 } 8378 8379 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8380 u8 init_val, u8 init_offset, 8381 bool init_mask_set) 8382 { 8383 ctxm->init_value = init_val; 8384 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8385 if (init_mask_set) 8386 ctxm->init_offset = init_offset * 4; 8387 else 8388 ctxm->init_value = 0; 8389 } 8390 8391 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8392 { 8393 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8394 u16 type; 8395 8396 for (type = 0; type < ctx_max; type++) { 8397 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8398 int n = 1; 8399 8400 if (!ctxm->max_entries || ctxm->pg_info) 8401 continue; 8402 8403 if (ctxm->instance_bmap) 8404 n = hweight32(ctxm->instance_bmap); 8405 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8406 if (!ctxm->pg_info) 8407 return -ENOMEM; 8408 } 8409 return 0; 8410 } 8411 8412 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8413 struct bnxt_ctx_mem_type *ctxm, bool force); 8414 8415 #define BNXT_CTX_INIT_VALID(flags) \ 8416 (!!((flags) & \ 8417 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8418 8419 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8420 { 8421 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8422 struct hwrm_func_backing_store_qcaps_v2_input *req; 8423 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8424 u16 type; 8425 int rc; 8426 8427 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8428 if (rc) 8429 return rc; 8430 8431 if (!ctx) { 8432 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8433 if (!ctx) 8434 return -ENOMEM; 8435 bp->ctx = ctx; 8436 } 8437 8438 resp = hwrm_req_hold(bp, req); 8439 8440 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8441 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8442 u8 init_val, init_off, i; 8443 u32 max_entries; 8444 u16 entry_size; 8445 __le32 *p; 8446 u32 flags; 8447 8448 req->type = cpu_to_le16(type); 8449 rc = hwrm_req_send(bp, req); 8450 if (rc) 8451 goto ctx_done; 8452 flags = le32_to_cpu(resp->flags); 8453 type = le16_to_cpu(resp->next_valid_type); 8454 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8455 bnxt_free_one_ctx_mem(bp, ctxm, true); 8456 continue; 8457 } 8458 entry_size = le16_to_cpu(resp->entry_size); 8459 max_entries = le32_to_cpu(resp->max_num_entries); 8460 if (ctxm->mem_valid) { 8461 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8462 ctxm->entry_size != entry_size || 8463 ctxm->max_entries != max_entries) 8464 bnxt_free_one_ctx_mem(bp, ctxm, true); 8465 else 8466 continue; 8467 } 8468 ctxm->type = le16_to_cpu(resp->type); 8469 ctxm->entry_size = entry_size; 8470 ctxm->flags = flags; 8471 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8472 ctxm->entry_multiple = resp->entry_multiple; 8473 ctxm->max_entries = max_entries; 8474 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8475 init_val = resp->ctx_init_value; 8476 init_off = resp->ctx_init_offset; 8477 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8478 BNXT_CTX_INIT_VALID(flags)); 8479 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8480 BNXT_MAX_SPLIT_ENTRY); 8481 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8482 i++, p++) 8483 ctxm->split[i] = le32_to_cpu(*p); 8484 } 8485 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8486 8487 ctx_done: 8488 hwrm_req_drop(bp, req); 8489 return rc; 8490 } 8491 8492 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8493 { 8494 struct hwrm_func_backing_store_qcaps_output *resp; 8495 struct hwrm_func_backing_store_qcaps_input *req; 8496 int rc; 8497 8498 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8499 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8500 return 0; 8501 8502 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8503 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8504 8505 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8506 if (rc) 8507 return rc; 8508 8509 resp = hwrm_req_hold(bp, req); 8510 rc = hwrm_req_send_silent(bp, req); 8511 if (!rc) { 8512 struct bnxt_ctx_mem_type *ctxm; 8513 struct bnxt_ctx_mem_info *ctx; 8514 u8 init_val, init_idx = 0; 8515 u16 init_mask; 8516 8517 ctx = bp->ctx; 8518 if (!ctx) { 8519 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8520 if (!ctx) { 8521 rc = -ENOMEM; 8522 goto ctx_err; 8523 } 8524 bp->ctx = ctx; 8525 } 8526 init_val = resp->ctx_kind_initializer; 8527 init_mask = le16_to_cpu(resp->ctx_init_mask); 8528 8529 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8530 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8531 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8532 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8533 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8534 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8535 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8536 (init_mask & (1 << init_idx++)) != 0); 8537 8538 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8539 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8540 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8541 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8542 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8543 (init_mask & (1 << init_idx++)) != 0); 8544 8545 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8546 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8547 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8548 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8549 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8550 (init_mask & (1 << init_idx++)) != 0); 8551 8552 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8553 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8554 ctxm->max_entries = ctxm->vnic_entries + 8555 le16_to_cpu(resp->vnic_max_ring_table_entries); 8556 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8557 bnxt_init_ctx_initializer(ctxm, init_val, 8558 resp->vnic_init_offset, 8559 (init_mask & (1 << init_idx++)) != 0); 8560 8561 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8562 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8563 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8564 bnxt_init_ctx_initializer(ctxm, init_val, 8565 resp->stat_init_offset, 8566 (init_mask & (1 << init_idx++)) != 0); 8567 8568 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8569 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8570 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8571 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8572 ctxm->entry_multiple = resp->tqm_entries_multiple; 8573 if (!ctxm->entry_multiple) 8574 ctxm->entry_multiple = 1; 8575 8576 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8577 8578 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8579 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8580 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8581 ctxm->mrav_num_entries_units = 8582 le16_to_cpu(resp->mrav_num_entries_units); 8583 bnxt_init_ctx_initializer(ctxm, init_val, 8584 resp->mrav_init_offset, 8585 (init_mask & (1 << init_idx++)) != 0); 8586 8587 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8588 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8589 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8590 8591 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8592 if (!ctx->tqm_fp_rings_count) 8593 ctx->tqm_fp_rings_count = bp->max_q; 8594 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8595 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8596 8597 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8598 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8599 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8600 8601 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8602 } else { 8603 rc = 0; 8604 } 8605 ctx_err: 8606 hwrm_req_drop(bp, req); 8607 return rc; 8608 } 8609 8610 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8611 __le64 *pg_dir) 8612 { 8613 if (!rmem->nr_pages) 8614 return; 8615 8616 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8617 if (rmem->depth >= 1) { 8618 if (rmem->depth == 2) 8619 *pg_attr |= 2; 8620 else 8621 *pg_attr |= 1; 8622 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8623 } else { 8624 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8625 } 8626 } 8627 8628 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8629 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8630 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8631 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8632 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8633 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8634 8635 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8636 { 8637 struct hwrm_func_backing_store_cfg_input *req; 8638 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8639 struct bnxt_ctx_pg_info *ctx_pg; 8640 struct bnxt_ctx_mem_type *ctxm; 8641 void **__req = (void **)&req; 8642 u32 req_len = sizeof(*req); 8643 __le32 *num_entries; 8644 __le64 *pg_dir; 8645 u32 flags = 0; 8646 u8 *pg_attr; 8647 u32 ena; 8648 int rc; 8649 int i; 8650 8651 if (!ctx) 8652 return 0; 8653 8654 if (req_len > bp->hwrm_max_ext_req_len) 8655 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8656 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8657 if (rc) 8658 return rc; 8659 8660 req->enables = cpu_to_le32(enables); 8661 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8662 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8663 ctx_pg = ctxm->pg_info; 8664 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8665 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8666 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8667 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8668 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8669 &req->qpc_pg_size_qpc_lvl, 8670 &req->qpc_page_dir); 8671 8672 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8673 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8674 } 8675 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8676 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8677 ctx_pg = ctxm->pg_info; 8678 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8679 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8680 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8681 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8682 &req->srq_pg_size_srq_lvl, 8683 &req->srq_page_dir); 8684 } 8685 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8686 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8687 ctx_pg = ctxm->pg_info; 8688 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8689 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8690 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8691 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8692 &req->cq_pg_size_cq_lvl, 8693 &req->cq_page_dir); 8694 } 8695 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8696 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8697 ctx_pg = ctxm->pg_info; 8698 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8699 req->vnic_num_ring_table_entries = 8700 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8701 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8702 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8703 &req->vnic_pg_size_vnic_lvl, 8704 &req->vnic_page_dir); 8705 } 8706 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8707 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8708 ctx_pg = ctxm->pg_info; 8709 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8710 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8711 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8712 &req->stat_pg_size_stat_lvl, 8713 &req->stat_page_dir); 8714 } 8715 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8716 u32 units; 8717 8718 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8719 ctx_pg = ctxm->pg_info; 8720 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8721 units = ctxm->mrav_num_entries_units; 8722 if (units) { 8723 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8724 u32 entries; 8725 8726 num_mr = ctx_pg->entries - num_ah; 8727 entries = ((num_mr / units) << 16) | (num_ah / units); 8728 req->mrav_num_entries = cpu_to_le32(entries); 8729 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8730 } 8731 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8732 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8733 &req->mrav_pg_size_mrav_lvl, 8734 &req->mrav_page_dir); 8735 } 8736 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8737 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8738 ctx_pg = ctxm->pg_info; 8739 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8740 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8741 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8742 &req->tim_pg_size_tim_lvl, 8743 &req->tim_page_dir); 8744 } 8745 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8746 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8747 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8748 pg_dir = &req->tqm_sp_page_dir, 8749 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8750 ctx_pg = ctxm->pg_info; 8751 i < BNXT_MAX_TQM_RINGS; 8752 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8753 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8754 if (!(enables & ena)) 8755 continue; 8756 8757 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8758 *num_entries = cpu_to_le32(ctx_pg->entries); 8759 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8760 } 8761 req->flags = cpu_to_le32(flags); 8762 return hwrm_req_send(bp, req); 8763 } 8764 8765 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8766 struct bnxt_ctx_pg_info *ctx_pg) 8767 { 8768 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8769 8770 rmem->page_size = BNXT_PAGE_SIZE; 8771 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8772 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8773 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8774 if (rmem->depth >= 1) 8775 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8776 return bnxt_alloc_ring(bp, rmem); 8777 } 8778 8779 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8780 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8781 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8782 { 8783 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8784 int rc; 8785 8786 if (!mem_size) 8787 return -EINVAL; 8788 8789 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8790 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8791 ctx_pg->nr_pages = 0; 8792 return -EINVAL; 8793 } 8794 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8795 int nr_tbls, i; 8796 8797 rmem->depth = 2; 8798 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8799 GFP_KERNEL); 8800 if (!ctx_pg->ctx_pg_tbl) 8801 return -ENOMEM; 8802 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8803 rmem->nr_pages = nr_tbls; 8804 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8805 if (rc) 8806 return rc; 8807 for (i = 0; i < nr_tbls; i++) { 8808 struct bnxt_ctx_pg_info *pg_tbl; 8809 8810 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8811 if (!pg_tbl) 8812 return -ENOMEM; 8813 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8814 rmem = &pg_tbl->ring_mem; 8815 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8816 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8817 rmem->depth = 1; 8818 rmem->nr_pages = MAX_CTX_PAGES; 8819 rmem->ctx_mem = ctxm; 8820 if (i == (nr_tbls - 1)) { 8821 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8822 8823 if (rem) 8824 rmem->nr_pages = rem; 8825 } 8826 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8827 if (rc) 8828 break; 8829 } 8830 } else { 8831 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8832 if (rmem->nr_pages > 1 || depth) 8833 rmem->depth = 1; 8834 rmem->ctx_mem = ctxm; 8835 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8836 } 8837 return rc; 8838 } 8839 8840 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 8841 struct bnxt_ctx_pg_info *ctx_pg, 8842 void *buf, size_t offset, size_t head, 8843 size_t tail) 8844 { 8845 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8846 size_t nr_pages = ctx_pg->nr_pages; 8847 int page_size = rmem->page_size; 8848 size_t len = 0, total_len = 0; 8849 u16 depth = rmem->depth; 8850 8851 tail %= nr_pages * page_size; 8852 do { 8853 if (depth > 1) { 8854 int i = head / (page_size * MAX_CTX_PAGES); 8855 struct bnxt_ctx_pg_info *pg_tbl; 8856 8857 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 8858 rmem = &pg_tbl->ring_mem; 8859 } 8860 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 8861 head += len; 8862 offset += len; 8863 total_len += len; 8864 if (head >= nr_pages * page_size) 8865 head = 0; 8866 } while (head != tail); 8867 return total_len; 8868 } 8869 8870 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 8871 struct bnxt_ctx_pg_info *ctx_pg) 8872 { 8873 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8874 8875 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 8876 ctx_pg->ctx_pg_tbl) { 8877 int i, nr_tbls = rmem->nr_pages; 8878 8879 for (i = 0; i < nr_tbls; i++) { 8880 struct bnxt_ctx_pg_info *pg_tbl; 8881 struct bnxt_ring_mem_info *rmem2; 8882 8883 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 8884 if (!pg_tbl) 8885 continue; 8886 rmem2 = &pg_tbl->ring_mem; 8887 bnxt_free_ring(bp, rmem2); 8888 ctx_pg->ctx_pg_arr[i] = NULL; 8889 kfree(pg_tbl); 8890 ctx_pg->ctx_pg_tbl[i] = NULL; 8891 } 8892 kfree(ctx_pg->ctx_pg_tbl); 8893 ctx_pg->ctx_pg_tbl = NULL; 8894 } 8895 bnxt_free_ring(bp, rmem); 8896 ctx_pg->nr_pages = 0; 8897 } 8898 8899 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 8900 struct bnxt_ctx_mem_type *ctxm, u32 entries, 8901 u8 pg_lvl) 8902 { 8903 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 8904 int i, rc = 0, n = 1; 8905 u32 mem_size; 8906 8907 if (!ctxm->entry_size || !ctx_pg) 8908 return -EINVAL; 8909 if (ctxm->instance_bmap) 8910 n = hweight32(ctxm->instance_bmap); 8911 if (ctxm->entry_multiple) 8912 entries = roundup(entries, ctxm->entry_multiple); 8913 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 8914 mem_size = entries * ctxm->entry_size; 8915 for (i = 0; i < n && !rc; i++) { 8916 ctx_pg[i].entries = entries; 8917 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 8918 ctxm->init_value ? ctxm : NULL); 8919 } 8920 if (!rc) 8921 ctxm->mem_valid = 1; 8922 return rc; 8923 } 8924 8925 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 8926 struct bnxt_ctx_mem_type *ctxm, 8927 bool last) 8928 { 8929 struct hwrm_func_backing_store_cfg_v2_input *req; 8930 u32 instance_bmap = ctxm->instance_bmap; 8931 int i, j, rc = 0, n = 1; 8932 __le32 *p; 8933 8934 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 8935 return 0; 8936 8937 if (instance_bmap) 8938 n = hweight32(ctxm->instance_bmap); 8939 else 8940 instance_bmap = 1; 8941 8942 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 8943 if (rc) 8944 return rc; 8945 hwrm_req_hold(bp, req); 8946 req->type = cpu_to_le16(ctxm->type); 8947 req->entry_size = cpu_to_le16(ctxm->entry_size); 8948 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 8949 bnxt_bs_trace_avail(bp, ctxm->type)) { 8950 struct bnxt_bs_trace_info *bs_trace; 8951 u32 enables; 8952 8953 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 8954 req->enables = cpu_to_le32(enables); 8955 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 8956 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 8957 } 8958 req->subtype_valid_cnt = ctxm->split_entry_cnt; 8959 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 8960 p[i] = cpu_to_le32(ctxm->split[i]); 8961 for (i = 0, j = 0; j < n && !rc; i++) { 8962 struct bnxt_ctx_pg_info *ctx_pg; 8963 8964 if (!(instance_bmap & (1 << i))) 8965 continue; 8966 req->instance = cpu_to_le16(i); 8967 ctx_pg = &ctxm->pg_info[j++]; 8968 if (!ctx_pg->entries) 8969 continue; 8970 req->num_entries = cpu_to_le32(ctx_pg->entries); 8971 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8972 &req->page_size_pbl_level, 8973 &req->page_dir); 8974 if (last && j == n) 8975 req->flags = 8976 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 8977 rc = hwrm_req_send(bp, req); 8978 } 8979 hwrm_req_drop(bp, req); 8980 return rc; 8981 } 8982 8983 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena) 8984 { 8985 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8986 struct bnxt_ctx_mem_type *ctxm; 8987 u16 last_type = BNXT_CTX_INV; 8988 int rc = 0; 8989 u16 type; 8990 8991 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) { 8992 ctxm = &ctx->ctx_arr[type]; 8993 if (!bnxt_bs_trace_avail(bp, type)) 8994 continue; 8995 if (!ctxm->mem_valid) { 8996 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 8997 ctxm->max_entries, 1); 8998 if (rc) { 8999 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9000 type); 9001 continue; 9002 } 9003 bnxt_bs_trace_init(bp, ctxm); 9004 } 9005 last_type = type; 9006 } 9007 9008 if (last_type == BNXT_CTX_INV) { 9009 if (!ena) 9010 return 0; 9011 else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) 9012 last_type = BNXT_CTX_MAX - 1; 9013 else 9014 last_type = BNXT_CTX_L2_MAX - 1; 9015 } 9016 ctx->ctx_arr[last_type].last = 1; 9017 9018 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9019 ctxm = &ctx->ctx_arr[type]; 9020 9021 if (!ctxm->mem_valid) 9022 continue; 9023 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9024 if (rc) 9025 return rc; 9026 } 9027 return 0; 9028 } 9029 9030 /** 9031 * __bnxt_copy_ctx_mem - copy host context memory 9032 * @bp: The driver context 9033 * @ctxm: The pointer to the context memory type 9034 * @buf: The destination buffer or NULL to just obtain the length 9035 * @offset: The buffer offset to copy the data to 9036 * @head: The head offset of context memory to copy from 9037 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9038 * 9039 * This function is called for debugging purposes to dump the host context 9040 * used by the chip. 9041 * 9042 * Return: Length of memory copied 9043 */ 9044 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9045 struct bnxt_ctx_mem_type *ctxm, void *buf, 9046 size_t offset, size_t head, size_t tail) 9047 { 9048 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9049 size_t len = 0, total_len = 0; 9050 int i, n = 1; 9051 9052 if (!ctx_pg) 9053 return 0; 9054 9055 if (ctxm->instance_bmap) 9056 n = hweight32(ctxm->instance_bmap); 9057 for (i = 0; i < n; i++) { 9058 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9059 tail); 9060 offset += len; 9061 total_len += len; 9062 } 9063 return total_len; 9064 } 9065 9066 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9067 void *buf, size_t offset) 9068 { 9069 size_t tail = ctxm->max_entries * ctxm->entry_size; 9070 9071 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9072 } 9073 9074 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9075 struct bnxt_ctx_mem_type *ctxm, bool force) 9076 { 9077 struct bnxt_ctx_pg_info *ctx_pg; 9078 int i, n = 1; 9079 9080 ctxm->last = 0; 9081 9082 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9083 return; 9084 9085 ctx_pg = ctxm->pg_info; 9086 if (ctx_pg) { 9087 if (ctxm->instance_bmap) 9088 n = hweight32(ctxm->instance_bmap); 9089 for (i = 0; i < n; i++) 9090 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9091 9092 kfree(ctx_pg); 9093 ctxm->pg_info = NULL; 9094 ctxm->mem_valid = 0; 9095 } 9096 memset(ctxm, 0, sizeof(*ctxm)); 9097 } 9098 9099 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9100 { 9101 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9102 u16 type; 9103 9104 if (!ctx) 9105 return; 9106 9107 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9108 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9109 9110 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9111 if (force) { 9112 kfree(ctx); 9113 bp->ctx = NULL; 9114 } 9115 } 9116 9117 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9118 { 9119 struct bnxt_ctx_mem_type *ctxm; 9120 struct bnxt_ctx_mem_info *ctx; 9121 u32 l2_qps, qp1_qps, max_qps; 9122 u32 ena, entries_sp, entries; 9123 u32 srqs, max_srqs, min; 9124 u32 num_mr, num_ah; 9125 u32 extra_srqs = 0; 9126 u32 extra_qps = 0; 9127 u32 fast_qpmd_qps; 9128 u8 pg_lvl = 1; 9129 int i, rc; 9130 9131 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9132 if (rc) { 9133 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9134 rc); 9135 return rc; 9136 } 9137 ctx = bp->ctx; 9138 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9139 return 0; 9140 9141 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9142 l2_qps = ctxm->qp_l2_entries; 9143 qp1_qps = ctxm->qp_qp1_entries; 9144 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9145 max_qps = ctxm->max_entries; 9146 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9147 srqs = ctxm->srq_l2_entries; 9148 max_srqs = ctxm->max_entries; 9149 ena = 0; 9150 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9151 pg_lvl = 2; 9152 if (BNXT_SW_RES_LMT(bp)) { 9153 extra_qps = max_qps - l2_qps - qp1_qps; 9154 extra_srqs = max_srqs - srqs; 9155 } else { 9156 extra_qps = min_t(u32, 65536, 9157 max_qps - l2_qps - qp1_qps); 9158 /* allocate extra qps if fw supports RoCE fast qp 9159 * destroy feature 9160 */ 9161 extra_qps += fast_qpmd_qps; 9162 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9163 } 9164 if (fast_qpmd_qps) 9165 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9166 } 9167 9168 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9169 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9170 pg_lvl); 9171 if (rc) 9172 return rc; 9173 9174 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9175 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9176 if (rc) 9177 return rc; 9178 9179 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9180 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9181 extra_qps * 2, pg_lvl); 9182 if (rc) 9183 return rc; 9184 9185 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9186 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9187 if (rc) 9188 return rc; 9189 9190 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9191 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9192 if (rc) 9193 return rc; 9194 9195 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9196 goto skip_rdma; 9197 9198 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9199 if (BNXT_SW_RES_LMT(bp) && 9200 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9201 num_ah = ctxm->mrav_av_entries; 9202 num_mr = ctxm->max_entries - num_ah; 9203 } else { 9204 /* 128K extra is needed to accommodate static AH context 9205 * allocation by f/w. 9206 */ 9207 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9208 num_ah = min_t(u32, num_mr, 1024 * 128); 9209 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9210 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9211 ctxm->mrav_av_entries = num_ah; 9212 } 9213 9214 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9215 if (rc) 9216 return rc; 9217 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9218 9219 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9220 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9221 if (rc) 9222 return rc; 9223 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9224 9225 skip_rdma: 9226 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9227 min = ctxm->min_entries; 9228 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9229 2 * (extra_qps + qp1_qps) + min; 9230 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9231 if (rc) 9232 return rc; 9233 9234 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9235 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9236 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9237 if (rc) 9238 return rc; 9239 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9240 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9241 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9242 9243 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9244 rc = bnxt_backing_store_cfg_v2(bp, ena); 9245 else 9246 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9247 if (rc) { 9248 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9249 rc); 9250 return rc; 9251 } 9252 ctx->flags |= BNXT_CTX_FLAG_INITED; 9253 return 0; 9254 } 9255 9256 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9257 { 9258 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9259 u16 page_attr; 9260 int rc; 9261 9262 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9263 return 0; 9264 9265 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9266 if (rc) 9267 return rc; 9268 9269 if (BNXT_PAGE_SIZE == 0x2000) 9270 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9271 else if (BNXT_PAGE_SIZE == 0x10000) 9272 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9273 else 9274 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9275 req->pg_size_lvl = cpu_to_le16(page_attr | 9276 bp->fw_crash_mem->ring_mem.depth); 9277 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9278 req->size = cpu_to_le32(bp->fw_crash_len); 9279 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9280 return hwrm_req_send(bp, req); 9281 } 9282 9283 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9284 { 9285 if (bp->fw_crash_mem) { 9286 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9287 kfree(bp->fw_crash_mem); 9288 bp->fw_crash_mem = NULL; 9289 } 9290 } 9291 9292 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9293 { 9294 u32 mem_size = 0; 9295 int rc; 9296 9297 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9298 return 0; 9299 9300 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9301 if (rc) 9302 return rc; 9303 9304 mem_size = round_up(mem_size, 4); 9305 9306 /* keep and use the existing pages */ 9307 if (bp->fw_crash_mem && 9308 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9309 goto alloc_done; 9310 9311 if (bp->fw_crash_mem) 9312 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9313 else 9314 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 9315 GFP_KERNEL); 9316 if (!bp->fw_crash_mem) 9317 return -ENOMEM; 9318 9319 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9320 if (rc) { 9321 bnxt_free_crash_dump_mem(bp); 9322 return rc; 9323 } 9324 9325 alloc_done: 9326 bp->fw_crash_len = mem_size; 9327 return 0; 9328 } 9329 9330 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9331 { 9332 struct hwrm_func_resource_qcaps_output *resp; 9333 struct hwrm_func_resource_qcaps_input *req; 9334 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9335 int rc; 9336 9337 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9338 if (rc) 9339 return rc; 9340 9341 req->fid = cpu_to_le16(0xffff); 9342 resp = hwrm_req_hold(bp, req); 9343 rc = hwrm_req_send_silent(bp, req); 9344 if (rc) 9345 goto hwrm_func_resc_qcaps_exit; 9346 9347 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9348 if (!all) 9349 goto hwrm_func_resc_qcaps_exit; 9350 9351 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9352 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9353 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9354 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9355 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9356 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9357 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9358 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9359 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9360 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9361 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9362 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9363 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9364 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9365 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9366 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9367 9368 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9369 u16 max_msix = le16_to_cpu(resp->max_msix); 9370 9371 hw_resc->max_nqs = max_msix; 9372 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9373 } 9374 9375 if (BNXT_PF(bp)) { 9376 struct bnxt_pf_info *pf = &bp->pf; 9377 9378 pf->vf_resv_strategy = 9379 le16_to_cpu(resp->vf_reservation_strategy); 9380 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9381 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9382 } 9383 hwrm_func_resc_qcaps_exit: 9384 hwrm_req_drop(bp, req); 9385 return rc; 9386 } 9387 9388 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9389 { 9390 struct hwrm_port_mac_ptp_qcfg_output *resp; 9391 struct hwrm_port_mac_ptp_qcfg_input *req; 9392 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9393 u8 flags; 9394 int rc; 9395 9396 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9397 rc = -ENODEV; 9398 goto no_ptp; 9399 } 9400 9401 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9402 if (rc) 9403 goto no_ptp; 9404 9405 req->port_id = cpu_to_le16(bp->pf.port_id); 9406 resp = hwrm_req_hold(bp, req); 9407 rc = hwrm_req_send(bp, req); 9408 if (rc) 9409 goto exit; 9410 9411 flags = resp->flags; 9412 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9413 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9414 rc = -ENODEV; 9415 goto exit; 9416 } 9417 if (!ptp) { 9418 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9419 if (!ptp) { 9420 rc = -ENOMEM; 9421 goto exit; 9422 } 9423 ptp->bp = bp; 9424 bp->ptp_cfg = ptp; 9425 } 9426 9427 if (flags & 9428 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9429 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9430 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9431 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9432 } else if (BNXT_CHIP_P5(bp)) { 9433 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9434 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9435 } else { 9436 rc = -ENODEV; 9437 goto exit; 9438 } 9439 ptp->rtc_configured = 9440 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9441 rc = bnxt_ptp_init(bp); 9442 if (rc) 9443 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9444 exit: 9445 hwrm_req_drop(bp, req); 9446 if (!rc) 9447 return 0; 9448 9449 no_ptp: 9450 bnxt_ptp_clear(bp); 9451 kfree(ptp); 9452 bp->ptp_cfg = NULL; 9453 return rc; 9454 } 9455 9456 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9457 { 9458 struct hwrm_func_qcaps_output *resp; 9459 struct hwrm_func_qcaps_input *req; 9460 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9461 u32 flags, flags_ext, flags_ext2; 9462 int rc; 9463 9464 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9465 if (rc) 9466 return rc; 9467 9468 req->fid = cpu_to_le16(0xffff); 9469 resp = hwrm_req_hold(bp, req); 9470 rc = hwrm_req_send(bp, req); 9471 if (rc) 9472 goto hwrm_func_qcaps_exit; 9473 9474 flags = le32_to_cpu(resp->flags); 9475 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9476 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9477 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9478 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9479 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9480 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9481 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9482 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9483 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9484 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9485 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9486 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9487 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9488 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9489 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9490 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9491 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9492 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9493 9494 flags_ext = le32_to_cpu(resp->flags_ext); 9495 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9496 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9497 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9498 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9499 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9500 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9501 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9502 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9503 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9504 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9505 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9506 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9507 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9508 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9509 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9510 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9511 9512 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9513 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9514 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9515 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9516 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9517 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9518 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9519 if (flags_ext2 & 9520 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9521 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9522 if (BNXT_PF(bp) && 9523 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9524 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9525 9526 bp->tx_push_thresh = 0; 9527 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9528 BNXT_FW_MAJ(bp) > 217) 9529 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9530 9531 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9532 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9533 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9534 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9535 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9536 if (!hw_resc->max_hw_ring_grps) 9537 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9538 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9539 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9540 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9541 9542 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9543 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9544 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9545 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9546 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9547 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9548 9549 if (BNXT_PF(bp)) { 9550 struct bnxt_pf_info *pf = &bp->pf; 9551 9552 pf->fw_fid = le16_to_cpu(resp->fid); 9553 pf->port_id = le16_to_cpu(resp->port_id); 9554 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9555 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9556 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9557 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9558 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9559 bp->flags |= BNXT_FLAG_WOL_CAP; 9560 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9561 bp->fw_cap |= BNXT_FW_CAP_PTP; 9562 } else { 9563 bnxt_ptp_clear(bp); 9564 kfree(bp->ptp_cfg); 9565 bp->ptp_cfg = NULL; 9566 } 9567 } else { 9568 #ifdef CONFIG_BNXT_SRIOV 9569 struct bnxt_vf_info *vf = &bp->vf; 9570 9571 vf->fw_fid = le16_to_cpu(resp->fid); 9572 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9573 #endif 9574 } 9575 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9576 9577 hwrm_func_qcaps_exit: 9578 hwrm_req_drop(bp, req); 9579 return rc; 9580 } 9581 9582 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9583 { 9584 struct hwrm_dbg_qcaps_output *resp; 9585 struct hwrm_dbg_qcaps_input *req; 9586 int rc; 9587 9588 bp->fw_dbg_cap = 0; 9589 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9590 return; 9591 9592 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9593 if (rc) 9594 return; 9595 9596 req->fid = cpu_to_le16(0xffff); 9597 resp = hwrm_req_hold(bp, req); 9598 rc = hwrm_req_send(bp, req); 9599 if (rc) 9600 goto hwrm_dbg_qcaps_exit; 9601 9602 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9603 9604 hwrm_dbg_qcaps_exit: 9605 hwrm_req_drop(bp, req); 9606 } 9607 9608 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9609 9610 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9611 { 9612 int rc; 9613 9614 rc = __bnxt_hwrm_func_qcaps(bp); 9615 if (rc) 9616 return rc; 9617 9618 bnxt_hwrm_dbg_qcaps(bp); 9619 9620 rc = bnxt_hwrm_queue_qportcfg(bp); 9621 if (rc) { 9622 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9623 return rc; 9624 } 9625 if (bp->hwrm_spec_code >= 0x10803) { 9626 rc = bnxt_alloc_ctx_mem(bp); 9627 if (rc) 9628 return rc; 9629 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9630 if (!rc) 9631 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9632 } 9633 return 0; 9634 } 9635 9636 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9637 { 9638 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9639 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9640 u32 flags; 9641 int rc; 9642 9643 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9644 return 0; 9645 9646 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9647 if (rc) 9648 return rc; 9649 9650 resp = hwrm_req_hold(bp, req); 9651 rc = hwrm_req_send(bp, req); 9652 if (rc) 9653 goto hwrm_cfa_adv_qcaps_exit; 9654 9655 flags = le32_to_cpu(resp->flags); 9656 if (flags & 9657 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9658 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9659 9660 if (flags & 9661 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9662 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9663 9664 if (flags & 9665 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9666 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9667 9668 hwrm_cfa_adv_qcaps_exit: 9669 hwrm_req_drop(bp, req); 9670 return rc; 9671 } 9672 9673 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9674 { 9675 if (bp->fw_health) 9676 return 0; 9677 9678 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9679 if (!bp->fw_health) 9680 return -ENOMEM; 9681 9682 mutex_init(&bp->fw_health->lock); 9683 return 0; 9684 } 9685 9686 static int bnxt_alloc_fw_health(struct bnxt *bp) 9687 { 9688 int rc; 9689 9690 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9691 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9692 return 0; 9693 9694 rc = __bnxt_alloc_fw_health(bp); 9695 if (rc) { 9696 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9697 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9698 return rc; 9699 } 9700 9701 return 0; 9702 } 9703 9704 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9705 { 9706 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9707 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9708 BNXT_FW_HEALTH_WIN_MAP_OFF); 9709 } 9710 9711 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9712 { 9713 struct bnxt_fw_health *fw_health = bp->fw_health; 9714 u32 reg_type; 9715 9716 if (!fw_health) 9717 return; 9718 9719 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9720 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9721 fw_health->status_reliable = false; 9722 9723 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9724 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9725 fw_health->resets_reliable = false; 9726 } 9727 9728 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9729 { 9730 void __iomem *hs; 9731 u32 status_loc; 9732 u32 reg_type; 9733 u32 sig; 9734 9735 if (bp->fw_health) 9736 bp->fw_health->status_reliable = false; 9737 9738 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9739 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9740 9741 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9742 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9743 if (!bp->chip_num) { 9744 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9745 bp->chip_num = readl(bp->bar0 + 9746 BNXT_FW_HEALTH_WIN_BASE + 9747 BNXT_GRC_REG_CHIP_NUM); 9748 } 9749 if (!BNXT_CHIP_P5_PLUS(bp)) 9750 return; 9751 9752 status_loc = BNXT_GRC_REG_STATUS_P5 | 9753 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9754 } else { 9755 status_loc = readl(hs + offsetof(struct hcomm_status, 9756 fw_status_loc)); 9757 } 9758 9759 if (__bnxt_alloc_fw_health(bp)) { 9760 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9761 return; 9762 } 9763 9764 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9765 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9766 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9767 __bnxt_map_fw_health_reg(bp, status_loc); 9768 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9769 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9770 } 9771 9772 bp->fw_health->status_reliable = true; 9773 } 9774 9775 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9776 { 9777 struct bnxt_fw_health *fw_health = bp->fw_health; 9778 u32 reg_base = 0xffffffff; 9779 int i; 9780 9781 bp->fw_health->status_reliable = false; 9782 bp->fw_health->resets_reliable = false; 9783 /* Only pre-map the monitoring GRC registers using window 3 */ 9784 for (i = 0; i < 4; i++) { 9785 u32 reg = fw_health->regs[i]; 9786 9787 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9788 continue; 9789 if (reg_base == 0xffffffff) 9790 reg_base = reg & BNXT_GRC_BASE_MASK; 9791 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9792 return -ERANGE; 9793 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9794 } 9795 bp->fw_health->status_reliable = true; 9796 bp->fw_health->resets_reliable = true; 9797 if (reg_base == 0xffffffff) 9798 return 0; 9799 9800 __bnxt_map_fw_health_reg(bp, reg_base); 9801 return 0; 9802 } 9803 9804 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9805 { 9806 if (!bp->fw_health) 9807 return; 9808 9809 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9810 bp->fw_health->status_reliable = true; 9811 bp->fw_health->resets_reliable = true; 9812 } else { 9813 bnxt_try_map_fw_health_reg(bp); 9814 } 9815 } 9816 9817 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 9818 { 9819 struct bnxt_fw_health *fw_health = bp->fw_health; 9820 struct hwrm_error_recovery_qcfg_output *resp; 9821 struct hwrm_error_recovery_qcfg_input *req; 9822 int rc, i; 9823 9824 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9825 return 0; 9826 9827 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 9828 if (rc) 9829 return rc; 9830 9831 resp = hwrm_req_hold(bp, req); 9832 rc = hwrm_req_send(bp, req); 9833 if (rc) 9834 goto err_recovery_out; 9835 fw_health->flags = le32_to_cpu(resp->flags); 9836 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 9837 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 9838 rc = -EINVAL; 9839 goto err_recovery_out; 9840 } 9841 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 9842 fw_health->master_func_wait_dsecs = 9843 le32_to_cpu(resp->master_func_wait_period); 9844 fw_health->normal_func_wait_dsecs = 9845 le32_to_cpu(resp->normal_func_wait_period); 9846 fw_health->post_reset_wait_dsecs = 9847 le32_to_cpu(resp->master_func_wait_period_after_reset); 9848 fw_health->post_reset_max_wait_dsecs = 9849 le32_to_cpu(resp->max_bailout_time_after_reset); 9850 fw_health->regs[BNXT_FW_HEALTH_REG] = 9851 le32_to_cpu(resp->fw_health_status_reg); 9852 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 9853 le32_to_cpu(resp->fw_heartbeat_reg); 9854 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 9855 le32_to_cpu(resp->fw_reset_cnt_reg); 9856 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 9857 le32_to_cpu(resp->reset_inprogress_reg); 9858 fw_health->fw_reset_inprog_reg_mask = 9859 le32_to_cpu(resp->reset_inprogress_reg_mask); 9860 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 9861 if (fw_health->fw_reset_seq_cnt >= 16) { 9862 rc = -EINVAL; 9863 goto err_recovery_out; 9864 } 9865 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 9866 fw_health->fw_reset_seq_regs[i] = 9867 le32_to_cpu(resp->reset_reg[i]); 9868 fw_health->fw_reset_seq_vals[i] = 9869 le32_to_cpu(resp->reset_reg_val[i]); 9870 fw_health->fw_reset_seq_delay_msec[i] = 9871 resp->delay_after_reset[i]; 9872 } 9873 err_recovery_out: 9874 hwrm_req_drop(bp, req); 9875 if (!rc) 9876 rc = bnxt_map_fw_health_regs(bp); 9877 if (rc) 9878 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9879 return rc; 9880 } 9881 9882 static int bnxt_hwrm_func_reset(struct bnxt *bp) 9883 { 9884 struct hwrm_func_reset_input *req; 9885 int rc; 9886 9887 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 9888 if (rc) 9889 return rc; 9890 9891 req->enables = 0; 9892 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 9893 return hwrm_req_send(bp, req); 9894 } 9895 9896 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 9897 { 9898 struct hwrm_nvm_get_dev_info_output nvm_info; 9899 9900 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 9901 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 9902 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 9903 nvm_info.nvm_cfg_ver_upd); 9904 } 9905 9906 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 9907 { 9908 struct hwrm_queue_qportcfg_output *resp; 9909 struct hwrm_queue_qportcfg_input *req; 9910 u8 i, j, *qptr; 9911 bool no_rdma; 9912 int rc = 0; 9913 9914 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 9915 if (rc) 9916 return rc; 9917 9918 resp = hwrm_req_hold(bp, req); 9919 rc = hwrm_req_send(bp, req); 9920 if (rc) 9921 goto qportcfg_exit; 9922 9923 if (!resp->max_configurable_queues) { 9924 rc = -EINVAL; 9925 goto qportcfg_exit; 9926 } 9927 bp->max_tc = resp->max_configurable_queues; 9928 bp->max_lltc = resp->max_configurable_lossless_queues; 9929 if (bp->max_tc > BNXT_MAX_QUEUE) 9930 bp->max_tc = BNXT_MAX_QUEUE; 9931 9932 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 9933 qptr = &resp->queue_id0; 9934 for (i = 0, j = 0; i < bp->max_tc; i++) { 9935 bp->q_info[j].queue_id = *qptr; 9936 bp->q_ids[i] = *qptr++; 9937 bp->q_info[j].queue_profile = *qptr++; 9938 bp->tc_to_qidx[j] = j; 9939 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 9940 (no_rdma && BNXT_PF(bp))) 9941 j++; 9942 } 9943 bp->max_q = bp->max_tc; 9944 bp->max_tc = max_t(u8, j, 1); 9945 9946 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 9947 bp->max_tc = 1; 9948 9949 if (bp->max_lltc > bp->max_tc) 9950 bp->max_lltc = bp->max_tc; 9951 9952 qportcfg_exit: 9953 hwrm_req_drop(bp, req); 9954 return rc; 9955 } 9956 9957 static int bnxt_hwrm_poll(struct bnxt *bp) 9958 { 9959 struct hwrm_ver_get_input *req; 9960 int rc; 9961 9962 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9963 if (rc) 9964 return rc; 9965 9966 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9967 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9968 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9969 9970 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 9971 rc = hwrm_req_send(bp, req); 9972 return rc; 9973 } 9974 9975 static int bnxt_hwrm_ver_get(struct bnxt *bp) 9976 { 9977 struct hwrm_ver_get_output *resp; 9978 struct hwrm_ver_get_input *req; 9979 u16 fw_maj, fw_min, fw_bld, fw_rsv; 9980 u32 dev_caps_cfg, hwrm_ver; 9981 int rc, len; 9982 9983 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9984 if (rc) 9985 return rc; 9986 9987 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 9988 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 9989 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9990 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9991 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9992 9993 resp = hwrm_req_hold(bp, req); 9994 rc = hwrm_req_send(bp, req); 9995 if (rc) 9996 goto hwrm_ver_get_exit; 9997 9998 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 9999 10000 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10001 resp->hwrm_intf_min_8b << 8 | 10002 resp->hwrm_intf_upd_8b; 10003 if (resp->hwrm_intf_maj_8b < 1) { 10004 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10005 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10006 resp->hwrm_intf_upd_8b); 10007 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10008 } 10009 10010 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10011 HWRM_VERSION_UPDATE; 10012 10013 if (bp->hwrm_spec_code > hwrm_ver) 10014 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10015 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10016 HWRM_VERSION_UPDATE); 10017 else 10018 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10019 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10020 resp->hwrm_intf_upd_8b); 10021 10022 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10023 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10024 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10025 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10026 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10027 len = FW_VER_STR_LEN; 10028 } else { 10029 fw_maj = resp->hwrm_fw_maj_8b; 10030 fw_min = resp->hwrm_fw_min_8b; 10031 fw_bld = resp->hwrm_fw_bld_8b; 10032 fw_rsv = resp->hwrm_fw_rsvd_8b; 10033 len = BC_HWRM_STR_LEN; 10034 } 10035 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10036 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10037 fw_rsv); 10038 10039 if (strlen(resp->active_pkg_name)) { 10040 int fw_ver_len = strlen(bp->fw_ver_str); 10041 10042 snprintf(bp->fw_ver_str + fw_ver_len, 10043 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10044 resp->active_pkg_name); 10045 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10046 } 10047 10048 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10049 if (!bp->hwrm_cmd_timeout) 10050 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10051 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10052 if (!bp->hwrm_cmd_max_timeout) 10053 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10054 else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT) 10055 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n", 10056 bp->hwrm_cmd_max_timeout / 1000); 10057 10058 if (resp->hwrm_intf_maj_8b >= 1) { 10059 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10060 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10061 } 10062 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10063 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10064 10065 bp->chip_num = le16_to_cpu(resp->chip_num); 10066 bp->chip_rev = resp->chip_rev; 10067 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10068 !resp->chip_metal) 10069 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10070 10071 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10072 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10073 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10074 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10075 10076 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10077 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10078 10079 if (dev_caps_cfg & 10080 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10081 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10082 10083 if (dev_caps_cfg & 10084 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10085 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10086 10087 if (dev_caps_cfg & 10088 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10089 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10090 10091 hwrm_ver_get_exit: 10092 hwrm_req_drop(bp, req); 10093 return rc; 10094 } 10095 10096 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10097 { 10098 struct hwrm_fw_set_time_input *req; 10099 struct tm tm; 10100 time64_t now = ktime_get_real_seconds(); 10101 int rc; 10102 10103 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10104 bp->hwrm_spec_code < 0x10400) 10105 return -EOPNOTSUPP; 10106 10107 time64_to_tm(now, 0, &tm); 10108 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10109 if (rc) 10110 return rc; 10111 10112 req->year = cpu_to_le16(1900 + tm.tm_year); 10113 req->month = 1 + tm.tm_mon; 10114 req->day = tm.tm_mday; 10115 req->hour = tm.tm_hour; 10116 req->minute = tm.tm_min; 10117 req->second = tm.tm_sec; 10118 return hwrm_req_send(bp, req); 10119 } 10120 10121 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10122 { 10123 u64 sw_tmp; 10124 10125 hw &= mask; 10126 sw_tmp = (*sw & ~mask) | hw; 10127 if (hw < (*sw & mask)) 10128 sw_tmp += mask + 1; 10129 WRITE_ONCE(*sw, sw_tmp); 10130 } 10131 10132 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10133 int count, bool ignore_zero) 10134 { 10135 int i; 10136 10137 for (i = 0; i < count; i++) { 10138 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10139 10140 if (ignore_zero && !hw) 10141 continue; 10142 10143 if (masks[i] == -1ULL) 10144 sw_stats[i] = hw; 10145 else 10146 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10147 } 10148 } 10149 10150 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10151 { 10152 if (!stats->hw_stats) 10153 return; 10154 10155 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10156 stats->hw_masks, stats->len / 8, false); 10157 } 10158 10159 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10160 { 10161 struct bnxt_stats_mem *ring0_stats; 10162 bool ignore_zero = false; 10163 int i; 10164 10165 /* Chip bug. Counter intermittently becomes 0. */ 10166 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10167 ignore_zero = true; 10168 10169 for (i = 0; i < bp->cp_nr_rings; i++) { 10170 struct bnxt_napi *bnapi = bp->bnapi[i]; 10171 struct bnxt_cp_ring_info *cpr; 10172 struct bnxt_stats_mem *stats; 10173 10174 cpr = &bnapi->cp_ring; 10175 stats = &cpr->stats; 10176 if (!i) 10177 ring0_stats = stats; 10178 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10179 ring0_stats->hw_masks, 10180 ring0_stats->len / 8, ignore_zero); 10181 } 10182 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10183 struct bnxt_stats_mem *stats = &bp->port_stats; 10184 __le64 *hw_stats = stats->hw_stats; 10185 u64 *sw_stats = stats->sw_stats; 10186 u64 *masks = stats->hw_masks; 10187 int cnt; 10188 10189 cnt = sizeof(struct rx_port_stats) / 8; 10190 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10191 10192 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10193 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10194 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10195 cnt = sizeof(struct tx_port_stats) / 8; 10196 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10197 } 10198 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10199 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10200 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10201 } 10202 } 10203 10204 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10205 { 10206 struct hwrm_port_qstats_input *req; 10207 struct bnxt_pf_info *pf = &bp->pf; 10208 int rc; 10209 10210 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10211 return 0; 10212 10213 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10214 return -EOPNOTSUPP; 10215 10216 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10217 if (rc) 10218 return rc; 10219 10220 req->flags = flags; 10221 req->port_id = cpu_to_le16(pf->port_id); 10222 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10223 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10224 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10225 return hwrm_req_send(bp, req); 10226 } 10227 10228 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10229 { 10230 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10231 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10232 struct hwrm_port_qstats_ext_output *resp_qs; 10233 struct hwrm_port_qstats_ext_input *req_qs; 10234 struct bnxt_pf_info *pf = &bp->pf; 10235 u32 tx_stat_size; 10236 int rc; 10237 10238 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10239 return 0; 10240 10241 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10242 return -EOPNOTSUPP; 10243 10244 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10245 if (rc) 10246 return rc; 10247 10248 req_qs->flags = flags; 10249 req_qs->port_id = cpu_to_le16(pf->port_id); 10250 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10251 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10252 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10253 sizeof(struct tx_port_stats_ext) : 0; 10254 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10255 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10256 resp_qs = hwrm_req_hold(bp, req_qs); 10257 rc = hwrm_req_send(bp, req_qs); 10258 if (!rc) { 10259 bp->fw_rx_stats_ext_size = 10260 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10261 if (BNXT_FW_MAJ(bp) < 220 && 10262 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10263 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10264 10265 bp->fw_tx_stats_ext_size = tx_stat_size ? 10266 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10267 } else { 10268 bp->fw_rx_stats_ext_size = 0; 10269 bp->fw_tx_stats_ext_size = 0; 10270 } 10271 hwrm_req_drop(bp, req_qs); 10272 10273 if (flags) 10274 return rc; 10275 10276 if (bp->fw_tx_stats_ext_size <= 10277 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10278 bp->pri2cos_valid = 0; 10279 return rc; 10280 } 10281 10282 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10283 if (rc) 10284 return rc; 10285 10286 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10287 10288 resp_qc = hwrm_req_hold(bp, req_qc); 10289 rc = hwrm_req_send(bp, req_qc); 10290 if (!rc) { 10291 u8 *pri2cos; 10292 int i, j; 10293 10294 pri2cos = &resp_qc->pri0_cos_queue_id; 10295 for (i = 0; i < 8; i++) { 10296 u8 queue_id = pri2cos[i]; 10297 u8 queue_idx; 10298 10299 /* Per port queue IDs start from 0, 10, 20, etc */ 10300 queue_idx = queue_id % 10; 10301 if (queue_idx > BNXT_MAX_QUEUE) { 10302 bp->pri2cos_valid = false; 10303 hwrm_req_drop(bp, req_qc); 10304 return rc; 10305 } 10306 for (j = 0; j < bp->max_q; j++) { 10307 if (bp->q_ids[j] == queue_id) 10308 bp->pri2cos_idx[i] = queue_idx; 10309 } 10310 } 10311 bp->pri2cos_valid = true; 10312 } 10313 hwrm_req_drop(bp, req_qc); 10314 10315 return rc; 10316 } 10317 10318 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10319 { 10320 bnxt_hwrm_tunnel_dst_port_free(bp, 10321 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10322 bnxt_hwrm_tunnel_dst_port_free(bp, 10323 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10324 } 10325 10326 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10327 { 10328 int rc, i; 10329 u32 tpa_flags = 0; 10330 10331 if (set_tpa) 10332 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10333 else if (BNXT_NO_FW_ACCESS(bp)) 10334 return 0; 10335 for (i = 0; i < bp->nr_vnics; i++) { 10336 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10337 if (rc) { 10338 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10339 i, rc); 10340 return rc; 10341 } 10342 } 10343 return 0; 10344 } 10345 10346 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10347 { 10348 int i; 10349 10350 for (i = 0; i < bp->nr_vnics; i++) 10351 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10352 } 10353 10354 static void bnxt_clear_vnic(struct bnxt *bp) 10355 { 10356 if (!bp->vnic_info) 10357 return; 10358 10359 bnxt_hwrm_clear_vnic_filter(bp); 10360 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10361 /* clear all RSS setting before free vnic ctx */ 10362 bnxt_hwrm_clear_vnic_rss(bp); 10363 bnxt_hwrm_vnic_ctx_free(bp); 10364 } 10365 /* before free the vnic, undo the vnic tpa settings */ 10366 if (bp->flags & BNXT_FLAG_TPA) 10367 bnxt_set_tpa(bp, false); 10368 bnxt_hwrm_vnic_free(bp); 10369 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10370 bnxt_hwrm_vnic_ctx_free(bp); 10371 } 10372 10373 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10374 bool irq_re_init) 10375 { 10376 bnxt_clear_vnic(bp); 10377 bnxt_hwrm_ring_free(bp, close_path); 10378 bnxt_hwrm_ring_grp_free(bp); 10379 if (irq_re_init) { 10380 bnxt_hwrm_stat_ctx_free(bp); 10381 bnxt_hwrm_free_tunnel_ports(bp); 10382 } 10383 } 10384 10385 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10386 { 10387 struct hwrm_func_cfg_input *req; 10388 u8 evb_mode; 10389 int rc; 10390 10391 if (br_mode == BRIDGE_MODE_VEB) 10392 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10393 else if (br_mode == BRIDGE_MODE_VEPA) 10394 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10395 else 10396 return -EINVAL; 10397 10398 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10399 if (rc) 10400 return rc; 10401 10402 req->fid = cpu_to_le16(0xffff); 10403 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10404 req->evb_mode = evb_mode; 10405 return hwrm_req_send(bp, req); 10406 } 10407 10408 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10409 { 10410 struct hwrm_func_cfg_input *req; 10411 int rc; 10412 10413 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10414 return 0; 10415 10416 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10417 if (rc) 10418 return rc; 10419 10420 req->fid = cpu_to_le16(0xffff); 10421 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10422 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10423 if (size == 128) 10424 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10425 10426 return hwrm_req_send(bp, req); 10427 } 10428 10429 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10430 { 10431 int rc; 10432 10433 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10434 goto skip_rss_ctx; 10435 10436 /* allocate context for vnic */ 10437 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10438 if (rc) { 10439 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10440 vnic->vnic_id, rc); 10441 goto vnic_setup_err; 10442 } 10443 bp->rsscos_nr_ctxs++; 10444 10445 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10446 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10447 if (rc) { 10448 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10449 vnic->vnic_id, rc); 10450 goto vnic_setup_err; 10451 } 10452 bp->rsscos_nr_ctxs++; 10453 } 10454 10455 skip_rss_ctx: 10456 /* configure default vnic, ring grp */ 10457 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10458 if (rc) { 10459 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10460 vnic->vnic_id, rc); 10461 goto vnic_setup_err; 10462 } 10463 10464 /* Enable RSS hashing on vnic */ 10465 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10466 if (rc) { 10467 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10468 vnic->vnic_id, rc); 10469 goto vnic_setup_err; 10470 } 10471 10472 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10473 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10474 if (rc) { 10475 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10476 vnic->vnic_id, rc); 10477 } 10478 } 10479 10480 vnic_setup_err: 10481 return rc; 10482 } 10483 10484 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10485 u8 valid) 10486 { 10487 struct hwrm_vnic_update_input *req; 10488 int rc; 10489 10490 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10491 if (rc) 10492 return rc; 10493 10494 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10495 10496 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10497 req->mru = cpu_to_le16(vnic->mru); 10498 10499 req->enables = cpu_to_le32(valid); 10500 10501 return hwrm_req_send(bp, req); 10502 } 10503 10504 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10505 { 10506 int rc; 10507 10508 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10509 if (rc) { 10510 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10511 vnic->vnic_id, rc); 10512 return rc; 10513 } 10514 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10515 if (rc) 10516 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10517 vnic->vnic_id, rc); 10518 return rc; 10519 } 10520 10521 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10522 { 10523 int rc, i, nr_ctxs; 10524 10525 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10526 for (i = 0; i < nr_ctxs; i++) { 10527 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10528 if (rc) { 10529 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10530 vnic->vnic_id, i, rc); 10531 break; 10532 } 10533 bp->rsscos_nr_ctxs++; 10534 } 10535 if (i < nr_ctxs) 10536 return -ENOMEM; 10537 10538 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10539 if (rc) 10540 return rc; 10541 10542 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10543 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10544 if (rc) { 10545 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10546 vnic->vnic_id, rc); 10547 } 10548 } 10549 return rc; 10550 } 10551 10552 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10553 { 10554 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10555 return __bnxt_setup_vnic_p5(bp, vnic); 10556 else 10557 return __bnxt_setup_vnic(bp, vnic); 10558 } 10559 10560 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10561 struct bnxt_vnic_info *vnic, 10562 u16 start_rx_ring_idx, int rx_rings) 10563 { 10564 int rc; 10565 10566 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10567 if (rc) { 10568 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10569 vnic->vnic_id, rc); 10570 return rc; 10571 } 10572 return bnxt_setup_vnic(bp, vnic); 10573 } 10574 10575 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10576 { 10577 struct bnxt_vnic_info *vnic; 10578 int i, rc = 0; 10579 10580 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10581 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10582 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10583 } 10584 10585 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10586 return 0; 10587 10588 for (i = 0; i < bp->rx_nr_rings; i++) { 10589 u16 vnic_id = i + 1; 10590 u16 ring_id = i; 10591 10592 if (vnic_id >= bp->nr_vnics) 10593 break; 10594 10595 vnic = &bp->vnic_info[vnic_id]; 10596 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10597 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10598 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10599 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10600 break; 10601 } 10602 return rc; 10603 } 10604 10605 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10606 bool all) 10607 { 10608 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10609 struct bnxt_filter_base *usr_fltr, *tmp; 10610 struct bnxt_ntuple_filter *ntp_fltr; 10611 int i; 10612 10613 if (netif_running(bp->dev)) { 10614 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10615 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10616 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10617 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10618 } 10619 } 10620 if (!all) 10621 return; 10622 10623 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10624 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10625 usr_fltr->fw_vnic_id == rss_ctx->index) { 10626 ntp_fltr = container_of(usr_fltr, 10627 struct bnxt_ntuple_filter, 10628 base); 10629 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10630 bnxt_del_ntp_filter(bp, ntp_fltr); 10631 bnxt_del_one_usr_fltr(bp, usr_fltr); 10632 } 10633 } 10634 10635 if (vnic->rss_table) 10636 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10637 vnic->rss_table, 10638 vnic->rss_table_dma_addr); 10639 bp->num_rss_ctx--; 10640 } 10641 10642 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10643 { 10644 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10645 struct ethtool_rxfh_context *ctx; 10646 unsigned long context; 10647 10648 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10649 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10650 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10651 10652 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10653 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10654 __bnxt_setup_vnic_p5(bp, vnic)) { 10655 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10656 rss_ctx->index); 10657 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10658 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10659 } 10660 } 10661 } 10662 10663 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10664 { 10665 struct ethtool_rxfh_context *ctx; 10666 unsigned long context; 10667 10668 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10669 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10670 10671 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10672 } 10673 } 10674 10675 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10676 static bool bnxt_promisc_ok(struct bnxt *bp) 10677 { 10678 #ifdef CONFIG_BNXT_SRIOV 10679 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10680 return false; 10681 #endif 10682 return true; 10683 } 10684 10685 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10686 { 10687 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10688 unsigned int rc = 0; 10689 10690 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10691 if (rc) { 10692 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10693 rc); 10694 return rc; 10695 } 10696 10697 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10698 if (rc) { 10699 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10700 rc); 10701 return rc; 10702 } 10703 return rc; 10704 } 10705 10706 static int bnxt_cfg_rx_mode(struct bnxt *); 10707 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10708 10709 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10710 { 10711 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10712 int rc = 0; 10713 unsigned int rx_nr_rings = bp->rx_nr_rings; 10714 10715 if (irq_re_init) { 10716 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10717 if (rc) { 10718 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10719 rc); 10720 goto err_out; 10721 } 10722 } 10723 10724 rc = bnxt_hwrm_ring_alloc(bp); 10725 if (rc) { 10726 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10727 goto err_out; 10728 } 10729 10730 rc = bnxt_hwrm_ring_grp_alloc(bp); 10731 if (rc) { 10732 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10733 goto err_out; 10734 } 10735 10736 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10737 rx_nr_rings--; 10738 10739 /* default vnic 0 */ 10740 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10741 if (rc) { 10742 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10743 goto err_out; 10744 } 10745 10746 if (BNXT_VF(bp)) 10747 bnxt_hwrm_func_qcfg(bp); 10748 10749 rc = bnxt_setup_vnic(bp, vnic); 10750 if (rc) 10751 goto err_out; 10752 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 10753 bnxt_hwrm_update_rss_hash_cfg(bp); 10754 10755 if (bp->flags & BNXT_FLAG_RFS) { 10756 rc = bnxt_alloc_rfs_vnics(bp); 10757 if (rc) 10758 goto err_out; 10759 } 10760 10761 if (bp->flags & BNXT_FLAG_TPA) { 10762 rc = bnxt_set_tpa(bp, true); 10763 if (rc) 10764 goto err_out; 10765 } 10766 10767 if (BNXT_VF(bp)) 10768 bnxt_update_vf_mac(bp); 10769 10770 /* Filter for default vnic 0 */ 10771 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 10772 if (rc) { 10773 if (BNXT_VF(bp) && rc == -ENODEV) 10774 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 10775 else 10776 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10777 goto err_out; 10778 } 10779 vnic->uc_filter_count = 1; 10780 10781 vnic->rx_mask = 0; 10782 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 10783 goto skip_rx_mask; 10784 10785 if (bp->dev->flags & IFF_BROADCAST) 10786 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 10787 10788 if (bp->dev->flags & IFF_PROMISC) 10789 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10790 10791 if (bp->dev->flags & IFF_ALLMULTI) { 10792 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10793 vnic->mc_list_count = 0; 10794 } else if (bp->dev->flags & IFF_MULTICAST) { 10795 u32 mask = 0; 10796 10797 bnxt_mc_list_updated(bp, &mask); 10798 vnic->rx_mask |= mask; 10799 } 10800 10801 rc = bnxt_cfg_rx_mode(bp); 10802 if (rc) 10803 goto err_out; 10804 10805 skip_rx_mask: 10806 rc = bnxt_hwrm_set_coal(bp); 10807 if (rc) 10808 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 10809 rc); 10810 10811 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10812 rc = bnxt_setup_nitroa0_vnic(bp); 10813 if (rc) 10814 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 10815 rc); 10816 } 10817 10818 if (BNXT_VF(bp)) { 10819 bnxt_hwrm_func_qcfg(bp); 10820 netdev_update_features(bp->dev); 10821 } 10822 10823 return 0; 10824 10825 err_out: 10826 bnxt_hwrm_resource_free(bp, 0, true); 10827 10828 return rc; 10829 } 10830 10831 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 10832 { 10833 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 10834 return 0; 10835 } 10836 10837 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 10838 { 10839 bnxt_init_cp_rings(bp); 10840 bnxt_init_rx_rings(bp); 10841 bnxt_init_tx_rings(bp); 10842 bnxt_init_ring_grps(bp, irq_re_init); 10843 bnxt_init_vnics(bp); 10844 10845 return bnxt_init_chip(bp, irq_re_init); 10846 } 10847 10848 static int bnxt_set_real_num_queues(struct bnxt *bp) 10849 { 10850 int rc; 10851 struct net_device *dev = bp->dev; 10852 10853 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 10854 bp->tx_nr_rings_xdp); 10855 if (rc) 10856 return rc; 10857 10858 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 10859 if (rc) 10860 return rc; 10861 10862 #ifdef CONFIG_RFS_ACCEL 10863 if (bp->flags & BNXT_FLAG_RFS) 10864 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 10865 #endif 10866 10867 return rc; 10868 } 10869 10870 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10871 bool shared) 10872 { 10873 int _rx = *rx, _tx = *tx; 10874 10875 if (shared) { 10876 *rx = min_t(int, _rx, max); 10877 *tx = min_t(int, _tx, max); 10878 } else { 10879 if (max < 2) 10880 return -ENOMEM; 10881 10882 while (_rx + _tx > max) { 10883 if (_rx > _tx && _rx > 1) 10884 _rx--; 10885 else if (_tx > 1) 10886 _tx--; 10887 } 10888 *rx = _rx; 10889 *tx = _tx; 10890 } 10891 return 0; 10892 } 10893 10894 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 10895 { 10896 return (tx - tx_xdp) / tx_sets + tx_xdp; 10897 } 10898 10899 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 10900 { 10901 int tcs = bp->num_tc; 10902 10903 if (!tcs) 10904 tcs = 1; 10905 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 10906 } 10907 10908 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 10909 { 10910 int tcs = bp->num_tc; 10911 10912 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 10913 bp->tx_nr_rings_xdp; 10914 } 10915 10916 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10917 bool sh) 10918 { 10919 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 10920 10921 if (tx_cp != *tx) { 10922 int tx_saved = tx_cp, rc; 10923 10924 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 10925 if (rc) 10926 return rc; 10927 if (tx_cp != tx_saved) 10928 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 10929 return 0; 10930 } 10931 return __bnxt_trim_rings(bp, rx, tx, max, sh); 10932 } 10933 10934 static void bnxt_setup_msix(struct bnxt *bp) 10935 { 10936 const int len = sizeof(bp->irq_tbl[0].name); 10937 struct net_device *dev = bp->dev; 10938 int tcs, i; 10939 10940 tcs = bp->num_tc; 10941 if (tcs) { 10942 int i, off, count; 10943 10944 for (i = 0; i < tcs; i++) { 10945 count = bp->tx_nr_rings_per_tc; 10946 off = BNXT_TC_TO_RING_BASE(bp, i); 10947 netdev_set_tc_queue(dev, i, count, off); 10948 } 10949 } 10950 10951 for (i = 0; i < bp->cp_nr_rings; i++) { 10952 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10953 char *attr; 10954 10955 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 10956 attr = "TxRx"; 10957 else if (i < bp->rx_nr_rings) 10958 attr = "rx"; 10959 else 10960 attr = "tx"; 10961 10962 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 10963 attr, i); 10964 bp->irq_tbl[map_idx].handler = bnxt_msix; 10965 } 10966 } 10967 10968 static int bnxt_init_int_mode(struct bnxt *bp); 10969 10970 static int bnxt_change_msix(struct bnxt *bp, int total) 10971 { 10972 struct msi_map map; 10973 int i; 10974 10975 /* add MSIX to the end if needed */ 10976 for (i = bp->total_irqs; i < total; i++) { 10977 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 10978 if (map.index < 0) 10979 return bp->total_irqs; 10980 bp->irq_tbl[i].vector = map.virq; 10981 bp->total_irqs++; 10982 } 10983 10984 /* trim MSIX from the end if needed */ 10985 for (i = bp->total_irqs; i > total; i--) { 10986 map.index = i - 1; 10987 map.virq = bp->irq_tbl[i - 1].vector; 10988 pci_msix_free_irq(bp->pdev, map); 10989 bp->total_irqs--; 10990 } 10991 return bp->total_irqs; 10992 } 10993 10994 static int bnxt_setup_int_mode(struct bnxt *bp) 10995 { 10996 int rc; 10997 10998 if (!bp->irq_tbl) { 10999 rc = bnxt_init_int_mode(bp); 11000 if (rc || !bp->irq_tbl) 11001 return rc ?: -ENODEV; 11002 } 11003 11004 bnxt_setup_msix(bp); 11005 11006 rc = bnxt_set_real_num_queues(bp); 11007 return rc; 11008 } 11009 11010 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11011 { 11012 return bp->hw_resc.max_rsscos_ctxs; 11013 } 11014 11015 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11016 { 11017 return bp->hw_resc.max_vnics; 11018 } 11019 11020 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11021 { 11022 return bp->hw_resc.max_stat_ctxs; 11023 } 11024 11025 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11026 { 11027 return bp->hw_resc.max_cp_rings; 11028 } 11029 11030 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11031 { 11032 unsigned int cp = bp->hw_resc.max_cp_rings; 11033 11034 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11035 cp -= bnxt_get_ulp_msix_num(bp); 11036 11037 return cp; 11038 } 11039 11040 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11041 { 11042 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11043 11044 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11045 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11046 11047 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11048 } 11049 11050 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11051 { 11052 bp->hw_resc.max_irqs = max_irqs; 11053 } 11054 11055 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11056 { 11057 unsigned int cp; 11058 11059 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11060 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11061 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11062 else 11063 return cp - bp->cp_nr_rings; 11064 } 11065 11066 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11067 { 11068 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11069 } 11070 11071 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11072 { 11073 int max_irq = bnxt_get_max_func_irqs(bp); 11074 int total_req = bp->cp_nr_rings + num; 11075 11076 if (max_irq < total_req) { 11077 num = max_irq - bp->cp_nr_rings; 11078 if (num <= 0) 11079 return 0; 11080 } 11081 return num; 11082 } 11083 11084 static int bnxt_get_num_msix(struct bnxt *bp) 11085 { 11086 if (!BNXT_NEW_RM(bp)) 11087 return bnxt_get_max_func_irqs(bp); 11088 11089 return bnxt_nq_rings_in_use(bp); 11090 } 11091 11092 static int bnxt_init_int_mode(struct bnxt *bp) 11093 { 11094 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 11095 11096 total_vecs = bnxt_get_num_msix(bp); 11097 max = bnxt_get_max_func_irqs(bp); 11098 if (total_vecs > max) 11099 total_vecs = max; 11100 11101 if (!total_vecs) 11102 return 0; 11103 11104 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11105 min = 2; 11106 11107 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11108 PCI_IRQ_MSIX); 11109 ulp_msix = bnxt_get_ulp_msix_num(bp); 11110 if (total_vecs < 0 || total_vecs < ulp_msix) { 11111 rc = -ENODEV; 11112 goto msix_setup_exit; 11113 } 11114 11115 tbl_size = total_vecs; 11116 if (pci_msix_can_alloc_dyn(bp->pdev)) 11117 tbl_size = max; 11118 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 11119 if (bp->irq_tbl) { 11120 for (i = 0; i < total_vecs; i++) 11121 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11122 11123 bp->total_irqs = total_vecs; 11124 /* Trim rings based upon num of vectors allocated */ 11125 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11126 total_vecs - ulp_msix, min == 1); 11127 if (rc) 11128 goto msix_setup_exit; 11129 11130 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11131 bp->cp_nr_rings = (min == 1) ? 11132 max_t(int, tx_cp, bp->rx_nr_rings) : 11133 tx_cp + bp->rx_nr_rings; 11134 11135 } else { 11136 rc = -ENOMEM; 11137 goto msix_setup_exit; 11138 } 11139 return 0; 11140 11141 msix_setup_exit: 11142 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11143 kfree(bp->irq_tbl); 11144 bp->irq_tbl = NULL; 11145 pci_free_irq_vectors(bp->pdev); 11146 return rc; 11147 } 11148 11149 static void bnxt_clear_int_mode(struct bnxt *bp) 11150 { 11151 pci_free_irq_vectors(bp->pdev); 11152 11153 kfree(bp->irq_tbl); 11154 bp->irq_tbl = NULL; 11155 } 11156 11157 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11158 { 11159 bool irq_cleared = false; 11160 bool irq_change = false; 11161 int tcs = bp->num_tc; 11162 int irqs_required; 11163 int rc; 11164 11165 if (!bnxt_need_reserve_rings(bp)) 11166 return 0; 11167 11168 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 11169 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11170 11171 if (ulp_msix > bp->ulp_num_msix_want) 11172 ulp_msix = bp->ulp_num_msix_want; 11173 irqs_required = ulp_msix + bp->cp_nr_rings; 11174 } else { 11175 irqs_required = bnxt_get_num_msix(bp); 11176 } 11177 11178 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11179 irq_change = true; 11180 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11181 bnxt_ulp_irq_stop(bp); 11182 bnxt_clear_int_mode(bp); 11183 irq_cleared = true; 11184 } 11185 } 11186 rc = __bnxt_reserve_rings(bp); 11187 if (irq_cleared) { 11188 if (!rc) 11189 rc = bnxt_init_int_mode(bp); 11190 bnxt_ulp_irq_restart(bp, rc); 11191 } else if (irq_change && !rc) { 11192 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11193 rc = -ENOSPC; 11194 } 11195 if (rc) { 11196 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11197 return rc; 11198 } 11199 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11200 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11201 netdev_err(bp->dev, "tx ring reservation failure\n"); 11202 netdev_reset_tc(bp->dev); 11203 bp->num_tc = 0; 11204 if (bp->tx_nr_rings_xdp) 11205 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11206 else 11207 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11208 return -ENOMEM; 11209 } 11210 return 0; 11211 } 11212 11213 static void bnxt_free_irq(struct bnxt *bp) 11214 { 11215 struct bnxt_irq *irq; 11216 int i; 11217 11218 #ifdef CONFIG_RFS_ACCEL 11219 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11220 bp->dev->rx_cpu_rmap = NULL; 11221 #endif 11222 if (!bp->irq_tbl || !bp->bnapi) 11223 return; 11224 11225 for (i = 0; i < bp->cp_nr_rings; i++) { 11226 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11227 11228 irq = &bp->irq_tbl[map_idx]; 11229 if (irq->requested) { 11230 if (irq->have_cpumask) { 11231 irq_update_affinity_hint(irq->vector, NULL); 11232 free_cpumask_var(irq->cpu_mask); 11233 irq->have_cpumask = 0; 11234 } 11235 free_irq(irq->vector, bp->bnapi[i]); 11236 } 11237 11238 irq->requested = 0; 11239 } 11240 } 11241 11242 static int bnxt_request_irq(struct bnxt *bp) 11243 { 11244 int i, j, rc = 0; 11245 unsigned long flags = 0; 11246 #ifdef CONFIG_RFS_ACCEL 11247 struct cpu_rmap *rmap; 11248 #endif 11249 11250 rc = bnxt_setup_int_mode(bp); 11251 if (rc) { 11252 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11253 rc); 11254 return rc; 11255 } 11256 #ifdef CONFIG_RFS_ACCEL 11257 rmap = bp->dev->rx_cpu_rmap; 11258 #endif 11259 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11260 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11261 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11262 11263 #ifdef CONFIG_RFS_ACCEL 11264 if (rmap && bp->bnapi[i]->rx_ring) { 11265 rc = irq_cpu_rmap_add(rmap, irq->vector); 11266 if (rc) 11267 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11268 j); 11269 j++; 11270 } 11271 #endif 11272 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11273 bp->bnapi[i]); 11274 if (rc) 11275 break; 11276 11277 netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector); 11278 irq->requested = 1; 11279 11280 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11281 int numa_node = dev_to_node(&bp->pdev->dev); 11282 11283 irq->have_cpumask = 1; 11284 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11285 irq->cpu_mask); 11286 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11287 if (rc) { 11288 netdev_warn(bp->dev, 11289 "Update affinity hint failed, IRQ = %d\n", 11290 irq->vector); 11291 break; 11292 } 11293 } 11294 } 11295 return rc; 11296 } 11297 11298 static void bnxt_del_napi(struct bnxt *bp) 11299 { 11300 int i; 11301 11302 if (!bp->bnapi) 11303 return; 11304 11305 for (i = 0; i < bp->rx_nr_rings; i++) 11306 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11307 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11308 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11309 11310 for (i = 0; i < bp->cp_nr_rings; i++) { 11311 struct bnxt_napi *bnapi = bp->bnapi[i]; 11312 11313 __netif_napi_del(&bnapi->napi); 11314 } 11315 /* We called __netif_napi_del(), we need 11316 * to respect an RCU grace period before freeing napi structures. 11317 */ 11318 synchronize_net(); 11319 } 11320 11321 static void bnxt_init_napi(struct bnxt *bp) 11322 { 11323 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11324 unsigned int cp_nr_rings = bp->cp_nr_rings; 11325 struct bnxt_napi *bnapi; 11326 int i; 11327 11328 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11329 poll_fn = bnxt_poll_p5; 11330 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11331 cp_nr_rings--; 11332 for (i = 0; i < cp_nr_rings; i++) { 11333 bnapi = bp->bnapi[i]; 11334 netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn, 11335 bnapi->index); 11336 } 11337 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11338 bnapi = bp->bnapi[cp_nr_rings]; 11339 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11340 } 11341 } 11342 11343 static void bnxt_disable_napi(struct bnxt *bp) 11344 { 11345 int i; 11346 11347 if (!bp->bnapi || 11348 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11349 return; 11350 11351 for (i = 0; i < bp->cp_nr_rings; i++) { 11352 struct bnxt_napi *bnapi = bp->bnapi[i]; 11353 struct bnxt_cp_ring_info *cpr; 11354 11355 cpr = &bnapi->cp_ring; 11356 if (bnapi->tx_fault) 11357 cpr->sw_stats->tx.tx_resets++; 11358 if (bnapi->in_reset) 11359 cpr->sw_stats->rx.rx_resets++; 11360 napi_disable(&bnapi->napi); 11361 } 11362 } 11363 11364 static void bnxt_enable_napi(struct bnxt *bp) 11365 { 11366 int i; 11367 11368 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11369 for (i = 0; i < bp->cp_nr_rings; i++) { 11370 struct bnxt_napi *bnapi = bp->bnapi[i]; 11371 struct bnxt_cp_ring_info *cpr; 11372 11373 bnapi->tx_fault = 0; 11374 11375 cpr = &bnapi->cp_ring; 11376 bnapi->in_reset = false; 11377 11378 if (bnapi->rx_ring) { 11379 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11380 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11381 } 11382 napi_enable(&bnapi->napi); 11383 } 11384 } 11385 11386 void bnxt_tx_disable(struct bnxt *bp) 11387 { 11388 int i; 11389 struct bnxt_tx_ring_info *txr; 11390 11391 if (bp->tx_ring) { 11392 for (i = 0; i < bp->tx_nr_rings; i++) { 11393 txr = &bp->tx_ring[i]; 11394 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11395 } 11396 } 11397 /* Make sure napi polls see @dev_state change */ 11398 synchronize_net(); 11399 /* Drop carrier first to prevent TX timeout */ 11400 netif_carrier_off(bp->dev); 11401 /* Stop all TX queues */ 11402 netif_tx_disable(bp->dev); 11403 } 11404 11405 void bnxt_tx_enable(struct bnxt *bp) 11406 { 11407 int i; 11408 struct bnxt_tx_ring_info *txr; 11409 11410 for (i = 0; i < bp->tx_nr_rings; i++) { 11411 txr = &bp->tx_ring[i]; 11412 WRITE_ONCE(txr->dev_state, 0); 11413 } 11414 /* Make sure napi polls see @dev_state change */ 11415 synchronize_net(); 11416 netif_tx_wake_all_queues(bp->dev); 11417 if (BNXT_LINK_IS_UP(bp)) 11418 netif_carrier_on(bp->dev); 11419 } 11420 11421 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11422 { 11423 u8 active_fec = link_info->active_fec_sig_mode & 11424 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11425 11426 switch (active_fec) { 11427 default: 11428 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11429 return "None"; 11430 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11431 return "Clause 74 BaseR"; 11432 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11433 return "Clause 91 RS(528,514)"; 11434 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11435 return "Clause 91 RS544_1XN"; 11436 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11437 return "Clause 91 RS(544,514)"; 11438 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11439 return "Clause 91 RS272_1XN"; 11440 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11441 return "Clause 91 RS(272,257)"; 11442 } 11443 } 11444 11445 void bnxt_report_link(struct bnxt *bp) 11446 { 11447 if (BNXT_LINK_IS_UP(bp)) { 11448 const char *signal = ""; 11449 const char *flow_ctrl; 11450 const char *duplex; 11451 u32 speed; 11452 u16 fec; 11453 11454 netif_carrier_on(bp->dev); 11455 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11456 if (speed == SPEED_UNKNOWN) { 11457 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11458 return; 11459 } 11460 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11461 duplex = "full"; 11462 else 11463 duplex = "half"; 11464 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11465 flow_ctrl = "ON - receive & transmit"; 11466 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11467 flow_ctrl = "ON - transmit"; 11468 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11469 flow_ctrl = "ON - receive"; 11470 else 11471 flow_ctrl = "none"; 11472 if (bp->link_info.phy_qcfg_resp.option_flags & 11473 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11474 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11475 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11476 switch (sig_mode) { 11477 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11478 signal = "(NRZ) "; 11479 break; 11480 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11481 signal = "(PAM4 56Gbps) "; 11482 break; 11483 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11484 signal = "(PAM4 112Gbps) "; 11485 break; 11486 default: 11487 break; 11488 } 11489 } 11490 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11491 speed, signal, duplex, flow_ctrl); 11492 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11493 netdev_info(bp->dev, "EEE is %s\n", 11494 bp->eee.eee_active ? "active" : 11495 "not active"); 11496 fec = bp->link_info.fec_cfg; 11497 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11498 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11499 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11500 bnxt_report_fec(&bp->link_info)); 11501 } else { 11502 netif_carrier_off(bp->dev); 11503 netdev_err(bp->dev, "NIC Link is Down\n"); 11504 } 11505 } 11506 11507 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11508 { 11509 if (!resp->supported_speeds_auto_mode && 11510 !resp->supported_speeds_force_mode && 11511 !resp->supported_pam4_speeds_auto_mode && 11512 !resp->supported_pam4_speeds_force_mode && 11513 !resp->supported_speeds2_auto_mode && 11514 !resp->supported_speeds2_force_mode) 11515 return true; 11516 return false; 11517 } 11518 11519 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11520 { 11521 struct bnxt_link_info *link_info = &bp->link_info; 11522 struct hwrm_port_phy_qcaps_output *resp; 11523 struct hwrm_port_phy_qcaps_input *req; 11524 int rc = 0; 11525 11526 if (bp->hwrm_spec_code < 0x10201) 11527 return 0; 11528 11529 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11530 if (rc) 11531 return rc; 11532 11533 resp = hwrm_req_hold(bp, req); 11534 rc = hwrm_req_send(bp, req); 11535 if (rc) 11536 goto hwrm_phy_qcaps_exit; 11537 11538 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11539 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11540 struct ethtool_keee *eee = &bp->eee; 11541 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11542 11543 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11544 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11545 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11546 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11547 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11548 } 11549 11550 if (bp->hwrm_spec_code >= 0x10a01) { 11551 if (bnxt_phy_qcaps_no_speed(resp)) { 11552 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11553 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11554 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11555 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11556 netdev_info(bp->dev, "Ethernet link enabled\n"); 11557 /* Phy re-enabled, reprobe the speeds */ 11558 link_info->support_auto_speeds = 0; 11559 link_info->support_pam4_auto_speeds = 0; 11560 link_info->support_auto_speeds2 = 0; 11561 } 11562 } 11563 if (resp->supported_speeds_auto_mode) 11564 link_info->support_auto_speeds = 11565 le16_to_cpu(resp->supported_speeds_auto_mode); 11566 if (resp->supported_pam4_speeds_auto_mode) 11567 link_info->support_pam4_auto_speeds = 11568 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11569 if (resp->supported_speeds2_auto_mode) 11570 link_info->support_auto_speeds2 = 11571 le16_to_cpu(resp->supported_speeds2_auto_mode); 11572 11573 bp->port_count = resp->port_cnt; 11574 11575 hwrm_phy_qcaps_exit: 11576 hwrm_req_drop(bp, req); 11577 return rc; 11578 } 11579 11580 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 11581 { 11582 struct hwrm_port_mac_qcaps_output *resp; 11583 struct hwrm_port_mac_qcaps_input *req; 11584 int rc; 11585 11586 if (bp->hwrm_spec_code < 0x10a03) 11587 return; 11588 11589 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 11590 if (rc) 11591 return; 11592 11593 resp = hwrm_req_hold(bp, req); 11594 rc = hwrm_req_send_silent(bp, req); 11595 if (!rc) 11596 bp->mac_flags = resp->flags; 11597 hwrm_req_drop(bp, req); 11598 } 11599 11600 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11601 { 11602 u16 diff = advertising ^ supported; 11603 11604 return ((supported | diff) != supported); 11605 } 11606 11607 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 11608 { 11609 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 11610 11611 /* Check if any advertised speeds are no longer supported. The caller 11612 * holds the link_lock mutex, so we can modify link_info settings. 11613 */ 11614 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11615 if (bnxt_support_dropped(link_info->advertising, 11616 link_info->support_auto_speeds2)) { 11617 link_info->advertising = link_info->support_auto_speeds2; 11618 return true; 11619 } 11620 return false; 11621 } 11622 if (bnxt_support_dropped(link_info->advertising, 11623 link_info->support_auto_speeds)) { 11624 link_info->advertising = link_info->support_auto_speeds; 11625 return true; 11626 } 11627 if (bnxt_support_dropped(link_info->advertising_pam4, 11628 link_info->support_pam4_auto_speeds)) { 11629 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 11630 return true; 11631 } 11632 return false; 11633 } 11634 11635 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 11636 { 11637 struct bnxt_link_info *link_info = &bp->link_info; 11638 struct hwrm_port_phy_qcfg_output *resp; 11639 struct hwrm_port_phy_qcfg_input *req; 11640 u8 link_state = link_info->link_state; 11641 bool support_changed; 11642 int rc; 11643 11644 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 11645 if (rc) 11646 return rc; 11647 11648 resp = hwrm_req_hold(bp, req); 11649 rc = hwrm_req_send(bp, req); 11650 if (rc) { 11651 hwrm_req_drop(bp, req); 11652 if (BNXT_VF(bp) && rc == -ENODEV) { 11653 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 11654 rc = 0; 11655 } 11656 return rc; 11657 } 11658 11659 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 11660 link_info->phy_link_status = resp->link; 11661 link_info->duplex = resp->duplex_cfg; 11662 if (bp->hwrm_spec_code >= 0x10800) 11663 link_info->duplex = resp->duplex_state; 11664 link_info->pause = resp->pause; 11665 link_info->auto_mode = resp->auto_mode; 11666 link_info->auto_pause_setting = resp->auto_pause; 11667 link_info->lp_pause = resp->link_partner_adv_pause; 11668 link_info->force_pause_setting = resp->force_pause; 11669 link_info->duplex_setting = resp->duplex_cfg; 11670 if (link_info->phy_link_status == BNXT_LINK_LINK) { 11671 link_info->link_speed = le16_to_cpu(resp->link_speed); 11672 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 11673 link_info->active_lanes = resp->active_lanes; 11674 } else { 11675 link_info->link_speed = 0; 11676 link_info->active_lanes = 0; 11677 } 11678 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 11679 link_info->force_pam4_link_speed = 11680 le16_to_cpu(resp->force_pam4_link_speed); 11681 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 11682 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 11683 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 11684 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 11685 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 11686 link_info->auto_pam4_link_speeds = 11687 le16_to_cpu(resp->auto_pam4_link_speed_mask); 11688 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 11689 link_info->lp_auto_link_speeds = 11690 le16_to_cpu(resp->link_partner_adv_speeds); 11691 link_info->lp_auto_pam4_link_speeds = 11692 resp->link_partner_pam4_adv_speeds; 11693 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 11694 link_info->phy_ver[0] = resp->phy_maj; 11695 link_info->phy_ver[1] = resp->phy_min; 11696 link_info->phy_ver[2] = resp->phy_bld; 11697 link_info->media_type = resp->media_type; 11698 link_info->phy_type = resp->phy_type; 11699 link_info->transceiver = resp->xcvr_pkg_type; 11700 link_info->phy_addr = resp->eee_config_phy_addr & 11701 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 11702 link_info->module_status = resp->module_status; 11703 11704 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 11705 struct ethtool_keee *eee = &bp->eee; 11706 u16 fw_speeds; 11707 11708 eee->eee_active = 0; 11709 if (resp->eee_config_phy_addr & 11710 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 11711 eee->eee_active = 1; 11712 fw_speeds = le16_to_cpu( 11713 resp->link_partner_adv_eee_link_speed_mask); 11714 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 11715 } 11716 11717 /* Pull initial EEE config */ 11718 if (!chng_link_state) { 11719 if (resp->eee_config_phy_addr & 11720 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 11721 eee->eee_enabled = 1; 11722 11723 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 11724 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 11725 11726 if (resp->eee_config_phy_addr & 11727 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 11728 __le32 tmr; 11729 11730 eee->tx_lpi_enabled = 1; 11731 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 11732 eee->tx_lpi_timer = le32_to_cpu(tmr) & 11733 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 11734 } 11735 } 11736 } 11737 11738 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 11739 if (bp->hwrm_spec_code >= 0x10504) { 11740 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 11741 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 11742 } 11743 /* TODO: need to add more logic to report VF link */ 11744 if (chng_link_state) { 11745 if (link_info->phy_link_status == BNXT_LINK_LINK) 11746 link_info->link_state = BNXT_LINK_STATE_UP; 11747 else 11748 link_info->link_state = BNXT_LINK_STATE_DOWN; 11749 if (link_state != link_info->link_state) 11750 bnxt_report_link(bp); 11751 } else { 11752 /* always link down if not require to update link state */ 11753 link_info->link_state = BNXT_LINK_STATE_DOWN; 11754 } 11755 hwrm_req_drop(bp, req); 11756 11757 if (!BNXT_PHY_CFG_ABLE(bp)) 11758 return 0; 11759 11760 support_changed = bnxt_support_speed_dropped(link_info); 11761 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 11762 bnxt_hwrm_set_link_setting(bp, true, false); 11763 return 0; 11764 } 11765 11766 static void bnxt_get_port_module_status(struct bnxt *bp) 11767 { 11768 struct bnxt_link_info *link_info = &bp->link_info; 11769 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 11770 u8 module_status; 11771 11772 if (bnxt_update_link(bp, true)) 11773 return; 11774 11775 module_status = link_info->module_status; 11776 switch (module_status) { 11777 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 11778 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 11779 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 11780 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 11781 bp->pf.port_id); 11782 if (bp->hwrm_spec_code >= 0x10201) { 11783 netdev_warn(bp->dev, "Module part number %s\n", 11784 resp->phy_vendor_partnumber); 11785 } 11786 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 11787 netdev_warn(bp->dev, "TX is disabled\n"); 11788 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 11789 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 11790 } 11791 } 11792 11793 static void 11794 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11795 { 11796 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 11797 if (bp->hwrm_spec_code >= 0x10201) 11798 req->auto_pause = 11799 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 11800 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11801 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 11802 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11803 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 11804 req->enables |= 11805 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11806 } else { 11807 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11808 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 11809 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11810 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 11811 req->enables |= 11812 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 11813 if (bp->hwrm_spec_code >= 0x10201) { 11814 req->auto_pause = req->force_pause; 11815 req->enables |= cpu_to_le32( 11816 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11817 } 11818 } 11819 } 11820 11821 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11822 { 11823 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 11824 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 11825 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11826 req->enables |= 11827 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 11828 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 11829 } else if (bp->link_info.advertising) { 11830 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 11831 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 11832 } 11833 if (bp->link_info.advertising_pam4) { 11834 req->enables |= 11835 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 11836 req->auto_link_pam4_speed_mask = 11837 cpu_to_le16(bp->link_info.advertising_pam4); 11838 } 11839 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 11840 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 11841 } else { 11842 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 11843 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11844 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 11845 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 11846 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 11847 (u32)bp->link_info.req_link_speed); 11848 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 11849 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11850 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 11851 } else { 11852 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11853 } 11854 } 11855 11856 /* tell chimp that the setting takes effect immediately */ 11857 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 11858 } 11859 11860 int bnxt_hwrm_set_pause(struct bnxt *bp) 11861 { 11862 struct hwrm_port_phy_cfg_input *req; 11863 int rc; 11864 11865 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11866 if (rc) 11867 return rc; 11868 11869 bnxt_hwrm_set_pause_common(bp, req); 11870 11871 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 11872 bp->link_info.force_link_chng) 11873 bnxt_hwrm_set_link_common(bp, req); 11874 11875 rc = hwrm_req_send(bp, req); 11876 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 11877 /* since changing of pause setting doesn't trigger any link 11878 * change event, the driver needs to update the current pause 11879 * result upon successfully return of the phy_cfg command 11880 */ 11881 bp->link_info.pause = 11882 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 11883 bp->link_info.auto_pause_setting = 0; 11884 if (!bp->link_info.force_link_chng) 11885 bnxt_report_link(bp); 11886 } 11887 bp->link_info.force_link_chng = false; 11888 return rc; 11889 } 11890 11891 static void bnxt_hwrm_set_eee(struct bnxt *bp, 11892 struct hwrm_port_phy_cfg_input *req) 11893 { 11894 struct ethtool_keee *eee = &bp->eee; 11895 11896 if (eee->eee_enabled) { 11897 u16 eee_speeds; 11898 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 11899 11900 if (eee->tx_lpi_enabled) 11901 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 11902 else 11903 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 11904 11905 req->flags |= cpu_to_le32(flags); 11906 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 11907 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 11908 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 11909 } else { 11910 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 11911 } 11912 } 11913 11914 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 11915 { 11916 struct hwrm_port_phy_cfg_input *req; 11917 int rc; 11918 11919 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11920 if (rc) 11921 return rc; 11922 11923 if (set_pause) 11924 bnxt_hwrm_set_pause_common(bp, req); 11925 11926 bnxt_hwrm_set_link_common(bp, req); 11927 11928 if (set_eee) 11929 bnxt_hwrm_set_eee(bp, req); 11930 return hwrm_req_send(bp, req); 11931 } 11932 11933 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 11934 { 11935 struct hwrm_port_phy_cfg_input *req; 11936 int rc; 11937 11938 if (!BNXT_SINGLE_PF(bp)) 11939 return 0; 11940 11941 if (pci_num_vf(bp->pdev) && 11942 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 11943 return 0; 11944 11945 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11946 if (rc) 11947 return rc; 11948 11949 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 11950 rc = hwrm_req_send(bp, req); 11951 if (!rc) { 11952 mutex_lock(&bp->link_lock); 11953 /* Device is not obliged link down in certain scenarios, even 11954 * when forced. Setting the state unknown is consistent with 11955 * driver startup and will force link state to be reported 11956 * during subsequent open based on PORT_PHY_QCFG. 11957 */ 11958 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 11959 mutex_unlock(&bp->link_lock); 11960 } 11961 return rc; 11962 } 11963 11964 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 11965 { 11966 #ifdef CONFIG_TEE_BNXT_FW 11967 int rc = tee_bnxt_fw_load(); 11968 11969 if (rc) 11970 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 11971 11972 return rc; 11973 #else 11974 netdev_err(bp->dev, "OP-TEE not supported\n"); 11975 return -ENODEV; 11976 #endif 11977 } 11978 11979 static int bnxt_try_recover_fw(struct bnxt *bp) 11980 { 11981 if (bp->fw_health && bp->fw_health->status_reliable) { 11982 int retry = 0, rc; 11983 u32 sts; 11984 11985 do { 11986 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 11987 rc = bnxt_hwrm_poll(bp); 11988 if (!BNXT_FW_IS_BOOTING(sts) && 11989 !BNXT_FW_IS_RECOVERING(sts)) 11990 break; 11991 retry++; 11992 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 11993 11994 if (!BNXT_FW_IS_HEALTHY(sts)) { 11995 netdev_err(bp->dev, 11996 "Firmware not responding, status: 0x%x\n", 11997 sts); 11998 rc = -ENODEV; 11999 } 12000 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12001 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12002 return bnxt_fw_reset_via_optee(bp); 12003 } 12004 return rc; 12005 } 12006 12007 return -ENODEV; 12008 } 12009 12010 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12011 { 12012 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12013 12014 if (!BNXT_NEW_RM(bp)) 12015 return; /* no resource reservations required */ 12016 12017 hw_resc->resv_cp_rings = 0; 12018 hw_resc->resv_stat_ctxs = 0; 12019 hw_resc->resv_irqs = 0; 12020 hw_resc->resv_tx_rings = 0; 12021 hw_resc->resv_rx_rings = 0; 12022 hw_resc->resv_hw_ring_grps = 0; 12023 hw_resc->resv_vnics = 0; 12024 hw_resc->resv_rsscos_ctxs = 0; 12025 if (!fw_reset) { 12026 bp->tx_nr_rings = 0; 12027 bp->rx_nr_rings = 0; 12028 } 12029 } 12030 12031 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12032 { 12033 int rc; 12034 12035 if (!BNXT_NEW_RM(bp)) 12036 return 0; /* no resource reservations required */ 12037 12038 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12039 if (rc) 12040 netdev_err(bp->dev, "resc_qcaps failed\n"); 12041 12042 bnxt_clear_reservations(bp, fw_reset); 12043 12044 return rc; 12045 } 12046 12047 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12048 { 12049 struct hwrm_func_drv_if_change_output *resp; 12050 struct hwrm_func_drv_if_change_input *req; 12051 bool fw_reset = !bp->irq_tbl; 12052 bool resc_reinit = false; 12053 int rc, retry = 0; 12054 u32 flags = 0; 12055 12056 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12057 return 0; 12058 12059 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12060 if (rc) 12061 return rc; 12062 12063 if (up) 12064 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12065 resp = hwrm_req_hold(bp, req); 12066 12067 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12068 while (retry < BNXT_FW_IF_RETRY) { 12069 rc = hwrm_req_send(bp, req); 12070 if (rc != -EAGAIN) 12071 break; 12072 12073 msleep(50); 12074 retry++; 12075 } 12076 12077 if (rc == -EAGAIN) { 12078 hwrm_req_drop(bp, req); 12079 return rc; 12080 } else if (!rc) { 12081 flags = le32_to_cpu(resp->flags); 12082 } else if (up) { 12083 rc = bnxt_try_recover_fw(bp); 12084 fw_reset = true; 12085 } 12086 hwrm_req_drop(bp, req); 12087 if (rc) 12088 return rc; 12089 12090 if (!up) { 12091 bnxt_inv_fw_health_reg(bp); 12092 return 0; 12093 } 12094 12095 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12096 resc_reinit = true; 12097 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12098 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12099 fw_reset = true; 12100 else 12101 bnxt_remap_fw_health_regs(bp); 12102 12103 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12104 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12105 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12106 return -ENODEV; 12107 } 12108 if (resc_reinit || fw_reset) { 12109 if (fw_reset) { 12110 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12111 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12112 bnxt_ulp_irq_stop(bp); 12113 bnxt_free_ctx_mem(bp, false); 12114 bnxt_dcb_free(bp); 12115 rc = bnxt_fw_init_one(bp); 12116 if (rc) { 12117 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12118 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12119 return rc; 12120 } 12121 bnxt_clear_int_mode(bp); 12122 rc = bnxt_init_int_mode(bp); 12123 if (rc) { 12124 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12125 netdev_err(bp->dev, "init int mode failed\n"); 12126 return rc; 12127 } 12128 } 12129 rc = bnxt_cancel_reservations(bp, fw_reset); 12130 } 12131 return rc; 12132 } 12133 12134 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12135 { 12136 struct hwrm_port_led_qcaps_output *resp; 12137 struct hwrm_port_led_qcaps_input *req; 12138 struct bnxt_pf_info *pf = &bp->pf; 12139 int rc; 12140 12141 bp->num_leds = 0; 12142 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12143 return 0; 12144 12145 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12146 if (rc) 12147 return rc; 12148 12149 req->port_id = cpu_to_le16(pf->port_id); 12150 resp = hwrm_req_hold(bp, req); 12151 rc = hwrm_req_send(bp, req); 12152 if (rc) { 12153 hwrm_req_drop(bp, req); 12154 return rc; 12155 } 12156 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12157 int i; 12158 12159 bp->num_leds = resp->num_leds; 12160 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12161 bp->num_leds); 12162 for (i = 0; i < bp->num_leds; i++) { 12163 struct bnxt_led_info *led = &bp->leds[i]; 12164 __le16 caps = led->led_state_caps; 12165 12166 if (!led->led_group_id || 12167 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12168 bp->num_leds = 0; 12169 break; 12170 } 12171 } 12172 } 12173 hwrm_req_drop(bp, req); 12174 return 0; 12175 } 12176 12177 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12178 { 12179 struct hwrm_wol_filter_alloc_output *resp; 12180 struct hwrm_wol_filter_alloc_input *req; 12181 int rc; 12182 12183 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12184 if (rc) 12185 return rc; 12186 12187 req->port_id = cpu_to_le16(bp->pf.port_id); 12188 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12189 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12190 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12191 12192 resp = hwrm_req_hold(bp, req); 12193 rc = hwrm_req_send(bp, req); 12194 if (!rc) 12195 bp->wol_filter_id = resp->wol_filter_id; 12196 hwrm_req_drop(bp, req); 12197 return rc; 12198 } 12199 12200 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12201 { 12202 struct hwrm_wol_filter_free_input *req; 12203 int rc; 12204 12205 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12206 if (rc) 12207 return rc; 12208 12209 req->port_id = cpu_to_le16(bp->pf.port_id); 12210 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12211 req->wol_filter_id = bp->wol_filter_id; 12212 12213 return hwrm_req_send(bp, req); 12214 } 12215 12216 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12217 { 12218 struct hwrm_wol_filter_qcfg_output *resp; 12219 struct hwrm_wol_filter_qcfg_input *req; 12220 u16 next_handle = 0; 12221 int rc; 12222 12223 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12224 if (rc) 12225 return rc; 12226 12227 req->port_id = cpu_to_le16(bp->pf.port_id); 12228 req->handle = cpu_to_le16(handle); 12229 resp = hwrm_req_hold(bp, req); 12230 rc = hwrm_req_send(bp, req); 12231 if (!rc) { 12232 next_handle = le16_to_cpu(resp->next_handle); 12233 if (next_handle != 0) { 12234 if (resp->wol_type == 12235 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12236 bp->wol = 1; 12237 bp->wol_filter_id = resp->wol_filter_id; 12238 } 12239 } 12240 } 12241 hwrm_req_drop(bp, req); 12242 return next_handle; 12243 } 12244 12245 static void bnxt_get_wol_settings(struct bnxt *bp) 12246 { 12247 u16 handle = 0; 12248 12249 bp->wol = 0; 12250 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12251 return; 12252 12253 do { 12254 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12255 } while (handle && handle != 0xffff); 12256 } 12257 12258 static bool bnxt_eee_config_ok(struct bnxt *bp) 12259 { 12260 struct ethtool_keee *eee = &bp->eee; 12261 struct bnxt_link_info *link_info = &bp->link_info; 12262 12263 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12264 return true; 12265 12266 if (eee->eee_enabled) { 12267 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12268 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12269 12270 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12271 12272 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12273 eee->eee_enabled = 0; 12274 return false; 12275 } 12276 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12277 linkmode_and(eee->advertised, advertising, 12278 eee->supported); 12279 return false; 12280 } 12281 } 12282 return true; 12283 } 12284 12285 static int bnxt_update_phy_setting(struct bnxt *bp) 12286 { 12287 int rc; 12288 bool update_link = false; 12289 bool update_pause = false; 12290 bool update_eee = false; 12291 struct bnxt_link_info *link_info = &bp->link_info; 12292 12293 rc = bnxt_update_link(bp, true); 12294 if (rc) { 12295 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12296 rc); 12297 return rc; 12298 } 12299 if (!BNXT_SINGLE_PF(bp)) 12300 return 0; 12301 12302 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12303 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12304 link_info->req_flow_ctrl) 12305 update_pause = true; 12306 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12307 link_info->force_pause_setting != link_info->req_flow_ctrl) 12308 update_pause = true; 12309 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12310 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12311 update_link = true; 12312 if (bnxt_force_speed_updated(link_info)) 12313 update_link = true; 12314 if (link_info->req_duplex != link_info->duplex_setting) 12315 update_link = true; 12316 } else { 12317 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12318 update_link = true; 12319 if (bnxt_auto_speed_updated(link_info)) 12320 update_link = true; 12321 } 12322 12323 /* The last close may have shutdown the link, so need to call 12324 * PHY_CFG to bring it back up. 12325 */ 12326 if (!BNXT_LINK_IS_UP(bp)) 12327 update_link = true; 12328 12329 if (!bnxt_eee_config_ok(bp)) 12330 update_eee = true; 12331 12332 if (update_link) 12333 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12334 else if (update_pause) 12335 rc = bnxt_hwrm_set_pause(bp); 12336 if (rc) { 12337 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12338 rc); 12339 return rc; 12340 } 12341 12342 return rc; 12343 } 12344 12345 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12346 12347 static int bnxt_reinit_after_abort(struct bnxt *bp) 12348 { 12349 int rc; 12350 12351 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12352 return -EBUSY; 12353 12354 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12355 return -ENODEV; 12356 12357 rc = bnxt_fw_init_one(bp); 12358 if (!rc) { 12359 bnxt_clear_int_mode(bp); 12360 rc = bnxt_init_int_mode(bp); 12361 if (!rc) { 12362 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12363 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12364 } 12365 } 12366 return rc; 12367 } 12368 12369 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12370 { 12371 struct bnxt_ntuple_filter *ntp_fltr; 12372 struct bnxt_l2_filter *l2_fltr; 12373 12374 if (list_empty(&fltr->list)) 12375 return; 12376 12377 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12378 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12379 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12380 atomic_inc(&l2_fltr->refcnt); 12381 ntp_fltr->l2_fltr = l2_fltr; 12382 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12383 bnxt_del_ntp_filter(bp, ntp_fltr); 12384 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12385 fltr->sw_id); 12386 } 12387 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12388 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12389 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12390 bnxt_del_l2_filter(bp, l2_fltr); 12391 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12392 fltr->sw_id); 12393 } 12394 } 12395 } 12396 12397 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12398 { 12399 struct bnxt_filter_base *usr_fltr, *tmp; 12400 12401 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12402 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12403 } 12404 12405 static int bnxt_set_xps_mapping(struct bnxt *bp) 12406 { 12407 int numa_node = dev_to_node(&bp->pdev->dev); 12408 unsigned int q_idx, map_idx, cpu, i; 12409 const struct cpumask *cpu_mask_ptr; 12410 int nr_cpus = num_online_cpus(); 12411 cpumask_t *q_map; 12412 int rc = 0; 12413 12414 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12415 if (!q_map) 12416 return -ENOMEM; 12417 12418 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12419 * Each TC has the same number of TX queues. The nth TX queue for each 12420 * TC will have the same CPU mask. 12421 */ 12422 for (i = 0; i < nr_cpus; i++) { 12423 map_idx = i % bp->tx_nr_rings_per_tc; 12424 cpu = cpumask_local_spread(i, numa_node); 12425 cpu_mask_ptr = get_cpu_mask(cpu); 12426 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12427 } 12428 12429 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12430 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12431 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12432 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12433 if (rc) { 12434 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12435 q_idx); 12436 break; 12437 } 12438 } 12439 12440 kfree(q_map); 12441 12442 return rc; 12443 } 12444 12445 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12446 { 12447 int rc = 0; 12448 12449 netif_carrier_off(bp->dev); 12450 if (irq_re_init) { 12451 /* Reserve rings now if none were reserved at driver probe. */ 12452 rc = bnxt_init_dflt_ring_mode(bp); 12453 if (rc) { 12454 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12455 return rc; 12456 } 12457 } 12458 rc = bnxt_reserve_rings(bp, irq_re_init); 12459 if (rc) 12460 return rc; 12461 12462 rc = bnxt_alloc_mem(bp, irq_re_init); 12463 if (rc) { 12464 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12465 goto open_err_free_mem; 12466 } 12467 12468 if (irq_re_init) { 12469 bnxt_init_napi(bp); 12470 rc = bnxt_request_irq(bp); 12471 if (rc) { 12472 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12473 goto open_err_irq; 12474 } 12475 } 12476 12477 rc = bnxt_init_nic(bp, irq_re_init); 12478 if (rc) { 12479 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12480 goto open_err_irq; 12481 } 12482 12483 bnxt_enable_napi(bp); 12484 bnxt_debug_dev_init(bp); 12485 12486 if (link_re_init) { 12487 mutex_lock(&bp->link_lock); 12488 rc = bnxt_update_phy_setting(bp); 12489 mutex_unlock(&bp->link_lock); 12490 if (rc) { 12491 netdev_warn(bp->dev, "failed to update phy settings\n"); 12492 if (BNXT_SINGLE_PF(bp)) { 12493 bp->link_info.phy_retry = true; 12494 bp->link_info.phy_retry_expires = 12495 jiffies + 5 * HZ; 12496 } 12497 } 12498 } 12499 12500 if (irq_re_init) { 12501 udp_tunnel_nic_reset_ntf(bp->dev); 12502 rc = bnxt_set_xps_mapping(bp); 12503 if (rc) 12504 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12505 } 12506 12507 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12508 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12509 static_branch_enable(&bnxt_xdp_locking_key); 12510 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12511 static_branch_disable(&bnxt_xdp_locking_key); 12512 } 12513 set_bit(BNXT_STATE_OPEN, &bp->state); 12514 bnxt_enable_int(bp); 12515 /* Enable TX queues */ 12516 bnxt_tx_enable(bp); 12517 mod_timer(&bp->timer, jiffies + bp->current_interval); 12518 /* Poll link status and check for SFP+ module status */ 12519 mutex_lock(&bp->link_lock); 12520 bnxt_get_port_module_status(bp); 12521 mutex_unlock(&bp->link_lock); 12522 12523 /* VF-reps may need to be re-opened after the PF is re-opened */ 12524 if (BNXT_PF(bp)) 12525 bnxt_vf_reps_open(bp); 12526 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 12527 WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS); 12528 bnxt_ptp_init_rtc(bp, true); 12529 bnxt_ptp_cfg_tstamp_filters(bp); 12530 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12531 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12532 bnxt_cfg_usr_fltrs(bp); 12533 return 0; 12534 12535 open_err_irq: 12536 bnxt_del_napi(bp); 12537 12538 open_err_free_mem: 12539 bnxt_free_skbs(bp); 12540 bnxt_free_irq(bp); 12541 bnxt_free_mem(bp, true); 12542 return rc; 12543 } 12544 12545 /* rtnl_lock held */ 12546 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12547 { 12548 int rc = 0; 12549 12550 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12551 rc = -EIO; 12552 if (!rc) 12553 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12554 if (rc) { 12555 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12556 dev_close(bp->dev); 12557 } 12558 return rc; 12559 } 12560 12561 /* rtnl_lock held, open the NIC half way by allocating all resources, but 12562 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline 12563 * self tests. 12564 */ 12565 int bnxt_half_open_nic(struct bnxt *bp) 12566 { 12567 int rc = 0; 12568 12569 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12570 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12571 rc = -ENODEV; 12572 goto half_open_err; 12573 } 12574 12575 rc = bnxt_alloc_mem(bp, true); 12576 if (rc) { 12577 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12578 goto half_open_err; 12579 } 12580 bnxt_init_napi(bp); 12581 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12582 rc = bnxt_init_nic(bp, true); 12583 if (rc) { 12584 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12585 bnxt_del_napi(bp); 12586 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12587 goto half_open_err; 12588 } 12589 return 0; 12590 12591 half_open_err: 12592 bnxt_free_skbs(bp); 12593 bnxt_free_mem(bp, true); 12594 dev_close(bp->dev); 12595 return rc; 12596 } 12597 12598 /* rtnl_lock held, this call can only be made after a previous successful 12599 * call to bnxt_half_open_nic(). 12600 */ 12601 void bnxt_half_close_nic(struct bnxt *bp) 12602 { 12603 bnxt_hwrm_resource_free(bp, false, true); 12604 bnxt_del_napi(bp); 12605 bnxt_free_skbs(bp); 12606 bnxt_free_mem(bp, true); 12607 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12608 } 12609 12610 void bnxt_reenable_sriov(struct bnxt *bp) 12611 { 12612 if (BNXT_PF(bp)) { 12613 struct bnxt_pf_info *pf = &bp->pf; 12614 int n = pf->active_vfs; 12615 12616 if (n) 12617 bnxt_cfg_hw_sriov(bp, &n, true); 12618 } 12619 } 12620 12621 static int bnxt_open(struct net_device *dev) 12622 { 12623 struct bnxt *bp = netdev_priv(dev); 12624 int rc; 12625 12626 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12627 rc = bnxt_reinit_after_abort(bp); 12628 if (rc) { 12629 if (rc == -EBUSY) 12630 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 12631 else 12632 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 12633 return -ENODEV; 12634 } 12635 } 12636 12637 rc = bnxt_hwrm_if_change(bp, true); 12638 if (rc) 12639 return rc; 12640 12641 rc = __bnxt_open_nic(bp, true, true); 12642 if (rc) { 12643 bnxt_hwrm_if_change(bp, false); 12644 } else { 12645 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 12646 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12647 bnxt_queue_sp_work(bp, 12648 BNXT_RESTART_ULP_SP_EVENT); 12649 } 12650 } 12651 12652 return rc; 12653 } 12654 12655 static bool bnxt_drv_busy(struct bnxt *bp) 12656 { 12657 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 12658 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 12659 } 12660 12661 static void bnxt_get_ring_stats(struct bnxt *bp, 12662 struct rtnl_link_stats64 *stats); 12663 12664 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 12665 bool link_re_init) 12666 { 12667 /* Close the VF-reps before closing PF */ 12668 if (BNXT_PF(bp)) 12669 bnxt_vf_reps_close(bp); 12670 12671 /* Change device state to avoid TX queue wake up's */ 12672 bnxt_tx_disable(bp); 12673 12674 clear_bit(BNXT_STATE_OPEN, &bp->state); 12675 smp_mb__after_atomic(); 12676 while (bnxt_drv_busy(bp)) 12677 msleep(20); 12678 12679 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12680 bnxt_clear_rss_ctxs(bp); 12681 /* Flush rings and disable interrupts */ 12682 bnxt_shutdown_nic(bp, irq_re_init); 12683 12684 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 12685 12686 bnxt_debug_dev_exit(bp); 12687 bnxt_disable_napi(bp); 12688 del_timer_sync(&bp->timer); 12689 bnxt_free_skbs(bp); 12690 12691 /* Save ring stats before shutdown */ 12692 if (bp->bnapi && irq_re_init) { 12693 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 12694 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 12695 } 12696 if (irq_re_init) { 12697 bnxt_free_irq(bp); 12698 bnxt_del_napi(bp); 12699 } 12700 bnxt_free_mem(bp, irq_re_init); 12701 } 12702 12703 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12704 { 12705 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 12706 /* If we get here, it means firmware reset is in progress 12707 * while we are trying to close. We can safely proceed with 12708 * the close because we are holding rtnl_lock(). Some firmware 12709 * messages may fail as we proceed to close. We set the 12710 * ABORT_ERR flag here so that the FW reset thread will later 12711 * abort when it gets the rtnl_lock() and sees the flag. 12712 */ 12713 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 12714 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12715 } 12716 12717 #ifdef CONFIG_BNXT_SRIOV 12718 if (bp->sriov_cfg) { 12719 int rc; 12720 12721 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 12722 !bp->sriov_cfg, 12723 BNXT_SRIOV_CFG_WAIT_TMO); 12724 if (!rc) 12725 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 12726 else if (rc < 0) 12727 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 12728 } 12729 #endif 12730 __bnxt_close_nic(bp, irq_re_init, link_re_init); 12731 } 12732 12733 static int bnxt_close(struct net_device *dev) 12734 { 12735 struct bnxt *bp = netdev_priv(dev); 12736 12737 bnxt_close_nic(bp, true, true); 12738 bnxt_hwrm_shutdown_link(bp); 12739 bnxt_hwrm_if_change(bp, false); 12740 return 0; 12741 } 12742 12743 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 12744 u16 *val) 12745 { 12746 struct hwrm_port_phy_mdio_read_output *resp; 12747 struct hwrm_port_phy_mdio_read_input *req; 12748 int rc; 12749 12750 if (bp->hwrm_spec_code < 0x10a00) 12751 return -EOPNOTSUPP; 12752 12753 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 12754 if (rc) 12755 return rc; 12756 12757 req->port_id = cpu_to_le16(bp->pf.port_id); 12758 req->phy_addr = phy_addr; 12759 req->reg_addr = cpu_to_le16(reg & 0x1f); 12760 if (mdio_phy_id_is_c45(phy_addr)) { 12761 req->cl45_mdio = 1; 12762 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12763 req->dev_addr = mdio_phy_id_devad(phy_addr); 12764 req->reg_addr = cpu_to_le16(reg); 12765 } 12766 12767 resp = hwrm_req_hold(bp, req); 12768 rc = hwrm_req_send(bp, req); 12769 if (!rc) 12770 *val = le16_to_cpu(resp->reg_data); 12771 hwrm_req_drop(bp, req); 12772 return rc; 12773 } 12774 12775 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 12776 u16 val) 12777 { 12778 struct hwrm_port_phy_mdio_write_input *req; 12779 int rc; 12780 12781 if (bp->hwrm_spec_code < 0x10a00) 12782 return -EOPNOTSUPP; 12783 12784 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 12785 if (rc) 12786 return rc; 12787 12788 req->port_id = cpu_to_le16(bp->pf.port_id); 12789 req->phy_addr = phy_addr; 12790 req->reg_addr = cpu_to_le16(reg & 0x1f); 12791 if (mdio_phy_id_is_c45(phy_addr)) { 12792 req->cl45_mdio = 1; 12793 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12794 req->dev_addr = mdio_phy_id_devad(phy_addr); 12795 req->reg_addr = cpu_to_le16(reg); 12796 } 12797 req->reg_data = cpu_to_le16(val); 12798 12799 return hwrm_req_send(bp, req); 12800 } 12801 12802 /* rtnl_lock held */ 12803 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 12804 { 12805 struct mii_ioctl_data *mdio = if_mii(ifr); 12806 struct bnxt *bp = netdev_priv(dev); 12807 int rc; 12808 12809 switch (cmd) { 12810 case SIOCGMIIPHY: 12811 mdio->phy_id = bp->link_info.phy_addr; 12812 12813 fallthrough; 12814 case SIOCGMIIREG: { 12815 u16 mii_regval = 0; 12816 12817 if (!netif_running(dev)) 12818 return -EAGAIN; 12819 12820 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 12821 &mii_regval); 12822 mdio->val_out = mii_regval; 12823 return rc; 12824 } 12825 12826 case SIOCSMIIREG: 12827 if (!netif_running(dev)) 12828 return -EAGAIN; 12829 12830 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 12831 mdio->val_in); 12832 12833 case SIOCSHWTSTAMP: 12834 return bnxt_hwtstamp_set(dev, ifr); 12835 12836 case SIOCGHWTSTAMP: 12837 return bnxt_hwtstamp_get(dev, ifr); 12838 12839 default: 12840 /* do nothing */ 12841 break; 12842 } 12843 return -EOPNOTSUPP; 12844 } 12845 12846 static void bnxt_get_ring_stats(struct bnxt *bp, 12847 struct rtnl_link_stats64 *stats) 12848 { 12849 int i; 12850 12851 for (i = 0; i < bp->cp_nr_rings; i++) { 12852 struct bnxt_napi *bnapi = bp->bnapi[i]; 12853 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 12854 u64 *sw = cpr->stats.sw_stats; 12855 12856 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 12857 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12858 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 12859 12860 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 12861 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 12862 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 12863 12864 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 12865 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 12866 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 12867 12868 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 12869 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 12870 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 12871 12872 stats->rx_missed_errors += 12873 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 12874 12875 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12876 12877 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 12878 12879 stats->rx_dropped += 12880 cpr->sw_stats->rx.rx_netpoll_discards + 12881 cpr->sw_stats->rx.rx_oom_discards; 12882 } 12883 } 12884 12885 static void bnxt_add_prev_stats(struct bnxt *bp, 12886 struct rtnl_link_stats64 *stats) 12887 { 12888 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 12889 12890 stats->rx_packets += prev_stats->rx_packets; 12891 stats->tx_packets += prev_stats->tx_packets; 12892 stats->rx_bytes += prev_stats->rx_bytes; 12893 stats->tx_bytes += prev_stats->tx_bytes; 12894 stats->rx_missed_errors += prev_stats->rx_missed_errors; 12895 stats->multicast += prev_stats->multicast; 12896 stats->rx_dropped += prev_stats->rx_dropped; 12897 stats->tx_dropped += prev_stats->tx_dropped; 12898 } 12899 12900 static void 12901 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 12902 { 12903 struct bnxt *bp = netdev_priv(dev); 12904 12905 set_bit(BNXT_STATE_READ_STATS, &bp->state); 12906 /* Make sure bnxt_close_nic() sees that we are reading stats before 12907 * we check the BNXT_STATE_OPEN flag. 12908 */ 12909 smp_mb__after_atomic(); 12910 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 12911 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12912 *stats = bp->net_stats_prev; 12913 return; 12914 } 12915 12916 bnxt_get_ring_stats(bp, stats); 12917 bnxt_add_prev_stats(bp, stats); 12918 12919 if (bp->flags & BNXT_FLAG_PORT_STATS) { 12920 u64 *rx = bp->port_stats.sw_stats; 12921 u64 *tx = bp->port_stats.sw_stats + 12922 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 12923 12924 stats->rx_crc_errors = 12925 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 12926 stats->rx_frame_errors = 12927 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 12928 stats->rx_length_errors = 12929 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 12930 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 12931 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 12932 stats->rx_errors = 12933 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 12934 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 12935 stats->collisions = 12936 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 12937 stats->tx_fifo_errors = 12938 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 12939 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 12940 } 12941 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12942 } 12943 12944 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 12945 struct bnxt_total_ring_err_stats *stats, 12946 struct bnxt_cp_ring_info *cpr) 12947 { 12948 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 12949 u64 *hw_stats = cpr->stats.sw_stats; 12950 12951 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 12952 stats->rx_total_resets += sw_stats->rx.rx_resets; 12953 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 12954 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 12955 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 12956 stats->rx_total_ring_discards += 12957 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 12958 stats->tx_total_resets += sw_stats->tx.tx_resets; 12959 stats->tx_total_ring_discards += 12960 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 12961 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 12962 } 12963 12964 void bnxt_get_ring_err_stats(struct bnxt *bp, 12965 struct bnxt_total_ring_err_stats *stats) 12966 { 12967 int i; 12968 12969 for (i = 0; i < bp->cp_nr_rings; i++) 12970 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 12971 } 12972 12973 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 12974 { 12975 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12976 struct net_device *dev = bp->dev; 12977 struct netdev_hw_addr *ha; 12978 u8 *haddr; 12979 int mc_count = 0; 12980 bool update = false; 12981 int off = 0; 12982 12983 netdev_for_each_mc_addr(ha, dev) { 12984 if (mc_count >= BNXT_MAX_MC_ADDRS) { 12985 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12986 vnic->mc_list_count = 0; 12987 return false; 12988 } 12989 haddr = ha->addr; 12990 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 12991 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 12992 update = true; 12993 } 12994 off += ETH_ALEN; 12995 mc_count++; 12996 } 12997 if (mc_count) 12998 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 12999 13000 if (mc_count != vnic->mc_list_count) { 13001 vnic->mc_list_count = mc_count; 13002 update = true; 13003 } 13004 return update; 13005 } 13006 13007 static bool bnxt_uc_list_updated(struct bnxt *bp) 13008 { 13009 struct net_device *dev = bp->dev; 13010 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13011 struct netdev_hw_addr *ha; 13012 int off = 0; 13013 13014 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13015 return true; 13016 13017 netdev_for_each_uc_addr(ha, dev) { 13018 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13019 return true; 13020 13021 off += ETH_ALEN; 13022 } 13023 return false; 13024 } 13025 13026 static void bnxt_set_rx_mode(struct net_device *dev) 13027 { 13028 struct bnxt *bp = netdev_priv(dev); 13029 struct bnxt_vnic_info *vnic; 13030 bool mc_update = false; 13031 bool uc_update; 13032 u32 mask; 13033 13034 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13035 return; 13036 13037 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13038 mask = vnic->rx_mask; 13039 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13040 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13041 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13042 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13043 13044 if (dev->flags & IFF_PROMISC) 13045 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13046 13047 uc_update = bnxt_uc_list_updated(bp); 13048 13049 if (dev->flags & IFF_BROADCAST) 13050 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13051 if (dev->flags & IFF_ALLMULTI) { 13052 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13053 vnic->mc_list_count = 0; 13054 } else if (dev->flags & IFF_MULTICAST) { 13055 mc_update = bnxt_mc_list_updated(bp, &mask); 13056 } 13057 13058 if (mask != vnic->rx_mask || uc_update || mc_update) { 13059 vnic->rx_mask = mask; 13060 13061 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13062 } 13063 } 13064 13065 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13066 { 13067 struct net_device *dev = bp->dev; 13068 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13069 struct netdev_hw_addr *ha; 13070 int i, off = 0, rc; 13071 bool uc_update; 13072 13073 netif_addr_lock_bh(dev); 13074 uc_update = bnxt_uc_list_updated(bp); 13075 netif_addr_unlock_bh(dev); 13076 13077 if (!uc_update) 13078 goto skip_uc; 13079 13080 for (i = 1; i < vnic->uc_filter_count; i++) { 13081 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13082 13083 bnxt_hwrm_l2_filter_free(bp, fltr); 13084 bnxt_del_l2_filter(bp, fltr); 13085 } 13086 13087 vnic->uc_filter_count = 1; 13088 13089 netif_addr_lock_bh(dev); 13090 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13091 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13092 } else { 13093 netdev_for_each_uc_addr(ha, dev) { 13094 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13095 off += ETH_ALEN; 13096 vnic->uc_filter_count++; 13097 } 13098 } 13099 netif_addr_unlock_bh(dev); 13100 13101 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13102 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13103 if (rc) { 13104 if (BNXT_VF(bp) && rc == -ENODEV) { 13105 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13106 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13107 else 13108 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13109 rc = 0; 13110 } else { 13111 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13112 } 13113 vnic->uc_filter_count = i; 13114 return rc; 13115 } 13116 } 13117 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13118 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13119 13120 skip_uc: 13121 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13122 !bnxt_promisc_ok(bp)) 13123 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13124 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13125 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13126 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13127 rc); 13128 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13129 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13130 vnic->mc_list_count = 0; 13131 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13132 } 13133 if (rc) 13134 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13135 rc); 13136 13137 return rc; 13138 } 13139 13140 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13141 { 13142 #ifdef CONFIG_BNXT_SRIOV 13143 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13144 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13145 13146 /* No minimum rings were provisioned by the PF. Don't 13147 * reserve rings by default when device is down. 13148 */ 13149 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13150 return true; 13151 13152 if (!netif_running(bp->dev)) 13153 return false; 13154 } 13155 #endif 13156 return true; 13157 } 13158 13159 /* If the chip and firmware supports RFS */ 13160 static bool bnxt_rfs_supported(struct bnxt *bp) 13161 { 13162 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13163 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13164 return true; 13165 return false; 13166 } 13167 /* 212 firmware is broken for aRFS */ 13168 if (BNXT_FW_MAJ(bp) == 212) 13169 return false; 13170 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13171 return true; 13172 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13173 return true; 13174 return false; 13175 } 13176 13177 /* If runtime conditions support RFS */ 13178 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13179 { 13180 struct bnxt_hw_rings hwr = {0}; 13181 int max_vnics, max_rss_ctxs; 13182 13183 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13184 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13185 return bnxt_rfs_supported(bp); 13186 13187 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13188 return false; 13189 13190 hwr.grp = bp->rx_nr_rings; 13191 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13192 if (new_rss_ctx) 13193 hwr.vnic++; 13194 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13195 max_vnics = bnxt_get_max_func_vnics(bp); 13196 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13197 13198 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13199 if (bp->rx_nr_rings > 1) 13200 netdev_warn(bp->dev, 13201 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13202 min(max_rss_ctxs - 1, max_vnics - 1)); 13203 return false; 13204 } 13205 13206 if (!BNXT_NEW_RM(bp)) 13207 return true; 13208 13209 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13210 * issue that will mess up the default VNIC if we reduce the 13211 * reservations. 13212 */ 13213 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13214 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13215 return true; 13216 13217 bnxt_hwrm_reserve_rings(bp, &hwr); 13218 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13219 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13220 return true; 13221 13222 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13223 hwr.vnic = 1; 13224 hwr.rss_ctx = 0; 13225 bnxt_hwrm_reserve_rings(bp, &hwr); 13226 return false; 13227 } 13228 13229 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13230 netdev_features_t features) 13231 { 13232 struct bnxt *bp = netdev_priv(dev); 13233 netdev_features_t vlan_features; 13234 13235 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13236 features &= ~NETIF_F_NTUPLE; 13237 13238 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13239 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13240 13241 if (!(features & NETIF_F_GRO)) 13242 features &= ~NETIF_F_GRO_HW; 13243 13244 if (features & NETIF_F_GRO_HW) 13245 features &= ~NETIF_F_LRO; 13246 13247 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13248 * turned on or off together. 13249 */ 13250 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13251 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13252 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13253 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13254 else if (vlan_features) 13255 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13256 } 13257 #ifdef CONFIG_BNXT_SRIOV 13258 if (BNXT_VF(bp) && bp->vf.vlan) 13259 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13260 #endif 13261 return features; 13262 } 13263 13264 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13265 bool link_re_init, u32 flags, bool update_tpa) 13266 { 13267 bnxt_close_nic(bp, irq_re_init, link_re_init); 13268 bp->flags = flags; 13269 if (update_tpa) 13270 bnxt_set_ring_params(bp); 13271 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13272 } 13273 13274 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13275 { 13276 bool update_tpa = false, update_ntuple = false; 13277 struct bnxt *bp = netdev_priv(dev); 13278 u32 flags = bp->flags; 13279 u32 changes; 13280 int rc = 0; 13281 bool re_init = false; 13282 13283 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13284 if (features & NETIF_F_GRO_HW) 13285 flags |= BNXT_FLAG_GRO; 13286 else if (features & NETIF_F_LRO) 13287 flags |= BNXT_FLAG_LRO; 13288 13289 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13290 flags &= ~BNXT_FLAG_TPA; 13291 13292 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13293 flags |= BNXT_FLAG_STRIP_VLAN; 13294 13295 if (features & NETIF_F_NTUPLE) 13296 flags |= BNXT_FLAG_RFS; 13297 else 13298 bnxt_clear_usr_fltrs(bp, true); 13299 13300 changes = flags ^ bp->flags; 13301 if (changes & BNXT_FLAG_TPA) { 13302 update_tpa = true; 13303 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13304 (flags & BNXT_FLAG_TPA) == 0 || 13305 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13306 re_init = true; 13307 } 13308 13309 if (changes & ~BNXT_FLAG_TPA) 13310 re_init = true; 13311 13312 if (changes & BNXT_FLAG_RFS) 13313 update_ntuple = true; 13314 13315 if (flags != bp->flags) { 13316 u32 old_flags = bp->flags; 13317 13318 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13319 bp->flags = flags; 13320 if (update_tpa) 13321 bnxt_set_ring_params(bp); 13322 return rc; 13323 } 13324 13325 if (update_ntuple) 13326 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13327 13328 if (re_init) 13329 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13330 13331 if (update_tpa) { 13332 bp->flags = flags; 13333 rc = bnxt_set_tpa(bp, 13334 (flags & BNXT_FLAG_TPA) ? 13335 true : false); 13336 if (rc) 13337 bp->flags = old_flags; 13338 } 13339 } 13340 return rc; 13341 } 13342 13343 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13344 u8 **nextp) 13345 { 13346 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13347 struct hop_jumbo_hdr *jhdr; 13348 int hdr_count = 0; 13349 u8 *nexthdr; 13350 int start; 13351 13352 /* Check that there are at most 2 IPv6 extension headers, no 13353 * fragment header, and each is <= 64 bytes. 13354 */ 13355 start = nw_off + sizeof(*ip6h); 13356 nexthdr = &ip6h->nexthdr; 13357 while (ipv6_ext_hdr(*nexthdr)) { 13358 struct ipv6_opt_hdr *hp; 13359 int hdrlen; 13360 13361 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13362 *nexthdr == NEXTHDR_FRAGMENT) 13363 return false; 13364 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13365 skb_headlen(skb), NULL); 13366 if (!hp) 13367 return false; 13368 if (*nexthdr == NEXTHDR_AUTH) 13369 hdrlen = ipv6_authlen(hp); 13370 else 13371 hdrlen = ipv6_optlen(hp); 13372 13373 if (hdrlen > 64) 13374 return false; 13375 13376 /* The ext header may be a hop-by-hop header inserted for 13377 * big TCP purposes. This will be removed before sending 13378 * from NIC, so do not count it. 13379 */ 13380 if (*nexthdr == NEXTHDR_HOP) { 13381 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13382 goto increment_hdr; 13383 13384 jhdr = (struct hop_jumbo_hdr *)hp; 13385 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13386 jhdr->nexthdr != IPPROTO_TCP) 13387 goto increment_hdr; 13388 13389 goto next_hdr; 13390 } 13391 increment_hdr: 13392 hdr_count++; 13393 next_hdr: 13394 nexthdr = &hp->nexthdr; 13395 start += hdrlen; 13396 } 13397 if (nextp) { 13398 /* Caller will check inner protocol */ 13399 if (skb->encapsulation) { 13400 *nextp = nexthdr; 13401 return true; 13402 } 13403 *nextp = NULL; 13404 } 13405 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13406 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13407 } 13408 13409 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13410 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13411 { 13412 struct udphdr *uh = udp_hdr(skb); 13413 __be16 udp_port = uh->dest; 13414 13415 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13416 udp_port != bp->vxlan_gpe_port) 13417 return false; 13418 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13419 struct ethhdr *eh = inner_eth_hdr(skb); 13420 13421 switch (eh->h_proto) { 13422 case htons(ETH_P_IP): 13423 return true; 13424 case htons(ETH_P_IPV6): 13425 return bnxt_exthdr_check(bp, skb, 13426 skb_inner_network_offset(skb), 13427 NULL); 13428 } 13429 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13430 return true; 13431 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13432 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13433 NULL); 13434 } 13435 return false; 13436 } 13437 13438 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13439 { 13440 switch (l4_proto) { 13441 case IPPROTO_UDP: 13442 return bnxt_udp_tunl_check(bp, skb); 13443 case IPPROTO_IPIP: 13444 return true; 13445 case IPPROTO_GRE: { 13446 switch (skb->inner_protocol) { 13447 default: 13448 return false; 13449 case htons(ETH_P_IP): 13450 return true; 13451 case htons(ETH_P_IPV6): 13452 fallthrough; 13453 } 13454 } 13455 case IPPROTO_IPV6: 13456 /* Check ext headers of inner ipv6 */ 13457 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13458 NULL); 13459 } 13460 return false; 13461 } 13462 13463 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13464 struct net_device *dev, 13465 netdev_features_t features) 13466 { 13467 struct bnxt *bp = netdev_priv(dev); 13468 u8 *l4_proto; 13469 13470 features = vlan_features_check(skb, features); 13471 switch (vlan_get_protocol(skb)) { 13472 case htons(ETH_P_IP): 13473 if (!skb->encapsulation) 13474 return features; 13475 l4_proto = &ip_hdr(skb)->protocol; 13476 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13477 return features; 13478 break; 13479 case htons(ETH_P_IPV6): 13480 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13481 &l4_proto)) 13482 break; 13483 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13484 return features; 13485 break; 13486 } 13487 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13488 } 13489 13490 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13491 u32 *reg_buf) 13492 { 13493 struct hwrm_dbg_read_direct_output *resp; 13494 struct hwrm_dbg_read_direct_input *req; 13495 __le32 *dbg_reg_buf; 13496 dma_addr_t mapping; 13497 int rc, i; 13498 13499 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13500 if (rc) 13501 return rc; 13502 13503 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13504 &mapping); 13505 if (!dbg_reg_buf) { 13506 rc = -ENOMEM; 13507 goto dbg_rd_reg_exit; 13508 } 13509 13510 req->host_dest_addr = cpu_to_le64(mapping); 13511 13512 resp = hwrm_req_hold(bp, req); 13513 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13514 req->read_len32 = cpu_to_le32(num_words); 13515 13516 rc = hwrm_req_send(bp, req); 13517 if (rc || resp->error_code) { 13518 rc = -EIO; 13519 goto dbg_rd_reg_exit; 13520 } 13521 for (i = 0; i < num_words; i++) 13522 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13523 13524 dbg_rd_reg_exit: 13525 hwrm_req_drop(bp, req); 13526 return rc; 13527 } 13528 13529 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13530 u32 ring_id, u32 *prod, u32 *cons) 13531 { 13532 struct hwrm_dbg_ring_info_get_output *resp; 13533 struct hwrm_dbg_ring_info_get_input *req; 13534 int rc; 13535 13536 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13537 if (rc) 13538 return rc; 13539 13540 req->ring_type = ring_type; 13541 req->fw_ring_id = cpu_to_le32(ring_id); 13542 resp = hwrm_req_hold(bp, req); 13543 rc = hwrm_req_send(bp, req); 13544 if (!rc) { 13545 *prod = le32_to_cpu(resp->producer_index); 13546 *cons = le32_to_cpu(resp->consumer_index); 13547 } 13548 hwrm_req_drop(bp, req); 13549 return rc; 13550 } 13551 13552 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13553 { 13554 struct bnxt_tx_ring_info *txr; 13555 int i = bnapi->index, j; 13556 13557 bnxt_for_each_napi_tx(j, bnapi, txr) 13558 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13559 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13560 txr->tx_cons); 13561 } 13562 13563 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13564 { 13565 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13566 int i = bnapi->index; 13567 13568 if (!rxr) 13569 return; 13570 13571 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", 13572 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13573 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13574 rxr->rx_sw_agg_prod); 13575 } 13576 13577 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13578 { 13579 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13580 int i = bnapi->index; 13581 13582 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13583 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13584 } 13585 13586 static void bnxt_dbg_dump_states(struct bnxt *bp) 13587 { 13588 int i; 13589 struct bnxt_napi *bnapi; 13590 13591 for (i = 0; i < bp->cp_nr_rings; i++) { 13592 bnapi = bp->bnapi[i]; 13593 if (netif_msg_drv(bp)) { 13594 bnxt_dump_tx_sw_state(bnapi); 13595 bnxt_dump_rx_sw_state(bnapi); 13596 bnxt_dump_cp_sw_state(bnapi); 13597 } 13598 } 13599 } 13600 13601 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13602 { 13603 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 13604 struct hwrm_ring_reset_input *req; 13605 struct bnxt_napi *bnapi = rxr->bnapi; 13606 struct bnxt_cp_ring_info *cpr; 13607 u16 cp_ring_id; 13608 int rc; 13609 13610 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 13611 if (rc) 13612 return rc; 13613 13614 cpr = &bnapi->cp_ring; 13615 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 13616 req->cmpl_ring = cpu_to_le16(cp_ring_id); 13617 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 13618 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 13619 return hwrm_req_send_silent(bp, req); 13620 } 13621 13622 static void bnxt_reset_task(struct bnxt *bp, bool silent) 13623 { 13624 if (!silent) 13625 bnxt_dbg_dump_states(bp); 13626 if (netif_running(bp->dev)) { 13627 bnxt_close_nic(bp, !silent, false); 13628 bnxt_open_nic(bp, !silent, false); 13629 } 13630 } 13631 13632 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 13633 { 13634 struct bnxt *bp = netdev_priv(dev); 13635 13636 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 13637 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 13638 } 13639 13640 static void bnxt_fw_health_check(struct bnxt *bp) 13641 { 13642 struct bnxt_fw_health *fw_health = bp->fw_health; 13643 struct pci_dev *pdev = bp->pdev; 13644 u32 val; 13645 13646 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13647 return; 13648 13649 /* Make sure it is enabled before checking the tmr_counter. */ 13650 smp_rmb(); 13651 if (fw_health->tmr_counter) { 13652 fw_health->tmr_counter--; 13653 return; 13654 } 13655 13656 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13657 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 13658 fw_health->arrests++; 13659 goto fw_reset; 13660 } 13661 13662 fw_health->last_fw_heartbeat = val; 13663 13664 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13665 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 13666 fw_health->discoveries++; 13667 goto fw_reset; 13668 } 13669 13670 fw_health->tmr_counter = fw_health->tmr_multiplier; 13671 return; 13672 13673 fw_reset: 13674 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 13675 } 13676 13677 static void bnxt_timer(struct timer_list *t) 13678 { 13679 struct bnxt *bp = from_timer(bp, t, timer); 13680 struct net_device *dev = bp->dev; 13681 13682 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 13683 return; 13684 13685 if (atomic_read(&bp->intr_sem) != 0) 13686 goto bnxt_restart_timer; 13687 13688 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 13689 bnxt_fw_health_check(bp); 13690 13691 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 13692 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 13693 13694 if (bnxt_tc_flower_enabled(bp)) 13695 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 13696 13697 #ifdef CONFIG_RFS_ACCEL 13698 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 13699 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 13700 #endif /*CONFIG_RFS_ACCEL*/ 13701 13702 if (bp->link_info.phy_retry) { 13703 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 13704 bp->link_info.phy_retry = false; 13705 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 13706 } else { 13707 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 13708 } 13709 } 13710 13711 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13712 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13713 13714 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 13715 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 13716 13717 bnxt_restart_timer: 13718 mod_timer(&bp->timer, jiffies + bp->current_interval); 13719 } 13720 13721 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 13722 { 13723 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 13724 * set. If the device is being closed, bnxt_close() may be holding 13725 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we 13726 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl(). 13727 */ 13728 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13729 rtnl_lock(); 13730 } 13731 13732 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 13733 { 13734 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13735 rtnl_unlock(); 13736 } 13737 13738 /* Only called from bnxt_sp_task() */ 13739 static void bnxt_reset(struct bnxt *bp, bool silent) 13740 { 13741 bnxt_rtnl_lock_sp(bp); 13742 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 13743 bnxt_reset_task(bp, silent); 13744 bnxt_rtnl_unlock_sp(bp); 13745 } 13746 13747 /* Only called from bnxt_sp_task() */ 13748 static void bnxt_rx_ring_reset(struct bnxt *bp) 13749 { 13750 int i; 13751 13752 bnxt_rtnl_lock_sp(bp); 13753 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13754 bnxt_rtnl_unlock_sp(bp); 13755 return; 13756 } 13757 /* Disable and flush TPA before resetting the RX ring */ 13758 if (bp->flags & BNXT_FLAG_TPA) 13759 bnxt_set_tpa(bp, false); 13760 for (i = 0; i < bp->rx_nr_rings; i++) { 13761 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 13762 struct bnxt_cp_ring_info *cpr; 13763 int rc; 13764 13765 if (!rxr->bnapi->in_reset) 13766 continue; 13767 13768 rc = bnxt_hwrm_rx_ring_reset(bp, i); 13769 if (rc) { 13770 if (rc == -EINVAL || rc == -EOPNOTSUPP) 13771 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 13772 else 13773 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 13774 rc); 13775 bnxt_reset_task(bp, true); 13776 break; 13777 } 13778 bnxt_free_one_rx_ring_skbs(bp, rxr); 13779 rxr->rx_prod = 0; 13780 rxr->rx_agg_prod = 0; 13781 rxr->rx_sw_agg_prod = 0; 13782 rxr->rx_next_cons = 0; 13783 rxr->bnapi->in_reset = false; 13784 bnxt_alloc_one_rx_ring(bp, i); 13785 cpr = &rxr->bnapi->cp_ring; 13786 cpr->sw_stats->rx.rx_resets++; 13787 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13788 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 13789 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 13790 } 13791 if (bp->flags & BNXT_FLAG_TPA) 13792 bnxt_set_tpa(bp, true); 13793 bnxt_rtnl_unlock_sp(bp); 13794 } 13795 13796 static void bnxt_fw_fatal_close(struct bnxt *bp) 13797 { 13798 bnxt_tx_disable(bp); 13799 bnxt_disable_napi(bp); 13800 bnxt_disable_int_sync(bp); 13801 bnxt_free_irq(bp); 13802 bnxt_clear_int_mode(bp); 13803 pci_disable_device(bp->pdev); 13804 } 13805 13806 static void bnxt_fw_reset_close(struct bnxt *bp) 13807 { 13808 /* When firmware is in fatal state, quiesce device and disable 13809 * bus master to prevent any potential bad DMAs before freeing 13810 * kernel memory. 13811 */ 13812 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 13813 u16 val = 0; 13814 13815 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 13816 if (val == 0xffff) 13817 bp->fw_reset_min_dsecs = 0; 13818 bnxt_fw_fatal_close(bp); 13819 } 13820 __bnxt_close_nic(bp, true, false); 13821 bnxt_vf_reps_free(bp); 13822 bnxt_clear_int_mode(bp); 13823 bnxt_hwrm_func_drv_unrgtr(bp); 13824 if (pci_is_enabled(bp->pdev)) 13825 pci_disable_device(bp->pdev); 13826 bnxt_free_ctx_mem(bp, false); 13827 } 13828 13829 static bool is_bnxt_fw_ok(struct bnxt *bp) 13830 { 13831 struct bnxt_fw_health *fw_health = bp->fw_health; 13832 bool no_heartbeat = false, has_reset = false; 13833 u32 val; 13834 13835 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13836 if (val == fw_health->last_fw_heartbeat) 13837 no_heartbeat = true; 13838 13839 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13840 if (val != fw_health->last_fw_reset_cnt) 13841 has_reset = true; 13842 13843 if (!no_heartbeat && has_reset) 13844 return true; 13845 13846 return false; 13847 } 13848 13849 /* rtnl_lock is acquired before calling this function */ 13850 static void bnxt_force_fw_reset(struct bnxt *bp) 13851 { 13852 struct bnxt_fw_health *fw_health = bp->fw_health; 13853 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13854 u32 wait_dsecs; 13855 13856 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 13857 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13858 return; 13859 13860 /* we have to serialize with bnxt_refclk_read()*/ 13861 if (ptp) { 13862 unsigned long flags; 13863 13864 write_seqlock_irqsave(&ptp->ptp_lock, flags); 13865 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13866 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 13867 } else { 13868 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13869 } 13870 bnxt_fw_reset_close(bp); 13871 wait_dsecs = fw_health->master_func_wait_dsecs; 13872 if (fw_health->primary) { 13873 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 13874 wait_dsecs = 0; 13875 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 13876 } else { 13877 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 13878 wait_dsecs = fw_health->normal_func_wait_dsecs; 13879 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13880 } 13881 13882 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 13883 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 13884 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 13885 } 13886 13887 void bnxt_fw_exception(struct bnxt *bp) 13888 { 13889 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 13890 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 13891 bnxt_ulp_stop(bp); 13892 bnxt_rtnl_lock_sp(bp); 13893 bnxt_force_fw_reset(bp); 13894 bnxt_rtnl_unlock_sp(bp); 13895 } 13896 13897 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 13898 * < 0 on error. 13899 */ 13900 static int bnxt_get_registered_vfs(struct bnxt *bp) 13901 { 13902 #ifdef CONFIG_BNXT_SRIOV 13903 int rc; 13904 13905 if (!BNXT_PF(bp)) 13906 return 0; 13907 13908 rc = bnxt_hwrm_func_qcfg(bp); 13909 if (rc) { 13910 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 13911 return rc; 13912 } 13913 if (bp->pf.registered_vfs) 13914 return bp->pf.registered_vfs; 13915 if (bp->sriov_cfg) 13916 return 1; 13917 #endif 13918 return 0; 13919 } 13920 13921 void bnxt_fw_reset(struct bnxt *bp) 13922 { 13923 bnxt_ulp_stop(bp); 13924 bnxt_rtnl_lock_sp(bp); 13925 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 13926 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13927 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13928 int n = 0, tmo; 13929 13930 /* we have to serialize with bnxt_refclk_read()*/ 13931 if (ptp) { 13932 unsigned long flags; 13933 13934 write_seqlock_irqsave(&ptp->ptp_lock, flags); 13935 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13936 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 13937 } else { 13938 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13939 } 13940 if (bp->pf.active_vfs && 13941 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 13942 n = bnxt_get_registered_vfs(bp); 13943 if (n < 0) { 13944 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 13945 n); 13946 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13947 dev_close(bp->dev); 13948 goto fw_reset_exit; 13949 } else if (n > 0) { 13950 u16 vf_tmo_dsecs = n * 10; 13951 13952 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 13953 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 13954 bp->fw_reset_state = 13955 BNXT_FW_RESET_STATE_POLL_VF; 13956 bnxt_queue_fw_reset_work(bp, HZ / 10); 13957 goto fw_reset_exit; 13958 } 13959 bnxt_fw_reset_close(bp); 13960 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 13961 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 13962 tmo = HZ / 10; 13963 } else { 13964 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13965 tmo = bp->fw_reset_min_dsecs * HZ / 10; 13966 } 13967 bnxt_queue_fw_reset_work(bp, tmo); 13968 } 13969 fw_reset_exit: 13970 bnxt_rtnl_unlock_sp(bp); 13971 } 13972 13973 static void bnxt_chk_missed_irq(struct bnxt *bp) 13974 { 13975 int i; 13976 13977 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13978 return; 13979 13980 for (i = 0; i < bp->cp_nr_rings; i++) { 13981 struct bnxt_napi *bnapi = bp->bnapi[i]; 13982 struct bnxt_cp_ring_info *cpr; 13983 u32 fw_ring_id; 13984 int j; 13985 13986 if (!bnapi) 13987 continue; 13988 13989 cpr = &bnapi->cp_ring; 13990 for (j = 0; j < cpr->cp_ring_count; j++) { 13991 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 13992 u32 val[2]; 13993 13994 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 13995 continue; 13996 13997 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 13998 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 13999 continue; 14000 } 14001 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14002 bnxt_dbg_hwrm_ring_info_get(bp, 14003 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14004 fw_ring_id, &val[0], &val[1]); 14005 cpr->sw_stats->cmn.missed_irqs++; 14006 } 14007 } 14008 } 14009 14010 static void bnxt_cfg_ntp_filters(struct bnxt *); 14011 14012 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14013 { 14014 struct bnxt_link_info *link_info = &bp->link_info; 14015 14016 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14017 link_info->autoneg = BNXT_AUTONEG_SPEED; 14018 if (bp->hwrm_spec_code >= 0x10201) { 14019 if (link_info->auto_pause_setting & 14020 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14021 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14022 } else { 14023 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14024 } 14025 bnxt_set_auto_speed(link_info); 14026 } else { 14027 bnxt_set_force_speed(link_info); 14028 link_info->req_duplex = link_info->duplex_setting; 14029 } 14030 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14031 link_info->req_flow_ctrl = 14032 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14033 else 14034 link_info->req_flow_ctrl = link_info->force_pause_setting; 14035 } 14036 14037 static void bnxt_fw_echo_reply(struct bnxt *bp) 14038 { 14039 struct bnxt_fw_health *fw_health = bp->fw_health; 14040 struct hwrm_func_echo_response_input *req; 14041 int rc; 14042 14043 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14044 if (rc) 14045 return; 14046 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14047 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14048 hwrm_req_send(bp, req); 14049 } 14050 14051 static void bnxt_ulp_restart(struct bnxt *bp) 14052 { 14053 bnxt_ulp_stop(bp); 14054 bnxt_ulp_start(bp, 0); 14055 } 14056 14057 static void bnxt_sp_task(struct work_struct *work) 14058 { 14059 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14060 14061 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14062 smp_mb__after_atomic(); 14063 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14064 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14065 return; 14066 } 14067 14068 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14069 bnxt_ulp_restart(bp); 14070 bnxt_reenable_sriov(bp); 14071 } 14072 14073 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14074 bnxt_cfg_rx_mode(bp); 14075 14076 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14077 bnxt_cfg_ntp_filters(bp); 14078 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14079 bnxt_hwrm_exec_fwd_req(bp); 14080 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14081 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14082 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14083 bnxt_hwrm_port_qstats(bp, 0); 14084 bnxt_hwrm_port_qstats_ext(bp, 0); 14085 bnxt_accumulate_all_stats(bp); 14086 } 14087 14088 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14089 int rc; 14090 14091 mutex_lock(&bp->link_lock); 14092 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14093 &bp->sp_event)) 14094 bnxt_hwrm_phy_qcaps(bp); 14095 14096 rc = bnxt_update_link(bp, true); 14097 if (rc) 14098 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14099 rc); 14100 14101 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14102 &bp->sp_event)) 14103 bnxt_init_ethtool_link_settings(bp); 14104 mutex_unlock(&bp->link_lock); 14105 } 14106 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14107 int rc; 14108 14109 mutex_lock(&bp->link_lock); 14110 rc = bnxt_update_phy_setting(bp); 14111 mutex_unlock(&bp->link_lock); 14112 if (rc) { 14113 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14114 } else { 14115 bp->link_info.phy_retry = false; 14116 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14117 } 14118 } 14119 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14120 mutex_lock(&bp->link_lock); 14121 bnxt_get_port_module_status(bp); 14122 mutex_unlock(&bp->link_lock); 14123 } 14124 14125 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14126 bnxt_tc_flow_stats_work(bp); 14127 14128 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14129 bnxt_chk_missed_irq(bp); 14130 14131 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14132 bnxt_fw_echo_reply(bp); 14133 14134 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14135 bnxt_hwmon_notify_event(bp); 14136 14137 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14138 * must be the last functions to be called before exiting. 14139 */ 14140 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14141 bnxt_reset(bp, false); 14142 14143 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14144 bnxt_reset(bp, true); 14145 14146 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14147 bnxt_rx_ring_reset(bp); 14148 14149 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14150 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14151 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14152 bnxt_devlink_health_fw_report(bp); 14153 else 14154 bnxt_fw_reset(bp); 14155 } 14156 14157 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14158 if (!is_bnxt_fw_ok(bp)) 14159 bnxt_devlink_health_fw_report(bp); 14160 } 14161 14162 smp_mb__before_atomic(); 14163 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14164 } 14165 14166 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14167 int *max_cp); 14168 14169 /* Under rtnl_lock */ 14170 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14171 int tx_xdp) 14172 { 14173 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14174 struct bnxt_hw_rings hwr = {0}; 14175 int rx_rings = rx; 14176 int rc; 14177 14178 if (tcs) 14179 tx_sets = tcs; 14180 14181 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14182 14183 if (max_rx < rx_rings) 14184 return -ENOMEM; 14185 14186 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14187 rx_rings <<= 1; 14188 14189 hwr.rx = rx_rings; 14190 hwr.tx = tx * tx_sets + tx_xdp; 14191 if (max_tx < hwr.tx) 14192 return -ENOMEM; 14193 14194 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14195 14196 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14197 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14198 if (max_cp < hwr.cp) 14199 return -ENOMEM; 14200 hwr.stat = hwr.cp; 14201 if (BNXT_NEW_RM(bp)) { 14202 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14203 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14204 hwr.grp = rx; 14205 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14206 } 14207 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14208 hwr.cp_p5 = hwr.tx + rx; 14209 rc = bnxt_hwrm_check_rings(bp, &hwr); 14210 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14211 if (!bnxt_ulp_registered(bp->edev)) { 14212 hwr.cp += bnxt_get_ulp_msix_num(bp); 14213 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14214 } 14215 if (hwr.cp > bp->total_irqs) { 14216 int total_msix = bnxt_change_msix(bp, hwr.cp); 14217 14218 if (total_msix < hwr.cp) { 14219 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14220 hwr.cp, total_msix); 14221 rc = -ENOSPC; 14222 } 14223 } 14224 } 14225 return rc; 14226 } 14227 14228 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14229 { 14230 if (bp->bar2) { 14231 pci_iounmap(pdev, bp->bar2); 14232 bp->bar2 = NULL; 14233 } 14234 14235 if (bp->bar1) { 14236 pci_iounmap(pdev, bp->bar1); 14237 bp->bar1 = NULL; 14238 } 14239 14240 if (bp->bar0) { 14241 pci_iounmap(pdev, bp->bar0); 14242 bp->bar0 = NULL; 14243 } 14244 } 14245 14246 static void bnxt_cleanup_pci(struct bnxt *bp) 14247 { 14248 bnxt_unmap_bars(bp, bp->pdev); 14249 pci_release_regions(bp->pdev); 14250 if (pci_is_enabled(bp->pdev)) 14251 pci_disable_device(bp->pdev); 14252 } 14253 14254 static void bnxt_init_dflt_coal(struct bnxt *bp) 14255 { 14256 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14257 struct bnxt_coal *coal; 14258 u16 flags = 0; 14259 14260 if (coal_cap->cmpl_params & 14261 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14262 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14263 14264 /* Tick values in micro seconds. 14265 * 1 coal_buf x bufs_per_record = 1 completion record. 14266 */ 14267 coal = &bp->rx_coal; 14268 coal->coal_ticks = 10; 14269 coal->coal_bufs = 30; 14270 coal->coal_ticks_irq = 1; 14271 coal->coal_bufs_irq = 2; 14272 coal->idle_thresh = 50; 14273 coal->bufs_per_record = 2; 14274 coal->budget = 64; /* NAPI budget */ 14275 coal->flags = flags; 14276 14277 coal = &bp->tx_coal; 14278 coal->coal_ticks = 28; 14279 coal->coal_bufs = 30; 14280 coal->coal_ticks_irq = 2; 14281 coal->coal_bufs_irq = 2; 14282 coal->bufs_per_record = 1; 14283 coal->flags = flags; 14284 14285 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14286 } 14287 14288 /* FW that pre-reserves 1 VNIC per function */ 14289 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14290 { 14291 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14292 14293 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14294 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14295 return true; 14296 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14297 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14298 return true; 14299 return false; 14300 } 14301 14302 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14303 { 14304 int rc; 14305 14306 bp->fw_cap = 0; 14307 rc = bnxt_hwrm_ver_get(bp); 14308 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14309 * so wait before continuing with recovery. 14310 */ 14311 if (rc) 14312 msleep(100); 14313 bnxt_try_map_fw_health_reg(bp); 14314 if (rc) { 14315 rc = bnxt_try_recover_fw(bp); 14316 if (rc) 14317 return rc; 14318 rc = bnxt_hwrm_ver_get(bp); 14319 if (rc) 14320 return rc; 14321 } 14322 14323 bnxt_nvm_cfg_ver_get(bp); 14324 14325 rc = bnxt_hwrm_func_reset(bp); 14326 if (rc) 14327 return -ENODEV; 14328 14329 bnxt_hwrm_fw_set_time(bp); 14330 return 0; 14331 } 14332 14333 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14334 { 14335 int rc; 14336 14337 /* Get the MAX capabilities for this function */ 14338 rc = bnxt_hwrm_func_qcaps(bp); 14339 if (rc) { 14340 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14341 rc); 14342 return -ENODEV; 14343 } 14344 14345 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14346 if (rc) 14347 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14348 rc); 14349 14350 if (bnxt_alloc_fw_health(bp)) { 14351 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14352 } else { 14353 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14354 if (rc) 14355 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14356 rc); 14357 } 14358 14359 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14360 if (rc) 14361 return -ENODEV; 14362 14363 rc = bnxt_alloc_crash_dump_mem(bp); 14364 if (rc) 14365 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14366 rc); 14367 if (!rc) { 14368 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14369 if (rc) { 14370 bnxt_free_crash_dump_mem(bp); 14371 netdev_warn(bp->dev, 14372 "hwrm crash dump mem failure rc: %d\n", rc); 14373 } 14374 } 14375 14376 if (bnxt_fw_pre_resv_vnics(bp)) 14377 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14378 14379 bnxt_hwrm_func_qcfg(bp); 14380 bnxt_hwrm_vnic_qcaps(bp); 14381 bnxt_hwrm_port_led_qcaps(bp); 14382 bnxt_ethtool_init(bp); 14383 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14384 __bnxt_hwrm_ptp_qcfg(bp); 14385 bnxt_dcb_init(bp); 14386 bnxt_hwmon_init(bp); 14387 return 0; 14388 } 14389 14390 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14391 { 14392 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14393 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14394 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14395 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14396 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14397 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14398 bp->rss_hash_delta = bp->rss_hash_cfg; 14399 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14400 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14401 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14402 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14403 } 14404 } 14405 14406 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14407 { 14408 struct net_device *dev = bp->dev; 14409 14410 dev->hw_features &= ~NETIF_F_NTUPLE; 14411 dev->features &= ~NETIF_F_NTUPLE; 14412 bp->flags &= ~BNXT_FLAG_RFS; 14413 if (bnxt_rfs_supported(bp)) { 14414 dev->hw_features |= NETIF_F_NTUPLE; 14415 if (bnxt_rfs_capable(bp, false)) { 14416 bp->flags |= BNXT_FLAG_RFS; 14417 dev->features |= NETIF_F_NTUPLE; 14418 } 14419 } 14420 } 14421 14422 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14423 { 14424 struct pci_dev *pdev = bp->pdev; 14425 14426 bnxt_set_dflt_rss_hash_type(bp); 14427 bnxt_set_dflt_rfs(bp); 14428 14429 bnxt_get_wol_settings(bp); 14430 if (bp->flags & BNXT_FLAG_WOL_CAP) 14431 device_set_wakeup_enable(&pdev->dev, bp->wol); 14432 else 14433 device_set_wakeup_capable(&pdev->dev, false); 14434 14435 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14436 bnxt_hwrm_coal_params_qcaps(bp); 14437 } 14438 14439 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14440 14441 int bnxt_fw_init_one(struct bnxt *bp) 14442 { 14443 int rc; 14444 14445 rc = bnxt_fw_init_one_p1(bp); 14446 if (rc) { 14447 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14448 return rc; 14449 } 14450 rc = bnxt_fw_init_one_p2(bp); 14451 if (rc) { 14452 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14453 return rc; 14454 } 14455 rc = bnxt_probe_phy(bp, false); 14456 if (rc) 14457 return rc; 14458 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14459 if (rc) 14460 return rc; 14461 14462 bnxt_fw_init_one_p3(bp); 14463 return 0; 14464 } 14465 14466 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14467 { 14468 struct bnxt_fw_health *fw_health = bp->fw_health; 14469 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14470 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14471 u32 reg_type, reg_off, delay_msecs; 14472 14473 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14474 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14475 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14476 switch (reg_type) { 14477 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14478 pci_write_config_dword(bp->pdev, reg_off, val); 14479 break; 14480 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14481 writel(reg_off & BNXT_GRC_BASE_MASK, 14482 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14483 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14484 fallthrough; 14485 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14486 writel(val, bp->bar0 + reg_off); 14487 break; 14488 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14489 writel(val, bp->bar1 + reg_off); 14490 break; 14491 } 14492 if (delay_msecs) { 14493 pci_read_config_dword(bp->pdev, 0, &val); 14494 msleep(delay_msecs); 14495 } 14496 } 14497 14498 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14499 { 14500 struct hwrm_func_qcfg_output *resp; 14501 struct hwrm_func_qcfg_input *req; 14502 bool result = true; /* firmware will enforce if unknown */ 14503 14504 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14505 return result; 14506 14507 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14508 return result; 14509 14510 req->fid = cpu_to_le16(0xffff); 14511 resp = hwrm_req_hold(bp, req); 14512 if (!hwrm_req_send(bp, req)) 14513 result = !!(le16_to_cpu(resp->flags) & 14514 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14515 hwrm_req_drop(bp, req); 14516 return result; 14517 } 14518 14519 static void bnxt_reset_all(struct bnxt *bp) 14520 { 14521 struct bnxt_fw_health *fw_health = bp->fw_health; 14522 int i, rc; 14523 14524 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14525 bnxt_fw_reset_via_optee(bp); 14526 bp->fw_reset_timestamp = jiffies; 14527 return; 14528 } 14529 14530 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14531 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14532 bnxt_fw_reset_writel(bp, i); 14533 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14534 struct hwrm_fw_reset_input *req; 14535 14536 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14537 if (!rc) { 14538 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14539 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14540 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14541 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14542 rc = hwrm_req_send(bp, req); 14543 } 14544 if (rc != -ENODEV) 14545 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14546 } 14547 bp->fw_reset_timestamp = jiffies; 14548 } 14549 14550 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14551 { 14552 return time_after(jiffies, bp->fw_reset_timestamp + 14553 (bp->fw_reset_max_dsecs * HZ / 10)); 14554 } 14555 14556 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14557 { 14558 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14559 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14560 bnxt_dl_health_fw_status_update(bp, false); 14561 bp->fw_reset_state = 0; 14562 dev_close(bp->dev); 14563 } 14564 14565 static void bnxt_fw_reset_task(struct work_struct *work) 14566 { 14567 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14568 int rc = 0; 14569 14570 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14571 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14572 return; 14573 } 14574 14575 switch (bp->fw_reset_state) { 14576 case BNXT_FW_RESET_STATE_POLL_VF: { 14577 int n = bnxt_get_registered_vfs(bp); 14578 int tmo; 14579 14580 if (n < 0) { 14581 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14582 n, jiffies_to_msecs(jiffies - 14583 bp->fw_reset_timestamp)); 14584 goto fw_reset_abort; 14585 } else if (n > 0) { 14586 if (bnxt_fw_reset_timeout(bp)) { 14587 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14588 bp->fw_reset_state = 0; 14589 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14590 n); 14591 goto ulp_start; 14592 } 14593 bnxt_queue_fw_reset_work(bp, HZ / 10); 14594 return; 14595 } 14596 bp->fw_reset_timestamp = jiffies; 14597 rtnl_lock(); 14598 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 14599 bnxt_fw_reset_abort(bp, rc); 14600 rtnl_unlock(); 14601 goto ulp_start; 14602 } 14603 bnxt_fw_reset_close(bp); 14604 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14605 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14606 tmo = HZ / 10; 14607 } else { 14608 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14609 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14610 } 14611 rtnl_unlock(); 14612 bnxt_queue_fw_reset_work(bp, tmo); 14613 return; 14614 } 14615 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 14616 u32 val; 14617 14618 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14619 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 14620 !bnxt_fw_reset_timeout(bp)) { 14621 bnxt_queue_fw_reset_work(bp, HZ / 5); 14622 return; 14623 } 14624 14625 if (!bp->fw_health->primary) { 14626 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 14627 14628 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14629 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14630 return; 14631 } 14632 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14633 } 14634 fallthrough; 14635 case BNXT_FW_RESET_STATE_RESET_FW: 14636 bnxt_reset_all(bp); 14637 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14638 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 14639 return; 14640 case BNXT_FW_RESET_STATE_ENABLE_DEV: 14641 bnxt_inv_fw_health_reg(bp); 14642 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 14643 !bp->fw_reset_min_dsecs) { 14644 u16 val; 14645 14646 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14647 if (val == 0xffff) { 14648 if (bnxt_fw_reset_timeout(bp)) { 14649 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 14650 rc = -ETIMEDOUT; 14651 goto fw_reset_abort; 14652 } 14653 bnxt_queue_fw_reset_work(bp, HZ / 1000); 14654 return; 14655 } 14656 } 14657 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14658 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 14659 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 14660 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 14661 bnxt_dl_remote_reload(bp); 14662 if (pci_enable_device(bp->pdev)) { 14663 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 14664 rc = -ENODEV; 14665 goto fw_reset_abort; 14666 } 14667 pci_set_master(bp->pdev); 14668 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 14669 fallthrough; 14670 case BNXT_FW_RESET_STATE_POLL_FW: 14671 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 14672 rc = bnxt_hwrm_poll(bp); 14673 if (rc) { 14674 if (bnxt_fw_reset_timeout(bp)) { 14675 netdev_err(bp->dev, "Firmware reset aborted\n"); 14676 goto fw_reset_abort_status; 14677 } 14678 bnxt_queue_fw_reset_work(bp, HZ / 5); 14679 return; 14680 } 14681 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 14682 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 14683 fallthrough; 14684 case BNXT_FW_RESET_STATE_OPENING: 14685 while (!rtnl_trylock()) { 14686 bnxt_queue_fw_reset_work(bp, HZ / 10); 14687 return; 14688 } 14689 rc = bnxt_open(bp->dev); 14690 if (rc) { 14691 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 14692 bnxt_fw_reset_abort(bp, rc); 14693 rtnl_unlock(); 14694 goto ulp_start; 14695 } 14696 14697 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 14698 bp->fw_health->enabled) { 14699 bp->fw_health->last_fw_reset_cnt = 14700 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14701 } 14702 bp->fw_reset_state = 0; 14703 /* Make sure fw_reset_state is 0 before clearing the flag */ 14704 smp_mb__before_atomic(); 14705 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14706 bnxt_ptp_reapply_pps(bp); 14707 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 14708 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 14709 bnxt_dl_health_fw_recovery_done(bp); 14710 bnxt_dl_health_fw_status_update(bp, true); 14711 } 14712 rtnl_unlock(); 14713 bnxt_ulp_start(bp, 0); 14714 bnxt_reenable_sriov(bp); 14715 rtnl_lock(); 14716 bnxt_vf_reps_alloc(bp); 14717 bnxt_vf_reps_open(bp); 14718 rtnl_unlock(); 14719 break; 14720 } 14721 return; 14722 14723 fw_reset_abort_status: 14724 if (bp->fw_health->status_reliable || 14725 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 14726 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14727 14728 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 14729 } 14730 fw_reset_abort: 14731 rtnl_lock(); 14732 bnxt_fw_reset_abort(bp, rc); 14733 rtnl_unlock(); 14734 ulp_start: 14735 bnxt_ulp_start(bp, rc); 14736 } 14737 14738 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 14739 { 14740 int rc; 14741 struct bnxt *bp = netdev_priv(dev); 14742 14743 SET_NETDEV_DEV(dev, &pdev->dev); 14744 14745 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 14746 rc = pci_enable_device(pdev); 14747 if (rc) { 14748 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 14749 goto init_err; 14750 } 14751 14752 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 14753 dev_err(&pdev->dev, 14754 "Cannot find PCI device base address, aborting\n"); 14755 rc = -ENODEV; 14756 goto init_err_disable; 14757 } 14758 14759 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 14760 if (rc) { 14761 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 14762 goto init_err_disable; 14763 } 14764 14765 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 14766 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 14767 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 14768 rc = -EIO; 14769 goto init_err_release; 14770 } 14771 14772 pci_set_master(pdev); 14773 14774 bp->dev = dev; 14775 bp->pdev = pdev; 14776 14777 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 14778 * determines the BAR size. 14779 */ 14780 bp->bar0 = pci_ioremap_bar(pdev, 0); 14781 if (!bp->bar0) { 14782 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 14783 rc = -ENOMEM; 14784 goto init_err_release; 14785 } 14786 14787 bp->bar2 = pci_ioremap_bar(pdev, 4); 14788 if (!bp->bar2) { 14789 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 14790 rc = -ENOMEM; 14791 goto init_err_release; 14792 } 14793 14794 INIT_WORK(&bp->sp_task, bnxt_sp_task); 14795 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 14796 14797 spin_lock_init(&bp->ntp_fltr_lock); 14798 #if BITS_PER_LONG == 32 14799 spin_lock_init(&bp->db_lock); 14800 #endif 14801 14802 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 14803 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 14804 14805 timer_setup(&bp->timer, bnxt_timer, 0); 14806 bp->current_interval = BNXT_TIMER_INTERVAL; 14807 14808 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 14809 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 14810 14811 clear_bit(BNXT_STATE_OPEN, &bp->state); 14812 return 0; 14813 14814 init_err_release: 14815 bnxt_unmap_bars(bp, pdev); 14816 pci_release_regions(pdev); 14817 14818 init_err_disable: 14819 pci_disable_device(pdev); 14820 14821 init_err: 14822 return rc; 14823 } 14824 14825 /* rtnl_lock held */ 14826 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 14827 { 14828 struct sockaddr *addr = p; 14829 struct bnxt *bp = netdev_priv(dev); 14830 int rc = 0; 14831 14832 if (!is_valid_ether_addr(addr->sa_data)) 14833 return -EADDRNOTAVAIL; 14834 14835 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 14836 return 0; 14837 14838 rc = bnxt_approve_mac(bp, addr->sa_data, true); 14839 if (rc) 14840 return rc; 14841 14842 eth_hw_addr_set(dev, addr->sa_data); 14843 bnxt_clear_usr_fltrs(bp, true); 14844 if (netif_running(dev)) { 14845 bnxt_close_nic(bp, false, false); 14846 rc = bnxt_open_nic(bp, false, false); 14847 } 14848 14849 return rc; 14850 } 14851 14852 /* rtnl_lock held */ 14853 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 14854 { 14855 struct bnxt *bp = netdev_priv(dev); 14856 14857 if (netif_running(dev)) 14858 bnxt_close_nic(bp, true, false); 14859 14860 WRITE_ONCE(dev->mtu, new_mtu); 14861 14862 /* MTU change may change the AGG ring settings if an XDP multi-buffer 14863 * program is attached. We need to set the AGG rings settings and 14864 * rx_skb_func accordingly. 14865 */ 14866 if (READ_ONCE(bp->xdp_prog)) 14867 bnxt_set_rx_skb_mode(bp, true); 14868 14869 bnxt_set_ring_params(bp); 14870 14871 if (netif_running(dev)) 14872 return bnxt_open_nic(bp, true, false); 14873 14874 return 0; 14875 } 14876 14877 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 14878 { 14879 struct bnxt *bp = netdev_priv(dev); 14880 bool sh = false; 14881 int rc, tx_cp; 14882 14883 if (tc > bp->max_tc) { 14884 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 14885 tc, bp->max_tc); 14886 return -EINVAL; 14887 } 14888 14889 if (bp->num_tc == tc) 14890 return 0; 14891 14892 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 14893 sh = true; 14894 14895 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 14896 sh, tc, bp->tx_nr_rings_xdp); 14897 if (rc) 14898 return rc; 14899 14900 /* Needs to close the device and do hw resource re-allocations */ 14901 if (netif_running(bp->dev)) 14902 bnxt_close_nic(bp, true, false); 14903 14904 if (tc) { 14905 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 14906 netdev_set_num_tc(dev, tc); 14907 bp->num_tc = tc; 14908 } else { 14909 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 14910 netdev_reset_tc(dev); 14911 bp->num_tc = 0; 14912 } 14913 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 14914 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 14915 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 14916 tx_cp + bp->rx_nr_rings; 14917 14918 if (netif_running(bp->dev)) 14919 return bnxt_open_nic(bp, true, false); 14920 14921 return 0; 14922 } 14923 14924 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 14925 void *cb_priv) 14926 { 14927 struct bnxt *bp = cb_priv; 14928 14929 if (!bnxt_tc_flower_enabled(bp) || 14930 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 14931 return -EOPNOTSUPP; 14932 14933 switch (type) { 14934 case TC_SETUP_CLSFLOWER: 14935 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 14936 default: 14937 return -EOPNOTSUPP; 14938 } 14939 } 14940 14941 LIST_HEAD(bnxt_block_cb_list); 14942 14943 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 14944 void *type_data) 14945 { 14946 struct bnxt *bp = netdev_priv(dev); 14947 14948 switch (type) { 14949 case TC_SETUP_BLOCK: 14950 return flow_block_cb_setup_simple(type_data, 14951 &bnxt_block_cb_list, 14952 bnxt_setup_tc_block_cb, 14953 bp, bp, true); 14954 case TC_SETUP_QDISC_MQPRIO: { 14955 struct tc_mqprio_qopt *mqprio = type_data; 14956 14957 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 14958 14959 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 14960 } 14961 default: 14962 return -EOPNOTSUPP; 14963 } 14964 } 14965 14966 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 14967 const struct sk_buff *skb) 14968 { 14969 struct bnxt_vnic_info *vnic; 14970 14971 if (skb) 14972 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 14973 14974 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 14975 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 14976 } 14977 14978 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 14979 u32 idx) 14980 { 14981 struct hlist_head *head; 14982 int bit_id; 14983 14984 spin_lock_bh(&bp->ntp_fltr_lock); 14985 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 14986 if (bit_id < 0) { 14987 spin_unlock_bh(&bp->ntp_fltr_lock); 14988 return -ENOMEM; 14989 } 14990 14991 fltr->base.sw_id = (u16)bit_id; 14992 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 14993 fltr->base.flags |= BNXT_ACT_RING_DST; 14994 head = &bp->ntp_fltr_hash_tbl[idx]; 14995 hlist_add_head_rcu(&fltr->base.hash, head); 14996 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 14997 bnxt_insert_usr_fltr(bp, &fltr->base); 14998 bp->ntp_fltr_count++; 14999 spin_unlock_bh(&bp->ntp_fltr_lock); 15000 return 0; 15001 } 15002 15003 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15004 struct bnxt_ntuple_filter *f2) 15005 { 15006 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15007 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15008 struct flow_keys *keys1 = &f1->fkeys; 15009 struct flow_keys *keys2 = &f2->fkeys; 15010 15011 if (keys1->basic.n_proto != keys2->basic.n_proto || 15012 keys1->basic.ip_proto != keys2->basic.ip_proto) 15013 return false; 15014 15015 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15016 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15017 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15018 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15019 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15020 return false; 15021 } else { 15022 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15023 &keys2->addrs.v6addrs.src) || 15024 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15025 &masks2->addrs.v6addrs.src) || 15026 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15027 &keys2->addrs.v6addrs.dst) || 15028 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15029 &masks2->addrs.v6addrs.dst)) 15030 return false; 15031 } 15032 15033 return keys1->ports.src == keys2->ports.src && 15034 masks1->ports.src == masks2->ports.src && 15035 keys1->ports.dst == keys2->ports.dst && 15036 masks1->ports.dst == masks2->ports.dst && 15037 keys1->control.flags == keys2->control.flags && 15038 f1->l2_fltr == f2->l2_fltr; 15039 } 15040 15041 struct bnxt_ntuple_filter * 15042 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15043 struct bnxt_ntuple_filter *fltr, u32 idx) 15044 { 15045 struct bnxt_ntuple_filter *f; 15046 struct hlist_head *head; 15047 15048 head = &bp->ntp_fltr_hash_tbl[idx]; 15049 hlist_for_each_entry_rcu(f, head, base.hash) { 15050 if (bnxt_fltr_match(f, fltr)) 15051 return f; 15052 } 15053 return NULL; 15054 } 15055 15056 #ifdef CONFIG_RFS_ACCEL 15057 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15058 u16 rxq_index, u32 flow_id) 15059 { 15060 struct bnxt *bp = netdev_priv(dev); 15061 struct bnxt_ntuple_filter *fltr, *new_fltr; 15062 struct flow_keys *fkeys; 15063 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15064 struct bnxt_l2_filter *l2_fltr; 15065 int rc = 0, idx; 15066 u32 flags; 15067 15068 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15069 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15070 atomic_inc(&l2_fltr->refcnt); 15071 } else { 15072 struct bnxt_l2_key key; 15073 15074 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15075 key.vlan = 0; 15076 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15077 if (!l2_fltr) 15078 return -EINVAL; 15079 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15080 bnxt_del_l2_filter(bp, l2_fltr); 15081 return -EINVAL; 15082 } 15083 } 15084 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15085 if (!new_fltr) { 15086 bnxt_del_l2_filter(bp, l2_fltr); 15087 return -ENOMEM; 15088 } 15089 15090 fkeys = &new_fltr->fkeys; 15091 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15092 rc = -EPROTONOSUPPORT; 15093 goto err_free; 15094 } 15095 15096 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15097 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15098 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15099 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15100 rc = -EPROTONOSUPPORT; 15101 goto err_free; 15102 } 15103 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15104 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15105 if (bp->hwrm_spec_code < 0x10601) { 15106 rc = -EPROTONOSUPPORT; 15107 goto err_free; 15108 } 15109 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15110 } 15111 flags = fkeys->control.flags; 15112 if (((flags & FLOW_DIS_ENCAPSULATION) && 15113 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15114 rc = -EPROTONOSUPPORT; 15115 goto err_free; 15116 } 15117 new_fltr->l2_fltr = l2_fltr; 15118 15119 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15120 rcu_read_lock(); 15121 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15122 if (fltr) { 15123 rc = fltr->base.sw_id; 15124 rcu_read_unlock(); 15125 goto err_free; 15126 } 15127 rcu_read_unlock(); 15128 15129 new_fltr->flow_id = flow_id; 15130 new_fltr->base.rxq = rxq_index; 15131 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15132 if (!rc) { 15133 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15134 return new_fltr->base.sw_id; 15135 } 15136 15137 err_free: 15138 bnxt_del_l2_filter(bp, l2_fltr); 15139 kfree(new_fltr); 15140 return rc; 15141 } 15142 #endif 15143 15144 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15145 { 15146 spin_lock_bh(&bp->ntp_fltr_lock); 15147 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15148 spin_unlock_bh(&bp->ntp_fltr_lock); 15149 return; 15150 } 15151 hlist_del_rcu(&fltr->base.hash); 15152 bnxt_del_one_usr_fltr(bp, &fltr->base); 15153 bp->ntp_fltr_count--; 15154 spin_unlock_bh(&bp->ntp_fltr_lock); 15155 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15156 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15157 kfree_rcu(fltr, base.rcu); 15158 } 15159 15160 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15161 { 15162 #ifdef CONFIG_RFS_ACCEL 15163 int i; 15164 15165 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15166 struct hlist_head *head; 15167 struct hlist_node *tmp; 15168 struct bnxt_ntuple_filter *fltr; 15169 int rc; 15170 15171 head = &bp->ntp_fltr_hash_tbl[i]; 15172 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15173 bool del = false; 15174 15175 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15176 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15177 continue; 15178 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15179 fltr->flow_id, 15180 fltr->base.sw_id)) { 15181 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15182 fltr); 15183 del = true; 15184 } 15185 } else { 15186 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15187 fltr); 15188 if (rc) 15189 del = true; 15190 else 15191 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15192 } 15193 15194 if (del) 15195 bnxt_del_ntp_filter(bp, fltr); 15196 } 15197 } 15198 #endif 15199 } 15200 15201 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15202 unsigned int entry, struct udp_tunnel_info *ti) 15203 { 15204 struct bnxt *bp = netdev_priv(netdev); 15205 unsigned int cmd; 15206 15207 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15208 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15209 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15210 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15211 else 15212 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15213 15214 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15215 } 15216 15217 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15218 unsigned int entry, struct udp_tunnel_info *ti) 15219 { 15220 struct bnxt *bp = netdev_priv(netdev); 15221 unsigned int cmd; 15222 15223 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15224 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15225 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15226 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15227 else 15228 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15229 15230 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15231 } 15232 15233 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15234 .set_port = bnxt_udp_tunnel_set_port, 15235 .unset_port = bnxt_udp_tunnel_unset_port, 15236 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15237 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15238 .tables = { 15239 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15240 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15241 }, 15242 }, bnxt_udp_tunnels_p7 = { 15243 .set_port = bnxt_udp_tunnel_set_port, 15244 .unset_port = bnxt_udp_tunnel_unset_port, 15245 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 15246 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15247 .tables = { 15248 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15249 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15250 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15251 }, 15252 }; 15253 15254 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15255 struct net_device *dev, u32 filter_mask, 15256 int nlflags) 15257 { 15258 struct bnxt *bp = netdev_priv(dev); 15259 15260 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15261 nlflags, filter_mask, NULL); 15262 } 15263 15264 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15265 u16 flags, struct netlink_ext_ack *extack) 15266 { 15267 struct bnxt *bp = netdev_priv(dev); 15268 struct nlattr *attr, *br_spec; 15269 int rem, rc = 0; 15270 15271 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15272 return -EOPNOTSUPP; 15273 15274 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15275 if (!br_spec) 15276 return -EINVAL; 15277 15278 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15279 u16 mode; 15280 15281 mode = nla_get_u16(attr); 15282 if (mode == bp->br_mode) 15283 break; 15284 15285 rc = bnxt_hwrm_set_br_mode(bp, mode); 15286 if (!rc) 15287 bp->br_mode = mode; 15288 break; 15289 } 15290 return rc; 15291 } 15292 15293 int bnxt_get_port_parent_id(struct net_device *dev, 15294 struct netdev_phys_item_id *ppid) 15295 { 15296 struct bnxt *bp = netdev_priv(dev); 15297 15298 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15299 return -EOPNOTSUPP; 15300 15301 /* The PF and it's VF-reps only support the switchdev framework */ 15302 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15303 return -EOPNOTSUPP; 15304 15305 ppid->id_len = sizeof(bp->dsn); 15306 memcpy(ppid->id, bp->dsn, ppid->id_len); 15307 15308 return 0; 15309 } 15310 15311 static const struct net_device_ops bnxt_netdev_ops = { 15312 .ndo_open = bnxt_open, 15313 .ndo_start_xmit = bnxt_start_xmit, 15314 .ndo_stop = bnxt_close, 15315 .ndo_get_stats64 = bnxt_get_stats64, 15316 .ndo_set_rx_mode = bnxt_set_rx_mode, 15317 .ndo_eth_ioctl = bnxt_ioctl, 15318 .ndo_validate_addr = eth_validate_addr, 15319 .ndo_set_mac_address = bnxt_change_mac_addr, 15320 .ndo_change_mtu = bnxt_change_mtu, 15321 .ndo_fix_features = bnxt_fix_features, 15322 .ndo_set_features = bnxt_set_features, 15323 .ndo_features_check = bnxt_features_check, 15324 .ndo_tx_timeout = bnxt_tx_timeout, 15325 #ifdef CONFIG_BNXT_SRIOV 15326 .ndo_get_vf_config = bnxt_get_vf_config, 15327 .ndo_set_vf_mac = bnxt_set_vf_mac, 15328 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15329 .ndo_set_vf_rate = bnxt_set_vf_bw, 15330 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15331 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15332 .ndo_set_vf_trust = bnxt_set_vf_trust, 15333 #endif 15334 .ndo_setup_tc = bnxt_setup_tc, 15335 #ifdef CONFIG_RFS_ACCEL 15336 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15337 #endif 15338 .ndo_bpf = bnxt_xdp, 15339 .ndo_xdp_xmit = bnxt_xdp_xmit, 15340 .ndo_bridge_getlink = bnxt_bridge_getlink, 15341 .ndo_bridge_setlink = bnxt_bridge_setlink, 15342 }; 15343 15344 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15345 struct netdev_queue_stats_rx *stats) 15346 { 15347 struct bnxt *bp = netdev_priv(dev); 15348 struct bnxt_cp_ring_info *cpr; 15349 u64 *sw; 15350 15351 cpr = &bp->bnapi[i]->cp_ring; 15352 sw = cpr->stats.sw_stats; 15353 15354 stats->packets = 0; 15355 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15356 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15357 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15358 15359 stats->bytes = 0; 15360 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15361 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15362 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15363 15364 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15365 } 15366 15367 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15368 struct netdev_queue_stats_tx *stats) 15369 { 15370 struct bnxt *bp = netdev_priv(dev); 15371 struct bnxt_napi *bnapi; 15372 u64 *sw; 15373 15374 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15375 sw = bnapi->cp_ring.stats.sw_stats; 15376 15377 stats->packets = 0; 15378 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15379 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15380 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15381 15382 stats->bytes = 0; 15383 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15384 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15385 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15386 } 15387 15388 static void bnxt_get_base_stats(struct net_device *dev, 15389 struct netdev_queue_stats_rx *rx, 15390 struct netdev_queue_stats_tx *tx) 15391 { 15392 struct bnxt *bp = netdev_priv(dev); 15393 15394 rx->packets = bp->net_stats_prev.rx_packets; 15395 rx->bytes = bp->net_stats_prev.rx_bytes; 15396 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15397 15398 tx->packets = bp->net_stats_prev.tx_packets; 15399 tx->bytes = bp->net_stats_prev.tx_bytes; 15400 } 15401 15402 static const struct netdev_stat_ops bnxt_stat_ops = { 15403 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15404 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15405 .get_base_stats = bnxt_get_base_stats, 15406 }; 15407 15408 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15409 { 15410 struct bnxt_rx_ring_info *rxr, *clone; 15411 struct bnxt *bp = netdev_priv(dev); 15412 struct bnxt_ring_struct *ring; 15413 int rc; 15414 15415 rxr = &bp->rx_ring[idx]; 15416 clone = qmem; 15417 memcpy(clone, rxr, sizeof(*rxr)); 15418 bnxt_init_rx_ring_struct(bp, clone); 15419 bnxt_reset_rx_ring_struct(bp, clone); 15420 15421 clone->rx_prod = 0; 15422 clone->rx_agg_prod = 0; 15423 clone->rx_sw_agg_prod = 0; 15424 clone->rx_next_cons = 0; 15425 15426 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15427 if (rc) 15428 return rc; 15429 15430 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15431 if (rc < 0) 15432 goto err_page_pool_destroy; 15433 15434 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15435 MEM_TYPE_PAGE_POOL, 15436 clone->page_pool); 15437 if (rc) 15438 goto err_rxq_info_unreg; 15439 15440 ring = &clone->rx_ring_struct; 15441 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15442 if (rc) 15443 goto err_free_rx_ring; 15444 15445 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15446 ring = &clone->rx_agg_ring_struct; 15447 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15448 if (rc) 15449 goto err_free_rx_agg_ring; 15450 15451 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15452 if (rc) 15453 goto err_free_rx_agg_ring; 15454 } 15455 15456 if (bp->flags & BNXT_FLAG_TPA) { 15457 rc = bnxt_alloc_one_tpa_info(bp, clone); 15458 if (rc) 15459 goto err_free_tpa_info; 15460 } 15461 15462 bnxt_init_one_rx_ring_rxbd(bp, clone); 15463 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15464 15465 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15466 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15467 bnxt_alloc_one_rx_ring_page(bp, clone, idx); 15468 if (bp->flags & BNXT_FLAG_TPA) 15469 bnxt_alloc_one_tpa_info_data(bp, clone); 15470 15471 return 0; 15472 15473 err_free_tpa_info: 15474 bnxt_free_one_tpa_info(bp, clone); 15475 err_free_rx_agg_ring: 15476 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15477 err_free_rx_ring: 15478 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15479 err_rxq_info_unreg: 15480 xdp_rxq_info_unreg(&clone->xdp_rxq); 15481 err_page_pool_destroy: 15482 page_pool_destroy(clone->page_pool); 15483 if (bnxt_separate_head_pool()) 15484 page_pool_destroy(clone->head_pool); 15485 clone->page_pool = NULL; 15486 clone->head_pool = NULL; 15487 return rc; 15488 } 15489 15490 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15491 { 15492 struct bnxt_rx_ring_info *rxr = qmem; 15493 struct bnxt *bp = netdev_priv(dev); 15494 struct bnxt_ring_struct *ring; 15495 15496 bnxt_free_one_rx_ring_skbs(bp, rxr); 15497 15498 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15499 15500 page_pool_destroy(rxr->page_pool); 15501 if (bnxt_separate_head_pool()) 15502 page_pool_destroy(rxr->head_pool); 15503 rxr->page_pool = NULL; 15504 rxr->head_pool = NULL; 15505 15506 ring = &rxr->rx_ring_struct; 15507 bnxt_free_ring(bp, &ring->ring_mem); 15508 15509 ring = &rxr->rx_agg_ring_struct; 15510 bnxt_free_ring(bp, &ring->ring_mem); 15511 15512 kfree(rxr->rx_agg_bmap); 15513 rxr->rx_agg_bmap = NULL; 15514 } 15515 15516 static void bnxt_copy_rx_ring(struct bnxt *bp, 15517 struct bnxt_rx_ring_info *dst, 15518 struct bnxt_rx_ring_info *src) 15519 { 15520 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15521 struct bnxt_ring_struct *dst_ring, *src_ring; 15522 int i; 15523 15524 dst_ring = &dst->rx_ring_struct; 15525 dst_rmem = &dst_ring->ring_mem; 15526 src_ring = &src->rx_ring_struct; 15527 src_rmem = &src_ring->ring_mem; 15528 15529 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15530 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15531 WARN_ON(dst_rmem->flags != src_rmem->flags); 15532 WARN_ON(dst_rmem->depth != src_rmem->depth); 15533 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15534 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15535 15536 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15537 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15538 *dst_rmem->vmem = *src_rmem->vmem; 15539 for (i = 0; i < dst_rmem->nr_pages; i++) { 15540 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15541 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15542 } 15543 15544 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15545 return; 15546 15547 dst_ring = &dst->rx_agg_ring_struct; 15548 dst_rmem = &dst_ring->ring_mem; 15549 src_ring = &src->rx_agg_ring_struct; 15550 src_rmem = &src_ring->ring_mem; 15551 15552 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15553 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15554 WARN_ON(dst_rmem->flags != src_rmem->flags); 15555 WARN_ON(dst_rmem->depth != src_rmem->depth); 15556 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15557 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15558 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15559 15560 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15561 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15562 *dst_rmem->vmem = *src_rmem->vmem; 15563 for (i = 0; i < dst_rmem->nr_pages; i++) { 15564 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15565 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15566 } 15567 15568 dst->rx_agg_bmap = src->rx_agg_bmap; 15569 } 15570 15571 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15572 { 15573 struct bnxt *bp = netdev_priv(dev); 15574 struct bnxt_rx_ring_info *rxr, *clone; 15575 struct bnxt_cp_ring_info *cpr; 15576 struct bnxt_vnic_info *vnic; 15577 int i, rc; 15578 15579 rxr = &bp->rx_ring[idx]; 15580 clone = qmem; 15581 15582 rxr->rx_prod = clone->rx_prod; 15583 rxr->rx_agg_prod = clone->rx_agg_prod; 15584 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 15585 rxr->rx_next_cons = clone->rx_next_cons; 15586 rxr->rx_tpa = clone->rx_tpa; 15587 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 15588 rxr->page_pool = clone->page_pool; 15589 rxr->head_pool = clone->head_pool; 15590 rxr->xdp_rxq = clone->xdp_rxq; 15591 15592 bnxt_copy_rx_ring(bp, rxr, clone); 15593 15594 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 15595 if (rc) 15596 return rc; 15597 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 15598 if (rc) 15599 goto err_free_hwrm_rx_ring; 15600 15601 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 15602 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15603 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 15604 15605 cpr = &rxr->bnapi->cp_ring; 15606 cpr->sw_stats->rx.rx_resets++; 15607 15608 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15609 vnic = &bp->vnic_info[i]; 15610 15611 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 15612 if (rc) { 15613 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 15614 vnic->vnic_id, rc); 15615 return rc; 15616 } 15617 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 15618 bnxt_hwrm_vnic_update(bp, vnic, 15619 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15620 } 15621 15622 return 0; 15623 15624 err_free_hwrm_rx_ring: 15625 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15626 return rc; 15627 } 15628 15629 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 15630 { 15631 struct bnxt *bp = netdev_priv(dev); 15632 struct bnxt_rx_ring_info *rxr; 15633 struct bnxt_vnic_info *vnic; 15634 int i; 15635 15636 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15637 vnic = &bp->vnic_info[i]; 15638 vnic->mru = 0; 15639 bnxt_hwrm_vnic_update(bp, vnic, 15640 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15641 } 15642 /* Make sure NAPI sees that the VNIC is disabled */ 15643 synchronize_net(); 15644 rxr = &bp->rx_ring[idx]; 15645 cancel_work_sync(&rxr->bnapi->cp_ring.dim.work); 15646 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15647 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 15648 rxr->rx_next_cons = 0; 15649 page_pool_disable_direct_recycling(rxr->page_pool); 15650 if (bnxt_separate_head_pool()) 15651 page_pool_disable_direct_recycling(rxr->head_pool); 15652 15653 memcpy(qmem, rxr, sizeof(*rxr)); 15654 bnxt_init_rx_ring_struct(bp, qmem); 15655 15656 return 0; 15657 } 15658 15659 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 15660 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 15661 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 15662 .ndo_queue_mem_free = bnxt_queue_mem_free, 15663 .ndo_queue_start = bnxt_queue_start, 15664 .ndo_queue_stop = bnxt_queue_stop, 15665 }; 15666 15667 static void bnxt_remove_one(struct pci_dev *pdev) 15668 { 15669 struct net_device *dev = pci_get_drvdata(pdev); 15670 struct bnxt *bp = netdev_priv(dev); 15671 15672 if (BNXT_PF(bp)) 15673 bnxt_sriov_disable(bp); 15674 15675 bnxt_rdma_aux_device_del(bp); 15676 15677 bnxt_ptp_clear(bp); 15678 unregister_netdev(dev); 15679 15680 bnxt_rdma_aux_device_uninit(bp); 15681 15682 bnxt_free_l2_filters(bp, true); 15683 bnxt_free_ntp_fltrs(bp, true); 15684 WARN_ON(bp->num_rss_ctx); 15685 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15686 /* Flush any pending tasks */ 15687 cancel_work_sync(&bp->sp_task); 15688 cancel_delayed_work_sync(&bp->fw_reset_task); 15689 bp->sp_event = 0; 15690 15691 bnxt_dl_fw_reporters_destroy(bp); 15692 bnxt_dl_unregister(bp); 15693 bnxt_shutdown_tc(bp); 15694 15695 bnxt_clear_int_mode(bp); 15696 bnxt_hwrm_func_drv_unrgtr(bp); 15697 bnxt_free_hwrm_resources(bp); 15698 bnxt_hwmon_uninit(bp); 15699 bnxt_ethtool_free(bp); 15700 bnxt_dcb_free(bp); 15701 kfree(bp->ptp_cfg); 15702 bp->ptp_cfg = NULL; 15703 kfree(bp->fw_health); 15704 bp->fw_health = NULL; 15705 bnxt_cleanup_pci(bp); 15706 bnxt_free_ctx_mem(bp, true); 15707 bnxt_free_crash_dump_mem(bp); 15708 kfree(bp->rss_indir_tbl); 15709 bp->rss_indir_tbl = NULL; 15710 bnxt_free_port_stats(bp); 15711 free_netdev(dev); 15712 } 15713 15714 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 15715 { 15716 int rc = 0; 15717 struct bnxt_link_info *link_info = &bp->link_info; 15718 15719 bp->phy_flags = 0; 15720 rc = bnxt_hwrm_phy_qcaps(bp); 15721 if (rc) { 15722 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 15723 rc); 15724 return rc; 15725 } 15726 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 15727 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 15728 else 15729 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 15730 15731 bp->mac_flags = 0; 15732 bnxt_hwrm_mac_qcaps(bp); 15733 15734 if (!fw_dflt) 15735 return 0; 15736 15737 mutex_lock(&bp->link_lock); 15738 rc = bnxt_update_link(bp, false); 15739 if (rc) { 15740 mutex_unlock(&bp->link_lock); 15741 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 15742 rc); 15743 return rc; 15744 } 15745 15746 /* Older firmware does not have supported_auto_speeds, so assume 15747 * that all supported speeds can be autonegotiated. 15748 */ 15749 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 15750 link_info->support_auto_speeds = link_info->support_speeds; 15751 15752 bnxt_init_ethtool_link_settings(bp); 15753 mutex_unlock(&bp->link_lock); 15754 return 0; 15755 } 15756 15757 static int bnxt_get_max_irq(struct pci_dev *pdev) 15758 { 15759 u16 ctrl; 15760 15761 if (!pdev->msix_cap) 15762 return 1; 15763 15764 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 15765 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 15766 } 15767 15768 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15769 int *max_cp) 15770 { 15771 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 15772 int max_ring_grps = 0, max_irq; 15773 15774 *max_tx = hw_resc->max_tx_rings; 15775 *max_rx = hw_resc->max_rx_rings; 15776 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 15777 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 15778 bnxt_get_ulp_msix_num_in_use(bp), 15779 hw_resc->max_stat_ctxs - 15780 bnxt_get_ulp_stat_ctxs_in_use(bp)); 15781 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 15782 *max_cp = min_t(int, *max_cp, max_irq); 15783 max_ring_grps = hw_resc->max_hw_ring_grps; 15784 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 15785 *max_cp -= 1; 15786 *max_rx -= 2; 15787 } 15788 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15789 *max_rx >>= 1; 15790 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 15791 int rc; 15792 15793 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 15794 if (rc) { 15795 *max_rx = 0; 15796 *max_tx = 0; 15797 } 15798 /* On P5 chips, max_cp output param should be available NQs */ 15799 *max_cp = max_irq; 15800 } 15801 *max_rx = min_t(int, *max_rx, max_ring_grps); 15802 } 15803 15804 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 15805 { 15806 int rx, tx, cp; 15807 15808 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 15809 *max_rx = rx; 15810 *max_tx = tx; 15811 if (!rx || !tx || !cp) 15812 return -ENOMEM; 15813 15814 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 15815 } 15816 15817 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15818 bool shared) 15819 { 15820 int rc; 15821 15822 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15823 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 15824 /* Not enough rings, try disabling agg rings. */ 15825 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 15826 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15827 if (rc) { 15828 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 15829 bp->flags |= BNXT_FLAG_AGG_RINGS; 15830 return rc; 15831 } 15832 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 15833 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15834 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15835 bnxt_set_ring_params(bp); 15836 } 15837 15838 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 15839 int max_cp, max_stat, max_irq; 15840 15841 /* Reserve minimum resources for RoCE */ 15842 max_cp = bnxt_get_max_func_cp_rings(bp); 15843 max_stat = bnxt_get_max_func_stat_ctxs(bp); 15844 max_irq = bnxt_get_max_func_irqs(bp); 15845 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 15846 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 15847 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 15848 return 0; 15849 15850 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 15851 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 15852 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 15853 max_cp = min_t(int, max_cp, max_irq); 15854 max_cp = min_t(int, max_cp, max_stat); 15855 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 15856 if (rc) 15857 rc = 0; 15858 } 15859 return rc; 15860 } 15861 15862 /* In initial default shared ring setting, each shared ring must have a 15863 * RX/TX ring pair. 15864 */ 15865 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 15866 { 15867 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 15868 bp->rx_nr_rings = bp->cp_nr_rings; 15869 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 15870 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15871 } 15872 15873 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 15874 { 15875 int dflt_rings, max_rx_rings, max_tx_rings, rc; 15876 int avail_msix; 15877 15878 if (!bnxt_can_reserve_rings(bp)) 15879 return 0; 15880 15881 if (sh) 15882 bp->flags |= BNXT_FLAG_SHARED_RINGS; 15883 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 15884 /* Reduce default rings on multi-port cards so that total default 15885 * rings do not exceed CPU count. 15886 */ 15887 if (bp->port_count > 1) { 15888 int max_rings = 15889 max_t(int, num_online_cpus() / bp->port_count, 1); 15890 15891 dflt_rings = min_t(int, dflt_rings, max_rings); 15892 } 15893 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 15894 if (rc) 15895 return rc; 15896 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 15897 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 15898 if (sh) 15899 bnxt_trim_dflt_sh_rings(bp); 15900 else 15901 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 15902 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15903 15904 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 15905 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 15906 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 15907 15908 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 15909 bnxt_set_dflt_ulp_stat_ctxs(bp); 15910 } 15911 15912 rc = __bnxt_reserve_rings(bp); 15913 if (rc && rc != -ENODEV) 15914 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 15915 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15916 if (sh) 15917 bnxt_trim_dflt_sh_rings(bp); 15918 15919 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 15920 if (bnxt_need_reserve_rings(bp)) { 15921 rc = __bnxt_reserve_rings(bp); 15922 if (rc && rc != -ENODEV) 15923 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 15924 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15925 } 15926 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 15927 bp->rx_nr_rings++; 15928 bp->cp_nr_rings++; 15929 } 15930 if (rc) { 15931 bp->tx_nr_rings = 0; 15932 bp->rx_nr_rings = 0; 15933 } 15934 return rc; 15935 } 15936 15937 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 15938 { 15939 int rc; 15940 15941 if (bp->tx_nr_rings) 15942 return 0; 15943 15944 bnxt_ulp_irq_stop(bp); 15945 bnxt_clear_int_mode(bp); 15946 rc = bnxt_set_dflt_rings(bp, true); 15947 if (rc) { 15948 if (BNXT_VF(bp) && rc == -ENODEV) 15949 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 15950 else 15951 netdev_err(bp->dev, "Not enough rings available.\n"); 15952 goto init_dflt_ring_err; 15953 } 15954 rc = bnxt_init_int_mode(bp); 15955 if (rc) 15956 goto init_dflt_ring_err; 15957 15958 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15959 15960 bnxt_set_dflt_rfs(bp); 15961 15962 init_dflt_ring_err: 15963 bnxt_ulp_irq_restart(bp, rc); 15964 return rc; 15965 } 15966 15967 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 15968 { 15969 int rc; 15970 15971 ASSERT_RTNL(); 15972 bnxt_hwrm_func_qcaps(bp); 15973 15974 if (netif_running(bp->dev)) 15975 __bnxt_close_nic(bp, true, false); 15976 15977 bnxt_ulp_irq_stop(bp); 15978 bnxt_clear_int_mode(bp); 15979 rc = bnxt_init_int_mode(bp); 15980 bnxt_ulp_irq_restart(bp, rc); 15981 15982 if (netif_running(bp->dev)) { 15983 if (rc) 15984 dev_close(bp->dev); 15985 else 15986 rc = bnxt_open_nic(bp, true, false); 15987 } 15988 15989 return rc; 15990 } 15991 15992 static int bnxt_init_mac_addr(struct bnxt *bp) 15993 { 15994 int rc = 0; 15995 15996 if (BNXT_PF(bp)) { 15997 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 15998 } else { 15999 #ifdef CONFIG_BNXT_SRIOV 16000 struct bnxt_vf_info *vf = &bp->vf; 16001 bool strict_approval = true; 16002 16003 if (is_valid_ether_addr(vf->mac_addr)) { 16004 /* overwrite netdev dev_addr with admin VF MAC */ 16005 eth_hw_addr_set(bp->dev, vf->mac_addr); 16006 /* Older PF driver or firmware may not approve this 16007 * correctly. 16008 */ 16009 strict_approval = false; 16010 } else { 16011 eth_hw_addr_random(bp->dev); 16012 } 16013 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16014 #endif 16015 } 16016 return rc; 16017 } 16018 16019 static void bnxt_vpd_read_info(struct bnxt *bp) 16020 { 16021 struct pci_dev *pdev = bp->pdev; 16022 unsigned int vpd_size, kw_len; 16023 int pos, size; 16024 u8 *vpd_data; 16025 16026 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16027 if (IS_ERR(vpd_data)) { 16028 pci_warn(pdev, "Unable to read VPD\n"); 16029 return; 16030 } 16031 16032 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16033 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16034 if (pos < 0) 16035 goto read_sn; 16036 16037 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16038 memcpy(bp->board_partno, &vpd_data[pos], size); 16039 16040 read_sn: 16041 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16042 PCI_VPD_RO_KEYWORD_SERIALNO, 16043 &kw_len); 16044 if (pos < 0) 16045 goto exit; 16046 16047 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16048 memcpy(bp->board_serialno, &vpd_data[pos], size); 16049 exit: 16050 kfree(vpd_data); 16051 } 16052 16053 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16054 { 16055 struct pci_dev *pdev = bp->pdev; 16056 u64 qword; 16057 16058 qword = pci_get_dsn(pdev); 16059 if (!qword) { 16060 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16061 return -EOPNOTSUPP; 16062 } 16063 16064 put_unaligned_le64(qword, dsn); 16065 16066 bp->flags |= BNXT_FLAG_DSN_VALID; 16067 return 0; 16068 } 16069 16070 static int bnxt_map_db_bar(struct bnxt *bp) 16071 { 16072 if (!bp->db_size) 16073 return -ENODEV; 16074 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16075 if (!bp->bar1) 16076 return -ENOMEM; 16077 return 0; 16078 } 16079 16080 void bnxt_print_device_info(struct bnxt *bp) 16081 { 16082 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16083 board_info[bp->board_idx].name, 16084 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16085 16086 pcie_print_link_status(bp->pdev); 16087 } 16088 16089 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16090 { 16091 struct bnxt_hw_resc *hw_resc; 16092 struct net_device *dev; 16093 struct bnxt *bp; 16094 int rc, max_irqs; 16095 16096 if (pci_is_bridge(pdev)) 16097 return -ENODEV; 16098 16099 if (!pdev->msix_cap) { 16100 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16101 return -ENODEV; 16102 } 16103 16104 /* Clear any pending DMA transactions from crash kernel 16105 * while loading driver in capture kernel. 16106 */ 16107 if (is_kdump_kernel()) { 16108 pci_clear_master(pdev); 16109 pcie_flr(pdev); 16110 } 16111 16112 max_irqs = bnxt_get_max_irq(pdev); 16113 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16114 max_irqs); 16115 if (!dev) 16116 return -ENOMEM; 16117 16118 bp = netdev_priv(dev); 16119 bp->board_idx = ent->driver_data; 16120 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16121 bnxt_set_max_func_irqs(bp, max_irqs); 16122 16123 if (bnxt_vf_pciid(bp->board_idx)) 16124 bp->flags |= BNXT_FLAG_VF; 16125 16126 /* No devlink port registration in case of a VF */ 16127 if (BNXT_PF(bp)) 16128 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16129 16130 rc = bnxt_init_board(pdev, dev); 16131 if (rc < 0) 16132 goto init_err_free; 16133 16134 dev->netdev_ops = &bnxt_netdev_ops; 16135 dev->stat_ops = &bnxt_stat_ops; 16136 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16137 dev->ethtool_ops = &bnxt_ethtool_ops; 16138 pci_set_drvdata(pdev, dev); 16139 16140 rc = bnxt_alloc_hwrm_resources(bp); 16141 if (rc) 16142 goto init_err_pci_clean; 16143 16144 mutex_init(&bp->hwrm_cmd_lock); 16145 mutex_init(&bp->link_lock); 16146 16147 rc = bnxt_fw_init_one_p1(bp); 16148 if (rc) 16149 goto init_err_pci_clean; 16150 16151 if (BNXT_PF(bp)) 16152 bnxt_vpd_read_info(bp); 16153 16154 if (BNXT_CHIP_P5_PLUS(bp)) { 16155 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16156 if (BNXT_CHIP_P7(bp)) 16157 bp->flags |= BNXT_FLAG_CHIP_P7; 16158 } 16159 16160 rc = bnxt_alloc_rss_indir_tbl(bp); 16161 if (rc) 16162 goto init_err_pci_clean; 16163 16164 rc = bnxt_fw_init_one_p2(bp); 16165 if (rc) 16166 goto init_err_pci_clean; 16167 16168 rc = bnxt_map_db_bar(bp); 16169 if (rc) { 16170 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16171 rc); 16172 goto init_err_pci_clean; 16173 } 16174 16175 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16176 NETIF_F_TSO | NETIF_F_TSO6 | 16177 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16178 NETIF_F_GSO_IPXIP4 | 16179 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16180 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16181 NETIF_F_RXCSUM | NETIF_F_GRO; 16182 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16183 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16184 16185 if (BNXT_SUPPORTS_TPA(bp)) 16186 dev->hw_features |= NETIF_F_LRO; 16187 16188 dev->hw_enc_features = 16189 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16190 NETIF_F_TSO | NETIF_F_TSO6 | 16191 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16192 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16193 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16194 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16195 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16196 if (bp->flags & BNXT_FLAG_CHIP_P7) 16197 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16198 else 16199 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16200 16201 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16202 NETIF_F_GSO_GRE_CSUM; 16203 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16204 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16205 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16206 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16207 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16208 if (BNXT_SUPPORTS_TPA(bp)) 16209 dev->hw_features |= NETIF_F_GRO_HW; 16210 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16211 if (dev->features & NETIF_F_GRO_HW) 16212 dev->features &= ~NETIF_F_LRO; 16213 dev->priv_flags |= IFF_UNICAST_FLT; 16214 16215 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16216 if (bp->tso_max_segs) 16217 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16218 16219 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16220 NETDEV_XDP_ACT_RX_SG; 16221 16222 #ifdef CONFIG_BNXT_SRIOV 16223 init_waitqueue_head(&bp->sriov_cfg_wait); 16224 #endif 16225 if (BNXT_SUPPORTS_TPA(bp)) { 16226 bp->gro_func = bnxt_gro_func_5730x; 16227 if (BNXT_CHIP_P4(bp)) 16228 bp->gro_func = bnxt_gro_func_5731x; 16229 else if (BNXT_CHIP_P5_PLUS(bp)) 16230 bp->gro_func = bnxt_gro_func_5750x; 16231 } 16232 if (!BNXT_CHIP_P4_PLUS(bp)) 16233 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16234 16235 rc = bnxt_init_mac_addr(bp); 16236 if (rc) { 16237 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16238 rc = -EADDRNOTAVAIL; 16239 goto init_err_pci_clean; 16240 } 16241 16242 if (BNXT_PF(bp)) { 16243 /* Read the adapter's DSN to use as the eswitch switch_id */ 16244 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16245 } 16246 16247 /* MTU range: 60 - FW defined max */ 16248 dev->min_mtu = ETH_ZLEN; 16249 dev->max_mtu = bp->max_mtu; 16250 16251 rc = bnxt_probe_phy(bp, true); 16252 if (rc) 16253 goto init_err_pci_clean; 16254 16255 hw_resc = &bp->hw_resc; 16256 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16257 BNXT_L2_FLTR_MAX_FLTR; 16258 /* Older firmware may not report these filters properly */ 16259 if (bp->max_fltr < BNXT_MAX_FLTR) 16260 bp->max_fltr = BNXT_MAX_FLTR; 16261 bnxt_init_l2_fltr_tbl(bp); 16262 bnxt_set_rx_skb_mode(bp, false); 16263 bnxt_set_tpa_flags(bp); 16264 bnxt_set_ring_params(bp); 16265 bnxt_rdma_aux_device_init(bp); 16266 rc = bnxt_set_dflt_rings(bp, true); 16267 if (rc) { 16268 if (BNXT_VF(bp) && rc == -ENODEV) { 16269 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16270 } else { 16271 netdev_err(bp->dev, "Not enough rings available.\n"); 16272 rc = -ENOMEM; 16273 } 16274 goto init_err_pci_clean; 16275 } 16276 16277 bnxt_fw_init_one_p3(bp); 16278 16279 bnxt_init_dflt_coal(bp); 16280 16281 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16282 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16283 16284 rc = bnxt_init_int_mode(bp); 16285 if (rc) 16286 goto init_err_pci_clean; 16287 16288 /* No TC has been set yet and rings may have been trimmed due to 16289 * limited MSIX, so we re-initialize the TX rings per TC. 16290 */ 16291 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16292 16293 if (BNXT_PF(bp)) { 16294 if (!bnxt_pf_wq) { 16295 bnxt_pf_wq = 16296 create_singlethread_workqueue("bnxt_pf_wq"); 16297 if (!bnxt_pf_wq) { 16298 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16299 rc = -ENOMEM; 16300 goto init_err_pci_clean; 16301 } 16302 } 16303 rc = bnxt_init_tc(bp); 16304 if (rc) 16305 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16306 rc); 16307 } 16308 16309 bnxt_inv_fw_health_reg(bp); 16310 rc = bnxt_dl_register(bp); 16311 if (rc) 16312 goto init_err_dl; 16313 16314 INIT_LIST_HEAD(&bp->usr_fltr_list); 16315 16316 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16317 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16318 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16319 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16320 16321 rc = register_netdev(dev); 16322 if (rc) 16323 goto init_err_cleanup; 16324 16325 bnxt_dl_fw_reporters_create(bp); 16326 16327 bnxt_rdma_aux_device_add(bp); 16328 16329 bnxt_print_device_info(bp); 16330 16331 pci_save_state(pdev); 16332 16333 return 0; 16334 init_err_cleanup: 16335 bnxt_rdma_aux_device_uninit(bp); 16336 bnxt_dl_unregister(bp); 16337 init_err_dl: 16338 bnxt_shutdown_tc(bp); 16339 bnxt_clear_int_mode(bp); 16340 16341 init_err_pci_clean: 16342 bnxt_hwrm_func_drv_unrgtr(bp); 16343 bnxt_free_hwrm_resources(bp); 16344 bnxt_hwmon_uninit(bp); 16345 bnxt_ethtool_free(bp); 16346 bnxt_ptp_clear(bp); 16347 kfree(bp->ptp_cfg); 16348 bp->ptp_cfg = NULL; 16349 kfree(bp->fw_health); 16350 bp->fw_health = NULL; 16351 bnxt_cleanup_pci(bp); 16352 bnxt_free_ctx_mem(bp, true); 16353 bnxt_free_crash_dump_mem(bp); 16354 kfree(bp->rss_indir_tbl); 16355 bp->rss_indir_tbl = NULL; 16356 16357 init_err_free: 16358 free_netdev(dev); 16359 return rc; 16360 } 16361 16362 static void bnxt_shutdown(struct pci_dev *pdev) 16363 { 16364 struct net_device *dev = pci_get_drvdata(pdev); 16365 struct bnxt *bp; 16366 16367 if (!dev) 16368 return; 16369 16370 rtnl_lock(); 16371 bp = netdev_priv(dev); 16372 if (!bp) 16373 goto shutdown_exit; 16374 16375 if (netif_running(dev)) 16376 dev_close(dev); 16377 16378 bnxt_ptp_clear(bp); 16379 bnxt_clear_int_mode(bp); 16380 pci_disable_device(pdev); 16381 16382 if (system_state == SYSTEM_POWER_OFF) { 16383 pci_wake_from_d3(pdev, bp->wol); 16384 pci_set_power_state(pdev, PCI_D3hot); 16385 } 16386 16387 shutdown_exit: 16388 rtnl_unlock(); 16389 } 16390 16391 #ifdef CONFIG_PM_SLEEP 16392 static int bnxt_suspend(struct device *device) 16393 { 16394 struct net_device *dev = dev_get_drvdata(device); 16395 struct bnxt *bp = netdev_priv(dev); 16396 int rc = 0; 16397 16398 bnxt_ulp_stop(bp); 16399 16400 rtnl_lock(); 16401 if (netif_running(dev)) { 16402 netif_device_detach(dev); 16403 rc = bnxt_close(dev); 16404 } 16405 bnxt_hwrm_func_drv_unrgtr(bp); 16406 bnxt_ptp_clear(bp); 16407 pci_disable_device(bp->pdev); 16408 bnxt_free_ctx_mem(bp, false); 16409 rtnl_unlock(); 16410 return rc; 16411 } 16412 16413 static int bnxt_resume(struct device *device) 16414 { 16415 struct net_device *dev = dev_get_drvdata(device); 16416 struct bnxt *bp = netdev_priv(dev); 16417 int rc = 0; 16418 16419 rtnl_lock(); 16420 rc = pci_enable_device(bp->pdev); 16421 if (rc) { 16422 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16423 rc); 16424 goto resume_exit; 16425 } 16426 pci_set_master(bp->pdev); 16427 if (bnxt_hwrm_ver_get(bp)) { 16428 rc = -ENODEV; 16429 goto resume_exit; 16430 } 16431 rc = bnxt_hwrm_func_reset(bp); 16432 if (rc) { 16433 rc = -EBUSY; 16434 goto resume_exit; 16435 } 16436 16437 rc = bnxt_hwrm_func_qcaps(bp); 16438 if (rc) 16439 goto resume_exit; 16440 16441 bnxt_clear_reservations(bp, true); 16442 16443 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16444 rc = -ENODEV; 16445 goto resume_exit; 16446 } 16447 if (bp->fw_crash_mem) 16448 bnxt_hwrm_crash_dump_mem_cfg(bp); 16449 16450 if (bnxt_ptp_init(bp)) { 16451 kfree(bp->ptp_cfg); 16452 bp->ptp_cfg = NULL; 16453 } 16454 bnxt_get_wol_settings(bp); 16455 if (netif_running(dev)) { 16456 rc = bnxt_open(dev); 16457 if (!rc) 16458 netif_device_attach(dev); 16459 } 16460 16461 resume_exit: 16462 rtnl_unlock(); 16463 bnxt_ulp_start(bp, rc); 16464 if (!rc) 16465 bnxt_reenable_sriov(bp); 16466 return rc; 16467 } 16468 16469 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16470 #define BNXT_PM_OPS (&bnxt_pm_ops) 16471 16472 #else 16473 16474 #define BNXT_PM_OPS NULL 16475 16476 #endif /* CONFIG_PM_SLEEP */ 16477 16478 /** 16479 * bnxt_io_error_detected - called when PCI error is detected 16480 * @pdev: Pointer to PCI device 16481 * @state: The current pci connection state 16482 * 16483 * This function is called after a PCI bus error affecting 16484 * this device has been detected. 16485 */ 16486 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16487 pci_channel_state_t state) 16488 { 16489 struct net_device *netdev = pci_get_drvdata(pdev); 16490 struct bnxt *bp = netdev_priv(netdev); 16491 bool abort = false; 16492 16493 netdev_info(netdev, "PCI I/O error detected\n"); 16494 16495 bnxt_ulp_stop(bp); 16496 16497 rtnl_lock(); 16498 netif_device_detach(netdev); 16499 16500 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16501 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16502 abort = true; 16503 } 16504 16505 if (abort || state == pci_channel_io_perm_failure) { 16506 rtnl_unlock(); 16507 return PCI_ERS_RESULT_DISCONNECT; 16508 } 16509 16510 /* Link is not reliable anymore if state is pci_channel_io_frozen 16511 * so we disable bus master to prevent any potential bad DMAs before 16512 * freeing kernel memory. 16513 */ 16514 if (state == pci_channel_io_frozen) { 16515 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16516 bnxt_fw_fatal_close(bp); 16517 } 16518 16519 if (netif_running(netdev)) 16520 __bnxt_close_nic(bp, true, true); 16521 16522 if (pci_is_enabled(pdev)) 16523 pci_disable_device(pdev); 16524 bnxt_free_ctx_mem(bp, false); 16525 rtnl_unlock(); 16526 16527 /* Request a slot slot reset. */ 16528 return PCI_ERS_RESULT_NEED_RESET; 16529 } 16530 16531 /** 16532 * bnxt_io_slot_reset - called after the pci bus has been reset. 16533 * @pdev: Pointer to PCI device 16534 * 16535 * Restart the card from scratch, as if from a cold-boot. 16536 * At this point, the card has experienced a hard reset, 16537 * followed by fixups by BIOS, and has its config space 16538 * set up identically to what it was at cold boot. 16539 */ 16540 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 16541 { 16542 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 16543 struct net_device *netdev = pci_get_drvdata(pdev); 16544 struct bnxt *bp = netdev_priv(netdev); 16545 int retry = 0; 16546 int err = 0; 16547 int off; 16548 16549 netdev_info(bp->dev, "PCI Slot Reset\n"); 16550 16551 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 16552 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 16553 msleep(900); 16554 16555 rtnl_lock(); 16556 16557 if (pci_enable_device(pdev)) { 16558 dev_err(&pdev->dev, 16559 "Cannot re-enable PCI device after reset.\n"); 16560 } else { 16561 pci_set_master(pdev); 16562 /* Upon fatal error, our device internal logic that latches to 16563 * BAR value is getting reset and will restore only upon 16564 * rewriting the BARs. 16565 * 16566 * As pci_restore_state() does not re-write the BARs if the 16567 * value is same as saved value earlier, driver needs to 16568 * write the BARs to 0 to force restore, in case of fatal error. 16569 */ 16570 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 16571 &bp->state)) { 16572 for (off = PCI_BASE_ADDRESS_0; 16573 off <= PCI_BASE_ADDRESS_5; off += 4) 16574 pci_write_config_dword(bp->pdev, off, 0); 16575 } 16576 pci_restore_state(pdev); 16577 pci_save_state(pdev); 16578 16579 bnxt_inv_fw_health_reg(bp); 16580 bnxt_try_map_fw_health_reg(bp); 16581 16582 /* In some PCIe AER scenarios, firmware may take up to 16583 * 10 seconds to become ready in the worst case. 16584 */ 16585 do { 16586 err = bnxt_try_recover_fw(bp); 16587 if (!err) 16588 break; 16589 retry++; 16590 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 16591 16592 if (err) { 16593 dev_err(&pdev->dev, "Firmware not ready\n"); 16594 goto reset_exit; 16595 } 16596 16597 err = bnxt_hwrm_func_reset(bp); 16598 if (!err) 16599 result = PCI_ERS_RESULT_RECOVERED; 16600 16601 bnxt_ulp_irq_stop(bp); 16602 bnxt_clear_int_mode(bp); 16603 err = bnxt_init_int_mode(bp); 16604 bnxt_ulp_irq_restart(bp, err); 16605 } 16606 16607 reset_exit: 16608 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16609 bnxt_clear_reservations(bp, true); 16610 rtnl_unlock(); 16611 16612 return result; 16613 } 16614 16615 /** 16616 * bnxt_io_resume - called when traffic can start flowing again. 16617 * @pdev: Pointer to PCI device 16618 * 16619 * This callback is called when the error recovery driver tells 16620 * us that its OK to resume normal operation. 16621 */ 16622 static void bnxt_io_resume(struct pci_dev *pdev) 16623 { 16624 struct net_device *netdev = pci_get_drvdata(pdev); 16625 struct bnxt *bp = netdev_priv(netdev); 16626 int err; 16627 16628 netdev_info(bp->dev, "PCI Slot Resume\n"); 16629 rtnl_lock(); 16630 16631 err = bnxt_hwrm_func_qcaps(bp); 16632 if (!err) { 16633 if (netif_running(netdev)) 16634 err = bnxt_open(netdev); 16635 else 16636 err = bnxt_reserve_rings(bp, true); 16637 } 16638 16639 if (!err) 16640 netif_device_attach(netdev); 16641 16642 rtnl_unlock(); 16643 bnxt_ulp_start(bp, err); 16644 if (!err) 16645 bnxt_reenable_sriov(bp); 16646 } 16647 16648 static const struct pci_error_handlers bnxt_err_handler = { 16649 .error_detected = bnxt_io_error_detected, 16650 .slot_reset = bnxt_io_slot_reset, 16651 .resume = bnxt_io_resume 16652 }; 16653 16654 static struct pci_driver bnxt_pci_driver = { 16655 .name = DRV_MODULE_NAME, 16656 .id_table = bnxt_pci_tbl, 16657 .probe = bnxt_init_one, 16658 .remove = bnxt_remove_one, 16659 .shutdown = bnxt_shutdown, 16660 .driver.pm = BNXT_PM_OPS, 16661 .err_handler = &bnxt_err_handler, 16662 #if defined(CONFIG_BNXT_SRIOV) 16663 .sriov_configure = bnxt_sriov_configure, 16664 #endif 16665 }; 16666 16667 static int __init bnxt_init(void) 16668 { 16669 int err; 16670 16671 bnxt_debug_init(); 16672 err = pci_register_driver(&bnxt_pci_driver); 16673 if (err) { 16674 bnxt_debug_exit(); 16675 return err; 16676 } 16677 16678 return 0; 16679 } 16680 16681 static void __exit bnxt_exit(void) 16682 { 16683 pci_unregister_driver(&bnxt_pci_driver); 16684 if (bnxt_pf_wq) 16685 destroy_workqueue(bnxt_pf_wq); 16686 bnxt_debug_exit(); 16687 } 16688 16689 module_init(bnxt_init); 16690 module_exit(bnxt_exit); 16691