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 63 #include "bnxt.h" 64 #include "bnxt_hwrm.h" 65 #include "bnxt_ulp.h" 66 #include "bnxt_sriov.h" 67 #include "bnxt_ethtool.h" 68 #include "bnxt_dcb.h" 69 #include "bnxt_xdp.h" 70 #include "bnxt_ptp.h" 71 #include "bnxt_vfr.h" 72 #include "bnxt_tc.h" 73 #include "bnxt_devlink.h" 74 #include "bnxt_debugfs.h" 75 #include "bnxt_coredump.h" 76 #include "bnxt_hwmon.h" 77 78 #define BNXT_TX_TIMEOUT (5 * HZ) 79 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 80 NETIF_MSG_TX_ERR) 81 82 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 83 MODULE_LICENSE("GPL"); 84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 85 86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 88 89 #define BNXT_TX_PUSH_THRESH 164 90 91 /* indexed by enum board_idx */ 92 static const struct { 93 char *name; 94 } board_info[] = { 95 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 96 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 97 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 98 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 99 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 100 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 101 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 102 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 103 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 104 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 105 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 106 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 108 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 109 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 110 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 111 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 112 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 113 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 114 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 115 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 116 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 117 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 118 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 119 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 120 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 121 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 122 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 123 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 124 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 125 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 126 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 127 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 128 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 129 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 130 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 131 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 132 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 133 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 134 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 135 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 136 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 137 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 138 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 139 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 140 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 141 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 142 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 143 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 144 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 145 [NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" }, 146 }; 147 148 static const struct pci_device_id bnxt_pci_tbl[] = { 149 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 150 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 151 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 152 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 153 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 154 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 155 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 156 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 157 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 158 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 159 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 160 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 163 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 164 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 165 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 166 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 167 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 168 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 169 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 170 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 171 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 172 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 173 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 175 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 176 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 178 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 179 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 180 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 181 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 182 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 183 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 184 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 185 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 186 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 187 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 188 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 189 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 190 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 191 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 193 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 194 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 195 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 196 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 197 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 198 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 199 #ifdef CONFIG_BNXT_SRIOV 200 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 201 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 202 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 203 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 204 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 206 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 207 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 208 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 209 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 210 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 211 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 212 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 214 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 215 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 216 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 217 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 218 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 219 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 220 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 221 { PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV }, 222 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 223 #endif 224 { 0 } 225 }; 226 227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 228 229 static const u16 bnxt_vf_req_snif[] = { 230 HWRM_FUNC_CFG, 231 HWRM_FUNC_VF_CFG, 232 HWRM_PORT_PHY_QCFG, 233 HWRM_CFA_L2_FILTER_ALLOC, 234 }; 235 236 static const u16 bnxt_async_events_arr[] = { 237 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 238 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 239 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 240 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 241 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 242 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 243 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 244 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 245 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 246 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 247 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 248 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 249 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 250 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 251 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 252 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 253 ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER, 254 }; 255 256 const u16 bnxt_bstore_to_trace[] = { 257 [BNXT_CTX_SRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE, 258 [BNXT_CTX_SRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE, 259 [BNXT_CTX_CRT] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE, 260 [BNXT_CTX_CRT2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE, 261 [BNXT_CTX_RIGP0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE, 262 [BNXT_CTX_L2HWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE, 263 [BNXT_CTX_REHWRM] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE, 264 [BNXT_CTX_CA0] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE, 265 [BNXT_CTX_CA1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE, 266 [BNXT_CTX_CA2] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE, 267 [BNXT_CTX_RIGP1] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE, 268 [BNXT_CTX_KONG] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE, 269 [BNXT_CTX_QPC] = DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE, 270 }; 271 272 static struct workqueue_struct *bnxt_pf_wq; 273 274 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 275 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 276 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 277 278 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 279 .ports = { 280 .src = 0, 281 .dst = 0, 282 }, 283 .addrs = { 284 .v6addrs = { 285 .src = BNXT_IPV6_MASK_NONE, 286 .dst = BNXT_IPV6_MASK_NONE, 287 }, 288 }, 289 }; 290 291 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 292 .ports = { 293 .src = cpu_to_be16(0xffff), 294 .dst = cpu_to_be16(0xffff), 295 }, 296 .addrs = { 297 .v6addrs = { 298 .src = BNXT_IPV6_MASK_ALL, 299 .dst = BNXT_IPV6_MASK_ALL, 300 }, 301 }, 302 }; 303 304 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 305 .ports = { 306 .src = cpu_to_be16(0xffff), 307 .dst = cpu_to_be16(0xffff), 308 }, 309 .addrs = { 310 .v4addrs = { 311 .src = cpu_to_be32(0xffffffff), 312 .dst = cpu_to_be32(0xffffffff), 313 }, 314 }, 315 }; 316 317 static bool bnxt_vf_pciid(enum board_idx idx) 318 { 319 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 320 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 321 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 322 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF || 323 idx == NETXTREME_E_P7_VF_HV); 324 } 325 326 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 327 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 328 329 #define BNXT_DB_CQ(db, idx) \ 330 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 331 332 #define BNXT_DB_NQ_P5(db, idx) \ 333 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 334 (db)->doorbell) 335 336 #define BNXT_DB_NQ_P7(db, idx) \ 337 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 338 DB_RING_IDX(db, idx), (db)->doorbell) 339 340 #define BNXT_DB_CQ_ARM(db, idx) \ 341 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 342 343 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 344 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 345 DB_RING_IDX(db, idx), (db)->doorbell) 346 347 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 348 { 349 if (bp->flags & BNXT_FLAG_CHIP_P7) 350 BNXT_DB_NQ_P7(db, idx); 351 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 352 BNXT_DB_NQ_P5(db, idx); 353 else 354 BNXT_DB_CQ(db, idx); 355 } 356 357 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 358 { 359 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 360 BNXT_DB_NQ_ARM_P5(db, idx); 361 else 362 BNXT_DB_CQ_ARM(db, idx); 363 } 364 365 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 366 { 367 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 368 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 369 DB_RING_IDX(db, idx), db->doorbell); 370 else 371 BNXT_DB_CQ(db, idx); 372 } 373 374 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 375 { 376 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 377 return; 378 379 if (BNXT_PF(bp)) 380 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 381 else 382 schedule_delayed_work(&bp->fw_reset_task, delay); 383 } 384 385 static void __bnxt_queue_sp_work(struct bnxt *bp) 386 { 387 if (BNXT_PF(bp)) 388 queue_work(bnxt_pf_wq, &bp->sp_task); 389 else 390 schedule_work(&bp->sp_task); 391 } 392 393 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 394 { 395 set_bit(event, &bp->sp_event); 396 __bnxt_queue_sp_work(bp); 397 } 398 399 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 400 { 401 if (!rxr->bnapi->in_reset) { 402 rxr->bnapi->in_reset = true; 403 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 404 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 405 else 406 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 407 __bnxt_queue_sp_work(bp); 408 } 409 rxr->rx_next_cons = 0xffff; 410 } 411 412 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 413 u16 curr) 414 { 415 struct bnxt_napi *bnapi = txr->bnapi; 416 417 if (bnapi->tx_fault) 418 return; 419 420 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 421 txr->txq_index, txr->tx_hw_cons, 422 txr->tx_cons, txr->tx_prod, curr); 423 WARN_ON_ONCE(1); 424 bnapi->tx_fault = 1; 425 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 426 } 427 428 const u16 bnxt_lhint_arr[] = { 429 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 430 TX_BD_FLAGS_LHINT_512_TO_1023, 431 TX_BD_FLAGS_LHINT_1024_TO_2047, 432 TX_BD_FLAGS_LHINT_1024_TO_2047, 433 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 434 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 435 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 436 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 437 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 438 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 439 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 440 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 441 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 442 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 443 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 }; 449 450 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 451 { 452 struct metadata_dst *md_dst = skb_metadata_dst(skb); 453 454 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 455 return 0; 456 457 return md_dst->u.port_info.port_id; 458 } 459 460 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 461 u16 prod) 462 { 463 /* Sync BD data before updating doorbell */ 464 wmb(); 465 bnxt_db_write(bp, &txr->tx_db, prod); 466 txr->kick_pending = 0; 467 } 468 469 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 470 { 471 struct bnxt *bp = netdev_priv(dev); 472 struct tx_bd *txbd, *txbd0; 473 struct tx_bd_ext *txbd1; 474 struct netdev_queue *txq; 475 int i; 476 dma_addr_t mapping; 477 unsigned int length, pad = 0; 478 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 479 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 480 struct pci_dev *pdev = bp->pdev; 481 u16 prod, last_frag, txts_prod; 482 struct bnxt_tx_ring_info *txr; 483 struct bnxt_sw_tx_bd *tx_buf; 484 __le32 lflags = 0; 485 skb_frag_t *frag; 486 487 i = skb_get_queue_mapping(skb); 488 if (unlikely(i >= bp->tx_nr_rings)) { 489 dev_kfree_skb_any(skb); 490 dev_core_stats_tx_dropped_inc(dev); 491 return NETDEV_TX_OK; 492 } 493 494 txq = netdev_get_tx_queue(dev, i); 495 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 496 prod = txr->tx_prod; 497 498 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS) 499 if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) { 500 netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d. SKB will be linearized.\n", 501 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS); 502 if (skb_linearize(skb)) { 503 dev_kfree_skb_any(skb); 504 dev_core_stats_tx_dropped_inc(dev); 505 return NETDEV_TX_OK; 506 } 507 } 508 #endif 509 free_size = bnxt_tx_avail(bp, txr); 510 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 511 /* We must have raced with NAPI cleanup */ 512 if (net_ratelimit() && txr->kick_pending) 513 netif_warn(bp, tx_err, dev, 514 "bnxt: ring busy w/ flush pending!\n"); 515 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 516 bp->tx_wake_thresh)) 517 return NETDEV_TX_BUSY; 518 } 519 520 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 521 goto tx_free; 522 523 length = skb->len; 524 len = skb_headlen(skb); 525 last_frag = skb_shinfo(skb)->nr_frags; 526 527 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 528 529 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 530 tx_buf->skb = skb; 531 tx_buf->nr_frags = last_frag; 532 533 vlan_tag_flags = 0; 534 cfa_action = bnxt_xmit_get_cfa_action(skb); 535 if (skb_vlan_tag_present(skb)) { 536 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 537 skb_vlan_tag_get(skb); 538 /* Currently supports 8021Q, 8021AD vlan offloads 539 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 540 */ 541 if (skb->vlan_proto == htons(ETH_P_8021Q)) 542 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 543 } 544 545 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 546 ptp->tx_tstamp_en) { 547 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 548 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 549 tx_buf->is_ts_pkt = 1; 550 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 551 } else if (!skb_is_gso(skb)) { 552 u16 seq_id, hdr_off; 553 554 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 555 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 556 if (vlan_tag_flags) 557 hdr_off += VLAN_HLEN; 558 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 559 tx_buf->is_ts_pkt = 1; 560 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 561 562 ptp->txts_req[txts_prod].tx_seqid = seq_id; 563 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 564 tx_buf->txts_prod = txts_prod; 565 } 566 } 567 } 568 if (unlikely(skb->no_fcs)) 569 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 570 571 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 572 skb_frags_readable(skb) && !lflags) { 573 struct tx_push_buffer *tx_push_buf = txr->tx_push; 574 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 575 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 576 void __iomem *db = txr->tx_db.doorbell; 577 void *pdata = tx_push_buf->data; 578 u64 *end; 579 int j, push_len; 580 581 /* Set COAL_NOW to be ready quickly for the next push */ 582 tx_push->tx_bd_len_flags_type = 583 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 584 TX_BD_TYPE_LONG_TX_BD | 585 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 586 TX_BD_FLAGS_COAL_NOW | 587 TX_BD_FLAGS_PACKET_END | 588 TX_BD_CNT(2)); 589 590 if (skb->ip_summed == CHECKSUM_PARTIAL) 591 tx_push1->tx_bd_hsize_lflags = 592 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 593 else 594 tx_push1->tx_bd_hsize_lflags = 0; 595 596 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 597 tx_push1->tx_bd_cfa_action = 598 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 599 600 end = pdata + length; 601 end = PTR_ALIGN(end, 8) - 1; 602 *end = 0; 603 604 skb_copy_from_linear_data(skb, pdata, len); 605 pdata += len; 606 for (j = 0; j < last_frag; j++) { 607 void *fptr; 608 609 frag = &skb_shinfo(skb)->frags[j]; 610 fptr = skb_frag_address_safe(frag); 611 if (!fptr) 612 goto normal_tx; 613 614 memcpy(pdata, fptr, skb_frag_size(frag)); 615 pdata += skb_frag_size(frag); 616 } 617 618 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 619 txbd->tx_bd_haddr = txr->data_mapping; 620 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 621 prod = NEXT_TX(prod); 622 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 623 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 624 memcpy(txbd, tx_push1, sizeof(*txbd)); 625 prod = NEXT_TX(prod); 626 tx_push->doorbell = 627 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 628 DB_RING_IDX(&txr->tx_db, prod)); 629 WRITE_ONCE(txr->tx_prod, prod); 630 631 tx_buf->is_push = 1; 632 netdev_tx_sent_queue(txq, skb->len); 633 wmb(); /* Sync is_push and byte queue before pushing data */ 634 635 push_len = (length + sizeof(*tx_push) + 7) / 8; 636 if (push_len > 16) { 637 __iowrite64_copy(db, tx_push_buf, 16); 638 __iowrite32_copy(db + 4, tx_push_buf + 1, 639 (push_len - 16) << 1); 640 } else { 641 __iowrite64_copy(db, tx_push_buf, push_len); 642 } 643 644 goto tx_done; 645 } 646 647 normal_tx: 648 if (length < BNXT_MIN_PKT_SIZE) { 649 pad = BNXT_MIN_PKT_SIZE - length; 650 if (skb_pad(skb, pad)) 651 /* SKB already freed. */ 652 goto tx_kick_pending; 653 length = BNXT_MIN_PKT_SIZE; 654 } 655 656 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 657 658 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 659 goto tx_free; 660 661 dma_unmap_addr_set(tx_buf, mapping, mapping); 662 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 663 TX_BD_CNT(last_frag + 2); 664 665 txbd->tx_bd_haddr = cpu_to_le64(mapping); 666 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 667 668 prod = NEXT_TX(prod); 669 txbd1 = (struct tx_bd_ext *) 670 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 671 672 txbd1->tx_bd_hsize_lflags = lflags; 673 if (skb_is_gso(skb)) { 674 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 675 u32 hdr_len; 676 677 if (skb->encapsulation) { 678 if (udp_gso) 679 hdr_len = skb_inner_transport_offset(skb) + 680 sizeof(struct udphdr); 681 else 682 hdr_len = skb_inner_tcp_all_headers(skb); 683 } else if (udp_gso) { 684 hdr_len = skb_transport_offset(skb) + 685 sizeof(struct udphdr); 686 } else { 687 hdr_len = skb_tcp_all_headers(skb); 688 } 689 690 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 691 TX_BD_FLAGS_T_IPID | 692 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 693 length = skb_shinfo(skb)->gso_size; 694 txbd1->tx_bd_mss = cpu_to_le32(length); 695 length += hdr_len; 696 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 697 txbd1->tx_bd_hsize_lflags |= 698 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 699 txbd1->tx_bd_mss = 0; 700 } 701 702 length >>= 9; 703 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 704 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 705 skb->len); 706 i = 0; 707 goto tx_dma_error; 708 } 709 flags |= bnxt_lhint_arr[length]; 710 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 711 712 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 713 txbd1->tx_bd_cfa_action = 714 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 715 txbd0 = txbd; 716 for (i = 0; i < last_frag; i++) { 717 frag = &skb_shinfo(skb)->frags[i]; 718 prod = NEXT_TX(prod); 719 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 720 721 len = skb_frag_size(frag); 722 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 723 DMA_TO_DEVICE); 724 725 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 726 goto tx_dma_error; 727 728 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 729 netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf, 730 mapping, mapping); 731 732 txbd->tx_bd_haddr = cpu_to_le64(mapping); 733 734 flags = len << TX_BD_LEN_SHIFT; 735 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 736 } 737 738 flags &= ~TX_BD_LEN; 739 txbd->tx_bd_len_flags_type = 740 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 741 TX_BD_FLAGS_PACKET_END); 742 743 netdev_tx_sent_queue(txq, skb->len); 744 745 skb_tx_timestamp(skb); 746 747 prod = NEXT_TX(prod); 748 WRITE_ONCE(txr->tx_prod, prod); 749 750 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 751 bnxt_txr_db_kick(bp, txr, prod); 752 } else { 753 if (free_size >= bp->tx_wake_thresh) 754 txbd0->tx_bd_len_flags_type |= 755 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 756 txr->kick_pending = 1; 757 } 758 759 tx_done: 760 761 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 762 if (netdev_xmit_more() && !tx_buf->is_push) { 763 txbd0->tx_bd_len_flags_type &= 764 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 765 bnxt_txr_db_kick(bp, txr, prod); 766 } 767 768 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 769 bp->tx_wake_thresh); 770 } 771 return NETDEV_TX_OK; 772 773 tx_dma_error: 774 last_frag = i; 775 776 /* start back at beginning and unmap skb */ 777 prod = txr->tx_prod; 778 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 779 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 780 skb_headlen(skb), DMA_TO_DEVICE); 781 prod = NEXT_TX(prod); 782 783 /* unmap remaining mapped pages */ 784 for (i = 0; i < last_frag; i++) { 785 prod = NEXT_TX(prod); 786 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 787 frag = &skb_shinfo(skb)->frags[i]; 788 netmem_dma_unmap_page_attrs(&pdev->dev, 789 dma_unmap_addr(tx_buf, mapping), 790 skb_frag_size(frag), 791 DMA_TO_DEVICE, 0); 792 } 793 794 tx_free: 795 dev_kfree_skb_any(skb); 796 tx_kick_pending: 797 if (BNXT_TX_PTP_IS_SET(lflags)) { 798 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0; 799 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 800 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 801 /* set SKB to err so PTP worker will clean up */ 802 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 803 } 804 if (txr->kick_pending) 805 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 806 txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL; 807 dev_core_stats_tx_dropped_inc(dev); 808 return NETDEV_TX_OK; 809 } 810 811 /* Returns true if some remaining TX packets not processed. */ 812 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 813 int budget) 814 { 815 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 816 struct pci_dev *pdev = bp->pdev; 817 u16 hw_cons = txr->tx_hw_cons; 818 unsigned int tx_bytes = 0; 819 u16 cons = txr->tx_cons; 820 skb_frag_t *frag; 821 int tx_pkts = 0; 822 bool rc = false; 823 824 while (RING_TX(bp, cons) != hw_cons) { 825 struct bnxt_sw_tx_bd *tx_buf; 826 struct sk_buff *skb; 827 bool is_ts_pkt; 828 int j, last; 829 830 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 831 skb = tx_buf->skb; 832 833 if (unlikely(!skb)) { 834 bnxt_sched_reset_txr(bp, txr, cons); 835 return rc; 836 } 837 838 is_ts_pkt = tx_buf->is_ts_pkt; 839 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 840 rc = true; 841 break; 842 } 843 844 cons = NEXT_TX(cons); 845 tx_pkts++; 846 tx_bytes += skb->len; 847 tx_buf->skb = NULL; 848 tx_buf->is_ts_pkt = 0; 849 850 if (tx_buf->is_push) { 851 tx_buf->is_push = 0; 852 goto next_tx_int; 853 } 854 855 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 856 skb_headlen(skb), DMA_TO_DEVICE); 857 last = tx_buf->nr_frags; 858 859 for (j = 0; j < last; j++) { 860 frag = &skb_shinfo(skb)->frags[j]; 861 cons = NEXT_TX(cons); 862 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 863 netmem_dma_unmap_page_attrs(&pdev->dev, 864 dma_unmap_addr(tx_buf, 865 mapping), 866 skb_frag_size(frag), 867 DMA_TO_DEVICE, 0); 868 } 869 if (unlikely(is_ts_pkt)) { 870 if (BNXT_CHIP_P5(bp)) { 871 /* PTP worker takes ownership of the skb */ 872 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 873 skb = NULL; 874 } 875 } 876 877 next_tx_int: 878 cons = NEXT_TX(cons); 879 880 napi_consume_skb(skb, budget); 881 } 882 883 WRITE_ONCE(txr->tx_cons, cons); 884 885 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 886 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 887 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 888 889 return rc; 890 } 891 892 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 893 { 894 struct bnxt_tx_ring_info *txr; 895 bool more = false; 896 int i; 897 898 bnxt_for_each_napi_tx(i, bnapi, txr) { 899 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 900 more |= __bnxt_tx_int(bp, txr, budget); 901 } 902 if (!more) 903 bnapi->events &= ~BNXT_TX_CMP_EVENT; 904 } 905 906 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr) 907 { 908 return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE; 909 } 910 911 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 912 struct bnxt_rx_ring_info *rxr, 913 unsigned int *offset, 914 gfp_t gfp) 915 { 916 struct page *page; 917 918 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 919 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 920 BNXT_RX_PAGE_SIZE); 921 } else { 922 page = page_pool_dev_alloc_pages(rxr->page_pool); 923 *offset = 0; 924 } 925 if (!page) 926 return NULL; 927 928 *mapping = page_pool_get_dma_addr(page) + *offset; 929 return page; 930 } 931 932 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping, 933 struct bnxt_rx_ring_info *rxr, 934 unsigned int *offset, 935 gfp_t gfp) 936 { 937 netmem_ref netmem; 938 939 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 940 netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset, BNXT_RX_PAGE_SIZE, gfp); 941 } else { 942 netmem = page_pool_alloc_netmems(rxr->page_pool, gfp); 943 *offset = 0; 944 } 945 if (!netmem) 946 return 0; 947 948 *mapping = page_pool_get_dma_addr_netmem(netmem) + *offset; 949 return netmem; 950 } 951 952 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 953 struct bnxt_rx_ring_info *rxr, 954 gfp_t gfp) 955 { 956 unsigned int offset; 957 struct page *page; 958 959 page = page_pool_alloc_frag(rxr->head_pool, &offset, 960 bp->rx_buf_size, gfp); 961 if (!page) 962 return NULL; 963 964 *mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset; 965 return page_address(page) + offset; 966 } 967 968 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 969 u16 prod, gfp_t gfp) 970 { 971 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 972 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 973 dma_addr_t mapping; 974 975 if (BNXT_RX_PAGE_MODE(bp)) { 976 unsigned int offset; 977 struct page *page = 978 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 979 980 if (!page) 981 return -ENOMEM; 982 983 mapping += bp->rx_dma_offset; 984 rx_buf->data = page; 985 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 986 } else { 987 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp); 988 989 if (!data) 990 return -ENOMEM; 991 992 rx_buf->data = data; 993 rx_buf->data_ptr = data + bp->rx_offset; 994 } 995 rx_buf->mapping = mapping; 996 997 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 998 return 0; 999 } 1000 1001 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 1002 { 1003 u16 prod = rxr->rx_prod; 1004 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1005 struct bnxt *bp = rxr->bnapi->bp; 1006 struct rx_bd *cons_bd, *prod_bd; 1007 1008 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1009 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1010 1011 prod_rx_buf->data = data; 1012 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 1013 1014 prod_rx_buf->mapping = cons_rx_buf->mapping; 1015 1016 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1017 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 1018 1019 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 1020 } 1021 1022 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1023 { 1024 u16 next, max = rxr->rx_agg_bmap_size; 1025 1026 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 1027 if (next >= max) 1028 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 1029 return next; 1030 } 1031 1032 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1033 u16 prod, gfp_t gfp) 1034 { 1035 struct rx_bd *rxbd = 1036 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1037 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 1038 u16 sw_prod = rxr->rx_sw_agg_prod; 1039 unsigned int offset = 0; 1040 dma_addr_t mapping; 1041 netmem_ref netmem; 1042 1043 netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp); 1044 if (!netmem) 1045 return -ENOMEM; 1046 1047 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1048 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1049 1050 __set_bit(sw_prod, rxr->rx_agg_bmap); 1051 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1052 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1053 1054 rx_agg_buf->netmem = netmem; 1055 rx_agg_buf->offset = offset; 1056 rx_agg_buf->mapping = mapping; 1057 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1058 rxbd->rx_bd_opaque = sw_prod; 1059 return 0; 1060 } 1061 1062 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1063 struct bnxt_cp_ring_info *cpr, 1064 u16 cp_cons, u16 curr) 1065 { 1066 struct rx_agg_cmp *agg; 1067 1068 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1069 agg = (struct rx_agg_cmp *) 1070 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1071 return agg; 1072 } 1073 1074 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1075 struct bnxt_rx_ring_info *rxr, 1076 u16 agg_id, u16 curr) 1077 { 1078 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1079 1080 return &tpa_info->agg_arr[curr]; 1081 } 1082 1083 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1084 u16 start, u32 agg_bufs, bool tpa) 1085 { 1086 struct bnxt_napi *bnapi = cpr->bnapi; 1087 struct bnxt *bp = bnapi->bp; 1088 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1089 u16 prod = rxr->rx_agg_prod; 1090 u16 sw_prod = rxr->rx_sw_agg_prod; 1091 bool p5_tpa = false; 1092 u32 i; 1093 1094 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1095 p5_tpa = true; 1096 1097 for (i = 0; i < agg_bufs; i++) { 1098 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1099 struct rx_agg_cmp *agg; 1100 struct rx_bd *prod_bd; 1101 netmem_ref netmem; 1102 u16 cons; 1103 1104 if (p5_tpa) 1105 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1106 else 1107 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1108 cons = agg->rx_agg_cmp_opaque; 1109 __clear_bit(cons, rxr->rx_agg_bmap); 1110 1111 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1112 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1113 1114 __set_bit(sw_prod, rxr->rx_agg_bmap); 1115 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1116 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1117 1118 /* It is possible for sw_prod to be equal to cons, so 1119 * set cons_rx_buf->netmem to 0 first. 1120 */ 1121 netmem = cons_rx_buf->netmem; 1122 cons_rx_buf->netmem = 0; 1123 prod_rx_buf->netmem = netmem; 1124 prod_rx_buf->offset = cons_rx_buf->offset; 1125 1126 prod_rx_buf->mapping = cons_rx_buf->mapping; 1127 1128 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1129 1130 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1131 prod_bd->rx_bd_opaque = sw_prod; 1132 1133 prod = NEXT_RX_AGG(prod); 1134 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1135 } 1136 rxr->rx_agg_prod = prod; 1137 rxr->rx_sw_agg_prod = sw_prod; 1138 } 1139 1140 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1141 struct bnxt_rx_ring_info *rxr, 1142 u16 cons, void *data, u8 *data_ptr, 1143 dma_addr_t dma_addr, 1144 unsigned int offset_and_len) 1145 { 1146 unsigned int len = offset_and_len & 0xffff; 1147 struct page *page = data; 1148 u16 prod = rxr->rx_prod; 1149 struct sk_buff *skb; 1150 int err; 1151 1152 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1153 if (unlikely(err)) { 1154 bnxt_reuse_rx_data(rxr, cons, data); 1155 return NULL; 1156 } 1157 dma_addr -= bp->rx_dma_offset; 1158 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1159 bp->rx_dir); 1160 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1161 if (!skb) { 1162 page_pool_recycle_direct(rxr->page_pool, page); 1163 return NULL; 1164 } 1165 skb_mark_for_recycle(skb); 1166 skb_reserve(skb, bp->rx_offset); 1167 __skb_put(skb, len); 1168 1169 return skb; 1170 } 1171 1172 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1173 struct bnxt_rx_ring_info *rxr, 1174 u16 cons, void *data, u8 *data_ptr, 1175 dma_addr_t dma_addr, 1176 unsigned int offset_and_len) 1177 { 1178 unsigned int payload = offset_and_len >> 16; 1179 unsigned int len = offset_and_len & 0xffff; 1180 skb_frag_t *frag; 1181 struct page *page = data; 1182 u16 prod = rxr->rx_prod; 1183 struct sk_buff *skb; 1184 int off, err; 1185 1186 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1187 if (unlikely(err)) { 1188 bnxt_reuse_rx_data(rxr, cons, data); 1189 return NULL; 1190 } 1191 dma_addr -= bp->rx_dma_offset; 1192 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1193 bp->rx_dir); 1194 1195 if (unlikely(!payload)) 1196 payload = eth_get_headlen(bp->dev, data_ptr, len); 1197 1198 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1199 if (!skb) { 1200 page_pool_recycle_direct(rxr->page_pool, page); 1201 return NULL; 1202 } 1203 1204 skb_mark_for_recycle(skb); 1205 off = (void *)data_ptr - page_address(page); 1206 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1207 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1208 payload + NET_IP_ALIGN); 1209 1210 frag = &skb_shinfo(skb)->frags[0]; 1211 skb_frag_size_sub(frag, payload); 1212 skb_frag_off_add(frag, payload); 1213 skb->data_len -= payload; 1214 skb->tail += payload; 1215 1216 return skb; 1217 } 1218 1219 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1220 struct bnxt_rx_ring_info *rxr, u16 cons, 1221 void *data, u8 *data_ptr, 1222 dma_addr_t dma_addr, 1223 unsigned int offset_and_len) 1224 { 1225 u16 prod = rxr->rx_prod; 1226 struct sk_buff *skb; 1227 int err; 1228 1229 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1230 if (unlikely(err)) { 1231 bnxt_reuse_rx_data(rxr, cons, data); 1232 return NULL; 1233 } 1234 1235 skb = napi_build_skb(data, bp->rx_buf_size); 1236 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1237 bp->rx_dir); 1238 if (!skb) { 1239 page_pool_free_va(rxr->head_pool, data, true); 1240 return NULL; 1241 } 1242 1243 skb_mark_for_recycle(skb); 1244 skb_reserve(skb, bp->rx_offset); 1245 skb_put(skb, offset_and_len & 0xffff); 1246 return skb; 1247 } 1248 1249 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp, 1250 struct bnxt_cp_ring_info *cpr, 1251 u16 idx, u32 agg_bufs, bool tpa, 1252 struct sk_buff *skb, 1253 struct xdp_buff *xdp) 1254 { 1255 struct bnxt_napi *bnapi = cpr->bnapi; 1256 struct skb_shared_info *shinfo; 1257 struct bnxt_rx_ring_info *rxr; 1258 u32 i, total_frag_len = 0; 1259 bool p5_tpa = false; 1260 u16 prod; 1261 1262 rxr = bnapi->rx_ring; 1263 prod = rxr->rx_agg_prod; 1264 1265 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1266 p5_tpa = true; 1267 1268 if (skb) 1269 shinfo = skb_shinfo(skb); 1270 else 1271 shinfo = xdp_get_shared_info_from_buff(xdp); 1272 1273 for (i = 0; i < agg_bufs; i++) { 1274 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1275 struct rx_agg_cmp *agg; 1276 u16 cons, frag_len; 1277 netmem_ref netmem; 1278 1279 if (p5_tpa) 1280 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1281 else 1282 agg = bnxt_get_agg(bp, cpr, idx, i); 1283 cons = agg->rx_agg_cmp_opaque; 1284 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1285 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1286 1287 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1288 if (skb) { 1289 skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem, 1290 cons_rx_buf->offset, 1291 frag_len, BNXT_RX_PAGE_SIZE); 1292 } else { 1293 skb_frag_t *frag = &shinfo->frags[i]; 1294 1295 skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem, 1296 cons_rx_buf->offset, 1297 frag_len); 1298 shinfo->nr_frags = i + 1; 1299 } 1300 __clear_bit(cons, rxr->rx_agg_bmap); 1301 1302 /* It is possible for bnxt_alloc_rx_netmem() to allocate 1303 * a sw_prod index that equals the cons index, so we 1304 * need to clear the cons entry now. 1305 */ 1306 netmem = cons_rx_buf->netmem; 1307 cons_rx_buf->netmem = 0; 1308 1309 if (xdp && netmem_is_pfmemalloc(netmem)) 1310 xdp_buff_set_frag_pfmemalloc(xdp); 1311 1312 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) { 1313 if (skb) { 1314 skb->len -= frag_len; 1315 skb->data_len -= frag_len; 1316 skb->truesize -= BNXT_RX_PAGE_SIZE; 1317 } 1318 1319 --shinfo->nr_frags; 1320 cons_rx_buf->netmem = netmem; 1321 1322 /* Update prod since possibly some netmems have been 1323 * allocated already. 1324 */ 1325 rxr->rx_agg_prod = prod; 1326 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1327 return 0; 1328 } 1329 1330 page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0, 1331 BNXT_RX_PAGE_SIZE); 1332 1333 total_frag_len += frag_len; 1334 prod = NEXT_RX_AGG(prod); 1335 } 1336 rxr->rx_agg_prod = prod; 1337 return total_frag_len; 1338 } 1339 1340 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp, 1341 struct bnxt_cp_ring_info *cpr, 1342 struct sk_buff *skb, u16 idx, 1343 u32 agg_bufs, bool tpa) 1344 { 1345 u32 total_frag_len = 0; 1346 1347 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1348 skb, NULL); 1349 if (!total_frag_len) { 1350 skb_mark_for_recycle(skb); 1351 dev_kfree_skb(skb); 1352 return NULL; 1353 } 1354 1355 return skb; 1356 } 1357 1358 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp, 1359 struct bnxt_cp_ring_info *cpr, 1360 struct xdp_buff *xdp, u16 idx, 1361 u32 agg_bufs, bool tpa) 1362 { 1363 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1364 u32 total_frag_len = 0; 1365 1366 if (!xdp_buff_has_frags(xdp)) 1367 shinfo->nr_frags = 0; 1368 1369 total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa, 1370 NULL, xdp); 1371 if (total_frag_len) { 1372 xdp_buff_set_frags_flag(xdp); 1373 shinfo->nr_frags = agg_bufs; 1374 shinfo->xdp_frags_size = total_frag_len; 1375 } 1376 return total_frag_len; 1377 } 1378 1379 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1380 u8 agg_bufs, u32 *raw_cons) 1381 { 1382 u16 last; 1383 struct rx_agg_cmp *agg; 1384 1385 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1386 last = RING_CMP(*raw_cons); 1387 agg = (struct rx_agg_cmp *) 1388 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1389 return RX_AGG_CMP_VALID(agg, *raw_cons); 1390 } 1391 1392 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1393 unsigned int len, 1394 dma_addr_t mapping) 1395 { 1396 struct bnxt *bp = bnapi->bp; 1397 struct pci_dev *pdev = bp->pdev; 1398 struct sk_buff *skb; 1399 1400 skb = napi_alloc_skb(&bnapi->napi, len); 1401 if (!skb) 1402 return NULL; 1403 1404 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak, 1405 bp->rx_dir); 1406 1407 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1408 len + NET_IP_ALIGN); 1409 1410 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak, 1411 bp->rx_dir); 1412 1413 skb_put(skb, len); 1414 1415 return skb; 1416 } 1417 1418 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1419 unsigned int len, 1420 dma_addr_t mapping) 1421 { 1422 return bnxt_copy_data(bnapi, data, len, mapping); 1423 } 1424 1425 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1426 struct xdp_buff *xdp, 1427 unsigned int len, 1428 dma_addr_t mapping) 1429 { 1430 unsigned int metasize = 0; 1431 u8 *data = xdp->data; 1432 struct sk_buff *skb; 1433 1434 len = xdp->data_end - xdp->data_meta; 1435 metasize = xdp->data - xdp->data_meta; 1436 data = xdp->data_meta; 1437 1438 skb = bnxt_copy_data(bnapi, data, len, mapping); 1439 if (!skb) 1440 return skb; 1441 1442 if (metasize) { 1443 skb_metadata_set(skb, metasize); 1444 __skb_pull(skb, metasize); 1445 } 1446 1447 return skb; 1448 } 1449 1450 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1451 u32 *raw_cons, void *cmp) 1452 { 1453 struct rx_cmp *rxcmp = cmp; 1454 u32 tmp_raw_cons = *raw_cons; 1455 u8 cmp_type, agg_bufs = 0; 1456 1457 cmp_type = RX_CMP_TYPE(rxcmp); 1458 1459 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1460 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1461 RX_CMP_AGG_BUFS) >> 1462 RX_CMP_AGG_BUFS_SHIFT; 1463 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1464 struct rx_tpa_end_cmp *tpa_end = cmp; 1465 1466 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1467 return 0; 1468 1469 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1470 } 1471 1472 if (agg_bufs) { 1473 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1474 return -EBUSY; 1475 } 1476 *raw_cons = tmp_raw_cons; 1477 return 0; 1478 } 1479 1480 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1481 { 1482 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1483 u16 idx = agg_id & MAX_TPA_P5_MASK; 1484 1485 if (test_bit(idx, map->agg_idx_bmap)) { 1486 idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5); 1487 if (idx >= MAX_TPA_P5) 1488 return INVALID_HW_RING_ID; 1489 } 1490 __set_bit(idx, map->agg_idx_bmap); 1491 map->agg_id_tbl[agg_id] = idx; 1492 return idx; 1493 } 1494 1495 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1496 { 1497 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1498 1499 __clear_bit(idx, map->agg_idx_bmap); 1500 } 1501 1502 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1503 { 1504 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1505 1506 return map->agg_id_tbl[agg_id]; 1507 } 1508 1509 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1510 struct rx_tpa_start_cmp *tpa_start, 1511 struct rx_tpa_start_cmp_ext *tpa_start1) 1512 { 1513 tpa_info->cfa_code_valid = 1; 1514 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1515 tpa_info->vlan_valid = 0; 1516 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1517 tpa_info->vlan_valid = 1; 1518 tpa_info->metadata = 1519 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1520 } 1521 } 1522 1523 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1524 struct rx_tpa_start_cmp *tpa_start, 1525 struct rx_tpa_start_cmp_ext *tpa_start1) 1526 { 1527 tpa_info->vlan_valid = 0; 1528 if (TPA_START_VLAN_VALID(tpa_start)) { 1529 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1530 u32 vlan_proto = ETH_P_8021Q; 1531 1532 tpa_info->vlan_valid = 1; 1533 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1534 vlan_proto = ETH_P_8021AD; 1535 tpa_info->metadata = vlan_proto << 16 | 1536 TPA_START_METADATA0_TCI(tpa_start1); 1537 } 1538 } 1539 1540 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1541 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1542 struct rx_tpa_start_cmp_ext *tpa_start1) 1543 { 1544 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1545 struct bnxt_tpa_info *tpa_info; 1546 u16 cons, prod, agg_id; 1547 struct rx_bd *prod_bd; 1548 dma_addr_t mapping; 1549 1550 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1551 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1552 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1553 if (unlikely(agg_id == INVALID_HW_RING_ID)) { 1554 netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n", 1555 rxr->bnapi->index, 1556 TPA_START_AGG_ID_P5(tpa_start)); 1557 bnxt_sched_reset_rxr(bp, rxr); 1558 return; 1559 } 1560 } else { 1561 agg_id = TPA_START_AGG_ID(tpa_start); 1562 } 1563 cons = tpa_start->rx_tpa_start_cmp_opaque; 1564 prod = rxr->rx_prod; 1565 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1566 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1567 tpa_info = &rxr->rx_tpa[agg_id]; 1568 1569 if (unlikely(cons != rxr->rx_next_cons || 1570 TPA_START_ERROR(tpa_start))) { 1571 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1572 cons, rxr->rx_next_cons, 1573 TPA_START_ERROR_CODE(tpa_start1)); 1574 bnxt_sched_reset_rxr(bp, rxr); 1575 return; 1576 } 1577 prod_rx_buf->data = tpa_info->data; 1578 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1579 1580 mapping = tpa_info->mapping; 1581 prod_rx_buf->mapping = mapping; 1582 1583 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1584 1585 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1586 1587 tpa_info->data = cons_rx_buf->data; 1588 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1589 cons_rx_buf->data = NULL; 1590 tpa_info->mapping = cons_rx_buf->mapping; 1591 1592 tpa_info->len = 1593 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1594 RX_TPA_START_CMP_LEN_SHIFT; 1595 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1596 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1597 tpa_info->gso_type = SKB_GSO_TCPV4; 1598 if (TPA_START_IS_IPV6(tpa_start1)) 1599 tpa_info->gso_type = SKB_GSO_TCPV6; 1600 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1601 else if (!BNXT_CHIP_P4_PLUS(bp) && 1602 TPA_START_HASH_TYPE(tpa_start) == 3) 1603 tpa_info->gso_type = SKB_GSO_TCPV6; 1604 tpa_info->rss_hash = 1605 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1606 } else { 1607 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1608 tpa_info->gso_type = 0; 1609 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1610 } 1611 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1612 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1613 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1614 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1615 else 1616 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1617 tpa_info->agg_count = 0; 1618 1619 rxr->rx_prod = NEXT_RX(prod); 1620 cons = RING_RX(bp, NEXT_RX(cons)); 1621 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1622 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1623 1624 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1625 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1626 cons_rx_buf->data = NULL; 1627 } 1628 1629 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1630 { 1631 if (agg_bufs) 1632 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1633 } 1634 1635 #ifdef CONFIG_INET 1636 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1637 { 1638 struct udphdr *uh = NULL; 1639 1640 if (ip_proto == htons(ETH_P_IP)) { 1641 struct iphdr *iph = (struct iphdr *)skb->data; 1642 1643 if (iph->protocol == IPPROTO_UDP) 1644 uh = (struct udphdr *)(iph + 1); 1645 } else { 1646 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1647 1648 if (iph->nexthdr == IPPROTO_UDP) 1649 uh = (struct udphdr *)(iph + 1); 1650 } 1651 if (uh) { 1652 if (uh->check) 1653 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1654 else 1655 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1656 } 1657 } 1658 #endif 1659 1660 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1661 int payload_off, int tcp_ts, 1662 struct sk_buff *skb) 1663 { 1664 #ifdef CONFIG_INET 1665 struct tcphdr *th; 1666 int len, nw_off; 1667 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1668 u32 hdr_info = tpa_info->hdr_info; 1669 bool loopback = false; 1670 1671 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1672 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1673 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1674 1675 /* If the packet is an internal loopback packet, the offsets will 1676 * have an extra 4 bytes. 1677 */ 1678 if (inner_mac_off == 4) { 1679 loopback = true; 1680 } else if (inner_mac_off > 4) { 1681 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1682 ETH_HLEN - 2)); 1683 1684 /* We only support inner iPv4/ipv6. If we don't see the 1685 * correct protocol ID, it must be a loopback packet where 1686 * the offsets are off by 4. 1687 */ 1688 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1689 loopback = true; 1690 } 1691 if (loopback) { 1692 /* internal loopback packet, subtract all offsets by 4 */ 1693 inner_ip_off -= 4; 1694 inner_mac_off -= 4; 1695 outer_ip_off -= 4; 1696 } 1697 1698 nw_off = inner_ip_off - ETH_HLEN; 1699 skb_set_network_header(skb, nw_off); 1700 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1701 struct ipv6hdr *iph = ipv6_hdr(skb); 1702 1703 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1704 len = skb->len - skb_transport_offset(skb); 1705 th = tcp_hdr(skb); 1706 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1707 } else { 1708 struct iphdr *iph = ip_hdr(skb); 1709 1710 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1711 len = skb->len - skb_transport_offset(skb); 1712 th = tcp_hdr(skb); 1713 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1714 } 1715 1716 if (inner_mac_off) { /* tunnel */ 1717 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1718 ETH_HLEN - 2)); 1719 1720 bnxt_gro_tunnel(skb, proto); 1721 } 1722 #endif 1723 return skb; 1724 } 1725 1726 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1727 int payload_off, int tcp_ts, 1728 struct sk_buff *skb) 1729 { 1730 #ifdef CONFIG_INET 1731 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1732 u32 hdr_info = tpa_info->hdr_info; 1733 int iphdr_len, nw_off; 1734 1735 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1736 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1737 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1738 1739 nw_off = inner_ip_off - ETH_HLEN; 1740 skb_set_network_header(skb, nw_off); 1741 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1742 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1743 skb_set_transport_header(skb, nw_off + iphdr_len); 1744 1745 if (inner_mac_off) { /* tunnel */ 1746 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1747 ETH_HLEN - 2)); 1748 1749 bnxt_gro_tunnel(skb, proto); 1750 } 1751 #endif 1752 return skb; 1753 } 1754 1755 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1756 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1757 1758 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1759 int payload_off, int tcp_ts, 1760 struct sk_buff *skb) 1761 { 1762 #ifdef CONFIG_INET 1763 struct tcphdr *th; 1764 int len, nw_off, tcp_opt_len = 0; 1765 1766 if (tcp_ts) 1767 tcp_opt_len = 12; 1768 1769 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1770 struct iphdr *iph; 1771 1772 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1773 ETH_HLEN; 1774 skb_set_network_header(skb, nw_off); 1775 iph = ip_hdr(skb); 1776 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1777 len = skb->len - skb_transport_offset(skb); 1778 th = tcp_hdr(skb); 1779 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1780 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1781 struct ipv6hdr *iph; 1782 1783 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1784 ETH_HLEN; 1785 skb_set_network_header(skb, nw_off); 1786 iph = ipv6_hdr(skb); 1787 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1788 len = skb->len - skb_transport_offset(skb); 1789 th = tcp_hdr(skb); 1790 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1791 } else { 1792 dev_kfree_skb_any(skb); 1793 return NULL; 1794 } 1795 1796 if (nw_off) /* tunnel */ 1797 bnxt_gro_tunnel(skb, skb->protocol); 1798 #endif 1799 return skb; 1800 } 1801 1802 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1803 struct bnxt_tpa_info *tpa_info, 1804 struct rx_tpa_end_cmp *tpa_end, 1805 struct rx_tpa_end_cmp_ext *tpa_end1, 1806 struct sk_buff *skb) 1807 { 1808 #ifdef CONFIG_INET 1809 int payload_off; 1810 u16 segs; 1811 1812 segs = TPA_END_TPA_SEGS(tpa_end); 1813 if (segs == 1) 1814 return skb; 1815 1816 NAPI_GRO_CB(skb)->count = segs; 1817 skb_shinfo(skb)->gso_size = 1818 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1819 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1820 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1821 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1822 else 1823 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1824 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1825 if (likely(skb)) 1826 tcp_gro_complete(skb); 1827 #endif 1828 return skb; 1829 } 1830 1831 /* Given the cfa_code of a received packet determine which 1832 * netdev (vf-rep or PF) the packet is destined to. 1833 */ 1834 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1835 { 1836 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1837 1838 /* if vf-rep dev is NULL, it must belong to the PF */ 1839 return dev ? dev : bp->dev; 1840 } 1841 1842 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1843 struct bnxt_cp_ring_info *cpr, 1844 u32 *raw_cons, 1845 struct rx_tpa_end_cmp *tpa_end, 1846 struct rx_tpa_end_cmp_ext *tpa_end1, 1847 u8 *event) 1848 { 1849 struct bnxt_napi *bnapi = cpr->bnapi; 1850 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1851 struct net_device *dev = bp->dev; 1852 u8 *data_ptr, agg_bufs; 1853 unsigned int len; 1854 struct bnxt_tpa_info *tpa_info; 1855 dma_addr_t mapping; 1856 struct sk_buff *skb; 1857 u16 idx = 0, agg_id; 1858 void *data; 1859 bool gro; 1860 1861 if (unlikely(bnapi->in_reset)) { 1862 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1863 1864 if (rc < 0) 1865 return ERR_PTR(-EBUSY); 1866 return NULL; 1867 } 1868 1869 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1870 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1871 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1872 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1873 tpa_info = &rxr->rx_tpa[agg_id]; 1874 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1875 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1876 agg_bufs, tpa_info->agg_count); 1877 agg_bufs = tpa_info->agg_count; 1878 } 1879 tpa_info->agg_count = 0; 1880 *event |= BNXT_AGG_EVENT; 1881 bnxt_free_agg_idx(rxr, agg_id); 1882 idx = agg_id; 1883 gro = !!(bp->flags & BNXT_FLAG_GRO); 1884 } else { 1885 agg_id = TPA_END_AGG_ID(tpa_end); 1886 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1887 tpa_info = &rxr->rx_tpa[agg_id]; 1888 idx = RING_CMP(*raw_cons); 1889 if (agg_bufs) { 1890 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1891 return ERR_PTR(-EBUSY); 1892 1893 *event |= BNXT_AGG_EVENT; 1894 idx = NEXT_CMP(idx); 1895 } 1896 gro = !!TPA_END_GRO(tpa_end); 1897 } 1898 data = tpa_info->data; 1899 data_ptr = tpa_info->data_ptr; 1900 prefetch(data_ptr); 1901 len = tpa_info->len; 1902 mapping = tpa_info->mapping; 1903 1904 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1905 bnxt_abort_tpa(cpr, idx, agg_bufs); 1906 if (agg_bufs > MAX_SKB_FRAGS) 1907 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1908 agg_bufs, (int)MAX_SKB_FRAGS); 1909 return NULL; 1910 } 1911 1912 if (len <= bp->rx_copybreak) { 1913 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1914 if (!skb) { 1915 bnxt_abort_tpa(cpr, idx, agg_bufs); 1916 cpr->sw_stats->rx.rx_oom_discards += 1; 1917 return NULL; 1918 } 1919 } else { 1920 u8 *new_data; 1921 dma_addr_t new_mapping; 1922 1923 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1924 GFP_ATOMIC); 1925 if (!new_data) { 1926 bnxt_abort_tpa(cpr, idx, agg_bufs); 1927 cpr->sw_stats->rx.rx_oom_discards += 1; 1928 return NULL; 1929 } 1930 1931 tpa_info->data = new_data; 1932 tpa_info->data_ptr = new_data + bp->rx_offset; 1933 tpa_info->mapping = new_mapping; 1934 1935 skb = napi_build_skb(data, bp->rx_buf_size); 1936 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 1937 bp->rx_buf_use_size, bp->rx_dir); 1938 1939 if (!skb) { 1940 page_pool_free_va(rxr->head_pool, data, true); 1941 bnxt_abort_tpa(cpr, idx, agg_bufs); 1942 cpr->sw_stats->rx.rx_oom_discards += 1; 1943 return NULL; 1944 } 1945 skb_mark_for_recycle(skb); 1946 skb_reserve(skb, bp->rx_offset); 1947 skb_put(skb, len); 1948 } 1949 1950 if (agg_bufs) { 1951 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs, 1952 true); 1953 if (!skb) { 1954 /* Page reuse already handled by bnxt_rx_pages(). */ 1955 cpr->sw_stats->rx.rx_oom_discards += 1; 1956 return NULL; 1957 } 1958 } 1959 1960 if (tpa_info->cfa_code_valid) 1961 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1962 skb->protocol = eth_type_trans(skb, dev); 1963 1964 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1965 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1966 1967 if (tpa_info->vlan_valid && 1968 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1969 __be16 vlan_proto = htons(tpa_info->metadata >> 1970 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1971 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1972 1973 if (eth_type_vlan(vlan_proto)) { 1974 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1975 } else { 1976 dev_kfree_skb(skb); 1977 return NULL; 1978 } 1979 } 1980 1981 skb_checksum_none_assert(skb); 1982 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1983 skb->ip_summed = CHECKSUM_UNNECESSARY; 1984 skb->csum_level = 1985 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1986 } 1987 1988 if (gro) 1989 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1990 1991 return skb; 1992 } 1993 1994 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1995 struct rx_agg_cmp *rx_agg) 1996 { 1997 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1998 struct bnxt_tpa_info *tpa_info; 1999 2000 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 2001 tpa_info = &rxr->rx_tpa[agg_id]; 2002 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 2003 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 2004 } 2005 2006 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 2007 struct sk_buff *skb) 2008 { 2009 skb_mark_for_recycle(skb); 2010 2011 if (skb->dev != bp->dev) { 2012 /* this packet belongs to a vf-rep */ 2013 bnxt_vf_rep_rx(bp, skb); 2014 return; 2015 } 2016 skb_record_rx_queue(skb, bnapi->index); 2017 napi_gro_receive(&bnapi->napi, skb); 2018 } 2019 2020 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 2021 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 2022 { 2023 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 2024 2025 if (BNXT_PTP_RX_TS_VALID(flags)) 2026 goto ts_valid; 2027 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 2028 return false; 2029 2030 ts_valid: 2031 *cmpl_ts = ts; 2032 return true; 2033 } 2034 2035 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 2036 struct rx_cmp *rxcmp, 2037 struct rx_cmp_ext *rxcmp1) 2038 { 2039 __be16 vlan_proto; 2040 u16 vtag; 2041 2042 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 2043 __le32 flags2 = rxcmp1->rx_cmp_flags2; 2044 u32 meta_data; 2045 2046 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 2047 return skb; 2048 2049 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 2050 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2051 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 2052 if (eth_type_vlan(vlan_proto)) 2053 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2054 else 2055 goto vlan_err; 2056 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2057 if (RX_CMP_VLAN_VALID(rxcmp)) { 2058 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 2059 2060 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 2061 vlan_proto = htons(ETH_P_8021Q); 2062 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 2063 vlan_proto = htons(ETH_P_8021AD); 2064 else 2065 goto vlan_err; 2066 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 2067 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2068 } 2069 } 2070 return skb; 2071 vlan_err: 2072 skb_mark_for_recycle(skb); 2073 dev_kfree_skb(skb); 2074 return NULL; 2075 } 2076 2077 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 2078 struct rx_cmp *rxcmp) 2079 { 2080 u8 ext_op; 2081 2082 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2083 switch (ext_op) { 2084 case EXT_OP_INNER_4: 2085 case EXT_OP_OUTER_4: 2086 case EXT_OP_INNFL_3: 2087 case EXT_OP_OUTFL_3: 2088 return PKT_HASH_TYPE_L4; 2089 default: 2090 return PKT_HASH_TYPE_L3; 2091 } 2092 } 2093 2094 /* returns the following: 2095 * 1 - 1 packet successfully received 2096 * 0 - successful TPA_START, packet not completed yet 2097 * -EBUSY - completion ring does not have all the agg buffers yet 2098 * -ENOMEM - packet aborted due to out of memory 2099 * -EIO - packet aborted due to hw error indicated in BD 2100 */ 2101 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2102 u32 *raw_cons, u8 *event) 2103 { 2104 struct bnxt_napi *bnapi = cpr->bnapi; 2105 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2106 struct net_device *dev = bp->dev; 2107 struct rx_cmp *rxcmp; 2108 struct rx_cmp_ext *rxcmp1; 2109 u32 tmp_raw_cons = *raw_cons; 2110 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2111 struct skb_shared_info *sinfo; 2112 struct bnxt_sw_rx_bd *rx_buf; 2113 unsigned int len; 2114 u8 *data_ptr, agg_bufs, cmp_type; 2115 bool xdp_active = false; 2116 dma_addr_t dma_addr; 2117 struct sk_buff *skb; 2118 struct xdp_buff xdp; 2119 u32 flags, misc; 2120 u32 cmpl_ts; 2121 void *data; 2122 int rc = 0; 2123 2124 rxcmp = (struct rx_cmp *) 2125 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2126 2127 cmp_type = RX_CMP_TYPE(rxcmp); 2128 2129 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2130 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2131 goto next_rx_no_prod_no_len; 2132 } 2133 2134 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2135 cp_cons = RING_CMP(tmp_raw_cons); 2136 rxcmp1 = (struct rx_cmp_ext *) 2137 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2138 2139 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2140 return -EBUSY; 2141 2142 /* The valid test of the entry must be done first before 2143 * reading any further. 2144 */ 2145 dma_rmb(); 2146 prod = rxr->rx_prod; 2147 2148 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2149 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2150 bnxt_tpa_start(bp, rxr, cmp_type, 2151 (struct rx_tpa_start_cmp *)rxcmp, 2152 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2153 2154 *event |= BNXT_RX_EVENT; 2155 goto next_rx_no_prod_no_len; 2156 2157 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2158 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2159 (struct rx_tpa_end_cmp *)rxcmp, 2160 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2161 2162 if (IS_ERR(skb)) 2163 return -EBUSY; 2164 2165 rc = -ENOMEM; 2166 if (likely(skb)) { 2167 bnxt_deliver_skb(bp, bnapi, skb); 2168 rc = 1; 2169 } 2170 *event |= BNXT_RX_EVENT; 2171 goto next_rx_no_prod_no_len; 2172 } 2173 2174 cons = rxcmp->rx_cmp_opaque; 2175 if (unlikely(cons != rxr->rx_next_cons)) { 2176 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2177 2178 /* 0xffff is forced error, don't print it */ 2179 if (rxr->rx_next_cons != 0xffff) 2180 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2181 cons, rxr->rx_next_cons); 2182 bnxt_sched_reset_rxr(bp, rxr); 2183 if (rc1) 2184 return rc1; 2185 goto next_rx_no_prod_no_len; 2186 } 2187 rx_buf = &rxr->rx_buf_ring[cons]; 2188 data = rx_buf->data; 2189 data_ptr = rx_buf->data_ptr; 2190 prefetch(data_ptr); 2191 2192 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2193 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2194 2195 if (agg_bufs) { 2196 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2197 return -EBUSY; 2198 2199 cp_cons = NEXT_CMP(cp_cons); 2200 *event |= BNXT_AGG_EVENT; 2201 } 2202 *event |= BNXT_RX_EVENT; 2203 2204 rx_buf->data = NULL; 2205 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2206 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2207 2208 bnxt_reuse_rx_data(rxr, cons, data); 2209 if (agg_bufs) 2210 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2211 false); 2212 2213 rc = -EIO; 2214 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2215 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2216 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2217 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2218 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2219 rx_err); 2220 bnxt_sched_reset_rxr(bp, rxr); 2221 } 2222 } 2223 goto next_rx_no_len; 2224 } 2225 2226 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2227 len = flags >> RX_CMP_LEN_SHIFT; 2228 dma_addr = rx_buf->mapping; 2229 2230 if (bnxt_xdp_attached(bp, rxr)) { 2231 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2232 if (agg_bufs) { 2233 u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp, 2234 cp_cons, 2235 agg_bufs, 2236 false); 2237 if (!frag_len) 2238 goto oom_next_rx; 2239 2240 } 2241 xdp_active = true; 2242 } 2243 2244 if (xdp_active) { 2245 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2246 rc = 1; 2247 goto next_rx; 2248 } 2249 if (xdp_buff_has_frags(&xdp)) { 2250 sinfo = xdp_get_shared_info_from_buff(&xdp); 2251 agg_bufs = sinfo->nr_frags; 2252 } else { 2253 agg_bufs = 0; 2254 } 2255 } 2256 2257 if (len <= bp->rx_copybreak) { 2258 if (!xdp_active) 2259 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2260 else 2261 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2262 bnxt_reuse_rx_data(rxr, cons, data); 2263 if (!skb) { 2264 if (agg_bufs) { 2265 if (!xdp_active) 2266 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2267 agg_bufs, false); 2268 else 2269 bnxt_xdp_buff_frags_free(rxr, &xdp); 2270 } 2271 goto oom_next_rx; 2272 } 2273 } else { 2274 u32 payload; 2275 2276 if (rx_buf->data_ptr == data_ptr) 2277 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2278 else 2279 payload = 0; 2280 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2281 payload | len); 2282 if (!skb) 2283 goto oom_next_rx; 2284 } 2285 2286 if (agg_bufs) { 2287 if (!xdp_active) { 2288 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons, 2289 agg_bufs, false); 2290 if (!skb) 2291 goto oom_next_rx; 2292 } else { 2293 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, 2294 rxr->page_pool, &xdp); 2295 if (!skb) { 2296 /* we should be able to free the old skb here */ 2297 bnxt_xdp_buff_frags_free(rxr, &xdp); 2298 goto oom_next_rx; 2299 } 2300 } 2301 } 2302 2303 if (RX_CMP_HASH_VALID(rxcmp)) { 2304 enum pkt_hash_types type; 2305 2306 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2307 type = bnxt_rss_ext_op(bp, rxcmp); 2308 } else { 2309 u32 itypes = RX_CMP_ITYPES(rxcmp); 2310 2311 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2312 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2313 type = PKT_HASH_TYPE_L4; 2314 else 2315 type = PKT_HASH_TYPE_L3; 2316 } 2317 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2318 } 2319 2320 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2321 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2322 skb->protocol = eth_type_trans(skb, dev); 2323 2324 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2325 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2326 if (!skb) 2327 goto next_rx; 2328 } 2329 2330 skb_checksum_none_assert(skb); 2331 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2332 if (dev->features & NETIF_F_RXCSUM) { 2333 skb->ip_summed = CHECKSUM_UNNECESSARY; 2334 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2335 } 2336 } else { 2337 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2338 if (dev->features & NETIF_F_RXCSUM) 2339 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2340 } 2341 } 2342 2343 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2344 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2345 u64 ns, ts; 2346 2347 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2348 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2349 2350 ns = bnxt_timecounter_cyc2time(ptp, ts); 2351 memset(skb_hwtstamps(skb), 0, 2352 sizeof(*skb_hwtstamps(skb))); 2353 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2354 } 2355 } 2356 } 2357 bnxt_deliver_skb(bp, bnapi, skb); 2358 rc = 1; 2359 2360 next_rx: 2361 cpr->rx_packets += 1; 2362 cpr->rx_bytes += len; 2363 2364 next_rx_no_len: 2365 rxr->rx_prod = NEXT_RX(prod); 2366 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2367 2368 next_rx_no_prod_no_len: 2369 *raw_cons = tmp_raw_cons; 2370 2371 return rc; 2372 2373 oom_next_rx: 2374 cpr->sw_stats->rx.rx_oom_discards += 1; 2375 rc = -ENOMEM; 2376 goto next_rx; 2377 } 2378 2379 /* In netpoll mode, if we are using a combined completion ring, we need to 2380 * discard the rx packets and recycle the buffers. 2381 */ 2382 static int bnxt_force_rx_discard(struct bnxt *bp, 2383 struct bnxt_cp_ring_info *cpr, 2384 u32 *raw_cons, u8 *event) 2385 { 2386 u32 tmp_raw_cons = *raw_cons; 2387 struct rx_cmp_ext *rxcmp1; 2388 struct rx_cmp *rxcmp; 2389 u16 cp_cons; 2390 u8 cmp_type; 2391 int rc; 2392 2393 cp_cons = RING_CMP(tmp_raw_cons); 2394 rxcmp = (struct rx_cmp *) 2395 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2396 2397 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2398 cp_cons = RING_CMP(tmp_raw_cons); 2399 rxcmp1 = (struct rx_cmp_ext *) 2400 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2401 2402 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2403 return -EBUSY; 2404 2405 /* The valid test of the entry must be done first before 2406 * reading any further. 2407 */ 2408 dma_rmb(); 2409 cmp_type = RX_CMP_TYPE(rxcmp); 2410 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2411 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2412 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2413 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2414 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2415 struct rx_tpa_end_cmp_ext *tpa_end1; 2416 2417 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2418 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2419 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2420 } 2421 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2422 if (rc && rc != -EBUSY) 2423 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2424 return rc; 2425 } 2426 2427 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2428 { 2429 struct bnxt_fw_health *fw_health = bp->fw_health; 2430 u32 reg = fw_health->regs[reg_idx]; 2431 u32 reg_type, reg_off, val = 0; 2432 2433 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2434 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2435 switch (reg_type) { 2436 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2437 pci_read_config_dword(bp->pdev, reg_off, &val); 2438 break; 2439 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2440 reg_off = fw_health->mapped_regs[reg_idx]; 2441 fallthrough; 2442 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2443 val = readl(bp->bar0 + reg_off); 2444 break; 2445 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2446 val = readl(bp->bar1 + reg_off); 2447 break; 2448 } 2449 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2450 val &= fw_health->fw_reset_inprog_reg_mask; 2451 return val; 2452 } 2453 2454 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2455 { 2456 int i; 2457 2458 for (i = 0; i < bp->rx_nr_rings; i++) { 2459 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2460 struct bnxt_ring_grp_info *grp_info; 2461 2462 grp_info = &bp->grp_info[grp_idx]; 2463 if (grp_info->agg_fw_ring_id == ring_id) 2464 return grp_idx; 2465 } 2466 return INVALID_HW_RING_ID; 2467 } 2468 2469 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2470 { 2471 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2472 2473 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2474 return link_info->force_link_speed2; 2475 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2476 return link_info->force_pam4_link_speed; 2477 return link_info->force_link_speed; 2478 } 2479 2480 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2481 { 2482 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2483 2484 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2485 link_info->req_link_speed = link_info->force_link_speed2; 2486 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2487 switch (link_info->req_link_speed) { 2488 case BNXT_LINK_SPEED_50GB_PAM4: 2489 case BNXT_LINK_SPEED_100GB_PAM4: 2490 case BNXT_LINK_SPEED_200GB_PAM4: 2491 case BNXT_LINK_SPEED_400GB_PAM4: 2492 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2493 break; 2494 case BNXT_LINK_SPEED_100GB_PAM4_112: 2495 case BNXT_LINK_SPEED_200GB_PAM4_112: 2496 case BNXT_LINK_SPEED_400GB_PAM4_112: 2497 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2498 break; 2499 default: 2500 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2501 } 2502 return; 2503 } 2504 link_info->req_link_speed = link_info->force_link_speed; 2505 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2506 if (link_info->force_pam4_link_speed) { 2507 link_info->req_link_speed = link_info->force_pam4_link_speed; 2508 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2509 } 2510 } 2511 2512 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2513 { 2514 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2515 2516 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2517 link_info->advertising = link_info->auto_link_speeds2; 2518 return; 2519 } 2520 link_info->advertising = link_info->auto_link_speeds; 2521 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2522 } 2523 2524 static bool bnxt_force_speed_updated(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 if (link_info->req_link_speed != link_info->force_link_speed2) 2530 return true; 2531 return false; 2532 } 2533 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2534 link_info->req_link_speed != link_info->force_link_speed) 2535 return true; 2536 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2537 link_info->req_link_speed != link_info->force_pam4_link_speed) 2538 return true; 2539 return false; 2540 } 2541 2542 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2543 { 2544 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2545 2546 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2547 if (link_info->advertising != link_info->auto_link_speeds2) 2548 return true; 2549 return false; 2550 } 2551 if (link_info->advertising != link_info->auto_link_speeds || 2552 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2553 return true; 2554 return false; 2555 } 2556 2557 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2558 { 2559 u32 flags = bp->ctx->ctx_arr[type].flags; 2560 2561 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2562 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2563 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2564 } 2565 2566 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2567 { 2568 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2569 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2570 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2571 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2572 struct bnxt_bs_trace_info *bs_trace; 2573 int last_pg; 2574 2575 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2576 return; 2577 2578 mem_size = ctxm->max_entries * ctxm->entry_size; 2579 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2580 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2581 2582 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2583 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2584 2585 rmem = &ctx_pg[0].ring_mem; 2586 bs_trace = &bp->bs_trace[trace_type]; 2587 bs_trace->ctx_type = ctxm->type; 2588 bs_trace->trace_type = trace_type; 2589 if (pages > MAX_CTX_PAGES) { 2590 int last_pg_dir = rmem->nr_pages - 1; 2591 2592 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2593 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2594 } else { 2595 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2596 } 2597 bs_trace->magic_byte += magic_byte_offset; 2598 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2599 } 2600 2601 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2602 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2603 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2604 2605 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2606 (((data2) & \ 2607 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2608 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2609 2610 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2611 ((data2) & \ 2612 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2613 2614 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2615 (((data2) & \ 2616 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2617 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2618 2619 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2620 ((data1) & \ 2621 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2622 2623 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2624 (((data1) & \ 2625 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2626 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2627 2628 /* Return true if the workqueue has to be scheduled */ 2629 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2630 { 2631 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2632 2633 switch (err_type) { 2634 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2635 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2636 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2637 break; 2638 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2639 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2640 break; 2641 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2642 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2643 break; 2644 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2645 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2646 char *threshold_type; 2647 bool notify = false; 2648 char *dir_str; 2649 2650 switch (type) { 2651 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2652 threshold_type = "warning"; 2653 break; 2654 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2655 threshold_type = "critical"; 2656 break; 2657 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2658 threshold_type = "fatal"; 2659 break; 2660 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2661 threshold_type = "shutdown"; 2662 break; 2663 default: 2664 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2665 return false; 2666 } 2667 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2668 dir_str = "above"; 2669 notify = true; 2670 } else { 2671 dir_str = "below"; 2672 } 2673 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2674 dir_str, threshold_type); 2675 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2676 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2677 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2678 if (notify) { 2679 bp->thermal_threshold_type = type; 2680 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2681 return true; 2682 } 2683 return false; 2684 } 2685 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2686 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2687 break; 2688 default: 2689 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2690 err_type); 2691 break; 2692 } 2693 return false; 2694 } 2695 2696 #define BNXT_GET_EVENT_PORT(data) \ 2697 ((data) & \ 2698 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2699 2700 #define BNXT_EVENT_RING_TYPE(data2) \ 2701 ((data2) & \ 2702 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2703 2704 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2705 (BNXT_EVENT_RING_TYPE(data2) == \ 2706 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2707 2708 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2709 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2710 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2711 2712 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2713 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2714 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2715 2716 #define BNXT_PHC_BITS 48 2717 2718 static int bnxt_async_event_process(struct bnxt *bp, 2719 struct hwrm_async_event_cmpl *cmpl) 2720 { 2721 u16 event_id = le16_to_cpu(cmpl->event_id); 2722 u32 data1 = le32_to_cpu(cmpl->event_data1); 2723 u32 data2 = le32_to_cpu(cmpl->event_data2); 2724 2725 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2726 event_id, data1, data2); 2727 2728 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2729 switch (event_id) { 2730 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2731 struct bnxt_link_info *link_info = &bp->link_info; 2732 2733 if (BNXT_VF(bp)) 2734 goto async_event_process_exit; 2735 2736 /* print unsupported speed warning in forced speed mode only */ 2737 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2738 (data1 & 0x20000)) { 2739 u16 fw_speed = bnxt_get_force_speed(link_info); 2740 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2741 2742 if (speed != SPEED_UNKNOWN) 2743 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2744 speed); 2745 } 2746 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2747 } 2748 fallthrough; 2749 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2750 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2751 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2752 fallthrough; 2753 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2754 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2755 break; 2756 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2757 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2758 break; 2759 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2760 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2761 2762 if (BNXT_VF(bp)) 2763 break; 2764 2765 if (bp->pf.port_id != port_id) 2766 break; 2767 2768 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2769 break; 2770 } 2771 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2772 if (BNXT_PF(bp)) 2773 goto async_event_process_exit; 2774 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2775 break; 2776 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2777 char *type_str = "Solicited"; 2778 2779 if (!bp->fw_health) 2780 goto async_event_process_exit; 2781 2782 bp->fw_reset_timestamp = jiffies; 2783 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2784 if (!bp->fw_reset_min_dsecs) 2785 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2786 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2787 if (!bp->fw_reset_max_dsecs) 2788 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2789 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2790 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2791 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2792 type_str = "Fatal"; 2793 bp->fw_health->fatalities++; 2794 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2795 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2796 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2797 type_str = "Non-fatal"; 2798 bp->fw_health->survivals++; 2799 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2800 } 2801 netif_warn(bp, hw, bp->dev, 2802 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2803 type_str, data1, data2, 2804 bp->fw_reset_min_dsecs * 100, 2805 bp->fw_reset_max_dsecs * 100); 2806 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2807 break; 2808 } 2809 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2810 struct bnxt_fw_health *fw_health = bp->fw_health; 2811 char *status_desc = "healthy"; 2812 u32 status; 2813 2814 if (!fw_health) 2815 goto async_event_process_exit; 2816 2817 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2818 fw_health->enabled = false; 2819 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2820 break; 2821 } 2822 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2823 fw_health->tmr_multiplier = 2824 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2825 bp->current_interval * 10); 2826 fw_health->tmr_counter = fw_health->tmr_multiplier; 2827 if (!fw_health->enabled) 2828 fw_health->last_fw_heartbeat = 2829 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2830 fw_health->last_fw_reset_cnt = 2831 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2832 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2833 if (status != BNXT_FW_STATUS_HEALTHY) 2834 status_desc = "unhealthy"; 2835 netif_info(bp, drv, bp->dev, 2836 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2837 fw_health->primary ? "primary" : "backup", status, 2838 status_desc, fw_health->last_fw_reset_cnt); 2839 if (!fw_health->enabled) { 2840 /* Make sure tmr_counter is set and visible to 2841 * bnxt_health_check() before setting enabled to true. 2842 */ 2843 smp_wmb(); 2844 fw_health->enabled = true; 2845 } 2846 goto async_event_process_exit; 2847 } 2848 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2849 netif_notice(bp, hw, bp->dev, 2850 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2851 data1, data2); 2852 goto async_event_process_exit; 2853 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2854 struct bnxt_rx_ring_info *rxr; 2855 u16 grp_idx; 2856 2857 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2858 goto async_event_process_exit; 2859 2860 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2861 BNXT_EVENT_RING_TYPE(data2), data1); 2862 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2863 goto async_event_process_exit; 2864 2865 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2866 if (grp_idx == INVALID_HW_RING_ID) { 2867 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2868 data1); 2869 goto async_event_process_exit; 2870 } 2871 rxr = bp->bnapi[grp_idx]->rx_ring; 2872 bnxt_sched_reset_rxr(bp, rxr); 2873 goto async_event_process_exit; 2874 } 2875 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2876 struct bnxt_fw_health *fw_health = bp->fw_health; 2877 2878 netif_notice(bp, hw, bp->dev, 2879 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2880 data1, data2); 2881 if (fw_health) { 2882 fw_health->echo_req_data1 = data1; 2883 fw_health->echo_req_data2 = data2; 2884 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2885 break; 2886 } 2887 goto async_event_process_exit; 2888 } 2889 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2890 bnxt_ptp_pps_event(bp, data1, data2); 2891 goto async_event_process_exit; 2892 } 2893 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2894 if (bnxt_event_error_report(bp, data1, data2)) 2895 break; 2896 goto async_event_process_exit; 2897 } 2898 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2899 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2900 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2901 if (BNXT_PTP_USE_RTC(bp)) { 2902 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2903 unsigned long flags; 2904 u64 ns; 2905 2906 if (!ptp) 2907 goto async_event_process_exit; 2908 2909 bnxt_ptp_update_current_time(bp); 2910 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2911 BNXT_PHC_BITS) | ptp->current_time); 2912 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2913 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2914 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2915 } 2916 break; 2917 } 2918 goto async_event_process_exit; 2919 } 2920 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2921 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2922 2923 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2924 goto async_event_process_exit; 2925 } 2926 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2927 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2928 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2929 2930 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2931 goto async_event_process_exit; 2932 } 2933 default: 2934 goto async_event_process_exit; 2935 } 2936 __bnxt_queue_sp_work(bp); 2937 async_event_process_exit: 2938 bnxt_ulp_async_events(bp, cmpl); 2939 return 0; 2940 } 2941 2942 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2943 { 2944 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2945 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2946 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2947 (struct hwrm_fwd_req_cmpl *)txcmp; 2948 2949 switch (cmpl_type) { 2950 case CMPL_BASE_TYPE_HWRM_DONE: 2951 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2952 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2953 break; 2954 2955 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2956 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2957 2958 if ((vf_id < bp->pf.first_vf_id) || 2959 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2960 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2961 vf_id); 2962 return -EINVAL; 2963 } 2964 2965 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2966 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2967 break; 2968 2969 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2970 bnxt_async_event_process(bp, 2971 (struct hwrm_async_event_cmpl *)txcmp); 2972 break; 2973 2974 default: 2975 break; 2976 } 2977 2978 return 0; 2979 } 2980 2981 static bool bnxt_vnic_is_active(struct bnxt *bp) 2982 { 2983 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 2984 2985 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 2986 } 2987 2988 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2989 { 2990 struct bnxt_napi *bnapi = dev_instance; 2991 struct bnxt *bp = bnapi->bp; 2992 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2993 u32 cons = RING_CMP(cpr->cp_raw_cons); 2994 2995 cpr->event_ctr++; 2996 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2997 napi_schedule(&bnapi->napi); 2998 return IRQ_HANDLED; 2999 } 3000 3001 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 3002 { 3003 u32 raw_cons = cpr->cp_raw_cons; 3004 u16 cons = RING_CMP(raw_cons); 3005 struct tx_cmp *txcmp; 3006 3007 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3008 3009 return TX_CMP_VALID(txcmp, raw_cons); 3010 } 3011 3012 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3013 int budget) 3014 { 3015 struct bnxt_napi *bnapi = cpr->bnapi; 3016 u32 raw_cons = cpr->cp_raw_cons; 3017 bool flush_xdp = false; 3018 u32 cons; 3019 int rx_pkts = 0; 3020 u8 event = 0; 3021 struct tx_cmp *txcmp; 3022 3023 cpr->has_more_work = 0; 3024 cpr->had_work_done = 1; 3025 while (1) { 3026 u8 cmp_type; 3027 int rc; 3028 3029 cons = RING_CMP(raw_cons); 3030 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3031 3032 if (!TX_CMP_VALID(txcmp, raw_cons)) 3033 break; 3034 3035 /* The valid test of the entry must be done first before 3036 * reading any further. 3037 */ 3038 dma_rmb(); 3039 cmp_type = TX_CMP_TYPE(txcmp); 3040 if (cmp_type == CMP_TYPE_TX_L2_CMP || 3041 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 3042 u32 opaque = txcmp->tx_cmp_opaque; 3043 struct bnxt_tx_ring_info *txr; 3044 u16 tx_freed; 3045 3046 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 3047 event |= BNXT_TX_CMP_EVENT; 3048 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 3049 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 3050 else 3051 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 3052 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 3053 bp->tx_ring_mask; 3054 /* return full budget so NAPI will complete. */ 3055 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 3056 rx_pkts = budget; 3057 raw_cons = NEXT_RAW_CMP(raw_cons); 3058 if (budget) 3059 cpr->has_more_work = 1; 3060 break; 3061 } 3062 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 3063 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 3064 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 3065 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 3066 if (likely(budget)) 3067 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3068 else 3069 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 3070 &event); 3071 if (event & BNXT_REDIRECT_EVENT) 3072 flush_xdp = true; 3073 if (likely(rc >= 0)) 3074 rx_pkts += rc; 3075 /* Increment rx_pkts when rc is -ENOMEM to count towards 3076 * the NAPI budget. Otherwise, we may potentially loop 3077 * here forever if we consistently cannot allocate 3078 * buffers. 3079 */ 3080 else if (rc == -ENOMEM && budget) 3081 rx_pkts++; 3082 else if (rc == -EBUSY) /* partial completion */ 3083 break; 3084 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3085 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3086 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3087 bnxt_hwrm_handler(bp, txcmp); 3088 } 3089 raw_cons = NEXT_RAW_CMP(raw_cons); 3090 3091 if (rx_pkts && rx_pkts == budget) { 3092 cpr->has_more_work = 1; 3093 break; 3094 } 3095 } 3096 3097 if (flush_xdp) { 3098 xdp_do_flush(); 3099 event &= ~BNXT_REDIRECT_EVENT; 3100 } 3101 3102 if (event & BNXT_TX_EVENT) { 3103 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3104 u16 prod = txr->tx_prod; 3105 3106 /* Sync BD data before updating doorbell */ 3107 wmb(); 3108 3109 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3110 event &= ~BNXT_TX_EVENT; 3111 } 3112 3113 cpr->cp_raw_cons = raw_cons; 3114 bnapi->events |= event; 3115 return rx_pkts; 3116 } 3117 3118 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3119 int budget) 3120 { 3121 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3122 bnapi->tx_int(bp, bnapi, budget); 3123 3124 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3125 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3126 3127 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3128 bnapi->events &= ~BNXT_RX_EVENT; 3129 } 3130 if (bnapi->events & BNXT_AGG_EVENT) { 3131 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3132 3133 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3134 bnapi->events &= ~BNXT_AGG_EVENT; 3135 } 3136 } 3137 3138 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3139 int budget) 3140 { 3141 struct bnxt_napi *bnapi = cpr->bnapi; 3142 int rx_pkts; 3143 3144 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3145 3146 /* ACK completion ring before freeing tx ring and producing new 3147 * buffers in rx/agg rings to prevent overflowing the completion 3148 * ring. 3149 */ 3150 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3151 3152 __bnxt_poll_work_done(bp, bnapi, budget); 3153 return rx_pkts; 3154 } 3155 3156 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3157 { 3158 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3159 struct bnxt *bp = bnapi->bp; 3160 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3161 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3162 struct tx_cmp *txcmp; 3163 struct rx_cmp_ext *rxcmp1; 3164 u32 cp_cons, tmp_raw_cons; 3165 u32 raw_cons = cpr->cp_raw_cons; 3166 bool flush_xdp = false; 3167 u32 rx_pkts = 0; 3168 u8 event = 0; 3169 3170 while (1) { 3171 int rc; 3172 3173 cp_cons = RING_CMP(raw_cons); 3174 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3175 3176 if (!TX_CMP_VALID(txcmp, raw_cons)) 3177 break; 3178 3179 /* The valid test of the entry must be done first before 3180 * reading any further. 3181 */ 3182 dma_rmb(); 3183 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3184 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3185 cp_cons = RING_CMP(tmp_raw_cons); 3186 rxcmp1 = (struct rx_cmp_ext *) 3187 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3188 3189 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3190 break; 3191 3192 /* force an error to recycle the buffer */ 3193 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3194 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3195 3196 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3197 if (likely(rc == -EIO) && budget) 3198 rx_pkts++; 3199 else if (rc == -EBUSY) /* partial completion */ 3200 break; 3201 if (event & BNXT_REDIRECT_EVENT) 3202 flush_xdp = true; 3203 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3204 CMPL_BASE_TYPE_HWRM_DONE)) { 3205 bnxt_hwrm_handler(bp, txcmp); 3206 } else { 3207 netdev_err(bp->dev, 3208 "Invalid completion received on special ring\n"); 3209 } 3210 raw_cons = NEXT_RAW_CMP(raw_cons); 3211 3212 if (rx_pkts == budget) 3213 break; 3214 } 3215 3216 cpr->cp_raw_cons = raw_cons; 3217 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3218 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3219 3220 if (event & BNXT_AGG_EVENT) 3221 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3222 if (flush_xdp) 3223 xdp_do_flush(); 3224 3225 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3226 napi_complete_done(napi, rx_pkts); 3227 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3228 } 3229 return rx_pkts; 3230 } 3231 3232 static int bnxt_poll(struct napi_struct *napi, int budget) 3233 { 3234 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3235 struct bnxt *bp = bnapi->bp; 3236 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3237 int work_done = 0; 3238 3239 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3240 napi_complete(napi); 3241 return 0; 3242 } 3243 while (1) { 3244 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3245 3246 if (work_done >= budget) { 3247 if (!budget) 3248 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3249 break; 3250 } 3251 3252 if (!bnxt_has_work(bp, cpr)) { 3253 if (napi_complete_done(napi, work_done)) 3254 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3255 break; 3256 } 3257 } 3258 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3259 struct dim_sample dim_sample = {}; 3260 3261 dim_update_sample(cpr->event_ctr, 3262 cpr->rx_packets, 3263 cpr->rx_bytes, 3264 &dim_sample); 3265 net_dim(&cpr->dim, &dim_sample); 3266 } 3267 return work_done; 3268 } 3269 3270 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3271 { 3272 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3273 int i, work_done = 0; 3274 3275 for (i = 0; i < cpr->cp_ring_count; i++) { 3276 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3277 3278 if (cpr2->had_nqe_notify) { 3279 work_done += __bnxt_poll_work(bp, cpr2, 3280 budget - work_done); 3281 cpr->has_more_work |= cpr2->has_more_work; 3282 } 3283 } 3284 return work_done; 3285 } 3286 3287 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3288 u64 dbr_type, int budget) 3289 { 3290 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3291 int i; 3292 3293 for (i = 0; i < cpr->cp_ring_count; i++) { 3294 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3295 struct bnxt_db_info *db; 3296 3297 if (cpr2->had_work_done) { 3298 u32 tgl = 0; 3299 3300 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3301 cpr2->had_nqe_notify = 0; 3302 tgl = cpr2->toggle; 3303 } 3304 db = &cpr2->cp_db; 3305 bnxt_writeq(bp, 3306 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3307 DB_RING_IDX(db, cpr2->cp_raw_cons), 3308 db->doorbell); 3309 cpr2->had_work_done = 0; 3310 } 3311 } 3312 __bnxt_poll_work_done(bp, bnapi, budget); 3313 } 3314 3315 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3316 { 3317 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3318 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3319 struct bnxt_cp_ring_info *cpr_rx; 3320 u32 raw_cons = cpr->cp_raw_cons; 3321 struct bnxt *bp = bnapi->bp; 3322 struct nqe_cn *nqcmp; 3323 int work_done = 0; 3324 u32 cons; 3325 3326 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3327 napi_complete(napi); 3328 return 0; 3329 } 3330 if (cpr->has_more_work) { 3331 cpr->has_more_work = 0; 3332 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3333 } 3334 while (1) { 3335 u16 type; 3336 3337 cons = RING_CMP(raw_cons); 3338 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3339 3340 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3341 if (cpr->has_more_work) 3342 break; 3343 3344 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3345 budget); 3346 cpr->cp_raw_cons = raw_cons; 3347 if (napi_complete_done(napi, work_done)) 3348 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3349 cpr->cp_raw_cons); 3350 goto poll_done; 3351 } 3352 3353 /* The valid test of the entry must be done first before 3354 * reading any further. 3355 */ 3356 dma_rmb(); 3357 3358 type = le16_to_cpu(nqcmp->type); 3359 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3360 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3361 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3362 struct bnxt_cp_ring_info *cpr2; 3363 3364 /* No more budget for RX work */ 3365 if (budget && work_done >= budget && 3366 cq_type == BNXT_NQ_HDL_TYPE_RX) 3367 break; 3368 3369 idx = BNXT_NQ_HDL_IDX(idx); 3370 cpr2 = &cpr->cp_ring_arr[idx]; 3371 cpr2->had_nqe_notify = 1; 3372 cpr2->toggle = NQE_CN_TOGGLE(type); 3373 work_done += __bnxt_poll_work(bp, cpr2, 3374 budget - work_done); 3375 cpr->has_more_work |= cpr2->has_more_work; 3376 } else { 3377 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3378 } 3379 raw_cons = NEXT_RAW_CMP(raw_cons); 3380 } 3381 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3382 if (raw_cons != cpr->cp_raw_cons) { 3383 cpr->cp_raw_cons = raw_cons; 3384 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3385 } 3386 poll_done: 3387 cpr_rx = &cpr->cp_ring_arr[0]; 3388 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3389 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3390 struct dim_sample dim_sample = {}; 3391 3392 dim_update_sample(cpr->event_ctr, 3393 cpr_rx->rx_packets, 3394 cpr_rx->rx_bytes, 3395 &dim_sample); 3396 net_dim(&cpr->dim, &dim_sample); 3397 } 3398 return work_done; 3399 } 3400 3401 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp, 3402 struct bnxt_tx_ring_info *txr, int idx) 3403 { 3404 int i, max_idx; 3405 struct pci_dev *pdev = bp->pdev; 3406 3407 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3408 3409 for (i = 0; i < max_idx;) { 3410 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i]; 3411 struct sk_buff *skb; 3412 int j, last; 3413 3414 if (idx < bp->tx_nr_rings_xdp && 3415 tx_buf->action == XDP_REDIRECT) { 3416 dma_unmap_single(&pdev->dev, 3417 dma_unmap_addr(tx_buf, mapping), 3418 dma_unmap_len(tx_buf, len), 3419 DMA_TO_DEVICE); 3420 xdp_return_frame(tx_buf->xdpf); 3421 tx_buf->action = 0; 3422 tx_buf->xdpf = NULL; 3423 i++; 3424 continue; 3425 } 3426 3427 skb = tx_buf->skb; 3428 if (!skb) { 3429 i++; 3430 continue; 3431 } 3432 3433 tx_buf->skb = NULL; 3434 3435 if (tx_buf->is_push) { 3436 dev_kfree_skb(skb); 3437 i += 2; 3438 continue; 3439 } 3440 3441 dma_unmap_single(&pdev->dev, 3442 dma_unmap_addr(tx_buf, mapping), 3443 skb_headlen(skb), 3444 DMA_TO_DEVICE); 3445 3446 last = tx_buf->nr_frags; 3447 i += 2; 3448 for (j = 0; j < last; j++, i++) { 3449 int ring_idx = i & bp->tx_ring_mask; 3450 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 3451 3452 tx_buf = &txr->tx_buf_ring[ring_idx]; 3453 netmem_dma_unmap_page_attrs(&pdev->dev, 3454 dma_unmap_addr(tx_buf, 3455 mapping), 3456 skb_frag_size(frag), 3457 DMA_TO_DEVICE, 0); 3458 } 3459 dev_kfree_skb(skb); 3460 } 3461 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx)); 3462 } 3463 3464 static void bnxt_free_tx_skbs(struct bnxt *bp) 3465 { 3466 int i; 3467 3468 if (!bp->tx_ring) 3469 return; 3470 3471 for (i = 0; i < bp->tx_nr_rings; i++) { 3472 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3473 3474 if (!txr->tx_buf_ring) 3475 continue; 3476 3477 bnxt_free_one_tx_ring_skbs(bp, txr, i); 3478 } 3479 3480 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 3481 bnxt_ptp_free_txts_skbs(bp->ptp_cfg); 3482 } 3483 3484 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3485 { 3486 int i, max_idx; 3487 3488 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3489 3490 for (i = 0; i < max_idx; i++) { 3491 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3492 void *data = rx_buf->data; 3493 3494 if (!data) 3495 continue; 3496 3497 rx_buf->data = NULL; 3498 if (BNXT_RX_PAGE_MODE(bp)) 3499 page_pool_recycle_direct(rxr->page_pool, data); 3500 else 3501 page_pool_free_va(rxr->head_pool, data, true); 3502 } 3503 } 3504 3505 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3506 { 3507 int i, max_idx; 3508 3509 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3510 3511 for (i = 0; i < max_idx; i++) { 3512 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3513 netmem_ref netmem = rx_agg_buf->netmem; 3514 3515 if (!netmem) 3516 continue; 3517 3518 rx_agg_buf->netmem = 0; 3519 __clear_bit(i, rxr->rx_agg_bmap); 3520 3521 page_pool_recycle_direct_netmem(rxr->page_pool, netmem); 3522 } 3523 } 3524 3525 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3526 struct bnxt_rx_ring_info *rxr) 3527 { 3528 int i; 3529 3530 for (i = 0; i < bp->max_tpa; i++) { 3531 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3532 u8 *data = tpa_info->data; 3533 3534 if (!data) 3535 continue; 3536 3537 tpa_info->data = NULL; 3538 page_pool_free_va(rxr->head_pool, data, false); 3539 } 3540 } 3541 3542 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3543 struct bnxt_rx_ring_info *rxr) 3544 { 3545 struct bnxt_tpa_idx_map *map; 3546 3547 if (!rxr->rx_tpa) 3548 goto skip_rx_tpa_free; 3549 3550 bnxt_free_one_tpa_info_data(bp, rxr); 3551 3552 skip_rx_tpa_free: 3553 if (!rxr->rx_buf_ring) 3554 goto skip_rx_buf_free; 3555 3556 bnxt_free_one_rx_ring(bp, rxr); 3557 3558 skip_rx_buf_free: 3559 if (!rxr->rx_agg_ring) 3560 goto skip_rx_agg_free; 3561 3562 bnxt_free_one_rx_agg_ring(bp, rxr); 3563 3564 skip_rx_agg_free: 3565 map = rxr->rx_tpa_idx_map; 3566 if (map) 3567 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3568 } 3569 3570 static void bnxt_free_rx_skbs(struct bnxt *bp) 3571 { 3572 int i; 3573 3574 if (!bp->rx_ring) 3575 return; 3576 3577 for (i = 0; i < bp->rx_nr_rings; i++) 3578 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3579 } 3580 3581 static void bnxt_free_skbs(struct bnxt *bp) 3582 { 3583 bnxt_free_tx_skbs(bp); 3584 bnxt_free_rx_skbs(bp); 3585 } 3586 3587 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3588 { 3589 u8 init_val = ctxm->init_value; 3590 u16 offset = ctxm->init_offset; 3591 u8 *p2 = p; 3592 int i; 3593 3594 if (!init_val) 3595 return; 3596 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3597 memset(p, init_val, len); 3598 return; 3599 } 3600 for (i = 0; i < len; i += ctxm->entry_size) 3601 *(p2 + i + offset) = init_val; 3602 } 3603 3604 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3605 void *buf, size_t offset, size_t head, 3606 size_t tail) 3607 { 3608 int i, head_page, start_idx, source_offset; 3609 size_t len, rem_len, total_len, max_bytes; 3610 3611 head_page = head / rmem->page_size; 3612 source_offset = head % rmem->page_size; 3613 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3614 if (!total_len) 3615 total_len = MAX_CTX_BYTES; 3616 start_idx = head_page % MAX_CTX_PAGES; 3617 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3618 source_offset; 3619 total_len = min(total_len, max_bytes); 3620 rem_len = total_len; 3621 3622 for (i = start_idx; rem_len; i++, source_offset = 0) { 3623 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3624 if (buf) 3625 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3626 len); 3627 offset += len; 3628 rem_len -= len; 3629 } 3630 return total_len; 3631 } 3632 3633 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3634 { 3635 struct pci_dev *pdev = bp->pdev; 3636 int i; 3637 3638 if (!rmem->pg_arr) 3639 goto skip_pages; 3640 3641 for (i = 0; i < rmem->nr_pages; i++) { 3642 if (!rmem->pg_arr[i]) 3643 continue; 3644 3645 dma_free_coherent(&pdev->dev, rmem->page_size, 3646 rmem->pg_arr[i], rmem->dma_arr[i]); 3647 3648 rmem->pg_arr[i] = NULL; 3649 } 3650 skip_pages: 3651 if (rmem->pg_tbl) { 3652 size_t pg_tbl_size = rmem->nr_pages * 8; 3653 3654 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3655 pg_tbl_size = rmem->page_size; 3656 dma_free_coherent(&pdev->dev, pg_tbl_size, 3657 rmem->pg_tbl, rmem->pg_tbl_map); 3658 rmem->pg_tbl = NULL; 3659 } 3660 if (rmem->vmem_size && *rmem->vmem) { 3661 vfree(*rmem->vmem); 3662 *rmem->vmem = NULL; 3663 } 3664 } 3665 3666 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3667 { 3668 struct pci_dev *pdev = bp->pdev; 3669 u64 valid_bit = 0; 3670 int i; 3671 3672 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3673 valid_bit = PTU_PTE_VALID; 3674 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3675 size_t pg_tbl_size = rmem->nr_pages * 8; 3676 3677 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3678 pg_tbl_size = rmem->page_size; 3679 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3680 &rmem->pg_tbl_map, 3681 GFP_KERNEL); 3682 if (!rmem->pg_tbl) 3683 return -ENOMEM; 3684 } 3685 3686 for (i = 0; i < rmem->nr_pages; i++) { 3687 u64 extra_bits = valid_bit; 3688 3689 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3690 rmem->page_size, 3691 &rmem->dma_arr[i], 3692 GFP_KERNEL); 3693 if (!rmem->pg_arr[i]) 3694 return -ENOMEM; 3695 3696 if (rmem->ctx_mem) 3697 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3698 rmem->page_size); 3699 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3700 if (i == rmem->nr_pages - 2 && 3701 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3702 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3703 else if (i == rmem->nr_pages - 1 && 3704 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3705 extra_bits |= PTU_PTE_LAST; 3706 rmem->pg_tbl[i] = 3707 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3708 } 3709 } 3710 3711 if (rmem->vmem_size) { 3712 *rmem->vmem = vzalloc(rmem->vmem_size); 3713 if (!(*rmem->vmem)) 3714 return -ENOMEM; 3715 } 3716 return 0; 3717 } 3718 3719 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3720 struct bnxt_rx_ring_info *rxr) 3721 { 3722 int i; 3723 3724 kfree(rxr->rx_tpa_idx_map); 3725 rxr->rx_tpa_idx_map = NULL; 3726 if (rxr->rx_tpa) { 3727 for (i = 0; i < bp->max_tpa; i++) { 3728 kfree(rxr->rx_tpa[i].agg_arr); 3729 rxr->rx_tpa[i].agg_arr = NULL; 3730 } 3731 } 3732 kfree(rxr->rx_tpa); 3733 rxr->rx_tpa = NULL; 3734 } 3735 3736 static void bnxt_free_tpa_info(struct bnxt *bp) 3737 { 3738 int i; 3739 3740 for (i = 0; i < bp->rx_nr_rings; i++) { 3741 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3742 3743 bnxt_free_one_tpa_info(bp, rxr); 3744 } 3745 } 3746 3747 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3748 struct bnxt_rx_ring_info *rxr) 3749 { 3750 struct rx_agg_cmp *agg; 3751 int i; 3752 3753 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3754 GFP_KERNEL); 3755 if (!rxr->rx_tpa) 3756 return -ENOMEM; 3757 3758 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3759 return 0; 3760 for (i = 0; i < bp->max_tpa; i++) { 3761 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3762 if (!agg) 3763 return -ENOMEM; 3764 rxr->rx_tpa[i].agg_arr = agg; 3765 } 3766 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3767 GFP_KERNEL); 3768 if (!rxr->rx_tpa_idx_map) 3769 return -ENOMEM; 3770 3771 return 0; 3772 } 3773 3774 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3775 { 3776 int i, rc; 3777 3778 bp->max_tpa = MAX_TPA; 3779 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3780 if (!bp->max_tpa_v2) 3781 return 0; 3782 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3783 } 3784 3785 for (i = 0; i < bp->rx_nr_rings; i++) { 3786 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3787 3788 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3789 if (rc) 3790 return rc; 3791 } 3792 return 0; 3793 } 3794 3795 static void bnxt_free_rx_rings(struct bnxt *bp) 3796 { 3797 int i; 3798 3799 if (!bp->rx_ring) 3800 return; 3801 3802 bnxt_free_tpa_info(bp); 3803 for (i = 0; i < bp->rx_nr_rings; i++) { 3804 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3805 struct bnxt_ring_struct *ring; 3806 3807 if (rxr->xdp_prog) 3808 bpf_prog_put(rxr->xdp_prog); 3809 3810 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3811 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3812 3813 page_pool_destroy(rxr->page_pool); 3814 page_pool_destroy(rxr->head_pool); 3815 rxr->page_pool = rxr->head_pool = NULL; 3816 3817 kfree(rxr->rx_agg_bmap); 3818 rxr->rx_agg_bmap = NULL; 3819 3820 ring = &rxr->rx_ring_struct; 3821 bnxt_free_ring(bp, &ring->ring_mem); 3822 3823 ring = &rxr->rx_agg_ring_struct; 3824 bnxt_free_ring(bp, &ring->ring_mem); 3825 } 3826 } 3827 3828 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3829 struct bnxt_rx_ring_info *rxr, 3830 int numa_node) 3831 { 3832 const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE; 3833 const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K; 3834 struct page_pool_params pp = { 0 }; 3835 struct page_pool *pool; 3836 3837 pp.pool_size = bp->rx_agg_ring_size / agg_size_fac; 3838 if (BNXT_RX_PAGE_MODE(bp)) 3839 pp.pool_size += bp->rx_ring_size / rx_size_fac; 3840 pp.nid = numa_node; 3841 pp.netdev = bp->dev; 3842 pp.dev = &bp->pdev->dev; 3843 pp.dma_dir = bp->rx_dir; 3844 pp.max_len = PAGE_SIZE; 3845 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | 3846 PP_FLAG_ALLOW_UNREADABLE_NETMEM; 3847 pp.queue_idx = rxr->bnapi->index; 3848 3849 pool = page_pool_create(&pp); 3850 if (IS_ERR(pool)) 3851 return PTR_ERR(pool); 3852 rxr->page_pool = pool; 3853 3854 rxr->need_head_pool = page_pool_is_unreadable(pool); 3855 if (bnxt_separate_head_pool(rxr)) { 3856 pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024); 3857 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3858 pool = page_pool_create(&pp); 3859 if (IS_ERR(pool)) 3860 goto err_destroy_pp; 3861 } else { 3862 page_pool_get(pool); 3863 } 3864 rxr->head_pool = pool; 3865 3866 return 0; 3867 3868 err_destroy_pp: 3869 page_pool_destroy(rxr->page_pool); 3870 rxr->page_pool = NULL; 3871 return PTR_ERR(pool); 3872 } 3873 3874 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr) 3875 { 3876 page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi); 3877 page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi); 3878 } 3879 3880 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3881 { 3882 u16 mem_size; 3883 3884 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3885 mem_size = rxr->rx_agg_bmap_size / 8; 3886 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3887 if (!rxr->rx_agg_bmap) 3888 return -ENOMEM; 3889 3890 return 0; 3891 } 3892 3893 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3894 { 3895 int numa_node = dev_to_node(&bp->pdev->dev); 3896 int i, rc = 0, agg_rings = 0, cpu; 3897 3898 if (!bp->rx_ring) 3899 return -ENOMEM; 3900 3901 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3902 agg_rings = 1; 3903 3904 for (i = 0; i < bp->rx_nr_rings; i++) { 3905 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3906 struct bnxt_ring_struct *ring; 3907 int cpu_node; 3908 3909 ring = &rxr->rx_ring_struct; 3910 3911 cpu = cpumask_local_spread(i, numa_node); 3912 cpu_node = cpu_to_node(cpu); 3913 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3914 i, cpu_node); 3915 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3916 if (rc) 3917 return rc; 3918 bnxt_enable_rx_page_pool(rxr); 3919 3920 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3921 if (rc < 0) 3922 return rc; 3923 3924 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3925 MEM_TYPE_PAGE_POOL, 3926 rxr->page_pool); 3927 if (rc) { 3928 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3929 return rc; 3930 } 3931 3932 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3933 if (rc) 3934 return rc; 3935 3936 ring->grp_idx = i; 3937 if (agg_rings) { 3938 ring = &rxr->rx_agg_ring_struct; 3939 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3940 if (rc) 3941 return rc; 3942 3943 ring->grp_idx = i; 3944 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 3945 if (rc) 3946 return rc; 3947 } 3948 } 3949 if (bp->flags & BNXT_FLAG_TPA) 3950 rc = bnxt_alloc_tpa_info(bp); 3951 return rc; 3952 } 3953 3954 static void bnxt_free_tx_rings(struct bnxt *bp) 3955 { 3956 int i; 3957 struct pci_dev *pdev = bp->pdev; 3958 3959 if (!bp->tx_ring) 3960 return; 3961 3962 for (i = 0; i < bp->tx_nr_rings; i++) { 3963 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3964 struct bnxt_ring_struct *ring; 3965 3966 if (txr->tx_push) { 3967 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3968 txr->tx_push, txr->tx_push_mapping); 3969 txr->tx_push = NULL; 3970 } 3971 3972 ring = &txr->tx_ring_struct; 3973 3974 bnxt_free_ring(bp, &ring->ring_mem); 3975 } 3976 } 3977 3978 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3979 ((tc) * (bp)->tx_nr_rings_per_tc) 3980 3981 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3982 ((tx) % (bp)->tx_nr_rings_per_tc) 3983 3984 #define BNXT_RING_TO_TC(bp, tx) \ 3985 ((tx) / (bp)->tx_nr_rings_per_tc) 3986 3987 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3988 { 3989 int i, j, rc; 3990 struct pci_dev *pdev = bp->pdev; 3991 3992 bp->tx_push_size = 0; 3993 if (bp->tx_push_thresh) { 3994 int push_size; 3995 3996 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3997 bp->tx_push_thresh); 3998 3999 if (push_size > 256) { 4000 push_size = 0; 4001 bp->tx_push_thresh = 0; 4002 } 4003 4004 bp->tx_push_size = push_size; 4005 } 4006 4007 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 4008 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4009 struct bnxt_ring_struct *ring; 4010 u8 qidx; 4011 4012 ring = &txr->tx_ring_struct; 4013 4014 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4015 if (rc) 4016 return rc; 4017 4018 ring->grp_idx = txr->bnapi->index; 4019 if (bp->tx_push_size) { 4020 dma_addr_t mapping; 4021 4022 /* One pre-allocated DMA buffer to backup 4023 * TX push operation 4024 */ 4025 txr->tx_push = dma_alloc_coherent(&pdev->dev, 4026 bp->tx_push_size, 4027 &txr->tx_push_mapping, 4028 GFP_KERNEL); 4029 4030 if (!txr->tx_push) 4031 return -ENOMEM; 4032 4033 mapping = txr->tx_push_mapping + 4034 sizeof(struct tx_push_bd); 4035 txr->data_mapping = cpu_to_le64(mapping); 4036 } 4037 qidx = bp->tc_to_qidx[j]; 4038 ring->queue_id = bp->q_info[qidx].queue_id; 4039 spin_lock_init(&txr->xdp_tx_lock); 4040 if (i < bp->tx_nr_rings_xdp) 4041 continue; 4042 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 4043 j++; 4044 } 4045 return 0; 4046 } 4047 4048 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 4049 { 4050 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4051 4052 kfree(cpr->cp_desc_ring); 4053 cpr->cp_desc_ring = NULL; 4054 ring->ring_mem.pg_arr = NULL; 4055 kfree(cpr->cp_desc_mapping); 4056 cpr->cp_desc_mapping = NULL; 4057 ring->ring_mem.dma_arr = NULL; 4058 } 4059 4060 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 4061 { 4062 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 4063 if (!cpr->cp_desc_ring) 4064 return -ENOMEM; 4065 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 4066 GFP_KERNEL); 4067 if (!cpr->cp_desc_mapping) 4068 return -ENOMEM; 4069 return 0; 4070 } 4071 4072 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 4073 { 4074 int i; 4075 4076 if (!bp->bnapi) 4077 return; 4078 for (i = 0; i < bp->cp_nr_rings; i++) { 4079 struct bnxt_napi *bnapi = bp->bnapi[i]; 4080 4081 if (!bnapi) 4082 continue; 4083 bnxt_free_cp_arrays(&bnapi->cp_ring); 4084 } 4085 } 4086 4087 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 4088 { 4089 int i, n = bp->cp_nr_pages; 4090 4091 for (i = 0; i < bp->cp_nr_rings; i++) { 4092 struct bnxt_napi *bnapi = bp->bnapi[i]; 4093 int rc; 4094 4095 if (!bnapi) 4096 continue; 4097 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 4098 if (rc) 4099 return rc; 4100 } 4101 return 0; 4102 } 4103 4104 static void bnxt_free_cp_rings(struct bnxt *bp) 4105 { 4106 int i; 4107 4108 if (!bp->bnapi) 4109 return; 4110 4111 for (i = 0; i < bp->cp_nr_rings; i++) { 4112 struct bnxt_napi *bnapi = bp->bnapi[i]; 4113 struct bnxt_cp_ring_info *cpr; 4114 struct bnxt_ring_struct *ring; 4115 int j; 4116 4117 if (!bnapi) 4118 continue; 4119 4120 cpr = &bnapi->cp_ring; 4121 ring = &cpr->cp_ring_struct; 4122 4123 bnxt_free_ring(bp, &ring->ring_mem); 4124 4125 if (!cpr->cp_ring_arr) 4126 continue; 4127 4128 for (j = 0; j < cpr->cp_ring_count; j++) { 4129 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4130 4131 ring = &cpr2->cp_ring_struct; 4132 bnxt_free_ring(bp, &ring->ring_mem); 4133 bnxt_free_cp_arrays(cpr2); 4134 } 4135 kfree(cpr->cp_ring_arr); 4136 cpr->cp_ring_arr = NULL; 4137 cpr->cp_ring_count = 0; 4138 } 4139 } 4140 4141 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4142 struct bnxt_cp_ring_info *cpr) 4143 { 4144 struct bnxt_ring_mem_info *rmem; 4145 struct bnxt_ring_struct *ring; 4146 int rc; 4147 4148 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4149 if (rc) { 4150 bnxt_free_cp_arrays(cpr); 4151 return -ENOMEM; 4152 } 4153 ring = &cpr->cp_ring_struct; 4154 rmem = &ring->ring_mem; 4155 rmem->nr_pages = bp->cp_nr_pages; 4156 rmem->page_size = HW_CMPD_RING_SIZE; 4157 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4158 rmem->dma_arr = cpr->cp_desc_mapping; 4159 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4160 rc = bnxt_alloc_ring(bp, rmem); 4161 if (rc) { 4162 bnxt_free_ring(bp, rmem); 4163 bnxt_free_cp_arrays(cpr); 4164 } 4165 return rc; 4166 } 4167 4168 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4169 { 4170 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4171 int i, j, rc, ulp_msix; 4172 int tcs = bp->num_tc; 4173 4174 if (!tcs) 4175 tcs = 1; 4176 ulp_msix = bnxt_get_ulp_msix_num(bp); 4177 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4178 struct bnxt_napi *bnapi = bp->bnapi[i]; 4179 struct bnxt_cp_ring_info *cpr, *cpr2; 4180 struct bnxt_ring_struct *ring; 4181 int cp_count = 0, k; 4182 int rx = 0, tx = 0; 4183 4184 if (!bnapi) 4185 continue; 4186 4187 cpr = &bnapi->cp_ring; 4188 cpr->bnapi = bnapi; 4189 ring = &cpr->cp_ring_struct; 4190 4191 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4192 if (rc) 4193 return rc; 4194 4195 ring->map_idx = ulp_msix + i; 4196 4197 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4198 continue; 4199 4200 if (i < bp->rx_nr_rings) { 4201 cp_count++; 4202 rx = 1; 4203 } 4204 if (i < bp->tx_nr_rings_xdp) { 4205 cp_count++; 4206 tx = 1; 4207 } else if ((sh && i < bp->tx_nr_rings) || 4208 (!sh && i >= bp->rx_nr_rings)) { 4209 cp_count += tcs; 4210 tx = 1; 4211 } 4212 4213 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 4214 GFP_KERNEL); 4215 if (!cpr->cp_ring_arr) 4216 return -ENOMEM; 4217 cpr->cp_ring_count = cp_count; 4218 4219 for (k = 0; k < cp_count; k++) { 4220 cpr2 = &cpr->cp_ring_arr[k]; 4221 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4222 if (rc) 4223 return rc; 4224 cpr2->bnapi = bnapi; 4225 cpr2->sw_stats = cpr->sw_stats; 4226 cpr2->cp_idx = k; 4227 if (!k && rx) { 4228 bp->rx_ring[i].rx_cpr = cpr2; 4229 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4230 } else { 4231 int n, tc = k - rx; 4232 4233 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4234 bp->tx_ring[n].tx_cpr = cpr2; 4235 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4236 } 4237 } 4238 if (tx) 4239 j++; 4240 } 4241 return 0; 4242 } 4243 4244 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4245 struct bnxt_rx_ring_info *rxr) 4246 { 4247 struct bnxt_ring_mem_info *rmem; 4248 struct bnxt_ring_struct *ring; 4249 4250 ring = &rxr->rx_ring_struct; 4251 rmem = &ring->ring_mem; 4252 rmem->nr_pages = bp->rx_nr_pages; 4253 rmem->page_size = HW_RXBD_RING_SIZE; 4254 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4255 rmem->dma_arr = rxr->rx_desc_mapping; 4256 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4257 rmem->vmem = (void **)&rxr->rx_buf_ring; 4258 4259 ring = &rxr->rx_agg_ring_struct; 4260 rmem = &ring->ring_mem; 4261 rmem->nr_pages = bp->rx_agg_nr_pages; 4262 rmem->page_size = HW_RXBD_RING_SIZE; 4263 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4264 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4265 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4266 rmem->vmem = (void **)&rxr->rx_agg_ring; 4267 } 4268 4269 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4270 struct bnxt_rx_ring_info *rxr) 4271 { 4272 struct bnxt_ring_mem_info *rmem; 4273 struct bnxt_ring_struct *ring; 4274 int i; 4275 4276 rxr->page_pool->p.napi = NULL; 4277 rxr->page_pool = NULL; 4278 rxr->head_pool->p.napi = NULL; 4279 rxr->head_pool = NULL; 4280 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4281 4282 ring = &rxr->rx_ring_struct; 4283 rmem = &ring->ring_mem; 4284 rmem->pg_tbl = NULL; 4285 rmem->pg_tbl_map = 0; 4286 for (i = 0; i < rmem->nr_pages; i++) { 4287 rmem->pg_arr[i] = NULL; 4288 rmem->dma_arr[i] = 0; 4289 } 4290 *rmem->vmem = NULL; 4291 4292 ring = &rxr->rx_agg_ring_struct; 4293 rmem = &ring->ring_mem; 4294 rmem->pg_tbl = NULL; 4295 rmem->pg_tbl_map = 0; 4296 for (i = 0; i < rmem->nr_pages; i++) { 4297 rmem->pg_arr[i] = NULL; 4298 rmem->dma_arr[i] = 0; 4299 } 4300 *rmem->vmem = NULL; 4301 } 4302 4303 static void bnxt_init_ring_struct(struct bnxt *bp) 4304 { 4305 int i, j; 4306 4307 for (i = 0; i < bp->cp_nr_rings; i++) { 4308 struct bnxt_napi *bnapi = bp->bnapi[i]; 4309 struct bnxt_ring_mem_info *rmem; 4310 struct bnxt_cp_ring_info *cpr; 4311 struct bnxt_rx_ring_info *rxr; 4312 struct bnxt_tx_ring_info *txr; 4313 struct bnxt_ring_struct *ring; 4314 4315 if (!bnapi) 4316 continue; 4317 4318 cpr = &bnapi->cp_ring; 4319 ring = &cpr->cp_ring_struct; 4320 rmem = &ring->ring_mem; 4321 rmem->nr_pages = bp->cp_nr_pages; 4322 rmem->page_size = HW_CMPD_RING_SIZE; 4323 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4324 rmem->dma_arr = cpr->cp_desc_mapping; 4325 rmem->vmem_size = 0; 4326 4327 rxr = bnapi->rx_ring; 4328 if (!rxr) 4329 goto skip_rx; 4330 4331 ring = &rxr->rx_ring_struct; 4332 rmem = &ring->ring_mem; 4333 rmem->nr_pages = bp->rx_nr_pages; 4334 rmem->page_size = HW_RXBD_RING_SIZE; 4335 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4336 rmem->dma_arr = rxr->rx_desc_mapping; 4337 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4338 rmem->vmem = (void **)&rxr->rx_buf_ring; 4339 4340 ring = &rxr->rx_agg_ring_struct; 4341 rmem = &ring->ring_mem; 4342 rmem->nr_pages = bp->rx_agg_nr_pages; 4343 rmem->page_size = HW_RXBD_RING_SIZE; 4344 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4345 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4346 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4347 rmem->vmem = (void **)&rxr->rx_agg_ring; 4348 4349 skip_rx: 4350 bnxt_for_each_napi_tx(j, bnapi, txr) { 4351 ring = &txr->tx_ring_struct; 4352 rmem = &ring->ring_mem; 4353 rmem->nr_pages = bp->tx_nr_pages; 4354 rmem->page_size = HW_TXBD_RING_SIZE; 4355 rmem->pg_arr = (void **)txr->tx_desc_ring; 4356 rmem->dma_arr = txr->tx_desc_mapping; 4357 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4358 rmem->vmem = (void **)&txr->tx_buf_ring; 4359 } 4360 } 4361 } 4362 4363 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4364 { 4365 int i; 4366 u32 prod; 4367 struct rx_bd **rx_buf_ring; 4368 4369 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4370 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4371 int j; 4372 struct rx_bd *rxbd; 4373 4374 rxbd = rx_buf_ring[i]; 4375 if (!rxbd) 4376 continue; 4377 4378 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4379 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4380 rxbd->rx_bd_opaque = prod; 4381 } 4382 } 4383 } 4384 4385 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4386 struct bnxt_rx_ring_info *rxr, 4387 int ring_nr) 4388 { 4389 u32 prod; 4390 int i; 4391 4392 prod = rxr->rx_prod; 4393 for (i = 0; i < bp->rx_ring_size; i++) { 4394 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4395 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4396 ring_nr, i, bp->rx_ring_size); 4397 break; 4398 } 4399 prod = NEXT_RX(prod); 4400 } 4401 rxr->rx_prod = prod; 4402 } 4403 4404 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp, 4405 struct bnxt_rx_ring_info *rxr, 4406 int ring_nr) 4407 { 4408 u32 prod; 4409 int i; 4410 4411 prod = rxr->rx_agg_prod; 4412 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4413 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) { 4414 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4415 ring_nr, i, bp->rx_agg_ring_size); 4416 break; 4417 } 4418 prod = NEXT_RX_AGG(prod); 4419 } 4420 rxr->rx_agg_prod = prod; 4421 } 4422 4423 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4424 struct bnxt_rx_ring_info *rxr) 4425 { 4426 dma_addr_t mapping; 4427 u8 *data; 4428 int i; 4429 4430 for (i = 0; i < bp->max_tpa; i++) { 4431 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4432 GFP_KERNEL); 4433 if (!data) 4434 return -ENOMEM; 4435 4436 rxr->rx_tpa[i].data = data; 4437 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4438 rxr->rx_tpa[i].mapping = mapping; 4439 } 4440 4441 return 0; 4442 } 4443 4444 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4445 { 4446 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4447 int rc; 4448 4449 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4450 4451 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4452 return 0; 4453 4454 bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr); 4455 4456 if (rxr->rx_tpa) { 4457 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4458 if (rc) 4459 return rc; 4460 } 4461 return 0; 4462 } 4463 4464 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4465 struct bnxt_rx_ring_info *rxr) 4466 { 4467 struct bnxt_ring_struct *ring; 4468 u32 type; 4469 4470 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4471 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4472 4473 if (NET_IP_ALIGN == 2) 4474 type |= RX_BD_FLAGS_SOP; 4475 4476 ring = &rxr->rx_ring_struct; 4477 bnxt_init_rxbd_pages(ring, type); 4478 ring->fw_ring_id = INVALID_HW_RING_ID; 4479 } 4480 4481 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4482 struct bnxt_rx_ring_info *rxr) 4483 { 4484 struct bnxt_ring_struct *ring; 4485 u32 type; 4486 4487 ring = &rxr->rx_agg_ring_struct; 4488 ring->fw_ring_id = INVALID_HW_RING_ID; 4489 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4490 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4491 RX_BD_TYPE_RX_AGG_BD; 4492 4493 /* On P7, setting EOP will cause the chip to disable 4494 * Relaxed Ordering (RO) for TPA data. Disable EOP for 4495 * potentially higher performance with RO. 4496 */ 4497 if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA)) 4498 type |= RX_BD_FLAGS_AGG_EOP; 4499 4500 bnxt_init_rxbd_pages(ring, type); 4501 } 4502 } 4503 4504 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4505 { 4506 struct bnxt_rx_ring_info *rxr; 4507 4508 rxr = &bp->rx_ring[ring_nr]; 4509 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4510 4511 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4512 &rxr->bnapi->napi); 4513 4514 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4515 bpf_prog_add(bp->xdp_prog, 1); 4516 rxr->xdp_prog = bp->xdp_prog; 4517 } 4518 4519 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4520 4521 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4522 } 4523 4524 static void bnxt_init_cp_rings(struct bnxt *bp) 4525 { 4526 int i, j; 4527 4528 for (i = 0; i < bp->cp_nr_rings; i++) { 4529 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4530 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4531 4532 ring->fw_ring_id = INVALID_HW_RING_ID; 4533 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4534 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4535 if (!cpr->cp_ring_arr) 4536 continue; 4537 for (j = 0; j < cpr->cp_ring_count; j++) { 4538 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4539 4540 ring = &cpr2->cp_ring_struct; 4541 ring->fw_ring_id = INVALID_HW_RING_ID; 4542 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4543 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4544 } 4545 } 4546 } 4547 4548 static int bnxt_init_rx_rings(struct bnxt *bp) 4549 { 4550 int i, rc = 0; 4551 4552 if (BNXT_RX_PAGE_MODE(bp)) { 4553 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4554 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4555 } else { 4556 bp->rx_offset = BNXT_RX_OFFSET; 4557 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4558 } 4559 4560 for (i = 0; i < bp->rx_nr_rings; i++) { 4561 rc = bnxt_init_one_rx_ring(bp, i); 4562 if (rc) 4563 break; 4564 } 4565 4566 return rc; 4567 } 4568 4569 static int bnxt_init_tx_rings(struct bnxt *bp) 4570 { 4571 u16 i; 4572 4573 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4574 BNXT_MIN_TX_DESC_CNT); 4575 4576 for (i = 0; i < bp->tx_nr_rings; i++) { 4577 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4578 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4579 4580 ring->fw_ring_id = INVALID_HW_RING_ID; 4581 4582 if (i >= bp->tx_nr_rings_xdp) 4583 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4584 NETDEV_QUEUE_TYPE_TX, 4585 &txr->bnapi->napi); 4586 } 4587 4588 return 0; 4589 } 4590 4591 static void bnxt_free_ring_grps(struct bnxt *bp) 4592 { 4593 kfree(bp->grp_info); 4594 bp->grp_info = NULL; 4595 } 4596 4597 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4598 { 4599 int i; 4600 4601 if (irq_re_init) { 4602 bp->grp_info = kcalloc(bp->cp_nr_rings, 4603 sizeof(struct bnxt_ring_grp_info), 4604 GFP_KERNEL); 4605 if (!bp->grp_info) 4606 return -ENOMEM; 4607 } 4608 for (i = 0; i < bp->cp_nr_rings; i++) { 4609 if (irq_re_init) 4610 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4611 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4612 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4613 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4614 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4615 } 4616 return 0; 4617 } 4618 4619 static void bnxt_free_vnics(struct bnxt *bp) 4620 { 4621 kfree(bp->vnic_info); 4622 bp->vnic_info = NULL; 4623 bp->nr_vnics = 0; 4624 } 4625 4626 static int bnxt_alloc_vnics(struct bnxt *bp) 4627 { 4628 int num_vnics = 1; 4629 4630 #ifdef CONFIG_RFS_ACCEL 4631 if (bp->flags & BNXT_FLAG_RFS) { 4632 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4633 num_vnics++; 4634 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4635 num_vnics += bp->rx_nr_rings; 4636 } 4637 #endif 4638 4639 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4640 num_vnics++; 4641 4642 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4643 GFP_KERNEL); 4644 if (!bp->vnic_info) 4645 return -ENOMEM; 4646 4647 bp->nr_vnics = num_vnics; 4648 return 0; 4649 } 4650 4651 static void bnxt_init_vnics(struct bnxt *bp) 4652 { 4653 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4654 int i; 4655 4656 for (i = 0; i < bp->nr_vnics; i++) { 4657 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4658 int j; 4659 4660 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4661 vnic->vnic_id = i; 4662 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4663 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4664 4665 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4666 4667 if (bp->vnic_info[i].rss_hash_key) { 4668 if (i == BNXT_VNIC_DEFAULT) { 4669 u8 *key = (void *)vnic->rss_hash_key; 4670 int k; 4671 4672 if (!bp->rss_hash_key_valid && 4673 !bp->rss_hash_key_updated) { 4674 get_random_bytes(bp->rss_hash_key, 4675 HW_HASH_KEY_SIZE); 4676 bp->rss_hash_key_updated = true; 4677 } 4678 4679 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4680 HW_HASH_KEY_SIZE); 4681 4682 if (!bp->rss_hash_key_updated) 4683 continue; 4684 4685 bp->rss_hash_key_updated = false; 4686 bp->rss_hash_key_valid = true; 4687 4688 bp->toeplitz_prefix = 0; 4689 for (k = 0; k < 8; k++) { 4690 bp->toeplitz_prefix <<= 8; 4691 bp->toeplitz_prefix |= key[k]; 4692 } 4693 } else { 4694 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4695 HW_HASH_KEY_SIZE); 4696 } 4697 } 4698 } 4699 } 4700 4701 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4702 { 4703 int pages; 4704 4705 pages = ring_size / desc_per_pg; 4706 4707 if (!pages) 4708 return 1; 4709 4710 pages++; 4711 4712 while (pages & (pages - 1)) 4713 pages++; 4714 4715 return pages; 4716 } 4717 4718 void bnxt_set_tpa_flags(struct bnxt *bp) 4719 { 4720 bp->flags &= ~BNXT_FLAG_TPA; 4721 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4722 return; 4723 if (bp->dev->features & NETIF_F_LRO) 4724 bp->flags |= BNXT_FLAG_LRO; 4725 else if (bp->dev->features & NETIF_F_GRO_HW) 4726 bp->flags |= BNXT_FLAG_GRO; 4727 } 4728 4729 static void bnxt_init_ring_params(struct bnxt *bp) 4730 { 4731 unsigned int rx_size; 4732 4733 bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK; 4734 /* Try to fit 4 chunks into a 4k page */ 4735 rx_size = SZ_1K - 4736 NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4737 bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size); 4738 } 4739 4740 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4741 * be set on entry. 4742 */ 4743 void bnxt_set_ring_params(struct bnxt *bp) 4744 { 4745 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4746 u32 agg_factor = 0, agg_ring_size = 0; 4747 4748 /* 8 for CRC and VLAN */ 4749 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4750 4751 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4752 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4753 4754 ring_size = bp->rx_ring_size; 4755 bp->rx_agg_ring_size = 0; 4756 bp->rx_agg_nr_pages = 0; 4757 4758 if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS) 4759 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4760 4761 bp->flags &= ~BNXT_FLAG_JUMBO; 4762 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4763 u32 jumbo_factor; 4764 4765 bp->flags |= BNXT_FLAG_JUMBO; 4766 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4767 if (jumbo_factor > agg_factor) 4768 agg_factor = jumbo_factor; 4769 } 4770 if (agg_factor) { 4771 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4772 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4773 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4774 bp->rx_ring_size, ring_size); 4775 bp->rx_ring_size = ring_size; 4776 } 4777 agg_ring_size = ring_size * agg_factor; 4778 4779 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4780 RX_DESC_CNT); 4781 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4782 u32 tmp = agg_ring_size; 4783 4784 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4785 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4786 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4787 tmp, agg_ring_size); 4788 } 4789 bp->rx_agg_ring_size = agg_ring_size; 4790 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4791 4792 if (BNXT_RX_PAGE_MODE(bp)) { 4793 rx_space = PAGE_SIZE; 4794 rx_size = PAGE_SIZE - 4795 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4796 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4797 } else { 4798 rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK, 4799 bp->rx_copybreak, 4800 bp->dev->cfg_pending->hds_thresh); 4801 rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN); 4802 rx_space = rx_size + NET_SKB_PAD + 4803 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4804 } 4805 } 4806 4807 bp->rx_buf_use_size = rx_size; 4808 bp->rx_buf_size = rx_space; 4809 4810 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4811 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4812 4813 ring_size = bp->tx_ring_size; 4814 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4815 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4816 4817 max_rx_cmpl = bp->rx_ring_size; 4818 /* MAX TPA needs to be added because TPA_START completions are 4819 * immediately recycled, so the TPA completions are not bound by 4820 * the RX ring size. 4821 */ 4822 if (bp->flags & BNXT_FLAG_TPA) 4823 max_rx_cmpl += bp->max_tpa; 4824 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4825 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4826 bp->cp_ring_size = ring_size; 4827 4828 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4829 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4830 bp->cp_nr_pages = MAX_CP_PAGES; 4831 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4832 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4833 ring_size, bp->cp_ring_size); 4834 } 4835 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4836 bp->cp_ring_mask = bp->cp_bit - 1; 4837 } 4838 4839 /* Changing allocation mode of RX rings. 4840 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4841 */ 4842 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4843 { 4844 struct net_device *dev = bp->dev; 4845 4846 if (page_mode) { 4847 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 4848 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4849 4850 if (bp->xdp_prog->aux->xdp_has_frags) 4851 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4852 else 4853 dev->max_mtu = 4854 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4855 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4856 bp->flags |= BNXT_FLAG_JUMBO; 4857 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4858 } else { 4859 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4860 bp->rx_skb_func = bnxt_rx_page_skb; 4861 } 4862 bp->rx_dir = DMA_BIDIRECTIONAL; 4863 } else { 4864 dev->max_mtu = bp->max_mtu; 4865 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4866 bp->rx_dir = DMA_FROM_DEVICE; 4867 bp->rx_skb_func = bnxt_rx_skb; 4868 } 4869 } 4870 4871 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4872 { 4873 __bnxt_set_rx_skb_mode(bp, page_mode); 4874 4875 if (!page_mode) { 4876 int rx, tx; 4877 4878 bnxt_get_max_rings(bp, &rx, &tx, true); 4879 if (rx > 1) { 4880 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 4881 bp->dev->hw_features |= NETIF_F_LRO; 4882 } 4883 } 4884 4885 /* Update LRO and GRO_HW availability */ 4886 netdev_update_features(bp->dev); 4887 } 4888 4889 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4890 { 4891 int i; 4892 struct bnxt_vnic_info *vnic; 4893 struct pci_dev *pdev = bp->pdev; 4894 4895 if (!bp->vnic_info) 4896 return; 4897 4898 for (i = 0; i < bp->nr_vnics; i++) { 4899 vnic = &bp->vnic_info[i]; 4900 4901 kfree(vnic->fw_grp_ids); 4902 vnic->fw_grp_ids = NULL; 4903 4904 kfree(vnic->uc_list); 4905 vnic->uc_list = NULL; 4906 4907 if (vnic->mc_list) { 4908 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4909 vnic->mc_list, vnic->mc_list_mapping); 4910 vnic->mc_list = NULL; 4911 } 4912 4913 if (vnic->rss_table) { 4914 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4915 vnic->rss_table, 4916 vnic->rss_table_dma_addr); 4917 vnic->rss_table = NULL; 4918 } 4919 4920 vnic->rss_hash_key = NULL; 4921 vnic->flags = 0; 4922 } 4923 } 4924 4925 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4926 { 4927 int i, rc = 0, size; 4928 struct bnxt_vnic_info *vnic; 4929 struct pci_dev *pdev = bp->pdev; 4930 int max_rings; 4931 4932 for (i = 0; i < bp->nr_vnics; i++) { 4933 vnic = &bp->vnic_info[i]; 4934 4935 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4936 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4937 4938 if (mem_size > 0) { 4939 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4940 if (!vnic->uc_list) { 4941 rc = -ENOMEM; 4942 goto out; 4943 } 4944 } 4945 } 4946 4947 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4948 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4949 vnic->mc_list = 4950 dma_alloc_coherent(&pdev->dev, 4951 vnic->mc_list_size, 4952 &vnic->mc_list_mapping, 4953 GFP_KERNEL); 4954 if (!vnic->mc_list) { 4955 rc = -ENOMEM; 4956 goto out; 4957 } 4958 } 4959 4960 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4961 goto vnic_skip_grps; 4962 4963 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4964 max_rings = bp->rx_nr_rings; 4965 else 4966 max_rings = 1; 4967 4968 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4969 if (!vnic->fw_grp_ids) { 4970 rc = -ENOMEM; 4971 goto out; 4972 } 4973 vnic_skip_grps: 4974 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4975 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4976 continue; 4977 4978 /* Allocate rss table and hash key */ 4979 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4980 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4981 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4982 4983 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4984 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4985 vnic->rss_table_size, 4986 &vnic->rss_table_dma_addr, 4987 GFP_KERNEL); 4988 if (!vnic->rss_table) { 4989 rc = -ENOMEM; 4990 goto out; 4991 } 4992 4993 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4994 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4995 } 4996 return 0; 4997 4998 out: 4999 return rc; 5000 } 5001 5002 static void bnxt_free_hwrm_resources(struct bnxt *bp) 5003 { 5004 struct bnxt_hwrm_wait_token *token; 5005 5006 dma_pool_destroy(bp->hwrm_dma_pool); 5007 bp->hwrm_dma_pool = NULL; 5008 5009 rcu_read_lock(); 5010 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 5011 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 5012 rcu_read_unlock(); 5013 } 5014 5015 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 5016 { 5017 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 5018 BNXT_HWRM_DMA_SIZE, 5019 BNXT_HWRM_DMA_ALIGN, 0); 5020 if (!bp->hwrm_dma_pool) 5021 return -ENOMEM; 5022 5023 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 5024 5025 return 0; 5026 } 5027 5028 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 5029 { 5030 kfree(stats->hw_masks); 5031 stats->hw_masks = NULL; 5032 kfree(stats->sw_stats); 5033 stats->sw_stats = NULL; 5034 if (stats->hw_stats) { 5035 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 5036 stats->hw_stats_map); 5037 stats->hw_stats = NULL; 5038 } 5039 } 5040 5041 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 5042 bool alloc_masks) 5043 { 5044 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 5045 &stats->hw_stats_map, GFP_KERNEL); 5046 if (!stats->hw_stats) 5047 return -ENOMEM; 5048 5049 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 5050 if (!stats->sw_stats) 5051 goto stats_mem_err; 5052 5053 if (alloc_masks) { 5054 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 5055 if (!stats->hw_masks) 5056 goto stats_mem_err; 5057 } 5058 return 0; 5059 5060 stats_mem_err: 5061 bnxt_free_stats_mem(bp, stats); 5062 return -ENOMEM; 5063 } 5064 5065 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 5066 { 5067 int i; 5068 5069 for (i = 0; i < count; i++) 5070 mask_arr[i] = mask; 5071 } 5072 5073 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 5074 { 5075 int i; 5076 5077 for (i = 0; i < count; i++) 5078 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 5079 } 5080 5081 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 5082 struct bnxt_stats_mem *stats) 5083 { 5084 struct hwrm_func_qstats_ext_output *resp; 5085 struct hwrm_func_qstats_ext_input *req; 5086 __le64 *hw_masks; 5087 int rc; 5088 5089 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 5090 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5091 return -EOPNOTSUPP; 5092 5093 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 5094 if (rc) 5095 return rc; 5096 5097 req->fid = cpu_to_le16(0xffff); 5098 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5099 5100 resp = hwrm_req_hold(bp, req); 5101 rc = hwrm_req_send(bp, req); 5102 if (!rc) { 5103 hw_masks = &resp->rx_ucast_pkts; 5104 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 5105 } 5106 hwrm_req_drop(bp, req); 5107 return rc; 5108 } 5109 5110 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 5111 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 5112 5113 static void bnxt_init_stats(struct bnxt *bp) 5114 { 5115 struct bnxt_napi *bnapi = bp->bnapi[0]; 5116 struct bnxt_cp_ring_info *cpr; 5117 struct bnxt_stats_mem *stats; 5118 __le64 *rx_stats, *tx_stats; 5119 int rc, rx_count, tx_count; 5120 u64 *rx_masks, *tx_masks; 5121 u64 mask; 5122 u8 flags; 5123 5124 cpr = &bnapi->cp_ring; 5125 stats = &cpr->stats; 5126 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 5127 if (rc) { 5128 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5129 mask = (1ULL << 48) - 1; 5130 else 5131 mask = -1ULL; 5132 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 5133 } 5134 if (bp->flags & BNXT_FLAG_PORT_STATS) { 5135 stats = &bp->port_stats; 5136 rx_stats = stats->hw_stats; 5137 rx_masks = stats->hw_masks; 5138 rx_count = sizeof(struct rx_port_stats) / 8; 5139 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5140 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5141 tx_count = sizeof(struct tx_port_stats) / 8; 5142 5143 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 5144 rc = bnxt_hwrm_port_qstats(bp, flags); 5145 if (rc) { 5146 mask = (1ULL << 40) - 1; 5147 5148 bnxt_fill_masks(rx_masks, mask, rx_count); 5149 bnxt_fill_masks(tx_masks, mask, tx_count); 5150 } else { 5151 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5152 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5153 bnxt_hwrm_port_qstats(bp, 0); 5154 } 5155 } 5156 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5157 stats = &bp->rx_port_stats_ext; 5158 rx_stats = stats->hw_stats; 5159 rx_masks = stats->hw_masks; 5160 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5161 stats = &bp->tx_port_stats_ext; 5162 tx_stats = stats->hw_stats; 5163 tx_masks = stats->hw_masks; 5164 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5165 5166 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5167 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5168 if (rc) { 5169 mask = (1ULL << 40) - 1; 5170 5171 bnxt_fill_masks(rx_masks, mask, rx_count); 5172 if (tx_stats) 5173 bnxt_fill_masks(tx_masks, mask, tx_count); 5174 } else { 5175 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5176 if (tx_stats) 5177 bnxt_copy_hw_masks(tx_masks, tx_stats, 5178 tx_count); 5179 bnxt_hwrm_port_qstats_ext(bp, 0); 5180 } 5181 } 5182 } 5183 5184 static void bnxt_free_port_stats(struct bnxt *bp) 5185 { 5186 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5187 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5188 5189 bnxt_free_stats_mem(bp, &bp->port_stats); 5190 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5191 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5192 } 5193 5194 static void bnxt_free_ring_stats(struct bnxt *bp) 5195 { 5196 int i; 5197 5198 if (!bp->bnapi) 5199 return; 5200 5201 for (i = 0; i < bp->cp_nr_rings; i++) { 5202 struct bnxt_napi *bnapi = bp->bnapi[i]; 5203 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5204 5205 bnxt_free_stats_mem(bp, &cpr->stats); 5206 5207 kfree(cpr->sw_stats); 5208 cpr->sw_stats = NULL; 5209 } 5210 } 5211 5212 static int bnxt_alloc_stats(struct bnxt *bp) 5213 { 5214 u32 size, i; 5215 int rc; 5216 5217 size = bp->hw_ring_stats_size; 5218 5219 for (i = 0; i < bp->cp_nr_rings; i++) { 5220 struct bnxt_napi *bnapi = bp->bnapi[i]; 5221 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5222 5223 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 5224 if (!cpr->sw_stats) 5225 return -ENOMEM; 5226 5227 cpr->stats.len = size; 5228 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5229 if (rc) 5230 return rc; 5231 5232 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5233 } 5234 5235 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5236 return 0; 5237 5238 if (bp->port_stats.hw_stats) 5239 goto alloc_ext_stats; 5240 5241 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5242 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5243 if (rc) 5244 return rc; 5245 5246 bp->flags |= BNXT_FLAG_PORT_STATS; 5247 5248 alloc_ext_stats: 5249 /* Display extended statistics only if FW supports it */ 5250 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5251 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5252 return 0; 5253 5254 if (bp->rx_port_stats_ext.hw_stats) 5255 goto alloc_tx_ext_stats; 5256 5257 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5258 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5259 /* Extended stats are optional */ 5260 if (rc) 5261 return 0; 5262 5263 alloc_tx_ext_stats: 5264 if (bp->tx_port_stats_ext.hw_stats) 5265 return 0; 5266 5267 if (bp->hwrm_spec_code >= 0x10902 || 5268 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5269 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5270 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5271 /* Extended stats are optional */ 5272 if (rc) 5273 return 0; 5274 } 5275 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5276 return 0; 5277 } 5278 5279 static void bnxt_clear_ring_indices(struct bnxt *bp) 5280 { 5281 int i, j; 5282 5283 if (!bp->bnapi) 5284 return; 5285 5286 for (i = 0; i < bp->cp_nr_rings; i++) { 5287 struct bnxt_napi *bnapi = bp->bnapi[i]; 5288 struct bnxt_cp_ring_info *cpr; 5289 struct bnxt_rx_ring_info *rxr; 5290 struct bnxt_tx_ring_info *txr; 5291 5292 if (!bnapi) 5293 continue; 5294 5295 cpr = &bnapi->cp_ring; 5296 cpr->cp_raw_cons = 0; 5297 5298 bnxt_for_each_napi_tx(j, bnapi, txr) { 5299 txr->tx_prod = 0; 5300 txr->tx_cons = 0; 5301 txr->tx_hw_cons = 0; 5302 } 5303 5304 rxr = bnapi->rx_ring; 5305 if (rxr) { 5306 rxr->rx_prod = 0; 5307 rxr->rx_agg_prod = 0; 5308 rxr->rx_sw_agg_prod = 0; 5309 rxr->rx_next_cons = 0; 5310 } 5311 bnapi->events = 0; 5312 } 5313 } 5314 5315 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5316 { 5317 u8 type = fltr->type, flags = fltr->flags; 5318 5319 INIT_LIST_HEAD(&fltr->list); 5320 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5321 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5322 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5323 } 5324 5325 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5326 { 5327 if (!list_empty(&fltr->list)) 5328 list_del_init(&fltr->list); 5329 } 5330 5331 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5332 { 5333 struct bnxt_filter_base *usr_fltr, *tmp; 5334 5335 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5336 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5337 continue; 5338 bnxt_del_one_usr_fltr(bp, usr_fltr); 5339 } 5340 } 5341 5342 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5343 { 5344 hlist_del(&fltr->hash); 5345 bnxt_del_one_usr_fltr(bp, fltr); 5346 if (fltr->flags) { 5347 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5348 bp->ntp_fltr_count--; 5349 } 5350 kfree(fltr); 5351 } 5352 5353 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5354 { 5355 int i; 5356 5357 netdev_assert_locked_or_invisible(bp->dev); 5358 5359 /* Under netdev instance lock and all our NAPIs have been disabled. 5360 * It's safe to delete the hash table. 5361 */ 5362 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5363 struct hlist_head *head; 5364 struct hlist_node *tmp; 5365 struct bnxt_ntuple_filter *fltr; 5366 5367 head = &bp->ntp_fltr_hash_tbl[i]; 5368 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5369 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5370 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5371 !list_empty(&fltr->base.list))) 5372 continue; 5373 bnxt_del_fltr(bp, &fltr->base); 5374 } 5375 } 5376 if (!all) 5377 return; 5378 5379 bitmap_free(bp->ntp_fltr_bmap); 5380 bp->ntp_fltr_bmap = NULL; 5381 bp->ntp_fltr_count = 0; 5382 } 5383 5384 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5385 { 5386 int i, rc = 0; 5387 5388 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5389 return 0; 5390 5391 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5392 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5393 5394 bp->ntp_fltr_count = 0; 5395 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5396 5397 if (!bp->ntp_fltr_bmap) 5398 rc = -ENOMEM; 5399 5400 return rc; 5401 } 5402 5403 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5404 { 5405 int i; 5406 5407 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5408 struct hlist_head *head; 5409 struct hlist_node *tmp; 5410 struct bnxt_l2_filter *fltr; 5411 5412 head = &bp->l2_fltr_hash_tbl[i]; 5413 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5414 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5415 !list_empty(&fltr->base.list))) 5416 continue; 5417 bnxt_del_fltr(bp, &fltr->base); 5418 } 5419 } 5420 } 5421 5422 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5423 { 5424 int i; 5425 5426 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5427 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5428 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5429 } 5430 5431 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5432 { 5433 bnxt_free_vnic_attributes(bp); 5434 bnxt_free_tx_rings(bp); 5435 bnxt_free_rx_rings(bp); 5436 bnxt_free_cp_rings(bp); 5437 bnxt_free_all_cp_arrays(bp); 5438 bnxt_free_ntp_fltrs(bp, false); 5439 bnxt_free_l2_filters(bp, false); 5440 if (irq_re_init) { 5441 bnxt_free_ring_stats(bp); 5442 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5443 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5444 bnxt_free_port_stats(bp); 5445 bnxt_free_ring_grps(bp); 5446 bnxt_free_vnics(bp); 5447 kfree(bp->tx_ring_map); 5448 bp->tx_ring_map = NULL; 5449 kfree(bp->tx_ring); 5450 bp->tx_ring = NULL; 5451 kfree(bp->rx_ring); 5452 bp->rx_ring = NULL; 5453 kfree(bp->bnapi); 5454 bp->bnapi = NULL; 5455 } else { 5456 bnxt_clear_ring_indices(bp); 5457 } 5458 } 5459 5460 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5461 { 5462 int i, j, rc, size, arr_size; 5463 void *bnapi; 5464 5465 if (irq_re_init) { 5466 /* Allocate bnapi mem pointer array and mem block for 5467 * all queues 5468 */ 5469 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5470 bp->cp_nr_rings); 5471 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5472 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5473 if (!bnapi) 5474 return -ENOMEM; 5475 5476 bp->bnapi = bnapi; 5477 bnapi += arr_size; 5478 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5479 bp->bnapi[i] = bnapi; 5480 bp->bnapi[i]->index = i; 5481 bp->bnapi[i]->bp = bp; 5482 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5483 struct bnxt_cp_ring_info *cpr = 5484 &bp->bnapi[i]->cp_ring; 5485 5486 cpr->cp_ring_struct.ring_mem.flags = 5487 BNXT_RMEM_RING_PTE_FLAG; 5488 } 5489 } 5490 5491 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5492 sizeof(struct bnxt_rx_ring_info), 5493 GFP_KERNEL); 5494 if (!bp->rx_ring) 5495 return -ENOMEM; 5496 5497 for (i = 0; i < bp->rx_nr_rings; i++) { 5498 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5499 5500 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5501 rxr->rx_ring_struct.ring_mem.flags = 5502 BNXT_RMEM_RING_PTE_FLAG; 5503 rxr->rx_agg_ring_struct.ring_mem.flags = 5504 BNXT_RMEM_RING_PTE_FLAG; 5505 } else { 5506 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5507 } 5508 rxr->bnapi = bp->bnapi[i]; 5509 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5510 } 5511 5512 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5513 sizeof(struct bnxt_tx_ring_info), 5514 GFP_KERNEL); 5515 if (!bp->tx_ring) 5516 return -ENOMEM; 5517 5518 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5519 GFP_KERNEL); 5520 5521 if (!bp->tx_ring_map) 5522 return -ENOMEM; 5523 5524 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5525 j = 0; 5526 else 5527 j = bp->rx_nr_rings; 5528 5529 for (i = 0; i < bp->tx_nr_rings; i++) { 5530 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5531 struct bnxt_napi *bnapi2; 5532 5533 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5534 txr->tx_ring_struct.ring_mem.flags = 5535 BNXT_RMEM_RING_PTE_FLAG; 5536 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5537 if (i >= bp->tx_nr_rings_xdp) { 5538 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5539 5540 bnapi2 = bp->bnapi[k]; 5541 txr->txq_index = i - bp->tx_nr_rings_xdp; 5542 txr->tx_napi_idx = 5543 BNXT_RING_TO_TC(bp, txr->txq_index); 5544 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5545 bnapi2->tx_int = bnxt_tx_int; 5546 } else { 5547 bnapi2 = bp->bnapi[j]; 5548 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5549 bnapi2->tx_ring[0] = txr; 5550 bnapi2->tx_int = bnxt_tx_int_xdp; 5551 j++; 5552 } 5553 txr->bnapi = bnapi2; 5554 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5555 txr->tx_cpr = &bnapi2->cp_ring; 5556 } 5557 5558 rc = bnxt_alloc_stats(bp); 5559 if (rc) 5560 goto alloc_mem_err; 5561 bnxt_init_stats(bp); 5562 5563 rc = bnxt_alloc_ntp_fltrs(bp); 5564 if (rc) 5565 goto alloc_mem_err; 5566 5567 rc = bnxt_alloc_vnics(bp); 5568 if (rc) 5569 goto alloc_mem_err; 5570 } 5571 5572 rc = bnxt_alloc_all_cp_arrays(bp); 5573 if (rc) 5574 goto alloc_mem_err; 5575 5576 bnxt_init_ring_struct(bp); 5577 5578 rc = bnxt_alloc_rx_rings(bp); 5579 if (rc) 5580 goto alloc_mem_err; 5581 5582 rc = bnxt_alloc_tx_rings(bp); 5583 if (rc) 5584 goto alloc_mem_err; 5585 5586 rc = bnxt_alloc_cp_rings(bp); 5587 if (rc) 5588 goto alloc_mem_err; 5589 5590 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5591 BNXT_VNIC_MCAST_FLAG | 5592 BNXT_VNIC_UCAST_FLAG; 5593 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5594 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5595 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5596 5597 rc = bnxt_alloc_vnic_attributes(bp); 5598 if (rc) 5599 goto alloc_mem_err; 5600 return 0; 5601 5602 alloc_mem_err: 5603 bnxt_free_mem(bp, true); 5604 return rc; 5605 } 5606 5607 static void bnxt_disable_int(struct bnxt *bp) 5608 { 5609 int i; 5610 5611 if (!bp->bnapi) 5612 return; 5613 5614 for (i = 0; i < bp->cp_nr_rings; i++) { 5615 struct bnxt_napi *bnapi = bp->bnapi[i]; 5616 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5617 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5618 5619 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5620 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5621 } 5622 } 5623 5624 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5625 { 5626 struct bnxt_napi *bnapi = bp->bnapi[n]; 5627 struct bnxt_cp_ring_info *cpr; 5628 5629 cpr = &bnapi->cp_ring; 5630 return cpr->cp_ring_struct.map_idx; 5631 } 5632 5633 static void bnxt_disable_int_sync(struct bnxt *bp) 5634 { 5635 int i; 5636 5637 if (!bp->irq_tbl) 5638 return; 5639 5640 atomic_inc(&bp->intr_sem); 5641 5642 bnxt_disable_int(bp); 5643 for (i = 0; i < bp->cp_nr_rings; i++) { 5644 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5645 5646 synchronize_irq(bp->irq_tbl[map_idx].vector); 5647 } 5648 } 5649 5650 static void bnxt_enable_int(struct bnxt *bp) 5651 { 5652 int i; 5653 5654 atomic_set(&bp->intr_sem, 0); 5655 for (i = 0; i < bp->cp_nr_rings; i++) { 5656 struct bnxt_napi *bnapi = bp->bnapi[i]; 5657 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5658 5659 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5660 } 5661 } 5662 5663 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5664 bool async_only) 5665 { 5666 DECLARE_BITMAP(async_events_bmap, 256); 5667 u32 *events = (u32 *)async_events_bmap; 5668 struct hwrm_func_drv_rgtr_output *resp; 5669 struct hwrm_func_drv_rgtr_input *req; 5670 u32 flags; 5671 int rc, i; 5672 5673 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5674 if (rc) 5675 return rc; 5676 5677 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5678 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5679 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5680 5681 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5682 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5683 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5684 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5685 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5686 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5687 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5688 if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2) 5689 flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT; 5690 req->flags = cpu_to_le32(flags); 5691 req->ver_maj_8b = DRV_VER_MAJ; 5692 req->ver_min_8b = DRV_VER_MIN; 5693 req->ver_upd_8b = DRV_VER_UPD; 5694 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5695 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5696 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5697 5698 if (BNXT_PF(bp)) { 5699 u32 data[8]; 5700 int i; 5701 5702 memset(data, 0, sizeof(data)); 5703 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5704 u16 cmd = bnxt_vf_req_snif[i]; 5705 unsigned int bit, idx; 5706 5707 if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) && 5708 cmd == HWRM_PORT_PHY_QCFG) 5709 continue; 5710 5711 idx = cmd / 32; 5712 bit = cmd % 32; 5713 data[idx] |= 1 << bit; 5714 } 5715 5716 for (i = 0; i < 8; i++) 5717 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5718 5719 req->enables |= 5720 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5721 } 5722 5723 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5724 req->flags |= cpu_to_le32( 5725 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5726 5727 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5728 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5729 u16 event_id = bnxt_async_events_arr[i]; 5730 5731 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5732 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5733 continue; 5734 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5735 !bp->ptp_cfg) 5736 continue; 5737 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5738 } 5739 if (bmap && bmap_size) { 5740 for (i = 0; i < bmap_size; i++) { 5741 if (test_bit(i, bmap)) 5742 __set_bit(i, async_events_bmap); 5743 } 5744 } 5745 for (i = 0; i < 8; i++) 5746 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5747 5748 if (async_only) 5749 req->enables = 5750 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5751 5752 resp = hwrm_req_hold(bp, req); 5753 rc = hwrm_req_send(bp, req); 5754 if (!rc) { 5755 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5756 if (resp->flags & 5757 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5758 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5759 } 5760 hwrm_req_drop(bp, req); 5761 return rc; 5762 } 5763 5764 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5765 { 5766 struct hwrm_func_drv_unrgtr_input *req; 5767 int rc; 5768 5769 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5770 return 0; 5771 5772 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5773 if (rc) 5774 return rc; 5775 return hwrm_req_send(bp, req); 5776 } 5777 5778 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5779 5780 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5781 { 5782 struct hwrm_tunnel_dst_port_free_input *req; 5783 int rc; 5784 5785 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5786 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5787 return 0; 5788 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5789 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5790 return 0; 5791 5792 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5793 if (rc) 5794 return rc; 5795 5796 req->tunnel_type = tunnel_type; 5797 5798 switch (tunnel_type) { 5799 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5800 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5801 bp->vxlan_port = 0; 5802 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5803 break; 5804 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5805 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5806 bp->nge_port = 0; 5807 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5808 break; 5809 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5810 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5811 bp->vxlan_gpe_port = 0; 5812 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5813 break; 5814 default: 5815 break; 5816 } 5817 5818 rc = hwrm_req_send(bp, req); 5819 if (rc) 5820 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5821 rc); 5822 if (bp->flags & BNXT_FLAG_TPA) 5823 bnxt_set_tpa(bp, true); 5824 return rc; 5825 } 5826 5827 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5828 u8 tunnel_type) 5829 { 5830 struct hwrm_tunnel_dst_port_alloc_output *resp; 5831 struct hwrm_tunnel_dst_port_alloc_input *req; 5832 int rc; 5833 5834 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5835 if (rc) 5836 return rc; 5837 5838 req->tunnel_type = tunnel_type; 5839 req->tunnel_dst_port_val = port; 5840 5841 resp = hwrm_req_hold(bp, req); 5842 rc = hwrm_req_send(bp, req); 5843 if (rc) { 5844 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5845 rc); 5846 goto err_out; 5847 } 5848 5849 switch (tunnel_type) { 5850 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5851 bp->vxlan_port = port; 5852 bp->vxlan_fw_dst_port_id = 5853 le16_to_cpu(resp->tunnel_dst_port_id); 5854 break; 5855 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5856 bp->nge_port = port; 5857 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5858 break; 5859 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5860 bp->vxlan_gpe_port = port; 5861 bp->vxlan_gpe_fw_dst_port_id = 5862 le16_to_cpu(resp->tunnel_dst_port_id); 5863 break; 5864 default: 5865 break; 5866 } 5867 if (bp->flags & BNXT_FLAG_TPA) 5868 bnxt_set_tpa(bp, true); 5869 5870 err_out: 5871 hwrm_req_drop(bp, req); 5872 return rc; 5873 } 5874 5875 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5876 { 5877 struct hwrm_cfa_l2_set_rx_mask_input *req; 5878 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5879 int rc; 5880 5881 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5882 if (rc) 5883 return rc; 5884 5885 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5886 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5887 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5888 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5889 } 5890 req->mask = cpu_to_le32(vnic->rx_mask); 5891 return hwrm_req_send_silent(bp, req); 5892 } 5893 5894 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5895 { 5896 if (!atomic_dec_and_test(&fltr->refcnt)) 5897 return; 5898 spin_lock_bh(&bp->ntp_fltr_lock); 5899 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5900 spin_unlock_bh(&bp->ntp_fltr_lock); 5901 return; 5902 } 5903 hlist_del_rcu(&fltr->base.hash); 5904 bnxt_del_one_usr_fltr(bp, &fltr->base); 5905 if (fltr->base.flags) { 5906 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5907 bp->ntp_fltr_count--; 5908 } 5909 spin_unlock_bh(&bp->ntp_fltr_lock); 5910 kfree_rcu(fltr, base.rcu); 5911 } 5912 5913 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5914 struct bnxt_l2_key *key, 5915 u32 idx) 5916 { 5917 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5918 struct bnxt_l2_filter *fltr; 5919 5920 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5921 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5922 5923 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5924 l2_key->vlan == key->vlan) 5925 return fltr; 5926 } 5927 return NULL; 5928 } 5929 5930 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5931 struct bnxt_l2_key *key, 5932 u32 idx) 5933 { 5934 struct bnxt_l2_filter *fltr = NULL; 5935 5936 rcu_read_lock(); 5937 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5938 if (fltr) 5939 atomic_inc(&fltr->refcnt); 5940 rcu_read_unlock(); 5941 return fltr; 5942 } 5943 5944 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5945 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5946 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5947 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5948 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5949 5950 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5951 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5952 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5953 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5954 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5955 5956 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5957 { 5958 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5959 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5960 return sizeof(fkeys->addrs.v4addrs) + 5961 sizeof(fkeys->ports); 5962 5963 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5964 return sizeof(fkeys->addrs.v4addrs); 5965 } 5966 5967 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5968 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5969 return sizeof(fkeys->addrs.v6addrs) + 5970 sizeof(fkeys->ports); 5971 5972 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5973 return sizeof(fkeys->addrs.v6addrs); 5974 } 5975 5976 return 0; 5977 } 5978 5979 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5980 const unsigned char *key) 5981 { 5982 u64 prefix = bp->toeplitz_prefix, hash = 0; 5983 struct bnxt_ipv4_tuple tuple4; 5984 struct bnxt_ipv6_tuple tuple6; 5985 int i, j, len = 0; 5986 u8 *four_tuple; 5987 5988 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5989 if (!len) 5990 return 0; 5991 5992 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5993 tuple4.v4addrs = fkeys->addrs.v4addrs; 5994 tuple4.ports = fkeys->ports; 5995 four_tuple = (unsigned char *)&tuple4; 5996 } else { 5997 tuple6.v6addrs = fkeys->addrs.v6addrs; 5998 tuple6.ports = fkeys->ports; 5999 four_tuple = (unsigned char *)&tuple6; 6000 } 6001 6002 for (i = 0, j = 8; i < len; i++, j++) { 6003 u8 byte = four_tuple[i]; 6004 int bit; 6005 6006 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 6007 if (byte & 0x80) 6008 hash ^= prefix; 6009 } 6010 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 6011 } 6012 6013 /* The valid part of the hash is in the upper 32 bits. */ 6014 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 6015 } 6016 6017 #ifdef CONFIG_RFS_ACCEL 6018 static struct bnxt_l2_filter * 6019 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 6020 { 6021 struct bnxt_l2_filter *fltr; 6022 u32 idx; 6023 6024 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6025 BNXT_L2_FLTR_HASH_MASK; 6026 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6027 return fltr; 6028 } 6029 #endif 6030 6031 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 6032 struct bnxt_l2_key *key, u32 idx) 6033 { 6034 struct hlist_head *head; 6035 6036 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 6037 fltr->l2_key.vlan = key->vlan; 6038 fltr->base.type = BNXT_FLTR_TYPE_L2; 6039 if (fltr->base.flags) { 6040 int bit_id; 6041 6042 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 6043 bp->max_fltr, 0); 6044 if (bit_id < 0) 6045 return -ENOMEM; 6046 fltr->base.sw_id = (u16)bit_id; 6047 bp->ntp_fltr_count++; 6048 } 6049 head = &bp->l2_fltr_hash_tbl[idx]; 6050 hlist_add_head_rcu(&fltr->base.hash, head); 6051 bnxt_insert_usr_fltr(bp, &fltr->base); 6052 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 6053 atomic_set(&fltr->refcnt, 1); 6054 return 0; 6055 } 6056 6057 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 6058 struct bnxt_l2_key *key, 6059 gfp_t gfp) 6060 { 6061 struct bnxt_l2_filter *fltr; 6062 u32 idx; 6063 int rc; 6064 6065 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6066 BNXT_L2_FLTR_HASH_MASK; 6067 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6068 if (fltr) 6069 return fltr; 6070 6071 fltr = kzalloc(sizeof(*fltr), gfp); 6072 if (!fltr) 6073 return ERR_PTR(-ENOMEM); 6074 spin_lock_bh(&bp->ntp_fltr_lock); 6075 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6076 spin_unlock_bh(&bp->ntp_fltr_lock); 6077 if (rc) { 6078 bnxt_del_l2_filter(bp, fltr); 6079 fltr = ERR_PTR(rc); 6080 } 6081 return fltr; 6082 } 6083 6084 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 6085 struct bnxt_l2_key *key, 6086 u16 flags) 6087 { 6088 struct bnxt_l2_filter *fltr; 6089 u32 idx; 6090 int rc; 6091 6092 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6093 BNXT_L2_FLTR_HASH_MASK; 6094 spin_lock_bh(&bp->ntp_fltr_lock); 6095 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6096 if (fltr) { 6097 fltr = ERR_PTR(-EEXIST); 6098 goto l2_filter_exit; 6099 } 6100 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 6101 if (!fltr) { 6102 fltr = ERR_PTR(-ENOMEM); 6103 goto l2_filter_exit; 6104 } 6105 fltr->base.flags = flags; 6106 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6107 if (rc) { 6108 spin_unlock_bh(&bp->ntp_fltr_lock); 6109 bnxt_del_l2_filter(bp, fltr); 6110 return ERR_PTR(rc); 6111 } 6112 6113 l2_filter_exit: 6114 spin_unlock_bh(&bp->ntp_fltr_lock); 6115 return fltr; 6116 } 6117 6118 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 6119 { 6120 #ifdef CONFIG_BNXT_SRIOV 6121 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 6122 6123 return vf->fw_fid; 6124 #else 6125 return INVALID_HW_RING_ID; 6126 #endif 6127 } 6128 6129 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6130 { 6131 struct hwrm_cfa_l2_filter_free_input *req; 6132 u16 target_id = 0xffff; 6133 int rc; 6134 6135 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6136 struct bnxt_pf_info *pf = &bp->pf; 6137 6138 if (fltr->base.vf_idx >= pf->active_vfs) 6139 return -EINVAL; 6140 6141 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6142 if (target_id == INVALID_HW_RING_ID) 6143 return -EINVAL; 6144 } 6145 6146 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 6147 if (rc) 6148 return rc; 6149 6150 req->target_id = cpu_to_le16(target_id); 6151 req->l2_filter_id = fltr->base.filter_id; 6152 return hwrm_req_send(bp, req); 6153 } 6154 6155 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6156 { 6157 struct hwrm_cfa_l2_filter_alloc_output *resp; 6158 struct hwrm_cfa_l2_filter_alloc_input *req; 6159 u16 target_id = 0xffff; 6160 int rc; 6161 6162 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6163 struct bnxt_pf_info *pf = &bp->pf; 6164 6165 if (fltr->base.vf_idx >= pf->active_vfs) 6166 return -EINVAL; 6167 6168 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6169 } 6170 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6171 if (rc) 6172 return rc; 6173 6174 req->target_id = cpu_to_le16(target_id); 6175 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6176 6177 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6178 req->flags |= 6179 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6180 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6181 req->enables = 6182 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6183 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6184 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6185 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6186 eth_broadcast_addr(req->l2_addr_mask); 6187 6188 if (fltr->l2_key.vlan) { 6189 req->enables |= 6190 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6191 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6192 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6193 req->num_vlans = 1; 6194 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6195 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6196 } 6197 6198 resp = hwrm_req_hold(bp, req); 6199 rc = hwrm_req_send(bp, req); 6200 if (!rc) { 6201 fltr->base.filter_id = resp->l2_filter_id; 6202 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6203 } 6204 hwrm_req_drop(bp, req); 6205 return rc; 6206 } 6207 6208 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6209 struct bnxt_ntuple_filter *fltr) 6210 { 6211 struct hwrm_cfa_ntuple_filter_free_input *req; 6212 int rc; 6213 6214 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6215 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6216 if (rc) 6217 return rc; 6218 6219 req->ntuple_filter_id = fltr->base.filter_id; 6220 return hwrm_req_send(bp, req); 6221 } 6222 6223 #define BNXT_NTP_FLTR_FLAGS \ 6224 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6225 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6226 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6227 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6228 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6229 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6230 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6231 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6232 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6233 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6234 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6235 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6236 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6237 6238 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6239 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6240 6241 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6242 { 6243 int i; 6244 6245 for (i = 0; i < 4; i++) 6246 mask[i] = cpu_to_be32(~0); 6247 } 6248 6249 static void 6250 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6251 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6252 struct bnxt_ntuple_filter *fltr) 6253 { 6254 u16 rxq = fltr->base.rxq; 6255 6256 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6257 struct ethtool_rxfh_context *ctx; 6258 struct bnxt_rss_ctx *rss_ctx; 6259 struct bnxt_vnic_info *vnic; 6260 6261 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6262 fltr->base.fw_vnic_id); 6263 if (ctx) { 6264 rss_ctx = ethtool_rxfh_context_priv(ctx); 6265 vnic = &rss_ctx->vnic; 6266 6267 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6268 } 6269 return; 6270 } 6271 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6272 struct bnxt_vnic_info *vnic; 6273 u32 enables; 6274 6275 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6276 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6277 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6278 req->enables |= cpu_to_le32(enables); 6279 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6280 } else { 6281 u32 flags; 6282 6283 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6284 req->flags |= cpu_to_le32(flags); 6285 req->dst_id = cpu_to_le16(rxq); 6286 } 6287 } 6288 6289 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6290 struct bnxt_ntuple_filter *fltr) 6291 { 6292 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6293 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6294 struct bnxt_flow_masks *masks = &fltr->fmasks; 6295 struct flow_keys *keys = &fltr->fkeys; 6296 struct bnxt_l2_filter *l2_fltr; 6297 struct bnxt_vnic_info *vnic; 6298 int rc; 6299 6300 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6301 if (rc) 6302 return rc; 6303 6304 l2_fltr = fltr->l2_fltr; 6305 req->l2_filter_id = l2_fltr->base.filter_id; 6306 6307 if (fltr->base.flags & BNXT_ACT_DROP) { 6308 req->flags = 6309 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6310 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6311 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6312 } else { 6313 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6314 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6315 } 6316 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6317 6318 req->ethertype = htons(ETH_P_IP); 6319 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6320 req->ip_protocol = keys->basic.ip_proto; 6321 6322 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6323 req->ethertype = htons(ETH_P_IPV6); 6324 req->ip_addr_type = 6325 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6326 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6327 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6328 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6329 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6330 } else { 6331 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6332 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6333 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6334 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6335 } 6336 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6337 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6338 req->tunnel_type = 6339 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6340 } 6341 6342 req->src_port = keys->ports.src; 6343 req->src_port_mask = masks->ports.src; 6344 req->dst_port = keys->ports.dst; 6345 req->dst_port_mask = masks->ports.dst; 6346 6347 resp = hwrm_req_hold(bp, req); 6348 rc = hwrm_req_send(bp, req); 6349 if (!rc) 6350 fltr->base.filter_id = resp->ntuple_filter_id; 6351 hwrm_req_drop(bp, req); 6352 return rc; 6353 } 6354 6355 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6356 const u8 *mac_addr) 6357 { 6358 struct bnxt_l2_filter *fltr; 6359 struct bnxt_l2_key key; 6360 int rc; 6361 6362 ether_addr_copy(key.dst_mac_addr, mac_addr); 6363 key.vlan = 0; 6364 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6365 if (IS_ERR(fltr)) 6366 return PTR_ERR(fltr); 6367 6368 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6369 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6370 if (rc) 6371 bnxt_del_l2_filter(bp, fltr); 6372 else 6373 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6374 return rc; 6375 } 6376 6377 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6378 { 6379 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6380 6381 /* Any associated ntuple filters will also be cleared by firmware. */ 6382 for (i = 0; i < num_of_vnics; i++) { 6383 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6384 6385 for (j = 0; j < vnic->uc_filter_count; j++) { 6386 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6387 6388 bnxt_hwrm_l2_filter_free(bp, fltr); 6389 bnxt_del_l2_filter(bp, fltr); 6390 } 6391 vnic->uc_filter_count = 0; 6392 } 6393 } 6394 6395 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6396 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6397 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6398 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6399 6400 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6401 struct hwrm_vnic_tpa_cfg_input *req) 6402 { 6403 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6404 6405 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6406 return; 6407 6408 if (bp->vxlan_port) 6409 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6410 if (bp->vxlan_gpe_port) 6411 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6412 if (bp->nge_port) 6413 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6414 6415 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6416 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6417 } 6418 6419 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6420 u32 tpa_flags) 6421 { 6422 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6423 struct hwrm_vnic_tpa_cfg_input *req; 6424 int rc; 6425 6426 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6427 return 0; 6428 6429 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6430 if (rc) 6431 return rc; 6432 6433 if (tpa_flags) { 6434 u16 mss = bp->dev->mtu - 40; 6435 u32 nsegs, n, segs = 0, flags; 6436 6437 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6438 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6439 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6440 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6441 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6442 if (tpa_flags & BNXT_FLAG_GRO) 6443 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6444 6445 req->flags = cpu_to_le32(flags); 6446 6447 req->enables = 6448 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6449 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6450 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6451 6452 /* Number of segs are log2 units, and first packet is not 6453 * included as part of this units. 6454 */ 6455 if (mss <= BNXT_RX_PAGE_SIZE) { 6456 n = BNXT_RX_PAGE_SIZE / mss; 6457 nsegs = (MAX_SKB_FRAGS - 1) * n; 6458 } else { 6459 n = mss / BNXT_RX_PAGE_SIZE; 6460 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6461 n++; 6462 nsegs = (MAX_SKB_FRAGS - n) / n; 6463 } 6464 6465 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6466 segs = MAX_TPA_SEGS_P5; 6467 max_aggs = bp->max_tpa; 6468 } else { 6469 segs = ilog2(nsegs); 6470 } 6471 req->max_agg_segs = cpu_to_le16(segs); 6472 req->max_aggs = cpu_to_le16(max_aggs); 6473 6474 req->min_agg_len = cpu_to_le32(512); 6475 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6476 } 6477 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6478 6479 return hwrm_req_send(bp, req); 6480 } 6481 6482 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6483 { 6484 struct bnxt_ring_grp_info *grp_info; 6485 6486 grp_info = &bp->grp_info[ring->grp_idx]; 6487 return grp_info->cp_fw_ring_id; 6488 } 6489 6490 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6491 { 6492 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6493 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6494 else 6495 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6496 } 6497 6498 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6499 { 6500 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6501 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6502 else 6503 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6504 } 6505 6506 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6507 { 6508 int entries; 6509 6510 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6511 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6512 else 6513 entries = HW_HASH_INDEX_SIZE; 6514 6515 bp->rss_indir_tbl_entries = entries; 6516 bp->rss_indir_tbl = 6517 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6518 if (!bp->rss_indir_tbl) 6519 return -ENOMEM; 6520 6521 return 0; 6522 } 6523 6524 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6525 struct ethtool_rxfh_context *rss_ctx) 6526 { 6527 u16 max_rings, max_entries, pad, i; 6528 u32 *rss_indir_tbl; 6529 6530 if (!bp->rx_nr_rings) 6531 return; 6532 6533 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6534 max_rings = bp->rx_nr_rings - 1; 6535 else 6536 max_rings = bp->rx_nr_rings; 6537 6538 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6539 if (rss_ctx) 6540 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6541 else 6542 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6543 6544 for (i = 0; i < max_entries; i++) 6545 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6546 6547 pad = bp->rss_indir_tbl_entries - max_entries; 6548 if (pad) 6549 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6550 } 6551 6552 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6553 { 6554 u32 i, tbl_size, max_ring = 0; 6555 6556 if (!bp->rss_indir_tbl) 6557 return 0; 6558 6559 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6560 for (i = 0; i < tbl_size; i++) 6561 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6562 return max_ring; 6563 } 6564 6565 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6566 { 6567 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6568 if (!rx_rings) 6569 return 0; 6570 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6571 BNXT_RSS_TABLE_ENTRIES_P5); 6572 } 6573 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6574 return 2; 6575 return 1; 6576 } 6577 6578 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6579 { 6580 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6581 u16 i, j; 6582 6583 /* Fill the RSS indirection table with ring group ids */ 6584 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6585 if (!no_rss) 6586 j = bp->rss_indir_tbl[i]; 6587 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6588 } 6589 } 6590 6591 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6592 struct bnxt_vnic_info *vnic) 6593 { 6594 __le16 *ring_tbl = vnic->rss_table; 6595 struct bnxt_rx_ring_info *rxr; 6596 u16 tbl_size, i; 6597 6598 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6599 6600 for (i = 0; i < tbl_size; i++) { 6601 u16 ring_id, j; 6602 6603 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6604 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6605 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6606 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6607 else 6608 j = bp->rss_indir_tbl[i]; 6609 rxr = &bp->rx_ring[j]; 6610 6611 ring_id = rxr->rx_ring_struct.fw_ring_id; 6612 *ring_tbl++ = cpu_to_le16(ring_id); 6613 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6614 *ring_tbl++ = cpu_to_le16(ring_id); 6615 } 6616 } 6617 6618 static void 6619 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6620 struct bnxt_vnic_info *vnic) 6621 { 6622 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6623 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6624 if (bp->flags & BNXT_FLAG_CHIP_P7) 6625 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6626 } else { 6627 bnxt_fill_hw_rss_tbl(bp, vnic); 6628 } 6629 6630 if (bp->rss_hash_delta) { 6631 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6632 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6633 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6634 else 6635 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6636 } else { 6637 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6638 } 6639 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6640 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6641 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6642 } 6643 6644 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6645 bool set_rss) 6646 { 6647 struct hwrm_vnic_rss_cfg_input *req; 6648 int rc; 6649 6650 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6651 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6652 return 0; 6653 6654 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6655 if (rc) 6656 return rc; 6657 6658 if (set_rss) 6659 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6660 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6661 return hwrm_req_send(bp, req); 6662 } 6663 6664 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6665 struct bnxt_vnic_info *vnic, bool set_rss) 6666 { 6667 struct hwrm_vnic_rss_cfg_input *req; 6668 dma_addr_t ring_tbl_map; 6669 u32 i, nr_ctxs; 6670 int rc; 6671 6672 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6673 if (rc) 6674 return rc; 6675 6676 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6677 if (!set_rss) 6678 return hwrm_req_send(bp, req); 6679 6680 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6681 ring_tbl_map = vnic->rss_table_dma_addr; 6682 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6683 6684 hwrm_req_hold(bp, req); 6685 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6686 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6687 req->ring_table_pair_index = i; 6688 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6689 rc = hwrm_req_send(bp, req); 6690 if (rc) 6691 goto exit; 6692 } 6693 6694 exit: 6695 hwrm_req_drop(bp, req); 6696 return rc; 6697 } 6698 6699 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6700 { 6701 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6702 struct hwrm_vnic_rss_qcfg_output *resp; 6703 struct hwrm_vnic_rss_qcfg_input *req; 6704 6705 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6706 return; 6707 6708 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6709 /* all contexts configured to same hash_type, zero always exists */ 6710 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6711 resp = hwrm_req_hold(bp, req); 6712 if (!hwrm_req_send(bp, req)) { 6713 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6714 bp->rss_hash_delta = 0; 6715 } 6716 hwrm_req_drop(bp, req); 6717 } 6718 6719 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6720 { 6721 u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh; 6722 struct hwrm_vnic_plcmodes_cfg_input *req; 6723 int rc; 6724 6725 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6726 if (rc) 6727 return rc; 6728 6729 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6730 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6731 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6732 6733 if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 6734 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6735 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6736 req->enables |= 6737 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6738 req->hds_threshold = cpu_to_le16(hds_thresh); 6739 } 6740 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6741 return hwrm_req_send(bp, req); 6742 } 6743 6744 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6745 struct bnxt_vnic_info *vnic, 6746 u16 ctx_idx) 6747 { 6748 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6749 6750 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6751 return; 6752 6753 req->rss_cos_lb_ctx_id = 6754 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6755 6756 hwrm_req_send(bp, req); 6757 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6758 } 6759 6760 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6761 { 6762 int i, j; 6763 6764 for (i = 0; i < bp->nr_vnics; i++) { 6765 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6766 6767 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6768 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6769 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6770 } 6771 } 6772 bp->rsscos_nr_ctxs = 0; 6773 } 6774 6775 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6776 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6777 { 6778 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6779 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6780 int rc; 6781 6782 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6783 if (rc) 6784 return rc; 6785 6786 resp = hwrm_req_hold(bp, req); 6787 rc = hwrm_req_send(bp, req); 6788 if (!rc) 6789 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6790 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6791 hwrm_req_drop(bp, req); 6792 6793 return rc; 6794 } 6795 6796 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6797 { 6798 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6799 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6800 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6801 } 6802 6803 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6804 { 6805 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6806 struct hwrm_vnic_cfg_input *req; 6807 unsigned int ring = 0, grp_idx; 6808 u16 def_vlan = 0; 6809 int rc; 6810 6811 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6812 if (rc) 6813 return rc; 6814 6815 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6816 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6817 6818 req->default_rx_ring_id = 6819 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6820 req->default_cmpl_ring_id = 6821 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6822 req->enables = 6823 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6824 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6825 goto vnic_mru; 6826 } 6827 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6828 /* Only RSS support for now TBD: COS & LB */ 6829 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6830 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6831 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6832 VNIC_CFG_REQ_ENABLES_MRU); 6833 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6834 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6835 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6836 VNIC_CFG_REQ_ENABLES_MRU); 6837 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6838 } else { 6839 req->rss_rule = cpu_to_le16(0xffff); 6840 } 6841 6842 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6843 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6844 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6845 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6846 } else { 6847 req->cos_rule = cpu_to_le16(0xffff); 6848 } 6849 6850 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6851 ring = 0; 6852 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6853 ring = vnic->vnic_id - 1; 6854 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6855 ring = bp->rx_nr_rings - 1; 6856 6857 grp_idx = bp->rx_ring[ring].bnapi->index; 6858 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6859 req->lb_rule = cpu_to_le16(0xffff); 6860 vnic_mru: 6861 vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN; 6862 req->mru = cpu_to_le16(vnic->mru); 6863 6864 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6865 #ifdef CONFIG_BNXT_SRIOV 6866 if (BNXT_VF(bp)) 6867 def_vlan = bp->vf.vlan; 6868 #endif 6869 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6870 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6871 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6872 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6873 6874 return hwrm_req_send(bp, req); 6875 } 6876 6877 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6878 struct bnxt_vnic_info *vnic) 6879 { 6880 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6881 struct hwrm_vnic_free_input *req; 6882 6883 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6884 return; 6885 6886 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6887 6888 hwrm_req_send(bp, req); 6889 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6890 } 6891 } 6892 6893 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6894 { 6895 u16 i; 6896 6897 for (i = 0; i < bp->nr_vnics; i++) 6898 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6899 } 6900 6901 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6902 unsigned int start_rx_ring_idx, 6903 unsigned int nr_rings) 6904 { 6905 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6906 struct hwrm_vnic_alloc_output *resp; 6907 struct hwrm_vnic_alloc_input *req; 6908 int rc; 6909 6910 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6911 if (rc) 6912 return rc; 6913 6914 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6915 goto vnic_no_ring_grps; 6916 6917 /* map ring groups to this vnic */ 6918 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6919 grp_idx = bp->rx_ring[i].bnapi->index; 6920 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6921 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6922 j, nr_rings); 6923 break; 6924 } 6925 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6926 } 6927 6928 vnic_no_ring_grps: 6929 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6930 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6931 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6932 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6933 6934 resp = hwrm_req_hold(bp, req); 6935 rc = hwrm_req_send(bp, req); 6936 if (!rc) 6937 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6938 hwrm_req_drop(bp, req); 6939 return rc; 6940 } 6941 6942 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6943 { 6944 struct hwrm_vnic_qcaps_output *resp; 6945 struct hwrm_vnic_qcaps_input *req; 6946 int rc; 6947 6948 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6949 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6950 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6951 if (bp->hwrm_spec_code < 0x10600) 6952 return 0; 6953 6954 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6955 if (rc) 6956 return rc; 6957 6958 resp = hwrm_req_hold(bp, req); 6959 rc = hwrm_req_send(bp, req); 6960 if (!rc) { 6961 u32 flags = le32_to_cpu(resp->flags); 6962 6963 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6964 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6965 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6966 if (flags & 6967 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6968 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6969 6970 /* Older P5 fw before EXT_HW_STATS support did not set 6971 * VLAN_STRIP_CAP properly. 6972 */ 6973 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6974 (BNXT_CHIP_P5(bp) && 6975 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6976 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6977 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6978 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6979 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6980 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6981 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6982 if (bp->max_tpa_v2) { 6983 if (BNXT_CHIP_P5(bp)) 6984 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6985 else 6986 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6987 } 6988 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6989 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6990 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6991 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6992 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6993 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6994 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6995 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6996 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6997 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6998 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP) 6999 bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP; 7000 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 7001 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 7002 } 7003 hwrm_req_drop(bp, req); 7004 return rc; 7005 } 7006 7007 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 7008 { 7009 struct hwrm_ring_grp_alloc_output *resp; 7010 struct hwrm_ring_grp_alloc_input *req; 7011 int rc; 7012 u16 i; 7013 7014 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7015 return 0; 7016 7017 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 7018 if (rc) 7019 return rc; 7020 7021 resp = hwrm_req_hold(bp, req); 7022 for (i = 0; i < bp->rx_nr_rings; i++) { 7023 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 7024 7025 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 7026 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 7027 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 7028 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 7029 7030 rc = hwrm_req_send(bp, req); 7031 7032 if (rc) 7033 break; 7034 7035 bp->grp_info[grp_idx].fw_grp_id = 7036 le32_to_cpu(resp->ring_group_id); 7037 } 7038 hwrm_req_drop(bp, req); 7039 return rc; 7040 } 7041 7042 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 7043 { 7044 struct hwrm_ring_grp_free_input *req; 7045 u16 i; 7046 7047 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7048 return; 7049 7050 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 7051 return; 7052 7053 hwrm_req_hold(bp, req); 7054 for (i = 0; i < bp->cp_nr_rings; i++) { 7055 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 7056 continue; 7057 req->ring_group_id = 7058 cpu_to_le32(bp->grp_info[i].fw_grp_id); 7059 7060 hwrm_req_send(bp, req); 7061 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 7062 } 7063 hwrm_req_drop(bp, req); 7064 } 7065 7066 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type, 7067 struct hwrm_ring_alloc_input *req, 7068 struct bnxt_ring_struct *ring) 7069 { 7070 struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx]; 7071 u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID | 7072 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID; 7073 7074 if (ring_type == HWRM_RING_ALLOC_AGG) { 7075 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 7076 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 7077 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 7078 enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID; 7079 } else { 7080 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 7081 if (NET_IP_ALIGN == 2) 7082 req->flags = 7083 cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD); 7084 } 7085 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7086 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7087 req->enables |= cpu_to_le32(enables); 7088 } 7089 7090 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 7091 struct bnxt_ring_struct *ring, 7092 u32 ring_type, u32 map_index) 7093 { 7094 struct hwrm_ring_alloc_output *resp; 7095 struct hwrm_ring_alloc_input *req; 7096 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 7097 struct bnxt_ring_grp_info *grp_info; 7098 int rc, err = 0; 7099 u16 ring_id; 7100 7101 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 7102 if (rc) 7103 goto exit; 7104 7105 req->enables = 0; 7106 if (rmem->nr_pages > 1) { 7107 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 7108 /* Page size is in log2 units */ 7109 req->page_size = BNXT_PAGE_SHIFT; 7110 req->page_tbl_depth = 1; 7111 } else { 7112 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 7113 } 7114 req->fbo = 0; 7115 /* Association of ring index with doorbell index and MSIX number */ 7116 req->logical_id = cpu_to_le16(map_index); 7117 7118 switch (ring_type) { 7119 case HWRM_RING_ALLOC_TX: { 7120 struct bnxt_tx_ring_info *txr; 7121 u16 flags = 0; 7122 7123 txr = container_of(ring, struct bnxt_tx_ring_info, 7124 tx_ring_struct); 7125 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 7126 /* Association of transmit ring with completion ring */ 7127 grp_info = &bp->grp_info[ring->grp_idx]; 7128 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 7129 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 7130 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7131 req->queue_id = cpu_to_le16(ring->queue_id); 7132 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 7133 req->cmpl_coal_cnt = 7134 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 7135 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 7136 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 7137 req->flags = cpu_to_le16(flags); 7138 break; 7139 } 7140 case HWRM_RING_ALLOC_RX: 7141 case HWRM_RING_ALLOC_AGG: 7142 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 7143 req->length = (ring_type == HWRM_RING_ALLOC_RX) ? 7144 cpu_to_le32(bp->rx_ring_mask + 1) : 7145 cpu_to_le32(bp->rx_agg_ring_mask + 1); 7146 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7147 bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring); 7148 break; 7149 case HWRM_RING_ALLOC_CMPL: 7150 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 7151 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7152 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7153 /* Association of cp ring with nq */ 7154 grp_info = &bp->grp_info[map_index]; 7155 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7156 req->cq_handle = cpu_to_le64(ring->handle); 7157 req->enables |= cpu_to_le32( 7158 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7159 } else { 7160 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7161 } 7162 break; 7163 case HWRM_RING_ALLOC_NQ: 7164 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7165 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7166 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7167 break; 7168 default: 7169 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7170 ring_type); 7171 return -EINVAL; 7172 } 7173 7174 resp = hwrm_req_hold(bp, req); 7175 rc = hwrm_req_send(bp, req); 7176 err = le16_to_cpu(resp->error_code); 7177 ring_id = le16_to_cpu(resp->ring_id); 7178 hwrm_req_drop(bp, req); 7179 7180 exit: 7181 if (rc || err) { 7182 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7183 ring_type, rc, err); 7184 return -EIO; 7185 } 7186 ring->fw_ring_id = ring_id; 7187 return rc; 7188 } 7189 7190 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7191 { 7192 int rc; 7193 7194 if (BNXT_PF(bp)) { 7195 struct hwrm_func_cfg_input *req; 7196 7197 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7198 if (rc) 7199 return rc; 7200 7201 req->fid = cpu_to_le16(0xffff); 7202 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7203 req->async_event_cr = cpu_to_le16(idx); 7204 return hwrm_req_send(bp, req); 7205 } else { 7206 struct hwrm_func_vf_cfg_input *req; 7207 7208 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7209 if (rc) 7210 return rc; 7211 7212 req->enables = 7213 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7214 req->async_event_cr = cpu_to_le16(idx); 7215 return hwrm_req_send(bp, req); 7216 } 7217 } 7218 7219 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7220 u32 ring_type) 7221 { 7222 switch (ring_type) { 7223 case HWRM_RING_ALLOC_TX: 7224 db->db_ring_mask = bp->tx_ring_mask; 7225 break; 7226 case HWRM_RING_ALLOC_RX: 7227 db->db_ring_mask = bp->rx_ring_mask; 7228 break; 7229 case HWRM_RING_ALLOC_AGG: 7230 db->db_ring_mask = bp->rx_agg_ring_mask; 7231 break; 7232 case HWRM_RING_ALLOC_CMPL: 7233 case HWRM_RING_ALLOC_NQ: 7234 db->db_ring_mask = bp->cp_ring_mask; 7235 break; 7236 } 7237 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7238 db->db_epoch_mask = db->db_ring_mask + 1; 7239 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7240 } 7241 } 7242 7243 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7244 u32 map_idx, u32 xid) 7245 { 7246 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7247 switch (ring_type) { 7248 case HWRM_RING_ALLOC_TX: 7249 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7250 break; 7251 case HWRM_RING_ALLOC_RX: 7252 case HWRM_RING_ALLOC_AGG: 7253 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7254 break; 7255 case HWRM_RING_ALLOC_CMPL: 7256 db->db_key64 = DBR_PATH_L2; 7257 break; 7258 case HWRM_RING_ALLOC_NQ: 7259 db->db_key64 = DBR_PATH_L2; 7260 break; 7261 } 7262 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7263 7264 if (bp->flags & BNXT_FLAG_CHIP_P7) 7265 db->db_key64 |= DBR_VALID; 7266 7267 db->doorbell = bp->bar1 + bp->db_offset; 7268 } else { 7269 db->doorbell = bp->bar1 + map_idx * 0x80; 7270 switch (ring_type) { 7271 case HWRM_RING_ALLOC_TX: 7272 db->db_key32 = DB_KEY_TX; 7273 break; 7274 case HWRM_RING_ALLOC_RX: 7275 case HWRM_RING_ALLOC_AGG: 7276 db->db_key32 = DB_KEY_RX; 7277 break; 7278 case HWRM_RING_ALLOC_CMPL: 7279 db->db_key32 = DB_KEY_CP; 7280 break; 7281 } 7282 } 7283 bnxt_set_db_mask(bp, db, ring_type); 7284 } 7285 7286 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7287 struct bnxt_rx_ring_info *rxr) 7288 { 7289 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7290 struct bnxt_napi *bnapi = rxr->bnapi; 7291 u32 type = HWRM_RING_ALLOC_RX; 7292 u32 map_idx = bnapi->index; 7293 int rc; 7294 7295 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7296 if (rc) 7297 return rc; 7298 7299 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7300 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7301 7302 return 0; 7303 } 7304 7305 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7306 struct bnxt_rx_ring_info *rxr) 7307 { 7308 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7309 u32 type = HWRM_RING_ALLOC_AGG; 7310 u32 grp_idx = ring->grp_idx; 7311 u32 map_idx; 7312 int rc; 7313 7314 map_idx = grp_idx + bp->rx_nr_rings; 7315 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7316 if (rc) 7317 return rc; 7318 7319 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7320 ring->fw_ring_id); 7321 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7322 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7323 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7324 7325 return 0; 7326 } 7327 7328 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp, 7329 struct bnxt_cp_ring_info *cpr) 7330 { 7331 const u32 type = HWRM_RING_ALLOC_CMPL; 7332 struct bnxt_napi *bnapi = cpr->bnapi; 7333 struct bnxt_ring_struct *ring; 7334 u32 map_idx = bnapi->index; 7335 int rc; 7336 7337 ring = &cpr->cp_ring_struct; 7338 ring->handle = BNXT_SET_NQ_HDL(cpr); 7339 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7340 if (rc) 7341 return rc; 7342 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7343 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7344 return 0; 7345 } 7346 7347 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp, 7348 struct bnxt_tx_ring_info *txr, u32 tx_idx) 7349 { 7350 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7351 const u32 type = HWRM_RING_ALLOC_TX; 7352 int rc; 7353 7354 rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx); 7355 if (rc) 7356 return rc; 7357 bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id); 7358 return 0; 7359 } 7360 7361 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7362 { 7363 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7364 int i, rc = 0; 7365 u32 type; 7366 7367 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7368 type = HWRM_RING_ALLOC_NQ; 7369 else 7370 type = HWRM_RING_ALLOC_CMPL; 7371 for (i = 0; i < bp->cp_nr_rings; i++) { 7372 struct bnxt_napi *bnapi = bp->bnapi[i]; 7373 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7374 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7375 u32 map_idx = ring->map_idx; 7376 unsigned int vector; 7377 7378 vector = bp->irq_tbl[map_idx].vector; 7379 disable_irq_nosync(vector); 7380 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7381 if (rc) { 7382 enable_irq(vector); 7383 goto err_out; 7384 } 7385 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7386 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7387 enable_irq(vector); 7388 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7389 7390 if (!i) { 7391 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7392 if (rc) 7393 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7394 } 7395 } 7396 7397 for (i = 0; i < bp->tx_nr_rings; i++) { 7398 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7399 7400 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7401 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 7402 if (rc) 7403 goto err_out; 7404 } 7405 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i); 7406 if (rc) 7407 goto err_out; 7408 } 7409 7410 for (i = 0; i < bp->rx_nr_rings; i++) { 7411 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7412 7413 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7414 if (rc) 7415 goto err_out; 7416 /* If we have agg rings, post agg buffers first. */ 7417 if (!agg_rings) 7418 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7419 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7420 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 7421 if (rc) 7422 goto err_out; 7423 } 7424 } 7425 7426 if (agg_rings) { 7427 for (i = 0; i < bp->rx_nr_rings; i++) { 7428 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7429 if (rc) 7430 goto err_out; 7431 } 7432 } 7433 err_out: 7434 return rc; 7435 } 7436 7437 static void bnxt_cancel_dim(struct bnxt *bp) 7438 { 7439 int i; 7440 7441 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7442 * if NAPI is enabled. 7443 */ 7444 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7445 return; 7446 7447 /* Make sure NAPI sees that the VNIC is disabled */ 7448 synchronize_net(); 7449 for (i = 0; i < bp->rx_nr_rings; i++) { 7450 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7451 struct bnxt_napi *bnapi = rxr->bnapi; 7452 7453 cancel_work_sync(&bnapi->cp_ring.dim.work); 7454 } 7455 } 7456 7457 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7458 struct bnxt_ring_struct *ring, 7459 u32 ring_type, int cmpl_ring_id) 7460 { 7461 struct hwrm_ring_free_output *resp; 7462 struct hwrm_ring_free_input *req; 7463 u16 error_code = 0; 7464 int rc; 7465 7466 if (BNXT_NO_FW_ACCESS(bp)) 7467 return 0; 7468 7469 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7470 if (rc) 7471 goto exit; 7472 7473 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7474 req->ring_type = ring_type; 7475 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7476 7477 resp = hwrm_req_hold(bp, req); 7478 rc = hwrm_req_send(bp, req); 7479 error_code = le16_to_cpu(resp->error_code); 7480 hwrm_req_drop(bp, req); 7481 exit: 7482 if (rc || error_code) { 7483 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7484 ring_type, rc, error_code); 7485 return -EIO; 7486 } 7487 return 0; 7488 } 7489 7490 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp, 7491 struct bnxt_tx_ring_info *txr, 7492 bool close_path) 7493 { 7494 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7495 u32 cmpl_ring_id; 7496 7497 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7498 return; 7499 7500 cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) : 7501 INVALID_HW_RING_ID; 7502 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX, 7503 cmpl_ring_id); 7504 ring->fw_ring_id = INVALID_HW_RING_ID; 7505 } 7506 7507 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7508 struct bnxt_rx_ring_info *rxr, 7509 bool close_path) 7510 { 7511 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7512 u32 grp_idx = rxr->bnapi->index; 7513 u32 cmpl_ring_id; 7514 7515 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7516 return; 7517 7518 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7519 hwrm_ring_free_send_msg(bp, ring, 7520 RING_FREE_REQ_RING_TYPE_RX, 7521 close_path ? cmpl_ring_id : 7522 INVALID_HW_RING_ID); 7523 ring->fw_ring_id = INVALID_HW_RING_ID; 7524 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7525 } 7526 7527 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7528 struct bnxt_rx_ring_info *rxr, 7529 bool close_path) 7530 { 7531 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7532 u32 grp_idx = rxr->bnapi->index; 7533 u32 type, cmpl_ring_id; 7534 7535 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7536 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7537 else 7538 type = RING_FREE_REQ_RING_TYPE_RX; 7539 7540 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7541 return; 7542 7543 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7544 hwrm_ring_free_send_msg(bp, ring, type, 7545 close_path ? cmpl_ring_id : 7546 INVALID_HW_RING_ID); 7547 ring->fw_ring_id = INVALID_HW_RING_ID; 7548 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7549 } 7550 7551 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp, 7552 struct bnxt_cp_ring_info *cpr) 7553 { 7554 struct bnxt_ring_struct *ring; 7555 7556 ring = &cpr->cp_ring_struct; 7557 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7558 return; 7559 7560 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL, 7561 INVALID_HW_RING_ID); 7562 ring->fw_ring_id = INVALID_HW_RING_ID; 7563 } 7564 7565 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 7566 { 7567 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7568 int i, size = ring->ring_mem.page_size; 7569 7570 cpr->cp_raw_cons = 0; 7571 cpr->toggle = 0; 7572 7573 for (i = 0; i < bp->cp_nr_pages; i++) 7574 if (cpr->cp_desc_ring[i]) 7575 memset(cpr->cp_desc_ring[i], 0, size); 7576 } 7577 7578 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7579 { 7580 u32 type; 7581 int i; 7582 7583 if (!bp->bnapi) 7584 return; 7585 7586 for (i = 0; i < bp->tx_nr_rings; i++) 7587 bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path); 7588 7589 bnxt_cancel_dim(bp); 7590 for (i = 0; i < bp->rx_nr_rings; i++) { 7591 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7592 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7593 } 7594 7595 /* The completion rings are about to be freed. After that the 7596 * IRQ doorbell will not work anymore. So we need to disable 7597 * IRQ here. 7598 */ 7599 bnxt_disable_int_sync(bp); 7600 7601 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7602 type = RING_FREE_REQ_RING_TYPE_NQ; 7603 else 7604 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7605 for (i = 0; i < bp->cp_nr_rings; i++) { 7606 struct bnxt_napi *bnapi = bp->bnapi[i]; 7607 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7608 struct bnxt_ring_struct *ring; 7609 int j; 7610 7611 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) 7612 bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]); 7613 7614 ring = &cpr->cp_ring_struct; 7615 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7616 hwrm_ring_free_send_msg(bp, ring, type, 7617 INVALID_HW_RING_ID); 7618 ring->fw_ring_id = INVALID_HW_RING_ID; 7619 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7620 } 7621 } 7622 } 7623 7624 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7625 bool shared); 7626 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7627 bool shared); 7628 7629 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7630 { 7631 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7632 struct hwrm_func_qcfg_output *resp; 7633 struct hwrm_func_qcfg_input *req; 7634 int rc; 7635 7636 if (bp->hwrm_spec_code < 0x10601) 7637 return 0; 7638 7639 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7640 if (rc) 7641 return rc; 7642 7643 req->fid = cpu_to_le16(0xffff); 7644 resp = hwrm_req_hold(bp, req); 7645 rc = hwrm_req_send(bp, req); 7646 if (rc) { 7647 hwrm_req_drop(bp, req); 7648 return rc; 7649 } 7650 7651 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7652 if (BNXT_NEW_RM(bp)) { 7653 u16 cp, stats; 7654 7655 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7656 hw_resc->resv_hw_ring_grps = 7657 le32_to_cpu(resp->alloc_hw_ring_grps); 7658 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7659 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7660 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7661 stats = le16_to_cpu(resp->alloc_stat_ctx); 7662 hw_resc->resv_irqs = cp; 7663 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7664 int rx = hw_resc->resv_rx_rings; 7665 int tx = hw_resc->resv_tx_rings; 7666 7667 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7668 rx >>= 1; 7669 if (cp < (rx + tx)) { 7670 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7671 if (rc) 7672 goto get_rings_exit; 7673 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7674 rx <<= 1; 7675 hw_resc->resv_rx_rings = rx; 7676 hw_resc->resv_tx_rings = tx; 7677 } 7678 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7679 hw_resc->resv_hw_ring_grps = rx; 7680 } 7681 hw_resc->resv_cp_rings = cp; 7682 hw_resc->resv_stat_ctxs = stats; 7683 } 7684 get_rings_exit: 7685 hwrm_req_drop(bp, req); 7686 return rc; 7687 } 7688 7689 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7690 { 7691 struct hwrm_func_qcfg_output *resp; 7692 struct hwrm_func_qcfg_input *req; 7693 int rc; 7694 7695 if (bp->hwrm_spec_code < 0x10601) 7696 return 0; 7697 7698 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7699 if (rc) 7700 return rc; 7701 7702 req->fid = cpu_to_le16(fid); 7703 resp = hwrm_req_hold(bp, req); 7704 rc = hwrm_req_send(bp, req); 7705 if (!rc) 7706 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7707 7708 hwrm_req_drop(bp, req); 7709 return rc; 7710 } 7711 7712 static bool bnxt_rfs_supported(struct bnxt *bp); 7713 7714 static struct hwrm_func_cfg_input * 7715 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7716 { 7717 struct hwrm_func_cfg_input *req; 7718 u32 enables = 0; 7719 7720 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7721 return NULL; 7722 7723 req->fid = cpu_to_le16(0xffff); 7724 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7725 req->num_tx_rings = cpu_to_le16(hwr->tx); 7726 if (BNXT_NEW_RM(bp)) { 7727 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7728 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7729 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7730 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7731 enables |= hwr->cp_p5 ? 7732 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7733 } else { 7734 enables |= hwr->cp ? 7735 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7736 enables |= hwr->grp ? 7737 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7738 } 7739 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7740 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7741 0; 7742 req->num_rx_rings = cpu_to_le16(hwr->rx); 7743 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7744 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7745 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7746 req->num_msix = cpu_to_le16(hwr->cp); 7747 } else { 7748 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7749 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7750 } 7751 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7752 req->num_vnics = cpu_to_le16(hwr->vnic); 7753 } 7754 req->enables = cpu_to_le32(enables); 7755 return req; 7756 } 7757 7758 static struct hwrm_func_vf_cfg_input * 7759 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7760 { 7761 struct hwrm_func_vf_cfg_input *req; 7762 u32 enables = 0; 7763 7764 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7765 return NULL; 7766 7767 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7768 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7769 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7770 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7771 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7772 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7773 enables |= hwr->cp_p5 ? 7774 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7775 } else { 7776 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7777 enables |= hwr->grp ? 7778 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7779 } 7780 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7781 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7782 7783 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7784 req->num_tx_rings = cpu_to_le16(hwr->tx); 7785 req->num_rx_rings = cpu_to_le16(hwr->rx); 7786 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7787 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7788 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7789 } else { 7790 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7791 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7792 } 7793 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7794 req->num_vnics = cpu_to_le16(hwr->vnic); 7795 7796 req->enables = cpu_to_le32(enables); 7797 return req; 7798 } 7799 7800 static int 7801 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7802 { 7803 struct hwrm_func_cfg_input *req; 7804 int rc; 7805 7806 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7807 if (!req) 7808 return -ENOMEM; 7809 7810 if (!req->enables) { 7811 hwrm_req_drop(bp, req); 7812 return 0; 7813 } 7814 7815 rc = hwrm_req_send(bp, req); 7816 if (rc) 7817 return rc; 7818 7819 if (bp->hwrm_spec_code < 0x10601) 7820 bp->hw_resc.resv_tx_rings = hwr->tx; 7821 7822 return bnxt_hwrm_get_rings(bp); 7823 } 7824 7825 static int 7826 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7827 { 7828 struct hwrm_func_vf_cfg_input *req; 7829 int rc; 7830 7831 if (!BNXT_NEW_RM(bp)) { 7832 bp->hw_resc.resv_tx_rings = hwr->tx; 7833 return 0; 7834 } 7835 7836 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7837 if (!req) 7838 return -ENOMEM; 7839 7840 rc = hwrm_req_send(bp, req); 7841 if (rc) 7842 return rc; 7843 7844 return bnxt_hwrm_get_rings(bp); 7845 } 7846 7847 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7848 { 7849 if (BNXT_PF(bp)) 7850 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7851 else 7852 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7853 } 7854 7855 int bnxt_nq_rings_in_use(struct bnxt *bp) 7856 { 7857 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7858 } 7859 7860 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7861 { 7862 int cp; 7863 7864 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7865 return bnxt_nq_rings_in_use(bp); 7866 7867 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7868 return cp; 7869 } 7870 7871 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7872 { 7873 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7874 } 7875 7876 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7877 { 7878 if (!hwr->grp) 7879 return 0; 7880 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7881 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7882 7883 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7884 rss_ctx *= hwr->vnic; 7885 return rss_ctx; 7886 } 7887 if (BNXT_VF(bp)) 7888 return BNXT_VF_MAX_RSS_CTX; 7889 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7890 return hwr->grp + 1; 7891 return 1; 7892 } 7893 7894 /* Check if a default RSS map needs to be setup. This function is only 7895 * used on older firmware that does not require reserving RX rings. 7896 */ 7897 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7898 { 7899 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7900 7901 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7902 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7903 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7904 if (!netif_is_rxfh_configured(bp->dev)) 7905 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7906 } 7907 } 7908 7909 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7910 { 7911 if (bp->flags & BNXT_FLAG_RFS) { 7912 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7913 return 2 + bp->num_rss_ctx; 7914 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7915 return rx_rings + 1; 7916 } 7917 return 1; 7918 } 7919 7920 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7921 { 7922 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7923 int cp = bnxt_cp_rings_in_use(bp); 7924 int nq = bnxt_nq_rings_in_use(bp); 7925 int rx = bp->rx_nr_rings, stat; 7926 int vnic, grp = rx; 7927 7928 /* Old firmware does not need RX ring reservations but we still 7929 * need to setup a default RSS map when needed. With new firmware 7930 * we go through RX ring reservations first and then set up the 7931 * RSS map for the successfully reserved RX rings when needed. 7932 */ 7933 if (!BNXT_NEW_RM(bp)) 7934 bnxt_check_rss_tbl_no_rmgr(bp); 7935 7936 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7937 bp->hwrm_spec_code >= 0x10601) 7938 return true; 7939 7940 if (!BNXT_NEW_RM(bp)) 7941 return false; 7942 7943 vnic = bnxt_get_total_vnics(bp, rx); 7944 7945 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7946 rx <<= 1; 7947 stat = bnxt_get_func_stat_ctxs(bp); 7948 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7949 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7950 (hw_resc->resv_hw_ring_grps != grp && 7951 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7952 return true; 7953 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7954 hw_resc->resv_irqs != nq) 7955 return true; 7956 return false; 7957 } 7958 7959 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7960 { 7961 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7962 7963 hwr->tx = hw_resc->resv_tx_rings; 7964 if (BNXT_NEW_RM(bp)) { 7965 hwr->rx = hw_resc->resv_rx_rings; 7966 hwr->cp = hw_resc->resv_irqs; 7967 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7968 hwr->cp_p5 = hw_resc->resv_cp_rings; 7969 hwr->grp = hw_resc->resv_hw_ring_grps; 7970 hwr->vnic = hw_resc->resv_vnics; 7971 hwr->stat = hw_resc->resv_stat_ctxs; 7972 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7973 } 7974 } 7975 7976 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7977 { 7978 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7979 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7980 } 7981 7982 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7983 7984 static int __bnxt_reserve_rings(struct bnxt *bp) 7985 { 7986 struct bnxt_hw_rings hwr = {0}; 7987 int rx_rings, old_rx_rings, rc; 7988 int cp = bp->cp_nr_rings; 7989 int ulp_msix = 0; 7990 bool sh = false; 7991 int tx_cp; 7992 7993 if (!bnxt_need_reserve_rings(bp)) 7994 return 0; 7995 7996 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7997 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7998 if (!ulp_msix) 7999 bnxt_set_ulp_stat_ctxs(bp, 0); 8000 8001 if (ulp_msix > bp->ulp_num_msix_want) 8002 ulp_msix = bp->ulp_num_msix_want; 8003 hwr.cp = cp + ulp_msix; 8004 } else { 8005 hwr.cp = bnxt_nq_rings_in_use(bp); 8006 } 8007 8008 hwr.tx = bp->tx_nr_rings; 8009 hwr.rx = bp->rx_nr_rings; 8010 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 8011 sh = true; 8012 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8013 hwr.cp_p5 = hwr.rx + hwr.tx; 8014 8015 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 8016 8017 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8018 hwr.rx <<= 1; 8019 hwr.grp = bp->rx_nr_rings; 8020 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 8021 hwr.stat = bnxt_get_func_stat_ctxs(bp); 8022 old_rx_rings = bp->hw_resc.resv_rx_rings; 8023 8024 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 8025 if (rc) 8026 return rc; 8027 8028 bnxt_copy_reserved_rings(bp, &hwr); 8029 8030 rx_rings = hwr.rx; 8031 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 8032 if (hwr.rx >= 2) { 8033 rx_rings = hwr.rx >> 1; 8034 } else { 8035 if (netif_running(bp->dev)) 8036 return -ENOMEM; 8037 8038 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 8039 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 8040 bp->dev->hw_features &= ~NETIF_F_LRO; 8041 bp->dev->features &= ~NETIF_F_LRO; 8042 bnxt_set_ring_params(bp); 8043 } 8044 } 8045 rx_rings = min_t(int, rx_rings, hwr.grp); 8046 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 8047 if (bnxt_ulp_registered(bp->edev) && 8048 hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 8049 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 8050 hwr.cp = min_t(int, hwr.cp, hwr.stat); 8051 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 8052 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8053 hwr.rx = rx_rings << 1; 8054 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 8055 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 8056 if (hwr.tx != bp->tx_nr_rings) { 8057 netdev_warn(bp->dev, 8058 "Able to reserve only %d out of %d requested TX rings\n", 8059 hwr.tx, bp->tx_nr_rings); 8060 } 8061 bp->tx_nr_rings = hwr.tx; 8062 8063 /* If we cannot reserve all the RX rings, reset the RSS map only 8064 * if absolutely necessary 8065 */ 8066 if (rx_rings != bp->rx_nr_rings) { 8067 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 8068 rx_rings, bp->rx_nr_rings); 8069 if (netif_is_rxfh_configured(bp->dev) && 8070 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 8071 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 8072 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 8073 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 8074 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 8075 } 8076 } 8077 bp->rx_nr_rings = rx_rings; 8078 bp->cp_nr_rings = hwr.cp; 8079 8080 if (!bnxt_rings_ok(bp, &hwr)) 8081 return -ENOMEM; 8082 8083 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 8084 !netif_is_rxfh_configured(bp->dev)) 8085 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8086 8087 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 8088 int resv_msix, resv_ctx, ulp_ctxs; 8089 struct bnxt_hw_resc *hw_resc; 8090 8091 hw_resc = &bp->hw_resc; 8092 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 8093 ulp_msix = min_t(int, resv_msix, ulp_msix); 8094 bnxt_set_ulp_msix_num(bp, ulp_msix); 8095 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 8096 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 8097 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 8098 } 8099 8100 return rc; 8101 } 8102 8103 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8104 { 8105 struct hwrm_func_vf_cfg_input *req; 8106 u32 flags; 8107 8108 if (!BNXT_NEW_RM(bp)) 8109 return 0; 8110 8111 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8112 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 8113 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8114 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8115 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8116 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 8117 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 8118 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8119 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8120 8121 req->flags = cpu_to_le32(flags); 8122 return hwrm_req_send_silent(bp, req); 8123 } 8124 8125 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8126 { 8127 struct hwrm_func_cfg_input *req; 8128 u32 flags; 8129 8130 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 8131 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 8132 if (BNXT_NEW_RM(bp)) { 8133 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8134 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8135 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8136 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 8137 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8138 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 8139 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 8140 else 8141 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8142 } 8143 8144 req->flags = cpu_to_le32(flags); 8145 return hwrm_req_send_silent(bp, req); 8146 } 8147 8148 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8149 { 8150 if (bp->hwrm_spec_code < 0x10801) 8151 return 0; 8152 8153 if (BNXT_PF(bp)) 8154 return bnxt_hwrm_check_pf_rings(bp, hwr); 8155 8156 return bnxt_hwrm_check_vf_rings(bp, hwr); 8157 } 8158 8159 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 8160 { 8161 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8162 struct hwrm_ring_aggint_qcaps_output *resp; 8163 struct hwrm_ring_aggint_qcaps_input *req; 8164 int rc; 8165 8166 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 8167 coal_cap->num_cmpl_dma_aggr_max = 63; 8168 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 8169 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 8170 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 8171 coal_cap->int_lat_tmr_min_max = 65535; 8172 coal_cap->int_lat_tmr_max_max = 65535; 8173 coal_cap->num_cmpl_aggr_int_max = 65535; 8174 coal_cap->timer_units = 80; 8175 8176 if (bp->hwrm_spec_code < 0x10902) 8177 return; 8178 8179 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 8180 return; 8181 8182 resp = hwrm_req_hold(bp, req); 8183 rc = hwrm_req_send_silent(bp, req); 8184 if (!rc) { 8185 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 8186 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 8187 coal_cap->num_cmpl_dma_aggr_max = 8188 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 8189 coal_cap->num_cmpl_dma_aggr_during_int_max = 8190 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8191 coal_cap->cmpl_aggr_dma_tmr_max = 8192 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8193 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8194 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8195 coal_cap->int_lat_tmr_min_max = 8196 le16_to_cpu(resp->int_lat_tmr_min_max); 8197 coal_cap->int_lat_tmr_max_max = 8198 le16_to_cpu(resp->int_lat_tmr_max_max); 8199 coal_cap->num_cmpl_aggr_int_max = 8200 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8201 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8202 } 8203 hwrm_req_drop(bp, req); 8204 } 8205 8206 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8207 { 8208 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8209 8210 return usec * 1000 / coal_cap->timer_units; 8211 } 8212 8213 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8214 struct bnxt_coal *hw_coal, 8215 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8216 { 8217 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8218 u16 val, tmr, max, flags = hw_coal->flags; 8219 u32 cmpl_params = coal_cap->cmpl_params; 8220 8221 max = hw_coal->bufs_per_record * 128; 8222 if (hw_coal->budget) 8223 max = hw_coal->bufs_per_record * hw_coal->budget; 8224 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8225 8226 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8227 req->num_cmpl_aggr_int = cpu_to_le16(val); 8228 8229 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8230 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8231 8232 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8233 coal_cap->num_cmpl_dma_aggr_during_int_max); 8234 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8235 8236 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8237 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8238 req->int_lat_tmr_max = cpu_to_le16(tmr); 8239 8240 /* min timer set to 1/2 of interrupt timer */ 8241 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8242 val = tmr / 2; 8243 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8244 req->int_lat_tmr_min = cpu_to_le16(val); 8245 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8246 } 8247 8248 /* buf timer set to 1/4 of interrupt timer */ 8249 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8250 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8251 8252 if (cmpl_params & 8253 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8254 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8255 val = clamp_t(u16, tmr, 1, 8256 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8257 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8258 req->enables |= 8259 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8260 } 8261 8262 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8263 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8264 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8265 req->flags = cpu_to_le16(flags); 8266 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8267 } 8268 8269 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8270 struct bnxt_coal *hw_coal) 8271 { 8272 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8273 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8274 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8275 u32 nq_params = coal_cap->nq_params; 8276 u16 tmr; 8277 int rc; 8278 8279 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8280 return 0; 8281 8282 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8283 if (rc) 8284 return rc; 8285 8286 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8287 req->flags = 8288 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8289 8290 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8291 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8292 req->int_lat_tmr_min = cpu_to_le16(tmr); 8293 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8294 return hwrm_req_send(bp, req); 8295 } 8296 8297 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8298 { 8299 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8300 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8301 struct bnxt_coal coal; 8302 int rc; 8303 8304 /* Tick values in micro seconds. 8305 * 1 coal_buf x bufs_per_record = 1 completion record. 8306 */ 8307 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8308 8309 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8310 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8311 8312 if (!bnapi->rx_ring) 8313 return -ENODEV; 8314 8315 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8316 if (rc) 8317 return rc; 8318 8319 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8320 8321 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8322 8323 return hwrm_req_send(bp, req_rx); 8324 } 8325 8326 static int 8327 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8328 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8329 { 8330 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8331 8332 req->ring_id = cpu_to_le16(ring_id); 8333 return hwrm_req_send(bp, req); 8334 } 8335 8336 static int 8337 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8338 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8339 { 8340 struct bnxt_tx_ring_info *txr; 8341 int i, rc; 8342 8343 bnxt_for_each_napi_tx(i, bnapi, txr) { 8344 u16 ring_id; 8345 8346 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8347 req->ring_id = cpu_to_le16(ring_id); 8348 rc = hwrm_req_send(bp, req); 8349 if (rc) 8350 return rc; 8351 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8352 return 0; 8353 } 8354 return 0; 8355 } 8356 8357 int bnxt_hwrm_set_coal(struct bnxt *bp) 8358 { 8359 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8360 int i, rc; 8361 8362 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8363 if (rc) 8364 return rc; 8365 8366 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8367 if (rc) { 8368 hwrm_req_drop(bp, req_rx); 8369 return rc; 8370 } 8371 8372 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8373 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8374 8375 hwrm_req_hold(bp, req_rx); 8376 hwrm_req_hold(bp, req_tx); 8377 for (i = 0; i < bp->cp_nr_rings; i++) { 8378 struct bnxt_napi *bnapi = bp->bnapi[i]; 8379 struct bnxt_coal *hw_coal; 8380 8381 if (!bnapi->rx_ring) 8382 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8383 else 8384 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8385 if (rc) 8386 break; 8387 8388 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8389 continue; 8390 8391 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8392 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8393 if (rc) 8394 break; 8395 } 8396 if (bnapi->rx_ring) 8397 hw_coal = &bp->rx_coal; 8398 else 8399 hw_coal = &bp->tx_coal; 8400 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8401 } 8402 hwrm_req_drop(bp, req_rx); 8403 hwrm_req_drop(bp, req_tx); 8404 return rc; 8405 } 8406 8407 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8408 { 8409 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8410 struct hwrm_stat_ctx_free_input *req; 8411 int i; 8412 8413 if (!bp->bnapi) 8414 return; 8415 8416 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8417 return; 8418 8419 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8420 return; 8421 if (BNXT_FW_MAJ(bp) <= 20) { 8422 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8423 hwrm_req_drop(bp, req); 8424 return; 8425 } 8426 hwrm_req_hold(bp, req0); 8427 } 8428 hwrm_req_hold(bp, req); 8429 for (i = 0; i < bp->cp_nr_rings; i++) { 8430 struct bnxt_napi *bnapi = bp->bnapi[i]; 8431 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8432 8433 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8434 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8435 if (req0) { 8436 req0->stat_ctx_id = req->stat_ctx_id; 8437 hwrm_req_send(bp, req0); 8438 } 8439 hwrm_req_send(bp, req); 8440 8441 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8442 } 8443 } 8444 hwrm_req_drop(bp, req); 8445 if (req0) 8446 hwrm_req_drop(bp, req0); 8447 } 8448 8449 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8450 { 8451 struct hwrm_stat_ctx_alloc_output *resp; 8452 struct hwrm_stat_ctx_alloc_input *req; 8453 int rc, i; 8454 8455 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8456 return 0; 8457 8458 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8459 if (rc) 8460 return rc; 8461 8462 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8463 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8464 8465 resp = hwrm_req_hold(bp, req); 8466 for (i = 0; i < bp->cp_nr_rings; i++) { 8467 struct bnxt_napi *bnapi = bp->bnapi[i]; 8468 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8469 8470 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8471 8472 rc = hwrm_req_send(bp, req); 8473 if (rc) 8474 break; 8475 8476 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8477 8478 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8479 } 8480 hwrm_req_drop(bp, req); 8481 return rc; 8482 } 8483 8484 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8485 { 8486 struct hwrm_func_qcfg_output *resp; 8487 struct hwrm_func_qcfg_input *req; 8488 u16 flags; 8489 int rc; 8490 8491 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8492 if (rc) 8493 return rc; 8494 8495 req->fid = cpu_to_le16(0xffff); 8496 resp = hwrm_req_hold(bp, req); 8497 rc = hwrm_req_send(bp, req); 8498 if (rc) 8499 goto func_qcfg_exit; 8500 8501 flags = le16_to_cpu(resp->flags); 8502 #ifdef CONFIG_BNXT_SRIOV 8503 if (BNXT_VF(bp)) { 8504 struct bnxt_vf_info *vf = &bp->vf; 8505 8506 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8507 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8508 vf->flags |= BNXT_VF_TRUST; 8509 else 8510 vf->flags &= ~BNXT_VF_TRUST; 8511 } else { 8512 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8513 } 8514 #endif 8515 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8516 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8517 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8518 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8519 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8520 } 8521 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8522 bp->flags |= BNXT_FLAG_MULTI_HOST; 8523 8524 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8525 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8526 8527 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8528 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8529 if (resp->roce_bidi_opt_mode & 8530 FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED) 8531 bp->cos0_cos1_shared = 1; 8532 else 8533 bp->cos0_cos1_shared = 0; 8534 8535 switch (resp->port_partition_type) { 8536 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8537 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2: 8538 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8539 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8540 bp->port_partition_type = resp->port_partition_type; 8541 break; 8542 } 8543 if (bp->hwrm_spec_code < 0x10707 || 8544 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8545 bp->br_mode = BRIDGE_MODE_VEB; 8546 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8547 bp->br_mode = BRIDGE_MODE_VEPA; 8548 else 8549 bp->br_mode = BRIDGE_MODE_UNDEF; 8550 8551 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8552 if (!bp->max_mtu) 8553 bp->max_mtu = BNXT_MAX_MTU; 8554 8555 if (bp->db_size) 8556 goto func_qcfg_exit; 8557 8558 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8559 if (BNXT_CHIP_P5(bp)) { 8560 if (BNXT_PF(bp)) 8561 bp->db_offset = DB_PF_OFFSET_P5; 8562 else 8563 bp->db_offset = DB_VF_OFFSET_P5; 8564 } 8565 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8566 1024); 8567 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8568 bp->db_size <= bp->db_offset) 8569 bp->db_size = pci_resource_len(bp->pdev, 2); 8570 8571 func_qcfg_exit: 8572 hwrm_req_drop(bp, req); 8573 return rc; 8574 } 8575 8576 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8577 u8 init_val, u8 init_offset, 8578 bool init_mask_set) 8579 { 8580 ctxm->init_value = init_val; 8581 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8582 if (init_mask_set) 8583 ctxm->init_offset = init_offset * 4; 8584 else 8585 ctxm->init_value = 0; 8586 } 8587 8588 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8589 { 8590 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8591 u16 type; 8592 8593 for (type = 0; type < ctx_max; type++) { 8594 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8595 int n = 1; 8596 8597 if (!ctxm->max_entries || ctxm->pg_info) 8598 continue; 8599 8600 if (ctxm->instance_bmap) 8601 n = hweight32(ctxm->instance_bmap); 8602 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8603 if (!ctxm->pg_info) 8604 return -ENOMEM; 8605 } 8606 return 0; 8607 } 8608 8609 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8610 struct bnxt_ctx_mem_type *ctxm, bool force); 8611 8612 #define BNXT_CTX_INIT_VALID(flags) \ 8613 (!!((flags) & \ 8614 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8615 8616 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8617 { 8618 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8619 struct hwrm_func_backing_store_qcaps_v2_input *req; 8620 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8621 u16 type; 8622 int rc; 8623 8624 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8625 if (rc) 8626 return rc; 8627 8628 if (!ctx) { 8629 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8630 if (!ctx) 8631 return -ENOMEM; 8632 bp->ctx = ctx; 8633 } 8634 8635 resp = hwrm_req_hold(bp, req); 8636 8637 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8638 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8639 u8 init_val, init_off, i; 8640 u32 max_entries; 8641 u16 entry_size; 8642 __le32 *p; 8643 u32 flags; 8644 8645 req->type = cpu_to_le16(type); 8646 rc = hwrm_req_send(bp, req); 8647 if (rc) 8648 goto ctx_done; 8649 flags = le32_to_cpu(resp->flags); 8650 type = le16_to_cpu(resp->next_valid_type); 8651 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8652 bnxt_free_one_ctx_mem(bp, ctxm, true); 8653 continue; 8654 } 8655 entry_size = le16_to_cpu(resp->entry_size); 8656 max_entries = le32_to_cpu(resp->max_num_entries); 8657 if (ctxm->mem_valid) { 8658 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8659 ctxm->entry_size != entry_size || 8660 ctxm->max_entries != max_entries) 8661 bnxt_free_one_ctx_mem(bp, ctxm, true); 8662 else 8663 continue; 8664 } 8665 ctxm->type = le16_to_cpu(resp->type); 8666 ctxm->entry_size = entry_size; 8667 ctxm->flags = flags; 8668 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8669 ctxm->entry_multiple = resp->entry_multiple; 8670 ctxm->max_entries = max_entries; 8671 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8672 init_val = resp->ctx_init_value; 8673 init_off = resp->ctx_init_offset; 8674 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8675 BNXT_CTX_INIT_VALID(flags)); 8676 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8677 BNXT_MAX_SPLIT_ENTRY); 8678 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8679 i++, p++) 8680 ctxm->split[i] = le32_to_cpu(*p); 8681 } 8682 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8683 8684 ctx_done: 8685 hwrm_req_drop(bp, req); 8686 return rc; 8687 } 8688 8689 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8690 { 8691 struct hwrm_func_backing_store_qcaps_output *resp; 8692 struct hwrm_func_backing_store_qcaps_input *req; 8693 int rc; 8694 8695 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8696 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8697 return 0; 8698 8699 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8700 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8701 8702 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8703 if (rc) 8704 return rc; 8705 8706 resp = hwrm_req_hold(bp, req); 8707 rc = hwrm_req_send_silent(bp, req); 8708 if (!rc) { 8709 struct bnxt_ctx_mem_type *ctxm; 8710 struct bnxt_ctx_mem_info *ctx; 8711 u8 init_val, init_idx = 0; 8712 u16 init_mask; 8713 8714 ctx = bp->ctx; 8715 if (!ctx) { 8716 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8717 if (!ctx) { 8718 rc = -ENOMEM; 8719 goto ctx_err; 8720 } 8721 bp->ctx = ctx; 8722 } 8723 init_val = resp->ctx_kind_initializer; 8724 init_mask = le16_to_cpu(resp->ctx_init_mask); 8725 8726 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8727 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8728 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8729 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8730 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8731 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8732 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8733 (init_mask & (1 << init_idx++)) != 0); 8734 8735 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8736 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8737 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8738 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8739 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8740 (init_mask & (1 << init_idx++)) != 0); 8741 8742 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8743 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8744 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8745 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8746 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8747 (init_mask & (1 << init_idx++)) != 0); 8748 8749 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8750 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8751 ctxm->max_entries = ctxm->vnic_entries + 8752 le16_to_cpu(resp->vnic_max_ring_table_entries); 8753 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8754 bnxt_init_ctx_initializer(ctxm, init_val, 8755 resp->vnic_init_offset, 8756 (init_mask & (1 << init_idx++)) != 0); 8757 8758 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8759 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8760 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8761 bnxt_init_ctx_initializer(ctxm, init_val, 8762 resp->stat_init_offset, 8763 (init_mask & (1 << init_idx++)) != 0); 8764 8765 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8766 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8767 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8768 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8769 ctxm->entry_multiple = resp->tqm_entries_multiple; 8770 if (!ctxm->entry_multiple) 8771 ctxm->entry_multiple = 1; 8772 8773 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8774 8775 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8776 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8777 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8778 ctxm->mrav_num_entries_units = 8779 le16_to_cpu(resp->mrav_num_entries_units); 8780 bnxt_init_ctx_initializer(ctxm, init_val, 8781 resp->mrav_init_offset, 8782 (init_mask & (1 << init_idx++)) != 0); 8783 8784 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8785 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8786 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8787 8788 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8789 if (!ctx->tqm_fp_rings_count) 8790 ctx->tqm_fp_rings_count = bp->max_q; 8791 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8792 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8793 8794 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8795 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8796 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8797 8798 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8799 } else { 8800 rc = 0; 8801 } 8802 ctx_err: 8803 hwrm_req_drop(bp, req); 8804 return rc; 8805 } 8806 8807 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8808 __le64 *pg_dir) 8809 { 8810 if (!rmem->nr_pages) 8811 return; 8812 8813 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8814 if (rmem->depth >= 1) { 8815 if (rmem->depth == 2) 8816 *pg_attr |= 2; 8817 else 8818 *pg_attr |= 1; 8819 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8820 } else { 8821 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8822 } 8823 } 8824 8825 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8826 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8827 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8828 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8829 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8830 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8831 8832 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8833 { 8834 struct hwrm_func_backing_store_cfg_input *req; 8835 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8836 struct bnxt_ctx_pg_info *ctx_pg; 8837 struct bnxt_ctx_mem_type *ctxm; 8838 void **__req = (void **)&req; 8839 u32 req_len = sizeof(*req); 8840 __le32 *num_entries; 8841 __le64 *pg_dir; 8842 u32 flags = 0; 8843 u8 *pg_attr; 8844 u32 ena; 8845 int rc; 8846 int i; 8847 8848 if (!ctx) 8849 return 0; 8850 8851 if (req_len > bp->hwrm_max_ext_req_len) 8852 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8853 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8854 if (rc) 8855 return rc; 8856 8857 req->enables = cpu_to_le32(enables); 8858 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8859 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8860 ctx_pg = ctxm->pg_info; 8861 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8862 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8863 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8864 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8865 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8866 &req->qpc_pg_size_qpc_lvl, 8867 &req->qpc_page_dir); 8868 8869 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8870 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8871 } 8872 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8873 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8874 ctx_pg = ctxm->pg_info; 8875 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8876 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8877 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8878 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8879 &req->srq_pg_size_srq_lvl, 8880 &req->srq_page_dir); 8881 } 8882 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8883 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8884 ctx_pg = ctxm->pg_info; 8885 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8886 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8887 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8888 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8889 &req->cq_pg_size_cq_lvl, 8890 &req->cq_page_dir); 8891 } 8892 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8893 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8894 ctx_pg = ctxm->pg_info; 8895 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8896 req->vnic_num_ring_table_entries = 8897 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8898 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8899 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8900 &req->vnic_pg_size_vnic_lvl, 8901 &req->vnic_page_dir); 8902 } 8903 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8904 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8905 ctx_pg = ctxm->pg_info; 8906 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8907 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8908 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8909 &req->stat_pg_size_stat_lvl, 8910 &req->stat_page_dir); 8911 } 8912 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8913 u32 units; 8914 8915 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8916 ctx_pg = ctxm->pg_info; 8917 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8918 units = ctxm->mrav_num_entries_units; 8919 if (units) { 8920 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8921 u32 entries; 8922 8923 num_mr = ctx_pg->entries - num_ah; 8924 entries = ((num_mr / units) << 16) | (num_ah / units); 8925 req->mrav_num_entries = cpu_to_le32(entries); 8926 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8927 } 8928 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8929 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8930 &req->mrav_pg_size_mrav_lvl, 8931 &req->mrav_page_dir); 8932 } 8933 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8934 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8935 ctx_pg = ctxm->pg_info; 8936 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8937 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8938 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8939 &req->tim_pg_size_tim_lvl, 8940 &req->tim_page_dir); 8941 } 8942 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8943 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8944 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8945 pg_dir = &req->tqm_sp_page_dir, 8946 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8947 ctx_pg = ctxm->pg_info; 8948 i < BNXT_MAX_TQM_RINGS; 8949 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8950 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8951 if (!(enables & ena)) 8952 continue; 8953 8954 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8955 *num_entries = cpu_to_le32(ctx_pg->entries); 8956 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8957 } 8958 req->flags = cpu_to_le32(flags); 8959 return hwrm_req_send(bp, req); 8960 } 8961 8962 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8963 struct bnxt_ctx_pg_info *ctx_pg) 8964 { 8965 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8966 8967 rmem->page_size = BNXT_PAGE_SIZE; 8968 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8969 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8970 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8971 if (rmem->depth >= 1) 8972 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8973 return bnxt_alloc_ring(bp, rmem); 8974 } 8975 8976 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8977 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8978 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8979 { 8980 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8981 int rc; 8982 8983 if (!mem_size) 8984 return -EINVAL; 8985 8986 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8987 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8988 ctx_pg->nr_pages = 0; 8989 return -EINVAL; 8990 } 8991 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8992 int nr_tbls, i; 8993 8994 rmem->depth = 2; 8995 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8996 GFP_KERNEL); 8997 if (!ctx_pg->ctx_pg_tbl) 8998 return -ENOMEM; 8999 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 9000 rmem->nr_pages = nr_tbls; 9001 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 9002 if (rc) 9003 return rc; 9004 for (i = 0; i < nr_tbls; i++) { 9005 struct bnxt_ctx_pg_info *pg_tbl; 9006 9007 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 9008 if (!pg_tbl) 9009 return -ENOMEM; 9010 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 9011 rmem = &pg_tbl->ring_mem; 9012 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 9013 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 9014 rmem->depth = 1; 9015 rmem->nr_pages = MAX_CTX_PAGES; 9016 rmem->ctx_mem = ctxm; 9017 if (i == (nr_tbls - 1)) { 9018 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 9019 9020 if (rem) 9021 rmem->nr_pages = rem; 9022 } 9023 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 9024 if (rc) 9025 break; 9026 } 9027 } else { 9028 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 9029 if (rmem->nr_pages > 1 || depth) 9030 rmem->depth = 1; 9031 rmem->ctx_mem = ctxm; 9032 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 9033 } 9034 return rc; 9035 } 9036 9037 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 9038 struct bnxt_ctx_pg_info *ctx_pg, 9039 void *buf, size_t offset, size_t head, 9040 size_t tail) 9041 { 9042 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9043 size_t nr_pages = ctx_pg->nr_pages; 9044 int page_size = rmem->page_size; 9045 size_t len = 0, total_len = 0; 9046 u16 depth = rmem->depth; 9047 9048 tail %= nr_pages * page_size; 9049 do { 9050 if (depth > 1) { 9051 int i = head / (page_size * MAX_CTX_PAGES); 9052 struct bnxt_ctx_pg_info *pg_tbl; 9053 9054 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9055 rmem = &pg_tbl->ring_mem; 9056 } 9057 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 9058 head += len; 9059 offset += len; 9060 total_len += len; 9061 if (head >= nr_pages * page_size) 9062 head = 0; 9063 } while (head != tail); 9064 return total_len; 9065 } 9066 9067 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 9068 struct bnxt_ctx_pg_info *ctx_pg) 9069 { 9070 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9071 9072 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 9073 ctx_pg->ctx_pg_tbl) { 9074 int i, nr_tbls = rmem->nr_pages; 9075 9076 for (i = 0; i < nr_tbls; i++) { 9077 struct bnxt_ctx_pg_info *pg_tbl; 9078 struct bnxt_ring_mem_info *rmem2; 9079 9080 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9081 if (!pg_tbl) 9082 continue; 9083 rmem2 = &pg_tbl->ring_mem; 9084 bnxt_free_ring(bp, rmem2); 9085 ctx_pg->ctx_pg_arr[i] = NULL; 9086 kfree(pg_tbl); 9087 ctx_pg->ctx_pg_tbl[i] = NULL; 9088 } 9089 kfree(ctx_pg->ctx_pg_tbl); 9090 ctx_pg->ctx_pg_tbl = NULL; 9091 } 9092 bnxt_free_ring(bp, rmem); 9093 ctx_pg->nr_pages = 0; 9094 } 9095 9096 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 9097 struct bnxt_ctx_mem_type *ctxm, u32 entries, 9098 u8 pg_lvl) 9099 { 9100 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9101 int i, rc = 0, n = 1; 9102 u32 mem_size; 9103 9104 if (!ctxm->entry_size || !ctx_pg) 9105 return -EINVAL; 9106 if (ctxm->instance_bmap) 9107 n = hweight32(ctxm->instance_bmap); 9108 if (ctxm->entry_multiple) 9109 entries = roundup(entries, ctxm->entry_multiple); 9110 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 9111 mem_size = entries * ctxm->entry_size; 9112 for (i = 0; i < n && !rc; i++) { 9113 ctx_pg[i].entries = entries; 9114 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 9115 ctxm->init_value ? ctxm : NULL); 9116 } 9117 if (!rc) 9118 ctxm->mem_valid = 1; 9119 return rc; 9120 } 9121 9122 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 9123 struct bnxt_ctx_mem_type *ctxm, 9124 bool last) 9125 { 9126 struct hwrm_func_backing_store_cfg_v2_input *req; 9127 u32 instance_bmap = ctxm->instance_bmap; 9128 int i, j, rc = 0, n = 1; 9129 __le32 *p; 9130 9131 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 9132 return 0; 9133 9134 if (instance_bmap) 9135 n = hweight32(ctxm->instance_bmap); 9136 else 9137 instance_bmap = 1; 9138 9139 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 9140 if (rc) 9141 return rc; 9142 hwrm_req_hold(bp, req); 9143 req->type = cpu_to_le16(ctxm->type); 9144 req->entry_size = cpu_to_le16(ctxm->entry_size); 9145 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 9146 bnxt_bs_trace_avail(bp, ctxm->type)) { 9147 struct bnxt_bs_trace_info *bs_trace; 9148 u32 enables; 9149 9150 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 9151 req->enables = cpu_to_le32(enables); 9152 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 9153 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 9154 } 9155 req->subtype_valid_cnt = ctxm->split_entry_cnt; 9156 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 9157 p[i] = cpu_to_le32(ctxm->split[i]); 9158 for (i = 0, j = 0; j < n && !rc; i++) { 9159 struct bnxt_ctx_pg_info *ctx_pg; 9160 9161 if (!(instance_bmap & (1 << i))) 9162 continue; 9163 req->instance = cpu_to_le16(i); 9164 ctx_pg = &ctxm->pg_info[j++]; 9165 if (!ctx_pg->entries) 9166 continue; 9167 req->num_entries = cpu_to_le32(ctx_pg->entries); 9168 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9169 &req->page_size_pbl_level, 9170 &req->page_dir); 9171 if (last && j == n) 9172 req->flags = 9173 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 9174 rc = hwrm_req_send(bp, req); 9175 } 9176 hwrm_req_drop(bp, req); 9177 return rc; 9178 } 9179 9180 static int bnxt_backing_store_cfg_v2(struct bnxt *bp) 9181 { 9182 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9183 struct bnxt_ctx_mem_type *ctxm; 9184 u16 last_type = BNXT_CTX_INV; 9185 int rc = 0; 9186 u16 type; 9187 9188 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) { 9189 ctxm = &ctx->ctx_arr[type]; 9190 if (!bnxt_bs_trace_avail(bp, type)) 9191 continue; 9192 if (!ctxm->mem_valid) { 9193 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 9194 ctxm->max_entries, 1); 9195 if (rc) { 9196 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9197 type); 9198 continue; 9199 } 9200 bnxt_bs_trace_init(bp, ctxm); 9201 } 9202 last_type = type; 9203 } 9204 9205 if (last_type == BNXT_CTX_INV) { 9206 for (type = 0; type < BNXT_CTX_MAX; type++) { 9207 ctxm = &ctx->ctx_arr[type]; 9208 if (ctxm->mem_valid) 9209 last_type = type; 9210 } 9211 if (last_type == BNXT_CTX_INV) 9212 return 0; 9213 } 9214 ctx->ctx_arr[last_type].last = 1; 9215 9216 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9217 ctxm = &ctx->ctx_arr[type]; 9218 9219 if (!ctxm->mem_valid) 9220 continue; 9221 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9222 if (rc) 9223 return rc; 9224 } 9225 return 0; 9226 } 9227 9228 /** 9229 * __bnxt_copy_ctx_mem - copy host context memory 9230 * @bp: The driver context 9231 * @ctxm: The pointer to the context memory type 9232 * @buf: The destination buffer or NULL to just obtain the length 9233 * @offset: The buffer offset to copy the data to 9234 * @head: The head offset of context memory to copy from 9235 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9236 * 9237 * This function is called for debugging purposes to dump the host context 9238 * used by the chip. 9239 * 9240 * Return: Length of memory copied 9241 */ 9242 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9243 struct bnxt_ctx_mem_type *ctxm, void *buf, 9244 size_t offset, size_t head, size_t tail) 9245 { 9246 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9247 size_t len = 0, total_len = 0; 9248 int i, n = 1; 9249 9250 if (!ctx_pg) 9251 return 0; 9252 9253 if (ctxm->instance_bmap) 9254 n = hweight32(ctxm->instance_bmap); 9255 for (i = 0; i < n; i++) { 9256 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9257 tail); 9258 offset += len; 9259 total_len += len; 9260 } 9261 return total_len; 9262 } 9263 9264 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9265 void *buf, size_t offset) 9266 { 9267 size_t tail = ctxm->max_entries * ctxm->entry_size; 9268 9269 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9270 } 9271 9272 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9273 struct bnxt_ctx_mem_type *ctxm, bool force) 9274 { 9275 struct bnxt_ctx_pg_info *ctx_pg; 9276 int i, n = 1; 9277 9278 ctxm->last = 0; 9279 9280 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9281 return; 9282 9283 ctx_pg = ctxm->pg_info; 9284 if (ctx_pg) { 9285 if (ctxm->instance_bmap) 9286 n = hweight32(ctxm->instance_bmap); 9287 for (i = 0; i < n; i++) 9288 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9289 9290 kfree(ctx_pg); 9291 ctxm->pg_info = NULL; 9292 ctxm->mem_valid = 0; 9293 } 9294 memset(ctxm, 0, sizeof(*ctxm)); 9295 } 9296 9297 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9298 { 9299 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9300 u16 type; 9301 9302 if (!ctx) 9303 return; 9304 9305 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9306 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9307 9308 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9309 if (force) { 9310 kfree(ctx); 9311 bp->ctx = NULL; 9312 } 9313 } 9314 9315 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9316 { 9317 struct bnxt_ctx_mem_type *ctxm; 9318 struct bnxt_ctx_mem_info *ctx; 9319 u32 l2_qps, qp1_qps, max_qps; 9320 u32 ena, entries_sp, entries; 9321 u32 srqs, max_srqs, min; 9322 u32 num_mr, num_ah; 9323 u32 extra_srqs = 0; 9324 u32 extra_qps = 0; 9325 u32 fast_qpmd_qps; 9326 u8 pg_lvl = 1; 9327 int i, rc; 9328 9329 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9330 if (rc) { 9331 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9332 rc); 9333 return rc; 9334 } 9335 ctx = bp->ctx; 9336 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9337 return 0; 9338 9339 ena = 0; 9340 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 9341 goto skip_legacy; 9342 9343 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9344 l2_qps = ctxm->qp_l2_entries; 9345 qp1_qps = ctxm->qp_qp1_entries; 9346 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9347 max_qps = ctxm->max_entries; 9348 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9349 srqs = ctxm->srq_l2_entries; 9350 max_srqs = ctxm->max_entries; 9351 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9352 pg_lvl = 2; 9353 if (BNXT_SW_RES_LMT(bp)) { 9354 extra_qps = max_qps - l2_qps - qp1_qps; 9355 extra_srqs = max_srqs - srqs; 9356 } else { 9357 extra_qps = min_t(u32, 65536, 9358 max_qps - l2_qps - qp1_qps); 9359 /* allocate extra qps if fw supports RoCE fast qp 9360 * destroy feature 9361 */ 9362 extra_qps += fast_qpmd_qps; 9363 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9364 } 9365 if (fast_qpmd_qps) 9366 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9367 } 9368 9369 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9370 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9371 pg_lvl); 9372 if (rc) 9373 return rc; 9374 9375 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9376 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9377 if (rc) 9378 return rc; 9379 9380 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9381 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9382 extra_qps * 2, pg_lvl); 9383 if (rc) 9384 return rc; 9385 9386 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9387 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9388 if (rc) 9389 return rc; 9390 9391 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9392 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9393 if (rc) 9394 return rc; 9395 9396 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9397 goto skip_rdma; 9398 9399 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9400 if (BNXT_SW_RES_LMT(bp) && 9401 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9402 num_ah = ctxm->mrav_av_entries; 9403 num_mr = ctxm->max_entries - num_ah; 9404 } else { 9405 /* 128K extra is needed to accommodate static AH context 9406 * allocation by f/w. 9407 */ 9408 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9409 num_ah = min_t(u32, num_mr, 1024 * 128); 9410 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9411 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9412 ctxm->mrav_av_entries = num_ah; 9413 } 9414 9415 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9416 if (rc) 9417 return rc; 9418 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9419 9420 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9421 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9422 if (rc) 9423 return rc; 9424 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9425 9426 skip_rdma: 9427 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9428 min = ctxm->min_entries; 9429 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9430 2 * (extra_qps + qp1_qps) + min; 9431 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9432 if (rc) 9433 return rc; 9434 9435 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9436 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9437 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9438 if (rc) 9439 return rc; 9440 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9441 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9442 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9443 9444 skip_legacy: 9445 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9446 rc = bnxt_backing_store_cfg_v2(bp); 9447 else 9448 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9449 if (rc) { 9450 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9451 rc); 9452 return rc; 9453 } 9454 ctx->flags |= BNXT_CTX_FLAG_INITED; 9455 return 0; 9456 } 9457 9458 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9459 { 9460 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9461 u16 page_attr; 9462 int rc; 9463 9464 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9465 return 0; 9466 9467 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9468 if (rc) 9469 return rc; 9470 9471 if (BNXT_PAGE_SIZE == 0x2000) 9472 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9473 else if (BNXT_PAGE_SIZE == 0x10000) 9474 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9475 else 9476 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9477 req->pg_size_lvl = cpu_to_le16(page_attr | 9478 bp->fw_crash_mem->ring_mem.depth); 9479 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9480 req->size = cpu_to_le32(bp->fw_crash_len); 9481 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9482 return hwrm_req_send(bp, req); 9483 } 9484 9485 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9486 { 9487 if (bp->fw_crash_mem) { 9488 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9489 kfree(bp->fw_crash_mem); 9490 bp->fw_crash_mem = NULL; 9491 } 9492 } 9493 9494 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9495 { 9496 u32 mem_size = 0; 9497 int rc; 9498 9499 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9500 return 0; 9501 9502 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9503 if (rc) 9504 return rc; 9505 9506 mem_size = round_up(mem_size, 4); 9507 9508 /* keep and use the existing pages */ 9509 if (bp->fw_crash_mem && 9510 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9511 goto alloc_done; 9512 9513 if (bp->fw_crash_mem) 9514 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9515 else 9516 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 9517 GFP_KERNEL); 9518 if (!bp->fw_crash_mem) 9519 return -ENOMEM; 9520 9521 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9522 if (rc) { 9523 bnxt_free_crash_dump_mem(bp); 9524 return rc; 9525 } 9526 9527 alloc_done: 9528 bp->fw_crash_len = mem_size; 9529 return 0; 9530 } 9531 9532 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9533 { 9534 struct hwrm_func_resource_qcaps_output *resp; 9535 struct hwrm_func_resource_qcaps_input *req; 9536 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9537 int rc; 9538 9539 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9540 if (rc) 9541 return rc; 9542 9543 req->fid = cpu_to_le16(0xffff); 9544 resp = hwrm_req_hold(bp, req); 9545 rc = hwrm_req_send_silent(bp, req); 9546 if (rc) 9547 goto hwrm_func_resc_qcaps_exit; 9548 9549 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9550 if (!all) 9551 goto hwrm_func_resc_qcaps_exit; 9552 9553 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9554 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9555 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9556 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9557 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9558 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9559 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9560 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9561 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9562 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9563 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9564 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9565 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9566 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9567 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9568 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9569 9570 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9571 u16 max_msix = le16_to_cpu(resp->max_msix); 9572 9573 hw_resc->max_nqs = max_msix; 9574 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9575 } 9576 9577 if (BNXT_PF(bp)) { 9578 struct bnxt_pf_info *pf = &bp->pf; 9579 9580 pf->vf_resv_strategy = 9581 le16_to_cpu(resp->vf_reservation_strategy); 9582 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9583 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9584 } 9585 hwrm_func_resc_qcaps_exit: 9586 hwrm_req_drop(bp, req); 9587 return rc; 9588 } 9589 9590 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9591 { 9592 struct hwrm_port_mac_ptp_qcfg_output *resp; 9593 struct hwrm_port_mac_ptp_qcfg_input *req; 9594 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9595 u8 flags; 9596 int rc; 9597 9598 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9599 rc = -ENODEV; 9600 goto no_ptp; 9601 } 9602 9603 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9604 if (rc) 9605 goto no_ptp; 9606 9607 req->port_id = cpu_to_le16(bp->pf.port_id); 9608 resp = hwrm_req_hold(bp, req); 9609 rc = hwrm_req_send(bp, req); 9610 if (rc) 9611 goto exit; 9612 9613 flags = resp->flags; 9614 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9615 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9616 rc = -ENODEV; 9617 goto exit; 9618 } 9619 if (!ptp) { 9620 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9621 if (!ptp) { 9622 rc = -ENOMEM; 9623 goto exit; 9624 } 9625 ptp->bp = bp; 9626 bp->ptp_cfg = ptp; 9627 } 9628 9629 if (flags & 9630 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9631 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9632 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9633 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9634 } else if (BNXT_CHIP_P5(bp)) { 9635 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9636 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9637 } else { 9638 rc = -ENODEV; 9639 goto exit; 9640 } 9641 ptp->rtc_configured = 9642 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9643 rc = bnxt_ptp_init(bp); 9644 if (rc) 9645 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9646 exit: 9647 hwrm_req_drop(bp, req); 9648 if (!rc) 9649 return 0; 9650 9651 no_ptp: 9652 bnxt_ptp_clear(bp); 9653 kfree(ptp); 9654 bp->ptp_cfg = NULL; 9655 return rc; 9656 } 9657 9658 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9659 { 9660 u32 flags, flags_ext, flags_ext2, flags_ext3; 9661 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9662 struct hwrm_func_qcaps_output *resp; 9663 struct hwrm_func_qcaps_input *req; 9664 int rc; 9665 9666 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9667 if (rc) 9668 return rc; 9669 9670 req->fid = cpu_to_le16(0xffff); 9671 resp = hwrm_req_hold(bp, req); 9672 rc = hwrm_req_send(bp, req); 9673 if (rc) 9674 goto hwrm_func_qcaps_exit; 9675 9676 flags = le32_to_cpu(resp->flags); 9677 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9678 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9679 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9680 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9681 if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED) 9682 bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN; 9683 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9684 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9685 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9686 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9687 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9688 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9689 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9690 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9691 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9692 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9693 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9694 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9695 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9696 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9697 9698 flags_ext = le32_to_cpu(resp->flags_ext); 9699 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9700 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9701 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9702 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9703 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9704 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9705 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9706 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9707 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9708 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9709 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED) 9710 bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2; 9711 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9712 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9713 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9714 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9715 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9716 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9717 9718 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9719 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9720 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9721 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9722 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9723 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9724 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9725 if (flags_ext2 & 9726 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9727 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9728 if (BNXT_PF(bp) && 9729 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9730 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9731 9732 flags_ext3 = le32_to_cpu(resp->flags_ext3); 9733 if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT) 9734 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT; 9735 if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED) 9736 bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE; 9737 9738 bp->tx_push_thresh = 0; 9739 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9740 BNXT_FW_MAJ(bp) > 217) 9741 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9742 9743 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9744 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9745 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9746 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9747 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9748 if (!hw_resc->max_hw_ring_grps) 9749 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9750 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9751 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9752 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9753 9754 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9755 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9756 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9757 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9758 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9759 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9760 9761 if (BNXT_PF(bp)) { 9762 struct bnxt_pf_info *pf = &bp->pf; 9763 9764 pf->fw_fid = le16_to_cpu(resp->fid); 9765 pf->port_id = le16_to_cpu(resp->port_id); 9766 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9767 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9768 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9769 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9770 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9771 bp->flags |= BNXT_FLAG_WOL_CAP; 9772 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9773 bp->fw_cap |= BNXT_FW_CAP_PTP; 9774 } else { 9775 bnxt_ptp_clear(bp); 9776 kfree(bp->ptp_cfg); 9777 bp->ptp_cfg = NULL; 9778 } 9779 } else { 9780 #ifdef CONFIG_BNXT_SRIOV 9781 struct bnxt_vf_info *vf = &bp->vf; 9782 9783 vf->fw_fid = le16_to_cpu(resp->fid); 9784 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9785 #endif 9786 } 9787 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9788 9789 hwrm_func_qcaps_exit: 9790 hwrm_req_drop(bp, req); 9791 return rc; 9792 } 9793 9794 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9795 { 9796 struct hwrm_dbg_qcaps_output *resp; 9797 struct hwrm_dbg_qcaps_input *req; 9798 int rc; 9799 9800 bp->fw_dbg_cap = 0; 9801 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9802 return; 9803 9804 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9805 if (rc) 9806 return; 9807 9808 req->fid = cpu_to_le16(0xffff); 9809 resp = hwrm_req_hold(bp, req); 9810 rc = hwrm_req_send(bp, req); 9811 if (rc) 9812 goto hwrm_dbg_qcaps_exit; 9813 9814 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9815 9816 hwrm_dbg_qcaps_exit: 9817 hwrm_req_drop(bp, req); 9818 } 9819 9820 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9821 9822 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9823 { 9824 int rc; 9825 9826 rc = __bnxt_hwrm_func_qcaps(bp); 9827 if (rc) 9828 return rc; 9829 9830 bnxt_hwrm_dbg_qcaps(bp); 9831 9832 rc = bnxt_hwrm_queue_qportcfg(bp); 9833 if (rc) { 9834 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9835 return rc; 9836 } 9837 if (bp->hwrm_spec_code >= 0x10803) { 9838 rc = bnxt_alloc_ctx_mem(bp); 9839 if (rc) 9840 return rc; 9841 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9842 if (!rc) 9843 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9844 } 9845 return 0; 9846 } 9847 9848 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9849 { 9850 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9851 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9852 u32 flags; 9853 int rc; 9854 9855 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9856 return 0; 9857 9858 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9859 if (rc) 9860 return rc; 9861 9862 resp = hwrm_req_hold(bp, req); 9863 rc = hwrm_req_send(bp, req); 9864 if (rc) 9865 goto hwrm_cfa_adv_qcaps_exit; 9866 9867 flags = le32_to_cpu(resp->flags); 9868 if (flags & 9869 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9870 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9871 9872 if (flags & 9873 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9874 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9875 9876 if (flags & 9877 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9878 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9879 9880 hwrm_cfa_adv_qcaps_exit: 9881 hwrm_req_drop(bp, req); 9882 return rc; 9883 } 9884 9885 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9886 { 9887 if (bp->fw_health) 9888 return 0; 9889 9890 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9891 if (!bp->fw_health) 9892 return -ENOMEM; 9893 9894 mutex_init(&bp->fw_health->lock); 9895 return 0; 9896 } 9897 9898 static int bnxt_alloc_fw_health(struct bnxt *bp) 9899 { 9900 int rc; 9901 9902 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9903 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9904 return 0; 9905 9906 rc = __bnxt_alloc_fw_health(bp); 9907 if (rc) { 9908 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9909 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9910 return rc; 9911 } 9912 9913 return 0; 9914 } 9915 9916 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9917 { 9918 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9919 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9920 BNXT_FW_HEALTH_WIN_MAP_OFF); 9921 } 9922 9923 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9924 { 9925 struct bnxt_fw_health *fw_health = bp->fw_health; 9926 u32 reg_type; 9927 9928 if (!fw_health) 9929 return; 9930 9931 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9932 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9933 fw_health->status_reliable = false; 9934 9935 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9936 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9937 fw_health->resets_reliable = false; 9938 } 9939 9940 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9941 { 9942 void __iomem *hs; 9943 u32 status_loc; 9944 u32 reg_type; 9945 u32 sig; 9946 9947 if (bp->fw_health) 9948 bp->fw_health->status_reliable = false; 9949 9950 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9951 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9952 9953 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9954 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9955 if (!bp->chip_num) { 9956 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9957 bp->chip_num = readl(bp->bar0 + 9958 BNXT_FW_HEALTH_WIN_BASE + 9959 BNXT_GRC_REG_CHIP_NUM); 9960 } 9961 if (!BNXT_CHIP_P5_PLUS(bp)) 9962 return; 9963 9964 status_loc = BNXT_GRC_REG_STATUS_P5 | 9965 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9966 } else { 9967 status_loc = readl(hs + offsetof(struct hcomm_status, 9968 fw_status_loc)); 9969 } 9970 9971 if (__bnxt_alloc_fw_health(bp)) { 9972 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9973 return; 9974 } 9975 9976 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9977 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9978 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9979 __bnxt_map_fw_health_reg(bp, status_loc); 9980 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9981 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9982 } 9983 9984 bp->fw_health->status_reliable = true; 9985 } 9986 9987 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9988 { 9989 struct bnxt_fw_health *fw_health = bp->fw_health; 9990 u32 reg_base = 0xffffffff; 9991 int i; 9992 9993 bp->fw_health->status_reliable = false; 9994 bp->fw_health->resets_reliable = false; 9995 /* Only pre-map the monitoring GRC registers using window 3 */ 9996 for (i = 0; i < 4; i++) { 9997 u32 reg = fw_health->regs[i]; 9998 9999 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 10000 continue; 10001 if (reg_base == 0xffffffff) 10002 reg_base = reg & BNXT_GRC_BASE_MASK; 10003 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 10004 return -ERANGE; 10005 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 10006 } 10007 bp->fw_health->status_reliable = true; 10008 bp->fw_health->resets_reliable = true; 10009 if (reg_base == 0xffffffff) 10010 return 0; 10011 10012 __bnxt_map_fw_health_reg(bp, reg_base); 10013 return 0; 10014 } 10015 10016 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 10017 { 10018 if (!bp->fw_health) 10019 return; 10020 10021 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 10022 bp->fw_health->status_reliable = true; 10023 bp->fw_health->resets_reliable = true; 10024 } else { 10025 bnxt_try_map_fw_health_reg(bp); 10026 } 10027 } 10028 10029 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 10030 { 10031 struct bnxt_fw_health *fw_health = bp->fw_health; 10032 struct hwrm_error_recovery_qcfg_output *resp; 10033 struct hwrm_error_recovery_qcfg_input *req; 10034 int rc, i; 10035 10036 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 10037 return 0; 10038 10039 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 10040 if (rc) 10041 return rc; 10042 10043 resp = hwrm_req_hold(bp, req); 10044 rc = hwrm_req_send(bp, req); 10045 if (rc) 10046 goto err_recovery_out; 10047 fw_health->flags = le32_to_cpu(resp->flags); 10048 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 10049 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 10050 rc = -EINVAL; 10051 goto err_recovery_out; 10052 } 10053 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 10054 fw_health->master_func_wait_dsecs = 10055 le32_to_cpu(resp->master_func_wait_period); 10056 fw_health->normal_func_wait_dsecs = 10057 le32_to_cpu(resp->normal_func_wait_period); 10058 fw_health->post_reset_wait_dsecs = 10059 le32_to_cpu(resp->master_func_wait_period_after_reset); 10060 fw_health->post_reset_max_wait_dsecs = 10061 le32_to_cpu(resp->max_bailout_time_after_reset); 10062 fw_health->regs[BNXT_FW_HEALTH_REG] = 10063 le32_to_cpu(resp->fw_health_status_reg); 10064 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 10065 le32_to_cpu(resp->fw_heartbeat_reg); 10066 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 10067 le32_to_cpu(resp->fw_reset_cnt_reg); 10068 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 10069 le32_to_cpu(resp->reset_inprogress_reg); 10070 fw_health->fw_reset_inprog_reg_mask = 10071 le32_to_cpu(resp->reset_inprogress_reg_mask); 10072 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 10073 if (fw_health->fw_reset_seq_cnt >= 16) { 10074 rc = -EINVAL; 10075 goto err_recovery_out; 10076 } 10077 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 10078 fw_health->fw_reset_seq_regs[i] = 10079 le32_to_cpu(resp->reset_reg[i]); 10080 fw_health->fw_reset_seq_vals[i] = 10081 le32_to_cpu(resp->reset_reg_val[i]); 10082 fw_health->fw_reset_seq_delay_msec[i] = 10083 resp->delay_after_reset[i]; 10084 } 10085 err_recovery_out: 10086 hwrm_req_drop(bp, req); 10087 if (!rc) 10088 rc = bnxt_map_fw_health_regs(bp); 10089 if (rc) 10090 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10091 return rc; 10092 } 10093 10094 static int bnxt_hwrm_func_reset(struct bnxt *bp) 10095 { 10096 struct hwrm_func_reset_input *req; 10097 int rc; 10098 10099 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 10100 if (rc) 10101 return rc; 10102 10103 req->enables = 0; 10104 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 10105 return hwrm_req_send(bp, req); 10106 } 10107 10108 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 10109 { 10110 struct hwrm_nvm_get_dev_info_output nvm_info; 10111 10112 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 10113 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 10114 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 10115 nvm_info.nvm_cfg_ver_upd); 10116 } 10117 10118 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 10119 { 10120 struct hwrm_queue_qportcfg_output *resp; 10121 struct hwrm_queue_qportcfg_input *req; 10122 u8 i, j, *qptr; 10123 bool no_rdma; 10124 int rc = 0; 10125 10126 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 10127 if (rc) 10128 return rc; 10129 10130 resp = hwrm_req_hold(bp, req); 10131 rc = hwrm_req_send(bp, req); 10132 if (rc) 10133 goto qportcfg_exit; 10134 10135 if (!resp->max_configurable_queues) { 10136 rc = -EINVAL; 10137 goto qportcfg_exit; 10138 } 10139 bp->max_tc = resp->max_configurable_queues; 10140 bp->max_lltc = resp->max_configurable_lossless_queues; 10141 if (bp->max_tc > BNXT_MAX_QUEUE) 10142 bp->max_tc = BNXT_MAX_QUEUE; 10143 10144 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 10145 qptr = &resp->queue_id0; 10146 for (i = 0, j = 0; i < bp->max_tc; i++) { 10147 bp->q_info[j].queue_id = *qptr; 10148 bp->q_ids[i] = *qptr++; 10149 bp->q_info[j].queue_profile = *qptr++; 10150 bp->tc_to_qidx[j] = j; 10151 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 10152 (no_rdma && BNXT_PF(bp))) 10153 j++; 10154 } 10155 bp->max_q = bp->max_tc; 10156 bp->max_tc = max_t(u8, j, 1); 10157 10158 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 10159 bp->max_tc = 1; 10160 10161 if (bp->max_lltc > bp->max_tc) 10162 bp->max_lltc = bp->max_tc; 10163 10164 qportcfg_exit: 10165 hwrm_req_drop(bp, req); 10166 return rc; 10167 } 10168 10169 static int bnxt_hwrm_poll(struct bnxt *bp) 10170 { 10171 struct hwrm_ver_get_input *req; 10172 int rc; 10173 10174 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10175 if (rc) 10176 return rc; 10177 10178 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10179 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10180 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10181 10182 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 10183 rc = hwrm_req_send(bp, req); 10184 return rc; 10185 } 10186 10187 static int bnxt_hwrm_ver_get(struct bnxt *bp) 10188 { 10189 struct hwrm_ver_get_output *resp; 10190 struct hwrm_ver_get_input *req; 10191 u16 fw_maj, fw_min, fw_bld, fw_rsv; 10192 u32 dev_caps_cfg, hwrm_ver; 10193 int rc, len, max_tmo_secs; 10194 10195 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10196 if (rc) 10197 return rc; 10198 10199 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 10200 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 10201 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10202 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10203 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10204 10205 resp = hwrm_req_hold(bp, req); 10206 rc = hwrm_req_send(bp, req); 10207 if (rc) 10208 goto hwrm_ver_get_exit; 10209 10210 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 10211 10212 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10213 resp->hwrm_intf_min_8b << 8 | 10214 resp->hwrm_intf_upd_8b; 10215 if (resp->hwrm_intf_maj_8b < 1) { 10216 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10217 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10218 resp->hwrm_intf_upd_8b); 10219 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10220 } 10221 10222 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10223 HWRM_VERSION_UPDATE; 10224 10225 if (bp->hwrm_spec_code > hwrm_ver) 10226 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10227 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10228 HWRM_VERSION_UPDATE); 10229 else 10230 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10231 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10232 resp->hwrm_intf_upd_8b); 10233 10234 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10235 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10236 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10237 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10238 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10239 len = FW_VER_STR_LEN; 10240 } else { 10241 fw_maj = resp->hwrm_fw_maj_8b; 10242 fw_min = resp->hwrm_fw_min_8b; 10243 fw_bld = resp->hwrm_fw_bld_8b; 10244 fw_rsv = resp->hwrm_fw_rsvd_8b; 10245 len = BC_HWRM_STR_LEN; 10246 } 10247 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10248 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10249 fw_rsv); 10250 10251 if (strlen(resp->active_pkg_name)) { 10252 int fw_ver_len = strlen(bp->fw_ver_str); 10253 10254 snprintf(bp->fw_ver_str + fw_ver_len, 10255 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10256 resp->active_pkg_name); 10257 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10258 } 10259 10260 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10261 if (!bp->hwrm_cmd_timeout) 10262 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10263 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10264 if (!bp->hwrm_cmd_max_timeout) 10265 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10266 max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000; 10267 #ifdef CONFIG_DETECT_HUNG_TASK 10268 if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT || 10269 max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) { 10270 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n", 10271 max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT); 10272 } 10273 #endif 10274 10275 if (resp->hwrm_intf_maj_8b >= 1) { 10276 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10277 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10278 } 10279 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10280 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10281 10282 bp->chip_num = le16_to_cpu(resp->chip_num); 10283 bp->chip_rev = resp->chip_rev; 10284 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10285 !resp->chip_metal) 10286 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10287 10288 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10289 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10290 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10291 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10292 10293 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10294 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10295 10296 if (dev_caps_cfg & 10297 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10298 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10299 10300 if (dev_caps_cfg & 10301 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10302 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10303 10304 if (dev_caps_cfg & 10305 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10306 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10307 10308 hwrm_ver_get_exit: 10309 hwrm_req_drop(bp, req); 10310 return rc; 10311 } 10312 10313 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10314 { 10315 struct hwrm_fw_set_time_input *req; 10316 struct tm tm; 10317 time64_t now = ktime_get_real_seconds(); 10318 int rc; 10319 10320 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10321 bp->hwrm_spec_code < 0x10400) 10322 return -EOPNOTSUPP; 10323 10324 time64_to_tm(now, 0, &tm); 10325 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10326 if (rc) 10327 return rc; 10328 10329 req->year = cpu_to_le16(1900 + tm.tm_year); 10330 req->month = 1 + tm.tm_mon; 10331 req->day = tm.tm_mday; 10332 req->hour = tm.tm_hour; 10333 req->minute = tm.tm_min; 10334 req->second = tm.tm_sec; 10335 return hwrm_req_send(bp, req); 10336 } 10337 10338 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10339 { 10340 u64 sw_tmp; 10341 10342 hw &= mask; 10343 sw_tmp = (*sw & ~mask) | hw; 10344 if (hw < (*sw & mask)) 10345 sw_tmp += mask + 1; 10346 WRITE_ONCE(*sw, sw_tmp); 10347 } 10348 10349 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10350 int count, bool ignore_zero) 10351 { 10352 int i; 10353 10354 for (i = 0; i < count; i++) { 10355 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10356 10357 if (ignore_zero && !hw) 10358 continue; 10359 10360 if (masks[i] == -1ULL) 10361 sw_stats[i] = hw; 10362 else 10363 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10364 } 10365 } 10366 10367 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10368 { 10369 if (!stats->hw_stats) 10370 return; 10371 10372 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10373 stats->hw_masks, stats->len / 8, false); 10374 } 10375 10376 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10377 { 10378 struct bnxt_stats_mem *ring0_stats; 10379 bool ignore_zero = false; 10380 int i; 10381 10382 /* Chip bug. Counter intermittently becomes 0. */ 10383 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10384 ignore_zero = true; 10385 10386 for (i = 0; i < bp->cp_nr_rings; i++) { 10387 struct bnxt_napi *bnapi = bp->bnapi[i]; 10388 struct bnxt_cp_ring_info *cpr; 10389 struct bnxt_stats_mem *stats; 10390 10391 cpr = &bnapi->cp_ring; 10392 stats = &cpr->stats; 10393 if (!i) 10394 ring0_stats = stats; 10395 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10396 ring0_stats->hw_masks, 10397 ring0_stats->len / 8, ignore_zero); 10398 } 10399 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10400 struct bnxt_stats_mem *stats = &bp->port_stats; 10401 __le64 *hw_stats = stats->hw_stats; 10402 u64 *sw_stats = stats->sw_stats; 10403 u64 *masks = stats->hw_masks; 10404 int cnt; 10405 10406 cnt = sizeof(struct rx_port_stats) / 8; 10407 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10408 10409 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10410 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10411 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10412 cnt = sizeof(struct tx_port_stats) / 8; 10413 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10414 } 10415 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10416 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10417 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10418 } 10419 } 10420 10421 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10422 { 10423 struct hwrm_port_qstats_input *req; 10424 struct bnxt_pf_info *pf = &bp->pf; 10425 int rc; 10426 10427 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10428 return 0; 10429 10430 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10431 return -EOPNOTSUPP; 10432 10433 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10434 if (rc) 10435 return rc; 10436 10437 req->flags = flags; 10438 req->port_id = cpu_to_le16(pf->port_id); 10439 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10440 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10441 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10442 return hwrm_req_send(bp, req); 10443 } 10444 10445 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10446 { 10447 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10448 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10449 struct hwrm_port_qstats_ext_output *resp_qs; 10450 struct hwrm_port_qstats_ext_input *req_qs; 10451 struct bnxt_pf_info *pf = &bp->pf; 10452 u32 tx_stat_size; 10453 int rc; 10454 10455 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10456 return 0; 10457 10458 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10459 return -EOPNOTSUPP; 10460 10461 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10462 if (rc) 10463 return rc; 10464 10465 req_qs->flags = flags; 10466 req_qs->port_id = cpu_to_le16(pf->port_id); 10467 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10468 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10469 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10470 sizeof(struct tx_port_stats_ext) : 0; 10471 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10472 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10473 resp_qs = hwrm_req_hold(bp, req_qs); 10474 rc = hwrm_req_send(bp, req_qs); 10475 if (!rc) { 10476 bp->fw_rx_stats_ext_size = 10477 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10478 if (BNXT_FW_MAJ(bp) < 220 && 10479 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10480 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10481 10482 bp->fw_tx_stats_ext_size = tx_stat_size ? 10483 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10484 } else { 10485 bp->fw_rx_stats_ext_size = 0; 10486 bp->fw_tx_stats_ext_size = 0; 10487 } 10488 hwrm_req_drop(bp, req_qs); 10489 10490 if (flags) 10491 return rc; 10492 10493 if (bp->fw_tx_stats_ext_size <= 10494 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10495 bp->pri2cos_valid = 0; 10496 return rc; 10497 } 10498 10499 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10500 if (rc) 10501 return rc; 10502 10503 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10504 10505 resp_qc = hwrm_req_hold(bp, req_qc); 10506 rc = hwrm_req_send(bp, req_qc); 10507 if (!rc) { 10508 u8 *pri2cos; 10509 int i, j; 10510 10511 pri2cos = &resp_qc->pri0_cos_queue_id; 10512 for (i = 0; i < 8; i++) { 10513 u8 queue_id = pri2cos[i]; 10514 u8 queue_idx; 10515 10516 /* Per port queue IDs start from 0, 10, 20, etc */ 10517 queue_idx = queue_id % 10; 10518 if (queue_idx > BNXT_MAX_QUEUE) { 10519 bp->pri2cos_valid = false; 10520 hwrm_req_drop(bp, req_qc); 10521 return rc; 10522 } 10523 for (j = 0; j < bp->max_q; j++) { 10524 if (bp->q_ids[j] == queue_id) 10525 bp->pri2cos_idx[i] = queue_idx; 10526 } 10527 } 10528 bp->pri2cos_valid = true; 10529 } 10530 hwrm_req_drop(bp, req_qc); 10531 10532 return rc; 10533 } 10534 10535 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10536 { 10537 bnxt_hwrm_tunnel_dst_port_free(bp, 10538 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10539 bnxt_hwrm_tunnel_dst_port_free(bp, 10540 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10541 } 10542 10543 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10544 { 10545 int rc, i; 10546 u32 tpa_flags = 0; 10547 10548 if (set_tpa) 10549 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10550 else if (BNXT_NO_FW_ACCESS(bp)) 10551 return 0; 10552 for (i = 0; i < bp->nr_vnics; i++) { 10553 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10554 if (rc) { 10555 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10556 i, rc); 10557 return rc; 10558 } 10559 } 10560 return 0; 10561 } 10562 10563 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10564 { 10565 int i; 10566 10567 for (i = 0; i < bp->nr_vnics; i++) 10568 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10569 } 10570 10571 static void bnxt_clear_vnic(struct bnxt *bp) 10572 { 10573 if (!bp->vnic_info) 10574 return; 10575 10576 bnxt_hwrm_clear_vnic_filter(bp); 10577 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10578 /* clear all RSS setting before free vnic ctx */ 10579 bnxt_hwrm_clear_vnic_rss(bp); 10580 bnxt_hwrm_vnic_ctx_free(bp); 10581 } 10582 /* before free the vnic, undo the vnic tpa settings */ 10583 if (bp->flags & BNXT_FLAG_TPA) 10584 bnxt_set_tpa(bp, false); 10585 bnxt_hwrm_vnic_free(bp); 10586 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10587 bnxt_hwrm_vnic_ctx_free(bp); 10588 } 10589 10590 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10591 bool irq_re_init) 10592 { 10593 bnxt_clear_vnic(bp); 10594 bnxt_hwrm_ring_free(bp, close_path); 10595 bnxt_hwrm_ring_grp_free(bp); 10596 if (irq_re_init) { 10597 bnxt_hwrm_stat_ctx_free(bp); 10598 bnxt_hwrm_free_tunnel_ports(bp); 10599 } 10600 } 10601 10602 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10603 { 10604 struct hwrm_func_cfg_input *req; 10605 u8 evb_mode; 10606 int rc; 10607 10608 if (br_mode == BRIDGE_MODE_VEB) 10609 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10610 else if (br_mode == BRIDGE_MODE_VEPA) 10611 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10612 else 10613 return -EINVAL; 10614 10615 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10616 if (rc) 10617 return rc; 10618 10619 req->fid = cpu_to_le16(0xffff); 10620 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10621 req->evb_mode = evb_mode; 10622 return hwrm_req_send(bp, req); 10623 } 10624 10625 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10626 { 10627 struct hwrm_func_cfg_input *req; 10628 int rc; 10629 10630 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10631 return 0; 10632 10633 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10634 if (rc) 10635 return rc; 10636 10637 req->fid = cpu_to_le16(0xffff); 10638 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10639 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10640 if (size == 128) 10641 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10642 10643 return hwrm_req_send(bp, req); 10644 } 10645 10646 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10647 { 10648 int rc; 10649 10650 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10651 goto skip_rss_ctx; 10652 10653 /* allocate context for vnic */ 10654 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10655 if (rc) { 10656 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10657 vnic->vnic_id, rc); 10658 goto vnic_setup_err; 10659 } 10660 bp->rsscos_nr_ctxs++; 10661 10662 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10663 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10664 if (rc) { 10665 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10666 vnic->vnic_id, rc); 10667 goto vnic_setup_err; 10668 } 10669 bp->rsscos_nr_ctxs++; 10670 } 10671 10672 skip_rss_ctx: 10673 /* configure default vnic, ring grp */ 10674 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10675 if (rc) { 10676 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10677 vnic->vnic_id, rc); 10678 goto vnic_setup_err; 10679 } 10680 10681 /* Enable RSS hashing on vnic */ 10682 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10683 if (rc) { 10684 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10685 vnic->vnic_id, rc); 10686 goto vnic_setup_err; 10687 } 10688 10689 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10690 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10691 if (rc) { 10692 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10693 vnic->vnic_id, rc); 10694 } 10695 } 10696 10697 vnic_setup_err: 10698 return rc; 10699 } 10700 10701 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10702 u8 valid) 10703 { 10704 struct hwrm_vnic_update_input *req; 10705 int rc; 10706 10707 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10708 if (rc) 10709 return rc; 10710 10711 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10712 10713 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10714 req->mru = cpu_to_le16(vnic->mru); 10715 10716 req->enables = cpu_to_le32(valid); 10717 10718 return hwrm_req_send(bp, req); 10719 } 10720 10721 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10722 { 10723 int rc; 10724 10725 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10726 if (rc) { 10727 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10728 vnic->vnic_id, rc); 10729 return rc; 10730 } 10731 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10732 if (rc) 10733 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10734 vnic->vnic_id, rc); 10735 return rc; 10736 } 10737 10738 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10739 { 10740 int rc, i, nr_ctxs; 10741 10742 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10743 for (i = 0; i < nr_ctxs; i++) { 10744 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10745 if (rc) { 10746 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10747 vnic->vnic_id, i, rc); 10748 break; 10749 } 10750 bp->rsscos_nr_ctxs++; 10751 } 10752 if (i < nr_ctxs) 10753 return -ENOMEM; 10754 10755 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10756 if (rc) 10757 return rc; 10758 10759 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10760 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10761 if (rc) { 10762 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10763 vnic->vnic_id, rc); 10764 } 10765 } 10766 return rc; 10767 } 10768 10769 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10770 { 10771 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10772 return __bnxt_setup_vnic_p5(bp, vnic); 10773 else 10774 return __bnxt_setup_vnic(bp, vnic); 10775 } 10776 10777 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10778 struct bnxt_vnic_info *vnic, 10779 u16 start_rx_ring_idx, int rx_rings) 10780 { 10781 int rc; 10782 10783 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10784 if (rc) { 10785 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10786 vnic->vnic_id, rc); 10787 return rc; 10788 } 10789 return bnxt_setup_vnic(bp, vnic); 10790 } 10791 10792 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10793 { 10794 struct bnxt_vnic_info *vnic; 10795 int i, rc = 0; 10796 10797 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10798 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10799 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10800 } 10801 10802 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10803 return 0; 10804 10805 for (i = 0; i < bp->rx_nr_rings; i++) { 10806 u16 vnic_id = i + 1; 10807 u16 ring_id = i; 10808 10809 if (vnic_id >= bp->nr_vnics) 10810 break; 10811 10812 vnic = &bp->vnic_info[vnic_id]; 10813 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10814 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10815 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10816 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10817 break; 10818 } 10819 return rc; 10820 } 10821 10822 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10823 bool all) 10824 { 10825 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10826 struct bnxt_filter_base *usr_fltr, *tmp; 10827 struct bnxt_ntuple_filter *ntp_fltr; 10828 int i; 10829 10830 if (netif_running(bp->dev)) { 10831 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10832 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10833 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10834 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10835 } 10836 } 10837 if (!all) 10838 return; 10839 10840 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10841 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10842 usr_fltr->fw_vnic_id == rss_ctx->index) { 10843 ntp_fltr = container_of(usr_fltr, 10844 struct bnxt_ntuple_filter, 10845 base); 10846 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10847 bnxt_del_ntp_filter(bp, ntp_fltr); 10848 bnxt_del_one_usr_fltr(bp, usr_fltr); 10849 } 10850 } 10851 10852 if (vnic->rss_table) 10853 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10854 vnic->rss_table, 10855 vnic->rss_table_dma_addr); 10856 bp->num_rss_ctx--; 10857 } 10858 10859 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10860 int rxr_id) 10861 { 10862 u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 10863 int i, vnic_rx; 10864 10865 /* Ntuple VNIC always has all the rx rings. Any change of ring id 10866 * must be updated because a future filter may use it. 10867 */ 10868 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 10869 return true; 10870 10871 for (i = 0; i < tbl_size; i++) { 10872 if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 10873 vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 10874 else 10875 vnic_rx = bp->rss_indir_tbl[i]; 10876 10877 if (rxr_id == vnic_rx) 10878 return true; 10879 } 10880 10881 return false; 10882 } 10883 10884 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10885 u16 mru, int rxr_id) 10886 { 10887 int rc; 10888 10889 if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id)) 10890 return 0; 10891 10892 if (mru) { 10893 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10894 if (rc) { 10895 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10896 vnic->vnic_id, rc); 10897 return rc; 10898 } 10899 } 10900 vnic->mru = mru; 10901 bnxt_hwrm_vnic_update(bp, vnic, 10902 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 10903 10904 return 0; 10905 } 10906 10907 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id) 10908 { 10909 struct ethtool_rxfh_context *ctx; 10910 unsigned long context; 10911 int rc; 10912 10913 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10914 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10915 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10916 10917 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id); 10918 if (rc) 10919 return rc; 10920 } 10921 10922 return 0; 10923 } 10924 10925 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10926 { 10927 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10928 struct ethtool_rxfh_context *ctx; 10929 unsigned long context; 10930 10931 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10932 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10933 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10934 10935 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10936 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10937 __bnxt_setup_vnic_p5(bp, vnic)) { 10938 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10939 rss_ctx->index); 10940 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10941 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10942 } 10943 } 10944 } 10945 10946 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10947 { 10948 struct ethtool_rxfh_context *ctx; 10949 unsigned long context; 10950 10951 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10952 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10953 10954 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10955 } 10956 } 10957 10958 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10959 static bool bnxt_promisc_ok(struct bnxt *bp) 10960 { 10961 #ifdef CONFIG_BNXT_SRIOV 10962 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10963 return false; 10964 #endif 10965 return true; 10966 } 10967 10968 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10969 { 10970 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10971 unsigned int rc = 0; 10972 10973 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10974 if (rc) { 10975 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10976 rc); 10977 return rc; 10978 } 10979 10980 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10981 if (rc) { 10982 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10983 rc); 10984 return rc; 10985 } 10986 return rc; 10987 } 10988 10989 static int bnxt_cfg_rx_mode(struct bnxt *); 10990 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10991 10992 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10993 { 10994 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10995 int rc = 0; 10996 unsigned int rx_nr_rings = bp->rx_nr_rings; 10997 10998 if (irq_re_init) { 10999 rc = bnxt_hwrm_stat_ctx_alloc(bp); 11000 if (rc) { 11001 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 11002 rc); 11003 goto err_out; 11004 } 11005 } 11006 11007 rc = bnxt_hwrm_ring_alloc(bp); 11008 if (rc) { 11009 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 11010 goto err_out; 11011 } 11012 11013 rc = bnxt_hwrm_ring_grp_alloc(bp); 11014 if (rc) { 11015 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 11016 goto err_out; 11017 } 11018 11019 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11020 rx_nr_rings--; 11021 11022 /* default vnic 0 */ 11023 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 11024 if (rc) { 11025 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 11026 goto err_out; 11027 } 11028 11029 if (BNXT_VF(bp)) 11030 bnxt_hwrm_func_qcfg(bp); 11031 11032 rc = bnxt_setup_vnic(bp, vnic); 11033 if (rc) 11034 goto err_out; 11035 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 11036 bnxt_hwrm_update_rss_hash_cfg(bp); 11037 11038 if (bp->flags & BNXT_FLAG_RFS) { 11039 rc = bnxt_alloc_rfs_vnics(bp); 11040 if (rc) 11041 goto err_out; 11042 } 11043 11044 if (bp->flags & BNXT_FLAG_TPA) { 11045 rc = bnxt_set_tpa(bp, true); 11046 if (rc) 11047 goto err_out; 11048 } 11049 11050 if (BNXT_VF(bp)) 11051 bnxt_update_vf_mac(bp); 11052 11053 /* Filter for default vnic 0 */ 11054 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 11055 if (rc) { 11056 if (BNXT_VF(bp) && rc == -ENODEV) 11057 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 11058 else 11059 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 11060 goto err_out; 11061 } 11062 vnic->uc_filter_count = 1; 11063 11064 vnic->rx_mask = 0; 11065 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 11066 goto skip_rx_mask; 11067 11068 if (bp->dev->flags & IFF_BROADCAST) 11069 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 11070 11071 if (bp->dev->flags & IFF_PROMISC) 11072 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 11073 11074 if (bp->dev->flags & IFF_ALLMULTI) { 11075 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 11076 vnic->mc_list_count = 0; 11077 } else if (bp->dev->flags & IFF_MULTICAST) { 11078 u32 mask = 0; 11079 11080 bnxt_mc_list_updated(bp, &mask); 11081 vnic->rx_mask |= mask; 11082 } 11083 11084 rc = bnxt_cfg_rx_mode(bp); 11085 if (rc) 11086 goto err_out; 11087 11088 skip_rx_mask: 11089 rc = bnxt_hwrm_set_coal(bp); 11090 if (rc) 11091 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 11092 rc); 11093 11094 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11095 rc = bnxt_setup_nitroa0_vnic(bp); 11096 if (rc) 11097 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 11098 rc); 11099 } 11100 11101 if (BNXT_VF(bp)) { 11102 bnxt_hwrm_func_qcfg(bp); 11103 netdev_update_features(bp->dev); 11104 } 11105 11106 return 0; 11107 11108 err_out: 11109 bnxt_hwrm_resource_free(bp, 0, true); 11110 11111 return rc; 11112 } 11113 11114 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 11115 { 11116 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 11117 return 0; 11118 } 11119 11120 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 11121 { 11122 bnxt_init_cp_rings(bp); 11123 bnxt_init_rx_rings(bp); 11124 bnxt_init_tx_rings(bp); 11125 bnxt_init_ring_grps(bp, irq_re_init); 11126 bnxt_init_vnics(bp); 11127 11128 return bnxt_init_chip(bp, irq_re_init); 11129 } 11130 11131 static int bnxt_set_real_num_queues(struct bnxt *bp) 11132 { 11133 int rc; 11134 struct net_device *dev = bp->dev; 11135 11136 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 11137 bp->tx_nr_rings_xdp); 11138 if (rc) 11139 return rc; 11140 11141 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 11142 if (rc) 11143 return rc; 11144 11145 #ifdef CONFIG_RFS_ACCEL 11146 if (bp->flags & BNXT_FLAG_RFS) 11147 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 11148 #endif 11149 11150 return rc; 11151 } 11152 11153 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11154 bool shared) 11155 { 11156 int _rx = *rx, _tx = *tx; 11157 11158 if (shared) { 11159 *rx = min_t(int, _rx, max); 11160 *tx = min_t(int, _tx, max); 11161 } else { 11162 if (max < 2) 11163 return -ENOMEM; 11164 11165 while (_rx + _tx > max) { 11166 if (_rx > _tx && _rx > 1) 11167 _rx--; 11168 else if (_tx > 1) 11169 _tx--; 11170 } 11171 *rx = _rx; 11172 *tx = _tx; 11173 } 11174 return 0; 11175 } 11176 11177 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 11178 { 11179 return (tx - tx_xdp) / tx_sets + tx_xdp; 11180 } 11181 11182 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 11183 { 11184 int tcs = bp->num_tc; 11185 11186 if (!tcs) 11187 tcs = 1; 11188 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 11189 } 11190 11191 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 11192 { 11193 int tcs = bp->num_tc; 11194 11195 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 11196 bp->tx_nr_rings_xdp; 11197 } 11198 11199 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11200 bool sh) 11201 { 11202 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 11203 11204 if (tx_cp != *tx) { 11205 int tx_saved = tx_cp, rc; 11206 11207 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 11208 if (rc) 11209 return rc; 11210 if (tx_cp != tx_saved) 11211 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 11212 return 0; 11213 } 11214 return __bnxt_trim_rings(bp, rx, tx, max, sh); 11215 } 11216 11217 static void bnxt_setup_msix(struct bnxt *bp) 11218 { 11219 const int len = sizeof(bp->irq_tbl[0].name); 11220 struct net_device *dev = bp->dev; 11221 int tcs, i; 11222 11223 tcs = bp->num_tc; 11224 if (tcs) { 11225 int i, off, count; 11226 11227 for (i = 0; i < tcs; i++) { 11228 count = bp->tx_nr_rings_per_tc; 11229 off = BNXT_TC_TO_RING_BASE(bp, i); 11230 netdev_set_tc_queue(dev, i, count, off); 11231 } 11232 } 11233 11234 for (i = 0; i < bp->cp_nr_rings; i++) { 11235 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11236 char *attr; 11237 11238 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 11239 attr = "TxRx"; 11240 else if (i < bp->rx_nr_rings) 11241 attr = "rx"; 11242 else 11243 attr = "tx"; 11244 11245 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 11246 attr, i); 11247 bp->irq_tbl[map_idx].handler = bnxt_msix; 11248 } 11249 } 11250 11251 static int bnxt_init_int_mode(struct bnxt *bp); 11252 11253 static int bnxt_change_msix(struct bnxt *bp, int total) 11254 { 11255 struct msi_map map; 11256 int i; 11257 11258 /* add MSIX to the end if needed */ 11259 for (i = bp->total_irqs; i < total; i++) { 11260 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 11261 if (map.index < 0) 11262 return bp->total_irqs; 11263 bp->irq_tbl[i].vector = map.virq; 11264 bp->total_irqs++; 11265 } 11266 11267 /* trim MSIX from the end if needed */ 11268 for (i = bp->total_irqs; i > total; i--) { 11269 map.index = i - 1; 11270 map.virq = bp->irq_tbl[i - 1].vector; 11271 pci_msix_free_irq(bp->pdev, map); 11272 bp->total_irqs--; 11273 } 11274 return bp->total_irqs; 11275 } 11276 11277 static int bnxt_setup_int_mode(struct bnxt *bp) 11278 { 11279 int rc; 11280 11281 if (!bp->irq_tbl) { 11282 rc = bnxt_init_int_mode(bp); 11283 if (rc || !bp->irq_tbl) 11284 return rc ?: -ENODEV; 11285 } 11286 11287 bnxt_setup_msix(bp); 11288 11289 rc = bnxt_set_real_num_queues(bp); 11290 return rc; 11291 } 11292 11293 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11294 { 11295 return bp->hw_resc.max_rsscos_ctxs; 11296 } 11297 11298 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11299 { 11300 return bp->hw_resc.max_vnics; 11301 } 11302 11303 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11304 { 11305 return bp->hw_resc.max_stat_ctxs; 11306 } 11307 11308 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11309 { 11310 return bp->hw_resc.max_cp_rings; 11311 } 11312 11313 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11314 { 11315 unsigned int cp = bp->hw_resc.max_cp_rings; 11316 11317 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11318 cp -= bnxt_get_ulp_msix_num(bp); 11319 11320 return cp; 11321 } 11322 11323 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11324 { 11325 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11326 11327 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11328 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11329 11330 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11331 } 11332 11333 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11334 { 11335 bp->hw_resc.max_irqs = max_irqs; 11336 } 11337 11338 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11339 { 11340 unsigned int cp; 11341 11342 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11343 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11344 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11345 else 11346 return cp - bp->cp_nr_rings; 11347 } 11348 11349 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11350 { 11351 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11352 } 11353 11354 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11355 { 11356 int max_irq = bnxt_get_max_func_irqs(bp); 11357 int total_req = bp->cp_nr_rings + num; 11358 11359 if (max_irq < total_req) { 11360 num = max_irq - bp->cp_nr_rings; 11361 if (num <= 0) 11362 return 0; 11363 } 11364 return num; 11365 } 11366 11367 static int bnxt_get_num_msix(struct bnxt *bp) 11368 { 11369 if (!BNXT_NEW_RM(bp)) 11370 return bnxt_get_max_func_irqs(bp); 11371 11372 return bnxt_nq_rings_in_use(bp); 11373 } 11374 11375 static int bnxt_init_int_mode(struct bnxt *bp) 11376 { 11377 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 11378 11379 total_vecs = bnxt_get_num_msix(bp); 11380 max = bnxt_get_max_func_irqs(bp); 11381 if (total_vecs > max) 11382 total_vecs = max; 11383 11384 if (!total_vecs) 11385 return 0; 11386 11387 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11388 min = 2; 11389 11390 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11391 PCI_IRQ_MSIX); 11392 ulp_msix = bnxt_get_ulp_msix_num(bp); 11393 if (total_vecs < 0 || total_vecs < ulp_msix) { 11394 rc = -ENODEV; 11395 goto msix_setup_exit; 11396 } 11397 11398 tbl_size = total_vecs; 11399 if (pci_msix_can_alloc_dyn(bp->pdev)) 11400 tbl_size = max; 11401 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 11402 if (bp->irq_tbl) { 11403 for (i = 0; i < total_vecs; i++) 11404 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11405 11406 bp->total_irqs = total_vecs; 11407 /* Trim rings based upon num of vectors allocated */ 11408 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11409 total_vecs - ulp_msix, min == 1); 11410 if (rc) 11411 goto msix_setup_exit; 11412 11413 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11414 bp->cp_nr_rings = (min == 1) ? 11415 max_t(int, tx_cp, bp->rx_nr_rings) : 11416 tx_cp + bp->rx_nr_rings; 11417 11418 } else { 11419 rc = -ENOMEM; 11420 goto msix_setup_exit; 11421 } 11422 return 0; 11423 11424 msix_setup_exit: 11425 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11426 kfree(bp->irq_tbl); 11427 bp->irq_tbl = NULL; 11428 pci_free_irq_vectors(bp->pdev); 11429 return rc; 11430 } 11431 11432 static void bnxt_clear_int_mode(struct bnxt *bp) 11433 { 11434 pci_free_irq_vectors(bp->pdev); 11435 11436 kfree(bp->irq_tbl); 11437 bp->irq_tbl = NULL; 11438 } 11439 11440 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11441 { 11442 bool irq_cleared = false; 11443 bool irq_change = false; 11444 int tcs = bp->num_tc; 11445 int irqs_required; 11446 int rc; 11447 11448 if (!bnxt_need_reserve_rings(bp)) 11449 return 0; 11450 11451 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 11452 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11453 11454 if (ulp_msix > bp->ulp_num_msix_want) 11455 ulp_msix = bp->ulp_num_msix_want; 11456 irqs_required = ulp_msix + bp->cp_nr_rings; 11457 } else { 11458 irqs_required = bnxt_get_num_msix(bp); 11459 } 11460 11461 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11462 irq_change = true; 11463 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11464 bnxt_ulp_irq_stop(bp); 11465 bnxt_clear_int_mode(bp); 11466 irq_cleared = true; 11467 } 11468 } 11469 rc = __bnxt_reserve_rings(bp); 11470 if (irq_cleared) { 11471 if (!rc) 11472 rc = bnxt_init_int_mode(bp); 11473 bnxt_ulp_irq_restart(bp, rc); 11474 } else if (irq_change && !rc) { 11475 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11476 rc = -ENOSPC; 11477 } 11478 if (rc) { 11479 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11480 return rc; 11481 } 11482 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11483 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11484 netdev_err(bp->dev, "tx ring reservation failure\n"); 11485 netdev_reset_tc(bp->dev); 11486 bp->num_tc = 0; 11487 if (bp->tx_nr_rings_xdp) 11488 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11489 else 11490 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11491 return -ENOMEM; 11492 } 11493 return 0; 11494 } 11495 11496 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx) 11497 { 11498 struct bnxt_tx_ring_info *txr; 11499 struct netdev_queue *txq; 11500 struct bnxt_napi *bnapi; 11501 int i; 11502 11503 bnapi = bp->bnapi[idx]; 11504 bnxt_for_each_napi_tx(i, bnapi, txr) { 11505 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11506 synchronize_net(); 11507 11508 if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) { 11509 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11510 if (txq) { 11511 __netif_tx_lock_bh(txq); 11512 netif_tx_stop_queue(txq); 11513 __netif_tx_unlock_bh(txq); 11514 } 11515 } 11516 11517 if (!bp->tph_mode) 11518 continue; 11519 11520 bnxt_hwrm_tx_ring_free(bp, txr, true); 11521 bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr); 11522 bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index); 11523 bnxt_clear_one_cp_ring(bp, txr->tx_cpr); 11524 } 11525 } 11526 11527 static int bnxt_tx_queue_start(struct bnxt *bp, int idx) 11528 { 11529 struct bnxt_tx_ring_info *txr; 11530 struct netdev_queue *txq; 11531 struct bnxt_napi *bnapi; 11532 int rc, i; 11533 11534 bnapi = bp->bnapi[idx]; 11535 /* All rings have been reserved and previously allocated. 11536 * Reallocating with the same parameters should never fail. 11537 */ 11538 bnxt_for_each_napi_tx(i, bnapi, txr) { 11539 if (!bp->tph_mode) 11540 goto start_tx; 11541 11542 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 11543 if (rc) 11544 return rc; 11545 11546 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false); 11547 if (rc) 11548 return rc; 11549 11550 txr->tx_prod = 0; 11551 txr->tx_cons = 0; 11552 txr->tx_hw_cons = 0; 11553 start_tx: 11554 WRITE_ONCE(txr->dev_state, 0); 11555 synchronize_net(); 11556 11557 if (bnapi->flags & BNXT_NAPI_FLAG_XDP) 11558 continue; 11559 11560 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11561 if (txq) 11562 netif_tx_start_queue(txq); 11563 } 11564 11565 return 0; 11566 } 11567 11568 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify, 11569 const cpumask_t *mask) 11570 { 11571 struct bnxt_irq *irq; 11572 u16 tag; 11573 int err; 11574 11575 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11576 11577 if (!irq->bp->tph_mode) 11578 return; 11579 11580 cpumask_copy(irq->cpu_mask, mask); 11581 11582 if (irq->ring_nr >= irq->bp->rx_nr_rings) 11583 return; 11584 11585 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11586 cpumask_first(irq->cpu_mask), &tag)) 11587 return; 11588 11589 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag)) 11590 return; 11591 11592 netdev_lock(irq->bp->dev); 11593 if (netif_running(irq->bp->dev)) { 11594 err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr); 11595 if (err) 11596 netdev_err(irq->bp->dev, 11597 "RX queue restart failed: err=%d\n", err); 11598 } 11599 netdev_unlock(irq->bp->dev); 11600 } 11601 11602 static void bnxt_irq_affinity_release(struct kref *ref) 11603 { 11604 struct irq_affinity_notify *notify = 11605 container_of(ref, struct irq_affinity_notify, kref); 11606 struct bnxt_irq *irq; 11607 11608 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11609 11610 if (!irq->bp->tph_mode) 11611 return; 11612 11613 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) { 11614 netdev_err(irq->bp->dev, 11615 "Setting ST=0 for MSIX entry %d failed\n", 11616 irq->msix_nr); 11617 return; 11618 } 11619 } 11620 11621 static void bnxt_release_irq_notifier(struct bnxt_irq *irq) 11622 { 11623 irq_set_affinity_notifier(irq->vector, NULL); 11624 } 11625 11626 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq) 11627 { 11628 struct irq_affinity_notify *notify; 11629 11630 irq->bp = bp; 11631 11632 /* Nothing to do if TPH is not enabled */ 11633 if (!bp->tph_mode) 11634 return; 11635 11636 /* Register IRQ affinity notifier */ 11637 notify = &irq->affinity_notify; 11638 notify->irq = irq->vector; 11639 notify->notify = bnxt_irq_affinity_notify; 11640 notify->release = bnxt_irq_affinity_release; 11641 11642 irq_set_affinity_notifier(irq->vector, notify); 11643 } 11644 11645 static void bnxt_free_irq(struct bnxt *bp) 11646 { 11647 struct bnxt_irq *irq; 11648 int i; 11649 11650 #ifdef CONFIG_RFS_ACCEL 11651 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11652 bp->dev->rx_cpu_rmap = NULL; 11653 #endif 11654 if (!bp->irq_tbl || !bp->bnapi) 11655 return; 11656 11657 for (i = 0; i < bp->cp_nr_rings; i++) { 11658 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11659 11660 irq = &bp->irq_tbl[map_idx]; 11661 if (irq->requested) { 11662 if (irq->have_cpumask) { 11663 irq_update_affinity_hint(irq->vector, NULL); 11664 free_cpumask_var(irq->cpu_mask); 11665 irq->have_cpumask = 0; 11666 } 11667 11668 bnxt_release_irq_notifier(irq); 11669 11670 free_irq(irq->vector, bp->bnapi[i]); 11671 } 11672 11673 irq->requested = 0; 11674 } 11675 11676 /* Disable TPH support */ 11677 pcie_disable_tph(bp->pdev); 11678 bp->tph_mode = 0; 11679 } 11680 11681 static int bnxt_request_irq(struct bnxt *bp) 11682 { 11683 struct cpu_rmap *rmap = NULL; 11684 int i, j, rc = 0; 11685 unsigned long flags = 0; 11686 11687 rc = bnxt_setup_int_mode(bp); 11688 if (rc) { 11689 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11690 rc); 11691 return rc; 11692 } 11693 #ifdef CONFIG_RFS_ACCEL 11694 rmap = bp->dev->rx_cpu_rmap; 11695 #endif 11696 11697 /* Enable TPH support as part of IRQ request */ 11698 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11699 if (!rc) 11700 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11701 11702 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11703 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11704 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11705 11706 if (IS_ENABLED(CONFIG_RFS_ACCEL) && 11707 rmap && bp->bnapi[i]->rx_ring) { 11708 rc = irq_cpu_rmap_add(rmap, irq->vector); 11709 if (rc) 11710 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11711 j); 11712 j++; 11713 } 11714 11715 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11716 bp->bnapi[i]); 11717 if (rc) 11718 break; 11719 11720 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 11721 irq->requested = 1; 11722 11723 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11724 int numa_node = dev_to_node(&bp->pdev->dev); 11725 u16 tag; 11726 11727 irq->have_cpumask = 1; 11728 irq->msix_nr = map_idx; 11729 irq->ring_nr = i; 11730 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11731 irq->cpu_mask); 11732 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11733 if (rc) { 11734 netdev_warn(bp->dev, 11735 "Update affinity hint failed, IRQ = %d\n", 11736 irq->vector); 11737 break; 11738 } 11739 11740 bnxt_register_irq_notifier(bp, irq); 11741 11742 /* Init ST table entry */ 11743 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11744 cpumask_first(irq->cpu_mask), 11745 &tag)) 11746 continue; 11747 11748 pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag); 11749 } 11750 } 11751 return rc; 11752 } 11753 11754 static void bnxt_del_napi(struct bnxt *bp) 11755 { 11756 int i; 11757 11758 if (!bp->bnapi) 11759 return; 11760 11761 for (i = 0; i < bp->rx_nr_rings; i++) 11762 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11763 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11764 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11765 11766 for (i = 0; i < bp->cp_nr_rings; i++) { 11767 struct bnxt_napi *bnapi = bp->bnapi[i]; 11768 11769 __netif_napi_del_locked(&bnapi->napi); 11770 } 11771 /* We called __netif_napi_del_locked(), we need 11772 * to respect an RCU grace period before freeing napi structures. 11773 */ 11774 synchronize_net(); 11775 } 11776 11777 static void bnxt_init_napi(struct bnxt *bp) 11778 { 11779 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11780 unsigned int cp_nr_rings = bp->cp_nr_rings; 11781 struct bnxt_napi *bnapi; 11782 int i; 11783 11784 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11785 poll_fn = bnxt_poll_p5; 11786 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11787 cp_nr_rings--; 11788 11789 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11790 11791 for (i = 0; i < cp_nr_rings; i++) { 11792 bnapi = bp->bnapi[i]; 11793 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 11794 bnapi->index); 11795 } 11796 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11797 bnapi = bp->bnapi[cp_nr_rings]; 11798 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11799 } 11800 } 11801 11802 static void bnxt_disable_napi(struct bnxt *bp) 11803 { 11804 int i; 11805 11806 if (!bp->bnapi || 11807 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11808 return; 11809 11810 for (i = 0; i < bp->cp_nr_rings; i++) { 11811 struct bnxt_napi *bnapi = bp->bnapi[i]; 11812 struct bnxt_cp_ring_info *cpr; 11813 11814 cpr = &bnapi->cp_ring; 11815 if (bnapi->tx_fault) 11816 cpr->sw_stats->tx.tx_resets++; 11817 if (bnapi->in_reset) 11818 cpr->sw_stats->rx.rx_resets++; 11819 napi_disable_locked(&bnapi->napi); 11820 } 11821 } 11822 11823 static void bnxt_enable_napi(struct bnxt *bp) 11824 { 11825 int i; 11826 11827 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11828 for (i = 0; i < bp->cp_nr_rings; i++) { 11829 struct bnxt_napi *bnapi = bp->bnapi[i]; 11830 struct bnxt_cp_ring_info *cpr; 11831 11832 bnapi->tx_fault = 0; 11833 11834 cpr = &bnapi->cp_ring; 11835 bnapi->in_reset = false; 11836 11837 if (bnapi->rx_ring) { 11838 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11839 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11840 } 11841 napi_enable_locked(&bnapi->napi); 11842 } 11843 } 11844 11845 void bnxt_tx_disable(struct bnxt *bp) 11846 { 11847 int i; 11848 struct bnxt_tx_ring_info *txr; 11849 11850 if (bp->tx_ring) { 11851 for (i = 0; i < bp->tx_nr_rings; i++) { 11852 txr = &bp->tx_ring[i]; 11853 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11854 } 11855 } 11856 /* Make sure napi polls see @dev_state change */ 11857 synchronize_net(); 11858 /* Drop carrier first to prevent TX timeout */ 11859 netif_carrier_off(bp->dev); 11860 /* Stop all TX queues */ 11861 netif_tx_disable(bp->dev); 11862 } 11863 11864 void bnxt_tx_enable(struct bnxt *bp) 11865 { 11866 int i; 11867 struct bnxt_tx_ring_info *txr; 11868 11869 for (i = 0; i < bp->tx_nr_rings; i++) { 11870 txr = &bp->tx_ring[i]; 11871 WRITE_ONCE(txr->dev_state, 0); 11872 } 11873 /* Make sure napi polls see @dev_state change */ 11874 synchronize_net(); 11875 netif_tx_wake_all_queues(bp->dev); 11876 if (BNXT_LINK_IS_UP(bp)) 11877 netif_carrier_on(bp->dev); 11878 } 11879 11880 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11881 { 11882 u8 active_fec = link_info->active_fec_sig_mode & 11883 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11884 11885 switch (active_fec) { 11886 default: 11887 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11888 return "None"; 11889 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11890 return "Clause 74 BaseR"; 11891 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11892 return "Clause 91 RS(528,514)"; 11893 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11894 return "Clause 91 RS544_1XN"; 11895 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11896 return "Clause 91 RS(544,514)"; 11897 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11898 return "Clause 91 RS272_1XN"; 11899 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11900 return "Clause 91 RS(272,257)"; 11901 } 11902 } 11903 11904 void bnxt_report_link(struct bnxt *bp) 11905 { 11906 if (BNXT_LINK_IS_UP(bp)) { 11907 const char *signal = ""; 11908 const char *flow_ctrl; 11909 const char *duplex; 11910 u32 speed; 11911 u16 fec; 11912 11913 netif_carrier_on(bp->dev); 11914 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11915 if (speed == SPEED_UNKNOWN) { 11916 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11917 return; 11918 } 11919 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11920 duplex = "full"; 11921 else 11922 duplex = "half"; 11923 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11924 flow_ctrl = "ON - receive & transmit"; 11925 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11926 flow_ctrl = "ON - transmit"; 11927 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11928 flow_ctrl = "ON - receive"; 11929 else 11930 flow_ctrl = "none"; 11931 if (bp->link_info.phy_qcfg_resp.option_flags & 11932 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11933 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11934 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11935 switch (sig_mode) { 11936 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11937 signal = "(NRZ) "; 11938 break; 11939 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11940 signal = "(PAM4 56Gbps) "; 11941 break; 11942 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11943 signal = "(PAM4 112Gbps) "; 11944 break; 11945 default: 11946 break; 11947 } 11948 } 11949 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11950 speed, signal, duplex, flow_ctrl); 11951 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11952 netdev_info(bp->dev, "EEE is %s\n", 11953 bp->eee.eee_active ? "active" : 11954 "not active"); 11955 fec = bp->link_info.fec_cfg; 11956 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11957 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11958 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11959 bnxt_report_fec(&bp->link_info)); 11960 } else { 11961 netif_carrier_off(bp->dev); 11962 netdev_err(bp->dev, "NIC Link is Down\n"); 11963 } 11964 } 11965 11966 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11967 { 11968 if (!resp->supported_speeds_auto_mode && 11969 !resp->supported_speeds_force_mode && 11970 !resp->supported_pam4_speeds_auto_mode && 11971 !resp->supported_pam4_speeds_force_mode && 11972 !resp->supported_speeds2_auto_mode && 11973 !resp->supported_speeds2_force_mode) 11974 return true; 11975 return false; 11976 } 11977 11978 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11979 { 11980 struct bnxt_link_info *link_info = &bp->link_info; 11981 struct hwrm_port_phy_qcaps_output *resp; 11982 struct hwrm_port_phy_qcaps_input *req; 11983 int rc = 0; 11984 11985 if (bp->hwrm_spec_code < 0x10201) 11986 return 0; 11987 11988 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11989 if (rc) 11990 return rc; 11991 11992 resp = hwrm_req_hold(bp, req); 11993 rc = hwrm_req_send(bp, req); 11994 if (rc) 11995 goto hwrm_phy_qcaps_exit; 11996 11997 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11998 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11999 struct ethtool_keee *eee = &bp->eee; 12000 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 12001 12002 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 12003 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 12004 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 12005 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 12006 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 12007 } 12008 12009 if (bp->hwrm_spec_code >= 0x10a01) { 12010 if (bnxt_phy_qcaps_no_speed(resp)) { 12011 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 12012 netdev_warn(bp->dev, "Ethernet link disabled\n"); 12013 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 12014 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 12015 netdev_info(bp->dev, "Ethernet link enabled\n"); 12016 /* Phy re-enabled, reprobe the speeds */ 12017 link_info->support_auto_speeds = 0; 12018 link_info->support_pam4_auto_speeds = 0; 12019 link_info->support_auto_speeds2 = 0; 12020 } 12021 } 12022 if (resp->supported_speeds_auto_mode) 12023 link_info->support_auto_speeds = 12024 le16_to_cpu(resp->supported_speeds_auto_mode); 12025 if (resp->supported_pam4_speeds_auto_mode) 12026 link_info->support_pam4_auto_speeds = 12027 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 12028 if (resp->supported_speeds2_auto_mode) 12029 link_info->support_auto_speeds2 = 12030 le16_to_cpu(resp->supported_speeds2_auto_mode); 12031 12032 bp->port_count = resp->port_cnt; 12033 12034 hwrm_phy_qcaps_exit: 12035 hwrm_req_drop(bp, req); 12036 return rc; 12037 } 12038 12039 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 12040 { 12041 struct hwrm_port_mac_qcaps_output *resp; 12042 struct hwrm_port_mac_qcaps_input *req; 12043 int rc; 12044 12045 if (bp->hwrm_spec_code < 0x10a03) 12046 return; 12047 12048 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 12049 if (rc) 12050 return; 12051 12052 resp = hwrm_req_hold(bp, req); 12053 rc = hwrm_req_send_silent(bp, req); 12054 if (!rc) 12055 bp->mac_flags = resp->flags; 12056 hwrm_req_drop(bp, req); 12057 } 12058 12059 static bool bnxt_support_dropped(u16 advertising, u16 supported) 12060 { 12061 u16 diff = advertising ^ supported; 12062 12063 return ((supported | diff) != supported); 12064 } 12065 12066 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 12067 { 12068 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 12069 12070 /* Check if any advertised speeds are no longer supported. The caller 12071 * holds the link_lock mutex, so we can modify link_info settings. 12072 */ 12073 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12074 if (bnxt_support_dropped(link_info->advertising, 12075 link_info->support_auto_speeds2)) { 12076 link_info->advertising = link_info->support_auto_speeds2; 12077 return true; 12078 } 12079 return false; 12080 } 12081 if (bnxt_support_dropped(link_info->advertising, 12082 link_info->support_auto_speeds)) { 12083 link_info->advertising = link_info->support_auto_speeds; 12084 return true; 12085 } 12086 if (bnxt_support_dropped(link_info->advertising_pam4, 12087 link_info->support_pam4_auto_speeds)) { 12088 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12089 return true; 12090 } 12091 return false; 12092 } 12093 12094 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12095 { 12096 struct bnxt_link_info *link_info = &bp->link_info; 12097 struct hwrm_port_phy_qcfg_output *resp; 12098 struct hwrm_port_phy_qcfg_input *req; 12099 u8 link_state = link_info->link_state; 12100 bool support_changed; 12101 int rc; 12102 12103 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12104 if (rc) 12105 return rc; 12106 12107 resp = hwrm_req_hold(bp, req); 12108 rc = hwrm_req_send(bp, req); 12109 if (rc) { 12110 hwrm_req_drop(bp, req); 12111 if (BNXT_VF(bp) && rc == -ENODEV) { 12112 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12113 rc = 0; 12114 } 12115 return rc; 12116 } 12117 12118 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12119 link_info->phy_link_status = resp->link; 12120 link_info->duplex = resp->duplex_cfg; 12121 if (bp->hwrm_spec_code >= 0x10800) 12122 link_info->duplex = resp->duplex_state; 12123 link_info->pause = resp->pause; 12124 link_info->auto_mode = resp->auto_mode; 12125 link_info->auto_pause_setting = resp->auto_pause; 12126 link_info->lp_pause = resp->link_partner_adv_pause; 12127 link_info->force_pause_setting = resp->force_pause; 12128 link_info->duplex_setting = resp->duplex_cfg; 12129 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12130 link_info->link_speed = le16_to_cpu(resp->link_speed); 12131 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12132 link_info->active_lanes = resp->active_lanes; 12133 } else { 12134 link_info->link_speed = 0; 12135 link_info->active_lanes = 0; 12136 } 12137 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12138 link_info->force_pam4_link_speed = 12139 le16_to_cpu(resp->force_pam4_link_speed); 12140 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12141 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12142 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12143 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12144 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12145 link_info->auto_pam4_link_speeds = 12146 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12147 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12148 link_info->lp_auto_link_speeds = 12149 le16_to_cpu(resp->link_partner_adv_speeds); 12150 link_info->lp_auto_pam4_link_speeds = 12151 resp->link_partner_pam4_adv_speeds; 12152 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12153 link_info->phy_ver[0] = resp->phy_maj; 12154 link_info->phy_ver[1] = resp->phy_min; 12155 link_info->phy_ver[2] = resp->phy_bld; 12156 link_info->media_type = resp->media_type; 12157 link_info->phy_type = resp->phy_type; 12158 link_info->transceiver = resp->xcvr_pkg_type; 12159 link_info->phy_addr = resp->eee_config_phy_addr & 12160 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12161 link_info->module_status = resp->module_status; 12162 12163 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12164 struct ethtool_keee *eee = &bp->eee; 12165 u16 fw_speeds; 12166 12167 eee->eee_active = 0; 12168 if (resp->eee_config_phy_addr & 12169 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12170 eee->eee_active = 1; 12171 fw_speeds = le16_to_cpu( 12172 resp->link_partner_adv_eee_link_speed_mask); 12173 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12174 } 12175 12176 /* Pull initial EEE config */ 12177 if (!chng_link_state) { 12178 if (resp->eee_config_phy_addr & 12179 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12180 eee->eee_enabled = 1; 12181 12182 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12183 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12184 12185 if (resp->eee_config_phy_addr & 12186 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12187 __le32 tmr; 12188 12189 eee->tx_lpi_enabled = 1; 12190 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12191 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12192 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12193 } 12194 } 12195 } 12196 12197 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12198 if (bp->hwrm_spec_code >= 0x10504) { 12199 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12200 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12201 } 12202 /* TODO: need to add more logic to report VF link */ 12203 if (chng_link_state) { 12204 if (link_info->phy_link_status == BNXT_LINK_LINK) 12205 link_info->link_state = BNXT_LINK_STATE_UP; 12206 else 12207 link_info->link_state = BNXT_LINK_STATE_DOWN; 12208 if (link_state != link_info->link_state) 12209 bnxt_report_link(bp); 12210 } else { 12211 /* always link down if not require to update link state */ 12212 link_info->link_state = BNXT_LINK_STATE_DOWN; 12213 } 12214 hwrm_req_drop(bp, req); 12215 12216 if (!BNXT_PHY_CFG_ABLE(bp)) 12217 return 0; 12218 12219 support_changed = bnxt_support_speed_dropped(link_info); 12220 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12221 bnxt_hwrm_set_link_setting(bp, true, false); 12222 return 0; 12223 } 12224 12225 static void bnxt_get_port_module_status(struct bnxt *bp) 12226 { 12227 struct bnxt_link_info *link_info = &bp->link_info; 12228 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12229 u8 module_status; 12230 12231 if (bnxt_update_link(bp, true)) 12232 return; 12233 12234 module_status = link_info->module_status; 12235 switch (module_status) { 12236 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12237 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12238 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12239 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12240 bp->pf.port_id); 12241 if (bp->hwrm_spec_code >= 0x10201) { 12242 netdev_warn(bp->dev, "Module part number %s\n", 12243 resp->phy_vendor_partnumber); 12244 } 12245 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12246 netdev_warn(bp->dev, "TX is disabled\n"); 12247 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12248 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12249 } 12250 } 12251 12252 static void 12253 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12254 { 12255 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12256 if (bp->hwrm_spec_code >= 0x10201) 12257 req->auto_pause = 12258 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12259 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12260 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12261 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12262 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12263 req->enables |= 12264 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12265 } else { 12266 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12267 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12268 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12269 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12270 req->enables |= 12271 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12272 if (bp->hwrm_spec_code >= 0x10201) { 12273 req->auto_pause = req->force_pause; 12274 req->enables |= cpu_to_le32( 12275 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12276 } 12277 } 12278 } 12279 12280 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12281 { 12282 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12283 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12284 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12285 req->enables |= 12286 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12287 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12288 } else if (bp->link_info.advertising) { 12289 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12290 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12291 } 12292 if (bp->link_info.advertising_pam4) { 12293 req->enables |= 12294 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12295 req->auto_link_pam4_speed_mask = 12296 cpu_to_le16(bp->link_info.advertising_pam4); 12297 } 12298 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12299 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12300 } else { 12301 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12302 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12303 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12304 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12305 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12306 (u32)bp->link_info.req_link_speed); 12307 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12308 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12309 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12310 } else { 12311 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12312 } 12313 } 12314 12315 /* tell chimp that the setting takes effect immediately */ 12316 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12317 } 12318 12319 int bnxt_hwrm_set_pause(struct bnxt *bp) 12320 { 12321 struct hwrm_port_phy_cfg_input *req; 12322 int rc; 12323 12324 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12325 if (rc) 12326 return rc; 12327 12328 bnxt_hwrm_set_pause_common(bp, req); 12329 12330 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12331 bp->link_info.force_link_chng) 12332 bnxt_hwrm_set_link_common(bp, req); 12333 12334 rc = hwrm_req_send(bp, req); 12335 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12336 /* since changing of pause setting doesn't trigger any link 12337 * change event, the driver needs to update the current pause 12338 * result upon successfully return of the phy_cfg command 12339 */ 12340 bp->link_info.pause = 12341 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12342 bp->link_info.auto_pause_setting = 0; 12343 if (!bp->link_info.force_link_chng) 12344 bnxt_report_link(bp); 12345 } 12346 bp->link_info.force_link_chng = false; 12347 return rc; 12348 } 12349 12350 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12351 struct hwrm_port_phy_cfg_input *req) 12352 { 12353 struct ethtool_keee *eee = &bp->eee; 12354 12355 if (eee->eee_enabled) { 12356 u16 eee_speeds; 12357 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12358 12359 if (eee->tx_lpi_enabled) 12360 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12361 else 12362 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12363 12364 req->flags |= cpu_to_le32(flags); 12365 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12366 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12367 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12368 } else { 12369 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12370 } 12371 } 12372 12373 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12374 { 12375 struct hwrm_port_phy_cfg_input *req; 12376 int rc; 12377 12378 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12379 if (rc) 12380 return rc; 12381 12382 if (set_pause) 12383 bnxt_hwrm_set_pause_common(bp, req); 12384 12385 bnxt_hwrm_set_link_common(bp, req); 12386 12387 if (set_eee) 12388 bnxt_hwrm_set_eee(bp, req); 12389 return hwrm_req_send(bp, req); 12390 } 12391 12392 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12393 { 12394 struct hwrm_port_phy_cfg_input *req; 12395 int rc; 12396 12397 if (!BNXT_SINGLE_PF(bp)) 12398 return 0; 12399 12400 if (pci_num_vf(bp->pdev) && 12401 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12402 return 0; 12403 12404 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12405 if (rc) 12406 return rc; 12407 12408 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12409 rc = hwrm_req_send(bp, req); 12410 if (!rc) { 12411 mutex_lock(&bp->link_lock); 12412 /* Device is not obliged link down in certain scenarios, even 12413 * when forced. Setting the state unknown is consistent with 12414 * driver startup and will force link state to be reported 12415 * during subsequent open based on PORT_PHY_QCFG. 12416 */ 12417 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12418 mutex_unlock(&bp->link_lock); 12419 } 12420 return rc; 12421 } 12422 12423 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12424 { 12425 #ifdef CONFIG_TEE_BNXT_FW 12426 int rc = tee_bnxt_fw_load(); 12427 12428 if (rc) 12429 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12430 12431 return rc; 12432 #else 12433 netdev_err(bp->dev, "OP-TEE not supported\n"); 12434 return -ENODEV; 12435 #endif 12436 } 12437 12438 static int bnxt_try_recover_fw(struct bnxt *bp) 12439 { 12440 if (bp->fw_health && bp->fw_health->status_reliable) { 12441 int retry = 0, rc; 12442 u32 sts; 12443 12444 do { 12445 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12446 rc = bnxt_hwrm_poll(bp); 12447 if (!BNXT_FW_IS_BOOTING(sts) && 12448 !BNXT_FW_IS_RECOVERING(sts)) 12449 break; 12450 retry++; 12451 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12452 12453 if (!BNXT_FW_IS_HEALTHY(sts)) { 12454 netdev_err(bp->dev, 12455 "Firmware not responding, status: 0x%x\n", 12456 sts); 12457 rc = -ENODEV; 12458 } 12459 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12460 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12461 return bnxt_fw_reset_via_optee(bp); 12462 } 12463 return rc; 12464 } 12465 12466 return -ENODEV; 12467 } 12468 12469 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12470 { 12471 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12472 12473 if (!BNXT_NEW_RM(bp)) 12474 return; /* no resource reservations required */ 12475 12476 hw_resc->resv_cp_rings = 0; 12477 hw_resc->resv_stat_ctxs = 0; 12478 hw_resc->resv_irqs = 0; 12479 hw_resc->resv_tx_rings = 0; 12480 hw_resc->resv_rx_rings = 0; 12481 hw_resc->resv_hw_ring_grps = 0; 12482 hw_resc->resv_vnics = 0; 12483 hw_resc->resv_rsscos_ctxs = 0; 12484 if (!fw_reset) { 12485 bp->tx_nr_rings = 0; 12486 bp->rx_nr_rings = 0; 12487 } 12488 } 12489 12490 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12491 { 12492 int rc; 12493 12494 if (!BNXT_NEW_RM(bp)) 12495 return 0; /* no resource reservations required */ 12496 12497 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12498 if (rc) 12499 netdev_err(bp->dev, "resc_qcaps failed\n"); 12500 12501 bnxt_clear_reservations(bp, fw_reset); 12502 12503 return rc; 12504 } 12505 12506 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12507 { 12508 struct hwrm_func_drv_if_change_output *resp; 12509 struct hwrm_func_drv_if_change_input *req; 12510 bool resc_reinit = false; 12511 bool caps_change = false; 12512 int rc, retry = 0; 12513 bool fw_reset; 12514 u32 flags = 0; 12515 12516 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12517 bp->fw_reset_state = 0; 12518 12519 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12520 return 0; 12521 12522 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12523 if (rc) 12524 return rc; 12525 12526 if (up) 12527 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12528 resp = hwrm_req_hold(bp, req); 12529 12530 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12531 while (retry < BNXT_FW_IF_RETRY) { 12532 rc = hwrm_req_send(bp, req); 12533 if (rc != -EAGAIN) 12534 break; 12535 12536 msleep(50); 12537 retry++; 12538 } 12539 12540 if (rc == -EAGAIN) { 12541 hwrm_req_drop(bp, req); 12542 return rc; 12543 } else if (!rc) { 12544 flags = le32_to_cpu(resp->flags); 12545 } else if (up) { 12546 rc = bnxt_try_recover_fw(bp); 12547 fw_reset = true; 12548 } 12549 hwrm_req_drop(bp, req); 12550 if (rc) 12551 return rc; 12552 12553 if (!up) { 12554 bnxt_inv_fw_health_reg(bp); 12555 return 0; 12556 } 12557 12558 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12559 resc_reinit = true; 12560 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12561 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12562 fw_reset = true; 12563 else 12564 bnxt_remap_fw_health_regs(bp); 12565 12566 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12567 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12568 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12569 return -ENODEV; 12570 } 12571 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12572 caps_change = true; 12573 12574 if (resc_reinit || fw_reset || caps_change) { 12575 if (fw_reset || caps_change) { 12576 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12577 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12578 bnxt_ulp_irq_stop(bp); 12579 bnxt_free_ctx_mem(bp, false); 12580 bnxt_dcb_free(bp); 12581 rc = bnxt_fw_init_one(bp); 12582 if (rc) { 12583 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12584 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12585 return rc; 12586 } 12587 /* IRQ will be initialized later in bnxt_request_irq()*/ 12588 bnxt_clear_int_mode(bp); 12589 } 12590 rc = bnxt_cancel_reservations(bp, fw_reset); 12591 } 12592 return rc; 12593 } 12594 12595 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12596 { 12597 struct hwrm_port_led_qcaps_output *resp; 12598 struct hwrm_port_led_qcaps_input *req; 12599 struct bnxt_pf_info *pf = &bp->pf; 12600 int rc; 12601 12602 bp->num_leds = 0; 12603 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12604 return 0; 12605 12606 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12607 if (rc) 12608 return rc; 12609 12610 req->port_id = cpu_to_le16(pf->port_id); 12611 resp = hwrm_req_hold(bp, req); 12612 rc = hwrm_req_send(bp, req); 12613 if (rc) { 12614 hwrm_req_drop(bp, req); 12615 return rc; 12616 } 12617 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12618 int i; 12619 12620 bp->num_leds = resp->num_leds; 12621 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12622 bp->num_leds); 12623 for (i = 0; i < bp->num_leds; i++) { 12624 struct bnxt_led_info *led = &bp->leds[i]; 12625 __le16 caps = led->led_state_caps; 12626 12627 if (!led->led_group_id || 12628 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12629 bp->num_leds = 0; 12630 break; 12631 } 12632 } 12633 } 12634 hwrm_req_drop(bp, req); 12635 return 0; 12636 } 12637 12638 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12639 { 12640 struct hwrm_wol_filter_alloc_output *resp; 12641 struct hwrm_wol_filter_alloc_input *req; 12642 int rc; 12643 12644 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12645 if (rc) 12646 return rc; 12647 12648 req->port_id = cpu_to_le16(bp->pf.port_id); 12649 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12650 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12651 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12652 12653 resp = hwrm_req_hold(bp, req); 12654 rc = hwrm_req_send(bp, req); 12655 if (!rc) 12656 bp->wol_filter_id = resp->wol_filter_id; 12657 hwrm_req_drop(bp, req); 12658 return rc; 12659 } 12660 12661 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12662 { 12663 struct hwrm_wol_filter_free_input *req; 12664 int rc; 12665 12666 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12667 if (rc) 12668 return rc; 12669 12670 req->port_id = cpu_to_le16(bp->pf.port_id); 12671 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12672 req->wol_filter_id = bp->wol_filter_id; 12673 12674 return hwrm_req_send(bp, req); 12675 } 12676 12677 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12678 { 12679 struct hwrm_wol_filter_qcfg_output *resp; 12680 struct hwrm_wol_filter_qcfg_input *req; 12681 u16 next_handle = 0; 12682 int rc; 12683 12684 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12685 if (rc) 12686 return rc; 12687 12688 req->port_id = cpu_to_le16(bp->pf.port_id); 12689 req->handle = cpu_to_le16(handle); 12690 resp = hwrm_req_hold(bp, req); 12691 rc = hwrm_req_send(bp, req); 12692 if (!rc) { 12693 next_handle = le16_to_cpu(resp->next_handle); 12694 if (next_handle != 0) { 12695 if (resp->wol_type == 12696 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12697 bp->wol = 1; 12698 bp->wol_filter_id = resp->wol_filter_id; 12699 } 12700 } 12701 } 12702 hwrm_req_drop(bp, req); 12703 return next_handle; 12704 } 12705 12706 static void bnxt_get_wol_settings(struct bnxt *bp) 12707 { 12708 u16 handle = 0; 12709 12710 bp->wol = 0; 12711 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12712 return; 12713 12714 do { 12715 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12716 } while (handle && handle != 0xffff); 12717 } 12718 12719 static bool bnxt_eee_config_ok(struct bnxt *bp) 12720 { 12721 struct ethtool_keee *eee = &bp->eee; 12722 struct bnxt_link_info *link_info = &bp->link_info; 12723 12724 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12725 return true; 12726 12727 if (eee->eee_enabled) { 12728 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12729 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12730 12731 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12732 12733 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12734 eee->eee_enabled = 0; 12735 return false; 12736 } 12737 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12738 linkmode_and(eee->advertised, advertising, 12739 eee->supported); 12740 return false; 12741 } 12742 } 12743 return true; 12744 } 12745 12746 static int bnxt_update_phy_setting(struct bnxt *bp) 12747 { 12748 int rc; 12749 bool update_link = false; 12750 bool update_pause = false; 12751 bool update_eee = false; 12752 struct bnxt_link_info *link_info = &bp->link_info; 12753 12754 rc = bnxt_update_link(bp, true); 12755 if (rc) { 12756 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12757 rc); 12758 return rc; 12759 } 12760 if (!BNXT_SINGLE_PF(bp)) 12761 return 0; 12762 12763 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12764 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12765 link_info->req_flow_ctrl) 12766 update_pause = true; 12767 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12768 link_info->force_pause_setting != link_info->req_flow_ctrl) 12769 update_pause = true; 12770 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12771 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12772 update_link = true; 12773 if (bnxt_force_speed_updated(link_info)) 12774 update_link = true; 12775 if (link_info->req_duplex != link_info->duplex_setting) 12776 update_link = true; 12777 } else { 12778 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12779 update_link = true; 12780 if (bnxt_auto_speed_updated(link_info)) 12781 update_link = true; 12782 } 12783 12784 /* The last close may have shutdown the link, so need to call 12785 * PHY_CFG to bring it back up. 12786 */ 12787 if (!BNXT_LINK_IS_UP(bp)) 12788 update_link = true; 12789 12790 if (!bnxt_eee_config_ok(bp)) 12791 update_eee = true; 12792 12793 if (update_link) 12794 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12795 else if (update_pause) 12796 rc = bnxt_hwrm_set_pause(bp); 12797 if (rc) { 12798 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12799 rc); 12800 return rc; 12801 } 12802 12803 return rc; 12804 } 12805 12806 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12807 12808 static int bnxt_reinit_after_abort(struct bnxt *bp) 12809 { 12810 int rc; 12811 12812 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12813 return -EBUSY; 12814 12815 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12816 return -ENODEV; 12817 12818 rc = bnxt_fw_init_one(bp); 12819 if (!rc) { 12820 bnxt_clear_int_mode(bp); 12821 rc = bnxt_init_int_mode(bp); 12822 if (!rc) { 12823 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12824 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12825 } 12826 } 12827 return rc; 12828 } 12829 12830 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12831 { 12832 struct bnxt_ntuple_filter *ntp_fltr; 12833 struct bnxt_l2_filter *l2_fltr; 12834 12835 if (list_empty(&fltr->list)) 12836 return; 12837 12838 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12839 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12840 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12841 atomic_inc(&l2_fltr->refcnt); 12842 ntp_fltr->l2_fltr = l2_fltr; 12843 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12844 bnxt_del_ntp_filter(bp, ntp_fltr); 12845 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12846 fltr->sw_id); 12847 } 12848 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12849 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12850 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12851 bnxt_del_l2_filter(bp, l2_fltr); 12852 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12853 fltr->sw_id); 12854 } 12855 } 12856 } 12857 12858 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12859 { 12860 struct bnxt_filter_base *usr_fltr, *tmp; 12861 12862 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12863 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12864 } 12865 12866 static int bnxt_set_xps_mapping(struct bnxt *bp) 12867 { 12868 int numa_node = dev_to_node(&bp->pdev->dev); 12869 unsigned int q_idx, map_idx, cpu, i; 12870 const struct cpumask *cpu_mask_ptr; 12871 int nr_cpus = num_online_cpus(); 12872 cpumask_t *q_map; 12873 int rc = 0; 12874 12875 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12876 if (!q_map) 12877 return -ENOMEM; 12878 12879 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12880 * Each TC has the same number of TX queues. The nth TX queue for each 12881 * TC will have the same CPU mask. 12882 */ 12883 for (i = 0; i < nr_cpus; i++) { 12884 map_idx = i % bp->tx_nr_rings_per_tc; 12885 cpu = cpumask_local_spread(i, numa_node); 12886 cpu_mask_ptr = get_cpu_mask(cpu); 12887 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12888 } 12889 12890 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12891 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12892 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12893 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12894 if (rc) { 12895 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12896 q_idx); 12897 break; 12898 } 12899 } 12900 12901 kfree(q_map); 12902 12903 return rc; 12904 } 12905 12906 static int bnxt_tx_nr_rings(struct bnxt *bp) 12907 { 12908 return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc : 12909 bp->tx_nr_rings_per_tc; 12910 } 12911 12912 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp) 12913 { 12914 return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings; 12915 } 12916 12917 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12918 { 12919 int rc = 0; 12920 12921 netif_carrier_off(bp->dev); 12922 if (irq_re_init) { 12923 /* Reserve rings now if none were reserved at driver probe. */ 12924 rc = bnxt_init_dflt_ring_mode(bp); 12925 if (rc) { 12926 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12927 return rc; 12928 } 12929 } 12930 rc = bnxt_reserve_rings(bp, irq_re_init); 12931 if (rc) 12932 return rc; 12933 12934 /* Make adjustments if reserved TX rings are less than requested */ 12935 bp->tx_nr_rings -= bp->tx_nr_rings_xdp; 12936 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 12937 if (bp->tx_nr_rings_xdp) { 12938 bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc; 12939 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 12940 } 12941 rc = bnxt_alloc_mem(bp, irq_re_init); 12942 if (rc) { 12943 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12944 goto open_err_free_mem; 12945 } 12946 12947 if (irq_re_init) { 12948 bnxt_init_napi(bp); 12949 rc = bnxt_request_irq(bp); 12950 if (rc) { 12951 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12952 goto open_err_irq; 12953 } 12954 } 12955 12956 rc = bnxt_init_nic(bp, irq_re_init); 12957 if (rc) { 12958 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12959 goto open_err_irq; 12960 } 12961 12962 bnxt_enable_napi(bp); 12963 bnxt_debug_dev_init(bp); 12964 12965 if (link_re_init) { 12966 mutex_lock(&bp->link_lock); 12967 rc = bnxt_update_phy_setting(bp); 12968 mutex_unlock(&bp->link_lock); 12969 if (rc) { 12970 netdev_warn(bp->dev, "failed to update phy settings\n"); 12971 if (BNXT_SINGLE_PF(bp)) { 12972 bp->link_info.phy_retry = true; 12973 bp->link_info.phy_retry_expires = 12974 jiffies + 5 * HZ; 12975 } 12976 } 12977 } 12978 12979 if (irq_re_init) { 12980 udp_tunnel_nic_reset_ntf(bp->dev); 12981 rc = bnxt_set_xps_mapping(bp); 12982 if (rc) 12983 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12984 } 12985 12986 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12987 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12988 static_branch_enable(&bnxt_xdp_locking_key); 12989 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12990 static_branch_disable(&bnxt_xdp_locking_key); 12991 } 12992 set_bit(BNXT_STATE_OPEN, &bp->state); 12993 bnxt_enable_int(bp); 12994 /* Enable TX queues */ 12995 bnxt_tx_enable(bp); 12996 mod_timer(&bp->timer, jiffies + bp->current_interval); 12997 /* Poll link status and check for SFP+ module status */ 12998 mutex_lock(&bp->link_lock); 12999 bnxt_get_port_module_status(bp); 13000 mutex_unlock(&bp->link_lock); 13001 13002 /* VF-reps may need to be re-opened after the PF is re-opened */ 13003 if (BNXT_PF(bp)) 13004 bnxt_vf_reps_open(bp); 13005 bnxt_ptp_init_rtc(bp, true); 13006 bnxt_ptp_cfg_tstamp_filters(bp); 13007 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13008 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 13009 bnxt_cfg_usr_fltrs(bp); 13010 return 0; 13011 13012 open_err_irq: 13013 bnxt_del_napi(bp); 13014 13015 open_err_free_mem: 13016 bnxt_free_skbs(bp); 13017 bnxt_free_irq(bp); 13018 bnxt_free_mem(bp, true); 13019 return rc; 13020 } 13021 13022 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13023 { 13024 int rc = 0; 13025 13026 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 13027 rc = -EIO; 13028 if (!rc) 13029 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 13030 if (rc) { 13031 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 13032 netif_close(bp->dev); 13033 } 13034 return rc; 13035 } 13036 13037 /* netdev instance lock held, open the NIC half way by allocating all 13038 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 13039 * for offline self tests. 13040 */ 13041 int bnxt_half_open_nic(struct bnxt *bp) 13042 { 13043 int rc = 0; 13044 13045 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13046 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 13047 rc = -ENODEV; 13048 goto half_open_err; 13049 } 13050 13051 rc = bnxt_alloc_mem(bp, true); 13052 if (rc) { 13053 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 13054 goto half_open_err; 13055 } 13056 bnxt_init_napi(bp); 13057 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13058 rc = bnxt_init_nic(bp, true); 13059 if (rc) { 13060 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13061 bnxt_del_napi(bp); 13062 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 13063 goto half_open_err; 13064 } 13065 return 0; 13066 13067 half_open_err: 13068 bnxt_free_skbs(bp); 13069 bnxt_free_mem(bp, true); 13070 netif_close(bp->dev); 13071 return rc; 13072 } 13073 13074 /* netdev instance lock held, this call can only be made after a previous 13075 * successful call to bnxt_half_open_nic(). 13076 */ 13077 void bnxt_half_close_nic(struct bnxt *bp) 13078 { 13079 bnxt_hwrm_resource_free(bp, false, true); 13080 bnxt_del_napi(bp); 13081 bnxt_free_skbs(bp); 13082 bnxt_free_mem(bp, true); 13083 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13084 } 13085 13086 void bnxt_reenable_sriov(struct bnxt *bp) 13087 { 13088 if (BNXT_PF(bp)) { 13089 struct bnxt_pf_info *pf = &bp->pf; 13090 int n = pf->active_vfs; 13091 13092 if (n) 13093 bnxt_cfg_hw_sriov(bp, &n, true); 13094 } 13095 } 13096 13097 static int bnxt_open(struct net_device *dev) 13098 { 13099 struct bnxt *bp = netdev_priv(dev); 13100 int rc; 13101 13102 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13103 rc = bnxt_reinit_after_abort(bp); 13104 if (rc) { 13105 if (rc == -EBUSY) 13106 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13107 else 13108 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13109 return -ENODEV; 13110 } 13111 } 13112 13113 rc = bnxt_hwrm_if_change(bp, true); 13114 if (rc) 13115 return rc; 13116 13117 rc = __bnxt_open_nic(bp, true, true); 13118 if (rc) { 13119 bnxt_hwrm_if_change(bp, false); 13120 } else { 13121 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13122 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13123 bnxt_queue_sp_work(bp, 13124 BNXT_RESTART_ULP_SP_EVENT); 13125 } 13126 } 13127 13128 return rc; 13129 } 13130 13131 static bool bnxt_drv_busy(struct bnxt *bp) 13132 { 13133 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13134 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13135 } 13136 13137 static void bnxt_get_ring_stats(struct bnxt *bp, 13138 struct rtnl_link_stats64 *stats); 13139 13140 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13141 bool link_re_init) 13142 { 13143 /* Close the VF-reps before closing PF */ 13144 if (BNXT_PF(bp)) 13145 bnxt_vf_reps_close(bp); 13146 13147 /* Change device state to avoid TX queue wake up's */ 13148 bnxt_tx_disable(bp); 13149 13150 clear_bit(BNXT_STATE_OPEN, &bp->state); 13151 smp_mb__after_atomic(); 13152 while (bnxt_drv_busy(bp)) 13153 msleep(20); 13154 13155 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13156 bnxt_clear_rss_ctxs(bp); 13157 /* Flush rings and disable interrupts */ 13158 bnxt_shutdown_nic(bp, irq_re_init); 13159 13160 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13161 13162 bnxt_debug_dev_exit(bp); 13163 bnxt_disable_napi(bp); 13164 timer_delete_sync(&bp->timer); 13165 bnxt_free_skbs(bp); 13166 13167 /* Save ring stats before shutdown */ 13168 if (bp->bnapi && irq_re_init) { 13169 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13170 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 13171 } 13172 if (irq_re_init) { 13173 bnxt_free_irq(bp); 13174 bnxt_del_napi(bp); 13175 } 13176 bnxt_free_mem(bp, irq_re_init); 13177 } 13178 13179 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13180 { 13181 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13182 /* If we get here, it means firmware reset is in progress 13183 * while we are trying to close. We can safely proceed with 13184 * the close because we are holding netdev instance lock. 13185 * Some firmware messages may fail as we proceed to close. 13186 * We set the ABORT_ERR flag here so that the FW reset thread 13187 * will later abort when it gets the netdev instance lock 13188 * and sees the flag. 13189 */ 13190 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13191 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13192 } 13193 13194 #ifdef CONFIG_BNXT_SRIOV 13195 if (bp->sriov_cfg) { 13196 int rc; 13197 13198 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13199 !bp->sriov_cfg, 13200 BNXT_SRIOV_CFG_WAIT_TMO); 13201 if (!rc) 13202 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13203 else if (rc < 0) 13204 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13205 } 13206 #endif 13207 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13208 } 13209 13210 static int bnxt_close(struct net_device *dev) 13211 { 13212 struct bnxt *bp = netdev_priv(dev); 13213 13214 bnxt_close_nic(bp, true, true); 13215 bnxt_hwrm_shutdown_link(bp); 13216 bnxt_hwrm_if_change(bp, false); 13217 return 0; 13218 } 13219 13220 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13221 u16 *val) 13222 { 13223 struct hwrm_port_phy_mdio_read_output *resp; 13224 struct hwrm_port_phy_mdio_read_input *req; 13225 int rc; 13226 13227 if (bp->hwrm_spec_code < 0x10a00) 13228 return -EOPNOTSUPP; 13229 13230 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13231 if (rc) 13232 return rc; 13233 13234 req->port_id = cpu_to_le16(bp->pf.port_id); 13235 req->phy_addr = phy_addr; 13236 req->reg_addr = cpu_to_le16(reg & 0x1f); 13237 if (mdio_phy_id_is_c45(phy_addr)) { 13238 req->cl45_mdio = 1; 13239 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13240 req->dev_addr = mdio_phy_id_devad(phy_addr); 13241 req->reg_addr = cpu_to_le16(reg); 13242 } 13243 13244 resp = hwrm_req_hold(bp, req); 13245 rc = hwrm_req_send(bp, req); 13246 if (!rc) 13247 *val = le16_to_cpu(resp->reg_data); 13248 hwrm_req_drop(bp, req); 13249 return rc; 13250 } 13251 13252 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13253 u16 val) 13254 { 13255 struct hwrm_port_phy_mdio_write_input *req; 13256 int rc; 13257 13258 if (bp->hwrm_spec_code < 0x10a00) 13259 return -EOPNOTSUPP; 13260 13261 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13262 if (rc) 13263 return rc; 13264 13265 req->port_id = cpu_to_le16(bp->pf.port_id); 13266 req->phy_addr = phy_addr; 13267 req->reg_addr = cpu_to_le16(reg & 0x1f); 13268 if (mdio_phy_id_is_c45(phy_addr)) { 13269 req->cl45_mdio = 1; 13270 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13271 req->dev_addr = mdio_phy_id_devad(phy_addr); 13272 req->reg_addr = cpu_to_le16(reg); 13273 } 13274 req->reg_data = cpu_to_le16(val); 13275 13276 return hwrm_req_send(bp, req); 13277 } 13278 13279 /* netdev instance lock held */ 13280 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13281 { 13282 struct mii_ioctl_data *mdio = if_mii(ifr); 13283 struct bnxt *bp = netdev_priv(dev); 13284 int rc; 13285 13286 switch (cmd) { 13287 case SIOCGMIIPHY: 13288 mdio->phy_id = bp->link_info.phy_addr; 13289 13290 fallthrough; 13291 case SIOCGMIIREG: { 13292 u16 mii_regval = 0; 13293 13294 if (!netif_running(dev)) 13295 return -EAGAIN; 13296 13297 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13298 &mii_regval); 13299 mdio->val_out = mii_regval; 13300 return rc; 13301 } 13302 13303 case SIOCSMIIREG: 13304 if (!netif_running(dev)) 13305 return -EAGAIN; 13306 13307 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13308 mdio->val_in); 13309 13310 default: 13311 /* do nothing */ 13312 break; 13313 } 13314 return -EOPNOTSUPP; 13315 } 13316 13317 static void bnxt_get_ring_stats(struct bnxt *bp, 13318 struct rtnl_link_stats64 *stats) 13319 { 13320 int i; 13321 13322 for (i = 0; i < bp->cp_nr_rings; i++) { 13323 struct bnxt_napi *bnapi = bp->bnapi[i]; 13324 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13325 u64 *sw = cpr->stats.sw_stats; 13326 13327 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13328 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13329 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13330 13331 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13332 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13333 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13334 13335 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13336 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13337 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13338 13339 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13340 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13341 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13342 13343 stats->rx_missed_errors += 13344 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13345 13346 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13347 13348 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13349 13350 stats->rx_dropped += 13351 cpr->sw_stats->rx.rx_netpoll_discards + 13352 cpr->sw_stats->rx.rx_oom_discards; 13353 } 13354 } 13355 13356 static void bnxt_add_prev_stats(struct bnxt *bp, 13357 struct rtnl_link_stats64 *stats) 13358 { 13359 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13360 13361 stats->rx_packets += prev_stats->rx_packets; 13362 stats->tx_packets += prev_stats->tx_packets; 13363 stats->rx_bytes += prev_stats->rx_bytes; 13364 stats->tx_bytes += prev_stats->tx_bytes; 13365 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13366 stats->multicast += prev_stats->multicast; 13367 stats->rx_dropped += prev_stats->rx_dropped; 13368 stats->tx_dropped += prev_stats->tx_dropped; 13369 } 13370 13371 static void 13372 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13373 { 13374 struct bnxt *bp = netdev_priv(dev); 13375 13376 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13377 /* Make sure bnxt_close_nic() sees that we are reading stats before 13378 * we check the BNXT_STATE_OPEN flag. 13379 */ 13380 smp_mb__after_atomic(); 13381 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13382 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13383 *stats = bp->net_stats_prev; 13384 return; 13385 } 13386 13387 bnxt_get_ring_stats(bp, stats); 13388 bnxt_add_prev_stats(bp, stats); 13389 13390 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13391 u64 *rx = bp->port_stats.sw_stats; 13392 u64 *tx = bp->port_stats.sw_stats + 13393 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13394 13395 stats->rx_crc_errors = 13396 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13397 stats->rx_frame_errors = 13398 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13399 stats->rx_length_errors = 13400 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13401 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13402 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13403 stats->rx_errors = 13404 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13405 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13406 stats->collisions = 13407 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13408 stats->tx_fifo_errors = 13409 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13410 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13411 } 13412 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13413 } 13414 13415 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 13416 struct bnxt_total_ring_err_stats *stats, 13417 struct bnxt_cp_ring_info *cpr) 13418 { 13419 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13420 u64 *hw_stats = cpr->stats.sw_stats; 13421 13422 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13423 stats->rx_total_resets += sw_stats->rx.rx_resets; 13424 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13425 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13426 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13427 stats->rx_total_ring_discards += 13428 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13429 stats->tx_total_resets += sw_stats->tx.tx_resets; 13430 stats->tx_total_ring_discards += 13431 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13432 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13433 } 13434 13435 void bnxt_get_ring_err_stats(struct bnxt *bp, 13436 struct bnxt_total_ring_err_stats *stats) 13437 { 13438 int i; 13439 13440 for (i = 0; i < bp->cp_nr_rings; i++) 13441 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13442 } 13443 13444 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 13445 { 13446 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13447 struct net_device *dev = bp->dev; 13448 struct netdev_hw_addr *ha; 13449 u8 *haddr; 13450 int mc_count = 0; 13451 bool update = false; 13452 int off = 0; 13453 13454 netdev_for_each_mc_addr(ha, dev) { 13455 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13456 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13457 vnic->mc_list_count = 0; 13458 return false; 13459 } 13460 haddr = ha->addr; 13461 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13462 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13463 update = true; 13464 } 13465 off += ETH_ALEN; 13466 mc_count++; 13467 } 13468 if (mc_count) 13469 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13470 13471 if (mc_count != vnic->mc_list_count) { 13472 vnic->mc_list_count = mc_count; 13473 update = true; 13474 } 13475 return update; 13476 } 13477 13478 static bool bnxt_uc_list_updated(struct bnxt *bp) 13479 { 13480 struct net_device *dev = bp->dev; 13481 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13482 struct netdev_hw_addr *ha; 13483 int off = 0; 13484 13485 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13486 return true; 13487 13488 netdev_for_each_uc_addr(ha, dev) { 13489 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13490 return true; 13491 13492 off += ETH_ALEN; 13493 } 13494 return false; 13495 } 13496 13497 static void bnxt_set_rx_mode(struct net_device *dev) 13498 { 13499 struct bnxt *bp = netdev_priv(dev); 13500 struct bnxt_vnic_info *vnic; 13501 bool mc_update = false; 13502 bool uc_update; 13503 u32 mask; 13504 13505 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13506 return; 13507 13508 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13509 mask = vnic->rx_mask; 13510 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13511 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13512 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13513 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13514 13515 if (dev->flags & IFF_PROMISC) 13516 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13517 13518 uc_update = bnxt_uc_list_updated(bp); 13519 13520 if (dev->flags & IFF_BROADCAST) 13521 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13522 if (dev->flags & IFF_ALLMULTI) { 13523 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13524 vnic->mc_list_count = 0; 13525 } else if (dev->flags & IFF_MULTICAST) { 13526 mc_update = bnxt_mc_list_updated(bp, &mask); 13527 } 13528 13529 if (mask != vnic->rx_mask || uc_update || mc_update) { 13530 vnic->rx_mask = mask; 13531 13532 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13533 } 13534 } 13535 13536 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13537 { 13538 struct net_device *dev = bp->dev; 13539 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13540 struct netdev_hw_addr *ha; 13541 int i, off = 0, rc; 13542 bool uc_update; 13543 13544 netif_addr_lock_bh(dev); 13545 uc_update = bnxt_uc_list_updated(bp); 13546 netif_addr_unlock_bh(dev); 13547 13548 if (!uc_update) 13549 goto skip_uc; 13550 13551 for (i = 1; i < vnic->uc_filter_count; i++) { 13552 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13553 13554 bnxt_hwrm_l2_filter_free(bp, fltr); 13555 bnxt_del_l2_filter(bp, fltr); 13556 } 13557 13558 vnic->uc_filter_count = 1; 13559 13560 netif_addr_lock_bh(dev); 13561 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13562 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13563 } else { 13564 netdev_for_each_uc_addr(ha, dev) { 13565 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13566 off += ETH_ALEN; 13567 vnic->uc_filter_count++; 13568 } 13569 } 13570 netif_addr_unlock_bh(dev); 13571 13572 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13573 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13574 if (rc) { 13575 if (BNXT_VF(bp) && rc == -ENODEV) { 13576 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13577 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13578 else 13579 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13580 rc = 0; 13581 } else { 13582 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13583 } 13584 vnic->uc_filter_count = i; 13585 return rc; 13586 } 13587 } 13588 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13589 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13590 13591 skip_uc: 13592 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13593 !bnxt_promisc_ok(bp)) 13594 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13595 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13596 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13597 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13598 rc); 13599 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13600 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13601 vnic->mc_list_count = 0; 13602 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13603 } 13604 if (rc) 13605 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13606 rc); 13607 13608 return rc; 13609 } 13610 13611 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13612 { 13613 #ifdef CONFIG_BNXT_SRIOV 13614 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13615 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13616 13617 /* No minimum rings were provisioned by the PF. Don't 13618 * reserve rings by default when device is down. 13619 */ 13620 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13621 return true; 13622 13623 if (!netif_running(bp->dev)) 13624 return false; 13625 } 13626 #endif 13627 return true; 13628 } 13629 13630 /* If the chip and firmware supports RFS */ 13631 static bool bnxt_rfs_supported(struct bnxt *bp) 13632 { 13633 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13634 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13635 return true; 13636 return false; 13637 } 13638 /* 212 firmware is broken for aRFS */ 13639 if (BNXT_FW_MAJ(bp) == 212) 13640 return false; 13641 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13642 return true; 13643 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13644 return true; 13645 return false; 13646 } 13647 13648 /* If runtime conditions support RFS */ 13649 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13650 { 13651 struct bnxt_hw_rings hwr = {0}; 13652 int max_vnics, max_rss_ctxs; 13653 13654 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13655 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13656 return bnxt_rfs_supported(bp); 13657 13658 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13659 return false; 13660 13661 hwr.grp = bp->rx_nr_rings; 13662 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13663 if (new_rss_ctx) 13664 hwr.vnic++; 13665 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13666 max_vnics = bnxt_get_max_func_vnics(bp); 13667 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13668 13669 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13670 if (bp->rx_nr_rings > 1) 13671 netdev_warn(bp->dev, 13672 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13673 min(max_rss_ctxs - 1, max_vnics - 1)); 13674 return false; 13675 } 13676 13677 if (!BNXT_NEW_RM(bp)) 13678 return true; 13679 13680 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13681 * issue that will mess up the default VNIC if we reduce the 13682 * reservations. 13683 */ 13684 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13685 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13686 return true; 13687 13688 bnxt_hwrm_reserve_rings(bp, &hwr); 13689 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13690 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13691 return true; 13692 13693 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13694 hwr.vnic = 1; 13695 hwr.rss_ctx = 0; 13696 bnxt_hwrm_reserve_rings(bp, &hwr); 13697 return false; 13698 } 13699 13700 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13701 netdev_features_t features) 13702 { 13703 struct bnxt *bp = netdev_priv(dev); 13704 netdev_features_t vlan_features; 13705 13706 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13707 features &= ~NETIF_F_NTUPLE; 13708 13709 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13710 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13711 13712 if (!(features & NETIF_F_GRO)) 13713 features &= ~NETIF_F_GRO_HW; 13714 13715 if (features & NETIF_F_GRO_HW) 13716 features &= ~NETIF_F_LRO; 13717 13718 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13719 * turned on or off together. 13720 */ 13721 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13722 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13723 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13724 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13725 else if (vlan_features) 13726 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13727 } 13728 #ifdef CONFIG_BNXT_SRIOV 13729 if (BNXT_VF(bp) && bp->vf.vlan) 13730 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13731 #endif 13732 return features; 13733 } 13734 13735 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13736 bool link_re_init, u32 flags, bool update_tpa) 13737 { 13738 bnxt_close_nic(bp, irq_re_init, link_re_init); 13739 bp->flags = flags; 13740 if (update_tpa) 13741 bnxt_set_ring_params(bp); 13742 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13743 } 13744 13745 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13746 { 13747 bool update_tpa = false, update_ntuple = false; 13748 struct bnxt *bp = netdev_priv(dev); 13749 u32 flags = bp->flags; 13750 u32 changes; 13751 int rc = 0; 13752 bool re_init = false; 13753 13754 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13755 if (features & NETIF_F_GRO_HW) 13756 flags |= BNXT_FLAG_GRO; 13757 else if (features & NETIF_F_LRO) 13758 flags |= BNXT_FLAG_LRO; 13759 13760 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13761 flags &= ~BNXT_FLAG_TPA; 13762 13763 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13764 flags |= BNXT_FLAG_STRIP_VLAN; 13765 13766 if (features & NETIF_F_NTUPLE) 13767 flags |= BNXT_FLAG_RFS; 13768 else 13769 bnxt_clear_usr_fltrs(bp, true); 13770 13771 changes = flags ^ bp->flags; 13772 if (changes & BNXT_FLAG_TPA) { 13773 update_tpa = true; 13774 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13775 (flags & BNXT_FLAG_TPA) == 0 || 13776 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13777 re_init = true; 13778 } 13779 13780 if (changes & ~BNXT_FLAG_TPA) 13781 re_init = true; 13782 13783 if (changes & BNXT_FLAG_RFS) 13784 update_ntuple = true; 13785 13786 if (flags != bp->flags) { 13787 u32 old_flags = bp->flags; 13788 13789 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13790 bp->flags = flags; 13791 if (update_tpa) 13792 bnxt_set_ring_params(bp); 13793 return rc; 13794 } 13795 13796 if (update_ntuple) 13797 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13798 13799 if (re_init) 13800 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13801 13802 if (update_tpa) { 13803 bp->flags = flags; 13804 rc = bnxt_set_tpa(bp, 13805 (flags & BNXT_FLAG_TPA) ? 13806 true : false); 13807 if (rc) 13808 bp->flags = old_flags; 13809 } 13810 } 13811 return rc; 13812 } 13813 13814 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13815 u8 **nextp) 13816 { 13817 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13818 struct hop_jumbo_hdr *jhdr; 13819 int hdr_count = 0; 13820 u8 *nexthdr; 13821 int start; 13822 13823 /* Check that there are at most 2 IPv6 extension headers, no 13824 * fragment header, and each is <= 64 bytes. 13825 */ 13826 start = nw_off + sizeof(*ip6h); 13827 nexthdr = &ip6h->nexthdr; 13828 while (ipv6_ext_hdr(*nexthdr)) { 13829 struct ipv6_opt_hdr *hp; 13830 int hdrlen; 13831 13832 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13833 *nexthdr == NEXTHDR_FRAGMENT) 13834 return false; 13835 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13836 skb_headlen(skb), NULL); 13837 if (!hp) 13838 return false; 13839 if (*nexthdr == NEXTHDR_AUTH) 13840 hdrlen = ipv6_authlen(hp); 13841 else 13842 hdrlen = ipv6_optlen(hp); 13843 13844 if (hdrlen > 64) 13845 return false; 13846 13847 /* The ext header may be a hop-by-hop header inserted for 13848 * big TCP purposes. This will be removed before sending 13849 * from NIC, so do not count it. 13850 */ 13851 if (*nexthdr == NEXTHDR_HOP) { 13852 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13853 goto increment_hdr; 13854 13855 jhdr = (struct hop_jumbo_hdr *)hp; 13856 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13857 jhdr->nexthdr != IPPROTO_TCP) 13858 goto increment_hdr; 13859 13860 goto next_hdr; 13861 } 13862 increment_hdr: 13863 hdr_count++; 13864 next_hdr: 13865 nexthdr = &hp->nexthdr; 13866 start += hdrlen; 13867 } 13868 if (nextp) { 13869 /* Caller will check inner protocol */ 13870 if (skb->encapsulation) { 13871 *nextp = nexthdr; 13872 return true; 13873 } 13874 *nextp = NULL; 13875 } 13876 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13877 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13878 } 13879 13880 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13881 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13882 { 13883 struct udphdr *uh = udp_hdr(skb); 13884 __be16 udp_port = uh->dest; 13885 13886 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13887 udp_port != bp->vxlan_gpe_port) 13888 return false; 13889 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13890 struct ethhdr *eh = inner_eth_hdr(skb); 13891 13892 switch (eh->h_proto) { 13893 case htons(ETH_P_IP): 13894 return true; 13895 case htons(ETH_P_IPV6): 13896 return bnxt_exthdr_check(bp, skb, 13897 skb_inner_network_offset(skb), 13898 NULL); 13899 } 13900 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13901 return true; 13902 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13903 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13904 NULL); 13905 } 13906 return false; 13907 } 13908 13909 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13910 { 13911 switch (l4_proto) { 13912 case IPPROTO_UDP: 13913 return bnxt_udp_tunl_check(bp, skb); 13914 case IPPROTO_IPIP: 13915 return true; 13916 case IPPROTO_GRE: { 13917 switch (skb->inner_protocol) { 13918 default: 13919 return false; 13920 case htons(ETH_P_IP): 13921 return true; 13922 case htons(ETH_P_IPV6): 13923 fallthrough; 13924 } 13925 } 13926 case IPPROTO_IPV6: 13927 /* Check ext headers of inner ipv6 */ 13928 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13929 NULL); 13930 } 13931 return false; 13932 } 13933 13934 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13935 struct net_device *dev, 13936 netdev_features_t features) 13937 { 13938 struct bnxt *bp = netdev_priv(dev); 13939 u8 *l4_proto; 13940 13941 features = vlan_features_check(skb, features); 13942 switch (vlan_get_protocol(skb)) { 13943 case htons(ETH_P_IP): 13944 if (!skb->encapsulation) 13945 return features; 13946 l4_proto = &ip_hdr(skb)->protocol; 13947 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13948 return features; 13949 break; 13950 case htons(ETH_P_IPV6): 13951 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13952 &l4_proto)) 13953 break; 13954 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13955 return features; 13956 break; 13957 } 13958 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13959 } 13960 13961 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13962 u32 *reg_buf) 13963 { 13964 struct hwrm_dbg_read_direct_output *resp; 13965 struct hwrm_dbg_read_direct_input *req; 13966 __le32 *dbg_reg_buf; 13967 dma_addr_t mapping; 13968 int rc, i; 13969 13970 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13971 if (rc) 13972 return rc; 13973 13974 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13975 &mapping); 13976 if (!dbg_reg_buf) { 13977 rc = -ENOMEM; 13978 goto dbg_rd_reg_exit; 13979 } 13980 13981 req->host_dest_addr = cpu_to_le64(mapping); 13982 13983 resp = hwrm_req_hold(bp, req); 13984 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13985 req->read_len32 = cpu_to_le32(num_words); 13986 13987 rc = hwrm_req_send(bp, req); 13988 if (rc || resp->error_code) { 13989 rc = -EIO; 13990 goto dbg_rd_reg_exit; 13991 } 13992 for (i = 0; i < num_words; i++) 13993 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13994 13995 dbg_rd_reg_exit: 13996 hwrm_req_drop(bp, req); 13997 return rc; 13998 } 13999 14000 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 14001 u32 ring_id, u32 *prod, u32 *cons) 14002 { 14003 struct hwrm_dbg_ring_info_get_output *resp; 14004 struct hwrm_dbg_ring_info_get_input *req; 14005 int rc; 14006 14007 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 14008 if (rc) 14009 return rc; 14010 14011 req->ring_type = ring_type; 14012 req->fw_ring_id = cpu_to_le32(ring_id); 14013 resp = hwrm_req_hold(bp, req); 14014 rc = hwrm_req_send(bp, req); 14015 if (!rc) { 14016 *prod = le32_to_cpu(resp->producer_index); 14017 *cons = le32_to_cpu(resp->consumer_index); 14018 } 14019 hwrm_req_drop(bp, req); 14020 return rc; 14021 } 14022 14023 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 14024 { 14025 struct bnxt_tx_ring_info *txr; 14026 int i = bnapi->index, j; 14027 14028 bnxt_for_each_napi_tx(j, bnapi, txr) 14029 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 14030 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 14031 txr->tx_cons); 14032 } 14033 14034 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 14035 { 14036 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 14037 int i = bnapi->index; 14038 14039 if (!rxr) 14040 return; 14041 14042 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", 14043 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 14044 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 14045 rxr->rx_sw_agg_prod); 14046 } 14047 14048 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 14049 { 14050 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2; 14051 int i = bnapi->index, j; 14052 14053 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 14054 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 14055 for (j = 0; j < cpr->cp_ring_count; j++) { 14056 cpr2 = &cpr->cp_ring_arr[j]; 14057 if (!cpr2->bnapi) 14058 continue; 14059 netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n", 14060 i, j, cpr2->cp_ring_struct.fw_ring_id, 14061 cpr2->cp_raw_cons); 14062 } 14063 } 14064 14065 static void bnxt_dbg_dump_states(struct bnxt *bp) 14066 { 14067 int i; 14068 struct bnxt_napi *bnapi; 14069 14070 for (i = 0; i < bp->cp_nr_rings; i++) { 14071 bnapi = bp->bnapi[i]; 14072 if (netif_msg_drv(bp)) { 14073 bnxt_dump_tx_sw_state(bnapi); 14074 bnxt_dump_rx_sw_state(bnapi); 14075 bnxt_dump_cp_sw_state(bnapi); 14076 } 14077 } 14078 } 14079 14080 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 14081 { 14082 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 14083 struct hwrm_ring_reset_input *req; 14084 struct bnxt_napi *bnapi = rxr->bnapi; 14085 struct bnxt_cp_ring_info *cpr; 14086 u16 cp_ring_id; 14087 int rc; 14088 14089 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 14090 if (rc) 14091 return rc; 14092 14093 cpr = &bnapi->cp_ring; 14094 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14095 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14096 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14097 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14098 return hwrm_req_send_silent(bp, req); 14099 } 14100 14101 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14102 { 14103 if (!silent) 14104 bnxt_dbg_dump_states(bp); 14105 if (netif_running(bp->dev)) { 14106 bnxt_close_nic(bp, !silent, false); 14107 bnxt_open_nic(bp, !silent, false); 14108 } 14109 } 14110 14111 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14112 { 14113 struct bnxt *bp = netdev_priv(dev); 14114 14115 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14116 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14117 } 14118 14119 static void bnxt_fw_health_check(struct bnxt *bp) 14120 { 14121 struct bnxt_fw_health *fw_health = bp->fw_health; 14122 struct pci_dev *pdev = bp->pdev; 14123 u32 val; 14124 14125 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14126 return; 14127 14128 /* Make sure it is enabled before checking the tmr_counter. */ 14129 smp_rmb(); 14130 if (fw_health->tmr_counter) { 14131 fw_health->tmr_counter--; 14132 return; 14133 } 14134 14135 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14136 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14137 fw_health->arrests++; 14138 goto fw_reset; 14139 } 14140 14141 fw_health->last_fw_heartbeat = val; 14142 14143 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14144 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14145 fw_health->discoveries++; 14146 goto fw_reset; 14147 } 14148 14149 fw_health->tmr_counter = fw_health->tmr_multiplier; 14150 return; 14151 14152 fw_reset: 14153 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14154 } 14155 14156 static void bnxt_timer(struct timer_list *t) 14157 { 14158 struct bnxt *bp = timer_container_of(bp, t, timer); 14159 struct net_device *dev = bp->dev; 14160 14161 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14162 return; 14163 14164 if (atomic_read(&bp->intr_sem) != 0) 14165 goto bnxt_restart_timer; 14166 14167 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14168 bnxt_fw_health_check(bp); 14169 14170 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14171 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14172 14173 if (bnxt_tc_flower_enabled(bp)) 14174 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14175 14176 #ifdef CONFIG_RFS_ACCEL 14177 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14178 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14179 #endif /*CONFIG_RFS_ACCEL*/ 14180 14181 if (bp->link_info.phy_retry) { 14182 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14183 bp->link_info.phy_retry = false; 14184 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14185 } else { 14186 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14187 } 14188 } 14189 14190 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 14191 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 14192 14193 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14194 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14195 14196 bnxt_restart_timer: 14197 mod_timer(&bp->timer, jiffies + bp->current_interval); 14198 } 14199 14200 static void bnxt_lock_sp(struct bnxt *bp) 14201 { 14202 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14203 * set. If the device is being closed, bnxt_close() may be holding 14204 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14205 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14206 * instance lock. 14207 */ 14208 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14209 netdev_lock(bp->dev); 14210 } 14211 14212 static void bnxt_unlock_sp(struct bnxt *bp) 14213 { 14214 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14215 netdev_unlock(bp->dev); 14216 } 14217 14218 /* Only called from bnxt_sp_task() */ 14219 static void bnxt_reset(struct bnxt *bp, bool silent) 14220 { 14221 bnxt_lock_sp(bp); 14222 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14223 bnxt_reset_task(bp, silent); 14224 bnxt_unlock_sp(bp); 14225 } 14226 14227 /* Only called from bnxt_sp_task() */ 14228 static void bnxt_rx_ring_reset(struct bnxt *bp) 14229 { 14230 int i; 14231 14232 bnxt_lock_sp(bp); 14233 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14234 bnxt_unlock_sp(bp); 14235 return; 14236 } 14237 /* Disable and flush TPA before resetting the RX ring */ 14238 if (bp->flags & BNXT_FLAG_TPA) 14239 bnxt_set_tpa(bp, false); 14240 for (i = 0; i < bp->rx_nr_rings; i++) { 14241 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14242 struct bnxt_cp_ring_info *cpr; 14243 int rc; 14244 14245 if (!rxr->bnapi->in_reset) 14246 continue; 14247 14248 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14249 if (rc) { 14250 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14251 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14252 else 14253 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14254 rc); 14255 bnxt_reset_task(bp, true); 14256 break; 14257 } 14258 bnxt_free_one_rx_ring_skbs(bp, rxr); 14259 rxr->rx_prod = 0; 14260 rxr->rx_agg_prod = 0; 14261 rxr->rx_sw_agg_prod = 0; 14262 rxr->rx_next_cons = 0; 14263 rxr->bnapi->in_reset = false; 14264 bnxt_alloc_one_rx_ring(bp, i); 14265 cpr = &rxr->bnapi->cp_ring; 14266 cpr->sw_stats->rx.rx_resets++; 14267 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14268 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14269 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14270 } 14271 if (bp->flags & BNXT_FLAG_TPA) 14272 bnxt_set_tpa(bp, true); 14273 bnxt_unlock_sp(bp); 14274 } 14275 14276 static void bnxt_fw_fatal_close(struct bnxt *bp) 14277 { 14278 bnxt_tx_disable(bp); 14279 bnxt_disable_napi(bp); 14280 bnxt_disable_int_sync(bp); 14281 bnxt_free_irq(bp); 14282 bnxt_clear_int_mode(bp); 14283 pci_disable_device(bp->pdev); 14284 } 14285 14286 static void bnxt_fw_reset_close(struct bnxt *bp) 14287 { 14288 /* When firmware is in fatal state, quiesce device and disable 14289 * bus master to prevent any potential bad DMAs before freeing 14290 * kernel memory. 14291 */ 14292 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14293 u16 val = 0; 14294 14295 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14296 if (val == 0xffff) 14297 bp->fw_reset_min_dsecs = 0; 14298 bnxt_fw_fatal_close(bp); 14299 } 14300 __bnxt_close_nic(bp, true, false); 14301 bnxt_vf_reps_free(bp); 14302 bnxt_clear_int_mode(bp); 14303 bnxt_hwrm_func_drv_unrgtr(bp); 14304 if (pci_is_enabled(bp->pdev)) 14305 pci_disable_device(bp->pdev); 14306 bnxt_free_ctx_mem(bp, false); 14307 } 14308 14309 static bool is_bnxt_fw_ok(struct bnxt *bp) 14310 { 14311 struct bnxt_fw_health *fw_health = bp->fw_health; 14312 bool no_heartbeat = false, has_reset = false; 14313 u32 val; 14314 14315 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14316 if (val == fw_health->last_fw_heartbeat) 14317 no_heartbeat = true; 14318 14319 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14320 if (val != fw_health->last_fw_reset_cnt) 14321 has_reset = true; 14322 14323 if (!no_heartbeat && has_reset) 14324 return true; 14325 14326 return false; 14327 } 14328 14329 /* netdev instance lock is acquired before calling this function */ 14330 static void bnxt_force_fw_reset(struct bnxt *bp) 14331 { 14332 struct bnxt_fw_health *fw_health = bp->fw_health; 14333 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14334 u32 wait_dsecs; 14335 14336 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14337 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14338 return; 14339 14340 /* we have to serialize with bnxt_refclk_read()*/ 14341 if (ptp) { 14342 unsigned long flags; 14343 14344 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14345 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14346 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14347 } else { 14348 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14349 } 14350 bnxt_fw_reset_close(bp); 14351 wait_dsecs = fw_health->master_func_wait_dsecs; 14352 if (fw_health->primary) { 14353 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14354 wait_dsecs = 0; 14355 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14356 } else { 14357 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14358 wait_dsecs = fw_health->normal_func_wait_dsecs; 14359 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14360 } 14361 14362 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14363 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14364 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14365 } 14366 14367 void bnxt_fw_exception(struct bnxt *bp) 14368 { 14369 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14370 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14371 bnxt_ulp_stop(bp); 14372 bnxt_lock_sp(bp); 14373 bnxt_force_fw_reset(bp); 14374 bnxt_unlock_sp(bp); 14375 } 14376 14377 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14378 * < 0 on error. 14379 */ 14380 static int bnxt_get_registered_vfs(struct bnxt *bp) 14381 { 14382 #ifdef CONFIG_BNXT_SRIOV 14383 int rc; 14384 14385 if (!BNXT_PF(bp)) 14386 return 0; 14387 14388 rc = bnxt_hwrm_func_qcfg(bp); 14389 if (rc) { 14390 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14391 return rc; 14392 } 14393 if (bp->pf.registered_vfs) 14394 return bp->pf.registered_vfs; 14395 if (bp->sriov_cfg) 14396 return 1; 14397 #endif 14398 return 0; 14399 } 14400 14401 void bnxt_fw_reset(struct bnxt *bp) 14402 { 14403 bnxt_ulp_stop(bp); 14404 bnxt_lock_sp(bp); 14405 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14406 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14407 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14408 int n = 0, tmo; 14409 14410 /* we have to serialize with bnxt_refclk_read()*/ 14411 if (ptp) { 14412 unsigned long flags; 14413 14414 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14415 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14416 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14417 } else { 14418 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14419 } 14420 if (bp->pf.active_vfs && 14421 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14422 n = bnxt_get_registered_vfs(bp); 14423 if (n < 0) { 14424 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14425 n); 14426 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14427 netif_close(bp->dev); 14428 goto fw_reset_exit; 14429 } else if (n > 0) { 14430 u16 vf_tmo_dsecs = n * 10; 14431 14432 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14433 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14434 bp->fw_reset_state = 14435 BNXT_FW_RESET_STATE_POLL_VF; 14436 bnxt_queue_fw_reset_work(bp, HZ / 10); 14437 goto fw_reset_exit; 14438 } 14439 bnxt_fw_reset_close(bp); 14440 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14441 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14442 tmo = HZ / 10; 14443 } else { 14444 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14445 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14446 } 14447 bnxt_queue_fw_reset_work(bp, tmo); 14448 } 14449 fw_reset_exit: 14450 bnxt_unlock_sp(bp); 14451 } 14452 14453 static void bnxt_chk_missed_irq(struct bnxt *bp) 14454 { 14455 int i; 14456 14457 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14458 return; 14459 14460 for (i = 0; i < bp->cp_nr_rings; i++) { 14461 struct bnxt_napi *bnapi = bp->bnapi[i]; 14462 struct bnxt_cp_ring_info *cpr; 14463 u32 fw_ring_id; 14464 int j; 14465 14466 if (!bnapi) 14467 continue; 14468 14469 cpr = &bnapi->cp_ring; 14470 for (j = 0; j < cpr->cp_ring_count; j++) { 14471 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14472 u32 val[2]; 14473 14474 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14475 continue; 14476 14477 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14478 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14479 continue; 14480 } 14481 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14482 bnxt_dbg_hwrm_ring_info_get(bp, 14483 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14484 fw_ring_id, &val[0], &val[1]); 14485 cpr->sw_stats->cmn.missed_irqs++; 14486 } 14487 } 14488 } 14489 14490 static void bnxt_cfg_ntp_filters(struct bnxt *); 14491 14492 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14493 { 14494 struct bnxt_link_info *link_info = &bp->link_info; 14495 14496 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14497 link_info->autoneg = BNXT_AUTONEG_SPEED; 14498 if (bp->hwrm_spec_code >= 0x10201) { 14499 if (link_info->auto_pause_setting & 14500 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14501 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14502 } else { 14503 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14504 } 14505 bnxt_set_auto_speed(link_info); 14506 } else { 14507 bnxt_set_force_speed(link_info); 14508 link_info->req_duplex = link_info->duplex_setting; 14509 } 14510 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14511 link_info->req_flow_ctrl = 14512 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14513 else 14514 link_info->req_flow_ctrl = link_info->force_pause_setting; 14515 } 14516 14517 static void bnxt_fw_echo_reply(struct bnxt *bp) 14518 { 14519 struct bnxt_fw_health *fw_health = bp->fw_health; 14520 struct hwrm_func_echo_response_input *req; 14521 int rc; 14522 14523 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14524 if (rc) 14525 return; 14526 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14527 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14528 hwrm_req_send(bp, req); 14529 } 14530 14531 static void bnxt_ulp_restart(struct bnxt *bp) 14532 { 14533 bnxt_ulp_stop(bp); 14534 bnxt_ulp_start(bp, 0); 14535 } 14536 14537 static void bnxt_sp_task(struct work_struct *work) 14538 { 14539 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14540 14541 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14542 smp_mb__after_atomic(); 14543 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14544 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14545 return; 14546 } 14547 14548 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14549 bnxt_ulp_restart(bp); 14550 bnxt_reenable_sriov(bp); 14551 } 14552 14553 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14554 bnxt_cfg_rx_mode(bp); 14555 14556 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14557 bnxt_cfg_ntp_filters(bp); 14558 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14559 bnxt_hwrm_exec_fwd_req(bp); 14560 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14561 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14562 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14563 bnxt_hwrm_port_qstats(bp, 0); 14564 bnxt_hwrm_port_qstats_ext(bp, 0); 14565 bnxt_accumulate_all_stats(bp); 14566 } 14567 14568 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14569 int rc; 14570 14571 mutex_lock(&bp->link_lock); 14572 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14573 &bp->sp_event)) 14574 bnxt_hwrm_phy_qcaps(bp); 14575 14576 rc = bnxt_update_link(bp, true); 14577 if (rc) 14578 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14579 rc); 14580 14581 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14582 &bp->sp_event)) 14583 bnxt_init_ethtool_link_settings(bp); 14584 mutex_unlock(&bp->link_lock); 14585 } 14586 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14587 int rc; 14588 14589 mutex_lock(&bp->link_lock); 14590 rc = bnxt_update_phy_setting(bp); 14591 mutex_unlock(&bp->link_lock); 14592 if (rc) { 14593 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14594 } else { 14595 bp->link_info.phy_retry = false; 14596 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14597 } 14598 } 14599 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14600 mutex_lock(&bp->link_lock); 14601 bnxt_get_port_module_status(bp); 14602 mutex_unlock(&bp->link_lock); 14603 } 14604 14605 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14606 bnxt_tc_flow_stats_work(bp); 14607 14608 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14609 bnxt_chk_missed_irq(bp); 14610 14611 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14612 bnxt_fw_echo_reply(bp); 14613 14614 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14615 bnxt_hwmon_notify_event(bp); 14616 14617 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14618 * must be the last functions to be called before exiting. 14619 */ 14620 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14621 bnxt_reset(bp, false); 14622 14623 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14624 bnxt_reset(bp, true); 14625 14626 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14627 bnxt_rx_ring_reset(bp); 14628 14629 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14630 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14631 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14632 bnxt_devlink_health_fw_report(bp); 14633 else 14634 bnxt_fw_reset(bp); 14635 } 14636 14637 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14638 if (!is_bnxt_fw_ok(bp)) 14639 bnxt_devlink_health_fw_report(bp); 14640 } 14641 14642 smp_mb__before_atomic(); 14643 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14644 } 14645 14646 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14647 int *max_cp); 14648 14649 /* Under netdev instance lock */ 14650 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14651 int tx_xdp) 14652 { 14653 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14654 struct bnxt_hw_rings hwr = {0}; 14655 int rx_rings = rx; 14656 int rc; 14657 14658 if (tcs) 14659 tx_sets = tcs; 14660 14661 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14662 14663 if (max_rx < rx_rings) 14664 return -ENOMEM; 14665 14666 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14667 rx_rings <<= 1; 14668 14669 hwr.rx = rx_rings; 14670 hwr.tx = tx * tx_sets + tx_xdp; 14671 if (max_tx < hwr.tx) 14672 return -ENOMEM; 14673 14674 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14675 14676 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14677 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14678 if (max_cp < hwr.cp) 14679 return -ENOMEM; 14680 hwr.stat = hwr.cp; 14681 if (BNXT_NEW_RM(bp)) { 14682 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14683 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14684 hwr.grp = rx; 14685 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14686 } 14687 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14688 hwr.cp_p5 = hwr.tx + rx; 14689 rc = bnxt_hwrm_check_rings(bp, &hwr); 14690 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14691 if (!bnxt_ulp_registered(bp->edev)) { 14692 hwr.cp += bnxt_get_ulp_msix_num(bp); 14693 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14694 } 14695 if (hwr.cp > bp->total_irqs) { 14696 int total_msix = bnxt_change_msix(bp, hwr.cp); 14697 14698 if (total_msix < hwr.cp) { 14699 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14700 hwr.cp, total_msix); 14701 rc = -ENOSPC; 14702 } 14703 } 14704 } 14705 return rc; 14706 } 14707 14708 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14709 { 14710 if (bp->bar2) { 14711 pci_iounmap(pdev, bp->bar2); 14712 bp->bar2 = NULL; 14713 } 14714 14715 if (bp->bar1) { 14716 pci_iounmap(pdev, bp->bar1); 14717 bp->bar1 = NULL; 14718 } 14719 14720 if (bp->bar0) { 14721 pci_iounmap(pdev, bp->bar0); 14722 bp->bar0 = NULL; 14723 } 14724 } 14725 14726 static void bnxt_cleanup_pci(struct bnxt *bp) 14727 { 14728 bnxt_unmap_bars(bp, bp->pdev); 14729 pci_release_regions(bp->pdev); 14730 if (pci_is_enabled(bp->pdev)) 14731 pci_disable_device(bp->pdev); 14732 } 14733 14734 static void bnxt_init_dflt_coal(struct bnxt *bp) 14735 { 14736 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14737 struct bnxt_coal *coal; 14738 u16 flags = 0; 14739 14740 if (coal_cap->cmpl_params & 14741 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14742 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14743 14744 /* Tick values in micro seconds. 14745 * 1 coal_buf x bufs_per_record = 1 completion record. 14746 */ 14747 coal = &bp->rx_coal; 14748 coal->coal_ticks = 10; 14749 coal->coal_bufs = 30; 14750 coal->coal_ticks_irq = 1; 14751 coal->coal_bufs_irq = 2; 14752 coal->idle_thresh = 50; 14753 coal->bufs_per_record = 2; 14754 coal->budget = 64; /* NAPI budget */ 14755 coal->flags = flags; 14756 14757 coal = &bp->tx_coal; 14758 coal->coal_ticks = 28; 14759 coal->coal_bufs = 30; 14760 coal->coal_ticks_irq = 2; 14761 coal->coal_bufs_irq = 2; 14762 coal->bufs_per_record = 1; 14763 coal->flags = flags; 14764 14765 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14766 } 14767 14768 /* FW that pre-reserves 1 VNIC per function */ 14769 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14770 { 14771 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14772 14773 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14774 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14775 return true; 14776 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14777 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14778 return true; 14779 return false; 14780 } 14781 14782 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp) 14783 { 14784 struct hwrm_queue_pfcwd_timeout_qcaps_output *resp; 14785 struct hwrm_queue_pfcwd_timeout_qcaps_input *req; 14786 int rc; 14787 14788 bp->max_pfcwd_tmo_ms = 0; 14789 rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS); 14790 if (rc) 14791 return; 14792 resp = hwrm_req_hold(bp, req); 14793 rc = hwrm_req_send_silent(bp, req); 14794 if (!rc) 14795 bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout); 14796 hwrm_req_drop(bp, req); 14797 } 14798 14799 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14800 { 14801 int rc; 14802 14803 bp->fw_cap = 0; 14804 rc = bnxt_hwrm_ver_get(bp); 14805 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14806 * so wait before continuing with recovery. 14807 */ 14808 if (rc) 14809 msleep(100); 14810 bnxt_try_map_fw_health_reg(bp); 14811 if (rc) { 14812 rc = bnxt_try_recover_fw(bp); 14813 if (rc) 14814 return rc; 14815 rc = bnxt_hwrm_ver_get(bp); 14816 if (rc) 14817 return rc; 14818 } 14819 14820 bnxt_nvm_cfg_ver_get(bp); 14821 14822 rc = bnxt_hwrm_func_reset(bp); 14823 if (rc) 14824 return -ENODEV; 14825 14826 bnxt_hwrm_fw_set_time(bp); 14827 return 0; 14828 } 14829 14830 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14831 { 14832 int rc; 14833 14834 /* Get the MAX capabilities for this function */ 14835 rc = bnxt_hwrm_func_qcaps(bp); 14836 if (rc) { 14837 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14838 rc); 14839 return -ENODEV; 14840 } 14841 14842 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14843 if (rc) 14844 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14845 rc); 14846 14847 if (bnxt_alloc_fw_health(bp)) { 14848 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14849 } else { 14850 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14851 if (rc) 14852 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14853 rc); 14854 } 14855 14856 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14857 if (rc) 14858 return -ENODEV; 14859 14860 rc = bnxt_alloc_crash_dump_mem(bp); 14861 if (rc) 14862 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14863 rc); 14864 if (!rc) { 14865 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14866 if (rc) { 14867 bnxt_free_crash_dump_mem(bp); 14868 netdev_warn(bp->dev, 14869 "hwrm crash dump mem failure rc: %d\n", rc); 14870 } 14871 } 14872 14873 if (bnxt_fw_pre_resv_vnics(bp)) 14874 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14875 14876 bnxt_hwrm_pfcwd_qcaps(bp); 14877 bnxt_hwrm_func_qcfg(bp); 14878 bnxt_hwrm_vnic_qcaps(bp); 14879 bnxt_hwrm_port_led_qcaps(bp); 14880 bnxt_ethtool_init(bp); 14881 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14882 __bnxt_hwrm_ptp_qcfg(bp); 14883 bnxt_dcb_init(bp); 14884 bnxt_hwmon_init(bp); 14885 return 0; 14886 } 14887 14888 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14889 { 14890 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14891 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14892 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14893 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14894 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14895 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14896 bp->rss_hash_delta = bp->rss_hash_cfg; 14897 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14898 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14899 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14900 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14901 } 14902 } 14903 14904 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14905 { 14906 struct net_device *dev = bp->dev; 14907 14908 dev->hw_features &= ~NETIF_F_NTUPLE; 14909 dev->features &= ~NETIF_F_NTUPLE; 14910 bp->flags &= ~BNXT_FLAG_RFS; 14911 if (bnxt_rfs_supported(bp)) { 14912 dev->hw_features |= NETIF_F_NTUPLE; 14913 if (bnxt_rfs_capable(bp, false)) { 14914 bp->flags |= BNXT_FLAG_RFS; 14915 dev->features |= NETIF_F_NTUPLE; 14916 } 14917 } 14918 } 14919 14920 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14921 { 14922 struct pci_dev *pdev = bp->pdev; 14923 14924 bnxt_set_dflt_rss_hash_type(bp); 14925 bnxt_set_dflt_rfs(bp); 14926 14927 bnxt_get_wol_settings(bp); 14928 if (bp->flags & BNXT_FLAG_WOL_CAP) 14929 device_set_wakeup_enable(&pdev->dev, bp->wol); 14930 else 14931 device_set_wakeup_capable(&pdev->dev, false); 14932 14933 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14934 bnxt_hwrm_coal_params_qcaps(bp); 14935 } 14936 14937 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14938 14939 int bnxt_fw_init_one(struct bnxt *bp) 14940 { 14941 int rc; 14942 14943 rc = bnxt_fw_init_one_p1(bp); 14944 if (rc) { 14945 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14946 return rc; 14947 } 14948 rc = bnxt_fw_init_one_p2(bp); 14949 if (rc) { 14950 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14951 return rc; 14952 } 14953 rc = bnxt_probe_phy(bp, false); 14954 if (rc) 14955 return rc; 14956 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14957 if (rc) 14958 return rc; 14959 14960 bnxt_fw_init_one_p3(bp); 14961 return 0; 14962 } 14963 14964 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14965 { 14966 struct bnxt_fw_health *fw_health = bp->fw_health; 14967 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14968 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14969 u32 reg_type, reg_off, delay_msecs; 14970 14971 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14972 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14973 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14974 switch (reg_type) { 14975 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14976 pci_write_config_dword(bp->pdev, reg_off, val); 14977 break; 14978 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14979 writel(reg_off & BNXT_GRC_BASE_MASK, 14980 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14981 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14982 fallthrough; 14983 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14984 writel(val, bp->bar0 + reg_off); 14985 break; 14986 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14987 writel(val, bp->bar1 + reg_off); 14988 break; 14989 } 14990 if (delay_msecs) { 14991 pci_read_config_dword(bp->pdev, 0, &val); 14992 msleep(delay_msecs); 14993 } 14994 } 14995 14996 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14997 { 14998 struct hwrm_func_qcfg_output *resp; 14999 struct hwrm_func_qcfg_input *req; 15000 bool result = true; /* firmware will enforce if unknown */ 15001 15002 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 15003 return result; 15004 15005 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 15006 return result; 15007 15008 req->fid = cpu_to_le16(0xffff); 15009 resp = hwrm_req_hold(bp, req); 15010 if (!hwrm_req_send(bp, req)) 15011 result = !!(le16_to_cpu(resp->flags) & 15012 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 15013 hwrm_req_drop(bp, req); 15014 return result; 15015 } 15016 15017 static void bnxt_reset_all(struct bnxt *bp) 15018 { 15019 struct bnxt_fw_health *fw_health = bp->fw_health; 15020 int i, rc; 15021 15022 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15023 bnxt_fw_reset_via_optee(bp); 15024 bp->fw_reset_timestamp = jiffies; 15025 return; 15026 } 15027 15028 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 15029 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 15030 bnxt_fw_reset_writel(bp, i); 15031 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 15032 struct hwrm_fw_reset_input *req; 15033 15034 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 15035 if (!rc) { 15036 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 15037 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 15038 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 15039 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 15040 rc = hwrm_req_send(bp, req); 15041 } 15042 if (rc != -ENODEV) 15043 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 15044 } 15045 bp->fw_reset_timestamp = jiffies; 15046 } 15047 15048 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 15049 { 15050 return time_after(jiffies, bp->fw_reset_timestamp + 15051 (bp->fw_reset_max_dsecs * HZ / 10)); 15052 } 15053 15054 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 15055 { 15056 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15057 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 15058 bnxt_dl_health_fw_status_update(bp, false); 15059 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 15060 netif_close(bp->dev); 15061 } 15062 15063 static void bnxt_fw_reset_task(struct work_struct *work) 15064 { 15065 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 15066 int rc = 0; 15067 15068 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 15069 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 15070 return; 15071 } 15072 15073 switch (bp->fw_reset_state) { 15074 case BNXT_FW_RESET_STATE_POLL_VF: { 15075 int n = bnxt_get_registered_vfs(bp); 15076 int tmo; 15077 15078 if (n < 0) { 15079 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 15080 n, jiffies_to_msecs(jiffies - 15081 bp->fw_reset_timestamp)); 15082 goto fw_reset_abort; 15083 } else if (n > 0) { 15084 if (bnxt_fw_reset_timeout(bp)) { 15085 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15086 bp->fw_reset_state = 0; 15087 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 15088 n); 15089 goto ulp_start; 15090 } 15091 bnxt_queue_fw_reset_work(bp, HZ / 10); 15092 return; 15093 } 15094 bp->fw_reset_timestamp = jiffies; 15095 netdev_lock(bp->dev); 15096 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 15097 bnxt_fw_reset_abort(bp, rc); 15098 netdev_unlock(bp->dev); 15099 goto ulp_start; 15100 } 15101 bnxt_fw_reset_close(bp); 15102 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15103 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15104 tmo = HZ / 10; 15105 } else { 15106 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15107 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15108 } 15109 netdev_unlock(bp->dev); 15110 bnxt_queue_fw_reset_work(bp, tmo); 15111 return; 15112 } 15113 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15114 u32 val; 15115 15116 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15117 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15118 !bnxt_fw_reset_timeout(bp)) { 15119 bnxt_queue_fw_reset_work(bp, HZ / 5); 15120 return; 15121 } 15122 15123 if (!bp->fw_health->primary) { 15124 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15125 15126 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15127 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15128 return; 15129 } 15130 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15131 } 15132 fallthrough; 15133 case BNXT_FW_RESET_STATE_RESET_FW: 15134 bnxt_reset_all(bp); 15135 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15136 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15137 return; 15138 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15139 bnxt_inv_fw_health_reg(bp); 15140 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15141 !bp->fw_reset_min_dsecs) { 15142 u16 val; 15143 15144 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15145 if (val == 0xffff) { 15146 if (bnxt_fw_reset_timeout(bp)) { 15147 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15148 rc = -ETIMEDOUT; 15149 goto fw_reset_abort; 15150 } 15151 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15152 return; 15153 } 15154 } 15155 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15156 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15157 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15158 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15159 bnxt_dl_remote_reload(bp); 15160 if (pci_enable_device(bp->pdev)) { 15161 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15162 rc = -ENODEV; 15163 goto fw_reset_abort; 15164 } 15165 pci_set_master(bp->pdev); 15166 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15167 fallthrough; 15168 case BNXT_FW_RESET_STATE_POLL_FW: 15169 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15170 rc = bnxt_hwrm_poll(bp); 15171 if (rc) { 15172 if (bnxt_fw_reset_timeout(bp)) { 15173 netdev_err(bp->dev, "Firmware reset aborted\n"); 15174 goto fw_reset_abort_status; 15175 } 15176 bnxt_queue_fw_reset_work(bp, HZ / 5); 15177 return; 15178 } 15179 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15180 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15181 fallthrough; 15182 case BNXT_FW_RESET_STATE_OPENING: 15183 while (!netdev_trylock(bp->dev)) { 15184 bnxt_queue_fw_reset_work(bp, HZ / 10); 15185 return; 15186 } 15187 rc = bnxt_open(bp->dev); 15188 if (rc) { 15189 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15190 bnxt_fw_reset_abort(bp, rc); 15191 netdev_unlock(bp->dev); 15192 goto ulp_start; 15193 } 15194 15195 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15196 bp->fw_health->enabled) { 15197 bp->fw_health->last_fw_reset_cnt = 15198 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15199 } 15200 bp->fw_reset_state = 0; 15201 /* Make sure fw_reset_state is 0 before clearing the flag */ 15202 smp_mb__before_atomic(); 15203 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15204 bnxt_ptp_reapply_pps(bp); 15205 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15206 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15207 bnxt_dl_health_fw_recovery_done(bp); 15208 bnxt_dl_health_fw_status_update(bp, true); 15209 } 15210 netdev_unlock(bp->dev); 15211 bnxt_ulp_start(bp, 0); 15212 bnxt_reenable_sriov(bp); 15213 netdev_lock(bp->dev); 15214 bnxt_vf_reps_alloc(bp); 15215 bnxt_vf_reps_open(bp); 15216 netdev_unlock(bp->dev); 15217 break; 15218 } 15219 return; 15220 15221 fw_reset_abort_status: 15222 if (bp->fw_health->status_reliable || 15223 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15224 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15225 15226 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15227 } 15228 fw_reset_abort: 15229 netdev_lock(bp->dev); 15230 bnxt_fw_reset_abort(bp, rc); 15231 netdev_unlock(bp->dev); 15232 ulp_start: 15233 bnxt_ulp_start(bp, rc); 15234 } 15235 15236 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15237 { 15238 int rc; 15239 struct bnxt *bp = netdev_priv(dev); 15240 15241 SET_NETDEV_DEV(dev, &pdev->dev); 15242 15243 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15244 rc = pci_enable_device(pdev); 15245 if (rc) { 15246 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15247 goto init_err; 15248 } 15249 15250 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15251 dev_err(&pdev->dev, 15252 "Cannot find PCI device base address, aborting\n"); 15253 rc = -ENODEV; 15254 goto init_err_disable; 15255 } 15256 15257 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15258 if (rc) { 15259 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15260 goto init_err_disable; 15261 } 15262 15263 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15264 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15265 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15266 rc = -EIO; 15267 goto init_err_release; 15268 } 15269 15270 pci_set_master(pdev); 15271 15272 bp->dev = dev; 15273 bp->pdev = pdev; 15274 15275 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15276 * determines the BAR size. 15277 */ 15278 bp->bar0 = pci_ioremap_bar(pdev, 0); 15279 if (!bp->bar0) { 15280 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15281 rc = -ENOMEM; 15282 goto init_err_release; 15283 } 15284 15285 bp->bar2 = pci_ioremap_bar(pdev, 4); 15286 if (!bp->bar2) { 15287 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15288 rc = -ENOMEM; 15289 goto init_err_release; 15290 } 15291 15292 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15293 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15294 15295 spin_lock_init(&bp->ntp_fltr_lock); 15296 #if BITS_PER_LONG == 32 15297 spin_lock_init(&bp->db_lock); 15298 #endif 15299 15300 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15301 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15302 15303 timer_setup(&bp->timer, bnxt_timer, 0); 15304 bp->current_interval = BNXT_TIMER_INTERVAL; 15305 15306 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15307 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15308 15309 clear_bit(BNXT_STATE_OPEN, &bp->state); 15310 return 0; 15311 15312 init_err_release: 15313 bnxt_unmap_bars(bp, pdev); 15314 pci_release_regions(pdev); 15315 15316 init_err_disable: 15317 pci_disable_device(pdev); 15318 15319 init_err: 15320 return rc; 15321 } 15322 15323 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15324 { 15325 struct sockaddr *addr = p; 15326 struct bnxt *bp = netdev_priv(dev); 15327 int rc = 0; 15328 15329 netdev_assert_locked(dev); 15330 15331 if (!is_valid_ether_addr(addr->sa_data)) 15332 return -EADDRNOTAVAIL; 15333 15334 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15335 return 0; 15336 15337 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15338 if (rc) 15339 return rc; 15340 15341 eth_hw_addr_set(dev, addr->sa_data); 15342 bnxt_clear_usr_fltrs(bp, true); 15343 if (netif_running(dev)) { 15344 bnxt_close_nic(bp, false, false); 15345 rc = bnxt_open_nic(bp, false, false); 15346 } 15347 15348 return rc; 15349 } 15350 15351 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15352 { 15353 struct bnxt *bp = netdev_priv(dev); 15354 15355 netdev_assert_locked(dev); 15356 15357 if (netif_running(dev)) 15358 bnxt_close_nic(bp, true, false); 15359 15360 WRITE_ONCE(dev->mtu, new_mtu); 15361 15362 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15363 * program is attached. We need to set the AGG rings settings and 15364 * rx_skb_func accordingly. 15365 */ 15366 if (READ_ONCE(bp->xdp_prog)) 15367 bnxt_set_rx_skb_mode(bp, true); 15368 15369 bnxt_set_ring_params(bp); 15370 15371 if (netif_running(dev)) 15372 return bnxt_open_nic(bp, true, false); 15373 15374 return 0; 15375 } 15376 15377 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15378 { 15379 struct bnxt *bp = netdev_priv(dev); 15380 bool sh = false; 15381 int rc, tx_cp; 15382 15383 if (tc > bp->max_tc) { 15384 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15385 tc, bp->max_tc); 15386 return -EINVAL; 15387 } 15388 15389 if (bp->num_tc == tc) 15390 return 0; 15391 15392 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15393 sh = true; 15394 15395 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15396 sh, tc, bp->tx_nr_rings_xdp); 15397 if (rc) 15398 return rc; 15399 15400 /* Needs to close the device and do hw resource re-allocations */ 15401 if (netif_running(bp->dev)) 15402 bnxt_close_nic(bp, true, false); 15403 15404 if (tc) { 15405 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15406 netdev_set_num_tc(dev, tc); 15407 bp->num_tc = tc; 15408 } else { 15409 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15410 netdev_reset_tc(dev); 15411 bp->num_tc = 0; 15412 } 15413 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15414 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15415 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15416 tx_cp + bp->rx_nr_rings; 15417 15418 if (netif_running(bp->dev)) 15419 return bnxt_open_nic(bp, true, false); 15420 15421 return 0; 15422 } 15423 15424 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15425 void *cb_priv) 15426 { 15427 struct bnxt *bp = cb_priv; 15428 15429 if (!bnxt_tc_flower_enabled(bp) || 15430 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15431 return -EOPNOTSUPP; 15432 15433 switch (type) { 15434 case TC_SETUP_CLSFLOWER: 15435 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15436 default: 15437 return -EOPNOTSUPP; 15438 } 15439 } 15440 15441 LIST_HEAD(bnxt_block_cb_list); 15442 15443 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15444 void *type_data) 15445 { 15446 struct bnxt *bp = netdev_priv(dev); 15447 15448 switch (type) { 15449 case TC_SETUP_BLOCK: 15450 return flow_block_cb_setup_simple(type_data, 15451 &bnxt_block_cb_list, 15452 bnxt_setup_tc_block_cb, 15453 bp, bp, true); 15454 case TC_SETUP_QDISC_MQPRIO: { 15455 struct tc_mqprio_qopt *mqprio = type_data; 15456 15457 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15458 15459 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15460 } 15461 default: 15462 return -EOPNOTSUPP; 15463 } 15464 } 15465 15466 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15467 const struct sk_buff *skb) 15468 { 15469 struct bnxt_vnic_info *vnic; 15470 15471 if (skb) 15472 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15473 15474 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15475 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15476 } 15477 15478 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15479 u32 idx) 15480 { 15481 struct hlist_head *head; 15482 int bit_id; 15483 15484 spin_lock_bh(&bp->ntp_fltr_lock); 15485 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15486 if (bit_id < 0) { 15487 spin_unlock_bh(&bp->ntp_fltr_lock); 15488 return -ENOMEM; 15489 } 15490 15491 fltr->base.sw_id = (u16)bit_id; 15492 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15493 fltr->base.flags |= BNXT_ACT_RING_DST; 15494 head = &bp->ntp_fltr_hash_tbl[idx]; 15495 hlist_add_head_rcu(&fltr->base.hash, head); 15496 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15497 bnxt_insert_usr_fltr(bp, &fltr->base); 15498 bp->ntp_fltr_count++; 15499 spin_unlock_bh(&bp->ntp_fltr_lock); 15500 return 0; 15501 } 15502 15503 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15504 struct bnxt_ntuple_filter *f2) 15505 { 15506 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15507 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15508 struct flow_keys *keys1 = &f1->fkeys; 15509 struct flow_keys *keys2 = &f2->fkeys; 15510 15511 if (keys1->basic.n_proto != keys2->basic.n_proto || 15512 keys1->basic.ip_proto != keys2->basic.ip_proto) 15513 return false; 15514 15515 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15516 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15517 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15518 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15519 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15520 return false; 15521 } else { 15522 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15523 &keys2->addrs.v6addrs.src) || 15524 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15525 &masks2->addrs.v6addrs.src) || 15526 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15527 &keys2->addrs.v6addrs.dst) || 15528 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15529 &masks2->addrs.v6addrs.dst)) 15530 return false; 15531 } 15532 15533 return keys1->ports.src == keys2->ports.src && 15534 masks1->ports.src == masks2->ports.src && 15535 keys1->ports.dst == keys2->ports.dst && 15536 masks1->ports.dst == masks2->ports.dst && 15537 keys1->control.flags == keys2->control.flags && 15538 f1->l2_fltr == f2->l2_fltr; 15539 } 15540 15541 struct bnxt_ntuple_filter * 15542 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15543 struct bnxt_ntuple_filter *fltr, u32 idx) 15544 { 15545 struct bnxt_ntuple_filter *f; 15546 struct hlist_head *head; 15547 15548 head = &bp->ntp_fltr_hash_tbl[idx]; 15549 hlist_for_each_entry_rcu(f, head, base.hash) { 15550 if (bnxt_fltr_match(f, fltr)) 15551 return f; 15552 } 15553 return NULL; 15554 } 15555 15556 #ifdef CONFIG_RFS_ACCEL 15557 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15558 u16 rxq_index, u32 flow_id) 15559 { 15560 struct bnxt *bp = netdev_priv(dev); 15561 struct bnxt_ntuple_filter *fltr, *new_fltr; 15562 struct flow_keys *fkeys; 15563 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15564 struct bnxt_l2_filter *l2_fltr; 15565 int rc = 0, idx; 15566 u32 flags; 15567 15568 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15569 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15570 atomic_inc(&l2_fltr->refcnt); 15571 } else { 15572 struct bnxt_l2_key key; 15573 15574 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15575 key.vlan = 0; 15576 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15577 if (!l2_fltr) 15578 return -EINVAL; 15579 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15580 bnxt_del_l2_filter(bp, l2_fltr); 15581 return -EINVAL; 15582 } 15583 } 15584 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15585 if (!new_fltr) { 15586 bnxt_del_l2_filter(bp, l2_fltr); 15587 return -ENOMEM; 15588 } 15589 15590 fkeys = &new_fltr->fkeys; 15591 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15592 rc = -EPROTONOSUPPORT; 15593 goto err_free; 15594 } 15595 15596 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15597 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15598 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15599 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15600 rc = -EPROTONOSUPPORT; 15601 goto err_free; 15602 } 15603 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15604 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15605 if (bp->hwrm_spec_code < 0x10601) { 15606 rc = -EPROTONOSUPPORT; 15607 goto err_free; 15608 } 15609 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15610 } 15611 flags = fkeys->control.flags; 15612 if (((flags & FLOW_DIS_ENCAPSULATION) && 15613 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15614 rc = -EPROTONOSUPPORT; 15615 goto err_free; 15616 } 15617 new_fltr->l2_fltr = l2_fltr; 15618 15619 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15620 rcu_read_lock(); 15621 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15622 if (fltr) { 15623 rc = fltr->base.sw_id; 15624 rcu_read_unlock(); 15625 goto err_free; 15626 } 15627 rcu_read_unlock(); 15628 15629 new_fltr->flow_id = flow_id; 15630 new_fltr->base.rxq = rxq_index; 15631 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15632 if (!rc) { 15633 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15634 return new_fltr->base.sw_id; 15635 } 15636 15637 err_free: 15638 bnxt_del_l2_filter(bp, l2_fltr); 15639 kfree(new_fltr); 15640 return rc; 15641 } 15642 #endif 15643 15644 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15645 { 15646 spin_lock_bh(&bp->ntp_fltr_lock); 15647 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15648 spin_unlock_bh(&bp->ntp_fltr_lock); 15649 return; 15650 } 15651 hlist_del_rcu(&fltr->base.hash); 15652 bnxt_del_one_usr_fltr(bp, &fltr->base); 15653 bp->ntp_fltr_count--; 15654 spin_unlock_bh(&bp->ntp_fltr_lock); 15655 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15656 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15657 kfree_rcu(fltr, base.rcu); 15658 } 15659 15660 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15661 { 15662 #ifdef CONFIG_RFS_ACCEL 15663 int i; 15664 15665 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15666 struct hlist_head *head; 15667 struct hlist_node *tmp; 15668 struct bnxt_ntuple_filter *fltr; 15669 int rc; 15670 15671 head = &bp->ntp_fltr_hash_tbl[i]; 15672 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15673 bool del = false; 15674 15675 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15676 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15677 continue; 15678 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15679 fltr->flow_id, 15680 fltr->base.sw_id)) { 15681 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15682 fltr); 15683 del = true; 15684 } 15685 } else { 15686 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15687 fltr); 15688 if (rc) 15689 del = true; 15690 else 15691 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15692 } 15693 15694 if (del) 15695 bnxt_del_ntp_filter(bp, fltr); 15696 } 15697 } 15698 #endif 15699 } 15700 15701 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15702 unsigned int entry, struct udp_tunnel_info *ti) 15703 { 15704 struct bnxt *bp = netdev_priv(netdev); 15705 unsigned int cmd; 15706 15707 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15708 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15709 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15710 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15711 else 15712 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15713 15714 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15715 } 15716 15717 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15718 unsigned int entry, struct udp_tunnel_info *ti) 15719 { 15720 struct bnxt *bp = netdev_priv(netdev); 15721 unsigned int cmd; 15722 15723 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15724 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15725 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15726 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15727 else 15728 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15729 15730 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15731 } 15732 15733 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15734 .set_port = bnxt_udp_tunnel_set_port, 15735 .unset_port = bnxt_udp_tunnel_unset_port, 15736 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15737 .tables = { 15738 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15739 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15740 }, 15741 }, bnxt_udp_tunnels_p7 = { 15742 .set_port = bnxt_udp_tunnel_set_port, 15743 .unset_port = bnxt_udp_tunnel_unset_port, 15744 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15745 .tables = { 15746 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15747 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15748 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15749 }, 15750 }; 15751 15752 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15753 struct net_device *dev, u32 filter_mask, 15754 int nlflags) 15755 { 15756 struct bnxt *bp = netdev_priv(dev); 15757 15758 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15759 nlflags, filter_mask, NULL); 15760 } 15761 15762 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15763 u16 flags, struct netlink_ext_ack *extack) 15764 { 15765 struct bnxt *bp = netdev_priv(dev); 15766 struct nlattr *attr, *br_spec; 15767 int rem, rc = 0; 15768 15769 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15770 return -EOPNOTSUPP; 15771 15772 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15773 if (!br_spec) 15774 return -EINVAL; 15775 15776 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15777 u16 mode; 15778 15779 mode = nla_get_u16(attr); 15780 if (mode == bp->br_mode) 15781 break; 15782 15783 rc = bnxt_hwrm_set_br_mode(bp, mode); 15784 if (!rc) 15785 bp->br_mode = mode; 15786 break; 15787 } 15788 return rc; 15789 } 15790 15791 int bnxt_get_port_parent_id(struct net_device *dev, 15792 struct netdev_phys_item_id *ppid) 15793 { 15794 struct bnxt *bp = netdev_priv(dev); 15795 15796 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15797 return -EOPNOTSUPP; 15798 15799 /* The PF and it's VF-reps only support the switchdev framework */ 15800 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15801 return -EOPNOTSUPP; 15802 15803 ppid->id_len = sizeof(bp->dsn); 15804 memcpy(ppid->id, bp->dsn, ppid->id_len); 15805 15806 return 0; 15807 } 15808 15809 static const struct net_device_ops bnxt_netdev_ops = { 15810 .ndo_open = bnxt_open, 15811 .ndo_start_xmit = bnxt_start_xmit, 15812 .ndo_stop = bnxt_close, 15813 .ndo_get_stats64 = bnxt_get_stats64, 15814 .ndo_set_rx_mode = bnxt_set_rx_mode, 15815 .ndo_eth_ioctl = bnxt_ioctl, 15816 .ndo_validate_addr = eth_validate_addr, 15817 .ndo_set_mac_address = bnxt_change_mac_addr, 15818 .ndo_change_mtu = bnxt_change_mtu, 15819 .ndo_fix_features = bnxt_fix_features, 15820 .ndo_set_features = bnxt_set_features, 15821 .ndo_features_check = bnxt_features_check, 15822 .ndo_tx_timeout = bnxt_tx_timeout, 15823 #ifdef CONFIG_BNXT_SRIOV 15824 .ndo_get_vf_config = bnxt_get_vf_config, 15825 .ndo_set_vf_mac = bnxt_set_vf_mac, 15826 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15827 .ndo_set_vf_rate = bnxt_set_vf_bw, 15828 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15829 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15830 .ndo_set_vf_trust = bnxt_set_vf_trust, 15831 #endif 15832 .ndo_setup_tc = bnxt_setup_tc, 15833 #ifdef CONFIG_RFS_ACCEL 15834 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15835 #endif 15836 .ndo_bpf = bnxt_xdp, 15837 .ndo_xdp_xmit = bnxt_xdp_xmit, 15838 .ndo_bridge_getlink = bnxt_bridge_getlink, 15839 .ndo_bridge_setlink = bnxt_bridge_setlink, 15840 .ndo_hwtstamp_get = bnxt_hwtstamp_get, 15841 .ndo_hwtstamp_set = bnxt_hwtstamp_set, 15842 }; 15843 15844 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15845 struct netdev_queue_stats_rx *stats) 15846 { 15847 struct bnxt *bp = netdev_priv(dev); 15848 struct bnxt_cp_ring_info *cpr; 15849 u64 *sw; 15850 15851 if (!bp->bnapi) 15852 return; 15853 15854 cpr = &bp->bnapi[i]->cp_ring; 15855 sw = cpr->stats.sw_stats; 15856 15857 stats->packets = 0; 15858 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15859 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15860 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15861 15862 stats->bytes = 0; 15863 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15864 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15865 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15866 15867 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15868 } 15869 15870 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15871 struct netdev_queue_stats_tx *stats) 15872 { 15873 struct bnxt *bp = netdev_priv(dev); 15874 struct bnxt_napi *bnapi; 15875 u64 *sw; 15876 15877 if (!bp->tx_ring) 15878 return; 15879 15880 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15881 sw = bnapi->cp_ring.stats.sw_stats; 15882 15883 stats->packets = 0; 15884 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15885 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15886 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15887 15888 stats->bytes = 0; 15889 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15890 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15891 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15892 } 15893 15894 static void bnxt_get_base_stats(struct net_device *dev, 15895 struct netdev_queue_stats_rx *rx, 15896 struct netdev_queue_stats_tx *tx) 15897 { 15898 struct bnxt *bp = netdev_priv(dev); 15899 15900 rx->packets = bp->net_stats_prev.rx_packets; 15901 rx->bytes = bp->net_stats_prev.rx_bytes; 15902 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15903 15904 tx->packets = bp->net_stats_prev.tx_packets; 15905 tx->bytes = bp->net_stats_prev.tx_bytes; 15906 } 15907 15908 static const struct netdev_stat_ops bnxt_stat_ops = { 15909 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15910 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15911 .get_base_stats = bnxt_get_base_stats, 15912 }; 15913 15914 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15915 { 15916 struct bnxt_rx_ring_info *rxr, *clone; 15917 struct bnxt *bp = netdev_priv(dev); 15918 struct bnxt_ring_struct *ring; 15919 int rc; 15920 15921 if (!bp->rx_ring) 15922 return -ENETDOWN; 15923 15924 rxr = &bp->rx_ring[idx]; 15925 clone = qmem; 15926 memcpy(clone, rxr, sizeof(*rxr)); 15927 bnxt_init_rx_ring_struct(bp, clone); 15928 bnxt_reset_rx_ring_struct(bp, clone); 15929 15930 clone->rx_prod = 0; 15931 clone->rx_agg_prod = 0; 15932 clone->rx_sw_agg_prod = 0; 15933 clone->rx_next_cons = 0; 15934 clone->need_head_pool = false; 15935 15936 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15937 if (rc) 15938 return rc; 15939 15940 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15941 if (rc < 0) 15942 goto err_page_pool_destroy; 15943 15944 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15945 MEM_TYPE_PAGE_POOL, 15946 clone->page_pool); 15947 if (rc) 15948 goto err_rxq_info_unreg; 15949 15950 ring = &clone->rx_ring_struct; 15951 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15952 if (rc) 15953 goto err_free_rx_ring; 15954 15955 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15956 ring = &clone->rx_agg_ring_struct; 15957 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15958 if (rc) 15959 goto err_free_rx_agg_ring; 15960 15961 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15962 if (rc) 15963 goto err_free_rx_agg_ring; 15964 } 15965 15966 if (bp->flags & BNXT_FLAG_TPA) { 15967 rc = bnxt_alloc_one_tpa_info(bp, clone); 15968 if (rc) 15969 goto err_free_tpa_info; 15970 } 15971 15972 bnxt_init_one_rx_ring_rxbd(bp, clone); 15973 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15974 15975 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15976 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15977 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 15978 if (bp->flags & BNXT_FLAG_TPA) 15979 bnxt_alloc_one_tpa_info_data(bp, clone); 15980 15981 return 0; 15982 15983 err_free_tpa_info: 15984 bnxt_free_one_tpa_info(bp, clone); 15985 err_free_rx_agg_ring: 15986 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15987 err_free_rx_ring: 15988 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15989 err_rxq_info_unreg: 15990 xdp_rxq_info_unreg(&clone->xdp_rxq); 15991 err_page_pool_destroy: 15992 page_pool_destroy(clone->page_pool); 15993 page_pool_destroy(clone->head_pool); 15994 clone->page_pool = NULL; 15995 clone->head_pool = NULL; 15996 return rc; 15997 } 15998 15999 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 16000 { 16001 struct bnxt_rx_ring_info *rxr = qmem; 16002 struct bnxt *bp = netdev_priv(dev); 16003 struct bnxt_ring_struct *ring; 16004 16005 bnxt_free_one_rx_ring_skbs(bp, rxr); 16006 bnxt_free_one_tpa_info(bp, rxr); 16007 16008 xdp_rxq_info_unreg(&rxr->xdp_rxq); 16009 16010 page_pool_destroy(rxr->page_pool); 16011 page_pool_destroy(rxr->head_pool); 16012 rxr->page_pool = NULL; 16013 rxr->head_pool = NULL; 16014 16015 ring = &rxr->rx_ring_struct; 16016 bnxt_free_ring(bp, &ring->ring_mem); 16017 16018 ring = &rxr->rx_agg_ring_struct; 16019 bnxt_free_ring(bp, &ring->ring_mem); 16020 16021 kfree(rxr->rx_agg_bmap); 16022 rxr->rx_agg_bmap = NULL; 16023 } 16024 16025 static void bnxt_copy_rx_ring(struct bnxt *bp, 16026 struct bnxt_rx_ring_info *dst, 16027 struct bnxt_rx_ring_info *src) 16028 { 16029 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 16030 struct bnxt_ring_struct *dst_ring, *src_ring; 16031 int i; 16032 16033 dst_ring = &dst->rx_ring_struct; 16034 dst_rmem = &dst_ring->ring_mem; 16035 src_ring = &src->rx_ring_struct; 16036 src_rmem = &src_ring->ring_mem; 16037 16038 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16039 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16040 WARN_ON(dst_rmem->flags != src_rmem->flags); 16041 WARN_ON(dst_rmem->depth != src_rmem->depth); 16042 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16043 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16044 16045 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16046 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16047 *dst_rmem->vmem = *src_rmem->vmem; 16048 for (i = 0; i < dst_rmem->nr_pages; i++) { 16049 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16050 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16051 } 16052 16053 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 16054 return; 16055 16056 dst_ring = &dst->rx_agg_ring_struct; 16057 dst_rmem = &dst_ring->ring_mem; 16058 src_ring = &src->rx_agg_ring_struct; 16059 src_rmem = &src_ring->ring_mem; 16060 16061 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16062 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16063 WARN_ON(dst_rmem->flags != src_rmem->flags); 16064 WARN_ON(dst_rmem->depth != src_rmem->depth); 16065 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16066 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16067 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 16068 16069 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16070 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16071 *dst_rmem->vmem = *src_rmem->vmem; 16072 for (i = 0; i < dst_rmem->nr_pages; i++) { 16073 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16074 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16075 } 16076 16077 dst->rx_agg_bmap = src->rx_agg_bmap; 16078 } 16079 16080 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 16081 { 16082 struct bnxt *bp = netdev_priv(dev); 16083 struct bnxt_rx_ring_info *rxr, *clone; 16084 struct bnxt_cp_ring_info *cpr; 16085 struct bnxt_vnic_info *vnic; 16086 struct bnxt_napi *bnapi; 16087 int i, rc; 16088 u16 mru; 16089 16090 rxr = &bp->rx_ring[idx]; 16091 clone = qmem; 16092 16093 rxr->rx_prod = clone->rx_prod; 16094 rxr->rx_agg_prod = clone->rx_agg_prod; 16095 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 16096 rxr->rx_next_cons = clone->rx_next_cons; 16097 rxr->rx_tpa = clone->rx_tpa; 16098 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 16099 rxr->page_pool = clone->page_pool; 16100 rxr->head_pool = clone->head_pool; 16101 rxr->xdp_rxq = clone->xdp_rxq; 16102 rxr->need_head_pool = clone->need_head_pool; 16103 16104 bnxt_copy_rx_ring(bp, rxr, clone); 16105 16106 bnapi = rxr->bnapi; 16107 cpr = &bnapi->cp_ring; 16108 16109 /* All rings have been reserved and previously allocated. 16110 * Reallocating with the same parameters should never fail. 16111 */ 16112 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16113 if (rc) 16114 goto err_reset; 16115 16116 if (bp->tph_mode) { 16117 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16118 if (rc) 16119 goto err_reset; 16120 } 16121 16122 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16123 if (rc) 16124 goto err_reset; 16125 16126 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16127 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16128 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16129 16130 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16131 rc = bnxt_tx_queue_start(bp, idx); 16132 if (rc) 16133 goto err_reset; 16134 } 16135 16136 bnxt_enable_rx_page_pool(rxr); 16137 napi_enable_locked(&bnapi->napi); 16138 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16139 16140 mru = bp->dev->mtu + VLAN_ETH_HLEN; 16141 for (i = 0; i < bp->nr_vnics; i++) { 16142 vnic = &bp->vnic_info[i]; 16143 16144 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16145 if (rc) 16146 return rc; 16147 } 16148 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16149 16150 err_reset: 16151 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16152 rc); 16153 napi_enable_locked(&bnapi->napi); 16154 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16155 bnxt_reset_task(bp, true); 16156 return rc; 16157 } 16158 16159 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16160 { 16161 struct bnxt *bp = netdev_priv(dev); 16162 struct bnxt_rx_ring_info *rxr; 16163 struct bnxt_cp_ring_info *cpr; 16164 struct bnxt_vnic_info *vnic; 16165 struct bnxt_napi *bnapi; 16166 int i; 16167 16168 for (i = 0; i < bp->nr_vnics; i++) { 16169 vnic = &bp->vnic_info[i]; 16170 16171 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16172 } 16173 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16174 /* Make sure NAPI sees that the VNIC is disabled */ 16175 synchronize_net(); 16176 rxr = &bp->rx_ring[idx]; 16177 bnapi = rxr->bnapi; 16178 cpr = &bnapi->cp_ring; 16179 cancel_work_sync(&cpr->dim.work); 16180 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16181 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16182 page_pool_disable_direct_recycling(rxr->page_pool); 16183 if (bnxt_separate_head_pool(rxr)) 16184 page_pool_disable_direct_recycling(rxr->head_pool); 16185 16186 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16187 bnxt_tx_queue_stop(bp, idx); 16188 16189 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16190 * completion is handled in NAPI to guarantee no more DMA on that ring 16191 * after seeing the completion. 16192 */ 16193 napi_disable_locked(&bnapi->napi); 16194 16195 if (bp->tph_mode) { 16196 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16197 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16198 } 16199 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16200 16201 memcpy(qmem, rxr, sizeof(*rxr)); 16202 bnxt_init_rx_ring_struct(bp, qmem); 16203 16204 return 0; 16205 } 16206 16207 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16208 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16209 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16210 .ndo_queue_mem_free = bnxt_queue_mem_free, 16211 .ndo_queue_start = bnxt_queue_start, 16212 .ndo_queue_stop = bnxt_queue_stop, 16213 }; 16214 16215 static void bnxt_remove_one(struct pci_dev *pdev) 16216 { 16217 struct net_device *dev = pci_get_drvdata(pdev); 16218 struct bnxt *bp = netdev_priv(dev); 16219 16220 if (BNXT_PF(bp)) 16221 __bnxt_sriov_disable(bp); 16222 16223 bnxt_rdma_aux_device_del(bp); 16224 16225 unregister_netdev(dev); 16226 bnxt_ptp_clear(bp); 16227 16228 bnxt_rdma_aux_device_uninit(bp); 16229 16230 bnxt_free_l2_filters(bp, true); 16231 bnxt_free_ntp_fltrs(bp, true); 16232 WARN_ON(bp->num_rss_ctx); 16233 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16234 /* Flush any pending tasks */ 16235 cancel_work_sync(&bp->sp_task); 16236 cancel_delayed_work_sync(&bp->fw_reset_task); 16237 bp->sp_event = 0; 16238 16239 bnxt_dl_fw_reporters_destroy(bp); 16240 bnxt_dl_unregister(bp); 16241 bnxt_shutdown_tc(bp); 16242 16243 bnxt_clear_int_mode(bp); 16244 bnxt_hwrm_func_drv_unrgtr(bp); 16245 bnxt_free_hwrm_resources(bp); 16246 bnxt_hwmon_uninit(bp); 16247 bnxt_ethtool_free(bp); 16248 bnxt_dcb_free(bp); 16249 kfree(bp->ptp_cfg); 16250 bp->ptp_cfg = NULL; 16251 kfree(bp->fw_health); 16252 bp->fw_health = NULL; 16253 bnxt_cleanup_pci(bp); 16254 bnxt_free_ctx_mem(bp, true); 16255 bnxt_free_crash_dump_mem(bp); 16256 kfree(bp->rss_indir_tbl); 16257 bp->rss_indir_tbl = NULL; 16258 bnxt_free_port_stats(bp); 16259 free_netdev(dev); 16260 } 16261 16262 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16263 { 16264 int rc = 0; 16265 struct bnxt_link_info *link_info = &bp->link_info; 16266 16267 bp->phy_flags = 0; 16268 rc = bnxt_hwrm_phy_qcaps(bp); 16269 if (rc) { 16270 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16271 rc); 16272 return rc; 16273 } 16274 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16275 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16276 else 16277 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16278 16279 bp->mac_flags = 0; 16280 bnxt_hwrm_mac_qcaps(bp); 16281 16282 if (!fw_dflt) 16283 return 0; 16284 16285 mutex_lock(&bp->link_lock); 16286 rc = bnxt_update_link(bp, false); 16287 if (rc) { 16288 mutex_unlock(&bp->link_lock); 16289 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16290 rc); 16291 return rc; 16292 } 16293 16294 /* Older firmware does not have supported_auto_speeds, so assume 16295 * that all supported speeds can be autonegotiated. 16296 */ 16297 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16298 link_info->support_auto_speeds = link_info->support_speeds; 16299 16300 bnxt_init_ethtool_link_settings(bp); 16301 mutex_unlock(&bp->link_lock); 16302 return 0; 16303 } 16304 16305 static int bnxt_get_max_irq(struct pci_dev *pdev) 16306 { 16307 u16 ctrl; 16308 16309 if (!pdev->msix_cap) 16310 return 1; 16311 16312 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16313 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16314 } 16315 16316 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16317 int *max_cp) 16318 { 16319 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16320 int max_ring_grps = 0, max_irq; 16321 16322 *max_tx = hw_resc->max_tx_rings; 16323 *max_rx = hw_resc->max_rx_rings; 16324 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16325 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16326 bnxt_get_ulp_msix_num_in_use(bp), 16327 hw_resc->max_stat_ctxs - 16328 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16329 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16330 *max_cp = min_t(int, *max_cp, max_irq); 16331 max_ring_grps = hw_resc->max_hw_ring_grps; 16332 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16333 *max_cp -= 1; 16334 *max_rx -= 2; 16335 } 16336 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16337 *max_rx >>= 1; 16338 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16339 int rc; 16340 16341 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16342 if (rc) { 16343 *max_rx = 0; 16344 *max_tx = 0; 16345 } 16346 /* On P5 chips, max_cp output param should be available NQs */ 16347 *max_cp = max_irq; 16348 } 16349 *max_rx = min_t(int, *max_rx, max_ring_grps); 16350 } 16351 16352 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16353 { 16354 int rx, tx, cp; 16355 16356 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16357 *max_rx = rx; 16358 *max_tx = tx; 16359 if (!rx || !tx || !cp) 16360 return -ENOMEM; 16361 16362 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16363 } 16364 16365 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16366 bool shared) 16367 { 16368 int rc; 16369 16370 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16371 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16372 /* Not enough rings, try disabling agg rings. */ 16373 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16374 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16375 if (rc) { 16376 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16377 bp->flags |= BNXT_FLAG_AGG_RINGS; 16378 return rc; 16379 } 16380 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16381 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16382 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16383 bnxt_set_ring_params(bp); 16384 } 16385 16386 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16387 int max_cp, max_stat, max_irq; 16388 16389 /* Reserve minimum resources for RoCE */ 16390 max_cp = bnxt_get_max_func_cp_rings(bp); 16391 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16392 max_irq = bnxt_get_max_func_irqs(bp); 16393 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16394 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16395 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16396 return 0; 16397 16398 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16399 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16400 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16401 max_cp = min_t(int, max_cp, max_irq); 16402 max_cp = min_t(int, max_cp, max_stat); 16403 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16404 if (rc) 16405 rc = 0; 16406 } 16407 return rc; 16408 } 16409 16410 /* In initial default shared ring setting, each shared ring must have a 16411 * RX/TX ring pair. 16412 */ 16413 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16414 { 16415 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16416 bp->rx_nr_rings = bp->cp_nr_rings; 16417 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16418 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16419 } 16420 16421 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16422 { 16423 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16424 int avail_msix; 16425 16426 if (!bnxt_can_reserve_rings(bp)) 16427 return 0; 16428 16429 if (sh) 16430 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16431 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16432 /* Reduce default rings on multi-port cards so that total default 16433 * rings do not exceed CPU count. 16434 */ 16435 if (bp->port_count > 1) { 16436 int max_rings = 16437 max_t(int, num_online_cpus() / bp->port_count, 1); 16438 16439 dflt_rings = min_t(int, dflt_rings, max_rings); 16440 } 16441 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16442 if (rc) 16443 return rc; 16444 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16445 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16446 if (sh) 16447 bnxt_trim_dflt_sh_rings(bp); 16448 else 16449 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16450 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16451 16452 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16453 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16454 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16455 16456 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16457 bnxt_set_dflt_ulp_stat_ctxs(bp); 16458 } 16459 16460 rc = __bnxt_reserve_rings(bp); 16461 if (rc && rc != -ENODEV) 16462 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16463 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16464 if (sh) 16465 bnxt_trim_dflt_sh_rings(bp); 16466 16467 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16468 if (bnxt_need_reserve_rings(bp)) { 16469 rc = __bnxt_reserve_rings(bp); 16470 if (rc && rc != -ENODEV) 16471 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16472 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16473 } 16474 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16475 bp->rx_nr_rings++; 16476 bp->cp_nr_rings++; 16477 } 16478 if (rc) { 16479 bp->tx_nr_rings = 0; 16480 bp->rx_nr_rings = 0; 16481 } 16482 return rc; 16483 } 16484 16485 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16486 { 16487 int rc; 16488 16489 if (bp->tx_nr_rings) 16490 return 0; 16491 16492 bnxt_ulp_irq_stop(bp); 16493 bnxt_clear_int_mode(bp); 16494 rc = bnxt_set_dflt_rings(bp, true); 16495 if (rc) { 16496 if (BNXT_VF(bp) && rc == -ENODEV) 16497 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16498 else 16499 netdev_err(bp->dev, "Not enough rings available.\n"); 16500 goto init_dflt_ring_err; 16501 } 16502 rc = bnxt_init_int_mode(bp); 16503 if (rc) 16504 goto init_dflt_ring_err; 16505 16506 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16507 16508 bnxt_set_dflt_rfs(bp); 16509 16510 init_dflt_ring_err: 16511 bnxt_ulp_irq_restart(bp, rc); 16512 return rc; 16513 } 16514 16515 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16516 { 16517 int rc; 16518 16519 netdev_ops_assert_locked(bp->dev); 16520 bnxt_hwrm_func_qcaps(bp); 16521 16522 if (netif_running(bp->dev)) 16523 __bnxt_close_nic(bp, true, false); 16524 16525 bnxt_ulp_irq_stop(bp); 16526 bnxt_clear_int_mode(bp); 16527 rc = bnxt_init_int_mode(bp); 16528 bnxt_ulp_irq_restart(bp, rc); 16529 16530 if (netif_running(bp->dev)) { 16531 if (rc) 16532 netif_close(bp->dev); 16533 else 16534 rc = bnxt_open_nic(bp, true, false); 16535 } 16536 16537 return rc; 16538 } 16539 16540 static int bnxt_init_mac_addr(struct bnxt *bp) 16541 { 16542 int rc = 0; 16543 16544 if (BNXT_PF(bp)) { 16545 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 16546 } else { 16547 #ifdef CONFIG_BNXT_SRIOV 16548 struct bnxt_vf_info *vf = &bp->vf; 16549 bool strict_approval = true; 16550 16551 if (is_valid_ether_addr(vf->mac_addr)) { 16552 /* overwrite netdev dev_addr with admin VF MAC */ 16553 eth_hw_addr_set(bp->dev, vf->mac_addr); 16554 /* Older PF driver or firmware may not approve this 16555 * correctly. 16556 */ 16557 strict_approval = false; 16558 } else { 16559 eth_hw_addr_random(bp->dev); 16560 } 16561 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16562 #endif 16563 } 16564 return rc; 16565 } 16566 16567 static void bnxt_vpd_read_info(struct bnxt *bp) 16568 { 16569 struct pci_dev *pdev = bp->pdev; 16570 unsigned int vpd_size, kw_len; 16571 int pos, size; 16572 u8 *vpd_data; 16573 16574 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16575 if (IS_ERR(vpd_data)) { 16576 pci_warn(pdev, "Unable to read VPD\n"); 16577 return; 16578 } 16579 16580 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16581 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16582 if (pos < 0) 16583 goto read_sn; 16584 16585 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16586 memcpy(bp->board_partno, &vpd_data[pos], size); 16587 16588 read_sn: 16589 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16590 PCI_VPD_RO_KEYWORD_SERIALNO, 16591 &kw_len); 16592 if (pos < 0) 16593 goto exit; 16594 16595 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16596 memcpy(bp->board_serialno, &vpd_data[pos], size); 16597 exit: 16598 kfree(vpd_data); 16599 } 16600 16601 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16602 { 16603 struct pci_dev *pdev = bp->pdev; 16604 u64 qword; 16605 16606 qword = pci_get_dsn(pdev); 16607 if (!qword) { 16608 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16609 return -EOPNOTSUPP; 16610 } 16611 16612 put_unaligned_le64(qword, dsn); 16613 16614 bp->flags |= BNXT_FLAG_DSN_VALID; 16615 return 0; 16616 } 16617 16618 static int bnxt_map_db_bar(struct bnxt *bp) 16619 { 16620 if (!bp->db_size) 16621 return -ENODEV; 16622 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16623 if (!bp->bar1) 16624 return -ENOMEM; 16625 return 0; 16626 } 16627 16628 void bnxt_print_device_info(struct bnxt *bp) 16629 { 16630 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16631 board_info[bp->board_idx].name, 16632 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16633 16634 pcie_print_link_status(bp->pdev); 16635 } 16636 16637 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16638 { 16639 struct bnxt_hw_resc *hw_resc; 16640 struct net_device *dev; 16641 struct bnxt *bp; 16642 int rc, max_irqs; 16643 16644 if (pci_is_bridge(pdev)) 16645 return -ENODEV; 16646 16647 if (!pdev->msix_cap) { 16648 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16649 return -ENODEV; 16650 } 16651 16652 /* Clear any pending DMA transactions from crash kernel 16653 * while loading driver in capture kernel. 16654 */ 16655 if (is_kdump_kernel()) { 16656 pci_clear_master(pdev); 16657 pcie_flr(pdev); 16658 } 16659 16660 max_irqs = bnxt_get_max_irq(pdev); 16661 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16662 max_irqs); 16663 if (!dev) 16664 return -ENOMEM; 16665 16666 bp = netdev_priv(dev); 16667 bp->board_idx = ent->driver_data; 16668 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16669 bnxt_set_max_func_irqs(bp, max_irqs); 16670 16671 if (bnxt_vf_pciid(bp->board_idx)) 16672 bp->flags |= BNXT_FLAG_VF; 16673 16674 /* No devlink port registration in case of a VF */ 16675 if (BNXT_PF(bp)) 16676 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16677 16678 rc = bnxt_init_board(pdev, dev); 16679 if (rc < 0) 16680 goto init_err_free; 16681 16682 dev->netdev_ops = &bnxt_netdev_ops; 16683 dev->stat_ops = &bnxt_stat_ops; 16684 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16685 dev->ethtool_ops = &bnxt_ethtool_ops; 16686 pci_set_drvdata(pdev, dev); 16687 16688 rc = bnxt_alloc_hwrm_resources(bp); 16689 if (rc) 16690 goto init_err_pci_clean; 16691 16692 mutex_init(&bp->hwrm_cmd_lock); 16693 mutex_init(&bp->link_lock); 16694 16695 rc = bnxt_fw_init_one_p1(bp); 16696 if (rc) 16697 goto init_err_pci_clean; 16698 16699 if (BNXT_PF(bp)) 16700 bnxt_vpd_read_info(bp); 16701 16702 if (BNXT_CHIP_P5_PLUS(bp)) { 16703 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16704 if (BNXT_CHIP_P7(bp)) 16705 bp->flags |= BNXT_FLAG_CHIP_P7; 16706 } 16707 16708 rc = bnxt_alloc_rss_indir_tbl(bp); 16709 if (rc) 16710 goto init_err_pci_clean; 16711 16712 rc = bnxt_fw_init_one_p2(bp); 16713 if (rc) 16714 goto init_err_pci_clean; 16715 16716 rc = bnxt_map_db_bar(bp); 16717 if (rc) { 16718 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16719 rc); 16720 goto init_err_pci_clean; 16721 } 16722 16723 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16724 NETIF_F_TSO | NETIF_F_TSO6 | 16725 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16726 NETIF_F_GSO_IPXIP4 | 16727 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16728 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16729 NETIF_F_RXCSUM | NETIF_F_GRO; 16730 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16731 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16732 16733 if (BNXT_SUPPORTS_TPA(bp)) 16734 dev->hw_features |= NETIF_F_LRO; 16735 16736 dev->hw_enc_features = 16737 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16738 NETIF_F_TSO | NETIF_F_TSO6 | 16739 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16740 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16741 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16742 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16743 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16744 if (bp->flags & BNXT_FLAG_CHIP_P7) 16745 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16746 else 16747 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16748 16749 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16750 NETIF_F_GSO_GRE_CSUM; 16751 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16752 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16753 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16754 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16755 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16756 if (BNXT_SUPPORTS_TPA(bp)) 16757 dev->hw_features |= NETIF_F_GRO_HW; 16758 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16759 if (dev->features & NETIF_F_GRO_HW) 16760 dev->features &= ~NETIF_F_LRO; 16761 dev->priv_flags |= IFF_UNICAST_FLT; 16762 16763 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16764 if (bp->tso_max_segs) 16765 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16766 16767 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16768 NETDEV_XDP_ACT_RX_SG; 16769 16770 #ifdef CONFIG_BNXT_SRIOV 16771 init_waitqueue_head(&bp->sriov_cfg_wait); 16772 #endif 16773 if (BNXT_SUPPORTS_TPA(bp)) { 16774 bp->gro_func = bnxt_gro_func_5730x; 16775 if (BNXT_CHIP_P4(bp)) 16776 bp->gro_func = bnxt_gro_func_5731x; 16777 else if (BNXT_CHIP_P5_PLUS(bp)) 16778 bp->gro_func = bnxt_gro_func_5750x; 16779 } 16780 if (!BNXT_CHIP_P4_PLUS(bp)) 16781 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16782 16783 rc = bnxt_init_mac_addr(bp); 16784 if (rc) { 16785 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16786 rc = -EADDRNOTAVAIL; 16787 goto init_err_pci_clean; 16788 } 16789 16790 if (BNXT_PF(bp)) { 16791 /* Read the adapter's DSN to use as the eswitch switch_id */ 16792 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16793 } 16794 16795 /* MTU range: 60 - FW defined max */ 16796 dev->min_mtu = ETH_ZLEN; 16797 dev->max_mtu = bp->max_mtu; 16798 16799 rc = bnxt_probe_phy(bp, true); 16800 if (rc) 16801 goto init_err_pci_clean; 16802 16803 hw_resc = &bp->hw_resc; 16804 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16805 BNXT_L2_FLTR_MAX_FLTR; 16806 /* Older firmware may not report these filters properly */ 16807 if (bp->max_fltr < BNXT_MAX_FLTR) 16808 bp->max_fltr = BNXT_MAX_FLTR; 16809 bnxt_init_l2_fltr_tbl(bp); 16810 __bnxt_set_rx_skb_mode(bp, false); 16811 bnxt_set_tpa_flags(bp); 16812 bnxt_init_ring_params(bp); 16813 bnxt_set_ring_params(bp); 16814 bnxt_rdma_aux_device_init(bp); 16815 rc = bnxt_set_dflt_rings(bp, true); 16816 if (rc) { 16817 if (BNXT_VF(bp) && rc == -ENODEV) { 16818 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16819 } else { 16820 netdev_err(bp->dev, "Not enough rings available.\n"); 16821 rc = -ENOMEM; 16822 } 16823 goto init_err_pci_clean; 16824 } 16825 16826 bnxt_fw_init_one_p3(bp); 16827 16828 bnxt_init_dflt_coal(bp); 16829 16830 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16831 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16832 16833 rc = bnxt_init_int_mode(bp); 16834 if (rc) 16835 goto init_err_pci_clean; 16836 16837 /* No TC has been set yet and rings may have been trimmed due to 16838 * limited MSIX, so we re-initialize the TX rings per TC. 16839 */ 16840 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16841 16842 if (BNXT_PF(bp)) { 16843 if (!bnxt_pf_wq) { 16844 bnxt_pf_wq = 16845 create_singlethread_workqueue("bnxt_pf_wq"); 16846 if (!bnxt_pf_wq) { 16847 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16848 rc = -ENOMEM; 16849 goto init_err_pci_clean; 16850 } 16851 } 16852 rc = bnxt_init_tc(bp); 16853 if (rc) 16854 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16855 rc); 16856 } 16857 16858 bnxt_inv_fw_health_reg(bp); 16859 rc = bnxt_dl_register(bp); 16860 if (rc) 16861 goto init_err_dl; 16862 16863 INIT_LIST_HEAD(&bp->usr_fltr_list); 16864 16865 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16866 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16867 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16868 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16869 dev->request_ops_lock = true; 16870 dev->netmem_tx = true; 16871 16872 rc = register_netdev(dev); 16873 if (rc) 16874 goto init_err_cleanup; 16875 16876 bnxt_dl_fw_reporters_create(bp); 16877 16878 bnxt_rdma_aux_device_add(bp); 16879 16880 bnxt_print_device_info(bp); 16881 16882 pci_save_state(pdev); 16883 16884 return 0; 16885 init_err_cleanup: 16886 bnxt_rdma_aux_device_uninit(bp); 16887 bnxt_dl_unregister(bp); 16888 init_err_dl: 16889 bnxt_shutdown_tc(bp); 16890 bnxt_clear_int_mode(bp); 16891 16892 init_err_pci_clean: 16893 bnxt_hwrm_func_drv_unrgtr(bp); 16894 bnxt_ptp_clear(bp); 16895 kfree(bp->ptp_cfg); 16896 bp->ptp_cfg = NULL; 16897 bnxt_free_hwrm_resources(bp); 16898 bnxt_hwmon_uninit(bp); 16899 bnxt_ethtool_free(bp); 16900 kfree(bp->fw_health); 16901 bp->fw_health = NULL; 16902 bnxt_cleanup_pci(bp); 16903 bnxt_free_ctx_mem(bp, true); 16904 bnxt_free_crash_dump_mem(bp); 16905 kfree(bp->rss_indir_tbl); 16906 bp->rss_indir_tbl = NULL; 16907 16908 init_err_free: 16909 free_netdev(dev); 16910 return rc; 16911 } 16912 16913 static void bnxt_shutdown(struct pci_dev *pdev) 16914 { 16915 struct net_device *dev = pci_get_drvdata(pdev); 16916 struct bnxt *bp; 16917 16918 if (!dev) 16919 return; 16920 16921 rtnl_lock(); 16922 netdev_lock(dev); 16923 bp = netdev_priv(dev); 16924 if (!bp) 16925 goto shutdown_exit; 16926 16927 if (netif_running(dev)) 16928 netif_close(dev); 16929 16930 if (bnxt_hwrm_func_drv_unrgtr(bp)) { 16931 pcie_flr(pdev); 16932 goto shutdown_exit; 16933 } 16934 bnxt_ptp_clear(bp); 16935 bnxt_clear_int_mode(bp); 16936 pci_disable_device(pdev); 16937 16938 if (system_state == SYSTEM_POWER_OFF) { 16939 pci_wake_from_d3(pdev, bp->wol); 16940 pci_set_power_state(pdev, PCI_D3hot); 16941 } 16942 16943 shutdown_exit: 16944 netdev_unlock(dev); 16945 rtnl_unlock(); 16946 } 16947 16948 #ifdef CONFIG_PM_SLEEP 16949 static int bnxt_suspend(struct device *device) 16950 { 16951 struct net_device *dev = dev_get_drvdata(device); 16952 struct bnxt *bp = netdev_priv(dev); 16953 int rc = 0; 16954 16955 bnxt_ulp_stop(bp); 16956 16957 netdev_lock(dev); 16958 if (netif_running(dev)) { 16959 netif_device_detach(dev); 16960 rc = bnxt_close(dev); 16961 } 16962 bnxt_hwrm_func_drv_unrgtr(bp); 16963 bnxt_ptp_clear(bp); 16964 pci_disable_device(bp->pdev); 16965 bnxt_free_ctx_mem(bp, false); 16966 netdev_unlock(dev); 16967 return rc; 16968 } 16969 16970 static int bnxt_resume(struct device *device) 16971 { 16972 struct net_device *dev = dev_get_drvdata(device); 16973 struct bnxt *bp = netdev_priv(dev); 16974 int rc = 0; 16975 16976 netdev_lock(dev); 16977 rc = pci_enable_device(bp->pdev); 16978 if (rc) { 16979 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16980 rc); 16981 goto resume_exit; 16982 } 16983 pci_set_master(bp->pdev); 16984 if (bnxt_hwrm_ver_get(bp)) { 16985 rc = -ENODEV; 16986 goto resume_exit; 16987 } 16988 rc = bnxt_hwrm_func_reset(bp); 16989 if (rc) { 16990 rc = -EBUSY; 16991 goto resume_exit; 16992 } 16993 16994 rc = bnxt_hwrm_func_qcaps(bp); 16995 if (rc) 16996 goto resume_exit; 16997 16998 bnxt_clear_reservations(bp, true); 16999 17000 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 17001 rc = -ENODEV; 17002 goto resume_exit; 17003 } 17004 if (bp->fw_crash_mem) 17005 bnxt_hwrm_crash_dump_mem_cfg(bp); 17006 17007 if (bnxt_ptp_init(bp)) { 17008 kfree(bp->ptp_cfg); 17009 bp->ptp_cfg = NULL; 17010 } 17011 bnxt_get_wol_settings(bp); 17012 if (netif_running(dev)) { 17013 rc = bnxt_open(dev); 17014 if (!rc) 17015 netif_device_attach(dev); 17016 } 17017 17018 resume_exit: 17019 netdev_unlock(bp->dev); 17020 bnxt_ulp_start(bp, rc); 17021 if (!rc) 17022 bnxt_reenable_sriov(bp); 17023 return rc; 17024 } 17025 17026 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 17027 #define BNXT_PM_OPS (&bnxt_pm_ops) 17028 17029 #else 17030 17031 #define BNXT_PM_OPS NULL 17032 17033 #endif /* CONFIG_PM_SLEEP */ 17034 17035 /** 17036 * bnxt_io_error_detected - called when PCI error is detected 17037 * @pdev: Pointer to PCI device 17038 * @state: The current pci connection state 17039 * 17040 * This function is called after a PCI bus error affecting 17041 * this device has been detected. 17042 */ 17043 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 17044 pci_channel_state_t state) 17045 { 17046 struct net_device *netdev = pci_get_drvdata(pdev); 17047 struct bnxt *bp = netdev_priv(netdev); 17048 bool abort = false; 17049 17050 netdev_info(netdev, "PCI I/O error detected\n"); 17051 17052 bnxt_ulp_stop(bp); 17053 17054 netdev_lock(netdev); 17055 netif_device_detach(netdev); 17056 17057 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 17058 netdev_err(bp->dev, "Firmware reset already in progress\n"); 17059 abort = true; 17060 } 17061 17062 if (abort || state == pci_channel_io_perm_failure) { 17063 netdev_unlock(netdev); 17064 return PCI_ERS_RESULT_DISCONNECT; 17065 } 17066 17067 /* Link is not reliable anymore if state is pci_channel_io_frozen 17068 * so we disable bus master to prevent any potential bad DMAs before 17069 * freeing kernel memory. 17070 */ 17071 if (state == pci_channel_io_frozen) { 17072 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 17073 bnxt_fw_fatal_close(bp); 17074 } 17075 17076 if (netif_running(netdev)) 17077 __bnxt_close_nic(bp, true, true); 17078 17079 if (pci_is_enabled(pdev)) 17080 pci_disable_device(pdev); 17081 bnxt_free_ctx_mem(bp, false); 17082 netdev_unlock(netdev); 17083 17084 /* Request a slot reset. */ 17085 return PCI_ERS_RESULT_NEED_RESET; 17086 } 17087 17088 /** 17089 * bnxt_io_slot_reset - called after the pci bus has been reset. 17090 * @pdev: Pointer to PCI device 17091 * 17092 * Restart the card from scratch, as if from a cold-boot. 17093 * At this point, the card has experienced a hard reset, 17094 * followed by fixups by BIOS, and has its config space 17095 * set up identically to what it was at cold boot. 17096 */ 17097 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 17098 { 17099 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 17100 struct net_device *netdev = pci_get_drvdata(pdev); 17101 struct bnxt *bp = netdev_priv(netdev); 17102 int retry = 0; 17103 int err = 0; 17104 int off; 17105 17106 netdev_info(bp->dev, "PCI Slot Reset\n"); 17107 17108 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 17109 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 17110 msleep(900); 17111 17112 netdev_lock(netdev); 17113 17114 if (pci_enable_device(pdev)) { 17115 dev_err(&pdev->dev, 17116 "Cannot re-enable PCI device after reset.\n"); 17117 } else { 17118 pci_set_master(pdev); 17119 /* Upon fatal error, our device internal logic that latches to 17120 * BAR value is getting reset and will restore only upon 17121 * rewriting the BARs. 17122 * 17123 * As pci_restore_state() does not re-write the BARs if the 17124 * value is same as saved value earlier, driver needs to 17125 * write the BARs to 0 to force restore, in case of fatal error. 17126 */ 17127 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17128 &bp->state)) { 17129 for (off = PCI_BASE_ADDRESS_0; 17130 off <= PCI_BASE_ADDRESS_5; off += 4) 17131 pci_write_config_dword(bp->pdev, off, 0); 17132 } 17133 pci_restore_state(pdev); 17134 pci_save_state(pdev); 17135 17136 bnxt_inv_fw_health_reg(bp); 17137 bnxt_try_map_fw_health_reg(bp); 17138 17139 /* In some PCIe AER scenarios, firmware may take up to 17140 * 10 seconds to become ready in the worst case. 17141 */ 17142 do { 17143 err = bnxt_try_recover_fw(bp); 17144 if (!err) 17145 break; 17146 retry++; 17147 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17148 17149 if (err) { 17150 dev_err(&pdev->dev, "Firmware not ready\n"); 17151 goto reset_exit; 17152 } 17153 17154 err = bnxt_hwrm_func_reset(bp); 17155 if (!err) 17156 result = PCI_ERS_RESULT_RECOVERED; 17157 17158 /* IRQ will be initialized later in bnxt_io_resume */ 17159 bnxt_ulp_irq_stop(bp); 17160 bnxt_clear_int_mode(bp); 17161 } 17162 17163 reset_exit: 17164 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17165 bnxt_clear_reservations(bp, true); 17166 netdev_unlock(netdev); 17167 17168 return result; 17169 } 17170 17171 /** 17172 * bnxt_io_resume - called when traffic can start flowing again. 17173 * @pdev: Pointer to PCI device 17174 * 17175 * This callback is called when the error recovery driver tells 17176 * us that its OK to resume normal operation. 17177 */ 17178 static void bnxt_io_resume(struct pci_dev *pdev) 17179 { 17180 struct net_device *netdev = pci_get_drvdata(pdev); 17181 struct bnxt *bp = netdev_priv(netdev); 17182 int err; 17183 17184 netdev_info(bp->dev, "PCI Slot Resume\n"); 17185 netdev_lock(netdev); 17186 17187 err = bnxt_hwrm_func_qcaps(bp); 17188 if (!err) { 17189 if (netif_running(netdev)) { 17190 err = bnxt_open(netdev); 17191 } else { 17192 err = bnxt_reserve_rings(bp, true); 17193 if (!err) 17194 err = bnxt_init_int_mode(bp); 17195 } 17196 } 17197 17198 if (!err) 17199 netif_device_attach(netdev); 17200 17201 netdev_unlock(netdev); 17202 bnxt_ulp_start(bp, err); 17203 if (!err) 17204 bnxt_reenable_sriov(bp); 17205 } 17206 17207 static const struct pci_error_handlers bnxt_err_handler = { 17208 .error_detected = bnxt_io_error_detected, 17209 .slot_reset = bnxt_io_slot_reset, 17210 .resume = bnxt_io_resume 17211 }; 17212 17213 static struct pci_driver bnxt_pci_driver = { 17214 .name = DRV_MODULE_NAME, 17215 .id_table = bnxt_pci_tbl, 17216 .probe = bnxt_init_one, 17217 .remove = bnxt_remove_one, 17218 .shutdown = bnxt_shutdown, 17219 .driver.pm = BNXT_PM_OPS, 17220 .err_handler = &bnxt_err_handler, 17221 #if defined(CONFIG_BNXT_SRIOV) 17222 .sriov_configure = bnxt_sriov_configure, 17223 #endif 17224 }; 17225 17226 static int __init bnxt_init(void) 17227 { 17228 int err; 17229 17230 bnxt_debug_init(); 17231 err = pci_register_driver(&bnxt_pci_driver); 17232 if (err) { 17233 bnxt_debug_exit(); 17234 return err; 17235 } 17236 17237 return 0; 17238 } 17239 17240 static void __exit bnxt_exit(void) 17241 { 17242 pci_unregister_driver(&bnxt_pci_driver); 17243 if (bnxt_pf_wq) 17244 destroy_workqueue(bnxt_pf_wq); 17245 bnxt_debug_exit(); 17246 } 17247 17248 module_init(bnxt_init); 17249 module_exit(bnxt_exit); 17250