1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2019 Broadcom Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation. 9 */ 10 11 #include <linux/module.h> 12 13 #include <linux/stringify.h> 14 #include <linux/kernel.h> 15 #include <linux/timer.h> 16 #include <linux/errno.h> 17 #include <linux/ioport.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/interrupt.h> 21 #include <linux/pci.h> 22 #include <linux/netdevice.h> 23 #include <linux/etherdevice.h> 24 #include <linux/skbuff.h> 25 #include <linux/dma-mapping.h> 26 #include <linux/bitops.h> 27 #include <linux/io.h> 28 #include <linux/irq.h> 29 #include <linux/delay.h> 30 #include <asm/byteorder.h> 31 #include <asm/page.h> 32 #include <linux/time.h> 33 #include <linux/mii.h> 34 #include <linux/mdio.h> 35 #include <linux/if.h> 36 #include <linux/if_vlan.h> 37 #include <linux/if_bridge.h> 38 #include <linux/rtc.h> 39 #include <linux/bpf.h> 40 #include <net/gro.h> 41 #include <net/ip.h> 42 #include <net/tcp.h> 43 #include <net/udp.h> 44 #include <net/checksum.h> 45 #include <net/ip6_checksum.h> 46 #include <net/udp_tunnel.h> 47 #include <linux/workqueue.h> 48 #include <linux/prefetch.h> 49 #include <linux/cache.h> 50 #include <linux/log2.h> 51 #include <linux/bitmap.h> 52 #include <linux/cpu_rmap.h> 53 #include <linux/cpumask.h> 54 #include <net/pkt_cls.h> 55 #include <net/page_pool/helpers.h> 56 #include <linux/align.h> 57 #include <net/netdev_lock.h> 58 #include <net/netdev_queues.h> 59 #include <net/netdev_rx_queue.h> 60 #include <linux/pci-tph.h> 61 62 #include "bnxt_hsi.h" 63 #include "bnxt.h" 64 #include "bnxt_hwrm.h" 65 #include "bnxt_ulp.h" 66 #include "bnxt_sriov.h" 67 #include "bnxt_ethtool.h" 68 #include "bnxt_dcb.h" 69 #include "bnxt_xdp.h" 70 #include "bnxt_ptp.h" 71 #include "bnxt_vfr.h" 72 #include "bnxt_tc.h" 73 #include "bnxt_devlink.h" 74 #include "bnxt_debugfs.h" 75 #include "bnxt_coredump.h" 76 #include "bnxt_hwmon.h" 77 78 #define BNXT_TX_TIMEOUT (5 * HZ) 79 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 80 NETIF_MSG_TX_ERR) 81 82 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 83 MODULE_LICENSE("GPL"); 84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 85 86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 88 89 #define BNXT_TX_PUSH_THRESH 164 90 91 /* indexed by enum board_idx */ 92 static const struct { 93 char *name; 94 } board_info[] = { 95 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 96 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 97 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 98 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 99 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 100 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 101 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 102 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 103 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 104 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 105 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 106 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 108 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 109 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 110 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 111 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 112 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 113 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 114 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 115 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 116 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 117 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 118 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 119 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 120 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 121 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 122 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 123 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 124 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 125 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 126 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 127 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 128 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 129 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 130 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 131 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 132 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 133 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 134 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 135 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 136 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 137 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 138 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 139 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 140 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 141 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 142 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 143 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 144 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 145 }; 146 147 static const struct pci_device_id bnxt_pci_tbl[] = { 148 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 149 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 150 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 151 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 152 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 153 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 154 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 155 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 156 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 157 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 158 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 159 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 160 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 163 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 164 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 165 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 166 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 167 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 168 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 169 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 170 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 171 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 172 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 173 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 175 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 179 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 180 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 181 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 182 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 183 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 184 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 185 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 186 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 187 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 188 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 189 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 190 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 191 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 193 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 194 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 195 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 196 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 197 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 198 #ifdef CONFIG_BNXT_SRIOV 199 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 200 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 201 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 202 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 203 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 205 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 206 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 207 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 208 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 209 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 210 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 211 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 212 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 215 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 216 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 217 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 218 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 219 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 220 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 221 #endif 222 { 0 } 223 }; 224 225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 226 227 static const u16 bnxt_vf_req_snif[] = { 228 HWRM_FUNC_CFG, 229 HWRM_FUNC_VF_CFG, 230 HWRM_PORT_PHY_QCFG, 231 HWRM_CFA_L2_FILTER_ALLOC, 232 }; 233 234 static const u16 bnxt_async_events_arr[] = { 235 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 236 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 237 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 238 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 239 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 240 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 241 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 242 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 243 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 244 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 245 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 246 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 247 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 248 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 249 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 250 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 251 ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER, 252 }; 253 254 const u16 bnxt_bstore_to_trace[] = { 255 [BNXT_CTX_SRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE, 256 [BNXT_CTX_SRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE, 257 [BNXT_CTX_CRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE, 258 [BNXT_CTX_CRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE, 259 [BNXT_CTX_RIGP0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE, 260 [BNXT_CTX_L2HWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE, 261 [BNXT_CTX_REHWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE, 262 [BNXT_CTX_CA0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE, 263 [BNXT_CTX_CA1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE, 264 [BNXT_CTX_CA2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE, 265 [BNXT_CTX_RIGP1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE, 266 }; 267 268 static struct workqueue_struct *bnxt_pf_wq; 269 270 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 271 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 272 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 273 274 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 275 .ports = { 276 .src = 0, 277 .dst = 0, 278 }, 279 .addrs = { 280 .v6addrs = { 281 .src = BNXT_IPV6_MASK_NONE, 282 .dst = BNXT_IPV6_MASK_NONE, 283 }, 284 }, 285 }; 286 287 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 288 .ports = { 289 .src = cpu_to_be16(0xffff), 290 .dst = cpu_to_be16(0xffff), 291 }, 292 .addrs = { 293 .v6addrs = { 294 .src = BNXT_IPV6_MASK_ALL, 295 .dst = BNXT_IPV6_MASK_ALL, 296 }, 297 }, 298 }; 299 300 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 301 .ports = { 302 .src = cpu_to_be16(0xffff), 303 .dst = cpu_to_be16(0xffff), 304 }, 305 .addrs = { 306 .v4addrs = { 307 .src = cpu_to_be32(0xffffffff), 308 .dst = cpu_to_be32(0xffffffff), 309 }, 310 }, 311 }; 312 313 static bool bnxt_vf_pciid(enum board_idx idx) 314 { 315 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 316 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 317 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 318 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF); 319 } 320 321 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 322 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 323 324 #define BNXT_DB_CQ(db, idx) \ 325 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 326 327 #define BNXT_DB_NQ_P5(db, idx) \ 328 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 329 (db)->doorbell) 330 331 #define BNXT_DB_NQ_P7(db, idx) \ 332 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 333 DB_RING_IDX(db, idx), (db)->doorbell) 334 335 #define BNXT_DB_CQ_ARM(db, idx) \ 336 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 337 338 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 339 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 340 DB_RING_IDX(db, idx), (db)->doorbell) 341 342 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 343 { 344 if (bp->flags & BNXT_FLAG_CHIP_P7) 345 BNXT_DB_NQ_P7(db, idx); 346 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 347 BNXT_DB_NQ_P5(db, idx); 348 else 349 BNXT_DB_CQ(db, idx); 350 } 351 352 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 353 { 354 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 355 BNXT_DB_NQ_ARM_P5(db, idx); 356 else 357 BNXT_DB_CQ_ARM(db, idx); 358 } 359 360 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 361 { 362 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 363 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 364 DB_RING_IDX(db, idx), db->doorbell); 365 else 366 BNXT_DB_CQ(db, idx); 367 } 368 369 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 370 { 371 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 372 return; 373 374 if (BNXT_PF(bp)) 375 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 376 else 377 schedule_delayed_work(&bp->fw_reset_task, delay); 378 } 379 380 static void __bnxt_queue_sp_work(struct bnxt *bp) 381 { 382 if (BNXT_PF(bp)) 383 queue_work(bnxt_pf_wq, &bp->sp_task); 384 else 385 schedule_work(&bp->sp_task); 386 } 387 388 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 389 { 390 set_bit(event, &bp->sp_event); 391 __bnxt_queue_sp_work(bp); 392 } 393 394 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 395 { 396 if (!rxr->bnapi->in_reset) { 397 rxr->bnapi->in_reset = true; 398 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 399 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 400 else 401 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 402 __bnxt_queue_sp_work(bp); 403 } 404 rxr->rx_next_cons = 0xffff; 405 } 406 407 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 408 u16 curr) 409 { 410 struct bnxt_napi *bnapi = txr->bnapi; 411 412 if (bnapi->tx_fault) 413 return; 414 415 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 416 txr->txq_index, txr->tx_hw_cons, 417 txr->tx_cons, txr->tx_prod, curr); 418 WARN_ON_ONCE(1); 419 bnapi->tx_fault = 1; 420 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 421 } 422 423 const u16 bnxt_lhint_arr[] = { 424 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 425 TX_BD_FLAGS_LHINT_512_TO_1023, 426 TX_BD_FLAGS_LHINT_1024_TO_2047, 427 TX_BD_FLAGS_LHINT_1024_TO_2047, 428 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 429 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 430 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 431 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 432 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 433 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 434 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 435 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 436 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 437 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 438 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 439 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 440 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 441 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 442 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 443 }; 444 445 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 446 { 447 struct metadata_dst *md_dst = skb_metadata_dst(skb); 448 449 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 450 return 0; 451 452 return md_dst->u.port_info.port_id; 453 } 454 455 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 456 u16 prod) 457 { 458 /* Sync BD data before updating doorbell */ 459 wmb(); 460 bnxt_db_write(bp, &txr->tx_db, prod); 461 txr->kick_pending = 0; 462 } 463 464 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 465 { 466 struct bnxt *bp = netdev_priv(dev); 467 struct tx_bd *txbd, *txbd0; 468 struct tx_bd_ext *txbd1; 469 struct netdev_queue *txq; 470 int i; 471 dma_addr_t mapping; 472 unsigned int length, pad = 0; 473 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 474 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 475 struct pci_dev *pdev = bp->pdev; 476 u16 prod, last_frag, txts_prod; 477 struct bnxt_tx_ring_info *txr; 478 struct bnxt_sw_tx_bd *tx_buf; 479 __le32 lflags = 0; 480 skb_frag_t *frag; 481 482 i = skb_get_queue_mapping(skb); 483 if (unlikely(i >= bp->tx_nr_rings)) { 484 dev_kfree_skb_any(skb); 485 dev_core_stats_tx_dropped_inc(dev); 486 return NETDEV_TX_OK; 487 } 488 489 txq = netdev_get_tx_queue(dev, i); 490 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 491 prod = txr->tx_prod; 492 493 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS) 494 if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) { 495 netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d. SKB will be linearized.\n", 496 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS); 497 if (skb_linearize(skb)) { 498 dev_kfree_skb_any(skb); 499 dev_core_stats_tx_dropped_inc(dev); 500 return NETDEV_TX_OK; 501 } 502 } 503 #endif 504 free_size = bnxt_tx_avail(bp, txr); 505 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 506 /* We must have raced with NAPI cleanup */ 507 if (net_ratelimit() && txr->kick_pending) 508 netif_warn(bp, tx_err, dev, 509 "bnxt: ring busy w/ flush pending!\n"); 510 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 511 bp->tx_wake_thresh)) 512 return NETDEV_TX_BUSY; 513 } 514 515 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 516 goto tx_free; 517 518 length = skb->len; 519 len = skb_headlen(skb); 520 last_frag = skb_shinfo(skb)->nr_frags; 521 522 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 523 524 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 525 tx_buf->skb = skb; 526 tx_buf->nr_frags = last_frag; 527 528 vlan_tag_flags = 0; 529 cfa_action = bnxt_xmit_get_cfa_action(skb); 530 if (skb_vlan_tag_present(skb)) { 531 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 532 skb_vlan_tag_get(skb); 533 /* Currently supports 8021Q, 8021AD vlan offloads 534 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 535 */ 536 if (skb->vlan_proto == htons(ETH_P_8021Q)) 537 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 538 } 539 540 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 541 ptp->tx_tstamp_en) { 542 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 543 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 544 tx_buf->is_ts_pkt = 1; 545 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 546 } else if (!skb_is_gso(skb)) { 547 u16 seq_id, hdr_off; 548 549 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 550 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 551 if (vlan_tag_flags) 552 hdr_off += VLAN_HLEN; 553 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 554 tx_buf->is_ts_pkt = 1; 555 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 556 557 ptp->txts_req[txts_prod].tx_seqid = seq_id; 558 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 559 tx_buf->txts_prod = txts_prod; 560 } 561 } 562 } 563 if (unlikely(skb->no_fcs)) 564 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 565 566 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 567 skb_frags_readable(skb) && !lflags) { 568 struct tx_push_buffer *tx_push_buf = txr->tx_push; 569 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 570 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 571 void __iomem *db = txr->tx_db.doorbell; 572 void *pdata = tx_push_buf->data; 573 u64 *end; 574 int j, push_len; 575 576 /* Set COAL_NOW to be ready quickly for the next push */ 577 tx_push->tx_bd_len_flags_type = 578 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 579 TX_BD_TYPE_LONG_TX_BD | 580 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 581 TX_BD_FLAGS_COAL_NOW | 582 TX_BD_FLAGS_PACKET_END | 583 TX_BD_CNT(2)); 584 585 if (skb->ip_summed == CHECKSUM_PARTIAL) 586 tx_push1->tx_bd_hsize_lflags = 587 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 588 else 589 tx_push1->tx_bd_hsize_lflags = 0; 590 591 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 592 tx_push1->tx_bd_cfa_action = 593 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 594 595 end = pdata + length; 596 end = PTR_ALIGN(end, 8) - 1; 597 *end = 0; 598 599 skb_copy_from_linear_data(skb, pdata, len); 600 pdata += len; 601 for (j = 0; j < last_frag; j++) { 602 void *fptr; 603 604 frag = &skb_shinfo(skb)->frags[j]; 605 fptr = skb_frag_address_safe(frag); 606 if (!fptr) 607 goto normal_tx; 608 609 memcpy(pdata, fptr, skb_frag_size(frag)); 610 pdata += skb_frag_size(frag); 611 } 612 613 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 614 txbd->tx_bd_haddr = txr->data_mapping; 615 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 616 prod = NEXT_TX(prod); 617 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 618 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 619 memcpy(txbd, tx_push1, sizeof(*txbd)); 620 prod = NEXT_TX(prod); 621 tx_push->doorbell = 622 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 623 DB_RING_IDX(&txr->tx_db, prod)); 624 WRITE_ONCE(txr->tx_prod, prod); 625 626 tx_buf->is_push = 1; 627 netdev_tx_sent_queue(txq, skb->len); 628 wmb(); /* Sync is_push and byte queue before pushing data */ 629 630 push_len = (length + sizeof(*tx_push) + 7) / 8; 631 if (push_len > 16) { 632 __iowrite64_copy(db, tx_push_buf, 16); 633 __iowrite32_copy(db + 4, tx_push_buf + 1, 634 (push_len - 16) << 1); 635 } else { 636 __iowrite64_copy(db, tx_push_buf, push_len); 637 } 638 639 goto tx_done; 640 } 641 642 normal_tx: 643 if (length < BNXT_MIN_PKT_SIZE) { 644 pad = BNXT_MIN_PKT_SIZE - length; 645 if (skb_pad(skb, pad)) 646 /* SKB already freed. */ 647 goto tx_kick_pending; 648 length = BNXT_MIN_PKT_SIZE; 649 } 650 651 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 652 653 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 654 goto tx_free; 655 656 dma_unmap_addr_set(tx_buf, mapping, mapping); 657 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 658 TX_BD_CNT(last_frag + 2); 659 660 txbd->tx_bd_haddr = cpu_to_le64(mapping); 661 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 662 663 prod = NEXT_TX(prod); 664 txbd1 = (struct tx_bd_ext *) 665 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 666 667 txbd1->tx_bd_hsize_lflags = lflags; 668 if (skb_is_gso(skb)) { 669 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 670 u32 hdr_len; 671 672 if (skb->encapsulation) { 673 if (udp_gso) 674 hdr_len = skb_inner_transport_offset(skb) + 675 sizeof(struct udphdr); 676 else 677 hdr_len = skb_inner_tcp_all_headers(skb); 678 } else if (udp_gso) { 679 hdr_len = skb_transport_offset(skb) + 680 sizeof(struct udphdr); 681 } else { 682 hdr_len = skb_tcp_all_headers(skb); 683 } 684 685 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 686 TX_BD_FLAGS_T_IPID | 687 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 688 length = skb_shinfo(skb)->gso_size; 689 txbd1->tx_bd_mss = cpu_to_le32(length); 690 length += hdr_len; 691 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 692 txbd1->tx_bd_hsize_lflags |= 693 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 694 txbd1->tx_bd_mss = 0; 695 } 696 697 length >>= 9; 698 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 699 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 700 skb->len); 701 i = 0; 702 goto tx_dma_error; 703 } 704 flags |= bnxt_lhint_arr[length]; 705 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 706 707 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 708 txbd1->tx_bd_cfa_action = 709 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 710 txbd0 = txbd; 711 for (i = 0; i < last_frag; i++) { 712 frag = &skb_shinfo(skb)->frags[i]; 713 prod = NEXT_TX(prod); 714 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 715 716 len = skb_frag_size(frag); 717 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 718 DMA_TO_DEVICE); 719 720 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 721 goto tx_dma_error; 722 723 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 724 netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf, 725 mapping, mapping); 726 727 txbd->tx_bd_haddr = cpu_to_le64(mapping); 728 729 flags = len << TX_BD_LEN_SHIFT; 730 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 731 } 732 733 flags &= ~TX_BD_LEN; 734 txbd->tx_bd_len_flags_type = 735 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 736 TX_BD_FLAGS_PACKET_END); 737 738 netdev_tx_sent_queue(txq, skb->len); 739 740 skb_tx_timestamp(skb); 741 742 prod = NEXT_TX(prod); 743 WRITE_ONCE(txr->tx_prod, prod); 744 745 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 746 bnxt_txr_db_kick(bp, txr, prod); 747 } else { 748 if (free_size >= bp->tx_wake_thresh) 749 txbd0->tx_bd_len_flags_type |= 750 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 751 txr->kick_pending = 1; 752 } 753 754 tx_done: 755 756 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 757 if (netdev_xmit_more() && !tx_buf->is_push) { 758 txbd0->tx_bd_len_flags_type &= 759 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 760 bnxt_txr_db_kick(bp, txr, prod); 761 } 762 763 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 764 bp->tx_wake_thresh); 765 } 766 return NETDEV_TX_OK; 767 768 tx_dma_error: 769 last_frag = i; 770 771 /* start back at beginning and unmap skb */ 772 prod = txr->tx_prod; 773 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 774 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 775 skb_headlen(skb), DMA_TO_DEVICE); 776 prod = NEXT_TX(prod); 777 778 /* unmap remaining mapped pages */ 779 for (i = 0; i < last_frag; i++) { 780 prod = NEXT_TX(prod); 781 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 782 frag = &skb_shinfo(skb)->frags[i]; 783 netmem_dma_unmap_page_attrs(&pdev->dev, 784 dma_unmap_addr(tx_buf, mapping), 785 skb_frag_size(frag), 786 DMA_TO_DEVICE, 0); 787 } 788 789 tx_free: 790 dev_kfree_skb_any(skb); 791 tx_kick_pending: 792 if (BNXT_TX_PTP_IS_SET(lflags)) { 793 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0; 794 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 795 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 796 /* set SKB to err so PTP worker will clean up */ 797 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 798 } 799 if (txr->kick_pending) 800 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 801 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL; 802 dev_core_stats_tx_dropped_inc(dev); 803 return NETDEV_TX_OK; 804 } 805 806 /* Returns true if some remaining TX packets not processed. */ 807 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 808 int budget) 809 { 810 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 811 struct pci_dev *pdev = bp->pdev; 812 u16 hw_cons = txr->tx_hw_cons; 813 unsigned int tx_bytes = 0; 814 u16 cons = txr->tx_cons; 815 skb_frag_t *frag; 816 int tx_pkts = 0; 817 bool rc = false; 818 819 while (RING_TX(bp, cons) != hw_cons) { 820 struct bnxt_sw_tx_bd *tx_buf; 821 struct sk_buff *skb; 822 bool is_ts_pkt; 823 int j, last; 824 825 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 826 skb = tx_buf->skb; 827 828 if (unlikely(!skb)) { 829 bnxt_sched_reset_txr(bp, txr, cons); 830 return rc; 831 } 832 833 is_ts_pkt = tx_buf->is_ts_pkt; 834 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 835 rc = true; 836 break; 837 } 838 839 cons = NEXT_TX(cons); 840 tx_pkts++; 841 tx_bytes += skb->len; 842 tx_buf->skb = NULL; 843 tx_buf->is_ts_pkt = 0; 844 845 if (tx_buf->is_push) { 846 tx_buf->is_push = 0; 847 goto next_tx_int; 848 } 849 850 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 851 skb_headlen(skb), DMA_TO_DEVICE); 852 last = tx_buf->nr_frags; 853 854 for (j = 0; j < last; j++) { 855 frag = &skb_shinfo(skb)->frags[j]; 856 cons = NEXT_TX(cons); 857 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 858 netmem_dma_unmap_page_attrs(&pdev->dev, 859 dma_unmap_addr(tx_buf, 860 mapping), 861 skb_frag_size(frag), 862 DMA_TO_DEVICE, 0); 863 } 864 if (unlikely(is_ts_pkt)) { 865 if (BNXT_CHIP_P5(bp)) { 866 /* PTP worker takes ownership of the skb */ 867 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 868 skb = NULL; 869 } 870 } 871 872 next_tx_int: 873 cons = NEXT_TX(cons); 874 875 dev_consume_skb_any(skb); 876 } 877 878 WRITE_ONCE(txr->tx_cons, cons); 879 880 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 881 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 882 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 883 884 return rc; 885 } 886 887 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 888 { 889 struct bnxt_tx_ring_info *txr; 890 bool more = false; 891 int i; 892 893 bnxt_for_each_napi_tx(i, bnapi, txr) { 894 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 895 more |= __bnxt_tx_int(bp, txr, budget); 896 } 897 if (!more) 898 bnapi->events &= ~BNXT_TX_CMP_EVENT; 899 } 900 901 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr) 902 { 903 return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE; 904 } 905 906 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 907 struct bnxt_rx_ring_info *rxr, 908 unsigned int *offset, 909 gfp_t gfp) 910 { 911 struct page *page; 912 913 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 914 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 915 BNXT_RX_PAGE_SIZE); 916 } else { 917 page = page_pool_dev_alloc_pages(rxr->page_pool); 918 *offset = 0; 919 } 920 if (!page) 921 return NULL; 922 923 *mapping = page_pool_get_dma_addr(page) + *offset; 924 return page; 925 } 926 927 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping, 928 struct bnxt_rx_ring_info *rxr, 929 gfp_t gfp) 930 { 931 netmem_ref netmem; 932 933 netmem = page_pool_alloc_netmems(rxr->page_pool, gfp); 934 if (!netmem) 935 return 0; 936 937 *mapping = page_pool_get_dma_addr_netmem(netmem); 938 return netmem; 939 } 940 941 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 942 struct bnxt_rx_ring_info *rxr, 943 gfp_t gfp) 944 { 945 unsigned int offset; 946 struct page *page; 947 948 page = page_pool_alloc_frag(rxr->head_pool, &offset, 949 bp->rx_buf_size, gfp); 950 if (!page) 951 return NULL; 952 953 *mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset; 954 return page_address(page) + offset; 955 } 956 957 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 958 u16 prod, gfp_t gfp) 959 { 960 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 961 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 962 dma_addr_t mapping; 963 964 if (BNXT_RX_PAGE_MODE(bp)) { 965 unsigned int offset; 966 struct page *page = 967 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 968 969 if (!page) 970 return -ENOMEM; 971 972 mapping += bp->rx_dma_offset; 973 rx_buf->data = page; 974 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 975 } else { 976 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp); 977 978 if (!data) 979 return -ENOMEM; 980 981 rx_buf->data = data; 982 rx_buf->data_ptr = data + bp->rx_offset; 983 } 984 rx_buf->mapping = mapping; 985 986 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 987 return 0; 988 } 989 990 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 991 { 992 u16 prod = rxr->rx_prod; 993 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 994 struct bnxt *bp = rxr->bnapi->bp; 995 struct rx_bd *cons_bd, *prod_bd; 996 997 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 998 cons_rx_buf = &rxr->rx_buf_ring[cons]; 999 1000 prod_rx_buf->data = data; 1001 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 1002 1003 prod_rx_buf->mapping = cons_rx_buf->mapping; 1004 1005 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1006 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 1007 1008 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 1009 } 1010 1011 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1012 { 1013 u16 next, max = rxr->rx_agg_bmap_size; 1014 1015 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 1016 if (next >= max) 1017 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 1018 return next; 1019 } 1020 1021 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1022 u16 prod, gfp_t gfp) 1023 { 1024 struct rx_bd *rxbd = 1025 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1026 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 1027 u16 sw_prod = rxr->rx_sw_agg_prod; 1028 unsigned int offset = 0; 1029 dma_addr_t mapping; 1030 netmem_ref netmem; 1031 1032 netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, gfp); 1033 if (!netmem) 1034 return -ENOMEM; 1035 1036 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1037 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1038 1039 __set_bit(sw_prod, rxr->rx_agg_bmap); 1040 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1041 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1042 1043 rx_agg_buf->netmem = netmem; 1044 rx_agg_buf->offset = offset; 1045 rx_agg_buf->mapping = mapping; 1046 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1047 rxbd->rx_bd_opaque = sw_prod; 1048 return 0; 1049 } 1050 1051 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1052 struct bnxt_cp_ring_info *cpr, 1053 u16 cp_cons, u16 curr) 1054 { 1055 struct rx_agg_cmp *agg; 1056 1057 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1058 agg = (struct rx_agg_cmp *) 1059 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1060 return agg; 1061 } 1062 1063 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1064 struct bnxt_rx_ring_info *rxr, 1065 u16 agg_id, u16 curr) 1066 { 1067 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1068 1069 return &tpa_info->agg_arr[curr]; 1070 } 1071 1072 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1073 u16 start, u32 agg_bufs, bool tpa) 1074 { 1075 struct bnxt_napi *bnapi = cpr->bnapi; 1076 struct bnxt *bp = bnapi->bp; 1077 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1078 u16 prod = rxr->rx_agg_prod; 1079 u16 sw_prod = rxr->rx_sw_agg_prod; 1080 bool p5_tpa = false; 1081 u32 i; 1082 1083 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1084 p5_tpa = true; 1085 1086 for (i = 0; i < agg_bufs; i++) { 1087 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1088 struct rx_agg_cmp *agg; 1089 struct rx_bd *prod_bd; 1090 netmem_ref netmem; 1091 u16 cons; 1092 1093 if (p5_tpa) 1094 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1095 else 1096 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1097 cons = agg->rx_agg_cmp_opaque; 1098 __clear_bit(cons, rxr->rx_agg_bmap); 1099 1100 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1101 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1102 1103 __set_bit(sw_prod, rxr->rx_agg_bmap); 1104 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1105 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1106 1107 /* It is possible for sw_prod to be equal to cons, so 1108 * set cons_rx_buf->netmem to 0 first. 1109 */ 1110 netmem = cons_rx_buf->netmem; 1111 cons_rx_buf->netmem = 0; 1112 prod_rx_buf->netmem = netmem; 1113 prod_rx_buf->offset = cons_rx_buf->offset; 1114 1115 prod_rx_buf->mapping = cons_rx_buf->mapping; 1116 1117 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1118 1119 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1120 prod_bd->rx_bd_opaque = sw_prod; 1121 1122 prod = NEXT_RX_AGG(prod); 1123 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1124 } 1125 rxr->rx_agg_prod = prod; 1126 rxr->rx_sw_agg_prod = sw_prod; 1127 } 1128 1129 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1130 struct bnxt_rx_ring_info *rxr, 1131 u16 cons, void *data, u8 *data_ptr, 1132 dma_addr_t dma_addr, 1133 unsigned int offset_and_len) 1134 { 1135 unsigned int len = offset_and_len & 0xffff; 1136 struct page *page = data; 1137 u16 prod = rxr->rx_prod; 1138 struct sk_buff *skb; 1139 int err; 1140 1141 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1142 if (unlikely(err)) { 1143 bnxt_reuse_rx_data(rxr, cons, data); 1144 return NULL; 1145 } 1146 dma_addr -= bp->rx_dma_offset; 1147 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1148 bp->rx_dir); 1149 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1150 if (!skb) { 1151 page_pool_recycle_direct(rxr->page_pool, page); 1152 return NULL; 1153 } 1154 skb_mark_for_recycle(skb); 1155 skb_reserve(skb, bp->rx_offset); 1156 __skb_put(skb, len); 1157 1158 return skb; 1159 } 1160 1161 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1162 struct bnxt_rx_ring_info *rxr, 1163 u16 cons, void *data, u8 *data_ptr, 1164 dma_addr_t dma_addr, 1165 unsigned int offset_and_len) 1166 { 1167 unsigned int payload = offset_and_len >> 16; 1168 unsigned int len = offset_and_len & 0xffff; 1169 skb_frag_t *frag; 1170 struct page *page = data; 1171 u16 prod = rxr->rx_prod; 1172 struct sk_buff *skb; 1173 int off, err; 1174 1175 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1176 if (unlikely(err)) { 1177 bnxt_reuse_rx_data(rxr, cons, data); 1178 return NULL; 1179 } 1180 dma_addr -= bp->rx_dma_offset; 1181 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1182 bp->rx_dir); 1183 1184 if (unlikely(!payload)) 1185 payload = eth_get_headlen(bp->dev, data_ptr, len); 1186 1187 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1188 if (!skb) { 1189 page_pool_recycle_direct(rxr->page_pool, page); 1190 return NULL; 1191 } 1192 1193 skb_mark_for_recycle(skb); 1194 off = (void *)data_ptr - page_address(page); 1195 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1196 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1197 payload + NET_IP_ALIGN); 1198 1199 frag = &skb_shinfo(skb)->frags[0]; 1200 skb_frag_size_sub(frag, payload); 1201 skb_frag_off_add(frag, payload); 1202 skb->data_len -= payload; 1203 skb->tail += payload; 1204 1205 return skb; 1206 } 1207 1208 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1209 struct bnxt_rx_ring_info *rxr, u16 cons, 1210 void *data, u8 *data_ptr, 1211 dma_addr_t dma_addr, 1212 unsigned int offset_and_len) 1213 { 1214 u16 prod = rxr->rx_prod; 1215 struct sk_buff *skb; 1216 int err; 1217 1218 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1219 if (unlikely(err)) { 1220 bnxt_reuse_rx_data(rxr, cons, data); 1221 return NULL; 1222 } 1223 1224 skb = napi_build_skb(data, bp->rx_buf_size); 1225 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1226 bp->rx_dir); 1227 if (!skb) { 1228 page_pool_free_va(rxr->head_pool, data, true); 1229 return NULL; 1230 } 1231 1232 skb_mark_for_recycle(skb); 1233 skb_reserve(skb, bp->rx_offset); 1234 skb_put(skb, offset_and_len & 0xffff); 1235 return skb; 1236 } 1237 1238 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp, 1239 struct bnxt_cp_ring_info *cpr, 1240 u16 idx, u32 agg_bufs, bool tpa, 1241 struct sk_buff *skb, 1242 struct xdp_buff *xdp) 1243 { 1244 struct bnxt_napi *bnapi = cpr->bnapi; 1245 struct skb_shared_info *shinfo; 1246 struct bnxt_rx_ring_info *rxr; 1247 u32 i, total_frag_len = 0; 1248 bool p5_tpa = false; 1249 u16 prod; 1250 1251 rxr = bnapi->rx_ring; 1252 prod = rxr->rx_agg_prod; 1253 1254 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1255 p5_tpa = true; 1256 1257 if (skb) 1258 shinfo = skb_shinfo(skb); 1259 else 1260 shinfo = xdp_get_shared_info_from_buff(xdp); 1261 1262 for (i = 0; i < agg_bufs; i++) { 1263 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1264 struct rx_agg_cmp *agg; 1265 u16 cons, frag_len; 1266 netmem_ref netmem; 1267 1268 if (p5_tpa) 1269 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1270 else 1271 agg = bnxt_get_agg(bp, cpr, idx, i); 1272 cons = agg->rx_agg_cmp_opaque; 1273 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1274 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1275 1276 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1277 if (skb) { 1278 skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem, 1279 cons_rx_buf->offset, 1280 frag_len, BNXT_RX_PAGE_SIZE); 1281 } else { 1282 skb_frag_t *frag = &shinfo->frags[i]; 1283 1284 skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem, 1285 cons_rx_buf->offset, 1286 frag_len); 1287 shinfo->nr_frags = i + 1; 1288 } 1289 __clear_bit(cons, rxr->rx_agg_bmap); 1290 1291 /* It is possible for bnxt_alloc_rx_netmem() to allocate 1292 * a sw_prod index that equals the cons index, so we 1293 * need to clear the cons entry now. 1294 */ 1295 netmem = cons_rx_buf->netmem; 1296 cons_rx_buf->netmem = 0; 1297 1298 if (xdp && netmem_is_pfmemalloc(netmem)) 1299 xdp_buff_set_frag_pfmemalloc(xdp); 1300 1301 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) { 1302 if (skb) { 1303 skb->len -= frag_len; 1304 skb->data_len -= frag_len; 1305 skb->truesize -= BNXT_RX_PAGE_SIZE; 1306 } 1307 1308 --shinfo->nr_frags; 1309 cons_rx_buf->netmem = netmem; 1310 1311 /* Update prod since possibly some netmems have been 1312 * allocated already. 1313 */ 1314 rxr->rx_agg_prod = prod; 1315 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1316 return 0; 1317 } 1318 1319 page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0, 1320 BNXT_RX_PAGE_SIZE); 1321 1322 total_frag_len += frag_len; 1323 prod = NEXT_RX_AGG(prod); 1324 } 1325 rxr->rx_agg_prod = prod; 1326 return total_frag_len; 1327 } 1328 1329 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp, 1330 struct bnxt_cp_ring_info *cpr, 1331 struct sk_buff *skb, u16 idx, 1332 u32 agg_bufs, bool tpa) 1333 { 1334 u32 total_frag_len = 0; 1335 1336 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1337 skb, NULL); 1338 if (!total_frag_len) { 1339 skb_mark_for_recycle(skb); 1340 dev_kfree_skb(skb); 1341 return NULL; 1342 } 1343 1344 return skb; 1345 } 1346 1347 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp, 1348 struct bnxt_cp_ring_info *cpr, 1349 struct xdp_buff *xdp, u16 idx, 1350 u32 agg_bufs, bool tpa) 1351 { 1352 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1353 u32 total_frag_len = 0; 1354 1355 if (!xdp_buff_has_frags(xdp)) 1356 shinfo->nr_frags = 0; 1357 1358 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1359 NULL, xdp); 1360 if (total_frag_len) { 1361 xdp_buff_set_frags_flag(xdp); 1362 shinfo->nr_frags = agg_bufs; 1363 shinfo->xdp_frags_size = total_frag_len; 1364 } 1365 return total_frag_len; 1366 } 1367 1368 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1369 u8 agg_bufs, u32 *raw_cons) 1370 { 1371 u16 last; 1372 struct rx_agg_cmp *agg; 1373 1374 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1375 last = RING_CMP(*raw_cons); 1376 agg = (struct rx_agg_cmp *) 1377 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1378 return RX_AGG_CMP_VALID(agg, *raw_cons); 1379 } 1380 1381 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1382 unsigned int len, 1383 dma_addr_t mapping) 1384 { 1385 struct bnxt *bp = bnapi->bp; 1386 struct pci_dev *pdev = bp->pdev; 1387 struct sk_buff *skb; 1388 1389 skb = napi_alloc_skb(&bnapi->napi, len); 1390 if (!skb) 1391 return NULL; 1392 1393 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak, 1394 bp->rx_dir); 1395 1396 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1397 len + NET_IP_ALIGN); 1398 1399 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak, 1400 bp->rx_dir); 1401 1402 skb_put(skb, len); 1403 1404 return skb; 1405 } 1406 1407 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1408 unsigned int len, 1409 dma_addr_t mapping) 1410 { 1411 return bnxt_copy_data(bnapi, data, len, mapping); 1412 } 1413 1414 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1415 struct xdp_buff *xdp, 1416 unsigned int len, 1417 dma_addr_t mapping) 1418 { 1419 unsigned int metasize = 0; 1420 u8 *data = xdp->data; 1421 struct sk_buff *skb; 1422 1423 len = xdp->data_end - xdp->data_meta; 1424 metasize = xdp->data - xdp->data_meta; 1425 data = xdp->data_meta; 1426 1427 skb = bnxt_copy_data(bnapi, data, len, mapping); 1428 if (!skb) 1429 return skb; 1430 1431 if (metasize) { 1432 skb_metadata_set(skb, metasize); 1433 __skb_pull(skb, metasize); 1434 } 1435 1436 return skb; 1437 } 1438 1439 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1440 u32 *raw_cons, void *cmp) 1441 { 1442 struct rx_cmp *rxcmp = cmp; 1443 u32 tmp_raw_cons = *raw_cons; 1444 u8 cmp_type, agg_bufs = 0; 1445 1446 cmp_type = RX_CMP_TYPE(rxcmp); 1447 1448 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1449 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1450 RX_CMP_AGG_BUFS) >> 1451 RX_CMP_AGG_BUFS_SHIFT; 1452 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1453 struct rx_tpa_end_cmp *tpa_end = cmp; 1454 1455 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1456 return 0; 1457 1458 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1459 } 1460 1461 if (agg_bufs) { 1462 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1463 return -EBUSY; 1464 } 1465 *raw_cons = tmp_raw_cons; 1466 return 0; 1467 } 1468 1469 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1470 { 1471 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1472 u16 idx = agg_id & MAX_TPA_P5_MASK; 1473 1474 if (test_bit(idx, map->agg_idx_bmap)) 1475 idx = find_first_zero_bit(map->agg_idx_bmap, 1476 BNXT_AGG_IDX_BMAP_SIZE); 1477 __set_bit(idx, map->agg_idx_bmap); 1478 map->agg_id_tbl[agg_id] = idx; 1479 return idx; 1480 } 1481 1482 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1483 { 1484 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1485 1486 __clear_bit(idx, map->agg_idx_bmap); 1487 } 1488 1489 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1490 { 1491 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1492 1493 return map->agg_id_tbl[agg_id]; 1494 } 1495 1496 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1497 struct rx_tpa_start_cmp *tpa_start, 1498 struct rx_tpa_start_cmp_ext *tpa_start1) 1499 { 1500 tpa_info->cfa_code_valid = 1; 1501 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1502 tpa_info->vlan_valid = 0; 1503 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1504 tpa_info->vlan_valid = 1; 1505 tpa_info->metadata = 1506 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1507 } 1508 } 1509 1510 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1511 struct rx_tpa_start_cmp *tpa_start, 1512 struct rx_tpa_start_cmp_ext *tpa_start1) 1513 { 1514 tpa_info->vlan_valid = 0; 1515 if (TPA_START_VLAN_VALID(tpa_start)) { 1516 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1517 u32 vlan_proto = ETH_P_8021Q; 1518 1519 tpa_info->vlan_valid = 1; 1520 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1521 vlan_proto = ETH_P_8021AD; 1522 tpa_info->metadata = vlan_proto << 16 | 1523 TPA_START_METADATA0_TCI(tpa_start1); 1524 } 1525 } 1526 1527 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1528 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1529 struct rx_tpa_start_cmp_ext *tpa_start1) 1530 { 1531 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1532 struct bnxt_tpa_info *tpa_info; 1533 u16 cons, prod, agg_id; 1534 struct rx_bd *prod_bd; 1535 dma_addr_t mapping; 1536 1537 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1538 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1539 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1540 } else { 1541 agg_id = TPA_START_AGG_ID(tpa_start); 1542 } 1543 cons = tpa_start->rx_tpa_start_cmp_opaque; 1544 prod = rxr->rx_prod; 1545 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1546 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1547 tpa_info = &rxr->rx_tpa[agg_id]; 1548 1549 if (unlikely(cons != rxr->rx_next_cons || 1550 TPA_START_ERROR(tpa_start))) { 1551 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1552 cons, rxr->rx_next_cons, 1553 TPA_START_ERROR_CODE(tpa_start1)); 1554 bnxt_sched_reset_rxr(bp, rxr); 1555 return; 1556 } 1557 prod_rx_buf->data = tpa_info->data; 1558 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1559 1560 mapping = tpa_info->mapping; 1561 prod_rx_buf->mapping = mapping; 1562 1563 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1564 1565 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1566 1567 tpa_info->data = cons_rx_buf->data; 1568 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1569 cons_rx_buf->data = NULL; 1570 tpa_info->mapping = cons_rx_buf->mapping; 1571 1572 tpa_info->len = 1573 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1574 RX_TPA_START_CMP_LEN_SHIFT; 1575 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1576 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1577 tpa_info->gso_type = SKB_GSO_TCPV4; 1578 if (TPA_START_IS_IPV6(tpa_start1)) 1579 tpa_info->gso_type = SKB_GSO_TCPV6; 1580 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1581 else if (!BNXT_CHIP_P4_PLUS(bp) && 1582 TPA_START_HASH_TYPE(tpa_start) == 3) 1583 tpa_info->gso_type = SKB_GSO_TCPV6; 1584 tpa_info->rss_hash = 1585 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1586 } else { 1587 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1588 tpa_info->gso_type = 0; 1589 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1590 } 1591 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1592 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1593 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1594 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1595 else 1596 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1597 tpa_info->agg_count = 0; 1598 1599 rxr->rx_prod = NEXT_RX(prod); 1600 cons = RING_RX(bp, NEXT_RX(cons)); 1601 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1602 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1603 1604 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1605 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1606 cons_rx_buf->data = NULL; 1607 } 1608 1609 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1610 { 1611 if (agg_bufs) 1612 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1613 } 1614 1615 #ifdef CONFIG_INET 1616 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1617 { 1618 struct udphdr *uh = NULL; 1619 1620 if (ip_proto == htons(ETH_P_IP)) { 1621 struct iphdr *iph = (struct iphdr *)skb->data; 1622 1623 if (iph->protocol == IPPROTO_UDP) 1624 uh = (struct udphdr *)(iph + 1); 1625 } else { 1626 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1627 1628 if (iph->nexthdr == IPPROTO_UDP) 1629 uh = (struct udphdr *)(iph + 1); 1630 } 1631 if (uh) { 1632 if (uh->check) 1633 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1634 else 1635 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1636 } 1637 } 1638 #endif 1639 1640 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1641 int payload_off, int tcp_ts, 1642 struct sk_buff *skb) 1643 { 1644 #ifdef CONFIG_INET 1645 struct tcphdr *th; 1646 int len, nw_off; 1647 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1648 u32 hdr_info = tpa_info->hdr_info; 1649 bool loopback = false; 1650 1651 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1652 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1653 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1654 1655 /* If the packet is an internal loopback packet, the offsets will 1656 * have an extra 4 bytes. 1657 */ 1658 if (inner_mac_off == 4) { 1659 loopback = true; 1660 } else if (inner_mac_off > 4) { 1661 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1662 ETH_HLEN - 2)); 1663 1664 /* We only support inner iPv4/ipv6. If we don't see the 1665 * correct protocol ID, it must be a loopback packet where 1666 * the offsets are off by 4. 1667 */ 1668 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1669 loopback = true; 1670 } 1671 if (loopback) { 1672 /* internal loopback packet, subtract all offsets by 4 */ 1673 inner_ip_off -= 4; 1674 inner_mac_off -= 4; 1675 outer_ip_off -= 4; 1676 } 1677 1678 nw_off = inner_ip_off - ETH_HLEN; 1679 skb_set_network_header(skb, nw_off); 1680 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1681 struct ipv6hdr *iph = ipv6_hdr(skb); 1682 1683 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1684 len = skb->len - skb_transport_offset(skb); 1685 th = tcp_hdr(skb); 1686 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1687 } else { 1688 struct iphdr *iph = ip_hdr(skb); 1689 1690 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1691 len = skb->len - skb_transport_offset(skb); 1692 th = tcp_hdr(skb); 1693 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1694 } 1695 1696 if (inner_mac_off) { /* tunnel */ 1697 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1698 ETH_HLEN - 2)); 1699 1700 bnxt_gro_tunnel(skb, proto); 1701 } 1702 #endif 1703 return skb; 1704 } 1705 1706 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1707 int payload_off, int tcp_ts, 1708 struct sk_buff *skb) 1709 { 1710 #ifdef CONFIG_INET 1711 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1712 u32 hdr_info = tpa_info->hdr_info; 1713 int iphdr_len, nw_off; 1714 1715 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1716 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1717 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1718 1719 nw_off = inner_ip_off - ETH_HLEN; 1720 skb_set_network_header(skb, nw_off); 1721 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1722 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1723 skb_set_transport_header(skb, nw_off + iphdr_len); 1724 1725 if (inner_mac_off) { /* tunnel */ 1726 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1727 ETH_HLEN - 2)); 1728 1729 bnxt_gro_tunnel(skb, proto); 1730 } 1731 #endif 1732 return skb; 1733 } 1734 1735 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1736 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1737 1738 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1739 int payload_off, int tcp_ts, 1740 struct sk_buff *skb) 1741 { 1742 #ifdef CONFIG_INET 1743 struct tcphdr *th; 1744 int len, nw_off, tcp_opt_len = 0; 1745 1746 if (tcp_ts) 1747 tcp_opt_len = 12; 1748 1749 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1750 struct iphdr *iph; 1751 1752 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1753 ETH_HLEN; 1754 skb_set_network_header(skb, nw_off); 1755 iph = ip_hdr(skb); 1756 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1757 len = skb->len - skb_transport_offset(skb); 1758 th = tcp_hdr(skb); 1759 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1760 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1761 struct ipv6hdr *iph; 1762 1763 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1764 ETH_HLEN; 1765 skb_set_network_header(skb, nw_off); 1766 iph = ipv6_hdr(skb); 1767 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1768 len = skb->len - skb_transport_offset(skb); 1769 th = tcp_hdr(skb); 1770 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1771 } else { 1772 dev_kfree_skb_any(skb); 1773 return NULL; 1774 } 1775 1776 if (nw_off) /* tunnel */ 1777 bnxt_gro_tunnel(skb, skb->protocol); 1778 #endif 1779 return skb; 1780 } 1781 1782 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1783 struct bnxt_tpa_info *tpa_info, 1784 struct rx_tpa_end_cmp *tpa_end, 1785 struct rx_tpa_end_cmp_ext *tpa_end1, 1786 struct sk_buff *skb) 1787 { 1788 #ifdef CONFIG_INET 1789 int payload_off; 1790 u16 segs; 1791 1792 segs = TPA_END_TPA_SEGS(tpa_end); 1793 if (segs == 1) 1794 return skb; 1795 1796 NAPI_GRO_CB(skb)->count = segs; 1797 skb_shinfo(skb)->gso_size = 1798 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1799 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1800 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1801 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1802 else 1803 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1804 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1805 if (likely(skb)) 1806 tcp_gro_complete(skb); 1807 #endif 1808 return skb; 1809 } 1810 1811 /* Given the cfa_code of a received packet determine which 1812 * netdev (vf-rep or PF) the packet is destined to. 1813 */ 1814 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1815 { 1816 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1817 1818 /* if vf-rep dev is NULL, it must belong to the PF */ 1819 return dev ? dev : bp->dev; 1820 } 1821 1822 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1823 struct bnxt_cp_ring_info *cpr, 1824 u32 *raw_cons, 1825 struct rx_tpa_end_cmp *tpa_end, 1826 struct rx_tpa_end_cmp_ext *tpa_end1, 1827 u8 *event) 1828 { 1829 struct bnxt_napi *bnapi = cpr->bnapi; 1830 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1831 struct net_device *dev = bp->dev; 1832 u8 *data_ptr, agg_bufs; 1833 unsigned int len; 1834 struct bnxt_tpa_info *tpa_info; 1835 dma_addr_t mapping; 1836 struct sk_buff *skb; 1837 u16 idx = 0, agg_id; 1838 void *data; 1839 bool gro; 1840 1841 if (unlikely(bnapi->in_reset)) { 1842 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1843 1844 if (rc < 0) 1845 return ERR_PTR(-EBUSY); 1846 return NULL; 1847 } 1848 1849 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1850 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1851 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1852 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1853 tpa_info = &rxr->rx_tpa[agg_id]; 1854 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1855 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1856 agg_bufs, tpa_info->agg_count); 1857 agg_bufs = tpa_info->agg_count; 1858 } 1859 tpa_info->agg_count = 0; 1860 *event |= BNXT_AGG_EVENT; 1861 bnxt_free_agg_idx(rxr, agg_id); 1862 idx = agg_id; 1863 gro = !!(bp->flags & BNXT_FLAG_GRO); 1864 } else { 1865 agg_id = TPA_END_AGG_ID(tpa_end); 1866 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1867 tpa_info = &rxr->rx_tpa[agg_id]; 1868 idx = RING_CMP(*raw_cons); 1869 if (agg_bufs) { 1870 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1871 return ERR_PTR(-EBUSY); 1872 1873 *event |= BNXT_AGG_EVENT; 1874 idx = NEXT_CMP(idx); 1875 } 1876 gro = !!TPA_END_GRO(tpa_end); 1877 } 1878 data = tpa_info->data; 1879 data_ptr = tpa_info->data_ptr; 1880 prefetch(data_ptr); 1881 len = tpa_info->len; 1882 mapping = tpa_info->mapping; 1883 1884 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1885 bnxt_abort_tpa(cpr, idx, agg_bufs); 1886 if (agg_bufs > MAX_SKB_FRAGS) 1887 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1888 agg_bufs, (int)MAX_SKB_FRAGS); 1889 return NULL; 1890 } 1891 1892 if (len <= bp->rx_copybreak) { 1893 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1894 if (!skb) { 1895 bnxt_abort_tpa(cpr, idx, agg_bufs); 1896 cpr->sw_stats->rx.rx_oom_discards += 1; 1897 return NULL; 1898 } 1899 } else { 1900 u8 *new_data; 1901 dma_addr_t new_mapping; 1902 1903 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1904 GFP_ATOMIC); 1905 if (!new_data) { 1906 bnxt_abort_tpa(cpr, idx, agg_bufs); 1907 cpr->sw_stats->rx.rx_oom_discards += 1; 1908 return NULL; 1909 } 1910 1911 tpa_info->data = new_data; 1912 tpa_info->data_ptr = new_data + bp->rx_offset; 1913 tpa_info->mapping = new_mapping; 1914 1915 skb = napi_build_skb(data, bp->rx_buf_size); 1916 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 1917 bp->rx_buf_use_size, bp->rx_dir); 1918 1919 if (!skb) { 1920 page_pool_free_va(rxr->head_pool, data, true); 1921 bnxt_abort_tpa(cpr, idx, agg_bufs); 1922 cpr->sw_stats->rx.rx_oom_discards += 1; 1923 return NULL; 1924 } 1925 skb_mark_for_recycle(skb); 1926 skb_reserve(skb, bp->rx_offset); 1927 skb_put(skb, len); 1928 } 1929 1930 if (agg_bufs) { 1931 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs, 1932 true); 1933 if (!skb) { 1934 /* Page reuse already handled by bnxt_rx_pages(). */ 1935 cpr->sw_stats->rx.rx_oom_discards += 1; 1936 return NULL; 1937 } 1938 } 1939 1940 if (tpa_info->cfa_code_valid) 1941 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1942 skb->protocol = eth_type_trans(skb, dev); 1943 1944 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1945 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1946 1947 if (tpa_info->vlan_valid && 1948 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1949 __be16 vlan_proto = htons(tpa_info->metadata >> 1950 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1951 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1952 1953 if (eth_type_vlan(vlan_proto)) { 1954 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1955 } else { 1956 dev_kfree_skb(skb); 1957 return NULL; 1958 } 1959 } 1960 1961 skb_checksum_none_assert(skb); 1962 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1963 skb->ip_summed = CHECKSUM_UNNECESSARY; 1964 skb->csum_level = 1965 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1966 } 1967 1968 if (gro) 1969 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1970 1971 return skb; 1972 } 1973 1974 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1975 struct rx_agg_cmp *rx_agg) 1976 { 1977 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1978 struct bnxt_tpa_info *tpa_info; 1979 1980 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1981 tpa_info = &rxr->rx_tpa[agg_id]; 1982 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1983 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1984 } 1985 1986 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1987 struct sk_buff *skb) 1988 { 1989 skb_mark_for_recycle(skb); 1990 1991 if (skb->dev != bp->dev) { 1992 /* this packet belongs to a vf-rep */ 1993 bnxt_vf_rep_rx(bp, skb); 1994 return; 1995 } 1996 skb_record_rx_queue(skb, bnapi->index); 1997 napi_gro_receive(&bnapi->napi, skb); 1998 } 1999 2000 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 2001 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 2002 { 2003 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 2004 2005 if (BNXT_PTP_RX_TS_VALID(flags)) 2006 goto ts_valid; 2007 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 2008 return false; 2009 2010 ts_valid: 2011 *cmpl_ts = ts; 2012 return true; 2013 } 2014 2015 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 2016 struct rx_cmp *rxcmp, 2017 struct rx_cmp_ext *rxcmp1) 2018 { 2019 __be16 vlan_proto; 2020 u16 vtag; 2021 2022 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 2023 __le32 flags2 = rxcmp1->rx_cmp_flags2; 2024 u32 meta_data; 2025 2026 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 2027 return skb; 2028 2029 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 2030 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2031 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 2032 if (eth_type_vlan(vlan_proto)) 2033 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2034 else 2035 goto vlan_err; 2036 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2037 if (RX_CMP_VLAN_VALID(rxcmp)) { 2038 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 2039 2040 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 2041 vlan_proto = htons(ETH_P_8021Q); 2042 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 2043 vlan_proto = htons(ETH_P_8021AD); 2044 else 2045 goto vlan_err; 2046 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 2047 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2048 } 2049 } 2050 return skb; 2051 vlan_err: 2052 skb_mark_for_recycle(skb); 2053 dev_kfree_skb(skb); 2054 return NULL; 2055 } 2056 2057 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 2058 struct rx_cmp *rxcmp) 2059 { 2060 u8 ext_op; 2061 2062 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2063 switch (ext_op) { 2064 case EXT_OP_INNER_4: 2065 case EXT_OP_OUTER_4: 2066 case EXT_OP_INNFL_3: 2067 case EXT_OP_OUTFL_3: 2068 return PKT_HASH_TYPE_L4; 2069 default: 2070 return PKT_HASH_TYPE_L3; 2071 } 2072 } 2073 2074 /* returns the following: 2075 * 1 - 1 packet successfully received 2076 * 0 - successful TPA_START, packet not completed yet 2077 * -EBUSY - completion ring does not have all the agg buffers yet 2078 * -ENOMEM - packet aborted due to out of memory 2079 * -EIO - packet aborted due to hw error indicated in BD 2080 */ 2081 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2082 u32 *raw_cons, u8 *event) 2083 { 2084 struct bnxt_napi *bnapi = cpr->bnapi; 2085 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2086 struct net_device *dev = bp->dev; 2087 struct rx_cmp *rxcmp; 2088 struct rx_cmp_ext *rxcmp1; 2089 u32 tmp_raw_cons = *raw_cons; 2090 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2091 struct skb_shared_info *sinfo; 2092 struct bnxt_sw_rx_bd *rx_buf; 2093 unsigned int len; 2094 u8 *data_ptr, agg_bufs, cmp_type; 2095 bool xdp_active = false; 2096 dma_addr_t dma_addr; 2097 struct sk_buff *skb; 2098 struct xdp_buff xdp; 2099 u32 flags, misc; 2100 u32 cmpl_ts; 2101 void *data; 2102 int rc = 0; 2103 2104 rxcmp = (struct rx_cmp *) 2105 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2106 2107 cmp_type = RX_CMP_TYPE(rxcmp); 2108 2109 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2110 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2111 goto next_rx_no_prod_no_len; 2112 } 2113 2114 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2115 cp_cons = RING_CMP(tmp_raw_cons); 2116 rxcmp1 = (struct rx_cmp_ext *) 2117 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2118 2119 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2120 return -EBUSY; 2121 2122 /* The valid test of the entry must be done first before 2123 * reading any further. 2124 */ 2125 dma_rmb(); 2126 prod = rxr->rx_prod; 2127 2128 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2129 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2130 bnxt_tpa_start(bp, rxr, cmp_type, 2131 (struct rx_tpa_start_cmp *)rxcmp, 2132 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2133 2134 *event |= BNXT_RX_EVENT; 2135 goto next_rx_no_prod_no_len; 2136 2137 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2138 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2139 (struct rx_tpa_end_cmp *)rxcmp, 2140 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2141 2142 if (IS_ERR(skb)) 2143 return -EBUSY; 2144 2145 rc = -ENOMEM; 2146 if (likely(skb)) { 2147 bnxt_deliver_skb(bp, bnapi, skb); 2148 rc = 1; 2149 } 2150 *event |= BNXT_RX_EVENT; 2151 goto next_rx_no_prod_no_len; 2152 } 2153 2154 cons = rxcmp->rx_cmp_opaque; 2155 if (unlikely(cons != rxr->rx_next_cons)) { 2156 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2157 2158 /* 0xffff is forced error, don't print it */ 2159 if (rxr->rx_next_cons != 0xffff) 2160 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2161 cons, rxr->rx_next_cons); 2162 bnxt_sched_reset_rxr(bp, rxr); 2163 if (rc1) 2164 return rc1; 2165 goto next_rx_no_prod_no_len; 2166 } 2167 rx_buf = &rxr->rx_buf_ring[cons]; 2168 data = rx_buf->data; 2169 data_ptr = rx_buf->data_ptr; 2170 prefetch(data_ptr); 2171 2172 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2173 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2174 2175 if (agg_bufs) { 2176 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2177 return -EBUSY; 2178 2179 cp_cons = NEXT_CMP(cp_cons); 2180 *event |= BNXT_AGG_EVENT; 2181 } 2182 *event |= BNXT_RX_EVENT; 2183 2184 rx_buf->data = NULL; 2185 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2186 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2187 2188 bnxt_reuse_rx_data(rxr, cons, data); 2189 if (agg_bufs) 2190 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2191 false); 2192 2193 rc = -EIO; 2194 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2195 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2196 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2197 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2198 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2199 rx_err); 2200 bnxt_sched_reset_rxr(bp, rxr); 2201 } 2202 } 2203 goto next_rx_no_len; 2204 } 2205 2206 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2207 len = flags >> RX_CMP_LEN_SHIFT; 2208 dma_addr = rx_buf->mapping; 2209 2210 if (bnxt_xdp_attached(bp, rxr)) { 2211 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2212 if (agg_bufs) { 2213 u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp, 2214 cp_cons, 2215 agg_bufs, 2216 false); 2217 if (!frag_len) 2218 goto oom_next_rx; 2219 2220 } 2221 xdp_active = true; 2222 } 2223 2224 if (xdp_active) { 2225 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2226 rc = 1; 2227 goto next_rx; 2228 } 2229 if (xdp_buff_has_frags(&xdp)) { 2230 sinfo = xdp_get_shared_info_from_buff(&xdp); 2231 agg_bufs = sinfo->nr_frags; 2232 } else { 2233 agg_bufs = 0; 2234 } 2235 } 2236 2237 if (len <= bp->rx_copybreak) { 2238 if (!xdp_active) 2239 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2240 else 2241 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2242 bnxt_reuse_rx_data(rxr, cons, data); 2243 if (!skb) { 2244 if (agg_bufs) { 2245 if (!xdp_active) 2246 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2247 agg_bufs, false); 2248 else 2249 bnxt_xdp_buff_frags_free(rxr, &xdp); 2250 } 2251 goto oom_next_rx; 2252 } 2253 } else { 2254 u32 payload; 2255 2256 if (rx_buf->data_ptr == data_ptr) 2257 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2258 else 2259 payload = 0; 2260 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2261 payload | len); 2262 if (!skb) 2263 goto oom_next_rx; 2264 } 2265 2266 if (agg_bufs) { 2267 if (!xdp_active) { 2268 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons, 2269 agg_bufs, false); 2270 if (!skb) 2271 goto oom_next_rx; 2272 } else { 2273 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, 2274 rxr->page_pool, &xdp); 2275 if (!skb) { 2276 /* we should be able to free the old skb here */ 2277 bnxt_xdp_buff_frags_free(rxr, &xdp); 2278 goto oom_next_rx; 2279 } 2280 } 2281 } 2282 2283 if (RX_CMP_HASH_VALID(rxcmp)) { 2284 enum pkt_hash_types type; 2285 2286 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2287 type = bnxt_rss_ext_op(bp, rxcmp); 2288 } else { 2289 u32 itypes = RX_CMP_ITYPES(rxcmp); 2290 2291 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2292 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2293 type = PKT_HASH_TYPE_L4; 2294 else 2295 type = PKT_HASH_TYPE_L3; 2296 } 2297 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2298 } 2299 2300 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2301 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2302 skb->protocol = eth_type_trans(skb, dev); 2303 2304 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2305 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2306 if (!skb) 2307 goto next_rx; 2308 } 2309 2310 skb_checksum_none_assert(skb); 2311 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2312 if (dev->features & NETIF_F_RXCSUM) { 2313 skb->ip_summed = CHECKSUM_UNNECESSARY; 2314 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2315 } 2316 } else { 2317 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2318 if (dev->features & NETIF_F_RXCSUM) 2319 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2320 } 2321 } 2322 2323 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2324 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2325 u64 ns, ts; 2326 2327 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2328 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2329 2330 ns = bnxt_timecounter_cyc2time(ptp, ts); 2331 memset(skb_hwtstamps(skb), 0, 2332 sizeof(*skb_hwtstamps(skb))); 2333 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2334 } 2335 } 2336 } 2337 bnxt_deliver_skb(bp, bnapi, skb); 2338 rc = 1; 2339 2340 next_rx: 2341 cpr->rx_packets += 1; 2342 cpr->rx_bytes += len; 2343 2344 next_rx_no_len: 2345 rxr->rx_prod = NEXT_RX(prod); 2346 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2347 2348 next_rx_no_prod_no_len: 2349 *raw_cons = tmp_raw_cons; 2350 2351 return rc; 2352 2353 oom_next_rx: 2354 cpr->sw_stats->rx.rx_oom_discards += 1; 2355 rc = -ENOMEM; 2356 goto next_rx; 2357 } 2358 2359 /* In netpoll mode, if we are using a combined completion ring, we need to 2360 * discard the rx packets and recycle the buffers. 2361 */ 2362 static int bnxt_force_rx_discard(struct bnxt *bp, 2363 struct bnxt_cp_ring_info *cpr, 2364 u32 *raw_cons, u8 *event) 2365 { 2366 u32 tmp_raw_cons = *raw_cons; 2367 struct rx_cmp_ext *rxcmp1; 2368 struct rx_cmp *rxcmp; 2369 u16 cp_cons; 2370 u8 cmp_type; 2371 int rc; 2372 2373 cp_cons = RING_CMP(tmp_raw_cons); 2374 rxcmp = (struct rx_cmp *) 2375 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2376 2377 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2378 cp_cons = RING_CMP(tmp_raw_cons); 2379 rxcmp1 = (struct rx_cmp_ext *) 2380 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2381 2382 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2383 return -EBUSY; 2384 2385 /* The valid test of the entry must be done first before 2386 * reading any further. 2387 */ 2388 dma_rmb(); 2389 cmp_type = RX_CMP_TYPE(rxcmp); 2390 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2391 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2392 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2393 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2394 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2395 struct rx_tpa_end_cmp_ext *tpa_end1; 2396 2397 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2398 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2399 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2400 } 2401 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2402 if (rc && rc != -EBUSY) 2403 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2404 return rc; 2405 } 2406 2407 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2408 { 2409 struct bnxt_fw_health *fw_health = bp->fw_health; 2410 u32 reg = fw_health->regs[reg_idx]; 2411 u32 reg_type, reg_off, val = 0; 2412 2413 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2414 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2415 switch (reg_type) { 2416 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2417 pci_read_config_dword(bp->pdev, reg_off, &val); 2418 break; 2419 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2420 reg_off = fw_health->mapped_regs[reg_idx]; 2421 fallthrough; 2422 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2423 val = readl(bp->bar0 + reg_off); 2424 break; 2425 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2426 val = readl(bp->bar1 + reg_off); 2427 break; 2428 } 2429 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2430 val &= fw_health->fw_reset_inprog_reg_mask; 2431 return val; 2432 } 2433 2434 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2435 { 2436 int i; 2437 2438 for (i = 0; i < bp->rx_nr_rings; i++) { 2439 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2440 struct bnxt_ring_grp_info *grp_info; 2441 2442 grp_info = &bp->grp_info[grp_idx]; 2443 if (grp_info->agg_fw_ring_id == ring_id) 2444 return grp_idx; 2445 } 2446 return INVALID_HW_RING_ID; 2447 } 2448 2449 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2450 { 2451 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2452 2453 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2454 return link_info->force_link_speed2; 2455 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2456 return link_info->force_pam4_link_speed; 2457 return link_info->force_link_speed; 2458 } 2459 2460 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2461 { 2462 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2463 2464 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2465 link_info->req_link_speed = link_info->force_link_speed2; 2466 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2467 switch (link_info->req_link_speed) { 2468 case BNXT_LINK_SPEED_50GB_PAM4: 2469 case BNXT_LINK_SPEED_100GB_PAM4: 2470 case BNXT_LINK_SPEED_200GB_PAM4: 2471 case BNXT_LINK_SPEED_400GB_PAM4: 2472 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2473 break; 2474 case BNXT_LINK_SPEED_100GB_PAM4_112: 2475 case BNXT_LINK_SPEED_200GB_PAM4_112: 2476 case BNXT_LINK_SPEED_400GB_PAM4_112: 2477 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2478 break; 2479 default: 2480 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2481 } 2482 return; 2483 } 2484 link_info->req_link_speed = link_info->force_link_speed; 2485 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2486 if (link_info->force_pam4_link_speed) { 2487 link_info->req_link_speed = link_info->force_pam4_link_speed; 2488 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2489 } 2490 } 2491 2492 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2493 { 2494 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2495 2496 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2497 link_info->advertising = link_info->auto_link_speeds2; 2498 return; 2499 } 2500 link_info->advertising = link_info->auto_link_speeds; 2501 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2502 } 2503 2504 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2505 { 2506 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2507 2508 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2509 if (link_info->req_link_speed != link_info->force_link_speed2) 2510 return true; 2511 return false; 2512 } 2513 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2514 link_info->req_link_speed != link_info->force_link_speed) 2515 return true; 2516 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2517 link_info->req_link_speed != link_info->force_pam4_link_speed) 2518 return true; 2519 return false; 2520 } 2521 2522 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2523 { 2524 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2525 2526 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2527 if (link_info->advertising != link_info->auto_link_speeds2) 2528 return true; 2529 return false; 2530 } 2531 if (link_info->advertising != link_info->auto_link_speeds || 2532 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2533 return true; 2534 return false; 2535 } 2536 2537 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2538 { 2539 u32 flags = bp->ctx->ctx_arr[type].flags; 2540 2541 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2542 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2543 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2544 } 2545 2546 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2547 { 2548 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2549 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2550 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2551 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2552 struct bnxt_bs_trace_info *bs_trace; 2553 int last_pg; 2554 2555 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2556 return; 2557 2558 mem_size = ctxm->max_entries * ctxm->entry_size; 2559 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2560 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2561 2562 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2563 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2564 2565 rmem = &ctx_pg[0].ring_mem; 2566 bs_trace = &bp->bs_trace[trace_type]; 2567 bs_trace->ctx_type = ctxm->type; 2568 bs_trace->trace_type = trace_type; 2569 if (pages > MAX_CTX_PAGES) { 2570 int last_pg_dir = rmem->nr_pages - 1; 2571 2572 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2573 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2574 } else { 2575 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2576 } 2577 bs_trace->magic_byte += magic_byte_offset; 2578 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2579 } 2580 2581 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2582 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2583 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2584 2585 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2586 (((data2) & \ 2587 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2588 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2589 2590 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2591 ((data2) & \ 2592 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2593 2594 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2595 (((data2) & \ 2596 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2597 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2598 2599 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2600 ((data1) & \ 2601 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2602 2603 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2604 (((data1) & \ 2605 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2606 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2607 2608 /* Return true if the workqueue has to be scheduled */ 2609 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2610 { 2611 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2612 2613 switch (err_type) { 2614 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2615 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2616 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2617 break; 2618 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2619 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2620 break; 2621 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2622 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2623 break; 2624 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2625 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2626 char *threshold_type; 2627 bool notify = false; 2628 char *dir_str; 2629 2630 switch (type) { 2631 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2632 threshold_type = "warning"; 2633 break; 2634 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2635 threshold_type = "critical"; 2636 break; 2637 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2638 threshold_type = "fatal"; 2639 break; 2640 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2641 threshold_type = "shutdown"; 2642 break; 2643 default: 2644 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2645 return false; 2646 } 2647 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2648 dir_str = "above"; 2649 notify = true; 2650 } else { 2651 dir_str = "below"; 2652 } 2653 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2654 dir_str, threshold_type); 2655 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2656 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2657 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2658 if (notify) { 2659 bp->thermal_threshold_type = type; 2660 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2661 return true; 2662 } 2663 return false; 2664 } 2665 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2666 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2667 break; 2668 default: 2669 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2670 err_type); 2671 break; 2672 } 2673 return false; 2674 } 2675 2676 #define BNXT_GET_EVENT_PORT(data) \ 2677 ((data) & \ 2678 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2679 2680 #define BNXT_EVENT_RING_TYPE(data2) \ 2681 ((data2) & \ 2682 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2683 2684 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2685 (BNXT_EVENT_RING_TYPE(data2) == \ 2686 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2687 2688 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2689 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2690 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2691 2692 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2693 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2694 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2695 2696 #define BNXT_PHC_BITS 48 2697 2698 static int bnxt_async_event_process(struct bnxt *bp, 2699 struct hwrm_async_event_cmpl *cmpl) 2700 { 2701 u16 event_id = le16_to_cpu(cmpl->event_id); 2702 u32 data1 = le32_to_cpu(cmpl->event_data1); 2703 u32 data2 = le32_to_cpu(cmpl->event_data2); 2704 2705 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2706 event_id, data1, data2); 2707 2708 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2709 switch (event_id) { 2710 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2711 struct bnxt_link_info *link_info = &bp->link_info; 2712 2713 if (BNXT_VF(bp)) 2714 goto async_event_process_exit; 2715 2716 /* print unsupported speed warning in forced speed mode only */ 2717 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2718 (data1 & 0x20000)) { 2719 u16 fw_speed = bnxt_get_force_speed(link_info); 2720 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2721 2722 if (speed != SPEED_UNKNOWN) 2723 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2724 speed); 2725 } 2726 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2727 } 2728 fallthrough; 2729 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2730 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2731 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2732 fallthrough; 2733 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2734 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2735 break; 2736 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2737 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2738 break; 2739 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2740 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2741 2742 if (BNXT_VF(bp)) 2743 break; 2744 2745 if (bp->pf.port_id != port_id) 2746 break; 2747 2748 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2749 break; 2750 } 2751 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2752 if (BNXT_PF(bp)) 2753 goto async_event_process_exit; 2754 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2755 break; 2756 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2757 char *type_str = "Solicited"; 2758 2759 if (!bp->fw_health) 2760 goto async_event_process_exit; 2761 2762 bp->fw_reset_timestamp = jiffies; 2763 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2764 if (!bp->fw_reset_min_dsecs) 2765 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2766 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2767 if (!bp->fw_reset_max_dsecs) 2768 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2769 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2770 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2771 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2772 type_str = "Fatal"; 2773 bp->fw_health->fatalities++; 2774 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2775 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2776 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2777 type_str = "Non-fatal"; 2778 bp->fw_health->survivals++; 2779 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2780 } 2781 netif_warn(bp, hw, bp->dev, 2782 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2783 type_str, data1, data2, 2784 bp->fw_reset_min_dsecs * 100, 2785 bp->fw_reset_max_dsecs * 100); 2786 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2787 break; 2788 } 2789 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2790 struct bnxt_fw_health *fw_health = bp->fw_health; 2791 char *status_desc = "healthy"; 2792 u32 status; 2793 2794 if (!fw_health) 2795 goto async_event_process_exit; 2796 2797 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2798 fw_health->enabled = false; 2799 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2800 break; 2801 } 2802 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2803 fw_health->tmr_multiplier = 2804 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2805 bp->current_interval * 10); 2806 fw_health->tmr_counter = fw_health->tmr_multiplier; 2807 if (!fw_health->enabled) 2808 fw_health->last_fw_heartbeat = 2809 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2810 fw_health->last_fw_reset_cnt = 2811 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2812 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2813 if (status != BNXT_FW_STATUS_HEALTHY) 2814 status_desc = "unhealthy"; 2815 netif_info(bp, drv, bp->dev, 2816 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2817 fw_health->primary ? "primary" : "backup", status, 2818 status_desc, fw_health->last_fw_reset_cnt); 2819 if (!fw_health->enabled) { 2820 /* Make sure tmr_counter is set and visible to 2821 * bnxt_health_check() before setting enabled to true. 2822 */ 2823 smp_wmb(); 2824 fw_health->enabled = true; 2825 } 2826 goto async_event_process_exit; 2827 } 2828 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2829 netif_notice(bp, hw, bp->dev, 2830 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2831 data1, data2); 2832 goto async_event_process_exit; 2833 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2834 struct bnxt_rx_ring_info *rxr; 2835 u16 grp_idx; 2836 2837 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2838 goto async_event_process_exit; 2839 2840 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2841 BNXT_EVENT_RING_TYPE(data2), data1); 2842 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2843 goto async_event_process_exit; 2844 2845 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2846 if (grp_idx == INVALID_HW_RING_ID) { 2847 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2848 data1); 2849 goto async_event_process_exit; 2850 } 2851 rxr = bp->bnapi[grp_idx]->rx_ring; 2852 bnxt_sched_reset_rxr(bp, rxr); 2853 goto async_event_process_exit; 2854 } 2855 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2856 struct bnxt_fw_health *fw_health = bp->fw_health; 2857 2858 netif_notice(bp, hw, bp->dev, 2859 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2860 data1, data2); 2861 if (fw_health) { 2862 fw_health->echo_req_data1 = data1; 2863 fw_health->echo_req_data2 = data2; 2864 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2865 break; 2866 } 2867 goto async_event_process_exit; 2868 } 2869 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2870 bnxt_ptp_pps_event(bp, data1, data2); 2871 goto async_event_process_exit; 2872 } 2873 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2874 if (bnxt_event_error_report(bp, data1, data2)) 2875 break; 2876 goto async_event_process_exit; 2877 } 2878 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2879 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2880 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2881 if (BNXT_PTP_USE_RTC(bp)) { 2882 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2883 unsigned long flags; 2884 u64 ns; 2885 2886 if (!ptp) 2887 goto async_event_process_exit; 2888 2889 bnxt_ptp_update_current_time(bp); 2890 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2891 BNXT_PHC_BITS) | ptp->current_time); 2892 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2893 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2894 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2895 } 2896 break; 2897 } 2898 goto async_event_process_exit; 2899 } 2900 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2901 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2902 2903 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2904 goto async_event_process_exit; 2905 } 2906 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2907 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2908 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2909 2910 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2911 goto async_event_process_exit; 2912 } 2913 default: 2914 goto async_event_process_exit; 2915 } 2916 __bnxt_queue_sp_work(bp); 2917 async_event_process_exit: 2918 bnxt_ulp_async_events(bp, cmpl); 2919 return 0; 2920 } 2921 2922 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2923 { 2924 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2925 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2926 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2927 (struct hwrm_fwd_req_cmpl *)txcmp; 2928 2929 switch (cmpl_type) { 2930 case CMPL_BASE_TYPE_HWRM_DONE: 2931 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2932 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2933 break; 2934 2935 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2936 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2937 2938 if ((vf_id < bp->pf.first_vf_id) || 2939 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2940 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2941 vf_id); 2942 return -EINVAL; 2943 } 2944 2945 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2946 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2947 break; 2948 2949 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2950 bnxt_async_event_process(bp, 2951 (struct hwrm_async_event_cmpl *)txcmp); 2952 break; 2953 2954 default: 2955 break; 2956 } 2957 2958 return 0; 2959 } 2960 2961 static bool bnxt_vnic_is_active(struct bnxt *bp) 2962 { 2963 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 2964 2965 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 2966 } 2967 2968 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2969 { 2970 struct bnxt_napi *bnapi = dev_instance; 2971 struct bnxt *bp = bnapi->bp; 2972 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2973 u32 cons = RING_CMP(cpr->cp_raw_cons); 2974 2975 cpr->event_ctr++; 2976 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2977 napi_schedule(&bnapi->napi); 2978 return IRQ_HANDLED; 2979 } 2980 2981 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2982 { 2983 u32 raw_cons = cpr->cp_raw_cons; 2984 u16 cons = RING_CMP(raw_cons); 2985 struct tx_cmp *txcmp; 2986 2987 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2988 2989 return TX_CMP_VALID(txcmp, raw_cons); 2990 } 2991 2992 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2993 int budget) 2994 { 2995 struct bnxt_napi *bnapi = cpr->bnapi; 2996 u32 raw_cons = cpr->cp_raw_cons; 2997 bool flush_xdp = false; 2998 u32 cons; 2999 int rx_pkts = 0; 3000 u8 event = 0; 3001 struct tx_cmp *txcmp; 3002 3003 cpr->has_more_work = 0; 3004 cpr->had_work_done = 1; 3005 while (1) { 3006 u8 cmp_type; 3007 int rc; 3008 3009 cons = RING_CMP(raw_cons); 3010 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3011 3012 if (!TX_CMP_VALID(txcmp, raw_cons)) 3013 break; 3014 3015 /* The valid test of the entry must be done first before 3016 * reading any further. 3017 */ 3018 dma_rmb(); 3019 cmp_type = TX_CMP_TYPE(txcmp); 3020 if (cmp_type == CMP_TYPE_TX_L2_CMP || 3021 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 3022 u32 opaque = txcmp->tx_cmp_opaque; 3023 struct bnxt_tx_ring_info *txr; 3024 u16 tx_freed; 3025 3026 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 3027 event |= BNXT_TX_CMP_EVENT; 3028 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 3029 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 3030 else 3031 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 3032 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 3033 bp->tx_ring_mask; 3034 /* return full budget so NAPI will complete. */ 3035 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 3036 rx_pkts = budget; 3037 raw_cons = NEXT_RAW_CMP(raw_cons); 3038 if (budget) 3039 cpr->has_more_work = 1; 3040 break; 3041 } 3042 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 3043 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 3044 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 3045 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 3046 if (likely(budget)) 3047 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3048 else 3049 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 3050 &event); 3051 if (event & BNXT_REDIRECT_EVENT) 3052 flush_xdp = true; 3053 if (likely(rc >= 0)) 3054 rx_pkts += rc; 3055 /* Increment rx_pkts when rc is -ENOMEM to count towards 3056 * the NAPI budget. Otherwise, we may potentially loop 3057 * here forever if we consistently cannot allocate 3058 * buffers. 3059 */ 3060 else if (rc == -ENOMEM && budget) 3061 rx_pkts++; 3062 else if (rc == -EBUSY) /* partial completion */ 3063 break; 3064 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3065 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3066 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3067 bnxt_hwrm_handler(bp, txcmp); 3068 } 3069 raw_cons = NEXT_RAW_CMP(raw_cons); 3070 3071 if (rx_pkts && rx_pkts == budget) { 3072 cpr->has_more_work = 1; 3073 break; 3074 } 3075 } 3076 3077 if (flush_xdp) { 3078 xdp_do_flush(); 3079 event &= ~BNXT_REDIRECT_EVENT; 3080 } 3081 3082 if (event & BNXT_TX_EVENT) { 3083 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3084 u16 prod = txr->tx_prod; 3085 3086 /* Sync BD data before updating doorbell */ 3087 wmb(); 3088 3089 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3090 event &= ~BNXT_TX_EVENT; 3091 } 3092 3093 cpr->cp_raw_cons = raw_cons; 3094 bnapi->events |= event; 3095 return rx_pkts; 3096 } 3097 3098 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3099 int budget) 3100 { 3101 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3102 bnapi->tx_int(bp, bnapi, budget); 3103 3104 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3105 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3106 3107 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3108 bnapi->events &= ~BNXT_RX_EVENT; 3109 } 3110 if (bnapi->events & BNXT_AGG_EVENT) { 3111 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3112 3113 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3114 bnapi->events &= ~BNXT_AGG_EVENT; 3115 } 3116 } 3117 3118 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3119 int budget) 3120 { 3121 struct bnxt_napi *bnapi = cpr->bnapi; 3122 int rx_pkts; 3123 3124 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3125 3126 /* ACK completion ring before freeing tx ring and producing new 3127 * buffers in rx/agg rings to prevent overflowing the completion 3128 * ring. 3129 */ 3130 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3131 3132 __bnxt_poll_work_done(bp, bnapi, budget); 3133 return rx_pkts; 3134 } 3135 3136 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3137 { 3138 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3139 struct bnxt *bp = bnapi->bp; 3140 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3141 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3142 struct tx_cmp *txcmp; 3143 struct rx_cmp_ext *rxcmp1; 3144 u32 cp_cons, tmp_raw_cons; 3145 u32 raw_cons = cpr->cp_raw_cons; 3146 bool flush_xdp = false; 3147 u32 rx_pkts = 0; 3148 u8 event = 0; 3149 3150 while (1) { 3151 int rc; 3152 3153 cp_cons = RING_CMP(raw_cons); 3154 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3155 3156 if (!TX_CMP_VALID(txcmp, raw_cons)) 3157 break; 3158 3159 /* The valid test of the entry must be done first before 3160 * reading any further. 3161 */ 3162 dma_rmb(); 3163 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3164 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3165 cp_cons = RING_CMP(tmp_raw_cons); 3166 rxcmp1 = (struct rx_cmp_ext *) 3167 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3168 3169 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3170 break; 3171 3172 /* force an error to recycle the buffer */ 3173 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3174 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3175 3176 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3177 if (likely(rc == -EIO) && budget) 3178 rx_pkts++; 3179 else if (rc == -EBUSY) /* partial completion */ 3180 break; 3181 if (event & BNXT_REDIRECT_EVENT) 3182 flush_xdp = true; 3183 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3184 CMPL_BASE_TYPE_HWRM_DONE)) { 3185 bnxt_hwrm_handler(bp, txcmp); 3186 } else { 3187 netdev_err(bp->dev, 3188 "Invalid completion received on special ring\n"); 3189 } 3190 raw_cons = NEXT_RAW_CMP(raw_cons); 3191 3192 if (rx_pkts == budget) 3193 break; 3194 } 3195 3196 cpr->cp_raw_cons = raw_cons; 3197 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3198 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3199 3200 if (event & BNXT_AGG_EVENT) 3201 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3202 if (flush_xdp) 3203 xdp_do_flush(); 3204 3205 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3206 napi_complete_done(napi, rx_pkts); 3207 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3208 } 3209 return rx_pkts; 3210 } 3211 3212 static int bnxt_poll(struct napi_struct *napi, int budget) 3213 { 3214 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3215 struct bnxt *bp = bnapi->bp; 3216 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3217 int work_done = 0; 3218 3219 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3220 napi_complete(napi); 3221 return 0; 3222 } 3223 while (1) { 3224 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3225 3226 if (work_done >= budget) { 3227 if (!budget) 3228 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3229 break; 3230 } 3231 3232 if (!bnxt_has_work(bp, cpr)) { 3233 if (napi_complete_done(napi, work_done)) 3234 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3235 break; 3236 } 3237 } 3238 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3239 struct dim_sample dim_sample = {}; 3240 3241 dim_update_sample(cpr->event_ctr, 3242 cpr->rx_packets, 3243 cpr->rx_bytes, 3244 &dim_sample); 3245 net_dim(&cpr->dim, &dim_sample); 3246 } 3247 return work_done; 3248 } 3249 3250 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3251 { 3252 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3253 int i, work_done = 0; 3254 3255 for (i = 0; i < cpr->cp_ring_count; i++) { 3256 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3257 3258 if (cpr2->had_nqe_notify) { 3259 work_done += __bnxt_poll_work(bp, cpr2, 3260 budget - work_done); 3261 cpr->has_more_work |= cpr2->has_more_work; 3262 } 3263 } 3264 return work_done; 3265 } 3266 3267 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3268 u64 dbr_type, int budget) 3269 { 3270 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3271 int i; 3272 3273 for (i = 0; i < cpr->cp_ring_count; i++) { 3274 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3275 struct bnxt_db_info *db; 3276 3277 if (cpr2->had_work_done) { 3278 u32 tgl = 0; 3279 3280 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3281 cpr2->had_nqe_notify = 0; 3282 tgl = cpr2->toggle; 3283 } 3284 db = &cpr2->cp_db; 3285 bnxt_writeq(bp, 3286 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3287 DB_RING_IDX(db, cpr2->cp_raw_cons), 3288 db->doorbell); 3289 cpr2->had_work_done = 0; 3290 } 3291 } 3292 __bnxt_poll_work_done(bp, bnapi, budget); 3293 } 3294 3295 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3296 { 3297 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3298 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3299 struct bnxt_cp_ring_info *cpr_rx; 3300 u32 raw_cons = cpr->cp_raw_cons; 3301 struct bnxt *bp = bnapi->bp; 3302 struct nqe_cn *nqcmp; 3303 int work_done = 0; 3304 u32 cons; 3305 3306 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3307 napi_complete(napi); 3308 return 0; 3309 } 3310 if (cpr->has_more_work) { 3311 cpr->has_more_work = 0; 3312 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3313 } 3314 while (1) { 3315 u16 type; 3316 3317 cons = RING_CMP(raw_cons); 3318 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3319 3320 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3321 if (cpr->has_more_work) 3322 break; 3323 3324 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3325 budget); 3326 cpr->cp_raw_cons = raw_cons; 3327 if (napi_complete_done(napi, work_done)) 3328 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3329 cpr->cp_raw_cons); 3330 goto poll_done; 3331 } 3332 3333 /* The valid test of the entry must be done first before 3334 * reading any further. 3335 */ 3336 dma_rmb(); 3337 3338 type = le16_to_cpu(nqcmp->type); 3339 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3340 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3341 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3342 struct bnxt_cp_ring_info *cpr2; 3343 3344 /* No more budget for RX work */ 3345 if (budget && work_done >= budget && 3346 cq_type == BNXT_NQ_HDL_TYPE_RX) 3347 break; 3348 3349 idx = BNXT_NQ_HDL_IDX(idx); 3350 cpr2 = &cpr->cp_ring_arr[idx]; 3351 cpr2->had_nqe_notify = 1; 3352 cpr2->toggle = NQE_CN_TOGGLE(type); 3353 work_done += __bnxt_poll_work(bp, cpr2, 3354 budget - work_done); 3355 cpr->has_more_work |= cpr2->has_more_work; 3356 } else { 3357 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3358 } 3359 raw_cons = NEXT_RAW_CMP(raw_cons); 3360 } 3361 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3362 if (raw_cons != cpr->cp_raw_cons) { 3363 cpr->cp_raw_cons = raw_cons; 3364 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3365 } 3366 poll_done: 3367 cpr_rx = &cpr->cp_ring_arr[0]; 3368 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3369 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3370 struct dim_sample dim_sample = {}; 3371 3372 dim_update_sample(cpr->event_ctr, 3373 cpr_rx->rx_packets, 3374 cpr_rx->rx_bytes, 3375 &dim_sample); 3376 net_dim(&cpr->dim, &dim_sample); 3377 } 3378 return work_done; 3379 } 3380 3381 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp, 3382 struct bnxt_tx_ring_info *txr, int idx) 3383 { 3384 int i, max_idx; 3385 struct pci_dev *pdev = bp->pdev; 3386 3387 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3388 3389 for (i = 0; i < max_idx;) { 3390 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i]; 3391 struct sk_buff *skb; 3392 int j, last; 3393 3394 if (idx < bp->tx_nr_rings_xdp && 3395 tx_buf->action == XDP_REDIRECT) { 3396 dma_unmap_single(&pdev->dev, 3397 dma_unmap_addr(tx_buf, mapping), 3398 dma_unmap_len(tx_buf, len), 3399 DMA_TO_DEVICE); 3400 xdp_return_frame(tx_buf->xdpf); 3401 tx_buf->action = 0; 3402 tx_buf->xdpf = NULL; 3403 i++; 3404 continue; 3405 } 3406 3407 skb = tx_buf->skb; 3408 if (!skb) { 3409 i++; 3410 continue; 3411 } 3412 3413 tx_buf->skb = NULL; 3414 3415 if (tx_buf->is_push) { 3416 dev_kfree_skb(skb); 3417 i += 2; 3418 continue; 3419 } 3420 3421 dma_unmap_single(&pdev->dev, 3422 dma_unmap_addr(tx_buf, mapping), 3423 skb_headlen(skb), 3424 DMA_TO_DEVICE); 3425 3426 last = tx_buf->nr_frags; 3427 i += 2; 3428 for (j = 0; j < last; j++, i++) { 3429 int ring_idx = i & bp->tx_ring_mask; 3430 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 3431 3432 tx_buf = &txr->tx_buf_ring[ring_idx]; 3433 netmem_dma_unmap_page_attrs(&pdev->dev, 3434 dma_unmap_addr(tx_buf, 3435 mapping), 3436 skb_frag_size(frag), 3437 DMA_TO_DEVICE, 0); 3438 } 3439 dev_kfree_skb(skb); 3440 } 3441 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx)); 3442 } 3443 3444 static void bnxt_free_tx_skbs(struct bnxt *bp) 3445 { 3446 int i; 3447 3448 if (!bp->tx_ring) 3449 return; 3450 3451 for (i = 0; i < bp->tx_nr_rings; i++) { 3452 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3453 3454 if (!txr->tx_buf_ring) 3455 continue; 3456 3457 bnxt_free_one_tx_ring_skbs(bp, txr, i); 3458 } 3459 3460 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 3461 bnxt_ptp_free_txts_skbs(bp->ptp_cfg); 3462 } 3463 3464 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3465 { 3466 int i, max_idx; 3467 3468 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3469 3470 for (i = 0; i < max_idx; i++) { 3471 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3472 void *data = rx_buf->data; 3473 3474 if (!data) 3475 continue; 3476 3477 rx_buf->data = NULL; 3478 if (BNXT_RX_PAGE_MODE(bp)) 3479 page_pool_recycle_direct(rxr->page_pool, data); 3480 else 3481 page_pool_free_va(rxr->head_pool, data, true); 3482 } 3483 } 3484 3485 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3486 { 3487 int i, max_idx; 3488 3489 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3490 3491 for (i = 0; i < max_idx; i++) { 3492 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3493 netmem_ref netmem = rx_agg_buf->netmem; 3494 3495 if (!netmem) 3496 continue; 3497 3498 rx_agg_buf->netmem = 0; 3499 __clear_bit(i, rxr->rx_agg_bmap); 3500 3501 page_pool_recycle_direct_netmem(rxr->page_pool, netmem); 3502 } 3503 } 3504 3505 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3506 struct bnxt_rx_ring_info *rxr) 3507 { 3508 int i; 3509 3510 for (i = 0; i < bp->max_tpa; i++) { 3511 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3512 u8 *data = tpa_info->data; 3513 3514 if (!data) 3515 continue; 3516 3517 tpa_info->data = NULL; 3518 page_pool_free_va(rxr->head_pool, data, false); 3519 } 3520 } 3521 3522 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3523 struct bnxt_rx_ring_info *rxr) 3524 { 3525 struct bnxt_tpa_idx_map *map; 3526 3527 if (!rxr->rx_tpa) 3528 goto skip_rx_tpa_free; 3529 3530 bnxt_free_one_tpa_info_data(bp, rxr); 3531 3532 skip_rx_tpa_free: 3533 if (!rxr->rx_buf_ring) 3534 goto skip_rx_buf_free; 3535 3536 bnxt_free_one_rx_ring(bp, rxr); 3537 3538 skip_rx_buf_free: 3539 if (!rxr->rx_agg_ring) 3540 goto skip_rx_agg_free; 3541 3542 bnxt_free_one_rx_agg_ring(bp, rxr); 3543 3544 skip_rx_agg_free: 3545 map = rxr->rx_tpa_idx_map; 3546 if (map) 3547 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3548 } 3549 3550 static void bnxt_free_rx_skbs(struct bnxt *bp) 3551 { 3552 int i; 3553 3554 if (!bp->rx_ring) 3555 return; 3556 3557 for (i = 0; i < bp->rx_nr_rings; i++) 3558 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3559 } 3560 3561 static void bnxt_free_skbs(struct bnxt *bp) 3562 { 3563 bnxt_free_tx_skbs(bp); 3564 bnxt_free_rx_skbs(bp); 3565 } 3566 3567 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3568 { 3569 u8 init_val = ctxm->init_value; 3570 u16 offset = ctxm->init_offset; 3571 u8 *p2 = p; 3572 int i; 3573 3574 if (!init_val) 3575 return; 3576 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3577 memset(p, init_val, len); 3578 return; 3579 } 3580 for (i = 0; i < len; i += ctxm->entry_size) 3581 *(p2 + i + offset) = init_val; 3582 } 3583 3584 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3585 void *buf, size_t offset, size_t head, 3586 size_t tail) 3587 { 3588 int i, head_page, start_idx, source_offset; 3589 size_t len, rem_len, total_len, max_bytes; 3590 3591 head_page = head / rmem->page_size; 3592 source_offset = head % rmem->page_size; 3593 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3594 if (!total_len) 3595 total_len = MAX_CTX_BYTES; 3596 start_idx = head_page % MAX_CTX_PAGES; 3597 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3598 source_offset; 3599 total_len = min(total_len, max_bytes); 3600 rem_len = total_len; 3601 3602 for (i = start_idx; rem_len; i++, source_offset = 0) { 3603 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3604 if (buf) 3605 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3606 len); 3607 offset += len; 3608 rem_len -= len; 3609 } 3610 return total_len; 3611 } 3612 3613 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3614 { 3615 struct pci_dev *pdev = bp->pdev; 3616 int i; 3617 3618 if (!rmem->pg_arr) 3619 goto skip_pages; 3620 3621 for (i = 0; i < rmem->nr_pages; i++) { 3622 if (!rmem->pg_arr[i]) 3623 continue; 3624 3625 dma_free_coherent(&pdev->dev, rmem->page_size, 3626 rmem->pg_arr[i], rmem->dma_arr[i]); 3627 3628 rmem->pg_arr[i] = NULL; 3629 } 3630 skip_pages: 3631 if (rmem->pg_tbl) { 3632 size_t pg_tbl_size = rmem->nr_pages * 8; 3633 3634 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3635 pg_tbl_size = rmem->page_size; 3636 dma_free_coherent(&pdev->dev, pg_tbl_size, 3637 rmem->pg_tbl, rmem->pg_tbl_map); 3638 rmem->pg_tbl = NULL; 3639 } 3640 if (rmem->vmem_size && *rmem->vmem) { 3641 vfree(*rmem->vmem); 3642 *rmem->vmem = NULL; 3643 } 3644 } 3645 3646 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3647 { 3648 struct pci_dev *pdev = bp->pdev; 3649 u64 valid_bit = 0; 3650 int i; 3651 3652 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3653 valid_bit = PTU_PTE_VALID; 3654 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3655 size_t pg_tbl_size = rmem->nr_pages * 8; 3656 3657 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3658 pg_tbl_size = rmem->page_size; 3659 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3660 &rmem->pg_tbl_map, 3661 GFP_KERNEL); 3662 if (!rmem->pg_tbl) 3663 return -ENOMEM; 3664 } 3665 3666 for (i = 0; i < rmem->nr_pages; i++) { 3667 u64 extra_bits = valid_bit; 3668 3669 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3670 rmem->page_size, 3671 &rmem->dma_arr[i], 3672 GFP_KERNEL); 3673 if (!rmem->pg_arr[i]) 3674 return -ENOMEM; 3675 3676 if (rmem->ctx_mem) 3677 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3678 rmem->page_size); 3679 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3680 if (i == rmem->nr_pages - 2 && 3681 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3682 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3683 else if (i == rmem->nr_pages - 1 && 3684 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3685 extra_bits |= PTU_PTE_LAST; 3686 rmem->pg_tbl[i] = 3687 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3688 } 3689 } 3690 3691 if (rmem->vmem_size) { 3692 *rmem->vmem = vzalloc(rmem->vmem_size); 3693 if (!(*rmem->vmem)) 3694 return -ENOMEM; 3695 } 3696 return 0; 3697 } 3698 3699 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3700 struct bnxt_rx_ring_info *rxr) 3701 { 3702 int i; 3703 3704 kfree(rxr->rx_tpa_idx_map); 3705 rxr->rx_tpa_idx_map = NULL; 3706 if (rxr->rx_tpa) { 3707 for (i = 0; i < bp->max_tpa; i++) { 3708 kfree(rxr->rx_tpa[i].agg_arr); 3709 rxr->rx_tpa[i].agg_arr = NULL; 3710 } 3711 } 3712 kfree(rxr->rx_tpa); 3713 rxr->rx_tpa = NULL; 3714 } 3715 3716 static void bnxt_free_tpa_info(struct bnxt *bp) 3717 { 3718 int i; 3719 3720 for (i = 0; i < bp->rx_nr_rings; i++) { 3721 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3722 3723 bnxt_free_one_tpa_info(bp, rxr); 3724 } 3725 } 3726 3727 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3728 struct bnxt_rx_ring_info *rxr) 3729 { 3730 struct rx_agg_cmp *agg; 3731 int i; 3732 3733 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3734 GFP_KERNEL); 3735 if (!rxr->rx_tpa) 3736 return -ENOMEM; 3737 3738 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3739 return 0; 3740 for (i = 0; i < bp->max_tpa; i++) { 3741 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3742 if (!agg) 3743 return -ENOMEM; 3744 rxr->rx_tpa[i].agg_arr = agg; 3745 } 3746 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3747 GFP_KERNEL); 3748 if (!rxr->rx_tpa_idx_map) 3749 return -ENOMEM; 3750 3751 return 0; 3752 } 3753 3754 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3755 { 3756 int i, rc; 3757 3758 bp->max_tpa = MAX_TPA; 3759 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3760 if (!bp->max_tpa_v2) 3761 return 0; 3762 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3763 } 3764 3765 for (i = 0; i < bp->rx_nr_rings; i++) { 3766 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3767 3768 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3769 if (rc) 3770 return rc; 3771 } 3772 return 0; 3773 } 3774 3775 static void bnxt_free_rx_rings(struct bnxt *bp) 3776 { 3777 int i; 3778 3779 if (!bp->rx_ring) 3780 return; 3781 3782 bnxt_free_tpa_info(bp); 3783 for (i = 0; i < bp->rx_nr_rings; i++) { 3784 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3785 struct bnxt_ring_struct *ring; 3786 3787 if (rxr->xdp_prog) 3788 bpf_prog_put(rxr->xdp_prog); 3789 3790 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3791 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3792 3793 page_pool_destroy(rxr->page_pool); 3794 if (bnxt_separate_head_pool(rxr)) 3795 page_pool_destroy(rxr->head_pool); 3796 rxr->page_pool = rxr->head_pool = NULL; 3797 3798 kfree(rxr->rx_agg_bmap); 3799 rxr->rx_agg_bmap = NULL; 3800 3801 ring = &rxr->rx_ring_struct; 3802 bnxt_free_ring(bp, &ring->ring_mem); 3803 3804 ring = &rxr->rx_agg_ring_struct; 3805 bnxt_free_ring(bp, &ring->ring_mem); 3806 } 3807 } 3808 3809 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3810 struct bnxt_rx_ring_info *rxr, 3811 int numa_node) 3812 { 3813 const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE; 3814 const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K; 3815 struct page_pool_params pp = { 0 }; 3816 struct page_pool *pool; 3817 3818 pp.pool_size = bp->rx_agg_ring_size / agg_size_fac; 3819 if (BNXT_RX_PAGE_MODE(bp)) 3820 pp.pool_size += bp->rx_ring_size / rx_size_fac; 3821 pp.nid = numa_node; 3822 pp.napi = &rxr->bnapi->napi; 3823 pp.netdev = bp->dev; 3824 pp.dev = &bp->pdev->dev; 3825 pp.dma_dir = bp->rx_dir; 3826 pp.max_len = PAGE_SIZE; 3827 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | 3828 PP_FLAG_ALLOW_UNREADABLE_NETMEM; 3829 pp.queue_idx = rxr->bnapi->index; 3830 3831 pool = page_pool_create(&pp); 3832 if (IS_ERR(pool)) 3833 return PTR_ERR(pool); 3834 rxr->page_pool = pool; 3835 3836 rxr->need_head_pool = page_pool_is_unreadable(pool); 3837 if (bnxt_separate_head_pool(rxr)) { 3838 pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024); 3839 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3840 pool = page_pool_create(&pp); 3841 if (IS_ERR(pool)) 3842 goto err_destroy_pp; 3843 } 3844 rxr->head_pool = pool; 3845 3846 return 0; 3847 3848 err_destroy_pp: 3849 page_pool_destroy(rxr->page_pool); 3850 rxr->page_pool = NULL; 3851 return PTR_ERR(pool); 3852 } 3853 3854 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3855 { 3856 u16 mem_size; 3857 3858 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3859 mem_size = rxr->rx_agg_bmap_size / 8; 3860 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3861 if (!rxr->rx_agg_bmap) 3862 return -ENOMEM; 3863 3864 return 0; 3865 } 3866 3867 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3868 { 3869 int numa_node = dev_to_node(&bp->pdev->dev); 3870 int i, rc = 0, agg_rings = 0, cpu; 3871 3872 if (!bp->rx_ring) 3873 return -ENOMEM; 3874 3875 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3876 agg_rings = 1; 3877 3878 for (i = 0; i < bp->rx_nr_rings; i++) { 3879 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3880 struct bnxt_ring_struct *ring; 3881 int cpu_node; 3882 3883 ring = &rxr->rx_ring_struct; 3884 3885 cpu = cpumask_local_spread(i, numa_node); 3886 cpu_node = cpu_to_node(cpu); 3887 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3888 i, cpu_node); 3889 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3890 if (rc) 3891 return rc; 3892 3893 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3894 if (rc < 0) 3895 return rc; 3896 3897 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3898 MEM_TYPE_PAGE_POOL, 3899 rxr->page_pool); 3900 if (rc) { 3901 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3902 return rc; 3903 } 3904 3905 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3906 if (rc) 3907 return rc; 3908 3909 ring->grp_idx = i; 3910 if (agg_rings) { 3911 ring = &rxr->rx_agg_ring_struct; 3912 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3913 if (rc) 3914 return rc; 3915 3916 ring->grp_idx = i; 3917 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 3918 if (rc) 3919 return rc; 3920 } 3921 } 3922 if (bp->flags & BNXT_FLAG_TPA) 3923 rc = bnxt_alloc_tpa_info(bp); 3924 return rc; 3925 } 3926 3927 static void bnxt_free_tx_rings(struct bnxt *bp) 3928 { 3929 int i; 3930 struct pci_dev *pdev = bp->pdev; 3931 3932 if (!bp->tx_ring) 3933 return; 3934 3935 for (i = 0; i < bp->tx_nr_rings; i++) { 3936 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3937 struct bnxt_ring_struct *ring; 3938 3939 if (txr->tx_push) { 3940 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3941 txr->tx_push, txr->tx_push_mapping); 3942 txr->tx_push = NULL; 3943 } 3944 3945 ring = &txr->tx_ring_struct; 3946 3947 bnxt_free_ring(bp, &ring->ring_mem); 3948 } 3949 } 3950 3951 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3952 ((tc) * (bp)->tx_nr_rings_per_tc) 3953 3954 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3955 ((tx) % (bp)->tx_nr_rings_per_tc) 3956 3957 #define BNXT_RING_TO_TC(bp, tx) \ 3958 ((tx) / (bp)->tx_nr_rings_per_tc) 3959 3960 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3961 { 3962 int i, j, rc; 3963 struct pci_dev *pdev = bp->pdev; 3964 3965 bp->tx_push_size = 0; 3966 if (bp->tx_push_thresh) { 3967 int push_size; 3968 3969 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3970 bp->tx_push_thresh); 3971 3972 if (push_size > 256) { 3973 push_size = 0; 3974 bp->tx_push_thresh = 0; 3975 } 3976 3977 bp->tx_push_size = push_size; 3978 } 3979 3980 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3981 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3982 struct bnxt_ring_struct *ring; 3983 u8 qidx; 3984 3985 ring = &txr->tx_ring_struct; 3986 3987 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3988 if (rc) 3989 return rc; 3990 3991 ring->grp_idx = txr->bnapi->index; 3992 if (bp->tx_push_size) { 3993 dma_addr_t mapping; 3994 3995 /* One pre-allocated DMA buffer to backup 3996 * TX push operation 3997 */ 3998 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3999 bp->tx_push_size, 4000 &txr->tx_push_mapping, 4001 GFP_KERNEL); 4002 4003 if (!txr->tx_push) 4004 return -ENOMEM; 4005 4006 mapping = txr->tx_push_mapping + 4007 sizeof(struct tx_push_bd); 4008 txr->data_mapping = cpu_to_le64(mapping); 4009 } 4010 qidx = bp->tc_to_qidx[j]; 4011 ring->queue_id = bp->q_info[qidx].queue_id; 4012 spin_lock_init(&txr->xdp_tx_lock); 4013 if (i < bp->tx_nr_rings_xdp) 4014 continue; 4015 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 4016 j++; 4017 } 4018 return 0; 4019 } 4020 4021 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 4022 { 4023 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4024 4025 kfree(cpr->cp_desc_ring); 4026 cpr->cp_desc_ring = NULL; 4027 ring->ring_mem.pg_arr = NULL; 4028 kfree(cpr->cp_desc_mapping); 4029 cpr->cp_desc_mapping = NULL; 4030 ring->ring_mem.dma_arr = NULL; 4031 } 4032 4033 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 4034 { 4035 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 4036 if (!cpr->cp_desc_ring) 4037 return -ENOMEM; 4038 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 4039 GFP_KERNEL); 4040 if (!cpr->cp_desc_mapping) 4041 return -ENOMEM; 4042 return 0; 4043 } 4044 4045 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 4046 { 4047 int i; 4048 4049 if (!bp->bnapi) 4050 return; 4051 for (i = 0; i < bp->cp_nr_rings; i++) { 4052 struct bnxt_napi *bnapi = bp->bnapi[i]; 4053 4054 if (!bnapi) 4055 continue; 4056 bnxt_free_cp_arrays(&bnapi->cp_ring); 4057 } 4058 } 4059 4060 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 4061 { 4062 int i, n = bp->cp_nr_pages; 4063 4064 for (i = 0; i < bp->cp_nr_rings; i++) { 4065 struct bnxt_napi *bnapi = bp->bnapi[i]; 4066 int rc; 4067 4068 if (!bnapi) 4069 continue; 4070 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 4071 if (rc) 4072 return rc; 4073 } 4074 return 0; 4075 } 4076 4077 static void bnxt_free_cp_rings(struct bnxt *bp) 4078 { 4079 int i; 4080 4081 if (!bp->bnapi) 4082 return; 4083 4084 for (i = 0; i < bp->cp_nr_rings; i++) { 4085 struct bnxt_napi *bnapi = bp->bnapi[i]; 4086 struct bnxt_cp_ring_info *cpr; 4087 struct bnxt_ring_struct *ring; 4088 int j; 4089 4090 if (!bnapi) 4091 continue; 4092 4093 cpr = &bnapi->cp_ring; 4094 ring = &cpr->cp_ring_struct; 4095 4096 bnxt_free_ring(bp, &ring->ring_mem); 4097 4098 if (!cpr->cp_ring_arr) 4099 continue; 4100 4101 for (j = 0; j < cpr->cp_ring_count; j++) { 4102 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4103 4104 ring = &cpr2->cp_ring_struct; 4105 bnxt_free_ring(bp, &ring->ring_mem); 4106 bnxt_free_cp_arrays(cpr2); 4107 } 4108 kfree(cpr->cp_ring_arr); 4109 cpr->cp_ring_arr = NULL; 4110 cpr->cp_ring_count = 0; 4111 } 4112 } 4113 4114 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4115 struct bnxt_cp_ring_info *cpr) 4116 { 4117 struct bnxt_ring_mem_info *rmem; 4118 struct bnxt_ring_struct *ring; 4119 int rc; 4120 4121 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4122 if (rc) { 4123 bnxt_free_cp_arrays(cpr); 4124 return -ENOMEM; 4125 } 4126 ring = &cpr->cp_ring_struct; 4127 rmem = &ring->ring_mem; 4128 rmem->nr_pages = bp->cp_nr_pages; 4129 rmem->page_size = HW_CMPD_RING_SIZE; 4130 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4131 rmem->dma_arr = cpr->cp_desc_mapping; 4132 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4133 rc = bnxt_alloc_ring(bp, rmem); 4134 if (rc) { 4135 bnxt_free_ring(bp, rmem); 4136 bnxt_free_cp_arrays(cpr); 4137 } 4138 return rc; 4139 } 4140 4141 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4142 { 4143 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4144 int i, j, rc, ulp_msix; 4145 int tcs = bp->num_tc; 4146 4147 if (!tcs) 4148 tcs = 1; 4149 ulp_msix = bnxt_get_ulp_msix_num(bp); 4150 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4151 struct bnxt_napi *bnapi = bp->bnapi[i]; 4152 struct bnxt_cp_ring_info *cpr, *cpr2; 4153 struct bnxt_ring_struct *ring; 4154 int cp_count = 0, k; 4155 int rx = 0, tx = 0; 4156 4157 if (!bnapi) 4158 continue; 4159 4160 cpr = &bnapi->cp_ring; 4161 cpr->bnapi = bnapi; 4162 ring = &cpr->cp_ring_struct; 4163 4164 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4165 if (rc) 4166 return rc; 4167 4168 ring->map_idx = ulp_msix + i; 4169 4170 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4171 continue; 4172 4173 if (i < bp->rx_nr_rings) { 4174 cp_count++; 4175 rx = 1; 4176 } 4177 if (i < bp->tx_nr_rings_xdp) { 4178 cp_count++; 4179 tx = 1; 4180 } else if ((sh && i < bp->tx_nr_rings) || 4181 (!sh && i >= bp->rx_nr_rings)) { 4182 cp_count += tcs; 4183 tx = 1; 4184 } 4185 4186 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 4187 GFP_KERNEL); 4188 if (!cpr->cp_ring_arr) 4189 return -ENOMEM; 4190 cpr->cp_ring_count = cp_count; 4191 4192 for (k = 0; k < cp_count; k++) { 4193 cpr2 = &cpr->cp_ring_arr[k]; 4194 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4195 if (rc) 4196 return rc; 4197 cpr2->bnapi = bnapi; 4198 cpr2->sw_stats = cpr->sw_stats; 4199 cpr2->cp_idx = k; 4200 if (!k && rx) { 4201 bp->rx_ring[i].rx_cpr = cpr2; 4202 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4203 } else { 4204 int n, tc = k - rx; 4205 4206 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4207 bp->tx_ring[n].tx_cpr = cpr2; 4208 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4209 } 4210 } 4211 if (tx) 4212 j++; 4213 } 4214 return 0; 4215 } 4216 4217 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4218 struct bnxt_rx_ring_info *rxr) 4219 { 4220 struct bnxt_ring_mem_info *rmem; 4221 struct bnxt_ring_struct *ring; 4222 4223 ring = &rxr->rx_ring_struct; 4224 rmem = &ring->ring_mem; 4225 rmem->nr_pages = bp->rx_nr_pages; 4226 rmem->page_size = HW_RXBD_RING_SIZE; 4227 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4228 rmem->dma_arr = rxr->rx_desc_mapping; 4229 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4230 rmem->vmem = (void **)&rxr->rx_buf_ring; 4231 4232 ring = &rxr->rx_agg_ring_struct; 4233 rmem = &ring->ring_mem; 4234 rmem->nr_pages = bp->rx_agg_nr_pages; 4235 rmem->page_size = HW_RXBD_RING_SIZE; 4236 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4237 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4238 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4239 rmem->vmem = (void **)&rxr->rx_agg_ring; 4240 } 4241 4242 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4243 struct bnxt_rx_ring_info *rxr) 4244 { 4245 struct bnxt_ring_mem_info *rmem; 4246 struct bnxt_ring_struct *ring; 4247 int i; 4248 4249 rxr->page_pool->p.napi = NULL; 4250 rxr->page_pool = NULL; 4251 rxr->head_pool->p.napi = NULL; 4252 rxr->head_pool = NULL; 4253 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4254 4255 ring = &rxr->rx_ring_struct; 4256 rmem = &ring->ring_mem; 4257 rmem->pg_tbl = NULL; 4258 rmem->pg_tbl_map = 0; 4259 for (i = 0; i < rmem->nr_pages; i++) { 4260 rmem->pg_arr[i] = NULL; 4261 rmem->dma_arr[i] = 0; 4262 } 4263 *rmem->vmem = NULL; 4264 4265 ring = &rxr->rx_agg_ring_struct; 4266 rmem = &ring->ring_mem; 4267 rmem->pg_tbl = NULL; 4268 rmem->pg_tbl_map = 0; 4269 for (i = 0; i < rmem->nr_pages; i++) { 4270 rmem->pg_arr[i] = NULL; 4271 rmem->dma_arr[i] = 0; 4272 } 4273 *rmem->vmem = NULL; 4274 } 4275 4276 static void bnxt_init_ring_struct(struct bnxt *bp) 4277 { 4278 int i, j; 4279 4280 for (i = 0; i < bp->cp_nr_rings; i++) { 4281 struct bnxt_napi *bnapi = bp->bnapi[i]; 4282 struct bnxt_ring_mem_info *rmem; 4283 struct bnxt_cp_ring_info *cpr; 4284 struct bnxt_rx_ring_info *rxr; 4285 struct bnxt_tx_ring_info *txr; 4286 struct bnxt_ring_struct *ring; 4287 4288 if (!bnapi) 4289 continue; 4290 4291 cpr = &bnapi->cp_ring; 4292 ring = &cpr->cp_ring_struct; 4293 rmem = &ring->ring_mem; 4294 rmem->nr_pages = bp->cp_nr_pages; 4295 rmem->page_size = HW_CMPD_RING_SIZE; 4296 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4297 rmem->dma_arr = cpr->cp_desc_mapping; 4298 rmem->vmem_size = 0; 4299 4300 rxr = bnapi->rx_ring; 4301 if (!rxr) 4302 goto skip_rx; 4303 4304 ring = &rxr->rx_ring_struct; 4305 rmem = &ring->ring_mem; 4306 rmem->nr_pages = bp->rx_nr_pages; 4307 rmem->page_size = HW_RXBD_RING_SIZE; 4308 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4309 rmem->dma_arr = rxr->rx_desc_mapping; 4310 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4311 rmem->vmem = (void **)&rxr->rx_buf_ring; 4312 4313 ring = &rxr->rx_agg_ring_struct; 4314 rmem = &ring->ring_mem; 4315 rmem->nr_pages = bp->rx_agg_nr_pages; 4316 rmem->page_size = HW_RXBD_RING_SIZE; 4317 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4318 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4319 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4320 rmem->vmem = (void **)&rxr->rx_agg_ring; 4321 4322 skip_rx: 4323 bnxt_for_each_napi_tx(j, bnapi, txr) { 4324 ring = &txr->tx_ring_struct; 4325 rmem = &ring->ring_mem; 4326 rmem->nr_pages = bp->tx_nr_pages; 4327 rmem->page_size = HW_TXBD_RING_SIZE; 4328 rmem->pg_arr = (void **)txr->tx_desc_ring; 4329 rmem->dma_arr = txr->tx_desc_mapping; 4330 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4331 rmem->vmem = (void **)&txr->tx_buf_ring; 4332 } 4333 } 4334 } 4335 4336 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4337 { 4338 int i; 4339 u32 prod; 4340 struct rx_bd **rx_buf_ring; 4341 4342 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4343 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4344 int j; 4345 struct rx_bd *rxbd; 4346 4347 rxbd = rx_buf_ring[i]; 4348 if (!rxbd) 4349 continue; 4350 4351 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4352 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4353 rxbd->rx_bd_opaque = prod; 4354 } 4355 } 4356 } 4357 4358 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4359 struct bnxt_rx_ring_info *rxr, 4360 int ring_nr) 4361 { 4362 u32 prod; 4363 int i; 4364 4365 prod = rxr->rx_prod; 4366 for (i = 0; i < bp->rx_ring_size; i++) { 4367 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4368 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4369 ring_nr, i, bp->rx_ring_size); 4370 break; 4371 } 4372 prod = NEXT_RX(prod); 4373 } 4374 rxr->rx_prod = prod; 4375 } 4376 4377 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp, 4378 struct bnxt_rx_ring_info *rxr, 4379 int ring_nr) 4380 { 4381 u32 prod; 4382 int i; 4383 4384 prod = rxr->rx_agg_prod; 4385 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4386 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) { 4387 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4388 ring_nr, i, bp->rx_ring_size); 4389 break; 4390 } 4391 prod = NEXT_RX_AGG(prod); 4392 } 4393 rxr->rx_agg_prod = prod; 4394 } 4395 4396 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4397 struct bnxt_rx_ring_info *rxr) 4398 { 4399 dma_addr_t mapping; 4400 u8 *data; 4401 int i; 4402 4403 for (i = 0; i < bp->max_tpa; i++) { 4404 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4405 GFP_KERNEL); 4406 if (!data) 4407 return -ENOMEM; 4408 4409 rxr->rx_tpa[i].data = data; 4410 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4411 rxr->rx_tpa[i].mapping = mapping; 4412 } 4413 4414 return 0; 4415 } 4416 4417 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4418 { 4419 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4420 int rc; 4421 4422 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4423 4424 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4425 return 0; 4426 4427 bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr); 4428 4429 if (rxr->rx_tpa) { 4430 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4431 if (rc) 4432 return rc; 4433 } 4434 return 0; 4435 } 4436 4437 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4438 struct bnxt_rx_ring_info *rxr) 4439 { 4440 struct bnxt_ring_struct *ring; 4441 u32 type; 4442 4443 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4444 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4445 4446 if (NET_IP_ALIGN == 2) 4447 type |= RX_BD_FLAGS_SOP; 4448 4449 ring = &rxr->rx_ring_struct; 4450 bnxt_init_rxbd_pages(ring, type); 4451 ring->fw_ring_id = INVALID_HW_RING_ID; 4452 } 4453 4454 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4455 struct bnxt_rx_ring_info *rxr) 4456 { 4457 struct bnxt_ring_struct *ring; 4458 u32 type; 4459 4460 ring = &rxr->rx_agg_ring_struct; 4461 ring->fw_ring_id = INVALID_HW_RING_ID; 4462 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4463 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4464 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4465 4466 bnxt_init_rxbd_pages(ring, type); 4467 } 4468 } 4469 4470 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4471 { 4472 struct bnxt_rx_ring_info *rxr; 4473 4474 rxr = &bp->rx_ring[ring_nr]; 4475 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4476 4477 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4478 &rxr->bnapi->napi); 4479 4480 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4481 bpf_prog_add(bp->xdp_prog, 1); 4482 rxr->xdp_prog = bp->xdp_prog; 4483 } 4484 4485 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4486 4487 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4488 } 4489 4490 static void bnxt_init_cp_rings(struct bnxt *bp) 4491 { 4492 int i, j; 4493 4494 for (i = 0; i < bp->cp_nr_rings; i++) { 4495 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4496 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4497 4498 ring->fw_ring_id = INVALID_HW_RING_ID; 4499 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4500 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4501 if (!cpr->cp_ring_arr) 4502 continue; 4503 for (j = 0; j < cpr->cp_ring_count; j++) { 4504 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4505 4506 ring = &cpr2->cp_ring_struct; 4507 ring->fw_ring_id = INVALID_HW_RING_ID; 4508 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4509 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4510 } 4511 } 4512 } 4513 4514 static int bnxt_init_rx_rings(struct bnxt *bp) 4515 { 4516 int i, rc = 0; 4517 4518 if (BNXT_RX_PAGE_MODE(bp)) { 4519 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4520 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4521 } else { 4522 bp->rx_offset = BNXT_RX_OFFSET; 4523 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4524 } 4525 4526 for (i = 0; i < bp->rx_nr_rings; i++) { 4527 rc = bnxt_init_one_rx_ring(bp, i); 4528 if (rc) 4529 break; 4530 } 4531 4532 return rc; 4533 } 4534 4535 static int bnxt_init_tx_rings(struct bnxt *bp) 4536 { 4537 u16 i; 4538 4539 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4540 BNXT_MIN_TX_DESC_CNT); 4541 4542 for (i = 0; i < bp->tx_nr_rings; i++) { 4543 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4544 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4545 4546 ring->fw_ring_id = INVALID_HW_RING_ID; 4547 4548 if (i >= bp->tx_nr_rings_xdp) 4549 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4550 NETDEV_QUEUE_TYPE_TX, 4551 &txr->bnapi->napi); 4552 } 4553 4554 return 0; 4555 } 4556 4557 static void bnxt_free_ring_grps(struct bnxt *bp) 4558 { 4559 kfree(bp->grp_info); 4560 bp->grp_info = NULL; 4561 } 4562 4563 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4564 { 4565 int i; 4566 4567 if (irq_re_init) { 4568 bp->grp_info = kcalloc(bp->cp_nr_rings, 4569 sizeof(struct bnxt_ring_grp_info), 4570 GFP_KERNEL); 4571 if (!bp->grp_info) 4572 return -ENOMEM; 4573 } 4574 for (i = 0; i < bp->cp_nr_rings; i++) { 4575 if (irq_re_init) 4576 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4577 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4578 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4579 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4580 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4581 } 4582 return 0; 4583 } 4584 4585 static void bnxt_free_vnics(struct bnxt *bp) 4586 { 4587 kfree(bp->vnic_info); 4588 bp->vnic_info = NULL; 4589 bp->nr_vnics = 0; 4590 } 4591 4592 static int bnxt_alloc_vnics(struct bnxt *bp) 4593 { 4594 int num_vnics = 1; 4595 4596 #ifdef CONFIG_RFS_ACCEL 4597 if (bp->flags & BNXT_FLAG_RFS) { 4598 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4599 num_vnics++; 4600 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4601 num_vnics += bp->rx_nr_rings; 4602 } 4603 #endif 4604 4605 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4606 num_vnics++; 4607 4608 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4609 GFP_KERNEL); 4610 if (!bp->vnic_info) 4611 return -ENOMEM; 4612 4613 bp->nr_vnics = num_vnics; 4614 return 0; 4615 } 4616 4617 static void bnxt_init_vnics(struct bnxt *bp) 4618 { 4619 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4620 int i; 4621 4622 for (i = 0; i < bp->nr_vnics; i++) { 4623 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4624 int j; 4625 4626 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4627 vnic->vnic_id = i; 4628 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4629 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4630 4631 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4632 4633 if (bp->vnic_info[i].rss_hash_key) { 4634 if (i == BNXT_VNIC_DEFAULT) { 4635 u8 *key = (void *)vnic->rss_hash_key; 4636 int k; 4637 4638 if (!bp->rss_hash_key_valid && 4639 !bp->rss_hash_key_updated) { 4640 get_random_bytes(bp->rss_hash_key, 4641 HW_HASH_KEY_SIZE); 4642 bp->rss_hash_key_updated = true; 4643 } 4644 4645 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4646 HW_HASH_KEY_SIZE); 4647 4648 if (!bp->rss_hash_key_updated) 4649 continue; 4650 4651 bp->rss_hash_key_updated = false; 4652 bp->rss_hash_key_valid = true; 4653 4654 bp->toeplitz_prefix = 0; 4655 for (k = 0; k < 8; k++) { 4656 bp->toeplitz_prefix <<= 8; 4657 bp->toeplitz_prefix |= key[k]; 4658 } 4659 } else { 4660 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4661 HW_HASH_KEY_SIZE); 4662 } 4663 } 4664 } 4665 } 4666 4667 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4668 { 4669 int pages; 4670 4671 pages = ring_size / desc_per_pg; 4672 4673 if (!pages) 4674 return 1; 4675 4676 pages++; 4677 4678 while (pages & (pages - 1)) 4679 pages++; 4680 4681 return pages; 4682 } 4683 4684 void bnxt_set_tpa_flags(struct bnxt *bp) 4685 { 4686 bp->flags &= ~BNXT_FLAG_TPA; 4687 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4688 return; 4689 if (bp->dev->features & NETIF_F_LRO) 4690 bp->flags |= BNXT_FLAG_LRO; 4691 else if (bp->dev->features & NETIF_F_GRO_HW) 4692 bp->flags |= BNXT_FLAG_GRO; 4693 } 4694 4695 static void bnxt_init_ring_params(struct bnxt *bp) 4696 { 4697 unsigned int rx_size; 4698 4699 bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK; 4700 /* Try to fit 4 chunks into a 4k page */ 4701 rx_size = SZ_1K - 4702 NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4703 bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size); 4704 } 4705 4706 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4707 * be set on entry. 4708 */ 4709 void bnxt_set_ring_params(struct bnxt *bp) 4710 { 4711 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4712 u32 agg_factor = 0, agg_ring_size = 0; 4713 4714 /* 8 for CRC and VLAN */ 4715 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4716 4717 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4718 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4719 4720 ring_size = bp->rx_ring_size; 4721 bp->rx_agg_ring_size = 0; 4722 bp->rx_agg_nr_pages = 0; 4723 4724 if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS) 4725 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4726 4727 bp->flags &= ~BNXT_FLAG_JUMBO; 4728 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4729 u32 jumbo_factor; 4730 4731 bp->flags |= BNXT_FLAG_JUMBO; 4732 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4733 if (jumbo_factor > agg_factor) 4734 agg_factor = jumbo_factor; 4735 } 4736 if (agg_factor) { 4737 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4738 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4739 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4740 bp->rx_ring_size, ring_size); 4741 bp->rx_ring_size = ring_size; 4742 } 4743 agg_ring_size = ring_size * agg_factor; 4744 4745 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4746 RX_DESC_CNT); 4747 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4748 u32 tmp = agg_ring_size; 4749 4750 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4751 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4752 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4753 tmp, agg_ring_size); 4754 } 4755 bp->rx_agg_ring_size = agg_ring_size; 4756 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4757 4758 if (BNXT_RX_PAGE_MODE(bp)) { 4759 rx_space = PAGE_SIZE; 4760 rx_size = PAGE_SIZE - 4761 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4762 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4763 } else { 4764 rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK, 4765 bp->rx_copybreak, 4766 bp->dev->cfg_pending->hds_thresh); 4767 rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN); 4768 rx_space = rx_size + NET_SKB_PAD + 4769 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4770 } 4771 } 4772 4773 bp->rx_buf_use_size = rx_size; 4774 bp->rx_buf_size = rx_space; 4775 4776 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4777 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4778 4779 ring_size = bp->tx_ring_size; 4780 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4781 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4782 4783 max_rx_cmpl = bp->rx_ring_size; 4784 /* MAX TPA needs to be added because TPA_START completions are 4785 * immediately recycled, so the TPA completions are not bound by 4786 * the RX ring size. 4787 */ 4788 if (bp->flags & BNXT_FLAG_TPA) 4789 max_rx_cmpl += bp->max_tpa; 4790 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4791 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4792 bp->cp_ring_size = ring_size; 4793 4794 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4795 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4796 bp->cp_nr_pages = MAX_CP_PAGES; 4797 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4798 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4799 ring_size, bp->cp_ring_size); 4800 } 4801 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4802 bp->cp_ring_mask = bp->cp_bit - 1; 4803 } 4804 4805 /* Changing allocation mode of RX rings. 4806 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4807 */ 4808 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4809 { 4810 struct net_device *dev = bp->dev; 4811 4812 if (page_mode) { 4813 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 4814 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4815 4816 if (bp->xdp_prog->aux->xdp_has_frags) 4817 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4818 else 4819 dev->max_mtu = 4820 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4821 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4822 bp->flags |= BNXT_FLAG_JUMBO; 4823 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4824 } else { 4825 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4826 bp->rx_skb_func = bnxt_rx_page_skb; 4827 } 4828 bp->rx_dir = DMA_BIDIRECTIONAL; 4829 } else { 4830 dev->max_mtu = bp->max_mtu; 4831 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4832 bp->rx_dir = DMA_FROM_DEVICE; 4833 bp->rx_skb_func = bnxt_rx_skb; 4834 } 4835 } 4836 4837 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4838 { 4839 __bnxt_set_rx_skb_mode(bp, page_mode); 4840 4841 if (!page_mode) { 4842 int rx, tx; 4843 4844 bnxt_get_max_rings(bp, &rx, &tx, true); 4845 if (rx > 1) { 4846 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 4847 bp->dev->hw_features |= NETIF_F_LRO; 4848 } 4849 } 4850 4851 /* Update LRO and GRO_HW availability */ 4852 netdev_update_features(bp->dev); 4853 } 4854 4855 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4856 { 4857 int i; 4858 struct bnxt_vnic_info *vnic; 4859 struct pci_dev *pdev = bp->pdev; 4860 4861 if (!bp->vnic_info) 4862 return; 4863 4864 for (i = 0; i < bp->nr_vnics; i++) { 4865 vnic = &bp->vnic_info[i]; 4866 4867 kfree(vnic->fw_grp_ids); 4868 vnic->fw_grp_ids = NULL; 4869 4870 kfree(vnic->uc_list); 4871 vnic->uc_list = NULL; 4872 4873 if (vnic->mc_list) { 4874 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4875 vnic->mc_list, vnic->mc_list_mapping); 4876 vnic->mc_list = NULL; 4877 } 4878 4879 if (vnic->rss_table) { 4880 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4881 vnic->rss_table, 4882 vnic->rss_table_dma_addr); 4883 vnic->rss_table = NULL; 4884 } 4885 4886 vnic->rss_hash_key = NULL; 4887 vnic->flags = 0; 4888 } 4889 } 4890 4891 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4892 { 4893 int i, rc = 0, size; 4894 struct bnxt_vnic_info *vnic; 4895 struct pci_dev *pdev = bp->pdev; 4896 int max_rings; 4897 4898 for (i = 0; i < bp->nr_vnics; i++) { 4899 vnic = &bp->vnic_info[i]; 4900 4901 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4902 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4903 4904 if (mem_size > 0) { 4905 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4906 if (!vnic->uc_list) { 4907 rc = -ENOMEM; 4908 goto out; 4909 } 4910 } 4911 } 4912 4913 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4914 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4915 vnic->mc_list = 4916 dma_alloc_coherent(&pdev->dev, 4917 vnic->mc_list_size, 4918 &vnic->mc_list_mapping, 4919 GFP_KERNEL); 4920 if (!vnic->mc_list) { 4921 rc = -ENOMEM; 4922 goto out; 4923 } 4924 } 4925 4926 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4927 goto vnic_skip_grps; 4928 4929 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4930 max_rings = bp->rx_nr_rings; 4931 else 4932 max_rings = 1; 4933 4934 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4935 if (!vnic->fw_grp_ids) { 4936 rc = -ENOMEM; 4937 goto out; 4938 } 4939 vnic_skip_grps: 4940 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4941 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4942 continue; 4943 4944 /* Allocate rss table and hash key */ 4945 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4946 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4947 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4948 4949 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4950 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4951 vnic->rss_table_size, 4952 &vnic->rss_table_dma_addr, 4953 GFP_KERNEL); 4954 if (!vnic->rss_table) { 4955 rc = -ENOMEM; 4956 goto out; 4957 } 4958 4959 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4960 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4961 } 4962 return 0; 4963 4964 out: 4965 return rc; 4966 } 4967 4968 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4969 { 4970 struct bnxt_hwrm_wait_token *token; 4971 4972 dma_pool_destroy(bp->hwrm_dma_pool); 4973 bp->hwrm_dma_pool = NULL; 4974 4975 rcu_read_lock(); 4976 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4977 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4978 rcu_read_unlock(); 4979 } 4980 4981 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 4982 { 4983 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 4984 BNXT_HWRM_DMA_SIZE, 4985 BNXT_HWRM_DMA_ALIGN, 0); 4986 if (!bp->hwrm_dma_pool) 4987 return -ENOMEM; 4988 4989 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4990 4991 return 0; 4992 } 4993 4994 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4995 { 4996 kfree(stats->hw_masks); 4997 stats->hw_masks = NULL; 4998 kfree(stats->sw_stats); 4999 stats->sw_stats = NULL; 5000 if (stats->hw_stats) { 5001 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 5002 stats->hw_stats_map); 5003 stats->hw_stats = NULL; 5004 } 5005 } 5006 5007 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 5008 bool alloc_masks) 5009 { 5010 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 5011 &stats->hw_stats_map, GFP_KERNEL); 5012 if (!stats->hw_stats) 5013 return -ENOMEM; 5014 5015 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 5016 if (!stats->sw_stats) 5017 goto stats_mem_err; 5018 5019 if (alloc_masks) { 5020 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 5021 if (!stats->hw_masks) 5022 goto stats_mem_err; 5023 } 5024 return 0; 5025 5026 stats_mem_err: 5027 bnxt_free_stats_mem(bp, stats); 5028 return -ENOMEM; 5029 } 5030 5031 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 5032 { 5033 int i; 5034 5035 for (i = 0; i < count; i++) 5036 mask_arr[i] = mask; 5037 } 5038 5039 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 5040 { 5041 int i; 5042 5043 for (i = 0; i < count; i++) 5044 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 5045 } 5046 5047 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 5048 struct bnxt_stats_mem *stats) 5049 { 5050 struct hwrm_func_qstats_ext_output *resp; 5051 struct hwrm_func_qstats_ext_input *req; 5052 __le64 *hw_masks; 5053 int rc; 5054 5055 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 5056 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5057 return -EOPNOTSUPP; 5058 5059 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 5060 if (rc) 5061 return rc; 5062 5063 req->fid = cpu_to_le16(0xffff); 5064 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5065 5066 resp = hwrm_req_hold(bp, req); 5067 rc = hwrm_req_send(bp, req); 5068 if (!rc) { 5069 hw_masks = &resp->rx_ucast_pkts; 5070 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 5071 } 5072 hwrm_req_drop(bp, req); 5073 return rc; 5074 } 5075 5076 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 5077 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 5078 5079 static void bnxt_init_stats(struct bnxt *bp) 5080 { 5081 struct bnxt_napi *bnapi = bp->bnapi[0]; 5082 struct bnxt_cp_ring_info *cpr; 5083 struct bnxt_stats_mem *stats; 5084 __le64 *rx_stats, *tx_stats; 5085 int rc, rx_count, tx_count; 5086 u64 *rx_masks, *tx_masks; 5087 u64 mask; 5088 u8 flags; 5089 5090 cpr = &bnapi->cp_ring; 5091 stats = &cpr->stats; 5092 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 5093 if (rc) { 5094 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5095 mask = (1ULL << 48) - 1; 5096 else 5097 mask = -1ULL; 5098 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 5099 } 5100 if (bp->flags & BNXT_FLAG_PORT_STATS) { 5101 stats = &bp->port_stats; 5102 rx_stats = stats->hw_stats; 5103 rx_masks = stats->hw_masks; 5104 rx_count = sizeof(struct rx_port_stats) / 8; 5105 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5106 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5107 tx_count = sizeof(struct tx_port_stats) / 8; 5108 5109 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 5110 rc = bnxt_hwrm_port_qstats(bp, flags); 5111 if (rc) { 5112 mask = (1ULL << 40) - 1; 5113 5114 bnxt_fill_masks(rx_masks, mask, rx_count); 5115 bnxt_fill_masks(tx_masks, mask, tx_count); 5116 } else { 5117 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5118 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5119 bnxt_hwrm_port_qstats(bp, 0); 5120 } 5121 } 5122 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5123 stats = &bp->rx_port_stats_ext; 5124 rx_stats = stats->hw_stats; 5125 rx_masks = stats->hw_masks; 5126 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5127 stats = &bp->tx_port_stats_ext; 5128 tx_stats = stats->hw_stats; 5129 tx_masks = stats->hw_masks; 5130 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5131 5132 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5133 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5134 if (rc) { 5135 mask = (1ULL << 40) - 1; 5136 5137 bnxt_fill_masks(rx_masks, mask, rx_count); 5138 if (tx_stats) 5139 bnxt_fill_masks(tx_masks, mask, tx_count); 5140 } else { 5141 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5142 if (tx_stats) 5143 bnxt_copy_hw_masks(tx_masks, tx_stats, 5144 tx_count); 5145 bnxt_hwrm_port_qstats_ext(bp, 0); 5146 } 5147 } 5148 } 5149 5150 static void bnxt_free_port_stats(struct bnxt *bp) 5151 { 5152 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5153 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5154 5155 bnxt_free_stats_mem(bp, &bp->port_stats); 5156 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5157 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5158 } 5159 5160 static void bnxt_free_ring_stats(struct bnxt *bp) 5161 { 5162 int i; 5163 5164 if (!bp->bnapi) 5165 return; 5166 5167 for (i = 0; i < bp->cp_nr_rings; i++) { 5168 struct bnxt_napi *bnapi = bp->bnapi[i]; 5169 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5170 5171 bnxt_free_stats_mem(bp, &cpr->stats); 5172 5173 kfree(cpr->sw_stats); 5174 cpr->sw_stats = NULL; 5175 } 5176 } 5177 5178 static int bnxt_alloc_stats(struct bnxt *bp) 5179 { 5180 u32 size, i; 5181 int rc; 5182 5183 size = bp->hw_ring_stats_size; 5184 5185 for (i = 0; i < bp->cp_nr_rings; i++) { 5186 struct bnxt_napi *bnapi = bp->bnapi[i]; 5187 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5188 5189 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 5190 if (!cpr->sw_stats) 5191 return -ENOMEM; 5192 5193 cpr->stats.len = size; 5194 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5195 if (rc) 5196 return rc; 5197 5198 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5199 } 5200 5201 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5202 return 0; 5203 5204 if (bp->port_stats.hw_stats) 5205 goto alloc_ext_stats; 5206 5207 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5208 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5209 if (rc) 5210 return rc; 5211 5212 bp->flags |= BNXT_FLAG_PORT_STATS; 5213 5214 alloc_ext_stats: 5215 /* Display extended statistics only if FW supports it */ 5216 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5217 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5218 return 0; 5219 5220 if (bp->rx_port_stats_ext.hw_stats) 5221 goto alloc_tx_ext_stats; 5222 5223 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5224 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5225 /* Extended stats are optional */ 5226 if (rc) 5227 return 0; 5228 5229 alloc_tx_ext_stats: 5230 if (bp->tx_port_stats_ext.hw_stats) 5231 return 0; 5232 5233 if (bp->hwrm_spec_code >= 0x10902 || 5234 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5235 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5236 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5237 /* Extended stats are optional */ 5238 if (rc) 5239 return 0; 5240 } 5241 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5242 return 0; 5243 } 5244 5245 static void bnxt_clear_ring_indices(struct bnxt *bp) 5246 { 5247 int i, j; 5248 5249 if (!bp->bnapi) 5250 return; 5251 5252 for (i = 0; i < bp->cp_nr_rings; i++) { 5253 struct bnxt_napi *bnapi = bp->bnapi[i]; 5254 struct bnxt_cp_ring_info *cpr; 5255 struct bnxt_rx_ring_info *rxr; 5256 struct bnxt_tx_ring_info *txr; 5257 5258 if (!bnapi) 5259 continue; 5260 5261 cpr = &bnapi->cp_ring; 5262 cpr->cp_raw_cons = 0; 5263 5264 bnxt_for_each_napi_tx(j, bnapi, txr) { 5265 txr->tx_prod = 0; 5266 txr->tx_cons = 0; 5267 txr->tx_hw_cons = 0; 5268 } 5269 5270 rxr = bnapi->rx_ring; 5271 if (rxr) { 5272 rxr->rx_prod = 0; 5273 rxr->rx_agg_prod = 0; 5274 rxr->rx_sw_agg_prod = 0; 5275 rxr->rx_next_cons = 0; 5276 } 5277 bnapi->events = 0; 5278 } 5279 } 5280 5281 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5282 { 5283 u8 type = fltr->type, flags = fltr->flags; 5284 5285 INIT_LIST_HEAD(&fltr->list); 5286 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5287 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5288 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5289 } 5290 5291 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5292 { 5293 if (!list_empty(&fltr->list)) 5294 list_del_init(&fltr->list); 5295 } 5296 5297 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5298 { 5299 struct bnxt_filter_base *usr_fltr, *tmp; 5300 5301 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5302 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5303 continue; 5304 bnxt_del_one_usr_fltr(bp, usr_fltr); 5305 } 5306 } 5307 5308 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5309 { 5310 hlist_del(&fltr->hash); 5311 bnxt_del_one_usr_fltr(bp, fltr); 5312 if (fltr->flags) { 5313 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5314 bp->ntp_fltr_count--; 5315 } 5316 kfree(fltr); 5317 } 5318 5319 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5320 { 5321 int i; 5322 5323 netdev_assert_locked(bp->dev); 5324 5325 /* Under netdev instance lock and all our NAPIs have been disabled. 5326 * It's safe to delete the hash table. 5327 */ 5328 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5329 struct hlist_head *head; 5330 struct hlist_node *tmp; 5331 struct bnxt_ntuple_filter *fltr; 5332 5333 head = &bp->ntp_fltr_hash_tbl[i]; 5334 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5335 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5336 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5337 !list_empty(&fltr->base.list))) 5338 continue; 5339 bnxt_del_fltr(bp, &fltr->base); 5340 } 5341 } 5342 if (!all) 5343 return; 5344 5345 bitmap_free(bp->ntp_fltr_bmap); 5346 bp->ntp_fltr_bmap = NULL; 5347 bp->ntp_fltr_count = 0; 5348 } 5349 5350 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5351 { 5352 int i, rc = 0; 5353 5354 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5355 return 0; 5356 5357 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5358 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5359 5360 bp->ntp_fltr_count = 0; 5361 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5362 5363 if (!bp->ntp_fltr_bmap) 5364 rc = -ENOMEM; 5365 5366 return rc; 5367 } 5368 5369 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5370 { 5371 int i; 5372 5373 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5374 struct hlist_head *head; 5375 struct hlist_node *tmp; 5376 struct bnxt_l2_filter *fltr; 5377 5378 head = &bp->l2_fltr_hash_tbl[i]; 5379 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5380 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5381 !list_empty(&fltr->base.list))) 5382 continue; 5383 bnxt_del_fltr(bp, &fltr->base); 5384 } 5385 } 5386 } 5387 5388 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5389 { 5390 int i; 5391 5392 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5393 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5394 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5395 } 5396 5397 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5398 { 5399 bnxt_free_vnic_attributes(bp); 5400 bnxt_free_tx_rings(bp); 5401 bnxt_free_rx_rings(bp); 5402 bnxt_free_cp_rings(bp); 5403 bnxt_free_all_cp_arrays(bp); 5404 bnxt_free_ntp_fltrs(bp, false); 5405 bnxt_free_l2_filters(bp, false); 5406 if (irq_re_init) { 5407 bnxt_free_ring_stats(bp); 5408 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5409 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5410 bnxt_free_port_stats(bp); 5411 bnxt_free_ring_grps(bp); 5412 bnxt_free_vnics(bp); 5413 kfree(bp->tx_ring_map); 5414 bp->tx_ring_map = NULL; 5415 kfree(bp->tx_ring); 5416 bp->tx_ring = NULL; 5417 kfree(bp->rx_ring); 5418 bp->rx_ring = NULL; 5419 kfree(bp->bnapi); 5420 bp->bnapi = NULL; 5421 } else { 5422 bnxt_clear_ring_indices(bp); 5423 } 5424 } 5425 5426 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5427 { 5428 int i, j, rc, size, arr_size; 5429 void *bnapi; 5430 5431 if (irq_re_init) { 5432 /* Allocate bnapi mem pointer array and mem block for 5433 * all queues 5434 */ 5435 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5436 bp->cp_nr_rings); 5437 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5438 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5439 if (!bnapi) 5440 return -ENOMEM; 5441 5442 bp->bnapi = bnapi; 5443 bnapi += arr_size; 5444 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5445 bp->bnapi[i] = bnapi; 5446 bp->bnapi[i]->index = i; 5447 bp->bnapi[i]->bp = bp; 5448 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5449 struct bnxt_cp_ring_info *cpr = 5450 &bp->bnapi[i]->cp_ring; 5451 5452 cpr->cp_ring_struct.ring_mem.flags = 5453 BNXT_RMEM_RING_PTE_FLAG; 5454 } 5455 } 5456 5457 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5458 sizeof(struct bnxt_rx_ring_info), 5459 GFP_KERNEL); 5460 if (!bp->rx_ring) 5461 return -ENOMEM; 5462 5463 for (i = 0; i < bp->rx_nr_rings; i++) { 5464 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5465 5466 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5467 rxr->rx_ring_struct.ring_mem.flags = 5468 BNXT_RMEM_RING_PTE_FLAG; 5469 rxr->rx_agg_ring_struct.ring_mem.flags = 5470 BNXT_RMEM_RING_PTE_FLAG; 5471 } else { 5472 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5473 } 5474 rxr->bnapi = bp->bnapi[i]; 5475 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5476 } 5477 5478 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5479 sizeof(struct bnxt_tx_ring_info), 5480 GFP_KERNEL); 5481 if (!bp->tx_ring) 5482 return -ENOMEM; 5483 5484 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5485 GFP_KERNEL); 5486 5487 if (!bp->tx_ring_map) 5488 return -ENOMEM; 5489 5490 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5491 j = 0; 5492 else 5493 j = bp->rx_nr_rings; 5494 5495 for (i = 0; i < bp->tx_nr_rings; i++) { 5496 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5497 struct bnxt_napi *bnapi2; 5498 5499 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5500 txr->tx_ring_struct.ring_mem.flags = 5501 BNXT_RMEM_RING_PTE_FLAG; 5502 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5503 if (i >= bp->tx_nr_rings_xdp) { 5504 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5505 5506 bnapi2 = bp->bnapi[k]; 5507 txr->txq_index = i - bp->tx_nr_rings_xdp; 5508 txr->tx_napi_idx = 5509 BNXT_RING_TO_TC(bp, txr->txq_index); 5510 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5511 bnapi2->tx_int = bnxt_tx_int; 5512 } else { 5513 bnapi2 = bp->bnapi[j]; 5514 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5515 bnapi2->tx_ring[0] = txr; 5516 bnapi2->tx_int = bnxt_tx_int_xdp; 5517 j++; 5518 } 5519 txr->bnapi = bnapi2; 5520 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5521 txr->tx_cpr = &bnapi2->cp_ring; 5522 } 5523 5524 rc = bnxt_alloc_stats(bp); 5525 if (rc) 5526 goto alloc_mem_err; 5527 bnxt_init_stats(bp); 5528 5529 rc = bnxt_alloc_ntp_fltrs(bp); 5530 if (rc) 5531 goto alloc_mem_err; 5532 5533 rc = bnxt_alloc_vnics(bp); 5534 if (rc) 5535 goto alloc_mem_err; 5536 } 5537 5538 rc = bnxt_alloc_all_cp_arrays(bp); 5539 if (rc) 5540 goto alloc_mem_err; 5541 5542 bnxt_init_ring_struct(bp); 5543 5544 rc = bnxt_alloc_rx_rings(bp); 5545 if (rc) 5546 goto alloc_mem_err; 5547 5548 rc = bnxt_alloc_tx_rings(bp); 5549 if (rc) 5550 goto alloc_mem_err; 5551 5552 rc = bnxt_alloc_cp_rings(bp); 5553 if (rc) 5554 goto alloc_mem_err; 5555 5556 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5557 BNXT_VNIC_MCAST_FLAG | 5558 BNXT_VNIC_UCAST_FLAG; 5559 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5560 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5561 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5562 5563 rc = bnxt_alloc_vnic_attributes(bp); 5564 if (rc) 5565 goto alloc_mem_err; 5566 return 0; 5567 5568 alloc_mem_err: 5569 bnxt_free_mem(bp, true); 5570 return rc; 5571 } 5572 5573 static void bnxt_disable_int(struct bnxt *bp) 5574 { 5575 int i; 5576 5577 if (!bp->bnapi) 5578 return; 5579 5580 for (i = 0; i < bp->cp_nr_rings; i++) { 5581 struct bnxt_napi *bnapi = bp->bnapi[i]; 5582 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5583 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5584 5585 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5586 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5587 } 5588 } 5589 5590 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5591 { 5592 struct bnxt_napi *bnapi = bp->bnapi[n]; 5593 struct bnxt_cp_ring_info *cpr; 5594 5595 cpr = &bnapi->cp_ring; 5596 return cpr->cp_ring_struct.map_idx; 5597 } 5598 5599 static void bnxt_disable_int_sync(struct bnxt *bp) 5600 { 5601 int i; 5602 5603 if (!bp->irq_tbl) 5604 return; 5605 5606 atomic_inc(&bp->intr_sem); 5607 5608 bnxt_disable_int(bp); 5609 for (i = 0; i < bp->cp_nr_rings; i++) { 5610 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5611 5612 synchronize_irq(bp->irq_tbl[map_idx].vector); 5613 } 5614 } 5615 5616 static void bnxt_enable_int(struct bnxt *bp) 5617 { 5618 int i; 5619 5620 atomic_set(&bp->intr_sem, 0); 5621 for (i = 0; i < bp->cp_nr_rings; i++) { 5622 struct bnxt_napi *bnapi = bp->bnapi[i]; 5623 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5624 5625 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5626 } 5627 } 5628 5629 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5630 bool async_only) 5631 { 5632 DECLARE_BITMAP(async_events_bmap, 256); 5633 u32 *events = (u32 *)async_events_bmap; 5634 struct hwrm_func_drv_rgtr_output *resp; 5635 struct hwrm_func_drv_rgtr_input *req; 5636 u32 flags; 5637 int rc, i; 5638 5639 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5640 if (rc) 5641 return rc; 5642 5643 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5644 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5645 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5646 5647 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5648 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5649 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5650 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5651 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5652 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5653 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5654 if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2) 5655 flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT; 5656 req->flags = cpu_to_le32(flags); 5657 req->ver_maj_8b = DRV_VER_MAJ; 5658 req->ver_min_8b = DRV_VER_MIN; 5659 req->ver_upd_8b = DRV_VER_UPD; 5660 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5661 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5662 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5663 5664 if (BNXT_PF(bp)) { 5665 u32 data[8]; 5666 int i; 5667 5668 memset(data, 0, sizeof(data)); 5669 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5670 u16 cmd = bnxt_vf_req_snif[i]; 5671 unsigned int bit, idx; 5672 5673 idx = cmd / 32; 5674 bit = cmd % 32; 5675 data[idx] |= 1 << bit; 5676 } 5677 5678 for (i = 0; i < 8; i++) 5679 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5680 5681 req->enables |= 5682 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5683 } 5684 5685 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5686 req->flags |= cpu_to_le32( 5687 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5688 5689 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5690 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5691 u16 event_id = bnxt_async_events_arr[i]; 5692 5693 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5694 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5695 continue; 5696 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5697 !bp->ptp_cfg) 5698 continue; 5699 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5700 } 5701 if (bmap && bmap_size) { 5702 for (i = 0; i < bmap_size; i++) { 5703 if (test_bit(i, bmap)) 5704 __set_bit(i, async_events_bmap); 5705 } 5706 } 5707 for (i = 0; i < 8; i++) 5708 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5709 5710 if (async_only) 5711 req->enables = 5712 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5713 5714 resp = hwrm_req_hold(bp, req); 5715 rc = hwrm_req_send(bp, req); 5716 if (!rc) { 5717 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5718 if (resp->flags & 5719 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5720 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5721 } 5722 hwrm_req_drop(bp, req); 5723 return rc; 5724 } 5725 5726 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5727 { 5728 struct hwrm_func_drv_unrgtr_input *req; 5729 int rc; 5730 5731 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5732 return 0; 5733 5734 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5735 if (rc) 5736 return rc; 5737 return hwrm_req_send(bp, req); 5738 } 5739 5740 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5741 5742 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5743 { 5744 struct hwrm_tunnel_dst_port_free_input *req; 5745 int rc; 5746 5747 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5748 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5749 return 0; 5750 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5751 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5752 return 0; 5753 5754 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5755 if (rc) 5756 return rc; 5757 5758 req->tunnel_type = tunnel_type; 5759 5760 switch (tunnel_type) { 5761 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5762 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5763 bp->vxlan_port = 0; 5764 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5765 break; 5766 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5767 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5768 bp->nge_port = 0; 5769 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5770 break; 5771 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5772 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5773 bp->vxlan_gpe_port = 0; 5774 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5775 break; 5776 default: 5777 break; 5778 } 5779 5780 rc = hwrm_req_send(bp, req); 5781 if (rc) 5782 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5783 rc); 5784 if (bp->flags & BNXT_FLAG_TPA) 5785 bnxt_set_tpa(bp, true); 5786 return rc; 5787 } 5788 5789 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5790 u8 tunnel_type) 5791 { 5792 struct hwrm_tunnel_dst_port_alloc_output *resp; 5793 struct hwrm_tunnel_dst_port_alloc_input *req; 5794 int rc; 5795 5796 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5797 if (rc) 5798 return rc; 5799 5800 req->tunnel_type = tunnel_type; 5801 req->tunnel_dst_port_val = port; 5802 5803 resp = hwrm_req_hold(bp, req); 5804 rc = hwrm_req_send(bp, req); 5805 if (rc) { 5806 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5807 rc); 5808 goto err_out; 5809 } 5810 5811 switch (tunnel_type) { 5812 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5813 bp->vxlan_port = port; 5814 bp->vxlan_fw_dst_port_id = 5815 le16_to_cpu(resp->tunnel_dst_port_id); 5816 break; 5817 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5818 bp->nge_port = port; 5819 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5820 break; 5821 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5822 bp->vxlan_gpe_port = port; 5823 bp->vxlan_gpe_fw_dst_port_id = 5824 le16_to_cpu(resp->tunnel_dst_port_id); 5825 break; 5826 default: 5827 break; 5828 } 5829 if (bp->flags & BNXT_FLAG_TPA) 5830 bnxt_set_tpa(bp, true); 5831 5832 err_out: 5833 hwrm_req_drop(bp, req); 5834 return rc; 5835 } 5836 5837 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5838 { 5839 struct hwrm_cfa_l2_set_rx_mask_input *req; 5840 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5841 int rc; 5842 5843 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5844 if (rc) 5845 return rc; 5846 5847 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5848 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5849 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5850 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5851 } 5852 req->mask = cpu_to_le32(vnic->rx_mask); 5853 return hwrm_req_send_silent(bp, req); 5854 } 5855 5856 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5857 { 5858 if (!atomic_dec_and_test(&fltr->refcnt)) 5859 return; 5860 spin_lock_bh(&bp->ntp_fltr_lock); 5861 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5862 spin_unlock_bh(&bp->ntp_fltr_lock); 5863 return; 5864 } 5865 hlist_del_rcu(&fltr->base.hash); 5866 bnxt_del_one_usr_fltr(bp, &fltr->base); 5867 if (fltr->base.flags) { 5868 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5869 bp->ntp_fltr_count--; 5870 } 5871 spin_unlock_bh(&bp->ntp_fltr_lock); 5872 kfree_rcu(fltr, base.rcu); 5873 } 5874 5875 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5876 struct bnxt_l2_key *key, 5877 u32 idx) 5878 { 5879 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5880 struct bnxt_l2_filter *fltr; 5881 5882 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5883 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5884 5885 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5886 l2_key->vlan == key->vlan) 5887 return fltr; 5888 } 5889 return NULL; 5890 } 5891 5892 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5893 struct bnxt_l2_key *key, 5894 u32 idx) 5895 { 5896 struct bnxt_l2_filter *fltr = NULL; 5897 5898 rcu_read_lock(); 5899 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5900 if (fltr) 5901 atomic_inc(&fltr->refcnt); 5902 rcu_read_unlock(); 5903 return fltr; 5904 } 5905 5906 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5907 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5908 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5909 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5910 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5911 5912 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5913 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5914 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5915 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5916 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5917 5918 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5919 { 5920 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5921 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5922 return sizeof(fkeys->addrs.v4addrs) + 5923 sizeof(fkeys->ports); 5924 5925 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5926 return sizeof(fkeys->addrs.v4addrs); 5927 } 5928 5929 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5930 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5931 return sizeof(fkeys->addrs.v6addrs) + 5932 sizeof(fkeys->ports); 5933 5934 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5935 return sizeof(fkeys->addrs.v6addrs); 5936 } 5937 5938 return 0; 5939 } 5940 5941 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5942 const unsigned char *key) 5943 { 5944 u64 prefix = bp->toeplitz_prefix, hash = 0; 5945 struct bnxt_ipv4_tuple tuple4; 5946 struct bnxt_ipv6_tuple tuple6; 5947 int i, j, len = 0; 5948 u8 *four_tuple; 5949 5950 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5951 if (!len) 5952 return 0; 5953 5954 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5955 tuple4.v4addrs = fkeys->addrs.v4addrs; 5956 tuple4.ports = fkeys->ports; 5957 four_tuple = (unsigned char *)&tuple4; 5958 } else { 5959 tuple6.v6addrs = fkeys->addrs.v6addrs; 5960 tuple6.ports = fkeys->ports; 5961 four_tuple = (unsigned char *)&tuple6; 5962 } 5963 5964 for (i = 0, j = 8; i < len; i++, j++) { 5965 u8 byte = four_tuple[i]; 5966 int bit; 5967 5968 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5969 if (byte & 0x80) 5970 hash ^= prefix; 5971 } 5972 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5973 } 5974 5975 /* The valid part of the hash is in the upper 32 bits. */ 5976 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5977 } 5978 5979 #ifdef CONFIG_RFS_ACCEL 5980 static struct bnxt_l2_filter * 5981 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 5982 { 5983 struct bnxt_l2_filter *fltr; 5984 u32 idx; 5985 5986 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5987 BNXT_L2_FLTR_HASH_MASK; 5988 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5989 return fltr; 5990 } 5991 #endif 5992 5993 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 5994 struct bnxt_l2_key *key, u32 idx) 5995 { 5996 struct hlist_head *head; 5997 5998 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 5999 fltr->l2_key.vlan = key->vlan; 6000 fltr->base.type = BNXT_FLTR_TYPE_L2; 6001 if (fltr->base.flags) { 6002 int bit_id; 6003 6004 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 6005 bp->max_fltr, 0); 6006 if (bit_id < 0) 6007 return -ENOMEM; 6008 fltr->base.sw_id = (u16)bit_id; 6009 bp->ntp_fltr_count++; 6010 } 6011 head = &bp->l2_fltr_hash_tbl[idx]; 6012 hlist_add_head_rcu(&fltr->base.hash, head); 6013 bnxt_insert_usr_fltr(bp, &fltr->base); 6014 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 6015 atomic_set(&fltr->refcnt, 1); 6016 return 0; 6017 } 6018 6019 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 6020 struct bnxt_l2_key *key, 6021 gfp_t gfp) 6022 { 6023 struct bnxt_l2_filter *fltr; 6024 u32 idx; 6025 int rc; 6026 6027 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6028 BNXT_L2_FLTR_HASH_MASK; 6029 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6030 if (fltr) 6031 return fltr; 6032 6033 fltr = kzalloc(sizeof(*fltr), gfp); 6034 if (!fltr) 6035 return ERR_PTR(-ENOMEM); 6036 spin_lock_bh(&bp->ntp_fltr_lock); 6037 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6038 spin_unlock_bh(&bp->ntp_fltr_lock); 6039 if (rc) { 6040 bnxt_del_l2_filter(bp, fltr); 6041 fltr = ERR_PTR(rc); 6042 } 6043 return fltr; 6044 } 6045 6046 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 6047 struct bnxt_l2_key *key, 6048 u16 flags) 6049 { 6050 struct bnxt_l2_filter *fltr; 6051 u32 idx; 6052 int rc; 6053 6054 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6055 BNXT_L2_FLTR_HASH_MASK; 6056 spin_lock_bh(&bp->ntp_fltr_lock); 6057 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6058 if (fltr) { 6059 fltr = ERR_PTR(-EEXIST); 6060 goto l2_filter_exit; 6061 } 6062 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 6063 if (!fltr) { 6064 fltr = ERR_PTR(-ENOMEM); 6065 goto l2_filter_exit; 6066 } 6067 fltr->base.flags = flags; 6068 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6069 if (rc) { 6070 spin_unlock_bh(&bp->ntp_fltr_lock); 6071 bnxt_del_l2_filter(bp, fltr); 6072 return ERR_PTR(rc); 6073 } 6074 6075 l2_filter_exit: 6076 spin_unlock_bh(&bp->ntp_fltr_lock); 6077 return fltr; 6078 } 6079 6080 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 6081 { 6082 #ifdef CONFIG_BNXT_SRIOV 6083 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 6084 6085 return vf->fw_fid; 6086 #else 6087 return INVALID_HW_RING_ID; 6088 #endif 6089 } 6090 6091 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6092 { 6093 struct hwrm_cfa_l2_filter_free_input *req; 6094 u16 target_id = 0xffff; 6095 int rc; 6096 6097 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6098 struct bnxt_pf_info *pf = &bp->pf; 6099 6100 if (fltr->base.vf_idx >= pf->active_vfs) 6101 return -EINVAL; 6102 6103 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6104 if (target_id == INVALID_HW_RING_ID) 6105 return -EINVAL; 6106 } 6107 6108 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 6109 if (rc) 6110 return rc; 6111 6112 req->target_id = cpu_to_le16(target_id); 6113 req->l2_filter_id = fltr->base.filter_id; 6114 return hwrm_req_send(bp, req); 6115 } 6116 6117 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6118 { 6119 struct hwrm_cfa_l2_filter_alloc_output *resp; 6120 struct hwrm_cfa_l2_filter_alloc_input *req; 6121 u16 target_id = 0xffff; 6122 int rc; 6123 6124 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6125 struct bnxt_pf_info *pf = &bp->pf; 6126 6127 if (fltr->base.vf_idx >= pf->active_vfs) 6128 return -EINVAL; 6129 6130 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6131 } 6132 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6133 if (rc) 6134 return rc; 6135 6136 req->target_id = cpu_to_le16(target_id); 6137 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6138 6139 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6140 req->flags |= 6141 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6142 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6143 req->enables = 6144 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6145 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6146 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6147 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6148 eth_broadcast_addr(req->l2_addr_mask); 6149 6150 if (fltr->l2_key.vlan) { 6151 req->enables |= 6152 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6153 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6154 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6155 req->num_vlans = 1; 6156 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6157 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6158 } 6159 6160 resp = hwrm_req_hold(bp, req); 6161 rc = hwrm_req_send(bp, req); 6162 if (!rc) { 6163 fltr->base.filter_id = resp->l2_filter_id; 6164 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6165 } 6166 hwrm_req_drop(bp, req); 6167 return rc; 6168 } 6169 6170 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6171 struct bnxt_ntuple_filter *fltr) 6172 { 6173 struct hwrm_cfa_ntuple_filter_free_input *req; 6174 int rc; 6175 6176 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6177 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6178 if (rc) 6179 return rc; 6180 6181 req->ntuple_filter_id = fltr->base.filter_id; 6182 return hwrm_req_send(bp, req); 6183 } 6184 6185 #define BNXT_NTP_FLTR_FLAGS \ 6186 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6187 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6188 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6189 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6190 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6191 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6192 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6193 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6194 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6195 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6196 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6197 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6198 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6199 6200 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6201 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6202 6203 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6204 { 6205 int i; 6206 6207 for (i = 0; i < 4; i++) 6208 mask[i] = cpu_to_be32(~0); 6209 } 6210 6211 static void 6212 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6213 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6214 struct bnxt_ntuple_filter *fltr) 6215 { 6216 u16 rxq = fltr->base.rxq; 6217 6218 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6219 struct ethtool_rxfh_context *ctx; 6220 struct bnxt_rss_ctx *rss_ctx; 6221 struct bnxt_vnic_info *vnic; 6222 6223 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6224 fltr->base.fw_vnic_id); 6225 if (ctx) { 6226 rss_ctx = ethtool_rxfh_context_priv(ctx); 6227 vnic = &rss_ctx->vnic; 6228 6229 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6230 } 6231 return; 6232 } 6233 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6234 struct bnxt_vnic_info *vnic; 6235 u32 enables; 6236 6237 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6238 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6239 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6240 req->enables |= cpu_to_le32(enables); 6241 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6242 } else { 6243 u32 flags; 6244 6245 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6246 req->flags |= cpu_to_le32(flags); 6247 req->dst_id = cpu_to_le16(rxq); 6248 } 6249 } 6250 6251 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6252 struct bnxt_ntuple_filter *fltr) 6253 { 6254 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6255 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6256 struct bnxt_flow_masks *masks = &fltr->fmasks; 6257 struct flow_keys *keys = &fltr->fkeys; 6258 struct bnxt_l2_filter *l2_fltr; 6259 struct bnxt_vnic_info *vnic; 6260 int rc; 6261 6262 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6263 if (rc) 6264 return rc; 6265 6266 l2_fltr = fltr->l2_fltr; 6267 req->l2_filter_id = l2_fltr->base.filter_id; 6268 6269 if (fltr->base.flags & BNXT_ACT_DROP) { 6270 req->flags = 6271 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6272 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6273 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6274 } else { 6275 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6276 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6277 } 6278 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6279 6280 req->ethertype = htons(ETH_P_IP); 6281 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6282 req->ip_protocol = keys->basic.ip_proto; 6283 6284 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6285 req->ethertype = htons(ETH_P_IPV6); 6286 req->ip_addr_type = 6287 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6288 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6289 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6290 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6291 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6292 } else { 6293 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6294 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6295 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6296 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6297 } 6298 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6299 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6300 req->tunnel_type = 6301 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6302 } 6303 6304 req->src_port = keys->ports.src; 6305 req->src_port_mask = masks->ports.src; 6306 req->dst_port = keys->ports.dst; 6307 req->dst_port_mask = masks->ports.dst; 6308 6309 resp = hwrm_req_hold(bp, req); 6310 rc = hwrm_req_send(bp, req); 6311 if (!rc) 6312 fltr->base.filter_id = resp->ntuple_filter_id; 6313 hwrm_req_drop(bp, req); 6314 return rc; 6315 } 6316 6317 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6318 const u8 *mac_addr) 6319 { 6320 struct bnxt_l2_filter *fltr; 6321 struct bnxt_l2_key key; 6322 int rc; 6323 6324 ether_addr_copy(key.dst_mac_addr, mac_addr); 6325 key.vlan = 0; 6326 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6327 if (IS_ERR(fltr)) 6328 return PTR_ERR(fltr); 6329 6330 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6331 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6332 if (rc) 6333 bnxt_del_l2_filter(bp, fltr); 6334 else 6335 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6336 return rc; 6337 } 6338 6339 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6340 { 6341 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6342 6343 /* Any associated ntuple filters will also be cleared by firmware. */ 6344 for (i = 0; i < num_of_vnics; i++) { 6345 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6346 6347 for (j = 0; j < vnic->uc_filter_count; j++) { 6348 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6349 6350 bnxt_hwrm_l2_filter_free(bp, fltr); 6351 bnxt_del_l2_filter(bp, fltr); 6352 } 6353 vnic->uc_filter_count = 0; 6354 } 6355 } 6356 6357 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6358 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6359 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6360 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6361 6362 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6363 struct hwrm_vnic_tpa_cfg_input *req) 6364 { 6365 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6366 6367 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6368 return; 6369 6370 if (bp->vxlan_port) 6371 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6372 if (bp->vxlan_gpe_port) 6373 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6374 if (bp->nge_port) 6375 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6376 6377 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6378 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6379 } 6380 6381 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6382 u32 tpa_flags) 6383 { 6384 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6385 struct hwrm_vnic_tpa_cfg_input *req; 6386 int rc; 6387 6388 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6389 return 0; 6390 6391 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6392 if (rc) 6393 return rc; 6394 6395 if (tpa_flags) { 6396 u16 mss = bp->dev->mtu - 40; 6397 u32 nsegs, n, segs = 0, flags; 6398 6399 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6400 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6401 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6402 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6403 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6404 if (tpa_flags & BNXT_FLAG_GRO) 6405 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6406 6407 req->flags = cpu_to_le32(flags); 6408 6409 req->enables = 6410 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6411 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6412 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6413 6414 /* Number of segs are log2 units, and first packet is not 6415 * included as part of this units. 6416 */ 6417 if (mss <= BNXT_RX_PAGE_SIZE) { 6418 n = BNXT_RX_PAGE_SIZE / mss; 6419 nsegs = (MAX_SKB_FRAGS - 1) * n; 6420 } else { 6421 n = mss / BNXT_RX_PAGE_SIZE; 6422 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6423 n++; 6424 nsegs = (MAX_SKB_FRAGS - n) / n; 6425 } 6426 6427 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6428 segs = MAX_TPA_SEGS_P5; 6429 max_aggs = bp->max_tpa; 6430 } else { 6431 segs = ilog2(nsegs); 6432 } 6433 req->max_agg_segs = cpu_to_le16(segs); 6434 req->max_aggs = cpu_to_le16(max_aggs); 6435 6436 req->min_agg_len = cpu_to_le32(512); 6437 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6438 } 6439 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6440 6441 return hwrm_req_send(bp, req); 6442 } 6443 6444 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6445 { 6446 struct bnxt_ring_grp_info *grp_info; 6447 6448 grp_info = &bp->grp_info[ring->grp_idx]; 6449 return grp_info->cp_fw_ring_id; 6450 } 6451 6452 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6453 { 6454 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6455 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6456 else 6457 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6458 } 6459 6460 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6461 { 6462 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6463 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6464 else 6465 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6466 } 6467 6468 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6469 { 6470 int entries; 6471 6472 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6473 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6474 else 6475 entries = HW_HASH_INDEX_SIZE; 6476 6477 bp->rss_indir_tbl_entries = entries; 6478 bp->rss_indir_tbl = 6479 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6480 if (!bp->rss_indir_tbl) 6481 return -ENOMEM; 6482 6483 return 0; 6484 } 6485 6486 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6487 struct ethtool_rxfh_context *rss_ctx) 6488 { 6489 u16 max_rings, max_entries, pad, i; 6490 u32 *rss_indir_tbl; 6491 6492 if (!bp->rx_nr_rings) 6493 return; 6494 6495 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6496 max_rings = bp->rx_nr_rings - 1; 6497 else 6498 max_rings = bp->rx_nr_rings; 6499 6500 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6501 if (rss_ctx) 6502 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6503 else 6504 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6505 6506 for (i = 0; i < max_entries; i++) 6507 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6508 6509 pad = bp->rss_indir_tbl_entries - max_entries; 6510 if (pad) 6511 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6512 } 6513 6514 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6515 { 6516 u32 i, tbl_size, max_ring = 0; 6517 6518 if (!bp->rss_indir_tbl) 6519 return 0; 6520 6521 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6522 for (i = 0; i < tbl_size; i++) 6523 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6524 return max_ring; 6525 } 6526 6527 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6528 { 6529 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6530 if (!rx_rings) 6531 return 0; 6532 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6533 BNXT_RSS_TABLE_ENTRIES_P5); 6534 } 6535 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6536 return 2; 6537 return 1; 6538 } 6539 6540 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6541 { 6542 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6543 u16 i, j; 6544 6545 /* Fill the RSS indirection table with ring group ids */ 6546 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6547 if (!no_rss) 6548 j = bp->rss_indir_tbl[i]; 6549 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6550 } 6551 } 6552 6553 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6554 struct bnxt_vnic_info *vnic) 6555 { 6556 __le16 *ring_tbl = vnic->rss_table; 6557 struct bnxt_rx_ring_info *rxr; 6558 u16 tbl_size, i; 6559 6560 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6561 6562 for (i = 0; i < tbl_size; i++) { 6563 u16 ring_id, j; 6564 6565 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6566 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6567 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6568 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6569 else 6570 j = bp->rss_indir_tbl[i]; 6571 rxr = &bp->rx_ring[j]; 6572 6573 ring_id = rxr->rx_ring_struct.fw_ring_id; 6574 *ring_tbl++ = cpu_to_le16(ring_id); 6575 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6576 *ring_tbl++ = cpu_to_le16(ring_id); 6577 } 6578 } 6579 6580 static void 6581 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6582 struct bnxt_vnic_info *vnic) 6583 { 6584 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6585 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6586 if (bp->flags & BNXT_FLAG_CHIP_P7) 6587 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6588 } else { 6589 bnxt_fill_hw_rss_tbl(bp, vnic); 6590 } 6591 6592 if (bp->rss_hash_delta) { 6593 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6594 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6595 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6596 else 6597 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6598 } else { 6599 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6600 } 6601 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6602 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6603 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6604 } 6605 6606 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6607 bool set_rss) 6608 { 6609 struct hwrm_vnic_rss_cfg_input *req; 6610 int rc; 6611 6612 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6613 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6614 return 0; 6615 6616 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6617 if (rc) 6618 return rc; 6619 6620 if (set_rss) 6621 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6622 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6623 return hwrm_req_send(bp, req); 6624 } 6625 6626 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6627 struct bnxt_vnic_info *vnic, bool set_rss) 6628 { 6629 struct hwrm_vnic_rss_cfg_input *req; 6630 dma_addr_t ring_tbl_map; 6631 u32 i, nr_ctxs; 6632 int rc; 6633 6634 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6635 if (rc) 6636 return rc; 6637 6638 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6639 if (!set_rss) 6640 return hwrm_req_send(bp, req); 6641 6642 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6643 ring_tbl_map = vnic->rss_table_dma_addr; 6644 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6645 6646 hwrm_req_hold(bp, req); 6647 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6648 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6649 req->ring_table_pair_index = i; 6650 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6651 rc = hwrm_req_send(bp, req); 6652 if (rc) 6653 goto exit; 6654 } 6655 6656 exit: 6657 hwrm_req_drop(bp, req); 6658 return rc; 6659 } 6660 6661 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6662 { 6663 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6664 struct hwrm_vnic_rss_qcfg_output *resp; 6665 struct hwrm_vnic_rss_qcfg_input *req; 6666 6667 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6668 return; 6669 6670 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6671 /* all contexts configured to same hash_type, zero always exists */ 6672 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6673 resp = hwrm_req_hold(bp, req); 6674 if (!hwrm_req_send(bp, req)) { 6675 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6676 bp->rss_hash_delta = 0; 6677 } 6678 hwrm_req_drop(bp, req); 6679 } 6680 6681 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6682 { 6683 u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh; 6684 struct hwrm_vnic_plcmodes_cfg_input *req; 6685 int rc; 6686 6687 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6688 if (rc) 6689 return rc; 6690 6691 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6692 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6693 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6694 6695 if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 6696 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6697 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6698 req->enables |= 6699 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6700 req->hds_threshold = cpu_to_le16(hds_thresh); 6701 } 6702 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6703 return hwrm_req_send(bp, req); 6704 } 6705 6706 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6707 struct bnxt_vnic_info *vnic, 6708 u16 ctx_idx) 6709 { 6710 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6711 6712 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6713 return; 6714 6715 req->rss_cos_lb_ctx_id = 6716 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6717 6718 hwrm_req_send(bp, req); 6719 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6720 } 6721 6722 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6723 { 6724 int i, j; 6725 6726 for (i = 0; i < bp->nr_vnics; i++) { 6727 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6728 6729 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6730 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6731 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6732 } 6733 } 6734 bp->rsscos_nr_ctxs = 0; 6735 } 6736 6737 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6738 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6739 { 6740 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6741 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6742 int rc; 6743 6744 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6745 if (rc) 6746 return rc; 6747 6748 resp = hwrm_req_hold(bp, req); 6749 rc = hwrm_req_send(bp, req); 6750 if (!rc) 6751 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6752 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6753 hwrm_req_drop(bp, req); 6754 6755 return rc; 6756 } 6757 6758 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6759 { 6760 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6761 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6762 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6763 } 6764 6765 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6766 { 6767 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6768 struct hwrm_vnic_cfg_input *req; 6769 unsigned int ring = 0, grp_idx; 6770 u16 def_vlan = 0; 6771 int rc; 6772 6773 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6774 if (rc) 6775 return rc; 6776 6777 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6778 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6779 6780 req->default_rx_ring_id = 6781 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6782 req->default_cmpl_ring_id = 6783 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6784 req->enables = 6785 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6786 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6787 goto vnic_mru; 6788 } 6789 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6790 /* Only RSS support for now TBD: COS & LB */ 6791 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6792 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6793 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6794 VNIC_CFG_REQ_ENABLES_MRU); 6795 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6796 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6797 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6798 VNIC_CFG_REQ_ENABLES_MRU); 6799 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6800 } else { 6801 req->rss_rule = cpu_to_le16(0xffff); 6802 } 6803 6804 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6805 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6806 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6807 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6808 } else { 6809 req->cos_rule = cpu_to_le16(0xffff); 6810 } 6811 6812 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6813 ring = 0; 6814 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6815 ring = vnic->vnic_id - 1; 6816 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6817 ring = bp->rx_nr_rings - 1; 6818 6819 grp_idx = bp->rx_ring[ring].bnapi->index; 6820 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6821 req->lb_rule = cpu_to_le16(0xffff); 6822 vnic_mru: 6823 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 6824 req->mru = cpu_to_le16(vnic->mru); 6825 6826 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6827 #ifdef CONFIG_BNXT_SRIOV 6828 if (BNXT_VF(bp)) 6829 def_vlan = bp->vf.vlan; 6830 #endif 6831 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6832 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6833 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6834 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6835 6836 return hwrm_req_send(bp, req); 6837 } 6838 6839 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6840 struct bnxt_vnic_info *vnic) 6841 { 6842 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6843 struct hwrm_vnic_free_input *req; 6844 6845 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6846 return; 6847 6848 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6849 6850 hwrm_req_send(bp, req); 6851 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6852 } 6853 } 6854 6855 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6856 { 6857 u16 i; 6858 6859 for (i = 0; i < bp->nr_vnics; i++) 6860 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6861 } 6862 6863 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6864 unsigned int start_rx_ring_idx, 6865 unsigned int nr_rings) 6866 { 6867 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6868 struct hwrm_vnic_alloc_output *resp; 6869 struct hwrm_vnic_alloc_input *req; 6870 int rc; 6871 6872 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6873 if (rc) 6874 return rc; 6875 6876 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6877 goto vnic_no_ring_grps; 6878 6879 /* map ring groups to this vnic */ 6880 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6881 grp_idx = bp->rx_ring[i].bnapi->index; 6882 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6883 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6884 j, nr_rings); 6885 break; 6886 } 6887 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6888 } 6889 6890 vnic_no_ring_grps: 6891 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6892 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6893 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6894 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6895 6896 resp = hwrm_req_hold(bp, req); 6897 rc = hwrm_req_send(bp, req); 6898 if (!rc) 6899 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6900 hwrm_req_drop(bp, req); 6901 return rc; 6902 } 6903 6904 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6905 { 6906 struct hwrm_vnic_qcaps_output *resp; 6907 struct hwrm_vnic_qcaps_input *req; 6908 int rc; 6909 6910 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6911 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6912 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6913 if (bp->hwrm_spec_code < 0x10600) 6914 return 0; 6915 6916 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6917 if (rc) 6918 return rc; 6919 6920 resp = hwrm_req_hold(bp, req); 6921 rc = hwrm_req_send(bp, req); 6922 if (!rc) { 6923 u32 flags = le32_to_cpu(resp->flags); 6924 6925 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6926 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6927 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6928 if (flags & 6929 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6930 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6931 6932 /* Older P5 fw before EXT_HW_STATS support did not set 6933 * VLAN_STRIP_CAP properly. 6934 */ 6935 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6936 (BNXT_CHIP_P5(bp) && 6937 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6938 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6939 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6940 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6941 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6942 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6943 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6944 if (bp->max_tpa_v2) { 6945 if (BNXT_CHIP_P5(bp)) 6946 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6947 else 6948 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6949 } 6950 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6951 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6952 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6953 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6954 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6955 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6956 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6957 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6958 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6959 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6960 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6961 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6962 } 6963 hwrm_req_drop(bp, req); 6964 return rc; 6965 } 6966 6967 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6968 { 6969 struct hwrm_ring_grp_alloc_output *resp; 6970 struct hwrm_ring_grp_alloc_input *req; 6971 int rc; 6972 u16 i; 6973 6974 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6975 return 0; 6976 6977 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6978 if (rc) 6979 return rc; 6980 6981 resp = hwrm_req_hold(bp, req); 6982 for (i = 0; i < bp->rx_nr_rings; i++) { 6983 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 6984 6985 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 6986 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 6987 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 6988 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 6989 6990 rc = hwrm_req_send(bp, req); 6991 6992 if (rc) 6993 break; 6994 6995 bp->grp_info[grp_idx].fw_grp_id = 6996 le32_to_cpu(resp->ring_group_id); 6997 } 6998 hwrm_req_drop(bp, req); 6999 return rc; 7000 } 7001 7002 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 7003 { 7004 struct hwrm_ring_grp_free_input *req; 7005 u16 i; 7006 7007 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7008 return; 7009 7010 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 7011 return; 7012 7013 hwrm_req_hold(bp, req); 7014 for (i = 0; i < bp->cp_nr_rings; i++) { 7015 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 7016 continue; 7017 req->ring_group_id = 7018 cpu_to_le32(bp->grp_info[i].fw_grp_id); 7019 7020 hwrm_req_send(bp, req); 7021 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 7022 } 7023 hwrm_req_drop(bp, req); 7024 } 7025 7026 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type, 7027 struct hwrm_ring_alloc_input *req, 7028 struct bnxt_ring_struct *ring) 7029 { 7030 struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx]; 7031 u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID | 7032 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID; 7033 7034 if (ring_type == HWRM_RING_ALLOC_AGG) { 7035 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 7036 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 7037 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 7038 enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID; 7039 } else { 7040 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 7041 if (NET_IP_ALIGN == 2) 7042 req->flags = 7043 cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD); 7044 } 7045 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7046 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7047 req->enables |= cpu_to_le32(enables); 7048 } 7049 7050 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 7051 struct bnxt_ring_struct *ring, 7052 u32 ring_type, u32 map_index) 7053 { 7054 struct hwrm_ring_alloc_output *resp; 7055 struct hwrm_ring_alloc_input *req; 7056 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 7057 struct bnxt_ring_grp_info *grp_info; 7058 int rc, err = 0; 7059 u16 ring_id; 7060 7061 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 7062 if (rc) 7063 goto exit; 7064 7065 req->enables = 0; 7066 if (rmem->nr_pages > 1) { 7067 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 7068 /* Page size is in log2 units */ 7069 req->page_size = BNXT_PAGE_SHIFT; 7070 req->page_tbl_depth = 1; 7071 } else { 7072 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 7073 } 7074 req->fbo = 0; 7075 /* Association of ring index with doorbell index and MSIX number */ 7076 req->logical_id = cpu_to_le16(map_index); 7077 7078 switch (ring_type) { 7079 case HWRM_RING_ALLOC_TX: { 7080 struct bnxt_tx_ring_info *txr; 7081 u16 flags = 0; 7082 7083 txr = container_of(ring, struct bnxt_tx_ring_info, 7084 tx_ring_struct); 7085 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 7086 /* Association of transmit ring with completion ring */ 7087 grp_info = &bp->grp_info[ring->grp_idx]; 7088 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 7089 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 7090 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7091 req->queue_id = cpu_to_le16(ring->queue_id); 7092 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 7093 req->cmpl_coal_cnt = 7094 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 7095 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 7096 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 7097 req->flags = cpu_to_le16(flags); 7098 break; 7099 } 7100 case HWRM_RING_ALLOC_RX: 7101 case HWRM_RING_ALLOC_AGG: 7102 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 7103 req->length = (ring_type == HWRM_RING_ALLOC_RX) ? 7104 cpu_to_le32(bp->rx_ring_mask + 1) : 7105 cpu_to_le32(bp->rx_agg_ring_mask + 1); 7106 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7107 bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring); 7108 break; 7109 case HWRM_RING_ALLOC_CMPL: 7110 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 7111 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7112 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7113 /* Association of cp ring with nq */ 7114 grp_info = &bp->grp_info[map_index]; 7115 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7116 req->cq_handle = cpu_to_le64(ring->handle); 7117 req->enables |= cpu_to_le32( 7118 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7119 } else { 7120 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7121 } 7122 break; 7123 case HWRM_RING_ALLOC_NQ: 7124 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7125 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7126 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7127 break; 7128 default: 7129 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7130 ring_type); 7131 return -EINVAL; 7132 } 7133 7134 resp = hwrm_req_hold(bp, req); 7135 rc = hwrm_req_send(bp, req); 7136 err = le16_to_cpu(resp->error_code); 7137 ring_id = le16_to_cpu(resp->ring_id); 7138 hwrm_req_drop(bp, req); 7139 7140 exit: 7141 if (rc || err) { 7142 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7143 ring_type, rc, err); 7144 return -EIO; 7145 } 7146 ring->fw_ring_id = ring_id; 7147 return rc; 7148 } 7149 7150 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7151 { 7152 int rc; 7153 7154 if (BNXT_PF(bp)) { 7155 struct hwrm_func_cfg_input *req; 7156 7157 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7158 if (rc) 7159 return rc; 7160 7161 req->fid = cpu_to_le16(0xffff); 7162 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7163 req->async_event_cr = cpu_to_le16(idx); 7164 return hwrm_req_send(bp, req); 7165 } else { 7166 struct hwrm_func_vf_cfg_input *req; 7167 7168 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7169 if (rc) 7170 return rc; 7171 7172 req->enables = 7173 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7174 req->async_event_cr = cpu_to_le16(idx); 7175 return hwrm_req_send(bp, req); 7176 } 7177 } 7178 7179 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7180 u32 ring_type) 7181 { 7182 switch (ring_type) { 7183 case HWRM_RING_ALLOC_TX: 7184 db->db_ring_mask = bp->tx_ring_mask; 7185 break; 7186 case HWRM_RING_ALLOC_RX: 7187 db->db_ring_mask = bp->rx_ring_mask; 7188 break; 7189 case HWRM_RING_ALLOC_AGG: 7190 db->db_ring_mask = bp->rx_agg_ring_mask; 7191 break; 7192 case HWRM_RING_ALLOC_CMPL: 7193 case HWRM_RING_ALLOC_NQ: 7194 db->db_ring_mask = bp->cp_ring_mask; 7195 break; 7196 } 7197 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7198 db->db_epoch_mask = db->db_ring_mask + 1; 7199 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7200 } 7201 } 7202 7203 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7204 u32 map_idx, u32 xid) 7205 { 7206 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7207 switch (ring_type) { 7208 case HWRM_RING_ALLOC_TX: 7209 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7210 break; 7211 case HWRM_RING_ALLOC_RX: 7212 case HWRM_RING_ALLOC_AGG: 7213 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7214 break; 7215 case HWRM_RING_ALLOC_CMPL: 7216 db->db_key64 = DBR_PATH_L2; 7217 break; 7218 case HWRM_RING_ALLOC_NQ: 7219 db->db_key64 = DBR_PATH_L2; 7220 break; 7221 } 7222 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7223 7224 if (bp->flags & BNXT_FLAG_CHIP_P7) 7225 db->db_key64 |= DBR_VALID; 7226 7227 db->doorbell = bp->bar1 + bp->db_offset; 7228 } else { 7229 db->doorbell = bp->bar1 + map_idx * 0x80; 7230 switch (ring_type) { 7231 case HWRM_RING_ALLOC_TX: 7232 db->db_key32 = DB_KEY_TX; 7233 break; 7234 case HWRM_RING_ALLOC_RX: 7235 case HWRM_RING_ALLOC_AGG: 7236 db->db_key32 = DB_KEY_RX; 7237 break; 7238 case HWRM_RING_ALLOC_CMPL: 7239 db->db_key32 = DB_KEY_CP; 7240 break; 7241 } 7242 } 7243 bnxt_set_db_mask(bp, db, ring_type); 7244 } 7245 7246 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7247 struct bnxt_rx_ring_info *rxr) 7248 { 7249 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7250 struct bnxt_napi *bnapi = rxr->bnapi; 7251 u32 type = HWRM_RING_ALLOC_RX; 7252 u32 map_idx = bnapi->index; 7253 int rc; 7254 7255 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7256 if (rc) 7257 return rc; 7258 7259 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7260 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7261 7262 return 0; 7263 } 7264 7265 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7266 struct bnxt_rx_ring_info *rxr) 7267 { 7268 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7269 u32 type = HWRM_RING_ALLOC_AGG; 7270 u32 grp_idx = ring->grp_idx; 7271 u32 map_idx; 7272 int rc; 7273 7274 map_idx = grp_idx + bp->rx_nr_rings; 7275 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7276 if (rc) 7277 return rc; 7278 7279 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7280 ring->fw_ring_id); 7281 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7282 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7283 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7284 7285 return 0; 7286 } 7287 7288 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp, 7289 struct bnxt_cp_ring_info *cpr) 7290 { 7291 const u32 type = HWRM_RING_ALLOC_CMPL; 7292 struct bnxt_napi *bnapi = cpr->bnapi; 7293 struct bnxt_ring_struct *ring; 7294 u32 map_idx = bnapi->index; 7295 int rc; 7296 7297 ring = &cpr->cp_ring_struct; 7298 ring->handle = BNXT_SET_NQ_HDL(cpr); 7299 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7300 if (rc) 7301 return rc; 7302 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7303 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7304 return 0; 7305 } 7306 7307 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp, 7308 struct bnxt_tx_ring_info *txr, u32 tx_idx) 7309 { 7310 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7311 const u32 type = HWRM_RING_ALLOC_TX; 7312 int rc; 7313 7314 rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx); 7315 if (rc) 7316 return rc; 7317 bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id); 7318 return 0; 7319 } 7320 7321 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7322 { 7323 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7324 int i, rc = 0; 7325 u32 type; 7326 7327 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7328 type = HWRM_RING_ALLOC_NQ; 7329 else 7330 type = HWRM_RING_ALLOC_CMPL; 7331 for (i = 0; i < bp->cp_nr_rings; i++) { 7332 struct bnxt_napi *bnapi = bp->bnapi[i]; 7333 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7334 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7335 u32 map_idx = ring->map_idx; 7336 unsigned int vector; 7337 7338 vector = bp->irq_tbl[map_idx].vector; 7339 disable_irq_nosync(vector); 7340 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7341 if (rc) { 7342 enable_irq(vector); 7343 goto err_out; 7344 } 7345 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7346 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7347 enable_irq(vector); 7348 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7349 7350 if (!i) { 7351 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7352 if (rc) 7353 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7354 } 7355 } 7356 7357 for (i = 0; i < bp->tx_nr_rings; i++) { 7358 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7359 7360 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7361 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 7362 if (rc) 7363 goto err_out; 7364 } 7365 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i); 7366 if (rc) 7367 goto err_out; 7368 } 7369 7370 for (i = 0; i < bp->rx_nr_rings; i++) { 7371 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7372 7373 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7374 if (rc) 7375 goto err_out; 7376 /* If we have agg rings, post agg buffers first. */ 7377 if (!agg_rings) 7378 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7379 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7380 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 7381 if (rc) 7382 goto err_out; 7383 } 7384 } 7385 7386 if (agg_rings) { 7387 for (i = 0; i < bp->rx_nr_rings; i++) { 7388 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7389 if (rc) 7390 goto err_out; 7391 } 7392 } 7393 err_out: 7394 return rc; 7395 } 7396 7397 static void bnxt_cancel_dim(struct bnxt *bp) 7398 { 7399 int i; 7400 7401 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7402 * if NAPI is enabled. 7403 */ 7404 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7405 return; 7406 7407 /* Make sure NAPI sees that the VNIC is disabled */ 7408 synchronize_net(); 7409 for (i = 0; i < bp->rx_nr_rings; i++) { 7410 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7411 struct bnxt_napi *bnapi = rxr->bnapi; 7412 7413 cancel_work_sync(&bnapi->cp_ring.dim.work); 7414 } 7415 } 7416 7417 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7418 struct bnxt_ring_struct *ring, 7419 u32 ring_type, int cmpl_ring_id) 7420 { 7421 struct hwrm_ring_free_output *resp; 7422 struct hwrm_ring_free_input *req; 7423 u16 error_code = 0; 7424 int rc; 7425 7426 if (BNXT_NO_FW_ACCESS(bp)) 7427 return 0; 7428 7429 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7430 if (rc) 7431 goto exit; 7432 7433 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7434 req->ring_type = ring_type; 7435 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7436 7437 resp = hwrm_req_hold(bp, req); 7438 rc = hwrm_req_send(bp, req); 7439 error_code = le16_to_cpu(resp->error_code); 7440 hwrm_req_drop(bp, req); 7441 exit: 7442 if (rc || error_code) { 7443 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7444 ring_type, rc, error_code); 7445 return -EIO; 7446 } 7447 return 0; 7448 } 7449 7450 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp, 7451 struct bnxt_tx_ring_info *txr, 7452 bool close_path) 7453 { 7454 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7455 u32 cmpl_ring_id; 7456 7457 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7458 return; 7459 7460 cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) : 7461 INVALID_HW_RING_ID; 7462 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX, 7463 cmpl_ring_id); 7464 ring->fw_ring_id = INVALID_HW_RING_ID; 7465 } 7466 7467 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7468 struct bnxt_rx_ring_info *rxr, 7469 bool close_path) 7470 { 7471 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7472 u32 grp_idx = rxr->bnapi->index; 7473 u32 cmpl_ring_id; 7474 7475 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7476 return; 7477 7478 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7479 hwrm_ring_free_send_msg(bp, ring, 7480 RING_FREE_REQ_RING_TYPE_RX, 7481 close_path ? cmpl_ring_id : 7482 INVALID_HW_RING_ID); 7483 ring->fw_ring_id = INVALID_HW_RING_ID; 7484 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7485 } 7486 7487 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7488 struct bnxt_rx_ring_info *rxr, 7489 bool close_path) 7490 { 7491 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7492 u32 grp_idx = rxr->bnapi->index; 7493 u32 type, cmpl_ring_id; 7494 7495 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7496 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7497 else 7498 type = RING_FREE_REQ_RING_TYPE_RX; 7499 7500 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7501 return; 7502 7503 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7504 hwrm_ring_free_send_msg(bp, ring, type, 7505 close_path ? cmpl_ring_id : 7506 INVALID_HW_RING_ID); 7507 ring->fw_ring_id = INVALID_HW_RING_ID; 7508 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7509 } 7510 7511 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp, 7512 struct bnxt_cp_ring_info *cpr) 7513 { 7514 struct bnxt_ring_struct *ring; 7515 7516 ring = &cpr->cp_ring_struct; 7517 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7518 return; 7519 7520 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL, 7521 INVALID_HW_RING_ID); 7522 ring->fw_ring_id = INVALID_HW_RING_ID; 7523 } 7524 7525 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 7526 { 7527 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7528 int i, size = ring->ring_mem.page_size; 7529 7530 cpr->cp_raw_cons = 0; 7531 cpr->toggle = 0; 7532 7533 for (i = 0; i < bp->cp_nr_pages; i++) 7534 if (cpr->cp_desc_ring[i]) 7535 memset(cpr->cp_desc_ring[i], 0, size); 7536 } 7537 7538 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7539 { 7540 u32 type; 7541 int i; 7542 7543 if (!bp->bnapi) 7544 return; 7545 7546 for (i = 0; i < bp->tx_nr_rings; i++) 7547 bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path); 7548 7549 bnxt_cancel_dim(bp); 7550 for (i = 0; i < bp->rx_nr_rings; i++) { 7551 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7552 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7553 } 7554 7555 /* The completion rings are about to be freed. After that the 7556 * IRQ doorbell will not work anymore. So we need to disable 7557 * IRQ here. 7558 */ 7559 bnxt_disable_int_sync(bp); 7560 7561 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7562 type = RING_FREE_REQ_RING_TYPE_NQ; 7563 else 7564 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7565 for (i = 0; i < bp->cp_nr_rings; i++) { 7566 struct bnxt_napi *bnapi = bp->bnapi[i]; 7567 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7568 struct bnxt_ring_struct *ring; 7569 int j; 7570 7571 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) 7572 bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]); 7573 7574 ring = &cpr->cp_ring_struct; 7575 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7576 hwrm_ring_free_send_msg(bp, ring, type, 7577 INVALID_HW_RING_ID); 7578 ring->fw_ring_id = INVALID_HW_RING_ID; 7579 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7580 } 7581 } 7582 } 7583 7584 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7585 bool shared); 7586 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7587 bool shared); 7588 7589 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7590 { 7591 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7592 struct hwrm_func_qcfg_output *resp; 7593 struct hwrm_func_qcfg_input *req; 7594 int rc; 7595 7596 if (bp->hwrm_spec_code < 0x10601) 7597 return 0; 7598 7599 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7600 if (rc) 7601 return rc; 7602 7603 req->fid = cpu_to_le16(0xffff); 7604 resp = hwrm_req_hold(bp, req); 7605 rc = hwrm_req_send(bp, req); 7606 if (rc) { 7607 hwrm_req_drop(bp, req); 7608 return rc; 7609 } 7610 7611 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7612 if (BNXT_NEW_RM(bp)) { 7613 u16 cp, stats; 7614 7615 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7616 hw_resc->resv_hw_ring_grps = 7617 le32_to_cpu(resp->alloc_hw_ring_grps); 7618 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7619 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7620 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7621 stats = le16_to_cpu(resp->alloc_stat_ctx); 7622 hw_resc->resv_irqs = cp; 7623 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7624 int rx = hw_resc->resv_rx_rings; 7625 int tx = hw_resc->resv_tx_rings; 7626 7627 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7628 rx >>= 1; 7629 if (cp < (rx + tx)) { 7630 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7631 if (rc) 7632 goto get_rings_exit; 7633 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7634 rx <<= 1; 7635 hw_resc->resv_rx_rings = rx; 7636 hw_resc->resv_tx_rings = tx; 7637 } 7638 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7639 hw_resc->resv_hw_ring_grps = rx; 7640 } 7641 hw_resc->resv_cp_rings = cp; 7642 hw_resc->resv_stat_ctxs = stats; 7643 } 7644 get_rings_exit: 7645 hwrm_req_drop(bp, req); 7646 return rc; 7647 } 7648 7649 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7650 { 7651 struct hwrm_func_qcfg_output *resp; 7652 struct hwrm_func_qcfg_input *req; 7653 int rc; 7654 7655 if (bp->hwrm_spec_code < 0x10601) 7656 return 0; 7657 7658 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7659 if (rc) 7660 return rc; 7661 7662 req->fid = cpu_to_le16(fid); 7663 resp = hwrm_req_hold(bp, req); 7664 rc = hwrm_req_send(bp, req); 7665 if (!rc) 7666 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7667 7668 hwrm_req_drop(bp, req); 7669 return rc; 7670 } 7671 7672 static bool bnxt_rfs_supported(struct bnxt *bp); 7673 7674 static struct hwrm_func_cfg_input * 7675 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7676 { 7677 struct hwrm_func_cfg_input *req; 7678 u32 enables = 0; 7679 7680 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7681 return NULL; 7682 7683 req->fid = cpu_to_le16(0xffff); 7684 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7685 req->num_tx_rings = cpu_to_le16(hwr->tx); 7686 if (BNXT_NEW_RM(bp)) { 7687 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7688 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7689 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7690 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7691 enables |= hwr->cp_p5 ? 7692 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7693 } else { 7694 enables |= hwr->cp ? 7695 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7696 enables |= hwr->grp ? 7697 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7698 } 7699 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7700 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7701 0; 7702 req->num_rx_rings = cpu_to_le16(hwr->rx); 7703 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7704 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7705 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7706 req->num_msix = cpu_to_le16(hwr->cp); 7707 } else { 7708 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7709 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7710 } 7711 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7712 req->num_vnics = cpu_to_le16(hwr->vnic); 7713 } 7714 req->enables = cpu_to_le32(enables); 7715 return req; 7716 } 7717 7718 static struct hwrm_func_vf_cfg_input * 7719 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7720 { 7721 struct hwrm_func_vf_cfg_input *req; 7722 u32 enables = 0; 7723 7724 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7725 return NULL; 7726 7727 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7728 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7729 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7730 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7731 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7732 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7733 enables |= hwr->cp_p5 ? 7734 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7735 } else { 7736 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7737 enables |= hwr->grp ? 7738 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7739 } 7740 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7741 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7742 7743 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7744 req->num_tx_rings = cpu_to_le16(hwr->tx); 7745 req->num_rx_rings = cpu_to_le16(hwr->rx); 7746 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7747 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7748 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7749 } else { 7750 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7751 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7752 } 7753 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7754 req->num_vnics = cpu_to_le16(hwr->vnic); 7755 7756 req->enables = cpu_to_le32(enables); 7757 return req; 7758 } 7759 7760 static int 7761 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7762 { 7763 struct hwrm_func_cfg_input *req; 7764 int rc; 7765 7766 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7767 if (!req) 7768 return -ENOMEM; 7769 7770 if (!req->enables) { 7771 hwrm_req_drop(bp, req); 7772 return 0; 7773 } 7774 7775 rc = hwrm_req_send(bp, req); 7776 if (rc) 7777 return rc; 7778 7779 if (bp->hwrm_spec_code < 0x10601) 7780 bp->hw_resc.resv_tx_rings = hwr->tx; 7781 7782 return bnxt_hwrm_get_rings(bp); 7783 } 7784 7785 static int 7786 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7787 { 7788 struct hwrm_func_vf_cfg_input *req; 7789 int rc; 7790 7791 if (!BNXT_NEW_RM(bp)) { 7792 bp->hw_resc.resv_tx_rings = hwr->tx; 7793 return 0; 7794 } 7795 7796 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7797 if (!req) 7798 return -ENOMEM; 7799 7800 rc = hwrm_req_send(bp, req); 7801 if (rc) 7802 return rc; 7803 7804 return bnxt_hwrm_get_rings(bp); 7805 } 7806 7807 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7808 { 7809 if (BNXT_PF(bp)) 7810 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7811 else 7812 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7813 } 7814 7815 int bnxt_nq_rings_in_use(struct bnxt *bp) 7816 { 7817 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7818 } 7819 7820 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7821 { 7822 int cp; 7823 7824 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7825 return bnxt_nq_rings_in_use(bp); 7826 7827 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7828 return cp; 7829 } 7830 7831 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7832 { 7833 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7834 } 7835 7836 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7837 { 7838 if (!hwr->grp) 7839 return 0; 7840 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7841 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7842 7843 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7844 rss_ctx *= hwr->vnic; 7845 return rss_ctx; 7846 } 7847 if (BNXT_VF(bp)) 7848 return BNXT_VF_MAX_RSS_CTX; 7849 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7850 return hwr->grp + 1; 7851 return 1; 7852 } 7853 7854 /* Check if a default RSS map needs to be setup. This function is only 7855 * used on older firmware that does not require reserving RX rings. 7856 */ 7857 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7858 { 7859 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7860 7861 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7862 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7863 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7864 if (!netif_is_rxfh_configured(bp->dev)) 7865 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7866 } 7867 } 7868 7869 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7870 { 7871 if (bp->flags & BNXT_FLAG_RFS) { 7872 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7873 return 2 + bp->num_rss_ctx; 7874 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7875 return rx_rings + 1; 7876 } 7877 return 1; 7878 } 7879 7880 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7881 { 7882 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7883 int cp = bnxt_cp_rings_in_use(bp); 7884 int nq = bnxt_nq_rings_in_use(bp); 7885 int rx = bp->rx_nr_rings, stat; 7886 int vnic, grp = rx; 7887 7888 /* Old firmware does not need RX ring reservations but we still 7889 * need to setup a default RSS map when needed. With new firmware 7890 * we go through RX ring reservations first and then set up the 7891 * RSS map for the successfully reserved RX rings when needed. 7892 */ 7893 if (!BNXT_NEW_RM(bp)) 7894 bnxt_check_rss_tbl_no_rmgr(bp); 7895 7896 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7897 bp->hwrm_spec_code >= 0x10601) 7898 return true; 7899 7900 if (!BNXT_NEW_RM(bp)) 7901 return false; 7902 7903 vnic = bnxt_get_total_vnics(bp, rx); 7904 7905 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7906 rx <<= 1; 7907 stat = bnxt_get_func_stat_ctxs(bp); 7908 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7909 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7910 (hw_resc->resv_hw_ring_grps != grp && 7911 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7912 return true; 7913 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7914 hw_resc->resv_irqs != nq) 7915 return true; 7916 return false; 7917 } 7918 7919 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7920 { 7921 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7922 7923 hwr->tx = hw_resc->resv_tx_rings; 7924 if (BNXT_NEW_RM(bp)) { 7925 hwr->rx = hw_resc->resv_rx_rings; 7926 hwr->cp = hw_resc->resv_irqs; 7927 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7928 hwr->cp_p5 = hw_resc->resv_cp_rings; 7929 hwr->grp = hw_resc->resv_hw_ring_grps; 7930 hwr->vnic = hw_resc->resv_vnics; 7931 hwr->stat = hw_resc->resv_stat_ctxs; 7932 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7933 } 7934 } 7935 7936 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7937 { 7938 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7939 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7940 } 7941 7942 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7943 7944 static int __bnxt_reserve_rings(struct bnxt *bp) 7945 { 7946 struct bnxt_hw_rings hwr = {0}; 7947 int rx_rings, old_rx_rings, rc; 7948 int cp = bp->cp_nr_rings; 7949 int ulp_msix = 0; 7950 bool sh = false; 7951 int tx_cp; 7952 7953 if (!bnxt_need_reserve_rings(bp)) 7954 return 0; 7955 7956 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7957 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7958 if (!ulp_msix) 7959 bnxt_set_ulp_stat_ctxs(bp, 0); 7960 7961 if (ulp_msix > bp->ulp_num_msix_want) 7962 ulp_msix = bp->ulp_num_msix_want; 7963 hwr.cp = cp + ulp_msix; 7964 } else { 7965 hwr.cp = bnxt_nq_rings_in_use(bp); 7966 } 7967 7968 hwr.tx = bp->tx_nr_rings; 7969 hwr.rx = bp->rx_nr_rings; 7970 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7971 sh = true; 7972 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7973 hwr.cp_p5 = hwr.rx + hwr.tx; 7974 7975 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7976 7977 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7978 hwr.rx <<= 1; 7979 hwr.grp = bp->rx_nr_rings; 7980 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 7981 hwr.stat = bnxt_get_func_stat_ctxs(bp); 7982 old_rx_rings = bp->hw_resc.resv_rx_rings; 7983 7984 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 7985 if (rc) 7986 return rc; 7987 7988 bnxt_copy_reserved_rings(bp, &hwr); 7989 7990 rx_rings = hwr.rx; 7991 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 7992 if (hwr.rx >= 2) { 7993 rx_rings = hwr.rx >> 1; 7994 } else { 7995 if (netif_running(bp->dev)) 7996 return -ENOMEM; 7997 7998 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 7999 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 8000 bp->dev->hw_features &= ~NETIF_F_LRO; 8001 bp->dev->features &= ~NETIF_F_LRO; 8002 bnxt_set_ring_params(bp); 8003 } 8004 } 8005 rx_rings = min_t(int, rx_rings, hwr.grp); 8006 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 8007 if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 8008 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 8009 hwr.cp = min_t(int, hwr.cp, hwr.stat); 8010 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 8011 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8012 hwr.rx = rx_rings << 1; 8013 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 8014 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 8015 bp->tx_nr_rings = hwr.tx; 8016 8017 /* If we cannot reserve all the RX rings, reset the RSS map only 8018 * if absolutely necessary 8019 */ 8020 if (rx_rings != bp->rx_nr_rings) { 8021 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 8022 rx_rings, bp->rx_nr_rings); 8023 if (netif_is_rxfh_configured(bp->dev) && 8024 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 8025 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 8026 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 8027 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 8028 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 8029 } 8030 } 8031 bp->rx_nr_rings = rx_rings; 8032 bp->cp_nr_rings = hwr.cp; 8033 8034 if (!bnxt_rings_ok(bp, &hwr)) 8035 return -ENOMEM; 8036 8037 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 8038 !netif_is_rxfh_configured(bp->dev)) 8039 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8040 8041 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 8042 int resv_msix, resv_ctx, ulp_ctxs; 8043 struct bnxt_hw_resc *hw_resc; 8044 8045 hw_resc = &bp->hw_resc; 8046 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 8047 ulp_msix = min_t(int, resv_msix, ulp_msix); 8048 bnxt_set_ulp_msix_num(bp, ulp_msix); 8049 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 8050 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 8051 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 8052 } 8053 8054 return rc; 8055 } 8056 8057 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8058 { 8059 struct hwrm_func_vf_cfg_input *req; 8060 u32 flags; 8061 8062 if (!BNXT_NEW_RM(bp)) 8063 return 0; 8064 8065 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8066 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 8067 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8068 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8069 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8070 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 8071 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 8072 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8073 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8074 8075 req->flags = cpu_to_le32(flags); 8076 return hwrm_req_send_silent(bp, req); 8077 } 8078 8079 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8080 { 8081 struct hwrm_func_cfg_input *req; 8082 u32 flags; 8083 8084 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 8085 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 8086 if (BNXT_NEW_RM(bp)) { 8087 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8088 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8089 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8090 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 8091 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8092 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 8093 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 8094 else 8095 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8096 } 8097 8098 req->flags = cpu_to_le32(flags); 8099 return hwrm_req_send_silent(bp, req); 8100 } 8101 8102 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8103 { 8104 if (bp->hwrm_spec_code < 0x10801) 8105 return 0; 8106 8107 if (BNXT_PF(bp)) 8108 return bnxt_hwrm_check_pf_rings(bp, hwr); 8109 8110 return bnxt_hwrm_check_vf_rings(bp, hwr); 8111 } 8112 8113 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 8114 { 8115 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8116 struct hwrm_ring_aggint_qcaps_output *resp; 8117 struct hwrm_ring_aggint_qcaps_input *req; 8118 int rc; 8119 8120 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 8121 coal_cap->num_cmpl_dma_aggr_max = 63; 8122 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 8123 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 8124 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 8125 coal_cap->int_lat_tmr_min_max = 65535; 8126 coal_cap->int_lat_tmr_max_max = 65535; 8127 coal_cap->num_cmpl_aggr_int_max = 65535; 8128 coal_cap->timer_units = 80; 8129 8130 if (bp->hwrm_spec_code < 0x10902) 8131 return; 8132 8133 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 8134 return; 8135 8136 resp = hwrm_req_hold(bp, req); 8137 rc = hwrm_req_send_silent(bp, req); 8138 if (!rc) { 8139 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 8140 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 8141 coal_cap->num_cmpl_dma_aggr_max = 8142 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 8143 coal_cap->num_cmpl_dma_aggr_during_int_max = 8144 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8145 coal_cap->cmpl_aggr_dma_tmr_max = 8146 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8147 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8148 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8149 coal_cap->int_lat_tmr_min_max = 8150 le16_to_cpu(resp->int_lat_tmr_min_max); 8151 coal_cap->int_lat_tmr_max_max = 8152 le16_to_cpu(resp->int_lat_tmr_max_max); 8153 coal_cap->num_cmpl_aggr_int_max = 8154 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8155 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8156 } 8157 hwrm_req_drop(bp, req); 8158 } 8159 8160 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8161 { 8162 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8163 8164 return usec * 1000 / coal_cap->timer_units; 8165 } 8166 8167 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8168 struct bnxt_coal *hw_coal, 8169 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8170 { 8171 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8172 u16 val, tmr, max, flags = hw_coal->flags; 8173 u32 cmpl_params = coal_cap->cmpl_params; 8174 8175 max = hw_coal->bufs_per_record * 128; 8176 if (hw_coal->budget) 8177 max = hw_coal->bufs_per_record * hw_coal->budget; 8178 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8179 8180 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8181 req->num_cmpl_aggr_int = cpu_to_le16(val); 8182 8183 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8184 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8185 8186 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8187 coal_cap->num_cmpl_dma_aggr_during_int_max); 8188 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8189 8190 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8191 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8192 req->int_lat_tmr_max = cpu_to_le16(tmr); 8193 8194 /* min timer set to 1/2 of interrupt timer */ 8195 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8196 val = tmr / 2; 8197 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8198 req->int_lat_tmr_min = cpu_to_le16(val); 8199 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8200 } 8201 8202 /* buf timer set to 1/4 of interrupt timer */ 8203 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8204 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8205 8206 if (cmpl_params & 8207 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8208 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8209 val = clamp_t(u16, tmr, 1, 8210 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8211 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8212 req->enables |= 8213 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8214 } 8215 8216 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8217 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8218 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8219 req->flags = cpu_to_le16(flags); 8220 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8221 } 8222 8223 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8224 struct bnxt_coal *hw_coal) 8225 { 8226 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8227 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8228 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8229 u32 nq_params = coal_cap->nq_params; 8230 u16 tmr; 8231 int rc; 8232 8233 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8234 return 0; 8235 8236 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8237 if (rc) 8238 return rc; 8239 8240 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8241 req->flags = 8242 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8243 8244 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8245 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8246 req->int_lat_tmr_min = cpu_to_le16(tmr); 8247 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8248 return hwrm_req_send(bp, req); 8249 } 8250 8251 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8252 { 8253 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8254 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8255 struct bnxt_coal coal; 8256 int rc; 8257 8258 /* Tick values in micro seconds. 8259 * 1 coal_buf x bufs_per_record = 1 completion record. 8260 */ 8261 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8262 8263 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8264 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8265 8266 if (!bnapi->rx_ring) 8267 return -ENODEV; 8268 8269 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8270 if (rc) 8271 return rc; 8272 8273 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8274 8275 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8276 8277 return hwrm_req_send(bp, req_rx); 8278 } 8279 8280 static int 8281 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8282 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8283 { 8284 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8285 8286 req->ring_id = cpu_to_le16(ring_id); 8287 return hwrm_req_send(bp, req); 8288 } 8289 8290 static int 8291 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8292 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8293 { 8294 struct bnxt_tx_ring_info *txr; 8295 int i, rc; 8296 8297 bnxt_for_each_napi_tx(i, bnapi, txr) { 8298 u16 ring_id; 8299 8300 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8301 req->ring_id = cpu_to_le16(ring_id); 8302 rc = hwrm_req_send(bp, req); 8303 if (rc) 8304 return rc; 8305 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8306 return 0; 8307 } 8308 return 0; 8309 } 8310 8311 int bnxt_hwrm_set_coal(struct bnxt *bp) 8312 { 8313 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8314 int i, rc; 8315 8316 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8317 if (rc) 8318 return rc; 8319 8320 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8321 if (rc) { 8322 hwrm_req_drop(bp, req_rx); 8323 return rc; 8324 } 8325 8326 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8327 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8328 8329 hwrm_req_hold(bp, req_rx); 8330 hwrm_req_hold(bp, req_tx); 8331 for (i = 0; i < bp->cp_nr_rings; i++) { 8332 struct bnxt_napi *bnapi = bp->bnapi[i]; 8333 struct bnxt_coal *hw_coal; 8334 8335 if (!bnapi->rx_ring) 8336 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8337 else 8338 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8339 if (rc) 8340 break; 8341 8342 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8343 continue; 8344 8345 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8346 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8347 if (rc) 8348 break; 8349 } 8350 if (bnapi->rx_ring) 8351 hw_coal = &bp->rx_coal; 8352 else 8353 hw_coal = &bp->tx_coal; 8354 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8355 } 8356 hwrm_req_drop(bp, req_rx); 8357 hwrm_req_drop(bp, req_tx); 8358 return rc; 8359 } 8360 8361 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8362 { 8363 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8364 struct hwrm_stat_ctx_free_input *req; 8365 int i; 8366 8367 if (!bp->bnapi) 8368 return; 8369 8370 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8371 return; 8372 8373 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8374 return; 8375 if (BNXT_FW_MAJ(bp) <= 20) { 8376 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8377 hwrm_req_drop(bp, req); 8378 return; 8379 } 8380 hwrm_req_hold(bp, req0); 8381 } 8382 hwrm_req_hold(bp, req); 8383 for (i = 0; i < bp->cp_nr_rings; i++) { 8384 struct bnxt_napi *bnapi = bp->bnapi[i]; 8385 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8386 8387 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8388 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8389 if (req0) { 8390 req0->stat_ctx_id = req->stat_ctx_id; 8391 hwrm_req_send(bp, req0); 8392 } 8393 hwrm_req_send(bp, req); 8394 8395 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8396 } 8397 } 8398 hwrm_req_drop(bp, req); 8399 if (req0) 8400 hwrm_req_drop(bp, req0); 8401 } 8402 8403 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8404 { 8405 struct hwrm_stat_ctx_alloc_output *resp; 8406 struct hwrm_stat_ctx_alloc_input *req; 8407 int rc, i; 8408 8409 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8410 return 0; 8411 8412 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8413 if (rc) 8414 return rc; 8415 8416 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8417 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8418 8419 resp = hwrm_req_hold(bp, req); 8420 for (i = 0; i < bp->cp_nr_rings; i++) { 8421 struct bnxt_napi *bnapi = bp->bnapi[i]; 8422 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8423 8424 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8425 8426 rc = hwrm_req_send(bp, req); 8427 if (rc) 8428 break; 8429 8430 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8431 8432 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8433 } 8434 hwrm_req_drop(bp, req); 8435 return rc; 8436 } 8437 8438 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8439 { 8440 struct hwrm_func_qcfg_output *resp; 8441 struct hwrm_func_qcfg_input *req; 8442 u16 flags; 8443 int rc; 8444 8445 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8446 if (rc) 8447 return rc; 8448 8449 req->fid = cpu_to_le16(0xffff); 8450 resp = hwrm_req_hold(bp, req); 8451 rc = hwrm_req_send(bp, req); 8452 if (rc) 8453 goto func_qcfg_exit; 8454 8455 flags = le16_to_cpu(resp->flags); 8456 #ifdef CONFIG_BNXT_SRIOV 8457 if (BNXT_VF(bp)) { 8458 struct bnxt_vf_info *vf = &bp->vf; 8459 8460 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8461 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8462 vf->flags |= BNXT_VF_TRUST; 8463 else 8464 vf->flags &= ~BNXT_VF_TRUST; 8465 } else { 8466 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8467 } 8468 #endif 8469 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8470 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8471 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8472 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8473 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8474 } 8475 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8476 bp->flags |= BNXT_FLAG_MULTI_HOST; 8477 8478 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8479 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8480 8481 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8482 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8483 8484 switch (resp->port_partition_type) { 8485 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8486 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2: 8487 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8488 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8489 bp->port_partition_type = resp->port_partition_type; 8490 break; 8491 } 8492 if (bp->hwrm_spec_code < 0x10707 || 8493 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8494 bp->br_mode = BRIDGE_MODE_VEB; 8495 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8496 bp->br_mode = BRIDGE_MODE_VEPA; 8497 else 8498 bp->br_mode = BRIDGE_MODE_UNDEF; 8499 8500 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8501 if (!bp->max_mtu) 8502 bp->max_mtu = BNXT_MAX_MTU; 8503 8504 if (bp->db_size) 8505 goto func_qcfg_exit; 8506 8507 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8508 if (BNXT_CHIP_P5(bp)) { 8509 if (BNXT_PF(bp)) 8510 bp->db_offset = DB_PF_OFFSET_P5; 8511 else 8512 bp->db_offset = DB_VF_OFFSET_P5; 8513 } 8514 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8515 1024); 8516 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8517 bp->db_size <= bp->db_offset) 8518 bp->db_size = pci_resource_len(bp->pdev, 2); 8519 8520 func_qcfg_exit: 8521 hwrm_req_drop(bp, req); 8522 return rc; 8523 } 8524 8525 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8526 u8 init_val, u8 init_offset, 8527 bool init_mask_set) 8528 { 8529 ctxm->init_value = init_val; 8530 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8531 if (init_mask_set) 8532 ctxm->init_offset = init_offset * 4; 8533 else 8534 ctxm->init_value = 0; 8535 } 8536 8537 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8538 { 8539 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8540 u16 type; 8541 8542 for (type = 0; type < ctx_max; type++) { 8543 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8544 int n = 1; 8545 8546 if (!ctxm->max_entries || ctxm->pg_info) 8547 continue; 8548 8549 if (ctxm->instance_bmap) 8550 n = hweight32(ctxm->instance_bmap); 8551 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8552 if (!ctxm->pg_info) 8553 return -ENOMEM; 8554 } 8555 return 0; 8556 } 8557 8558 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8559 struct bnxt_ctx_mem_type *ctxm, bool force); 8560 8561 #define BNXT_CTX_INIT_VALID(flags) \ 8562 (!!((flags) & \ 8563 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8564 8565 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8566 { 8567 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8568 struct hwrm_func_backing_store_qcaps_v2_input *req; 8569 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8570 u16 type; 8571 int rc; 8572 8573 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8574 if (rc) 8575 return rc; 8576 8577 if (!ctx) { 8578 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8579 if (!ctx) 8580 return -ENOMEM; 8581 bp->ctx = ctx; 8582 } 8583 8584 resp = hwrm_req_hold(bp, req); 8585 8586 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8587 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8588 u8 init_val, init_off, i; 8589 u32 max_entries; 8590 u16 entry_size; 8591 __le32 *p; 8592 u32 flags; 8593 8594 req->type = cpu_to_le16(type); 8595 rc = hwrm_req_send(bp, req); 8596 if (rc) 8597 goto ctx_done; 8598 flags = le32_to_cpu(resp->flags); 8599 type = le16_to_cpu(resp->next_valid_type); 8600 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8601 bnxt_free_one_ctx_mem(bp, ctxm, true); 8602 continue; 8603 } 8604 entry_size = le16_to_cpu(resp->entry_size); 8605 max_entries = le32_to_cpu(resp->max_num_entries); 8606 if (ctxm->mem_valid) { 8607 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8608 ctxm->entry_size != entry_size || 8609 ctxm->max_entries != max_entries) 8610 bnxt_free_one_ctx_mem(bp, ctxm, true); 8611 else 8612 continue; 8613 } 8614 ctxm->type = le16_to_cpu(resp->type); 8615 ctxm->entry_size = entry_size; 8616 ctxm->flags = flags; 8617 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8618 ctxm->entry_multiple = resp->entry_multiple; 8619 ctxm->max_entries = max_entries; 8620 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8621 init_val = resp->ctx_init_value; 8622 init_off = resp->ctx_init_offset; 8623 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8624 BNXT_CTX_INIT_VALID(flags)); 8625 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8626 BNXT_MAX_SPLIT_ENTRY); 8627 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8628 i++, p++) 8629 ctxm->split[i] = le32_to_cpu(*p); 8630 } 8631 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8632 8633 ctx_done: 8634 hwrm_req_drop(bp, req); 8635 return rc; 8636 } 8637 8638 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8639 { 8640 struct hwrm_func_backing_store_qcaps_output *resp; 8641 struct hwrm_func_backing_store_qcaps_input *req; 8642 int rc; 8643 8644 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8645 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8646 return 0; 8647 8648 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8649 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8650 8651 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8652 if (rc) 8653 return rc; 8654 8655 resp = hwrm_req_hold(bp, req); 8656 rc = hwrm_req_send_silent(bp, req); 8657 if (!rc) { 8658 struct bnxt_ctx_mem_type *ctxm; 8659 struct bnxt_ctx_mem_info *ctx; 8660 u8 init_val, init_idx = 0; 8661 u16 init_mask; 8662 8663 ctx = bp->ctx; 8664 if (!ctx) { 8665 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8666 if (!ctx) { 8667 rc = -ENOMEM; 8668 goto ctx_err; 8669 } 8670 bp->ctx = ctx; 8671 } 8672 init_val = resp->ctx_kind_initializer; 8673 init_mask = le16_to_cpu(resp->ctx_init_mask); 8674 8675 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8676 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8677 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8678 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8679 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8680 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8681 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8682 (init_mask & (1 << init_idx++)) != 0); 8683 8684 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8685 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8686 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8687 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8688 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8689 (init_mask & (1 << init_idx++)) != 0); 8690 8691 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8692 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8693 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8694 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8695 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8696 (init_mask & (1 << init_idx++)) != 0); 8697 8698 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8699 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8700 ctxm->max_entries = ctxm->vnic_entries + 8701 le16_to_cpu(resp->vnic_max_ring_table_entries); 8702 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8703 bnxt_init_ctx_initializer(ctxm, init_val, 8704 resp->vnic_init_offset, 8705 (init_mask & (1 << init_idx++)) != 0); 8706 8707 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8708 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8709 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8710 bnxt_init_ctx_initializer(ctxm, init_val, 8711 resp->stat_init_offset, 8712 (init_mask & (1 << init_idx++)) != 0); 8713 8714 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8715 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8716 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8717 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8718 ctxm->entry_multiple = resp->tqm_entries_multiple; 8719 if (!ctxm->entry_multiple) 8720 ctxm->entry_multiple = 1; 8721 8722 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8723 8724 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8725 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8726 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8727 ctxm->mrav_num_entries_units = 8728 le16_to_cpu(resp->mrav_num_entries_units); 8729 bnxt_init_ctx_initializer(ctxm, init_val, 8730 resp->mrav_init_offset, 8731 (init_mask & (1 << init_idx++)) != 0); 8732 8733 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8734 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8735 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8736 8737 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8738 if (!ctx->tqm_fp_rings_count) 8739 ctx->tqm_fp_rings_count = bp->max_q; 8740 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8741 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8742 8743 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8744 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8745 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8746 8747 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8748 } else { 8749 rc = 0; 8750 } 8751 ctx_err: 8752 hwrm_req_drop(bp, req); 8753 return rc; 8754 } 8755 8756 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8757 __le64 *pg_dir) 8758 { 8759 if (!rmem->nr_pages) 8760 return; 8761 8762 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8763 if (rmem->depth >= 1) { 8764 if (rmem->depth == 2) 8765 *pg_attr |= 2; 8766 else 8767 *pg_attr |= 1; 8768 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8769 } else { 8770 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8771 } 8772 } 8773 8774 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8775 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8776 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8777 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8778 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8779 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8780 8781 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8782 { 8783 struct hwrm_func_backing_store_cfg_input *req; 8784 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8785 struct bnxt_ctx_pg_info *ctx_pg; 8786 struct bnxt_ctx_mem_type *ctxm; 8787 void **__req = (void **)&req; 8788 u32 req_len = sizeof(*req); 8789 __le32 *num_entries; 8790 __le64 *pg_dir; 8791 u32 flags = 0; 8792 u8 *pg_attr; 8793 u32 ena; 8794 int rc; 8795 int i; 8796 8797 if (!ctx) 8798 return 0; 8799 8800 if (req_len > bp->hwrm_max_ext_req_len) 8801 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8802 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8803 if (rc) 8804 return rc; 8805 8806 req->enables = cpu_to_le32(enables); 8807 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8808 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8809 ctx_pg = ctxm->pg_info; 8810 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8811 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8812 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8813 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8814 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8815 &req->qpc_pg_size_qpc_lvl, 8816 &req->qpc_page_dir); 8817 8818 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8819 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8820 } 8821 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8822 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8823 ctx_pg = ctxm->pg_info; 8824 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8825 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8826 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8827 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8828 &req->srq_pg_size_srq_lvl, 8829 &req->srq_page_dir); 8830 } 8831 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8832 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8833 ctx_pg = ctxm->pg_info; 8834 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8835 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8836 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8837 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8838 &req->cq_pg_size_cq_lvl, 8839 &req->cq_page_dir); 8840 } 8841 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8842 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8843 ctx_pg = ctxm->pg_info; 8844 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8845 req->vnic_num_ring_table_entries = 8846 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8847 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8848 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8849 &req->vnic_pg_size_vnic_lvl, 8850 &req->vnic_page_dir); 8851 } 8852 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8853 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8854 ctx_pg = ctxm->pg_info; 8855 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8856 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8857 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8858 &req->stat_pg_size_stat_lvl, 8859 &req->stat_page_dir); 8860 } 8861 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8862 u32 units; 8863 8864 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8865 ctx_pg = ctxm->pg_info; 8866 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8867 units = ctxm->mrav_num_entries_units; 8868 if (units) { 8869 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8870 u32 entries; 8871 8872 num_mr = ctx_pg->entries - num_ah; 8873 entries = ((num_mr / units) << 16) | (num_ah / units); 8874 req->mrav_num_entries = cpu_to_le32(entries); 8875 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8876 } 8877 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8878 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8879 &req->mrav_pg_size_mrav_lvl, 8880 &req->mrav_page_dir); 8881 } 8882 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8883 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8884 ctx_pg = ctxm->pg_info; 8885 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8886 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8887 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8888 &req->tim_pg_size_tim_lvl, 8889 &req->tim_page_dir); 8890 } 8891 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8892 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8893 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8894 pg_dir = &req->tqm_sp_page_dir, 8895 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8896 ctx_pg = ctxm->pg_info; 8897 i < BNXT_MAX_TQM_RINGS; 8898 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8899 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8900 if (!(enables & ena)) 8901 continue; 8902 8903 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8904 *num_entries = cpu_to_le32(ctx_pg->entries); 8905 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8906 } 8907 req->flags = cpu_to_le32(flags); 8908 return hwrm_req_send(bp, req); 8909 } 8910 8911 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8912 struct bnxt_ctx_pg_info *ctx_pg) 8913 { 8914 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8915 8916 rmem->page_size = BNXT_PAGE_SIZE; 8917 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8918 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8919 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8920 if (rmem->depth >= 1) 8921 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8922 return bnxt_alloc_ring(bp, rmem); 8923 } 8924 8925 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8926 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8927 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8928 { 8929 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8930 int rc; 8931 8932 if (!mem_size) 8933 return -EINVAL; 8934 8935 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8936 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8937 ctx_pg->nr_pages = 0; 8938 return -EINVAL; 8939 } 8940 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8941 int nr_tbls, i; 8942 8943 rmem->depth = 2; 8944 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8945 GFP_KERNEL); 8946 if (!ctx_pg->ctx_pg_tbl) 8947 return -ENOMEM; 8948 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8949 rmem->nr_pages = nr_tbls; 8950 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8951 if (rc) 8952 return rc; 8953 for (i = 0; i < nr_tbls; i++) { 8954 struct bnxt_ctx_pg_info *pg_tbl; 8955 8956 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8957 if (!pg_tbl) 8958 return -ENOMEM; 8959 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8960 rmem = &pg_tbl->ring_mem; 8961 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8962 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8963 rmem->depth = 1; 8964 rmem->nr_pages = MAX_CTX_PAGES; 8965 rmem->ctx_mem = ctxm; 8966 if (i == (nr_tbls - 1)) { 8967 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8968 8969 if (rem) 8970 rmem->nr_pages = rem; 8971 } 8972 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8973 if (rc) 8974 break; 8975 } 8976 } else { 8977 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8978 if (rmem->nr_pages > 1 || depth) 8979 rmem->depth = 1; 8980 rmem->ctx_mem = ctxm; 8981 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8982 } 8983 return rc; 8984 } 8985 8986 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 8987 struct bnxt_ctx_pg_info *ctx_pg, 8988 void *buf, size_t offset, size_t head, 8989 size_t tail) 8990 { 8991 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8992 size_t nr_pages = ctx_pg->nr_pages; 8993 int page_size = rmem->page_size; 8994 size_t len = 0, total_len = 0; 8995 u16 depth = rmem->depth; 8996 8997 tail %= nr_pages * page_size; 8998 do { 8999 if (depth > 1) { 9000 int i = head / (page_size * MAX_CTX_PAGES); 9001 struct bnxt_ctx_pg_info *pg_tbl; 9002 9003 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9004 rmem = &pg_tbl->ring_mem; 9005 } 9006 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 9007 head += len; 9008 offset += len; 9009 total_len += len; 9010 if (head >= nr_pages * page_size) 9011 head = 0; 9012 } while (head != tail); 9013 return total_len; 9014 } 9015 9016 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 9017 struct bnxt_ctx_pg_info *ctx_pg) 9018 { 9019 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9020 9021 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 9022 ctx_pg->ctx_pg_tbl) { 9023 int i, nr_tbls = rmem->nr_pages; 9024 9025 for (i = 0; i < nr_tbls; i++) { 9026 struct bnxt_ctx_pg_info *pg_tbl; 9027 struct bnxt_ring_mem_info *rmem2; 9028 9029 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9030 if (!pg_tbl) 9031 continue; 9032 rmem2 = &pg_tbl->ring_mem; 9033 bnxt_free_ring(bp, rmem2); 9034 ctx_pg->ctx_pg_arr[i] = NULL; 9035 kfree(pg_tbl); 9036 ctx_pg->ctx_pg_tbl[i] = NULL; 9037 } 9038 kfree(ctx_pg->ctx_pg_tbl); 9039 ctx_pg->ctx_pg_tbl = NULL; 9040 } 9041 bnxt_free_ring(bp, rmem); 9042 ctx_pg->nr_pages = 0; 9043 } 9044 9045 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 9046 struct bnxt_ctx_mem_type *ctxm, u32 entries, 9047 u8 pg_lvl) 9048 { 9049 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9050 int i, rc = 0, n = 1; 9051 u32 mem_size; 9052 9053 if (!ctxm->entry_size || !ctx_pg) 9054 return -EINVAL; 9055 if (ctxm->instance_bmap) 9056 n = hweight32(ctxm->instance_bmap); 9057 if (ctxm->entry_multiple) 9058 entries = roundup(entries, ctxm->entry_multiple); 9059 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 9060 mem_size = entries * ctxm->entry_size; 9061 for (i = 0; i < n && !rc; i++) { 9062 ctx_pg[i].entries = entries; 9063 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 9064 ctxm->init_value ? ctxm : NULL); 9065 } 9066 if (!rc) 9067 ctxm->mem_valid = 1; 9068 return rc; 9069 } 9070 9071 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 9072 struct bnxt_ctx_mem_type *ctxm, 9073 bool last) 9074 { 9075 struct hwrm_func_backing_store_cfg_v2_input *req; 9076 u32 instance_bmap = ctxm->instance_bmap; 9077 int i, j, rc = 0, n = 1; 9078 __le32 *p; 9079 9080 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 9081 return 0; 9082 9083 if (instance_bmap) 9084 n = hweight32(ctxm->instance_bmap); 9085 else 9086 instance_bmap = 1; 9087 9088 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 9089 if (rc) 9090 return rc; 9091 hwrm_req_hold(bp, req); 9092 req->type = cpu_to_le16(ctxm->type); 9093 req->entry_size = cpu_to_le16(ctxm->entry_size); 9094 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 9095 bnxt_bs_trace_avail(bp, ctxm->type)) { 9096 struct bnxt_bs_trace_info *bs_trace; 9097 u32 enables; 9098 9099 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 9100 req->enables = cpu_to_le32(enables); 9101 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 9102 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 9103 } 9104 req->subtype_valid_cnt = ctxm->split_entry_cnt; 9105 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 9106 p[i] = cpu_to_le32(ctxm->split[i]); 9107 for (i = 0, j = 0; j < n && !rc; i++) { 9108 struct bnxt_ctx_pg_info *ctx_pg; 9109 9110 if (!(instance_bmap & (1 << i))) 9111 continue; 9112 req->instance = cpu_to_le16(i); 9113 ctx_pg = &ctxm->pg_info[j++]; 9114 if (!ctx_pg->entries) 9115 continue; 9116 req->num_entries = cpu_to_le32(ctx_pg->entries); 9117 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9118 &req->page_size_pbl_level, 9119 &req->page_dir); 9120 if (last && j == n) 9121 req->flags = 9122 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 9123 rc = hwrm_req_send(bp, req); 9124 } 9125 hwrm_req_drop(bp, req); 9126 return rc; 9127 } 9128 9129 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena) 9130 { 9131 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9132 struct bnxt_ctx_mem_type *ctxm; 9133 u16 last_type = BNXT_CTX_INV; 9134 int rc = 0; 9135 u16 type; 9136 9137 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) { 9138 ctxm = &ctx->ctx_arr[type]; 9139 if (!bnxt_bs_trace_avail(bp, type)) 9140 continue; 9141 if (!ctxm->mem_valid) { 9142 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 9143 ctxm->max_entries, 1); 9144 if (rc) { 9145 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9146 type); 9147 continue; 9148 } 9149 bnxt_bs_trace_init(bp, ctxm); 9150 } 9151 last_type = type; 9152 } 9153 9154 if (last_type == BNXT_CTX_INV) { 9155 if (!ena) 9156 return 0; 9157 else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) 9158 last_type = BNXT_CTX_MAX - 1; 9159 else 9160 last_type = BNXT_CTX_L2_MAX - 1; 9161 } 9162 ctx->ctx_arr[last_type].last = 1; 9163 9164 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9165 ctxm = &ctx->ctx_arr[type]; 9166 9167 if (!ctxm->mem_valid) 9168 continue; 9169 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9170 if (rc) 9171 return rc; 9172 } 9173 return 0; 9174 } 9175 9176 /** 9177 * __bnxt_copy_ctx_mem - copy host context memory 9178 * @bp: The driver context 9179 * @ctxm: The pointer to the context memory type 9180 * @buf: The destination buffer or NULL to just obtain the length 9181 * @offset: The buffer offset to copy the data to 9182 * @head: The head offset of context memory to copy from 9183 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9184 * 9185 * This function is called for debugging purposes to dump the host context 9186 * used by the chip. 9187 * 9188 * Return: Length of memory copied 9189 */ 9190 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9191 struct bnxt_ctx_mem_type *ctxm, void *buf, 9192 size_t offset, size_t head, size_t tail) 9193 { 9194 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9195 size_t len = 0, total_len = 0; 9196 int i, n = 1; 9197 9198 if (!ctx_pg) 9199 return 0; 9200 9201 if (ctxm->instance_bmap) 9202 n = hweight32(ctxm->instance_bmap); 9203 for (i = 0; i < n; i++) { 9204 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9205 tail); 9206 offset += len; 9207 total_len += len; 9208 } 9209 return total_len; 9210 } 9211 9212 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9213 void *buf, size_t offset) 9214 { 9215 size_t tail = ctxm->max_entries * ctxm->entry_size; 9216 9217 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9218 } 9219 9220 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9221 struct bnxt_ctx_mem_type *ctxm, bool force) 9222 { 9223 struct bnxt_ctx_pg_info *ctx_pg; 9224 int i, n = 1; 9225 9226 ctxm->last = 0; 9227 9228 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9229 return; 9230 9231 ctx_pg = ctxm->pg_info; 9232 if (ctx_pg) { 9233 if (ctxm->instance_bmap) 9234 n = hweight32(ctxm->instance_bmap); 9235 for (i = 0; i < n; i++) 9236 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9237 9238 kfree(ctx_pg); 9239 ctxm->pg_info = NULL; 9240 ctxm->mem_valid = 0; 9241 } 9242 memset(ctxm, 0, sizeof(*ctxm)); 9243 } 9244 9245 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9246 { 9247 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9248 u16 type; 9249 9250 if (!ctx) 9251 return; 9252 9253 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9254 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9255 9256 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9257 if (force) { 9258 kfree(ctx); 9259 bp->ctx = NULL; 9260 } 9261 } 9262 9263 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9264 { 9265 struct bnxt_ctx_mem_type *ctxm; 9266 struct bnxt_ctx_mem_info *ctx; 9267 u32 l2_qps, qp1_qps, max_qps; 9268 u32 ena, entries_sp, entries; 9269 u32 srqs, max_srqs, min; 9270 u32 num_mr, num_ah; 9271 u32 extra_srqs = 0; 9272 u32 extra_qps = 0; 9273 u32 fast_qpmd_qps; 9274 u8 pg_lvl = 1; 9275 int i, rc; 9276 9277 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9278 if (rc) { 9279 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9280 rc); 9281 return rc; 9282 } 9283 ctx = bp->ctx; 9284 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9285 return 0; 9286 9287 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9288 l2_qps = ctxm->qp_l2_entries; 9289 qp1_qps = ctxm->qp_qp1_entries; 9290 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9291 max_qps = ctxm->max_entries; 9292 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9293 srqs = ctxm->srq_l2_entries; 9294 max_srqs = ctxm->max_entries; 9295 ena = 0; 9296 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9297 pg_lvl = 2; 9298 if (BNXT_SW_RES_LMT(bp)) { 9299 extra_qps = max_qps - l2_qps - qp1_qps; 9300 extra_srqs = max_srqs - srqs; 9301 } else { 9302 extra_qps = min_t(u32, 65536, 9303 max_qps - l2_qps - qp1_qps); 9304 /* allocate extra qps if fw supports RoCE fast qp 9305 * destroy feature 9306 */ 9307 extra_qps += fast_qpmd_qps; 9308 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9309 } 9310 if (fast_qpmd_qps) 9311 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9312 } 9313 9314 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9315 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9316 pg_lvl); 9317 if (rc) 9318 return rc; 9319 9320 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9321 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9322 if (rc) 9323 return rc; 9324 9325 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9326 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9327 extra_qps * 2, pg_lvl); 9328 if (rc) 9329 return rc; 9330 9331 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9332 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9333 if (rc) 9334 return rc; 9335 9336 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9337 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9338 if (rc) 9339 return rc; 9340 9341 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9342 goto skip_rdma; 9343 9344 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9345 if (BNXT_SW_RES_LMT(bp) && 9346 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9347 num_ah = ctxm->mrav_av_entries; 9348 num_mr = ctxm->max_entries - num_ah; 9349 } else { 9350 /* 128K extra is needed to accommodate static AH context 9351 * allocation by f/w. 9352 */ 9353 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9354 num_ah = min_t(u32, num_mr, 1024 * 128); 9355 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9356 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9357 ctxm->mrav_av_entries = num_ah; 9358 } 9359 9360 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9361 if (rc) 9362 return rc; 9363 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9364 9365 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9366 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9367 if (rc) 9368 return rc; 9369 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9370 9371 skip_rdma: 9372 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9373 min = ctxm->min_entries; 9374 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9375 2 * (extra_qps + qp1_qps) + min; 9376 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9377 if (rc) 9378 return rc; 9379 9380 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9381 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9382 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9383 if (rc) 9384 return rc; 9385 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9386 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9387 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9388 9389 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9390 rc = bnxt_backing_store_cfg_v2(bp, ena); 9391 else 9392 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9393 if (rc) { 9394 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9395 rc); 9396 return rc; 9397 } 9398 ctx->flags |= BNXT_CTX_FLAG_INITED; 9399 return 0; 9400 } 9401 9402 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9403 { 9404 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9405 u16 page_attr; 9406 int rc; 9407 9408 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9409 return 0; 9410 9411 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9412 if (rc) 9413 return rc; 9414 9415 if (BNXT_PAGE_SIZE == 0x2000) 9416 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9417 else if (BNXT_PAGE_SIZE == 0x10000) 9418 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9419 else 9420 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9421 req->pg_size_lvl = cpu_to_le16(page_attr | 9422 bp->fw_crash_mem->ring_mem.depth); 9423 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9424 req->size = cpu_to_le32(bp->fw_crash_len); 9425 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9426 return hwrm_req_send(bp, req); 9427 } 9428 9429 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9430 { 9431 if (bp->fw_crash_mem) { 9432 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9433 kfree(bp->fw_crash_mem); 9434 bp->fw_crash_mem = NULL; 9435 } 9436 } 9437 9438 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9439 { 9440 u32 mem_size = 0; 9441 int rc; 9442 9443 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9444 return 0; 9445 9446 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9447 if (rc) 9448 return rc; 9449 9450 mem_size = round_up(mem_size, 4); 9451 9452 /* keep and use the existing pages */ 9453 if (bp->fw_crash_mem && 9454 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9455 goto alloc_done; 9456 9457 if (bp->fw_crash_mem) 9458 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9459 else 9460 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 9461 GFP_KERNEL); 9462 if (!bp->fw_crash_mem) 9463 return -ENOMEM; 9464 9465 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9466 if (rc) { 9467 bnxt_free_crash_dump_mem(bp); 9468 return rc; 9469 } 9470 9471 alloc_done: 9472 bp->fw_crash_len = mem_size; 9473 return 0; 9474 } 9475 9476 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9477 { 9478 struct hwrm_func_resource_qcaps_output *resp; 9479 struct hwrm_func_resource_qcaps_input *req; 9480 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9481 int rc; 9482 9483 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9484 if (rc) 9485 return rc; 9486 9487 req->fid = cpu_to_le16(0xffff); 9488 resp = hwrm_req_hold(bp, req); 9489 rc = hwrm_req_send_silent(bp, req); 9490 if (rc) 9491 goto hwrm_func_resc_qcaps_exit; 9492 9493 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9494 if (!all) 9495 goto hwrm_func_resc_qcaps_exit; 9496 9497 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9498 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9499 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9500 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9501 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9502 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9503 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9504 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9505 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9506 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9507 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9508 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9509 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9510 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9511 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9512 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9513 9514 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9515 u16 max_msix = le16_to_cpu(resp->max_msix); 9516 9517 hw_resc->max_nqs = max_msix; 9518 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9519 } 9520 9521 if (BNXT_PF(bp)) { 9522 struct bnxt_pf_info *pf = &bp->pf; 9523 9524 pf->vf_resv_strategy = 9525 le16_to_cpu(resp->vf_reservation_strategy); 9526 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9527 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9528 } 9529 hwrm_func_resc_qcaps_exit: 9530 hwrm_req_drop(bp, req); 9531 return rc; 9532 } 9533 9534 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9535 { 9536 struct hwrm_port_mac_ptp_qcfg_output *resp; 9537 struct hwrm_port_mac_ptp_qcfg_input *req; 9538 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9539 u8 flags; 9540 int rc; 9541 9542 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9543 rc = -ENODEV; 9544 goto no_ptp; 9545 } 9546 9547 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9548 if (rc) 9549 goto no_ptp; 9550 9551 req->port_id = cpu_to_le16(bp->pf.port_id); 9552 resp = hwrm_req_hold(bp, req); 9553 rc = hwrm_req_send(bp, req); 9554 if (rc) 9555 goto exit; 9556 9557 flags = resp->flags; 9558 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9559 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9560 rc = -ENODEV; 9561 goto exit; 9562 } 9563 if (!ptp) { 9564 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9565 if (!ptp) { 9566 rc = -ENOMEM; 9567 goto exit; 9568 } 9569 ptp->bp = bp; 9570 bp->ptp_cfg = ptp; 9571 } 9572 9573 if (flags & 9574 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9575 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9576 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9577 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9578 } else if (BNXT_CHIP_P5(bp)) { 9579 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9580 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9581 } else { 9582 rc = -ENODEV; 9583 goto exit; 9584 } 9585 ptp->rtc_configured = 9586 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9587 rc = bnxt_ptp_init(bp); 9588 if (rc) 9589 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9590 exit: 9591 hwrm_req_drop(bp, req); 9592 if (!rc) 9593 return 0; 9594 9595 no_ptp: 9596 bnxt_ptp_clear(bp); 9597 kfree(ptp); 9598 bp->ptp_cfg = NULL; 9599 return rc; 9600 } 9601 9602 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9603 { 9604 struct hwrm_func_qcaps_output *resp; 9605 struct hwrm_func_qcaps_input *req; 9606 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9607 u32 flags, flags_ext, flags_ext2; 9608 int rc; 9609 9610 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9611 if (rc) 9612 return rc; 9613 9614 req->fid = cpu_to_le16(0xffff); 9615 resp = hwrm_req_hold(bp, req); 9616 rc = hwrm_req_send(bp, req); 9617 if (rc) 9618 goto hwrm_func_qcaps_exit; 9619 9620 flags = le32_to_cpu(resp->flags); 9621 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9622 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9623 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9624 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9625 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9626 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9627 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9628 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9629 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9630 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9631 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9632 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9633 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9634 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9635 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9636 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9637 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9638 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9639 9640 flags_ext = le32_to_cpu(resp->flags_ext); 9641 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9642 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9643 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9644 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9645 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9646 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9647 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9648 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9649 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9650 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9651 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED) 9652 bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2; 9653 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9654 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9655 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9656 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9657 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9658 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9659 9660 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9661 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9662 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9663 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9664 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9665 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9666 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9667 if (flags_ext2 & 9668 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9669 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9670 if (BNXT_PF(bp) && 9671 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9672 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9673 9674 bp->tx_push_thresh = 0; 9675 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9676 BNXT_FW_MAJ(bp) > 217) 9677 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9678 9679 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9680 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9681 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9682 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9683 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9684 if (!hw_resc->max_hw_ring_grps) 9685 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9686 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9687 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9688 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9689 9690 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9691 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9692 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9693 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9694 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9695 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9696 9697 if (BNXT_PF(bp)) { 9698 struct bnxt_pf_info *pf = &bp->pf; 9699 9700 pf->fw_fid = le16_to_cpu(resp->fid); 9701 pf->port_id = le16_to_cpu(resp->port_id); 9702 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9703 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9704 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9705 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9706 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9707 bp->flags |= BNXT_FLAG_WOL_CAP; 9708 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9709 bp->fw_cap |= BNXT_FW_CAP_PTP; 9710 } else { 9711 bnxt_ptp_clear(bp); 9712 kfree(bp->ptp_cfg); 9713 bp->ptp_cfg = NULL; 9714 } 9715 } else { 9716 #ifdef CONFIG_BNXT_SRIOV 9717 struct bnxt_vf_info *vf = &bp->vf; 9718 9719 vf->fw_fid = le16_to_cpu(resp->fid); 9720 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9721 #endif 9722 } 9723 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9724 9725 hwrm_func_qcaps_exit: 9726 hwrm_req_drop(bp, req); 9727 return rc; 9728 } 9729 9730 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9731 { 9732 struct hwrm_dbg_qcaps_output *resp; 9733 struct hwrm_dbg_qcaps_input *req; 9734 int rc; 9735 9736 bp->fw_dbg_cap = 0; 9737 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9738 return; 9739 9740 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9741 if (rc) 9742 return; 9743 9744 req->fid = cpu_to_le16(0xffff); 9745 resp = hwrm_req_hold(bp, req); 9746 rc = hwrm_req_send(bp, req); 9747 if (rc) 9748 goto hwrm_dbg_qcaps_exit; 9749 9750 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9751 9752 hwrm_dbg_qcaps_exit: 9753 hwrm_req_drop(bp, req); 9754 } 9755 9756 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9757 9758 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9759 { 9760 int rc; 9761 9762 rc = __bnxt_hwrm_func_qcaps(bp); 9763 if (rc) 9764 return rc; 9765 9766 bnxt_hwrm_dbg_qcaps(bp); 9767 9768 rc = bnxt_hwrm_queue_qportcfg(bp); 9769 if (rc) { 9770 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9771 return rc; 9772 } 9773 if (bp->hwrm_spec_code >= 0x10803) { 9774 rc = bnxt_alloc_ctx_mem(bp); 9775 if (rc) 9776 return rc; 9777 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9778 if (!rc) 9779 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9780 } 9781 return 0; 9782 } 9783 9784 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9785 { 9786 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9787 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9788 u32 flags; 9789 int rc; 9790 9791 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9792 return 0; 9793 9794 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9795 if (rc) 9796 return rc; 9797 9798 resp = hwrm_req_hold(bp, req); 9799 rc = hwrm_req_send(bp, req); 9800 if (rc) 9801 goto hwrm_cfa_adv_qcaps_exit; 9802 9803 flags = le32_to_cpu(resp->flags); 9804 if (flags & 9805 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9806 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9807 9808 if (flags & 9809 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9810 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9811 9812 if (flags & 9813 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9814 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9815 9816 hwrm_cfa_adv_qcaps_exit: 9817 hwrm_req_drop(bp, req); 9818 return rc; 9819 } 9820 9821 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9822 { 9823 if (bp->fw_health) 9824 return 0; 9825 9826 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9827 if (!bp->fw_health) 9828 return -ENOMEM; 9829 9830 mutex_init(&bp->fw_health->lock); 9831 return 0; 9832 } 9833 9834 static int bnxt_alloc_fw_health(struct bnxt *bp) 9835 { 9836 int rc; 9837 9838 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9839 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9840 return 0; 9841 9842 rc = __bnxt_alloc_fw_health(bp); 9843 if (rc) { 9844 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9845 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9846 return rc; 9847 } 9848 9849 return 0; 9850 } 9851 9852 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9853 { 9854 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9855 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9856 BNXT_FW_HEALTH_WIN_MAP_OFF); 9857 } 9858 9859 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9860 { 9861 struct bnxt_fw_health *fw_health = bp->fw_health; 9862 u32 reg_type; 9863 9864 if (!fw_health) 9865 return; 9866 9867 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9868 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9869 fw_health->status_reliable = false; 9870 9871 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9872 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9873 fw_health->resets_reliable = false; 9874 } 9875 9876 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9877 { 9878 void __iomem *hs; 9879 u32 status_loc; 9880 u32 reg_type; 9881 u32 sig; 9882 9883 if (bp->fw_health) 9884 bp->fw_health->status_reliable = false; 9885 9886 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9887 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9888 9889 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9890 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9891 if (!bp->chip_num) { 9892 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9893 bp->chip_num = readl(bp->bar0 + 9894 BNXT_FW_HEALTH_WIN_BASE + 9895 BNXT_GRC_REG_CHIP_NUM); 9896 } 9897 if (!BNXT_CHIP_P5_PLUS(bp)) 9898 return; 9899 9900 status_loc = BNXT_GRC_REG_STATUS_P5 | 9901 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9902 } else { 9903 status_loc = readl(hs + offsetof(struct hcomm_status, 9904 fw_status_loc)); 9905 } 9906 9907 if (__bnxt_alloc_fw_health(bp)) { 9908 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9909 return; 9910 } 9911 9912 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9913 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9914 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9915 __bnxt_map_fw_health_reg(bp, status_loc); 9916 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9917 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9918 } 9919 9920 bp->fw_health->status_reliable = true; 9921 } 9922 9923 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9924 { 9925 struct bnxt_fw_health *fw_health = bp->fw_health; 9926 u32 reg_base = 0xffffffff; 9927 int i; 9928 9929 bp->fw_health->status_reliable = false; 9930 bp->fw_health->resets_reliable = false; 9931 /* Only pre-map the monitoring GRC registers using window 3 */ 9932 for (i = 0; i < 4; i++) { 9933 u32 reg = fw_health->regs[i]; 9934 9935 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9936 continue; 9937 if (reg_base == 0xffffffff) 9938 reg_base = reg & BNXT_GRC_BASE_MASK; 9939 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9940 return -ERANGE; 9941 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9942 } 9943 bp->fw_health->status_reliable = true; 9944 bp->fw_health->resets_reliable = true; 9945 if (reg_base == 0xffffffff) 9946 return 0; 9947 9948 __bnxt_map_fw_health_reg(bp, reg_base); 9949 return 0; 9950 } 9951 9952 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9953 { 9954 if (!bp->fw_health) 9955 return; 9956 9957 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9958 bp->fw_health->status_reliable = true; 9959 bp->fw_health->resets_reliable = true; 9960 } else { 9961 bnxt_try_map_fw_health_reg(bp); 9962 } 9963 } 9964 9965 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 9966 { 9967 struct bnxt_fw_health *fw_health = bp->fw_health; 9968 struct hwrm_error_recovery_qcfg_output *resp; 9969 struct hwrm_error_recovery_qcfg_input *req; 9970 int rc, i; 9971 9972 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9973 return 0; 9974 9975 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 9976 if (rc) 9977 return rc; 9978 9979 resp = hwrm_req_hold(bp, req); 9980 rc = hwrm_req_send(bp, req); 9981 if (rc) 9982 goto err_recovery_out; 9983 fw_health->flags = le32_to_cpu(resp->flags); 9984 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 9985 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 9986 rc = -EINVAL; 9987 goto err_recovery_out; 9988 } 9989 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 9990 fw_health->master_func_wait_dsecs = 9991 le32_to_cpu(resp->master_func_wait_period); 9992 fw_health->normal_func_wait_dsecs = 9993 le32_to_cpu(resp->normal_func_wait_period); 9994 fw_health->post_reset_wait_dsecs = 9995 le32_to_cpu(resp->master_func_wait_period_after_reset); 9996 fw_health->post_reset_max_wait_dsecs = 9997 le32_to_cpu(resp->max_bailout_time_after_reset); 9998 fw_health->regs[BNXT_FW_HEALTH_REG] = 9999 le32_to_cpu(resp->fw_health_status_reg); 10000 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 10001 le32_to_cpu(resp->fw_heartbeat_reg); 10002 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 10003 le32_to_cpu(resp->fw_reset_cnt_reg); 10004 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 10005 le32_to_cpu(resp->reset_inprogress_reg); 10006 fw_health->fw_reset_inprog_reg_mask = 10007 le32_to_cpu(resp->reset_inprogress_reg_mask); 10008 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 10009 if (fw_health->fw_reset_seq_cnt >= 16) { 10010 rc = -EINVAL; 10011 goto err_recovery_out; 10012 } 10013 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 10014 fw_health->fw_reset_seq_regs[i] = 10015 le32_to_cpu(resp->reset_reg[i]); 10016 fw_health->fw_reset_seq_vals[i] = 10017 le32_to_cpu(resp->reset_reg_val[i]); 10018 fw_health->fw_reset_seq_delay_msec[i] = 10019 resp->delay_after_reset[i]; 10020 } 10021 err_recovery_out: 10022 hwrm_req_drop(bp, req); 10023 if (!rc) 10024 rc = bnxt_map_fw_health_regs(bp); 10025 if (rc) 10026 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10027 return rc; 10028 } 10029 10030 static int bnxt_hwrm_func_reset(struct bnxt *bp) 10031 { 10032 struct hwrm_func_reset_input *req; 10033 int rc; 10034 10035 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 10036 if (rc) 10037 return rc; 10038 10039 req->enables = 0; 10040 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 10041 return hwrm_req_send(bp, req); 10042 } 10043 10044 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 10045 { 10046 struct hwrm_nvm_get_dev_info_output nvm_info; 10047 10048 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 10049 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 10050 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 10051 nvm_info.nvm_cfg_ver_upd); 10052 } 10053 10054 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 10055 { 10056 struct hwrm_queue_qportcfg_output *resp; 10057 struct hwrm_queue_qportcfg_input *req; 10058 u8 i, j, *qptr; 10059 bool no_rdma; 10060 int rc = 0; 10061 10062 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 10063 if (rc) 10064 return rc; 10065 10066 resp = hwrm_req_hold(bp, req); 10067 rc = hwrm_req_send(bp, req); 10068 if (rc) 10069 goto qportcfg_exit; 10070 10071 if (!resp->max_configurable_queues) { 10072 rc = -EINVAL; 10073 goto qportcfg_exit; 10074 } 10075 bp->max_tc = resp->max_configurable_queues; 10076 bp->max_lltc = resp->max_configurable_lossless_queues; 10077 if (bp->max_tc > BNXT_MAX_QUEUE) 10078 bp->max_tc = BNXT_MAX_QUEUE; 10079 10080 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 10081 qptr = &resp->queue_id0; 10082 for (i = 0, j = 0; i < bp->max_tc; i++) { 10083 bp->q_info[j].queue_id = *qptr; 10084 bp->q_ids[i] = *qptr++; 10085 bp->q_info[j].queue_profile = *qptr++; 10086 bp->tc_to_qidx[j] = j; 10087 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 10088 (no_rdma && BNXT_PF(bp))) 10089 j++; 10090 } 10091 bp->max_q = bp->max_tc; 10092 bp->max_tc = max_t(u8, j, 1); 10093 10094 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 10095 bp->max_tc = 1; 10096 10097 if (bp->max_lltc > bp->max_tc) 10098 bp->max_lltc = bp->max_tc; 10099 10100 qportcfg_exit: 10101 hwrm_req_drop(bp, req); 10102 return rc; 10103 } 10104 10105 static int bnxt_hwrm_poll(struct bnxt *bp) 10106 { 10107 struct hwrm_ver_get_input *req; 10108 int rc; 10109 10110 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10111 if (rc) 10112 return rc; 10113 10114 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10115 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10116 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10117 10118 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 10119 rc = hwrm_req_send(bp, req); 10120 return rc; 10121 } 10122 10123 static int bnxt_hwrm_ver_get(struct bnxt *bp) 10124 { 10125 struct hwrm_ver_get_output *resp; 10126 struct hwrm_ver_get_input *req; 10127 u16 fw_maj, fw_min, fw_bld, fw_rsv; 10128 u32 dev_caps_cfg, hwrm_ver; 10129 int rc, len, max_tmo_secs; 10130 10131 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10132 if (rc) 10133 return rc; 10134 10135 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 10136 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 10137 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10138 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10139 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10140 10141 resp = hwrm_req_hold(bp, req); 10142 rc = hwrm_req_send(bp, req); 10143 if (rc) 10144 goto hwrm_ver_get_exit; 10145 10146 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 10147 10148 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10149 resp->hwrm_intf_min_8b << 8 | 10150 resp->hwrm_intf_upd_8b; 10151 if (resp->hwrm_intf_maj_8b < 1) { 10152 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10153 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10154 resp->hwrm_intf_upd_8b); 10155 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10156 } 10157 10158 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10159 HWRM_VERSION_UPDATE; 10160 10161 if (bp->hwrm_spec_code > hwrm_ver) 10162 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10163 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10164 HWRM_VERSION_UPDATE); 10165 else 10166 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10167 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10168 resp->hwrm_intf_upd_8b); 10169 10170 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10171 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10172 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10173 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10174 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10175 len = FW_VER_STR_LEN; 10176 } else { 10177 fw_maj = resp->hwrm_fw_maj_8b; 10178 fw_min = resp->hwrm_fw_min_8b; 10179 fw_bld = resp->hwrm_fw_bld_8b; 10180 fw_rsv = resp->hwrm_fw_rsvd_8b; 10181 len = BC_HWRM_STR_LEN; 10182 } 10183 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10184 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10185 fw_rsv); 10186 10187 if (strlen(resp->active_pkg_name)) { 10188 int fw_ver_len = strlen(bp->fw_ver_str); 10189 10190 snprintf(bp->fw_ver_str + fw_ver_len, 10191 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10192 resp->active_pkg_name); 10193 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10194 } 10195 10196 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10197 if (!bp->hwrm_cmd_timeout) 10198 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10199 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10200 if (!bp->hwrm_cmd_max_timeout) 10201 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10202 max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000; 10203 #ifdef CONFIG_DETECT_HUNG_TASK 10204 if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT || 10205 max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) { 10206 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n", 10207 max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT); 10208 } 10209 #endif 10210 10211 if (resp->hwrm_intf_maj_8b >= 1) { 10212 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10213 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10214 } 10215 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10216 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10217 10218 bp->chip_num = le16_to_cpu(resp->chip_num); 10219 bp->chip_rev = resp->chip_rev; 10220 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10221 !resp->chip_metal) 10222 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10223 10224 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10225 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10226 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10227 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10228 10229 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10230 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10231 10232 if (dev_caps_cfg & 10233 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10234 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10235 10236 if (dev_caps_cfg & 10237 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10238 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10239 10240 if (dev_caps_cfg & 10241 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10242 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10243 10244 hwrm_ver_get_exit: 10245 hwrm_req_drop(bp, req); 10246 return rc; 10247 } 10248 10249 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10250 { 10251 struct hwrm_fw_set_time_input *req; 10252 struct tm tm; 10253 time64_t now = ktime_get_real_seconds(); 10254 int rc; 10255 10256 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10257 bp->hwrm_spec_code < 0x10400) 10258 return -EOPNOTSUPP; 10259 10260 time64_to_tm(now, 0, &tm); 10261 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10262 if (rc) 10263 return rc; 10264 10265 req->year = cpu_to_le16(1900 + tm.tm_year); 10266 req->month = 1 + tm.tm_mon; 10267 req->day = tm.tm_mday; 10268 req->hour = tm.tm_hour; 10269 req->minute = tm.tm_min; 10270 req->second = tm.tm_sec; 10271 return hwrm_req_send(bp, req); 10272 } 10273 10274 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10275 { 10276 u64 sw_tmp; 10277 10278 hw &= mask; 10279 sw_tmp = (*sw & ~mask) | hw; 10280 if (hw < (*sw & mask)) 10281 sw_tmp += mask + 1; 10282 WRITE_ONCE(*sw, sw_tmp); 10283 } 10284 10285 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10286 int count, bool ignore_zero) 10287 { 10288 int i; 10289 10290 for (i = 0; i < count; i++) { 10291 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10292 10293 if (ignore_zero && !hw) 10294 continue; 10295 10296 if (masks[i] == -1ULL) 10297 sw_stats[i] = hw; 10298 else 10299 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10300 } 10301 } 10302 10303 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10304 { 10305 if (!stats->hw_stats) 10306 return; 10307 10308 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10309 stats->hw_masks, stats->len / 8, false); 10310 } 10311 10312 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10313 { 10314 struct bnxt_stats_mem *ring0_stats; 10315 bool ignore_zero = false; 10316 int i; 10317 10318 /* Chip bug. Counter intermittently becomes 0. */ 10319 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10320 ignore_zero = true; 10321 10322 for (i = 0; i < bp->cp_nr_rings; i++) { 10323 struct bnxt_napi *bnapi = bp->bnapi[i]; 10324 struct bnxt_cp_ring_info *cpr; 10325 struct bnxt_stats_mem *stats; 10326 10327 cpr = &bnapi->cp_ring; 10328 stats = &cpr->stats; 10329 if (!i) 10330 ring0_stats = stats; 10331 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10332 ring0_stats->hw_masks, 10333 ring0_stats->len / 8, ignore_zero); 10334 } 10335 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10336 struct bnxt_stats_mem *stats = &bp->port_stats; 10337 __le64 *hw_stats = stats->hw_stats; 10338 u64 *sw_stats = stats->sw_stats; 10339 u64 *masks = stats->hw_masks; 10340 int cnt; 10341 10342 cnt = sizeof(struct rx_port_stats) / 8; 10343 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10344 10345 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10346 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10347 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10348 cnt = sizeof(struct tx_port_stats) / 8; 10349 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10350 } 10351 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10352 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10353 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10354 } 10355 } 10356 10357 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10358 { 10359 struct hwrm_port_qstats_input *req; 10360 struct bnxt_pf_info *pf = &bp->pf; 10361 int rc; 10362 10363 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10364 return 0; 10365 10366 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10367 return -EOPNOTSUPP; 10368 10369 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10370 if (rc) 10371 return rc; 10372 10373 req->flags = flags; 10374 req->port_id = cpu_to_le16(pf->port_id); 10375 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10376 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10377 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10378 return hwrm_req_send(bp, req); 10379 } 10380 10381 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10382 { 10383 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10384 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10385 struct hwrm_port_qstats_ext_output *resp_qs; 10386 struct hwrm_port_qstats_ext_input *req_qs; 10387 struct bnxt_pf_info *pf = &bp->pf; 10388 u32 tx_stat_size; 10389 int rc; 10390 10391 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10392 return 0; 10393 10394 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10395 return -EOPNOTSUPP; 10396 10397 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10398 if (rc) 10399 return rc; 10400 10401 req_qs->flags = flags; 10402 req_qs->port_id = cpu_to_le16(pf->port_id); 10403 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10404 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10405 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10406 sizeof(struct tx_port_stats_ext) : 0; 10407 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10408 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10409 resp_qs = hwrm_req_hold(bp, req_qs); 10410 rc = hwrm_req_send(bp, req_qs); 10411 if (!rc) { 10412 bp->fw_rx_stats_ext_size = 10413 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10414 if (BNXT_FW_MAJ(bp) < 220 && 10415 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10416 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10417 10418 bp->fw_tx_stats_ext_size = tx_stat_size ? 10419 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10420 } else { 10421 bp->fw_rx_stats_ext_size = 0; 10422 bp->fw_tx_stats_ext_size = 0; 10423 } 10424 hwrm_req_drop(bp, req_qs); 10425 10426 if (flags) 10427 return rc; 10428 10429 if (bp->fw_tx_stats_ext_size <= 10430 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10431 bp->pri2cos_valid = 0; 10432 return rc; 10433 } 10434 10435 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10436 if (rc) 10437 return rc; 10438 10439 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10440 10441 resp_qc = hwrm_req_hold(bp, req_qc); 10442 rc = hwrm_req_send(bp, req_qc); 10443 if (!rc) { 10444 u8 *pri2cos; 10445 int i, j; 10446 10447 pri2cos = &resp_qc->pri0_cos_queue_id; 10448 for (i = 0; i < 8; i++) { 10449 u8 queue_id = pri2cos[i]; 10450 u8 queue_idx; 10451 10452 /* Per port queue IDs start from 0, 10, 20, etc */ 10453 queue_idx = queue_id % 10; 10454 if (queue_idx > BNXT_MAX_QUEUE) { 10455 bp->pri2cos_valid = false; 10456 hwrm_req_drop(bp, req_qc); 10457 return rc; 10458 } 10459 for (j = 0; j < bp->max_q; j++) { 10460 if (bp->q_ids[j] == queue_id) 10461 bp->pri2cos_idx[i] = queue_idx; 10462 } 10463 } 10464 bp->pri2cos_valid = true; 10465 } 10466 hwrm_req_drop(bp, req_qc); 10467 10468 return rc; 10469 } 10470 10471 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10472 { 10473 bnxt_hwrm_tunnel_dst_port_free(bp, 10474 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10475 bnxt_hwrm_tunnel_dst_port_free(bp, 10476 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10477 } 10478 10479 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10480 { 10481 int rc, i; 10482 u32 tpa_flags = 0; 10483 10484 if (set_tpa) 10485 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10486 else if (BNXT_NO_FW_ACCESS(bp)) 10487 return 0; 10488 for (i = 0; i < bp->nr_vnics; i++) { 10489 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10490 if (rc) { 10491 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10492 i, rc); 10493 return rc; 10494 } 10495 } 10496 return 0; 10497 } 10498 10499 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10500 { 10501 int i; 10502 10503 for (i = 0; i < bp->nr_vnics; i++) 10504 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10505 } 10506 10507 static void bnxt_clear_vnic(struct bnxt *bp) 10508 { 10509 if (!bp->vnic_info) 10510 return; 10511 10512 bnxt_hwrm_clear_vnic_filter(bp); 10513 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10514 /* clear all RSS setting before free vnic ctx */ 10515 bnxt_hwrm_clear_vnic_rss(bp); 10516 bnxt_hwrm_vnic_ctx_free(bp); 10517 } 10518 /* before free the vnic, undo the vnic tpa settings */ 10519 if (bp->flags & BNXT_FLAG_TPA) 10520 bnxt_set_tpa(bp, false); 10521 bnxt_hwrm_vnic_free(bp); 10522 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10523 bnxt_hwrm_vnic_ctx_free(bp); 10524 } 10525 10526 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10527 bool irq_re_init) 10528 { 10529 bnxt_clear_vnic(bp); 10530 bnxt_hwrm_ring_free(bp, close_path); 10531 bnxt_hwrm_ring_grp_free(bp); 10532 if (irq_re_init) { 10533 bnxt_hwrm_stat_ctx_free(bp); 10534 bnxt_hwrm_free_tunnel_ports(bp); 10535 } 10536 } 10537 10538 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10539 { 10540 struct hwrm_func_cfg_input *req; 10541 u8 evb_mode; 10542 int rc; 10543 10544 if (br_mode == BRIDGE_MODE_VEB) 10545 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10546 else if (br_mode == BRIDGE_MODE_VEPA) 10547 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10548 else 10549 return -EINVAL; 10550 10551 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10552 if (rc) 10553 return rc; 10554 10555 req->fid = cpu_to_le16(0xffff); 10556 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10557 req->evb_mode = evb_mode; 10558 return hwrm_req_send(bp, req); 10559 } 10560 10561 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10562 { 10563 struct hwrm_func_cfg_input *req; 10564 int rc; 10565 10566 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10567 return 0; 10568 10569 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10570 if (rc) 10571 return rc; 10572 10573 req->fid = cpu_to_le16(0xffff); 10574 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10575 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10576 if (size == 128) 10577 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10578 10579 return hwrm_req_send(bp, req); 10580 } 10581 10582 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10583 { 10584 int rc; 10585 10586 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10587 goto skip_rss_ctx; 10588 10589 /* allocate context for vnic */ 10590 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10591 if (rc) { 10592 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10593 vnic->vnic_id, rc); 10594 goto vnic_setup_err; 10595 } 10596 bp->rsscos_nr_ctxs++; 10597 10598 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10599 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10600 if (rc) { 10601 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10602 vnic->vnic_id, rc); 10603 goto vnic_setup_err; 10604 } 10605 bp->rsscos_nr_ctxs++; 10606 } 10607 10608 skip_rss_ctx: 10609 /* configure default vnic, ring grp */ 10610 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10611 if (rc) { 10612 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10613 vnic->vnic_id, rc); 10614 goto vnic_setup_err; 10615 } 10616 10617 /* Enable RSS hashing on vnic */ 10618 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10619 if (rc) { 10620 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10621 vnic->vnic_id, rc); 10622 goto vnic_setup_err; 10623 } 10624 10625 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10626 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10627 if (rc) { 10628 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10629 vnic->vnic_id, rc); 10630 } 10631 } 10632 10633 vnic_setup_err: 10634 return rc; 10635 } 10636 10637 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10638 u8 valid) 10639 { 10640 struct hwrm_vnic_update_input *req; 10641 int rc; 10642 10643 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10644 if (rc) 10645 return rc; 10646 10647 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10648 10649 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10650 req->mru = cpu_to_le16(vnic->mru); 10651 10652 req->enables = cpu_to_le32(valid); 10653 10654 return hwrm_req_send(bp, req); 10655 } 10656 10657 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10658 { 10659 int rc; 10660 10661 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10662 if (rc) { 10663 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10664 vnic->vnic_id, rc); 10665 return rc; 10666 } 10667 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10668 if (rc) 10669 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10670 vnic->vnic_id, rc); 10671 return rc; 10672 } 10673 10674 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10675 { 10676 int rc, i, nr_ctxs; 10677 10678 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10679 for (i = 0; i < nr_ctxs; i++) { 10680 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10681 if (rc) { 10682 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10683 vnic->vnic_id, i, rc); 10684 break; 10685 } 10686 bp->rsscos_nr_ctxs++; 10687 } 10688 if (i < nr_ctxs) 10689 return -ENOMEM; 10690 10691 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10692 if (rc) 10693 return rc; 10694 10695 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10696 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10697 if (rc) { 10698 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10699 vnic->vnic_id, rc); 10700 } 10701 } 10702 return rc; 10703 } 10704 10705 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10706 { 10707 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10708 return __bnxt_setup_vnic_p5(bp, vnic); 10709 else 10710 return __bnxt_setup_vnic(bp, vnic); 10711 } 10712 10713 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10714 struct bnxt_vnic_info *vnic, 10715 u16 start_rx_ring_idx, int rx_rings) 10716 { 10717 int rc; 10718 10719 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10720 if (rc) { 10721 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10722 vnic->vnic_id, rc); 10723 return rc; 10724 } 10725 return bnxt_setup_vnic(bp, vnic); 10726 } 10727 10728 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10729 { 10730 struct bnxt_vnic_info *vnic; 10731 int i, rc = 0; 10732 10733 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10734 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10735 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10736 } 10737 10738 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10739 return 0; 10740 10741 for (i = 0; i < bp->rx_nr_rings; i++) { 10742 u16 vnic_id = i + 1; 10743 u16 ring_id = i; 10744 10745 if (vnic_id >= bp->nr_vnics) 10746 break; 10747 10748 vnic = &bp->vnic_info[vnic_id]; 10749 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10750 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10751 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10752 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10753 break; 10754 } 10755 return rc; 10756 } 10757 10758 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10759 bool all) 10760 { 10761 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10762 struct bnxt_filter_base *usr_fltr, *tmp; 10763 struct bnxt_ntuple_filter *ntp_fltr; 10764 int i; 10765 10766 if (netif_running(bp->dev)) { 10767 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10768 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10769 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10770 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10771 } 10772 } 10773 if (!all) 10774 return; 10775 10776 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10777 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10778 usr_fltr->fw_vnic_id == rss_ctx->index) { 10779 ntp_fltr = container_of(usr_fltr, 10780 struct bnxt_ntuple_filter, 10781 base); 10782 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10783 bnxt_del_ntp_filter(bp, ntp_fltr); 10784 bnxt_del_one_usr_fltr(bp, usr_fltr); 10785 } 10786 } 10787 10788 if (vnic->rss_table) 10789 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10790 vnic->rss_table, 10791 vnic->rss_table_dma_addr); 10792 bp->num_rss_ctx--; 10793 } 10794 10795 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10796 int rxr_id) 10797 { 10798 u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 10799 int i, vnic_rx; 10800 10801 /* Ntuple VNIC always has all the rx rings. Any change of ring id 10802 * must be updated because a future filter may use it. 10803 */ 10804 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 10805 return true; 10806 10807 for (i = 0; i < tbl_size; i++) { 10808 if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 10809 vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 10810 else 10811 vnic_rx = bp->rss_indir_tbl[i]; 10812 10813 if (rxr_id == vnic_rx) 10814 return true; 10815 } 10816 10817 return false; 10818 } 10819 10820 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10821 u16 mru, int rxr_id) 10822 { 10823 int rc; 10824 10825 if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id)) 10826 return 0; 10827 10828 if (mru) { 10829 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10830 if (rc) { 10831 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10832 vnic->vnic_id, rc); 10833 return rc; 10834 } 10835 } 10836 vnic->mru = mru; 10837 bnxt_hwrm_vnic_update(bp, vnic, 10838 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 10839 10840 return 0; 10841 } 10842 10843 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id) 10844 { 10845 struct ethtool_rxfh_context *ctx; 10846 unsigned long context; 10847 int rc; 10848 10849 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10850 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10851 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10852 10853 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id); 10854 if (rc) 10855 return rc; 10856 } 10857 10858 return 0; 10859 } 10860 10861 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10862 { 10863 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10864 struct ethtool_rxfh_context *ctx; 10865 unsigned long context; 10866 10867 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10868 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10869 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10870 10871 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10872 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10873 __bnxt_setup_vnic_p5(bp, vnic)) { 10874 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10875 rss_ctx->index); 10876 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10877 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10878 } 10879 } 10880 } 10881 10882 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10883 { 10884 struct ethtool_rxfh_context *ctx; 10885 unsigned long context; 10886 10887 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10888 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10889 10890 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10891 } 10892 } 10893 10894 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10895 static bool bnxt_promisc_ok(struct bnxt *bp) 10896 { 10897 #ifdef CONFIG_BNXT_SRIOV 10898 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10899 return false; 10900 #endif 10901 return true; 10902 } 10903 10904 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10905 { 10906 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10907 unsigned int rc = 0; 10908 10909 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10910 if (rc) { 10911 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10912 rc); 10913 return rc; 10914 } 10915 10916 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10917 if (rc) { 10918 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10919 rc); 10920 return rc; 10921 } 10922 return rc; 10923 } 10924 10925 static int bnxt_cfg_rx_mode(struct bnxt *); 10926 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10927 10928 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10929 { 10930 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10931 int rc = 0; 10932 unsigned int rx_nr_rings = bp->rx_nr_rings; 10933 10934 if (irq_re_init) { 10935 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10936 if (rc) { 10937 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10938 rc); 10939 goto err_out; 10940 } 10941 } 10942 10943 rc = bnxt_hwrm_ring_alloc(bp); 10944 if (rc) { 10945 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10946 goto err_out; 10947 } 10948 10949 rc = bnxt_hwrm_ring_grp_alloc(bp); 10950 if (rc) { 10951 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10952 goto err_out; 10953 } 10954 10955 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10956 rx_nr_rings--; 10957 10958 /* default vnic 0 */ 10959 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10960 if (rc) { 10961 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10962 goto err_out; 10963 } 10964 10965 if (BNXT_VF(bp)) 10966 bnxt_hwrm_func_qcfg(bp); 10967 10968 rc = bnxt_setup_vnic(bp, vnic); 10969 if (rc) 10970 goto err_out; 10971 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 10972 bnxt_hwrm_update_rss_hash_cfg(bp); 10973 10974 if (bp->flags & BNXT_FLAG_RFS) { 10975 rc = bnxt_alloc_rfs_vnics(bp); 10976 if (rc) 10977 goto err_out; 10978 } 10979 10980 if (bp->flags & BNXT_FLAG_TPA) { 10981 rc = bnxt_set_tpa(bp, true); 10982 if (rc) 10983 goto err_out; 10984 } 10985 10986 if (BNXT_VF(bp)) 10987 bnxt_update_vf_mac(bp); 10988 10989 /* Filter for default vnic 0 */ 10990 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 10991 if (rc) { 10992 if (BNXT_VF(bp) && rc == -ENODEV) 10993 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 10994 else 10995 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10996 goto err_out; 10997 } 10998 vnic->uc_filter_count = 1; 10999 11000 vnic->rx_mask = 0; 11001 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 11002 goto skip_rx_mask; 11003 11004 if (bp->dev->flags & IFF_BROADCAST) 11005 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 11006 11007 if (bp->dev->flags & IFF_PROMISC) 11008 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 11009 11010 if (bp->dev->flags & IFF_ALLMULTI) { 11011 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 11012 vnic->mc_list_count = 0; 11013 } else if (bp->dev->flags & IFF_MULTICAST) { 11014 u32 mask = 0; 11015 11016 bnxt_mc_list_updated(bp, &mask); 11017 vnic->rx_mask |= mask; 11018 } 11019 11020 rc = bnxt_cfg_rx_mode(bp); 11021 if (rc) 11022 goto err_out; 11023 11024 skip_rx_mask: 11025 rc = bnxt_hwrm_set_coal(bp); 11026 if (rc) 11027 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 11028 rc); 11029 11030 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11031 rc = bnxt_setup_nitroa0_vnic(bp); 11032 if (rc) 11033 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 11034 rc); 11035 } 11036 11037 if (BNXT_VF(bp)) { 11038 bnxt_hwrm_func_qcfg(bp); 11039 netdev_update_features(bp->dev); 11040 } 11041 11042 return 0; 11043 11044 err_out: 11045 bnxt_hwrm_resource_free(bp, 0, true); 11046 11047 return rc; 11048 } 11049 11050 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 11051 { 11052 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 11053 return 0; 11054 } 11055 11056 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 11057 { 11058 bnxt_init_cp_rings(bp); 11059 bnxt_init_rx_rings(bp); 11060 bnxt_init_tx_rings(bp); 11061 bnxt_init_ring_grps(bp, irq_re_init); 11062 bnxt_init_vnics(bp); 11063 11064 return bnxt_init_chip(bp, irq_re_init); 11065 } 11066 11067 static int bnxt_set_real_num_queues(struct bnxt *bp) 11068 { 11069 int rc; 11070 struct net_device *dev = bp->dev; 11071 11072 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 11073 bp->tx_nr_rings_xdp); 11074 if (rc) 11075 return rc; 11076 11077 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 11078 if (rc) 11079 return rc; 11080 11081 #ifdef CONFIG_RFS_ACCEL 11082 if (bp->flags & BNXT_FLAG_RFS) 11083 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 11084 #endif 11085 11086 return rc; 11087 } 11088 11089 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11090 bool shared) 11091 { 11092 int _rx = *rx, _tx = *tx; 11093 11094 if (shared) { 11095 *rx = min_t(int, _rx, max); 11096 *tx = min_t(int, _tx, max); 11097 } else { 11098 if (max < 2) 11099 return -ENOMEM; 11100 11101 while (_rx + _tx > max) { 11102 if (_rx > _tx && _rx > 1) 11103 _rx--; 11104 else if (_tx > 1) 11105 _tx--; 11106 } 11107 *rx = _rx; 11108 *tx = _tx; 11109 } 11110 return 0; 11111 } 11112 11113 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 11114 { 11115 return (tx - tx_xdp) / tx_sets + tx_xdp; 11116 } 11117 11118 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 11119 { 11120 int tcs = bp->num_tc; 11121 11122 if (!tcs) 11123 tcs = 1; 11124 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 11125 } 11126 11127 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 11128 { 11129 int tcs = bp->num_tc; 11130 11131 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 11132 bp->tx_nr_rings_xdp; 11133 } 11134 11135 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11136 bool sh) 11137 { 11138 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 11139 11140 if (tx_cp != *tx) { 11141 int tx_saved = tx_cp, rc; 11142 11143 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 11144 if (rc) 11145 return rc; 11146 if (tx_cp != tx_saved) 11147 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 11148 return 0; 11149 } 11150 return __bnxt_trim_rings(bp, rx, tx, max, sh); 11151 } 11152 11153 static void bnxt_setup_msix(struct bnxt *bp) 11154 { 11155 const int len = sizeof(bp->irq_tbl[0].name); 11156 struct net_device *dev = bp->dev; 11157 int tcs, i; 11158 11159 tcs = bp->num_tc; 11160 if (tcs) { 11161 int i, off, count; 11162 11163 for (i = 0; i < tcs; i++) { 11164 count = bp->tx_nr_rings_per_tc; 11165 off = BNXT_TC_TO_RING_BASE(bp, i); 11166 netdev_set_tc_queue(dev, i, count, off); 11167 } 11168 } 11169 11170 for (i = 0; i < bp->cp_nr_rings; i++) { 11171 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11172 char *attr; 11173 11174 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 11175 attr = "TxRx"; 11176 else if (i < bp->rx_nr_rings) 11177 attr = "rx"; 11178 else 11179 attr = "tx"; 11180 11181 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 11182 attr, i); 11183 bp->irq_tbl[map_idx].handler = bnxt_msix; 11184 } 11185 } 11186 11187 static int bnxt_init_int_mode(struct bnxt *bp); 11188 11189 static int bnxt_change_msix(struct bnxt *bp, int total) 11190 { 11191 struct msi_map map; 11192 int i; 11193 11194 /* add MSIX to the end if needed */ 11195 for (i = bp->total_irqs; i < total; i++) { 11196 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 11197 if (map.index < 0) 11198 return bp->total_irqs; 11199 bp->irq_tbl[i].vector = map.virq; 11200 bp->total_irqs++; 11201 } 11202 11203 /* trim MSIX from the end if needed */ 11204 for (i = bp->total_irqs; i > total; i--) { 11205 map.index = i - 1; 11206 map.virq = bp->irq_tbl[i - 1].vector; 11207 pci_msix_free_irq(bp->pdev, map); 11208 bp->total_irqs--; 11209 } 11210 return bp->total_irqs; 11211 } 11212 11213 static int bnxt_setup_int_mode(struct bnxt *bp) 11214 { 11215 int rc; 11216 11217 if (!bp->irq_tbl) { 11218 rc = bnxt_init_int_mode(bp); 11219 if (rc || !bp->irq_tbl) 11220 return rc ?: -ENODEV; 11221 } 11222 11223 bnxt_setup_msix(bp); 11224 11225 rc = bnxt_set_real_num_queues(bp); 11226 return rc; 11227 } 11228 11229 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11230 { 11231 return bp->hw_resc.max_rsscos_ctxs; 11232 } 11233 11234 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11235 { 11236 return bp->hw_resc.max_vnics; 11237 } 11238 11239 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11240 { 11241 return bp->hw_resc.max_stat_ctxs; 11242 } 11243 11244 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11245 { 11246 return bp->hw_resc.max_cp_rings; 11247 } 11248 11249 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11250 { 11251 unsigned int cp = bp->hw_resc.max_cp_rings; 11252 11253 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11254 cp -= bnxt_get_ulp_msix_num(bp); 11255 11256 return cp; 11257 } 11258 11259 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11260 { 11261 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11262 11263 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11264 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11265 11266 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11267 } 11268 11269 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11270 { 11271 bp->hw_resc.max_irqs = max_irqs; 11272 } 11273 11274 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11275 { 11276 unsigned int cp; 11277 11278 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11279 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11280 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11281 else 11282 return cp - bp->cp_nr_rings; 11283 } 11284 11285 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11286 { 11287 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11288 } 11289 11290 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11291 { 11292 int max_irq = bnxt_get_max_func_irqs(bp); 11293 int total_req = bp->cp_nr_rings + num; 11294 11295 if (max_irq < total_req) { 11296 num = max_irq - bp->cp_nr_rings; 11297 if (num <= 0) 11298 return 0; 11299 } 11300 return num; 11301 } 11302 11303 static int bnxt_get_num_msix(struct bnxt *bp) 11304 { 11305 if (!BNXT_NEW_RM(bp)) 11306 return bnxt_get_max_func_irqs(bp); 11307 11308 return bnxt_nq_rings_in_use(bp); 11309 } 11310 11311 static int bnxt_init_int_mode(struct bnxt *bp) 11312 { 11313 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 11314 11315 total_vecs = bnxt_get_num_msix(bp); 11316 max = bnxt_get_max_func_irqs(bp); 11317 if (total_vecs > max) 11318 total_vecs = max; 11319 11320 if (!total_vecs) 11321 return 0; 11322 11323 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11324 min = 2; 11325 11326 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11327 PCI_IRQ_MSIX); 11328 ulp_msix = bnxt_get_ulp_msix_num(bp); 11329 if (total_vecs < 0 || total_vecs < ulp_msix) { 11330 rc = -ENODEV; 11331 goto msix_setup_exit; 11332 } 11333 11334 tbl_size = total_vecs; 11335 if (pci_msix_can_alloc_dyn(bp->pdev)) 11336 tbl_size = max; 11337 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 11338 if (bp->irq_tbl) { 11339 for (i = 0; i < total_vecs; i++) 11340 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11341 11342 bp->total_irqs = total_vecs; 11343 /* Trim rings based upon num of vectors allocated */ 11344 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11345 total_vecs - ulp_msix, min == 1); 11346 if (rc) 11347 goto msix_setup_exit; 11348 11349 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11350 bp->cp_nr_rings = (min == 1) ? 11351 max_t(int, tx_cp, bp->rx_nr_rings) : 11352 tx_cp + bp->rx_nr_rings; 11353 11354 } else { 11355 rc = -ENOMEM; 11356 goto msix_setup_exit; 11357 } 11358 return 0; 11359 11360 msix_setup_exit: 11361 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11362 kfree(bp->irq_tbl); 11363 bp->irq_tbl = NULL; 11364 pci_free_irq_vectors(bp->pdev); 11365 return rc; 11366 } 11367 11368 static void bnxt_clear_int_mode(struct bnxt *bp) 11369 { 11370 pci_free_irq_vectors(bp->pdev); 11371 11372 kfree(bp->irq_tbl); 11373 bp->irq_tbl = NULL; 11374 } 11375 11376 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11377 { 11378 bool irq_cleared = false; 11379 bool irq_change = false; 11380 int tcs = bp->num_tc; 11381 int irqs_required; 11382 int rc; 11383 11384 if (!bnxt_need_reserve_rings(bp)) 11385 return 0; 11386 11387 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 11388 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11389 11390 if (ulp_msix > bp->ulp_num_msix_want) 11391 ulp_msix = bp->ulp_num_msix_want; 11392 irqs_required = ulp_msix + bp->cp_nr_rings; 11393 } else { 11394 irqs_required = bnxt_get_num_msix(bp); 11395 } 11396 11397 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11398 irq_change = true; 11399 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11400 bnxt_ulp_irq_stop(bp); 11401 bnxt_clear_int_mode(bp); 11402 irq_cleared = true; 11403 } 11404 } 11405 rc = __bnxt_reserve_rings(bp); 11406 if (irq_cleared) { 11407 if (!rc) 11408 rc = bnxt_init_int_mode(bp); 11409 bnxt_ulp_irq_restart(bp, rc); 11410 } else if (irq_change && !rc) { 11411 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11412 rc = -ENOSPC; 11413 } 11414 if (rc) { 11415 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11416 return rc; 11417 } 11418 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11419 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11420 netdev_err(bp->dev, "tx ring reservation failure\n"); 11421 netdev_reset_tc(bp->dev); 11422 bp->num_tc = 0; 11423 if (bp->tx_nr_rings_xdp) 11424 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11425 else 11426 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11427 return -ENOMEM; 11428 } 11429 return 0; 11430 } 11431 11432 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx) 11433 { 11434 struct bnxt_tx_ring_info *txr; 11435 struct netdev_queue *txq; 11436 struct bnxt_napi *bnapi; 11437 int i; 11438 11439 bnapi = bp->bnapi[idx]; 11440 bnxt_for_each_napi_tx(i, bnapi, txr) { 11441 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11442 synchronize_net(); 11443 11444 if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) { 11445 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11446 if (txq) { 11447 __netif_tx_lock_bh(txq); 11448 netif_tx_stop_queue(txq); 11449 __netif_tx_unlock_bh(txq); 11450 } 11451 } 11452 11453 if (!bp->tph_mode) 11454 continue; 11455 11456 bnxt_hwrm_tx_ring_free(bp, txr, true); 11457 bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr); 11458 bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index); 11459 bnxt_clear_one_cp_ring(bp, txr->tx_cpr); 11460 } 11461 } 11462 11463 static int bnxt_tx_queue_start(struct bnxt *bp, int idx) 11464 { 11465 struct bnxt_tx_ring_info *txr; 11466 struct netdev_queue *txq; 11467 struct bnxt_napi *bnapi; 11468 int rc, i; 11469 11470 bnapi = bp->bnapi[idx]; 11471 /* All rings have been reserved and previously allocated. 11472 * Reallocating with the same parameters should never fail. 11473 */ 11474 bnxt_for_each_napi_tx(i, bnapi, txr) { 11475 if (!bp->tph_mode) 11476 goto start_tx; 11477 11478 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 11479 if (rc) 11480 return rc; 11481 11482 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false); 11483 if (rc) 11484 return rc; 11485 11486 txr->tx_prod = 0; 11487 txr->tx_cons = 0; 11488 txr->tx_hw_cons = 0; 11489 start_tx: 11490 WRITE_ONCE(txr->dev_state, 0); 11491 synchronize_net(); 11492 11493 if (bnapi->flags & BNXT_NAPI_FLAG_XDP) 11494 continue; 11495 11496 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11497 if (txq) 11498 netif_tx_start_queue(txq); 11499 } 11500 11501 return 0; 11502 } 11503 11504 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify, 11505 const cpumask_t *mask) 11506 { 11507 struct bnxt_irq *irq; 11508 u16 tag; 11509 int err; 11510 11511 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11512 11513 if (!irq->bp->tph_mode) 11514 return; 11515 11516 cpumask_copy(irq->cpu_mask, mask); 11517 11518 if (irq->ring_nr >= irq->bp->rx_nr_rings) 11519 return; 11520 11521 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11522 cpumask_first(irq->cpu_mask), &tag)) 11523 return; 11524 11525 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag)) 11526 return; 11527 11528 netdev_lock(irq->bp->dev); 11529 if (netif_running(irq->bp->dev)) { 11530 err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr); 11531 if (err) 11532 netdev_err(irq->bp->dev, 11533 "RX queue restart failed: err=%d\n", err); 11534 } 11535 netdev_unlock(irq->bp->dev); 11536 } 11537 11538 static void bnxt_irq_affinity_release(struct kref *ref) 11539 { 11540 struct irq_affinity_notify *notify = 11541 container_of(ref, struct irq_affinity_notify, kref); 11542 struct bnxt_irq *irq; 11543 11544 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11545 11546 if (!irq->bp->tph_mode) 11547 return; 11548 11549 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) { 11550 netdev_err(irq->bp->dev, 11551 "Setting ST=0 for MSIX entry %d failed\n", 11552 irq->msix_nr); 11553 return; 11554 } 11555 } 11556 11557 static void bnxt_release_irq_notifier(struct bnxt_irq *irq) 11558 { 11559 irq_set_affinity_notifier(irq->vector, NULL); 11560 } 11561 11562 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq) 11563 { 11564 struct irq_affinity_notify *notify; 11565 11566 irq->bp = bp; 11567 11568 /* Nothing to do if TPH is not enabled */ 11569 if (!bp->tph_mode) 11570 return; 11571 11572 /* Register IRQ affinity notifier */ 11573 notify = &irq->affinity_notify; 11574 notify->irq = irq->vector; 11575 notify->notify = bnxt_irq_affinity_notify; 11576 notify->release = bnxt_irq_affinity_release; 11577 11578 irq_set_affinity_notifier(irq->vector, notify); 11579 } 11580 11581 static void bnxt_free_irq(struct bnxt *bp) 11582 { 11583 struct bnxt_irq *irq; 11584 int i; 11585 11586 #ifdef CONFIG_RFS_ACCEL 11587 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11588 bp->dev->rx_cpu_rmap = NULL; 11589 #endif 11590 if (!bp->irq_tbl || !bp->bnapi) 11591 return; 11592 11593 for (i = 0; i < bp->cp_nr_rings; i++) { 11594 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11595 11596 irq = &bp->irq_tbl[map_idx]; 11597 if (irq->requested) { 11598 if (irq->have_cpumask) { 11599 irq_update_affinity_hint(irq->vector, NULL); 11600 free_cpumask_var(irq->cpu_mask); 11601 irq->have_cpumask = 0; 11602 } 11603 11604 bnxt_release_irq_notifier(irq); 11605 11606 free_irq(irq->vector, bp->bnapi[i]); 11607 } 11608 11609 irq->requested = 0; 11610 } 11611 11612 /* Disable TPH support */ 11613 pcie_disable_tph(bp->pdev); 11614 bp->tph_mode = 0; 11615 } 11616 11617 static int bnxt_request_irq(struct bnxt *bp) 11618 { 11619 int i, j, rc = 0; 11620 unsigned long flags = 0; 11621 #ifdef CONFIG_RFS_ACCEL 11622 struct cpu_rmap *rmap; 11623 #endif 11624 11625 rc = bnxt_setup_int_mode(bp); 11626 if (rc) { 11627 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11628 rc); 11629 return rc; 11630 } 11631 #ifdef CONFIG_RFS_ACCEL 11632 rmap = bp->dev->rx_cpu_rmap; 11633 #endif 11634 11635 /* Enable TPH support as part of IRQ request */ 11636 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11637 if (!rc) 11638 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11639 11640 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11641 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11642 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11643 11644 #ifdef CONFIG_RFS_ACCEL 11645 if (rmap && bp->bnapi[i]->rx_ring) { 11646 rc = irq_cpu_rmap_add(rmap, irq->vector); 11647 if (rc) 11648 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11649 j); 11650 j++; 11651 } 11652 #endif 11653 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11654 bp->bnapi[i]); 11655 if (rc) 11656 break; 11657 11658 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 11659 irq->requested = 1; 11660 11661 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11662 int numa_node = dev_to_node(&bp->pdev->dev); 11663 u16 tag; 11664 11665 irq->have_cpumask = 1; 11666 irq->msix_nr = map_idx; 11667 irq->ring_nr = i; 11668 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11669 irq->cpu_mask); 11670 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11671 if (rc) { 11672 netdev_warn(bp->dev, 11673 "Update affinity hint failed, IRQ = %d\n", 11674 irq->vector); 11675 break; 11676 } 11677 11678 bnxt_register_irq_notifier(bp, irq); 11679 11680 /* Init ST table entry */ 11681 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11682 cpumask_first(irq->cpu_mask), 11683 &tag)) 11684 continue; 11685 11686 pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag); 11687 } 11688 } 11689 return rc; 11690 } 11691 11692 static void bnxt_del_napi(struct bnxt *bp) 11693 { 11694 int i; 11695 11696 if (!bp->bnapi) 11697 return; 11698 11699 for (i = 0; i < bp->rx_nr_rings; i++) 11700 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11701 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11702 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11703 11704 for (i = 0; i < bp->cp_nr_rings; i++) { 11705 struct bnxt_napi *bnapi = bp->bnapi[i]; 11706 11707 __netif_napi_del_locked(&bnapi->napi); 11708 } 11709 /* We called __netif_napi_del_locked(), we need 11710 * to respect an RCU grace period before freeing napi structures. 11711 */ 11712 synchronize_net(); 11713 } 11714 11715 static void bnxt_init_napi(struct bnxt *bp) 11716 { 11717 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11718 unsigned int cp_nr_rings = bp->cp_nr_rings; 11719 struct bnxt_napi *bnapi; 11720 int i; 11721 11722 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11723 poll_fn = bnxt_poll_p5; 11724 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11725 cp_nr_rings--; 11726 11727 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11728 11729 for (i = 0; i < cp_nr_rings; i++) { 11730 bnapi = bp->bnapi[i]; 11731 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 11732 bnapi->index); 11733 } 11734 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11735 bnapi = bp->bnapi[cp_nr_rings]; 11736 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11737 } 11738 } 11739 11740 static void bnxt_disable_napi(struct bnxt *bp) 11741 { 11742 int i; 11743 11744 if (!bp->bnapi || 11745 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11746 return; 11747 11748 for (i = 0; i < bp->cp_nr_rings; i++) { 11749 struct bnxt_napi *bnapi = bp->bnapi[i]; 11750 struct bnxt_cp_ring_info *cpr; 11751 11752 cpr = &bnapi->cp_ring; 11753 if (bnapi->tx_fault) 11754 cpr->sw_stats->tx.tx_resets++; 11755 if (bnapi->in_reset) 11756 cpr->sw_stats->rx.rx_resets++; 11757 napi_disable_locked(&bnapi->napi); 11758 } 11759 } 11760 11761 static void bnxt_enable_napi(struct bnxt *bp) 11762 { 11763 int i; 11764 11765 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11766 for (i = 0; i < bp->cp_nr_rings; i++) { 11767 struct bnxt_napi *bnapi = bp->bnapi[i]; 11768 struct bnxt_cp_ring_info *cpr; 11769 11770 bnapi->tx_fault = 0; 11771 11772 cpr = &bnapi->cp_ring; 11773 bnapi->in_reset = false; 11774 11775 if (bnapi->rx_ring) { 11776 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11777 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11778 } 11779 napi_enable_locked(&bnapi->napi); 11780 } 11781 } 11782 11783 void bnxt_tx_disable(struct bnxt *bp) 11784 { 11785 int i; 11786 struct bnxt_tx_ring_info *txr; 11787 11788 if (bp->tx_ring) { 11789 for (i = 0; i < bp->tx_nr_rings; i++) { 11790 txr = &bp->tx_ring[i]; 11791 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11792 } 11793 } 11794 /* Make sure napi polls see @dev_state change */ 11795 synchronize_net(); 11796 /* Drop carrier first to prevent TX timeout */ 11797 netif_carrier_off(bp->dev); 11798 /* Stop all TX queues */ 11799 netif_tx_disable(bp->dev); 11800 } 11801 11802 void bnxt_tx_enable(struct bnxt *bp) 11803 { 11804 int i; 11805 struct bnxt_tx_ring_info *txr; 11806 11807 for (i = 0; i < bp->tx_nr_rings; i++) { 11808 txr = &bp->tx_ring[i]; 11809 WRITE_ONCE(txr->dev_state, 0); 11810 } 11811 /* Make sure napi polls see @dev_state change */ 11812 synchronize_net(); 11813 netif_tx_wake_all_queues(bp->dev); 11814 if (BNXT_LINK_IS_UP(bp)) 11815 netif_carrier_on(bp->dev); 11816 } 11817 11818 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11819 { 11820 u8 active_fec = link_info->active_fec_sig_mode & 11821 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11822 11823 switch (active_fec) { 11824 default: 11825 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11826 return "None"; 11827 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11828 return "Clause 74 BaseR"; 11829 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11830 return "Clause 91 RS(528,514)"; 11831 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11832 return "Clause 91 RS544_1XN"; 11833 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11834 return "Clause 91 RS(544,514)"; 11835 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11836 return "Clause 91 RS272_1XN"; 11837 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11838 return "Clause 91 RS(272,257)"; 11839 } 11840 } 11841 11842 void bnxt_report_link(struct bnxt *bp) 11843 { 11844 if (BNXT_LINK_IS_UP(bp)) { 11845 const char *signal = ""; 11846 const char *flow_ctrl; 11847 const char *duplex; 11848 u32 speed; 11849 u16 fec; 11850 11851 netif_carrier_on(bp->dev); 11852 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11853 if (speed == SPEED_UNKNOWN) { 11854 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11855 return; 11856 } 11857 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11858 duplex = "full"; 11859 else 11860 duplex = "half"; 11861 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11862 flow_ctrl = "ON - receive & transmit"; 11863 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11864 flow_ctrl = "ON - transmit"; 11865 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11866 flow_ctrl = "ON - receive"; 11867 else 11868 flow_ctrl = "none"; 11869 if (bp->link_info.phy_qcfg_resp.option_flags & 11870 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11871 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11872 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11873 switch (sig_mode) { 11874 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11875 signal = "(NRZ) "; 11876 break; 11877 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11878 signal = "(PAM4 56Gbps) "; 11879 break; 11880 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11881 signal = "(PAM4 112Gbps) "; 11882 break; 11883 default: 11884 break; 11885 } 11886 } 11887 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11888 speed, signal, duplex, flow_ctrl); 11889 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11890 netdev_info(bp->dev, "EEE is %s\n", 11891 bp->eee.eee_active ? "active" : 11892 "not active"); 11893 fec = bp->link_info.fec_cfg; 11894 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11895 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11896 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11897 bnxt_report_fec(&bp->link_info)); 11898 } else { 11899 netif_carrier_off(bp->dev); 11900 netdev_err(bp->dev, "NIC Link is Down\n"); 11901 } 11902 } 11903 11904 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11905 { 11906 if (!resp->supported_speeds_auto_mode && 11907 !resp->supported_speeds_force_mode && 11908 !resp->supported_pam4_speeds_auto_mode && 11909 !resp->supported_pam4_speeds_force_mode && 11910 !resp->supported_speeds2_auto_mode && 11911 !resp->supported_speeds2_force_mode) 11912 return true; 11913 return false; 11914 } 11915 11916 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11917 { 11918 struct bnxt_link_info *link_info = &bp->link_info; 11919 struct hwrm_port_phy_qcaps_output *resp; 11920 struct hwrm_port_phy_qcaps_input *req; 11921 int rc = 0; 11922 11923 if (bp->hwrm_spec_code < 0x10201) 11924 return 0; 11925 11926 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11927 if (rc) 11928 return rc; 11929 11930 resp = hwrm_req_hold(bp, req); 11931 rc = hwrm_req_send(bp, req); 11932 if (rc) 11933 goto hwrm_phy_qcaps_exit; 11934 11935 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11936 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11937 struct ethtool_keee *eee = &bp->eee; 11938 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11939 11940 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11941 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11942 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11943 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11944 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11945 } 11946 11947 if (bp->hwrm_spec_code >= 0x10a01) { 11948 if (bnxt_phy_qcaps_no_speed(resp)) { 11949 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11950 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11951 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11952 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11953 netdev_info(bp->dev, "Ethernet link enabled\n"); 11954 /* Phy re-enabled, reprobe the speeds */ 11955 link_info->support_auto_speeds = 0; 11956 link_info->support_pam4_auto_speeds = 0; 11957 link_info->support_auto_speeds2 = 0; 11958 } 11959 } 11960 if (resp->supported_speeds_auto_mode) 11961 link_info->support_auto_speeds = 11962 le16_to_cpu(resp->supported_speeds_auto_mode); 11963 if (resp->supported_pam4_speeds_auto_mode) 11964 link_info->support_pam4_auto_speeds = 11965 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11966 if (resp->supported_speeds2_auto_mode) 11967 link_info->support_auto_speeds2 = 11968 le16_to_cpu(resp->supported_speeds2_auto_mode); 11969 11970 bp->port_count = resp->port_cnt; 11971 11972 hwrm_phy_qcaps_exit: 11973 hwrm_req_drop(bp, req); 11974 return rc; 11975 } 11976 11977 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 11978 { 11979 struct hwrm_port_mac_qcaps_output *resp; 11980 struct hwrm_port_mac_qcaps_input *req; 11981 int rc; 11982 11983 if (bp->hwrm_spec_code < 0x10a03) 11984 return; 11985 11986 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 11987 if (rc) 11988 return; 11989 11990 resp = hwrm_req_hold(bp, req); 11991 rc = hwrm_req_send_silent(bp, req); 11992 if (!rc) 11993 bp->mac_flags = resp->flags; 11994 hwrm_req_drop(bp, req); 11995 } 11996 11997 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11998 { 11999 u16 diff = advertising ^ supported; 12000 12001 return ((supported | diff) != supported); 12002 } 12003 12004 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 12005 { 12006 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 12007 12008 /* Check if any advertised speeds are no longer supported. The caller 12009 * holds the link_lock mutex, so we can modify link_info settings. 12010 */ 12011 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12012 if (bnxt_support_dropped(link_info->advertising, 12013 link_info->support_auto_speeds2)) { 12014 link_info->advertising = link_info->support_auto_speeds2; 12015 return true; 12016 } 12017 return false; 12018 } 12019 if (bnxt_support_dropped(link_info->advertising, 12020 link_info->support_auto_speeds)) { 12021 link_info->advertising = link_info->support_auto_speeds; 12022 return true; 12023 } 12024 if (bnxt_support_dropped(link_info->advertising_pam4, 12025 link_info->support_pam4_auto_speeds)) { 12026 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12027 return true; 12028 } 12029 return false; 12030 } 12031 12032 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12033 { 12034 struct bnxt_link_info *link_info = &bp->link_info; 12035 struct hwrm_port_phy_qcfg_output *resp; 12036 struct hwrm_port_phy_qcfg_input *req; 12037 u8 link_state = link_info->link_state; 12038 bool support_changed; 12039 int rc; 12040 12041 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12042 if (rc) 12043 return rc; 12044 12045 resp = hwrm_req_hold(bp, req); 12046 rc = hwrm_req_send(bp, req); 12047 if (rc) { 12048 hwrm_req_drop(bp, req); 12049 if (BNXT_VF(bp) && rc == -ENODEV) { 12050 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12051 rc = 0; 12052 } 12053 return rc; 12054 } 12055 12056 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12057 link_info->phy_link_status = resp->link; 12058 link_info->duplex = resp->duplex_cfg; 12059 if (bp->hwrm_spec_code >= 0x10800) 12060 link_info->duplex = resp->duplex_state; 12061 link_info->pause = resp->pause; 12062 link_info->auto_mode = resp->auto_mode; 12063 link_info->auto_pause_setting = resp->auto_pause; 12064 link_info->lp_pause = resp->link_partner_adv_pause; 12065 link_info->force_pause_setting = resp->force_pause; 12066 link_info->duplex_setting = resp->duplex_cfg; 12067 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12068 link_info->link_speed = le16_to_cpu(resp->link_speed); 12069 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12070 link_info->active_lanes = resp->active_lanes; 12071 } else { 12072 link_info->link_speed = 0; 12073 link_info->active_lanes = 0; 12074 } 12075 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12076 link_info->force_pam4_link_speed = 12077 le16_to_cpu(resp->force_pam4_link_speed); 12078 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12079 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12080 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12081 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12082 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12083 link_info->auto_pam4_link_speeds = 12084 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12085 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12086 link_info->lp_auto_link_speeds = 12087 le16_to_cpu(resp->link_partner_adv_speeds); 12088 link_info->lp_auto_pam4_link_speeds = 12089 resp->link_partner_pam4_adv_speeds; 12090 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12091 link_info->phy_ver[0] = resp->phy_maj; 12092 link_info->phy_ver[1] = resp->phy_min; 12093 link_info->phy_ver[2] = resp->phy_bld; 12094 link_info->media_type = resp->media_type; 12095 link_info->phy_type = resp->phy_type; 12096 link_info->transceiver = resp->xcvr_pkg_type; 12097 link_info->phy_addr = resp->eee_config_phy_addr & 12098 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12099 link_info->module_status = resp->module_status; 12100 12101 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12102 struct ethtool_keee *eee = &bp->eee; 12103 u16 fw_speeds; 12104 12105 eee->eee_active = 0; 12106 if (resp->eee_config_phy_addr & 12107 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12108 eee->eee_active = 1; 12109 fw_speeds = le16_to_cpu( 12110 resp->link_partner_adv_eee_link_speed_mask); 12111 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12112 } 12113 12114 /* Pull initial EEE config */ 12115 if (!chng_link_state) { 12116 if (resp->eee_config_phy_addr & 12117 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12118 eee->eee_enabled = 1; 12119 12120 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12121 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12122 12123 if (resp->eee_config_phy_addr & 12124 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12125 __le32 tmr; 12126 12127 eee->tx_lpi_enabled = 1; 12128 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12129 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12130 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12131 } 12132 } 12133 } 12134 12135 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12136 if (bp->hwrm_spec_code >= 0x10504) { 12137 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12138 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12139 } 12140 /* TODO: need to add more logic to report VF link */ 12141 if (chng_link_state) { 12142 if (link_info->phy_link_status == BNXT_LINK_LINK) 12143 link_info->link_state = BNXT_LINK_STATE_UP; 12144 else 12145 link_info->link_state = BNXT_LINK_STATE_DOWN; 12146 if (link_state != link_info->link_state) 12147 bnxt_report_link(bp); 12148 } else { 12149 /* always link down if not require to update link state */ 12150 link_info->link_state = BNXT_LINK_STATE_DOWN; 12151 } 12152 hwrm_req_drop(bp, req); 12153 12154 if (!BNXT_PHY_CFG_ABLE(bp)) 12155 return 0; 12156 12157 support_changed = bnxt_support_speed_dropped(link_info); 12158 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12159 bnxt_hwrm_set_link_setting(bp, true, false); 12160 return 0; 12161 } 12162 12163 static void bnxt_get_port_module_status(struct bnxt *bp) 12164 { 12165 struct bnxt_link_info *link_info = &bp->link_info; 12166 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12167 u8 module_status; 12168 12169 if (bnxt_update_link(bp, true)) 12170 return; 12171 12172 module_status = link_info->module_status; 12173 switch (module_status) { 12174 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12175 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12176 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12177 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12178 bp->pf.port_id); 12179 if (bp->hwrm_spec_code >= 0x10201) { 12180 netdev_warn(bp->dev, "Module part number %s\n", 12181 resp->phy_vendor_partnumber); 12182 } 12183 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12184 netdev_warn(bp->dev, "TX is disabled\n"); 12185 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12186 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12187 } 12188 } 12189 12190 static void 12191 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12192 { 12193 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12194 if (bp->hwrm_spec_code >= 0x10201) 12195 req->auto_pause = 12196 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12197 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12198 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12199 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12200 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12201 req->enables |= 12202 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12203 } else { 12204 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12205 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12206 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12207 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12208 req->enables |= 12209 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12210 if (bp->hwrm_spec_code >= 0x10201) { 12211 req->auto_pause = req->force_pause; 12212 req->enables |= cpu_to_le32( 12213 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12214 } 12215 } 12216 } 12217 12218 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12219 { 12220 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12221 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12222 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12223 req->enables |= 12224 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12225 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12226 } else if (bp->link_info.advertising) { 12227 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12228 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12229 } 12230 if (bp->link_info.advertising_pam4) { 12231 req->enables |= 12232 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12233 req->auto_link_pam4_speed_mask = 12234 cpu_to_le16(bp->link_info.advertising_pam4); 12235 } 12236 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12237 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12238 } else { 12239 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12240 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12241 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12242 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12243 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12244 (u32)bp->link_info.req_link_speed); 12245 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12246 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12247 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12248 } else { 12249 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12250 } 12251 } 12252 12253 /* tell chimp that the setting takes effect immediately */ 12254 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12255 } 12256 12257 int bnxt_hwrm_set_pause(struct bnxt *bp) 12258 { 12259 struct hwrm_port_phy_cfg_input *req; 12260 int rc; 12261 12262 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12263 if (rc) 12264 return rc; 12265 12266 bnxt_hwrm_set_pause_common(bp, req); 12267 12268 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12269 bp->link_info.force_link_chng) 12270 bnxt_hwrm_set_link_common(bp, req); 12271 12272 rc = hwrm_req_send(bp, req); 12273 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12274 /* since changing of pause setting doesn't trigger any link 12275 * change event, the driver needs to update the current pause 12276 * result upon successfully return of the phy_cfg command 12277 */ 12278 bp->link_info.pause = 12279 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12280 bp->link_info.auto_pause_setting = 0; 12281 if (!bp->link_info.force_link_chng) 12282 bnxt_report_link(bp); 12283 } 12284 bp->link_info.force_link_chng = false; 12285 return rc; 12286 } 12287 12288 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12289 struct hwrm_port_phy_cfg_input *req) 12290 { 12291 struct ethtool_keee *eee = &bp->eee; 12292 12293 if (eee->eee_enabled) { 12294 u16 eee_speeds; 12295 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12296 12297 if (eee->tx_lpi_enabled) 12298 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12299 else 12300 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12301 12302 req->flags |= cpu_to_le32(flags); 12303 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12304 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12305 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12306 } else { 12307 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12308 } 12309 } 12310 12311 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12312 { 12313 struct hwrm_port_phy_cfg_input *req; 12314 int rc; 12315 12316 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12317 if (rc) 12318 return rc; 12319 12320 if (set_pause) 12321 bnxt_hwrm_set_pause_common(bp, req); 12322 12323 bnxt_hwrm_set_link_common(bp, req); 12324 12325 if (set_eee) 12326 bnxt_hwrm_set_eee(bp, req); 12327 return hwrm_req_send(bp, req); 12328 } 12329 12330 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12331 { 12332 struct hwrm_port_phy_cfg_input *req; 12333 int rc; 12334 12335 if (!BNXT_SINGLE_PF(bp)) 12336 return 0; 12337 12338 if (pci_num_vf(bp->pdev) && 12339 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12340 return 0; 12341 12342 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12343 if (rc) 12344 return rc; 12345 12346 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12347 rc = hwrm_req_send(bp, req); 12348 if (!rc) { 12349 mutex_lock(&bp->link_lock); 12350 /* Device is not obliged link down in certain scenarios, even 12351 * when forced. Setting the state unknown is consistent with 12352 * driver startup and will force link state to be reported 12353 * during subsequent open based on PORT_PHY_QCFG. 12354 */ 12355 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12356 mutex_unlock(&bp->link_lock); 12357 } 12358 return rc; 12359 } 12360 12361 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12362 { 12363 #ifdef CONFIG_TEE_BNXT_FW 12364 int rc = tee_bnxt_fw_load(); 12365 12366 if (rc) 12367 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12368 12369 return rc; 12370 #else 12371 netdev_err(bp->dev, "OP-TEE not supported\n"); 12372 return -ENODEV; 12373 #endif 12374 } 12375 12376 static int bnxt_try_recover_fw(struct bnxt *bp) 12377 { 12378 if (bp->fw_health && bp->fw_health->status_reliable) { 12379 int retry = 0, rc; 12380 u32 sts; 12381 12382 do { 12383 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12384 rc = bnxt_hwrm_poll(bp); 12385 if (!BNXT_FW_IS_BOOTING(sts) && 12386 !BNXT_FW_IS_RECOVERING(sts)) 12387 break; 12388 retry++; 12389 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12390 12391 if (!BNXT_FW_IS_HEALTHY(sts)) { 12392 netdev_err(bp->dev, 12393 "Firmware not responding, status: 0x%x\n", 12394 sts); 12395 rc = -ENODEV; 12396 } 12397 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12398 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12399 return bnxt_fw_reset_via_optee(bp); 12400 } 12401 return rc; 12402 } 12403 12404 return -ENODEV; 12405 } 12406 12407 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12408 { 12409 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12410 12411 if (!BNXT_NEW_RM(bp)) 12412 return; /* no resource reservations required */ 12413 12414 hw_resc->resv_cp_rings = 0; 12415 hw_resc->resv_stat_ctxs = 0; 12416 hw_resc->resv_irqs = 0; 12417 hw_resc->resv_tx_rings = 0; 12418 hw_resc->resv_rx_rings = 0; 12419 hw_resc->resv_hw_ring_grps = 0; 12420 hw_resc->resv_vnics = 0; 12421 hw_resc->resv_rsscos_ctxs = 0; 12422 if (!fw_reset) { 12423 bp->tx_nr_rings = 0; 12424 bp->rx_nr_rings = 0; 12425 } 12426 } 12427 12428 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12429 { 12430 int rc; 12431 12432 if (!BNXT_NEW_RM(bp)) 12433 return 0; /* no resource reservations required */ 12434 12435 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12436 if (rc) 12437 netdev_err(bp->dev, "resc_qcaps failed\n"); 12438 12439 bnxt_clear_reservations(bp, fw_reset); 12440 12441 return rc; 12442 } 12443 12444 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12445 { 12446 struct hwrm_func_drv_if_change_output *resp; 12447 struct hwrm_func_drv_if_change_input *req; 12448 bool resc_reinit = false; 12449 bool caps_change = false; 12450 int rc, retry = 0; 12451 bool fw_reset; 12452 u32 flags = 0; 12453 12454 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12455 bp->fw_reset_state = 0; 12456 12457 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12458 return 0; 12459 12460 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12461 if (rc) 12462 return rc; 12463 12464 if (up) 12465 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12466 resp = hwrm_req_hold(bp, req); 12467 12468 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12469 while (retry < BNXT_FW_IF_RETRY) { 12470 rc = hwrm_req_send(bp, req); 12471 if (rc != -EAGAIN) 12472 break; 12473 12474 msleep(50); 12475 retry++; 12476 } 12477 12478 if (rc == -EAGAIN) { 12479 hwrm_req_drop(bp, req); 12480 return rc; 12481 } else if (!rc) { 12482 flags = le32_to_cpu(resp->flags); 12483 } else if (up) { 12484 rc = bnxt_try_recover_fw(bp); 12485 fw_reset = true; 12486 } 12487 hwrm_req_drop(bp, req); 12488 if (rc) 12489 return rc; 12490 12491 if (!up) { 12492 bnxt_inv_fw_health_reg(bp); 12493 return 0; 12494 } 12495 12496 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12497 resc_reinit = true; 12498 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12499 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12500 fw_reset = true; 12501 else 12502 bnxt_remap_fw_health_regs(bp); 12503 12504 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12505 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12506 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12507 return -ENODEV; 12508 } 12509 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12510 caps_change = true; 12511 12512 if (resc_reinit || fw_reset || caps_change) { 12513 if (fw_reset || caps_change) { 12514 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12515 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12516 bnxt_ulp_irq_stop(bp); 12517 bnxt_free_ctx_mem(bp, false); 12518 bnxt_dcb_free(bp); 12519 rc = bnxt_fw_init_one(bp); 12520 if (rc) { 12521 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12522 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12523 return rc; 12524 } 12525 /* IRQ will be initialized later in bnxt_request_irq()*/ 12526 bnxt_clear_int_mode(bp); 12527 } 12528 rc = bnxt_cancel_reservations(bp, fw_reset); 12529 } 12530 return rc; 12531 } 12532 12533 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12534 { 12535 struct hwrm_port_led_qcaps_output *resp; 12536 struct hwrm_port_led_qcaps_input *req; 12537 struct bnxt_pf_info *pf = &bp->pf; 12538 int rc; 12539 12540 bp->num_leds = 0; 12541 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12542 return 0; 12543 12544 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12545 if (rc) 12546 return rc; 12547 12548 req->port_id = cpu_to_le16(pf->port_id); 12549 resp = hwrm_req_hold(bp, req); 12550 rc = hwrm_req_send(bp, req); 12551 if (rc) { 12552 hwrm_req_drop(bp, req); 12553 return rc; 12554 } 12555 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12556 int i; 12557 12558 bp->num_leds = resp->num_leds; 12559 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12560 bp->num_leds); 12561 for (i = 0; i < bp->num_leds; i++) { 12562 struct bnxt_led_info *led = &bp->leds[i]; 12563 __le16 caps = led->led_state_caps; 12564 12565 if (!led->led_group_id || 12566 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12567 bp->num_leds = 0; 12568 break; 12569 } 12570 } 12571 } 12572 hwrm_req_drop(bp, req); 12573 return 0; 12574 } 12575 12576 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12577 { 12578 struct hwrm_wol_filter_alloc_output *resp; 12579 struct hwrm_wol_filter_alloc_input *req; 12580 int rc; 12581 12582 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12583 if (rc) 12584 return rc; 12585 12586 req->port_id = cpu_to_le16(bp->pf.port_id); 12587 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12588 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12589 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12590 12591 resp = hwrm_req_hold(bp, req); 12592 rc = hwrm_req_send(bp, req); 12593 if (!rc) 12594 bp->wol_filter_id = resp->wol_filter_id; 12595 hwrm_req_drop(bp, req); 12596 return rc; 12597 } 12598 12599 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12600 { 12601 struct hwrm_wol_filter_free_input *req; 12602 int rc; 12603 12604 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12605 if (rc) 12606 return rc; 12607 12608 req->port_id = cpu_to_le16(bp->pf.port_id); 12609 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12610 req->wol_filter_id = bp->wol_filter_id; 12611 12612 return hwrm_req_send(bp, req); 12613 } 12614 12615 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12616 { 12617 struct hwrm_wol_filter_qcfg_output *resp; 12618 struct hwrm_wol_filter_qcfg_input *req; 12619 u16 next_handle = 0; 12620 int rc; 12621 12622 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12623 if (rc) 12624 return rc; 12625 12626 req->port_id = cpu_to_le16(bp->pf.port_id); 12627 req->handle = cpu_to_le16(handle); 12628 resp = hwrm_req_hold(bp, req); 12629 rc = hwrm_req_send(bp, req); 12630 if (!rc) { 12631 next_handle = le16_to_cpu(resp->next_handle); 12632 if (next_handle != 0) { 12633 if (resp->wol_type == 12634 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12635 bp->wol = 1; 12636 bp->wol_filter_id = resp->wol_filter_id; 12637 } 12638 } 12639 } 12640 hwrm_req_drop(bp, req); 12641 return next_handle; 12642 } 12643 12644 static void bnxt_get_wol_settings(struct bnxt *bp) 12645 { 12646 u16 handle = 0; 12647 12648 bp->wol = 0; 12649 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12650 return; 12651 12652 do { 12653 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12654 } while (handle && handle != 0xffff); 12655 } 12656 12657 static bool bnxt_eee_config_ok(struct bnxt *bp) 12658 { 12659 struct ethtool_keee *eee = &bp->eee; 12660 struct bnxt_link_info *link_info = &bp->link_info; 12661 12662 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12663 return true; 12664 12665 if (eee->eee_enabled) { 12666 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12667 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12668 12669 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12670 12671 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12672 eee->eee_enabled = 0; 12673 return false; 12674 } 12675 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12676 linkmode_and(eee->advertised, advertising, 12677 eee->supported); 12678 return false; 12679 } 12680 } 12681 return true; 12682 } 12683 12684 static int bnxt_update_phy_setting(struct bnxt *bp) 12685 { 12686 int rc; 12687 bool update_link = false; 12688 bool update_pause = false; 12689 bool update_eee = false; 12690 struct bnxt_link_info *link_info = &bp->link_info; 12691 12692 rc = bnxt_update_link(bp, true); 12693 if (rc) { 12694 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12695 rc); 12696 return rc; 12697 } 12698 if (!BNXT_SINGLE_PF(bp)) 12699 return 0; 12700 12701 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12702 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12703 link_info->req_flow_ctrl) 12704 update_pause = true; 12705 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12706 link_info->force_pause_setting != link_info->req_flow_ctrl) 12707 update_pause = true; 12708 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12709 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12710 update_link = true; 12711 if (bnxt_force_speed_updated(link_info)) 12712 update_link = true; 12713 if (link_info->req_duplex != link_info->duplex_setting) 12714 update_link = true; 12715 } else { 12716 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12717 update_link = true; 12718 if (bnxt_auto_speed_updated(link_info)) 12719 update_link = true; 12720 } 12721 12722 /* The last close may have shutdown the link, so need to call 12723 * PHY_CFG to bring it back up. 12724 */ 12725 if (!BNXT_LINK_IS_UP(bp)) 12726 update_link = true; 12727 12728 if (!bnxt_eee_config_ok(bp)) 12729 update_eee = true; 12730 12731 if (update_link) 12732 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12733 else if (update_pause) 12734 rc = bnxt_hwrm_set_pause(bp); 12735 if (rc) { 12736 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12737 rc); 12738 return rc; 12739 } 12740 12741 return rc; 12742 } 12743 12744 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12745 12746 static int bnxt_reinit_after_abort(struct bnxt *bp) 12747 { 12748 int rc; 12749 12750 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12751 return -EBUSY; 12752 12753 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12754 return -ENODEV; 12755 12756 rc = bnxt_fw_init_one(bp); 12757 if (!rc) { 12758 bnxt_clear_int_mode(bp); 12759 rc = bnxt_init_int_mode(bp); 12760 if (!rc) { 12761 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12762 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12763 } 12764 } 12765 return rc; 12766 } 12767 12768 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12769 { 12770 struct bnxt_ntuple_filter *ntp_fltr; 12771 struct bnxt_l2_filter *l2_fltr; 12772 12773 if (list_empty(&fltr->list)) 12774 return; 12775 12776 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12777 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12778 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12779 atomic_inc(&l2_fltr->refcnt); 12780 ntp_fltr->l2_fltr = l2_fltr; 12781 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12782 bnxt_del_ntp_filter(bp, ntp_fltr); 12783 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12784 fltr->sw_id); 12785 } 12786 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12787 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12788 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12789 bnxt_del_l2_filter(bp, l2_fltr); 12790 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12791 fltr->sw_id); 12792 } 12793 } 12794 } 12795 12796 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12797 { 12798 struct bnxt_filter_base *usr_fltr, *tmp; 12799 12800 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12801 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12802 } 12803 12804 static int bnxt_set_xps_mapping(struct bnxt *bp) 12805 { 12806 int numa_node = dev_to_node(&bp->pdev->dev); 12807 unsigned int q_idx, map_idx, cpu, i; 12808 const struct cpumask *cpu_mask_ptr; 12809 int nr_cpus = num_online_cpus(); 12810 cpumask_t *q_map; 12811 int rc = 0; 12812 12813 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12814 if (!q_map) 12815 return -ENOMEM; 12816 12817 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12818 * Each TC has the same number of TX queues. The nth TX queue for each 12819 * TC will have the same CPU mask. 12820 */ 12821 for (i = 0; i < nr_cpus; i++) { 12822 map_idx = i % bp->tx_nr_rings_per_tc; 12823 cpu = cpumask_local_spread(i, numa_node); 12824 cpu_mask_ptr = get_cpu_mask(cpu); 12825 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12826 } 12827 12828 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12829 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12830 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12831 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12832 if (rc) { 12833 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12834 q_idx); 12835 break; 12836 } 12837 } 12838 12839 kfree(q_map); 12840 12841 return rc; 12842 } 12843 12844 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12845 { 12846 int rc = 0; 12847 12848 netif_carrier_off(bp->dev); 12849 if (irq_re_init) { 12850 /* Reserve rings now if none were reserved at driver probe. */ 12851 rc = bnxt_init_dflt_ring_mode(bp); 12852 if (rc) { 12853 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12854 return rc; 12855 } 12856 } 12857 rc = bnxt_reserve_rings(bp, irq_re_init); 12858 if (rc) 12859 return rc; 12860 12861 rc = bnxt_alloc_mem(bp, irq_re_init); 12862 if (rc) { 12863 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12864 goto open_err_free_mem; 12865 } 12866 12867 if (irq_re_init) { 12868 bnxt_init_napi(bp); 12869 rc = bnxt_request_irq(bp); 12870 if (rc) { 12871 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12872 goto open_err_irq; 12873 } 12874 } 12875 12876 rc = bnxt_init_nic(bp, irq_re_init); 12877 if (rc) { 12878 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12879 goto open_err_irq; 12880 } 12881 12882 bnxt_enable_napi(bp); 12883 bnxt_debug_dev_init(bp); 12884 12885 if (link_re_init) { 12886 mutex_lock(&bp->link_lock); 12887 rc = bnxt_update_phy_setting(bp); 12888 mutex_unlock(&bp->link_lock); 12889 if (rc) { 12890 netdev_warn(bp->dev, "failed to update phy settings\n"); 12891 if (BNXT_SINGLE_PF(bp)) { 12892 bp->link_info.phy_retry = true; 12893 bp->link_info.phy_retry_expires = 12894 jiffies + 5 * HZ; 12895 } 12896 } 12897 } 12898 12899 if (irq_re_init) { 12900 udp_tunnel_nic_reset_ntf(bp->dev); 12901 rc = bnxt_set_xps_mapping(bp); 12902 if (rc) 12903 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12904 } 12905 12906 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12907 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12908 static_branch_enable(&bnxt_xdp_locking_key); 12909 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12910 static_branch_disable(&bnxt_xdp_locking_key); 12911 } 12912 set_bit(BNXT_STATE_OPEN, &bp->state); 12913 bnxt_enable_int(bp); 12914 /* Enable TX queues */ 12915 bnxt_tx_enable(bp); 12916 mod_timer(&bp->timer, jiffies + bp->current_interval); 12917 /* Poll link status and check for SFP+ module status */ 12918 mutex_lock(&bp->link_lock); 12919 bnxt_get_port_module_status(bp); 12920 mutex_unlock(&bp->link_lock); 12921 12922 /* VF-reps may need to be re-opened after the PF is re-opened */ 12923 if (BNXT_PF(bp)) 12924 bnxt_vf_reps_open(bp); 12925 bnxt_ptp_init_rtc(bp, true); 12926 bnxt_ptp_cfg_tstamp_filters(bp); 12927 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12928 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12929 bnxt_cfg_usr_fltrs(bp); 12930 return 0; 12931 12932 open_err_irq: 12933 bnxt_del_napi(bp); 12934 12935 open_err_free_mem: 12936 bnxt_free_skbs(bp); 12937 bnxt_free_irq(bp); 12938 bnxt_free_mem(bp, true); 12939 return rc; 12940 } 12941 12942 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12943 { 12944 int rc = 0; 12945 12946 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12947 rc = -EIO; 12948 if (!rc) 12949 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12950 if (rc) { 12951 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12952 netif_close(bp->dev); 12953 } 12954 return rc; 12955 } 12956 12957 /* netdev instance lock held, open the NIC half way by allocating all 12958 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 12959 * for offline self tests. 12960 */ 12961 int bnxt_half_open_nic(struct bnxt *bp) 12962 { 12963 int rc = 0; 12964 12965 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12966 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12967 rc = -ENODEV; 12968 goto half_open_err; 12969 } 12970 12971 rc = bnxt_alloc_mem(bp, true); 12972 if (rc) { 12973 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12974 goto half_open_err; 12975 } 12976 bnxt_init_napi(bp); 12977 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12978 rc = bnxt_init_nic(bp, true); 12979 if (rc) { 12980 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12981 bnxt_del_napi(bp); 12982 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12983 goto half_open_err; 12984 } 12985 return 0; 12986 12987 half_open_err: 12988 bnxt_free_skbs(bp); 12989 bnxt_free_mem(bp, true); 12990 netif_close(bp->dev); 12991 return rc; 12992 } 12993 12994 /* netdev instance lock held, this call can only be made after a previous 12995 * successful call to bnxt_half_open_nic(). 12996 */ 12997 void bnxt_half_close_nic(struct bnxt *bp) 12998 { 12999 bnxt_hwrm_resource_free(bp, false, true); 13000 bnxt_del_napi(bp); 13001 bnxt_free_skbs(bp); 13002 bnxt_free_mem(bp, true); 13003 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13004 } 13005 13006 void bnxt_reenable_sriov(struct bnxt *bp) 13007 { 13008 if (BNXT_PF(bp)) { 13009 struct bnxt_pf_info *pf = &bp->pf; 13010 int n = pf->active_vfs; 13011 13012 if (n) 13013 bnxt_cfg_hw_sriov(bp, &n, true); 13014 } 13015 } 13016 13017 static int bnxt_open(struct net_device *dev) 13018 { 13019 struct bnxt *bp = netdev_priv(dev); 13020 int rc; 13021 13022 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13023 rc = bnxt_reinit_after_abort(bp); 13024 if (rc) { 13025 if (rc == -EBUSY) 13026 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13027 else 13028 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13029 return -ENODEV; 13030 } 13031 } 13032 13033 rc = bnxt_hwrm_if_change(bp, true); 13034 if (rc) 13035 return rc; 13036 13037 rc = __bnxt_open_nic(bp, true, true); 13038 if (rc) { 13039 bnxt_hwrm_if_change(bp, false); 13040 } else { 13041 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13042 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13043 bnxt_queue_sp_work(bp, 13044 BNXT_RESTART_ULP_SP_EVENT); 13045 } 13046 } 13047 13048 return rc; 13049 } 13050 13051 static bool bnxt_drv_busy(struct bnxt *bp) 13052 { 13053 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13054 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13055 } 13056 13057 static void bnxt_get_ring_stats(struct bnxt *bp, 13058 struct rtnl_link_stats64 *stats); 13059 13060 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13061 bool link_re_init) 13062 { 13063 /* Close the VF-reps before closing PF */ 13064 if (BNXT_PF(bp)) 13065 bnxt_vf_reps_close(bp); 13066 13067 /* Change device state to avoid TX queue wake up's */ 13068 bnxt_tx_disable(bp); 13069 13070 clear_bit(BNXT_STATE_OPEN, &bp->state); 13071 smp_mb__after_atomic(); 13072 while (bnxt_drv_busy(bp)) 13073 msleep(20); 13074 13075 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13076 bnxt_clear_rss_ctxs(bp); 13077 /* Flush rings and disable interrupts */ 13078 bnxt_shutdown_nic(bp, irq_re_init); 13079 13080 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13081 13082 bnxt_debug_dev_exit(bp); 13083 bnxt_disable_napi(bp); 13084 timer_delete_sync(&bp->timer); 13085 bnxt_free_skbs(bp); 13086 13087 /* Save ring stats before shutdown */ 13088 if (bp->bnapi && irq_re_init) { 13089 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13090 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 13091 } 13092 if (irq_re_init) { 13093 bnxt_free_irq(bp); 13094 bnxt_del_napi(bp); 13095 } 13096 bnxt_free_mem(bp, irq_re_init); 13097 } 13098 13099 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13100 { 13101 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13102 /* If we get here, it means firmware reset is in progress 13103 * while we are trying to close. We can safely proceed with 13104 * the close because we are holding netdev instance lock. 13105 * Some firmware messages may fail as we proceed to close. 13106 * We set the ABORT_ERR flag here so that the FW reset thread 13107 * will later abort when it gets the netdev instance lock 13108 * and sees the flag. 13109 */ 13110 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13111 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13112 } 13113 13114 #ifdef CONFIG_BNXT_SRIOV 13115 if (bp->sriov_cfg) { 13116 int rc; 13117 13118 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13119 !bp->sriov_cfg, 13120 BNXT_SRIOV_CFG_WAIT_TMO); 13121 if (!rc) 13122 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13123 else if (rc < 0) 13124 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13125 } 13126 #endif 13127 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13128 } 13129 13130 static int bnxt_close(struct net_device *dev) 13131 { 13132 struct bnxt *bp = netdev_priv(dev); 13133 13134 bnxt_close_nic(bp, true, true); 13135 bnxt_hwrm_shutdown_link(bp); 13136 bnxt_hwrm_if_change(bp, false); 13137 return 0; 13138 } 13139 13140 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13141 u16 *val) 13142 { 13143 struct hwrm_port_phy_mdio_read_output *resp; 13144 struct hwrm_port_phy_mdio_read_input *req; 13145 int rc; 13146 13147 if (bp->hwrm_spec_code < 0x10a00) 13148 return -EOPNOTSUPP; 13149 13150 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13151 if (rc) 13152 return rc; 13153 13154 req->port_id = cpu_to_le16(bp->pf.port_id); 13155 req->phy_addr = phy_addr; 13156 req->reg_addr = cpu_to_le16(reg & 0x1f); 13157 if (mdio_phy_id_is_c45(phy_addr)) { 13158 req->cl45_mdio = 1; 13159 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13160 req->dev_addr = mdio_phy_id_devad(phy_addr); 13161 req->reg_addr = cpu_to_le16(reg); 13162 } 13163 13164 resp = hwrm_req_hold(bp, req); 13165 rc = hwrm_req_send(bp, req); 13166 if (!rc) 13167 *val = le16_to_cpu(resp->reg_data); 13168 hwrm_req_drop(bp, req); 13169 return rc; 13170 } 13171 13172 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13173 u16 val) 13174 { 13175 struct hwrm_port_phy_mdio_write_input *req; 13176 int rc; 13177 13178 if (bp->hwrm_spec_code < 0x10a00) 13179 return -EOPNOTSUPP; 13180 13181 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13182 if (rc) 13183 return rc; 13184 13185 req->port_id = cpu_to_le16(bp->pf.port_id); 13186 req->phy_addr = phy_addr; 13187 req->reg_addr = cpu_to_le16(reg & 0x1f); 13188 if (mdio_phy_id_is_c45(phy_addr)) { 13189 req->cl45_mdio = 1; 13190 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13191 req->dev_addr = mdio_phy_id_devad(phy_addr); 13192 req->reg_addr = cpu_to_le16(reg); 13193 } 13194 req->reg_data = cpu_to_le16(val); 13195 13196 return hwrm_req_send(bp, req); 13197 } 13198 13199 /* netdev instance lock held */ 13200 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13201 { 13202 struct mii_ioctl_data *mdio = if_mii(ifr); 13203 struct bnxt *bp = netdev_priv(dev); 13204 int rc; 13205 13206 switch (cmd) { 13207 case SIOCGMIIPHY: 13208 mdio->phy_id = bp->link_info.phy_addr; 13209 13210 fallthrough; 13211 case SIOCGMIIREG: { 13212 u16 mii_regval = 0; 13213 13214 if (!netif_running(dev)) 13215 return -EAGAIN; 13216 13217 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13218 &mii_regval); 13219 mdio->val_out = mii_regval; 13220 return rc; 13221 } 13222 13223 case SIOCSMIIREG: 13224 if (!netif_running(dev)) 13225 return -EAGAIN; 13226 13227 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13228 mdio->val_in); 13229 13230 case SIOCSHWTSTAMP: 13231 return bnxt_hwtstamp_set(dev, ifr); 13232 13233 case SIOCGHWTSTAMP: 13234 return bnxt_hwtstamp_get(dev, ifr); 13235 13236 default: 13237 /* do nothing */ 13238 break; 13239 } 13240 return -EOPNOTSUPP; 13241 } 13242 13243 static void bnxt_get_ring_stats(struct bnxt *bp, 13244 struct rtnl_link_stats64 *stats) 13245 { 13246 int i; 13247 13248 for (i = 0; i < bp->cp_nr_rings; i++) { 13249 struct bnxt_napi *bnapi = bp->bnapi[i]; 13250 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13251 u64 *sw = cpr->stats.sw_stats; 13252 13253 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13254 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13255 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13256 13257 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13258 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13259 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13260 13261 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13262 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13263 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13264 13265 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13266 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13267 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13268 13269 stats->rx_missed_errors += 13270 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13271 13272 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13273 13274 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13275 13276 stats->rx_dropped += 13277 cpr->sw_stats->rx.rx_netpoll_discards + 13278 cpr->sw_stats->rx.rx_oom_discards; 13279 } 13280 } 13281 13282 static void bnxt_add_prev_stats(struct bnxt *bp, 13283 struct rtnl_link_stats64 *stats) 13284 { 13285 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13286 13287 stats->rx_packets += prev_stats->rx_packets; 13288 stats->tx_packets += prev_stats->tx_packets; 13289 stats->rx_bytes += prev_stats->rx_bytes; 13290 stats->tx_bytes += prev_stats->tx_bytes; 13291 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13292 stats->multicast += prev_stats->multicast; 13293 stats->rx_dropped += prev_stats->rx_dropped; 13294 stats->tx_dropped += prev_stats->tx_dropped; 13295 } 13296 13297 static void 13298 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13299 { 13300 struct bnxt *bp = netdev_priv(dev); 13301 13302 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13303 /* Make sure bnxt_close_nic() sees that we are reading stats before 13304 * we check the BNXT_STATE_OPEN flag. 13305 */ 13306 smp_mb__after_atomic(); 13307 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13308 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13309 *stats = bp->net_stats_prev; 13310 return; 13311 } 13312 13313 bnxt_get_ring_stats(bp, stats); 13314 bnxt_add_prev_stats(bp, stats); 13315 13316 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13317 u64 *rx = bp->port_stats.sw_stats; 13318 u64 *tx = bp->port_stats.sw_stats + 13319 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13320 13321 stats->rx_crc_errors = 13322 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13323 stats->rx_frame_errors = 13324 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13325 stats->rx_length_errors = 13326 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13327 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13328 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13329 stats->rx_errors = 13330 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13331 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13332 stats->collisions = 13333 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13334 stats->tx_fifo_errors = 13335 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13336 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13337 } 13338 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13339 } 13340 13341 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 13342 struct bnxt_total_ring_err_stats *stats, 13343 struct bnxt_cp_ring_info *cpr) 13344 { 13345 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13346 u64 *hw_stats = cpr->stats.sw_stats; 13347 13348 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13349 stats->rx_total_resets += sw_stats->rx.rx_resets; 13350 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13351 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13352 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13353 stats->rx_total_ring_discards += 13354 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13355 stats->tx_total_resets += sw_stats->tx.tx_resets; 13356 stats->tx_total_ring_discards += 13357 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13358 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13359 } 13360 13361 void bnxt_get_ring_err_stats(struct bnxt *bp, 13362 struct bnxt_total_ring_err_stats *stats) 13363 { 13364 int i; 13365 13366 for (i = 0; i < bp->cp_nr_rings; i++) 13367 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13368 } 13369 13370 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 13371 { 13372 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13373 struct net_device *dev = bp->dev; 13374 struct netdev_hw_addr *ha; 13375 u8 *haddr; 13376 int mc_count = 0; 13377 bool update = false; 13378 int off = 0; 13379 13380 netdev_for_each_mc_addr(ha, dev) { 13381 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13382 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13383 vnic->mc_list_count = 0; 13384 return false; 13385 } 13386 haddr = ha->addr; 13387 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13388 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13389 update = true; 13390 } 13391 off += ETH_ALEN; 13392 mc_count++; 13393 } 13394 if (mc_count) 13395 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13396 13397 if (mc_count != vnic->mc_list_count) { 13398 vnic->mc_list_count = mc_count; 13399 update = true; 13400 } 13401 return update; 13402 } 13403 13404 static bool bnxt_uc_list_updated(struct bnxt *bp) 13405 { 13406 struct net_device *dev = bp->dev; 13407 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13408 struct netdev_hw_addr *ha; 13409 int off = 0; 13410 13411 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13412 return true; 13413 13414 netdev_for_each_uc_addr(ha, dev) { 13415 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13416 return true; 13417 13418 off += ETH_ALEN; 13419 } 13420 return false; 13421 } 13422 13423 static void bnxt_set_rx_mode(struct net_device *dev) 13424 { 13425 struct bnxt *bp = netdev_priv(dev); 13426 struct bnxt_vnic_info *vnic; 13427 bool mc_update = false; 13428 bool uc_update; 13429 u32 mask; 13430 13431 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13432 return; 13433 13434 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13435 mask = vnic->rx_mask; 13436 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13437 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13438 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13439 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13440 13441 if (dev->flags & IFF_PROMISC) 13442 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13443 13444 uc_update = bnxt_uc_list_updated(bp); 13445 13446 if (dev->flags & IFF_BROADCAST) 13447 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13448 if (dev->flags & IFF_ALLMULTI) { 13449 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13450 vnic->mc_list_count = 0; 13451 } else if (dev->flags & IFF_MULTICAST) { 13452 mc_update = bnxt_mc_list_updated(bp, &mask); 13453 } 13454 13455 if (mask != vnic->rx_mask || uc_update || mc_update) { 13456 vnic->rx_mask = mask; 13457 13458 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13459 } 13460 } 13461 13462 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13463 { 13464 struct net_device *dev = bp->dev; 13465 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13466 struct netdev_hw_addr *ha; 13467 int i, off = 0, rc; 13468 bool uc_update; 13469 13470 netif_addr_lock_bh(dev); 13471 uc_update = bnxt_uc_list_updated(bp); 13472 netif_addr_unlock_bh(dev); 13473 13474 if (!uc_update) 13475 goto skip_uc; 13476 13477 for (i = 1; i < vnic->uc_filter_count; i++) { 13478 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13479 13480 bnxt_hwrm_l2_filter_free(bp, fltr); 13481 bnxt_del_l2_filter(bp, fltr); 13482 } 13483 13484 vnic->uc_filter_count = 1; 13485 13486 netif_addr_lock_bh(dev); 13487 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13488 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13489 } else { 13490 netdev_for_each_uc_addr(ha, dev) { 13491 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13492 off += ETH_ALEN; 13493 vnic->uc_filter_count++; 13494 } 13495 } 13496 netif_addr_unlock_bh(dev); 13497 13498 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13499 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13500 if (rc) { 13501 if (BNXT_VF(bp) && rc == -ENODEV) { 13502 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13503 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13504 else 13505 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13506 rc = 0; 13507 } else { 13508 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13509 } 13510 vnic->uc_filter_count = i; 13511 return rc; 13512 } 13513 } 13514 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13515 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13516 13517 skip_uc: 13518 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13519 !bnxt_promisc_ok(bp)) 13520 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13521 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13522 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13523 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13524 rc); 13525 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13526 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13527 vnic->mc_list_count = 0; 13528 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13529 } 13530 if (rc) 13531 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13532 rc); 13533 13534 return rc; 13535 } 13536 13537 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13538 { 13539 #ifdef CONFIG_BNXT_SRIOV 13540 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13541 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13542 13543 /* No minimum rings were provisioned by the PF. Don't 13544 * reserve rings by default when device is down. 13545 */ 13546 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13547 return true; 13548 13549 if (!netif_running(bp->dev)) 13550 return false; 13551 } 13552 #endif 13553 return true; 13554 } 13555 13556 /* If the chip and firmware supports RFS */ 13557 static bool bnxt_rfs_supported(struct bnxt *bp) 13558 { 13559 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13560 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13561 return true; 13562 return false; 13563 } 13564 /* 212 firmware is broken for aRFS */ 13565 if (BNXT_FW_MAJ(bp) == 212) 13566 return false; 13567 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13568 return true; 13569 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13570 return true; 13571 return false; 13572 } 13573 13574 /* If runtime conditions support RFS */ 13575 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13576 { 13577 struct bnxt_hw_rings hwr = {0}; 13578 int max_vnics, max_rss_ctxs; 13579 13580 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13581 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13582 return bnxt_rfs_supported(bp); 13583 13584 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13585 return false; 13586 13587 hwr.grp = bp->rx_nr_rings; 13588 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13589 if (new_rss_ctx) 13590 hwr.vnic++; 13591 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13592 max_vnics = bnxt_get_max_func_vnics(bp); 13593 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13594 13595 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13596 if (bp->rx_nr_rings > 1) 13597 netdev_warn(bp->dev, 13598 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13599 min(max_rss_ctxs - 1, max_vnics - 1)); 13600 return false; 13601 } 13602 13603 if (!BNXT_NEW_RM(bp)) 13604 return true; 13605 13606 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13607 * issue that will mess up the default VNIC if we reduce the 13608 * reservations. 13609 */ 13610 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13611 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13612 return true; 13613 13614 bnxt_hwrm_reserve_rings(bp, &hwr); 13615 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13616 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13617 return true; 13618 13619 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13620 hwr.vnic = 1; 13621 hwr.rss_ctx = 0; 13622 bnxt_hwrm_reserve_rings(bp, &hwr); 13623 return false; 13624 } 13625 13626 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13627 netdev_features_t features) 13628 { 13629 struct bnxt *bp = netdev_priv(dev); 13630 netdev_features_t vlan_features; 13631 13632 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13633 features &= ~NETIF_F_NTUPLE; 13634 13635 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13636 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13637 13638 if (!(features & NETIF_F_GRO)) 13639 features &= ~NETIF_F_GRO_HW; 13640 13641 if (features & NETIF_F_GRO_HW) 13642 features &= ~NETIF_F_LRO; 13643 13644 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13645 * turned on or off together. 13646 */ 13647 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13648 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13649 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13650 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13651 else if (vlan_features) 13652 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13653 } 13654 #ifdef CONFIG_BNXT_SRIOV 13655 if (BNXT_VF(bp) && bp->vf.vlan) 13656 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13657 #endif 13658 return features; 13659 } 13660 13661 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13662 bool link_re_init, u32 flags, bool update_tpa) 13663 { 13664 bnxt_close_nic(bp, irq_re_init, link_re_init); 13665 bp->flags = flags; 13666 if (update_tpa) 13667 bnxt_set_ring_params(bp); 13668 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13669 } 13670 13671 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13672 { 13673 bool update_tpa = false, update_ntuple = false; 13674 struct bnxt *bp = netdev_priv(dev); 13675 u32 flags = bp->flags; 13676 u32 changes; 13677 int rc = 0; 13678 bool re_init = false; 13679 13680 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13681 if (features & NETIF_F_GRO_HW) 13682 flags |= BNXT_FLAG_GRO; 13683 else if (features & NETIF_F_LRO) 13684 flags |= BNXT_FLAG_LRO; 13685 13686 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13687 flags &= ~BNXT_FLAG_TPA; 13688 13689 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13690 flags |= BNXT_FLAG_STRIP_VLAN; 13691 13692 if (features & NETIF_F_NTUPLE) 13693 flags |= BNXT_FLAG_RFS; 13694 else 13695 bnxt_clear_usr_fltrs(bp, true); 13696 13697 changes = flags ^ bp->flags; 13698 if (changes & BNXT_FLAG_TPA) { 13699 update_tpa = true; 13700 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13701 (flags & BNXT_FLAG_TPA) == 0 || 13702 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13703 re_init = true; 13704 } 13705 13706 if (changes & ~BNXT_FLAG_TPA) 13707 re_init = true; 13708 13709 if (changes & BNXT_FLAG_RFS) 13710 update_ntuple = true; 13711 13712 if (flags != bp->flags) { 13713 u32 old_flags = bp->flags; 13714 13715 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13716 bp->flags = flags; 13717 if (update_tpa) 13718 bnxt_set_ring_params(bp); 13719 return rc; 13720 } 13721 13722 if (update_ntuple) 13723 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13724 13725 if (re_init) 13726 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13727 13728 if (update_tpa) { 13729 bp->flags = flags; 13730 rc = bnxt_set_tpa(bp, 13731 (flags & BNXT_FLAG_TPA) ? 13732 true : false); 13733 if (rc) 13734 bp->flags = old_flags; 13735 } 13736 } 13737 return rc; 13738 } 13739 13740 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13741 u8 **nextp) 13742 { 13743 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13744 struct hop_jumbo_hdr *jhdr; 13745 int hdr_count = 0; 13746 u8 *nexthdr; 13747 int start; 13748 13749 /* Check that there are at most 2 IPv6 extension headers, no 13750 * fragment header, and each is <= 64 bytes. 13751 */ 13752 start = nw_off + sizeof(*ip6h); 13753 nexthdr = &ip6h->nexthdr; 13754 while (ipv6_ext_hdr(*nexthdr)) { 13755 struct ipv6_opt_hdr *hp; 13756 int hdrlen; 13757 13758 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13759 *nexthdr == NEXTHDR_FRAGMENT) 13760 return false; 13761 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13762 skb_headlen(skb), NULL); 13763 if (!hp) 13764 return false; 13765 if (*nexthdr == NEXTHDR_AUTH) 13766 hdrlen = ipv6_authlen(hp); 13767 else 13768 hdrlen = ipv6_optlen(hp); 13769 13770 if (hdrlen > 64) 13771 return false; 13772 13773 /* The ext header may be a hop-by-hop header inserted for 13774 * big TCP purposes. This will be removed before sending 13775 * from NIC, so do not count it. 13776 */ 13777 if (*nexthdr == NEXTHDR_HOP) { 13778 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13779 goto increment_hdr; 13780 13781 jhdr = (struct hop_jumbo_hdr *)hp; 13782 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13783 jhdr->nexthdr != IPPROTO_TCP) 13784 goto increment_hdr; 13785 13786 goto next_hdr; 13787 } 13788 increment_hdr: 13789 hdr_count++; 13790 next_hdr: 13791 nexthdr = &hp->nexthdr; 13792 start += hdrlen; 13793 } 13794 if (nextp) { 13795 /* Caller will check inner protocol */ 13796 if (skb->encapsulation) { 13797 *nextp = nexthdr; 13798 return true; 13799 } 13800 *nextp = NULL; 13801 } 13802 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13803 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13804 } 13805 13806 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13807 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13808 { 13809 struct udphdr *uh = udp_hdr(skb); 13810 __be16 udp_port = uh->dest; 13811 13812 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13813 udp_port != bp->vxlan_gpe_port) 13814 return false; 13815 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13816 struct ethhdr *eh = inner_eth_hdr(skb); 13817 13818 switch (eh->h_proto) { 13819 case htons(ETH_P_IP): 13820 return true; 13821 case htons(ETH_P_IPV6): 13822 return bnxt_exthdr_check(bp, skb, 13823 skb_inner_network_offset(skb), 13824 NULL); 13825 } 13826 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13827 return true; 13828 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13829 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13830 NULL); 13831 } 13832 return false; 13833 } 13834 13835 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13836 { 13837 switch (l4_proto) { 13838 case IPPROTO_UDP: 13839 return bnxt_udp_tunl_check(bp, skb); 13840 case IPPROTO_IPIP: 13841 return true; 13842 case IPPROTO_GRE: { 13843 switch (skb->inner_protocol) { 13844 default: 13845 return false; 13846 case htons(ETH_P_IP): 13847 return true; 13848 case htons(ETH_P_IPV6): 13849 fallthrough; 13850 } 13851 } 13852 case IPPROTO_IPV6: 13853 /* Check ext headers of inner ipv6 */ 13854 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13855 NULL); 13856 } 13857 return false; 13858 } 13859 13860 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13861 struct net_device *dev, 13862 netdev_features_t features) 13863 { 13864 struct bnxt *bp = netdev_priv(dev); 13865 u8 *l4_proto; 13866 13867 features = vlan_features_check(skb, features); 13868 switch (vlan_get_protocol(skb)) { 13869 case htons(ETH_P_IP): 13870 if (!skb->encapsulation) 13871 return features; 13872 l4_proto = &ip_hdr(skb)->protocol; 13873 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13874 return features; 13875 break; 13876 case htons(ETH_P_IPV6): 13877 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13878 &l4_proto)) 13879 break; 13880 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13881 return features; 13882 break; 13883 } 13884 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13885 } 13886 13887 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13888 u32 *reg_buf) 13889 { 13890 struct hwrm_dbg_read_direct_output *resp; 13891 struct hwrm_dbg_read_direct_input *req; 13892 __le32 *dbg_reg_buf; 13893 dma_addr_t mapping; 13894 int rc, i; 13895 13896 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13897 if (rc) 13898 return rc; 13899 13900 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13901 &mapping); 13902 if (!dbg_reg_buf) { 13903 rc = -ENOMEM; 13904 goto dbg_rd_reg_exit; 13905 } 13906 13907 req->host_dest_addr = cpu_to_le64(mapping); 13908 13909 resp = hwrm_req_hold(bp, req); 13910 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13911 req->read_len32 = cpu_to_le32(num_words); 13912 13913 rc = hwrm_req_send(bp, req); 13914 if (rc || resp->error_code) { 13915 rc = -EIO; 13916 goto dbg_rd_reg_exit; 13917 } 13918 for (i = 0; i < num_words; i++) 13919 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13920 13921 dbg_rd_reg_exit: 13922 hwrm_req_drop(bp, req); 13923 return rc; 13924 } 13925 13926 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13927 u32 ring_id, u32 *prod, u32 *cons) 13928 { 13929 struct hwrm_dbg_ring_info_get_output *resp; 13930 struct hwrm_dbg_ring_info_get_input *req; 13931 int rc; 13932 13933 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13934 if (rc) 13935 return rc; 13936 13937 req->ring_type = ring_type; 13938 req->fw_ring_id = cpu_to_le32(ring_id); 13939 resp = hwrm_req_hold(bp, req); 13940 rc = hwrm_req_send(bp, req); 13941 if (!rc) { 13942 *prod = le32_to_cpu(resp->producer_index); 13943 *cons = le32_to_cpu(resp->consumer_index); 13944 } 13945 hwrm_req_drop(bp, req); 13946 return rc; 13947 } 13948 13949 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13950 { 13951 struct bnxt_tx_ring_info *txr; 13952 int i = bnapi->index, j; 13953 13954 bnxt_for_each_napi_tx(j, bnapi, txr) 13955 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13956 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13957 txr->tx_cons); 13958 } 13959 13960 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13961 { 13962 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13963 int i = bnapi->index; 13964 13965 if (!rxr) 13966 return; 13967 13968 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", 13969 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13970 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13971 rxr->rx_sw_agg_prod); 13972 } 13973 13974 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13975 { 13976 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13977 int i = bnapi->index; 13978 13979 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13980 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13981 } 13982 13983 static void bnxt_dbg_dump_states(struct bnxt *bp) 13984 { 13985 int i; 13986 struct bnxt_napi *bnapi; 13987 13988 for (i = 0; i < bp->cp_nr_rings; i++) { 13989 bnapi = bp->bnapi[i]; 13990 if (netif_msg_drv(bp)) { 13991 bnxt_dump_tx_sw_state(bnapi); 13992 bnxt_dump_rx_sw_state(bnapi); 13993 bnxt_dump_cp_sw_state(bnapi); 13994 } 13995 } 13996 } 13997 13998 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13999 { 14000 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 14001 struct hwrm_ring_reset_input *req; 14002 struct bnxt_napi *bnapi = rxr->bnapi; 14003 struct bnxt_cp_ring_info *cpr; 14004 u16 cp_ring_id; 14005 int rc; 14006 14007 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 14008 if (rc) 14009 return rc; 14010 14011 cpr = &bnapi->cp_ring; 14012 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14013 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14014 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14015 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14016 return hwrm_req_send_silent(bp, req); 14017 } 14018 14019 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14020 { 14021 if (!silent) 14022 bnxt_dbg_dump_states(bp); 14023 if (netif_running(bp->dev)) { 14024 bnxt_close_nic(bp, !silent, false); 14025 bnxt_open_nic(bp, !silent, false); 14026 } 14027 } 14028 14029 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14030 { 14031 struct bnxt *bp = netdev_priv(dev); 14032 14033 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14034 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14035 } 14036 14037 static void bnxt_fw_health_check(struct bnxt *bp) 14038 { 14039 struct bnxt_fw_health *fw_health = bp->fw_health; 14040 struct pci_dev *pdev = bp->pdev; 14041 u32 val; 14042 14043 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14044 return; 14045 14046 /* Make sure it is enabled before checking the tmr_counter. */ 14047 smp_rmb(); 14048 if (fw_health->tmr_counter) { 14049 fw_health->tmr_counter--; 14050 return; 14051 } 14052 14053 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14054 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14055 fw_health->arrests++; 14056 goto fw_reset; 14057 } 14058 14059 fw_health->last_fw_heartbeat = val; 14060 14061 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14062 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14063 fw_health->discoveries++; 14064 goto fw_reset; 14065 } 14066 14067 fw_health->tmr_counter = fw_health->tmr_multiplier; 14068 return; 14069 14070 fw_reset: 14071 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14072 } 14073 14074 static void bnxt_timer(struct timer_list *t) 14075 { 14076 struct bnxt *bp = timer_container_of(bp, t, timer); 14077 struct net_device *dev = bp->dev; 14078 14079 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14080 return; 14081 14082 if (atomic_read(&bp->intr_sem) != 0) 14083 goto bnxt_restart_timer; 14084 14085 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14086 bnxt_fw_health_check(bp); 14087 14088 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14089 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14090 14091 if (bnxt_tc_flower_enabled(bp)) 14092 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14093 14094 #ifdef CONFIG_RFS_ACCEL 14095 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14096 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14097 #endif /*CONFIG_RFS_ACCEL*/ 14098 14099 if (bp->link_info.phy_retry) { 14100 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14101 bp->link_info.phy_retry = false; 14102 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14103 } else { 14104 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14105 } 14106 } 14107 14108 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 14109 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 14110 14111 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14112 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14113 14114 bnxt_restart_timer: 14115 mod_timer(&bp->timer, jiffies + bp->current_interval); 14116 } 14117 14118 static void bnxt_lock_sp(struct bnxt *bp) 14119 { 14120 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14121 * set. If the device is being closed, bnxt_close() may be holding 14122 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14123 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14124 * instance lock. 14125 */ 14126 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14127 netdev_lock(bp->dev); 14128 } 14129 14130 static void bnxt_unlock_sp(struct bnxt *bp) 14131 { 14132 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14133 netdev_unlock(bp->dev); 14134 } 14135 14136 /* Only called from bnxt_sp_task() */ 14137 static void bnxt_reset(struct bnxt *bp, bool silent) 14138 { 14139 bnxt_lock_sp(bp); 14140 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14141 bnxt_reset_task(bp, silent); 14142 bnxt_unlock_sp(bp); 14143 } 14144 14145 /* Only called from bnxt_sp_task() */ 14146 static void bnxt_rx_ring_reset(struct bnxt *bp) 14147 { 14148 int i; 14149 14150 bnxt_lock_sp(bp); 14151 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14152 bnxt_unlock_sp(bp); 14153 return; 14154 } 14155 /* Disable and flush TPA before resetting the RX ring */ 14156 if (bp->flags & BNXT_FLAG_TPA) 14157 bnxt_set_tpa(bp, false); 14158 for (i = 0; i < bp->rx_nr_rings; i++) { 14159 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14160 struct bnxt_cp_ring_info *cpr; 14161 int rc; 14162 14163 if (!rxr->bnapi->in_reset) 14164 continue; 14165 14166 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14167 if (rc) { 14168 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14169 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14170 else 14171 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14172 rc); 14173 bnxt_reset_task(bp, true); 14174 break; 14175 } 14176 bnxt_free_one_rx_ring_skbs(bp, rxr); 14177 rxr->rx_prod = 0; 14178 rxr->rx_agg_prod = 0; 14179 rxr->rx_sw_agg_prod = 0; 14180 rxr->rx_next_cons = 0; 14181 rxr->bnapi->in_reset = false; 14182 bnxt_alloc_one_rx_ring(bp, i); 14183 cpr = &rxr->bnapi->cp_ring; 14184 cpr->sw_stats->rx.rx_resets++; 14185 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14186 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14187 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14188 } 14189 if (bp->flags & BNXT_FLAG_TPA) 14190 bnxt_set_tpa(bp, true); 14191 bnxt_unlock_sp(bp); 14192 } 14193 14194 static void bnxt_fw_fatal_close(struct bnxt *bp) 14195 { 14196 bnxt_tx_disable(bp); 14197 bnxt_disable_napi(bp); 14198 bnxt_disable_int_sync(bp); 14199 bnxt_free_irq(bp); 14200 bnxt_clear_int_mode(bp); 14201 pci_disable_device(bp->pdev); 14202 } 14203 14204 static void bnxt_fw_reset_close(struct bnxt *bp) 14205 { 14206 /* When firmware is in fatal state, quiesce device and disable 14207 * bus master to prevent any potential bad DMAs before freeing 14208 * kernel memory. 14209 */ 14210 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14211 u16 val = 0; 14212 14213 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14214 if (val == 0xffff) 14215 bp->fw_reset_min_dsecs = 0; 14216 bnxt_fw_fatal_close(bp); 14217 } 14218 __bnxt_close_nic(bp, true, false); 14219 bnxt_vf_reps_free(bp); 14220 bnxt_clear_int_mode(bp); 14221 bnxt_hwrm_func_drv_unrgtr(bp); 14222 if (pci_is_enabled(bp->pdev)) 14223 pci_disable_device(bp->pdev); 14224 bnxt_free_ctx_mem(bp, false); 14225 } 14226 14227 static bool is_bnxt_fw_ok(struct bnxt *bp) 14228 { 14229 struct bnxt_fw_health *fw_health = bp->fw_health; 14230 bool no_heartbeat = false, has_reset = false; 14231 u32 val; 14232 14233 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14234 if (val == fw_health->last_fw_heartbeat) 14235 no_heartbeat = true; 14236 14237 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14238 if (val != fw_health->last_fw_reset_cnt) 14239 has_reset = true; 14240 14241 if (!no_heartbeat && has_reset) 14242 return true; 14243 14244 return false; 14245 } 14246 14247 /* netdev instance lock is acquired before calling this function */ 14248 static void bnxt_force_fw_reset(struct bnxt *bp) 14249 { 14250 struct bnxt_fw_health *fw_health = bp->fw_health; 14251 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14252 u32 wait_dsecs; 14253 14254 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14255 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14256 return; 14257 14258 /* we have to serialize with bnxt_refclk_read()*/ 14259 if (ptp) { 14260 unsigned long flags; 14261 14262 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14263 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14264 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14265 } else { 14266 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14267 } 14268 bnxt_fw_reset_close(bp); 14269 wait_dsecs = fw_health->master_func_wait_dsecs; 14270 if (fw_health->primary) { 14271 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14272 wait_dsecs = 0; 14273 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14274 } else { 14275 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14276 wait_dsecs = fw_health->normal_func_wait_dsecs; 14277 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14278 } 14279 14280 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14281 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14282 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14283 } 14284 14285 void bnxt_fw_exception(struct bnxt *bp) 14286 { 14287 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14288 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14289 bnxt_ulp_stop(bp); 14290 bnxt_lock_sp(bp); 14291 bnxt_force_fw_reset(bp); 14292 bnxt_unlock_sp(bp); 14293 } 14294 14295 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14296 * < 0 on error. 14297 */ 14298 static int bnxt_get_registered_vfs(struct bnxt *bp) 14299 { 14300 #ifdef CONFIG_BNXT_SRIOV 14301 int rc; 14302 14303 if (!BNXT_PF(bp)) 14304 return 0; 14305 14306 rc = bnxt_hwrm_func_qcfg(bp); 14307 if (rc) { 14308 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14309 return rc; 14310 } 14311 if (bp->pf.registered_vfs) 14312 return bp->pf.registered_vfs; 14313 if (bp->sriov_cfg) 14314 return 1; 14315 #endif 14316 return 0; 14317 } 14318 14319 void bnxt_fw_reset(struct bnxt *bp) 14320 { 14321 bnxt_ulp_stop(bp); 14322 bnxt_lock_sp(bp); 14323 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14324 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14325 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14326 int n = 0, tmo; 14327 14328 /* we have to serialize with bnxt_refclk_read()*/ 14329 if (ptp) { 14330 unsigned long flags; 14331 14332 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14333 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14334 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14335 } else { 14336 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14337 } 14338 if (bp->pf.active_vfs && 14339 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14340 n = bnxt_get_registered_vfs(bp); 14341 if (n < 0) { 14342 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14343 n); 14344 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14345 netif_close(bp->dev); 14346 goto fw_reset_exit; 14347 } else if (n > 0) { 14348 u16 vf_tmo_dsecs = n * 10; 14349 14350 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14351 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14352 bp->fw_reset_state = 14353 BNXT_FW_RESET_STATE_POLL_VF; 14354 bnxt_queue_fw_reset_work(bp, HZ / 10); 14355 goto fw_reset_exit; 14356 } 14357 bnxt_fw_reset_close(bp); 14358 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14359 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14360 tmo = HZ / 10; 14361 } else { 14362 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14363 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14364 } 14365 bnxt_queue_fw_reset_work(bp, tmo); 14366 } 14367 fw_reset_exit: 14368 bnxt_unlock_sp(bp); 14369 } 14370 14371 static void bnxt_chk_missed_irq(struct bnxt *bp) 14372 { 14373 int i; 14374 14375 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14376 return; 14377 14378 for (i = 0; i < bp->cp_nr_rings; i++) { 14379 struct bnxt_napi *bnapi = bp->bnapi[i]; 14380 struct bnxt_cp_ring_info *cpr; 14381 u32 fw_ring_id; 14382 int j; 14383 14384 if (!bnapi) 14385 continue; 14386 14387 cpr = &bnapi->cp_ring; 14388 for (j = 0; j < cpr->cp_ring_count; j++) { 14389 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14390 u32 val[2]; 14391 14392 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14393 continue; 14394 14395 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14396 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14397 continue; 14398 } 14399 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14400 bnxt_dbg_hwrm_ring_info_get(bp, 14401 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14402 fw_ring_id, &val[0], &val[1]); 14403 cpr->sw_stats->cmn.missed_irqs++; 14404 } 14405 } 14406 } 14407 14408 static void bnxt_cfg_ntp_filters(struct bnxt *); 14409 14410 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14411 { 14412 struct bnxt_link_info *link_info = &bp->link_info; 14413 14414 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14415 link_info->autoneg = BNXT_AUTONEG_SPEED; 14416 if (bp->hwrm_spec_code >= 0x10201) { 14417 if (link_info->auto_pause_setting & 14418 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14419 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14420 } else { 14421 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14422 } 14423 bnxt_set_auto_speed(link_info); 14424 } else { 14425 bnxt_set_force_speed(link_info); 14426 link_info->req_duplex = link_info->duplex_setting; 14427 } 14428 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14429 link_info->req_flow_ctrl = 14430 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14431 else 14432 link_info->req_flow_ctrl = link_info->force_pause_setting; 14433 } 14434 14435 static void bnxt_fw_echo_reply(struct bnxt *bp) 14436 { 14437 struct bnxt_fw_health *fw_health = bp->fw_health; 14438 struct hwrm_func_echo_response_input *req; 14439 int rc; 14440 14441 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14442 if (rc) 14443 return; 14444 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14445 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14446 hwrm_req_send(bp, req); 14447 } 14448 14449 static void bnxt_ulp_restart(struct bnxt *bp) 14450 { 14451 bnxt_ulp_stop(bp); 14452 bnxt_ulp_start(bp, 0); 14453 } 14454 14455 static void bnxt_sp_task(struct work_struct *work) 14456 { 14457 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14458 14459 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14460 smp_mb__after_atomic(); 14461 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14462 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14463 return; 14464 } 14465 14466 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14467 bnxt_ulp_restart(bp); 14468 bnxt_reenable_sriov(bp); 14469 } 14470 14471 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14472 bnxt_cfg_rx_mode(bp); 14473 14474 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14475 bnxt_cfg_ntp_filters(bp); 14476 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14477 bnxt_hwrm_exec_fwd_req(bp); 14478 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14479 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14480 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14481 bnxt_hwrm_port_qstats(bp, 0); 14482 bnxt_hwrm_port_qstats_ext(bp, 0); 14483 bnxt_accumulate_all_stats(bp); 14484 } 14485 14486 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14487 int rc; 14488 14489 mutex_lock(&bp->link_lock); 14490 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14491 &bp->sp_event)) 14492 bnxt_hwrm_phy_qcaps(bp); 14493 14494 rc = bnxt_update_link(bp, true); 14495 if (rc) 14496 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14497 rc); 14498 14499 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14500 &bp->sp_event)) 14501 bnxt_init_ethtool_link_settings(bp); 14502 mutex_unlock(&bp->link_lock); 14503 } 14504 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14505 int rc; 14506 14507 mutex_lock(&bp->link_lock); 14508 rc = bnxt_update_phy_setting(bp); 14509 mutex_unlock(&bp->link_lock); 14510 if (rc) { 14511 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14512 } else { 14513 bp->link_info.phy_retry = false; 14514 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14515 } 14516 } 14517 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14518 mutex_lock(&bp->link_lock); 14519 bnxt_get_port_module_status(bp); 14520 mutex_unlock(&bp->link_lock); 14521 } 14522 14523 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14524 bnxt_tc_flow_stats_work(bp); 14525 14526 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14527 bnxt_chk_missed_irq(bp); 14528 14529 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14530 bnxt_fw_echo_reply(bp); 14531 14532 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14533 bnxt_hwmon_notify_event(bp); 14534 14535 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14536 * must be the last functions to be called before exiting. 14537 */ 14538 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14539 bnxt_reset(bp, false); 14540 14541 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14542 bnxt_reset(bp, true); 14543 14544 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14545 bnxt_rx_ring_reset(bp); 14546 14547 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14548 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14549 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14550 bnxt_devlink_health_fw_report(bp); 14551 else 14552 bnxt_fw_reset(bp); 14553 } 14554 14555 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14556 if (!is_bnxt_fw_ok(bp)) 14557 bnxt_devlink_health_fw_report(bp); 14558 } 14559 14560 smp_mb__before_atomic(); 14561 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14562 } 14563 14564 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14565 int *max_cp); 14566 14567 /* Under netdev instance lock */ 14568 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14569 int tx_xdp) 14570 { 14571 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14572 struct bnxt_hw_rings hwr = {0}; 14573 int rx_rings = rx; 14574 int rc; 14575 14576 if (tcs) 14577 tx_sets = tcs; 14578 14579 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14580 14581 if (max_rx < rx_rings) 14582 return -ENOMEM; 14583 14584 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14585 rx_rings <<= 1; 14586 14587 hwr.rx = rx_rings; 14588 hwr.tx = tx * tx_sets + tx_xdp; 14589 if (max_tx < hwr.tx) 14590 return -ENOMEM; 14591 14592 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14593 14594 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14595 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14596 if (max_cp < hwr.cp) 14597 return -ENOMEM; 14598 hwr.stat = hwr.cp; 14599 if (BNXT_NEW_RM(bp)) { 14600 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14601 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14602 hwr.grp = rx; 14603 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14604 } 14605 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14606 hwr.cp_p5 = hwr.tx + rx; 14607 rc = bnxt_hwrm_check_rings(bp, &hwr); 14608 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14609 if (!bnxt_ulp_registered(bp->edev)) { 14610 hwr.cp += bnxt_get_ulp_msix_num(bp); 14611 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14612 } 14613 if (hwr.cp > bp->total_irqs) { 14614 int total_msix = bnxt_change_msix(bp, hwr.cp); 14615 14616 if (total_msix < hwr.cp) { 14617 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14618 hwr.cp, total_msix); 14619 rc = -ENOSPC; 14620 } 14621 } 14622 } 14623 return rc; 14624 } 14625 14626 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14627 { 14628 if (bp->bar2) { 14629 pci_iounmap(pdev, bp->bar2); 14630 bp->bar2 = NULL; 14631 } 14632 14633 if (bp->bar1) { 14634 pci_iounmap(pdev, bp->bar1); 14635 bp->bar1 = NULL; 14636 } 14637 14638 if (bp->bar0) { 14639 pci_iounmap(pdev, bp->bar0); 14640 bp->bar0 = NULL; 14641 } 14642 } 14643 14644 static void bnxt_cleanup_pci(struct bnxt *bp) 14645 { 14646 bnxt_unmap_bars(bp, bp->pdev); 14647 pci_release_regions(bp->pdev); 14648 if (pci_is_enabled(bp->pdev)) 14649 pci_disable_device(bp->pdev); 14650 } 14651 14652 static void bnxt_init_dflt_coal(struct bnxt *bp) 14653 { 14654 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14655 struct bnxt_coal *coal; 14656 u16 flags = 0; 14657 14658 if (coal_cap->cmpl_params & 14659 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14660 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14661 14662 /* Tick values in micro seconds. 14663 * 1 coal_buf x bufs_per_record = 1 completion record. 14664 */ 14665 coal = &bp->rx_coal; 14666 coal->coal_ticks = 10; 14667 coal->coal_bufs = 30; 14668 coal->coal_ticks_irq = 1; 14669 coal->coal_bufs_irq = 2; 14670 coal->idle_thresh = 50; 14671 coal->bufs_per_record = 2; 14672 coal->budget = 64; /* NAPI budget */ 14673 coal->flags = flags; 14674 14675 coal = &bp->tx_coal; 14676 coal->coal_ticks = 28; 14677 coal->coal_bufs = 30; 14678 coal->coal_ticks_irq = 2; 14679 coal->coal_bufs_irq = 2; 14680 coal->bufs_per_record = 1; 14681 coal->flags = flags; 14682 14683 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14684 } 14685 14686 /* FW that pre-reserves 1 VNIC per function */ 14687 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14688 { 14689 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14690 14691 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14692 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14693 return true; 14694 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14695 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14696 return true; 14697 return false; 14698 } 14699 14700 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14701 { 14702 int rc; 14703 14704 bp->fw_cap = 0; 14705 rc = bnxt_hwrm_ver_get(bp); 14706 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14707 * so wait before continuing with recovery. 14708 */ 14709 if (rc) 14710 msleep(100); 14711 bnxt_try_map_fw_health_reg(bp); 14712 if (rc) { 14713 rc = bnxt_try_recover_fw(bp); 14714 if (rc) 14715 return rc; 14716 rc = bnxt_hwrm_ver_get(bp); 14717 if (rc) 14718 return rc; 14719 } 14720 14721 bnxt_nvm_cfg_ver_get(bp); 14722 14723 rc = bnxt_hwrm_func_reset(bp); 14724 if (rc) 14725 return -ENODEV; 14726 14727 bnxt_hwrm_fw_set_time(bp); 14728 return 0; 14729 } 14730 14731 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14732 { 14733 int rc; 14734 14735 /* Get the MAX capabilities for this function */ 14736 rc = bnxt_hwrm_func_qcaps(bp); 14737 if (rc) { 14738 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14739 rc); 14740 return -ENODEV; 14741 } 14742 14743 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14744 if (rc) 14745 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14746 rc); 14747 14748 if (bnxt_alloc_fw_health(bp)) { 14749 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14750 } else { 14751 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14752 if (rc) 14753 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14754 rc); 14755 } 14756 14757 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14758 if (rc) 14759 return -ENODEV; 14760 14761 rc = bnxt_alloc_crash_dump_mem(bp); 14762 if (rc) 14763 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14764 rc); 14765 if (!rc) { 14766 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14767 if (rc) { 14768 bnxt_free_crash_dump_mem(bp); 14769 netdev_warn(bp->dev, 14770 "hwrm crash dump mem failure rc: %d\n", rc); 14771 } 14772 } 14773 14774 if (bnxt_fw_pre_resv_vnics(bp)) 14775 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14776 14777 bnxt_hwrm_func_qcfg(bp); 14778 bnxt_hwrm_vnic_qcaps(bp); 14779 bnxt_hwrm_port_led_qcaps(bp); 14780 bnxt_ethtool_init(bp); 14781 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14782 __bnxt_hwrm_ptp_qcfg(bp); 14783 bnxt_dcb_init(bp); 14784 bnxt_hwmon_init(bp); 14785 return 0; 14786 } 14787 14788 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14789 { 14790 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14791 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14792 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14793 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14794 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14795 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14796 bp->rss_hash_delta = bp->rss_hash_cfg; 14797 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14798 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14799 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14800 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14801 } 14802 } 14803 14804 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14805 { 14806 struct net_device *dev = bp->dev; 14807 14808 dev->hw_features &= ~NETIF_F_NTUPLE; 14809 dev->features &= ~NETIF_F_NTUPLE; 14810 bp->flags &= ~BNXT_FLAG_RFS; 14811 if (bnxt_rfs_supported(bp)) { 14812 dev->hw_features |= NETIF_F_NTUPLE; 14813 if (bnxt_rfs_capable(bp, false)) { 14814 bp->flags |= BNXT_FLAG_RFS; 14815 dev->features |= NETIF_F_NTUPLE; 14816 } 14817 } 14818 } 14819 14820 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14821 { 14822 struct pci_dev *pdev = bp->pdev; 14823 14824 bnxt_set_dflt_rss_hash_type(bp); 14825 bnxt_set_dflt_rfs(bp); 14826 14827 bnxt_get_wol_settings(bp); 14828 if (bp->flags & BNXT_FLAG_WOL_CAP) 14829 device_set_wakeup_enable(&pdev->dev, bp->wol); 14830 else 14831 device_set_wakeup_capable(&pdev->dev, false); 14832 14833 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14834 bnxt_hwrm_coal_params_qcaps(bp); 14835 } 14836 14837 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14838 14839 int bnxt_fw_init_one(struct bnxt *bp) 14840 { 14841 int rc; 14842 14843 rc = bnxt_fw_init_one_p1(bp); 14844 if (rc) { 14845 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14846 return rc; 14847 } 14848 rc = bnxt_fw_init_one_p2(bp); 14849 if (rc) { 14850 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14851 return rc; 14852 } 14853 rc = bnxt_probe_phy(bp, false); 14854 if (rc) 14855 return rc; 14856 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14857 if (rc) 14858 return rc; 14859 14860 bnxt_fw_init_one_p3(bp); 14861 return 0; 14862 } 14863 14864 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14865 { 14866 struct bnxt_fw_health *fw_health = bp->fw_health; 14867 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14868 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14869 u32 reg_type, reg_off, delay_msecs; 14870 14871 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14872 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14873 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14874 switch (reg_type) { 14875 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14876 pci_write_config_dword(bp->pdev, reg_off, val); 14877 break; 14878 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14879 writel(reg_off & BNXT_GRC_BASE_MASK, 14880 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14881 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14882 fallthrough; 14883 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14884 writel(val, bp->bar0 + reg_off); 14885 break; 14886 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14887 writel(val, bp->bar1 + reg_off); 14888 break; 14889 } 14890 if (delay_msecs) { 14891 pci_read_config_dword(bp->pdev, 0, &val); 14892 msleep(delay_msecs); 14893 } 14894 } 14895 14896 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14897 { 14898 struct hwrm_func_qcfg_output *resp; 14899 struct hwrm_func_qcfg_input *req; 14900 bool result = true; /* firmware will enforce if unknown */ 14901 14902 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14903 return result; 14904 14905 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14906 return result; 14907 14908 req->fid = cpu_to_le16(0xffff); 14909 resp = hwrm_req_hold(bp, req); 14910 if (!hwrm_req_send(bp, req)) 14911 result = !!(le16_to_cpu(resp->flags) & 14912 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14913 hwrm_req_drop(bp, req); 14914 return result; 14915 } 14916 14917 static void bnxt_reset_all(struct bnxt *bp) 14918 { 14919 struct bnxt_fw_health *fw_health = bp->fw_health; 14920 int i, rc; 14921 14922 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14923 bnxt_fw_reset_via_optee(bp); 14924 bp->fw_reset_timestamp = jiffies; 14925 return; 14926 } 14927 14928 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14929 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14930 bnxt_fw_reset_writel(bp, i); 14931 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14932 struct hwrm_fw_reset_input *req; 14933 14934 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14935 if (!rc) { 14936 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14937 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14938 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14939 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14940 rc = hwrm_req_send(bp, req); 14941 } 14942 if (rc != -ENODEV) 14943 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14944 } 14945 bp->fw_reset_timestamp = jiffies; 14946 } 14947 14948 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14949 { 14950 return time_after(jiffies, bp->fw_reset_timestamp + 14951 (bp->fw_reset_max_dsecs * HZ / 10)); 14952 } 14953 14954 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14955 { 14956 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14957 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14958 bnxt_dl_health_fw_status_update(bp, false); 14959 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 14960 netif_close(bp->dev); 14961 } 14962 14963 static void bnxt_fw_reset_task(struct work_struct *work) 14964 { 14965 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14966 int rc = 0; 14967 14968 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14969 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14970 return; 14971 } 14972 14973 switch (bp->fw_reset_state) { 14974 case BNXT_FW_RESET_STATE_POLL_VF: { 14975 int n = bnxt_get_registered_vfs(bp); 14976 int tmo; 14977 14978 if (n < 0) { 14979 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14980 n, jiffies_to_msecs(jiffies - 14981 bp->fw_reset_timestamp)); 14982 goto fw_reset_abort; 14983 } else if (n > 0) { 14984 if (bnxt_fw_reset_timeout(bp)) { 14985 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14986 bp->fw_reset_state = 0; 14987 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14988 n); 14989 goto ulp_start; 14990 } 14991 bnxt_queue_fw_reset_work(bp, HZ / 10); 14992 return; 14993 } 14994 bp->fw_reset_timestamp = jiffies; 14995 netdev_lock(bp->dev); 14996 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 14997 bnxt_fw_reset_abort(bp, rc); 14998 netdev_unlock(bp->dev); 14999 goto ulp_start; 15000 } 15001 bnxt_fw_reset_close(bp); 15002 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15003 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15004 tmo = HZ / 10; 15005 } else { 15006 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15007 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15008 } 15009 netdev_unlock(bp->dev); 15010 bnxt_queue_fw_reset_work(bp, tmo); 15011 return; 15012 } 15013 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15014 u32 val; 15015 15016 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15017 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15018 !bnxt_fw_reset_timeout(bp)) { 15019 bnxt_queue_fw_reset_work(bp, HZ / 5); 15020 return; 15021 } 15022 15023 if (!bp->fw_health->primary) { 15024 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15025 15026 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15027 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15028 return; 15029 } 15030 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15031 } 15032 fallthrough; 15033 case BNXT_FW_RESET_STATE_RESET_FW: 15034 bnxt_reset_all(bp); 15035 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15036 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15037 return; 15038 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15039 bnxt_inv_fw_health_reg(bp); 15040 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15041 !bp->fw_reset_min_dsecs) { 15042 u16 val; 15043 15044 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15045 if (val == 0xffff) { 15046 if (bnxt_fw_reset_timeout(bp)) { 15047 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15048 rc = -ETIMEDOUT; 15049 goto fw_reset_abort; 15050 } 15051 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15052 return; 15053 } 15054 } 15055 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15056 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15057 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15058 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15059 bnxt_dl_remote_reload(bp); 15060 if (pci_enable_device(bp->pdev)) { 15061 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15062 rc = -ENODEV; 15063 goto fw_reset_abort; 15064 } 15065 pci_set_master(bp->pdev); 15066 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15067 fallthrough; 15068 case BNXT_FW_RESET_STATE_POLL_FW: 15069 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15070 rc = bnxt_hwrm_poll(bp); 15071 if (rc) { 15072 if (bnxt_fw_reset_timeout(bp)) { 15073 netdev_err(bp->dev, "Firmware reset aborted\n"); 15074 goto fw_reset_abort_status; 15075 } 15076 bnxt_queue_fw_reset_work(bp, HZ / 5); 15077 return; 15078 } 15079 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15080 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15081 fallthrough; 15082 case BNXT_FW_RESET_STATE_OPENING: 15083 while (!netdev_trylock(bp->dev)) { 15084 bnxt_queue_fw_reset_work(bp, HZ / 10); 15085 return; 15086 } 15087 rc = bnxt_open(bp->dev); 15088 if (rc) { 15089 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15090 bnxt_fw_reset_abort(bp, rc); 15091 netdev_unlock(bp->dev); 15092 goto ulp_start; 15093 } 15094 15095 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15096 bp->fw_health->enabled) { 15097 bp->fw_health->last_fw_reset_cnt = 15098 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15099 } 15100 bp->fw_reset_state = 0; 15101 /* Make sure fw_reset_state is 0 before clearing the flag */ 15102 smp_mb__before_atomic(); 15103 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15104 bnxt_ptp_reapply_pps(bp); 15105 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15106 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15107 bnxt_dl_health_fw_recovery_done(bp); 15108 bnxt_dl_health_fw_status_update(bp, true); 15109 } 15110 netdev_unlock(bp->dev); 15111 bnxt_ulp_start(bp, 0); 15112 bnxt_reenable_sriov(bp); 15113 netdev_lock(bp->dev); 15114 bnxt_vf_reps_alloc(bp); 15115 bnxt_vf_reps_open(bp); 15116 netdev_unlock(bp->dev); 15117 break; 15118 } 15119 return; 15120 15121 fw_reset_abort_status: 15122 if (bp->fw_health->status_reliable || 15123 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15124 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15125 15126 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15127 } 15128 fw_reset_abort: 15129 netdev_lock(bp->dev); 15130 bnxt_fw_reset_abort(bp, rc); 15131 netdev_unlock(bp->dev); 15132 ulp_start: 15133 bnxt_ulp_start(bp, rc); 15134 } 15135 15136 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15137 { 15138 int rc; 15139 struct bnxt *bp = netdev_priv(dev); 15140 15141 SET_NETDEV_DEV(dev, &pdev->dev); 15142 15143 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15144 rc = pci_enable_device(pdev); 15145 if (rc) { 15146 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15147 goto init_err; 15148 } 15149 15150 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15151 dev_err(&pdev->dev, 15152 "Cannot find PCI device base address, aborting\n"); 15153 rc = -ENODEV; 15154 goto init_err_disable; 15155 } 15156 15157 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15158 if (rc) { 15159 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15160 goto init_err_disable; 15161 } 15162 15163 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15164 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15165 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15166 rc = -EIO; 15167 goto init_err_release; 15168 } 15169 15170 pci_set_master(pdev); 15171 15172 bp->dev = dev; 15173 bp->pdev = pdev; 15174 15175 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15176 * determines the BAR size. 15177 */ 15178 bp->bar0 = pci_ioremap_bar(pdev, 0); 15179 if (!bp->bar0) { 15180 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15181 rc = -ENOMEM; 15182 goto init_err_release; 15183 } 15184 15185 bp->bar2 = pci_ioremap_bar(pdev, 4); 15186 if (!bp->bar2) { 15187 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15188 rc = -ENOMEM; 15189 goto init_err_release; 15190 } 15191 15192 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15193 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15194 15195 spin_lock_init(&bp->ntp_fltr_lock); 15196 #if BITS_PER_LONG == 32 15197 spin_lock_init(&bp->db_lock); 15198 #endif 15199 15200 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15201 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15202 15203 timer_setup(&bp->timer, bnxt_timer, 0); 15204 bp->current_interval = BNXT_TIMER_INTERVAL; 15205 15206 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15207 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15208 15209 clear_bit(BNXT_STATE_OPEN, &bp->state); 15210 return 0; 15211 15212 init_err_release: 15213 bnxt_unmap_bars(bp, pdev); 15214 pci_release_regions(pdev); 15215 15216 init_err_disable: 15217 pci_disable_device(pdev); 15218 15219 init_err: 15220 return rc; 15221 } 15222 15223 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15224 { 15225 struct sockaddr *addr = p; 15226 struct bnxt *bp = netdev_priv(dev); 15227 int rc = 0; 15228 15229 netdev_assert_locked(dev); 15230 15231 if (!is_valid_ether_addr(addr->sa_data)) 15232 return -EADDRNOTAVAIL; 15233 15234 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15235 return 0; 15236 15237 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15238 if (rc) 15239 return rc; 15240 15241 eth_hw_addr_set(dev, addr->sa_data); 15242 bnxt_clear_usr_fltrs(bp, true); 15243 if (netif_running(dev)) { 15244 bnxt_close_nic(bp, false, false); 15245 rc = bnxt_open_nic(bp, false, false); 15246 } 15247 15248 return rc; 15249 } 15250 15251 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15252 { 15253 struct bnxt *bp = netdev_priv(dev); 15254 15255 netdev_assert_locked(dev); 15256 15257 if (netif_running(dev)) 15258 bnxt_close_nic(bp, true, false); 15259 15260 WRITE_ONCE(dev->mtu, new_mtu); 15261 15262 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15263 * program is attached. We need to set the AGG rings settings and 15264 * rx_skb_func accordingly. 15265 */ 15266 if (READ_ONCE(bp->xdp_prog)) 15267 bnxt_set_rx_skb_mode(bp, true); 15268 15269 bnxt_set_ring_params(bp); 15270 15271 if (netif_running(dev)) 15272 return bnxt_open_nic(bp, true, false); 15273 15274 return 0; 15275 } 15276 15277 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15278 { 15279 struct bnxt *bp = netdev_priv(dev); 15280 bool sh = false; 15281 int rc, tx_cp; 15282 15283 if (tc > bp->max_tc) { 15284 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15285 tc, bp->max_tc); 15286 return -EINVAL; 15287 } 15288 15289 if (bp->num_tc == tc) 15290 return 0; 15291 15292 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15293 sh = true; 15294 15295 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15296 sh, tc, bp->tx_nr_rings_xdp); 15297 if (rc) 15298 return rc; 15299 15300 /* Needs to close the device and do hw resource re-allocations */ 15301 if (netif_running(bp->dev)) 15302 bnxt_close_nic(bp, true, false); 15303 15304 if (tc) { 15305 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15306 netdev_set_num_tc(dev, tc); 15307 bp->num_tc = tc; 15308 } else { 15309 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15310 netdev_reset_tc(dev); 15311 bp->num_tc = 0; 15312 } 15313 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15314 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15315 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15316 tx_cp + bp->rx_nr_rings; 15317 15318 if (netif_running(bp->dev)) 15319 return bnxt_open_nic(bp, true, false); 15320 15321 return 0; 15322 } 15323 15324 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15325 void *cb_priv) 15326 { 15327 struct bnxt *bp = cb_priv; 15328 15329 if (!bnxt_tc_flower_enabled(bp) || 15330 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15331 return -EOPNOTSUPP; 15332 15333 switch (type) { 15334 case TC_SETUP_CLSFLOWER: 15335 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15336 default: 15337 return -EOPNOTSUPP; 15338 } 15339 } 15340 15341 LIST_HEAD(bnxt_block_cb_list); 15342 15343 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15344 void *type_data) 15345 { 15346 struct bnxt *bp = netdev_priv(dev); 15347 15348 switch (type) { 15349 case TC_SETUP_BLOCK: 15350 return flow_block_cb_setup_simple(type_data, 15351 &bnxt_block_cb_list, 15352 bnxt_setup_tc_block_cb, 15353 bp, bp, true); 15354 case TC_SETUP_QDISC_MQPRIO: { 15355 struct tc_mqprio_qopt *mqprio = type_data; 15356 15357 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15358 15359 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15360 } 15361 default: 15362 return -EOPNOTSUPP; 15363 } 15364 } 15365 15366 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15367 const struct sk_buff *skb) 15368 { 15369 struct bnxt_vnic_info *vnic; 15370 15371 if (skb) 15372 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15373 15374 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15375 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15376 } 15377 15378 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15379 u32 idx) 15380 { 15381 struct hlist_head *head; 15382 int bit_id; 15383 15384 spin_lock_bh(&bp->ntp_fltr_lock); 15385 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15386 if (bit_id < 0) { 15387 spin_unlock_bh(&bp->ntp_fltr_lock); 15388 return -ENOMEM; 15389 } 15390 15391 fltr->base.sw_id = (u16)bit_id; 15392 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15393 fltr->base.flags |= BNXT_ACT_RING_DST; 15394 head = &bp->ntp_fltr_hash_tbl[idx]; 15395 hlist_add_head_rcu(&fltr->base.hash, head); 15396 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15397 bnxt_insert_usr_fltr(bp, &fltr->base); 15398 bp->ntp_fltr_count++; 15399 spin_unlock_bh(&bp->ntp_fltr_lock); 15400 return 0; 15401 } 15402 15403 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15404 struct bnxt_ntuple_filter *f2) 15405 { 15406 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15407 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15408 struct flow_keys *keys1 = &f1->fkeys; 15409 struct flow_keys *keys2 = &f2->fkeys; 15410 15411 if (keys1->basic.n_proto != keys2->basic.n_proto || 15412 keys1->basic.ip_proto != keys2->basic.ip_proto) 15413 return false; 15414 15415 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15416 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15417 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15418 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15419 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15420 return false; 15421 } else { 15422 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15423 &keys2->addrs.v6addrs.src) || 15424 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15425 &masks2->addrs.v6addrs.src) || 15426 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15427 &keys2->addrs.v6addrs.dst) || 15428 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15429 &masks2->addrs.v6addrs.dst)) 15430 return false; 15431 } 15432 15433 return keys1->ports.src == keys2->ports.src && 15434 masks1->ports.src == masks2->ports.src && 15435 keys1->ports.dst == keys2->ports.dst && 15436 masks1->ports.dst == masks2->ports.dst && 15437 keys1->control.flags == keys2->control.flags && 15438 f1->l2_fltr == f2->l2_fltr; 15439 } 15440 15441 struct bnxt_ntuple_filter * 15442 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15443 struct bnxt_ntuple_filter *fltr, u32 idx) 15444 { 15445 struct bnxt_ntuple_filter *f; 15446 struct hlist_head *head; 15447 15448 head = &bp->ntp_fltr_hash_tbl[idx]; 15449 hlist_for_each_entry_rcu(f, head, base.hash) { 15450 if (bnxt_fltr_match(f, fltr)) 15451 return f; 15452 } 15453 return NULL; 15454 } 15455 15456 #ifdef CONFIG_RFS_ACCEL 15457 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15458 u16 rxq_index, u32 flow_id) 15459 { 15460 struct bnxt *bp = netdev_priv(dev); 15461 struct bnxt_ntuple_filter *fltr, *new_fltr; 15462 struct flow_keys *fkeys; 15463 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15464 struct bnxt_l2_filter *l2_fltr; 15465 int rc = 0, idx; 15466 u32 flags; 15467 15468 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15469 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15470 atomic_inc(&l2_fltr->refcnt); 15471 } else { 15472 struct bnxt_l2_key key; 15473 15474 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15475 key.vlan = 0; 15476 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15477 if (!l2_fltr) 15478 return -EINVAL; 15479 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15480 bnxt_del_l2_filter(bp, l2_fltr); 15481 return -EINVAL; 15482 } 15483 } 15484 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15485 if (!new_fltr) { 15486 bnxt_del_l2_filter(bp, l2_fltr); 15487 return -ENOMEM; 15488 } 15489 15490 fkeys = &new_fltr->fkeys; 15491 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15492 rc = -EPROTONOSUPPORT; 15493 goto err_free; 15494 } 15495 15496 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15497 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15498 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15499 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15500 rc = -EPROTONOSUPPORT; 15501 goto err_free; 15502 } 15503 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15504 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15505 if (bp->hwrm_spec_code < 0x10601) { 15506 rc = -EPROTONOSUPPORT; 15507 goto err_free; 15508 } 15509 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15510 } 15511 flags = fkeys->control.flags; 15512 if (((flags & FLOW_DIS_ENCAPSULATION) && 15513 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15514 rc = -EPROTONOSUPPORT; 15515 goto err_free; 15516 } 15517 new_fltr->l2_fltr = l2_fltr; 15518 15519 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15520 rcu_read_lock(); 15521 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15522 if (fltr) { 15523 rc = fltr->base.sw_id; 15524 rcu_read_unlock(); 15525 goto err_free; 15526 } 15527 rcu_read_unlock(); 15528 15529 new_fltr->flow_id = flow_id; 15530 new_fltr->base.rxq = rxq_index; 15531 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15532 if (!rc) { 15533 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15534 return new_fltr->base.sw_id; 15535 } 15536 15537 err_free: 15538 bnxt_del_l2_filter(bp, l2_fltr); 15539 kfree(new_fltr); 15540 return rc; 15541 } 15542 #endif 15543 15544 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15545 { 15546 spin_lock_bh(&bp->ntp_fltr_lock); 15547 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15548 spin_unlock_bh(&bp->ntp_fltr_lock); 15549 return; 15550 } 15551 hlist_del_rcu(&fltr->base.hash); 15552 bnxt_del_one_usr_fltr(bp, &fltr->base); 15553 bp->ntp_fltr_count--; 15554 spin_unlock_bh(&bp->ntp_fltr_lock); 15555 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15556 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15557 kfree_rcu(fltr, base.rcu); 15558 } 15559 15560 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15561 { 15562 #ifdef CONFIG_RFS_ACCEL 15563 int i; 15564 15565 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15566 struct hlist_head *head; 15567 struct hlist_node *tmp; 15568 struct bnxt_ntuple_filter *fltr; 15569 int rc; 15570 15571 head = &bp->ntp_fltr_hash_tbl[i]; 15572 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15573 bool del = false; 15574 15575 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15576 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15577 continue; 15578 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15579 fltr->flow_id, 15580 fltr->base.sw_id)) { 15581 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15582 fltr); 15583 del = true; 15584 } 15585 } else { 15586 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15587 fltr); 15588 if (rc) 15589 del = true; 15590 else 15591 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15592 } 15593 15594 if (del) 15595 bnxt_del_ntp_filter(bp, fltr); 15596 } 15597 } 15598 #endif 15599 } 15600 15601 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15602 unsigned int entry, struct udp_tunnel_info *ti) 15603 { 15604 struct bnxt *bp = netdev_priv(netdev); 15605 unsigned int cmd; 15606 15607 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15608 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15609 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15610 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15611 else 15612 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15613 15614 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15615 } 15616 15617 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15618 unsigned int entry, struct udp_tunnel_info *ti) 15619 { 15620 struct bnxt *bp = netdev_priv(netdev); 15621 unsigned int cmd; 15622 15623 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15624 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15625 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15626 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15627 else 15628 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15629 15630 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15631 } 15632 15633 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15634 .set_port = bnxt_udp_tunnel_set_port, 15635 .unset_port = bnxt_udp_tunnel_unset_port, 15636 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15637 .tables = { 15638 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15639 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15640 }, 15641 }, bnxt_udp_tunnels_p7 = { 15642 .set_port = bnxt_udp_tunnel_set_port, 15643 .unset_port = bnxt_udp_tunnel_unset_port, 15644 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15645 .tables = { 15646 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15647 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15648 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15649 }, 15650 }; 15651 15652 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15653 struct net_device *dev, u32 filter_mask, 15654 int nlflags) 15655 { 15656 struct bnxt *bp = netdev_priv(dev); 15657 15658 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15659 nlflags, filter_mask, NULL); 15660 } 15661 15662 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15663 u16 flags, struct netlink_ext_ack *extack) 15664 { 15665 struct bnxt *bp = netdev_priv(dev); 15666 struct nlattr *attr, *br_spec; 15667 int rem, rc = 0; 15668 15669 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15670 return -EOPNOTSUPP; 15671 15672 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15673 if (!br_spec) 15674 return -EINVAL; 15675 15676 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15677 u16 mode; 15678 15679 mode = nla_get_u16(attr); 15680 if (mode == bp->br_mode) 15681 break; 15682 15683 rc = bnxt_hwrm_set_br_mode(bp, mode); 15684 if (!rc) 15685 bp->br_mode = mode; 15686 break; 15687 } 15688 return rc; 15689 } 15690 15691 int bnxt_get_port_parent_id(struct net_device *dev, 15692 struct netdev_phys_item_id *ppid) 15693 { 15694 struct bnxt *bp = netdev_priv(dev); 15695 15696 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15697 return -EOPNOTSUPP; 15698 15699 /* The PF and it's VF-reps only support the switchdev framework */ 15700 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15701 return -EOPNOTSUPP; 15702 15703 ppid->id_len = sizeof(bp->dsn); 15704 memcpy(ppid->id, bp->dsn, ppid->id_len); 15705 15706 return 0; 15707 } 15708 15709 static const struct net_device_ops bnxt_netdev_ops = { 15710 .ndo_open = bnxt_open, 15711 .ndo_start_xmit = bnxt_start_xmit, 15712 .ndo_stop = bnxt_close, 15713 .ndo_get_stats64 = bnxt_get_stats64, 15714 .ndo_set_rx_mode = bnxt_set_rx_mode, 15715 .ndo_eth_ioctl = bnxt_ioctl, 15716 .ndo_validate_addr = eth_validate_addr, 15717 .ndo_set_mac_address = bnxt_change_mac_addr, 15718 .ndo_change_mtu = bnxt_change_mtu, 15719 .ndo_fix_features = bnxt_fix_features, 15720 .ndo_set_features = bnxt_set_features, 15721 .ndo_features_check = bnxt_features_check, 15722 .ndo_tx_timeout = bnxt_tx_timeout, 15723 #ifdef CONFIG_BNXT_SRIOV 15724 .ndo_get_vf_config = bnxt_get_vf_config, 15725 .ndo_set_vf_mac = bnxt_set_vf_mac, 15726 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15727 .ndo_set_vf_rate = bnxt_set_vf_bw, 15728 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15729 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15730 .ndo_set_vf_trust = bnxt_set_vf_trust, 15731 #endif 15732 .ndo_setup_tc = bnxt_setup_tc, 15733 #ifdef CONFIG_RFS_ACCEL 15734 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15735 #endif 15736 .ndo_bpf = bnxt_xdp, 15737 .ndo_xdp_xmit = bnxt_xdp_xmit, 15738 .ndo_bridge_getlink = bnxt_bridge_getlink, 15739 .ndo_bridge_setlink = bnxt_bridge_setlink, 15740 }; 15741 15742 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15743 struct netdev_queue_stats_rx *stats) 15744 { 15745 struct bnxt *bp = netdev_priv(dev); 15746 struct bnxt_cp_ring_info *cpr; 15747 u64 *sw; 15748 15749 if (!bp->bnapi) 15750 return; 15751 15752 cpr = &bp->bnapi[i]->cp_ring; 15753 sw = cpr->stats.sw_stats; 15754 15755 stats->packets = 0; 15756 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15757 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15758 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15759 15760 stats->bytes = 0; 15761 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15762 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15763 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15764 15765 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15766 } 15767 15768 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15769 struct netdev_queue_stats_tx *stats) 15770 { 15771 struct bnxt *bp = netdev_priv(dev); 15772 struct bnxt_napi *bnapi; 15773 u64 *sw; 15774 15775 if (!bp->tx_ring) 15776 return; 15777 15778 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15779 sw = bnapi->cp_ring.stats.sw_stats; 15780 15781 stats->packets = 0; 15782 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15783 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15784 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15785 15786 stats->bytes = 0; 15787 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15788 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15789 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15790 } 15791 15792 static void bnxt_get_base_stats(struct net_device *dev, 15793 struct netdev_queue_stats_rx *rx, 15794 struct netdev_queue_stats_tx *tx) 15795 { 15796 struct bnxt *bp = netdev_priv(dev); 15797 15798 rx->packets = bp->net_stats_prev.rx_packets; 15799 rx->bytes = bp->net_stats_prev.rx_bytes; 15800 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15801 15802 tx->packets = bp->net_stats_prev.tx_packets; 15803 tx->bytes = bp->net_stats_prev.tx_bytes; 15804 } 15805 15806 static const struct netdev_stat_ops bnxt_stat_ops = { 15807 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15808 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15809 .get_base_stats = bnxt_get_base_stats, 15810 }; 15811 15812 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15813 { 15814 struct bnxt_rx_ring_info *rxr, *clone; 15815 struct bnxt *bp = netdev_priv(dev); 15816 struct bnxt_ring_struct *ring; 15817 int rc; 15818 15819 if (!bp->rx_ring) 15820 return -ENETDOWN; 15821 15822 rxr = &bp->rx_ring[idx]; 15823 clone = qmem; 15824 memcpy(clone, rxr, sizeof(*rxr)); 15825 bnxt_init_rx_ring_struct(bp, clone); 15826 bnxt_reset_rx_ring_struct(bp, clone); 15827 15828 clone->rx_prod = 0; 15829 clone->rx_agg_prod = 0; 15830 clone->rx_sw_agg_prod = 0; 15831 clone->rx_next_cons = 0; 15832 clone->need_head_pool = false; 15833 15834 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15835 if (rc) 15836 return rc; 15837 15838 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15839 if (rc < 0) 15840 goto err_page_pool_destroy; 15841 15842 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15843 MEM_TYPE_PAGE_POOL, 15844 clone->page_pool); 15845 if (rc) 15846 goto err_rxq_info_unreg; 15847 15848 ring = &clone->rx_ring_struct; 15849 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15850 if (rc) 15851 goto err_free_rx_ring; 15852 15853 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15854 ring = &clone->rx_agg_ring_struct; 15855 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15856 if (rc) 15857 goto err_free_rx_agg_ring; 15858 15859 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15860 if (rc) 15861 goto err_free_rx_agg_ring; 15862 } 15863 15864 if (bp->flags & BNXT_FLAG_TPA) { 15865 rc = bnxt_alloc_one_tpa_info(bp, clone); 15866 if (rc) 15867 goto err_free_tpa_info; 15868 } 15869 15870 bnxt_init_one_rx_ring_rxbd(bp, clone); 15871 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15872 15873 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15874 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15875 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 15876 if (bp->flags & BNXT_FLAG_TPA) 15877 bnxt_alloc_one_tpa_info_data(bp, clone); 15878 15879 return 0; 15880 15881 err_free_tpa_info: 15882 bnxt_free_one_tpa_info(bp, clone); 15883 err_free_rx_agg_ring: 15884 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15885 err_free_rx_ring: 15886 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15887 err_rxq_info_unreg: 15888 xdp_rxq_info_unreg(&clone->xdp_rxq); 15889 err_page_pool_destroy: 15890 page_pool_destroy(clone->page_pool); 15891 if (bnxt_separate_head_pool(clone)) 15892 page_pool_destroy(clone->head_pool); 15893 clone->page_pool = NULL; 15894 clone->head_pool = NULL; 15895 return rc; 15896 } 15897 15898 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15899 { 15900 struct bnxt_rx_ring_info *rxr = qmem; 15901 struct bnxt *bp = netdev_priv(dev); 15902 struct bnxt_ring_struct *ring; 15903 15904 bnxt_free_one_rx_ring_skbs(bp, rxr); 15905 bnxt_free_one_tpa_info(bp, rxr); 15906 15907 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15908 15909 page_pool_destroy(rxr->page_pool); 15910 if (bnxt_separate_head_pool(rxr)) 15911 page_pool_destroy(rxr->head_pool); 15912 rxr->page_pool = NULL; 15913 rxr->head_pool = NULL; 15914 15915 ring = &rxr->rx_ring_struct; 15916 bnxt_free_ring(bp, &ring->ring_mem); 15917 15918 ring = &rxr->rx_agg_ring_struct; 15919 bnxt_free_ring(bp, &ring->ring_mem); 15920 15921 kfree(rxr->rx_agg_bmap); 15922 rxr->rx_agg_bmap = NULL; 15923 } 15924 15925 static void bnxt_copy_rx_ring(struct bnxt *bp, 15926 struct bnxt_rx_ring_info *dst, 15927 struct bnxt_rx_ring_info *src) 15928 { 15929 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15930 struct bnxt_ring_struct *dst_ring, *src_ring; 15931 int i; 15932 15933 dst_ring = &dst->rx_ring_struct; 15934 dst_rmem = &dst_ring->ring_mem; 15935 src_ring = &src->rx_ring_struct; 15936 src_rmem = &src_ring->ring_mem; 15937 15938 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15939 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15940 WARN_ON(dst_rmem->flags != src_rmem->flags); 15941 WARN_ON(dst_rmem->depth != src_rmem->depth); 15942 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15943 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15944 15945 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15946 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15947 *dst_rmem->vmem = *src_rmem->vmem; 15948 for (i = 0; i < dst_rmem->nr_pages; i++) { 15949 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15950 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15951 } 15952 15953 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15954 return; 15955 15956 dst_ring = &dst->rx_agg_ring_struct; 15957 dst_rmem = &dst_ring->ring_mem; 15958 src_ring = &src->rx_agg_ring_struct; 15959 src_rmem = &src_ring->ring_mem; 15960 15961 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15962 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15963 WARN_ON(dst_rmem->flags != src_rmem->flags); 15964 WARN_ON(dst_rmem->depth != src_rmem->depth); 15965 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15966 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15967 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15968 15969 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15970 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15971 *dst_rmem->vmem = *src_rmem->vmem; 15972 for (i = 0; i < dst_rmem->nr_pages; i++) { 15973 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15974 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15975 } 15976 15977 dst->rx_agg_bmap = src->rx_agg_bmap; 15978 } 15979 15980 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15981 { 15982 struct bnxt *bp = netdev_priv(dev); 15983 struct bnxt_rx_ring_info *rxr, *clone; 15984 struct bnxt_cp_ring_info *cpr; 15985 struct bnxt_vnic_info *vnic; 15986 struct bnxt_napi *bnapi; 15987 int i, rc; 15988 u16 mru; 15989 15990 rxr = &bp->rx_ring[idx]; 15991 clone = qmem; 15992 15993 rxr->rx_prod = clone->rx_prod; 15994 rxr->rx_agg_prod = clone->rx_agg_prod; 15995 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 15996 rxr->rx_next_cons = clone->rx_next_cons; 15997 rxr->rx_tpa = clone->rx_tpa; 15998 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 15999 rxr->page_pool = clone->page_pool; 16000 rxr->head_pool = clone->head_pool; 16001 rxr->xdp_rxq = clone->xdp_rxq; 16002 rxr->need_head_pool = clone->need_head_pool; 16003 16004 bnxt_copy_rx_ring(bp, rxr, clone); 16005 16006 bnapi = rxr->bnapi; 16007 cpr = &bnapi->cp_ring; 16008 16009 /* All rings have been reserved and previously allocated. 16010 * Reallocating with the same parameters should never fail. 16011 */ 16012 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16013 if (rc) 16014 goto err_reset; 16015 16016 if (bp->tph_mode) { 16017 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16018 if (rc) 16019 goto err_reset; 16020 } 16021 16022 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16023 if (rc) 16024 goto err_reset; 16025 16026 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16027 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16028 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16029 16030 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16031 rc = bnxt_tx_queue_start(bp, idx); 16032 if (rc) 16033 goto err_reset; 16034 } 16035 16036 napi_enable_locked(&bnapi->napi); 16037 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16038 16039 mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 16040 for (i = 0; i < bp->nr_vnics; i++) { 16041 vnic = &bp->vnic_info[i]; 16042 16043 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16044 if (rc) 16045 return rc; 16046 } 16047 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16048 16049 err_reset: 16050 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16051 rc); 16052 napi_enable_locked(&bnapi->napi); 16053 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16054 bnxt_reset_task(bp, true); 16055 return rc; 16056 } 16057 16058 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16059 { 16060 struct bnxt *bp = netdev_priv(dev); 16061 struct bnxt_rx_ring_info *rxr; 16062 struct bnxt_cp_ring_info *cpr; 16063 struct bnxt_vnic_info *vnic; 16064 struct bnxt_napi *bnapi; 16065 int i; 16066 16067 for (i = 0; i < bp->nr_vnics; i++) { 16068 vnic = &bp->vnic_info[i]; 16069 16070 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16071 } 16072 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16073 /* Make sure NAPI sees that the VNIC is disabled */ 16074 synchronize_net(); 16075 rxr = &bp->rx_ring[idx]; 16076 bnapi = rxr->bnapi; 16077 cpr = &bnapi->cp_ring; 16078 cancel_work_sync(&cpr->dim.work); 16079 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16080 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16081 page_pool_disable_direct_recycling(rxr->page_pool); 16082 if (bnxt_separate_head_pool(rxr)) 16083 page_pool_disable_direct_recycling(rxr->head_pool); 16084 16085 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16086 bnxt_tx_queue_stop(bp, idx); 16087 16088 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16089 * completion is handled in NAPI to guarantee no more DMA on that ring 16090 * after seeing the completion. 16091 */ 16092 napi_disable_locked(&bnapi->napi); 16093 16094 if (bp->tph_mode) { 16095 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16096 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16097 } 16098 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16099 16100 memcpy(qmem, rxr, sizeof(*rxr)); 16101 bnxt_init_rx_ring_struct(bp, qmem); 16102 16103 return 0; 16104 } 16105 16106 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16107 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16108 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16109 .ndo_queue_mem_free = bnxt_queue_mem_free, 16110 .ndo_queue_start = bnxt_queue_start, 16111 .ndo_queue_stop = bnxt_queue_stop, 16112 }; 16113 16114 static void bnxt_remove_one(struct pci_dev *pdev) 16115 { 16116 struct net_device *dev = pci_get_drvdata(pdev); 16117 struct bnxt *bp = netdev_priv(dev); 16118 16119 if (BNXT_PF(bp)) 16120 bnxt_sriov_disable(bp); 16121 16122 bnxt_rdma_aux_device_del(bp); 16123 16124 unregister_netdev(dev); 16125 bnxt_ptp_clear(bp); 16126 16127 bnxt_rdma_aux_device_uninit(bp); 16128 16129 bnxt_free_l2_filters(bp, true); 16130 bnxt_free_ntp_fltrs(bp, true); 16131 WARN_ON(bp->num_rss_ctx); 16132 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16133 /* Flush any pending tasks */ 16134 cancel_work_sync(&bp->sp_task); 16135 cancel_delayed_work_sync(&bp->fw_reset_task); 16136 bp->sp_event = 0; 16137 16138 bnxt_dl_fw_reporters_destroy(bp); 16139 bnxt_dl_unregister(bp); 16140 bnxt_shutdown_tc(bp); 16141 16142 bnxt_clear_int_mode(bp); 16143 bnxt_hwrm_func_drv_unrgtr(bp); 16144 bnxt_free_hwrm_resources(bp); 16145 bnxt_hwmon_uninit(bp); 16146 bnxt_ethtool_free(bp); 16147 bnxt_dcb_free(bp); 16148 kfree(bp->ptp_cfg); 16149 bp->ptp_cfg = NULL; 16150 kfree(bp->fw_health); 16151 bp->fw_health = NULL; 16152 bnxt_cleanup_pci(bp); 16153 bnxt_free_ctx_mem(bp, true); 16154 bnxt_free_crash_dump_mem(bp); 16155 kfree(bp->rss_indir_tbl); 16156 bp->rss_indir_tbl = NULL; 16157 bnxt_free_port_stats(bp); 16158 free_netdev(dev); 16159 } 16160 16161 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16162 { 16163 int rc = 0; 16164 struct bnxt_link_info *link_info = &bp->link_info; 16165 16166 bp->phy_flags = 0; 16167 rc = bnxt_hwrm_phy_qcaps(bp); 16168 if (rc) { 16169 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16170 rc); 16171 return rc; 16172 } 16173 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16174 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16175 else 16176 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16177 16178 bp->mac_flags = 0; 16179 bnxt_hwrm_mac_qcaps(bp); 16180 16181 if (!fw_dflt) 16182 return 0; 16183 16184 mutex_lock(&bp->link_lock); 16185 rc = bnxt_update_link(bp, false); 16186 if (rc) { 16187 mutex_unlock(&bp->link_lock); 16188 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16189 rc); 16190 return rc; 16191 } 16192 16193 /* Older firmware does not have supported_auto_speeds, so assume 16194 * that all supported speeds can be autonegotiated. 16195 */ 16196 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16197 link_info->support_auto_speeds = link_info->support_speeds; 16198 16199 bnxt_init_ethtool_link_settings(bp); 16200 mutex_unlock(&bp->link_lock); 16201 return 0; 16202 } 16203 16204 static int bnxt_get_max_irq(struct pci_dev *pdev) 16205 { 16206 u16 ctrl; 16207 16208 if (!pdev->msix_cap) 16209 return 1; 16210 16211 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16212 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16213 } 16214 16215 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16216 int *max_cp) 16217 { 16218 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16219 int max_ring_grps = 0, max_irq; 16220 16221 *max_tx = hw_resc->max_tx_rings; 16222 *max_rx = hw_resc->max_rx_rings; 16223 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16224 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16225 bnxt_get_ulp_msix_num_in_use(bp), 16226 hw_resc->max_stat_ctxs - 16227 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16228 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16229 *max_cp = min_t(int, *max_cp, max_irq); 16230 max_ring_grps = hw_resc->max_hw_ring_grps; 16231 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16232 *max_cp -= 1; 16233 *max_rx -= 2; 16234 } 16235 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16236 *max_rx >>= 1; 16237 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16238 int rc; 16239 16240 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16241 if (rc) { 16242 *max_rx = 0; 16243 *max_tx = 0; 16244 } 16245 /* On P5 chips, max_cp output param should be available NQs */ 16246 *max_cp = max_irq; 16247 } 16248 *max_rx = min_t(int, *max_rx, max_ring_grps); 16249 } 16250 16251 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16252 { 16253 int rx, tx, cp; 16254 16255 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16256 *max_rx = rx; 16257 *max_tx = tx; 16258 if (!rx || !tx || !cp) 16259 return -ENOMEM; 16260 16261 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16262 } 16263 16264 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16265 bool shared) 16266 { 16267 int rc; 16268 16269 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16270 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16271 /* Not enough rings, try disabling agg rings. */ 16272 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16273 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16274 if (rc) { 16275 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16276 bp->flags |= BNXT_FLAG_AGG_RINGS; 16277 return rc; 16278 } 16279 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16280 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16281 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16282 bnxt_set_ring_params(bp); 16283 } 16284 16285 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16286 int max_cp, max_stat, max_irq; 16287 16288 /* Reserve minimum resources for RoCE */ 16289 max_cp = bnxt_get_max_func_cp_rings(bp); 16290 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16291 max_irq = bnxt_get_max_func_irqs(bp); 16292 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16293 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16294 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16295 return 0; 16296 16297 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16298 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16299 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16300 max_cp = min_t(int, max_cp, max_irq); 16301 max_cp = min_t(int, max_cp, max_stat); 16302 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16303 if (rc) 16304 rc = 0; 16305 } 16306 return rc; 16307 } 16308 16309 /* In initial default shared ring setting, each shared ring must have a 16310 * RX/TX ring pair. 16311 */ 16312 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16313 { 16314 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16315 bp->rx_nr_rings = bp->cp_nr_rings; 16316 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16317 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16318 } 16319 16320 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16321 { 16322 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16323 int avail_msix; 16324 16325 if (!bnxt_can_reserve_rings(bp)) 16326 return 0; 16327 16328 if (sh) 16329 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16330 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16331 /* Reduce default rings on multi-port cards so that total default 16332 * rings do not exceed CPU count. 16333 */ 16334 if (bp->port_count > 1) { 16335 int max_rings = 16336 max_t(int, num_online_cpus() / bp->port_count, 1); 16337 16338 dflt_rings = min_t(int, dflt_rings, max_rings); 16339 } 16340 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16341 if (rc) 16342 return rc; 16343 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16344 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16345 if (sh) 16346 bnxt_trim_dflt_sh_rings(bp); 16347 else 16348 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16349 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 16350 16351 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16352 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16353 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16354 16355 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16356 bnxt_set_dflt_ulp_stat_ctxs(bp); 16357 } 16358 16359 rc = __bnxt_reserve_rings(bp); 16360 if (rc && rc != -ENODEV) 16361 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16362 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16363 if (sh) 16364 bnxt_trim_dflt_sh_rings(bp); 16365 16366 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16367 if (bnxt_need_reserve_rings(bp)) { 16368 rc = __bnxt_reserve_rings(bp); 16369 if (rc && rc != -ENODEV) 16370 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16371 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16372 } 16373 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16374 bp->rx_nr_rings++; 16375 bp->cp_nr_rings++; 16376 } 16377 if (rc) { 16378 bp->tx_nr_rings = 0; 16379 bp->rx_nr_rings = 0; 16380 } 16381 return rc; 16382 } 16383 16384 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16385 { 16386 int rc; 16387 16388 if (bp->tx_nr_rings) 16389 return 0; 16390 16391 bnxt_ulp_irq_stop(bp); 16392 bnxt_clear_int_mode(bp); 16393 rc = bnxt_set_dflt_rings(bp, true); 16394 if (rc) { 16395 if (BNXT_VF(bp) && rc == -ENODEV) 16396 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16397 else 16398 netdev_err(bp->dev, "Not enough rings available.\n"); 16399 goto init_dflt_ring_err; 16400 } 16401 rc = bnxt_init_int_mode(bp); 16402 if (rc) 16403 goto init_dflt_ring_err; 16404 16405 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16406 16407 bnxt_set_dflt_rfs(bp); 16408 16409 init_dflt_ring_err: 16410 bnxt_ulp_irq_restart(bp, rc); 16411 return rc; 16412 } 16413 16414 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16415 { 16416 int rc; 16417 16418 netdev_ops_assert_locked(bp->dev); 16419 bnxt_hwrm_func_qcaps(bp); 16420 16421 if (netif_running(bp->dev)) 16422 __bnxt_close_nic(bp, true, false); 16423 16424 bnxt_ulp_irq_stop(bp); 16425 bnxt_clear_int_mode(bp); 16426 rc = bnxt_init_int_mode(bp); 16427 bnxt_ulp_irq_restart(bp, rc); 16428 16429 if (netif_running(bp->dev)) { 16430 if (rc) 16431 netif_close(bp->dev); 16432 else 16433 rc = bnxt_open_nic(bp, true, false); 16434 } 16435 16436 return rc; 16437 } 16438 16439 static int bnxt_init_mac_addr(struct bnxt *bp) 16440 { 16441 int rc = 0; 16442 16443 if (BNXT_PF(bp)) { 16444 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 16445 } else { 16446 #ifdef CONFIG_BNXT_SRIOV 16447 struct bnxt_vf_info *vf = &bp->vf; 16448 bool strict_approval = true; 16449 16450 if (is_valid_ether_addr(vf->mac_addr)) { 16451 /* overwrite netdev dev_addr with admin VF MAC */ 16452 eth_hw_addr_set(bp->dev, vf->mac_addr); 16453 /* Older PF driver or firmware may not approve this 16454 * correctly. 16455 */ 16456 strict_approval = false; 16457 } else { 16458 eth_hw_addr_random(bp->dev); 16459 } 16460 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16461 #endif 16462 } 16463 return rc; 16464 } 16465 16466 static void bnxt_vpd_read_info(struct bnxt *bp) 16467 { 16468 struct pci_dev *pdev = bp->pdev; 16469 unsigned int vpd_size, kw_len; 16470 int pos, size; 16471 u8 *vpd_data; 16472 16473 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16474 if (IS_ERR(vpd_data)) { 16475 pci_warn(pdev, "Unable to read VPD\n"); 16476 return; 16477 } 16478 16479 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16480 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16481 if (pos < 0) 16482 goto read_sn; 16483 16484 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16485 memcpy(bp->board_partno, &vpd_data[pos], size); 16486 16487 read_sn: 16488 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16489 PCI_VPD_RO_KEYWORD_SERIALNO, 16490 &kw_len); 16491 if (pos < 0) 16492 goto exit; 16493 16494 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16495 memcpy(bp->board_serialno, &vpd_data[pos], size); 16496 exit: 16497 kfree(vpd_data); 16498 } 16499 16500 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16501 { 16502 struct pci_dev *pdev = bp->pdev; 16503 u64 qword; 16504 16505 qword = pci_get_dsn(pdev); 16506 if (!qword) { 16507 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16508 return -EOPNOTSUPP; 16509 } 16510 16511 put_unaligned_le64(qword, dsn); 16512 16513 bp->flags |= BNXT_FLAG_DSN_VALID; 16514 return 0; 16515 } 16516 16517 static int bnxt_map_db_bar(struct bnxt *bp) 16518 { 16519 if (!bp->db_size) 16520 return -ENODEV; 16521 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16522 if (!bp->bar1) 16523 return -ENOMEM; 16524 return 0; 16525 } 16526 16527 void bnxt_print_device_info(struct bnxt *bp) 16528 { 16529 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16530 board_info[bp->board_idx].name, 16531 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16532 16533 pcie_print_link_status(bp->pdev); 16534 } 16535 16536 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16537 { 16538 struct bnxt_hw_resc *hw_resc; 16539 struct net_device *dev; 16540 struct bnxt *bp; 16541 int rc, max_irqs; 16542 16543 if (pci_is_bridge(pdev)) 16544 return -ENODEV; 16545 16546 if (!pdev->msix_cap) { 16547 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16548 return -ENODEV; 16549 } 16550 16551 /* Clear any pending DMA transactions from crash kernel 16552 * while loading driver in capture kernel. 16553 */ 16554 if (is_kdump_kernel()) { 16555 pci_clear_master(pdev); 16556 pcie_flr(pdev); 16557 } 16558 16559 max_irqs = bnxt_get_max_irq(pdev); 16560 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16561 max_irqs); 16562 if (!dev) 16563 return -ENOMEM; 16564 16565 bp = netdev_priv(dev); 16566 bp->board_idx = ent->driver_data; 16567 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16568 bnxt_set_max_func_irqs(bp, max_irqs); 16569 16570 if (bnxt_vf_pciid(bp->board_idx)) 16571 bp->flags |= BNXT_FLAG_VF; 16572 16573 /* No devlink port registration in case of a VF */ 16574 if (BNXT_PF(bp)) 16575 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16576 16577 rc = bnxt_init_board(pdev, dev); 16578 if (rc < 0) 16579 goto init_err_free; 16580 16581 dev->netdev_ops = &bnxt_netdev_ops; 16582 dev->stat_ops = &bnxt_stat_ops; 16583 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16584 dev->ethtool_ops = &bnxt_ethtool_ops; 16585 pci_set_drvdata(pdev, dev); 16586 16587 rc = bnxt_alloc_hwrm_resources(bp); 16588 if (rc) 16589 goto init_err_pci_clean; 16590 16591 mutex_init(&bp->hwrm_cmd_lock); 16592 mutex_init(&bp->link_lock); 16593 16594 rc = bnxt_fw_init_one_p1(bp); 16595 if (rc) 16596 goto init_err_pci_clean; 16597 16598 if (BNXT_PF(bp)) 16599 bnxt_vpd_read_info(bp); 16600 16601 if (BNXT_CHIP_P5_PLUS(bp)) { 16602 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16603 if (BNXT_CHIP_P7(bp)) 16604 bp->flags |= BNXT_FLAG_CHIP_P7; 16605 } 16606 16607 rc = bnxt_alloc_rss_indir_tbl(bp); 16608 if (rc) 16609 goto init_err_pci_clean; 16610 16611 rc = bnxt_fw_init_one_p2(bp); 16612 if (rc) 16613 goto init_err_pci_clean; 16614 16615 rc = bnxt_map_db_bar(bp); 16616 if (rc) { 16617 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16618 rc); 16619 goto init_err_pci_clean; 16620 } 16621 16622 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16623 NETIF_F_TSO | NETIF_F_TSO6 | 16624 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16625 NETIF_F_GSO_IPXIP4 | 16626 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16627 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16628 NETIF_F_RXCSUM | NETIF_F_GRO; 16629 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16630 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16631 16632 if (BNXT_SUPPORTS_TPA(bp)) 16633 dev->hw_features |= NETIF_F_LRO; 16634 16635 dev->hw_enc_features = 16636 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16637 NETIF_F_TSO | NETIF_F_TSO6 | 16638 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16639 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16640 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16641 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16642 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16643 if (bp->flags & BNXT_FLAG_CHIP_P7) 16644 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16645 else 16646 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16647 16648 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16649 NETIF_F_GSO_GRE_CSUM; 16650 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16651 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16652 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16653 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16654 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16655 if (BNXT_SUPPORTS_TPA(bp)) 16656 dev->hw_features |= NETIF_F_GRO_HW; 16657 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16658 if (dev->features & NETIF_F_GRO_HW) 16659 dev->features &= ~NETIF_F_LRO; 16660 dev->priv_flags |= IFF_UNICAST_FLT; 16661 16662 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16663 if (bp->tso_max_segs) 16664 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16665 16666 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16667 NETDEV_XDP_ACT_RX_SG; 16668 16669 #ifdef CONFIG_BNXT_SRIOV 16670 init_waitqueue_head(&bp->sriov_cfg_wait); 16671 #endif 16672 if (BNXT_SUPPORTS_TPA(bp)) { 16673 bp->gro_func = bnxt_gro_func_5730x; 16674 if (BNXT_CHIP_P4(bp)) 16675 bp->gro_func = bnxt_gro_func_5731x; 16676 else if (BNXT_CHIP_P5_PLUS(bp)) 16677 bp->gro_func = bnxt_gro_func_5750x; 16678 } 16679 if (!BNXT_CHIP_P4_PLUS(bp)) 16680 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16681 16682 rc = bnxt_init_mac_addr(bp); 16683 if (rc) { 16684 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16685 rc = -EADDRNOTAVAIL; 16686 goto init_err_pci_clean; 16687 } 16688 16689 if (BNXT_PF(bp)) { 16690 /* Read the adapter's DSN to use as the eswitch switch_id */ 16691 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16692 } 16693 16694 /* MTU range: 60 - FW defined max */ 16695 dev->min_mtu = ETH_ZLEN; 16696 dev->max_mtu = bp->max_mtu; 16697 16698 rc = bnxt_probe_phy(bp, true); 16699 if (rc) 16700 goto init_err_pci_clean; 16701 16702 hw_resc = &bp->hw_resc; 16703 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16704 BNXT_L2_FLTR_MAX_FLTR; 16705 /* Older firmware may not report these filters properly */ 16706 if (bp->max_fltr < BNXT_MAX_FLTR) 16707 bp->max_fltr = BNXT_MAX_FLTR; 16708 bnxt_init_l2_fltr_tbl(bp); 16709 __bnxt_set_rx_skb_mode(bp, false); 16710 bnxt_set_tpa_flags(bp); 16711 bnxt_init_ring_params(bp); 16712 bnxt_set_ring_params(bp); 16713 bnxt_rdma_aux_device_init(bp); 16714 rc = bnxt_set_dflt_rings(bp, true); 16715 if (rc) { 16716 if (BNXT_VF(bp) && rc == -ENODEV) { 16717 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16718 } else { 16719 netdev_err(bp->dev, "Not enough rings available.\n"); 16720 rc = -ENOMEM; 16721 } 16722 goto init_err_pci_clean; 16723 } 16724 16725 bnxt_fw_init_one_p3(bp); 16726 16727 bnxt_init_dflt_coal(bp); 16728 16729 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16730 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16731 16732 rc = bnxt_init_int_mode(bp); 16733 if (rc) 16734 goto init_err_pci_clean; 16735 16736 /* No TC has been set yet and rings may have been trimmed due to 16737 * limited MSIX, so we re-initialize the TX rings per TC. 16738 */ 16739 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16740 16741 if (BNXT_PF(bp)) { 16742 if (!bnxt_pf_wq) { 16743 bnxt_pf_wq = 16744 create_singlethread_workqueue("bnxt_pf_wq"); 16745 if (!bnxt_pf_wq) { 16746 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16747 rc = -ENOMEM; 16748 goto init_err_pci_clean; 16749 } 16750 } 16751 rc = bnxt_init_tc(bp); 16752 if (rc) 16753 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16754 rc); 16755 } 16756 16757 bnxt_inv_fw_health_reg(bp); 16758 rc = bnxt_dl_register(bp); 16759 if (rc) 16760 goto init_err_dl; 16761 16762 INIT_LIST_HEAD(&bp->usr_fltr_list); 16763 16764 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16765 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16766 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16767 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16768 dev->request_ops_lock = true; 16769 dev->netmem_tx = true; 16770 16771 rc = register_netdev(dev); 16772 if (rc) 16773 goto init_err_cleanup; 16774 16775 bnxt_dl_fw_reporters_create(bp); 16776 16777 bnxt_rdma_aux_device_add(bp); 16778 16779 bnxt_print_device_info(bp); 16780 16781 pci_save_state(pdev); 16782 16783 return 0; 16784 init_err_cleanup: 16785 bnxt_rdma_aux_device_uninit(bp); 16786 bnxt_dl_unregister(bp); 16787 init_err_dl: 16788 bnxt_shutdown_tc(bp); 16789 bnxt_clear_int_mode(bp); 16790 16791 init_err_pci_clean: 16792 bnxt_hwrm_func_drv_unrgtr(bp); 16793 bnxt_free_hwrm_resources(bp); 16794 bnxt_hwmon_uninit(bp); 16795 bnxt_ethtool_free(bp); 16796 bnxt_ptp_clear(bp); 16797 kfree(bp->ptp_cfg); 16798 bp->ptp_cfg = NULL; 16799 kfree(bp->fw_health); 16800 bp->fw_health = NULL; 16801 bnxt_cleanup_pci(bp); 16802 bnxt_free_ctx_mem(bp, true); 16803 bnxt_free_crash_dump_mem(bp); 16804 kfree(bp->rss_indir_tbl); 16805 bp->rss_indir_tbl = NULL; 16806 16807 init_err_free: 16808 free_netdev(dev); 16809 return rc; 16810 } 16811 16812 static void bnxt_shutdown(struct pci_dev *pdev) 16813 { 16814 struct net_device *dev = pci_get_drvdata(pdev); 16815 struct bnxt *bp; 16816 16817 if (!dev) 16818 return; 16819 16820 rtnl_lock(); 16821 netdev_lock(dev); 16822 bp = netdev_priv(dev); 16823 if (!bp) 16824 goto shutdown_exit; 16825 16826 if (netif_running(dev)) 16827 netif_close(dev); 16828 16829 bnxt_ptp_clear(bp); 16830 bnxt_clear_int_mode(bp); 16831 pci_disable_device(pdev); 16832 16833 if (system_state == SYSTEM_POWER_OFF) { 16834 pci_wake_from_d3(pdev, bp->wol); 16835 pci_set_power_state(pdev, PCI_D3hot); 16836 } 16837 16838 shutdown_exit: 16839 netdev_unlock(dev); 16840 rtnl_unlock(); 16841 } 16842 16843 #ifdef CONFIG_PM_SLEEP 16844 static int bnxt_suspend(struct device *device) 16845 { 16846 struct net_device *dev = dev_get_drvdata(device); 16847 struct bnxt *bp = netdev_priv(dev); 16848 int rc = 0; 16849 16850 bnxt_ulp_stop(bp); 16851 16852 netdev_lock(dev); 16853 if (netif_running(dev)) { 16854 netif_device_detach(dev); 16855 rc = bnxt_close(dev); 16856 } 16857 bnxt_hwrm_func_drv_unrgtr(bp); 16858 bnxt_ptp_clear(bp); 16859 pci_disable_device(bp->pdev); 16860 bnxt_free_ctx_mem(bp, false); 16861 netdev_unlock(dev); 16862 return rc; 16863 } 16864 16865 static int bnxt_resume(struct device *device) 16866 { 16867 struct net_device *dev = dev_get_drvdata(device); 16868 struct bnxt *bp = netdev_priv(dev); 16869 int rc = 0; 16870 16871 netdev_lock(dev); 16872 rc = pci_enable_device(bp->pdev); 16873 if (rc) { 16874 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16875 rc); 16876 goto resume_exit; 16877 } 16878 pci_set_master(bp->pdev); 16879 if (bnxt_hwrm_ver_get(bp)) { 16880 rc = -ENODEV; 16881 goto resume_exit; 16882 } 16883 rc = bnxt_hwrm_func_reset(bp); 16884 if (rc) { 16885 rc = -EBUSY; 16886 goto resume_exit; 16887 } 16888 16889 rc = bnxt_hwrm_func_qcaps(bp); 16890 if (rc) 16891 goto resume_exit; 16892 16893 bnxt_clear_reservations(bp, true); 16894 16895 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16896 rc = -ENODEV; 16897 goto resume_exit; 16898 } 16899 if (bp->fw_crash_mem) 16900 bnxt_hwrm_crash_dump_mem_cfg(bp); 16901 16902 if (bnxt_ptp_init(bp)) { 16903 kfree(bp->ptp_cfg); 16904 bp->ptp_cfg = NULL; 16905 } 16906 bnxt_get_wol_settings(bp); 16907 if (netif_running(dev)) { 16908 rc = bnxt_open(dev); 16909 if (!rc) 16910 netif_device_attach(dev); 16911 } 16912 16913 resume_exit: 16914 netdev_unlock(bp->dev); 16915 bnxt_ulp_start(bp, rc); 16916 if (!rc) 16917 bnxt_reenable_sriov(bp); 16918 return rc; 16919 } 16920 16921 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16922 #define BNXT_PM_OPS (&bnxt_pm_ops) 16923 16924 #else 16925 16926 #define BNXT_PM_OPS NULL 16927 16928 #endif /* CONFIG_PM_SLEEP */ 16929 16930 /** 16931 * bnxt_io_error_detected - called when PCI error is detected 16932 * @pdev: Pointer to PCI device 16933 * @state: The current pci connection state 16934 * 16935 * This function is called after a PCI bus error affecting 16936 * this device has been detected. 16937 */ 16938 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16939 pci_channel_state_t state) 16940 { 16941 struct net_device *netdev = pci_get_drvdata(pdev); 16942 struct bnxt *bp = netdev_priv(netdev); 16943 bool abort = false; 16944 16945 netdev_info(netdev, "PCI I/O error detected\n"); 16946 16947 bnxt_ulp_stop(bp); 16948 16949 netdev_lock(netdev); 16950 netif_device_detach(netdev); 16951 16952 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16953 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16954 abort = true; 16955 } 16956 16957 if (abort || state == pci_channel_io_perm_failure) { 16958 netdev_unlock(netdev); 16959 return PCI_ERS_RESULT_DISCONNECT; 16960 } 16961 16962 /* Link is not reliable anymore if state is pci_channel_io_frozen 16963 * so we disable bus master to prevent any potential bad DMAs before 16964 * freeing kernel memory. 16965 */ 16966 if (state == pci_channel_io_frozen) { 16967 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16968 bnxt_fw_fatal_close(bp); 16969 } 16970 16971 if (netif_running(netdev)) 16972 __bnxt_close_nic(bp, true, true); 16973 16974 if (pci_is_enabled(pdev)) 16975 pci_disable_device(pdev); 16976 bnxt_free_ctx_mem(bp, false); 16977 netdev_unlock(netdev); 16978 16979 /* Request a slot slot reset. */ 16980 return PCI_ERS_RESULT_NEED_RESET; 16981 } 16982 16983 /** 16984 * bnxt_io_slot_reset - called after the pci bus has been reset. 16985 * @pdev: Pointer to PCI device 16986 * 16987 * Restart the card from scratch, as if from a cold-boot. 16988 * At this point, the card has experienced a hard reset, 16989 * followed by fixups by BIOS, and has its config space 16990 * set up identically to what it was at cold boot. 16991 */ 16992 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 16993 { 16994 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 16995 struct net_device *netdev = pci_get_drvdata(pdev); 16996 struct bnxt *bp = netdev_priv(netdev); 16997 int retry = 0; 16998 int err = 0; 16999 int off; 17000 17001 netdev_info(bp->dev, "PCI Slot Reset\n"); 17002 17003 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 17004 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 17005 msleep(900); 17006 17007 netdev_lock(netdev); 17008 17009 if (pci_enable_device(pdev)) { 17010 dev_err(&pdev->dev, 17011 "Cannot re-enable PCI device after reset.\n"); 17012 } else { 17013 pci_set_master(pdev); 17014 /* Upon fatal error, our device internal logic that latches to 17015 * BAR value is getting reset and will restore only upon 17016 * rewriting the BARs. 17017 * 17018 * As pci_restore_state() does not re-write the BARs if the 17019 * value is same as saved value earlier, driver needs to 17020 * write the BARs to 0 to force restore, in case of fatal error. 17021 */ 17022 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17023 &bp->state)) { 17024 for (off = PCI_BASE_ADDRESS_0; 17025 off <= PCI_BASE_ADDRESS_5; off += 4) 17026 pci_write_config_dword(bp->pdev, off, 0); 17027 } 17028 pci_restore_state(pdev); 17029 pci_save_state(pdev); 17030 17031 bnxt_inv_fw_health_reg(bp); 17032 bnxt_try_map_fw_health_reg(bp); 17033 17034 /* In some PCIe AER scenarios, firmware may take up to 17035 * 10 seconds to become ready in the worst case. 17036 */ 17037 do { 17038 err = bnxt_try_recover_fw(bp); 17039 if (!err) 17040 break; 17041 retry++; 17042 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17043 17044 if (err) { 17045 dev_err(&pdev->dev, "Firmware not ready\n"); 17046 goto reset_exit; 17047 } 17048 17049 err = bnxt_hwrm_func_reset(bp); 17050 if (!err) 17051 result = PCI_ERS_RESULT_RECOVERED; 17052 17053 /* IRQ will be initialized later in bnxt_io_resume */ 17054 bnxt_ulp_irq_stop(bp); 17055 bnxt_clear_int_mode(bp); 17056 } 17057 17058 reset_exit: 17059 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17060 bnxt_clear_reservations(bp, true); 17061 netdev_unlock(netdev); 17062 17063 return result; 17064 } 17065 17066 /** 17067 * bnxt_io_resume - called when traffic can start flowing again. 17068 * @pdev: Pointer to PCI device 17069 * 17070 * This callback is called when the error recovery driver tells 17071 * us that its OK to resume normal operation. 17072 */ 17073 static void bnxt_io_resume(struct pci_dev *pdev) 17074 { 17075 struct net_device *netdev = pci_get_drvdata(pdev); 17076 struct bnxt *bp = netdev_priv(netdev); 17077 int err; 17078 17079 netdev_info(bp->dev, "PCI Slot Resume\n"); 17080 netdev_lock(netdev); 17081 17082 err = bnxt_hwrm_func_qcaps(bp); 17083 if (!err) { 17084 if (netif_running(netdev)) { 17085 err = bnxt_open(netdev); 17086 } else { 17087 err = bnxt_reserve_rings(bp, true); 17088 if (!err) 17089 err = bnxt_init_int_mode(bp); 17090 } 17091 } 17092 17093 if (!err) 17094 netif_device_attach(netdev); 17095 17096 netdev_unlock(netdev); 17097 bnxt_ulp_start(bp, err); 17098 if (!err) 17099 bnxt_reenable_sriov(bp); 17100 } 17101 17102 static const struct pci_error_handlers bnxt_err_handler = { 17103 .error_detected = bnxt_io_error_detected, 17104 .slot_reset = bnxt_io_slot_reset, 17105 .resume = bnxt_io_resume 17106 }; 17107 17108 static struct pci_driver bnxt_pci_driver = { 17109 .name = DRV_MODULE_NAME, 17110 .id_table = bnxt_pci_tbl, 17111 .probe = bnxt_init_one, 17112 .remove = bnxt_remove_one, 17113 .shutdown = bnxt_shutdown, 17114 .driver.pm = BNXT_PM_OPS, 17115 .err_handler = &bnxt_err_handler, 17116 #if defined(CONFIG_BNXT_SRIOV) 17117 .sriov_configure = bnxt_sriov_configure, 17118 #endif 17119 }; 17120 17121 static int __init bnxt_init(void) 17122 { 17123 int err; 17124 17125 bnxt_debug_init(); 17126 err = pci_register_driver(&bnxt_pci_driver); 17127 if (err) { 17128 bnxt_debug_exit(); 17129 return err; 17130 } 17131 17132 return 0; 17133 } 17134 17135 static void __exit bnxt_exit(void) 17136 { 17137 pci_unregister_driver(&bnxt_pci_driver); 17138 if (bnxt_pf_wq) 17139 destroy_workqueue(bnxt_pf_wq); 17140 bnxt_debug_exit(); 17141 } 17142 17143 module_init(bnxt_init); 17144 module_exit(bnxt_exit); 17145