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 #include <linux/bnxt/hsi.h> 62 #include <linux/bnxt/ulp.h> 63 64 #include "bnxt.h" 65 #include "bnxt_hwrm.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 #include "bnxt_gso.h" 78 #include <net/tso.h> 79 80 #define BNXT_TX_TIMEOUT (5 * HZ) 81 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 82 NETIF_MSG_TX_ERR) 83 84 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 85 MODULE_LICENSE("GPL"); 86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 87 88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 90 91 #define BNXT_TX_PUSH_THRESH 164 92 93 /* indexed by enum board_idx */ 94 static const struct { 95 char *name; 96 } board_info[] = { 97 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 98 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 99 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 100 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 101 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 102 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 103 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 104 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 105 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 106 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 108 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 109 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 110 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 111 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 112 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 113 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 114 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 115 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 116 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 117 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 118 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 119 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 120 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 121 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 122 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 123 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 124 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 125 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 126 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 127 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 128 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 129 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 130 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 131 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 132 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 133 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 134 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 135 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 136 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 137 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 138 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 139 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 140 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 141 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 142 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 143 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 144 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 145 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 146 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 147 [NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" }, 148 }; 149 150 static const struct pci_device_id bnxt_pci_tbl[] = { 151 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 152 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 153 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 154 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 155 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 156 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 157 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 158 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 159 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 160 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 161 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 163 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 164 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 165 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 166 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 167 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 168 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 169 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 170 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 171 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 172 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 173 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 174 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 175 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 178 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 179 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 180 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 181 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 182 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 183 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 184 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 185 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 186 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 187 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 188 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 189 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 190 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 191 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 192 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 193 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 194 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 195 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 196 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 197 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 198 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 199 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 200 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 201 #ifdef CONFIG_BNXT_SRIOV 202 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 203 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 206 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 207 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 208 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 209 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 210 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 211 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 212 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 215 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 216 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 217 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 218 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 219 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 220 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 221 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 222 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 223 { PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV }, 224 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 225 #endif 226 { 0 } 227 }; 228 229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 230 231 static const u16 bnxt_vf_req_snif[] = { 232 HWRM_FUNC_CFG, 233 HWRM_FUNC_VF_CFG, 234 HWRM_PORT_PHY_QCFG, 235 HWRM_CFA_L2_FILTER_ALLOC, 236 }; 237 238 static const u16 bnxt_async_events_arr[] = { 239 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 240 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 241 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 242 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 243 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 244 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 245 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 246 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 247 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 248 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 249 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 250 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 251 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 252 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 253 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 254 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 255 ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER, 256 }; 257 258 const u16 bnxt_bstore_to_trace[] = { 259 [BNXT_CTX_SRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE, 260 [BNXT_CTX_SRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE, 261 [BNXT_CTX_CRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE, 262 [BNXT_CTX_CRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE, 263 [BNXT_CTX_RIGP0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE, 264 [BNXT_CTX_L2HWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE, 265 [BNXT_CTX_REHWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE, 266 [BNXT_CTX_CA0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE, 267 [BNXT_CTX_CA1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE, 268 [BNXT_CTX_CA2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE, 269 [BNXT_CTX_RIGP1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE, 270 [BNXT_CTX_KONG] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE, 271 [BNXT_CTX_QPC] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE, 272 }; 273 274 static struct workqueue_struct *bnxt_pf_wq; 275 276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 277 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 279 280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 281 .ports = { 282 .src = 0, 283 .dst = 0, 284 }, 285 .addrs = { 286 .v6addrs = { 287 .src = BNXT_IPV6_MASK_NONE, 288 .dst = BNXT_IPV6_MASK_NONE, 289 }, 290 }, 291 }; 292 293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 294 .ports = { 295 .src = cpu_to_be16(0xffff), 296 .dst = cpu_to_be16(0xffff), 297 }, 298 .addrs = { 299 .v6addrs = { 300 .src = BNXT_IPV6_MASK_ALL, 301 .dst = BNXT_IPV6_MASK_ALL, 302 }, 303 }, 304 }; 305 306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 307 .ports = { 308 .src = cpu_to_be16(0xffff), 309 .dst = cpu_to_be16(0xffff), 310 }, 311 .addrs = { 312 .v4addrs = { 313 .src = cpu_to_be32(0xffffffff), 314 .dst = cpu_to_be32(0xffffffff), 315 }, 316 }, 317 }; 318 319 static bool bnxt_vf_pciid(enum board_idx idx) 320 { 321 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 322 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 323 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 324 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF || 325 idx == NETXTREME_E_P7_VF_HV); 326 } 327 328 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 329 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 330 331 #define BNXT_DB_CQ(db, idx) \ 332 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 333 334 #define BNXT_DB_NQ_P5(db, idx) \ 335 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 336 (db)->doorbell) 337 338 #define BNXT_DB_NQ_P7(db, idx) \ 339 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 340 DB_RING_IDX(db, idx), (db)->doorbell) 341 342 #define BNXT_DB_CQ_ARM(db, idx) \ 343 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 344 345 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 346 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 347 DB_RING_IDX(db, idx), (db)->doorbell) 348 349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 350 { 351 if (bp->flags & BNXT_FLAG_CHIP_P7) 352 BNXT_DB_NQ_P7(db, idx); 353 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 354 BNXT_DB_NQ_P5(db, idx); 355 else 356 BNXT_DB_CQ(db, idx); 357 } 358 359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 360 { 361 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 362 BNXT_DB_NQ_ARM_P5(db, idx); 363 else 364 BNXT_DB_CQ_ARM(db, idx); 365 } 366 367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 368 { 369 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 370 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 371 DB_RING_IDX(db, idx), db->doorbell); 372 else 373 BNXT_DB_CQ(db, idx); 374 } 375 376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 377 { 378 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 379 return; 380 381 if (BNXT_PF(bp)) 382 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 383 else 384 schedule_delayed_work(&bp->fw_reset_task, delay); 385 } 386 387 static void __bnxt_queue_sp_work(struct bnxt *bp) 388 { 389 if (BNXT_PF(bp)) 390 queue_work(bnxt_pf_wq, &bp->sp_task); 391 else 392 schedule_work(&bp->sp_task); 393 } 394 395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 396 { 397 set_bit(event, &bp->sp_event); 398 __bnxt_queue_sp_work(bp); 399 } 400 401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 402 { 403 if (!rxr->bnapi->in_reset) { 404 rxr->bnapi->in_reset = true; 405 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 406 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 407 else 408 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 409 __bnxt_queue_sp_work(bp); 410 } 411 rxr->rx_next_cons = 0xffff; 412 } 413 414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 415 u16 curr) 416 { 417 struct bnxt_napi *bnapi = txr->bnapi; 418 419 if (bnapi->tx_fault) 420 return; 421 422 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 423 txr->txq_index, txr->tx_hw_cons, 424 txr->tx_cons, txr->tx_prod, curr); 425 WARN_ON_ONCE(1); 426 bnapi->tx_fault = 1; 427 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 428 } 429 430 const u16 bnxt_lhint_arr[] = { 431 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 432 TX_BD_FLAGS_LHINT_512_TO_1023, 433 TX_BD_FLAGS_LHINT_1024_TO_2047, 434 TX_BD_FLAGS_LHINT_1024_TO_2047, 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 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 444 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 445 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 446 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 447 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 448 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 449 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 450 }; 451 452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 453 { 454 struct metadata_dst *md_dst = skb_metadata_dst(skb); 455 456 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 457 return 0; 458 459 return md_dst->u.port_info.port_id; 460 } 461 462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 463 u16 prod) 464 { 465 /* If the most recent BD has its completion suppressed, unset the bit 466 * so that a completion is generated, otherwise nothing is left to 467 * clean the ring and wake the queue. 468 */ 469 if (txr->kick_txbd0) { 470 txr->kick_txbd0->tx_bd_len_flags_type &= 471 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 472 txr->kick_txbd0 = NULL; 473 } 474 475 /* Sync BD data before updating doorbell */ 476 wmb(); 477 bnxt_db_write(bp, &txr->tx_db, prod); 478 txr->kick_pending = 0; 479 } 480 481 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 482 { 483 struct bnxt *bp = netdev_priv(dev); 484 struct tx_bd *txbd, *txbd0; 485 struct tx_bd_ext *txbd1; 486 struct netdev_queue *txq; 487 int i; 488 dma_addr_t mapping; 489 unsigned int length, pad = 0; 490 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 491 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 492 struct pci_dev *pdev = bp->pdev; 493 u16 prod, last_frag, txts_prod; 494 struct bnxt_tx_ring_info *txr; 495 struct bnxt_sw_tx_bd *tx_buf; 496 __le32 lflags = 0; 497 skb_frag_t *frag; 498 499 i = skb_get_queue_mapping(skb); 500 if (unlikely(i >= bp->tx_nr_rings)) { 501 dev_kfree_skb_any(skb); 502 dev_core_stats_tx_dropped_inc(dev); 503 return NETDEV_TX_OK; 504 } 505 506 txq = netdev_get_tx_queue(dev, i); 507 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 508 prod = txr->tx_prod; 509 510 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS) 511 if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) { 512 netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d. SKB will be linearized.\n", 513 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS); 514 if (skb_linearize(skb)) 515 goto tx_free; 516 } 517 #endif 518 if (skb_is_gso(skb) && 519 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) && 520 !(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) { 521 int rc = bnxt_sw_udp_gso_xmit(bp, txr, txq, skb); 522 523 /* if SW USO queued a packet, the doorbell will be written 524 * below and there is no reason to track the last BD with 525 * suppressed completions 526 */ 527 if (rc > 0) 528 txr->kick_txbd0 = NULL; 529 530 /* if a packet was queued by SW USO or a doorbell was pending 531 * from a previous xmit that was deferred, write the doorbell. 532 */ 533 if (rc > 0 || txr->kick_pending) 534 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 535 536 return rc < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK; 537 } 538 539 free_size = bnxt_tx_avail(bp, txr); 540 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 541 /* We must have raced with NAPI cleanup */ 542 if (net_ratelimit() && txr->kick_pending) 543 netif_warn(bp, tx_err, dev, 544 "bnxt: ring busy w/ flush pending!\n"); 545 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 546 bp->tx_wake_thresh)) 547 return NETDEV_TX_BUSY; 548 } 549 550 length = skb->len; 551 len = skb_headlen(skb); 552 last_frag = skb_shinfo(skb)->nr_frags; 553 554 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 555 556 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 557 tx_buf->skb = skb; 558 tx_buf->nr_frags = last_frag; 559 560 vlan_tag_flags = 0; 561 cfa_action = bnxt_xmit_get_cfa_action(skb); 562 if (skb_vlan_tag_present(skb)) { 563 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 564 skb_vlan_tag_get(skb); 565 /* Currently supports 8021Q, 8021AD vlan offloads 566 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 567 */ 568 if (skb->vlan_proto == htons(ETH_P_8021Q)) 569 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 570 } 571 572 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 573 ptp->tx_tstamp_en) { 574 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 575 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 576 tx_buf->is_ts_pkt = 1; 577 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 578 } else if (!skb_is_gso(skb)) { 579 u16 seq_id, hdr_off; 580 581 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 582 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 583 if (vlan_tag_flags) 584 hdr_off += VLAN_HLEN; 585 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 586 tx_buf->is_ts_pkt = 1; 587 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 588 589 ptp->txts_req[txts_prod].tx_seqid = seq_id; 590 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 591 tx_buf->txts_prod = txts_prod; 592 } 593 } 594 } 595 if (unlikely(skb->no_fcs)) 596 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 597 598 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 599 skb_frags_readable(skb) && !lflags) { 600 struct tx_push_buffer *tx_push_buf = txr->tx_push; 601 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 602 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 603 void __iomem *db = txr->tx_db.doorbell; 604 void *pdata = tx_push_buf->data; 605 u64 *end; 606 int j, push_len; 607 608 /* Set COAL_NOW to be ready quickly for the next push */ 609 tx_push->tx_bd_len_flags_type = 610 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 611 TX_BD_TYPE_LONG_TX_BD | 612 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 613 TX_BD_FLAGS_COAL_NOW | 614 TX_BD_FLAGS_PACKET_END | 615 TX_BD_CNT(2)); 616 617 if (skb->ip_summed == CHECKSUM_PARTIAL) 618 tx_push1->tx_bd_hsize_lflags = 619 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 620 else 621 tx_push1->tx_bd_hsize_lflags = 0; 622 623 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 624 tx_push1->tx_bd_cfa_action = 625 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 626 627 end = pdata + length; 628 end = PTR_ALIGN(end, 8) - 1; 629 *end = 0; 630 631 skb_copy_from_linear_data(skb, pdata, len); 632 pdata += len; 633 for (j = 0; j < last_frag; j++) { 634 void *fptr; 635 636 frag = &skb_shinfo(skb)->frags[j]; 637 fptr = skb_frag_address_safe(frag); 638 if (!fptr) 639 goto normal_tx; 640 641 memcpy(pdata, fptr, skb_frag_size(frag)); 642 pdata += skb_frag_size(frag); 643 } 644 645 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 646 txbd->tx_bd_haddr = txr->data_mapping; 647 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 648 prod = NEXT_TX(prod); 649 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 650 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 651 memcpy(txbd, tx_push1, sizeof(*txbd)); 652 prod = NEXT_TX(prod); 653 tx_push->doorbell = 654 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 655 DB_RING_IDX(&txr->tx_db, prod)); 656 WRITE_ONCE(txr->tx_prod, prod); 657 658 tx_buf->is_push = 1; 659 netdev_tx_sent_queue(txq, skb->len); 660 wmb(); /* Sync is_push and byte queue before pushing data */ 661 662 push_len = (length + sizeof(*tx_push) + 7) / 8; 663 if (push_len > 16) { 664 __iowrite64_copy(db, tx_push_buf, 16); 665 __iowrite32_copy(db + 4, tx_push_buf + 1, 666 (push_len - 16) << 1); 667 } else { 668 __iowrite64_copy(db, tx_push_buf, push_len); 669 } 670 671 goto tx_done; 672 } 673 674 normal_tx: 675 if (length < BNXT_MIN_PKT_SIZE) { 676 pad = BNXT_MIN_PKT_SIZE - length; 677 if (skb_pad(skb, pad)) 678 /* SKB already freed. */ 679 goto tx_kick_pending; 680 length = BNXT_MIN_PKT_SIZE; 681 } 682 683 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 684 685 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 686 goto tx_free; 687 688 dma_unmap_addr_set(tx_buf, mapping, mapping); 689 dma_unmap_len_set(tx_buf, len, len); 690 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 691 TX_BD_CNT(last_frag + 2); 692 693 txbd->tx_bd_haddr = cpu_to_le64(mapping); 694 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 695 696 prod = NEXT_TX(prod); 697 txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags, 698 cfa_action); 699 700 if (skb_is_gso(skb)) { 701 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 702 u32 hdr_len; 703 704 if (skb->encapsulation) { 705 if (udp_gso) 706 hdr_len = skb_inner_transport_offset(skb) + 707 sizeof(struct udphdr); 708 else 709 hdr_len = skb_inner_tcp_all_headers(skb); 710 } else if (udp_gso) { 711 hdr_len = skb_transport_offset(skb) + 712 sizeof(struct udphdr); 713 } else { 714 hdr_len = skb_tcp_all_headers(skb); 715 } 716 717 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 718 TX_BD_FLAGS_T_IPID | 719 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 720 length = skb_shinfo(skb)->gso_size; 721 txbd1->tx_bd_mss = cpu_to_le32(length); 722 length += hdr_len; 723 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 724 txbd1->tx_bd_hsize_lflags |= 725 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 726 } 727 728 length >>= 9; 729 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 730 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 731 skb->len); 732 i = 0; 733 goto tx_dma_error; 734 } 735 flags |= bnxt_lhint_arr[length]; 736 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 737 738 txbd0 = txbd; 739 for (i = 0; i < last_frag; i++) { 740 frag = &skb_shinfo(skb)->frags[i]; 741 prod = NEXT_TX(prod); 742 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 743 744 len = skb_frag_size(frag); 745 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 746 DMA_TO_DEVICE); 747 748 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 749 goto tx_dma_error; 750 751 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 752 netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf, 753 mapping, mapping); 754 dma_unmap_len_set(tx_buf, len, len); 755 756 txbd->tx_bd_haddr = cpu_to_le64(mapping); 757 758 flags = len << TX_BD_LEN_SHIFT; 759 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 760 } 761 762 flags &= ~TX_BD_LEN; 763 txbd->tx_bd_len_flags_type = 764 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 765 TX_BD_FLAGS_PACKET_END); 766 767 netdev_tx_sent_queue(txq, skb->len); 768 769 skb_tx_timestamp(skb); 770 771 prod = NEXT_TX(prod); 772 WRITE_ONCE(txr->tx_prod, prod); 773 774 txr->kick_txbd0 = NULL; 775 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 776 bnxt_txr_db_kick(bp, txr, prod); 777 } else { 778 if (free_size >= bp->tx_wake_thresh) { 779 txbd0->tx_bd_len_flags_type |= 780 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 781 txr->kick_txbd0 = txbd0; 782 } 783 txr->kick_pending = 1; 784 } 785 786 tx_done: 787 788 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 789 if (txr->kick_pending) 790 bnxt_txr_db_kick(bp, txr, prod); 791 792 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 793 bp->tx_wake_thresh); 794 } 795 return NETDEV_TX_OK; 796 797 tx_dma_error: 798 last_frag = i; 799 800 /* start back at beginning and unmap skb */ 801 prod = txr->tx_prod; 802 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 803 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 804 skb_headlen(skb), DMA_TO_DEVICE); 805 prod = NEXT_TX(prod); 806 807 /* unmap remaining mapped pages */ 808 for (i = 0; i < last_frag; i++) { 809 prod = NEXT_TX(prod); 810 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 811 frag = &skb_shinfo(skb)->frags[i]; 812 netmem_dma_unmap_page_attrs(&pdev->dev, 813 dma_unmap_addr(tx_buf, mapping), 814 skb_frag_size(frag), 815 DMA_TO_DEVICE, 0); 816 } 817 818 tx_free: 819 dev_kfree_skb_any(skb); 820 tx_kick_pending: 821 if (BNXT_TX_PTP_IS_SET(lflags)) { 822 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0; 823 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 824 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 825 /* set SKB to err so PTP worker will clean up */ 826 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 827 } 828 if (txr->kick_pending) 829 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 830 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL; 831 dev_core_stats_tx_dropped_inc(dev); 832 return NETDEV_TX_OK; 833 } 834 835 /* Returns true if some remaining TX packets not processed. */ 836 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 837 int budget) 838 { 839 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 840 struct pci_dev *pdev = bp->pdev; 841 u16 hw_cons = txr->tx_hw_cons; 842 unsigned int tx_bytes = 0; 843 u16 cons = txr->tx_cons; 844 unsigned int dma_len; 845 dma_addr_t dma_addr; 846 int tx_pkts = 0; 847 bool rc = false; 848 849 while (RING_TX(bp, cons) != hw_cons) { 850 struct bnxt_sw_tx_bd *tx_buf, *head_buf; 851 struct sk_buff *skb; 852 bool is_ts_pkt; 853 int j, last; 854 855 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 856 head_buf = tx_buf; 857 skb = tx_buf->skb; 858 859 if (unlikely(!skb)) { 860 bnxt_sched_reset_txr(bp, txr, cons); 861 return rc; 862 } 863 864 is_ts_pkt = tx_buf->is_ts_pkt; 865 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 866 rc = true; 867 break; 868 } 869 870 cons = NEXT_TX(cons); 871 tx_pkts++; 872 tx_bytes += skb->len; 873 tx_buf->skb = NULL; 874 tx_buf->is_ts_pkt = 0; 875 876 if (tx_buf->is_push) { 877 tx_buf->is_push = 0; 878 goto next_tx_int; 879 } 880 881 if (dma_unmap_len(tx_buf, len)) { 882 dma_addr = dma_unmap_addr(tx_buf, mapping); 883 dma_len = dma_unmap_len(tx_buf, len); 884 885 dma_unmap_single(&pdev->dev, dma_addr, dma_len, 886 DMA_TO_DEVICE); 887 } 888 889 last = tx_buf->nr_frags; 890 891 for (j = 0; j < last; j++) { 892 cons = NEXT_TX(cons); 893 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 894 if (dma_unmap_len(tx_buf, len)) { 895 dma_addr = dma_unmap_addr(tx_buf, mapping); 896 dma_len = dma_unmap_len(tx_buf, len); 897 898 netmem_dma_unmap_page_attrs(&pdev->dev, 899 dma_addr, dma_len, 900 DMA_TO_DEVICE, 0); 901 } 902 } 903 904 if (unlikely(head_buf->is_sw_gso)) { 905 u16 inline_cons = txr->tx_inline_cons + 1; 906 907 WRITE_ONCE(txr->tx_inline_cons, inline_cons); 908 if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) { 909 tso_dma_map_complete(&pdev->dev, 910 &head_buf->sw_gso_cstate); 911 } else { 912 tx_pkts--; 913 tx_bytes -= skb->len; 914 skb = NULL; 915 } 916 head_buf->is_sw_gso = 0; 917 } 918 919 if (unlikely(is_ts_pkt)) { 920 if (BNXT_CHIP_P5(bp)) { 921 /* PTP worker takes ownership of the skb */ 922 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 923 skb = NULL; 924 } 925 } 926 927 next_tx_int: 928 cons = NEXT_TX(cons); 929 930 napi_consume_skb(skb, budget); 931 } 932 933 WRITE_ONCE(txr->tx_cons, cons); 934 935 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 936 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 937 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 938 939 return rc; 940 } 941 942 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 943 { 944 struct bnxt_tx_ring_info *txr; 945 bool more = false; 946 int i; 947 948 bnxt_for_each_napi_tx(i, bnapi, txr) { 949 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 950 more |= __bnxt_tx_int(bp, txr, budget); 951 } 952 if (!more) 953 bnapi->events &= ~BNXT_TX_CMP_EVENT; 954 } 955 956 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr) 957 { 958 return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE; 959 } 960 961 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 962 struct bnxt_rx_ring_info *rxr, 963 unsigned int *offset, 964 gfp_t gfp) 965 { 966 struct page *page; 967 968 if (rxr->rx_page_size < PAGE_SIZE) { 969 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 970 rxr->rx_page_size); 971 } else { 972 page = page_pool_dev_alloc_pages(rxr->page_pool); 973 *offset = 0; 974 } 975 if (!page) 976 return NULL; 977 978 *mapping = page_pool_get_dma_addr(page) + *offset; 979 return page; 980 } 981 982 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping, 983 struct bnxt_rx_ring_info *rxr, 984 unsigned int *offset, 985 gfp_t gfp) 986 { 987 netmem_ref netmem; 988 989 if (rxr->rx_page_size < PAGE_SIZE) { 990 netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset, 991 rxr->rx_page_size, gfp); 992 } else { 993 netmem = page_pool_alloc_netmems(rxr->page_pool, gfp); 994 *offset = 0; 995 } 996 if (!netmem) 997 return 0; 998 999 *mapping = page_pool_get_dma_addr_netmem(netmem) + *offset; 1000 return netmem; 1001 } 1002 1003 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 1004 struct bnxt_rx_ring_info *rxr, 1005 gfp_t gfp) 1006 { 1007 unsigned int offset; 1008 struct page *page; 1009 1010 page = page_pool_alloc_frag(rxr->head_pool, &offset, 1011 bp->rx_buf_size, gfp); 1012 if (!page) 1013 return NULL; 1014 1015 *mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset; 1016 return page_address(page) + offset; 1017 } 1018 1019 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1020 u16 prod, gfp_t gfp) 1021 { 1022 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1023 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1024 dma_addr_t mapping; 1025 1026 if (BNXT_RX_PAGE_MODE(bp)) { 1027 unsigned int offset; 1028 struct page *page = 1029 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 1030 1031 if (!page) 1032 return -ENOMEM; 1033 1034 mapping += bp->rx_dma_offset; 1035 rx_buf->data = page; 1036 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 1037 rx_buf->offset = offset; 1038 } else { 1039 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp); 1040 1041 if (!data) 1042 return -ENOMEM; 1043 1044 rx_buf->data = data; 1045 rx_buf->data_ptr = data + bp->rx_offset; 1046 rx_buf->offset = 0; 1047 } 1048 rx_buf->mapping = mapping; 1049 1050 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1051 return 0; 1052 } 1053 1054 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 1055 { 1056 u16 prod = rxr->rx_prod; 1057 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1058 struct bnxt *bp = rxr->bnapi->bp; 1059 struct rx_bd *cons_bd, *prod_bd; 1060 1061 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1062 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1063 1064 prod_rx_buf->data = data; 1065 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 1066 1067 prod_rx_buf->mapping = cons_rx_buf->mapping; 1068 prod_rx_buf->offset = cons_rx_buf->offset; 1069 1070 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1071 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 1072 1073 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 1074 } 1075 1076 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1077 { 1078 u16 next, max = rxr->rx_agg_bmap_size; 1079 1080 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 1081 if (next >= max) 1082 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 1083 return next; 1084 } 1085 1086 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1087 u16 prod, gfp_t gfp) 1088 { 1089 struct rx_bd *rxbd = 1090 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1091 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 1092 u16 sw_prod = rxr->rx_sw_agg_prod; 1093 unsigned int offset = 0; 1094 dma_addr_t mapping; 1095 netmem_ref netmem; 1096 1097 netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp); 1098 if (!netmem) 1099 return -ENOMEM; 1100 1101 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1102 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1103 1104 __set_bit(sw_prod, rxr->rx_agg_bmap); 1105 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1106 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1107 1108 rx_agg_buf->netmem = netmem; 1109 rx_agg_buf->offset = offset; 1110 rx_agg_buf->mapping = mapping; 1111 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1112 rxbd->rx_bd_opaque = sw_prod; 1113 return 0; 1114 } 1115 1116 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1117 struct bnxt_cp_ring_info *cpr, 1118 u16 cp_cons, u16 curr) 1119 { 1120 struct rx_agg_cmp *agg; 1121 1122 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1123 agg = (struct rx_agg_cmp *) 1124 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1125 return agg; 1126 } 1127 1128 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1129 struct bnxt_rx_ring_info *rxr, 1130 u16 agg_id, u16 curr) 1131 { 1132 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1133 1134 return &tpa_info->agg_arr[curr]; 1135 } 1136 1137 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1138 u16 start, u32 agg_bufs, bool tpa) 1139 { 1140 struct bnxt_napi *bnapi = cpr->bnapi; 1141 struct bnxt *bp = bnapi->bp; 1142 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1143 u16 prod = rxr->rx_agg_prod; 1144 u16 sw_prod = rxr->rx_sw_agg_prod; 1145 bool p5_tpa = false; 1146 u32 i; 1147 1148 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1149 p5_tpa = true; 1150 1151 for (i = 0; i < agg_bufs; i++) { 1152 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1153 struct rx_agg_cmp *agg; 1154 struct rx_bd *prod_bd; 1155 netmem_ref netmem; 1156 u16 cons; 1157 1158 if (p5_tpa) 1159 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1160 else 1161 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1162 cons = agg->rx_agg_cmp_opaque; 1163 __clear_bit(cons, rxr->rx_agg_bmap); 1164 1165 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1166 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1167 1168 __set_bit(sw_prod, rxr->rx_agg_bmap); 1169 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1170 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1171 1172 /* It is possible for sw_prod to be equal to cons, so 1173 * set cons_rx_buf->netmem to 0 first. 1174 */ 1175 netmem = cons_rx_buf->netmem; 1176 cons_rx_buf->netmem = 0; 1177 prod_rx_buf->netmem = netmem; 1178 prod_rx_buf->offset = cons_rx_buf->offset; 1179 1180 prod_rx_buf->mapping = cons_rx_buf->mapping; 1181 1182 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1183 1184 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1185 prod_bd->rx_bd_opaque = sw_prod; 1186 1187 prod = NEXT_RX_AGG(prod); 1188 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1189 } 1190 rxr->rx_agg_prod = prod; 1191 rxr->rx_sw_agg_prod = sw_prod; 1192 } 1193 1194 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1195 struct bnxt_rx_ring_info *rxr, 1196 u16 cons, void *data, u8 *data_ptr, 1197 dma_addr_t dma_addr, 1198 unsigned int offset_and_len) 1199 { 1200 unsigned int len = offset_and_len & 0xffff; 1201 struct page *page = data; 1202 u16 prod = rxr->rx_prod; 1203 struct sk_buff *skb; 1204 void *frag_start; 1205 int err; 1206 1207 frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset; 1208 1209 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1210 if (unlikely(err)) { 1211 bnxt_reuse_rx_data(rxr, cons, data); 1212 return NULL; 1213 } 1214 dma_addr -= bp->rx_dma_offset; 1215 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size, 1216 bp->rx_dir); 1217 skb = napi_build_skb(frag_start, rxr->rx_page_size); 1218 if (!skb) { 1219 page_pool_recycle_direct(rxr->page_pool, page); 1220 return NULL; 1221 } 1222 skb_mark_for_recycle(skb); 1223 skb_reserve(skb, data_ptr - (u8 *)frag_start); 1224 __skb_put(skb, len); 1225 1226 return skb; 1227 } 1228 1229 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1230 struct bnxt_rx_ring_info *rxr, 1231 u16 cons, void *data, u8 *data_ptr, 1232 dma_addr_t dma_addr, 1233 unsigned int offset_and_len) 1234 { 1235 unsigned int payload = offset_and_len >> 16; 1236 unsigned int len = offset_and_len & 0xffff; 1237 skb_frag_t *frag; 1238 struct page *page = data; 1239 u16 prod = rxr->rx_prod; 1240 struct sk_buff *skb; 1241 int off, err; 1242 1243 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1244 if (unlikely(err)) { 1245 bnxt_reuse_rx_data(rxr, cons, data); 1246 return NULL; 1247 } 1248 dma_addr -= bp->rx_dma_offset; 1249 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size, 1250 bp->rx_dir); 1251 1252 if (unlikely(!payload)) 1253 payload = eth_get_headlen(bp->dev, data_ptr, len); 1254 1255 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1256 if (!skb) { 1257 page_pool_recycle_direct(rxr->page_pool, page); 1258 return NULL; 1259 } 1260 1261 skb_mark_for_recycle(skb); 1262 off = (void *)data_ptr - page_address(page); 1263 skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size); 1264 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1265 payload + NET_IP_ALIGN); 1266 1267 frag = &skb_shinfo(skb)->frags[0]; 1268 skb_frag_size_sub(frag, payload); 1269 skb_frag_off_add(frag, payload); 1270 skb->data_len -= payload; 1271 skb->tail += payload; 1272 1273 return skb; 1274 } 1275 1276 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1277 struct bnxt_rx_ring_info *rxr, u16 cons, 1278 void *data, u8 *data_ptr, 1279 dma_addr_t dma_addr, 1280 unsigned int offset_and_len) 1281 { 1282 u16 prod = rxr->rx_prod; 1283 struct sk_buff *skb; 1284 int err; 1285 1286 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1287 if (unlikely(err)) { 1288 bnxt_reuse_rx_data(rxr, cons, data); 1289 return NULL; 1290 } 1291 1292 skb = napi_build_skb(data, bp->rx_buf_size); 1293 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1294 bp->rx_dir); 1295 if (!skb) { 1296 page_pool_free_va(rxr->head_pool, data, true); 1297 return NULL; 1298 } 1299 1300 skb_mark_for_recycle(skb); 1301 skb_reserve(skb, bp->rx_offset); 1302 skb_put(skb, offset_and_len & 0xffff); 1303 return skb; 1304 } 1305 1306 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp, 1307 struct bnxt_cp_ring_info *cpr, 1308 u16 idx, u32 agg_bufs, bool tpa, 1309 struct sk_buff *skb, 1310 struct xdp_buff *xdp) 1311 { 1312 struct bnxt_napi *bnapi = cpr->bnapi; 1313 struct skb_shared_info *shinfo; 1314 struct bnxt_rx_ring_info *rxr; 1315 u32 i, total_frag_len = 0; 1316 bool p5_tpa = false; 1317 u16 prod; 1318 1319 rxr = bnapi->rx_ring; 1320 prod = rxr->rx_agg_prod; 1321 1322 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1323 p5_tpa = true; 1324 1325 if (skb) 1326 shinfo = skb_shinfo(skb); 1327 else 1328 shinfo = xdp_get_shared_info_from_buff(xdp); 1329 1330 for (i = 0; i < agg_bufs; i++) { 1331 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1332 struct rx_agg_cmp *agg; 1333 u16 cons, frag_len; 1334 netmem_ref netmem; 1335 1336 if (p5_tpa) 1337 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1338 else 1339 agg = bnxt_get_agg(bp, cpr, idx, i); 1340 cons = agg->rx_agg_cmp_opaque; 1341 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1342 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1343 1344 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1345 if (skb) { 1346 skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem, 1347 cons_rx_buf->offset, 1348 frag_len, rxr->rx_page_size); 1349 } else { 1350 skb_frag_t *frag = &shinfo->frags[i]; 1351 1352 skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem, 1353 cons_rx_buf->offset, 1354 frag_len); 1355 shinfo->nr_frags = i + 1; 1356 } 1357 __clear_bit(cons, rxr->rx_agg_bmap); 1358 1359 /* It is possible for bnxt_alloc_rx_netmem() to allocate 1360 * a sw_prod index that equals the cons index, so we 1361 * need to clear the cons entry now. 1362 */ 1363 netmem = cons_rx_buf->netmem; 1364 cons_rx_buf->netmem = 0; 1365 1366 if (xdp && netmem_is_pfmemalloc(netmem)) 1367 xdp_buff_set_frag_pfmemalloc(xdp); 1368 1369 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) { 1370 if (skb) { 1371 skb->len -= frag_len; 1372 skb->data_len -= frag_len; 1373 skb->truesize -= rxr->rx_page_size; 1374 } 1375 1376 --shinfo->nr_frags; 1377 cons_rx_buf->netmem = netmem; 1378 1379 /* Update prod since possibly some netmems have been 1380 * allocated already. 1381 */ 1382 rxr->rx_agg_prod = prod; 1383 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1384 return 0; 1385 } 1386 1387 page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0, 1388 rxr->rx_page_size); 1389 1390 total_frag_len += frag_len; 1391 prod = NEXT_RX_AGG(prod); 1392 } 1393 rxr->rx_agg_prod = prod; 1394 return total_frag_len; 1395 } 1396 1397 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp, 1398 struct bnxt_cp_ring_info *cpr, 1399 struct sk_buff *skb, u16 idx, 1400 u32 agg_bufs, bool tpa) 1401 { 1402 u32 total_frag_len = 0; 1403 1404 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1405 skb, NULL); 1406 if (!total_frag_len) { 1407 skb_mark_for_recycle(skb); 1408 dev_kfree_skb(skb); 1409 return NULL; 1410 } 1411 1412 return skb; 1413 } 1414 1415 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp, 1416 struct bnxt_cp_ring_info *cpr, 1417 struct xdp_buff *xdp, u16 idx, 1418 u32 agg_bufs, bool tpa) 1419 { 1420 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1421 u32 total_frag_len = 0; 1422 1423 if (!xdp_buff_has_frags(xdp)) 1424 shinfo->nr_frags = 0; 1425 1426 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1427 NULL, xdp); 1428 if (total_frag_len) { 1429 xdp_buff_set_frags_flag(xdp); 1430 shinfo->nr_frags = agg_bufs; 1431 shinfo->xdp_frags_size = total_frag_len; 1432 } 1433 return total_frag_len; 1434 } 1435 1436 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1437 u8 agg_bufs, u32 *raw_cons) 1438 { 1439 u16 last; 1440 struct rx_agg_cmp *agg; 1441 1442 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1443 last = RING_CMP(*raw_cons); 1444 agg = (struct rx_agg_cmp *) 1445 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1446 return RX_AGG_CMP_VALID(agg, *raw_cons); 1447 } 1448 1449 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1450 unsigned int len, 1451 dma_addr_t mapping) 1452 { 1453 struct bnxt *bp = bnapi->bp; 1454 struct pci_dev *pdev = bp->pdev; 1455 struct sk_buff *skb; 1456 1457 skb = napi_alloc_skb(&bnapi->napi, len); 1458 if (!skb) 1459 return NULL; 1460 1461 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak, 1462 bp->rx_dir); 1463 1464 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1465 len + NET_IP_ALIGN); 1466 1467 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak, 1468 bp->rx_dir); 1469 1470 skb_put(skb, len); 1471 1472 return skb; 1473 } 1474 1475 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1476 unsigned int len, 1477 dma_addr_t mapping) 1478 { 1479 return bnxt_copy_data(bnapi, data, len, mapping); 1480 } 1481 1482 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1483 struct xdp_buff *xdp, 1484 unsigned int len, 1485 dma_addr_t mapping) 1486 { 1487 unsigned int metasize = 0; 1488 u8 *data = xdp->data; 1489 struct sk_buff *skb; 1490 1491 len = xdp->data_end - xdp->data_meta; 1492 metasize = xdp->data - xdp->data_meta; 1493 data = xdp->data_meta; 1494 1495 skb = bnxt_copy_data(bnapi, data, len, mapping); 1496 if (!skb) 1497 return skb; 1498 1499 if (metasize) { 1500 skb_metadata_set(skb, metasize); 1501 __skb_pull(skb, metasize); 1502 } 1503 1504 return skb; 1505 } 1506 1507 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1508 u32 *raw_cons, void *cmp) 1509 { 1510 struct rx_cmp *rxcmp = cmp; 1511 u32 tmp_raw_cons = *raw_cons; 1512 u8 cmp_type, agg_bufs = 0; 1513 1514 cmp_type = RX_CMP_TYPE(rxcmp); 1515 1516 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1517 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1518 RX_CMP_AGG_BUFS) >> 1519 RX_CMP_AGG_BUFS_SHIFT; 1520 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1521 struct rx_tpa_end_cmp *tpa_end = cmp; 1522 1523 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1524 return 0; 1525 1526 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1527 } 1528 1529 if (agg_bufs) { 1530 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1531 return -EBUSY; 1532 } 1533 *raw_cons = tmp_raw_cons; 1534 return 0; 1535 } 1536 1537 static u16 bnxt_alloc_agg_idx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1538 u16 agg_id) 1539 { 1540 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1541 u16 idx = agg_id & (bp->max_tpa_roundup_size - 1); 1542 1543 if (test_bit(idx, map->agg_idx_bmap)) { 1544 idx = find_first_zero_bit(map->agg_idx_bmap, 1545 bp->max_tpa_roundup_size); 1546 if (idx >= bp->max_tpa_roundup_size) 1547 return INVALID_HW_RING_ID; 1548 } 1549 __set_bit(idx, map->agg_idx_bmap); 1550 map->agg_id_tbl[agg_id] = idx; 1551 return idx; 1552 } 1553 1554 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1555 { 1556 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1557 1558 __clear_bit(idx, map->agg_idx_bmap); 1559 } 1560 1561 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1562 { 1563 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1564 1565 return map->agg_id_tbl[agg_id]; 1566 } 1567 1568 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1569 struct rx_tpa_start_cmp *tpa_start, 1570 struct rx_tpa_start_cmp_ext *tpa_start1) 1571 { 1572 tpa_info->cfa_code_valid = 1; 1573 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1574 tpa_info->vlan_valid = 0; 1575 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1576 tpa_info->vlan_valid = 1; 1577 tpa_info->metadata = 1578 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1579 } 1580 } 1581 1582 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1583 struct rx_tpa_start_cmp *tpa_start, 1584 struct rx_tpa_start_cmp_ext *tpa_start1) 1585 { 1586 tpa_info->vlan_valid = 0; 1587 if (TPA_START_VLAN_VALID(tpa_start)) { 1588 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1589 u32 vlan_proto = ETH_P_8021Q; 1590 1591 tpa_info->vlan_valid = 1; 1592 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1593 vlan_proto = ETH_P_8021AD; 1594 tpa_info->metadata = vlan_proto << 16 | 1595 TPA_START_METADATA0_TCI(tpa_start1); 1596 } 1597 } 1598 1599 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1600 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1601 struct rx_tpa_start_cmp_ext *tpa_start1) 1602 { 1603 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1604 struct bnxt_tpa_info *tpa_info; 1605 u16 cons, prod, agg_id; 1606 struct rx_bd *prod_bd; 1607 dma_addr_t mapping; 1608 1609 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1610 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1611 agg_id = bnxt_alloc_agg_idx(bp, rxr, agg_id); 1612 if (unlikely(agg_id == INVALID_HW_RING_ID)) { 1613 netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n", 1614 rxr->bnapi->index, 1615 TPA_START_AGG_ID_P5(tpa_start)); 1616 bnxt_sched_reset_rxr(bp, rxr); 1617 return; 1618 } 1619 } else { 1620 agg_id = TPA_START_AGG_ID(tpa_start); 1621 } 1622 cons = tpa_start->rx_tpa_start_cmp_opaque; 1623 prod = rxr->rx_prod; 1624 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1625 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1626 tpa_info = &rxr->rx_tpa[agg_id]; 1627 1628 if (unlikely(cons != rxr->rx_next_cons || 1629 TPA_START_ERROR(tpa_start))) { 1630 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1631 cons, rxr->rx_next_cons, 1632 TPA_START_ERROR_CODE(tpa_start1)); 1633 bnxt_sched_reset_rxr(bp, rxr); 1634 return; 1635 } 1636 prod_rx_buf->data = tpa_info->data; 1637 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1638 1639 mapping = tpa_info->mapping; 1640 prod_rx_buf->mapping = mapping; 1641 1642 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1643 1644 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1645 1646 tpa_info->data = cons_rx_buf->data; 1647 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1648 cons_rx_buf->data = NULL; 1649 tpa_info->mapping = cons_rx_buf->mapping; 1650 1651 tpa_info->len = 1652 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1653 RX_TPA_START_CMP_LEN_SHIFT; 1654 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1655 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1656 tpa_info->gso_type = SKB_GSO_TCPV4; 1657 if (TPA_START_IS_IPV6(tpa_start1)) 1658 tpa_info->gso_type = SKB_GSO_TCPV6; 1659 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1660 else if (!BNXT_CHIP_P4_PLUS(bp) && 1661 TPA_START_HASH_TYPE(tpa_start) == 3) 1662 tpa_info->gso_type = SKB_GSO_TCPV6; 1663 tpa_info->rss_hash = 1664 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1665 } else { 1666 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1667 tpa_info->gso_type = 0; 1668 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1669 } 1670 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1671 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1672 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1673 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1674 else 1675 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1676 tpa_info->agg_count = 0; 1677 1678 rxr->rx_prod = NEXT_RX(prod); 1679 cons = RING_RX(bp, NEXT_RX(cons)); 1680 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1681 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1682 1683 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1684 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1685 cons_rx_buf->data = NULL; 1686 } 1687 1688 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1689 { 1690 if (agg_bufs) 1691 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1692 } 1693 1694 #ifdef CONFIG_INET 1695 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1696 { 1697 struct udphdr *uh = NULL; 1698 1699 if (ip_proto == htons(ETH_P_IP)) { 1700 struct iphdr *iph = (struct iphdr *)skb->data; 1701 1702 if (iph->protocol == IPPROTO_UDP) 1703 uh = (struct udphdr *)(iph + 1); 1704 } else { 1705 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1706 1707 if (iph->nexthdr == IPPROTO_UDP) 1708 uh = (struct udphdr *)(iph + 1); 1709 } 1710 if (uh) { 1711 if (uh->check) 1712 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1713 else 1714 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1715 } 1716 } 1717 #endif 1718 1719 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1720 int payload_off, int tcp_ts, 1721 struct sk_buff *skb) 1722 { 1723 #ifdef CONFIG_INET 1724 struct tcphdr *th; 1725 int len, nw_off; 1726 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1727 u32 hdr_info = tpa_info->hdr_info; 1728 bool loopback = false; 1729 1730 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1731 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1732 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1733 1734 /* If the packet is an internal loopback packet, the offsets will 1735 * have an extra 4 bytes. 1736 */ 1737 if (inner_mac_off == 4) { 1738 loopback = true; 1739 } else if (inner_mac_off > 4) { 1740 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1741 ETH_HLEN - 2)); 1742 1743 /* We only support inner iPv4/ipv6. If we don't see the 1744 * correct protocol ID, it must be a loopback packet where 1745 * the offsets are off by 4. 1746 */ 1747 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1748 loopback = true; 1749 } 1750 if (loopback) { 1751 /* internal loopback packet, subtract all offsets by 4 */ 1752 inner_ip_off -= 4; 1753 inner_mac_off -= 4; 1754 outer_ip_off -= 4; 1755 } 1756 1757 nw_off = inner_ip_off - ETH_HLEN; 1758 skb_set_network_header(skb, nw_off); 1759 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1760 struct ipv6hdr *iph = ipv6_hdr(skb); 1761 1762 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1763 len = skb->len - skb_transport_offset(skb); 1764 th = tcp_hdr(skb); 1765 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1766 } else { 1767 struct iphdr *iph = ip_hdr(skb); 1768 1769 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1770 len = skb->len - skb_transport_offset(skb); 1771 th = tcp_hdr(skb); 1772 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1773 } 1774 1775 if (inner_mac_off) { /* tunnel */ 1776 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1777 ETH_HLEN - 2)); 1778 1779 bnxt_gro_tunnel(skb, proto); 1780 } 1781 #endif 1782 return skb; 1783 } 1784 1785 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1786 int payload_off, int tcp_ts, 1787 struct sk_buff *skb) 1788 { 1789 #ifdef CONFIG_INET 1790 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1791 u32 hdr_info = tpa_info->hdr_info; 1792 int iphdr_len, nw_off; 1793 1794 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1795 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1796 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1797 1798 nw_off = inner_ip_off - ETH_HLEN; 1799 skb_set_network_header(skb, nw_off); 1800 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1801 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1802 skb_set_transport_header(skb, nw_off + iphdr_len); 1803 1804 if (inner_mac_off) { /* tunnel */ 1805 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1806 ETH_HLEN - 2)); 1807 1808 bnxt_gro_tunnel(skb, proto); 1809 } 1810 #endif 1811 return skb; 1812 } 1813 1814 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1815 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1816 1817 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1818 int payload_off, int tcp_ts, 1819 struct sk_buff *skb) 1820 { 1821 #ifdef CONFIG_INET 1822 struct tcphdr *th; 1823 int len, nw_off, tcp_opt_len = 0; 1824 1825 if (tcp_ts) 1826 tcp_opt_len = 12; 1827 1828 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1829 struct iphdr *iph; 1830 1831 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1832 ETH_HLEN; 1833 skb_set_network_header(skb, nw_off); 1834 iph = ip_hdr(skb); 1835 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1836 len = skb->len - skb_transport_offset(skb); 1837 th = tcp_hdr(skb); 1838 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1839 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1840 struct ipv6hdr *iph; 1841 1842 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1843 ETH_HLEN; 1844 skb_set_network_header(skb, nw_off); 1845 iph = ipv6_hdr(skb); 1846 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1847 len = skb->len - skb_transport_offset(skb); 1848 th = tcp_hdr(skb); 1849 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1850 } else { 1851 dev_kfree_skb_any(skb); 1852 return NULL; 1853 } 1854 1855 if (nw_off) /* tunnel */ 1856 bnxt_gro_tunnel(skb, skb->protocol); 1857 #endif 1858 return skb; 1859 } 1860 1861 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1862 struct bnxt_tpa_info *tpa_info, 1863 struct rx_tpa_end_cmp *tpa_end, 1864 struct rx_tpa_end_cmp_ext *tpa_end1, 1865 struct sk_buff *skb, 1866 struct bnxt_rx_sw_stats *rx_stats) 1867 { 1868 #ifdef CONFIG_INET 1869 int payload_off; 1870 u16 segs; 1871 1872 segs = TPA_END_TPA_SEGS(tpa_end); 1873 if (segs == 1) 1874 return skb; 1875 1876 rx_stats->rx_hw_gro_packets++; 1877 rx_stats->rx_hw_gro_wire_packets += segs; 1878 1879 NAPI_GRO_CB(skb)->count = segs; 1880 skb_shinfo(skb)->gso_size = 1881 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1882 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1883 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1884 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1885 else 1886 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1887 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1888 if (likely(skb)) 1889 tcp_gro_complete(skb); 1890 #endif 1891 return skb; 1892 } 1893 1894 /* Given the cfa_code of a received packet determine which 1895 * netdev (vf-rep or PF) the packet is destined to. 1896 */ 1897 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1898 { 1899 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1900 1901 /* if vf-rep dev is NULL, it must belong to the PF */ 1902 return dev ? dev : bp->dev; 1903 } 1904 1905 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1906 struct bnxt_cp_ring_info *cpr, 1907 u32 *raw_cons, 1908 struct rx_tpa_end_cmp *tpa_end, 1909 struct rx_tpa_end_cmp_ext *tpa_end1, 1910 u8 *event) 1911 { 1912 struct bnxt_napi *bnapi = cpr->bnapi; 1913 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1914 struct net_device *dev = bp->dev; 1915 u8 *data_ptr, agg_bufs; 1916 unsigned int len; 1917 struct bnxt_tpa_info *tpa_info; 1918 dma_addr_t mapping; 1919 struct sk_buff *skb; 1920 u16 idx = 0, agg_id; 1921 void *data; 1922 bool gro; 1923 1924 if (unlikely(bnapi->in_reset)) { 1925 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1926 1927 if (rc < 0) 1928 return ERR_PTR(-EBUSY); 1929 return NULL; 1930 } 1931 1932 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1933 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1934 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1935 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1936 tpa_info = &rxr->rx_tpa[agg_id]; 1937 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1938 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1939 agg_bufs, tpa_info->agg_count); 1940 agg_bufs = tpa_info->agg_count; 1941 } 1942 tpa_info->agg_count = 0; 1943 *event |= BNXT_AGG_EVENT; 1944 bnxt_free_agg_idx(rxr, agg_id); 1945 idx = agg_id; 1946 gro = !!(bp->flags & BNXT_FLAG_GRO); 1947 } else { 1948 agg_id = TPA_END_AGG_ID(tpa_end); 1949 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1950 tpa_info = &rxr->rx_tpa[agg_id]; 1951 idx = RING_CMP(*raw_cons); 1952 if (agg_bufs) { 1953 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1954 return ERR_PTR(-EBUSY); 1955 1956 *event |= BNXT_AGG_EVENT; 1957 idx = NEXT_CMP(idx); 1958 } 1959 gro = !!TPA_END_GRO(tpa_end); 1960 } 1961 data = tpa_info->data; 1962 data_ptr = tpa_info->data_ptr; 1963 prefetch(data_ptr); 1964 len = tpa_info->len; 1965 mapping = tpa_info->mapping; 1966 1967 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1968 bnxt_abort_tpa(cpr, idx, agg_bufs); 1969 if (agg_bufs > MAX_SKB_FRAGS) 1970 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1971 agg_bufs, (int)MAX_SKB_FRAGS); 1972 return NULL; 1973 } 1974 1975 if (len <= bp->rx_copybreak) { 1976 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1977 if (!skb) { 1978 bnxt_abort_tpa(cpr, idx, agg_bufs); 1979 cpr->sw_stats->rx.rx_oom_discards += 1; 1980 return NULL; 1981 } 1982 } else { 1983 u8 *new_data; 1984 dma_addr_t new_mapping; 1985 1986 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1987 GFP_ATOMIC); 1988 if (!new_data) { 1989 bnxt_abort_tpa(cpr, idx, agg_bufs); 1990 cpr->sw_stats->rx.rx_oom_discards += 1; 1991 return NULL; 1992 } 1993 1994 tpa_info->data = new_data; 1995 tpa_info->data_ptr = new_data + bp->rx_offset; 1996 tpa_info->mapping = new_mapping; 1997 1998 skb = napi_build_skb(data, bp->rx_buf_size); 1999 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 2000 bp->rx_buf_use_size, bp->rx_dir); 2001 2002 if (!skb) { 2003 page_pool_free_va(rxr->head_pool, data, true); 2004 bnxt_abort_tpa(cpr, idx, agg_bufs); 2005 cpr->sw_stats->rx.rx_oom_discards += 1; 2006 return NULL; 2007 } 2008 skb_mark_for_recycle(skb); 2009 skb_reserve(skb, bp->rx_offset); 2010 skb_put(skb, len); 2011 } 2012 2013 if (agg_bufs) { 2014 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs, 2015 true); 2016 if (!skb) { 2017 /* Page reuse already handled by bnxt_rx_pages(). */ 2018 cpr->sw_stats->rx.rx_oom_discards += 1; 2019 return NULL; 2020 } 2021 } 2022 2023 if (tpa_info->cfa_code_valid) 2024 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 2025 skb->protocol = eth_type_trans(skb, dev); 2026 2027 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 2028 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 2029 2030 if (tpa_info->vlan_valid && 2031 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 2032 __be16 vlan_proto = htons(tpa_info->metadata >> 2033 RX_CMP_FLAGS2_METADATA_TPID_SFT); 2034 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2035 2036 if (eth_type_vlan(vlan_proto)) { 2037 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2038 } else { 2039 dev_kfree_skb(skb); 2040 return NULL; 2041 } 2042 } 2043 2044 skb_checksum_none_assert(skb); 2045 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 2046 skb->ip_summed = CHECKSUM_UNNECESSARY; 2047 skb->csum_level = 2048 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 2049 } 2050 2051 if (gro) 2052 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb, 2053 &cpr->sw_stats->rx); 2054 2055 return skb; 2056 } 2057 2058 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 2059 struct rx_agg_cmp *rx_agg) 2060 { 2061 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 2062 struct bnxt_tpa_info *tpa_info; 2063 2064 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 2065 tpa_info = &rxr->rx_tpa[agg_id]; 2066 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 2067 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 2068 } 2069 2070 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 2071 struct sk_buff *skb) 2072 { 2073 skb_mark_for_recycle(skb); 2074 2075 if (skb->dev != bp->dev) { 2076 /* this packet belongs to a vf-rep */ 2077 bnxt_vf_rep_rx(bp, skb); 2078 return; 2079 } 2080 skb_record_rx_queue(skb, bnapi->index); 2081 napi_gro_receive(&bnapi->napi, skb); 2082 } 2083 2084 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 2085 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 2086 { 2087 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 2088 2089 if (BNXT_PTP_RX_TS_VALID(flags)) 2090 goto ts_valid; 2091 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 2092 return false; 2093 2094 ts_valid: 2095 *cmpl_ts = ts; 2096 return true; 2097 } 2098 2099 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 2100 struct rx_cmp *rxcmp, 2101 struct rx_cmp_ext *rxcmp1) 2102 { 2103 __be16 vlan_proto; 2104 u16 vtag; 2105 2106 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 2107 __le32 flags2 = rxcmp1->rx_cmp_flags2; 2108 u32 meta_data; 2109 2110 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 2111 return skb; 2112 2113 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 2114 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2115 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 2116 if (eth_type_vlan(vlan_proto)) 2117 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2118 else 2119 goto vlan_err; 2120 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2121 if (RX_CMP_VLAN_VALID(rxcmp)) { 2122 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 2123 2124 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 2125 vlan_proto = htons(ETH_P_8021Q); 2126 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 2127 vlan_proto = htons(ETH_P_8021AD); 2128 else 2129 goto vlan_err; 2130 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 2131 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2132 } 2133 } 2134 return skb; 2135 vlan_err: 2136 skb_mark_for_recycle(skb); 2137 dev_kfree_skb(skb); 2138 return NULL; 2139 } 2140 2141 /* returns the following: 2142 * 1 - 1 packet successfully received 2143 * 0 - successful TPA_START, packet not completed yet 2144 * -EBUSY - completion ring does not have all the agg buffers yet 2145 * -ENOMEM - packet aborted due to out of memory 2146 * -EIO - packet aborted due to hw error indicated in BD 2147 */ 2148 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2149 u32 *raw_cons, u8 *event) 2150 { 2151 struct bnxt_napi *bnapi = cpr->bnapi; 2152 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2153 struct net_device *dev = bp->dev; 2154 struct rx_cmp *rxcmp; 2155 struct rx_cmp_ext *rxcmp1; 2156 u32 tmp_raw_cons = *raw_cons; 2157 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2158 struct skb_shared_info *sinfo; 2159 struct bnxt_xdp_buff bnxt_xdp; 2160 struct bnxt_sw_rx_bd *rx_buf; 2161 unsigned int len; 2162 u8 *data_ptr, agg_bufs, cmp_type; 2163 bool xdp_active = false; 2164 dma_addr_t dma_addr; 2165 struct sk_buff *skb; 2166 u32 flags, misc; 2167 u32 cmpl_ts; 2168 void *data; 2169 int rc = 0; 2170 2171 rxcmp = (struct rx_cmp *) 2172 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2173 2174 cmp_type = RX_CMP_TYPE(rxcmp); 2175 2176 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2177 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2178 goto next_rx_no_prod_no_len; 2179 } 2180 2181 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2182 cp_cons = RING_CMP(tmp_raw_cons); 2183 rxcmp1 = (struct rx_cmp_ext *) 2184 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2185 2186 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2187 return -EBUSY; 2188 2189 /* The valid test of the entry must be done first before 2190 * reading any further. 2191 */ 2192 dma_rmb(); 2193 prod = rxr->rx_prod; 2194 2195 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2196 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2197 bnxt_tpa_start(bp, rxr, cmp_type, 2198 (struct rx_tpa_start_cmp *)rxcmp, 2199 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2200 2201 *event |= BNXT_RX_EVENT; 2202 goto next_rx_no_prod_no_len; 2203 2204 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2205 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2206 (struct rx_tpa_end_cmp *)rxcmp, 2207 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2208 2209 if (IS_ERR(skb)) 2210 return -EBUSY; 2211 2212 rc = -ENOMEM; 2213 if (likely(skb)) { 2214 bnxt_deliver_skb(bp, bnapi, skb); 2215 rc = 1; 2216 } 2217 *event |= BNXT_RX_EVENT; 2218 goto next_rx_no_prod_no_len; 2219 } 2220 2221 cons = rxcmp->rx_cmp_opaque; 2222 if (unlikely(cons != rxr->rx_next_cons)) { 2223 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2224 2225 /* 0xffff is forced error, don't print it */ 2226 if (rxr->rx_next_cons != 0xffff) 2227 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2228 cons, rxr->rx_next_cons); 2229 bnxt_sched_reset_rxr(bp, rxr); 2230 if (rc1) 2231 return rc1; 2232 goto next_rx_no_prod_no_len; 2233 } 2234 rx_buf = &rxr->rx_buf_ring[cons]; 2235 data = rx_buf->data; 2236 data_ptr = rx_buf->data_ptr; 2237 prefetch(data_ptr); 2238 2239 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2240 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2241 2242 if (agg_bufs) { 2243 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2244 return -EBUSY; 2245 2246 cp_cons = NEXT_CMP(cp_cons); 2247 *event |= BNXT_AGG_EVENT; 2248 } 2249 *event |= BNXT_RX_EVENT; 2250 2251 rx_buf->data = NULL; 2252 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2253 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2254 2255 bnxt_reuse_rx_data(rxr, cons, data); 2256 if (agg_bufs) 2257 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2258 false); 2259 2260 rc = -EIO; 2261 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2262 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2263 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2264 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2265 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2266 rx_err); 2267 bnxt_sched_reset_rxr(bp, rxr); 2268 } 2269 } 2270 goto next_rx_no_len; 2271 } 2272 2273 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2274 len = flags >> RX_CMP_LEN_SHIFT; 2275 dma_addr = rx_buf->mapping; 2276 2277 if (bnxt_xdp_attached(bp, rxr)) { 2278 bnxt_xdp.rxcmp = rxcmp; 2279 bnxt_xdp.rxcmp1 = rxcmp1; 2280 bnxt_xdp.cmp_type = cmp_type; 2281 2282 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp); 2283 if (agg_bufs) { 2284 u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, 2285 &bnxt_xdp.xdp, 2286 cp_cons, 2287 agg_bufs, 2288 false); 2289 if (!frag_len) 2290 goto oom_next_rx; 2291 2292 } 2293 xdp_active = true; 2294 } 2295 2296 if (xdp_active) { 2297 if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr, 2298 &len, event)) { 2299 rc = 1; 2300 goto next_rx; 2301 } 2302 if (xdp_buff_has_frags(&bnxt_xdp.xdp)) { 2303 sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp); 2304 agg_bufs = sinfo->nr_frags; 2305 } else { 2306 agg_bufs = 0; 2307 } 2308 } 2309 2310 if (len <= bp->rx_copybreak) { 2311 if (!xdp_active) 2312 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2313 else 2314 skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len, 2315 dma_addr); 2316 bnxt_reuse_rx_data(rxr, cons, data); 2317 if (!skb) { 2318 if (agg_bufs) { 2319 if (!xdp_active) 2320 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2321 agg_bufs, false); 2322 else 2323 bnxt_xdp_buff_frags_free(rxr, 2324 &bnxt_xdp.xdp); 2325 } 2326 goto oom_next_rx; 2327 } 2328 } else { 2329 u32 payload; 2330 2331 if (rx_buf->data_ptr == data_ptr) 2332 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2333 else 2334 payload = 0; 2335 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2336 payload | len); 2337 if (!skb) 2338 goto oom_next_rx; 2339 } 2340 2341 if (agg_bufs) { 2342 if (!xdp_active) { 2343 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons, 2344 agg_bufs, false); 2345 if (!skb) 2346 goto oom_next_rx; 2347 } else { 2348 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, 2349 rxr, &bnxt_xdp.xdp); 2350 if (!skb) { 2351 /* we should be able to free the old skb here */ 2352 bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp); 2353 goto oom_next_rx; 2354 } 2355 } 2356 } 2357 2358 if (RX_CMP_HASH_VALID(rxcmp)) { 2359 enum pkt_hash_types type; 2360 2361 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2362 type = bnxt_rss_ext_op(bp, rxcmp); 2363 } else { 2364 u32 itypes = RX_CMP_ITYPES(rxcmp); 2365 2366 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2367 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2368 type = PKT_HASH_TYPE_L4; 2369 else 2370 type = PKT_HASH_TYPE_L3; 2371 } 2372 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2373 } 2374 2375 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2376 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2377 skb->protocol = eth_type_trans(skb, dev); 2378 2379 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2380 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2381 if (!skb) 2382 goto next_rx; 2383 } 2384 2385 skb_checksum_none_assert(skb); 2386 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2387 if (dev->features & NETIF_F_RXCSUM) { 2388 skb->ip_summed = CHECKSUM_UNNECESSARY; 2389 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2390 } 2391 } else { 2392 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2393 if (dev->features & NETIF_F_RXCSUM) 2394 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2395 } 2396 } 2397 2398 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2399 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2400 u64 ns, ts; 2401 2402 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2403 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2404 2405 ns = bnxt_timecounter_cyc2time(ptp, ts); 2406 memset(skb_hwtstamps(skb), 0, 2407 sizeof(*skb_hwtstamps(skb))); 2408 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2409 } 2410 } 2411 } 2412 bnxt_deliver_skb(bp, bnapi, skb); 2413 rc = 1; 2414 2415 next_rx: 2416 cpr->rx_packets += 1; 2417 cpr->rx_bytes += len; 2418 2419 next_rx_no_len: 2420 rxr->rx_prod = NEXT_RX(prod); 2421 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2422 2423 next_rx_no_prod_no_len: 2424 *raw_cons = tmp_raw_cons; 2425 2426 return rc; 2427 2428 oom_next_rx: 2429 cpr->sw_stats->rx.rx_oom_discards += 1; 2430 rc = -ENOMEM; 2431 goto next_rx; 2432 } 2433 2434 /* In netpoll mode, if we are using a combined completion ring, we need to 2435 * discard the rx packets and recycle the buffers. 2436 */ 2437 static int bnxt_force_rx_discard(struct bnxt *bp, 2438 struct bnxt_cp_ring_info *cpr, 2439 u32 *raw_cons, u8 *event) 2440 { 2441 u32 tmp_raw_cons = *raw_cons; 2442 struct rx_cmp_ext *rxcmp1; 2443 struct rx_cmp *rxcmp; 2444 u16 cp_cons; 2445 u8 cmp_type; 2446 int rc; 2447 2448 cp_cons = RING_CMP(tmp_raw_cons); 2449 rxcmp = (struct rx_cmp *) 2450 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2451 2452 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2453 cp_cons = RING_CMP(tmp_raw_cons); 2454 rxcmp1 = (struct rx_cmp_ext *) 2455 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2456 2457 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2458 return -EBUSY; 2459 2460 /* The valid test of the entry must be done first before 2461 * reading any further. 2462 */ 2463 dma_rmb(); 2464 cmp_type = RX_CMP_TYPE(rxcmp); 2465 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2466 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2467 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2468 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2469 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2470 struct rx_tpa_end_cmp_ext *tpa_end1; 2471 2472 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2473 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2474 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2475 } 2476 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2477 if (rc && rc != -EBUSY) 2478 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2479 return rc; 2480 } 2481 2482 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2483 { 2484 struct bnxt_fw_health *fw_health = bp->fw_health; 2485 u32 reg = fw_health->regs[reg_idx]; 2486 u32 reg_type, reg_off, val = 0; 2487 2488 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2489 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2490 switch (reg_type) { 2491 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2492 pci_read_config_dword(bp->pdev, reg_off, &val); 2493 break; 2494 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2495 reg_off = fw_health->mapped_regs[reg_idx]; 2496 fallthrough; 2497 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2498 val = readl(bp->bar0 + reg_off); 2499 break; 2500 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2501 val = readl(bp->bar1 + reg_off); 2502 break; 2503 } 2504 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2505 val &= fw_health->fw_reset_inprog_reg_mask; 2506 return val; 2507 } 2508 2509 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2510 { 2511 int i; 2512 2513 for (i = 0; i < bp->rx_nr_rings; i++) { 2514 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2515 struct bnxt_ring_grp_info *grp_info; 2516 2517 grp_info = &bp->grp_info[grp_idx]; 2518 if (grp_info->agg_fw_ring_id == ring_id) 2519 return grp_idx; 2520 } 2521 return INVALID_HW_RING_ID; 2522 } 2523 2524 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2525 { 2526 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2527 2528 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2529 return link_info->force_link_speed2; 2530 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2531 return link_info->force_pam4_link_speed; 2532 return link_info->force_link_speed; 2533 } 2534 2535 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2536 { 2537 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2538 2539 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2540 link_info->req_link_speed = link_info->force_link_speed2; 2541 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2542 switch (link_info->req_link_speed) { 2543 case BNXT_LINK_SPEED_50GB_PAM4: 2544 case BNXT_LINK_SPEED_100GB_PAM4: 2545 case BNXT_LINK_SPEED_200GB_PAM4: 2546 case BNXT_LINK_SPEED_400GB_PAM4: 2547 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2548 break; 2549 case BNXT_LINK_SPEED_100GB_PAM4_112: 2550 case BNXT_LINK_SPEED_200GB_PAM4_112: 2551 case BNXT_LINK_SPEED_400GB_PAM4_112: 2552 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2553 break; 2554 default: 2555 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2556 } 2557 return; 2558 } 2559 link_info->req_link_speed = link_info->force_link_speed; 2560 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2561 if (link_info->force_pam4_link_speed) { 2562 link_info->req_link_speed = link_info->force_pam4_link_speed; 2563 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2564 } 2565 } 2566 2567 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2568 { 2569 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2570 2571 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2572 link_info->advertising = link_info->auto_link_speeds2; 2573 return; 2574 } 2575 link_info->advertising = link_info->auto_link_speeds; 2576 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2577 } 2578 2579 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2580 { 2581 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2582 2583 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2584 if (link_info->req_link_speed != link_info->force_link_speed2) 2585 return true; 2586 return false; 2587 } 2588 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2589 link_info->req_link_speed != link_info->force_link_speed) 2590 return true; 2591 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2592 link_info->req_link_speed != link_info->force_pam4_link_speed) 2593 return true; 2594 return false; 2595 } 2596 2597 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2598 { 2599 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2600 2601 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2602 if (link_info->advertising != link_info->auto_link_speeds2) 2603 return true; 2604 return false; 2605 } 2606 if (link_info->advertising != link_info->auto_link_speeds || 2607 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2608 return true; 2609 return false; 2610 } 2611 2612 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2613 { 2614 u32 flags = bp->ctx->ctx_arr[type].flags; 2615 2616 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2617 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2618 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2619 } 2620 2621 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2622 { 2623 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2624 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2625 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2626 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2627 struct bnxt_bs_trace_info *bs_trace; 2628 int last_pg; 2629 2630 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2631 return; 2632 2633 mem_size = ctxm->max_entries * ctxm->entry_size; 2634 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2635 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2636 2637 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2638 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2639 2640 rmem = &ctx_pg[0].ring_mem; 2641 bs_trace = &bp->bs_trace[trace_type]; 2642 bs_trace->ctx_type = ctxm->type; 2643 bs_trace->trace_type = trace_type; 2644 if (pages > MAX_CTX_PAGES) { 2645 int last_pg_dir = rmem->nr_pages - 1; 2646 2647 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2648 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2649 } else { 2650 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2651 } 2652 bs_trace->magic_byte += magic_byte_offset; 2653 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2654 } 2655 2656 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2657 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2658 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2659 2660 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2661 (((data2) & \ 2662 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2663 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2664 2665 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2666 ((data2) & \ 2667 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2668 2669 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2670 (((data2) & \ 2671 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2672 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2673 2674 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2675 ((data1) & \ 2676 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2677 2678 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2679 (((data1) & \ 2680 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2681 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2682 2683 /* Return true if the workqueue has to be scheduled */ 2684 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2685 { 2686 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2687 2688 switch (err_type) { 2689 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2690 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2691 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2692 break; 2693 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2694 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2695 break; 2696 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2697 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2698 break; 2699 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2700 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2701 char *threshold_type; 2702 bool notify = false; 2703 char *dir_str; 2704 2705 switch (type) { 2706 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2707 threshold_type = "warning"; 2708 break; 2709 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2710 threshold_type = "critical"; 2711 break; 2712 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2713 threshold_type = "fatal"; 2714 break; 2715 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2716 threshold_type = "shutdown"; 2717 break; 2718 default: 2719 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2720 return false; 2721 } 2722 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2723 dir_str = "above"; 2724 notify = true; 2725 } else { 2726 dir_str = "below"; 2727 } 2728 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2729 dir_str, threshold_type); 2730 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2731 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2732 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2733 if (notify) { 2734 bp->thermal_threshold_type = type; 2735 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2736 return true; 2737 } 2738 return false; 2739 } 2740 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2741 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2742 break; 2743 default: 2744 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2745 err_type); 2746 break; 2747 } 2748 return false; 2749 } 2750 2751 #define BNXT_GET_EVENT_PORT(data) \ 2752 ((data) & \ 2753 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2754 2755 #define BNXT_EVENT_RING_TYPE(data2) \ 2756 ((data2) & \ 2757 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2758 2759 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2760 (BNXT_EVENT_RING_TYPE(data2) == \ 2761 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2762 2763 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2764 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2765 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2766 2767 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2768 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2769 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2770 2771 #define BNXT_PHC_BITS 48 2772 2773 static int bnxt_async_event_process(struct bnxt *bp, 2774 struct hwrm_async_event_cmpl *cmpl) 2775 { 2776 u16 event_id = le16_to_cpu(cmpl->event_id); 2777 u32 data1 = le32_to_cpu(cmpl->event_data1); 2778 u32 data2 = le32_to_cpu(cmpl->event_data2); 2779 2780 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2781 event_id, data1, data2); 2782 2783 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2784 switch (event_id) { 2785 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2786 struct bnxt_link_info *link_info = &bp->link_info; 2787 2788 if (BNXT_VF(bp)) 2789 goto async_event_process_exit; 2790 2791 /* print unsupported speed warning in forced speed mode only */ 2792 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2793 (data1 & 0x20000)) { 2794 u16 fw_speed = bnxt_get_force_speed(link_info); 2795 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2796 2797 if (speed != SPEED_UNKNOWN) 2798 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2799 speed); 2800 } 2801 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2802 } 2803 fallthrough; 2804 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2805 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2806 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2807 fallthrough; 2808 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2809 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2810 break; 2811 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2812 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2813 break; 2814 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2815 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2816 2817 if (BNXT_VF(bp)) 2818 break; 2819 2820 if (bp->pf.port_id != port_id) 2821 break; 2822 2823 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2824 break; 2825 } 2826 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2827 if (BNXT_PF(bp)) 2828 goto async_event_process_exit; 2829 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2830 break; 2831 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2832 char *type_str = "Solicited"; 2833 2834 if (!bp->fw_health) 2835 goto async_event_process_exit; 2836 2837 bp->fw_reset_timestamp = jiffies; 2838 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2839 if (!bp->fw_reset_min_dsecs) 2840 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2841 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2842 if (!bp->fw_reset_max_dsecs) 2843 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2844 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2845 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2846 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2847 type_str = "Fatal"; 2848 bp->fw_health->fatalities++; 2849 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2850 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2851 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2852 type_str = "Non-fatal"; 2853 bp->fw_health->survivals++; 2854 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2855 } 2856 netif_warn(bp, hw, bp->dev, 2857 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2858 type_str, data1, data2, 2859 bp->fw_reset_min_dsecs * 100, 2860 bp->fw_reset_max_dsecs * 100); 2861 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2862 break; 2863 } 2864 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2865 struct bnxt_fw_health *fw_health = bp->fw_health; 2866 char *status_desc = "healthy"; 2867 u32 status; 2868 2869 if (!fw_health) 2870 goto async_event_process_exit; 2871 2872 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2873 fw_health->enabled = false; 2874 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2875 break; 2876 } 2877 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2878 fw_health->tmr_multiplier = 2879 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2880 bp->current_interval * 10); 2881 fw_health->tmr_counter = fw_health->tmr_multiplier; 2882 if (!fw_health->enabled) 2883 fw_health->last_fw_heartbeat = 2884 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2885 fw_health->last_fw_reset_cnt = 2886 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2887 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2888 if (status != BNXT_FW_STATUS_HEALTHY) 2889 status_desc = "unhealthy"; 2890 netif_info(bp, drv, bp->dev, 2891 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2892 fw_health->primary ? "primary" : "backup", status, 2893 status_desc, fw_health->last_fw_reset_cnt); 2894 if (!fw_health->enabled) { 2895 /* Make sure tmr_counter is set and visible to 2896 * bnxt_health_check() before setting enabled to true. 2897 */ 2898 smp_wmb(); 2899 fw_health->enabled = true; 2900 } 2901 goto async_event_process_exit; 2902 } 2903 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2904 netif_notice(bp, hw, bp->dev, 2905 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2906 data1, data2); 2907 goto async_event_process_exit; 2908 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2909 struct bnxt_rx_ring_info *rxr; 2910 u16 grp_idx; 2911 2912 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2913 goto async_event_process_exit; 2914 2915 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2916 BNXT_EVENT_RING_TYPE(data2), data1); 2917 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2918 goto async_event_process_exit; 2919 2920 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2921 if (grp_idx == INVALID_HW_RING_ID) { 2922 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2923 data1); 2924 goto async_event_process_exit; 2925 } 2926 rxr = bp->bnapi[grp_idx]->rx_ring; 2927 bnxt_sched_reset_rxr(bp, rxr); 2928 goto async_event_process_exit; 2929 } 2930 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2931 struct bnxt_fw_health *fw_health = bp->fw_health; 2932 2933 netif_notice(bp, hw, bp->dev, 2934 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2935 data1, data2); 2936 if (fw_health) { 2937 fw_health->echo_req_data1 = data1; 2938 fw_health->echo_req_data2 = data2; 2939 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2940 break; 2941 } 2942 goto async_event_process_exit; 2943 } 2944 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2945 bnxt_ptp_pps_event(bp, data1, data2); 2946 goto async_event_process_exit; 2947 } 2948 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2949 if (bnxt_event_error_report(bp, data1, data2)) 2950 break; 2951 goto async_event_process_exit; 2952 } 2953 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2954 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2955 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2956 if (BNXT_PTP_USE_RTC(bp)) { 2957 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2958 unsigned long flags; 2959 u64 ns; 2960 2961 if (!ptp) 2962 goto async_event_process_exit; 2963 2964 bnxt_ptp_update_current_time(bp); 2965 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2966 BNXT_PHC_BITS) | ptp->current_time); 2967 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2968 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2969 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2970 } 2971 break; 2972 } 2973 goto async_event_process_exit; 2974 } 2975 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2976 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2977 2978 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2979 goto async_event_process_exit; 2980 } 2981 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2982 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2983 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2984 2985 if (type >= ARRAY_SIZE(bp->bs_trace)) 2986 goto async_event_process_exit; 2987 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2988 goto async_event_process_exit; 2989 } 2990 default: 2991 goto async_event_process_exit; 2992 } 2993 __bnxt_queue_sp_work(bp); 2994 async_event_process_exit: 2995 bnxt_ulp_async_events(bp, cmpl); 2996 return 0; 2997 } 2998 2999 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 3000 { 3001 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 3002 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 3003 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 3004 (struct hwrm_fwd_req_cmpl *)txcmp; 3005 3006 switch (cmpl_type) { 3007 case CMPL_BASE_TYPE_HWRM_DONE: 3008 seq_id = le16_to_cpu(h_cmpl->sequence_id); 3009 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 3010 break; 3011 3012 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 3013 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 3014 3015 if ((vf_id < bp->pf.first_vf_id) || 3016 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 3017 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 3018 vf_id); 3019 return -EINVAL; 3020 } 3021 3022 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 3023 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 3024 break; 3025 3026 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 3027 bnxt_async_event_process(bp, 3028 (struct hwrm_async_event_cmpl *)txcmp); 3029 break; 3030 3031 default: 3032 break; 3033 } 3034 3035 return 0; 3036 } 3037 3038 static bool bnxt_vnic_is_active(struct bnxt *bp) 3039 { 3040 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 3041 3042 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 3043 } 3044 3045 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 3046 { 3047 struct bnxt_napi *bnapi = dev_instance; 3048 struct bnxt *bp = bnapi->bp; 3049 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3050 u32 cons = RING_CMP(cpr->cp_raw_cons); 3051 3052 cpr->event_ctr++; 3053 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 3054 napi_schedule(&bnapi->napi); 3055 return IRQ_HANDLED; 3056 } 3057 3058 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 3059 { 3060 u32 raw_cons = cpr->cp_raw_cons; 3061 u16 cons = RING_CMP(raw_cons); 3062 struct tx_cmp *txcmp; 3063 3064 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3065 3066 return TX_CMP_VALID(txcmp, raw_cons); 3067 } 3068 3069 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3070 int budget) 3071 { 3072 struct bnxt_napi *bnapi = cpr->bnapi; 3073 u32 raw_cons = cpr->cp_raw_cons; 3074 bool flush_xdp = false; 3075 u32 cons; 3076 int rx_pkts = 0; 3077 u8 event = 0; 3078 struct tx_cmp *txcmp; 3079 3080 cpr->has_more_work = 0; 3081 cpr->had_work_done = 1; 3082 while (1) { 3083 u8 cmp_type; 3084 int rc; 3085 3086 cons = RING_CMP(raw_cons); 3087 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3088 3089 if (!TX_CMP_VALID(txcmp, raw_cons)) 3090 break; 3091 3092 /* The valid test of the entry must be done first before 3093 * reading any further. 3094 */ 3095 dma_rmb(); 3096 cmp_type = TX_CMP_TYPE(txcmp); 3097 if (cmp_type == CMP_TYPE_TX_L2_CMP || 3098 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 3099 u32 opaque = txcmp->tx_cmp_opaque; 3100 struct bnxt_tx_ring_info *txr; 3101 u16 tx_freed; 3102 3103 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 3104 event |= BNXT_TX_CMP_EVENT; 3105 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 3106 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 3107 else 3108 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 3109 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 3110 bp->tx_ring_mask; 3111 /* return full budget so NAPI will complete. */ 3112 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 3113 rx_pkts = budget; 3114 raw_cons = NEXT_RAW_CMP(raw_cons); 3115 if (budget) 3116 cpr->has_more_work = 1; 3117 break; 3118 } 3119 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 3120 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 3121 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 3122 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 3123 if (likely(budget)) 3124 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3125 else 3126 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 3127 &event); 3128 if (event & BNXT_REDIRECT_EVENT) 3129 flush_xdp = true; 3130 if (likely(rc >= 0)) 3131 rx_pkts += rc; 3132 /* Increment rx_pkts when rc is -ENOMEM to count towards 3133 * the NAPI budget. Otherwise, we may potentially loop 3134 * here forever if we consistently cannot allocate 3135 * buffers. 3136 */ 3137 else if (rc == -ENOMEM && budget) 3138 rx_pkts++; 3139 else if (rc == -EBUSY) /* partial completion */ 3140 break; 3141 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3142 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3143 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3144 bnxt_hwrm_handler(bp, txcmp); 3145 } 3146 raw_cons = NEXT_RAW_CMP(raw_cons); 3147 3148 if (rx_pkts && rx_pkts == budget) { 3149 cpr->has_more_work = 1; 3150 break; 3151 } 3152 } 3153 3154 if (flush_xdp) { 3155 xdp_do_flush(); 3156 event &= ~BNXT_REDIRECT_EVENT; 3157 } 3158 3159 if (event & BNXT_TX_EVENT) { 3160 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3161 u16 prod = txr->tx_prod; 3162 3163 /* Sync BD data before updating doorbell */ 3164 wmb(); 3165 3166 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3167 event &= ~BNXT_TX_EVENT; 3168 } 3169 3170 cpr->cp_raw_cons = raw_cons; 3171 bnapi->events |= event; 3172 return rx_pkts; 3173 } 3174 3175 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3176 int budget) 3177 { 3178 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3179 bnapi->tx_int(bp, bnapi, budget); 3180 3181 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3182 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3183 3184 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3185 bnapi->events &= ~BNXT_RX_EVENT; 3186 } 3187 if (bnapi->events & BNXT_AGG_EVENT) { 3188 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3189 3190 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3191 bnapi->events &= ~BNXT_AGG_EVENT; 3192 } 3193 } 3194 3195 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3196 int budget) 3197 { 3198 struct bnxt_napi *bnapi = cpr->bnapi; 3199 int rx_pkts; 3200 3201 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3202 3203 /* ACK completion ring before freeing tx ring and producing new 3204 * buffers in rx/agg rings to prevent overflowing the completion 3205 * ring. 3206 */ 3207 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3208 3209 __bnxt_poll_work_done(bp, bnapi, budget); 3210 return rx_pkts; 3211 } 3212 3213 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3214 { 3215 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3216 struct bnxt *bp = bnapi->bp; 3217 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3218 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3219 struct tx_cmp *txcmp; 3220 struct rx_cmp_ext *rxcmp1; 3221 u32 cp_cons, tmp_raw_cons; 3222 u32 raw_cons = cpr->cp_raw_cons; 3223 bool flush_xdp = false; 3224 u32 rx_pkts = 0; 3225 u8 event = 0; 3226 3227 while (1) { 3228 int rc; 3229 3230 cp_cons = RING_CMP(raw_cons); 3231 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3232 3233 if (!TX_CMP_VALID(txcmp, raw_cons)) 3234 break; 3235 3236 /* The valid test of the entry must be done first before 3237 * reading any further. 3238 */ 3239 dma_rmb(); 3240 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3241 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3242 cp_cons = RING_CMP(tmp_raw_cons); 3243 rxcmp1 = (struct rx_cmp_ext *) 3244 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3245 3246 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3247 break; 3248 3249 /* force an error to recycle the buffer */ 3250 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3251 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3252 3253 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3254 if (likely(rc == -EIO) && budget) 3255 rx_pkts++; 3256 else if (rc == -EBUSY) /* partial completion */ 3257 break; 3258 if (event & BNXT_REDIRECT_EVENT) 3259 flush_xdp = true; 3260 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3261 CMPL_BASE_TYPE_HWRM_DONE)) { 3262 bnxt_hwrm_handler(bp, txcmp); 3263 } else { 3264 netdev_err(bp->dev, 3265 "Invalid completion received on special ring\n"); 3266 } 3267 raw_cons = NEXT_RAW_CMP(raw_cons); 3268 3269 if (rx_pkts == budget) 3270 break; 3271 } 3272 3273 cpr->cp_raw_cons = raw_cons; 3274 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3275 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3276 3277 if (event & BNXT_AGG_EVENT) 3278 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3279 if (flush_xdp) 3280 xdp_do_flush(); 3281 3282 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3283 napi_complete_done(napi, rx_pkts); 3284 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3285 } 3286 return rx_pkts; 3287 } 3288 3289 static int bnxt_poll(struct napi_struct *napi, int budget) 3290 { 3291 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3292 struct bnxt *bp = bnapi->bp; 3293 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3294 int work_done = 0; 3295 3296 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3297 napi_complete(napi); 3298 return 0; 3299 } 3300 while (1) { 3301 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3302 3303 if (work_done >= budget) { 3304 if (!budget) 3305 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3306 break; 3307 } 3308 3309 if (!bnxt_has_work(bp, cpr)) { 3310 if (napi_complete_done(napi, work_done)) 3311 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3312 break; 3313 } 3314 } 3315 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3316 struct dim_sample dim_sample = {}; 3317 3318 dim_update_sample(cpr->event_ctr, 3319 cpr->rx_packets, 3320 cpr->rx_bytes, 3321 &dim_sample); 3322 net_dim(&cpr->dim, &dim_sample); 3323 } 3324 return work_done; 3325 } 3326 3327 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3328 { 3329 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3330 int i, work_done = 0; 3331 3332 for (i = 0; i < cpr->cp_ring_count; i++) { 3333 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3334 3335 if (cpr2->had_nqe_notify) { 3336 work_done += __bnxt_poll_work(bp, cpr2, 3337 budget - work_done); 3338 cpr->has_more_work |= cpr2->has_more_work; 3339 } 3340 } 3341 return work_done; 3342 } 3343 3344 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3345 u64 dbr_type, int budget) 3346 { 3347 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3348 int i; 3349 3350 for (i = 0; i < cpr->cp_ring_count; i++) { 3351 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3352 struct bnxt_db_info *db; 3353 3354 if (cpr2->had_work_done) { 3355 u32 tgl = 0; 3356 3357 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3358 cpr2->had_nqe_notify = 0; 3359 tgl = cpr2->toggle; 3360 } 3361 db = &cpr2->cp_db; 3362 bnxt_writeq(bp, 3363 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3364 DB_RING_IDX(db, cpr2->cp_raw_cons), 3365 db->doorbell); 3366 cpr2->had_work_done = 0; 3367 } 3368 } 3369 __bnxt_poll_work_done(bp, bnapi, budget); 3370 } 3371 3372 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3373 { 3374 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3375 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3376 struct bnxt_cp_ring_info *cpr_rx; 3377 u32 raw_cons = cpr->cp_raw_cons; 3378 struct bnxt *bp = bnapi->bp; 3379 struct nqe_cn *nqcmp; 3380 int work_done = 0; 3381 u32 cons; 3382 3383 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3384 napi_complete(napi); 3385 return 0; 3386 } 3387 if (cpr->has_more_work) { 3388 cpr->has_more_work = 0; 3389 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3390 } 3391 while (1) { 3392 u16 type; 3393 3394 cons = RING_CMP(raw_cons); 3395 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3396 3397 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3398 if (cpr->has_more_work) 3399 break; 3400 3401 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3402 budget); 3403 cpr->cp_raw_cons = raw_cons; 3404 if (napi_complete_done(napi, work_done)) 3405 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3406 cpr->cp_raw_cons); 3407 goto poll_done; 3408 } 3409 3410 /* The valid test of the entry must be done first before 3411 * reading any further. 3412 */ 3413 dma_rmb(); 3414 3415 type = le16_to_cpu(nqcmp->type); 3416 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3417 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3418 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3419 struct bnxt_cp_ring_info *cpr2; 3420 3421 /* No more budget for RX work */ 3422 if (budget && work_done >= budget && 3423 cq_type == BNXT_NQ_HDL_TYPE_RX) 3424 break; 3425 3426 idx = BNXT_NQ_HDL_IDX(idx); 3427 cpr2 = &cpr->cp_ring_arr[idx]; 3428 cpr2->had_nqe_notify = 1; 3429 cpr2->toggle = NQE_CN_TOGGLE(type); 3430 work_done += __bnxt_poll_work(bp, cpr2, 3431 budget - work_done); 3432 cpr->has_more_work |= cpr2->has_more_work; 3433 } else { 3434 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3435 } 3436 raw_cons = NEXT_RAW_CMP(raw_cons); 3437 } 3438 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3439 if (raw_cons != cpr->cp_raw_cons) { 3440 cpr->cp_raw_cons = raw_cons; 3441 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3442 } 3443 poll_done: 3444 cpr_rx = &cpr->cp_ring_arr[0]; 3445 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3446 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3447 struct dim_sample dim_sample = {}; 3448 3449 dim_update_sample(cpr->event_ctr, 3450 cpr_rx->rx_packets, 3451 cpr_rx->rx_bytes, 3452 &dim_sample); 3453 net_dim(&cpr->dim, &dim_sample); 3454 } 3455 return work_done; 3456 } 3457 3458 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp, 3459 struct bnxt_tx_ring_info *txr, int idx) 3460 { 3461 int i, max_idx; 3462 struct pci_dev *pdev = bp->pdev; 3463 unsigned int dma_len; 3464 dma_addr_t dma_addr; 3465 3466 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3467 3468 for (i = 0; i < max_idx;) { 3469 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i]; 3470 struct bnxt_sw_tx_bd *head_buf = tx_buf; 3471 struct sk_buff *skb; 3472 int j, last; 3473 3474 if (idx < bp->tx_nr_rings_xdp && 3475 tx_buf->action == XDP_REDIRECT) { 3476 dma_addr = dma_unmap_addr(tx_buf, mapping); 3477 dma_len = dma_unmap_len(tx_buf, len); 3478 3479 dma_unmap_single(&pdev->dev, dma_addr, dma_len, 3480 DMA_TO_DEVICE); 3481 xdp_return_frame(tx_buf->xdpf); 3482 tx_buf->action = 0; 3483 tx_buf->xdpf = NULL; 3484 i++; 3485 continue; 3486 } 3487 3488 skb = tx_buf->skb; 3489 if (!skb) { 3490 i++; 3491 continue; 3492 } 3493 3494 tx_buf->skb = NULL; 3495 3496 if (tx_buf->is_push) { 3497 dev_kfree_skb(skb); 3498 i += 2; 3499 continue; 3500 } 3501 3502 if (dma_unmap_len(tx_buf, len)) { 3503 dma_addr = dma_unmap_addr(tx_buf, mapping); 3504 dma_len = dma_unmap_len(tx_buf, len); 3505 3506 dma_unmap_single(&pdev->dev, dma_addr, dma_len, 3507 DMA_TO_DEVICE); 3508 } 3509 3510 last = tx_buf->nr_frags; 3511 i += 2; 3512 for (j = 0; j < last; j++, i++) { 3513 int ring_idx = i & bp->tx_ring_mask; 3514 3515 tx_buf = &txr->tx_buf_ring[ring_idx]; 3516 if (dma_unmap_len(tx_buf, len)) { 3517 dma_addr = dma_unmap_addr(tx_buf, mapping); 3518 dma_len = dma_unmap_len(tx_buf, len); 3519 3520 netmem_dma_unmap_page_attrs(&pdev->dev, 3521 dma_addr, dma_len, 3522 DMA_TO_DEVICE, 0); 3523 } 3524 } 3525 if (head_buf->is_sw_gso) { 3526 u16 inline_cons = txr->tx_inline_cons + 1; 3527 3528 WRITE_ONCE(txr->tx_inline_cons, inline_cons); 3529 if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) { 3530 tso_dma_map_complete(&pdev->dev, 3531 &head_buf->sw_gso_cstate); 3532 } else { 3533 skb = NULL; 3534 } 3535 head_buf->is_sw_gso = 0; 3536 } 3537 if (skb) 3538 dev_kfree_skb(skb); 3539 } 3540 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx)); 3541 } 3542 3543 static void bnxt_free_tx_skbs(struct bnxt *bp) 3544 { 3545 int i; 3546 3547 if (!bp->tx_ring) 3548 return; 3549 3550 for (i = 0; i < bp->tx_nr_rings; i++) { 3551 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3552 3553 if (!txr->tx_buf_ring) 3554 continue; 3555 3556 bnxt_free_one_tx_ring_skbs(bp, txr, i); 3557 } 3558 3559 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 3560 bnxt_ptp_free_txts_skbs(bp->ptp_cfg); 3561 } 3562 3563 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3564 { 3565 int i, max_idx; 3566 3567 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3568 3569 for (i = 0; i < max_idx; i++) { 3570 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3571 void *data = rx_buf->data; 3572 3573 if (!data) 3574 continue; 3575 3576 rx_buf->data = NULL; 3577 if (BNXT_RX_PAGE_MODE(bp)) 3578 page_pool_recycle_direct(rxr->page_pool, data); 3579 else 3580 page_pool_free_va(rxr->head_pool, data, true); 3581 } 3582 } 3583 3584 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3585 { 3586 int i, max_idx; 3587 3588 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3589 3590 for (i = 0; i < max_idx; i++) { 3591 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3592 netmem_ref netmem = rx_agg_buf->netmem; 3593 3594 if (!netmem) 3595 continue; 3596 3597 rx_agg_buf->netmem = 0; 3598 __clear_bit(i, rxr->rx_agg_bmap); 3599 3600 page_pool_recycle_direct_netmem(rxr->page_pool, netmem); 3601 } 3602 } 3603 3604 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3605 struct bnxt_rx_ring_info *rxr) 3606 { 3607 int i; 3608 3609 for (i = 0; i < bp->max_tpa_roundup_size; i++) { 3610 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3611 u8 *data = tpa_info->data; 3612 3613 if (!data) 3614 continue; 3615 3616 tpa_info->data = NULL; 3617 page_pool_free_va(rxr->head_pool, data, false); 3618 } 3619 } 3620 3621 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3622 struct bnxt_rx_ring_info *rxr) 3623 { 3624 struct bnxt_tpa_idx_map *map; 3625 3626 if (!rxr->rx_tpa) 3627 goto skip_rx_tpa_free; 3628 3629 bnxt_free_one_tpa_info_data(bp, rxr); 3630 3631 skip_rx_tpa_free: 3632 if (!rxr->rx_buf_ring) 3633 goto skip_rx_buf_free; 3634 3635 bnxt_free_one_rx_ring(bp, rxr); 3636 3637 skip_rx_buf_free: 3638 if (!rxr->rx_agg_ring) 3639 goto skip_rx_agg_free; 3640 3641 bnxt_free_one_rx_agg_ring(bp, rxr); 3642 3643 skip_rx_agg_free: 3644 map = rxr->rx_tpa_idx_map; 3645 if (map) 3646 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3647 } 3648 3649 static void bnxt_free_rx_skbs(struct bnxt *bp) 3650 { 3651 int i; 3652 3653 if (!bp->rx_ring) 3654 return; 3655 3656 for (i = 0; i < bp->rx_nr_rings; i++) 3657 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3658 } 3659 3660 static void bnxt_free_skbs(struct bnxt *bp) 3661 { 3662 bnxt_free_tx_skbs(bp); 3663 bnxt_free_rx_skbs(bp); 3664 } 3665 3666 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3667 { 3668 u8 init_val = ctxm->init_value; 3669 u16 offset = ctxm->init_offset; 3670 u8 *p2 = p; 3671 int i; 3672 3673 if (!init_val) 3674 return; 3675 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3676 memset(p, init_val, len); 3677 return; 3678 } 3679 for (i = 0; i < len; i += ctxm->entry_size) 3680 *(p2 + i + offset) = init_val; 3681 } 3682 3683 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3684 void *buf, size_t offset, size_t head, 3685 size_t tail) 3686 { 3687 int i, head_page, start_idx, source_offset; 3688 size_t len, rem_len, total_len, max_bytes; 3689 3690 head_page = head / rmem->page_size; 3691 source_offset = head % rmem->page_size; 3692 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3693 if (!total_len) 3694 total_len = MAX_CTX_BYTES; 3695 start_idx = head_page % MAX_CTX_PAGES; 3696 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3697 source_offset; 3698 total_len = min(total_len, max_bytes); 3699 rem_len = total_len; 3700 3701 for (i = start_idx; rem_len; i++, source_offset = 0) { 3702 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3703 if (buf) 3704 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3705 len); 3706 offset += len; 3707 rem_len -= len; 3708 } 3709 return total_len; 3710 } 3711 3712 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3713 { 3714 struct pci_dev *pdev = bp->pdev; 3715 int i; 3716 3717 if (!rmem->pg_arr) 3718 goto skip_pages; 3719 3720 for (i = 0; i < rmem->nr_pages; i++) { 3721 if (!rmem->pg_arr[i]) 3722 continue; 3723 3724 dma_free_coherent(&pdev->dev, rmem->page_size, 3725 rmem->pg_arr[i], rmem->dma_arr[i]); 3726 3727 rmem->pg_arr[i] = NULL; 3728 } 3729 skip_pages: 3730 if (rmem->pg_tbl) { 3731 size_t pg_tbl_size = rmem->nr_pages * 8; 3732 3733 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3734 pg_tbl_size = rmem->page_size; 3735 dma_free_coherent(&pdev->dev, pg_tbl_size, 3736 rmem->pg_tbl, rmem->pg_tbl_map); 3737 rmem->pg_tbl = NULL; 3738 } 3739 if (rmem->vmem_size && *rmem->vmem) { 3740 vfree(*rmem->vmem); 3741 *rmem->vmem = NULL; 3742 } 3743 } 3744 3745 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3746 { 3747 struct pci_dev *pdev = bp->pdev; 3748 u64 valid_bit = 0; 3749 int i; 3750 3751 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3752 valid_bit = PTU_PTE_VALID; 3753 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3754 size_t pg_tbl_size = rmem->nr_pages * 8; 3755 3756 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3757 pg_tbl_size = rmem->page_size; 3758 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3759 &rmem->pg_tbl_map, 3760 GFP_KERNEL); 3761 if (!rmem->pg_tbl) 3762 return -ENOMEM; 3763 } 3764 3765 for (i = 0; i < rmem->nr_pages; i++) { 3766 u64 extra_bits = valid_bit; 3767 3768 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3769 rmem->page_size, 3770 &rmem->dma_arr[i], 3771 GFP_KERNEL); 3772 if (!rmem->pg_arr[i]) 3773 return -ENOMEM; 3774 3775 if (rmem->ctx_mem) 3776 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3777 rmem->page_size); 3778 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3779 if (i == rmem->nr_pages - 2 && 3780 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3781 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3782 else if (i == rmem->nr_pages - 1 && 3783 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3784 extra_bits |= PTU_PTE_LAST; 3785 rmem->pg_tbl[i] = 3786 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3787 } 3788 } 3789 3790 if (rmem->vmem_size) { 3791 *rmem->vmem = vzalloc(rmem->vmem_size); 3792 if (!(*rmem->vmem)) 3793 return -ENOMEM; 3794 } 3795 return 0; 3796 } 3797 3798 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3799 struct bnxt_rx_ring_info *rxr) 3800 { 3801 int i; 3802 3803 kfree(rxr->rx_tpa_idx_map); 3804 rxr->rx_tpa_idx_map = NULL; 3805 if (rxr->rx_tpa) { 3806 for (i = 0; i < bp->max_tpa_roundup_size; i++) { 3807 kfree(rxr->rx_tpa[i].agg_arr); 3808 rxr->rx_tpa[i].agg_arr = NULL; 3809 } 3810 } 3811 kfree(rxr->rx_tpa); 3812 rxr->rx_tpa = NULL; 3813 } 3814 3815 static void bnxt_free_tpa_info(struct bnxt *bp) 3816 { 3817 int i; 3818 3819 for (i = 0; i < bp->rx_nr_rings; i++) { 3820 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3821 3822 bnxt_free_one_tpa_info(bp, rxr); 3823 } 3824 } 3825 3826 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3827 struct bnxt_rx_ring_info *rxr) 3828 { 3829 struct rx_agg_cmp *agg; 3830 int i; 3831 3832 rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, 3833 bp->max_tpa_roundup_size); 3834 if (!rxr->rx_tpa) 3835 return -ENOMEM; 3836 3837 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3838 return 0; 3839 for (i = 0; i < bp->max_tpa_roundup_size; i++) { 3840 agg = kzalloc_objs(*agg, MAX_SKB_FRAGS); 3841 if (!agg) 3842 return -ENOMEM; 3843 rxr->rx_tpa[i].agg_arr = agg; 3844 } 3845 rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map); 3846 if (!rxr->rx_tpa_idx_map) 3847 return -ENOMEM; 3848 3849 return 0; 3850 } 3851 3852 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3853 { 3854 int i, rc; 3855 3856 bp->max_tpa = MAX_TPA; 3857 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3858 /* TPA is not supported at all, so there is nothing to 3859 * allocate. 3860 */ 3861 if (!bp->max_tpa_v2) 3862 return 0; 3863 bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3864 /* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */ 3865 if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp)) 3866 bp->max_tpa = MAX_TPA_P5; 3867 } 3868 bp->max_tpa_roundup_size = roundup_pow_of_two(bp->max_tpa); 3869 3870 for (i = 0; i < bp->rx_nr_rings; i++) { 3871 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3872 3873 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3874 if (rc) 3875 return rc; 3876 } 3877 return 0; 3878 } 3879 3880 static void bnxt_free_rx_rings(struct bnxt *bp) 3881 { 3882 int i; 3883 3884 if (!bp->rx_ring) 3885 return; 3886 3887 bnxt_free_tpa_info(bp); 3888 for (i = 0; i < bp->rx_nr_rings; i++) { 3889 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3890 struct bnxt_ring_struct *ring; 3891 3892 if (rxr->xdp_prog) 3893 bpf_prog_put(rxr->xdp_prog); 3894 3895 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3896 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3897 3898 page_pool_destroy(rxr->page_pool); 3899 page_pool_destroy(rxr->head_pool); 3900 rxr->page_pool = rxr->head_pool = NULL; 3901 3902 kfree(rxr->rx_agg_bmap); 3903 rxr->rx_agg_bmap = NULL; 3904 3905 ring = &rxr->rx_ring_struct; 3906 bnxt_free_ring(bp, &ring->ring_mem); 3907 3908 ring = &rxr->rx_agg_ring_struct; 3909 bnxt_free_ring(bp, &ring->ring_mem); 3910 } 3911 } 3912 3913 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp, 3914 struct bnxt_rx_ring_info *rxr) 3915 { 3916 /* User may have chosen larger than default rx_page_size, 3917 * we keep the ring sizes uniform and also want uniform amount 3918 * of bytes consumed per ring, so cap how much of the rings we fill. 3919 */ 3920 int fill_level = bp->rx_agg_ring_size; 3921 3922 if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE) 3923 fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE; 3924 3925 return fill_level; 3926 } 3927 3928 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3929 struct bnxt_rx_ring_info *rxr, 3930 int numa_node) 3931 { 3932 unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE; 3933 const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K; 3934 struct page_pool_params pp = { 0 }; 3935 struct page_pool *pool; 3936 3937 pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac; 3938 if (BNXT_RX_PAGE_MODE(bp)) 3939 pp.pool_size += bp->rx_ring_size / rx_size_fac; 3940 3941 pp.order = get_order(rxr->rx_page_size); 3942 pp.nid = numa_node; 3943 pp.netdev = bp->dev; 3944 pp.dev = &bp->pdev->dev; 3945 pp.dma_dir = bp->rx_dir; 3946 pp.max_len = PAGE_SIZE << pp.order; 3947 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | 3948 PP_FLAG_ALLOW_UNREADABLE_NETMEM; 3949 pp.queue_idx = rxr->bnapi->index; 3950 3951 pool = page_pool_create(&pp); 3952 if (IS_ERR(pool)) 3953 return PTR_ERR(pool); 3954 rxr->page_pool = pool; 3955 3956 rxr->need_head_pool = page_pool_is_unreadable(pool); 3957 rxr->need_head_pool |= !!pp.order; 3958 if (bnxt_separate_head_pool(rxr)) { 3959 pp.order = 0; 3960 pp.max_len = PAGE_SIZE; 3961 pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024); 3962 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3963 pool = page_pool_create(&pp); 3964 if (IS_ERR(pool)) 3965 goto err_destroy_pp; 3966 } else { 3967 page_pool_get(pool); 3968 } 3969 rxr->head_pool = pool; 3970 3971 return 0; 3972 3973 err_destroy_pp: 3974 page_pool_destroy(rxr->page_pool); 3975 rxr->page_pool = NULL; 3976 return PTR_ERR(pool); 3977 } 3978 3979 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr) 3980 { 3981 page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi); 3982 page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi); 3983 } 3984 3985 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3986 { 3987 u16 mem_size; 3988 3989 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3990 mem_size = rxr->rx_agg_bmap_size / 8; 3991 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3992 if (!rxr->rx_agg_bmap) 3993 return -ENOMEM; 3994 3995 return 0; 3996 } 3997 3998 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3999 { 4000 int numa_node = dev_to_node(&bp->pdev->dev); 4001 int i, rc = 0, agg_rings = 0, cpu; 4002 4003 if (!bp->rx_ring) 4004 return -ENOMEM; 4005 4006 if (bp->flags & BNXT_FLAG_AGG_RINGS) 4007 agg_rings = 1; 4008 4009 for (i = 0; i < bp->rx_nr_rings; i++) { 4010 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 4011 struct bnxt_ring_struct *ring; 4012 int cpu_node; 4013 4014 ring = &rxr->rx_ring_struct; 4015 4016 cpu = cpumask_local_spread(i, numa_node); 4017 cpu_node = cpu_to_node(cpu); 4018 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 4019 i, cpu_node); 4020 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 4021 if (rc) 4022 return rc; 4023 bnxt_enable_rx_page_pool(rxr); 4024 4025 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 4026 if (rc < 0) 4027 return rc; 4028 4029 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 4030 MEM_TYPE_PAGE_POOL, 4031 rxr->page_pool); 4032 if (rc) { 4033 xdp_rxq_info_unreg(&rxr->xdp_rxq); 4034 return rc; 4035 } 4036 4037 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4038 if (rc) 4039 return rc; 4040 4041 ring->grp_idx = i; 4042 if (agg_rings) { 4043 ring = &rxr->rx_agg_ring_struct; 4044 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4045 if (rc) 4046 return rc; 4047 4048 ring->grp_idx = i; 4049 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 4050 if (rc) 4051 return rc; 4052 } 4053 } 4054 if (bp->flags & BNXT_FLAG_TPA) 4055 rc = bnxt_alloc_tpa_info(bp); 4056 return rc; 4057 } 4058 4059 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr, 4060 struct pci_dev *pdev) 4061 { 4062 if (!txr->tx_inline_buf) 4063 return; 4064 4065 dma_unmap_single(&pdev->dev, txr->tx_inline_dma, 4066 txr->tx_inline_size, DMA_TO_DEVICE); 4067 kfree(txr->tx_inline_buf); 4068 txr->tx_inline_buf = NULL; 4069 txr->tx_inline_size = 0; 4070 } 4071 4072 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr, 4073 struct pci_dev *pdev, 4074 unsigned int size) 4075 { 4076 txr->tx_inline_buf = kmalloc(size, GFP_KERNEL); 4077 if (!txr->tx_inline_buf) 4078 return -ENOMEM; 4079 4080 txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf, 4081 size, DMA_TO_DEVICE); 4082 if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) { 4083 kfree(txr->tx_inline_buf); 4084 txr->tx_inline_buf = NULL; 4085 return -ENOMEM; 4086 } 4087 txr->tx_inline_size = size; 4088 4089 return 0; 4090 } 4091 4092 static void bnxt_free_tx_rings(struct bnxt *bp) 4093 { 4094 int i; 4095 struct pci_dev *pdev = bp->pdev; 4096 4097 if (!bp->tx_ring) 4098 return; 4099 4100 for (i = 0; i < bp->tx_nr_rings; i++) { 4101 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4102 struct bnxt_ring_struct *ring; 4103 4104 if (txr->tx_push) { 4105 dma_free_coherent(&pdev->dev, bp->tx_push_size, 4106 txr->tx_push, txr->tx_push_mapping); 4107 txr->tx_push = NULL; 4108 } 4109 4110 bnxt_free_tx_inline_buf(txr, pdev); 4111 4112 ring = &txr->tx_ring_struct; 4113 4114 bnxt_free_ring(bp, &ring->ring_mem); 4115 } 4116 } 4117 4118 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 4119 ((tc) * (bp)->tx_nr_rings_per_tc) 4120 4121 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 4122 ((tx) % (bp)->tx_nr_rings_per_tc) 4123 4124 #define BNXT_RING_TO_TC(bp, tx) \ 4125 ((tx) / (bp)->tx_nr_rings_per_tc) 4126 4127 static int bnxt_alloc_tx_rings(struct bnxt *bp) 4128 { 4129 int i, j, rc; 4130 struct pci_dev *pdev = bp->pdev; 4131 4132 bp->tx_push_size = 0; 4133 if (bp->tx_push_thresh) { 4134 int push_size; 4135 4136 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 4137 bp->tx_push_thresh); 4138 4139 if (push_size > 256) { 4140 push_size = 0; 4141 bp->tx_push_thresh = 0; 4142 } 4143 4144 bp->tx_push_size = push_size; 4145 } 4146 4147 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 4148 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4149 struct bnxt_ring_struct *ring; 4150 u8 qidx; 4151 4152 ring = &txr->tx_ring_struct; 4153 4154 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4155 if (rc) 4156 return rc; 4157 4158 ring->grp_idx = txr->bnapi->index; 4159 if (bp->tx_push_size) { 4160 dma_addr_t mapping; 4161 4162 /* One pre-allocated DMA buffer to backup 4163 * TX push operation 4164 */ 4165 txr->tx_push = dma_alloc_coherent(&pdev->dev, 4166 bp->tx_push_size, 4167 &txr->tx_push_mapping, 4168 GFP_KERNEL); 4169 4170 if (!txr->tx_push) 4171 return -ENOMEM; 4172 4173 mapping = txr->tx_push_mapping + 4174 sizeof(struct tx_push_bd); 4175 txr->data_mapping = cpu_to_le64(mapping); 4176 } 4177 if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) { 4178 rc = bnxt_alloc_tx_inline_buf(txr, pdev, 4179 BNXT_SW_USO_MAX_SEGS * 4180 TSO_HEADER_SIZE); 4181 if (rc) 4182 return rc; 4183 } 4184 qidx = bp->tc_to_qidx[j]; 4185 ring->queue_id = bp->q_info[qidx].queue_id; 4186 spin_lock_init(&txr->xdp_tx_lock); 4187 if (i < bp->tx_nr_rings_xdp) 4188 continue; 4189 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 4190 j++; 4191 } 4192 return 0; 4193 } 4194 4195 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 4196 { 4197 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4198 4199 kfree(cpr->cp_desc_ring); 4200 cpr->cp_desc_ring = NULL; 4201 ring->ring_mem.pg_arr = NULL; 4202 kfree(cpr->cp_desc_mapping); 4203 cpr->cp_desc_mapping = NULL; 4204 ring->ring_mem.dma_arr = NULL; 4205 } 4206 4207 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 4208 { 4209 cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n); 4210 if (!cpr->cp_desc_ring) 4211 return -ENOMEM; 4212 cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n); 4213 if (!cpr->cp_desc_mapping) 4214 return -ENOMEM; 4215 return 0; 4216 } 4217 4218 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 4219 { 4220 int i; 4221 4222 if (!bp->bnapi) 4223 return; 4224 for (i = 0; i < bp->cp_nr_rings; i++) { 4225 struct bnxt_napi *bnapi = bp->bnapi[i]; 4226 4227 if (!bnapi) 4228 continue; 4229 bnxt_free_cp_arrays(&bnapi->cp_ring); 4230 } 4231 } 4232 4233 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 4234 { 4235 int i, n = bp->cp_nr_pages; 4236 4237 for (i = 0; i < bp->cp_nr_rings; i++) { 4238 struct bnxt_napi *bnapi = bp->bnapi[i]; 4239 int rc; 4240 4241 if (!bnapi) 4242 continue; 4243 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 4244 if (rc) 4245 return rc; 4246 } 4247 return 0; 4248 } 4249 4250 static void bnxt_free_cp_rings(struct bnxt *bp) 4251 { 4252 int i; 4253 4254 if (!bp->bnapi) 4255 return; 4256 4257 for (i = 0; i < bp->cp_nr_rings; i++) { 4258 struct bnxt_napi *bnapi = bp->bnapi[i]; 4259 struct bnxt_cp_ring_info *cpr; 4260 struct bnxt_ring_struct *ring; 4261 int j; 4262 4263 if (!bnapi) 4264 continue; 4265 4266 cpr = &bnapi->cp_ring; 4267 ring = &cpr->cp_ring_struct; 4268 4269 bnxt_free_ring(bp, &ring->ring_mem); 4270 4271 if (!cpr->cp_ring_arr) 4272 continue; 4273 4274 for (j = 0; j < cpr->cp_ring_count; j++) { 4275 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4276 4277 ring = &cpr2->cp_ring_struct; 4278 bnxt_free_ring(bp, &ring->ring_mem); 4279 bnxt_free_cp_arrays(cpr2); 4280 } 4281 kfree(cpr->cp_ring_arr); 4282 cpr->cp_ring_arr = NULL; 4283 cpr->cp_ring_count = 0; 4284 } 4285 } 4286 4287 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4288 struct bnxt_cp_ring_info *cpr) 4289 { 4290 struct bnxt_ring_mem_info *rmem; 4291 struct bnxt_ring_struct *ring; 4292 int rc; 4293 4294 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4295 if (rc) { 4296 bnxt_free_cp_arrays(cpr); 4297 return -ENOMEM; 4298 } 4299 ring = &cpr->cp_ring_struct; 4300 rmem = &ring->ring_mem; 4301 rmem->nr_pages = bp->cp_nr_pages; 4302 rmem->page_size = HW_CMPD_RING_SIZE; 4303 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4304 rmem->dma_arr = cpr->cp_desc_mapping; 4305 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4306 rc = bnxt_alloc_ring(bp, rmem); 4307 if (rc) { 4308 bnxt_free_ring(bp, rmem); 4309 bnxt_free_cp_arrays(cpr); 4310 } 4311 return rc; 4312 } 4313 4314 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4315 { 4316 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4317 int i, j, rc, ulp_msix; 4318 int tcs = bp->num_tc; 4319 4320 if (!tcs) 4321 tcs = 1; 4322 ulp_msix = bnxt_get_ulp_msix_num(bp); 4323 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4324 struct bnxt_napi *bnapi = bp->bnapi[i]; 4325 struct bnxt_cp_ring_info *cpr, *cpr2; 4326 struct bnxt_ring_struct *ring; 4327 int cp_count = 0, k; 4328 int rx = 0, tx = 0; 4329 4330 if (!bnapi) 4331 continue; 4332 4333 cpr = &bnapi->cp_ring; 4334 cpr->bnapi = bnapi; 4335 ring = &cpr->cp_ring_struct; 4336 4337 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4338 if (rc) 4339 return rc; 4340 4341 ring->map_idx = ulp_msix + i; 4342 4343 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4344 continue; 4345 4346 if (i < bp->rx_nr_rings) { 4347 cp_count++; 4348 rx = 1; 4349 } 4350 if (i < bp->tx_nr_rings_xdp) { 4351 cp_count++; 4352 tx = 1; 4353 } else if ((sh && i < bp->tx_nr_rings) || 4354 (!sh && i >= bp->rx_nr_rings)) { 4355 cp_count += tcs; 4356 tx = 1; 4357 } 4358 4359 cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count); 4360 if (!cpr->cp_ring_arr) 4361 return -ENOMEM; 4362 cpr->cp_ring_count = cp_count; 4363 4364 for (k = 0; k < cp_count; k++) { 4365 cpr2 = &cpr->cp_ring_arr[k]; 4366 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4367 if (rc) 4368 return rc; 4369 cpr2->bnapi = bnapi; 4370 cpr2->sw_stats = cpr->sw_stats; 4371 cpr2->cp_idx = k; 4372 if (!k && rx) { 4373 bp->rx_ring[i].rx_cpr = cpr2; 4374 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4375 } else { 4376 int n, tc = k - rx; 4377 4378 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4379 bp->tx_ring[n].tx_cpr = cpr2; 4380 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4381 } 4382 } 4383 if (tx) 4384 j++; 4385 } 4386 return 0; 4387 } 4388 4389 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4390 struct bnxt_rx_ring_info *rxr) 4391 { 4392 struct bnxt_ring_mem_info *rmem; 4393 struct bnxt_ring_struct *ring; 4394 4395 ring = &rxr->rx_ring_struct; 4396 rmem = &ring->ring_mem; 4397 rmem->nr_pages = bp->rx_nr_pages; 4398 rmem->page_size = HW_RXBD_RING_SIZE; 4399 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4400 rmem->dma_arr = rxr->rx_desc_mapping; 4401 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4402 rmem->vmem = (void **)&rxr->rx_buf_ring; 4403 4404 ring = &rxr->rx_agg_ring_struct; 4405 rmem = &ring->ring_mem; 4406 rmem->nr_pages = bp->rx_agg_nr_pages; 4407 rmem->page_size = HW_RXBD_RING_SIZE; 4408 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4409 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4410 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4411 rmem->vmem = (void **)&rxr->rx_agg_ring; 4412 } 4413 4414 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4415 struct bnxt_rx_ring_info *rxr) 4416 { 4417 struct bnxt_ring_mem_info *rmem; 4418 struct bnxt_ring_struct *ring; 4419 int i; 4420 4421 rxr->page_pool->p.napi = NULL; 4422 rxr->page_pool = NULL; 4423 rxr->head_pool->p.napi = NULL; 4424 rxr->head_pool = NULL; 4425 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4426 4427 ring = &rxr->rx_ring_struct; 4428 rmem = &ring->ring_mem; 4429 rmem->pg_tbl = NULL; 4430 rmem->pg_tbl_map = 0; 4431 for (i = 0; i < rmem->nr_pages; i++) { 4432 rmem->pg_arr[i] = NULL; 4433 rmem->dma_arr[i] = 0; 4434 } 4435 *rmem->vmem = NULL; 4436 4437 ring = &rxr->rx_agg_ring_struct; 4438 rmem = &ring->ring_mem; 4439 rmem->pg_tbl = NULL; 4440 rmem->pg_tbl_map = 0; 4441 for (i = 0; i < rmem->nr_pages; i++) { 4442 rmem->pg_arr[i] = NULL; 4443 rmem->dma_arr[i] = 0; 4444 } 4445 *rmem->vmem = NULL; 4446 } 4447 4448 static void bnxt_init_ring_struct(struct bnxt *bp) 4449 { 4450 int i, j; 4451 4452 for (i = 0; i < bp->cp_nr_rings; i++) { 4453 struct bnxt_napi *bnapi = bp->bnapi[i]; 4454 struct netdev_queue_config qcfg; 4455 struct bnxt_ring_mem_info *rmem; 4456 struct bnxt_cp_ring_info *cpr; 4457 struct bnxt_rx_ring_info *rxr; 4458 struct bnxt_tx_ring_info *txr; 4459 struct bnxt_ring_struct *ring; 4460 4461 if (!bnapi) 4462 continue; 4463 4464 cpr = &bnapi->cp_ring; 4465 ring = &cpr->cp_ring_struct; 4466 rmem = &ring->ring_mem; 4467 rmem->nr_pages = bp->cp_nr_pages; 4468 rmem->page_size = HW_CMPD_RING_SIZE; 4469 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4470 rmem->dma_arr = cpr->cp_desc_mapping; 4471 rmem->vmem_size = 0; 4472 4473 rxr = bnapi->rx_ring; 4474 if (!rxr) 4475 goto skip_rx; 4476 4477 netdev_queue_config(bp->dev, i, &qcfg); 4478 rxr->rx_page_size = qcfg.rx_page_size; 4479 4480 ring = &rxr->rx_ring_struct; 4481 rmem = &ring->ring_mem; 4482 rmem->nr_pages = bp->rx_nr_pages; 4483 rmem->page_size = HW_RXBD_RING_SIZE; 4484 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4485 rmem->dma_arr = rxr->rx_desc_mapping; 4486 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4487 rmem->vmem = (void **)&rxr->rx_buf_ring; 4488 4489 ring = &rxr->rx_agg_ring_struct; 4490 rmem = &ring->ring_mem; 4491 rmem->nr_pages = bp->rx_agg_nr_pages; 4492 rmem->page_size = HW_RXBD_RING_SIZE; 4493 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4494 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4495 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4496 rmem->vmem = (void **)&rxr->rx_agg_ring; 4497 4498 skip_rx: 4499 bnxt_for_each_napi_tx(j, bnapi, txr) { 4500 ring = &txr->tx_ring_struct; 4501 rmem = &ring->ring_mem; 4502 rmem->nr_pages = bp->tx_nr_pages; 4503 rmem->page_size = HW_TXBD_RING_SIZE; 4504 rmem->pg_arr = (void **)txr->tx_desc_ring; 4505 rmem->dma_arr = txr->tx_desc_mapping; 4506 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4507 rmem->vmem = (void **)&txr->tx_buf_ring; 4508 } 4509 } 4510 } 4511 4512 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4513 { 4514 int i; 4515 u32 prod; 4516 struct rx_bd **rx_buf_ring; 4517 4518 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4519 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4520 int j; 4521 struct rx_bd *rxbd; 4522 4523 rxbd = rx_buf_ring[i]; 4524 if (!rxbd) 4525 continue; 4526 4527 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4528 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4529 rxbd->rx_bd_opaque = prod; 4530 } 4531 } 4532 } 4533 4534 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4535 struct bnxt_rx_ring_info *rxr, 4536 int ring_nr) 4537 { 4538 u32 prod; 4539 int i; 4540 4541 prod = rxr->rx_prod; 4542 for (i = 0; i < bp->rx_ring_size; i++) { 4543 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4544 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4545 ring_nr, i, bp->rx_ring_size); 4546 break; 4547 } 4548 prod = NEXT_RX(prod); 4549 } 4550 rxr->rx_prod = prod; 4551 } 4552 4553 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp, 4554 struct bnxt_rx_ring_info *rxr, 4555 int ring_nr) 4556 { 4557 int fill_level, i; 4558 u32 prod; 4559 4560 fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr); 4561 4562 prod = rxr->rx_agg_prod; 4563 for (i = 0; i < fill_level; i++) { 4564 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) { 4565 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4566 ring_nr, i, bp->rx_agg_ring_size); 4567 break; 4568 } 4569 prod = NEXT_RX_AGG(prod); 4570 } 4571 rxr->rx_agg_prod = prod; 4572 } 4573 4574 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4575 struct bnxt_rx_ring_info *rxr) 4576 { 4577 dma_addr_t mapping; 4578 u8 *data; 4579 int i; 4580 4581 for (i = 0; i < bp->max_tpa_roundup_size; i++) { 4582 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4583 GFP_KERNEL); 4584 if (!data) 4585 return -ENOMEM; 4586 4587 rxr->rx_tpa[i].data = data; 4588 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4589 rxr->rx_tpa[i].mapping = mapping; 4590 } 4591 4592 return 0; 4593 } 4594 4595 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4596 { 4597 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4598 int rc; 4599 4600 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4601 4602 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4603 return 0; 4604 4605 bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr); 4606 4607 if (rxr->rx_tpa) { 4608 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4609 if (rc) 4610 return rc; 4611 } 4612 return 0; 4613 } 4614 4615 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4616 struct bnxt_rx_ring_info *rxr) 4617 { 4618 struct bnxt_ring_struct *ring; 4619 u32 type; 4620 4621 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4622 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4623 4624 if (NET_IP_ALIGN == 2) 4625 type |= RX_BD_FLAGS_SOP; 4626 4627 ring = &rxr->rx_ring_struct; 4628 bnxt_init_rxbd_pages(ring, type); 4629 ring->fw_ring_id = INVALID_HW_RING_ID; 4630 } 4631 4632 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4633 struct bnxt_rx_ring_info *rxr) 4634 { 4635 struct bnxt_ring_struct *ring; 4636 u32 type; 4637 4638 ring = &rxr->rx_agg_ring_struct; 4639 ring->fw_ring_id = INVALID_HW_RING_ID; 4640 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4641 type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) | 4642 RX_BD_TYPE_RX_AGG_BD; 4643 4644 /* Disable EOP if TPA is enabled to prevent overlapping zero 4645 * padding with the next segment's data. On P7_PLUS, EOP will 4646 * automatically disable Relaxed Ordering (RO) to prevent 4647 * potential data corruption (and may degrade performance). On 4648 * older chips, RO will not be automatically disabled and may 4649 * cause corruption. 4650 */ 4651 if (!(bp->flags & BNXT_FLAG_TPA)) 4652 type |= RX_BD_FLAGS_AGG_EOP; 4653 4654 bnxt_init_rxbd_pages(ring, type); 4655 } 4656 } 4657 4658 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4659 { 4660 struct bnxt_rx_ring_info *rxr; 4661 4662 rxr = &bp->rx_ring[ring_nr]; 4663 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4664 4665 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4666 &rxr->bnapi->napi); 4667 4668 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4669 bpf_prog_add(bp->xdp_prog, 1); 4670 rxr->xdp_prog = bp->xdp_prog; 4671 } 4672 4673 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4674 4675 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4676 } 4677 4678 static void bnxt_init_cp_rings(struct bnxt *bp) 4679 { 4680 int i, j; 4681 4682 for (i = 0; i < bp->cp_nr_rings; i++) { 4683 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4684 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4685 4686 ring->fw_ring_id = INVALID_HW_RING_ID; 4687 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4688 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4689 if (!cpr->cp_ring_arr) 4690 continue; 4691 for (j = 0; j < cpr->cp_ring_count; j++) { 4692 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4693 4694 ring = &cpr2->cp_ring_struct; 4695 ring->fw_ring_id = INVALID_HW_RING_ID; 4696 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4697 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4698 } 4699 } 4700 } 4701 4702 static int bnxt_init_rx_rings(struct bnxt *bp) 4703 { 4704 int i, rc = 0; 4705 4706 if (BNXT_RX_PAGE_MODE(bp)) { 4707 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4708 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4709 } else { 4710 bp->rx_offset = BNXT_RX_OFFSET; 4711 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4712 } 4713 4714 for (i = 0; i < bp->rx_nr_rings; i++) { 4715 rc = bnxt_init_one_rx_ring(bp, i); 4716 if (rc) 4717 break; 4718 } 4719 4720 return rc; 4721 } 4722 4723 static int bnxt_init_tx_rings(struct bnxt *bp) 4724 { 4725 netdev_features_t features; 4726 u16 i; 4727 4728 features = bp->dev->features; 4729 4730 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4731 bnxt_min_tx_desc_cnt(bp, features)); 4732 4733 for (i = 0; i < bp->tx_nr_rings; i++) { 4734 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4735 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4736 4737 ring->fw_ring_id = INVALID_HW_RING_ID; 4738 4739 if (i >= bp->tx_nr_rings_xdp) 4740 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4741 NETDEV_QUEUE_TYPE_TX, 4742 &txr->bnapi->napi); 4743 } 4744 4745 return 0; 4746 } 4747 4748 static void bnxt_free_ring_grps(struct bnxt *bp) 4749 { 4750 kfree(bp->grp_info); 4751 bp->grp_info = NULL; 4752 } 4753 4754 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4755 { 4756 int i; 4757 4758 if (irq_re_init) { 4759 bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info, 4760 bp->cp_nr_rings); 4761 if (!bp->grp_info) 4762 return -ENOMEM; 4763 } 4764 for (i = 0; i < bp->cp_nr_rings; i++) { 4765 if (irq_re_init) 4766 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4767 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4768 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4769 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4770 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4771 } 4772 return 0; 4773 } 4774 4775 static void bnxt_free_vnics(struct bnxt *bp) 4776 { 4777 kfree(bp->vnic_info); 4778 bp->vnic_info = NULL; 4779 bp->nr_vnics = 0; 4780 } 4781 4782 static int bnxt_alloc_vnics(struct bnxt *bp) 4783 { 4784 int num_vnics = 1; 4785 4786 #ifdef CONFIG_RFS_ACCEL 4787 if (bp->flags & BNXT_FLAG_RFS) { 4788 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4789 num_vnics++; 4790 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4791 num_vnics += bp->rx_nr_rings; 4792 } 4793 #endif 4794 4795 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4796 num_vnics++; 4797 4798 bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics); 4799 if (!bp->vnic_info) 4800 return -ENOMEM; 4801 4802 bp->nr_vnics = num_vnics; 4803 return 0; 4804 } 4805 4806 static void bnxt_init_vnics(struct bnxt *bp) 4807 { 4808 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4809 int i; 4810 4811 for (i = 0; i < bp->nr_vnics; i++) { 4812 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4813 int j; 4814 4815 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4816 vnic->vnic_id = i; 4817 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4818 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4819 4820 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4821 4822 if (bp->vnic_info[i].rss_hash_key) { 4823 if (i == BNXT_VNIC_DEFAULT) { 4824 u8 *key = (void *)vnic->rss_hash_key; 4825 int k; 4826 4827 if (!bp->rss_hash_key_valid && 4828 !bp->rss_hash_key_updated) { 4829 get_random_bytes(bp->rss_hash_key, 4830 HW_HASH_KEY_SIZE); 4831 bp->rss_hash_key_updated = true; 4832 } 4833 4834 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4835 HW_HASH_KEY_SIZE); 4836 4837 if (!bp->rss_hash_key_updated) 4838 continue; 4839 4840 bp->rss_hash_key_updated = false; 4841 bp->rss_hash_key_valid = true; 4842 4843 bp->toeplitz_prefix = 0; 4844 for (k = 0; k < 8; k++) { 4845 bp->toeplitz_prefix <<= 8; 4846 bp->toeplitz_prefix |= key[k]; 4847 } 4848 } else { 4849 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4850 HW_HASH_KEY_SIZE); 4851 } 4852 } 4853 } 4854 } 4855 4856 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4857 { 4858 int pages; 4859 4860 pages = ring_size / desc_per_pg; 4861 4862 if (!pages) 4863 return 1; 4864 4865 pages++; 4866 4867 while (pages & (pages - 1)) 4868 pages++; 4869 4870 return pages; 4871 } 4872 4873 void bnxt_set_tpa_flags(struct bnxt *bp) 4874 { 4875 bp->flags &= ~BNXT_FLAG_TPA; 4876 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4877 return; 4878 if (bp->dev->features & NETIF_F_LRO) 4879 bp->flags |= BNXT_FLAG_LRO; 4880 else if (bp->dev->features & NETIF_F_GRO_HW) 4881 bp->flags |= BNXT_FLAG_GRO; 4882 } 4883 4884 static void bnxt_init_ring_params(struct bnxt *bp) 4885 { 4886 unsigned int rx_size; 4887 4888 bp->rx_copybreak = 0; /* rx-copybreak disabled by default */ 4889 /* Try to fit 4 chunks into a 4k page */ 4890 rx_size = SZ_1K - 4891 NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4892 bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size); 4893 } 4894 4895 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4896 * be set on entry. 4897 */ 4898 void bnxt_set_ring_params(struct bnxt *bp) 4899 { 4900 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4901 u32 agg_factor = 0, agg_ring_size = 0; 4902 4903 /* 8 for CRC and VLAN */ 4904 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4905 4906 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4907 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4908 4909 ring_size = bp->rx_ring_size; 4910 bp->rx_agg_ring_size = 0; 4911 bp->rx_agg_nr_pages = 0; 4912 4913 if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS) 4914 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4915 4916 bp->flags &= ~BNXT_FLAG_JUMBO; 4917 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4918 u32 jumbo_factor; 4919 4920 bp->flags |= BNXT_FLAG_JUMBO; 4921 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4922 if (jumbo_factor > agg_factor) 4923 agg_factor = jumbo_factor; 4924 } 4925 if (agg_factor) { 4926 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4927 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4928 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4929 bp->rx_ring_size, ring_size); 4930 bp->rx_ring_size = ring_size; 4931 } 4932 agg_ring_size = ring_size * agg_factor; 4933 4934 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4935 RX_DESC_CNT); 4936 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4937 u32 tmp = agg_ring_size; 4938 4939 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4940 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4941 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4942 tmp, agg_ring_size); 4943 } 4944 bp->rx_agg_ring_size = agg_ring_size; 4945 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4946 4947 if (BNXT_RX_PAGE_MODE(bp)) { 4948 rx_space = PAGE_SIZE; 4949 rx_size = PAGE_SIZE - 4950 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4951 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4952 } else { 4953 rx_size = max3(BNXT_MIN_RX_HDR_BUF, 4954 bp->rx_copybreak, 4955 bp->dev->cfg_pending->hds_thresh); 4956 rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN); 4957 rx_space = rx_size + NET_SKB_PAD + 4958 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4959 } 4960 } 4961 4962 bp->rx_buf_use_size = rx_size; 4963 bp->rx_buf_size = rx_space; 4964 4965 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4966 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4967 4968 ring_size = bp->tx_ring_size; 4969 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4970 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4971 4972 max_rx_cmpl = bp->rx_ring_size; 4973 /* MAX TPA needs to be added because TPA_START completions are 4974 * immediately recycled, so the TPA completions are not bound by 4975 * the RX ring size. 4976 */ 4977 if (bp->flags & BNXT_FLAG_TPA) 4978 max_rx_cmpl += bp->max_tpa; 4979 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4980 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4981 bp->cp_ring_size = ring_size; 4982 4983 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4984 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4985 bp->cp_nr_pages = MAX_CP_PAGES; 4986 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4987 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4988 ring_size, bp->cp_ring_size); 4989 } 4990 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4991 bp->cp_ring_mask = bp->cp_bit - 1; 4992 } 4993 4994 /* Changing allocation mode of RX rings. 4995 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4996 */ 4997 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4998 { 4999 struct net_device *dev = bp->dev; 5000 5001 if (page_mode) { 5002 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 5003 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 5004 5005 if (bp->xdp_prog->aux->xdp_has_frags) 5006 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 5007 else 5008 dev->max_mtu = 5009 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 5010 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 5011 bp->flags |= BNXT_FLAG_JUMBO; 5012 bp->rx_skb_func = bnxt_rx_multi_page_skb; 5013 } else { 5014 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 5015 bp->rx_skb_func = bnxt_rx_page_skb; 5016 } 5017 bp->rx_dir = DMA_BIDIRECTIONAL; 5018 } else { 5019 dev->max_mtu = bp->max_mtu; 5020 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 5021 bp->rx_dir = DMA_FROM_DEVICE; 5022 bp->rx_skb_func = bnxt_rx_skb; 5023 } 5024 } 5025 5026 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 5027 { 5028 __bnxt_set_rx_skb_mode(bp, page_mode); 5029 5030 if (!page_mode) { 5031 int rx, tx; 5032 5033 bnxt_get_max_rings(bp, &rx, &tx, true); 5034 if (rx > 1) { 5035 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 5036 if (BNXT_SUPPORTS_TPA(bp)) 5037 bp->dev->hw_features |= NETIF_F_LRO; 5038 } 5039 } 5040 5041 /* Update LRO and GRO_HW availability */ 5042 netdev_update_features(bp->dev); 5043 } 5044 5045 static void bnxt_free_vnic_attributes(struct bnxt *bp) 5046 { 5047 int i; 5048 struct bnxt_vnic_info *vnic; 5049 struct pci_dev *pdev = bp->pdev; 5050 5051 if (!bp->vnic_info) 5052 return; 5053 5054 for (i = 0; i < bp->nr_vnics; i++) { 5055 vnic = &bp->vnic_info[i]; 5056 5057 kfree(vnic->fw_grp_ids); 5058 vnic->fw_grp_ids = NULL; 5059 5060 kfree(vnic->uc_list); 5061 vnic->uc_list = NULL; 5062 5063 if (vnic->mc_list) { 5064 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 5065 vnic->mc_list, vnic->mc_list_mapping); 5066 vnic->mc_list = NULL; 5067 } 5068 5069 if (vnic->rss_table) { 5070 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 5071 vnic->rss_table, 5072 vnic->rss_table_dma_addr); 5073 vnic->rss_table = NULL; 5074 } 5075 5076 vnic->rss_hash_key = NULL; 5077 vnic->flags = 0; 5078 } 5079 } 5080 5081 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 5082 { 5083 int i, rc = 0, size; 5084 struct bnxt_vnic_info *vnic; 5085 struct pci_dev *pdev = bp->pdev; 5086 int max_rings; 5087 5088 for (i = 0; i < bp->nr_vnics; i++) { 5089 vnic = &bp->vnic_info[i]; 5090 5091 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 5092 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 5093 5094 if (mem_size > 0) { 5095 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 5096 if (!vnic->uc_list) { 5097 rc = -ENOMEM; 5098 goto out; 5099 } 5100 } 5101 } 5102 5103 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 5104 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 5105 vnic->mc_list = 5106 dma_alloc_coherent(&pdev->dev, 5107 vnic->mc_list_size, 5108 &vnic->mc_list_mapping, 5109 GFP_KERNEL); 5110 if (!vnic->mc_list) { 5111 rc = -ENOMEM; 5112 goto out; 5113 } 5114 } 5115 5116 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5117 goto vnic_skip_grps; 5118 5119 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 5120 max_rings = bp->rx_nr_rings; 5121 else 5122 max_rings = 1; 5123 5124 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 5125 if (!vnic->fw_grp_ids) { 5126 rc = -ENOMEM; 5127 goto out; 5128 } 5129 vnic_skip_grps: 5130 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 5131 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 5132 continue; 5133 5134 /* Allocate rss table and hash key */ 5135 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 5136 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5137 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 5138 5139 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 5140 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 5141 vnic->rss_table_size, 5142 &vnic->rss_table_dma_addr, 5143 GFP_KERNEL); 5144 if (!vnic->rss_table) { 5145 rc = -ENOMEM; 5146 goto out; 5147 } 5148 5149 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 5150 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 5151 } 5152 return 0; 5153 5154 out: 5155 return rc; 5156 } 5157 5158 static void bnxt_free_hwrm_resources(struct bnxt *bp) 5159 { 5160 struct bnxt_hwrm_wait_token *token; 5161 5162 dma_pool_destroy(bp->hwrm_dma_pool); 5163 bp->hwrm_dma_pool = NULL; 5164 5165 rcu_read_lock(); 5166 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 5167 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 5168 rcu_read_unlock(); 5169 } 5170 5171 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 5172 { 5173 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 5174 BNXT_HWRM_DMA_SIZE, 5175 BNXT_HWRM_DMA_ALIGN, 0); 5176 if (!bp->hwrm_dma_pool) 5177 return -ENOMEM; 5178 5179 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 5180 5181 return 0; 5182 } 5183 5184 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 5185 { 5186 kfree(stats->hw_masks); 5187 stats->hw_masks = NULL; 5188 kfree(stats->sw_stats); 5189 stats->sw_stats = NULL; 5190 if (stats->hw_stats) { 5191 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 5192 stats->hw_stats_map); 5193 stats->hw_stats = NULL; 5194 } 5195 } 5196 5197 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 5198 bool alloc_masks) 5199 { 5200 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 5201 &stats->hw_stats_map, GFP_KERNEL); 5202 if (!stats->hw_stats) 5203 return -ENOMEM; 5204 5205 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 5206 if (!stats->sw_stats) 5207 goto stats_mem_err; 5208 5209 if (alloc_masks) { 5210 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 5211 if (!stats->hw_masks) 5212 goto stats_mem_err; 5213 } 5214 return 0; 5215 5216 stats_mem_err: 5217 bnxt_free_stats_mem(bp, stats); 5218 return -ENOMEM; 5219 } 5220 5221 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 5222 { 5223 int i; 5224 5225 for (i = 0; i < count; i++) 5226 mask_arr[i] = mask; 5227 } 5228 5229 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 5230 { 5231 int i; 5232 5233 for (i = 0; i < count; i++) 5234 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 5235 } 5236 5237 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 5238 struct bnxt_stats_mem *stats) 5239 { 5240 struct hwrm_func_qstats_ext_output *resp; 5241 struct hwrm_func_qstats_ext_input *req; 5242 __le64 *hw_masks; 5243 int rc; 5244 5245 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 5246 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5247 return -EOPNOTSUPP; 5248 5249 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 5250 if (rc) 5251 return rc; 5252 5253 req->fid = cpu_to_le16(0xffff); 5254 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5255 5256 resp = hwrm_req_hold(bp, req); 5257 rc = hwrm_req_send(bp, req); 5258 if (!rc) { 5259 hw_masks = &resp->rx_ucast_pkts; 5260 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 5261 } 5262 hwrm_req_drop(bp, req); 5263 return rc; 5264 } 5265 5266 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 5267 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 5268 5269 static void bnxt_init_stats(struct bnxt *bp) 5270 { 5271 struct bnxt_napi *bnapi = bp->bnapi[0]; 5272 struct bnxt_cp_ring_info *cpr; 5273 struct bnxt_stats_mem *stats; 5274 __le64 *rx_stats, *tx_stats; 5275 int rc, rx_count, tx_count; 5276 u64 *rx_masks, *tx_masks; 5277 u64 mask; 5278 u8 flags; 5279 5280 cpr = &bnapi->cp_ring; 5281 stats = &cpr->stats; 5282 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 5283 if (rc) { 5284 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5285 mask = (1ULL << 48) - 1; 5286 else 5287 mask = -1ULL; 5288 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 5289 } 5290 if (bp->flags & BNXT_FLAG_PORT_STATS) { 5291 stats = &bp->port_stats; 5292 rx_stats = stats->hw_stats; 5293 rx_masks = stats->hw_masks; 5294 rx_count = sizeof(struct rx_port_stats) / 8; 5295 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5296 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5297 tx_count = sizeof(struct tx_port_stats) / 8; 5298 5299 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 5300 rc = bnxt_hwrm_port_qstats(bp, flags); 5301 if (rc) { 5302 mask = (1ULL << 40) - 1; 5303 5304 bnxt_fill_masks(rx_masks, mask, rx_count); 5305 bnxt_fill_masks(tx_masks, mask, tx_count); 5306 } else { 5307 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5308 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5309 bnxt_hwrm_port_qstats(bp, 0); 5310 } 5311 } 5312 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5313 stats = &bp->rx_port_stats_ext; 5314 rx_stats = stats->hw_stats; 5315 rx_masks = stats->hw_masks; 5316 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5317 stats = &bp->tx_port_stats_ext; 5318 tx_stats = stats->hw_stats; 5319 tx_masks = stats->hw_masks; 5320 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5321 5322 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5323 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5324 if (rc) { 5325 mask = (1ULL << 40) - 1; 5326 5327 bnxt_fill_masks(rx_masks, mask, rx_count); 5328 if (tx_stats) 5329 bnxt_fill_masks(tx_masks, mask, tx_count); 5330 } else { 5331 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5332 if (tx_stats) 5333 bnxt_copy_hw_masks(tx_masks, tx_stats, 5334 tx_count); 5335 bnxt_hwrm_port_qstats_ext(bp, 0); 5336 } 5337 } 5338 } 5339 5340 static void bnxt_free_port_stats(struct bnxt *bp) 5341 { 5342 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5343 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5344 5345 bnxt_free_stats_mem(bp, &bp->port_stats); 5346 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5347 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5348 } 5349 5350 static void bnxt_free_ring_stats(struct bnxt *bp) 5351 { 5352 int i; 5353 5354 if (!bp->bnapi) 5355 return; 5356 5357 for (i = 0; i < bp->cp_nr_rings; i++) { 5358 struct bnxt_napi *bnapi = bp->bnapi[i]; 5359 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5360 5361 bnxt_free_stats_mem(bp, &cpr->stats); 5362 5363 kfree(cpr->sw_stats); 5364 cpr->sw_stats = NULL; 5365 } 5366 } 5367 5368 static int bnxt_alloc_stats(struct bnxt *bp) 5369 { 5370 u32 size, i; 5371 int rc; 5372 5373 size = bp->hw_ring_stats_size; 5374 5375 for (i = 0; i < bp->cp_nr_rings; i++) { 5376 struct bnxt_napi *bnapi = bp->bnapi[i]; 5377 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5378 5379 cpr->sw_stats = kzalloc_obj(*cpr->sw_stats); 5380 if (!cpr->sw_stats) 5381 return -ENOMEM; 5382 5383 cpr->stats.len = size; 5384 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5385 if (rc) 5386 return rc; 5387 5388 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5389 } 5390 5391 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5392 return 0; 5393 5394 if (bp->port_stats.hw_stats) 5395 goto alloc_ext_stats; 5396 5397 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5398 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5399 if (rc) 5400 return rc; 5401 5402 bp->flags |= BNXT_FLAG_PORT_STATS; 5403 5404 alloc_ext_stats: 5405 /* Display extended statistics only if FW supports it */ 5406 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5407 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5408 return 0; 5409 5410 if (bp->rx_port_stats_ext.hw_stats) 5411 goto alloc_tx_ext_stats; 5412 5413 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5414 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5415 /* Extended stats are optional */ 5416 if (rc) 5417 return 0; 5418 5419 alloc_tx_ext_stats: 5420 if (bp->tx_port_stats_ext.hw_stats) 5421 return 0; 5422 5423 if (bp->hwrm_spec_code >= 0x10902 || 5424 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5425 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5426 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5427 /* Extended stats are optional */ 5428 if (rc) 5429 return 0; 5430 } 5431 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5432 return 0; 5433 } 5434 5435 static void bnxt_clear_ring_indices(struct bnxt *bp) 5436 { 5437 int i, j; 5438 5439 if (!bp->bnapi) 5440 return; 5441 5442 for (i = 0; i < bp->cp_nr_rings; i++) { 5443 struct bnxt_napi *bnapi = bp->bnapi[i]; 5444 struct bnxt_cp_ring_info *cpr; 5445 struct bnxt_rx_ring_info *rxr; 5446 struct bnxt_tx_ring_info *txr; 5447 5448 if (!bnapi) 5449 continue; 5450 5451 cpr = &bnapi->cp_ring; 5452 cpr->cp_raw_cons = 0; 5453 5454 bnxt_for_each_napi_tx(j, bnapi, txr) { 5455 txr->tx_prod = 0; 5456 txr->tx_cons = 0; 5457 txr->tx_hw_cons = 0; 5458 txr->kick_pending = 0; 5459 txr->kick_txbd0 = NULL; 5460 } 5461 5462 rxr = bnapi->rx_ring; 5463 if (rxr) { 5464 rxr->rx_prod = 0; 5465 rxr->rx_agg_prod = 0; 5466 rxr->rx_sw_agg_prod = 0; 5467 rxr->rx_next_cons = 0; 5468 } 5469 bnapi->events = 0; 5470 } 5471 } 5472 5473 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5474 { 5475 u8 type = fltr->type, flags = fltr->flags; 5476 5477 INIT_LIST_HEAD(&fltr->list); 5478 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5479 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5480 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5481 } 5482 5483 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5484 { 5485 if (!list_empty(&fltr->list)) 5486 list_del_init(&fltr->list); 5487 } 5488 5489 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5490 { 5491 struct bnxt_filter_base *usr_fltr, *tmp; 5492 5493 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5494 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5495 continue; 5496 bnxt_del_one_usr_fltr(bp, usr_fltr); 5497 } 5498 } 5499 5500 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5501 { 5502 hlist_del(&fltr->hash); 5503 bnxt_del_one_usr_fltr(bp, fltr); 5504 if (fltr->flags) { 5505 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5506 bp->ntp_fltr_count--; 5507 } 5508 kfree(fltr); 5509 } 5510 5511 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5512 { 5513 int i; 5514 5515 netdev_assert_locked_or_invisible(bp->dev); 5516 5517 /* Under netdev instance lock and all our NAPIs have been disabled. 5518 * It's safe to delete the hash table. 5519 */ 5520 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5521 struct hlist_head *head; 5522 struct hlist_node *tmp; 5523 struct bnxt_ntuple_filter *fltr; 5524 5525 head = &bp->ntp_fltr_hash_tbl[i]; 5526 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5527 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5528 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5529 !list_empty(&fltr->base.list))) 5530 continue; 5531 bnxt_del_fltr(bp, &fltr->base); 5532 } 5533 } 5534 if (!all) 5535 return; 5536 5537 bitmap_free(bp->ntp_fltr_bmap); 5538 bp->ntp_fltr_bmap = NULL; 5539 bp->ntp_fltr_count = 0; 5540 } 5541 5542 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5543 { 5544 int i, rc = 0; 5545 5546 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5547 return 0; 5548 5549 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5550 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5551 5552 bp->ntp_fltr_count = 0; 5553 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5554 5555 if (!bp->ntp_fltr_bmap) 5556 rc = -ENOMEM; 5557 5558 return rc; 5559 } 5560 5561 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5562 { 5563 int i; 5564 5565 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5566 struct hlist_head *head; 5567 struct hlist_node *tmp; 5568 struct bnxt_l2_filter *fltr; 5569 5570 head = &bp->l2_fltr_hash_tbl[i]; 5571 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5572 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5573 !list_empty(&fltr->base.list))) 5574 continue; 5575 bnxt_del_fltr(bp, &fltr->base); 5576 } 5577 } 5578 } 5579 5580 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5581 { 5582 int i; 5583 5584 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5585 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5586 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5587 } 5588 5589 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5590 { 5591 bnxt_free_vnic_attributes(bp); 5592 bnxt_free_tx_rings(bp); 5593 bnxt_free_rx_rings(bp); 5594 bnxt_free_cp_rings(bp); 5595 bnxt_free_all_cp_arrays(bp); 5596 bnxt_free_ntp_fltrs(bp, false); 5597 bnxt_free_l2_filters(bp, false); 5598 if (irq_re_init) { 5599 bnxt_free_ring_stats(bp); 5600 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5601 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5602 bnxt_free_port_stats(bp); 5603 bnxt_free_ring_grps(bp); 5604 bnxt_free_vnics(bp); 5605 kfree(bp->tx_ring_map); 5606 bp->tx_ring_map = NULL; 5607 kfree(bp->tx_ring); 5608 bp->tx_ring = NULL; 5609 kfree(bp->rx_ring); 5610 bp->rx_ring = NULL; 5611 kfree(bp->bnapi); 5612 bp->bnapi = NULL; 5613 } else { 5614 bnxt_clear_ring_indices(bp); 5615 } 5616 } 5617 5618 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5619 { 5620 int i, j, rc, size, arr_size; 5621 void *bnapi; 5622 5623 if (irq_re_init) { 5624 /* Allocate bnapi mem pointer array and mem block for 5625 * all queues 5626 */ 5627 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5628 bp->cp_nr_rings); 5629 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5630 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5631 if (!bnapi) 5632 return -ENOMEM; 5633 5634 bp->bnapi = bnapi; 5635 bnapi += arr_size; 5636 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5637 bp->bnapi[i] = bnapi; 5638 bp->bnapi[i]->index = i; 5639 bp->bnapi[i]->bp = bp; 5640 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5641 struct bnxt_cp_ring_info *cpr = 5642 &bp->bnapi[i]->cp_ring; 5643 5644 cpr->cp_ring_struct.ring_mem.flags = 5645 BNXT_RMEM_RING_PTE_FLAG; 5646 } 5647 } 5648 5649 bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info, 5650 bp->rx_nr_rings); 5651 if (!bp->rx_ring) 5652 return -ENOMEM; 5653 5654 for (i = 0; i < bp->rx_nr_rings; i++) { 5655 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5656 5657 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5658 rxr->rx_ring_struct.ring_mem.flags = 5659 BNXT_RMEM_RING_PTE_FLAG; 5660 rxr->rx_agg_ring_struct.ring_mem.flags = 5661 BNXT_RMEM_RING_PTE_FLAG; 5662 } else { 5663 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5664 } 5665 rxr->bnapi = bp->bnapi[i]; 5666 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5667 } 5668 5669 bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info, 5670 bp->tx_nr_rings); 5671 if (!bp->tx_ring) 5672 return -ENOMEM; 5673 5674 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5675 GFP_KERNEL); 5676 5677 if (!bp->tx_ring_map) 5678 return -ENOMEM; 5679 5680 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5681 j = 0; 5682 else 5683 j = bp->rx_nr_rings; 5684 5685 for (i = 0; i < bp->tx_nr_rings; i++) { 5686 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5687 struct bnxt_napi *bnapi2; 5688 5689 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5690 txr->tx_ring_struct.ring_mem.flags = 5691 BNXT_RMEM_RING_PTE_FLAG; 5692 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5693 if (i >= bp->tx_nr_rings_xdp) { 5694 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5695 5696 bnapi2 = bp->bnapi[k]; 5697 txr->txq_index = i - bp->tx_nr_rings_xdp; 5698 txr->tx_napi_idx = 5699 BNXT_RING_TO_TC(bp, txr->txq_index); 5700 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5701 bnapi2->tx_int = bnxt_tx_int; 5702 } else { 5703 bnapi2 = bp->bnapi[j]; 5704 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5705 bnapi2->tx_ring[0] = txr; 5706 bnapi2->tx_int = bnxt_tx_int_xdp; 5707 j++; 5708 } 5709 txr->bnapi = bnapi2; 5710 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5711 txr->tx_cpr = &bnapi2->cp_ring; 5712 } 5713 5714 rc = bnxt_alloc_stats(bp); 5715 if (rc) 5716 goto alloc_mem_err; 5717 bnxt_init_stats(bp); 5718 5719 rc = bnxt_alloc_ntp_fltrs(bp); 5720 if (rc) 5721 goto alloc_mem_err; 5722 5723 rc = bnxt_alloc_vnics(bp); 5724 if (rc) 5725 goto alloc_mem_err; 5726 } 5727 5728 rc = bnxt_alloc_all_cp_arrays(bp); 5729 if (rc) 5730 goto alloc_mem_err; 5731 5732 bnxt_init_ring_struct(bp); 5733 5734 rc = bnxt_alloc_rx_rings(bp); 5735 if (rc) 5736 goto alloc_mem_err; 5737 5738 rc = bnxt_alloc_tx_rings(bp); 5739 if (rc) 5740 goto alloc_mem_err; 5741 5742 rc = bnxt_alloc_cp_rings(bp); 5743 if (rc) 5744 goto alloc_mem_err; 5745 5746 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5747 BNXT_VNIC_MCAST_FLAG | 5748 BNXT_VNIC_UCAST_FLAG; 5749 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5750 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5751 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5752 5753 rc = bnxt_alloc_vnic_attributes(bp); 5754 if (rc) 5755 goto alloc_mem_err; 5756 return 0; 5757 5758 alloc_mem_err: 5759 bnxt_free_mem(bp, true); 5760 return rc; 5761 } 5762 5763 static void bnxt_disable_int(struct bnxt *bp) 5764 { 5765 int i; 5766 5767 if (!bp->bnapi) 5768 return; 5769 5770 for (i = 0; i < bp->cp_nr_rings; i++) { 5771 struct bnxt_napi *bnapi = bp->bnapi[i]; 5772 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5773 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5774 5775 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5776 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5777 } 5778 } 5779 5780 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5781 { 5782 struct bnxt_napi *bnapi = bp->bnapi[n]; 5783 struct bnxt_cp_ring_info *cpr; 5784 5785 cpr = &bnapi->cp_ring; 5786 return cpr->cp_ring_struct.map_idx; 5787 } 5788 5789 static void bnxt_disable_int_sync(struct bnxt *bp) 5790 { 5791 int i; 5792 5793 if (!bp->irq_tbl || !bp->bnapi) 5794 return; 5795 5796 atomic_inc(&bp->intr_sem); 5797 5798 bnxt_disable_int(bp); 5799 for (i = 0; i < bp->cp_nr_rings; i++) { 5800 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5801 5802 synchronize_irq(bp->irq_tbl[map_idx].vector); 5803 } 5804 } 5805 5806 static void bnxt_enable_int(struct bnxt *bp) 5807 { 5808 int i; 5809 5810 atomic_set(&bp->intr_sem, 0); 5811 for (i = 0; i < bp->cp_nr_rings; i++) { 5812 struct bnxt_napi *bnapi = bp->bnapi[i]; 5813 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5814 5815 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5816 } 5817 } 5818 5819 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5820 bool async_only) 5821 { 5822 DECLARE_BITMAP(async_events_bmap, 256); 5823 u32 *events = (u32 *)async_events_bmap; 5824 struct hwrm_func_drv_rgtr_output *resp; 5825 struct hwrm_func_drv_rgtr_input *req; 5826 u32 flags; 5827 int rc, i; 5828 5829 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5830 if (rc) 5831 return rc; 5832 5833 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5834 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5835 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5836 5837 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5838 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5839 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5840 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5841 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5842 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5843 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5844 if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2) 5845 flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT; 5846 req->flags = cpu_to_le32(flags); 5847 req->ver_maj_8b = DRV_VER_MAJ; 5848 req->ver_min_8b = DRV_VER_MIN; 5849 req->ver_upd_8b = DRV_VER_UPD; 5850 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5851 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5852 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5853 5854 if (BNXT_PF(bp)) { 5855 u32 data[8]; 5856 int i; 5857 5858 memset(data, 0, sizeof(data)); 5859 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5860 u16 cmd = bnxt_vf_req_snif[i]; 5861 unsigned int bit, idx; 5862 5863 if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) && 5864 cmd == HWRM_PORT_PHY_QCFG) 5865 continue; 5866 5867 idx = cmd / 32; 5868 bit = cmd % 32; 5869 data[idx] |= 1 << bit; 5870 } 5871 5872 for (i = 0; i < 8; i++) 5873 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5874 5875 req->enables |= 5876 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5877 } 5878 5879 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5880 req->flags |= cpu_to_le32( 5881 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5882 5883 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5884 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5885 u16 event_id = bnxt_async_events_arr[i]; 5886 5887 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5888 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5889 continue; 5890 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5891 !bp->ptp_cfg) 5892 continue; 5893 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5894 } 5895 if (bmap && bmap_size) { 5896 for (i = 0; i < bmap_size; i++) { 5897 if (test_bit(i, bmap)) 5898 __set_bit(i, async_events_bmap); 5899 } 5900 } 5901 for (i = 0; i < 8; i++) 5902 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5903 5904 if (async_only) 5905 req->enables = 5906 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5907 5908 resp = hwrm_req_hold(bp, req); 5909 rc = hwrm_req_send(bp, req); 5910 if (!rc) { 5911 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5912 if (resp->flags & 5913 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5914 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5915 } 5916 hwrm_req_drop(bp, req); 5917 return rc; 5918 } 5919 5920 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5921 { 5922 struct hwrm_func_drv_unrgtr_input *req; 5923 int rc; 5924 5925 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5926 return 0; 5927 5928 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5929 if (rc) 5930 return rc; 5931 return hwrm_req_send(bp, req); 5932 } 5933 5934 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5935 5936 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5937 { 5938 struct hwrm_tunnel_dst_port_free_input *req; 5939 int rc; 5940 5941 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5942 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5943 return 0; 5944 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5945 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5946 return 0; 5947 5948 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5949 if (rc) 5950 return rc; 5951 5952 req->tunnel_type = tunnel_type; 5953 5954 switch (tunnel_type) { 5955 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5956 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5957 bp->vxlan_port = 0; 5958 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5959 break; 5960 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5961 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5962 bp->nge_port = 0; 5963 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5964 break; 5965 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5966 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5967 bp->vxlan_gpe_port = 0; 5968 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5969 break; 5970 default: 5971 break; 5972 } 5973 5974 rc = hwrm_req_send(bp, req); 5975 if (rc) 5976 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5977 rc); 5978 if (bp->flags & BNXT_FLAG_TPA) 5979 bnxt_set_tpa(bp, true); 5980 return rc; 5981 } 5982 5983 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5984 u8 tunnel_type) 5985 { 5986 struct hwrm_tunnel_dst_port_alloc_output *resp; 5987 struct hwrm_tunnel_dst_port_alloc_input *req; 5988 int rc; 5989 5990 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5991 if (rc) 5992 return rc; 5993 5994 req->tunnel_type = tunnel_type; 5995 req->tunnel_dst_port_val = port; 5996 5997 resp = hwrm_req_hold(bp, req); 5998 rc = hwrm_req_send(bp, req); 5999 if (rc) { 6000 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 6001 rc); 6002 goto err_out; 6003 } 6004 6005 switch (tunnel_type) { 6006 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 6007 bp->vxlan_port = port; 6008 bp->vxlan_fw_dst_port_id = 6009 le16_to_cpu(resp->tunnel_dst_port_id); 6010 break; 6011 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 6012 bp->nge_port = port; 6013 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 6014 break; 6015 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 6016 bp->vxlan_gpe_port = port; 6017 bp->vxlan_gpe_fw_dst_port_id = 6018 le16_to_cpu(resp->tunnel_dst_port_id); 6019 break; 6020 default: 6021 break; 6022 } 6023 if (bp->flags & BNXT_FLAG_TPA) 6024 bnxt_set_tpa(bp, true); 6025 6026 err_out: 6027 hwrm_req_drop(bp, req); 6028 return rc; 6029 } 6030 6031 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 6032 { 6033 struct hwrm_cfa_l2_set_rx_mask_input *req; 6034 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 6035 int rc; 6036 6037 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 6038 if (rc) 6039 return rc; 6040 6041 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6042 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 6043 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 6044 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 6045 } 6046 req->mask = cpu_to_le32(vnic->rx_mask); 6047 return hwrm_req_send_silent(bp, req); 6048 } 6049 6050 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6051 { 6052 if (!atomic_dec_and_test(&fltr->refcnt)) 6053 return; 6054 spin_lock_bh(&bp->ntp_fltr_lock); 6055 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 6056 spin_unlock_bh(&bp->ntp_fltr_lock); 6057 return; 6058 } 6059 hlist_del_rcu(&fltr->base.hash); 6060 bnxt_del_one_usr_fltr(bp, &fltr->base); 6061 if (fltr->base.flags) { 6062 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 6063 bp->ntp_fltr_count--; 6064 } 6065 spin_unlock_bh(&bp->ntp_fltr_lock); 6066 kfree_rcu(fltr, base.rcu); 6067 } 6068 6069 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 6070 struct bnxt_l2_key *key, 6071 u32 idx) 6072 { 6073 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 6074 struct bnxt_l2_filter *fltr; 6075 6076 hlist_for_each_entry_rcu(fltr, head, base.hash) { 6077 struct bnxt_l2_key *l2_key = &fltr->l2_key; 6078 6079 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 6080 l2_key->vlan == key->vlan) 6081 return fltr; 6082 } 6083 return NULL; 6084 } 6085 6086 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 6087 struct bnxt_l2_key *key, 6088 u32 idx) 6089 { 6090 struct bnxt_l2_filter *fltr = NULL; 6091 6092 rcu_read_lock(); 6093 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6094 if (fltr) 6095 atomic_inc(&fltr->refcnt); 6096 rcu_read_unlock(); 6097 return fltr; 6098 } 6099 6100 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 6101 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 6102 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 6103 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 6104 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 6105 6106 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 6107 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 6108 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 6109 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 6110 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 6111 6112 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 6113 { 6114 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 6115 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 6116 return sizeof(fkeys->addrs.v4addrs) + 6117 sizeof(fkeys->ports); 6118 6119 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 6120 return sizeof(fkeys->addrs.v4addrs); 6121 } 6122 6123 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 6124 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 6125 return sizeof(fkeys->addrs.v6addrs) + 6126 sizeof(fkeys->ports); 6127 6128 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 6129 return sizeof(fkeys->addrs.v6addrs); 6130 } 6131 6132 return 0; 6133 } 6134 6135 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 6136 const unsigned char *key) 6137 { 6138 u64 prefix = bp->toeplitz_prefix, hash = 0; 6139 struct bnxt_ipv4_tuple tuple4; 6140 struct bnxt_ipv6_tuple tuple6; 6141 int i, j, len = 0; 6142 u8 *four_tuple; 6143 6144 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 6145 if (!len) 6146 return 0; 6147 6148 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 6149 tuple4.v4addrs = fkeys->addrs.v4addrs; 6150 tuple4.ports = fkeys->ports; 6151 four_tuple = (unsigned char *)&tuple4; 6152 } else { 6153 tuple6.v6addrs = fkeys->addrs.v6addrs; 6154 tuple6.ports = fkeys->ports; 6155 four_tuple = (unsigned char *)&tuple6; 6156 } 6157 6158 for (i = 0, j = 8; i < len; i++, j++) { 6159 u8 byte = four_tuple[i]; 6160 int bit; 6161 6162 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 6163 if (byte & 0x80) 6164 hash ^= prefix; 6165 } 6166 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 6167 } 6168 6169 /* The valid part of the hash is in the upper 32 bits. */ 6170 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 6171 } 6172 6173 #ifdef CONFIG_RFS_ACCEL 6174 static struct bnxt_l2_filter * 6175 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 6176 { 6177 struct bnxt_l2_filter *fltr; 6178 u32 idx; 6179 6180 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6181 BNXT_L2_FLTR_HASH_MASK; 6182 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6183 return fltr; 6184 } 6185 #endif 6186 6187 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 6188 struct bnxt_l2_key *key, u32 idx) 6189 { 6190 struct hlist_head *head; 6191 6192 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 6193 fltr->l2_key.vlan = key->vlan; 6194 fltr->base.type = BNXT_FLTR_TYPE_L2; 6195 if (fltr->base.flags) { 6196 int bit_id; 6197 6198 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 6199 bp->max_fltr, 0); 6200 if (bit_id < 0) 6201 return -ENOMEM; 6202 fltr->base.sw_id = (u16)bit_id; 6203 bp->ntp_fltr_count++; 6204 } 6205 head = &bp->l2_fltr_hash_tbl[idx]; 6206 hlist_add_head_rcu(&fltr->base.hash, head); 6207 bnxt_insert_usr_fltr(bp, &fltr->base); 6208 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 6209 atomic_set(&fltr->refcnt, 1); 6210 return 0; 6211 } 6212 6213 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 6214 struct bnxt_l2_key *key, 6215 gfp_t gfp) 6216 { 6217 struct bnxt_l2_filter *fltr; 6218 u32 idx; 6219 int rc; 6220 6221 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6222 BNXT_L2_FLTR_HASH_MASK; 6223 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6224 if (fltr) 6225 return fltr; 6226 6227 fltr = kzalloc_obj(*fltr, gfp); 6228 if (!fltr) 6229 return ERR_PTR(-ENOMEM); 6230 spin_lock_bh(&bp->ntp_fltr_lock); 6231 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6232 spin_unlock_bh(&bp->ntp_fltr_lock); 6233 if (rc) { 6234 bnxt_del_l2_filter(bp, fltr); 6235 fltr = ERR_PTR(rc); 6236 } 6237 return fltr; 6238 } 6239 6240 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 6241 struct bnxt_l2_key *key, 6242 u16 flags) 6243 { 6244 struct bnxt_l2_filter *fltr; 6245 u32 idx; 6246 int rc; 6247 6248 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6249 BNXT_L2_FLTR_HASH_MASK; 6250 spin_lock_bh(&bp->ntp_fltr_lock); 6251 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6252 if (fltr) { 6253 fltr = ERR_PTR(-EEXIST); 6254 goto l2_filter_exit; 6255 } 6256 fltr = kzalloc_obj(*fltr, GFP_ATOMIC); 6257 if (!fltr) { 6258 fltr = ERR_PTR(-ENOMEM); 6259 goto l2_filter_exit; 6260 } 6261 fltr->base.flags = flags; 6262 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6263 if (rc) { 6264 spin_unlock_bh(&bp->ntp_fltr_lock); 6265 bnxt_del_l2_filter(bp, fltr); 6266 return ERR_PTR(rc); 6267 } 6268 6269 l2_filter_exit: 6270 spin_unlock_bh(&bp->ntp_fltr_lock); 6271 return fltr; 6272 } 6273 6274 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 6275 { 6276 #ifdef CONFIG_BNXT_SRIOV 6277 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 6278 6279 return vf->fw_fid; 6280 #else 6281 return INVALID_HW_RING_ID; 6282 #endif 6283 } 6284 6285 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6286 { 6287 struct hwrm_cfa_l2_filter_free_input *req; 6288 u16 target_id = 0xffff; 6289 int rc; 6290 6291 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6292 struct bnxt_pf_info *pf = &bp->pf; 6293 6294 if (fltr->base.vf_idx >= pf->active_vfs) 6295 return -EINVAL; 6296 6297 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6298 if (target_id == INVALID_HW_RING_ID) 6299 return -EINVAL; 6300 } 6301 6302 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 6303 if (rc) 6304 return rc; 6305 6306 req->target_id = cpu_to_le16(target_id); 6307 req->l2_filter_id = fltr->base.filter_id; 6308 return hwrm_req_send(bp, req); 6309 } 6310 6311 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6312 { 6313 struct hwrm_cfa_l2_filter_alloc_output *resp; 6314 struct hwrm_cfa_l2_filter_alloc_input *req; 6315 u16 target_id = 0xffff; 6316 int rc; 6317 6318 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6319 struct bnxt_pf_info *pf = &bp->pf; 6320 6321 if (fltr->base.vf_idx >= pf->active_vfs) 6322 return -EINVAL; 6323 6324 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6325 } 6326 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6327 if (rc) 6328 return rc; 6329 6330 req->target_id = cpu_to_le16(target_id); 6331 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6332 6333 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6334 req->flags |= 6335 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6336 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6337 req->enables = 6338 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6339 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6340 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6341 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6342 eth_broadcast_addr(req->l2_addr_mask); 6343 6344 if (fltr->l2_key.vlan) { 6345 req->enables |= 6346 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6347 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6348 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6349 req->num_vlans = 1; 6350 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6351 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6352 } 6353 6354 resp = hwrm_req_hold(bp, req); 6355 rc = hwrm_req_send(bp, req); 6356 if (!rc) { 6357 fltr->base.filter_id = resp->l2_filter_id; 6358 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6359 } 6360 hwrm_req_drop(bp, req); 6361 return rc; 6362 } 6363 6364 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6365 struct bnxt_ntuple_filter *fltr) 6366 { 6367 struct hwrm_cfa_ntuple_filter_free_input *req; 6368 int rc; 6369 6370 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6371 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 6372 return 0; 6373 6374 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6375 if (rc) 6376 return rc; 6377 6378 req->ntuple_filter_id = fltr->base.filter_id; 6379 return hwrm_req_send(bp, req); 6380 } 6381 6382 #define BNXT_NTP_FLTR_FLAGS \ 6383 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6384 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6385 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6386 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6387 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6388 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6389 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6390 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6391 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6392 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6393 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6394 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6395 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6396 6397 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6398 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6399 6400 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6401 { 6402 int i; 6403 6404 for (i = 0; i < 4; i++) 6405 mask[i] = cpu_to_be32(~0); 6406 } 6407 6408 static void 6409 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6410 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6411 struct bnxt_ntuple_filter *fltr) 6412 { 6413 u16 rxq = fltr->base.rxq; 6414 6415 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6416 struct ethtool_rxfh_context *ctx; 6417 struct bnxt_rss_ctx *rss_ctx; 6418 struct bnxt_vnic_info *vnic; 6419 6420 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6421 fltr->base.fw_vnic_id); 6422 if (ctx) { 6423 rss_ctx = ethtool_rxfh_context_priv(ctx); 6424 vnic = &rss_ctx->vnic; 6425 6426 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6427 } 6428 return; 6429 } 6430 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6431 struct bnxt_vnic_info *vnic; 6432 u32 enables; 6433 6434 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6435 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6436 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6437 req->enables |= cpu_to_le32(enables); 6438 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6439 } else { 6440 u32 flags; 6441 6442 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6443 req->flags |= cpu_to_le32(flags); 6444 req->dst_id = cpu_to_le16(rxq); 6445 } 6446 } 6447 6448 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6449 struct bnxt_ntuple_filter *fltr) 6450 { 6451 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6452 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6453 struct bnxt_flow_masks *masks = &fltr->fmasks; 6454 struct flow_keys *keys = &fltr->fkeys; 6455 struct bnxt_l2_filter *l2_fltr; 6456 struct bnxt_vnic_info *vnic; 6457 int rc; 6458 6459 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6460 if (rc) 6461 return rc; 6462 6463 l2_fltr = fltr->l2_fltr; 6464 req->l2_filter_id = l2_fltr->base.filter_id; 6465 6466 if (fltr->base.flags & BNXT_ACT_DROP) { 6467 req->flags = 6468 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6469 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6470 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6471 } else { 6472 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6473 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6474 } 6475 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6476 6477 req->ethertype = htons(ETH_P_IP); 6478 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6479 req->ip_protocol = keys->basic.ip_proto; 6480 6481 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6482 req->ethertype = htons(ETH_P_IPV6); 6483 req->ip_addr_type = 6484 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6485 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6486 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6487 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6488 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6489 } else { 6490 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6491 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6492 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6493 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6494 } 6495 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6496 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6497 req->tunnel_type = 6498 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6499 } 6500 6501 req->src_port = keys->ports.src; 6502 req->src_port_mask = masks->ports.src; 6503 req->dst_port = keys->ports.dst; 6504 req->dst_port_mask = masks->ports.dst; 6505 6506 resp = hwrm_req_hold(bp, req); 6507 rc = hwrm_req_send(bp, req); 6508 if (!rc) 6509 fltr->base.filter_id = resp->ntuple_filter_id; 6510 hwrm_req_drop(bp, req); 6511 return rc; 6512 } 6513 6514 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6515 const u8 *mac_addr) 6516 { 6517 struct bnxt_l2_filter *fltr; 6518 struct bnxt_l2_key key; 6519 int rc; 6520 6521 ether_addr_copy(key.dst_mac_addr, mac_addr); 6522 key.vlan = 0; 6523 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6524 if (IS_ERR(fltr)) 6525 return PTR_ERR(fltr); 6526 6527 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6528 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6529 if (rc) 6530 bnxt_del_l2_filter(bp, fltr); 6531 else 6532 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6533 return rc; 6534 } 6535 6536 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6537 { 6538 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6539 6540 /* Any associated ntuple filters will also be cleared by firmware. */ 6541 for (i = 0; i < num_of_vnics; i++) { 6542 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6543 6544 for (j = 0; j < vnic->uc_filter_count; j++) { 6545 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6546 6547 bnxt_hwrm_l2_filter_free(bp, fltr); 6548 bnxt_del_l2_filter(bp, fltr); 6549 } 6550 vnic->uc_filter_count = 0; 6551 } 6552 } 6553 6554 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6555 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6556 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6557 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6558 6559 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6560 struct hwrm_vnic_tpa_cfg_input *req) 6561 { 6562 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6563 6564 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6565 return; 6566 6567 if (bp->vxlan_port) 6568 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6569 if (bp->vxlan_gpe_port) 6570 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6571 if (bp->nge_port) 6572 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6573 6574 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6575 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6576 } 6577 6578 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6579 u32 tpa_flags) 6580 { 6581 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6582 struct hwrm_vnic_tpa_cfg_input *req; 6583 int rc; 6584 6585 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6586 return 0; 6587 6588 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6589 if (rc) 6590 return rc; 6591 6592 if (tpa_flags) { 6593 u16 mss = bp->dev->mtu - 40; 6594 u32 nsegs, n, segs = 0, flags; 6595 6596 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6597 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6598 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6599 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6600 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6601 if (tpa_flags & BNXT_FLAG_GRO) 6602 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6603 6604 req->flags = cpu_to_le32(flags); 6605 6606 req->enables = 6607 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6608 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6609 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6610 6611 /* Number of segs are log2 units, and first packet is not 6612 * included as part of this units. 6613 */ 6614 if (mss <= BNXT_RX_PAGE_SIZE) { 6615 n = BNXT_RX_PAGE_SIZE / mss; 6616 nsegs = (MAX_SKB_FRAGS - 1) * n; 6617 } else { 6618 n = mss / BNXT_RX_PAGE_SIZE; 6619 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6620 n++; 6621 nsegs = (MAX_SKB_FRAGS - n) / n; 6622 } 6623 6624 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6625 segs = MAX_TPA_SEGS_P5; 6626 max_aggs = bp->max_tpa; 6627 } else { 6628 segs = ilog2(nsegs); 6629 } 6630 req->max_agg_segs = cpu_to_le16(segs); 6631 req->max_aggs = cpu_to_le16(max_aggs); 6632 6633 req->min_agg_len = cpu_to_le32(512); 6634 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6635 } 6636 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6637 6638 return hwrm_req_send(bp, req); 6639 } 6640 6641 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6642 { 6643 struct bnxt_ring_grp_info *grp_info; 6644 6645 grp_info = &bp->grp_info[ring->grp_idx]; 6646 return grp_info->cp_fw_ring_id; 6647 } 6648 6649 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6650 { 6651 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6652 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6653 else 6654 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6655 } 6656 6657 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6658 { 6659 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6660 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6661 else 6662 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6663 } 6664 6665 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6666 { 6667 int entries; 6668 6669 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6670 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6671 else 6672 entries = HW_HASH_INDEX_SIZE; 6673 6674 bp->rss_indir_tbl_entries = entries; 6675 bp->rss_indir_tbl = 6676 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6677 if (!bp->rss_indir_tbl) 6678 return -ENOMEM; 6679 6680 return 0; 6681 } 6682 6683 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6684 struct ethtool_rxfh_context *rss_ctx) 6685 { 6686 u16 max_rings, max_entries, pad, i; 6687 u32 *rss_indir_tbl; 6688 6689 if (!bp->rx_nr_rings) 6690 return; 6691 6692 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6693 max_rings = bp->rx_nr_rings - 1; 6694 else 6695 max_rings = bp->rx_nr_rings; 6696 6697 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6698 if (rss_ctx) 6699 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6700 else 6701 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6702 6703 for (i = 0; i < max_entries; i++) 6704 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6705 6706 pad = bp->rss_indir_tbl_entries - max_entries; 6707 if (pad) 6708 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6709 } 6710 6711 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6712 { 6713 u32 i, tbl_size, max_ring = 0; 6714 6715 if (!bp->rss_indir_tbl) 6716 return 0; 6717 6718 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6719 for (i = 0; i < tbl_size; i++) 6720 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6721 return max_ring; 6722 } 6723 6724 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6725 { 6726 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6727 if (!rx_rings) 6728 return 0; 6729 if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) 6730 return BNXT_RSS_TABLE_MAX_TBL_P5; 6731 6732 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6733 BNXT_RSS_TABLE_ENTRIES_P5); 6734 } 6735 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6736 return 2; 6737 return 1; 6738 } 6739 6740 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6741 { 6742 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6743 u16 i, j, min_j = bp->rx_nr_rings - 1; 6744 6745 if (!vnic->rss_table) 6746 goto skip_rss_tbl; 6747 6748 /* Fill the RSS indirection table with ring group ids */ 6749 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6750 if (!no_rss) 6751 j = bp->rss_indir_tbl[i]; 6752 min_j = min(j, min_j); 6753 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6754 } 6755 6756 skip_rss_tbl: 6757 if (vnic->rss_table && !no_rss) 6758 vnic->default_rx_ring = min_j; 6759 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6760 vnic->default_rx_ring = vnic->vnic_id - 1; 6761 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6762 vnic->default_rx_ring = bp->rx_nr_rings - 1; 6763 else 6764 vnic->default_rx_ring = 0; 6765 } 6766 6767 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6768 struct bnxt_vnic_info *vnic) 6769 { 6770 u16 tbl_size, i, min_j = bp->rx_nr_rings - 1; 6771 __le16 *ring_tbl = vnic->rss_table; 6772 struct bnxt_rx_ring_info *rxr; 6773 6774 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6775 6776 for (i = 0; i < tbl_size; i++) { 6777 u16 ring_id, j; 6778 6779 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6780 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6781 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6782 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6783 else 6784 j = bp->rss_indir_tbl[i]; 6785 min_j = min(j, min_j); 6786 rxr = &bp->rx_ring[j]; 6787 6788 ring_id = rxr->rx_ring_struct.fw_ring_id; 6789 *ring_tbl++ = cpu_to_le16(ring_id); 6790 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6791 *ring_tbl++ = cpu_to_le16(ring_id); 6792 } 6793 vnic->default_rx_ring = min_j; 6794 } 6795 6796 static void 6797 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6798 struct bnxt_vnic_info *vnic) 6799 { 6800 if (bp->flags & BNXT_FLAG_CHIP_P7) 6801 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6802 6803 if (bp->rss_hash_delta) { 6804 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6805 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6806 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6807 else 6808 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6809 } else { 6810 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6811 } 6812 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6813 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6814 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6815 } 6816 6817 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6818 bool set_rss) 6819 { 6820 struct hwrm_vnic_rss_cfg_input *req; 6821 int rc; 6822 6823 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6824 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6825 return 0; 6826 6827 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6828 if (rc) 6829 return rc; 6830 6831 if (set_rss) 6832 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6833 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6834 return hwrm_req_send(bp, req); 6835 } 6836 6837 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6838 struct bnxt_vnic_info *vnic, bool set_rss) 6839 { 6840 struct hwrm_vnic_rss_cfg_input *req; 6841 dma_addr_t ring_tbl_map; 6842 u32 i, nr_ctxs; 6843 int rc; 6844 6845 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6846 if (rc) 6847 return rc; 6848 6849 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6850 if (!set_rss) 6851 return hwrm_req_send(bp, req); 6852 6853 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6854 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6855 ring_tbl_map = vnic->rss_table_dma_addr; 6856 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6857 6858 hwrm_req_hold(bp, req); 6859 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6860 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6861 req->ring_table_pair_index = i; 6862 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6863 rc = hwrm_req_send(bp, req); 6864 if (rc) 6865 goto exit; 6866 } 6867 6868 exit: 6869 hwrm_req_drop(bp, req); 6870 return rc; 6871 } 6872 6873 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6874 { 6875 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6876 struct hwrm_vnic_rss_qcfg_output *resp; 6877 struct hwrm_vnic_rss_qcfg_input *req; 6878 6879 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6880 return; 6881 6882 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6883 /* all contexts configured to same hash_type, zero always exists */ 6884 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6885 resp = hwrm_req_hold(bp, req); 6886 if (!hwrm_req_send(bp, req)) { 6887 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6888 bp->rss_hash_delta = 0; 6889 } 6890 hwrm_req_drop(bp, req); 6891 } 6892 6893 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6894 { 6895 u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh; 6896 struct hwrm_vnic_plcmodes_cfg_input *req; 6897 int rc; 6898 6899 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6900 if (rc) 6901 return rc; 6902 6903 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6904 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6905 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6906 6907 if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 6908 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6909 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6910 req->enables |= 6911 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6912 req->hds_threshold = cpu_to_le16(hds_thresh); 6913 } 6914 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6915 return hwrm_req_send(bp, req); 6916 } 6917 6918 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6919 struct bnxt_vnic_info *vnic, 6920 u16 ctx_idx) 6921 { 6922 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6923 6924 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6925 return; 6926 6927 req->rss_cos_lb_ctx_id = 6928 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6929 6930 hwrm_req_send(bp, req); 6931 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6932 } 6933 6934 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6935 { 6936 int i, j; 6937 6938 for (i = 0; i < bp->nr_vnics; i++) { 6939 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6940 6941 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6942 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6943 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6944 } 6945 } 6946 bp->rsscos_nr_ctxs = 0; 6947 } 6948 6949 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6950 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6951 { 6952 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6953 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6954 int rc; 6955 6956 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6957 if (rc) 6958 return rc; 6959 6960 resp = hwrm_req_hold(bp, req); 6961 rc = hwrm_req_send(bp, req); 6962 if (!rc) 6963 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6964 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6965 hwrm_req_drop(bp, req); 6966 6967 return rc; 6968 } 6969 6970 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6971 { 6972 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6973 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6974 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6975 } 6976 6977 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6978 { 6979 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6980 struct hwrm_vnic_cfg_input *req; 6981 unsigned int ring = 0, grp_idx; 6982 u16 def_vlan = 0; 6983 int rc; 6984 6985 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6986 if (rc) 6987 return rc; 6988 6989 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6990 struct bnxt_rx_ring_info *rxr; 6991 6992 rxr = &bp->rx_ring[vnic->default_rx_ring]; 6993 req->default_rx_ring_id = 6994 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6995 req->default_cmpl_ring_id = 6996 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6997 req->enables = 6998 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6999 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 7000 goto vnic_mru; 7001 } 7002 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 7003 /* Only RSS support for now TBD: COS & LB */ 7004 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 7005 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 7006 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 7007 VNIC_CFG_REQ_ENABLES_MRU); 7008 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 7009 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 7010 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 7011 VNIC_CFG_REQ_ENABLES_MRU); 7012 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 7013 } else { 7014 req->rss_rule = cpu_to_le16(0xffff); 7015 } 7016 7017 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 7018 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 7019 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 7020 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 7021 } else { 7022 req->cos_rule = cpu_to_le16(0xffff); 7023 } 7024 7025 ring = vnic->default_rx_ring; 7026 grp_idx = bp->rx_ring[ring].bnapi->index; 7027 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 7028 req->lb_rule = cpu_to_le16(0xffff); 7029 vnic_mru: 7030 vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN; 7031 req->mru = cpu_to_le16(vnic->mru); 7032 7033 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 7034 #ifdef CONFIG_BNXT_SRIOV 7035 if (BNXT_VF(bp)) 7036 def_vlan = bp->vf.vlan; 7037 #endif 7038 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 7039 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 7040 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && 7041 bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) 7042 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 7043 7044 return hwrm_req_send(bp, req); 7045 } 7046 7047 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 7048 struct bnxt_vnic_info *vnic) 7049 { 7050 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 7051 struct hwrm_vnic_free_input *req; 7052 7053 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 7054 return; 7055 7056 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 7057 7058 hwrm_req_send(bp, req); 7059 vnic->fw_vnic_id = INVALID_HW_RING_ID; 7060 } 7061 } 7062 7063 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 7064 { 7065 u16 i; 7066 7067 for (i = 0; i < bp->nr_vnics; i++) 7068 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 7069 } 7070 7071 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 7072 unsigned int start_rx_ring_idx, 7073 unsigned int nr_rings) 7074 { 7075 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 7076 struct hwrm_vnic_alloc_output *resp; 7077 struct hwrm_vnic_alloc_input *req; 7078 int rc; 7079 7080 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 7081 if (rc) 7082 return rc; 7083 7084 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7085 goto vnic_no_ring_grps; 7086 7087 /* map ring groups to this vnic */ 7088 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 7089 grp_idx = bp->rx_ring[i].bnapi->index; 7090 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 7091 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 7092 j, nr_rings); 7093 break; 7094 } 7095 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 7096 } 7097 7098 vnic_no_ring_grps: 7099 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 7100 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 7101 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 7102 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 7103 7104 resp = hwrm_req_hold(bp, req); 7105 rc = hwrm_req_send(bp, req); 7106 if (!rc) 7107 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 7108 hwrm_req_drop(bp, req); 7109 return rc; 7110 } 7111 7112 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 7113 { 7114 struct hwrm_vnic_qcaps_output *resp; 7115 struct hwrm_vnic_qcaps_input *req; 7116 int rc; 7117 7118 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 7119 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 7120 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 7121 if (bp->hwrm_spec_code < 0x10600) 7122 return 0; 7123 7124 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 7125 if (rc) 7126 return rc; 7127 7128 resp = hwrm_req_hold(bp, req); 7129 rc = hwrm_req_send(bp, req); 7130 if (!rc) { 7131 u32 flags = le32_to_cpu(resp->flags); 7132 7133 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 7134 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 7135 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 7136 if (flags & 7137 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 7138 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 7139 7140 /* Older P5 fw before EXT_HW_STATS support did not set 7141 * VLAN_STRIP_CAP properly. 7142 */ 7143 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 7144 (BNXT_CHIP_P5(bp) && 7145 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 7146 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 7147 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 7148 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 7149 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 7150 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 7151 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 7152 if (bp->max_tpa_v2) { 7153 if (BNXT_CHIP_P5(bp)) 7154 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 7155 else 7156 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 7157 } 7158 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 7159 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 7160 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 7161 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 7162 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 7163 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 7164 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 7165 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 7166 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 7167 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 7168 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP) 7169 bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP; 7170 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 7171 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 7172 } 7173 hwrm_req_drop(bp, req); 7174 return rc; 7175 } 7176 7177 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 7178 { 7179 struct hwrm_ring_grp_alloc_output *resp; 7180 struct hwrm_ring_grp_alloc_input *req; 7181 int rc; 7182 u16 i; 7183 7184 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7185 return 0; 7186 7187 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 7188 if (rc) 7189 return rc; 7190 7191 resp = hwrm_req_hold(bp, req); 7192 for (i = 0; i < bp->rx_nr_rings; i++) { 7193 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 7194 7195 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 7196 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 7197 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 7198 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 7199 7200 rc = hwrm_req_send(bp, req); 7201 7202 if (rc) 7203 break; 7204 7205 bp->grp_info[grp_idx].fw_grp_id = 7206 le32_to_cpu(resp->ring_group_id); 7207 } 7208 hwrm_req_drop(bp, req); 7209 return rc; 7210 } 7211 7212 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 7213 { 7214 struct hwrm_ring_grp_free_input *req; 7215 u16 i; 7216 7217 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7218 return; 7219 7220 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 7221 return; 7222 7223 hwrm_req_hold(bp, req); 7224 for (i = 0; i < bp->cp_nr_rings; i++) { 7225 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 7226 continue; 7227 req->ring_group_id = 7228 cpu_to_le32(bp->grp_info[i].fw_grp_id); 7229 7230 hwrm_req_send(bp, req); 7231 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 7232 } 7233 hwrm_req_drop(bp, req); 7234 } 7235 7236 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type, 7237 struct hwrm_ring_alloc_input *req, 7238 struct bnxt_rx_ring_info *rxr, 7239 struct bnxt_ring_struct *ring) 7240 { 7241 struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx]; 7242 u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID | 7243 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID; 7244 7245 if (ring_type == HWRM_RING_ALLOC_AGG) { 7246 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 7247 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 7248 req->rx_buf_size = cpu_to_le16(rxr->rx_page_size); 7249 enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID; 7250 } else { 7251 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 7252 if (NET_IP_ALIGN == 2) 7253 req->flags = 7254 cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD); 7255 } 7256 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7257 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7258 req->enables |= cpu_to_le32(enables); 7259 } 7260 7261 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 7262 struct bnxt_rx_ring_info *rxr, 7263 struct bnxt_ring_struct *ring, 7264 u32 ring_type, u32 map_index) 7265 { 7266 struct hwrm_ring_alloc_output *resp; 7267 struct hwrm_ring_alloc_input *req; 7268 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 7269 struct bnxt_ring_grp_info *grp_info; 7270 int rc, err = 0; 7271 u16 ring_id; 7272 7273 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 7274 if (rc) 7275 goto exit; 7276 7277 req->enables = 0; 7278 if (rmem->nr_pages > 1) { 7279 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 7280 /* Page size is in log2 units */ 7281 req->page_size = BNXT_PAGE_SHIFT; 7282 req->page_tbl_depth = 1; 7283 } else { 7284 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 7285 } 7286 req->fbo = 0; 7287 /* Association of ring index with doorbell index and MSIX number */ 7288 req->logical_id = cpu_to_le16(map_index); 7289 7290 switch (ring_type) { 7291 case HWRM_RING_ALLOC_TX: { 7292 struct bnxt_tx_ring_info *txr; 7293 u16 flags = 0; 7294 7295 txr = container_of(ring, struct bnxt_tx_ring_info, 7296 tx_ring_struct); 7297 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 7298 /* Association of transmit ring with completion ring */ 7299 grp_info = &bp->grp_info[ring->grp_idx]; 7300 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 7301 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 7302 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7303 req->queue_id = cpu_to_le16(ring->queue_id); 7304 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 7305 req->cmpl_coal_cnt = 7306 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 7307 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 7308 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 7309 req->flags = cpu_to_le16(flags); 7310 break; 7311 } 7312 case HWRM_RING_ALLOC_RX: 7313 case HWRM_RING_ALLOC_AGG: 7314 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 7315 req->length = (ring_type == HWRM_RING_ALLOC_RX) ? 7316 cpu_to_le32(bp->rx_ring_mask + 1) : 7317 cpu_to_le32(bp->rx_agg_ring_mask + 1); 7318 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7319 bnxt_set_rx_ring_params_p5(bp, ring_type, req, 7320 rxr, ring); 7321 break; 7322 case HWRM_RING_ALLOC_CMPL: 7323 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 7324 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7325 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7326 /* Association of cp ring with nq */ 7327 grp_info = &bp->grp_info[map_index]; 7328 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7329 req->cq_handle = cpu_to_le64(ring->handle); 7330 req->enables |= cpu_to_le32( 7331 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7332 } else { 7333 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7334 } 7335 break; 7336 case HWRM_RING_ALLOC_NQ: 7337 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7338 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7339 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7340 break; 7341 default: 7342 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7343 ring_type); 7344 return -EINVAL; 7345 } 7346 7347 resp = hwrm_req_hold(bp, req); 7348 rc = hwrm_req_send(bp, req); 7349 err = le16_to_cpu(resp->error_code); 7350 ring_id = le16_to_cpu(resp->ring_id); 7351 hwrm_req_drop(bp, req); 7352 7353 exit: 7354 if (rc || err) { 7355 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7356 ring_type, rc, err); 7357 return -EIO; 7358 } 7359 ring->fw_ring_id = ring_id; 7360 return rc; 7361 } 7362 7363 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7364 { 7365 int rc; 7366 7367 if (BNXT_PF(bp)) { 7368 struct hwrm_func_cfg_input *req; 7369 7370 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7371 if (rc) 7372 return rc; 7373 7374 req->fid = cpu_to_le16(0xffff); 7375 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7376 req->async_event_cr = cpu_to_le16(idx); 7377 return hwrm_req_send(bp, req); 7378 } else { 7379 struct hwrm_func_vf_cfg_input *req; 7380 7381 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7382 if (rc) 7383 return rc; 7384 7385 req->enables = 7386 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7387 req->async_event_cr = cpu_to_le16(idx); 7388 return hwrm_req_send(bp, req); 7389 } 7390 } 7391 7392 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7393 u32 ring_type) 7394 { 7395 switch (ring_type) { 7396 case HWRM_RING_ALLOC_TX: 7397 db->db_ring_mask = bp->tx_ring_mask; 7398 break; 7399 case HWRM_RING_ALLOC_RX: 7400 db->db_ring_mask = bp->rx_ring_mask; 7401 break; 7402 case HWRM_RING_ALLOC_AGG: 7403 db->db_ring_mask = bp->rx_agg_ring_mask; 7404 break; 7405 case HWRM_RING_ALLOC_CMPL: 7406 case HWRM_RING_ALLOC_NQ: 7407 db->db_ring_mask = bp->cp_ring_mask; 7408 break; 7409 } 7410 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7411 db->db_epoch_mask = db->db_ring_mask + 1; 7412 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7413 } 7414 } 7415 7416 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7417 u32 map_idx, u32 xid) 7418 { 7419 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7420 switch (ring_type) { 7421 case HWRM_RING_ALLOC_TX: 7422 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7423 break; 7424 case HWRM_RING_ALLOC_RX: 7425 case HWRM_RING_ALLOC_AGG: 7426 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7427 break; 7428 case HWRM_RING_ALLOC_CMPL: 7429 db->db_key64 = DBR_PATH_L2; 7430 break; 7431 case HWRM_RING_ALLOC_NQ: 7432 db->db_key64 = DBR_PATH_L2; 7433 break; 7434 } 7435 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7436 7437 if (bp->flags & BNXT_FLAG_CHIP_P7) 7438 db->db_key64 |= DBR_VALID; 7439 7440 db->doorbell = bp->bar1 + bp->db_offset; 7441 } else { 7442 db->doorbell = bp->bar1 + map_idx * 0x80; 7443 switch (ring_type) { 7444 case HWRM_RING_ALLOC_TX: 7445 db->db_key32 = DB_KEY_TX; 7446 break; 7447 case HWRM_RING_ALLOC_RX: 7448 case HWRM_RING_ALLOC_AGG: 7449 db->db_key32 = DB_KEY_RX; 7450 break; 7451 case HWRM_RING_ALLOC_CMPL: 7452 db->db_key32 = DB_KEY_CP; 7453 break; 7454 } 7455 } 7456 bnxt_set_db_mask(bp, db, ring_type); 7457 } 7458 7459 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7460 struct bnxt_rx_ring_info *rxr) 7461 { 7462 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7463 struct bnxt_napi *bnapi = rxr->bnapi; 7464 u32 type = HWRM_RING_ALLOC_RX; 7465 u32 map_idx = bnapi->index; 7466 int rc; 7467 7468 rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx); 7469 if (rc) 7470 return rc; 7471 7472 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7473 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7474 7475 return 0; 7476 } 7477 7478 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7479 struct bnxt_rx_ring_info *rxr) 7480 { 7481 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7482 u32 type = HWRM_RING_ALLOC_AGG; 7483 u32 grp_idx = ring->grp_idx; 7484 u32 map_idx; 7485 int rc; 7486 7487 map_idx = grp_idx + bp->rx_nr_rings; 7488 rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx); 7489 if (rc) 7490 return rc; 7491 7492 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7493 ring->fw_ring_id); 7494 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7495 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7496 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7497 7498 return 0; 7499 } 7500 7501 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp, 7502 struct bnxt_cp_ring_info *cpr) 7503 { 7504 const u32 type = HWRM_RING_ALLOC_CMPL; 7505 struct bnxt_napi *bnapi = cpr->bnapi; 7506 struct bnxt_ring_struct *ring; 7507 u32 map_idx = bnapi->index; 7508 int rc; 7509 7510 ring = &cpr->cp_ring_struct; 7511 ring->handle = BNXT_SET_NQ_HDL(cpr); 7512 rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx); 7513 if (rc) 7514 return rc; 7515 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7516 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7517 return 0; 7518 } 7519 7520 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp, 7521 struct bnxt_tx_ring_info *txr, u32 tx_idx) 7522 { 7523 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7524 const u32 type = HWRM_RING_ALLOC_TX; 7525 int rc; 7526 7527 rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx); 7528 if (rc) 7529 return rc; 7530 bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id); 7531 return 0; 7532 } 7533 7534 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7535 { 7536 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7537 int i, rc = 0; 7538 u32 type; 7539 7540 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7541 type = HWRM_RING_ALLOC_NQ; 7542 else 7543 type = HWRM_RING_ALLOC_CMPL; 7544 for (i = 0; i < bp->cp_nr_rings; i++) { 7545 struct bnxt_napi *bnapi = bp->bnapi[i]; 7546 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7547 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7548 u32 map_idx = ring->map_idx; 7549 unsigned int vector; 7550 7551 vector = bp->irq_tbl[map_idx].vector; 7552 disable_irq_nosync(vector); 7553 rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx); 7554 if (rc) { 7555 enable_irq(vector); 7556 goto err_out; 7557 } 7558 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7559 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7560 enable_irq(vector); 7561 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7562 7563 if (!i) { 7564 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7565 if (rc) 7566 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7567 } 7568 } 7569 7570 for (i = 0; i < bp->tx_nr_rings; i++) { 7571 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7572 7573 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7574 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 7575 if (rc) 7576 goto err_out; 7577 } 7578 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i); 7579 if (rc) 7580 goto err_out; 7581 } 7582 7583 for (i = 0; i < bp->rx_nr_rings; i++) { 7584 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7585 7586 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7587 if (rc) 7588 goto err_out; 7589 /* If we have agg rings, post agg buffers first. */ 7590 if (!agg_rings) 7591 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7592 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7593 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 7594 if (rc) 7595 goto err_out; 7596 } 7597 } 7598 7599 if (agg_rings) { 7600 for (i = 0; i < bp->rx_nr_rings; i++) { 7601 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7602 if (rc) 7603 goto err_out; 7604 } 7605 } 7606 err_out: 7607 return rc; 7608 } 7609 7610 static void bnxt_cancel_dim(struct bnxt *bp) 7611 { 7612 int i; 7613 7614 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7615 * if NAPI is enabled. 7616 */ 7617 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7618 return; 7619 7620 /* Make sure NAPI sees that the VNIC is disabled */ 7621 synchronize_net(); 7622 for (i = 0; i < bp->rx_nr_rings; i++) { 7623 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7624 struct bnxt_napi *bnapi = rxr->bnapi; 7625 7626 cancel_work_sync(&bnapi->cp_ring.dim.work); 7627 } 7628 } 7629 7630 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7631 struct bnxt_ring_struct *ring, 7632 u32 ring_type, int cmpl_ring_id) 7633 { 7634 struct hwrm_ring_free_output *resp; 7635 struct hwrm_ring_free_input *req; 7636 u16 error_code = 0; 7637 int rc; 7638 7639 if (BNXT_NO_FW_ACCESS(bp)) 7640 return 0; 7641 7642 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7643 if (rc) 7644 goto exit; 7645 7646 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7647 req->ring_type = ring_type; 7648 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7649 7650 resp = hwrm_req_hold(bp, req); 7651 rc = hwrm_req_send(bp, req); 7652 error_code = le16_to_cpu(resp->error_code); 7653 hwrm_req_drop(bp, req); 7654 exit: 7655 if (rc || error_code) { 7656 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7657 ring_type, rc, error_code); 7658 return -EIO; 7659 } 7660 return 0; 7661 } 7662 7663 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp, 7664 struct bnxt_tx_ring_info *txr, 7665 bool close_path) 7666 { 7667 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7668 u32 cmpl_ring_id; 7669 7670 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7671 return; 7672 7673 cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) : 7674 INVALID_HW_RING_ID; 7675 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX, 7676 cmpl_ring_id); 7677 ring->fw_ring_id = INVALID_HW_RING_ID; 7678 } 7679 7680 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7681 struct bnxt_rx_ring_info *rxr, 7682 bool close_path) 7683 { 7684 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7685 u32 grp_idx = rxr->bnapi->index; 7686 u32 cmpl_ring_id; 7687 7688 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7689 return; 7690 7691 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7692 hwrm_ring_free_send_msg(bp, ring, 7693 RING_FREE_REQ_RING_TYPE_RX, 7694 close_path ? cmpl_ring_id : 7695 INVALID_HW_RING_ID); 7696 ring->fw_ring_id = INVALID_HW_RING_ID; 7697 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7698 } 7699 7700 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7701 struct bnxt_rx_ring_info *rxr, 7702 bool close_path) 7703 { 7704 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7705 u32 grp_idx = rxr->bnapi->index; 7706 u32 type, cmpl_ring_id; 7707 7708 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7709 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7710 else 7711 type = RING_FREE_REQ_RING_TYPE_RX; 7712 7713 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7714 return; 7715 7716 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7717 hwrm_ring_free_send_msg(bp, ring, type, 7718 close_path ? cmpl_ring_id : 7719 INVALID_HW_RING_ID); 7720 ring->fw_ring_id = INVALID_HW_RING_ID; 7721 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7722 } 7723 7724 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp, 7725 struct bnxt_cp_ring_info *cpr) 7726 { 7727 struct bnxt_ring_struct *ring; 7728 7729 ring = &cpr->cp_ring_struct; 7730 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7731 return; 7732 7733 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL, 7734 INVALID_HW_RING_ID); 7735 ring->fw_ring_id = INVALID_HW_RING_ID; 7736 } 7737 7738 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 7739 { 7740 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7741 int i, size = ring->ring_mem.page_size; 7742 7743 cpr->cp_raw_cons = 0; 7744 cpr->toggle = 0; 7745 7746 for (i = 0; i < bp->cp_nr_pages; i++) 7747 if (cpr->cp_desc_ring[i]) 7748 memset(cpr->cp_desc_ring[i], 0, size); 7749 } 7750 7751 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7752 { 7753 u32 type; 7754 int i; 7755 7756 if (!bp->bnapi) 7757 return; 7758 7759 for (i = 0; i < bp->tx_nr_rings; i++) 7760 bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path); 7761 7762 bnxt_cancel_dim(bp); 7763 for (i = 0; i < bp->rx_nr_rings; i++) { 7764 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7765 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7766 } 7767 7768 /* The completion rings are about to be freed. After that the 7769 * IRQ doorbell will not work anymore. So we need to disable 7770 * IRQ here. 7771 */ 7772 bnxt_disable_int_sync(bp); 7773 7774 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7775 type = RING_FREE_REQ_RING_TYPE_NQ; 7776 else 7777 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7778 for (i = 0; i < bp->cp_nr_rings; i++) { 7779 struct bnxt_napi *bnapi = bp->bnapi[i]; 7780 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7781 struct bnxt_ring_struct *ring; 7782 int j; 7783 7784 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) 7785 bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]); 7786 7787 ring = &cpr->cp_ring_struct; 7788 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7789 hwrm_ring_free_send_msg(bp, ring, type, 7790 INVALID_HW_RING_ID); 7791 ring->fw_ring_id = INVALID_HW_RING_ID; 7792 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7793 } 7794 } 7795 } 7796 7797 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7798 bool shared); 7799 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7800 bool shared); 7801 7802 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7803 { 7804 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7805 struct hwrm_func_qcfg_output *resp; 7806 struct hwrm_func_qcfg_input *req; 7807 int rc; 7808 7809 if (bp->hwrm_spec_code < 0x10601) 7810 return 0; 7811 7812 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7813 if (rc) 7814 return rc; 7815 7816 req->fid = cpu_to_le16(0xffff); 7817 resp = hwrm_req_hold(bp, req); 7818 rc = hwrm_req_send(bp, req); 7819 if (rc) { 7820 hwrm_req_drop(bp, req); 7821 return rc; 7822 } 7823 7824 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7825 if (BNXT_NEW_RM(bp)) { 7826 u16 cp, stats; 7827 7828 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7829 hw_resc->resv_hw_ring_grps = 7830 le32_to_cpu(resp->alloc_hw_ring_grps); 7831 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7832 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7833 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7834 stats = le16_to_cpu(resp->alloc_stat_ctx); 7835 hw_resc->resv_irqs = cp; 7836 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7837 int rx = hw_resc->resv_rx_rings; 7838 int tx = hw_resc->resv_tx_rings; 7839 7840 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7841 rx >>= 1; 7842 if (cp < (rx + tx)) { 7843 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7844 if (rc) 7845 goto get_rings_exit; 7846 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7847 rx <<= 1; 7848 hw_resc->resv_rx_rings = rx; 7849 hw_resc->resv_tx_rings = tx; 7850 } 7851 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7852 hw_resc->resv_hw_ring_grps = rx; 7853 } 7854 hw_resc->resv_cp_rings = cp; 7855 hw_resc->resv_stat_ctxs = stats; 7856 } 7857 get_rings_exit: 7858 hwrm_req_drop(bp, req); 7859 return rc; 7860 } 7861 7862 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7863 { 7864 struct hwrm_func_qcfg_output *resp; 7865 struct hwrm_func_qcfg_input *req; 7866 int rc; 7867 7868 if (bp->hwrm_spec_code < 0x10601) 7869 return 0; 7870 7871 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7872 if (rc) 7873 return rc; 7874 7875 req->fid = cpu_to_le16(fid); 7876 resp = hwrm_req_hold(bp, req); 7877 rc = hwrm_req_send(bp, req); 7878 if (!rc) 7879 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7880 7881 hwrm_req_drop(bp, req); 7882 return rc; 7883 } 7884 7885 static bool bnxt_rfs_supported(struct bnxt *bp); 7886 7887 static struct hwrm_func_cfg_input * 7888 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7889 { 7890 struct hwrm_func_cfg_input *req; 7891 u32 enables = 0; 7892 7893 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7894 return NULL; 7895 7896 req->fid = cpu_to_le16(0xffff); 7897 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7898 req->num_tx_rings = cpu_to_le16(hwr->tx); 7899 if (BNXT_NEW_RM(bp)) { 7900 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7901 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7902 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7903 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7904 enables |= hwr->cp_p5 ? 7905 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7906 } else { 7907 enables |= hwr->cp ? 7908 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7909 enables |= hwr->grp ? 7910 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7911 } 7912 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7913 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7914 0; 7915 req->num_rx_rings = cpu_to_le16(hwr->rx); 7916 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7917 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7918 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7919 req->num_msix = cpu_to_le16(hwr->cp); 7920 } else { 7921 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7922 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7923 } 7924 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7925 req->num_vnics = cpu_to_le16(hwr->vnic); 7926 } 7927 req->enables = cpu_to_le32(enables); 7928 return req; 7929 } 7930 7931 static struct hwrm_func_vf_cfg_input * 7932 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7933 { 7934 struct hwrm_func_vf_cfg_input *req; 7935 u32 enables = 0; 7936 7937 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7938 return NULL; 7939 7940 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7941 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7942 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7943 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7944 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7945 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7946 enables |= hwr->cp_p5 ? 7947 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7948 } else { 7949 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7950 enables |= hwr->grp ? 7951 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7952 } 7953 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7954 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7955 7956 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7957 req->num_tx_rings = cpu_to_le16(hwr->tx); 7958 req->num_rx_rings = cpu_to_le16(hwr->rx); 7959 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7960 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7961 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7962 } else { 7963 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7964 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7965 } 7966 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7967 req->num_vnics = cpu_to_le16(hwr->vnic); 7968 7969 req->enables = cpu_to_le32(enables); 7970 return req; 7971 } 7972 7973 static int 7974 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7975 { 7976 struct hwrm_func_cfg_input *req; 7977 int rc; 7978 7979 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7980 if (!req) 7981 return -ENOMEM; 7982 7983 if (!req->enables) { 7984 hwrm_req_drop(bp, req); 7985 return 0; 7986 } 7987 7988 rc = hwrm_req_send(bp, req); 7989 if (rc) 7990 return rc; 7991 7992 if (bp->hwrm_spec_code < 0x10601) 7993 bp->hw_resc.resv_tx_rings = hwr->tx; 7994 7995 return bnxt_hwrm_get_rings(bp); 7996 } 7997 7998 static int 7999 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8000 { 8001 struct hwrm_func_vf_cfg_input *req; 8002 int rc; 8003 8004 if (!BNXT_NEW_RM(bp)) { 8005 bp->hw_resc.resv_tx_rings = hwr->tx; 8006 return 0; 8007 } 8008 8009 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8010 if (!req) 8011 return -ENOMEM; 8012 8013 rc = hwrm_req_send(bp, req); 8014 if (rc) 8015 return rc; 8016 8017 return bnxt_hwrm_get_rings(bp); 8018 } 8019 8020 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8021 { 8022 if (BNXT_PF(bp)) 8023 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 8024 else 8025 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 8026 } 8027 8028 int bnxt_nq_rings_in_use(struct bnxt *bp) 8029 { 8030 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 8031 } 8032 8033 static int bnxt_cp_rings_in_use(struct bnxt *bp) 8034 { 8035 int cp; 8036 8037 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8038 return bnxt_nq_rings_in_use(bp); 8039 8040 cp = bp->tx_nr_rings + bp->rx_nr_rings; 8041 return cp; 8042 } 8043 8044 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 8045 { 8046 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 8047 } 8048 8049 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8050 { 8051 if (!hwr->grp) 8052 return 0; 8053 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 8054 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 8055 8056 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 8057 rss_ctx *= hwr->vnic; 8058 return rss_ctx; 8059 } 8060 if (BNXT_VF(bp)) 8061 return BNXT_VF_MAX_RSS_CTX; 8062 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 8063 return hwr->grp + 1; 8064 return 1; 8065 } 8066 8067 /* Check if a default RSS map needs to be setup. This function is only 8068 * used on older firmware that does not require reserving RX rings. 8069 */ 8070 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 8071 { 8072 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8073 8074 /* The RSS map is valid for RX rings set to resv_rx_rings */ 8075 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 8076 hw_resc->resv_rx_rings = bp->rx_nr_rings; 8077 if (!netif_is_rxfh_configured(bp->dev)) 8078 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8079 } 8080 } 8081 8082 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 8083 { 8084 if (bp->flags & BNXT_FLAG_RFS) { 8085 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 8086 return 2 + bp->num_rss_ctx; 8087 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8088 return rx_rings + 1; 8089 } 8090 return 1; 8091 } 8092 8093 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8094 { 8095 hwr->cp = bnxt_nq_rings_in_use(bp); 8096 hwr->cp_p5 = 0; 8097 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8098 hwr->cp_p5 = bnxt_cp_rings_in_use(bp); 8099 hwr->tx = bp->tx_nr_rings; 8100 hwr->rx = bp->rx_nr_rings; 8101 hwr->grp = hwr->rx; 8102 hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx); 8103 hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr); 8104 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8105 hwr->rx <<= 1; 8106 hwr->stat = bnxt_get_func_stat_ctxs(bp); 8107 } 8108 8109 static bool bnxt_need_reserve_rings(struct bnxt *bp) 8110 { 8111 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8112 struct bnxt_hw_rings hwr; 8113 8114 bnxt_get_total_resources(bp, &hwr); 8115 8116 /* Old firmware does not need RX ring reservations but we still 8117 * need to setup a default RSS map when needed. With new firmware 8118 * we go through RX ring reservations first and then set up the 8119 * RSS map for the successfully reserved RX rings when needed. 8120 */ 8121 if (!BNXT_NEW_RM(bp)) 8122 bnxt_check_rss_tbl_no_rmgr(bp); 8123 8124 if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601) 8125 return true; 8126 8127 if (!BNXT_NEW_RM(bp)) 8128 return false; 8129 8130 if (hw_resc->resv_rx_rings != hwr.rx || 8131 hw_resc->resv_vnics != hwr.vnic || 8132 hw_resc->resv_stat_ctxs != hwr.stat || 8133 hw_resc->resv_rsscos_ctxs != hwr.rss_ctx || 8134 (hw_resc->resv_hw_ring_grps != hwr.grp && 8135 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 8136 return true; 8137 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 8138 if (hw_resc->resv_cp_rings != hwr.cp_p5) 8139 return true; 8140 } else if (hw_resc->resv_cp_rings != hwr.cp) { 8141 return true; 8142 } 8143 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 8144 hw_resc->resv_irqs != hwr.cp) 8145 return true; 8146 return false; 8147 } 8148 8149 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8150 { 8151 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8152 8153 hwr->tx = hw_resc->resv_tx_rings; 8154 if (BNXT_NEW_RM(bp)) { 8155 hwr->rx = hw_resc->resv_rx_rings; 8156 hwr->cp = hw_resc->resv_irqs; 8157 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8158 hwr->cp_p5 = hw_resc->resv_cp_rings; 8159 hwr->grp = hw_resc->resv_hw_ring_grps; 8160 hwr->vnic = hw_resc->resv_vnics; 8161 hwr->stat = hw_resc->resv_stat_ctxs; 8162 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 8163 } 8164 } 8165 8166 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8167 { 8168 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 8169 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 8170 } 8171 8172 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 8173 8174 static int __bnxt_reserve_rings(struct bnxt *bp) 8175 { 8176 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 8177 struct bnxt_hw_rings hwr = {0}; 8178 int rx_rings, old_rx_rings, rc; 8179 int cp = bp->cp_nr_rings; 8180 int ulp_msix = 0; 8181 bool sh = false; 8182 int tx_cp; 8183 8184 if (!bnxt_need_reserve_rings(bp)) 8185 return 0; 8186 8187 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) { 8188 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 8189 if (!ulp_msix) 8190 bnxt_set_ulp_stat_ctxs(bp, 0); 8191 else 8192 bnxt_set_dflt_ulp_stat_ctxs(bp); 8193 8194 if (ulp_msix > bp->ulp_num_msix_want) 8195 ulp_msix = bp->ulp_num_msix_want; 8196 hwr.cp = cp + ulp_msix; 8197 } else { 8198 hwr.cp = bnxt_nq_rings_in_use(bp); 8199 } 8200 8201 hwr.tx = bp->tx_nr_rings; 8202 hwr.rx = bp->rx_nr_rings; 8203 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 8204 sh = true; 8205 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8206 hwr.cp_p5 = hwr.rx + hwr.tx; 8207 8208 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 8209 8210 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8211 hwr.rx <<= 1; 8212 hwr.grp = bp->rx_nr_rings; 8213 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 8214 hwr.stat = bnxt_get_func_stat_ctxs(bp); 8215 old_rx_rings = bp->hw_resc.resv_rx_rings; 8216 8217 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 8218 if (rc) 8219 return rc; 8220 8221 bnxt_copy_reserved_rings(bp, &hwr); 8222 8223 rx_rings = hwr.rx; 8224 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 8225 if (hwr.rx >= 2) { 8226 rx_rings = hwr.rx >> 1; 8227 } else { 8228 if (netif_running(bp->dev)) 8229 return -ENOMEM; 8230 8231 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 8232 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 8233 bp->dev->hw_features &= ~NETIF_F_LRO; 8234 bp->dev->features &= ~NETIF_F_LRO; 8235 bnxt_set_ring_params(bp); 8236 } 8237 } 8238 rx_rings = min_t(int, rx_rings, hwr.grp); 8239 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 8240 if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 8241 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 8242 hwr.cp = min_t(int, hwr.cp, hwr.stat); 8243 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 8244 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8245 hwr.rx = rx_rings << 1; 8246 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 8247 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 8248 if (hwr.tx != bp->tx_nr_rings) { 8249 netdev_warn(bp->dev, 8250 "Able to reserve only %d out of %d requested TX rings\n", 8251 hwr.tx, bp->tx_nr_rings); 8252 } 8253 bp->tx_nr_rings = hwr.tx; 8254 8255 /* If we cannot reserve all the RX rings, reset the RSS map only 8256 * if absolutely necessary 8257 */ 8258 if (rx_rings != bp->rx_nr_rings) { 8259 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 8260 rx_rings, bp->rx_nr_rings); 8261 if (netif_is_rxfh_configured(bp->dev) && 8262 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 8263 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 8264 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 8265 ethtool_rxfh_indir_lost(bp->dev); 8266 } 8267 } 8268 bp->rx_nr_rings = rx_rings; 8269 bp->cp_nr_rings = hwr.cp; 8270 8271 /* Fall back if we cannot reserve enough HW RSS contexts */ 8272 if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) && 8273 hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr)) 8274 bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX; 8275 8276 if (!bnxt_rings_ok(bp, &hwr)) 8277 return -ENOMEM; 8278 8279 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 8280 !netif_is_rxfh_configured(bp->dev)) 8281 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8282 8283 if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) { 8284 int resv_msix, resv_ctx, ulp_ctxs; 8285 struct bnxt_hw_resc *hw_resc; 8286 8287 hw_resc = &bp->hw_resc; 8288 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 8289 ulp_msix = min_t(int, resv_msix, ulp_msix); 8290 bnxt_set_ulp_msix_num(bp, ulp_msix); 8291 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 8292 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 8293 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 8294 } 8295 8296 return rc; 8297 } 8298 8299 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8300 { 8301 struct hwrm_func_vf_cfg_input *req; 8302 u32 flags; 8303 8304 if (!BNXT_NEW_RM(bp)) 8305 return 0; 8306 8307 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8308 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 8309 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8310 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8311 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8312 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 8313 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 8314 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8315 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8316 8317 req->flags = cpu_to_le32(flags); 8318 return hwrm_req_send_silent(bp, req); 8319 } 8320 8321 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8322 { 8323 struct hwrm_func_cfg_input *req; 8324 u32 flags; 8325 8326 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 8327 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 8328 if (BNXT_NEW_RM(bp)) { 8329 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8330 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8331 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8332 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 8333 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8334 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 8335 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 8336 else 8337 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8338 } 8339 8340 req->flags = cpu_to_le32(flags); 8341 return hwrm_req_send_silent(bp, req); 8342 } 8343 8344 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8345 { 8346 if (bp->hwrm_spec_code < 0x10801) 8347 return 0; 8348 8349 if (BNXT_PF(bp)) 8350 return bnxt_hwrm_check_pf_rings(bp, hwr); 8351 8352 return bnxt_hwrm_check_vf_rings(bp, hwr); 8353 } 8354 8355 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 8356 { 8357 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8358 struct hwrm_ring_aggint_qcaps_output *resp; 8359 struct hwrm_ring_aggint_qcaps_input *req; 8360 int rc; 8361 8362 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 8363 coal_cap->num_cmpl_dma_aggr_max = 63; 8364 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 8365 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 8366 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 8367 coal_cap->int_lat_tmr_min_max = 65535; 8368 coal_cap->int_lat_tmr_max_max = 65535; 8369 coal_cap->num_cmpl_aggr_int_max = 65535; 8370 coal_cap->timer_units = 80; 8371 8372 if (bp->hwrm_spec_code < 0x10902) 8373 return; 8374 8375 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 8376 return; 8377 8378 resp = hwrm_req_hold(bp, req); 8379 rc = hwrm_req_send_silent(bp, req); 8380 if (!rc) { 8381 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 8382 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 8383 coal_cap->num_cmpl_dma_aggr_max = 8384 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 8385 coal_cap->num_cmpl_dma_aggr_during_int_max = 8386 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8387 coal_cap->cmpl_aggr_dma_tmr_max = 8388 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8389 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8390 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8391 coal_cap->int_lat_tmr_min_max = 8392 le16_to_cpu(resp->int_lat_tmr_min_max); 8393 coal_cap->int_lat_tmr_max_max = 8394 le16_to_cpu(resp->int_lat_tmr_max_max); 8395 coal_cap->num_cmpl_aggr_int_max = 8396 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8397 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8398 } 8399 hwrm_req_drop(bp, req); 8400 } 8401 8402 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8403 { 8404 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8405 8406 return usec * 1000 / coal_cap->timer_units; 8407 } 8408 8409 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8410 struct bnxt_coal *hw_coal, 8411 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8412 { 8413 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8414 u16 val, tmr, max, flags = hw_coal->flags; 8415 u32 cmpl_params = coal_cap->cmpl_params; 8416 8417 max = hw_coal->bufs_per_record * 128; 8418 if (hw_coal->budget) 8419 max = hw_coal->bufs_per_record * hw_coal->budget; 8420 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8421 8422 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8423 req->num_cmpl_aggr_int = cpu_to_le16(val); 8424 8425 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8426 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8427 8428 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8429 coal_cap->num_cmpl_dma_aggr_during_int_max); 8430 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8431 8432 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8433 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8434 req->int_lat_tmr_max = cpu_to_le16(tmr); 8435 8436 /* min timer set to 1/2 of interrupt timer */ 8437 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8438 val = tmr / 2; 8439 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8440 req->int_lat_tmr_min = cpu_to_le16(val); 8441 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8442 } 8443 8444 /* buf timer set to 1/4 of interrupt timer */ 8445 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8446 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8447 8448 if (cmpl_params & 8449 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8450 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8451 val = clamp_t(u16, tmr, 1, 8452 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8453 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8454 req->enables |= 8455 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8456 } 8457 8458 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8459 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8460 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8461 req->flags = cpu_to_le16(flags); 8462 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8463 } 8464 8465 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8466 struct bnxt_coal *hw_coal) 8467 { 8468 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8469 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8470 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8471 u32 nq_params = coal_cap->nq_params; 8472 u16 tmr; 8473 int rc; 8474 8475 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8476 return 0; 8477 8478 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8479 if (rc) 8480 return rc; 8481 8482 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8483 req->flags = 8484 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8485 8486 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8487 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8488 req->int_lat_tmr_min = cpu_to_le16(tmr); 8489 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8490 return hwrm_req_send(bp, req); 8491 } 8492 8493 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8494 { 8495 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8496 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8497 struct bnxt_coal coal; 8498 int rc; 8499 8500 /* Tick values in micro seconds. 8501 * 1 coal_buf x bufs_per_record = 1 completion record. 8502 */ 8503 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8504 8505 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8506 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8507 8508 if (!bnapi->rx_ring) 8509 return -ENODEV; 8510 8511 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8512 if (rc) 8513 return rc; 8514 8515 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8516 8517 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8518 8519 return hwrm_req_send(bp, req_rx); 8520 } 8521 8522 static int 8523 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8524 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8525 { 8526 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8527 8528 req->ring_id = cpu_to_le16(ring_id); 8529 return hwrm_req_send(bp, req); 8530 } 8531 8532 static int 8533 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8534 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8535 { 8536 struct bnxt_tx_ring_info *txr; 8537 int i, rc; 8538 8539 bnxt_for_each_napi_tx(i, bnapi, txr) { 8540 u16 ring_id; 8541 8542 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8543 req->ring_id = cpu_to_le16(ring_id); 8544 rc = hwrm_req_send(bp, req); 8545 if (rc) 8546 return rc; 8547 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8548 return 0; 8549 } 8550 return 0; 8551 } 8552 8553 int bnxt_hwrm_set_coal(struct bnxt *bp) 8554 { 8555 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8556 int i, rc; 8557 8558 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8559 if (rc) 8560 return rc; 8561 8562 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8563 if (rc) { 8564 hwrm_req_drop(bp, req_rx); 8565 return rc; 8566 } 8567 8568 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8569 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8570 8571 hwrm_req_hold(bp, req_rx); 8572 hwrm_req_hold(bp, req_tx); 8573 for (i = 0; i < bp->cp_nr_rings; i++) { 8574 struct bnxt_napi *bnapi = bp->bnapi[i]; 8575 struct bnxt_coal *hw_coal; 8576 8577 if (!bnapi->rx_ring) 8578 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8579 else 8580 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8581 if (rc) 8582 break; 8583 8584 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8585 continue; 8586 8587 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8588 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8589 if (rc) 8590 break; 8591 } 8592 if (bnapi->rx_ring) 8593 hw_coal = &bp->rx_coal; 8594 else 8595 hw_coal = &bp->tx_coal; 8596 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8597 } 8598 hwrm_req_drop(bp, req_rx); 8599 hwrm_req_drop(bp, req_tx); 8600 return rc; 8601 } 8602 8603 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8604 { 8605 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8606 struct hwrm_stat_ctx_free_input *req; 8607 int i; 8608 8609 if (!bp->bnapi) 8610 return; 8611 8612 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8613 return; 8614 8615 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8616 return; 8617 if (BNXT_FW_MAJ(bp) <= 20) { 8618 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8619 hwrm_req_drop(bp, req); 8620 return; 8621 } 8622 hwrm_req_hold(bp, req0); 8623 } 8624 hwrm_req_hold(bp, req); 8625 for (i = 0; i < bp->cp_nr_rings; i++) { 8626 struct bnxt_napi *bnapi = bp->bnapi[i]; 8627 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8628 8629 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8630 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8631 if (req0) { 8632 req0->stat_ctx_id = req->stat_ctx_id; 8633 hwrm_req_send(bp, req0); 8634 } 8635 hwrm_req_send(bp, req); 8636 8637 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8638 } 8639 } 8640 hwrm_req_drop(bp, req); 8641 if (req0) 8642 hwrm_req_drop(bp, req0); 8643 } 8644 8645 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8646 { 8647 struct hwrm_stat_ctx_alloc_output *resp; 8648 struct hwrm_stat_ctx_alloc_input *req; 8649 int rc, i; 8650 8651 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8652 return 0; 8653 8654 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8655 if (rc) 8656 return rc; 8657 8658 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8659 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8660 8661 resp = hwrm_req_hold(bp, req); 8662 for (i = 0; i < bp->cp_nr_rings; i++) { 8663 struct bnxt_napi *bnapi = bp->bnapi[i]; 8664 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8665 8666 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8667 8668 rc = hwrm_req_send(bp, req); 8669 if (rc) 8670 break; 8671 8672 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8673 8674 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8675 } 8676 hwrm_req_drop(bp, req); 8677 return rc; 8678 } 8679 8680 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8681 { 8682 struct hwrm_func_qcfg_output *resp; 8683 struct hwrm_func_qcfg_input *req; 8684 u16 flags; 8685 int rc; 8686 8687 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8688 if (rc) 8689 return rc; 8690 8691 req->fid = cpu_to_le16(0xffff); 8692 resp = hwrm_req_hold(bp, req); 8693 rc = hwrm_req_send(bp, req); 8694 if (rc) 8695 goto func_qcfg_exit; 8696 8697 flags = le16_to_cpu(resp->flags); 8698 #ifdef CONFIG_BNXT_SRIOV 8699 if (BNXT_VF(bp)) { 8700 struct bnxt_vf_info *vf = &bp->vf; 8701 8702 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8703 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8704 vf->flags |= BNXT_VF_TRUST; 8705 else 8706 vf->flags &= ~BNXT_VF_TRUST; 8707 } else { 8708 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8709 } 8710 #endif 8711 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8712 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8713 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8714 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8715 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8716 } 8717 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8718 bp->flags |= BNXT_FLAG_MULTI_HOST; 8719 8720 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8721 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8722 8723 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8724 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8725 if (resp->roce_bidi_opt_mode & 8726 FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED) 8727 bp->cos0_cos1_shared = 1; 8728 else 8729 bp->cos0_cos1_shared = 0; 8730 8731 switch (resp->port_partition_type) { 8732 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8733 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2: 8734 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8735 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8736 bp->port_partition_type = resp->port_partition_type; 8737 break; 8738 } 8739 if (bp->hwrm_spec_code < 0x10707 || 8740 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8741 bp->br_mode = BRIDGE_MODE_VEB; 8742 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8743 bp->br_mode = BRIDGE_MODE_VEPA; 8744 else 8745 bp->br_mode = BRIDGE_MODE_UNDEF; 8746 8747 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8748 if (!bp->max_mtu) 8749 bp->max_mtu = BNXT_MAX_MTU; 8750 8751 if (bp->db_size) 8752 goto func_qcfg_exit; 8753 8754 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8755 if (BNXT_CHIP_P5(bp)) { 8756 if (BNXT_PF(bp)) 8757 bp->db_offset = DB_PF_OFFSET_P5; 8758 else 8759 bp->db_offset = DB_VF_OFFSET_P5; 8760 } 8761 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8762 1024); 8763 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8764 bp->db_size <= bp->db_offset) 8765 bp->db_size = pci_resource_len(bp->pdev, 2); 8766 8767 func_qcfg_exit: 8768 hwrm_req_drop(bp, req); 8769 return rc; 8770 } 8771 8772 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8773 u8 init_val, u8 init_offset, 8774 bool init_mask_set) 8775 { 8776 ctxm->init_value = init_val; 8777 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8778 if (init_mask_set) 8779 ctxm->init_offset = init_offset * 4; 8780 else 8781 ctxm->init_value = 0; 8782 } 8783 8784 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8785 { 8786 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8787 u16 type; 8788 8789 for (type = 0; type < ctx_max; type++) { 8790 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8791 int n = 1; 8792 8793 if (!ctxm->max_entries || ctxm->pg_info) 8794 continue; 8795 8796 if (ctxm->instance_bmap) 8797 n = hweight32(ctxm->instance_bmap); 8798 ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n); 8799 if (!ctxm->pg_info) 8800 return -ENOMEM; 8801 } 8802 return 0; 8803 } 8804 8805 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8806 struct bnxt_ctx_mem_type *ctxm, bool force); 8807 8808 #define BNXT_CTX_INIT_VALID(flags) \ 8809 (!!((flags) & \ 8810 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8811 8812 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8813 { 8814 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8815 struct hwrm_func_backing_store_qcaps_v2_input *req; 8816 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8817 u16 type, next_type = 0; 8818 int rc; 8819 8820 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8821 if (rc) 8822 return rc; 8823 8824 if (!ctx) { 8825 ctx = kzalloc_obj(*ctx); 8826 if (!ctx) 8827 return -ENOMEM; 8828 bp->ctx = ctx; 8829 } 8830 8831 resp = hwrm_req_hold(bp, req); 8832 8833 for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) { 8834 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8835 u8 init_val, init_off, i; 8836 u32 max_entries; 8837 u16 entry_size; 8838 __le32 *p; 8839 u32 flags; 8840 8841 req->type = cpu_to_le16(type); 8842 rc = hwrm_req_send(bp, req); 8843 if (rc) 8844 goto ctx_done; 8845 flags = le32_to_cpu(resp->flags); 8846 next_type = le16_to_cpu(resp->next_valid_type); 8847 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8848 bnxt_free_one_ctx_mem(bp, ctxm, true); 8849 continue; 8850 } 8851 entry_size = le16_to_cpu(resp->entry_size); 8852 max_entries = le32_to_cpu(resp->max_num_entries); 8853 if (ctxm->mem_valid) { 8854 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8855 ctxm->entry_size != entry_size || 8856 ctxm->max_entries != max_entries) 8857 bnxt_free_one_ctx_mem(bp, ctxm, true); 8858 else 8859 continue; 8860 } 8861 ctxm->type = type; 8862 ctxm->entry_size = entry_size; 8863 ctxm->flags = flags; 8864 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8865 ctxm->entry_multiple = resp->entry_multiple; 8866 ctxm->max_entries = max_entries; 8867 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8868 init_val = resp->ctx_init_value; 8869 init_off = resp->ctx_init_offset; 8870 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8871 BNXT_CTX_INIT_VALID(flags)); 8872 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8873 BNXT_MAX_SPLIT_ENTRY); 8874 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8875 i++, p++) 8876 ctxm->split[i] = le32_to_cpu(*p); 8877 } 8878 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8879 8880 ctx_done: 8881 hwrm_req_drop(bp, req); 8882 return rc; 8883 } 8884 8885 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8886 { 8887 struct hwrm_func_backing_store_qcaps_output *resp; 8888 struct hwrm_func_backing_store_qcaps_input *req; 8889 int rc; 8890 8891 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8892 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8893 return 0; 8894 8895 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8896 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8897 8898 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8899 if (rc) 8900 return rc; 8901 8902 resp = hwrm_req_hold(bp, req); 8903 rc = hwrm_req_send_silent(bp, req); 8904 if (!rc) { 8905 struct bnxt_ctx_mem_type *ctxm; 8906 struct bnxt_ctx_mem_info *ctx; 8907 u8 init_val, init_idx = 0; 8908 u16 init_mask; 8909 8910 ctx = bp->ctx; 8911 if (!ctx) { 8912 ctx = kzalloc_obj(*ctx); 8913 if (!ctx) { 8914 rc = -ENOMEM; 8915 goto ctx_err; 8916 } 8917 bp->ctx = ctx; 8918 } 8919 init_val = resp->ctx_kind_initializer; 8920 init_mask = le16_to_cpu(resp->ctx_init_mask); 8921 8922 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8923 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8924 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8925 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8926 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8927 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8928 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8929 (init_mask & (1 << init_idx++)) != 0); 8930 8931 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8932 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8933 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8934 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8935 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8936 (init_mask & (1 << init_idx++)) != 0); 8937 8938 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8939 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8940 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8941 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8942 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8943 (init_mask & (1 << init_idx++)) != 0); 8944 8945 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8946 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8947 ctxm->max_entries = ctxm->vnic_entries + 8948 le16_to_cpu(resp->vnic_max_ring_table_entries); 8949 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8950 bnxt_init_ctx_initializer(ctxm, init_val, 8951 resp->vnic_init_offset, 8952 (init_mask & (1 << init_idx++)) != 0); 8953 8954 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8955 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8956 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8957 bnxt_init_ctx_initializer(ctxm, init_val, 8958 resp->stat_init_offset, 8959 (init_mask & (1 << init_idx++)) != 0); 8960 8961 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8962 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8963 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8964 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8965 ctxm->entry_multiple = resp->tqm_entries_multiple; 8966 if (!ctxm->entry_multiple) 8967 ctxm->entry_multiple = 1; 8968 8969 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8970 8971 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8972 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8973 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8974 ctxm->mrav_num_entries_units = 8975 le16_to_cpu(resp->mrav_num_entries_units); 8976 bnxt_init_ctx_initializer(ctxm, init_val, 8977 resp->mrav_init_offset, 8978 (init_mask & (1 << init_idx++)) != 0); 8979 8980 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8981 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8982 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8983 8984 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8985 if (!ctx->tqm_fp_rings_count) 8986 ctx->tqm_fp_rings_count = bp->max_q; 8987 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8988 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8989 8990 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8991 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8992 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8993 8994 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8995 } else { 8996 rc = 0; 8997 } 8998 ctx_err: 8999 hwrm_req_drop(bp, req); 9000 return rc; 9001 } 9002 9003 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 9004 __le64 *pg_dir) 9005 { 9006 if (!rmem->nr_pages) 9007 return; 9008 9009 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 9010 if (rmem->depth >= 1) { 9011 if (rmem->depth == 2) 9012 *pg_attr |= 2; 9013 else 9014 *pg_attr |= 1; 9015 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 9016 } else { 9017 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 9018 } 9019 } 9020 9021 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 9022 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 9023 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 9024 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 9025 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 9026 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 9027 9028 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 9029 { 9030 struct hwrm_func_backing_store_cfg_input *req; 9031 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9032 struct bnxt_ctx_pg_info *ctx_pg; 9033 struct bnxt_ctx_mem_type *ctxm; 9034 void **__req = (void **)&req; 9035 u32 req_len = sizeof(*req); 9036 __le32 *num_entries; 9037 __le64 *pg_dir; 9038 u32 flags = 0; 9039 u8 *pg_attr; 9040 u32 ena; 9041 int rc; 9042 int i; 9043 9044 if (!ctx) 9045 return 0; 9046 9047 if (req_len > bp->hwrm_max_ext_req_len) 9048 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 9049 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 9050 if (rc) 9051 return rc; 9052 9053 req->enables = cpu_to_le32(enables); 9054 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 9055 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9056 ctx_pg = ctxm->pg_info; 9057 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 9058 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 9059 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 9060 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 9061 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9062 &req->qpc_pg_size_qpc_lvl, 9063 &req->qpc_page_dir); 9064 9065 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 9066 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 9067 } 9068 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 9069 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9070 ctx_pg = ctxm->pg_info; 9071 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 9072 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 9073 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 9074 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9075 &req->srq_pg_size_srq_lvl, 9076 &req->srq_page_dir); 9077 } 9078 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 9079 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9080 ctx_pg = ctxm->pg_info; 9081 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 9082 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 9083 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 9084 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9085 &req->cq_pg_size_cq_lvl, 9086 &req->cq_page_dir); 9087 } 9088 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 9089 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9090 ctx_pg = ctxm->pg_info; 9091 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 9092 req->vnic_num_ring_table_entries = 9093 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 9094 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 9095 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9096 &req->vnic_pg_size_vnic_lvl, 9097 &req->vnic_page_dir); 9098 } 9099 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 9100 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9101 ctx_pg = ctxm->pg_info; 9102 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 9103 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 9104 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9105 &req->stat_pg_size_stat_lvl, 9106 &req->stat_page_dir); 9107 } 9108 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 9109 u32 units; 9110 9111 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9112 ctx_pg = ctxm->pg_info; 9113 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 9114 units = ctxm->mrav_num_entries_units; 9115 if (units) { 9116 u32 num_mr, num_ah = ctxm->mrav_av_entries; 9117 u32 entries; 9118 9119 num_mr = ctx_pg->entries - num_ah; 9120 entries = ((num_mr / units) << 16) | (num_ah / units); 9121 req->mrav_num_entries = cpu_to_le32(entries); 9122 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 9123 } 9124 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 9125 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9126 &req->mrav_pg_size_mrav_lvl, 9127 &req->mrav_page_dir); 9128 } 9129 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 9130 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9131 ctx_pg = ctxm->pg_info; 9132 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 9133 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 9134 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9135 &req->tim_pg_size_tim_lvl, 9136 &req->tim_page_dir); 9137 } 9138 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9139 for (i = 0, num_entries = &req->tqm_sp_num_entries, 9140 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 9141 pg_dir = &req->tqm_sp_page_dir, 9142 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 9143 ctx_pg = ctxm->pg_info; 9144 i < BNXT_MAX_TQM_RINGS; 9145 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 9146 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 9147 if (!(enables & ena)) 9148 continue; 9149 9150 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 9151 *num_entries = cpu_to_le32(ctx_pg->entries); 9152 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 9153 } 9154 req->flags = cpu_to_le32(flags); 9155 return hwrm_req_send(bp, req); 9156 } 9157 9158 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 9159 struct bnxt_ctx_pg_info *ctx_pg) 9160 { 9161 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9162 9163 rmem->page_size = BNXT_PAGE_SIZE; 9164 rmem->pg_arr = ctx_pg->ctx_pg_arr; 9165 rmem->dma_arr = ctx_pg->ctx_dma_arr; 9166 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 9167 if (rmem->depth >= 1) 9168 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 9169 return bnxt_alloc_ring(bp, rmem); 9170 } 9171 9172 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 9173 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 9174 u8 depth, struct bnxt_ctx_mem_type *ctxm) 9175 { 9176 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9177 int rc; 9178 9179 if (!mem_size) 9180 return -EINVAL; 9181 9182 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 9183 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 9184 ctx_pg->nr_pages = 0; 9185 return -EINVAL; 9186 } 9187 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 9188 int nr_tbls, i; 9189 9190 rmem->depth = 2; 9191 ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES); 9192 if (!ctx_pg->ctx_pg_tbl) 9193 return -ENOMEM; 9194 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 9195 rmem->nr_pages = nr_tbls; 9196 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 9197 if (rc) 9198 return rc; 9199 for (i = 0; i < nr_tbls; i++) { 9200 struct bnxt_ctx_pg_info *pg_tbl; 9201 9202 pg_tbl = kzalloc_obj(*pg_tbl); 9203 if (!pg_tbl) 9204 return -ENOMEM; 9205 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 9206 rmem = &pg_tbl->ring_mem; 9207 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 9208 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 9209 rmem->depth = 1; 9210 rmem->nr_pages = MAX_CTX_PAGES; 9211 rmem->ctx_mem = ctxm; 9212 if (i == (nr_tbls - 1)) { 9213 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 9214 9215 if (rem) 9216 rmem->nr_pages = rem; 9217 } 9218 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 9219 if (rc) 9220 break; 9221 } 9222 } else { 9223 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 9224 if (rmem->nr_pages > 1 || depth) 9225 rmem->depth = 1; 9226 rmem->ctx_mem = ctxm; 9227 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 9228 } 9229 return rc; 9230 } 9231 9232 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 9233 struct bnxt_ctx_pg_info *ctx_pg, 9234 void *buf, size_t offset, size_t head, 9235 size_t tail) 9236 { 9237 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9238 size_t nr_pages = ctx_pg->nr_pages; 9239 int page_size = rmem->page_size; 9240 size_t len = 0, total_len = 0; 9241 u16 depth = rmem->depth; 9242 9243 tail %= nr_pages * page_size; 9244 do { 9245 if (depth > 1) { 9246 int i = head / (page_size * MAX_CTX_PAGES); 9247 struct bnxt_ctx_pg_info *pg_tbl; 9248 9249 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9250 rmem = &pg_tbl->ring_mem; 9251 } 9252 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 9253 head += len; 9254 offset += len; 9255 total_len += len; 9256 if (head >= nr_pages * page_size) 9257 head = 0; 9258 } while (head != tail); 9259 return total_len; 9260 } 9261 9262 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 9263 struct bnxt_ctx_pg_info *ctx_pg) 9264 { 9265 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9266 9267 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 9268 ctx_pg->ctx_pg_tbl) { 9269 int i, nr_tbls = rmem->nr_pages; 9270 9271 for (i = 0; i < nr_tbls; i++) { 9272 struct bnxt_ctx_pg_info *pg_tbl; 9273 struct bnxt_ring_mem_info *rmem2; 9274 9275 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9276 if (!pg_tbl) 9277 continue; 9278 rmem2 = &pg_tbl->ring_mem; 9279 bnxt_free_ring(bp, rmem2); 9280 ctx_pg->ctx_pg_arr[i] = NULL; 9281 kfree(pg_tbl); 9282 ctx_pg->ctx_pg_tbl[i] = NULL; 9283 } 9284 kfree(ctx_pg->ctx_pg_tbl); 9285 ctx_pg->ctx_pg_tbl = NULL; 9286 } 9287 bnxt_free_ring(bp, rmem); 9288 ctx_pg->nr_pages = 0; 9289 } 9290 9291 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 9292 struct bnxt_ctx_mem_type *ctxm, u32 entries, 9293 u8 pg_lvl) 9294 { 9295 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9296 int i, rc = 0, n = 1; 9297 u32 mem_size; 9298 9299 if (!ctxm->entry_size || !ctx_pg) 9300 return -EINVAL; 9301 if (ctxm->instance_bmap) 9302 n = hweight32(ctxm->instance_bmap); 9303 if (ctxm->entry_multiple) 9304 entries = roundup(entries, ctxm->entry_multiple); 9305 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 9306 mem_size = entries * ctxm->entry_size; 9307 for (i = 0; i < n && !rc; i++) { 9308 ctx_pg[i].entries = entries; 9309 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 9310 ctxm->init_value ? ctxm : NULL); 9311 } 9312 if (!rc) 9313 ctxm->mem_valid = 1; 9314 return rc; 9315 } 9316 9317 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 9318 struct bnxt_ctx_mem_type *ctxm, 9319 bool last) 9320 { 9321 struct hwrm_func_backing_store_cfg_v2_input *req; 9322 u32 instance_bmap = ctxm->instance_bmap; 9323 int i, j, rc = 0, n = 1; 9324 __le32 *p; 9325 9326 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 9327 return 0; 9328 9329 if (instance_bmap) 9330 n = hweight32(ctxm->instance_bmap); 9331 else 9332 instance_bmap = 1; 9333 9334 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 9335 if (rc) 9336 return rc; 9337 hwrm_req_hold(bp, req); 9338 req->type = cpu_to_le16(ctxm->type); 9339 req->entry_size = cpu_to_le16(ctxm->entry_size); 9340 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 9341 bnxt_bs_trace_avail(bp, ctxm->type)) { 9342 struct bnxt_bs_trace_info *bs_trace; 9343 u32 enables; 9344 9345 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 9346 req->enables = cpu_to_le32(enables); 9347 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 9348 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 9349 } 9350 req->subtype_valid_cnt = ctxm->split_entry_cnt; 9351 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 9352 p[i] = cpu_to_le32(ctxm->split[i]); 9353 for (i = 0, j = 0; j < n && !rc; i++) { 9354 struct bnxt_ctx_pg_info *ctx_pg; 9355 9356 if (!(instance_bmap & (1 << i))) 9357 continue; 9358 req->instance = cpu_to_le16(i); 9359 ctx_pg = &ctxm->pg_info[j++]; 9360 if (!ctx_pg->entries) 9361 continue; 9362 req->num_entries = cpu_to_le32(ctx_pg->entries); 9363 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9364 &req->page_size_pbl_level, 9365 &req->page_dir); 9366 if (last && j == n) 9367 req->flags = 9368 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 9369 rc = hwrm_req_send(bp, req); 9370 } 9371 hwrm_req_drop(bp, req); 9372 return rc; 9373 } 9374 9375 static int bnxt_backing_store_cfg_v2(struct bnxt *bp) 9376 { 9377 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9378 struct bnxt_ctx_mem_type *ctxm; 9379 u16 last_type = BNXT_CTX_INV; 9380 int rc = 0; 9381 u16 type; 9382 9383 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) { 9384 ctxm = &ctx->ctx_arr[type]; 9385 if (!bnxt_bs_trace_avail(bp, type)) 9386 continue; 9387 if (!ctxm->mem_valid) { 9388 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 9389 ctxm->max_entries, 1); 9390 if (rc) { 9391 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9392 type); 9393 continue; 9394 } 9395 bnxt_bs_trace_init(bp, ctxm); 9396 } 9397 last_type = type; 9398 } 9399 9400 if (last_type == BNXT_CTX_INV) { 9401 for (type = 0; type < BNXT_CTX_MAX; type++) { 9402 ctxm = &ctx->ctx_arr[type]; 9403 if (ctxm->mem_valid) 9404 last_type = type; 9405 } 9406 if (last_type == BNXT_CTX_INV) 9407 return 0; 9408 } 9409 ctx->ctx_arr[last_type].last = 1; 9410 9411 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9412 ctxm = &ctx->ctx_arr[type]; 9413 9414 if (!ctxm->mem_valid) 9415 continue; 9416 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9417 if (rc) 9418 return rc; 9419 } 9420 return 0; 9421 } 9422 9423 /** 9424 * __bnxt_copy_ctx_mem - copy host context memory 9425 * @bp: The driver context 9426 * @ctxm: The pointer to the context memory type 9427 * @buf: The destination buffer or NULL to just obtain the length 9428 * @offset: The buffer offset to copy the data to 9429 * @head: The head offset of context memory to copy from 9430 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9431 * 9432 * This function is called for debugging purposes to dump the host context 9433 * used by the chip. 9434 * 9435 * Return: Length of memory copied 9436 */ 9437 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9438 struct bnxt_ctx_mem_type *ctxm, void *buf, 9439 size_t offset, size_t head, size_t tail) 9440 { 9441 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9442 size_t len = 0, total_len = 0; 9443 int i, n = 1; 9444 9445 if (!ctx_pg) 9446 return 0; 9447 9448 if (ctxm->instance_bmap) 9449 n = hweight32(ctxm->instance_bmap); 9450 for (i = 0; i < n; i++) { 9451 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9452 tail); 9453 offset += len; 9454 total_len += len; 9455 } 9456 return total_len; 9457 } 9458 9459 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9460 void *buf, size_t offset) 9461 { 9462 size_t tail = ctxm->max_entries * ctxm->entry_size; 9463 9464 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9465 } 9466 9467 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9468 struct bnxt_ctx_mem_type *ctxm, bool force) 9469 { 9470 struct bnxt_ctx_pg_info *ctx_pg; 9471 int i, n = 1; 9472 9473 ctxm->last = 0; 9474 9475 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9476 return; 9477 9478 ctx_pg = ctxm->pg_info; 9479 if (ctx_pg) { 9480 if (ctxm->instance_bmap) 9481 n = hweight32(ctxm->instance_bmap); 9482 for (i = 0; i < n; i++) 9483 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9484 9485 kfree(ctx_pg); 9486 ctxm->pg_info = NULL; 9487 ctxm->mem_valid = 0; 9488 } 9489 memset(ctxm, 0, sizeof(*ctxm)); 9490 } 9491 9492 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9493 { 9494 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9495 u16 type; 9496 9497 if (!ctx) 9498 return; 9499 9500 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9501 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9502 9503 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9504 if (force) { 9505 kfree(ctx); 9506 bp->ctx = NULL; 9507 } 9508 } 9509 9510 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9511 { 9512 struct bnxt_ctx_mem_type *ctxm; 9513 struct bnxt_ctx_mem_info *ctx; 9514 u32 l2_qps, qp1_qps, max_qps; 9515 u32 ena, entries_sp, entries; 9516 u32 srqs, max_srqs, min; 9517 u32 num_mr, num_ah; 9518 u32 extra_srqs = 0; 9519 u32 extra_qps = 0; 9520 u32 fast_qpmd_qps; 9521 u8 pg_lvl = 1; 9522 int i, rc; 9523 9524 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9525 if (rc) { 9526 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9527 rc); 9528 return rc; 9529 } 9530 ctx = bp->ctx; 9531 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9532 return 0; 9533 9534 ena = 0; 9535 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 9536 goto skip_legacy; 9537 9538 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9539 l2_qps = ctxm->qp_l2_entries; 9540 qp1_qps = ctxm->qp_qp1_entries; 9541 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9542 max_qps = ctxm->max_entries; 9543 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9544 srqs = ctxm->srq_l2_entries; 9545 max_srqs = ctxm->max_entries; 9546 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9547 pg_lvl = 2; 9548 if (BNXT_SW_RES_LMT(bp)) { 9549 extra_qps = max_qps - l2_qps - qp1_qps; 9550 extra_srqs = max_srqs - srqs; 9551 } else { 9552 extra_qps = min_t(u32, 65536, 9553 max_qps - l2_qps - qp1_qps); 9554 /* allocate extra qps if fw supports RoCE fast qp 9555 * destroy feature 9556 */ 9557 extra_qps += fast_qpmd_qps; 9558 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9559 } 9560 if (fast_qpmd_qps) 9561 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9562 } 9563 9564 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9565 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9566 pg_lvl); 9567 if (rc) 9568 return rc; 9569 9570 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9571 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9572 if (rc) 9573 return rc; 9574 9575 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9576 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9577 extra_qps * 2, pg_lvl); 9578 if (rc) 9579 return rc; 9580 9581 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9582 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9583 if (rc) 9584 return rc; 9585 9586 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9587 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9588 if (rc) 9589 return rc; 9590 9591 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9592 goto skip_rdma; 9593 9594 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9595 if (BNXT_SW_RES_LMT(bp) && 9596 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9597 num_ah = ctxm->mrav_av_entries; 9598 num_mr = ctxm->max_entries - num_ah; 9599 } else { 9600 /* 128K extra is needed to accommodate static AH context 9601 * allocation by f/w. 9602 */ 9603 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9604 num_ah = min_t(u32, num_mr, 1024 * 128); 9605 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9606 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9607 ctxm->mrav_av_entries = num_ah; 9608 } 9609 9610 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9611 if (rc) 9612 return rc; 9613 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9614 9615 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9616 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9617 if (rc) 9618 return rc; 9619 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9620 9621 skip_rdma: 9622 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9623 min = ctxm->min_entries; 9624 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9625 2 * (extra_qps + qp1_qps) + min; 9626 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9627 if (rc) 9628 return rc; 9629 9630 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9631 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9632 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9633 if (rc) 9634 return rc; 9635 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9636 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9637 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9638 9639 skip_legacy: 9640 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9641 rc = bnxt_backing_store_cfg_v2(bp); 9642 else 9643 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9644 if (rc) { 9645 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9646 rc); 9647 return rc; 9648 } 9649 ctx->flags |= BNXT_CTX_FLAG_INITED; 9650 return 0; 9651 } 9652 9653 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9654 { 9655 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9656 u16 page_attr; 9657 int rc; 9658 9659 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9660 return 0; 9661 9662 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9663 if (rc) 9664 return rc; 9665 9666 if (BNXT_PAGE_SIZE == 0x2000) 9667 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9668 else if (BNXT_PAGE_SIZE == 0x10000) 9669 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9670 else 9671 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9672 req->pg_size_lvl = cpu_to_le16(page_attr | 9673 bp->fw_crash_mem->ring_mem.depth); 9674 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9675 req->size = cpu_to_le32(bp->fw_crash_len); 9676 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9677 return hwrm_req_send(bp, req); 9678 } 9679 9680 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9681 { 9682 if (bp->fw_crash_mem) { 9683 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9684 kfree(bp->fw_crash_mem); 9685 bp->fw_crash_mem = NULL; 9686 } 9687 } 9688 9689 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9690 { 9691 u32 mem_size = 0; 9692 int rc; 9693 9694 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9695 return 0; 9696 9697 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9698 if (rc) 9699 return rc; 9700 9701 mem_size = round_up(mem_size, 4); 9702 9703 /* keep and use the existing pages */ 9704 if (bp->fw_crash_mem && 9705 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9706 goto alloc_done; 9707 9708 if (bp->fw_crash_mem) 9709 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9710 else 9711 bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem); 9712 if (!bp->fw_crash_mem) 9713 return -ENOMEM; 9714 9715 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9716 if (rc) { 9717 bnxt_free_crash_dump_mem(bp); 9718 return rc; 9719 } 9720 9721 alloc_done: 9722 bp->fw_crash_len = mem_size; 9723 return 0; 9724 } 9725 9726 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9727 { 9728 struct hwrm_func_resource_qcaps_output *resp; 9729 struct hwrm_func_resource_qcaps_input *req; 9730 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9731 int rc; 9732 9733 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9734 if (rc) 9735 return rc; 9736 9737 req->fid = cpu_to_le16(0xffff); 9738 resp = hwrm_req_hold(bp, req); 9739 rc = hwrm_req_send_silent(bp, req); 9740 if (rc) 9741 goto hwrm_func_resc_qcaps_exit; 9742 9743 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9744 if (!all) 9745 goto hwrm_func_resc_qcaps_exit; 9746 9747 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9748 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9749 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9750 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9751 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9752 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9753 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9754 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9755 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9756 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9757 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9758 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9759 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9760 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9761 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9762 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9763 9764 if (hw_resc->max_rsscos_ctxs >= 9765 hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO) 9766 bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX; 9767 9768 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9769 u16 max_msix = le16_to_cpu(resp->max_msix); 9770 9771 hw_resc->max_nqs = max_msix; 9772 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9773 } 9774 9775 if (BNXT_PF(bp)) { 9776 struct bnxt_pf_info *pf = &bp->pf; 9777 9778 pf->vf_resv_strategy = 9779 le16_to_cpu(resp->vf_reservation_strategy); 9780 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9781 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9782 } 9783 hwrm_func_resc_qcaps_exit: 9784 hwrm_req_drop(bp, req); 9785 return rc; 9786 } 9787 9788 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9789 { 9790 struct hwrm_port_mac_ptp_qcfg_output *resp; 9791 struct hwrm_port_mac_ptp_qcfg_input *req; 9792 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9793 u8 flags; 9794 int rc; 9795 9796 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9797 rc = -ENODEV; 9798 goto no_ptp; 9799 } 9800 9801 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9802 if (rc) 9803 goto no_ptp; 9804 9805 req->port_id = cpu_to_le16(bp->pf.port_id); 9806 resp = hwrm_req_hold(bp, req); 9807 rc = hwrm_req_send(bp, req); 9808 if (rc) 9809 goto exit; 9810 9811 flags = resp->flags; 9812 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9813 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9814 rc = -ENODEV; 9815 goto exit; 9816 } 9817 if (!ptp) { 9818 ptp = kzalloc_obj(*ptp); 9819 if (!ptp) { 9820 rc = -ENOMEM; 9821 goto exit; 9822 } 9823 ptp->bp = bp; 9824 bp->ptp_cfg = ptp; 9825 } 9826 9827 if (flags & 9828 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9829 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9830 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9831 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9832 } else if (BNXT_CHIP_P5(bp)) { 9833 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9834 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9835 } else { 9836 rc = -ENODEV; 9837 goto exit; 9838 } 9839 ptp->rtc_configured = 9840 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9841 rc = bnxt_ptp_init(bp); 9842 if (rc) 9843 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9844 exit: 9845 hwrm_req_drop(bp, req); 9846 if (!rc) 9847 return 0; 9848 9849 no_ptp: 9850 bnxt_ptp_clear(bp); 9851 kfree(ptp); 9852 bp->ptp_cfg = NULL; 9853 return rc; 9854 } 9855 9856 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9857 { 9858 u32 flags, flags_ext, flags_ext2, flags_ext3; 9859 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9860 struct hwrm_func_qcaps_output *resp; 9861 struct hwrm_func_qcaps_input *req; 9862 int rc; 9863 9864 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9865 if (rc) 9866 return rc; 9867 9868 req->fid = cpu_to_le16(0xffff); 9869 resp = hwrm_req_hold(bp, req); 9870 rc = hwrm_req_send(bp, req); 9871 if (rc) 9872 goto hwrm_func_qcaps_exit; 9873 9874 flags = le32_to_cpu(resp->flags); 9875 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9876 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9877 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9878 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9879 if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED) 9880 bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN; 9881 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9882 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9883 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9884 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9885 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9886 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9887 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9888 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9889 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9890 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9891 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9892 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9893 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9894 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9895 9896 flags_ext = le32_to_cpu(resp->flags_ext); 9897 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9898 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9899 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9900 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9901 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED) 9902 bp->fw_cap |= BNXT_FW_CAP_PTP_PTM; 9903 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9904 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9905 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9906 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9907 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9908 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9909 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED) 9910 bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2; 9911 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9912 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9913 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9914 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9915 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9916 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9917 9918 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9919 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9920 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9921 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9922 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9923 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9924 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9925 if (flags_ext2 & 9926 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9927 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9928 if (BNXT_PF(bp) && 9929 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9930 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9931 9932 flags_ext3 = le32_to_cpu(resp->flags_ext3); 9933 if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT) 9934 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT; 9935 if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED) 9936 bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE; 9937 9938 bp->tx_push_thresh = 0; 9939 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9940 BNXT_FW_MAJ(bp) > 217) 9941 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9942 9943 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9944 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9945 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9946 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9947 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9948 if (!hw_resc->max_hw_ring_grps) 9949 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9950 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9951 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9952 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9953 9954 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9955 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9956 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9957 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9958 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9959 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9960 9961 if (BNXT_PF(bp)) { 9962 struct bnxt_pf_info *pf = &bp->pf; 9963 9964 pf->fw_fid = le16_to_cpu(resp->fid); 9965 pf->port_id = le16_to_cpu(resp->port_id); 9966 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9967 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9968 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9969 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9970 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9971 bp->flags |= BNXT_FLAG_WOL_CAP; 9972 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9973 bp->fw_cap |= BNXT_FW_CAP_PTP; 9974 } else { 9975 bnxt_ptp_clear(bp); 9976 kfree(bp->ptp_cfg); 9977 bp->ptp_cfg = NULL; 9978 } 9979 } else { 9980 #ifdef CONFIG_BNXT_SRIOV 9981 struct bnxt_vf_info *vf = &bp->vf; 9982 9983 vf->fw_fid = le16_to_cpu(resp->fid); 9984 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9985 #endif 9986 } 9987 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9988 9989 hwrm_func_qcaps_exit: 9990 hwrm_req_drop(bp, req); 9991 return rc; 9992 } 9993 9994 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9995 { 9996 struct hwrm_dbg_qcaps_output *resp; 9997 struct hwrm_dbg_qcaps_input *req; 9998 int rc; 9999 10000 bp->fw_dbg_cap = 0; 10001 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 10002 return; 10003 10004 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 10005 if (rc) 10006 return; 10007 10008 req->fid = cpu_to_le16(0xffff); 10009 resp = hwrm_req_hold(bp, req); 10010 rc = hwrm_req_send(bp, req); 10011 if (rc) 10012 goto hwrm_dbg_qcaps_exit; 10013 10014 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 10015 10016 hwrm_dbg_qcaps_exit: 10017 hwrm_req_drop(bp, req); 10018 } 10019 10020 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 10021 10022 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 10023 { 10024 int rc; 10025 10026 rc = __bnxt_hwrm_func_qcaps(bp); 10027 if (rc) 10028 return rc; 10029 10030 bnxt_hwrm_dbg_qcaps(bp); 10031 10032 rc = bnxt_hwrm_queue_qportcfg(bp); 10033 if (rc) { 10034 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 10035 return rc; 10036 } 10037 if (bp->hwrm_spec_code >= 0x10803) { 10038 rc = bnxt_alloc_ctx_mem(bp); 10039 if (rc) 10040 return rc; 10041 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 10042 if (!rc) 10043 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 10044 } 10045 return 0; 10046 } 10047 10048 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 10049 { 10050 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 10051 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 10052 u32 flags; 10053 int rc; 10054 10055 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 10056 return 0; 10057 10058 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 10059 if (rc) 10060 return rc; 10061 10062 resp = hwrm_req_hold(bp, req); 10063 rc = hwrm_req_send(bp, req); 10064 if (rc) 10065 goto hwrm_cfa_adv_qcaps_exit; 10066 10067 flags = le32_to_cpu(resp->flags); 10068 if (flags & 10069 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 10070 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 10071 10072 if (flags & 10073 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 10074 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 10075 10076 if (flags & 10077 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 10078 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 10079 10080 hwrm_cfa_adv_qcaps_exit: 10081 hwrm_req_drop(bp, req); 10082 return rc; 10083 } 10084 10085 static int __bnxt_alloc_fw_health(struct bnxt *bp) 10086 { 10087 if (bp->fw_health) 10088 return 0; 10089 10090 bp->fw_health = kzalloc_obj(*bp->fw_health); 10091 if (!bp->fw_health) 10092 return -ENOMEM; 10093 10094 mutex_init(&bp->fw_health->lock); 10095 return 0; 10096 } 10097 10098 static int bnxt_alloc_fw_health(struct bnxt *bp) 10099 { 10100 int rc; 10101 10102 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 10103 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 10104 return 0; 10105 10106 rc = __bnxt_alloc_fw_health(bp); 10107 if (rc) { 10108 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 10109 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10110 return rc; 10111 } 10112 10113 return 0; 10114 } 10115 10116 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 10117 { 10118 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 10119 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 10120 BNXT_FW_HEALTH_WIN_MAP_OFF); 10121 } 10122 10123 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 10124 { 10125 struct bnxt_fw_health *fw_health = bp->fw_health; 10126 u32 reg_type; 10127 10128 if (!fw_health) 10129 return; 10130 10131 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 10132 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 10133 fw_health->status_reliable = false; 10134 10135 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 10136 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 10137 fw_health->resets_reliable = false; 10138 } 10139 10140 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 10141 { 10142 void __iomem *hs; 10143 u32 status_loc; 10144 u32 reg_type; 10145 u32 sig; 10146 10147 if (bp->fw_health) 10148 bp->fw_health->status_reliable = false; 10149 10150 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 10151 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 10152 10153 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 10154 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 10155 if (!bp->chip_num) { 10156 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 10157 bp->chip_num = readl(bp->bar0 + 10158 BNXT_FW_HEALTH_WIN_BASE + 10159 BNXT_GRC_REG_CHIP_NUM); 10160 } 10161 if (!BNXT_CHIP_P5_PLUS(bp)) 10162 return; 10163 10164 status_loc = BNXT_GRC_REG_STATUS_P5 | 10165 BNXT_FW_HEALTH_REG_TYPE_BAR0; 10166 } else { 10167 status_loc = readl(hs + offsetof(struct hcomm_status, 10168 fw_status_loc)); 10169 } 10170 10171 if (__bnxt_alloc_fw_health(bp)) { 10172 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 10173 return; 10174 } 10175 10176 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 10177 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 10178 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 10179 __bnxt_map_fw_health_reg(bp, status_loc); 10180 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 10181 BNXT_FW_HEALTH_WIN_OFF(status_loc); 10182 } 10183 10184 bp->fw_health->status_reliable = true; 10185 } 10186 10187 static int bnxt_map_fw_health_regs(struct bnxt *bp) 10188 { 10189 struct bnxt_fw_health *fw_health = bp->fw_health; 10190 u32 reg_base = 0xffffffff; 10191 int i; 10192 10193 bp->fw_health->status_reliable = false; 10194 bp->fw_health->resets_reliable = false; 10195 /* Only pre-map the monitoring GRC registers using window 3 */ 10196 for (i = 0; i < 4; i++) { 10197 u32 reg = fw_health->regs[i]; 10198 10199 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 10200 continue; 10201 if (reg_base == 0xffffffff) 10202 reg_base = reg & BNXT_GRC_BASE_MASK; 10203 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 10204 return -ERANGE; 10205 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 10206 } 10207 bp->fw_health->status_reliable = true; 10208 bp->fw_health->resets_reliable = true; 10209 if (reg_base == 0xffffffff) 10210 return 0; 10211 10212 __bnxt_map_fw_health_reg(bp, reg_base); 10213 return 0; 10214 } 10215 10216 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 10217 { 10218 if (!bp->fw_health) 10219 return; 10220 10221 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 10222 bp->fw_health->status_reliable = true; 10223 bp->fw_health->resets_reliable = true; 10224 } else { 10225 bnxt_try_map_fw_health_reg(bp); 10226 } 10227 } 10228 10229 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 10230 { 10231 struct bnxt_fw_health *fw_health = bp->fw_health; 10232 struct hwrm_error_recovery_qcfg_output *resp; 10233 struct hwrm_error_recovery_qcfg_input *req; 10234 int rc, i; 10235 10236 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 10237 return 0; 10238 10239 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 10240 if (rc) 10241 return rc; 10242 10243 resp = hwrm_req_hold(bp, req); 10244 rc = hwrm_req_send(bp, req); 10245 if (rc) 10246 goto err_recovery_out; 10247 fw_health->flags = le32_to_cpu(resp->flags); 10248 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 10249 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 10250 rc = -EINVAL; 10251 goto err_recovery_out; 10252 } 10253 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 10254 fw_health->master_func_wait_dsecs = 10255 le32_to_cpu(resp->master_func_wait_period); 10256 fw_health->normal_func_wait_dsecs = 10257 le32_to_cpu(resp->normal_func_wait_period); 10258 fw_health->post_reset_wait_dsecs = 10259 le32_to_cpu(resp->master_func_wait_period_after_reset); 10260 fw_health->post_reset_max_wait_dsecs = 10261 le32_to_cpu(resp->max_bailout_time_after_reset); 10262 fw_health->regs[BNXT_FW_HEALTH_REG] = 10263 le32_to_cpu(resp->fw_health_status_reg); 10264 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 10265 le32_to_cpu(resp->fw_heartbeat_reg); 10266 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 10267 le32_to_cpu(resp->fw_reset_cnt_reg); 10268 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 10269 le32_to_cpu(resp->reset_inprogress_reg); 10270 fw_health->fw_reset_inprog_reg_mask = 10271 le32_to_cpu(resp->reset_inprogress_reg_mask); 10272 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 10273 if (fw_health->fw_reset_seq_cnt >= 16) { 10274 rc = -EINVAL; 10275 goto err_recovery_out; 10276 } 10277 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 10278 fw_health->fw_reset_seq_regs[i] = 10279 le32_to_cpu(resp->reset_reg[i]); 10280 fw_health->fw_reset_seq_vals[i] = 10281 le32_to_cpu(resp->reset_reg_val[i]); 10282 fw_health->fw_reset_seq_delay_msec[i] = 10283 resp->delay_after_reset[i]; 10284 } 10285 err_recovery_out: 10286 hwrm_req_drop(bp, req); 10287 if (!rc) 10288 rc = bnxt_map_fw_health_regs(bp); 10289 if (rc) 10290 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10291 return rc; 10292 } 10293 10294 static int bnxt_hwrm_func_reset(struct bnxt *bp) 10295 { 10296 struct hwrm_func_reset_input *req; 10297 int rc; 10298 10299 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 10300 if (rc) 10301 return rc; 10302 10303 req->enables = 0; 10304 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 10305 return hwrm_req_send(bp, req); 10306 } 10307 10308 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 10309 { 10310 struct hwrm_nvm_get_dev_info_output nvm_info; 10311 10312 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 10313 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 10314 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 10315 nvm_info.nvm_cfg_ver_upd); 10316 } 10317 10318 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 10319 { 10320 struct hwrm_queue_qportcfg_output *resp; 10321 struct hwrm_queue_qportcfg_input *req; 10322 u8 i, j, *qptr; 10323 bool no_rdma; 10324 int rc = 0; 10325 10326 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 10327 if (rc) 10328 return rc; 10329 10330 resp = hwrm_req_hold(bp, req); 10331 rc = hwrm_req_send(bp, req); 10332 if (rc) 10333 goto qportcfg_exit; 10334 10335 if (!resp->max_configurable_queues) { 10336 rc = -EINVAL; 10337 goto qportcfg_exit; 10338 } 10339 bp->max_tc = resp->max_configurable_queues; 10340 bp->max_lltc = resp->max_configurable_lossless_queues; 10341 if (bp->max_tc > BNXT_MAX_QUEUE) 10342 bp->max_tc = BNXT_MAX_QUEUE; 10343 10344 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 10345 qptr = &resp->queue_id0; 10346 for (i = 0, j = 0; i < bp->max_tc; i++) { 10347 bp->q_info[j].queue_id = *qptr; 10348 bp->q_ids[i] = *qptr++; 10349 bp->q_info[j].queue_profile = *qptr++; 10350 bp->tc_to_qidx[j] = j; 10351 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 10352 (no_rdma && BNXT_PF(bp))) 10353 j++; 10354 } 10355 bp->max_q = bp->max_tc; 10356 bp->max_tc = max_t(u8, j, 1); 10357 10358 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 10359 bp->max_tc = 1; 10360 10361 if (bp->max_lltc > bp->max_tc) 10362 bp->max_lltc = bp->max_tc; 10363 10364 qportcfg_exit: 10365 hwrm_req_drop(bp, req); 10366 return rc; 10367 } 10368 10369 static int bnxt_hwrm_poll(struct bnxt *bp) 10370 { 10371 struct hwrm_ver_get_input *req; 10372 int rc; 10373 10374 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10375 if (rc) 10376 return rc; 10377 10378 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10379 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10380 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10381 10382 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 10383 rc = hwrm_req_send(bp, req); 10384 return rc; 10385 } 10386 10387 static int bnxt_hwrm_ver_get(struct bnxt *bp) 10388 { 10389 struct hwrm_ver_get_output *resp; 10390 struct hwrm_ver_get_input *req; 10391 u16 fw_maj, fw_min, fw_bld, fw_rsv; 10392 u32 dev_caps_cfg, hwrm_ver; 10393 int rc, len, max_tmo_secs; 10394 10395 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10396 if (rc) 10397 return rc; 10398 10399 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 10400 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 10401 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10402 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10403 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10404 10405 resp = hwrm_req_hold(bp, req); 10406 rc = hwrm_req_send(bp, req); 10407 if (rc) 10408 goto hwrm_ver_get_exit; 10409 10410 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 10411 10412 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10413 resp->hwrm_intf_min_8b << 8 | 10414 resp->hwrm_intf_upd_8b; 10415 if (resp->hwrm_intf_maj_8b < 1) { 10416 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10417 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10418 resp->hwrm_intf_upd_8b); 10419 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10420 } 10421 10422 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10423 HWRM_VERSION_UPDATE; 10424 10425 if (bp->hwrm_spec_code > hwrm_ver) 10426 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10427 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10428 HWRM_VERSION_UPDATE); 10429 else 10430 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10431 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10432 resp->hwrm_intf_upd_8b); 10433 10434 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10435 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10436 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10437 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10438 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10439 len = FW_VER_STR_LEN; 10440 } else { 10441 fw_maj = resp->hwrm_fw_maj_8b; 10442 fw_min = resp->hwrm_fw_min_8b; 10443 fw_bld = resp->hwrm_fw_bld_8b; 10444 fw_rsv = resp->hwrm_fw_rsvd_8b; 10445 len = BC_HWRM_STR_LEN; 10446 } 10447 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10448 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10449 fw_rsv); 10450 10451 if (strlen(resp->active_pkg_name)) { 10452 int fw_ver_len = strlen(bp->fw_ver_str); 10453 10454 snprintf(bp->fw_ver_str + fw_ver_len, 10455 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10456 resp->active_pkg_name); 10457 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10458 } 10459 10460 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10461 if (!bp->hwrm_cmd_timeout) 10462 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10463 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10464 if (!bp->hwrm_cmd_max_timeout) 10465 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10466 max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000; 10467 #ifdef CONFIG_DETECT_HUNG_TASK 10468 if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT || 10469 max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) { 10470 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n", 10471 max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT); 10472 } 10473 #endif 10474 10475 if (resp->hwrm_intf_maj_8b >= 1) { 10476 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10477 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10478 } 10479 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10480 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10481 10482 bp->chip_num = le16_to_cpu(resp->chip_num); 10483 bp->chip_rev = resp->chip_rev; 10484 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10485 !resp->chip_metal) 10486 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10487 10488 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10489 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10490 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10491 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10492 10493 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10494 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10495 10496 if (dev_caps_cfg & 10497 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10498 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10499 10500 if (dev_caps_cfg & 10501 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10502 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10503 10504 if (dev_caps_cfg & 10505 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10506 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10507 10508 hwrm_ver_get_exit: 10509 hwrm_req_drop(bp, req); 10510 return rc; 10511 } 10512 10513 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10514 { 10515 struct hwrm_fw_set_time_input *req; 10516 struct tm tm; 10517 time64_t now = ktime_get_real_seconds(); 10518 int rc; 10519 10520 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10521 bp->hwrm_spec_code < 0x10400) 10522 return -EOPNOTSUPP; 10523 10524 time64_to_tm(now, 0, &tm); 10525 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10526 if (rc) 10527 return rc; 10528 10529 req->year = cpu_to_le16(1900 + tm.tm_year); 10530 req->month = 1 + tm.tm_mon; 10531 req->day = tm.tm_mday; 10532 req->hour = tm.tm_hour; 10533 req->minute = tm.tm_min; 10534 req->second = tm.tm_sec; 10535 return hwrm_req_send(bp, req); 10536 } 10537 10538 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10539 { 10540 u64 sw_tmp; 10541 10542 hw &= mask; 10543 sw_tmp = (*sw & ~mask) | hw; 10544 if (hw < (*sw & mask)) 10545 sw_tmp += mask + 1; 10546 WRITE_ONCE(*sw, sw_tmp); 10547 } 10548 10549 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10550 int count, bool ignore_zero) 10551 { 10552 int i; 10553 10554 for (i = 0; i < count; i++) { 10555 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10556 10557 if (ignore_zero && !hw) 10558 continue; 10559 10560 if (masks[i] == -1ULL) 10561 sw_stats[i] = hw; 10562 else 10563 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10564 } 10565 } 10566 10567 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10568 { 10569 if (!stats->hw_stats) 10570 return; 10571 10572 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10573 stats->hw_masks, stats->len / 8, false); 10574 } 10575 10576 static void bnxt_accumulate_ring_stats(struct bnxt *bp) 10577 { 10578 struct bnxt_stats_mem *ring0_stats; 10579 bool ignore_zero = false; 10580 int i; 10581 10582 /* Chip bug. Counter intermittently becomes 0. */ 10583 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10584 ignore_zero = true; 10585 10586 for (i = 0; i < bp->cp_nr_rings; i++) { 10587 struct bnxt_napi *bnapi = bp->bnapi[i]; 10588 struct bnxt_cp_ring_info *cpr; 10589 struct bnxt_stats_mem *stats; 10590 10591 cpr = &bnapi->cp_ring; 10592 stats = &cpr->stats; 10593 if (!i) 10594 ring0_stats = stats; 10595 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10596 ring0_stats->hw_masks, 10597 ring0_stats->len / 8, ignore_zero); 10598 } 10599 } 10600 10601 static void bnxt_accumulate_port_stats(struct bnxt *bp) 10602 { 10603 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10604 struct bnxt_stats_mem *stats = &bp->port_stats; 10605 __le64 *hw_stats = stats->hw_stats; 10606 u64 *sw_stats = stats->sw_stats; 10607 u64 *masks = stats->hw_masks; 10608 int cnt; 10609 10610 cnt = sizeof(struct rx_port_stats) / 8; 10611 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10612 10613 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10614 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10615 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10616 cnt = sizeof(struct tx_port_stats) / 8; 10617 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10618 } 10619 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10620 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10621 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10622 } 10623 } 10624 10625 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10626 { 10627 bnxt_accumulate_ring_stats(bp); 10628 bnxt_accumulate_port_stats(bp); 10629 } 10630 10631 /* Re-accumulate ring stats from DMA buffers if stale. 10632 * uAPIs for reading sw_stats should call this first. 10633 * 10634 * We promise user space update frequency of bp->stats_coal_ticks but 10635 * the update is a two step process - first device updates the DMA buffer, 10636 * then we have to update from that buffer to driver stats in the service work. 10637 * Worst case we would be 2x off from the desired frequency. 10638 * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily. 10639 * 10640 * Ideally we would split the user-configured time into two portions, 10641 * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have 10642 * too coarse granularity to play such tricks. 10643 */ 10644 void bnxt_sync_ring_stats(struct bnxt *bp) 10645 { 10646 unsigned long stale; 10647 10648 if (!netif_running(bp->dev) || !bp->stats_coal_ticks) 10649 return; 10650 10651 spin_lock(&bp->stats_lock); 10652 stale = usecs_to_jiffies(bp->stats_coal_ticks / 5); 10653 if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) { 10654 bnxt_accumulate_ring_stats(bp); 10655 bp->stats_updated_jiffies = jiffies; 10656 } 10657 spin_unlock(&bp->stats_lock); 10658 } 10659 10660 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10661 { 10662 struct hwrm_port_qstats_input *req; 10663 struct bnxt_pf_info *pf = &bp->pf; 10664 int rc; 10665 10666 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10667 return 0; 10668 10669 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10670 return -EOPNOTSUPP; 10671 10672 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10673 if (rc) 10674 return rc; 10675 10676 req->flags = flags; 10677 req->port_id = cpu_to_le16(pf->port_id); 10678 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10679 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10680 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10681 return hwrm_req_send(bp, req); 10682 } 10683 10684 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10685 { 10686 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10687 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10688 struct hwrm_port_qstats_ext_output *resp_qs; 10689 struct hwrm_port_qstats_ext_input *req_qs; 10690 struct bnxt_pf_info *pf = &bp->pf; 10691 u32 tx_stat_size; 10692 int rc; 10693 10694 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10695 return 0; 10696 10697 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10698 return -EOPNOTSUPP; 10699 10700 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10701 if (rc) 10702 return rc; 10703 10704 req_qs->flags = flags; 10705 req_qs->port_id = cpu_to_le16(pf->port_id); 10706 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10707 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10708 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10709 sizeof(struct tx_port_stats_ext) : 0; 10710 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10711 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10712 resp_qs = hwrm_req_hold(bp, req_qs); 10713 rc = hwrm_req_send(bp, req_qs); 10714 if (!rc) { 10715 bp->fw_rx_stats_ext_size = 10716 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10717 if (BNXT_FW_MAJ(bp) < 220 && 10718 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10719 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10720 10721 bp->fw_tx_stats_ext_size = tx_stat_size ? 10722 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10723 } else { 10724 bp->fw_rx_stats_ext_size = 0; 10725 bp->fw_tx_stats_ext_size = 0; 10726 } 10727 hwrm_req_drop(bp, req_qs); 10728 10729 if (flags) 10730 return rc; 10731 10732 if (bp->fw_tx_stats_ext_size <= 10733 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10734 bp->pri2cos_valid = 0; 10735 return rc; 10736 } 10737 10738 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10739 if (rc) 10740 return rc; 10741 10742 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10743 10744 resp_qc = hwrm_req_hold(bp, req_qc); 10745 rc = hwrm_req_send(bp, req_qc); 10746 if (!rc) { 10747 u8 *pri2cos; 10748 int i, j; 10749 10750 pri2cos = &resp_qc->pri0_cos_queue_id; 10751 for (i = 0; i < 8; i++) { 10752 u8 queue_id = pri2cos[i]; 10753 u8 queue_idx; 10754 10755 /* Per port queue IDs start from 0, 10, 20, etc */ 10756 queue_idx = queue_id % 10; 10757 if (queue_idx > BNXT_MAX_QUEUE) { 10758 bp->pri2cos_valid = false; 10759 hwrm_req_drop(bp, req_qc); 10760 return rc; 10761 } 10762 for (j = 0; j < bp->max_q; j++) { 10763 if (bp->q_ids[j] == queue_id) 10764 bp->pri2cos_idx[i] = queue_idx; 10765 } 10766 } 10767 bp->pri2cos_valid = true; 10768 } 10769 hwrm_req_drop(bp, req_qc); 10770 10771 return rc; 10772 } 10773 10774 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10775 { 10776 bnxt_hwrm_tunnel_dst_port_free(bp, 10777 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10778 bnxt_hwrm_tunnel_dst_port_free(bp, 10779 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10780 } 10781 10782 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10783 { 10784 int rc, i; 10785 u32 tpa_flags = 0; 10786 10787 if (set_tpa) 10788 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10789 else if (BNXT_NO_FW_ACCESS(bp)) 10790 return 0; 10791 for (i = 0; i < bp->nr_vnics; i++) { 10792 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10793 if (rc) { 10794 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10795 i, rc); 10796 return rc; 10797 } 10798 } 10799 return 0; 10800 } 10801 10802 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10803 { 10804 int i; 10805 10806 for (i = 0; i < bp->nr_vnics; i++) 10807 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10808 } 10809 10810 static void bnxt_clear_vnic(struct bnxt *bp) 10811 { 10812 if (!bp->vnic_info) 10813 return; 10814 10815 bnxt_hwrm_clear_vnic_filter(bp); 10816 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10817 /* clear all RSS setting before free vnic ctx */ 10818 bnxt_hwrm_clear_vnic_rss(bp); 10819 bnxt_hwrm_vnic_ctx_free(bp); 10820 } 10821 /* before free the vnic, undo the vnic tpa settings */ 10822 if (bp->flags & BNXT_FLAG_TPA) 10823 bnxt_set_tpa(bp, false); 10824 bnxt_hwrm_vnic_free(bp); 10825 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10826 bnxt_hwrm_vnic_ctx_free(bp); 10827 } 10828 10829 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10830 bool irq_re_init) 10831 { 10832 bnxt_clear_vnic(bp); 10833 bnxt_hwrm_ring_free(bp, close_path); 10834 bnxt_hwrm_ring_grp_free(bp); 10835 if (irq_re_init) { 10836 bnxt_hwrm_stat_ctx_free(bp); 10837 bnxt_hwrm_free_tunnel_ports(bp); 10838 } 10839 } 10840 10841 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10842 { 10843 struct hwrm_func_cfg_input *req; 10844 u8 evb_mode; 10845 int rc; 10846 10847 if (br_mode == BRIDGE_MODE_VEB) 10848 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10849 else if (br_mode == BRIDGE_MODE_VEPA) 10850 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10851 else 10852 return -EINVAL; 10853 10854 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10855 if (rc) 10856 return rc; 10857 10858 req->fid = cpu_to_le16(0xffff); 10859 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10860 req->evb_mode = evb_mode; 10861 return hwrm_req_send(bp, req); 10862 } 10863 10864 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10865 { 10866 struct hwrm_func_cfg_input *req; 10867 int rc; 10868 10869 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10870 return 0; 10871 10872 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10873 if (rc) 10874 return rc; 10875 10876 req->fid = cpu_to_le16(0xffff); 10877 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10878 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10879 if (size == 128) 10880 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10881 10882 return hwrm_req_send(bp, req); 10883 } 10884 10885 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10886 { 10887 int rc; 10888 10889 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10890 goto skip_rss_ctx; 10891 10892 /* allocate context for vnic */ 10893 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10894 if (rc) { 10895 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10896 vnic->vnic_id, rc); 10897 goto vnic_setup_err; 10898 } 10899 bp->rsscos_nr_ctxs++; 10900 10901 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10902 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10903 if (rc) { 10904 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10905 vnic->vnic_id, rc); 10906 goto vnic_setup_err; 10907 } 10908 bp->rsscos_nr_ctxs++; 10909 } 10910 10911 skip_rss_ctx: 10912 bnxt_fill_hw_rss_tbl(bp, vnic); 10913 /* configure default vnic, ring grp */ 10914 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10915 if (rc) { 10916 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10917 vnic->vnic_id, rc); 10918 goto vnic_setup_err; 10919 } 10920 10921 /* Enable RSS hashing on vnic */ 10922 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10923 if (rc) { 10924 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10925 vnic->vnic_id, rc); 10926 goto vnic_setup_err; 10927 } 10928 10929 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10930 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10931 if (rc) { 10932 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10933 vnic->vnic_id, rc); 10934 } 10935 } 10936 10937 vnic_setup_err: 10938 return rc; 10939 } 10940 10941 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10942 u8 valid) 10943 { 10944 struct hwrm_vnic_update_input *req; 10945 int rc; 10946 10947 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10948 if (rc) 10949 return rc; 10950 10951 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10952 10953 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10954 req->mru = cpu_to_le16(vnic->mru); 10955 10956 req->enables = cpu_to_le32(valid); 10957 10958 return hwrm_req_send(bp, req); 10959 } 10960 10961 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10962 { 10963 int rc; 10964 10965 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10966 if (rc) { 10967 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10968 vnic->vnic_id, rc); 10969 return rc; 10970 } 10971 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10972 if (rc) 10973 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10974 vnic->vnic_id, rc); 10975 return rc; 10976 } 10977 10978 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10979 { 10980 int rc, i, nr_ctxs; 10981 10982 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10983 for (i = 0; i < nr_ctxs; i++) { 10984 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10985 if (rc) { 10986 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10987 vnic->vnic_id, i, rc); 10988 break; 10989 } 10990 bp->rsscos_nr_ctxs++; 10991 } 10992 if (i < nr_ctxs) 10993 return -ENOMEM; 10994 10995 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10996 if (rc) 10997 return rc; 10998 10999 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 11000 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 11001 if (rc) { 11002 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 11003 vnic->vnic_id, rc); 11004 } 11005 } 11006 return rc; 11007 } 11008 11009 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 11010 { 11011 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11012 return __bnxt_setup_vnic_p5(bp, vnic); 11013 else 11014 return __bnxt_setup_vnic(bp, vnic); 11015 } 11016 11017 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 11018 struct bnxt_vnic_info *vnic, 11019 u16 start_rx_ring_idx, int rx_rings) 11020 { 11021 int rc; 11022 11023 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 11024 if (rc) { 11025 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 11026 vnic->vnic_id, rc); 11027 return rc; 11028 } 11029 return bnxt_setup_vnic(bp, vnic); 11030 } 11031 11032 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 11033 { 11034 struct bnxt_vnic_info *vnic; 11035 int i, rc = 0; 11036 11037 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 11038 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 11039 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 11040 } 11041 11042 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11043 return 0; 11044 11045 for (i = 0; i < bp->rx_nr_rings; i++) { 11046 u16 vnic_id = i + 1; 11047 u16 ring_id = i; 11048 11049 if (vnic_id >= bp->nr_vnics) 11050 break; 11051 11052 vnic = &bp->vnic_info[vnic_id]; 11053 vnic->flags |= BNXT_VNIC_RFS_FLAG; 11054 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 11055 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 11056 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 11057 break; 11058 } 11059 return rc; 11060 } 11061 11062 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 11063 bool all) 11064 { 11065 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 11066 struct bnxt_filter_base *usr_fltr, *tmp; 11067 struct bnxt_ntuple_filter *ntp_fltr; 11068 int i; 11069 11070 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 11071 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 11072 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 11073 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 11074 } 11075 if (!all) 11076 return; 11077 11078 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 11079 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 11080 usr_fltr->fw_vnic_id == rss_ctx->index) { 11081 ntp_fltr = container_of(usr_fltr, 11082 struct bnxt_ntuple_filter, 11083 base); 11084 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 11085 bnxt_del_ntp_filter(bp, ntp_fltr); 11086 bnxt_del_one_usr_fltr(bp, usr_fltr); 11087 } 11088 } 11089 11090 if (vnic->rss_table) 11091 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 11092 vnic->rss_table, 11093 vnic->rss_table_dma_addr); 11094 bp->num_rss_ctx--; 11095 } 11096 11097 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic, 11098 int rxr_id) 11099 { 11100 u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 11101 int i, vnic_rx; 11102 11103 /* Ntuple VNIC always has all the rx rings. Any change of ring id 11104 * must be updated because a future filter may use it. 11105 */ 11106 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 11107 return true; 11108 11109 for (i = 0; i < tbl_size; i++) { 11110 if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 11111 vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 11112 else 11113 vnic_rx = bp->rss_indir_tbl[i]; 11114 11115 if (rxr_id == vnic_rx) 11116 return true; 11117 } 11118 11119 return false; 11120 } 11121 11122 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic, 11123 u16 mru, int rxr_id) 11124 { 11125 int rc; 11126 11127 if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id)) 11128 return 0; 11129 11130 if (mru) { 11131 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 11132 if (rc) { 11133 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 11134 vnic->vnic_id, rc); 11135 return rc; 11136 } 11137 if (rxr_id == vnic->default_rx_ring) { 11138 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 11139 if (rc) 11140 return rc; 11141 } 11142 } 11143 vnic->mru = mru; 11144 bnxt_hwrm_vnic_update(bp, vnic, 11145 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 11146 11147 return 0; 11148 } 11149 11150 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id) 11151 { 11152 struct ethtool_rxfh_context *ctx; 11153 unsigned long context; 11154 int rc; 11155 11156 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 11157 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 11158 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 11159 11160 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id); 11161 if (rc) 11162 return rc; 11163 } 11164 11165 return 0; 11166 } 11167 11168 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 11169 { 11170 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 11171 struct ethtool_rxfh_context *ctx; 11172 unsigned long context; 11173 11174 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 11175 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 11176 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 11177 11178 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 11179 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 11180 __bnxt_setup_vnic_p5(bp, vnic)) { 11181 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 11182 rss_ctx->index); 11183 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 11184 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 11185 } 11186 } 11187 } 11188 11189 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 11190 { 11191 struct ethtool_rxfh_context *ctx; 11192 unsigned long context; 11193 11194 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 11195 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 11196 11197 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 11198 } 11199 } 11200 11201 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 11202 static bool bnxt_promisc_ok(struct bnxt *bp) 11203 { 11204 #ifdef CONFIG_BNXT_SRIOV 11205 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 11206 return false; 11207 #endif 11208 return true; 11209 } 11210 11211 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 11212 { 11213 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 11214 unsigned int rc = 0; 11215 11216 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 11217 if (rc) { 11218 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 11219 rc); 11220 return rc; 11221 } 11222 11223 /* Setup the proper default RX ring */ 11224 bnxt_fill_hw_rss_tbl(bp, vnic); 11225 11226 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 11227 if (rc) { 11228 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 11229 rc); 11230 return rc; 11231 } 11232 return rc; 11233 } 11234 11235 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool); 11236 static bool bnxt_mc_list_updated(struct bnxt *, u32 *, 11237 const struct netdev_hw_addr_list *); 11238 11239 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 11240 { 11241 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 11242 int rc = 0; 11243 unsigned int rx_nr_rings = bp->rx_nr_rings; 11244 11245 if (irq_re_init) { 11246 rc = bnxt_hwrm_stat_ctx_alloc(bp); 11247 if (rc) { 11248 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 11249 rc); 11250 goto err_out; 11251 } 11252 } 11253 11254 rc = bnxt_hwrm_ring_alloc(bp); 11255 if (rc) { 11256 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 11257 goto err_out; 11258 } 11259 11260 rc = bnxt_hwrm_ring_grp_alloc(bp); 11261 if (rc) { 11262 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 11263 goto err_out; 11264 } 11265 11266 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11267 rx_nr_rings--; 11268 11269 /* default vnic 0 */ 11270 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 11271 if (rc) { 11272 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 11273 goto err_out; 11274 } 11275 11276 if (BNXT_VF(bp)) 11277 bnxt_hwrm_func_qcfg(bp); 11278 11279 rc = bnxt_setup_vnic(bp, vnic); 11280 if (rc) 11281 goto err_out; 11282 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 11283 bnxt_hwrm_update_rss_hash_cfg(bp); 11284 11285 if (bp->flags & BNXT_FLAG_RFS) { 11286 rc = bnxt_alloc_rfs_vnics(bp); 11287 if (rc) 11288 goto err_out; 11289 } 11290 11291 if (bp->flags & BNXT_FLAG_TPA) { 11292 rc = bnxt_set_tpa(bp, true); 11293 if (rc) 11294 goto err_out; 11295 } 11296 11297 if (BNXT_VF(bp)) 11298 bnxt_update_vf_mac(bp); 11299 11300 /* Filter for default vnic 0 */ 11301 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 11302 if (rc) { 11303 if (BNXT_VF(bp) && rc == -ENODEV) 11304 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 11305 else 11306 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 11307 goto err_out; 11308 } 11309 vnic->uc_filter_count = 1; 11310 11311 vnic->rx_mask = 0; 11312 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 11313 goto skip_rx_mask; 11314 11315 if (bp->dev->flags & IFF_BROADCAST) 11316 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 11317 11318 if (bp->dev->flags & IFF_PROMISC) 11319 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 11320 11321 if (bp->dev->flags & IFF_ALLMULTI) { 11322 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 11323 vnic->mc_list_count = 0; 11324 } else if (bp->dev->flags & IFF_MULTICAST) { 11325 u32 mask = 0; 11326 11327 bnxt_mc_list_updated(bp, &mask, &bp->dev->mc); 11328 vnic->rx_mask |= mask; 11329 } 11330 11331 rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true); 11332 if (rc == -EAGAIN) { 11333 netif_rx_mode_schedule_retry(bp->dev); 11334 rc = 0; 11335 } else if (rc) { 11336 goto err_out; 11337 } 11338 11339 skip_rx_mask: 11340 rc = bnxt_hwrm_set_coal(bp); 11341 if (rc) 11342 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 11343 rc); 11344 11345 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11346 rc = bnxt_setup_nitroa0_vnic(bp); 11347 if (rc) 11348 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 11349 rc); 11350 } 11351 11352 if (BNXT_VF(bp)) { 11353 bnxt_hwrm_func_qcfg(bp); 11354 netdev_update_features(bp->dev); 11355 } 11356 11357 return 0; 11358 11359 err_out: 11360 bnxt_hwrm_resource_free(bp, 0, true); 11361 11362 return rc; 11363 } 11364 11365 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 11366 { 11367 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 11368 return 0; 11369 } 11370 11371 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 11372 { 11373 int rc; 11374 11375 bnxt_init_cp_rings(bp); 11376 rc = bnxt_init_rx_rings(bp); 11377 if (rc) 11378 return rc; 11379 11380 bnxt_init_tx_rings(bp); 11381 bnxt_init_ring_grps(bp, irq_re_init); 11382 bnxt_init_vnics(bp); 11383 11384 return bnxt_init_chip(bp, irq_re_init); 11385 } 11386 11387 static int bnxt_set_real_num_queues(struct bnxt *bp) 11388 { 11389 int rc; 11390 struct net_device *dev = bp->dev; 11391 11392 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 11393 bp->tx_nr_rings_xdp); 11394 if (rc) 11395 return rc; 11396 11397 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 11398 if (rc) 11399 return rc; 11400 11401 #ifdef CONFIG_RFS_ACCEL 11402 if (bp->flags & BNXT_FLAG_RFS) 11403 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 11404 #endif 11405 11406 return rc; 11407 } 11408 11409 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11410 bool shared) 11411 { 11412 int _rx = *rx, _tx = *tx; 11413 11414 if (shared) { 11415 *rx = min_t(int, _rx, max); 11416 *tx = min_t(int, _tx, max); 11417 } else { 11418 if (max < 2) 11419 return -ENOMEM; 11420 11421 while (_rx + _tx > max) { 11422 if (_rx > _tx && _rx > 1) 11423 _rx--; 11424 else if (_tx > 1) 11425 _tx--; 11426 } 11427 *rx = _rx; 11428 *tx = _tx; 11429 } 11430 return 0; 11431 } 11432 11433 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 11434 { 11435 return (tx - tx_xdp) / tx_sets + tx_xdp; 11436 } 11437 11438 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 11439 { 11440 int tcs = bp->num_tc; 11441 11442 if (!tcs) 11443 tcs = 1; 11444 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 11445 } 11446 11447 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 11448 { 11449 int tcs = bp->num_tc; 11450 11451 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 11452 bp->tx_nr_rings_xdp; 11453 } 11454 11455 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11456 bool sh) 11457 { 11458 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 11459 11460 if (tx_cp != *tx) { 11461 int tx_saved = tx_cp, rc; 11462 11463 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 11464 if (rc) 11465 return rc; 11466 if (tx_cp != tx_saved) 11467 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 11468 return 0; 11469 } 11470 return __bnxt_trim_rings(bp, rx, tx, max, sh); 11471 } 11472 11473 static void bnxt_setup_msix(struct bnxt *bp) 11474 { 11475 const int len = sizeof(bp->irq_tbl[0].name); 11476 struct net_device *dev = bp->dev; 11477 int tcs, i; 11478 11479 tcs = bp->num_tc; 11480 if (tcs) { 11481 int i, off, count; 11482 11483 for (i = 0; i < tcs; i++) { 11484 count = bp->tx_nr_rings_per_tc; 11485 off = BNXT_TC_TO_RING_BASE(bp, i); 11486 netdev_set_tc_queue(dev, i, count, off); 11487 } 11488 } 11489 11490 for (i = 0; i < bp->cp_nr_rings; i++) { 11491 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11492 char *attr; 11493 11494 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 11495 attr = "TxRx"; 11496 else if (i < bp->rx_nr_rings) 11497 attr = "rx"; 11498 else 11499 attr = "tx"; 11500 11501 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 11502 attr, i); 11503 bp->irq_tbl[map_idx].handler = bnxt_msix; 11504 } 11505 } 11506 11507 static int bnxt_init_int_mode(struct bnxt *bp); 11508 11509 static int bnxt_change_msix(struct bnxt *bp, int total) 11510 { 11511 struct msi_map map; 11512 int i; 11513 11514 /* add MSIX to the end if needed */ 11515 for (i = bp->total_irqs; i < total; i++) { 11516 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 11517 if (map.index < 0) 11518 return bp->total_irqs; 11519 bp->irq_tbl[i].vector = map.virq; 11520 bp->total_irqs++; 11521 } 11522 11523 /* trim MSIX from the end if needed */ 11524 for (i = bp->total_irqs; i > total; i--) { 11525 map.index = i - 1; 11526 map.virq = bp->irq_tbl[i - 1].vector; 11527 pci_msix_free_irq(bp->pdev, map); 11528 bp->total_irqs--; 11529 } 11530 return bp->total_irqs; 11531 } 11532 11533 static int bnxt_setup_int_mode(struct bnxt *bp) 11534 { 11535 int rc; 11536 11537 if (!bp->irq_tbl) { 11538 rc = bnxt_init_int_mode(bp); 11539 if (rc || !bp->irq_tbl) 11540 return rc ?: -ENODEV; 11541 } 11542 11543 bnxt_setup_msix(bp); 11544 11545 rc = bnxt_set_real_num_queues(bp); 11546 return rc; 11547 } 11548 11549 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11550 { 11551 return bp->hw_resc.max_rsscos_ctxs; 11552 } 11553 11554 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11555 { 11556 return bp->hw_resc.max_vnics; 11557 } 11558 11559 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11560 { 11561 return bp->hw_resc.max_stat_ctxs; 11562 } 11563 11564 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11565 { 11566 return bp->hw_resc.max_cp_rings; 11567 } 11568 11569 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11570 { 11571 unsigned int cp = bp->hw_resc.max_cp_rings; 11572 11573 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11574 cp -= bnxt_get_ulp_msix_num(bp); 11575 11576 return cp; 11577 } 11578 11579 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11580 { 11581 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11582 11583 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11584 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11585 11586 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11587 } 11588 11589 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11590 { 11591 bp->hw_resc.max_irqs = max_irqs; 11592 } 11593 11594 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11595 { 11596 unsigned int cp; 11597 11598 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11599 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11600 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11601 else 11602 return cp - bp->cp_nr_rings; 11603 } 11604 11605 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11606 { 11607 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11608 } 11609 11610 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11611 { 11612 int max_irq = bnxt_get_max_func_irqs(bp); 11613 int total_req = bp->cp_nr_rings + num; 11614 11615 if (max_irq < total_req) { 11616 num = max_irq - bp->cp_nr_rings; 11617 if (num <= 0) 11618 return 0; 11619 } 11620 return num; 11621 } 11622 11623 static int bnxt_get_num_msix(struct bnxt *bp) 11624 { 11625 if (!BNXT_NEW_RM(bp)) 11626 return bnxt_get_max_func_irqs(bp); 11627 11628 return bnxt_nq_rings_in_use(bp); 11629 } 11630 11631 static int bnxt_init_int_mode(struct bnxt *bp) 11632 { 11633 int i, total_vecs, max, rc, min = 1, ulp_msix, tx_cp, tbl_size; 11634 11635 total_vecs = bnxt_get_num_msix(bp); 11636 max = bnxt_get_max_func_irqs(bp); 11637 if (total_vecs > max) 11638 total_vecs = max; 11639 11640 if (!total_vecs) 11641 return 0; 11642 11643 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11644 min = 2; 11645 11646 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11647 PCI_IRQ_MSIX); 11648 ulp_msix = bnxt_get_ulp_msix_num(bp); 11649 if (total_vecs < 0 || total_vecs < ulp_msix) { 11650 rc = -ENODEV; 11651 goto msix_setup_exit; 11652 } 11653 11654 tbl_size = total_vecs; 11655 if (pci_msix_can_alloc_dyn(bp->pdev)) 11656 tbl_size = max; 11657 bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size); 11658 if (!bp->irq_tbl) { 11659 rc = -ENOMEM; 11660 goto msix_setup_exit; 11661 } 11662 11663 for (i = 0; i < total_vecs; i++) 11664 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11665 11666 bp->total_irqs = total_vecs; 11667 /* Trim rings based upon num of vectors allocated */ 11668 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11669 total_vecs - ulp_msix, min == 1); 11670 if (rc) 11671 goto msix_setup_exit; 11672 11673 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11674 bp->cp_nr_rings = (min == 1) ? 11675 max_t(int, tx_cp, bp->rx_nr_rings) : 11676 tx_cp + bp->rx_nr_rings; 11677 11678 return 0; 11679 11680 msix_setup_exit: 11681 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11682 kfree(bp->irq_tbl); 11683 bp->irq_tbl = NULL; 11684 pci_free_irq_vectors(bp->pdev); 11685 return rc; 11686 } 11687 11688 static void bnxt_clear_int_mode(struct bnxt *bp) 11689 { 11690 pci_free_irq_vectors(bp->pdev); 11691 11692 kfree(bp->irq_tbl); 11693 bp->irq_tbl = NULL; 11694 } 11695 11696 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11697 { 11698 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 11699 bool irq_cleared = false; 11700 bool irq_change = false; 11701 int tcs = bp->num_tc; 11702 int irqs_required; 11703 int rc; 11704 11705 if (!bnxt_need_reserve_rings(bp)) 11706 return 0; 11707 11708 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) { 11709 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11710 11711 if (ulp_msix > bp->ulp_num_msix_want) 11712 ulp_msix = bp->ulp_num_msix_want; 11713 irqs_required = ulp_msix + bp->cp_nr_rings; 11714 } else { 11715 irqs_required = bnxt_get_num_msix(bp); 11716 } 11717 11718 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11719 irq_change = true; 11720 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11721 bnxt_ulp_irq_stop(bp); 11722 bnxt_clear_int_mode(bp); 11723 irq_cleared = true; 11724 } 11725 } 11726 rc = __bnxt_reserve_rings(bp); 11727 if (irq_cleared) { 11728 if (!rc) 11729 rc = bnxt_init_int_mode(bp); 11730 bnxt_ulp_irq_restart(bp, rc); 11731 } else if (irq_change && !rc) { 11732 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11733 rc = -ENOSPC; 11734 } 11735 if (rc) { 11736 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11737 return rc; 11738 } 11739 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11740 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11741 netdev_err(bp->dev, "tx ring reservation failure\n"); 11742 netdev_reset_tc(bp->dev); 11743 bp->num_tc = 0; 11744 if (bp->tx_nr_rings_xdp) 11745 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11746 else 11747 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11748 return -ENOMEM; 11749 } 11750 return 0; 11751 } 11752 11753 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx) 11754 { 11755 struct bnxt_tx_ring_info *txr; 11756 struct netdev_queue *txq; 11757 struct bnxt_napi *bnapi; 11758 int i; 11759 11760 bnapi = bp->bnapi[idx]; 11761 bnxt_for_each_napi_tx(i, bnapi, txr) { 11762 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11763 synchronize_net(); 11764 11765 if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) { 11766 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11767 if (txq) { 11768 __netif_tx_lock_bh(txq); 11769 netif_tx_stop_queue(txq); 11770 __netif_tx_unlock_bh(txq); 11771 } 11772 } 11773 11774 if (!bp->tph_mode) 11775 continue; 11776 11777 bnxt_hwrm_tx_ring_free(bp, txr, true); 11778 bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr); 11779 bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index); 11780 bnxt_clear_one_cp_ring(bp, txr->tx_cpr); 11781 } 11782 } 11783 11784 static int bnxt_tx_queue_start(struct bnxt *bp, int idx) 11785 { 11786 struct bnxt_tx_ring_info *txr; 11787 struct netdev_queue *txq; 11788 struct bnxt_napi *bnapi; 11789 int rc, i; 11790 11791 bnapi = bp->bnapi[idx]; 11792 /* All rings have been reserved and previously allocated. 11793 * Reallocating with the same parameters should never fail. 11794 */ 11795 bnxt_for_each_napi_tx(i, bnapi, txr) { 11796 if (!bp->tph_mode) 11797 goto start_tx; 11798 11799 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 11800 if (rc) 11801 return rc; 11802 11803 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false); 11804 if (rc) 11805 return rc; 11806 11807 txr->tx_prod = 0; 11808 txr->tx_cons = 0; 11809 txr->tx_hw_cons = 0; 11810 txr->kick_pending = 0; 11811 txr->kick_txbd0 = NULL; 11812 start_tx: 11813 WRITE_ONCE(txr->dev_state, 0); 11814 synchronize_net(); 11815 11816 if (bnapi->flags & BNXT_NAPI_FLAG_XDP) 11817 continue; 11818 11819 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11820 if (txq) 11821 netif_tx_start_queue(txq); 11822 } 11823 11824 return 0; 11825 } 11826 11827 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify, 11828 const cpumask_t *mask) 11829 { 11830 struct bnxt_irq *irq; 11831 u16 tag; 11832 11833 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11834 11835 cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask); 11836 set_bit(irq->ring_nr, irq->bp->ring_affinity_set); 11837 11838 #ifdef CONFIG_RFS_ACCEL 11839 if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) { 11840 int err; 11841 11842 err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr, 11843 mask); 11844 if (err) 11845 netdev_warn(irq->bp->dev, 11846 "aRFS rmap update failed: %d\n", err); 11847 } 11848 #endif 11849 11850 if (!irq->bp->tph_mode) 11851 return; 11852 11853 if (irq->ring_nr >= irq->bp->rx_nr_rings) 11854 return; 11855 11856 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11857 cpumask_first(mask), &tag)) 11858 return; 11859 11860 WRITE_ONCE(irq->new_tag, tag); 11861 bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT); 11862 } 11863 11864 static void bnxt_irq_affinity_release(struct kref *ref) 11865 { 11866 struct irq_affinity_notify *notify = 11867 container_of(ref, struct irq_affinity_notify, kref); 11868 struct bnxt_irq *irq; 11869 11870 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11871 11872 if (!irq->bp->tph_mode) 11873 return; 11874 11875 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) { 11876 netdev_err(irq->bp->dev, 11877 "Setting ST=0 for MSIX entry %d failed\n", 11878 irq->msix_nr); 11879 return; 11880 } 11881 } 11882 11883 static void bnxt_release_irq_notifier(struct bnxt_irq *irq) 11884 { 11885 irq_set_affinity_notifier(irq->vector, NULL); 11886 } 11887 11888 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq) 11889 { 11890 struct irq_affinity_notify *notify; 11891 11892 irq->bp = bp; 11893 11894 /* Register IRQ affinity notifier */ 11895 notify = &irq->affinity_notify; 11896 notify->irq = irq->vector; 11897 notify->notify = bnxt_irq_affinity_notify; 11898 notify->release = bnxt_irq_affinity_release; 11899 11900 irq_set_affinity_notifier(irq->vector, notify); 11901 } 11902 11903 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp) 11904 { 11905 int i; 11906 11907 bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs); 11908 if (!bp->ring_cpu_mask) 11909 return -ENOMEM; 11910 11911 bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL); 11912 if (!bp->ring_affinity_set) 11913 return -ENOMEM; 11914 11915 for (i = 0; i < bp->max_irqs; i++) 11916 if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL)) 11917 return -ENOMEM; 11918 11919 return 0; 11920 } 11921 11922 static void bnxt_free_ring_cpu_masks(struct bnxt *bp) 11923 { 11924 int i; 11925 11926 if (!bp->ring_cpu_mask) 11927 return; 11928 11929 for (i = 0; i < bp->max_irqs; i++) 11930 free_cpumask_var(bp->ring_cpu_mask[i]); 11931 11932 bitmap_free(bp->ring_affinity_set); 11933 bp->ring_affinity_set = NULL; 11934 kfree(bp->ring_cpu_mask); 11935 bp->ring_cpu_mask = NULL; 11936 } 11937 11938 static void bnxt_free_irq(struct bnxt *bp) 11939 { 11940 struct bnxt_irq *irq; 11941 int i; 11942 11943 if (!bp->irq_tbl || !bp->bnapi) 11944 return; 11945 11946 for (i = 0; i < bp->cp_nr_rings; i++) { 11947 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11948 11949 irq = &bp->irq_tbl[map_idx]; 11950 if (irq->requested) { 11951 bnxt_release_irq_notifier(irq); 11952 irq_update_affinity_hint(irq->vector, NULL); 11953 free_irq(irq->vector, bp->bnapi[i]); 11954 } 11955 11956 irq->requested = 0; 11957 irq->tag = 0; 11958 irq->new_tag = 0; 11959 } 11960 11961 /* Disable TPH support */ 11962 pcie_disable_tph(bp->pdev); 11963 bp->tph_mode = 0; 11964 11965 #ifdef CONFIG_RFS_ACCEL 11966 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11967 bp->dev->rx_cpu_rmap = NULL; 11968 #endif 11969 } 11970 11971 static int bnxt_request_irq(struct bnxt *bp) 11972 { 11973 const int numa_node = dev_to_node(&bp->pdev->dev); 11974 struct cpu_rmap *rmap = NULL; 11975 int i, j, rc = 0; 11976 unsigned long flags = 0; 11977 11978 rc = bnxt_setup_int_mode(bp); 11979 if (rc) { 11980 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11981 rc); 11982 return rc; 11983 } 11984 #ifdef CONFIG_RFS_ACCEL 11985 rmap = bp->dev->rx_cpu_rmap; 11986 #endif 11987 11988 /* Enable TPH support as part of IRQ request */ 11989 if (BNXT_SUPPORTS_QUEUE_API(bp)) { 11990 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11991 if (!rc) 11992 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11993 } 11994 11995 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11996 struct cpumask *cpu_mask = bp->ring_cpu_mask[i]; 11997 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11998 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11999 u16 tag; 12000 12001 if (IS_ENABLED(CONFIG_RFS_ACCEL) && 12002 rmap && bp->bnapi[i]->rx_ring) { 12003 rc = irq_cpu_rmap_add(rmap, irq->vector); 12004 if (rc) 12005 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 12006 j); 12007 j++; 12008 } 12009 12010 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 12011 bp->bnapi[i]); 12012 if (rc) 12013 break; 12014 12015 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 12016 irq->requested = 1; 12017 irq->msix_nr = map_idx; 12018 irq->ring_nr = i; 12019 12020 /* Reuse the mask recorded before the IRQs were freed. Nothing 12021 * was recorded yet on the very first request, and the mask 12022 * may have gone stale if the CPUs went offline in between. 12023 */ 12024 if (!test_bit(i, bp->ring_affinity_set) || 12025 !cpumask_intersects(cpu_mask, cpu_online_mask)) { 12026 clear_bit(i, bp->ring_affinity_set); 12027 cpumask_clear(cpu_mask); 12028 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 12029 cpu_mask); 12030 } 12031 12032 /* Init ST table entry if we can get the mapping */ 12033 if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM, 12034 cpumask_first(cpu_mask), &tag)) { 12035 pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag); 12036 irq->tag = tag; 12037 irq->new_tag = tag; 12038 } 12039 12040 bnxt_register_irq_notifier(bp, irq); 12041 12042 /* Only put the IRQ back where it was configured to be, our own 12043 * placement is just a hint, the core spreads within 12044 * irq_default_affinity which we know nothing about. 12045 * Set after installing the notifier, if we race with the user 12046 * it's better to overwrite than miss the notification. 12047 */ 12048 if (test_bit(i, bp->ring_affinity_set)) 12049 rc = irq_set_affinity_and_hint(irq->vector, cpu_mask); 12050 else 12051 rc = irq_update_affinity_hint(irq->vector, cpu_mask); 12052 if (rc) { 12053 netdev_warn(bp->dev, 12054 "Setting IRQ affinity failed, IRQ = %d\n", 12055 irq->vector); 12056 break; 12057 } 12058 } 12059 return rc; 12060 } 12061 12062 static void bnxt_del_napi(struct bnxt *bp) 12063 { 12064 int i; 12065 12066 if (!bp->bnapi) 12067 return; 12068 12069 for (i = 0; i < bp->rx_nr_rings; i++) 12070 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 12071 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 12072 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 12073 12074 for (i = 0; i < bp->cp_nr_rings; i++) { 12075 struct bnxt_napi *bnapi = bp->bnapi[i]; 12076 12077 __netif_napi_del_locked(&bnapi->napi); 12078 } 12079 /* We called __netif_napi_del_locked(), we need 12080 * to respect an RCU grace period before freeing napi structures. 12081 */ 12082 synchronize_net(); 12083 } 12084 12085 static void bnxt_init_napi(struct bnxt *bp) 12086 { 12087 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 12088 unsigned int cp_nr_rings = bp->cp_nr_rings; 12089 struct bnxt_napi *bnapi; 12090 int i; 12091 12092 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 12093 poll_fn = bnxt_poll_p5; 12094 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 12095 cp_nr_rings--; 12096 12097 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 12098 12099 for (i = 0; i < cp_nr_rings; i++) { 12100 bnapi = bp->bnapi[i]; 12101 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 12102 bnapi->index); 12103 } 12104 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 12105 bnapi = bp->bnapi[cp_nr_rings]; 12106 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 12107 } 12108 } 12109 12110 static void bnxt_disable_napi(struct bnxt *bp) 12111 { 12112 int i; 12113 12114 if (!bp->bnapi || 12115 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 12116 return; 12117 12118 for (i = 0; i < bp->cp_nr_rings; i++) { 12119 struct bnxt_napi *bnapi = bp->bnapi[i]; 12120 struct bnxt_cp_ring_info *cpr; 12121 12122 cpr = &bnapi->cp_ring; 12123 if (bnapi->tx_fault) 12124 cpr->sw_stats->tx.tx_resets++; 12125 if (bnapi->in_reset) 12126 cpr->sw_stats->rx.rx_resets++; 12127 napi_disable_locked(&bnapi->napi); 12128 } 12129 } 12130 12131 static void bnxt_enable_napi(struct bnxt *bp) 12132 { 12133 int i; 12134 12135 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 12136 for (i = 0; i < bp->cp_nr_rings; i++) { 12137 struct bnxt_napi *bnapi = bp->bnapi[i]; 12138 struct bnxt_cp_ring_info *cpr; 12139 12140 bnapi->tx_fault = 0; 12141 12142 cpr = &bnapi->cp_ring; 12143 bnapi->in_reset = false; 12144 12145 if (bnapi->rx_ring) { 12146 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 12147 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 12148 } 12149 napi_enable_locked(&bnapi->napi); 12150 } 12151 } 12152 12153 void bnxt_tx_disable(struct bnxt *bp) 12154 { 12155 int i; 12156 struct bnxt_tx_ring_info *txr; 12157 12158 if (bp->tx_ring) { 12159 for (i = 0; i < bp->tx_nr_rings; i++) { 12160 txr = &bp->tx_ring[i]; 12161 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 12162 } 12163 } 12164 /* Make sure napi polls see @dev_state change */ 12165 synchronize_net(); 12166 /* Drop carrier first to prevent TX timeout */ 12167 netif_carrier_off(bp->dev); 12168 /* Stop all TX queues */ 12169 netif_tx_disable(bp->dev); 12170 } 12171 12172 void bnxt_tx_enable(struct bnxt *bp) 12173 { 12174 int i; 12175 struct bnxt_tx_ring_info *txr; 12176 12177 for (i = 0; i < bp->tx_nr_rings; i++) { 12178 txr = &bp->tx_ring[i]; 12179 WRITE_ONCE(txr->dev_state, 0); 12180 } 12181 /* Make sure napi polls see @dev_state change */ 12182 synchronize_net(); 12183 netif_tx_wake_all_queues(bp->dev); 12184 if (BNXT_LINK_IS_UP(bp)) 12185 netif_carrier_on(bp->dev); 12186 } 12187 12188 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 12189 { 12190 u8 active_fec = link_info->active_fec_sig_mode & 12191 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 12192 12193 switch (active_fec) { 12194 default: 12195 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 12196 return "None"; 12197 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 12198 return "Clause 74 BaseR"; 12199 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 12200 return "Clause 91 RS(528,514)"; 12201 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 12202 return "Clause 91 RS544_1XN"; 12203 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 12204 return "Clause 91 RS(544,514)"; 12205 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 12206 return "Clause 91 RS272_1XN"; 12207 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 12208 return "Clause 91 RS(272,257)"; 12209 } 12210 } 12211 12212 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info) 12213 { 12214 u8 reason = link_info->link_down_reason; 12215 12216 /* Multiple bits can be set, we report 1 bit only in order of 12217 * priority. 12218 */ 12219 if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF) 12220 return "(Remote fault)"; 12221 if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION) 12222 return "(OTP Speed limit violation)"; 12223 if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED) 12224 return "(Cable removed)"; 12225 if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT) 12226 return "(Module fault)"; 12227 if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST) 12228 return "(BMC request down)"; 12229 return ""; 12230 } 12231 12232 void bnxt_report_link(struct bnxt *bp) 12233 { 12234 if (BNXT_LINK_IS_UP(bp)) { 12235 const char *signal = ""; 12236 const char *flow_ctrl; 12237 const char *duplex; 12238 u32 speed; 12239 u16 fec; 12240 12241 netif_carrier_on(bp->dev); 12242 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 12243 if (speed == SPEED_UNKNOWN) { 12244 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 12245 return; 12246 } 12247 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 12248 duplex = "full"; 12249 else 12250 duplex = "half"; 12251 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 12252 flow_ctrl = "ON - receive & transmit"; 12253 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 12254 flow_ctrl = "ON - transmit"; 12255 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 12256 flow_ctrl = "ON - receive"; 12257 else 12258 flow_ctrl = "none"; 12259 if (bp->link_info.phy_qcfg_resp.option_flags & 12260 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 12261 u8 sig_mode = bp->link_info.active_fec_sig_mode & 12262 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 12263 switch (sig_mode) { 12264 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 12265 signal = "(NRZ) "; 12266 break; 12267 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 12268 signal = "(PAM4 56Gbps) "; 12269 break; 12270 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 12271 signal = "(PAM4 112Gbps) "; 12272 break; 12273 default: 12274 break; 12275 } 12276 } 12277 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 12278 speed, signal, duplex, flow_ctrl); 12279 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 12280 netdev_info(bp->dev, "EEE is %s\n", 12281 bp->eee.eee_active ? "active" : 12282 "not active"); 12283 fec = bp->link_info.fec_cfg; 12284 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 12285 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 12286 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 12287 bnxt_report_fec(&bp->link_info)); 12288 } else { 12289 char *str = bnxt_link_down_reason(&bp->link_info); 12290 12291 netif_carrier_off(bp->dev); 12292 netdev_err(bp->dev, "NIC Link is Down %s\n", str); 12293 } 12294 } 12295 12296 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 12297 { 12298 if (!resp->supported_speeds_auto_mode && 12299 !resp->supported_speeds_force_mode && 12300 !resp->supported_pam4_speeds_auto_mode && 12301 !resp->supported_pam4_speeds_force_mode && 12302 !resp->supported_speeds2_auto_mode && 12303 !resp->supported_speeds2_force_mode) 12304 return true; 12305 return false; 12306 } 12307 12308 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 12309 { 12310 struct bnxt_link_info *link_info = &bp->link_info; 12311 struct hwrm_port_phy_qcaps_output *resp; 12312 struct hwrm_port_phy_qcaps_input *req; 12313 int rc = 0; 12314 12315 if (bp->hwrm_spec_code < 0x10201) 12316 return 0; 12317 12318 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 12319 if (rc) 12320 return rc; 12321 12322 resp = hwrm_req_hold(bp, req); 12323 rc = hwrm_req_send(bp, req); 12324 if (rc) 12325 goto hwrm_phy_qcaps_exit; 12326 12327 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 12328 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 12329 struct ethtool_keee *eee = &bp->eee; 12330 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 12331 12332 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 12333 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 12334 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 12335 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 12336 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 12337 } 12338 12339 if (bp->hwrm_spec_code >= 0x10a01) { 12340 if (bnxt_phy_qcaps_no_speed(resp)) { 12341 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 12342 netdev_warn(bp->dev, "Ethernet link disabled\n"); 12343 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 12344 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 12345 netdev_info(bp->dev, "Ethernet link enabled\n"); 12346 /* Phy re-enabled, reprobe the speeds */ 12347 link_info->support_auto_speeds = 0; 12348 link_info->support_pam4_auto_speeds = 0; 12349 link_info->support_auto_speeds2 = 0; 12350 } 12351 } 12352 if (resp->supported_speeds_auto_mode) 12353 link_info->support_auto_speeds = 12354 le16_to_cpu(resp->supported_speeds_auto_mode); 12355 if (resp->supported_pam4_speeds_auto_mode) 12356 link_info->support_pam4_auto_speeds = 12357 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 12358 if (resp->supported_speeds2_auto_mode) 12359 link_info->support_auto_speeds2 = 12360 le16_to_cpu(resp->supported_speeds2_auto_mode); 12361 12362 bp->port_count = resp->port_cnt; 12363 12364 hwrm_phy_qcaps_exit: 12365 hwrm_req_drop(bp, req); 12366 return rc; 12367 } 12368 12369 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 12370 { 12371 struct hwrm_port_mac_qcaps_output *resp; 12372 struct hwrm_port_mac_qcaps_input *req; 12373 int rc; 12374 12375 if (bp->hwrm_spec_code < 0x10a03) 12376 return; 12377 12378 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 12379 if (rc) 12380 return; 12381 12382 resp = hwrm_req_hold(bp, req); 12383 rc = hwrm_req_send_silent(bp, req); 12384 if (!rc) 12385 bp->mac_flags = resp->flags; 12386 hwrm_req_drop(bp, req); 12387 } 12388 12389 static bool bnxt_support_dropped(u16 advertising, u16 supported) 12390 { 12391 u16 diff = advertising ^ supported; 12392 12393 return ((supported | diff) != supported); 12394 } 12395 12396 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 12397 { 12398 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 12399 12400 /* Check if any advertised speeds are no longer supported. The caller 12401 * holds the link_lock mutex, so we can modify link_info settings. 12402 */ 12403 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12404 if (bnxt_support_dropped(link_info->advertising, 12405 link_info->support_auto_speeds2)) { 12406 link_info->advertising = link_info->support_auto_speeds2; 12407 return true; 12408 } 12409 return false; 12410 } 12411 if (bnxt_support_dropped(link_info->advertising, 12412 link_info->support_auto_speeds)) { 12413 link_info->advertising = link_info->support_auto_speeds; 12414 return true; 12415 } 12416 if (bnxt_support_dropped(link_info->advertising_pam4, 12417 link_info->support_pam4_auto_speeds)) { 12418 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12419 return true; 12420 } 12421 return false; 12422 } 12423 12424 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12425 { 12426 struct bnxt_link_info *link_info = &bp->link_info; 12427 struct hwrm_port_phy_qcfg_output *resp; 12428 struct hwrm_port_phy_qcfg_input *req; 12429 u8 link_state = link_info->link_state; 12430 bool support_changed; 12431 int rc; 12432 12433 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12434 if (rc) 12435 return rc; 12436 12437 resp = hwrm_req_hold(bp, req); 12438 rc = hwrm_req_send(bp, req); 12439 if (rc) { 12440 hwrm_req_drop(bp, req); 12441 if (BNXT_VF(bp) && rc == -ENODEV) { 12442 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12443 rc = 0; 12444 } 12445 return rc; 12446 } 12447 12448 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12449 link_info->phy_link_status = resp->link; 12450 link_info->duplex = resp->duplex_cfg; 12451 if (bp->hwrm_spec_code >= 0x10800) 12452 link_info->duplex = resp->duplex_state; 12453 link_info->pause = resp->pause; 12454 link_info->auto_mode = resp->auto_mode; 12455 link_info->auto_pause_setting = resp->auto_pause; 12456 link_info->lp_pause = resp->link_partner_adv_pause; 12457 link_info->force_pause_setting = resp->force_pause; 12458 link_info->duplex_setting = resp->duplex_cfg; 12459 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12460 link_info->link_speed = le16_to_cpu(resp->link_speed); 12461 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12462 link_info->active_lanes = resp->active_lanes; 12463 } else { 12464 link_info->link_speed = 0; 12465 link_info->active_lanes = 0; 12466 } 12467 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12468 link_info->force_pam4_link_speed = 12469 le16_to_cpu(resp->force_pam4_link_speed); 12470 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12471 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12472 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12473 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12474 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12475 link_info->auto_pam4_link_speeds = 12476 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12477 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12478 link_info->lp_auto_link_speeds = 12479 le16_to_cpu(resp->link_partner_adv_speeds); 12480 link_info->lp_auto_pam4_link_speeds = 12481 resp->link_partner_pam4_adv_speeds; 12482 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12483 link_info->phy_ver[0] = resp->phy_maj; 12484 link_info->phy_ver[1] = resp->phy_min; 12485 link_info->phy_ver[2] = resp->phy_bld; 12486 link_info->media_type = resp->media_type; 12487 link_info->phy_type = resp->phy_type; 12488 link_info->transceiver = resp->xcvr_pkg_type; 12489 link_info->phy_addr = resp->eee_config_phy_addr & 12490 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12491 link_info->module_status = resp->module_status; 12492 link_info->link_down_reason = resp->link_down_reason; 12493 12494 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12495 struct ethtool_keee *eee = &bp->eee; 12496 u16 fw_speeds; 12497 12498 eee->eee_active = 0; 12499 if (resp->eee_config_phy_addr & 12500 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12501 eee->eee_active = 1; 12502 fw_speeds = le16_to_cpu( 12503 resp->link_partner_adv_eee_link_speed_mask); 12504 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12505 } 12506 12507 /* Pull initial EEE config */ 12508 if (!chng_link_state) { 12509 if (resp->eee_config_phy_addr & 12510 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12511 eee->eee_enabled = 1; 12512 12513 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12514 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12515 12516 if (resp->eee_config_phy_addr & 12517 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12518 __le32 tmr; 12519 12520 eee->tx_lpi_enabled = 1; 12521 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12522 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12523 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12524 } 12525 } 12526 } 12527 12528 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12529 if (bp->hwrm_spec_code >= 0x10504) { 12530 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12531 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12532 } 12533 /* TODO: need to add more logic to report VF link */ 12534 if (chng_link_state) { 12535 if (link_info->phy_link_status == BNXT_LINK_LINK) 12536 link_info->link_state = BNXT_LINK_STATE_UP; 12537 else 12538 link_info->link_state = BNXT_LINK_STATE_DOWN; 12539 if (link_state != link_info->link_state) 12540 bnxt_report_link(bp); 12541 } else { 12542 /* always link down if not require to update link state */ 12543 link_info->link_state = BNXT_LINK_STATE_DOWN; 12544 } 12545 hwrm_req_drop(bp, req); 12546 12547 if (!BNXT_PHY_CFG_ABLE(bp)) 12548 return 0; 12549 12550 support_changed = bnxt_support_speed_dropped(link_info); 12551 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12552 bnxt_hwrm_set_link_setting(bp, true, false); 12553 return 0; 12554 } 12555 12556 static void bnxt_get_port_module_status(struct bnxt *bp) 12557 { 12558 struct bnxt_link_info *link_info = &bp->link_info; 12559 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12560 u8 module_status; 12561 12562 if (bnxt_update_link(bp, true)) 12563 return; 12564 12565 module_status = link_info->module_status; 12566 switch (module_status) { 12567 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12568 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12569 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12570 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12571 bp->pf.port_id); 12572 if (bp->hwrm_spec_code >= 0x10201) { 12573 netdev_warn(bp->dev, "Module part number %s\n", 12574 resp->phy_vendor_partnumber); 12575 } 12576 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12577 netdev_warn(bp->dev, "TX is disabled\n"); 12578 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12579 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12580 } 12581 } 12582 12583 static void 12584 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12585 { 12586 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12587 if (bp->hwrm_spec_code >= 0x10201) 12588 req->auto_pause = 12589 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12590 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12591 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12592 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12593 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12594 req->enables |= 12595 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12596 } else { 12597 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12598 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12599 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12600 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12601 req->enables |= 12602 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12603 if (bp->hwrm_spec_code >= 0x10201) { 12604 req->auto_pause = req->force_pause; 12605 req->enables |= cpu_to_le32( 12606 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12607 } 12608 } 12609 } 12610 12611 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12612 { 12613 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12614 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12615 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12616 req->enables |= 12617 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12618 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12619 } else if (bp->link_info.advertising) { 12620 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12621 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12622 } 12623 if (bp->link_info.advertising_pam4) { 12624 req->enables |= 12625 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12626 req->auto_link_pam4_speed_mask = 12627 cpu_to_le16(bp->link_info.advertising_pam4); 12628 } 12629 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12630 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12631 } else { 12632 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12633 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12634 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12635 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12636 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12637 (u32)bp->link_info.req_link_speed); 12638 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12639 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12640 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12641 } else { 12642 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12643 } 12644 } 12645 12646 /* tell chimp that the setting takes effect immediately */ 12647 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12648 } 12649 12650 int bnxt_hwrm_set_pause(struct bnxt *bp) 12651 { 12652 struct hwrm_port_phy_cfg_input *req; 12653 int rc; 12654 12655 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12656 if (rc) 12657 return rc; 12658 12659 bnxt_hwrm_set_pause_common(bp, req); 12660 12661 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12662 bp->link_info.force_link_chng) 12663 bnxt_hwrm_set_link_common(bp, req); 12664 12665 rc = hwrm_req_send(bp, req); 12666 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12667 /* since changing of pause setting doesn't trigger any link 12668 * change event, the driver needs to update the current pause 12669 * result upon successfully return of the phy_cfg command 12670 */ 12671 bp->link_info.pause = 12672 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12673 bp->link_info.auto_pause_setting = 0; 12674 if (!bp->link_info.force_link_chng) 12675 bnxt_report_link(bp); 12676 } 12677 bp->link_info.force_link_chng = false; 12678 return rc; 12679 } 12680 12681 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12682 struct hwrm_port_phy_cfg_input *req) 12683 { 12684 struct ethtool_keee *eee = &bp->eee; 12685 12686 if (eee->eee_enabled) { 12687 u16 eee_speeds; 12688 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12689 12690 if (eee->tx_lpi_enabled) 12691 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12692 else 12693 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12694 12695 req->flags |= cpu_to_le32(flags); 12696 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12697 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12698 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12699 } else { 12700 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12701 } 12702 } 12703 12704 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12705 { 12706 struct hwrm_port_phy_cfg_input *req; 12707 int rc; 12708 12709 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12710 if (rc) 12711 return rc; 12712 12713 if (set_pause) 12714 bnxt_hwrm_set_pause_common(bp, req); 12715 12716 bnxt_hwrm_set_link_common(bp, req); 12717 12718 if (set_eee) 12719 bnxt_hwrm_set_eee(bp, req); 12720 return hwrm_req_send(bp, req); 12721 } 12722 12723 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12724 { 12725 struct hwrm_port_phy_cfg_input *req; 12726 int rc; 12727 12728 if (!BNXT_SINGLE_PF(bp)) 12729 return 0; 12730 12731 if (pci_num_vf(bp->pdev) && 12732 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12733 return 0; 12734 12735 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12736 if (rc) 12737 return rc; 12738 12739 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12740 rc = hwrm_req_send(bp, req); 12741 if (!rc) { 12742 mutex_lock(&bp->link_lock); 12743 /* Device is not obliged link down in certain scenarios, even 12744 * when forced. Setting the state unknown is consistent with 12745 * driver startup and will force link state to be reported 12746 * during subsequent open based on PORT_PHY_QCFG. 12747 */ 12748 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12749 mutex_unlock(&bp->link_lock); 12750 } 12751 return rc; 12752 } 12753 12754 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12755 { 12756 #ifdef CONFIG_TEE_BNXT_FW 12757 int rc = tee_bnxt_fw_load(); 12758 12759 if (rc) 12760 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12761 12762 return rc; 12763 #else 12764 netdev_err(bp->dev, "OP-TEE not supported\n"); 12765 return -ENODEV; 12766 #endif 12767 } 12768 12769 static int bnxt_try_recover_fw(struct bnxt *bp) 12770 { 12771 if (bp->fw_health && bp->fw_health->status_reliable) { 12772 int retry = 0, rc; 12773 u32 sts; 12774 12775 do { 12776 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12777 rc = bnxt_hwrm_poll(bp); 12778 if (!BNXT_FW_IS_BOOTING(sts) && 12779 !BNXT_FW_IS_RECOVERING(sts)) 12780 break; 12781 retry++; 12782 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12783 12784 if (!BNXT_FW_IS_HEALTHY(sts)) { 12785 netdev_err(bp->dev, 12786 "Firmware not responding, status: 0x%x\n", 12787 sts); 12788 rc = -ENODEV; 12789 } 12790 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12791 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12792 return bnxt_fw_reset_via_optee(bp); 12793 } 12794 return rc; 12795 } 12796 12797 return -ENODEV; 12798 } 12799 12800 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12801 { 12802 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12803 12804 if (!BNXT_NEW_RM(bp)) 12805 return; /* no resource reservations required */ 12806 12807 hw_resc->resv_cp_rings = 0; 12808 hw_resc->resv_stat_ctxs = 0; 12809 hw_resc->resv_irqs = 0; 12810 hw_resc->resv_tx_rings = 0; 12811 hw_resc->resv_rx_rings = 0; 12812 hw_resc->resv_hw_ring_grps = 0; 12813 hw_resc->resv_vnics = 0; 12814 hw_resc->resv_rsscos_ctxs = 0; 12815 if (!fw_reset) { 12816 bp->tx_nr_rings = 0; 12817 bp->rx_nr_rings = 0; 12818 } 12819 } 12820 12821 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12822 { 12823 int rc; 12824 12825 if (!BNXT_NEW_RM(bp)) 12826 return 0; /* no resource reservations required */ 12827 12828 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12829 if (rc) 12830 netdev_err(bp->dev, "resc_qcaps failed\n"); 12831 12832 bnxt_clear_reservations(bp, fw_reset); 12833 12834 return rc; 12835 } 12836 12837 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12838 { 12839 struct hwrm_func_drv_if_change_output *resp; 12840 struct hwrm_func_drv_if_change_input *req; 12841 bool resc_reinit = false; 12842 bool caps_change = false; 12843 int rc, retry = 0; 12844 bool fw_reset; 12845 u32 flags = 0; 12846 12847 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12848 bp->fw_reset_state = 0; 12849 12850 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12851 return 0; 12852 12853 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12854 if (rc) 12855 return rc; 12856 12857 if (up) 12858 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12859 resp = hwrm_req_hold(bp, req); 12860 12861 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12862 while (retry < BNXT_FW_IF_RETRY) { 12863 rc = hwrm_req_send(bp, req); 12864 if (rc != -EAGAIN) 12865 break; 12866 12867 msleep(50); 12868 retry++; 12869 } 12870 12871 if (rc == -EAGAIN) { 12872 hwrm_req_drop(bp, req); 12873 return rc; 12874 } else if (!rc) { 12875 flags = le32_to_cpu(resp->flags); 12876 } else if (up) { 12877 rc = bnxt_try_recover_fw(bp); 12878 fw_reset = true; 12879 } 12880 hwrm_req_drop(bp, req); 12881 if (rc) 12882 return rc; 12883 12884 if (!up) { 12885 bnxt_inv_fw_health_reg(bp); 12886 return 0; 12887 } 12888 12889 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12890 resc_reinit = true; 12891 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12892 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12893 fw_reset = true; 12894 else 12895 bnxt_remap_fw_health_regs(bp); 12896 12897 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12898 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12899 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12900 return -ENODEV; 12901 } 12902 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12903 caps_change = true; 12904 12905 if (resc_reinit || fw_reset || caps_change) { 12906 if (fw_reset || caps_change) { 12907 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12908 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12909 bnxt_ulp_irq_stop(bp); 12910 bnxt_free_ctx_mem(bp, false); 12911 bnxt_dcb_free(bp); 12912 rc = bnxt_fw_init_one(bp); 12913 if (rc) { 12914 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12915 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12916 return rc; 12917 } 12918 /* IRQ will be initialized later in bnxt_request_irq()*/ 12919 bnxt_clear_int_mode(bp); 12920 } 12921 rc = bnxt_cancel_reservations(bp, fw_reset); 12922 } 12923 return rc; 12924 } 12925 12926 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12927 { 12928 struct hwrm_port_led_qcaps_output *resp; 12929 struct hwrm_port_led_qcaps_input *req; 12930 struct bnxt_pf_info *pf = &bp->pf; 12931 int rc; 12932 12933 bp->num_leds = 0; 12934 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12935 return 0; 12936 12937 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12938 if (rc) 12939 return rc; 12940 12941 req->port_id = cpu_to_le16(pf->port_id); 12942 resp = hwrm_req_hold(bp, req); 12943 rc = hwrm_req_send(bp, req); 12944 if (rc) { 12945 hwrm_req_drop(bp, req); 12946 return rc; 12947 } 12948 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12949 int i; 12950 12951 bp->num_leds = resp->num_leds; 12952 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12953 bp->num_leds); 12954 for (i = 0; i < bp->num_leds; i++) { 12955 struct bnxt_led_info *led = &bp->leds[i]; 12956 __le16 caps = led->led_state_caps; 12957 12958 if (!led->led_group_id || 12959 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12960 bp->num_leds = 0; 12961 break; 12962 } 12963 } 12964 } 12965 hwrm_req_drop(bp, req); 12966 return 0; 12967 } 12968 12969 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12970 { 12971 struct hwrm_wol_filter_alloc_output *resp; 12972 struct hwrm_wol_filter_alloc_input *req; 12973 int rc; 12974 12975 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12976 if (rc) 12977 return rc; 12978 12979 req->port_id = cpu_to_le16(bp->pf.port_id); 12980 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12981 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12982 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12983 12984 resp = hwrm_req_hold(bp, req); 12985 rc = hwrm_req_send(bp, req); 12986 if (!rc) 12987 bp->wol_filter_id = resp->wol_filter_id; 12988 hwrm_req_drop(bp, req); 12989 return rc; 12990 } 12991 12992 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12993 { 12994 struct hwrm_wol_filter_free_input *req; 12995 int rc; 12996 12997 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12998 if (rc) 12999 return rc; 13000 13001 req->port_id = cpu_to_le16(bp->pf.port_id); 13002 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 13003 req->wol_filter_id = bp->wol_filter_id; 13004 13005 return hwrm_req_send(bp, req); 13006 } 13007 13008 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 13009 { 13010 struct hwrm_wol_filter_qcfg_output *resp; 13011 struct hwrm_wol_filter_qcfg_input *req; 13012 u16 next_handle = 0; 13013 int rc; 13014 13015 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 13016 if (rc) 13017 return rc; 13018 13019 req->port_id = cpu_to_le16(bp->pf.port_id); 13020 req->handle = cpu_to_le16(handle); 13021 resp = hwrm_req_hold(bp, req); 13022 rc = hwrm_req_send(bp, req); 13023 if (!rc) { 13024 next_handle = le16_to_cpu(resp->next_handle); 13025 if (next_handle != 0) { 13026 if (resp->wol_type == 13027 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 13028 bp->wol = 1; 13029 bp->wol_filter_id = resp->wol_filter_id; 13030 } 13031 } 13032 } 13033 hwrm_req_drop(bp, req); 13034 return next_handle; 13035 } 13036 13037 static void bnxt_get_wol_settings(struct bnxt *bp) 13038 { 13039 u16 handle = 0; 13040 13041 bp->wol = 0; 13042 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 13043 return; 13044 13045 do { 13046 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 13047 } while (handle && handle != 0xffff); 13048 } 13049 13050 static bool bnxt_eee_config_ok(struct bnxt *bp) 13051 { 13052 struct ethtool_keee *eee = &bp->eee; 13053 struct bnxt_link_info *link_info = &bp->link_info; 13054 13055 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 13056 return true; 13057 13058 if (eee->eee_enabled) { 13059 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 13060 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 13061 13062 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 13063 13064 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 13065 eee->eee_enabled = 0; 13066 return false; 13067 } 13068 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 13069 linkmode_and(eee->advertised, advertising, 13070 eee->supported); 13071 return false; 13072 } 13073 } 13074 return true; 13075 } 13076 13077 static int bnxt_update_phy_setting(struct bnxt *bp) 13078 { 13079 int rc; 13080 bool update_link = false; 13081 bool update_pause = false; 13082 bool update_eee = false; 13083 struct bnxt_link_info *link_info = &bp->link_info; 13084 13085 rc = bnxt_update_link(bp, true); 13086 if (rc) { 13087 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 13088 rc); 13089 return rc; 13090 } 13091 if (!BNXT_SINGLE_PF(bp)) 13092 return 0; 13093 13094 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 13095 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 13096 link_info->req_flow_ctrl) 13097 update_pause = true; 13098 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 13099 link_info->force_pause_setting != link_info->req_flow_ctrl) 13100 update_pause = true; 13101 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 13102 if (BNXT_AUTO_MODE(link_info->auto_mode)) 13103 update_link = true; 13104 if (bnxt_force_speed_updated(link_info)) 13105 update_link = true; 13106 if (link_info->req_duplex != link_info->duplex_setting) 13107 update_link = true; 13108 } else { 13109 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 13110 update_link = true; 13111 if (bnxt_auto_speed_updated(link_info)) 13112 update_link = true; 13113 } 13114 13115 /* The last close may have shutdown the link, so need to call 13116 * PHY_CFG to bring it back up. 13117 */ 13118 if (!BNXT_LINK_IS_UP(bp)) 13119 update_link = true; 13120 13121 if (!bnxt_eee_config_ok(bp)) 13122 update_eee = true; 13123 13124 if (update_link) 13125 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 13126 else if (update_pause) 13127 rc = bnxt_hwrm_set_pause(bp); 13128 if (rc) { 13129 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 13130 rc); 13131 return rc; 13132 } 13133 13134 return rc; 13135 } 13136 13137 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 13138 13139 static int bnxt_reinit_after_abort(struct bnxt *bp) 13140 { 13141 int rc; 13142 13143 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13144 return -EBUSY; 13145 13146 if (bp->dev->reg_state == NETREG_UNREGISTERED) 13147 return -ENODEV; 13148 13149 rc = bnxt_fw_init_one(bp); 13150 if (!rc) { 13151 bnxt_clear_int_mode(bp); 13152 rc = bnxt_init_int_mode(bp); 13153 if (!rc) { 13154 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13155 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 13156 } 13157 } 13158 return rc; 13159 } 13160 13161 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 13162 { 13163 struct bnxt_ntuple_filter *ntp_fltr; 13164 struct bnxt_l2_filter *l2_fltr; 13165 13166 if (list_empty(&fltr->list)) 13167 return; 13168 13169 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 13170 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 13171 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 13172 atomic_inc(&l2_fltr->refcnt); 13173 ntp_fltr->l2_fltr = l2_fltr; 13174 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 13175 bnxt_del_ntp_filter(bp, ntp_fltr); 13176 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 13177 fltr->sw_id); 13178 } 13179 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 13180 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 13181 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 13182 bnxt_del_l2_filter(bp, l2_fltr); 13183 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 13184 fltr->sw_id); 13185 } 13186 } 13187 } 13188 13189 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 13190 { 13191 struct bnxt_filter_base *usr_fltr, *tmp; 13192 13193 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 13194 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 13195 } 13196 13197 static int bnxt_set_xps_mapping(struct bnxt *bp) 13198 { 13199 int numa_node = dev_to_node(&bp->pdev->dev); 13200 unsigned int q_idx, map_idx, cpu, i; 13201 const struct cpumask *cpu_mask_ptr; 13202 int nr_cpus = num_online_cpus(); 13203 cpumask_t *q_map; 13204 int rc = 0; 13205 13206 q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc); 13207 if (!q_map) 13208 return -ENOMEM; 13209 13210 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 13211 * Each TC has the same number of TX queues. The nth TX queue for each 13212 * TC will have the same CPU mask. 13213 */ 13214 for (i = 0; i < nr_cpus; i++) { 13215 map_idx = i % bp->tx_nr_rings_per_tc; 13216 cpu = cpumask_local_spread(i, numa_node); 13217 cpu_mask_ptr = get_cpu_mask(cpu); 13218 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 13219 } 13220 13221 /* Register CPU mask for each TX queue except the ones marked for XDP */ 13222 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 13223 map_idx = q_idx % bp->tx_nr_rings_per_tc; 13224 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 13225 if (rc) { 13226 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 13227 q_idx); 13228 break; 13229 } 13230 } 13231 13232 kfree(q_map); 13233 13234 return rc; 13235 } 13236 13237 static int bnxt_tx_nr_rings(struct bnxt *bp) 13238 { 13239 return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc : 13240 bp->tx_nr_rings_per_tc; 13241 } 13242 13243 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp) 13244 { 13245 return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings; 13246 } 13247 13248 static void bnxt_set_xdp_tx_rings(struct bnxt *bp) 13249 { 13250 bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc; 13251 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 13252 } 13253 13254 static void bnxt_adj_tx_rings(struct bnxt *bp) 13255 { 13256 /* Make adjustments if reserved TX rings are less than requested */ 13257 bp->tx_nr_rings -= bp->tx_nr_rings_xdp; 13258 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 13259 if (bp->tx_nr_rings_xdp) 13260 bnxt_set_xdp_tx_rings(bp); 13261 } 13262 13263 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13264 { 13265 int rc = 0; 13266 13267 netif_carrier_off(bp->dev); 13268 if (irq_re_init) { 13269 /* Reserve rings now if none were reserved at driver probe. */ 13270 rc = bnxt_init_dflt_ring_mode(bp); 13271 if (rc) { 13272 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 13273 return rc; 13274 } 13275 } 13276 rc = bnxt_reserve_rings(bp, irq_re_init); 13277 if (rc) 13278 return rc; 13279 13280 bnxt_adj_tx_rings(bp); 13281 rc = bnxt_alloc_mem(bp, irq_re_init); 13282 if (rc) { 13283 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 13284 goto open_err_free_mem; 13285 } 13286 13287 if (irq_re_init) { 13288 bnxt_init_napi(bp); 13289 rc = bnxt_request_irq(bp); 13290 if (rc) { 13291 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 13292 goto open_err_irq; 13293 } 13294 } 13295 13296 rc = bnxt_init_nic(bp, irq_re_init); 13297 if (rc) { 13298 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 13299 goto open_err_irq; 13300 } 13301 13302 bnxt_enable_napi(bp); 13303 bnxt_debug_dev_init(bp); 13304 13305 if (link_re_init) { 13306 mutex_lock(&bp->link_lock); 13307 rc = bnxt_update_phy_setting(bp); 13308 mutex_unlock(&bp->link_lock); 13309 if (rc) { 13310 netdev_warn(bp->dev, "failed to update phy settings\n"); 13311 if (BNXT_SINGLE_PF(bp)) { 13312 bp->link_info.phy_retry = true; 13313 bp->link_info.phy_retry_expires = 13314 jiffies + 5 * HZ; 13315 } 13316 } 13317 } 13318 13319 if (irq_re_init) { 13320 udp_tunnel_nic_reset_ntf(bp->dev); 13321 rc = bnxt_set_xps_mapping(bp); 13322 if (rc) 13323 netdev_warn(bp->dev, "failed to set xps mapping\n"); 13324 } 13325 13326 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 13327 if (!static_key_enabled(&bnxt_xdp_locking_key)) 13328 static_branch_enable(&bnxt_xdp_locking_key); 13329 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 13330 static_branch_disable(&bnxt_xdp_locking_key); 13331 } 13332 set_bit(BNXT_STATE_OPEN, &bp->state); 13333 bnxt_enable_int(bp); 13334 /* Enable TX queues */ 13335 bnxt_tx_enable(bp); 13336 mod_timer(&bp->timer, jiffies + bp->current_interval); 13337 /* Poll link status and check for SFP+ module status */ 13338 mutex_lock(&bp->link_lock); 13339 bnxt_get_port_module_status(bp); 13340 mutex_unlock(&bp->link_lock); 13341 13342 /* VF-reps may need to be re-opened after the PF is re-opened */ 13343 if (BNXT_PF(bp)) 13344 bnxt_vf_reps_open(bp); 13345 bnxt_ptp_init_rtc(bp, true); 13346 bnxt_ptp_cfg_tstamp_filters(bp); 13347 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13348 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 13349 bnxt_cfg_usr_fltrs(bp); 13350 return 0; 13351 13352 open_err_irq: 13353 bnxt_del_napi(bp); 13354 13355 open_err_free_mem: 13356 bnxt_free_skbs(bp); 13357 bnxt_free_irq(bp); 13358 bnxt_free_mem(bp, true); 13359 return rc; 13360 } 13361 13362 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13363 { 13364 int rc = 0; 13365 13366 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 13367 rc = -EIO; 13368 if (!rc) 13369 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 13370 if (rc) { 13371 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 13372 netif_close(bp->dev); 13373 } 13374 return rc; 13375 } 13376 13377 /* netdev instance lock held, open the NIC half way by allocating all 13378 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 13379 * for offline self tests. 13380 */ 13381 int bnxt_half_open_nic(struct bnxt *bp) 13382 { 13383 int rc = 0; 13384 13385 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13386 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 13387 rc = -ENODEV; 13388 goto half_open_err; 13389 } 13390 13391 rc = bnxt_alloc_mem(bp, true); 13392 if (rc) { 13393 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 13394 goto half_open_err; 13395 } 13396 bnxt_init_napi(bp); 13397 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13398 rc = bnxt_init_nic(bp, true); 13399 if (rc) { 13400 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13401 bnxt_del_napi(bp); 13402 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 13403 goto half_open_err; 13404 } 13405 return 0; 13406 13407 half_open_err: 13408 bnxt_free_skbs(bp); 13409 bnxt_free_mem(bp, true); 13410 netif_close(bp->dev); 13411 return rc; 13412 } 13413 13414 /* netdev instance lock held, this call can only be made after a previous 13415 * successful call to bnxt_half_open_nic(). 13416 */ 13417 void bnxt_half_close_nic(struct bnxt *bp) 13418 { 13419 bnxt_hwrm_resource_free(bp, false, true); 13420 bnxt_del_napi(bp); 13421 bnxt_free_skbs(bp); 13422 bnxt_free_mem(bp, true); 13423 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13424 } 13425 13426 void bnxt_reenable_sriov(struct bnxt *bp) 13427 { 13428 if (BNXT_PF(bp)) { 13429 struct bnxt_pf_info *pf = &bp->pf; 13430 int n = pf->active_vfs; 13431 13432 if (n) 13433 bnxt_cfg_hw_sriov(bp, &n, true); 13434 } 13435 } 13436 13437 static int bnxt_open(struct net_device *dev) 13438 { 13439 struct bnxt *bp = netdev_priv(dev); 13440 int rc; 13441 13442 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13443 rc = bnxt_reinit_after_abort(bp); 13444 if (rc) { 13445 if (rc == -EBUSY) 13446 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13447 else 13448 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13449 return -ENODEV; 13450 } 13451 } 13452 13453 rc = bnxt_hwrm_if_change(bp, true); 13454 if (rc) 13455 return rc; 13456 13457 rc = __bnxt_open_nic(bp, true, true); 13458 if (rc) { 13459 bnxt_hwrm_if_change(bp, false); 13460 } else { 13461 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13462 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13463 bnxt_queue_sp_work(bp, 13464 BNXT_RESTART_ULP_SP_EVENT); 13465 } 13466 } 13467 13468 return rc; 13469 } 13470 13471 static bool bnxt_drv_busy(struct bnxt *bp) 13472 { 13473 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13474 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13475 } 13476 13477 static void bnxt_get_ring_stats(struct bnxt *bp, 13478 struct rtnl_link_stats64 *stats); 13479 13480 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13481 bool link_re_init) 13482 { 13483 /* Close the VF-reps before closing PF */ 13484 if (BNXT_PF(bp)) 13485 bnxt_vf_reps_close(bp); 13486 13487 /* Change device state to avoid TX queue wake up's */ 13488 bnxt_tx_disable(bp); 13489 13490 clear_bit(BNXT_STATE_OPEN, &bp->state); 13491 smp_mb__after_atomic(); 13492 while (bnxt_drv_busy(bp)) 13493 msleep(20); 13494 13495 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13496 bnxt_clear_rss_ctxs(bp); 13497 /* Flush rings and disable interrupts */ 13498 bnxt_shutdown_nic(bp, irq_re_init); 13499 13500 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13501 13502 bnxt_debug_dev_exit(bp); 13503 bnxt_disable_napi(bp); 13504 timer_delete_sync(&bp->timer); 13505 bnxt_free_skbs(bp); 13506 13507 /* Save ring stats before shutdown */ 13508 if (bp->bnapi && irq_re_init) { 13509 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13510 bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev); 13511 } 13512 if (irq_re_init) { 13513 bnxt_free_irq(bp); 13514 bnxt_del_napi(bp); 13515 } 13516 bnxt_free_mem(bp, irq_re_init); 13517 } 13518 13519 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13520 { 13521 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13522 /* If we get here, it means firmware reset is in progress 13523 * while we are trying to close. We can safely proceed with 13524 * the close because we are holding netdev instance lock. 13525 * Some firmware messages may fail as we proceed to close. 13526 * We set the ABORT_ERR flag here so that the FW reset thread 13527 * will later abort when it gets the netdev instance lock 13528 * and sees the flag. 13529 */ 13530 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13531 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13532 } 13533 13534 #ifdef CONFIG_BNXT_SRIOV 13535 if (bp->sriov_cfg) { 13536 int rc; 13537 13538 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13539 !bp->sriov_cfg, 13540 BNXT_SRIOV_CFG_WAIT_TMO); 13541 if (!rc) 13542 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13543 else if (rc < 0) 13544 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13545 } 13546 #endif 13547 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13548 } 13549 13550 static int bnxt_close(struct net_device *dev) 13551 { 13552 struct bnxt *bp = netdev_priv(dev); 13553 13554 bnxt_close_nic(bp, true, true); 13555 bnxt_hwrm_shutdown_link(bp); 13556 bnxt_hwrm_if_change(bp, false); 13557 return 0; 13558 } 13559 13560 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13561 u16 *val) 13562 { 13563 struct hwrm_port_phy_mdio_read_output *resp; 13564 struct hwrm_port_phy_mdio_read_input *req; 13565 int rc; 13566 13567 if (bp->hwrm_spec_code < 0x10a00) 13568 return -EOPNOTSUPP; 13569 13570 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13571 if (rc) 13572 return rc; 13573 13574 req->port_id = cpu_to_le16(bp->pf.port_id); 13575 req->phy_addr = phy_addr; 13576 req->reg_addr = cpu_to_le16(reg & 0x1f); 13577 if (mdio_phy_id_is_c45(phy_addr)) { 13578 req->cl45_mdio = 1; 13579 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13580 req->dev_addr = mdio_phy_id_devad(phy_addr); 13581 req->reg_addr = cpu_to_le16(reg); 13582 } 13583 13584 resp = hwrm_req_hold(bp, req); 13585 rc = hwrm_req_send(bp, req); 13586 if (!rc) 13587 *val = le16_to_cpu(resp->reg_data); 13588 hwrm_req_drop(bp, req); 13589 return rc; 13590 } 13591 13592 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13593 u16 val) 13594 { 13595 struct hwrm_port_phy_mdio_write_input *req; 13596 int rc; 13597 13598 if (bp->hwrm_spec_code < 0x10a00) 13599 return -EOPNOTSUPP; 13600 13601 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13602 if (rc) 13603 return rc; 13604 13605 req->port_id = cpu_to_le16(bp->pf.port_id); 13606 req->phy_addr = phy_addr; 13607 req->reg_addr = cpu_to_le16(reg & 0x1f); 13608 if (mdio_phy_id_is_c45(phy_addr)) { 13609 req->cl45_mdio = 1; 13610 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13611 req->dev_addr = mdio_phy_id_devad(phy_addr); 13612 req->reg_addr = cpu_to_le16(reg); 13613 } 13614 req->reg_data = cpu_to_le16(val); 13615 13616 return hwrm_req_send(bp, req); 13617 } 13618 13619 /* netdev instance lock held */ 13620 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13621 { 13622 struct mii_ioctl_data *mdio = if_mii(ifr); 13623 struct bnxt *bp = netdev_priv(dev); 13624 int rc; 13625 13626 switch (cmd) { 13627 case SIOCGMIIPHY: 13628 mdio->phy_id = bp->link_info.phy_addr; 13629 13630 fallthrough; 13631 case SIOCGMIIREG: { 13632 u16 mii_regval = 0; 13633 13634 if (!netif_running(dev)) 13635 return -EAGAIN; 13636 13637 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13638 &mii_regval); 13639 mdio->val_out = mii_regval; 13640 return rc; 13641 } 13642 13643 case SIOCSMIIREG: 13644 if (!netif_running(dev)) 13645 return -EAGAIN; 13646 13647 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13648 mdio->val_in); 13649 13650 default: 13651 /* do nothing */ 13652 break; 13653 } 13654 return -EOPNOTSUPP; 13655 } 13656 13657 static void bnxt_get_ring_stats(struct bnxt *bp, 13658 struct rtnl_link_stats64 *stats) 13659 { 13660 int i; 13661 13662 for (i = 0; i < bp->cp_nr_rings; i++) { 13663 struct bnxt_napi *bnapi = bp->bnapi[i]; 13664 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13665 u64 *sw = cpr->stats.sw_stats; 13666 13667 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13668 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13669 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13670 13671 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13672 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13673 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13674 13675 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13676 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13677 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13678 13679 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13680 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13681 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13682 13683 stats->rx_missed_errors += 13684 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13685 13686 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13687 13688 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13689 13690 stats->rx_dropped += 13691 cpr->sw_stats->rx.rx_netpoll_discards + 13692 cpr->sw_stats->rx.rx_oom_discards; 13693 } 13694 } 13695 13696 static void bnxt_add_prev_stats(struct bnxt *bp, 13697 struct rtnl_link_stats64 *stats) 13698 { 13699 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13700 13701 stats->rx_packets += prev_stats->rx_packets; 13702 stats->tx_packets += prev_stats->tx_packets; 13703 stats->rx_bytes += prev_stats->rx_bytes; 13704 stats->tx_bytes += prev_stats->tx_bytes; 13705 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13706 stats->multicast += prev_stats->multicast; 13707 stats->rx_dropped += prev_stats->rx_dropped; 13708 stats->tx_dropped += prev_stats->tx_dropped; 13709 } 13710 13711 static void 13712 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13713 { 13714 struct bnxt *bp = netdev_priv(dev); 13715 13716 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13717 /* Make sure bnxt_close_nic() sees that we are reading stats before 13718 * we check the BNXT_STATE_OPEN flag. 13719 */ 13720 smp_mb__after_atomic(); 13721 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13722 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13723 *stats = bp->net_stats_prev; 13724 return; 13725 } 13726 13727 bnxt_sync_ring_stats(bp); 13728 bnxt_get_ring_stats(bp, stats); 13729 bnxt_add_prev_stats(bp, stats); 13730 13731 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13732 u64 *rx = bp->port_stats.sw_stats; 13733 u64 *tx = bp->port_stats.sw_stats + 13734 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13735 13736 stats->rx_crc_errors = 13737 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13738 stats->rx_frame_errors = 13739 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13740 stats->rx_length_errors = 13741 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13742 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13743 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13744 stats->rx_errors = 13745 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13746 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13747 stats->collisions = 13748 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13749 stats->tx_fifo_errors = 13750 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13751 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13752 } 13753 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13754 } 13755 13756 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp, 13757 struct bnxt_total_ring_drv_stats *stats, 13758 struct bnxt_cp_ring_info *cpr) 13759 { 13760 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13761 u64 *hw_stats = cpr->stats.sw_stats; 13762 13763 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13764 stats->rx_total_resets += sw_stats->rx.rx_resets; 13765 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13766 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13767 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13768 stats->rx_total_ring_discards += 13769 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13770 stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets; 13771 stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets; 13772 stats->tx_total_resets += sw_stats->tx.tx_resets; 13773 stats->tx_total_ring_discards += 13774 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13775 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13776 } 13777 13778 void bnxt_get_ring_drv_stats(struct bnxt *bp, 13779 struct bnxt_total_ring_drv_stats *stats) 13780 { 13781 int i; 13782 13783 for (i = 0; i < bp->cp_nr_rings; i++) 13784 bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13785 } 13786 13787 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask, 13788 const struct netdev_hw_addr_list *mc) 13789 { 13790 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13791 struct netdev_hw_addr *ha; 13792 u8 *haddr; 13793 int mc_count = 0; 13794 bool update = false; 13795 int off = 0; 13796 13797 netdev_hw_addr_list_for_each(ha, mc) { 13798 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13799 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13800 vnic->mc_list_count = 0; 13801 return false; 13802 } 13803 haddr = ha->addr; 13804 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13805 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13806 update = true; 13807 } 13808 off += ETH_ALEN; 13809 mc_count++; 13810 } 13811 if (mc_count) 13812 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13813 13814 if (mc_count != vnic->mc_list_count) { 13815 vnic->mc_list_count = mc_count; 13816 update = true; 13817 } 13818 return update; 13819 } 13820 13821 static bool bnxt_uc_list_updated(struct bnxt *bp, 13822 const struct netdev_hw_addr_list *uc) 13823 { 13824 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13825 struct netdev_hw_addr *ha; 13826 int off = 0; 13827 13828 if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1)) 13829 return true; 13830 13831 netdev_hw_addr_list_for_each(ha, uc) { 13832 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13833 return true; 13834 13835 off += ETH_ALEN; 13836 } 13837 return false; 13838 } 13839 13840 static int bnxt_set_rx_mode(struct net_device *dev, 13841 struct netdev_hw_addr_list *uc, 13842 struct netdev_hw_addr_list *mc) 13843 { 13844 struct bnxt *bp = netdev_priv(dev); 13845 struct bnxt_vnic_info *vnic; 13846 bool mc_update = false; 13847 bool uc_update; 13848 u32 mask; 13849 13850 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13851 return 0; 13852 13853 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13854 mask = vnic->rx_mask; 13855 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13856 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13857 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13858 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13859 13860 if (dev->flags & IFF_PROMISC) 13861 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13862 13863 uc_update = bnxt_uc_list_updated(bp, uc); 13864 13865 if (dev->flags & IFF_BROADCAST) 13866 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13867 if (dev->flags & IFF_ALLMULTI) { 13868 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13869 vnic->mc_list_count = 0; 13870 } else if (dev->flags & IFF_MULTICAST) { 13871 mc_update = bnxt_mc_list_updated(bp, &mask, mc); 13872 } 13873 13874 if (mask != vnic->rx_mask || uc_update || mc_update) { 13875 vnic->rx_mask = mask; 13876 13877 return bnxt_cfg_rx_mode(bp, uc, uc_update); 13878 } 13879 13880 return 0; 13881 } 13882 13883 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc, 13884 bool uc_update) 13885 { 13886 struct net_device *dev = bp->dev; 13887 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13888 struct netdev_hw_addr *ha; 13889 int i, off = 0, rc; 13890 13891 if (!uc_update) 13892 goto skip_uc; 13893 13894 for (i = 1; i < vnic->uc_filter_count; i++) { 13895 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13896 13897 bnxt_hwrm_l2_filter_free(bp, fltr); 13898 bnxt_del_l2_filter(bp, fltr); 13899 } 13900 13901 vnic->uc_filter_count = 1; 13902 13903 netif_addr_lock_bh(dev); 13904 if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) { 13905 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13906 } else { 13907 netdev_hw_addr_list_for_each(ha, uc) { 13908 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13909 off += ETH_ALEN; 13910 vnic->uc_filter_count++; 13911 } 13912 } 13913 netif_addr_unlock_bh(dev); 13914 13915 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13916 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13917 if (rc) { 13918 if (BNXT_VF(bp) && rc == -ENODEV) { 13919 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13920 rc = -EAGAIN; 13921 } else if (rc == -EAGAIN) { 13922 netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n"); 13923 } else { 13924 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13925 } 13926 vnic->uc_filter_count = i; 13927 return rc; 13928 } 13929 } 13930 13931 skip_uc: 13932 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13933 !bnxt_promisc_ok(bp)) 13934 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13935 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13936 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13937 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13938 rc); 13939 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13940 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13941 vnic->mc_list_count = 0; 13942 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13943 } 13944 if (rc) 13945 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13946 rc); 13947 13948 return rc; 13949 } 13950 13951 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13952 { 13953 #ifdef CONFIG_BNXT_SRIOV 13954 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13955 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13956 13957 /* No minimum rings were provisioned by the PF. Don't 13958 * reserve rings by default when device is down. 13959 */ 13960 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13961 return true; 13962 13963 if (!netif_running(bp->dev)) 13964 return false; 13965 } 13966 #endif 13967 return true; 13968 } 13969 13970 /* If the chip and firmware supports RFS */ 13971 static bool bnxt_rfs_supported(struct bnxt *bp) 13972 { 13973 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13974 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13975 return true; 13976 return false; 13977 } 13978 /* 212 firmware is broken for aRFS */ 13979 if (BNXT_FW_MAJ(bp) == 212) 13980 return false; 13981 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13982 return true; 13983 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13984 return true; 13985 return false; 13986 } 13987 13988 /* If runtime conditions support RFS */ 13989 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13990 { 13991 struct bnxt_hw_rings hwr = {0}; 13992 int max_vnics, max_rss_ctxs; 13993 13994 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13995 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13996 return bnxt_rfs_supported(bp); 13997 13998 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13999 return false; 14000 14001 hwr.grp = bp->rx_nr_rings; 14002 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 14003 if (new_rss_ctx) 14004 hwr.vnic++; 14005 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14006 max_vnics = bnxt_get_max_func_vnics(bp); 14007 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 14008 14009 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 14010 if (bp->rx_nr_rings > 1) 14011 netdev_warn(bp->dev, 14012 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 14013 min(max_rss_ctxs - 1, max_vnics - 1)); 14014 return false; 14015 } 14016 14017 if (!BNXT_NEW_RM(bp)) 14018 return true; 14019 14020 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 14021 * issue that will mess up the default VNIC if we reduce the 14022 * reservations. 14023 */ 14024 if (hwr.vnic <= bp->hw_resc.resv_vnics && 14025 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 14026 return true; 14027 14028 bnxt_hwrm_reserve_rings(bp, &hwr); 14029 if (hwr.vnic <= bp->hw_resc.resv_vnics && 14030 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 14031 return true; 14032 14033 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 14034 hwr.vnic = 1; 14035 hwr.rss_ctx = 0; 14036 bnxt_hwrm_reserve_rings(bp, &hwr); 14037 return false; 14038 } 14039 14040 static netdev_features_t bnxt_fix_features(struct net_device *dev, 14041 netdev_features_t features) 14042 { 14043 struct bnxt *bp = netdev_priv(dev); 14044 netdev_features_t vlan_features; 14045 14046 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 14047 features &= ~NETIF_F_NTUPLE; 14048 14049 if ((features & NETIF_F_GSO_UDP_L4) && 14050 !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) && 14051 bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS) 14052 features &= ~NETIF_F_GSO_UDP_L4; 14053 14054 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 14055 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 14056 14057 if (!(features & NETIF_F_GRO)) 14058 features &= ~NETIF_F_GRO_HW; 14059 14060 if (features & NETIF_F_GRO_HW) 14061 features &= ~NETIF_F_LRO; 14062 14063 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 14064 * turned on or off together. 14065 */ 14066 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 14067 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 14068 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 14069 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 14070 else if (vlan_features) 14071 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 14072 } 14073 #ifdef CONFIG_BNXT_SRIOV 14074 if (BNXT_VF(bp) && bp->vf.vlan) 14075 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 14076 #endif 14077 return features; 14078 } 14079 14080 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 14081 bool link_re_init, u32 flags, bool update_tpa) 14082 { 14083 bnxt_close_nic(bp, irq_re_init, link_re_init); 14084 bp->flags = flags; 14085 if (update_tpa) 14086 bnxt_set_ring_params(bp); 14087 return bnxt_open_nic(bp, irq_re_init, link_re_init); 14088 } 14089 14090 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 14091 { 14092 bool update_tpa = false, update_ntuple = false; 14093 struct bnxt *bp = netdev_priv(dev); 14094 u32 flags = bp->flags; 14095 u32 changes; 14096 int rc = 0; 14097 bool re_init = false; 14098 14099 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 14100 bnxt_min_tx_desc_cnt(bp, features)); 14101 14102 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 14103 if (features & NETIF_F_GRO_HW) 14104 flags |= BNXT_FLAG_GRO; 14105 else if (features & NETIF_F_LRO) 14106 flags |= BNXT_FLAG_LRO; 14107 14108 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 14109 flags &= ~BNXT_FLAG_TPA; 14110 14111 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 14112 flags |= BNXT_FLAG_STRIP_VLAN; 14113 14114 if (features & NETIF_F_NTUPLE) 14115 flags |= BNXT_FLAG_RFS; 14116 else 14117 bnxt_clear_usr_fltrs(bp, true); 14118 14119 changes = flags ^ bp->flags; 14120 if (changes & BNXT_FLAG_TPA) { 14121 update_tpa = true; 14122 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 14123 (flags & BNXT_FLAG_TPA) == 0 || 14124 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14125 re_init = true; 14126 } 14127 14128 if (changes & ~BNXT_FLAG_TPA) 14129 re_init = true; 14130 14131 if (changes & BNXT_FLAG_RFS) 14132 update_ntuple = true; 14133 14134 if (flags != bp->flags) { 14135 u32 old_flags = bp->flags; 14136 14137 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14138 bp->flags = flags; 14139 if (update_tpa) 14140 bnxt_set_ring_params(bp); 14141 return rc; 14142 } 14143 14144 if (update_ntuple) 14145 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 14146 14147 if (re_init) 14148 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 14149 14150 if (update_tpa) { 14151 bp->flags = flags; 14152 rc = bnxt_set_tpa(bp, 14153 (flags & BNXT_FLAG_TPA) ? 14154 true : false); 14155 if (rc) 14156 bp->flags = old_flags; 14157 } 14158 } 14159 return rc; 14160 } 14161 14162 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 14163 u8 **nextp) 14164 { 14165 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 14166 int hdr_count = 0; 14167 u8 *nexthdr; 14168 int start; 14169 14170 /* Check that there are at most 2 IPv6 extension headers, no 14171 * fragment header, and each is <= 64 bytes. 14172 */ 14173 start = nw_off + sizeof(*ip6h); 14174 nexthdr = &ip6h->nexthdr; 14175 while (ipv6_ext_hdr(*nexthdr)) { 14176 struct ipv6_opt_hdr *hp; 14177 int hdrlen; 14178 14179 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 14180 *nexthdr == NEXTHDR_FRAGMENT) 14181 return false; 14182 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 14183 skb_headlen(skb), NULL); 14184 if (!hp) 14185 return false; 14186 if (*nexthdr == NEXTHDR_AUTH) 14187 hdrlen = ipv6_authlen(hp); 14188 else 14189 hdrlen = ipv6_optlen(hp); 14190 14191 if (hdrlen > 64) 14192 return false; 14193 14194 hdr_count++; 14195 nexthdr = &hp->nexthdr; 14196 start += hdrlen; 14197 } 14198 if (nextp) { 14199 /* Caller will check inner protocol */ 14200 if (skb->encapsulation) { 14201 *nextp = nexthdr; 14202 return true; 14203 } 14204 *nextp = NULL; 14205 } 14206 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 14207 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 14208 } 14209 14210 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 14211 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 14212 { 14213 struct udphdr *uh = udp_hdr(skb); 14214 __be16 udp_port = uh->dest; 14215 14216 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 14217 udp_port != bp->vxlan_gpe_port) 14218 return false; 14219 if (skb->inner_protocol == htons(ETH_P_TEB)) { 14220 struct ethhdr *eh = inner_eth_hdr(skb); 14221 14222 switch (eh->h_proto) { 14223 case htons(ETH_P_IP): 14224 return true; 14225 case htons(ETH_P_IPV6): 14226 return bnxt_exthdr_check(bp, skb, 14227 skb_inner_network_offset(skb), 14228 NULL); 14229 } 14230 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 14231 return true; 14232 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 14233 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 14234 NULL); 14235 } 14236 return false; 14237 } 14238 14239 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 14240 { 14241 switch (l4_proto) { 14242 case IPPROTO_UDP: 14243 return bnxt_udp_tunl_check(bp, skb); 14244 case IPPROTO_IPIP: 14245 return true; 14246 case IPPROTO_GRE: { 14247 switch (skb->inner_protocol) { 14248 default: 14249 return false; 14250 case htons(ETH_P_IP): 14251 return true; 14252 case htons(ETH_P_IPV6): 14253 fallthrough; 14254 } 14255 } 14256 case IPPROTO_IPV6: 14257 /* Check ext headers of inner ipv6 */ 14258 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 14259 NULL); 14260 } 14261 return false; 14262 } 14263 14264 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 14265 struct net_device *dev, 14266 netdev_features_t features) 14267 { 14268 struct bnxt *bp = netdev_priv(dev); 14269 u8 *l4_proto; 14270 14271 features = vlan_features_check(skb, features); 14272 switch (vlan_get_protocol(skb)) { 14273 case htons(ETH_P_IP): 14274 if (!skb->encapsulation) 14275 return features; 14276 l4_proto = &ip_hdr(skb)->protocol; 14277 if (bnxt_tunl_check(bp, skb, *l4_proto)) 14278 return features; 14279 break; 14280 case htons(ETH_P_IPV6): 14281 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 14282 &l4_proto)) 14283 break; 14284 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 14285 return features; 14286 break; 14287 } 14288 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 14289 } 14290 14291 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 14292 u32 *reg_buf) 14293 { 14294 struct hwrm_dbg_read_direct_output *resp; 14295 struct hwrm_dbg_read_direct_input *req; 14296 __le32 *dbg_reg_buf; 14297 dma_addr_t mapping; 14298 int rc, i; 14299 14300 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 14301 if (rc) 14302 return rc; 14303 14304 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 14305 &mapping); 14306 if (!dbg_reg_buf) { 14307 rc = -ENOMEM; 14308 goto dbg_rd_reg_exit; 14309 } 14310 14311 req->host_dest_addr = cpu_to_le64(mapping); 14312 14313 resp = hwrm_req_hold(bp, req); 14314 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 14315 req->read_len32 = cpu_to_le32(num_words); 14316 14317 rc = hwrm_req_send(bp, req); 14318 if (rc || resp->error_code) { 14319 rc = -EIO; 14320 goto dbg_rd_reg_exit; 14321 } 14322 for (i = 0; i < num_words; i++) 14323 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 14324 14325 dbg_rd_reg_exit: 14326 hwrm_req_drop(bp, req); 14327 return rc; 14328 } 14329 14330 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 14331 u32 ring_id, u32 *prod, u32 *cons) 14332 { 14333 struct hwrm_dbg_ring_info_get_output *resp; 14334 struct hwrm_dbg_ring_info_get_input *req; 14335 int rc; 14336 14337 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 14338 if (rc) 14339 return rc; 14340 14341 req->ring_type = ring_type; 14342 req->fw_ring_id = cpu_to_le32(ring_id); 14343 resp = hwrm_req_hold(bp, req); 14344 rc = hwrm_req_send(bp, req); 14345 if (!rc) { 14346 *prod = le32_to_cpu(resp->producer_index); 14347 *cons = le32_to_cpu(resp->consumer_index); 14348 } 14349 hwrm_req_drop(bp, req); 14350 return rc; 14351 } 14352 14353 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 14354 { 14355 struct bnxt_tx_ring_info *txr; 14356 int i = bnapi->index, j; 14357 14358 bnxt_for_each_napi_tx(j, bnapi, txr) 14359 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 14360 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 14361 txr->tx_cons); 14362 } 14363 14364 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 14365 { 14366 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 14367 int i = bnapi->index; 14368 14369 if (!rxr) 14370 return; 14371 14372 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", 14373 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 14374 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 14375 rxr->rx_sw_agg_prod); 14376 } 14377 14378 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 14379 { 14380 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2; 14381 int i = bnapi->index, j; 14382 14383 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 14384 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 14385 for (j = 0; j < cpr->cp_ring_count; j++) { 14386 cpr2 = &cpr->cp_ring_arr[j]; 14387 if (!cpr2->bnapi) 14388 continue; 14389 netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n", 14390 i, j, cpr2->cp_ring_struct.fw_ring_id, 14391 cpr2->cp_raw_cons); 14392 } 14393 } 14394 14395 static void bnxt_dbg_dump_states(struct bnxt *bp) 14396 { 14397 int i; 14398 struct bnxt_napi *bnapi; 14399 14400 for (i = 0; i < bp->cp_nr_rings; i++) { 14401 bnapi = bp->bnapi[i]; 14402 if (netif_msg_drv(bp)) { 14403 bnxt_dump_tx_sw_state(bnapi); 14404 bnxt_dump_rx_sw_state(bnapi); 14405 bnxt_dump_cp_sw_state(bnapi); 14406 } 14407 } 14408 } 14409 14410 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 14411 { 14412 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 14413 struct hwrm_ring_reset_input *req; 14414 struct bnxt_napi *bnapi = rxr->bnapi; 14415 struct bnxt_cp_ring_info *cpr; 14416 u16 cp_ring_id; 14417 int rc; 14418 14419 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 14420 if (rc) 14421 return rc; 14422 14423 cpr = &bnapi->cp_ring; 14424 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14425 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14426 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14427 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14428 return hwrm_req_send_silent(bp, req); 14429 } 14430 14431 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14432 { 14433 if (!silent) 14434 bnxt_dbg_dump_states(bp); 14435 if (netif_running(bp->dev)) { 14436 bnxt_close_nic(bp, !silent, false); 14437 bnxt_open_nic(bp, !silent, false); 14438 } 14439 } 14440 14441 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14442 { 14443 struct bnxt *bp = netdev_priv(dev); 14444 14445 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14446 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14447 } 14448 14449 static void bnxt_fw_health_check(struct bnxt *bp) 14450 { 14451 struct bnxt_fw_health *fw_health = bp->fw_health; 14452 struct pci_dev *pdev = bp->pdev; 14453 u32 val; 14454 14455 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14456 return; 14457 14458 /* Make sure it is enabled before checking the tmr_counter. */ 14459 smp_rmb(); 14460 if (fw_health->tmr_counter) { 14461 fw_health->tmr_counter--; 14462 return; 14463 } 14464 14465 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14466 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14467 fw_health->arrests++; 14468 goto fw_reset; 14469 } 14470 14471 fw_health->last_fw_heartbeat = val; 14472 14473 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14474 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14475 fw_health->discoveries++; 14476 goto fw_reset; 14477 } 14478 14479 fw_health->tmr_counter = fw_health->tmr_multiplier; 14480 return; 14481 14482 fw_reset: 14483 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14484 } 14485 14486 static void bnxt_timer(struct timer_list *t) 14487 { 14488 struct bnxt *bp = timer_container_of(bp, t, timer); 14489 struct net_device *dev = bp->dev; 14490 14491 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14492 return; 14493 14494 if (atomic_read(&bp->intr_sem) != 0) 14495 goto bnxt_restart_timer; 14496 14497 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14498 bnxt_fw_health_check(bp); 14499 14500 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14501 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14502 14503 if (bnxt_tc_flower_enabled(bp)) 14504 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14505 14506 #ifdef CONFIG_RFS_ACCEL 14507 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14508 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14509 #endif /*CONFIG_RFS_ACCEL*/ 14510 14511 if (bp->link_info.phy_retry) { 14512 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14513 bp->link_info.phy_retry = false; 14514 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14515 } else { 14516 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14517 } 14518 } 14519 14520 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14521 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14522 14523 bnxt_restart_timer: 14524 mod_timer(&bp->timer, jiffies + bp->current_interval); 14525 } 14526 14527 static void bnxt_lock_sp(struct bnxt *bp) 14528 { 14529 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14530 * set. If the device is being closed, bnxt_close() may be holding 14531 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14532 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14533 * instance lock. 14534 */ 14535 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14536 netdev_lock(bp->dev); 14537 } 14538 14539 static void bnxt_unlock_sp(struct bnxt *bp) 14540 { 14541 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14542 netdev_unlock(bp->dev); 14543 } 14544 14545 /* Same as bnxt_lock_sp() with additional rtnl_lock */ 14546 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 14547 { 14548 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14549 rtnl_lock(); 14550 netdev_lock(bp->dev); 14551 } 14552 14553 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 14554 { 14555 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14556 netdev_unlock(bp->dev); 14557 rtnl_unlock(); 14558 } 14559 14560 static void bnxt_tph_update(struct bnxt *bp) 14561 { 14562 struct net_device *dev = bp->dev; 14563 int i; 14564 14565 bnxt_lock_sp(bp); 14566 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 14567 goto unlock; 14568 14569 for (i = 0; i < bp->rx_nr_rings; i++) { 14570 struct bnxt_irq *irq; 14571 int map_idx, err; 14572 u16 tag; 14573 14574 map_idx = bnxt_cp_num_to_irq_num(bp, i); 14575 irq = &bp->irq_tbl[map_idx]; 14576 tag = READ_ONCE(irq->new_tag); 14577 if (irq->tag == tag) 14578 continue; 14579 14580 if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag)) 14581 continue; 14582 14583 err = netdev_rx_queue_restart(dev, irq->ring_nr); 14584 if (err) { 14585 netdev_err(dev, "RX queue restart failed: err=%d\n", 14586 err); 14587 continue; 14588 } 14589 14590 irq->tag = tag; 14591 } 14592 14593 unlock: 14594 bnxt_unlock_sp(bp); 14595 } 14596 14597 /* Only called from bnxt_sp_task() */ 14598 static void bnxt_reset(struct bnxt *bp, bool silent) 14599 { 14600 bnxt_rtnl_lock_sp(bp); 14601 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14602 bnxt_reset_task(bp, silent); 14603 bnxt_rtnl_unlock_sp(bp); 14604 } 14605 14606 /* Only called from bnxt_sp_task() */ 14607 static void bnxt_rx_ring_reset(struct bnxt *bp) 14608 { 14609 int i; 14610 14611 bnxt_rtnl_lock_sp(bp); 14612 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14613 bnxt_rtnl_unlock_sp(bp); 14614 return; 14615 } 14616 /* Disable and flush TPA before resetting the RX ring */ 14617 if (bp->flags & BNXT_FLAG_TPA) 14618 bnxt_set_tpa(bp, false); 14619 for (i = 0; i < bp->rx_nr_rings; i++) { 14620 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14621 struct bnxt_cp_ring_info *cpr; 14622 int rc; 14623 14624 if (!rxr->bnapi->in_reset) 14625 continue; 14626 14627 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14628 if (rc) { 14629 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14630 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14631 else 14632 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14633 rc); 14634 bnxt_reset_task(bp, true); 14635 break; 14636 } 14637 bnxt_free_one_rx_ring_skbs(bp, rxr); 14638 rxr->rx_prod = 0; 14639 rxr->rx_agg_prod = 0; 14640 rxr->rx_sw_agg_prod = 0; 14641 rxr->rx_next_cons = 0; 14642 rxr->bnapi->in_reset = false; 14643 rc = bnxt_alloc_one_rx_ring(bp, i); 14644 if (rc) { 14645 netdev_warn(bp->dev, "RX ring reset failed to allocate buffers, rc = %d, falling back to global reset\n", 14646 rc); 14647 bnxt_reset_task(bp, true); 14648 bnxt_rtnl_unlock_sp(bp); 14649 return; 14650 } 14651 cpr = &rxr->bnapi->cp_ring; 14652 cpr->sw_stats->rx.rx_resets++; 14653 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14654 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14655 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14656 } 14657 if (bp->flags & BNXT_FLAG_TPA) 14658 bnxt_set_tpa(bp, true); 14659 bnxt_rtnl_unlock_sp(bp); 14660 } 14661 14662 static void bnxt_fw_fatal_close(struct bnxt *bp) 14663 { 14664 bnxt_tx_disable(bp); 14665 bnxt_disable_napi(bp); 14666 bnxt_disable_int_sync(bp); 14667 bnxt_free_irq(bp); 14668 bnxt_clear_int_mode(bp); 14669 pci_disable_device(bp->pdev); 14670 } 14671 14672 static void bnxt_fw_reset_close(struct bnxt *bp) 14673 { 14674 /* When firmware is in fatal state, quiesce device and disable 14675 * bus master to prevent any potential bad DMAs before freeing 14676 * kernel memory. 14677 */ 14678 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14679 u16 val = 0; 14680 14681 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14682 if (val == 0xffff) 14683 bp->fw_reset_min_dsecs = 0; 14684 bnxt_fw_fatal_close(bp); 14685 } 14686 __bnxt_close_nic(bp, true, false); 14687 bnxt_vf_reps_free(bp); 14688 bnxt_clear_int_mode(bp); 14689 bnxt_hwrm_func_drv_unrgtr(bp); 14690 if (pci_is_enabled(bp->pdev)) 14691 pci_disable_device(bp->pdev); 14692 bnxt_free_ctx_mem(bp, false); 14693 } 14694 14695 static bool is_bnxt_fw_ok(struct bnxt *bp) 14696 { 14697 struct bnxt_fw_health *fw_health = bp->fw_health; 14698 bool no_heartbeat = false, has_reset = false; 14699 u32 val; 14700 14701 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14702 if (val == fw_health->last_fw_heartbeat) 14703 no_heartbeat = true; 14704 14705 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14706 if (val != fw_health->last_fw_reset_cnt) 14707 has_reset = true; 14708 14709 if (!no_heartbeat && has_reset) 14710 return true; 14711 14712 return false; 14713 } 14714 14715 /* netdev instance lock is acquired before calling this function */ 14716 static void bnxt_force_fw_reset(struct bnxt *bp) 14717 { 14718 struct bnxt_fw_health *fw_health = bp->fw_health; 14719 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14720 u32 wait_dsecs; 14721 14722 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14723 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14724 return; 14725 14726 /* we have to serialize with bnxt_refclk_read()*/ 14727 if (ptp) { 14728 unsigned long flags; 14729 14730 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14731 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14732 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14733 } else { 14734 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14735 } 14736 bnxt_fw_reset_close(bp); 14737 wait_dsecs = fw_health->master_func_wait_dsecs; 14738 if (fw_health->primary) { 14739 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14740 wait_dsecs = 0; 14741 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14742 } else { 14743 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14744 wait_dsecs = fw_health->normal_func_wait_dsecs; 14745 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14746 } 14747 14748 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14749 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14750 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14751 } 14752 14753 void bnxt_fw_exception(struct bnxt *bp) 14754 { 14755 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14756 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14757 bnxt_ulp_stop(bp); 14758 bnxt_lock_sp(bp); 14759 bnxt_force_fw_reset(bp); 14760 bnxt_unlock_sp(bp); 14761 } 14762 14763 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14764 * < 0 on error. 14765 */ 14766 static int bnxt_get_registered_vfs(struct bnxt *bp) 14767 { 14768 #ifdef CONFIG_BNXT_SRIOV 14769 int rc; 14770 14771 if (!BNXT_PF(bp)) 14772 return 0; 14773 14774 rc = bnxt_hwrm_func_qcfg(bp); 14775 if (rc) { 14776 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14777 return rc; 14778 } 14779 if (bp->pf.registered_vfs) 14780 return bp->pf.registered_vfs; 14781 if (bp->sriov_cfg) 14782 return 1; 14783 #endif 14784 return 0; 14785 } 14786 14787 void bnxt_fw_reset(struct bnxt *bp) 14788 { 14789 bnxt_ulp_stop(bp); 14790 bnxt_lock_sp(bp); 14791 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14792 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14793 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14794 int n = 0, tmo; 14795 14796 /* we have to serialize with bnxt_refclk_read()*/ 14797 if (ptp) { 14798 unsigned long flags; 14799 14800 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14801 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14802 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14803 } else { 14804 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14805 } 14806 if (bp->pf.active_vfs && 14807 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14808 n = bnxt_get_registered_vfs(bp); 14809 if (n < 0) { 14810 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14811 n); 14812 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14813 netif_close(bp->dev); 14814 goto fw_reset_exit; 14815 } else if (n > 0) { 14816 u16 vf_tmo_dsecs = n * 10; 14817 14818 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14819 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14820 bp->fw_reset_state = 14821 BNXT_FW_RESET_STATE_POLL_VF; 14822 bnxt_queue_fw_reset_work(bp, HZ / 10); 14823 goto fw_reset_exit; 14824 } 14825 bnxt_fw_reset_close(bp); 14826 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14827 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14828 tmo = HZ / 10; 14829 } else { 14830 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14831 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14832 } 14833 bnxt_queue_fw_reset_work(bp, tmo); 14834 } 14835 fw_reset_exit: 14836 bnxt_unlock_sp(bp); 14837 } 14838 14839 static void bnxt_chk_missed_irq(struct bnxt *bp) 14840 { 14841 int i; 14842 14843 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14844 return; 14845 14846 for (i = 0; i < bp->cp_nr_rings; i++) { 14847 struct bnxt_napi *bnapi = bp->bnapi[i]; 14848 struct bnxt_cp_ring_info *cpr; 14849 u32 fw_ring_id; 14850 int j; 14851 14852 if (!bnapi) 14853 continue; 14854 14855 cpr = &bnapi->cp_ring; 14856 for (j = 0; j < cpr->cp_ring_count; j++) { 14857 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14858 u32 val[2]; 14859 14860 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14861 continue; 14862 14863 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14864 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14865 continue; 14866 } 14867 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14868 bnxt_dbg_hwrm_ring_info_get(bp, 14869 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14870 fw_ring_id, &val[0], &val[1]); 14871 cpr->sw_stats->cmn.missed_irqs++; 14872 } 14873 } 14874 } 14875 14876 static void bnxt_cfg_ntp_filters(struct bnxt *); 14877 14878 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14879 { 14880 struct bnxt_link_info *link_info = &bp->link_info; 14881 14882 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14883 link_info->autoneg = BNXT_AUTONEG_SPEED; 14884 if (bp->hwrm_spec_code >= 0x10201) { 14885 if (link_info->auto_pause_setting & 14886 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14887 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14888 } else { 14889 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14890 } 14891 bnxt_set_auto_speed(link_info); 14892 } else { 14893 bnxt_set_force_speed(link_info); 14894 link_info->req_duplex = link_info->duplex_setting; 14895 } 14896 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14897 link_info->req_flow_ctrl = 14898 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14899 else 14900 link_info->req_flow_ctrl = link_info->force_pause_setting; 14901 } 14902 14903 static void bnxt_fw_echo_reply(struct bnxt *bp) 14904 { 14905 struct bnxt_fw_health *fw_health = bp->fw_health; 14906 struct hwrm_func_echo_response_input *req; 14907 int rc; 14908 14909 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14910 if (rc) 14911 return; 14912 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14913 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14914 hwrm_req_send(bp, req); 14915 } 14916 14917 static void bnxt_ulp_restart(struct bnxt *bp) 14918 { 14919 bnxt_ulp_stop(bp); 14920 bnxt_ulp_start(bp); 14921 } 14922 14923 static void bnxt_sp_task(struct work_struct *work) 14924 { 14925 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14926 14927 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14928 smp_mb__after_atomic(); 14929 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14930 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14931 return; 14932 } 14933 14934 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14935 bnxt_ulp_restart(bp); 14936 bnxt_reenable_sriov(bp); 14937 } 14938 14939 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14940 bnxt_cfg_ntp_filters(bp); 14941 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14942 bnxt_hwrm_exec_fwd_req(bp); 14943 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14944 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14945 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14946 bnxt_hwrm_port_qstats(bp, 0); 14947 bnxt_hwrm_port_qstats_ext(bp, 0); 14948 spin_lock(&bp->stats_lock); 14949 bnxt_accumulate_all_stats(bp); 14950 bp->stats_updated_jiffies = jiffies; 14951 spin_unlock(&bp->stats_lock); 14952 } 14953 14954 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14955 int rc; 14956 14957 mutex_lock(&bp->link_lock); 14958 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14959 &bp->sp_event)) 14960 bnxt_hwrm_phy_qcaps(bp); 14961 14962 rc = bnxt_update_link(bp, true); 14963 if (rc) 14964 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14965 rc); 14966 14967 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14968 &bp->sp_event)) 14969 bnxt_init_ethtool_link_settings(bp); 14970 mutex_unlock(&bp->link_lock); 14971 } 14972 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14973 int rc; 14974 14975 mutex_lock(&bp->link_lock); 14976 rc = bnxt_update_phy_setting(bp); 14977 mutex_unlock(&bp->link_lock); 14978 if (rc) { 14979 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14980 } else { 14981 bp->link_info.phy_retry = false; 14982 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14983 } 14984 } 14985 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14986 mutex_lock(&bp->link_lock); 14987 bnxt_get_port_module_status(bp); 14988 mutex_unlock(&bp->link_lock); 14989 } 14990 14991 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14992 bnxt_tc_flow_stats_work(bp); 14993 14994 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14995 bnxt_chk_missed_irq(bp); 14996 14997 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14998 bnxt_fw_echo_reply(bp); 14999 15000 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 15001 bnxt_hwmon_notify_event(bp); 15002 15003 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 15004 * must be the last functions to be called before exiting. 15005 */ 15006 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 15007 bnxt_reset(bp, false); 15008 15009 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 15010 bnxt_reset(bp, true); 15011 15012 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 15013 bnxt_rx_ring_reset(bp); 15014 15015 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 15016 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 15017 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 15018 bnxt_devlink_health_fw_report(bp); 15019 else 15020 bnxt_fw_reset(bp); 15021 } 15022 15023 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 15024 if (!is_bnxt_fw_ok(bp)) 15025 bnxt_devlink_health_fw_report(bp); 15026 } 15027 15028 if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event)) 15029 bnxt_tph_update(bp); 15030 15031 smp_mb__before_atomic(); 15032 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 15033 } 15034 15035 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15036 int *max_cp); 15037 15038 /* Under netdev instance lock */ 15039 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 15040 int tx_xdp) 15041 { 15042 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 15043 struct bnxt_hw_rings hwr = {0}; 15044 int rx_rings = rx; 15045 int rc; 15046 15047 if (tcs) 15048 tx_sets = tcs; 15049 15050 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 15051 15052 if (max_rx < rx_rings) 15053 return -ENOMEM; 15054 15055 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15056 rx_rings <<= 1; 15057 15058 hwr.rx = rx_rings; 15059 hwr.tx = tx * tx_sets + tx_xdp; 15060 if (max_tx < hwr.tx) 15061 return -ENOMEM; 15062 15063 hwr.vnic = bnxt_get_total_vnics(bp, rx); 15064 15065 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 15066 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 15067 if (max_cp < hwr.cp) 15068 return -ENOMEM; 15069 hwr.stat = hwr.cp; 15070 if (BNXT_NEW_RM(bp)) { 15071 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 15072 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 15073 hwr.grp = rx; 15074 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 15075 } 15076 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 15077 hwr.cp_p5 = hwr.tx + rx; 15078 rc = bnxt_hwrm_check_rings(bp, &hwr); 15079 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 15080 if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) { 15081 hwr.cp += bnxt_get_ulp_msix_num(bp); 15082 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 15083 } 15084 if (hwr.cp > bp->total_irqs) { 15085 int total_msix = bnxt_change_msix(bp, hwr.cp); 15086 15087 if (total_msix < hwr.cp) { 15088 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 15089 hwr.cp, total_msix); 15090 rc = -ENOSPC; 15091 } 15092 } 15093 } 15094 return rc; 15095 } 15096 15097 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 15098 { 15099 if (bp->bar2) { 15100 pci_iounmap(pdev, bp->bar2); 15101 bp->bar2 = NULL; 15102 } 15103 15104 if (bp->bar1) { 15105 pci_iounmap(pdev, bp->bar1); 15106 bp->bar1 = NULL; 15107 } 15108 15109 if (bp->bar0) { 15110 pci_iounmap(pdev, bp->bar0); 15111 bp->bar0 = NULL; 15112 } 15113 } 15114 15115 static void bnxt_cleanup_pci(struct bnxt *bp) 15116 { 15117 pci_disable_ptm(bp->pdev); 15118 bnxt_unmap_bars(bp, bp->pdev); 15119 pci_release_regions(bp->pdev); 15120 if (pci_is_enabled(bp->pdev)) 15121 pci_disable_device(bp->pdev); 15122 } 15123 15124 static void bnxt_init_dflt_coal(struct bnxt *bp) 15125 { 15126 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 15127 struct bnxt_coal *coal; 15128 u16 flags = 0; 15129 15130 if (coal_cap->cmpl_params & 15131 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 15132 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 15133 15134 /* Tick values in micro seconds. 15135 * 1 coal_buf x bufs_per_record = 1 completion record. 15136 */ 15137 coal = &bp->rx_coal; 15138 coal->coal_ticks = 10; 15139 coal->coal_bufs = 30; 15140 coal->coal_ticks_irq = 1; 15141 coal->coal_bufs_irq = 2; 15142 coal->idle_thresh = 50; 15143 coal->bufs_per_record = 2; 15144 coal->budget = 64; /* NAPI budget */ 15145 coal->flags = flags; 15146 15147 coal = &bp->tx_coal; 15148 coal->coal_ticks = 28; 15149 coal->coal_bufs = 30; 15150 coal->coal_ticks_irq = 2; 15151 coal->coal_bufs_irq = 2; 15152 coal->bufs_per_record = 1; 15153 coal->flags = flags; 15154 15155 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 15156 } 15157 15158 /* FW that pre-reserves 1 VNIC per function */ 15159 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 15160 { 15161 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 15162 15163 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 15164 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 15165 return true; 15166 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 15167 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 15168 return true; 15169 return false; 15170 } 15171 15172 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp) 15173 { 15174 struct hwrm_queue_pfcwd_timeout_qcaps_output *resp; 15175 struct hwrm_queue_pfcwd_timeout_qcaps_input *req; 15176 int rc; 15177 15178 bp->max_pfcwd_tmo_ms = 0; 15179 rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS); 15180 if (rc) 15181 return; 15182 resp = hwrm_req_hold(bp, req); 15183 rc = hwrm_req_send_silent(bp, req); 15184 if (!rc) 15185 bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout); 15186 hwrm_req_drop(bp, req); 15187 } 15188 15189 static int bnxt_fw_init_one_p1(struct bnxt *bp) 15190 { 15191 int rc; 15192 15193 bp->fw_cap = 0; 15194 rc = bnxt_hwrm_ver_get(bp); 15195 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 15196 * so wait before continuing with recovery. 15197 */ 15198 if (rc) 15199 msleep(100); 15200 bnxt_try_map_fw_health_reg(bp); 15201 if (rc) { 15202 rc = bnxt_try_recover_fw(bp); 15203 if (rc) 15204 return rc; 15205 rc = bnxt_hwrm_ver_get(bp); 15206 if (rc) 15207 return rc; 15208 } 15209 15210 bnxt_nvm_cfg_ver_get(bp); 15211 15212 rc = bnxt_hwrm_func_reset(bp); 15213 if (rc) 15214 return -ENODEV; 15215 15216 bnxt_hwrm_fw_set_time(bp); 15217 return 0; 15218 } 15219 15220 static int bnxt_fw_init_one_p2(struct bnxt *bp) 15221 { 15222 int rc; 15223 15224 /* Get the MAX capabilities for this function */ 15225 rc = bnxt_hwrm_func_qcaps(bp); 15226 if (rc) { 15227 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 15228 rc); 15229 return -ENODEV; 15230 } 15231 15232 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 15233 if (rc) 15234 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 15235 rc); 15236 15237 if (bnxt_alloc_fw_health(bp)) { 15238 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 15239 } else { 15240 rc = bnxt_hwrm_error_recovery_qcfg(bp); 15241 if (rc) 15242 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 15243 rc); 15244 } 15245 15246 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 15247 if (rc) 15248 return -ENODEV; 15249 15250 rc = bnxt_alloc_crash_dump_mem(bp); 15251 if (rc) 15252 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 15253 rc); 15254 if (!rc) { 15255 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 15256 if (rc) { 15257 bnxt_free_crash_dump_mem(bp); 15258 netdev_warn(bp->dev, 15259 "hwrm crash dump mem failure rc: %d\n", rc); 15260 } 15261 } 15262 15263 if (bnxt_fw_pre_resv_vnics(bp)) 15264 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 15265 15266 bnxt_hwrm_pfcwd_qcaps(bp); 15267 bnxt_hwrm_func_qcfg(bp); 15268 bnxt_hwrm_vnic_qcaps(bp); 15269 bnxt_hwrm_port_led_qcaps(bp); 15270 bnxt_ethtool_init(bp); 15271 if (bp->fw_cap & BNXT_FW_CAP_PTP) 15272 __bnxt_hwrm_ptp_qcfg(bp); 15273 bnxt_dcb_init(bp); 15274 bnxt_hwmon_init(bp); 15275 return 0; 15276 } 15277 15278 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 15279 { 15280 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 15281 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 15282 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 15283 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 15284 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 15285 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 15286 bp->rss_hash_delta = bp->rss_hash_cfg; 15287 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 15288 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 15289 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 15290 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 15291 } 15292 } 15293 15294 static void bnxt_set_dflt_rfs(struct bnxt *bp) 15295 { 15296 struct net_device *dev = bp->dev; 15297 15298 dev->hw_features &= ~NETIF_F_NTUPLE; 15299 dev->features &= ~NETIF_F_NTUPLE; 15300 bp->flags &= ~BNXT_FLAG_RFS; 15301 if (bnxt_rfs_supported(bp)) { 15302 dev->hw_features |= NETIF_F_NTUPLE; 15303 if (bnxt_rfs_capable(bp, false)) { 15304 bp->flags |= BNXT_FLAG_RFS; 15305 dev->features |= NETIF_F_NTUPLE; 15306 } 15307 } 15308 } 15309 15310 static void bnxt_fw_init_one_p3(struct bnxt *bp) 15311 { 15312 struct pci_dev *pdev = bp->pdev; 15313 15314 bnxt_set_dflt_rss_hash_type(bp); 15315 bnxt_set_dflt_rfs(bp); 15316 15317 bnxt_get_wol_settings(bp); 15318 if (bp->flags & BNXT_FLAG_WOL_CAP) 15319 device_set_wakeup_enable(&pdev->dev, bp->wol); 15320 else 15321 device_set_wakeup_capable(&pdev->dev, false); 15322 15323 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 15324 bnxt_hwrm_coal_params_qcaps(bp); 15325 } 15326 15327 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 15328 15329 int bnxt_fw_init_one(struct bnxt *bp) 15330 { 15331 int rc; 15332 15333 rc = bnxt_fw_init_one_p1(bp); 15334 if (rc) { 15335 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 15336 return rc; 15337 } 15338 rc = bnxt_fw_init_one_p2(bp); 15339 if (rc) { 15340 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 15341 return rc; 15342 } 15343 rc = bnxt_probe_phy(bp, false); 15344 if (rc) 15345 return rc; 15346 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 15347 if (rc) 15348 return rc; 15349 15350 bnxt_fw_init_one_p3(bp); 15351 return 0; 15352 } 15353 15354 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 15355 { 15356 struct bnxt_fw_health *fw_health = bp->fw_health; 15357 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 15358 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 15359 u32 reg_type, reg_off, delay_msecs; 15360 15361 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 15362 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 15363 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 15364 switch (reg_type) { 15365 case BNXT_FW_HEALTH_REG_TYPE_CFG: 15366 pci_write_config_dword(bp->pdev, reg_off, val); 15367 break; 15368 case BNXT_FW_HEALTH_REG_TYPE_GRC: 15369 writel(reg_off & BNXT_GRC_BASE_MASK, 15370 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 15371 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 15372 fallthrough; 15373 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 15374 writel(val, bp->bar0 + reg_off); 15375 break; 15376 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 15377 writel(val, bp->bar1 + reg_off); 15378 break; 15379 } 15380 if (delay_msecs) { 15381 pci_read_config_dword(bp->pdev, 0, &val); 15382 msleep(delay_msecs); 15383 } 15384 } 15385 15386 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 15387 { 15388 struct hwrm_func_qcfg_output *resp; 15389 struct hwrm_func_qcfg_input *req; 15390 bool result = true; /* firmware will enforce if unknown */ 15391 15392 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 15393 return result; 15394 15395 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 15396 return result; 15397 15398 req->fid = cpu_to_le16(0xffff); 15399 resp = hwrm_req_hold(bp, req); 15400 if (!hwrm_req_send(bp, req)) 15401 result = !!(le16_to_cpu(resp->flags) & 15402 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 15403 hwrm_req_drop(bp, req); 15404 return result; 15405 } 15406 15407 static void bnxt_reset_all(struct bnxt *bp) 15408 { 15409 struct bnxt_fw_health *fw_health = bp->fw_health; 15410 int i, rc; 15411 15412 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15413 bnxt_fw_reset_via_optee(bp); 15414 bp->fw_reset_timestamp = jiffies; 15415 return; 15416 } 15417 15418 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 15419 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 15420 bnxt_fw_reset_writel(bp, i); 15421 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 15422 struct hwrm_fw_reset_input *req; 15423 15424 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 15425 if (!rc) { 15426 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 15427 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 15428 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 15429 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 15430 rc = hwrm_req_send(bp, req); 15431 } 15432 if (rc != -ENODEV) 15433 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 15434 } 15435 bp->fw_reset_timestamp = jiffies; 15436 } 15437 15438 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 15439 { 15440 return time_after(jiffies, bp->fw_reset_timestamp + 15441 (bp->fw_reset_max_dsecs * HZ / 10)); 15442 } 15443 15444 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 15445 { 15446 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15447 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 15448 bnxt_dl_health_fw_status_update(bp, false); 15449 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 15450 netif_close(bp->dev); 15451 } 15452 15453 static void bnxt_fw_reset_task(struct work_struct *work) 15454 { 15455 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 15456 int rc = 0; 15457 15458 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 15459 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 15460 return; 15461 } 15462 15463 switch (bp->fw_reset_state) { 15464 case BNXT_FW_RESET_STATE_POLL_VF: { 15465 int n = bnxt_get_registered_vfs(bp); 15466 int tmo; 15467 15468 if (n < 0) { 15469 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 15470 n, jiffies_to_msecs(jiffies - 15471 bp->fw_reset_timestamp)); 15472 goto fw_reset_abort; 15473 } else if (n > 0) { 15474 if (bnxt_fw_reset_timeout(bp)) { 15475 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15476 bp->fw_reset_state = 0; 15477 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 15478 n); 15479 goto ulp_start; 15480 } 15481 bnxt_queue_fw_reset_work(bp, HZ / 10); 15482 return; 15483 } 15484 bp->fw_reset_timestamp = jiffies; 15485 netdev_lock(bp->dev); 15486 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 15487 bnxt_fw_reset_abort(bp, rc); 15488 netdev_unlock(bp->dev); 15489 goto ulp_start; 15490 } 15491 bnxt_fw_reset_close(bp); 15492 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15493 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15494 tmo = HZ / 10; 15495 } else { 15496 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15497 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15498 } 15499 netdev_unlock(bp->dev); 15500 bnxt_queue_fw_reset_work(bp, tmo); 15501 return; 15502 } 15503 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15504 u32 val; 15505 15506 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15507 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15508 !bnxt_fw_reset_timeout(bp)) { 15509 bnxt_queue_fw_reset_work(bp, HZ / 5); 15510 return; 15511 } 15512 15513 if (!bp->fw_health->primary) { 15514 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15515 15516 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15517 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15518 return; 15519 } 15520 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15521 } 15522 fallthrough; 15523 case BNXT_FW_RESET_STATE_RESET_FW: 15524 bnxt_reset_all(bp); 15525 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15526 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15527 return; 15528 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15529 bnxt_inv_fw_health_reg(bp); 15530 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15531 !bp->fw_reset_min_dsecs) { 15532 u16 val; 15533 15534 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15535 if (val == 0xffff) { 15536 if (bnxt_fw_reset_timeout(bp)) { 15537 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15538 rc = -ETIMEDOUT; 15539 goto fw_reset_abort; 15540 } 15541 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15542 return; 15543 } 15544 } 15545 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15546 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15547 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15548 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15549 bnxt_dl_remote_reload(bp); 15550 if (pci_enable_device(bp->pdev)) { 15551 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15552 rc = -ENODEV; 15553 goto fw_reset_abort; 15554 } 15555 pci_set_master(bp->pdev); 15556 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15557 fallthrough; 15558 case BNXT_FW_RESET_STATE_POLL_FW: 15559 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15560 rc = bnxt_hwrm_poll(bp); 15561 if (rc) { 15562 if (bnxt_fw_reset_timeout(bp)) { 15563 netdev_err(bp->dev, "Firmware reset aborted\n"); 15564 goto fw_reset_abort_status; 15565 } 15566 bnxt_queue_fw_reset_work(bp, HZ / 5); 15567 return; 15568 } 15569 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15570 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15571 fallthrough; 15572 case BNXT_FW_RESET_STATE_OPENING: 15573 while (!rtnl_trylock()) { 15574 bnxt_queue_fw_reset_work(bp, HZ / 10); 15575 return; 15576 } 15577 netdev_lock(bp->dev); 15578 rc = bnxt_open(bp->dev); 15579 if (rc) { 15580 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15581 bnxt_fw_reset_abort(bp, rc); 15582 netdev_unlock(bp->dev); 15583 rtnl_unlock(); 15584 goto ulp_start; 15585 } 15586 15587 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15588 bp->fw_health->enabled) { 15589 bp->fw_health->last_fw_reset_cnt = 15590 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15591 } 15592 bp->fw_reset_state = 0; 15593 /* Make sure fw_reset_state is 0 before clearing the flag */ 15594 smp_mb__before_atomic(); 15595 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15596 bnxt_ptp_reapply_pps(bp); 15597 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15598 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15599 bnxt_dl_health_fw_recovery_done(bp); 15600 bnxt_dl_health_fw_status_update(bp, true); 15601 } 15602 netdev_unlock(bp->dev); 15603 rtnl_unlock(); 15604 bnxt_ulp_start(bp); 15605 bnxt_reenable_sriov(bp); 15606 netdev_lock(bp->dev); 15607 bnxt_vf_reps_alloc(bp); 15608 bnxt_vf_reps_open(bp); 15609 netdev_unlock(bp->dev); 15610 break; 15611 } 15612 return; 15613 15614 fw_reset_abort_status: 15615 if (bp->fw_health->status_reliable || 15616 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15617 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15618 15619 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15620 } 15621 fw_reset_abort: 15622 netdev_lock(bp->dev); 15623 bnxt_fw_reset_abort(bp, rc); 15624 netdev_unlock(bp->dev); 15625 ulp_start: 15626 if (!rc) 15627 bnxt_ulp_start(bp); 15628 } 15629 15630 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15631 { 15632 int rc; 15633 struct bnxt *bp = netdev_priv(dev); 15634 15635 SET_NETDEV_DEV(dev, &pdev->dev); 15636 15637 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15638 rc = pci_enable_device(pdev); 15639 if (rc) { 15640 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15641 goto init_err; 15642 } 15643 15644 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15645 dev_err(&pdev->dev, 15646 "Cannot find PCI device base address, aborting\n"); 15647 rc = -ENODEV; 15648 goto init_err_disable; 15649 } 15650 15651 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15652 if (rc) { 15653 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15654 goto init_err_disable; 15655 } 15656 15657 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15658 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15659 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15660 rc = -EIO; 15661 goto init_err_release; 15662 } 15663 15664 pci_set_master(pdev); 15665 15666 bp->dev = dev; 15667 bp->pdev = pdev; 15668 15669 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15670 * determines the BAR size. 15671 */ 15672 bp->bar0 = pci_ioremap_bar(pdev, 0); 15673 if (!bp->bar0) { 15674 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15675 rc = -ENOMEM; 15676 goto init_err_release; 15677 } 15678 15679 bp->bar2 = pci_ioremap_bar(pdev, 4); 15680 if (!bp->bar2) { 15681 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15682 rc = -ENOMEM; 15683 goto init_err_release; 15684 } 15685 15686 pci_enable_ptm(pdev); 15687 15688 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15689 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15690 15691 spin_lock_init(&bp->ntp_fltr_lock); 15692 spin_lock_init(&bp->stats_lock); 15693 #if BITS_PER_LONG == 32 15694 spin_lock_init(&bp->db_lock); 15695 #endif 15696 15697 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15698 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15699 15700 timer_setup(&bp->timer, bnxt_timer, 0); 15701 bp->current_interval = BNXT_TIMER_INTERVAL; 15702 15703 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15704 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15705 15706 clear_bit(BNXT_STATE_OPEN, &bp->state); 15707 return 0; 15708 15709 init_err_release: 15710 bnxt_unmap_bars(bp, pdev); 15711 pci_release_regions(pdev); 15712 15713 init_err_disable: 15714 pci_disable_device(pdev); 15715 15716 init_err: 15717 return rc; 15718 } 15719 15720 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15721 { 15722 struct sockaddr *addr = p; 15723 struct bnxt *bp = netdev_priv(dev); 15724 int rc = 0; 15725 15726 netdev_assert_locked(dev); 15727 15728 if (!is_valid_ether_addr(addr->sa_data)) 15729 return -EADDRNOTAVAIL; 15730 15731 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15732 return 0; 15733 15734 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15735 if (rc) 15736 return rc; 15737 15738 eth_hw_addr_set(dev, addr->sa_data); 15739 bnxt_clear_usr_fltrs(bp, true); 15740 if (netif_running(dev)) { 15741 bnxt_close_nic(bp, false, false); 15742 rc = bnxt_open_nic(bp, false, false); 15743 } 15744 15745 return rc; 15746 } 15747 15748 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15749 { 15750 struct bnxt *bp = netdev_priv(dev); 15751 15752 netdev_assert_locked(dev); 15753 15754 if (netif_running(dev)) 15755 bnxt_close_nic(bp, true, false); 15756 15757 WRITE_ONCE(dev->mtu, new_mtu); 15758 15759 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15760 * program is attached. We need to set the AGG rings settings and 15761 * rx_skb_func accordingly. 15762 */ 15763 if (READ_ONCE(bp->xdp_prog)) 15764 bnxt_set_rx_skb_mode(bp, true); 15765 15766 bnxt_set_ring_params(bp); 15767 15768 if (netif_running(dev)) 15769 return bnxt_open_nic(bp, true, false); 15770 15771 return 0; 15772 } 15773 15774 void bnxt_set_cp_rings(struct bnxt *bp, bool sh) 15775 { 15776 int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15777 15778 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15779 tx_cp + bp->rx_nr_rings; 15780 } 15781 15782 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15783 { 15784 struct bnxt *bp = netdev_priv(dev); 15785 bool sh = false; 15786 int rc; 15787 15788 if (tc > bp->max_tc) { 15789 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15790 tc, bp->max_tc); 15791 return -EINVAL; 15792 } 15793 15794 if (bp->num_tc == tc) 15795 return 0; 15796 15797 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15798 sh = true; 15799 15800 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15801 sh, tc, bp->tx_nr_rings_xdp); 15802 if (rc) 15803 return rc; 15804 15805 /* Needs to close the device and do hw resource re-allocations */ 15806 if (netif_running(bp->dev)) 15807 bnxt_close_nic(bp, true, false); 15808 15809 if (tc) { 15810 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15811 netdev_set_num_tc(dev, tc); 15812 bp->num_tc = tc; 15813 } else { 15814 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15815 netdev_reset_tc(dev); 15816 bp->num_tc = 0; 15817 } 15818 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15819 bnxt_set_cp_rings(bp, sh); 15820 15821 if (netif_running(bp->dev)) 15822 return bnxt_open_nic(bp, true, false); 15823 15824 return 0; 15825 } 15826 15827 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15828 void *cb_priv) 15829 { 15830 struct bnxt *bp = cb_priv; 15831 15832 if (!bnxt_tc_flower_enabled(bp) || 15833 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15834 return -EOPNOTSUPP; 15835 15836 switch (type) { 15837 case TC_SETUP_CLSFLOWER: 15838 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15839 default: 15840 return -EOPNOTSUPP; 15841 } 15842 } 15843 15844 LIST_HEAD(bnxt_block_cb_list); 15845 15846 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15847 void *type_data) 15848 { 15849 struct bnxt *bp = netdev_priv(dev); 15850 15851 switch (type) { 15852 case TC_SETUP_BLOCK: 15853 return flow_block_cb_setup_simple(type_data, 15854 &bnxt_block_cb_list, 15855 bnxt_setup_tc_block_cb, 15856 bp, bp, true); 15857 case TC_SETUP_QDISC_MQPRIO: { 15858 struct tc_mqprio_qopt *mqprio = type_data; 15859 15860 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15861 15862 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15863 } 15864 default: 15865 return -EOPNOTSUPP; 15866 } 15867 } 15868 15869 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15870 const struct sk_buff *skb) 15871 { 15872 struct bnxt_vnic_info *vnic; 15873 15874 if (skb) 15875 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15876 15877 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15878 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15879 } 15880 15881 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15882 u32 idx) 15883 { 15884 struct hlist_head *head; 15885 int bit_id; 15886 15887 spin_lock_bh(&bp->ntp_fltr_lock); 15888 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15889 if (bit_id < 0) { 15890 spin_unlock_bh(&bp->ntp_fltr_lock); 15891 return -ENOMEM; 15892 } 15893 15894 fltr->base.sw_id = (u16)bit_id; 15895 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15896 fltr->base.flags |= BNXT_ACT_RING_DST; 15897 head = &bp->ntp_fltr_hash_tbl[idx]; 15898 hlist_add_head_rcu(&fltr->base.hash, head); 15899 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15900 bnxt_insert_usr_fltr(bp, &fltr->base); 15901 bp->ntp_fltr_count++; 15902 spin_unlock_bh(&bp->ntp_fltr_lock); 15903 return 0; 15904 } 15905 15906 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15907 struct bnxt_ntuple_filter *f2) 15908 { 15909 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15910 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15911 struct flow_keys *keys1 = &f1->fkeys; 15912 struct flow_keys *keys2 = &f2->fkeys; 15913 15914 if (keys1->basic.n_proto != keys2->basic.n_proto || 15915 keys1->basic.ip_proto != keys2->basic.ip_proto) 15916 return false; 15917 15918 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15919 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15920 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15921 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15922 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15923 return false; 15924 } else { 15925 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15926 &keys2->addrs.v6addrs.src) || 15927 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15928 &masks2->addrs.v6addrs.src) || 15929 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15930 &keys2->addrs.v6addrs.dst) || 15931 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15932 &masks2->addrs.v6addrs.dst)) 15933 return false; 15934 } 15935 15936 return keys1->ports.src == keys2->ports.src && 15937 masks1->ports.src == masks2->ports.src && 15938 keys1->ports.dst == keys2->ports.dst && 15939 masks1->ports.dst == masks2->ports.dst && 15940 keys1->control.flags == keys2->control.flags && 15941 f1->l2_fltr == f2->l2_fltr; 15942 } 15943 15944 struct bnxt_ntuple_filter * 15945 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15946 struct bnxt_ntuple_filter *fltr, u32 idx) 15947 { 15948 struct bnxt_ntuple_filter *f; 15949 struct hlist_head *head; 15950 15951 head = &bp->ntp_fltr_hash_tbl[idx]; 15952 hlist_for_each_entry_rcu(f, head, base.hash) { 15953 if (bnxt_fltr_match(f, fltr)) 15954 return f; 15955 } 15956 return NULL; 15957 } 15958 15959 #ifdef CONFIG_RFS_ACCEL 15960 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15961 u16 rxq_index, u32 flow_id) 15962 { 15963 struct bnxt *bp = netdev_priv(dev); 15964 struct bnxt_ntuple_filter *fltr, *new_fltr; 15965 struct flow_keys *fkeys; 15966 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15967 struct bnxt_l2_filter *l2_fltr; 15968 int rc = 0, idx; 15969 u32 flags; 15970 15971 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15972 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15973 atomic_inc(&l2_fltr->refcnt); 15974 } else { 15975 struct bnxt_l2_key key; 15976 15977 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15978 key.vlan = 0; 15979 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15980 if (!l2_fltr) 15981 return -EINVAL; 15982 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15983 bnxt_del_l2_filter(bp, l2_fltr); 15984 return -EINVAL; 15985 } 15986 } 15987 new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC); 15988 if (!new_fltr) { 15989 bnxt_del_l2_filter(bp, l2_fltr); 15990 return -ENOMEM; 15991 } 15992 15993 fkeys = &new_fltr->fkeys; 15994 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15995 rc = -EPROTONOSUPPORT; 15996 goto err_free; 15997 } 15998 15999 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 16000 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 16001 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 16002 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 16003 rc = -EPROTONOSUPPORT; 16004 goto err_free; 16005 } 16006 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 16007 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 16008 if (bp->hwrm_spec_code < 0x10601) { 16009 rc = -EPROTONOSUPPORT; 16010 goto err_free; 16011 } 16012 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 16013 } 16014 flags = fkeys->control.flags; 16015 if (((flags & FLOW_DIS_ENCAPSULATION) && 16016 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 16017 rc = -EPROTONOSUPPORT; 16018 goto err_free; 16019 } 16020 new_fltr->l2_fltr = l2_fltr; 16021 16022 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 16023 rcu_read_lock(); 16024 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 16025 if (fltr) { 16026 rc = fltr->base.sw_id; 16027 rcu_read_unlock(); 16028 goto err_free; 16029 } 16030 rcu_read_unlock(); 16031 16032 new_fltr->flow_id = flow_id; 16033 new_fltr->base.rxq = rxq_index; 16034 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 16035 if (!rc) { 16036 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 16037 return new_fltr->base.sw_id; 16038 } 16039 16040 err_free: 16041 bnxt_del_l2_filter(bp, l2_fltr); 16042 kfree(new_fltr); 16043 return rc; 16044 } 16045 #endif 16046 16047 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 16048 { 16049 spin_lock_bh(&bp->ntp_fltr_lock); 16050 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 16051 spin_unlock_bh(&bp->ntp_fltr_lock); 16052 return; 16053 } 16054 hlist_del_rcu(&fltr->base.hash); 16055 bnxt_del_one_usr_fltr(bp, &fltr->base); 16056 bp->ntp_fltr_count--; 16057 spin_unlock_bh(&bp->ntp_fltr_lock); 16058 bnxt_del_l2_filter(bp, fltr->l2_fltr); 16059 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 16060 kfree_rcu(fltr, base.rcu); 16061 } 16062 16063 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 16064 { 16065 #ifdef CONFIG_RFS_ACCEL 16066 int i; 16067 16068 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 16069 struct hlist_head *head; 16070 struct hlist_node *tmp; 16071 struct bnxt_ntuple_filter *fltr; 16072 int rc; 16073 16074 head = &bp->ntp_fltr_hash_tbl[i]; 16075 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 16076 bool del = false; 16077 16078 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 16079 if (fltr->base.flags & BNXT_ACT_NO_AGING) 16080 continue; 16081 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 16082 fltr->flow_id, 16083 fltr->base.sw_id)) { 16084 bnxt_hwrm_cfa_ntuple_filter_free(bp, 16085 fltr); 16086 del = true; 16087 } 16088 } else { 16089 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 16090 fltr); 16091 if (rc) 16092 del = true; 16093 else 16094 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 16095 } 16096 16097 if (del) 16098 bnxt_del_ntp_filter(bp, fltr); 16099 } 16100 } 16101 #endif 16102 } 16103 16104 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 16105 unsigned int entry, struct udp_tunnel_info *ti) 16106 { 16107 struct bnxt *bp = netdev_priv(netdev); 16108 unsigned int cmd; 16109 16110 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 16111 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 16112 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 16113 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 16114 else 16115 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 16116 16117 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 16118 } 16119 16120 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 16121 unsigned int entry, struct udp_tunnel_info *ti) 16122 { 16123 struct bnxt *bp = netdev_priv(netdev); 16124 unsigned int cmd; 16125 16126 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 16127 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 16128 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 16129 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 16130 else 16131 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 16132 16133 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 16134 } 16135 16136 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 16137 .set_port = bnxt_udp_tunnel_set_port, 16138 .unset_port = bnxt_udp_tunnel_unset_port, 16139 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 16140 .tables = { 16141 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 16142 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 16143 }, 16144 }, bnxt_udp_tunnels_p7 = { 16145 .set_port = bnxt_udp_tunnel_set_port, 16146 .unset_port = bnxt_udp_tunnel_unset_port, 16147 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 16148 .tables = { 16149 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 16150 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 16151 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 16152 }, 16153 }; 16154 16155 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 16156 struct net_device *dev, u32 filter_mask, 16157 int nlflags) 16158 { 16159 struct bnxt *bp = netdev_priv(dev); 16160 16161 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 16162 nlflags, filter_mask, NULL); 16163 } 16164 16165 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 16166 u16 flags, struct netlink_ext_ack *extack) 16167 { 16168 struct bnxt *bp = netdev_priv(dev); 16169 struct nlattr *attr, *br_spec; 16170 int rem, rc = 0; 16171 16172 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 16173 return -EOPNOTSUPP; 16174 16175 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 16176 if (!br_spec) 16177 return -EINVAL; 16178 16179 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 16180 u16 mode; 16181 16182 mode = nla_get_u16(attr); 16183 if (mode == bp->br_mode) 16184 break; 16185 16186 rc = bnxt_hwrm_set_br_mode(bp, mode); 16187 if (!rc) 16188 bp->br_mode = mode; 16189 break; 16190 } 16191 return rc; 16192 } 16193 16194 int bnxt_get_port_parent_id(struct net_device *dev, 16195 struct netdev_phys_item_id *ppid) 16196 { 16197 struct bnxt *bp = netdev_priv(dev); 16198 16199 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 16200 return -EOPNOTSUPP; 16201 16202 /* The PF and it's VF-reps only support the switchdev framework */ 16203 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 16204 return -EOPNOTSUPP; 16205 16206 ppid->id_len = sizeof(bp->dsn); 16207 memcpy(ppid->id, bp->dsn, ppid->id_len); 16208 16209 return 0; 16210 } 16211 16212 static const struct net_device_ops bnxt_netdev_ops = { 16213 .ndo_open = bnxt_open, 16214 .ndo_start_xmit = bnxt_start_xmit, 16215 .ndo_stop = bnxt_close, 16216 .ndo_get_stats64 = bnxt_get_stats64, 16217 .ndo_set_rx_mode_async = bnxt_set_rx_mode, 16218 .ndo_eth_ioctl = bnxt_ioctl, 16219 .ndo_validate_addr = eth_validate_addr, 16220 .ndo_set_mac_address = bnxt_change_mac_addr, 16221 .ndo_change_mtu = bnxt_change_mtu, 16222 .ndo_fix_features = bnxt_fix_features, 16223 .ndo_set_features = bnxt_set_features, 16224 .ndo_features_check = bnxt_features_check, 16225 .ndo_tx_timeout = bnxt_tx_timeout, 16226 #ifdef CONFIG_BNXT_SRIOV 16227 .ndo_get_vf_config = bnxt_get_vf_config, 16228 .ndo_set_vf_mac = bnxt_set_vf_mac, 16229 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 16230 .ndo_set_vf_rate = bnxt_set_vf_bw, 16231 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 16232 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 16233 .ndo_set_vf_trust = bnxt_set_vf_trust, 16234 #endif 16235 .ndo_setup_tc = bnxt_setup_tc, 16236 #ifdef CONFIG_RFS_ACCEL 16237 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 16238 #endif 16239 .ndo_bpf = bnxt_xdp, 16240 .ndo_xdp_xmit = bnxt_xdp_xmit, 16241 .ndo_bridge_getlink = bnxt_bridge_getlink, 16242 .ndo_bridge_setlink = bnxt_bridge_setlink, 16243 .ndo_hwtstamp_get = bnxt_hwtstamp_get, 16244 .ndo_hwtstamp_set = bnxt_hwtstamp_set, 16245 }; 16246 16247 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = { 16248 .xmo_rx_hash = bnxt_xdp_rx_hash, 16249 }; 16250 16251 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 16252 struct netdev_queue_stats_rx *stats) 16253 { 16254 struct bnxt *bp = netdev_priv(dev); 16255 struct bnxt_cp_ring_info *cpr; 16256 u64 *sw; 16257 16258 if (!bp->bnapi) 16259 return; 16260 16261 bnxt_sync_ring_stats(bp); 16262 cpr = &bp->bnapi[i]->cp_ring; 16263 sw = cpr->stats.sw_stats; 16264 16265 stats->packets = 0; 16266 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 16267 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 16268 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 16269 16270 stats->bytes = 0; 16271 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 16272 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 16273 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 16274 16275 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 16276 stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets; 16277 stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets; 16278 } 16279 16280 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 16281 struct netdev_queue_stats_tx *stats) 16282 { 16283 struct bnxt *bp = netdev_priv(dev); 16284 struct bnxt_napi *bnapi; 16285 u64 *sw; 16286 16287 if (!bp->tx_ring) 16288 return; 16289 16290 bnxt_sync_ring_stats(bp); 16291 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 16292 sw = bnapi->cp_ring.stats.sw_stats; 16293 16294 stats->packets = 0; 16295 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 16296 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 16297 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 16298 16299 stats->bytes = 0; 16300 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 16301 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 16302 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 16303 } 16304 16305 static void bnxt_get_base_stats(struct net_device *dev, 16306 struct netdev_queue_stats_rx *rx, 16307 struct netdev_queue_stats_tx *tx) 16308 { 16309 struct bnxt *bp = netdev_priv(dev); 16310 16311 rx->packets = bp->net_stats_prev.rx_packets; 16312 rx->bytes = bp->net_stats_prev.rx_bytes; 16313 rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards; 16314 rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets; 16315 rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets; 16316 16317 tx->packets = bp->net_stats_prev.tx_packets; 16318 tx->bytes = bp->net_stats_prev.tx_bytes; 16319 } 16320 16321 static const struct netdev_stat_ops bnxt_stat_ops = { 16322 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 16323 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 16324 .get_base_stats = bnxt_get_base_stats, 16325 }; 16326 16327 static void bnxt_queue_default_qcfg(struct net_device *dev, 16328 struct netdev_queue_config *qcfg) 16329 { 16330 qcfg->rx_page_size = BNXT_RX_PAGE_SIZE; 16331 } 16332 16333 static int bnxt_validate_qcfg(struct net_device *dev, 16334 struct netdev_queue_config *qcfg, 16335 struct netlink_ext_ack *extack) 16336 { 16337 struct bnxt *bp = netdev_priv(dev); 16338 16339 /* Older chips need MSS calc so rx_page_size is not supported */ 16340 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 16341 qcfg->rx_page_size != BNXT_RX_PAGE_SIZE) 16342 return -EINVAL; 16343 16344 if (!is_power_of_2(qcfg->rx_page_size)) 16345 return -ERANGE; 16346 16347 if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE || 16348 qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE) 16349 return -ERANGE; 16350 16351 return 0; 16352 } 16353 16354 static int bnxt_queue_mem_alloc(struct net_device *dev, 16355 struct netdev_queue_config *qcfg, 16356 void *qmem, int idx) 16357 { 16358 struct bnxt_rx_ring_info *rxr, *clone; 16359 struct bnxt *bp = netdev_priv(dev); 16360 struct bnxt_ring_struct *ring; 16361 int rc; 16362 16363 if (!bp->rx_ring) 16364 return -ENETDOWN; 16365 16366 rxr = &bp->rx_ring[idx]; 16367 clone = qmem; 16368 memcpy(clone, rxr, sizeof(*rxr)); 16369 bnxt_init_rx_ring_struct(bp, clone); 16370 bnxt_reset_rx_ring_struct(bp, clone); 16371 16372 clone->rx_prod = 0; 16373 clone->rx_agg_prod = 0; 16374 clone->rx_sw_agg_prod = 0; 16375 clone->rx_next_cons = 0; 16376 clone->need_head_pool = false; 16377 clone->rx_page_size = qcfg->rx_page_size; 16378 clone->rx_agg_bmap = NULL; 16379 clone->rx_tpa = NULL; 16380 clone->rx_tpa_idx_map = NULL; 16381 16382 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 16383 if (rc) 16384 return rc; 16385 16386 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 16387 if (rc < 0) 16388 goto err_page_pool_destroy; 16389 16390 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 16391 MEM_TYPE_PAGE_POOL, 16392 clone->page_pool); 16393 if (rc) 16394 goto err_rxq_info_unreg; 16395 16396 ring = &clone->rx_ring_struct; 16397 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 16398 if (rc) 16399 goto err_free_rx_ring; 16400 16401 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 16402 ring = &clone->rx_agg_ring_struct; 16403 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 16404 if (rc) 16405 goto err_free_rx_agg_ring; 16406 16407 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 16408 if (rc) 16409 goto err_free_rx_agg_ring; 16410 } 16411 16412 if (bp->flags & BNXT_FLAG_TPA) { 16413 rc = bnxt_alloc_one_tpa_info(bp, clone); 16414 if (rc) 16415 goto err_free_tpa_info; 16416 } 16417 16418 bnxt_init_one_rx_ring_rxbd(bp, clone); 16419 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 16420 16421 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 16422 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16423 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 16424 if (bp->flags & BNXT_FLAG_TPA) { 16425 rc = bnxt_alloc_one_tpa_info_data(bp, clone); 16426 if (rc) 16427 goto err_free_rx_ring_skbs; 16428 } 16429 16430 return 0; 16431 16432 err_free_rx_ring_skbs: 16433 bnxt_free_one_rx_ring_skbs(bp, clone); 16434 err_free_tpa_info: 16435 bnxt_free_one_tpa_info(bp, clone); 16436 err_free_rx_agg_ring: 16437 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 16438 kfree(clone->rx_agg_bmap); 16439 clone->rx_agg_bmap = NULL; 16440 err_free_rx_ring: 16441 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 16442 err_rxq_info_unreg: 16443 xdp_rxq_info_unreg(&clone->xdp_rxq); 16444 err_page_pool_destroy: 16445 page_pool_destroy(clone->page_pool); 16446 page_pool_destroy(clone->head_pool); 16447 clone->page_pool = NULL; 16448 clone->head_pool = NULL; 16449 return rc; 16450 } 16451 16452 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 16453 { 16454 struct bnxt_rx_ring_info *rxr = qmem; 16455 struct bnxt *bp = netdev_priv(dev); 16456 struct bnxt_ring_struct *ring; 16457 16458 bnxt_free_one_rx_ring_skbs(bp, rxr); 16459 bnxt_free_one_tpa_info(bp, rxr); 16460 16461 xdp_rxq_info_unreg(&rxr->xdp_rxq); 16462 16463 page_pool_destroy(rxr->page_pool); 16464 page_pool_destroy(rxr->head_pool); 16465 rxr->page_pool = NULL; 16466 rxr->head_pool = NULL; 16467 16468 ring = &rxr->rx_ring_struct; 16469 bnxt_free_ring(bp, &ring->ring_mem); 16470 16471 ring = &rxr->rx_agg_ring_struct; 16472 bnxt_free_ring(bp, &ring->ring_mem); 16473 16474 kfree(rxr->rx_agg_bmap); 16475 rxr->rx_agg_bmap = NULL; 16476 } 16477 16478 static void bnxt_copy_rx_ring(struct bnxt *bp, 16479 struct bnxt_rx_ring_info *dst, 16480 struct bnxt_rx_ring_info *src) 16481 { 16482 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 16483 struct bnxt_ring_struct *dst_ring, *src_ring; 16484 int i; 16485 16486 dst_ring = &dst->rx_ring_struct; 16487 dst_rmem = &dst_ring->ring_mem; 16488 src_ring = &src->rx_ring_struct; 16489 src_rmem = &src_ring->ring_mem; 16490 16491 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16492 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16493 WARN_ON(dst_rmem->flags != src_rmem->flags); 16494 WARN_ON(dst_rmem->depth != src_rmem->depth); 16495 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16496 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16497 16498 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16499 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16500 *dst_rmem->vmem = *src_rmem->vmem; 16501 for (i = 0; i < dst_rmem->nr_pages; i++) { 16502 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16503 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16504 } 16505 16506 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 16507 return; 16508 16509 dst_ring = &dst->rx_agg_ring_struct; 16510 dst_rmem = &dst_ring->ring_mem; 16511 src_ring = &src->rx_agg_ring_struct; 16512 src_rmem = &src_ring->ring_mem; 16513 16514 dst->rx_page_size = src->rx_page_size; 16515 16516 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16517 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16518 WARN_ON(dst_rmem->flags != src_rmem->flags); 16519 WARN_ON(dst_rmem->depth != src_rmem->depth); 16520 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16521 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16522 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 16523 16524 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16525 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16526 *dst_rmem->vmem = *src_rmem->vmem; 16527 for (i = 0; i < dst_rmem->nr_pages; i++) { 16528 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16529 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16530 } 16531 16532 dst->rx_agg_bmap = src->rx_agg_bmap; 16533 } 16534 16535 static int bnxt_queue_start(struct net_device *dev, 16536 struct netdev_queue_config *qcfg, 16537 void *qmem, int idx) 16538 { 16539 struct bnxt *bp = netdev_priv(dev); 16540 struct bnxt_rx_ring_info *rxr, *clone; 16541 struct bnxt_cp_ring_info *cpr; 16542 struct bnxt_vnic_info *vnic; 16543 struct bnxt_napi *bnapi; 16544 int i, rc; 16545 u16 mru; 16546 16547 rxr = &bp->rx_ring[idx]; 16548 clone = qmem; 16549 16550 rxr->rx_prod = clone->rx_prod; 16551 rxr->rx_agg_prod = clone->rx_agg_prod; 16552 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 16553 rxr->rx_next_cons = clone->rx_next_cons; 16554 rxr->rx_tpa = clone->rx_tpa; 16555 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 16556 rxr->page_pool = clone->page_pool; 16557 rxr->head_pool = clone->head_pool; 16558 rxr->xdp_rxq = clone->xdp_rxq; 16559 rxr->need_head_pool = clone->need_head_pool; 16560 16561 bnxt_copy_rx_ring(bp, rxr, clone); 16562 16563 bnapi = rxr->bnapi; 16564 cpr = &bnapi->cp_ring; 16565 16566 /* All rings have been reserved and previously allocated. 16567 * Reallocating with the same parameters should never fail. 16568 */ 16569 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16570 if (rc) 16571 goto err_reset; 16572 16573 if (bp->tph_mode) { 16574 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16575 if (rc) 16576 goto err_reset; 16577 } 16578 16579 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16580 if (rc) 16581 goto err_reset; 16582 16583 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16584 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16585 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16586 16587 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16588 rc = bnxt_tx_queue_start(bp, idx); 16589 if (rc) 16590 goto err_reset; 16591 } 16592 16593 bnxt_enable_rx_page_pool(rxr); 16594 napi_enable_locked(&bnapi->napi); 16595 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16596 16597 mru = bp->dev->mtu + VLAN_ETH_HLEN; 16598 for (i = 0; i < bp->nr_vnics; i++) { 16599 vnic = &bp->vnic_info[i]; 16600 16601 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16602 if (rc) 16603 return rc; 16604 } 16605 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16606 16607 err_reset: 16608 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16609 rc); 16610 napi_enable_locked(&bnapi->napi); 16611 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16612 netif_close(dev); 16613 return rc; 16614 } 16615 16616 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16617 { 16618 struct bnxt *bp = netdev_priv(dev); 16619 struct bnxt_rx_ring_info *rxr; 16620 struct bnxt_cp_ring_info *cpr; 16621 struct bnxt_vnic_info *vnic; 16622 struct bnxt_napi *bnapi; 16623 int i; 16624 16625 for (i = 0; i < bp->nr_vnics; i++) { 16626 vnic = &bp->vnic_info[i]; 16627 16628 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16629 } 16630 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16631 /* Make sure NAPI sees that the VNIC is disabled */ 16632 synchronize_net(); 16633 rxr = &bp->rx_ring[idx]; 16634 bnapi = rxr->bnapi; 16635 cpr = &bnapi->cp_ring; 16636 cancel_work_sync(&cpr->dim.work); 16637 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16638 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16639 page_pool_disable_direct_recycling(rxr->page_pool); 16640 if (bnxt_separate_head_pool(rxr)) 16641 page_pool_disable_direct_recycling(rxr->head_pool); 16642 16643 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16644 bnxt_tx_queue_stop(bp, idx); 16645 16646 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16647 * completion is handled in NAPI to guarantee no more DMA on that ring 16648 * after seeing the completion. 16649 */ 16650 napi_disable_locked(&bnapi->napi); 16651 16652 if (bp->tph_mode) { 16653 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16654 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16655 } 16656 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16657 16658 memcpy(qmem, rxr, sizeof(*rxr)); 16659 bnxt_init_rx_ring_struct(bp, qmem); 16660 16661 return 0; 16662 } 16663 16664 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16665 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16666 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16667 .ndo_queue_mem_free = bnxt_queue_mem_free, 16668 .ndo_queue_start = bnxt_queue_start, 16669 .ndo_queue_stop = bnxt_queue_stop, 16670 .ndo_default_qcfg = bnxt_queue_default_qcfg, 16671 .ndo_validate_qcfg = bnxt_validate_qcfg, 16672 .supported_params = QCFG_RX_PAGE_SIZE, 16673 }; 16674 16675 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = { 16676 .ndo_default_qcfg = bnxt_queue_default_qcfg, 16677 }; 16678 16679 static void bnxt_remove_one(struct pci_dev *pdev) 16680 { 16681 struct net_device *dev = pci_get_drvdata(pdev); 16682 struct bnxt *bp = netdev_priv(dev); 16683 16684 if (BNXT_PF(bp)) 16685 __bnxt_sriov_disable(bp); 16686 16687 bnxt_aux_devices_del(bp); 16688 16689 unregister_netdev(dev); 16690 bnxt_ptp_clear(bp); 16691 16692 bnxt_aux_devices_uninit(bp); 16693 bnxt_auxdev_id_free(bp, bp->auxdev_id); 16694 16695 bnxt_free_l2_filters(bp, true); 16696 bnxt_free_ntp_fltrs(bp, true); 16697 WARN_ON(bp->num_rss_ctx); 16698 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16699 /* Flush any pending tasks */ 16700 cancel_work_sync(&bp->sp_task); 16701 cancel_delayed_work_sync(&bp->fw_reset_task); 16702 bp->sp_event = 0; 16703 16704 bnxt_dl_fw_reporters_destroy(bp); 16705 bnxt_dl_unregister(bp); 16706 bnxt_shutdown_tc(bp); 16707 16708 bnxt_clear_int_mode(bp); 16709 bnxt_free_ring_cpu_masks(bp); 16710 bnxt_hwrm_func_drv_unrgtr(bp); 16711 bnxt_free_hwrm_resources(bp); 16712 bnxt_hwmon_uninit(bp); 16713 bnxt_ethtool_free(bp); 16714 bnxt_dcb_free(bp); 16715 kfree(bp->ptp_cfg); 16716 bp->ptp_cfg = NULL; 16717 kfree(bp->fw_health); 16718 bp->fw_health = NULL; 16719 bnxt_cleanup_pci(bp); 16720 bnxt_free_ctx_mem(bp, true); 16721 bnxt_free_crash_dump_mem(bp); 16722 kfree(bp->rss_indir_tbl); 16723 bp->rss_indir_tbl = NULL; 16724 bnxt_free_port_stats(bp); 16725 free_netdev(dev); 16726 } 16727 16728 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16729 { 16730 int rc = 0; 16731 struct bnxt_link_info *link_info = &bp->link_info; 16732 16733 bp->phy_flags = 0; 16734 rc = bnxt_hwrm_phy_qcaps(bp); 16735 if (rc) { 16736 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16737 rc); 16738 return rc; 16739 } 16740 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16741 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16742 else 16743 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16744 16745 bp->mac_flags = 0; 16746 bnxt_hwrm_mac_qcaps(bp); 16747 16748 if (!fw_dflt) 16749 return 0; 16750 16751 mutex_lock(&bp->link_lock); 16752 rc = bnxt_update_link(bp, false); 16753 if (rc) { 16754 mutex_unlock(&bp->link_lock); 16755 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16756 rc); 16757 return rc; 16758 } 16759 16760 /* Older firmware does not have supported_auto_speeds, so assume 16761 * that all supported speeds can be autonegotiated. 16762 */ 16763 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16764 link_info->support_auto_speeds = link_info->support_speeds; 16765 16766 bnxt_init_ethtool_link_settings(bp); 16767 mutex_unlock(&bp->link_lock); 16768 return 0; 16769 } 16770 16771 static int bnxt_get_max_irq(struct pci_dev *pdev) 16772 { 16773 u16 ctrl; 16774 16775 if (!pdev->msix_cap) 16776 return 1; 16777 16778 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16779 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16780 } 16781 16782 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16783 int *max_cp) 16784 { 16785 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16786 int max_ring_grps = 0, max_irq; 16787 16788 *max_tx = hw_resc->max_tx_rings; 16789 *max_rx = hw_resc->max_rx_rings; 16790 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16791 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16792 bnxt_get_ulp_msix_num_in_use(bp), 16793 hw_resc->max_stat_ctxs - 16794 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16795 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16796 *max_cp = min_t(int, *max_cp, max_irq); 16797 max_ring_grps = hw_resc->max_hw_ring_grps; 16798 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16799 *max_cp -= 1; 16800 *max_rx -= 2; 16801 } 16802 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16803 *max_rx >>= 1; 16804 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16805 int rc; 16806 16807 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16808 if (rc) { 16809 *max_rx = 0; 16810 *max_tx = 0; 16811 } 16812 /* On P5 chips, max_cp output param should be available NQs */ 16813 *max_cp = max_irq; 16814 } 16815 *max_rx = min_t(int, *max_rx, max_ring_grps); 16816 } 16817 16818 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16819 { 16820 int rx, tx, cp; 16821 16822 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16823 *max_rx = rx; 16824 *max_tx = tx; 16825 if (!rx || !tx || !cp) 16826 return -ENOMEM; 16827 16828 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16829 } 16830 16831 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16832 bool shared) 16833 { 16834 int rc; 16835 16836 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16837 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16838 /* Not enough rings, try disabling agg rings. */ 16839 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16840 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16841 if (rc) { 16842 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16843 bp->flags |= BNXT_FLAG_AGG_RINGS; 16844 return rc; 16845 } 16846 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16847 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16848 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16849 bnxt_set_ring_params(bp); 16850 } 16851 16852 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16853 int max_cp, max_stat, max_irq; 16854 16855 /* Reserve minimum resources for RoCE */ 16856 max_cp = bnxt_get_max_func_cp_rings(bp); 16857 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16858 max_irq = bnxt_get_max_func_irqs(bp); 16859 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16860 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16861 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16862 return 0; 16863 16864 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16865 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16866 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16867 max_cp = min_t(int, max_cp, max_irq); 16868 max_cp = min_t(int, max_cp, max_stat); 16869 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16870 if (rc) 16871 rc = 0; 16872 } 16873 return rc; 16874 } 16875 16876 /* In initial default shared ring setting, each shared ring must have a 16877 * RX/TX ring pair. 16878 */ 16879 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16880 { 16881 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16882 bp->rx_nr_rings = bp->cp_nr_rings; 16883 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16884 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16885 } 16886 16887 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh) 16888 { 16889 if (sh) 16890 bnxt_trim_dflt_sh_rings(bp); 16891 else 16892 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16893 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16894 if (sh && READ_ONCE(bp->xdp_prog)) { 16895 bnxt_set_xdp_tx_rings(bp); 16896 bnxt_set_cp_rings(bp, true); 16897 } 16898 } 16899 16900 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16901 { 16902 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16903 int avail_msix; 16904 16905 if (!bnxt_can_reserve_rings(bp)) 16906 return 0; 16907 16908 if (sh) 16909 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16910 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16911 /* Reduce default rings on multi-port cards so that total default 16912 * rings do not exceed CPU count. 16913 */ 16914 if (bp->port_count > 1) { 16915 int max_rings = 16916 max_t(int, num_online_cpus() / bp->port_count, 1); 16917 16918 dflt_rings = min_t(int, dflt_rings, max_rings); 16919 } 16920 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16921 if (rc) 16922 return rc; 16923 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16924 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16925 16926 bnxt_adj_dflt_rings(bp, sh); 16927 16928 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16929 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16930 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16931 16932 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16933 bnxt_set_dflt_ulp_stat_ctxs(bp); 16934 } 16935 16936 rc = __bnxt_reserve_rings(bp); 16937 if (rc && rc != -ENODEV) 16938 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16939 16940 bnxt_adj_tx_rings(bp); 16941 if (sh) 16942 bnxt_adj_dflt_rings(bp, true); 16943 16944 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16945 if (bnxt_need_reserve_rings(bp)) { 16946 rc = __bnxt_reserve_rings(bp); 16947 if (rc && rc != -ENODEV) 16948 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16949 bnxt_adj_tx_rings(bp); 16950 } 16951 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16952 bp->rx_nr_rings++; 16953 bp->cp_nr_rings++; 16954 } 16955 if (rc) { 16956 bp->tx_nr_rings = 0; 16957 bp->rx_nr_rings = 0; 16958 } 16959 return rc; 16960 } 16961 16962 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16963 { 16964 int rc; 16965 16966 if (bp->tx_nr_rings) 16967 return 0; 16968 16969 bnxt_ulp_irq_stop(bp); 16970 bnxt_clear_int_mode(bp); 16971 rc = bnxt_set_dflt_rings(bp, true); 16972 if (rc) { 16973 if (BNXT_VF(bp) && rc == -ENODEV) 16974 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16975 else 16976 netdev_err(bp->dev, "Not enough rings available.\n"); 16977 goto init_dflt_ring_err; 16978 } 16979 rc = bnxt_init_int_mode(bp); 16980 if (rc) 16981 goto init_dflt_ring_err; 16982 16983 bnxt_adj_tx_rings(bp); 16984 16985 bnxt_set_dflt_rfs(bp); 16986 16987 init_dflt_ring_err: 16988 bnxt_ulp_irq_restart(bp, rc); 16989 return rc; 16990 } 16991 16992 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16993 { 16994 int rc; 16995 16996 netdev_assert_locked_ops_compat(bp->dev); 16997 bnxt_hwrm_func_qcaps(bp); 16998 16999 if (netif_running(bp->dev)) 17000 __bnxt_close_nic(bp, true, false); 17001 17002 bnxt_ulp_irq_stop(bp); 17003 bnxt_clear_int_mode(bp); 17004 rc = bnxt_init_int_mode(bp); 17005 bnxt_ulp_irq_restart(bp, rc); 17006 17007 if (netif_running(bp->dev)) { 17008 if (rc) 17009 netif_close(bp->dev); 17010 else 17011 rc = bnxt_open_nic(bp, true, false); 17012 } 17013 17014 return rc; 17015 } 17016 17017 static int bnxt_init_mac_addr(struct bnxt *bp) 17018 { 17019 int rc = 0; 17020 17021 if (BNXT_PF(bp)) { 17022 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 17023 } else { 17024 #ifdef CONFIG_BNXT_SRIOV 17025 struct bnxt_vf_info *vf = &bp->vf; 17026 bool strict_approval = true; 17027 17028 if (is_valid_ether_addr(vf->mac_addr)) { 17029 /* overwrite netdev dev_addr with admin VF MAC */ 17030 eth_hw_addr_set(bp->dev, vf->mac_addr); 17031 /* Older PF driver or firmware may not approve this 17032 * correctly. 17033 */ 17034 strict_approval = false; 17035 } else { 17036 eth_hw_addr_random(bp->dev); 17037 } 17038 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 17039 #endif 17040 } 17041 return rc; 17042 } 17043 17044 static void bnxt_vpd_read_info(struct bnxt *bp) 17045 { 17046 struct pci_dev *pdev = bp->pdev; 17047 unsigned int vpd_size, kw_len; 17048 int pos, size; 17049 u8 *vpd_data; 17050 17051 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 17052 if (IS_ERR(vpd_data)) { 17053 pci_warn(pdev, "Unable to read VPD\n"); 17054 return; 17055 } 17056 17057 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 17058 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 17059 if (pos < 0) 17060 goto read_sn; 17061 17062 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 17063 memcpy(bp->board_partno, &vpd_data[pos], size); 17064 17065 read_sn: 17066 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 17067 PCI_VPD_RO_KEYWORD_SERIALNO, 17068 &kw_len); 17069 if (pos < 0) 17070 goto exit; 17071 17072 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 17073 memcpy(bp->board_serialno, &vpd_data[pos], size); 17074 exit: 17075 kfree(vpd_data); 17076 } 17077 17078 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 17079 { 17080 struct pci_dev *pdev = bp->pdev; 17081 u64 qword; 17082 17083 qword = pci_get_dsn(pdev); 17084 if (!qword) { 17085 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 17086 return -EOPNOTSUPP; 17087 } 17088 17089 put_unaligned_le64(qword, dsn); 17090 17091 bp->flags |= BNXT_FLAG_DSN_VALID; 17092 return 0; 17093 } 17094 17095 static int bnxt_map_db_bar(struct bnxt *bp) 17096 { 17097 if (!bp->db_size) 17098 return -ENODEV; 17099 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 17100 if (!bp->bar1) 17101 return -ENOMEM; 17102 return 0; 17103 } 17104 17105 void bnxt_print_device_info(struct bnxt *bp) 17106 { 17107 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 17108 board_info[bp->board_idx].name, 17109 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 17110 17111 pcie_print_link_status(bp->pdev); 17112 } 17113 17114 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 17115 { 17116 struct bnxt_hw_resc *hw_resc; 17117 struct net_device *dev; 17118 struct bnxt *bp; 17119 int rc, max_irqs; 17120 17121 if (pci_is_bridge(pdev)) 17122 return -ENODEV; 17123 17124 if (!pdev->msix_cap) { 17125 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 17126 return -ENODEV; 17127 } 17128 17129 /* Clear any pending DMA transactions from crash kernel 17130 * while loading driver in capture kernel. 17131 */ 17132 if (is_kdump_kernel()) { 17133 pci_clear_master(pdev); 17134 pcie_flr(pdev); 17135 } 17136 17137 max_irqs = bnxt_get_max_irq(pdev); 17138 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 17139 max_irqs); 17140 if (!dev) 17141 return -ENOMEM; 17142 17143 bp = netdev_priv(dev); 17144 bp->board_idx = ent->driver_data; 17145 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 17146 bnxt_set_max_func_irqs(bp, max_irqs); 17147 17148 bp->max_irqs = max_irqs; 17149 rc = bnxt_alloc_ring_cpu_masks(bp); 17150 if (rc) 17151 goto init_err_free; 17152 17153 if (bnxt_vf_pciid(bp->board_idx)) 17154 bp->flags |= BNXT_FLAG_VF; 17155 17156 /* No devlink port registration in case of a VF */ 17157 if (BNXT_PF(bp)) 17158 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 17159 17160 rc = bnxt_init_board(pdev, dev); 17161 if (rc < 0) 17162 goto init_err_free; 17163 17164 dev->netdev_ops = &bnxt_netdev_ops; 17165 dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops; 17166 dev->stat_ops = &bnxt_stat_ops; 17167 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 17168 dev->ethtool_ops = &bnxt_ethtool_ops; 17169 pci_set_drvdata(pdev, dev); 17170 17171 rc = bnxt_alloc_hwrm_resources(bp); 17172 if (rc) 17173 goto init_err_pci_clean; 17174 17175 mutex_init(&bp->hwrm_cmd_lock); 17176 mutex_init(&bp->link_lock); 17177 17178 rc = bnxt_fw_init_one_p1(bp); 17179 if (rc) 17180 goto init_err_pci_clean; 17181 17182 if (BNXT_PF(bp)) 17183 bnxt_vpd_read_info(bp); 17184 17185 if (BNXT_CHIP_P5_PLUS(bp)) { 17186 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 17187 if (BNXT_CHIP_P7(bp)) 17188 bp->flags |= BNXT_FLAG_CHIP_P7; 17189 } 17190 17191 rc = bnxt_alloc_rss_indir_tbl(bp); 17192 if (rc) 17193 goto init_err_pci_clean; 17194 17195 rc = bnxt_fw_init_one_p2(bp); 17196 if (rc) 17197 goto init_err_pci_clean; 17198 17199 rc = bnxt_map_db_bar(bp); 17200 if (rc) { 17201 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 17202 rc); 17203 goto init_err_pci_clean; 17204 } 17205 17206 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 17207 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN | 17208 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 17209 NETIF_F_GSO_IPXIP4 | 17210 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 17211 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 17212 NETIF_F_RXCSUM | NETIF_F_GRO; 17213 dev->hw_features |= NETIF_F_GSO_UDP_L4; 17214 17215 if (BNXT_SUPPORTS_TPA(bp)) 17216 dev->hw_features |= NETIF_F_LRO; 17217 17218 dev->hw_enc_features = 17219 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 17220 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN | 17221 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 17222 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 17223 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 17224 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 17225 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 17226 if (bp->flags & BNXT_FLAG_CHIP_P7) 17227 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 17228 else 17229 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 17230 17231 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 17232 NETIF_F_GSO_GRE_CSUM; 17233 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 17234 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 17235 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 17236 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 17237 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 17238 if (BNXT_SUPPORTS_TPA(bp)) 17239 dev->hw_features |= NETIF_F_GRO_HW; 17240 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 17241 if (dev->features & NETIF_F_GRO_HW) 17242 dev->features &= ~NETIF_F_LRO; 17243 dev->priv_flags |= IFF_UNICAST_FLT; 17244 17245 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 17246 if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) { 17247 u16 max_segs = BNXT_SW_USO_MAX_SEGS; 17248 17249 if (bp->tso_max_segs) 17250 max_segs = min_t(u16, max_segs, bp->tso_max_segs); 17251 netif_set_tso_max_segs(dev, max_segs); 17252 } else if (bp->tso_max_segs) { 17253 netif_set_tso_max_segs(dev, bp->tso_max_segs); 17254 } 17255 17256 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 17257 NETDEV_XDP_ACT_RX_SG; 17258 17259 #ifdef CONFIG_BNXT_SRIOV 17260 init_waitqueue_head(&bp->sriov_cfg_wait); 17261 #endif 17262 if (BNXT_SUPPORTS_TPA(bp)) { 17263 bp->gro_func = bnxt_gro_func_5730x; 17264 if (BNXT_CHIP_P4(bp)) 17265 bp->gro_func = bnxt_gro_func_5731x; 17266 else if (BNXT_CHIP_P5_PLUS(bp)) 17267 bp->gro_func = bnxt_gro_func_5750x; 17268 } 17269 if (!BNXT_CHIP_P4_PLUS(bp)) 17270 bp->flags |= BNXT_FLAG_DOUBLE_DB; 17271 17272 rc = bnxt_init_mac_addr(bp); 17273 if (rc) { 17274 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 17275 rc = -EADDRNOTAVAIL; 17276 goto init_err_pci_clean; 17277 } 17278 17279 if (BNXT_PF(bp)) { 17280 /* Read the adapter's DSN to use as the eswitch switch_id */ 17281 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 17282 } 17283 17284 /* MTU range: 60 - FW defined max */ 17285 dev->min_mtu = ETH_ZLEN; 17286 dev->max_mtu = bp->max_mtu; 17287 17288 rc = bnxt_probe_phy(bp, true); 17289 if (rc) 17290 goto init_err_pci_clean; 17291 17292 hw_resc = &bp->hw_resc; 17293 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 17294 BNXT_L2_FLTR_MAX_FLTR; 17295 /* Older firmware may not report these filters properly */ 17296 if (bp->max_fltr < BNXT_MAX_FLTR) 17297 bp->max_fltr = BNXT_MAX_FLTR; 17298 bnxt_init_l2_fltr_tbl(bp); 17299 __bnxt_set_rx_skb_mode(bp, false); 17300 bnxt_set_tpa_flags(bp); 17301 bnxt_init_ring_params(bp); 17302 bnxt_set_ring_params(bp); 17303 mutex_init(&bp->auxdev_lock); 17304 if (!bnxt_auxdev_id_alloc(bp)) 17305 bnxt_aux_devices_init(bp); 17306 rc = bnxt_set_dflt_rings(bp, true); 17307 if (rc) { 17308 if (BNXT_VF(bp) && rc == -ENODEV) { 17309 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 17310 } else { 17311 netdev_err(bp->dev, "Not enough rings available.\n"); 17312 rc = -ENOMEM; 17313 } 17314 goto init_err_pci_clean; 17315 } 17316 17317 bnxt_fw_init_one_p3(bp); 17318 17319 bnxt_init_dflt_coal(bp); 17320 17321 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 17322 bp->flags |= BNXT_FLAG_STRIP_VLAN; 17323 17324 rc = bnxt_init_int_mode(bp); 17325 if (rc) 17326 goto init_err_pci_clean; 17327 17328 /* No TC has been set yet and rings may have been trimmed due to 17329 * limited MSIX, so we re-initialize the TX rings per TC. 17330 */ 17331 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 17332 17333 if (BNXT_PF(bp)) { 17334 if (!bnxt_pf_wq) { 17335 bnxt_pf_wq = 17336 create_singlethread_workqueue("bnxt_pf_wq"); 17337 if (!bnxt_pf_wq) { 17338 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 17339 rc = -ENOMEM; 17340 goto init_err_pci_clean; 17341 } 17342 } 17343 rc = bnxt_init_tc(bp); 17344 if (rc) 17345 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 17346 rc); 17347 } 17348 17349 bnxt_inv_fw_health_reg(bp); 17350 rc = bnxt_dl_register(bp); 17351 if (rc) 17352 goto init_err_dl; 17353 17354 INIT_LIST_HEAD(&bp->usr_fltr_list); 17355 17356 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 17357 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 17358 17359 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp; 17360 if (BNXT_SUPPORTS_QUEUE_API(bp)) 17361 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 17362 dev->netmem_tx = NETMEM_TX_DMA; 17363 17364 rc = register_netdev(dev); 17365 if (rc) 17366 goto init_err_cleanup; 17367 17368 bnxt_dl_fw_reporters_create(bp); 17369 17370 bnxt_aux_devices_add(bp); 17371 17372 bnxt_print_device_info(bp); 17373 17374 pci_save_state(pdev); 17375 17376 return 0; 17377 init_err_cleanup: 17378 bnxt_aux_devices_uninit(bp); 17379 bnxt_auxdev_id_free(bp, bp->auxdev_id); 17380 bnxt_dl_unregister(bp); 17381 init_err_dl: 17382 bnxt_shutdown_tc(bp); 17383 bnxt_clear_int_mode(bp); 17384 17385 init_err_pci_clean: 17386 bnxt_hwrm_func_drv_unrgtr(bp); 17387 bnxt_ptp_clear(bp); 17388 kfree(bp->ptp_cfg); 17389 bp->ptp_cfg = NULL; 17390 bnxt_free_hwrm_resources(bp); 17391 bnxt_hwmon_uninit(bp); 17392 bnxt_ethtool_free(bp); 17393 kfree(bp->fw_health); 17394 bp->fw_health = NULL; 17395 bnxt_cleanup_pci(bp); 17396 bnxt_free_ctx_mem(bp, true); 17397 bnxt_free_crash_dump_mem(bp); 17398 kfree(bp->rss_indir_tbl); 17399 bp->rss_indir_tbl = NULL; 17400 17401 init_err_free: 17402 bnxt_free_ring_cpu_masks(bp); 17403 free_netdev(dev); 17404 return rc; 17405 } 17406 17407 static void bnxt_shutdown(struct pci_dev *pdev) 17408 { 17409 struct net_device *dev = pci_get_drvdata(pdev); 17410 struct bnxt *bp; 17411 17412 if (!dev) 17413 return; 17414 17415 rtnl_lock(); 17416 netdev_lock(dev); 17417 bp = netdev_priv(dev); 17418 if (!bp) 17419 goto shutdown_exit; 17420 17421 if (netif_running(dev)) 17422 netif_close(dev); 17423 17424 if (bnxt_hwrm_func_drv_unrgtr(bp)) { 17425 pcie_flr(pdev); 17426 goto shutdown_exit; 17427 } 17428 bnxt_ptp_clear(bp); 17429 bnxt_clear_int_mode(bp); 17430 pci_disable_device(pdev); 17431 17432 if (system_state == SYSTEM_POWER_OFF) { 17433 pci_wake_from_d3(pdev, bp->wol); 17434 pci_set_power_state(pdev, PCI_D3hot); 17435 } 17436 17437 shutdown_exit: 17438 netdev_unlock(dev); 17439 rtnl_unlock(); 17440 } 17441 17442 #ifdef CONFIG_PM_SLEEP 17443 static int bnxt_suspend(struct device *device) 17444 { 17445 struct net_device *dev = dev_get_drvdata(device); 17446 struct bnxt *bp = netdev_priv(dev); 17447 int rc = 0; 17448 17449 bnxt_ulp_stop(bp); 17450 17451 netdev_lock(dev); 17452 if (netif_running(dev)) { 17453 netif_device_detach(dev); 17454 rc = bnxt_close(dev); 17455 } 17456 bnxt_hwrm_func_drv_unrgtr(bp); 17457 bnxt_ptp_clear(bp); 17458 pci_disable_device(bp->pdev); 17459 bnxt_free_ctx_mem(bp, false); 17460 netdev_unlock(dev); 17461 return rc; 17462 } 17463 17464 static int bnxt_resume(struct device *device) 17465 { 17466 struct net_device *dev = dev_get_drvdata(device); 17467 struct bnxt *bp = netdev_priv(dev); 17468 int rc = 0; 17469 17470 rtnl_lock(); 17471 netdev_lock(dev); 17472 rc = pci_enable_device(bp->pdev); 17473 if (rc) { 17474 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 17475 rc); 17476 goto resume_exit; 17477 } 17478 pci_set_master(bp->pdev); 17479 if (bnxt_hwrm_ver_get(bp)) { 17480 rc = -ENODEV; 17481 goto resume_exit; 17482 } 17483 rc = bnxt_hwrm_func_reset(bp); 17484 if (rc) { 17485 rc = -EBUSY; 17486 goto resume_exit; 17487 } 17488 17489 rc = bnxt_hwrm_func_qcaps(bp); 17490 if (rc) 17491 goto resume_exit; 17492 17493 bnxt_clear_reservations(bp, true); 17494 17495 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 17496 rc = -ENODEV; 17497 goto resume_exit; 17498 } 17499 if (bp->fw_crash_mem) 17500 bnxt_hwrm_crash_dump_mem_cfg(bp); 17501 17502 if (bnxt_ptp_init(bp)) { 17503 kfree(bp->ptp_cfg); 17504 bp->ptp_cfg = NULL; 17505 } 17506 bnxt_get_wol_settings(bp); 17507 if (netif_running(dev)) { 17508 rc = bnxt_open(dev); 17509 if (!rc) 17510 netif_device_attach(dev); 17511 } 17512 17513 resume_exit: 17514 netdev_unlock(bp->dev); 17515 rtnl_unlock(); 17516 if (!rc) { 17517 bnxt_ulp_start(bp); 17518 bnxt_reenable_sriov(bp); 17519 } 17520 return rc; 17521 } 17522 17523 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 17524 #define BNXT_PM_OPS (&bnxt_pm_ops) 17525 17526 #else 17527 17528 #define BNXT_PM_OPS NULL 17529 17530 #endif /* CONFIG_PM_SLEEP */ 17531 17532 /** 17533 * bnxt_io_error_detected - called when PCI error is detected 17534 * @pdev: Pointer to PCI device 17535 * @state: The current pci connection state 17536 * 17537 * This function is called after a PCI bus error affecting 17538 * this device has been detected. 17539 */ 17540 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 17541 pci_channel_state_t state) 17542 { 17543 struct net_device *netdev = pci_get_drvdata(pdev); 17544 struct bnxt *bp = netdev_priv(netdev); 17545 bool abort = false; 17546 17547 netdev_info(netdev, "PCI I/O error detected\n"); 17548 17549 bnxt_ulp_stop(bp); 17550 17551 netdev_lock(netdev); 17552 netif_device_detach(netdev); 17553 17554 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 17555 netdev_err(bp->dev, "Firmware reset already in progress\n"); 17556 abort = true; 17557 } 17558 17559 if (abort || state == pci_channel_io_perm_failure) { 17560 netdev_unlock(netdev); 17561 return PCI_ERS_RESULT_DISCONNECT; 17562 } 17563 17564 /* Link is not reliable anymore if state is pci_channel_io_frozen 17565 * so we disable bus master to prevent any potential bad DMAs before 17566 * freeing kernel memory. 17567 */ 17568 if (state == pci_channel_io_frozen) { 17569 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 17570 bnxt_fw_fatal_close(bp); 17571 } 17572 17573 if (netif_running(netdev)) 17574 __bnxt_close_nic(bp, true, true); 17575 17576 if (pci_is_enabled(pdev)) 17577 pci_disable_device(pdev); 17578 bnxt_free_ctx_mem(bp, false); 17579 netdev_unlock(netdev); 17580 17581 /* Request a slot reset. */ 17582 return PCI_ERS_RESULT_NEED_RESET; 17583 } 17584 17585 /** 17586 * bnxt_io_slot_reset - called after the pci bus has been reset. 17587 * @pdev: Pointer to PCI device 17588 * 17589 * Restart the card from scratch, as if from a cold-boot. 17590 * At this point, the card has experienced a hard reset, 17591 * followed by fixups by BIOS, and has its config space 17592 * set up identically to what it was at cold boot. 17593 */ 17594 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 17595 { 17596 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 17597 struct net_device *netdev = pci_get_drvdata(pdev); 17598 struct bnxt *bp = netdev_priv(netdev); 17599 int retry = 0; 17600 int err = 0; 17601 int off; 17602 17603 netdev_info(bp->dev, "PCI Slot Reset\n"); 17604 17605 if (test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) { 17606 /* After DPC, the chip should return CRS when the vendor ID 17607 * config register is read until it is ready. On all chips, 17608 * this is not happening reliably so add a 5-second delay as a 17609 * workaround. 17610 */ 17611 msleep(5000); 17612 } 17613 17614 netdev_lock(netdev); 17615 17616 if (pci_enable_device(pdev)) { 17617 dev_err(&pdev->dev, 17618 "Cannot re-enable PCI device after reset.\n"); 17619 } else { 17620 pci_set_master(pdev); 17621 /* Upon fatal error, our device internal logic that latches to 17622 * BAR value is getting reset and will restore only upon 17623 * rewriting the BARs. 17624 * 17625 * As pci_restore_state() does not re-write the BARs if the 17626 * value is same as saved value earlier, driver needs to 17627 * write the BARs to 0 to force restore, in case of fatal error. 17628 */ 17629 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17630 &bp->state)) { 17631 for (off = PCI_BASE_ADDRESS_0; 17632 off <= PCI_BASE_ADDRESS_5; off += 4) 17633 pci_write_config_dword(bp->pdev, off, 0); 17634 } 17635 pci_restore_state(pdev); 17636 17637 bnxt_inv_fw_health_reg(bp); 17638 bnxt_try_map_fw_health_reg(bp); 17639 17640 /* In some PCIe AER scenarios, firmware may take up to 17641 * 10 seconds to become ready in the worst case. 17642 */ 17643 do { 17644 err = bnxt_try_recover_fw(bp); 17645 if (!err) 17646 break; 17647 retry++; 17648 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17649 17650 if (err) { 17651 dev_err(&pdev->dev, "Firmware not ready\n"); 17652 goto reset_exit; 17653 } 17654 17655 err = bnxt_hwrm_func_reset(bp); 17656 if (!err) 17657 result = PCI_ERS_RESULT_RECOVERED; 17658 17659 /* IRQ will be initialized later in bnxt_io_resume */ 17660 bnxt_ulp_irq_stop(bp); 17661 bnxt_clear_int_mode(bp); 17662 } 17663 17664 reset_exit: 17665 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17666 bnxt_clear_reservations(bp, true); 17667 netdev_unlock(netdev); 17668 17669 return result; 17670 } 17671 17672 /** 17673 * bnxt_io_resume - called when traffic can start flowing again. 17674 * @pdev: Pointer to PCI device 17675 * 17676 * This callback is called when the error recovery driver tells 17677 * us that its OK to resume normal operation. 17678 */ 17679 static void bnxt_io_resume(struct pci_dev *pdev) 17680 { 17681 struct net_device *netdev = pci_get_drvdata(pdev); 17682 struct bnxt *bp = netdev_priv(netdev); 17683 int err; 17684 17685 netdev_info(bp->dev, "PCI Slot Resume\n"); 17686 rtnl_lock(); 17687 netdev_lock(netdev); 17688 17689 err = bnxt_hwrm_func_qcaps(bp); 17690 if (!err) { 17691 if (netif_running(netdev)) { 17692 err = bnxt_open(netdev); 17693 } else { 17694 err = bnxt_reserve_rings(bp, true); 17695 if (!err) 17696 err = bnxt_init_int_mode(bp); 17697 } 17698 } 17699 17700 if (!err) 17701 netif_device_attach(netdev); 17702 17703 netdev_unlock(netdev); 17704 rtnl_unlock(); 17705 if (!err) { 17706 bnxt_ulp_start(bp); 17707 bnxt_reenable_sriov(bp); 17708 } 17709 } 17710 17711 static const struct pci_error_handlers bnxt_err_handler = { 17712 .error_detected = bnxt_io_error_detected, 17713 .slot_reset = bnxt_io_slot_reset, 17714 .resume = bnxt_io_resume 17715 }; 17716 17717 static struct pci_driver bnxt_pci_driver = { 17718 .name = DRV_MODULE_NAME, 17719 .id_table = bnxt_pci_tbl, 17720 .probe = bnxt_init_one, 17721 .remove = bnxt_remove_one, 17722 .shutdown = bnxt_shutdown, 17723 .driver.pm = BNXT_PM_OPS, 17724 .err_handler = &bnxt_err_handler, 17725 #if defined(CONFIG_BNXT_SRIOV) 17726 .sriov_configure = bnxt_sriov_configure, 17727 #endif 17728 }; 17729 17730 static int __init bnxt_init(void) 17731 { 17732 int err; 17733 17734 bnxt_debug_init(); 17735 err = pci_register_driver(&bnxt_pci_driver); 17736 if (err) { 17737 bnxt_debug_exit(); 17738 return err; 17739 } 17740 17741 return 0; 17742 } 17743 17744 static void __exit bnxt_exit(void) 17745 { 17746 pci_unregister_driver(&bnxt_pci_driver); 17747 if (bnxt_pf_wq) 17748 destroy_workqueue(bnxt_pf_wq); 17749 bnxt_debug_exit(); 17750 } 17751 17752 module_init(bnxt_init); 17753 module_exit(bnxt_exit); 17754